From 0e2d11e487d4ae78e189f4c2e7fca73c36e0a714 Mon Sep 17 00:00:00 2001 From: Markus Makela Date: Mon, 6 Apr 2015 06:10:17 +0300 Subject: [PATCH 1/3] Fixed missing wildcard character from regex string. --- server/core/dbusers.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/server/core/dbusers.c b/server/core/dbusers.c index 2bb400489..5fb870228 100644 --- a/server/core/dbusers.c +++ b/server/core/dbusers.c @@ -2140,7 +2140,9 @@ int add_wildcard_users(USERS *users, char* name, char* host, char* password, cha while(ptr) { - memmove(ptr,ptr+1,(len - (ptr - restr)) + 1); + memmove(ptr+1,ptr,(len - (ptr - restr)) + 1); + *ptr++ = '.'; + *ptr = '*'; len = strlen(restr); ptr = strchr(restr,'%'); } From 7904b3dcb3e1784ef10eaf6a76cfd8002e66551b Mon Sep 17 00:00:00 2001 From: Markus Makela Date: Mon, 6 Apr 2015 09:35:29 +0300 Subject: [PATCH 2/3] Added doxygen comments to new functions and more logging output when resolving wildcard database names. --- server/core/dbusers.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/server/core/dbusers.c b/server/core/dbusers.c index 5fb870228..d4479f0e8 100644 --- a/server/core/dbusers.c +++ b/server/core/dbusers.c @@ -912,6 +912,7 @@ getAllUsers(SERVICE *service, USERS *users) if(havedb && wildcard_db_grant(dbnm)) { rc = add_wildcard_users(users, row[0], row[1], password, row[4], dbnm, service->resources); + skygw_log_write(LOGFILE_DEBUG,"%s: Converted '%s' to %d individual database grants.",service->name,dbnm,rc); } else { @@ -1395,6 +1396,7 @@ getUsers(SERVICE *service, USERS *users) if(wildcard_db_grant(row[5])) { rc = add_wildcard_users(users, row[0], row[1], password, row[4], row[5], service->resources); + skygw_log_write(LOGFILE_DEBUG,"%s: Converted '%s' to %d individual database grants.",service->name,row[5],rc); } else { @@ -2112,6 +2114,17 @@ int wildcard_db_grant(char* str) return 0; } +/** + * + * @param users Pointer to USERS struct + * @param name Username of the client + * @param host Host address of the client + * @param password Client password + * @param anydb If the user has access to all databases + * @param db Database, in wildcard form + * @param hash Hashtable with all database names + * @return number of unique grants generated from wildcard database name + */ int add_wildcard_users(USERS *users, char* name, char* host, char* password, char* anydb, char* db, HASHTABLE* hash) { HASHITERATOR* iter; @@ -2163,7 +2176,7 @@ int add_wildcard_users(USERS *users, char* name, char* host, char* password, cha { if(regexec(&re,value,0,NULL,0) == 0) { - rval = add_mysql_users_with_host_ipv4(users, name, host, password, anydb, value); + rval += add_mysql_users_with_host_ipv4(users, name, host, password, anydb, value); } } From 71c6ab1d66a687526bddb873ea0403345b511e6f Mon Sep 17 00:00:00 2001 From: Markus Makela Date: Tue, 7 Apr 2015 13:15:48 +0300 Subject: [PATCH 3/3] Backend databases are queried from all backends before querying users in getAllUsers. --- server/core/dbusers.c | 93 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 77 insertions(+), 16 deletions(-) diff --git a/server/core/dbusers.c b/server/core/dbusers.c index d4479f0e8..b6ea54a01 100644 --- a/server/core/dbusers.c +++ b/server/core/dbusers.c @@ -430,8 +430,10 @@ addDatabases(SERVICE *service, MYSQL *con) /* insert key and value "" */ while ((row = mysql_fetch_row(result))) { - skygw_log_write(LOGFILE_DEBUG,"%s: Adding database %s to the resouce hash.",service->name,row[0]); - resource_add(service->resources, row[0], ""); + if(resource_add(service->resources, row[0], "")) + { + skygw_log_write(LOGFILE_DEBUG,"%s: Adding database %s to the resouce hash.",service->name,row[0]); + } } mysql_free_result(result); @@ -608,6 +610,78 @@ getAllUsers(SERVICE *service, USERS *users) service->resources = resource_alloc(); + while(server != NULL) + { + + con = mysql_init(NULL); + + if (con == NULL) + { + LOGIF(LE, (skygw_log_write_flush( + LOGFILE_ERROR, + "Error : mysql_init: %s", + mysql_error(con)))); + goto cleanup; + } + + /** Set read, write and connect timeout values */ + if (gw_mysql_set_timeouts(con, + DEFAULT_READ_TIMEOUT, + DEFAULT_WRITE_TIMEOUT, + DEFAULT_CONNECT_TIMEOUT)) + { + LOGIF(LE, (skygw_log_write_flush( + LOGFILE_ERROR, + "Error : failed to set timeout values for backend " + "connection."))); + mysql_close(con); + goto cleanup; + } + + if (mysql_options(con, MYSQL_OPT_USE_REMOTE_CONNECTION, NULL)) + { + LOGIF(LE, (skygw_log_write_flush( + LOGFILE_ERROR, + "Error : failed to set external connection. " + "It is needed for backend server connections."))); + mysql_close(con); + goto cleanup; + } + + + while(!service->svc_do_shutdown && + server != NULL && + (mysql_real_connect(con, + server->server->name, + service_user, + dpwd, + NULL, + server->server->port, + NULL, + 0) == NULL)) + { + server = server->next; + } + + + if (server == NULL) + { + LOGIF(LE, (skygw_log_write_flush( + LOGFILE_ERROR, + "Error : Unable to get user data from backend database " + "for service [%s]. Missing server information.", + service->name))); + mysql_close(con); + goto cleanup; + } + + addDatabases(service, con); + mysql_close(con); + server = server->next; + } + + server = service->dbref; + while(server != NULL) { @@ -839,20 +913,7 @@ getAllUsers(SERVICE *service, USERS *users) goto cleanup; } - - if (db_grants) { - /* load all mysql database names */ - dbnames = addDatabases(service, con); - - LOGIF(LD, (skygw_log_write( - LOGFILE_DEBUG, - "Loaded %d MySQL Database Names for service [%s]", - dbnames, - service->name))); - } else { - service->resources = NULL; - } - + while ((row = mysql_fetch_row(result))) { /**