diff options
author | David Sommerseth <dazo@users.sourceforge.net> | 2008-11-28 23:27:30 +0100 |
---|---|---|
committer | David Sommerseth <dazo@users.sourceforge.net> | 2008-11-28 23:27:30 +0100 |
commit | 0c35035dc8ac5d099f53353938a66b33227d3342 (patch) | |
tree | fd33e6d285eef6b27fb29e4632d3fcd1d50f5594 | |
parent | 4dabdc0e154b3be0c9b64d97041502b01662ed43 (diff) | |
download | eurephia-0c35035dc8ac5d099f53353938a66b33227d3342.tar.gz eurephia-0c35035dc8ac5d099f53353938a66b33227d3342.tar.xz eurephia-0c35035dc8ac5d099f53353938a66b33227d3342.zip |
Splitted plugin/eurephiadb_session.[ch] into two parts
One part is a generic session handling part
(common/eurephiadb_session_common.[ch]) and the other part
is left in the old plugin/eurephiadb_session.[ch].
This splitting should make it easiser to reuse some of the session
handling functions for the admin utils.
-rw-r--r-- | common/eurephiadb_session_common.c | 128 | ||||
-rw-r--r-- | common/eurephiadb_session_common.h | 44 | ||||
-rw-r--r-- | database/sqlite/CMakeLists.txt | 2 | ||||
-rw-r--r-- | database/sqlite/edb-sqlite.c | 2 | ||||
-rw-r--r-- | plugin/CMakeLists.txt | 1 | ||||
-rw-r--r-- | plugin/eurephia.c | 1 | ||||
-rw-r--r-- | plugin/eurephiadb_session.c | 106 | ||||
-rw-r--r-- | plugin/eurephiadb_session.h | 16 |
8 files changed, 179 insertions, 121 deletions
diff --git a/common/eurephiadb_session_common.c b/common/eurephiadb_session_common.c new file mode 100644 index 0000000..8fdcbc1 --- /dev/null +++ b/common/eurephiadb_session_common.c @@ -0,0 +1,128 @@ +/* eurephiadb_session_common.c -- Common function for handling sessions + * + * GPLv2 - Copyright (C) 2008 David Sommerseth <dazo@users.sourceforge.net> + * + * This program 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 + * of the License. + * + * 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. + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <openssl/rand.h> + +#include <eurephia_nullsafe.h> +#include <eurephia_context.h> +#include <eurephia_log.h> +#include <eurephia_values.h> +#include <eurephiadb_session_struct.h> +#include <eurephiadb_session_common.h> + + +// Functions needed to be found in the database driver +extern int eDBstore_session_value(eurephiaCTX *ctx, eurephiaSESSION *session, int mode, + const char *key, const char *val); + + +// Adds or updates a key in the eurephiaVALUES stack. Database is updated before the stack is updated. +// If database fails, the stack is not updated. +int eDBset_session_value(eurephiaCTX *ctx, eurephiaSESSION *session, const char *key, const char *val) { + eurephiaVALUES *svals = NULL; + + if( (session == NULL) || (key == NULL) ) { + return 0; + } + + DEBUG(ctx, 30, "Function call: eDBset_session_value(ctx, '%s','%s','%s')", + session->sessionkey, key, val); + + // Create a new session value buffer if it does not exist + if( session->sessvals == NULL ) { + session->sessvals = eCreate_value_space(ctx, 10); + if( session->sessvals == NULL ) { + eurephia_log(ctx, LOG_PANIC, 0, "Could not allocate memory for session values"); + return 0; + } + } + + // Check if the session value exists already. If it does update it, or else add it + svals = eGet_valuestruct(session->sessvals, key); + if( (svals == NULL) && (val != NULL) ) { + DEBUG(ctx, 32, "eDBset_session_value ... New session value: %s = '%s'", key, val); + // Add a new session value + if( eDBstore_session_value(ctx, session, SESSVAL_NEW, key, val) ) { + DEBUG(ctx, 32, "eDBset_session_value ... Adding value to value stack: %s = '%s'", + key, val); + // Add value to the stack + eAdd_value(ctx, session->sessvals, key, val); + + DEBUG(ctx, 32, "Registered session variable to session '%s': %s = %s", + session->sessionkey, key, val); + } + } else if( svals != NULL ) { + if( (val != NULL) && (strcmp(svals->val, val) == 0) ) { + DEBUG(ctx, 32, "Session value not changed('%s','%s','%s)", + session->sessionkey, key, val); + return 1; + } + // Update the value in the stack if database is updated without errors + if( eDBstore_session_value(ctx, session,(val != NULL ? SESSVAL_UPDATE : SESSVAL_DELETE), key,val)){ + free_nullsafe(svals->val); + svals->val = strdup_nullsafe(val); + DEBUG(ctx, 32, "Session variable updated in session '%s': %s = %s", + session->sessionkey, key, val); + } + } else if( (svals == NULL) && (val == NULL ) ) { + DEBUG(ctx, 32, "Ignoring saving new session value '%s' == NULL", key); + } + return 1; +} + + +// Generate some random data and return a string. +static int rand_init = 0; +int eDBsessionGetRandString(eurephiaCTX *ctx, char *rndstr, int len) { + int attempts = 0; + do { + if( !rand_init ) { + if( !RAND_load_file("/dev/urandom", 64) ) { + eurephia_log(ctx, LOG_FATAL, 0, "Could not load random data from /dev/urandom"); + return 0; + } + rand_init = 1; + } + + if( RAND_pseudo_bytes((unsigned char *) rndstr, len) ) { + return 1; + } + sleep(1); + rand_init = 0; + } while( attempts++ < 11 ); + eurephia_log(ctx, LOG_FATAL, 0, "RAND_pseudo_bytes() could not generate enough random data"); + return 0; +} + + +// Free up the memory used by a session structure +void eDBfree_session_func(eurephiaCTX *ctx, eurephiaSESSION *session) { + if( session == NULL ) { + return; + } + DEBUG(ctx, 12, "Function call: eDBfree_session(ctx, '%s')", session->sessionkey); + eFree_values(ctx, session->sessvals); + free_nullsafe(session->sessionkey); + free_nullsafe(session); +} diff --git a/common/eurephiadb_session_common.h b/common/eurephiadb_session_common.h new file mode 100644 index 0000000..f8e296d --- /dev/null +++ b/common/eurephiadb_session_common.h @@ -0,0 +1,44 @@ +/* eurephiadb_session_common.h -- Common function for handling sessions + * + * GPLv2 - Copyright (C) 2008 David Sommerseth <dazo@users.sourceforge.net> + * + * This program 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 + * of the License. + * + * 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. + * + */ + +#ifndef EUREPHIADB_SESSION_COMMON_H_ +#define EUREPHIADB_SESSION_COMMON_H_ + +#include <eurephia_values.h> + +#define SESSION_NEW 1 +#define SESSION_EXISTING 2 +#define SESSION_REGISTERED 3 +#define SESSION_LOGGEDOUT 4 + +#define SESSVAL_NEW 10 +#define SESSVAL_UPDATE 11 +#define SESSVAL_DELETE 12 + +int eDBset_session_value(eurephiaCTX *ctx, eurephiaSESSION *session, const char *key, const char *val); +#define eDBget_session_value(s, k) eGet_value(s->sessvals, k); + +int eDBsessionGetRandString(eurephiaCTX *ctx, char *rndstr, int len); + +#define eDBfree_session(c, s) { eDBfree_session_func(c, s); s = NULL; } +void eDBfree_session_func(eurephiaCTX *ctx, eurephiaSESSION *sk); + + +#endif /* !EUREPHIADB_SESSION_COMMON_H_ */ diff --git a/database/sqlite/CMakeLists.txt b/database/sqlite/CMakeLists.txt index 8ccc8d3..7569a36 100644 --- a/database/sqlite/CMakeLists.txt +++ b/database/sqlite/CMakeLists.txt @@ -8,7 +8,7 @@ SET(edb_sqlite_SRC ) SET(COMMON ../../common/eurephia_log.c - ../../plugin/eurephiadb_session.c + ../../common/eurephiadb_session_common.c ../../common/eurephia_values.c ../../common/passwd.c ../../common/sha512.c diff --git a/database/sqlite/edb-sqlite.c b/database/sqlite/edb-sqlite.c index d4c0a90..46c2132 100644 --- a/database/sqlite/edb-sqlite.c +++ b/database/sqlite/edb-sqlite.c @@ -31,7 +31,7 @@ #include <eurephia_nullsafe.h> #include <eurephia_log.h> #include <eurephia_values.h> -#include <eurephiadb_session.h> +#include <eurephiadb_session_common.h> #include <passwd.h> #include "sqlite.h" diff --git a/plugin/CMakeLists.txt b/plugin/CMakeLists.txt index 60d221f..8a1b43f 100644 --- a/plugin/CMakeLists.txt +++ b/plugin/CMakeLists.txt @@ -11,6 +11,7 @@ SET(eurephia_auth_SRC ../common/eurephia_getsym.c ../common/eurephia_log.c ../common/eurephia_values.c + ../common/eurephiadb_session_common.c ../common/passwd.c ../common/sha512.c ) diff --git a/plugin/eurephia.c b/plugin/eurephia.c index 4853591..728ddd0 100644 --- a/plugin/eurephia.c +++ b/plugin/eurephia.c @@ -31,6 +31,7 @@ #include <eurephiafw.h> #include <eurephia_nullsafe.h> #include <eurephia_values.h> +#include <eurephiadb_session_common.h> #include <eurephiadb_session.h> #include <certinfo.h> diff --git a/plugin/eurephiadb_session.c b/plugin/eurephiadb_session.c index ab03e37..a9c886c 100644 --- a/plugin/eurephiadb_session.c +++ b/plugin/eurephiadb_session.c @@ -1,4 +1,4 @@ -/* eurephiadb_session.c -- Global API for handling eurephia sessions +/* eurephiadb_session.c -- Functions for handling sessions from eurephia-auth * * GPLv2 - Copyright (C) 2008 David Sommerseth <dazo@users.sourceforge.net> * @@ -22,10 +22,6 @@ #include <stdlib.h> #include <string.h> #include <unistd.h> -#include <sys/types.h> -#include <sys/time.h> -#include <time.h> -#include <openssl/rand.h> #define EUREPHIA_FWINTF #include <eurephiafw_struct.h> @@ -33,7 +29,7 @@ #include "eurephia_nullsafe.h" #include "eurephia_log.h" #include "eurephiadb_session.h" -#include "eurephia_values.h" +#include <eurephiadb_session_common.h> #include "sha512.h" @@ -47,90 +43,6 @@ extern int (*eDBregister_sessionkey) (eurephiaCTX *ctx, const char *seed, const extern eurephiaVALUES *(*eDBload_sessiondata) (eurephiaCTX *ctx, const char *sesskey); -extern int eDBstore_session_value(eurephiaCTX *ctx, eurephiaSESSION *session, int mode, - const char *key, const char *val); - - -// Adds or updates a key in the eurephiaVALUES stack. Database is updated before the stack is updated. -// If database fails, the stack is not updated. -int eDBset_session_value(eurephiaCTX *ctx, eurephiaSESSION *session, const char *key, const char *val) { - eurephiaVALUES *svals = NULL; - - if( (session == NULL) || (key == NULL) ) { - return 0; - } - - DEBUG(ctx, 30, "Function call: eDBset_session_value(ctx, '%s','%s','%s')", - session->sessionkey, key, val); - - // Create a new session value buffer if it does not exist - if( session->sessvals == NULL ) { - session->sessvals = eCreate_value_space(ctx, 10); - if( session->sessvals == NULL ) { - eurephia_log(ctx, LOG_PANIC, 0, "Could not allocate memory for session values"); - return 0; - } - } - - // Check if the session value exists already. If it does update it, or else add it - svals = eGet_valuestruct(session->sessvals, key); - if( (svals == NULL) && (val != NULL) ) { - DEBUG(ctx, 32, "eDBset_session_value ... New session value: %s = '%s'", key, val); - // Add a new session value - if( eDBstore_session_value(ctx, session, SESSVAL_NEW, key, val) ) { - DEBUG(ctx, 32, "eDBset_session_value ... Adding value to value stack: %s = '%s'", - key, val); - // Add value to the stack - eAdd_value(ctx, session->sessvals, key, val); - - DEBUG(ctx, 32, "Registered session variable to session '%s': %s = %s", - session->sessionkey, key, val); - } - } else if( svals != NULL ) { - if( (val != NULL) && (strcmp(svals->val, val) == 0) ) { - DEBUG(ctx, 32, "Session value not changed('%s','%s','%s)", - session->sessionkey, key, val); - return 1; - } - // Update the value in the stack if database is updated without errors - if( eDBstore_session_value(ctx, session,(val != NULL ? SESSVAL_UPDATE : SESSVAL_DELETE), key,val)){ - free_nullsafe(svals->val); - svals->val = strdup_nullsafe(val); - DEBUG(ctx, 32, "Session variable updated in session '%s': %s = %s", - session->sessionkey, key, val); - } - } else if( (svals == NULL) && (val == NULL ) ) { - DEBUG(ctx, 32, "Ignoring saving new session value '%s' == NULL", key); - } - return 1; -} - - -// Generate some random data and return a string. -static int rand_init = 0; -int get_randstring(eurephiaCTX *ctx, char *rndstr, int len) { - int attempts = 0; - do { - if( !rand_init ) { - if( !RAND_load_file("/dev/urandom", 64) ) { - eurephia_log(ctx, LOG_FATAL, 0, "Could not load random data from /dev/urandom"); - return 0; - } - rand_init = 1; - } - - if( RAND_pseudo_bytes((unsigned char *) rndstr, len) ) { - return 1; - } - sleep(1); - rand_init = 0; - } while( attempts++ < 11 ); - eurephia_log(ctx, LOG_FATAL, 0, "RAND_pseudo_bytes() could not generate enough random data"); - return 0; -} - - - // Generates a new session structure. Session key will be created if session seed (input params) are not known. // If session seed is known, the already generated session key will be used. eurephiaSESSION *eDBopen_session_seed(eurephiaCTX *ctx, const char *digest, @@ -235,7 +147,7 @@ eurephiaSESSION *eDBopen_session_seed(eurephiaCTX *ctx, const char *digest, memset(rndstr, 0, (totlen * 2)); rndlen = ((totlen * 2) - strlen_nullsafe(seed) - 2); - if( !get_randstring(ctx, rndstr, rndlen) ) { + if( !eDBsessionGetRandString(ctx, rndstr, rndlen) ) { eurephia_log(ctx, LOG_PANIC, 0, "Could not generate enough random data for session key"); free_nullsafe(new_session->sessionkey); @@ -340,15 +252,3 @@ eurephiaSESSION *eDBopen_session_macaddr(eurephiaCTX *ctx, const char *macaddr) // Return struct which contains the current session return new_session; } - - -// Free up the memory used by a session structure -void eDBfree_session_func(eurephiaCTX *ctx, eurephiaSESSION *session) { - if( session == NULL ) { - return; - } - DEBUG(ctx, 12, "Function call: eDBfree_session(ctx, '%s')", session->sessionkey); - eFree_values(ctx, session->sessvals); - free_nullsafe(session->sessionkey); - free_nullsafe(session); -} diff --git a/plugin/eurephiadb_session.h b/plugin/eurephiadb_session.h index 17271be..b57e579 100644 --- a/plugin/eurephiadb_session.h +++ b/plugin/eurephiadb_session.h @@ -23,21 +23,8 @@ #include <eurephiadb_session_struct.h> -#define SESSION_NEW 1 -#define SESSION_EXISTING 2 -#define SESSION_REGISTERED 3 -#define SESSION_LOGGEDOUT 4 - -#define SESSVAL_NEW 10 -#define SESSVAL_UPDATE 11 -#define SESSVAL_DELETE 12 - -eurephiaVALUES *eDBfind_session_value(eurephiaCTX *ctx, eurephiaSESSION *seskey, const char *key); - int eDBset_session_value(eurephiaCTX *ctx, eurephiaSESSION *sesskey, const char *key, const char *val); -char *eDBget_session_value(eurephiaCTX *ctx, eurephiaSESSION *sesskey, const char *key); - eurephiaSESSION *eDBopen_session_seed(eurephiaCTX *ctx, const char *digest, const char *cname, const char *username, const char *vpnipaddr, const char *vpnipmask, @@ -45,7 +32,4 @@ eurephiaSESSION *eDBopen_session_seed(eurephiaCTX *ctx, const char *digest, eurephiaSESSION *eDBopen_session_macaddr(eurephiaCTX *ctx, const char *macaddr); -#define eDBfree_session(c, s) { eDBfree_session_func(c, s); s = NULL;} -void eDBfree_session_func(eurephiaCTX *ctx, eurephiaSESSION *sk); - #endif /* !EUREPHIADB_SESSION_H_ */ |