Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9631dc1

Browse files
committed
Added PostgreSQL UDF to execute commands on the underlying system:
* sys_eval() to return the standard output * sys_exec() to return the exit status Inspired by lib_mysqludf_sys 0.0.3 (https://svn.sqlmap.org/sqlmap/trunk/sqlmap/extra/mysqludfsys/)
1 parent ae0f198 commit 9631dc1

6 files changed

Lines changed: 170 additions & 0 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
LIBDIR=/usr/lib
2+
3+
install:
4+
gcc -Wall -I/usr/include/postgresql/8.3/server -I. -shared lib_postgresqludf_sys.c -o $(LIBDIR)/lib_postgresqludf_sys.so
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/bash
2+
# lib_postgresqludf_sys - a library with miscellaneous (operating) system level functions
3+
# Copyright (C) 2009 Bernardo Damele A. G.
4+
# web: http://bernardodamele.blogspot.com/
5+
6+
#
7+
# This library is free software; you can redistribute it and/or
8+
# modify it under the terms of the GNU Lesser General Public
9+
# License as published by the Free Software Foundation; either
10+
# version 2.1 of the License, or (at your option) any later version.
11+
#
12+
# This library is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
# Lesser General Public License for more details.
16+
#
17+
# You should have received a copy of the GNU Lesser General Public
18+
# License along with this library; if not, write to the Free Software
19+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
21+
echo "Compiling the PostgreSQL UDF"
22+
make
23+
24+
if test $? -ne 0; then
25+
echo "ERROR: You need postgresql-server development software installed "
26+
echo "to be able to compile this UDF, on Debian/Ubuntu just run:"
27+
echo "apt-get install postgresql-server-dev-8.3"
28+
exit 1
29+
else
30+
echo "PostgreSQL UDF compiled successfully"
31+
fi
32+
33+
echo -e "\nPlease provide your PostgreSQL 'postgres' user's password"
34+
35+
/usr/lib/postgresql/8.3/bin/psql -h 127.0.0.1 -p 5432 -U postgres -q template1 < lib_postgresqludf_sys.sql
36+
#psql -h 127.0.0.1 -p 5432 -U postgres -q template1 < lib_postgresqludf_sys.sql
37+
38+
if test $? -ne 0; then
39+
echo "ERROR: unable to install the UDF"
40+
exit 1
41+
else
42+
echo "PostgreSQL UDF installed successfully"
43+
fi
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
lib_postgresqludf_sys - a library with miscellaneous (operating) system level functions
3+
Copyright (C) 2009 Bernardo Damele A. G.
4+
web: http://bernardodamele.blogspot.com/
5+
6+
7+
This library is free software; you can redistribute it and/or
8+
modify it under the terms of the GNU Lesser General Public
9+
License as published by the Free Software Foundation; either
10+
version 2.1 of the License, or (at your option) any later version.
11+
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General Public
18+
License along with this library; if not, write to the Free Software
19+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*/
21+
22+
#include <stdlib.h>
23+
#include <postgres.h>
24+
#include <fmgr.h>
25+
26+
#ifdef PG_MODULE_MAGIC
27+
PG_MODULE_MAGIC;
28+
#endif
29+
30+
PG_FUNCTION_INFO_V1(sys_exec);
31+
Datum sys_exec(PG_FUNCTION_ARGS) {
32+
text *argv0 = PG_GETARG_TEXT_P(0);
33+
int32 argv0_size;
34+
int32 result = 0;
35+
char *command;
36+
37+
argv0_size = VARSIZE(argv0) - VARHDRSZ;
38+
command = (char *)palloc(argv0_size + 1);
39+
40+
memcpy(command, VARDATA(argv0), argv0_size);
41+
command[argv0_size] = '\0';
42+
43+
/*
44+
Only if you want to log
45+
elog(NOTICE, "Command execution: %s", command);
46+
*/
47+
48+
result = system(command);
49+
pfree(command);
50+
51+
PG_FREE_IF_COPY(argv0, 0);
52+
PG_RETURN_INT32(result);
53+
}
54+
55+
PG_FUNCTION_INFO_V1(sys_eval);
56+
Datum sys_eval(PG_FUNCTION_ARGS) {
57+
text *argv0 = PG_GETARG_TEXT_P(0);
58+
text *result_text;
59+
int32 argv0_size;
60+
char *command;
61+
char *result;
62+
FILE *pipe;
63+
char line[1024];
64+
int32 outlen, linelen;
65+
66+
argv0_size = VARSIZE(argv0) - VARHDRSZ;
67+
command = (char *)palloc(argv0_size + 1);
68+
69+
memcpy(command, VARDATA(argv0), argv0_size);
70+
command[argv0_size] = '\0';
71+
72+
/*
73+
Only if you want to log
74+
elog(NOTICE, "Command evaluated: %s", command);
75+
*/
76+
77+
result = malloc(1);
78+
outlen = 0;
79+
80+
pipe = popen(command, "r");
81+
82+
while (fgets(line, sizeof(line), pipe) != NULL) {
83+
linelen = strlen(line);
84+
result = realloc(result, outlen + linelen);
85+
strncpy(result + outlen, line, linelen);
86+
outlen = outlen + linelen;
87+
}
88+
89+
pclose(pipe);
90+
91+
if (*result) {
92+
result[outlen] = 0x00;
93+
}
94+
95+
result_text = (text *)palloc(VARHDRSZ + strlen(result));
96+
SET_VARSIZE(result_text, VARHDRSZ + strlen(result));
97+
memcpy(VARDATA(result_text), result, strlen(result));
98+
99+
PG_RETURN_POINTER(result_text);
100+
}
Binary file not shown.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
lib_postgresqludf_sys - a library with miscellaneous (operating) system level functions
3+
Copyright (C) 2009 Bernardo Damele A. G.
4+
web: http://bernardodamele.blogspot.com/
5+
6+
7+
This library is free software; you can redistribute it and/or
8+
modify it under the terms of the GNU Lesser General Public
9+
License as published by the Free Software Foundation; either
10+
version 2.1 of the License, or (at your option) any later version.
11+
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General Public
18+
License along with this library; if not, write to the Free Software
19+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*/
21+
22+
CREATE OR REPLACE FUNCTION sys_exec(text) RETURNS int4 AS '/usr/lib/lib_postgresqludf_sys.so', 'sys_exec' LANGUAGE C RETURNS NULL ON NULL INPUT IMMUTABLE;
23+
CREATE OR REPLACE FUNCTION sys_eval(text) RETURNS text AS '/usr/lib/lib_postgresqludf_sys.so', 'sys_eval' LANGUAGE C RETURNS NULL ON NULL INPUT IMMUTABLE;
5.57 KB
Binary file not shown.

0 commit comments

Comments
 (0)