From b9e079ce17854de0b660df80f135e6f78180bef6 Mon Sep 17 00:00:00 2001 From: Mark Riddoch Date: Mon, 24 Jun 2013 00:59:12 +0200 Subject: [PATCH] Addition of the administration user in the service Addition of general purpose hashtable (not complete) and users table (not complete) --- core/Makefile | 6 ++-- core/config.c | 7 +++++ core/depend.mk | 28 ++++++++++++++++++- core/hashtable.c | 62 +++++++++++++++++++++++++++++++++++++++++ core/service.c | 51 ++++++++++++++++++++++++++++++++++ core/users.c | 67 +++++++++++++++++++++++++++++++++++++++++++++ include/hashtable.h | 62 +++++++++++++++++++++++++++++++++++++++++ include/service.h | 17 ++++++++++++ include/users.h | 43 +++++++++++++++++++++++++++++ 9 files changed, 340 insertions(+), 3 deletions(-) create mode 100644 core/hashtable.c create mode 100644 core/users.c create mode 100644 include/hashtable.h create mode 100644 include/users.h diff --git a/core/Makefile b/core/Makefile index 7be7e61a0..a6629bebb 100644 --- a/core/Makefile +++ b/core/Makefile @@ -24,11 +24,13 @@ CC=cc CFLAGS=-c -I/usr/include -I../include -I../inih -Wall -g LDFLAGS=-rdynamic SRCS= atomic.c buffer.c spinlock.c gateway.c gateway_mysql_protocol.c gw_utils.c \ - utils.c dcb.c load_utils.c session.c service.c server.c poll.c config.c + utils.c dcb.c load_utils.c session.c service.c server.c poll.c config.c \ + users.c hashtable.c HDRS= ../include/atomic.h ../include/buffer.h ../include/dcb.h \ ../include/gateway_mysql.h ../include/gw.h ../include/mysql_protocol.h \ ../include/session.h ../include/spinlock.h ../include/thread.h \ - ../include/modules.h ../include/poll.h ../include/config.h + ../include/modules.h ../include/poll.h ../include/config.h ../include/users.h \ + ../include/hashtable.h OBJ=$(SRCS:.c=.o) LIBS=-L../inih/extra -linih -lssl -lstdc++ diff --git a/core/config.c b/core/config.c index b5d2b414c..d1d00a9e9 100644 --- a/core/config.c +++ b/core/config.c @@ -34,6 +34,7 @@ #include #include #include +#include static int process_config_context(CONFIG_CONTEXT *); @@ -125,7 +126,13 @@ CONFIG_CONTEXT *obj; { char *router = config_get_value(obj->parameters, "router"); if (router) + { obj->element = service_alloc(obj->object, router); + char *user = config_get_value(obj->parameters, "user"); + char *auth = config_get_value(obj->parameters, "auth"); + if (obj->element && user && auth) + serviceSetUser(obj->element, user, auth); + } else fprintf(stderr, "No router define for service '%s'\n", obj->object); diff --git a/core/depend.mk b/core/depend.mk index a7cd9ba42..e3c44166a 100644 --- a/core/depend.mk +++ b/core/depend.mk @@ -395,4 +395,30 @@ config.o: config.c /usr/include/stdio.h /usr/include/features.h \ ../include/service.h ../include/spinlock.h ../include/thread.h \ /usr/include/pthread.h /usr/include/sched.h /usr/include/bits/sched.h \ /usr/include/bits/setjmp.h ../include/dcb.h ../include/buffer.h \ - ../include/server.h + ../include/server.h ../include/users.h ../include/hashtable.h +users.o: users.c /usr/include/stdlib.h /usr/include/features.h \ + /usr/include/sys/cdefs.h /usr/include/bits/wordsize.h \ + /usr/include/gnu/stubs.h /usr/include/gnu/stubs-64.h \ + /usr/lib/gcc/x86_64-redhat-linux/4.4.6/include/stddef.h \ + /usr/include/bits/waitflags.h /usr/include/bits/waitstatus.h \ + /usr/include/endian.h /usr/include/bits/endian.h \ + /usr/include/bits/byteswap.h /usr/include/sys/types.h \ + /usr/include/bits/types.h /usr/include/bits/typesizes.h \ + /usr/include/time.h /usr/include/sys/select.h /usr/include/bits/select.h \ + /usr/include/bits/sigset.h /usr/include/bits/time.h \ + /usr/include/sys/sysmacros.h /usr/include/bits/pthreadtypes.h \ + /usr/include/alloca.h /usr/include/string.h /usr/include/xlocale.h \ + ../include/users.h ../include/hashtable.h +hashtable.o: hashtable.c /usr/include/stdlib.h /usr/include/features.h \ + /usr/include/sys/cdefs.h /usr/include/bits/wordsize.h \ + /usr/include/gnu/stubs.h /usr/include/gnu/stubs-64.h \ + /usr/lib/gcc/x86_64-redhat-linux/4.4.6/include/stddef.h \ + /usr/include/bits/waitflags.h /usr/include/bits/waitstatus.h \ + /usr/include/endian.h /usr/include/bits/endian.h \ + /usr/include/bits/byteswap.h /usr/include/sys/types.h \ + /usr/include/bits/types.h /usr/include/bits/typesizes.h \ + /usr/include/time.h /usr/include/sys/select.h /usr/include/bits/select.h \ + /usr/include/bits/sigset.h /usr/include/bits/time.h \ + /usr/include/sys/sysmacros.h /usr/include/bits/pthreadtypes.h \ + /usr/include/alloca.h /usr/include/string.h /usr/include/xlocale.h \ + ../include/hashtable.h diff --git a/core/hashtable.c b/core/hashtable.c new file mode 100644 index 000000000..eca63f9ae --- /dev/null +++ b/core/hashtable.c @@ -0,0 +1,62 @@ +/* + * This file is distributed as part of the SkySQL Gateway. It is free + * software: you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation, + * version 2. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright SkySQL Ab 2013 + */ +#include +#include +#include + +/** + * @file hashtable.c General purpose hashtable routines + * + * @verbatim + * Revision History + * + * Date Who Description + * 23/06/13 Mark Riddoch Initial implementation + * + * @endverbatim + */ + + +/** + * Allocate a new users table + * + * @param size The size of the hash table + * @param hashfn The user supplied hash function + * @param cmpfn The user supplied key comparison function + * @return The hashtable table + */ +HASHTABLE * +hashtable_alloc(int size, int (*hashfn)(), int (*cmpfn)()) +{ +HASHTABLE *rval; + + if ((rval = malloc(sizeof(HASHTABLE))) == NULL) + return NULL; + rval->hashsize = size; + rval->hashfn = hashfn; + rval->cmpfn = cmpfn; + if ((rval->entries = calloc(size, sizeof(HASHENTRIES))) == NULL) + { + free(rval); + return NULL; + } + memset(rval->entries, 0, size * sizeof(HASHENTRIES)); + + return rval; +} + diff --git a/core/service.c b/core/service.c index eaa1cdb2c..4492c5d50 100644 --- a/core/service.c +++ b/core/service.c @@ -68,6 +68,9 @@ SERVICE *service; service->ports = NULL; service->stats.started = time(0); service->state = SERVICE_STATE_ALLOC; + service->credentials.name = NULL; + service->credentials.authdata = NULL; + service->users = NULL; spinlock_acquire(&service_spin); service->next = allServices; @@ -180,6 +183,10 @@ SERVICE *ptr; /* Clean up session and free the memory */ free(service->name); free(service->routerModule); + if (service->credentials.name) + free(service->credentials.name); + if (service->credentials.authdata) + free(service->credentials.authdata); free(service); return 1; } @@ -226,6 +233,50 @@ serviceAddBackend(SERVICE *service, SERVER *server) spinlock_release(&service->spin); } +/** + * Set the service user that is used to log in to the backebd servers + * associated with this service. + * + * @param service The service we are setting the data for + * @param user The user name to use for connections + * @param auth The authentication data we need, e.g. MySQL SHA1 password + * @return 0 on failure + */ +int +serviceSetUser(SERVICE *service, char *user, char *auth) +{ + if (service->credentials.name) + free(service->credentials.name); + if (service->credentials.authdata) + free(service->credentials.authdata); + service->credentials.name = strdup(user); + service->credentials.authdata = strdup(auth); + + if (service->credentials.name == NULL || service->credentials.authdata == NULL) + return 0; + return 1; +} + + +/** + * Get the service user that is used to log in to the backebd servers + * associated with this service. + * + * @param service The service we are setting the data for + * @param user The user name to use for connections + * @param auth The authentication data we need, e.g. MySQL SHA1 password + * @return 0 on failure + */ +int +serviceGetUser(SERVICE *service, char **user, char **auth) +{ + if (service->credentials.name == NULL || service->credentials.authdata == NULL) + return 0; + *user = service->credentials.name; + *auth = service->credentials.authdata; + return 1; +} + /** * Print details of an individual service * diff --git a/core/users.c b/core/users.c new file mode 100644 index 000000000..d341b81eb --- /dev/null +++ b/core/users.c @@ -0,0 +1,67 @@ +/* + * This file is distributed as part of the SkySQL Gateway. It is free + * software: you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation, + * version 2. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright SkySQL Ab 2013 + */ +#include +#include +#include + +/** + * @file users.c User table maintenance routines + * + * @verbatim + * Revision History + * + * Date Who Description + * 23/06/13 Mark Riddoch Initial implementation + * + * @endverbatim + */ + +/** + * The hash function we user for storing users. + * + * @param key The key value, i.e. username + * @return The hash key + */ +static int +user_hash(char *key) +{ + return (*key + *(key + 1)) % 52; +} + +/** + * Allocate a new users table + * + * @return The users table + */ +USERS * +users_alloc() +{ +USERS *rval; + + if ((rval = malloc(sizeof(USERS))) == NULL) + return NULL; + + if ((rval->data = hashtable_alloc(52, user_hash, strcmp)) == NULL) + { + free(rval); + return NULL; + } + + return rval; +} + diff --git a/include/hashtable.h b/include/hashtable.h new file mode 100644 index 000000000..79da81c3c --- /dev/null +++ b/include/hashtable.h @@ -0,0 +1,62 @@ +#ifndef _HASTABLE_H +#define _HASTABLE_H +/* + * This file is distributed as part of the SkySQL Gateway. It is free + * software: you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation, + * version 2. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright SkySQL Ab 2013 + */ + +/** + * @file hashtable.h A general purpose hashtable mechanism for use within the + * gateway + * + * @verbatim + * Revision History + * + * Date Who Description + * 23/06/13 Mark Riddoch Initial implementation + * + * @endverbatim + */ + +/** + * The entries within a hashtable. + * + * A NULL value for key indicates an empty entry. + * The next pointer is the overflow chain for this hashentry. + */ +typedef struct hashentry { + void *key; /**< The value of the key or NULL if empty entry */ + void *value; /**< The value associated with key */ + struct hashentry *next; /**< The overflow chain */ +} HASHENTRIES; + +/** + * The general purpose hashtable struct. + */ +typedef struct hashtable { + int hashsize; /**< The number of HASHENTRIES */ + HASHENTRIES *entries; /**< The entries themselves */ + int (*hashfn)(void *); /**< The hash function */ + int (*cmpfn)(void *, void *); /**< The key comparison function */ +} HASHTABLE; + +extern HASHTABLE *hashtable_alloc(int, int (*hashfn)(), + int (*cmpfn)()); /**< Allocate a hashtable */ +extern void hashtable_free(HASHTABLE *); /**< Free a hashtable */ +extern int hashtable_add(HASHTABLE *, char *, void *); /**< Add an entry */ +extern int hashtable_delete(HASHTABLE *, char *); /**< Delete an entry table */ +extern void *hashtable_fetch(HASHTABLE *, char *); /**< Fetch the data for a given key */ +#endif diff --git a/include/service.h b/include/service.h index 2e94291e0..f17e83d9f 100644 --- a/include/service.h +++ b/include/service.h @@ -35,12 +35,14 @@ * 14/06/13 Mark Riddoch Initial implementation * 18/06/13 Mark Riddoch Addition of statistics and function * prototypes + * 23/06/13 Mark Riddoch Added service user and users * * @endverbatim */ struct server; struct router; struct router_object; +struct users; /** * The servprotocol structure is used to link a service to the protocols that @@ -64,6 +66,17 @@ typedef struct { int n_sessions; /**< Number of sessions created on service since start */ int n_current; /**< Current number of sessions */ } SERVICE_STATS; + +/** + * The service user structure holds the information that is needed for this service to + * allow the gateway to login to the backend database and extact information such as + * the user table or other database status or configuration data. + */ +typedef struct { + char *name; /**< The user name to use to extract information */ + char *authdata; /**< The authentication data requied */ +} SERVICE_USER; + /** * Defines a service within the gateway. * @@ -83,8 +96,10 @@ typedef struct service { void *router_instance; /**< The router instance for this service */ struct server *databases; /**< The set of servers in the backend */ + SERVICE_USER credentials; /**< The cedentials of the service user */ SPINLOCK spin; /**< The service spinlock */ SERVICE_STATS stats; /**< The service statistics */ + struct users *users; /**< The user data for this service */ struct service *next; /**< The next service in the linked list */ } SERVICE; @@ -97,6 +112,8 @@ extern int serviceAddProtocol(SERVICE *, char *, unsigned short); extern void serviceAddBackend(SERVICE *, SERVER *); extern int serviceStart(SERVICE *); extern int serviceStartAll(); +extern int serviceSetUser(SERVICE *, char *, char *); +extern int serviceGetUser(SERVICE *, char **, char **); extern void printService(SERVICE *); extern void printAllServices(); extern void dprintAllServices(DCB *); diff --git a/include/users.h b/include/users.h new file mode 100644 index 000000000..67284ae94 --- /dev/null +++ b/include/users.h @@ -0,0 +1,43 @@ +#ifndef _USERS_H +#define _USERS_H +/* + * This file is distributed as part of the SkySQL Gateway. It is free + * software: you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation, + * version 2. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Copyright SkySQL Ab 2013 + */ +#include +/** + * @file users.h The functions to manipulate the table of users maintained + * for each service + * + * @verbatim + * Revision History + * + * Date Who Description + * 23/06/13 Mark Riddoch Initial implementation + * + * @endverbatim + */ + +typedef struct users { + HASHTABLE *data; +} USERS; + +extern USERS *users_alloc(); /**< Allocate a users table */ +extern users_free(USERS *); /**< Free a users table */ +extern int users_add(USERS *, char *, char *); /**< Add a user to the users table */ +extern int users_delete(USERS *, char *); /**< Delete a user from the users table */ +extern char *users_fetch(USERS *, char *); /**< Fetch the authentication data for a user */ +#endif