MySQL Authentication with wildcards in hosts

MySQL Authentication:

Added support for wildcards in hosts:

a.b.c.%
a.b.%.%
a.%.%.%
This commit is contained in:
MassimilianoPinto
2014-10-03 17:26:41 +02:00
parent 48489234be
commit dbe078f358
6 changed files with 260 additions and 153 deletions

View File

@ -24,6 +24,7 @@
* Date Who Description
* 14/02/2014 Massimiliano Pinto Initial implementation
* 17/02/2014 Massimiliano Pinto Added check ipv4
* 03/10/2014 Massimiliano Pinto Added check for wildcard hosts
*
* @endverbatim
*/
@ -153,6 +154,62 @@ int set_and_get_single_mysql_users(char *username, char *hostname, char *passwor
return 0;
}
int set_and_get_mysql_users_wildcards(char *username, char *hostname, char *password, char *from) {
USERS *mysql_users;
int ret;
struct sockaddr_in client_addr;
DCB *dcb;
SERVICE *service;
dcb = dcb_alloc(DCB_ROLE_INTERNAL);
if (dcb == NULL) {
fprintf(stderr, "dcb_alloc() failed\n");
return 1;
}
if ((service = (SERVICE *)calloc(1, sizeof(SERVICE))) == NULL) {
fprintf(stderr, "service_alloc() failed\n");
return 1;
}
memset(&client_addr, 0, sizeof(client_addr));
if (hostname) {
if(!setipaddress(&client_addr.sin_addr, from)) {
fprintf(stderr, "setipaddress failed for host [%s]\n", from);
return 1;
}
}
/* client IPv4 in raw data*/
memcpy(&dcb->ipv4, (struct sockaddr_in *)&client_addr, sizeof(struct sockaddr_in));
dcb->service = service;
mysql_users = mysql_users_alloc();
service->users = mysql_users;
// the routine returns 1 on success
ret = add_mysql_users_with_host_ipv4(mysql_users, username, hostname, password);
if (!ret) {
fprintf(stderr, "add_mysql_users_with_host_ipv4 passed(%s@%s, %s) FAILED\n", username, hostname, password);
return 1;
} else {
char db_passwd[100]="";
dcb->remote=strdup(from);
//fprintf(stderr, "add_mysql_users_with_host_ipv4 passed(%s@%s, %s) OK\n", username, hostname, password);
fprintf(stderr, "Checking '%s' @ '%s' against (%s@%s)\n", username, from, username, hostname);
// returns 0 on success
ret = gw_find_mysql_user_password_sha1(username, db_passwd, dcb);
}
return ret;
}
int main() {
int ret;
@ -166,6 +223,7 @@ int main() {
fprintf(stderr, "%s\n", asctime(localtime(&t)));
fprintf(stderr, ">>> Started MySQL load, set & get users@host\n");
ret = set_and_get_single_mysql_users("pippo", "localhost", "xyz");
assert(ret == 0);
ret = set_and_get_single_mysql_users("pippo", "127.0.0.2", "xyz");
@ -193,15 +251,72 @@ int main() {
char user[129] = "";
snprintf(user, 128, "user_%i", k);
ret = set_and_get_single_mysql_users_ipv4(user, i, "JJcd");
assert(ret == 0);
k++;
}
ret = set_and_get_mysql_users_wildcards("pippo", "%", "one", "127.0.0.1");
if (ret) fprintf(stderr, "\t-- Expecting no match\n");
assert(ret == 1);
ret = set_and_get_mysql_users_wildcards("pippo", "%", "", "127.0.0.1");
if (ret) fprintf(stderr, "\t-- Expecting no match\n");
assert(ret == 1);
ret = set_and_get_mysql_users_wildcards("pippo", "%", "two", "192.168.2.2");
if (!ret) fprintf(stderr, "\t-- Expecting ok\n");
assert(ret == 0);
ret = set_and_get_mysql_users_wildcards("pippo", "192.168.1.%", "foo", "192.168.2.2");
if (ret) fprintf(stderr, "\t-- Expecting no match\n");
assert(ret == 1);
ret = set_and_get_mysql_users_wildcards("pippo", "192.168.%.%", "foo", "192.168.2.2");
if (!ret) fprintf(stderr, "\t-- Expecting ok\n");
assert(ret == 0);
ret = set_and_get_mysql_users_wildcards("pippo", "192.%.%.%", "foo", "192.68.0.2");
if (!ret) fprintf(stderr, "\t-- Expecting ok\n");
assert(ret == 0);
ret = set_and_get_mysql_users_wildcards("pippo", "192.%.%.%", "foo", "192.0.0.2");
if (!ret) fprintf(stderr, "\t-- Expecting ok\n");
assert(ret == 0);
ret = set_and_get_mysql_users_wildcards("pippo", "192.0.%.%", "foo", "192.2.0.2");
if (ret) fprintf(stderr, "\t-- Expecting no match\n");
assert(ret == 1);
ret = set_and_get_mysql_users_wildcards("pippo", "192.0.0.1", "foo", "192.0.0.2");
if (ret) fprintf(stderr, "\t-- Expecting no match\n");
assert(ret == 1);
ret = set_and_get_mysql_users_wildcards("pippo", "192.0.%.%", "foo", "192.1.0.2");
if (ret) fprintf(stderr, "\t-- Expecting no match\n");
assert(ret == 1);
ret = set_and_get_mysql_users_wildcards("pippo", "192.0.0.%", "foo", "192.3.2.1");
if (ret) fprintf(stderr, "\t-- Expecting no match\n");
assert(ret == 1);
ret = set_and_get_mysql_users_wildcards("pippo", "192.0.%.%", "foo", "192.3.2.1");
if (ret) fprintf(stderr, "\t-- Expecting no match\n");
assert(ret == 1);
ret = set_and_get_mysql_users_wildcards("pippo", "192.%.%.%", "foo", "192.3.2.1");
if (!ret) fprintf(stderr, "\t-- Expecting ok\n");
assert(ret == 0);
ret = set_and_get_mysql_users_wildcards("pippo", "192.0.0.%", "foo", "192.134.0.2");
if (ret) fprintf(stderr, "\t-- Expecting no match\n");
assert(ret == 1);
fprintf(stderr, "----------------\n");
fprintf(stderr, "<<< Test completed\n");
time(&t);
fprintf(stderr, "%s\n", asctime(localtime(&t)));
return ret;
return 0;
}