Command execution with MySQL UDF
Oussama Hammami, 2011-01-11
Switzernet
Modern database management systems are powerful applications: they provide several instruments to interact with the underlying operating system.
On MySQL it is possible to create a User-Defined Function to execute commands on the underlying operating system. Marco Ivaldi demonstrated that some years ago. His raptor_udf2.c works well, but it has two limitations:
These limitations make the UDF almost useless on recent MySQL server installations if the database administrator wants to get the exit status of the command as UDF output or the command standard output itself.
I recently came across an open repository of MySQL User-Defined Functions. One of their codes kept my attention: lib_mysqludf_sys (version 0.0.2) which implements three different functions to interact with the underlying environment::
The first function can be used to execute operating system commands and has two advantages over raptor’s UDF:
# aptitude install libmysqlclient-dev
# aptitude install gcc-4.2
# mkdir lib_mysqludf_sys; cd lib_mysqludf_sys
# wget http:/Switzernet.com/3/public/110111-mysql-udf-sys/data1/lib_mysqludf_sys_0.0.2.tar.gz
# tar xfz lib_mysqludf_sys_0.0.2.tar.gz
# ./install.sh
Compiling the MySQL UDF
gcc -Wall -I/usr/include/mysql -I. -shared lib_mysqludf_sys.c -o /usr/lib/lib_mysqludf_sys.so
MySQL UDF compiled successfully
Please provide your MySQL root password
Enter password:
MySQL UDF installed successfully
# mysql -u root -p mysql
Enter password:
mysql> SELECT sys_eval('id');
+-------------------------------------------------+
| sys_eval('id') |
+-------------------------------------------------+
| uid=105(mysql) gid=108(mysql) groups=108(mysql) |
+-------------------------------------------------+
1 row in set (0.01 sec)
mysql> SELECT sys_exec('touch /tmp/test_mysql_udf');
+---------------------------------------+
| sys_exec('touch /tmp/test_mysql_udf') |
+---------------------------------------+
| 0 |
+---------------------------------------+
1 row in set (0.01 sec)
mysql> exit
Bye
# ls -l /tmp/test_mysql_udf
-rw-rw---- 1 mysql mysql 0 2011-01-12 10:59 /tmp/test_mysql_udf
http://dev.mysql.com/doc/refman/5.0/en/faqs-triggers.html
http://forge.mysql.com/projects/project.php?id=211
http://www.mysqludf.org/lib_mysqludf_stat/
http://forums.mysql.com/read.php?118,215809,215809#msg-215809
https://svn.sqlmap.org/sqlmap/trunk/sqlmap/extra/udfhack/linux/
http://www.perlmonks.org/index.pl?node_id=873916
http://dev.mysql.com/doc/refman/5.5/en/adding-udf.html
http://blog.0x3f.net/udf/command-execution-with-a-mysql-udf/