From 2a416a8cbab5140595f4d2de73d76cefdf3667de Mon Sep 17 00:00:00 2001 From: Markus Makela Date: Mon, 6 Apr 2015 05:46:42 +0300 Subject: [PATCH] Added resolving of wildcard database grants to multiple single database grants to mysql_client. --- server/core/dbusers.c | 110 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 109 insertions(+), 1 deletion(-) diff --git a/server/core/dbusers.c b/server/core/dbusers.c index a023f9821..2bb400489 100644 --- a/server/core/dbusers.c +++ b/server/core/dbusers.c @@ -50,6 +50,7 @@ #include #include #include +#include #define DEFAULT_CONNECT_TIMEOUT 3 @@ -110,6 +111,16 @@ void *resource_fetch(HASHTABLE *, char *); int resource_add(HASHTABLE *, char *, char *); int resource_hash(char *); static int normalize_hostname(char *input_host, char *output_host); +int wildcard_db_grant(char* str); + +int add_wildcard_users(USERS *users, + char* name, + char* host, + char* password, + char* anydb, + char* db, + HASHTABLE* hash); + static int gw_mysql_set_timeouts( MYSQL* handle, int read_timeout, @@ -897,7 +908,16 @@ getAllUsers(SERVICE *service, USERS *users) dbnm))); } } - rc = add_mysql_users_with_host_ipv4(users, row[0], row[1], password, row[4],havedb ? dbnm : NULL); + + if(havedb && wildcard_db_grant(dbnm)) + { + rc = add_wildcard_users(users, row[0], row[1], password, row[4], dbnm, service->resources); + } + else + { + rc = add_mysql_users_with_host_ipv4(users, row[0], row[1], password, row[4], havedb ? dbnm : NULL); + } + skygw_log_write(LOGFILE_DEBUG,"%s: Adding user:%s host:%s anydb:%s db:%s.", service->name,row[0],row[1],row[4], havedb ? dbnm : NULL); @@ -1371,7 +1391,16 @@ getUsers(SERVICE *service, USERS *users) if (db_grants) { /* we have dbgrants, store them */ + + if(wildcard_db_grant(row[5])) + { + rc = add_wildcard_users(users, row[0], row[1], password, row[4], row[5], service->resources); + } + else + { rc = add_mysql_users_with_host_ipv4(users, row[0], row[1], password, row[4], row[5]); + } + } else { /* we don't have dbgrants, simply set ANY DB for the user */ rc = add_mysql_users_with_host_ipv4(users, row[0], row[1], password, "Y", NULL); @@ -2063,3 +2092,82 @@ dbusers_load(USERS *users, char *filename) { return hashtable_load(users->data, filename, dbusers_keyread, dbusers_valueread); } + +/** + * Check if the database name contains a wildcard character + * @param str Database grant + * @return 1 if the name contains the '%' wildcard character, 0 if it does not + */ +int wildcard_db_grant(char* str) +{ + char* ptr = str; + + while(ptr && *ptr != '\0') + { + if(*ptr == '%') + return 1; + ptr++; + } + + return 0; +} + +int add_wildcard_users(USERS *users, char* name, char* host, char* password, char* anydb, char* db, HASHTABLE* hash) +{ + HASHITERATOR* iter; + HASHTABLE* ht = hash; + char *restr,*ptr,*value; + int len,err,rval = 0; + char errbuf[1024]; + regex_t re; + + if(db == NULL || hash == NULL) + return 0; + + if((restr = malloc(sizeof(char)*strlen(db)*2)) == NULL) + return 0; + + strcpy(restr,db); + + len = strlen(restr); + ptr = strchr(restr,'%'); + + if(ptr == NULL) + { + free(restr); + return 0; + } + + while(ptr) + { + memmove(ptr,ptr+1,(len - (ptr - restr)) + 1); + len = strlen(restr); + ptr = strchr(restr,'%'); + } + + if((err = regcomp(&re,restr,REG_ICASE|REG_NOSUB))) + { + regerror(err,&re,errbuf,1024); + skygw_log_write(LOGFILE_ERROR,"Error: Failed to compile regex " + "when resolving wildcard database grants: %s", + errbuf); + free(restr); + return 0; + } + + iter = hashtable_iterator(ht); + + while(iter && (value = hashtable_next(iter))) + { + if(regexec(&re,value,0,NULL,0) == 0) + { + rval = add_mysql_users_with_host_ipv4(users, name, host, password, anydb, value); + } + } + + hashtable_iterator_free(iter); + regfree(&re); + free(restr); + + return rval; +} \ No newline at end of file