diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5952efe --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +.anjuta/ +.anjuta_sym_db.db +aclocal.m4 +build/ +configure +debian/build/ +vn-mysql.anjuta diff --git a/configure.ac b/configure.ac index 1ec24d6..9f3d43e 100644 --- a/configure.ac +++ b/configure.ac @@ -17,5 +17,6 @@ AC_CONFIG_FILES([ Makefile src/Makefile src/proxy-auth/Makefile + src/functions/Makefile ]) AC_OUTPUT diff --git a/debian/control b/debian/control index 11ade09..c01a205 100644 --- a/debian/control +++ b/debian/control @@ -2,7 +2,7 @@ Source: vn-mysql Section: misc Priority: extra Maintainer: Alejandro T. Colombini Gómez -Build-Depends: debhelper (>= 8.0.0), autotools-dev, libglib2.0-dev, libmysqld-dev +Build-Depends: build-essential, dh-autoreconf, devscripts, libglib2.0-dev, libmysqld-dev Standards-Version: 3.9.4 Homepage: http://www.verdnatura.es diff --git a/debian/postinst b/debian/postinst index e3e3361..ff5d43c 100644 --- a/debian/postinst +++ b/debian/postinst @@ -6,5 +6,5 @@ chmod u=rw,go-rwx /etc/mysql/proxy-auth.sql chown mysql:mysql /etc/mysql/proxy-auth.ini chmod u=rw,go-rwx /etc/mysql/proxy-auth.ini -service mysql restart +#service mysql restart exit 0 diff --git a/debian/postrm b/debian/postrm index d747df4..c28a9b5 100644 --- a/debian/postrm +++ b/debian/postrm @@ -1,4 +1,4 @@ #! /bin/sh -service mysql restart +#service mysql restart exit 0 diff --git a/proxy-test/command.sh b/proxy-test/command.sh new file mode 100755 index 0000000..e837846 --- /dev/null +++ b/proxy-test/command.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +mysql --enable-cleartext-plugin -u test-user --password=1234 < query.sql + diff --git a/proxy-test/connect.sh b/proxy-test/connect.sh new file mode 100755 index 0000000..29ee443 --- /dev/null +++ b/proxy-test/connect.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +I=0 +NATTEMPS=$1 + +while [ $I -lt $NATTEMPS ]; +do + I=$((I+1)) + ./command.sh +done diff --git a/proxy-test/proxy-test.sh b/proxy-test/proxy-test.sh new file mode 100755 index 0000000..8ef39cd --- /dev/null +++ b/proxy-test/proxy-test.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +CONCURRENCY=400 +NATTEMPS=40 + +I=0 + +while [ $I -lt $CONCURRENCY ]; +do + I=$((I+1)) + ./connect.sh $NATTEMPS & +done diff --git a/proxy-test/query.sql b/proxy-test/query.sql new file mode 100644 index 0000000..52c0c7b --- /dev/null +++ b/proxy-test/query.sql @@ -0,0 +1 @@ +SELECT USER() user, CURRENT_USER() proxy diff --git a/proxy-test/test.sql b/proxy-test/test.sql new file mode 100644 index 0000000..ce6a110 --- /dev/null +++ b/proxy-test/test.sql @@ -0,0 +1,13 @@ + +CREATE SCHEMA account; + +CREATE TABLE account.user +SELECT 'proxy' mysql_user, 'test-user' user, '1234' password; +-- SELECT mysql_user FROM user WHERE user = #user AND password = #pass + +CREATE USER ''@'%' IDENTIFIED WITH proxy_auth; +GRANT USAGE ON *.* TO ''@'%'; + +CREATE USER 'proxy'@'%' IDENTIFIED BY '123456'; +GRANT USAGE ON *.* TO 'proxy'@'%'; +GRANT PROXY ON 'proxy'@'%' TO ''@'%'; diff --git a/sql/install.sql b/sql/install.sql new file mode 100644 index 0000000..e6603f2 --- /dev/null +++ b/sql/install.sql @@ -0,0 +1,7 @@ + +INSTALL PLUGIN proxy_auth SONAME 'proxy_auth.so'; + +CREATE AGGREGATE FUNCTION minacum RETURNS INT SONAME 'minacum.so'; +CREATE AGGREGATE FUNCTION multimax RETURNS INT SONAME 'multimax.so'; +CREATE FUNCTION sql_printf RETURNS STRING SONAME 'sql_printf.so'; + diff --git a/sql/uninstall.sql b/sql/uninstall.sql new file mode 100644 index 0000000..3416aab --- /dev/null +++ b/sql/uninstall.sql @@ -0,0 +1,7 @@ + +UNINSTALL PLUGIN proxy_auth; + +DROP FUNCTION IF EXISTS minacum; +DROP FUNCTION IF EXISTS multimax; +DROP FUNCTION IF EXISTS sql_printf; + diff --git a/src/Makefile.am b/src/Makefile.am index 99c314a..dc5ef9e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,3 +1,4 @@ SUBDIRS = \ - proxy-auth + proxy-auth \ + functions diff --git a/src/functions/Makefile.am b/src/functions/Makefile.am new file mode 100644 index 0000000..bbb2e9b --- /dev/null +++ b/src/functions/Makefile.am @@ -0,0 +1,55 @@ + +plugindir = $(libdir)/mysql/plugin + +# minacum + +minacum_LTLIBRARIES = minacum.la +minacum_la_SOURCES = minacum.c +minacum_la_LIBADD = -lmysqlclient -lz +minacum_la_CFLAGS = -Wall -O3 -I/usr/include/mysql +minacum_la_LDFLAGS = \ + -no-undefined \ + -module \ + -avoid-version \ + -export-dynamic \ + -lmysqlclient +minacumdir = $(plugindir) + +# sql_printf + +sql_printf_LTLIBRARIES = sql_printf.la +sql_printf_la_SOURCES = sql_printf.c +sql_printf_la_LIBADD = -lmysqlclient -lz +sql_printf_la_CFLAGS = -Wall -O3 -I/usr/include/mysql +sql_printf_la_LDFLAGS = \ + -no-undefined \ + -module \ + -avoid-version \ + -export-dynamic \ + -lmysqlclient +sql_printfdir = $(plugindir) + +# multimax + +multimax_LTLIBRARIES = multimax.la +multimax_la_SOURCES = multimax.c +multimax_la_LIBADD = -lmysqlclient -lz +multimax_la_CFLAGS = -Wall -O3 -I/usr/include/mysql +multimax_la_LDFLAGS = \ + -no-undefined \ + -module \ + -avoid-version \ + -export-dynamic \ + -lmysqlclient +multimaxdir = $(plugindir) + +# Clean + +install-data-hook: + rm -f $(DESTDIR)$(plugindir)/minacum.la + rm -f $(DESTDIR)$(plugindir)/minacum.a + rm -f $(DESTDIR)$(plugindir)/sql_printf.la + rm -f $(DESTDIR)$(plugindir)/sql_printf.a + rm -f $(DESTDIR)$(plugindir)/multimax.la + rm -f $(DESTDIR)$(plugindir)/multimax.a + diff --git a/src/sql_printf/makefile b/src/functions/makefile.old similarity index 100% rename from src/sql_printf/makefile rename to src/functions/makefile.old diff --git a/src/minacum/minacum.c b/src/functions/minacum.c similarity index 97% rename from src/minacum/minacum.c rename to src/functions/minacum.c index 12731ea..108228a 100644 --- a/src/minacum/minacum.c +++ b/src/functions/minacum.c @@ -41,7 +41,7 @@ void minacum_add (UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error) } if (ival == NULL || ival->num > num) { Interval* new; - if (new = (Interval*) malloc (sizeof (Interval))) { + if ((new = (Interval*) malloc (sizeof (Interval)))) { new->num = num; new->sum = amount; new->next = ival; diff --git a/src/multimax/multimax.c b/src/functions/multimax.c similarity index 100% rename from src/multimax/multimax.c rename to src/functions/multimax.c diff --git a/src/sql_printf/sql_printf.c b/src/functions/sql_printf.c similarity index 100% rename from src/sql_printf/sql_printf.c rename to src/functions/sql_printf.c diff --git a/src/minacum/makefile b/src/minacum/makefile deleted file mode 100644 index b113bdb..0000000 --- a/src/minacum/makefile +++ /dev/null @@ -1,14 +0,0 @@ -FILE=minacum.so -SRC=minacum.c -#URL=/usr/lib/$(FILE) -URL=/usr/lib/mysql/plugin/$(FILE) - -main: $(SRC) - rm -f $(FILE) - gcc -shared -o $(FILE) $(SRC) -fPIC -install: - cp $(FILE) $(URL) -uninstall: - rm -f $(URL) -clean: - rm -f $(FILE) diff --git a/src/multimax/makefile b/src/multimax/makefile deleted file mode 100644 index 15d8d07..0000000 --- a/src/multimax/makefile +++ /dev/null @@ -1,25 +0,0 @@ -NAME=multimax -FILE=$(NAME).so -SRC=$(NAME).c -URL=/usr/lib/mysql/plugin/$(FILE) - -SQL_DROP="DROP FUNCTION IF EXISTS $(NAME);" - -.PHONY: install uninstall clean - -$(FILE): $(SRC) - rm -f $@ - gcc -shared -o $@ $< -fPIC - -install: - cp $(FILE) $(URL) - echo $(SQL_DROP) - echo "CREATE AGGREGATE FUNCTION $(NAME) RETURNS INT SONAME '$(FILE)';" - -uninstall: - rm -f $(URL) - echo $(SQL_DROP) - -clean: - rm -f $(FILE) - diff --git a/src/proxy-auth/proxy-auth.c b/src/proxy-auth/proxy-auth.c index 534fa61..0b7f25f 100644 --- a/src/proxy-auth/proxy-auth.c +++ b/src/proxy-auth/proxy-auth.c @@ -22,8 +22,8 @@ #include #include -#define CONFIG_FILE _CONFIG_DIR"/proxy_auth.ini" -#define SQL_FILE _SQL_DIR"/proxy_auth.sql" +#define CONFIG_FILE _CONFIG_DIR"/proxy-auth.ini" +#define SQL_FILE _SQL_DIR"/proxy-auth.sql" typedef struct { @@ -79,13 +79,19 @@ proxy_auth_deinit (ProxyAuth * self) if (self->regex) g_regex_unref (self->regex); - if (self->conn_pool) - g_async_queue_unref (self->conn_pool); + + // FIXME: Connection can't be closed because mysql_close() causes MySQL + // process crash with signal 11 when the process is shutdown. + // MySQL version 5.5.40 + //if (self->conn_pool) + // g_async_queue_unref (self->conn_pool); } void proxy_auth_free (ProxyAuth * self) { + g_return_if_fail (self != NULL); + proxy_auth_deinit (self); g_mutex_clear (&self->mutex); g_free (self); @@ -94,6 +100,8 @@ proxy_auth_free (ProxyAuth * self) gboolean proxy_auth_init (ProxyAuth * self) { + g_return_val_if_fail (self != NULL, FALSE); + gboolean res = FALSE; GError * error = NULL; @@ -191,13 +199,16 @@ proxy_auth_regex_func (const GMatchInfo * info, GString * res, gpointer data) if (str) { + unsigned long scaped_len; + char escaped_str[str_len * 2 + 1]; + g_string_append_c (res, '\''); // FIXME: mysql_real_escape_string() causes MySQL process crash with signal 11 - char escaped_str[str_len * 2 + 1]; - unsigned long scaped_len = mysql_escape_string (escaped_str, str, str_len); + //scaped_len = mysql_real_escape_string (regex_data->conn, escaped_str, str, str_len); + scaped_len = mysql_escape_string (escaped_str, str, str_len); + g_string_append_len (res, escaped_str, (gssize) scaped_len); - g_string_append_c (res, '\''); } else @@ -210,6 +221,8 @@ proxy_auth_regex_func (const GMatchInfo * info, GString * res, gpointer data) static gboolean proxy_auth_reconnect (ProxyAuth * self, MYSQL * conn) { + g_return_val_if_fail (self != NULL, FALSE); + gboolean connected = mysql_real_connect (conn, self->host, self->user, self->pass, self->schema, self->port, self->socket, 0) != NULL; guint conn_error = mysql_errno (conn); @@ -226,6 +239,8 @@ proxy_auth_reconnect (ProxyAuth * self, MYSQL * conn) int proxy_auth_authenticate (ProxyAuth * self, MYSQL_PLUGIN_VIO * vio, MYSQL_SERVER_AUTH_INFO * info) { + g_return_val_if_fail (self != NULL, CR_ERROR); + int res = CR_ERROR; gchar * query = NULL; GError * error = NULL; @@ -354,12 +369,12 @@ end: return res; } -ProxyAuth * pauth; +ProxyAuth * pauth = NULL; static int proxy_auth_plugin_main (MYSQL_PLUGIN_VIO * vio, MYSQL_SERVER_AUTH_INFO * info) { - return proxy_auth_authenticate (pauth, vio, info);; + return proxy_auth_authenticate (pauth, vio, info); } static int diff --git a/src/proxy-auth/proxy-auth.ini b/src/proxy-auth/proxy-auth.ini index ad798ac..bd0cf4e 100644 --- a/src/proxy-auth/proxy-auth.ini +++ b/src/proxy-auth/proxy-auth.ini @@ -1,5 +1,5 @@ [db] -user = auth-user +user = authentication pass = password socket = /var/run/mysqld/mysqld.sock schema = account