Move dbusers.c out of the core
The dbusers.c was a MySQL protocol specific file which was used directly by some of the modules. Added a new return value for the loadusers authenticator entry point which allows fatal failures to occur when users are loaded. Currently this is only taken into notice when the service is first started. If a listener later returns a fatal error, it is only logged but the service stays in operation. Moved the MySQLAuth authenticator sources and the tests that relate to this module into a subdirectory in the authenticator directory. Eventually, all authenticators could have a subdirectory of their own.
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
add_library(maxscale-common SHARED adminusers.c alloc.c authenticator.c atomic.c buffer.c config.c dbusers.c dcb.c filter.c externcmd.c gwbitmask.c gwdirs.c hashtable.c hint.c housekeeper.c listmanager.c load_utils.c log_manager.cc maxscale_pcre2.c memlog.c misc.c mlist.c modutil.c monitor.c queuemanager.c query_classifier.c poll.c random_jkiss.c resultset.c secrets.c server.c service.c session.c spinlock.c thread.c users.c utils.c skygw_utils.cc statistics.c listener.c gw_ssl.c mysql_utils.c mysql_binlog.c)
|
||||
add_library(maxscale-common SHARED adminusers.c alloc.c authenticator.c atomic.c buffer.c config.c dcb.c filter.c externcmd.c gwbitmask.c gwdirs.c hashtable.c hint.c housekeeper.c listmanager.c load_utils.c log_manager.cc maxscale_pcre2.c memlog.c misc.c mlist.c modutil.c monitor.c queuemanager.c query_classifier.c poll.c random_jkiss.c resultset.c secrets.c server.c service.c session.c spinlock.c thread.c users.c utils.c skygw_utils.cc statistics.c listener.c gw_ssl.c mysql_utils.c mysql_binlog.c)
|
||||
|
||||
target_link_libraries(maxscale-common ${MARIADB_CONNECTOR_LIBRARIES} ${LZMA_LINK_FLAGS} ${PCRE2_LIBRARIES} ${CURL_LIBRARIES} ssl pthread crypt dl crypto inih z rt m stdc++)
|
||||
|
||||
|
||||
@ -89,14 +89,14 @@ bool authenticator_init(void** dest, const char *authenticator, const char *opti
|
||||
* @return The default authenticator for the protocol or NULL if the protocol
|
||||
* does not provide one
|
||||
*/
|
||||
char* get_default_authenticator(const char *protocol)
|
||||
const char* get_default_authenticator(const char *protocol)
|
||||
{
|
||||
char *rval = NULL;
|
||||
GWPROTOCOL *protofuncs = (GWPROTOCOL*)load_module(protocol, MODULE_PROTOCOL);
|
||||
|
||||
if (protofuncs && protofuncs->auth_default)
|
||||
{
|
||||
rval = MXS_STRDUP(protofuncs->auth_default());
|
||||
rval = protofuncs->auth_default();
|
||||
}
|
||||
|
||||
return rval;
|
||||
|
||||
@ -72,7 +72,6 @@
|
||||
#include <netinet/in.h>
|
||||
#include <string.h>
|
||||
#include <sys/utsname.h>
|
||||
#include <maxscale/dbusers.h>
|
||||
#include <maxscale/alloc.h>
|
||||
#include <maxscale/limits.h>
|
||||
#define PCRE2_CODE_UNIT_WIDTH 8
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -71,7 +71,8 @@ listener_alloc(struct service* service, char* name, char *protocol, char *addres
|
||||
{
|
||||
authenticator = MXS_STRDUP(authenticator);
|
||||
}
|
||||
else if ((authenticator = get_default_authenticator(protocol)) == NULL)
|
||||
else if ((authenticator = (char*)get_default_authenticator(protocol)) == NULL ||
|
||||
(authenticator = MXS_STRDUP(authenticator)) == NULL)
|
||||
{
|
||||
MXS_ERROR("No authenticator defined for listener '%s' and could not get "
|
||||
"default authenticator for protocol '%s'.", name, protocol);
|
||||
@ -83,6 +84,9 @@ listener_alloc(struct service* service, char* name, char *protocol, char *addres
|
||||
{
|
||||
MXS_ERROR("Failed to initialize authenticator module '%s' for "
|
||||
"listener '%s'.", authenticator, name);
|
||||
MXS_FREE(address);
|
||||
MXS_FREE(authenticator);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
protocol = MXS_STRDUP(protocol);
|
||||
|
||||
@ -22,7 +22,6 @@
|
||||
#include <string.h>
|
||||
#include <maxscale/debug.h>
|
||||
#include <maxscale/users.h>
|
||||
#include <maxscale/dbusers.h>
|
||||
#include <strings.h>
|
||||
|
||||
/**
|
||||
|
||||
@ -69,7 +69,8 @@ server_alloc(char *servname, char *protocol, unsigned short port, char *authenti
|
||||
{
|
||||
authenticator = MXS_STRDUP(authenticator);
|
||||
}
|
||||
else if ((authenticator = get_default_authenticator(protocol)) == NULL)
|
||||
else if ((authenticator = (char*)get_default_authenticator(protocol)) == NULL ||
|
||||
(authenticator = MXS_STRDUP(authenticator)) == NULL)
|
||||
{
|
||||
MXS_ERROR("No authenticator defined for server at %s:%u and no default "
|
||||
"authenticator for protocol '%s'.", servname, port, protocol);
|
||||
|
||||
@ -53,7 +53,6 @@
|
||||
#include <maxscale/dcb.h>
|
||||
#include <maxscale/users.h>
|
||||
#include <maxscale/filter.h>
|
||||
#include <maxscale/dbusers.h>
|
||||
#include <maxscale/poll.h>
|
||||
#include <maxscale/log_manager.h>
|
||||
#include <sys/stat.h>
|
||||
@ -210,6 +209,16 @@ service_isvalid(SERVICE *service)
|
||||
return rval;
|
||||
}
|
||||
|
||||
static inline void close_port(SERV_LISTENER *port)
|
||||
{
|
||||
port->service->state = SERVICE_STATE_FAILED;
|
||||
if (port->listener)
|
||||
{
|
||||
dcb_close(port->listener);
|
||||
port->listener = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Start an individual port/protocol pair
|
||||
*
|
||||
@ -232,7 +241,9 @@ serviceStartPort(SERVICE *service, SERV_LISTENER *port)
|
||||
{
|
||||
/* Should never happen, this guarantees it can't */
|
||||
MXS_ERROR("Attempt to start port with null or incomplete service");
|
||||
goto retblock;
|
||||
close_port(port);
|
||||
ss_dassert(false);
|
||||
return 0;
|
||||
}
|
||||
|
||||
port->listener = dcb_alloc(DCB_ROLE_SERVICE_LISTENER, port);
|
||||
@ -240,7 +251,8 @@ serviceStartPort(SERVICE *service, SERV_LISTENER *port)
|
||||
if (port->listener == NULL)
|
||||
{
|
||||
MXS_ERROR("Failed to create listener for service %s.", service->name);
|
||||
goto retblock;
|
||||
close_port(port);
|
||||
return 0;
|
||||
}
|
||||
|
||||
port->listener->service = service;
|
||||
@ -252,13 +264,10 @@ serviceStartPort(SERVICE *service, SERV_LISTENER *port)
|
||||
|
||||
if ((funcs = (GWPROTOCOL *)load_module(port->protocol, MODULE_PROTOCOL)) == NULL)
|
||||
{
|
||||
dcb_close(port->listener);
|
||||
port->listener = NULL;
|
||||
MXS_ERROR("Unable to load protocol module %s. Listener "
|
||||
"for service %s not started.",
|
||||
port->protocol,
|
||||
service->name);
|
||||
goto retblock;
|
||||
MXS_ERROR("Unable to load protocol module %s. Listener for service %s not started.",
|
||||
port->protocol, service->name);
|
||||
close_port(port);
|
||||
return 0;
|
||||
}
|
||||
|
||||
memcpy(&(port->listener->func), funcs, sizeof(GWPROTOCOL));
|
||||
@ -280,8 +289,7 @@ serviceStartPort(SERVICE *service, SERV_LISTENER *port)
|
||||
{
|
||||
MXS_ERROR("Failed to load authenticator module '%s' for listener '%s'",
|
||||
authenticator_name, port->name);
|
||||
dcb_close(port->listener);
|
||||
port->listener = NULL;
|
||||
close_port(port);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -302,11 +310,24 @@ serviceStartPort(SERVICE *service, SERV_LISTENER *port)
|
||||
}
|
||||
|
||||
/** Load the authentication users before before starting the listener */
|
||||
if (port->listener->authfunc.loadusers &&
|
||||
port->listener->authfunc.loadusers(port) != MXS_AUTH_LOADUSERS_OK)
|
||||
if (port->listener->authfunc.loadusers)
|
||||
{
|
||||
MXS_ERROR("[%s] Failed to load users for listener '%s', authentication might not work.",
|
||||
service->name, port->name);
|
||||
switch (port->listener->authfunc.loadusers(port))
|
||||
{
|
||||
case MXS_AUTH_LOADUSERS_FATAL:
|
||||
MXS_ERROR("[%s] Fatal error when loading users for listener '%s', "
|
||||
"service is not started.", service->name, port->name);
|
||||
close_port(port);
|
||||
return 0;
|
||||
|
||||
case MXS_AUTH_LOADUSERS_ERROR:
|
||||
MXS_WARNING("[%s] Failed to load users for listener '%s', authentication"
|
||||
" might not work.", service->name, port->name);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -327,24 +348,16 @@ serviceStartPort(SERVICE *service, SERV_LISTENER *port)
|
||||
}
|
||||
else
|
||||
{
|
||||
MXS_ERROR("Failed to create session to service %s.",
|
||||
service->name);
|
||||
dcb_close(port->listener);
|
||||
port->listener = NULL;
|
||||
goto retblock;
|
||||
MXS_ERROR("[%s] Failed to create listener session.", service->name);
|
||||
close_port(port);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
MXS_ERROR("Unable to start to listen port %d for %s %s.",
|
||||
port->port,
|
||||
port->protocol,
|
||||
service->name);
|
||||
dcb_close(port->listener);
|
||||
port->listener = NULL;
|
||||
MXS_ERROR("[%s] Failed to listen on %s", service->name, config_bind);
|
||||
close_port(port);
|
||||
}
|
||||
|
||||
retblock:
|
||||
return listeners;
|
||||
}
|
||||
|
||||
@ -369,7 +382,11 @@ int serviceStartAllPorts(SERVICE* service)
|
||||
port = port->next;
|
||||
}
|
||||
|
||||
if (listeners)
|
||||
if (service->state == SERVICE_STATE_FAILED)
|
||||
{
|
||||
listeners = 0;
|
||||
}
|
||||
else if (listeners)
|
||||
{
|
||||
service->state = SERVICE_STATE_STARTED;
|
||||
service->stats.started = time(0);
|
||||
@ -457,29 +474,20 @@ int
|
||||
serviceStart(SERVICE *service)
|
||||
{
|
||||
int listeners = 0;
|
||||
char **router_options = copy_string_array(service->routerOptions);
|
||||
|
||||
if (check_service_permissions(service))
|
||||
if ((service->router_instance = service->router->createInstance(service, router_options)))
|
||||
{
|
||||
char **router_options = copy_string_array(service->routerOptions);
|
||||
if ((service->router_instance = service->router->createInstance(
|
||||
service, router_options)))
|
||||
{
|
||||
listeners += serviceStartAllPorts(service);
|
||||
}
|
||||
else
|
||||
{
|
||||
MXS_ERROR("%s: Failed to create router instance for service. Service not started.",
|
||||
service->name);
|
||||
service->state = SERVICE_STATE_FAILED;
|
||||
}
|
||||
free_string_array(router_options);
|
||||
listeners = serviceStartAllPorts(service);
|
||||
}
|
||||
else
|
||||
{
|
||||
MXS_ERROR("%s: Inadequate user permissions for service. Service not started.",
|
||||
service->name);
|
||||
MXS_ERROR("%s: Failed to create router instance. Service not started.", service->name);
|
||||
service->state = SERVICE_STATE_FAILED;
|
||||
}
|
||||
|
||||
free_string_array(router_options);
|
||||
|
||||
return listeners;
|
||||
}
|
||||
|
||||
@ -1461,12 +1469,26 @@ int service_refresh_users(SERVICE *service)
|
||||
|
||||
for (SERV_LISTENER *port = service->ports; port; port = port->next)
|
||||
{
|
||||
if (port->listener->authfunc.loadusers &&
|
||||
port->listener->authfunc.loadusers(port) != MXS_AUTH_LOADUSERS_OK)
|
||||
/** Load the authentication users before before starting the listener */
|
||||
if (port->listener->authfunc.loadusers)
|
||||
{
|
||||
MXS_ERROR("[%s] Failed to load users for listener '%s', authentication might not work.",
|
||||
service->name, port->name);
|
||||
ret = 1;
|
||||
switch (port->listener->authfunc.loadusers(port))
|
||||
{
|
||||
case MXS_AUTH_LOADUSERS_FATAL:
|
||||
MXS_ERROR("[%s] Fatal error when loading users for listener '%s',"
|
||||
" authentication will not work.", service->name, port->name);
|
||||
ret = 1;
|
||||
break;
|
||||
|
||||
case MXS_AUTH_LOADUSERS_ERROR:
|
||||
MXS_WARNING("[%s] Failed to load users for listener '%s', authentication"
|
||||
" might not work.", service->name, port->name);
|
||||
ret = 1;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -9,7 +9,6 @@ add_executable(test_log testlog.c)
|
||||
add_executable(test_logorder testlogorder.c)
|
||||
add_executable(test_logthrottling testlogthrottling.cc)
|
||||
add_executable(test_modutil testmodutil.c)
|
||||
add_executable(test_mysql_users test_mysql_users.c)
|
||||
add_executable(test_poll testpoll.c)
|
||||
add_executable(test_queuemanager testqueuemanager.c)
|
||||
add_executable(test_server testserver.c)
|
||||
@ -30,7 +29,6 @@ target_link_libraries(test_log maxscale-common)
|
||||
target_link_libraries(test_logorder maxscale-common)
|
||||
target_link_libraries(test_logthrottling maxscale-common)
|
||||
target_link_libraries(test_modutil maxscale-common)
|
||||
target_link_libraries(test_mysql_users MySQLAuth MySQLCommon maxscale-common)
|
||||
target_link_libraries(test_poll maxscale-common)
|
||||
target_link_libraries(test_queuemanager maxscale-common)
|
||||
target_link_libraries(test_server maxscale-common)
|
||||
@ -53,7 +51,6 @@ add_test(TestLogThrottling test_logthrottling)
|
||||
add_test(TestMaxScalePCRE2 testmaxscalepcre2)
|
||||
add_test(TestMemlog testmemlog)
|
||||
add_test(TestModutil test_modutil)
|
||||
add_test(TestMySQLUsers test_mysql_users)
|
||||
add_test(NAME TestMaxPasswd COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/testmaxpasswd.sh)
|
||||
add_test(TestPoll test_poll)
|
||||
add_test(TestQueueManager test_queuemanager)
|
||||
|
||||
@ -1,546 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2016 MariaDB Corporation Ab
|
||||
*
|
||||
* Use of this software is governed by the Business Source License included
|
||||
* in the LICENSE.TXT file and at www.mariadb.com/bsl.
|
||||
*
|
||||
* Change Date: 2019-07-01
|
||||
*
|
||||
* On the date above, in accordance with the Business Source License, use
|
||||
* of this software will be governed by version 2 or later of the General
|
||||
* Public License.
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @verbatim
|
||||
* Revision History
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <maxscale/dcb.h>
|
||||
#include <maxscale/service.h>
|
||||
#include <maxscale/users.h>
|
||||
#include <maxscale/log_manager.h>
|
||||
#include <maxscale/secrets.h>
|
||||
#include <maxscale/dbusers.h>
|
||||
#include <maxscale/protocol/mysql.h>
|
||||
#include <mysql_auth.h>
|
||||
#include <maxscale/listener.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <maxscale/alloc.h>
|
||||
|
||||
extern int setipaddress();
|
||||
|
||||
int set_and_get_single_mysql_users_ipv4(char *username, unsigned long ipv4, char *password)
|
||||
{
|
||||
struct sockaddr_in serv_addr;
|
||||
MYSQL_USER_HOST key;
|
||||
MYSQL_USER_HOST find_key;
|
||||
USERS *mysql_users;
|
||||
char ret_ip[200] = "";
|
||||
char *fetch_data;
|
||||
char *db = "";
|
||||
DCB *dcb;
|
||||
SERVICE *service;
|
||||
SERV_LISTENER dummy;
|
||||
unsigned long fix_ipv4;
|
||||
|
||||
dcb = dcb_alloc(DCB_ROLE_INTERNAL, &dummy);
|
||||
|
||||
if (dcb == NULL)
|
||||
{
|
||||
fprintf(stderr, "dcb_alloc() failed\n");
|
||||
return 1;
|
||||
}
|
||||
if ((service = (SERVICE *)MXS_CALLOC(1, sizeof(SERVICE))) == NULL)
|
||||
{
|
||||
dcb_close(dcb);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (ipv4 > UINT_MAX)
|
||||
{
|
||||
fix_ipv4 = UINT_MAX;
|
||||
}
|
||||
else
|
||||
{
|
||||
fix_ipv4 = ipv4;
|
||||
}
|
||||
|
||||
mysql_users = mysql_users_alloc();
|
||||
/* prepare the user@host data struct */
|
||||
memset(&key, 0, sizeof(key));
|
||||
memset(&serv_addr, 0, sizeof(serv_addr));
|
||||
serv_addr.sin_family = AF_INET;
|
||||
memcpy(&(serv_addr).sin_addr.s_addr, &fix_ipv4, sizeof(ipv4));
|
||||
|
||||
key.user = username;
|
||||
memcpy(&key.ipv4, &serv_addr, sizeof(serv_addr));
|
||||
key.resource = db;
|
||||
|
||||
inet_ntop(AF_INET, &(serv_addr).sin_addr, ret_ip, INET_ADDRSTRLEN);
|
||||
|
||||
fprintf(stderr, "IPv4 passed/fixed [%lu/%lu] is [%s]\n", ipv4, fix_ipv4, ret_ip);
|
||||
|
||||
/* add user@host as key and passwd as value in the MySQL users hash table */
|
||||
if (!mysql_users_add(mysql_users, &key, password))
|
||||
{
|
||||
fprintf(stderr, "Failed adding %s@%s(%lu)\n", username, ret_ip, fix_ipv4);
|
||||
users_free(mysql_users);
|
||||
MXS_FREE(service);
|
||||
dcb_close(dcb);
|
||||
return 1;
|
||||
}
|
||||
|
||||
memset(&serv_addr, 0, sizeof(serv_addr));
|
||||
memset(&find_key, 0, sizeof(find_key));
|
||||
|
||||
find_key.user = username;
|
||||
memcpy(&(serv_addr).sin_addr.s_addr, &ipv4, sizeof(ipv4));
|
||||
find_key.resource = db;
|
||||
|
||||
memcpy(&find_key.ipv4, &serv_addr, sizeof(serv_addr));
|
||||
|
||||
fetch_data = mysql_users_fetch(mysql_users, &find_key);
|
||||
|
||||
users_free(mysql_users);
|
||||
MXS_FREE(service);
|
||||
dcb_close(dcb);
|
||||
|
||||
if (!fetch_data)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int set_and_get_single_mysql_users(char *username, char *hostname, char *password)
|
||||
{
|
||||
struct sockaddr_in serv_addr;
|
||||
MYSQL_USER_HOST key;
|
||||
USERS *mysql_users;
|
||||
char ret_ip[200] = "";
|
||||
char *fetch_data;
|
||||
char *db = "";
|
||||
|
||||
mysql_users = mysql_users_alloc();
|
||||
|
||||
/* prepare the user@host data struct */
|
||||
memset(&serv_addr, 0, sizeof(serv_addr));
|
||||
memset(&key, 0, sizeof(key));
|
||||
|
||||
|
||||
if (hostname)
|
||||
if (!setipaddress(&serv_addr.sin_addr, hostname))
|
||||
{
|
||||
fprintf(stderr, "setipaddress failed for host [%s]\n", hostname);
|
||||
users_free(mysql_users);
|
||||
return 1;
|
||||
}
|
||||
if (username)
|
||||
{
|
||||
key.user = username;
|
||||
}
|
||||
|
||||
memcpy(&key.ipv4, &serv_addr, sizeof(serv_addr));
|
||||
key.resource = db;
|
||||
|
||||
inet_ntop(AF_INET, &(serv_addr).sin_addr, ret_ip, INET_ADDRSTRLEN);
|
||||
|
||||
fprintf(stderr, "set/get [%s@%s]: IPV4 %lu is [%u].[%u].[%u].[%u]\n", username, hostname,
|
||||
(unsigned long) serv_addr.sin_addr.s_addr, serv_addr.sin_addr.s_addr & 0xFF,
|
||||
(serv_addr.sin_addr.s_addr & 0xFF00), (serv_addr.sin_addr.s_addr & 0xFF0000),
|
||||
((serv_addr.sin_addr.s_addr & 0xFF000000) / (256 * 256 * 256)));
|
||||
|
||||
/* add user@host as key and passwd as value in the MySQL users hash table */
|
||||
if (!mysql_users_add(mysql_users, &key, password))
|
||||
{
|
||||
fprintf(stderr, "mysql_users_add() failed for %s@%s\n", username, hostname);
|
||||
users_free(mysql_users);
|
||||
return 1;
|
||||
}
|
||||
|
||||
memset(&serv_addr, 0, sizeof(serv_addr));
|
||||
|
||||
if (hostname)
|
||||
if (!setipaddress(&serv_addr.sin_addr, hostname))
|
||||
{
|
||||
fprintf(stderr, "setipaddress failed for host [%s]\n", hostname);
|
||||
users_free(mysql_users);
|
||||
return 1;
|
||||
}
|
||||
key.user = username;
|
||||
memcpy(&key.ipv4, &serv_addr, sizeof(serv_addr));
|
||||
key.resource = db;
|
||||
|
||||
fetch_data = mysql_users_fetch(mysql_users, &key);
|
||||
|
||||
users_free(mysql_users);
|
||||
|
||||
if (!fetch_data)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int set_and_get_mysql_users_wildcards(char *username, char *hostname, char *password, char *from, char *anydb,
|
||||
char *db, char *db_from)
|
||||
{
|
||||
USERS *mysql_users;
|
||||
int ret = -1;
|
||||
struct sockaddr_in client_addr;
|
||||
DCB *dcb;
|
||||
SERVICE *service;
|
||||
MYSQL_session *data;
|
||||
|
||||
if ((service = (SERVICE *)MXS_CALLOC(1, sizeof(SERVICE))) == NULL)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
|
||||
SERV_LISTENER *port = listener_alloc(service, "testlistener", "MySQLClient", NULL, 4006, "MySQLAuth", NULL, NULL);
|
||||
|
||||
dcb = dcb_alloc(DCB_ROLE_INTERNAL, port);
|
||||
|
||||
if (dcb == NULL)
|
||||
{
|
||||
fprintf(stderr, "dcb_alloc() failed\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
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);
|
||||
MXS_FREE(service);
|
||||
dcb_close(dcb);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
if ((data = (MYSQL_session *) MXS_CALLOC(1, sizeof(MYSQL_session))) == NULL)
|
||||
{
|
||||
MXS_FREE(service);
|
||||
dcb_close(dcb);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/* 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->ports = port;
|
||||
service->ports->users = mysql_users;
|
||||
|
||||
if (db_from != NULL)
|
||||
{
|
||||
strcpy(data->db, db_from);
|
||||
}
|
||||
else
|
||||
{
|
||||
data->db[0] = 0;
|
||||
}
|
||||
|
||||
/* freed by dcb_close(dcb) */
|
||||
dcb->data = data;
|
||||
|
||||
// the routine returns 1 on success
|
||||
if (anydb != NULL)
|
||||
{
|
||||
if (strcmp(anydb, "N") == 0)
|
||||
{
|
||||
ret = add_mysql_users_with_host_ipv4(mysql_users, username, hostname, password, anydb, db);
|
||||
}
|
||||
else if (strcmp(anydb, "Y") == 0)
|
||||
{
|
||||
ret = add_mysql_users_with_host_ipv4(mysql_users, username, hostname, password, "Y", "");
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = add_mysql_users_with_host_ipv4(mysql_users, username, hostname, password, "N", NULL);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = add_mysql_users_with_host_ipv4(mysql_users, username, hostname, password, "N", NULL);
|
||||
}
|
||||
|
||||
if (ret == 0)
|
||||
{
|
||||
fprintf(stderr, "add_mysql_users_with_host_ipv4 (%s@%s, %s) FAILED\n", username, hostname, password);
|
||||
}
|
||||
else
|
||||
{
|
||||
unsigned char db_passwd[100] = "";
|
||||
|
||||
dcb->remote = MXS_STRDUP_A(from);
|
||||
|
||||
// returns 0 on success
|
||||
ret = gw_find_mysql_user_password_sha1(username, db_passwd, dcb);
|
||||
}
|
||||
|
||||
users_free(mysql_users);
|
||||
MXS_FREE(service);
|
||||
dcb_close(dcb);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int ret;
|
||||
int i = 0;
|
||||
int k = 0;
|
||||
time_t t;
|
||||
|
||||
fprintf(stderr, "----------------\n");
|
||||
|
||||
time(&t);
|
||||
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");
|
||||
assert(ret == 0);
|
||||
ret = set_and_get_single_mysql_users("pippo", "%", "xyz");
|
||||
assert(ret == 1);
|
||||
ret = set_and_get_single_mysql_users("rootuser", NULL, "wwwww");
|
||||
assert(ret == 0);
|
||||
ret = set_and_get_single_mysql_users("nullpwd", "this_host_does_not_exists", NULL);
|
||||
assert(ret == 1);
|
||||
ret = set_and_get_single_mysql_users("myuser", "345.-1.5.40997", "password");
|
||||
assert(ret == 1);
|
||||
ret = set_and_get_single_mysql_users(NULL, NULL, NULL);
|
||||
assert(ret == 1);
|
||||
|
||||
ret = set_and_get_single_mysql_users_ipv4("negative", -467295, "_ncd");
|
||||
assert(ret == 1);
|
||||
ret = set_and_get_single_mysql_users_ipv4("extra", 0xFFFFFFFFFUL * 100, "JJcd");
|
||||
assert(ret == 1);
|
||||
ret = set_and_get_single_mysql_users_ipv4("aaapo", 0, "JJcd");
|
||||
assert(ret == 0);
|
||||
ret = set_and_get_single_mysql_users_ipv4(NULL, '\0', "JJcd");
|
||||
assert(ret == 1);
|
||||
|
||||
|
||||
for (i = 256 * 256 * 256; i <= 256 * 256 * 256 + 5; i++)
|
||||
{
|
||||
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", NULL, NULL, NULL);
|
||||
if (ret)
|
||||
{
|
||||
fprintf(stderr, "\t-- Expecting no match\n");
|
||||
}
|
||||
assert(ret == 1);
|
||||
|
||||
ret = set_and_get_mysql_users_wildcards("pippo", "%", "", "127.0.0.1", NULL, NULL, NULL);
|
||||
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", NULL, NULL, NULL);
|
||||
if (!ret)
|
||||
{
|
||||
fprintf(stderr, "\t-- Expecting ok\n");
|
||||
}
|
||||
assert(ret == 0);
|
||||
|
||||
ret = set_and_get_mysql_users_wildcards("pippo", "192.168.4.%", "ffoo", "192.168.2.2", NULL, NULL, NULL);
|
||||
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", NULL, NULL, NULL);
|
||||
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", NULL, NULL, NULL);
|
||||
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", "Y", NULL, "cossa");
|
||||
if (!ret)
|
||||
{
|
||||
fprintf(stderr, "\t-- Expecting ok\n");
|
||||
}
|
||||
assert(ret == 0);
|
||||
|
||||
fprintf(stderr, "Adding pippo, 192.%%.%%.%%, foo, 192.0.0.2, N, NULL, ragione\n");
|
||||
ret = set_and_get_mysql_users_wildcards("pippo", "192.%.%.%", "foo", "192.0.0.2", "N", NULL, "ragione");
|
||||
if (!ret)
|
||||
{
|
||||
fprintf(stderr, "\t-- Expecting no match\n");
|
||||
}
|
||||
assert(ret == 1);
|
||||
|
||||
ret = set_and_get_mysql_users_wildcards("pippo", "192.0.%.%", "foo", "192.2.0.2", NULL, NULL, NULL);
|
||||
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", NULL, NULL, NULL);
|
||||
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", NULL, NULL, NULL);
|
||||
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", NULL, NULL, NULL);
|
||||
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", "Y", NULL, NULL);
|
||||
if (ret)
|
||||
{
|
||||
fprintf(stderr, "\t-- Expecting no match\n");
|
||||
}
|
||||
assert(ret == 1);
|
||||
|
||||
ret = set_and_get_mysql_users_wildcards("pippo", "192.%.%.%", "foo", "192.254.254.245", "N", "matto",
|
||||
"matto");
|
||||
if (!ret)
|
||||
{
|
||||
fprintf(stderr, "\t-- Expecting ok\n");
|
||||
}
|
||||
assert(ret == 0);
|
||||
|
||||
ret = set_and_get_mysql_users_wildcards("pippo", "192.%.%.%", "foo", "192.254.254.245", "N", "matto",
|
||||
"fatto");
|
||||
if (!ret)
|
||||
{
|
||||
fprintf(stderr, "\t-- Expecting no match\n");
|
||||
}
|
||||
assert(ret == 1);
|
||||
|
||||
ret = set_and_get_mysql_users_wildcards("pippo", "192.%.%.%", "foo", "192.254.254.245", "Y", "matto",
|
||||
"fatto");
|
||||
if (!ret)
|
||||
{
|
||||
fprintf(stderr, "\t-- Expecting ok\n");
|
||||
}
|
||||
assert(ret == 0);
|
||||
|
||||
ret = set_and_get_mysql_users_wildcards("pippo", "192.%.%.%", "foo", "192.254.254.245", "Y", "", "fto");
|
||||
if (!ret)
|
||||
{
|
||||
fprintf(stderr, "\t-- Expecting ok\n");
|
||||
}
|
||||
assert(ret == 0);
|
||||
|
||||
ret = set_and_get_mysql_users_wildcards("pippo", "192.%.%.%", "foo", "192.254.254.245", "Y", NULL, "grewao");
|
||||
if (!ret)
|
||||
{
|
||||
fprintf(stderr, "\t-- Expecting ok\n");
|
||||
}
|
||||
assert(ret == 0);
|
||||
|
||||
ret = set_and_get_mysql_users_wildcards("pippo", "192.%.%.%", "foo", "192.254.254.242", NULL, NULL, NULL);
|
||||
if (!ret)
|
||||
{
|
||||
fprintf(stderr, "\t-- Expecting ok\n");
|
||||
}
|
||||
assert(ret == 0);
|
||||
|
||||
ret = set_and_get_mysql_users_wildcards("pippo", "192.%", "foo", "192.254.254.242", NULL, NULL, NULL);
|
||||
if (!ret)
|
||||
{
|
||||
fprintf(stderr, "\t-- Expecting ok\n");
|
||||
}
|
||||
assert(ret == 0);
|
||||
|
||||
ret = set_and_get_mysql_users_wildcards("pippo", "192.%.%", "foo", "192.254.254.242", NULL, NULL, NULL);
|
||||
if (!ret)
|
||||
{
|
||||
fprintf(stderr, "\t-- Expecting ok\n");
|
||||
}
|
||||
assert(ret == 0);
|
||||
|
||||
ret = set_and_get_mysql_users_wildcards("pippo", "192.254.%", "foo", "192.254.254.242", NULL, NULL, NULL);
|
||||
if (!ret)
|
||||
{
|
||||
fprintf(stderr, "\t-- Expecting ok\n");
|
||||
}
|
||||
assert(ret == 0);
|
||||
|
||||
ret = set_and_get_mysql_users_wildcards("pippo", "192.254.%", "foo", "192.254.0.242", NULL, NULL, NULL);
|
||||
if (!ret)
|
||||
{
|
||||
fprintf(stderr, "\t-- Expecting ok\n");
|
||||
}
|
||||
assert(ret == 0);
|
||||
|
||||
ret = set_and_get_mysql_users_wildcards("riccio", "192.0.0.%", "foo", "192.134.0.2", NULL, NULL, NULL);
|
||||
if (ret)
|
||||
{
|
||||
fprintf(stderr, "\t-- Expecting no match\n");
|
||||
}
|
||||
assert(ret == 1);
|
||||
|
||||
ret = set_and_get_mysql_users_wildcards("pippo", "192.%.%.%", "12345678901234567890123456789012345678901234",
|
||||
"192.254.254.245", "Y", NULL, NULL);
|
||||
if (!ret)
|
||||
{
|
||||
fprintf(stderr, "\t-- Expecting ok\n");
|
||||
}
|
||||
assert(ret == 0);
|
||||
|
||||
fprintf(stderr, "----------------\n");
|
||||
fprintf(stderr, "<<< Test completed\n");
|
||||
|
||||
time(&t);
|
||||
fprintf(stderr, "%s\n", asctime(localtime(&t)));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user