Addition of the administration user in the service

Addition of general purpose hashtable (not complete) and users table (not complete)
This commit is contained in:
Mark Riddoch 2013-06-24 00:59:12 +02:00
parent 0b7803a122
commit b9e079ce17
9 changed files with 340 additions and 3 deletions

View File

@ -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++

View File

@ -34,6 +34,7 @@
#include <config.h>
#include <service.h>
#include <server.h>
#include <users.h>
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);

View File

@ -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

62
core/hashtable.c Normal file
View File

@ -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 <stdlib.h>
#include <string.h>
#include <hashtable.h>
/**
* @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;
}

View File

@ -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
*

67
core/users.c Normal file
View File

@ -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 <stdlib.h>
#include <string.h>
#include <users.h>
/**
* @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;
}

62
include/hashtable.h Normal file
View File

@ -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

View File

@ -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 *);

43
include/users.h Normal file
View File

@ -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 <hashtable.h>
/**
* @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