diff options
author | David Sommerseth <dazo@users.sourceforge.net> | 2008-12-01 16:52:42 +0100 |
---|---|---|
committer | David Sommerseth <dazo@users.sourceforge.net> | 2008-12-01 16:52:42 +0100 |
commit | 3710027b43a178c02f4eee439d4d92030466df04 (patch) | |
tree | e4a60c9c6b911551ecff8f89729f9e0db4f5444a | |
parent | 5084b8eb16d6a8c3bae1863f5aa217521c1a12a1 (diff) | |
download | eurephia-3710027b43a178c02f4eee439d4d92030466df04.tar.gz eurephia-3710027b43a178c02f4eee439d4d92030466df04.tar.xz eurephia-3710027b43a178c02f4eee439d4d92030466df04.zip |
Split eurephiadm.c into several files
As the code size on eurephiadm.c have grown quite a lot, it was about
time to split it into categories.
client_config.[ch] Manages unified config file and filenames, including
directory for these files. It also has the possibility
to also override defaults via environment variables.
It also contains a simple config file parser which
puts the config values into an eurephiaVALUES struct.
client_context.[ch] Functions for creating and destroying an eurephia context
needed for proper implementation.
client_session.[ch] Functions for creating and reopening old eurephia sessions,
needed for proper implementation.
Also went through all include statements, to make sure each file do not include
more than absolutely needed.
-rw-r--r-- | eurephiadm/CMakeLists.txt | 3 | ||||
-rw-r--r-- | eurephiadm/client_config.c | 173 | ||||
-rw-r--r-- | eurephiadm/client_config.h | 29 | ||||
-rw-r--r-- | eurephiadm/client_context.c | 76 | ||||
-rw-r--r-- | eurephiadm/client_context.h | 27 | ||||
-rw-r--r-- | eurephiadm/client_session.c | 181 | ||||
-rw-r--r-- | eurephiadm/client_session.h | 30 | ||||
-rw-r--r-- | eurephiadm/eurephiadm.c | 347 |
8 files changed, 523 insertions, 343 deletions
diff --git a/eurephiadm/CMakeLists.txt b/eurephiadm/CMakeLists.txt index 4f4527f..00b798f 100644 --- a/eurephiadm/CMakeLists.txt +++ b/eurephiadm/CMakeLists.txt @@ -4,6 +4,9 @@ cmake_minimum_required(VERSION 2.6) SET(efw_ipt_SRC eurephiadm.c get_console_input.c + client_config.c + client_context.c + client_session.c ../common/eurephia_log.c ../common/eurephia_getsym.c ../common/eurephia_values.c diff --git a/eurephiadm/client_config.c b/eurephiadm/client_config.c new file mode 100644 index 0000000..1652a49 --- /dev/null +++ b/eurephiadm/client_config.c @@ -0,0 +1,173 @@ +/* client_config.c -- Handles reading and parsing of config files + * + * 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 <sys/stat.h> +#include <assert.h> + +#include <eurephia_nullsafe.h> +#include <eurephia_values.h> + + +char *get_config_filename(const char *env, const char *file) { + struct stat chk; + static char fname[1026]; + char *ptr; + int flen = 1024 - strlen_nullsafe(file); + + assert( (file != NULL)); + memset(&fname, 0, 1026); + + if( env != NULL ) { + // Use the explicit eurephia session file, if set in environment + if( (ptr = getenv(env)) != NULL ) { + snprintf(fname, 1024, "%s", ptr); + return fname; + } + } + + // Use the explicit set eurephia session directory for a session file + if( ((ptr = getenv("EUREPHIA_DIR")) != NULL) && (strlen_nullsafe(ptr) <= 1016) ) { + strncat(fname, ptr, flen-1); + strcat(fname, "/"); + strcat(fname, file); + // Make sure we have this directory + if( stat(ptr, &chk) == -1 ) { + if( mkdir(ptr, S_IRWXU) == -1 ) { + fprintf(stderr, "Could not create eurephia directory: %s\n", ptr); + return NULL; + } + } + return fname; + } + + // Use default ~/.eurephia directory for session file + if( ((ptr = getenv("HOME")) != NULL) && (strlen_nullsafe(ptr) <= 1016) ) { + flen -= 10; + strncat(fname, ptr, flen); + strcat(fname, "/.eurephia"); + + // Make sure we have this directory + if( stat(fname, &chk) == -1 ) { + if( mkdir(fname, S_IRWXU) == -1 ) { + fprintf(stderr, "Could not create eurephia directory: %s\n", fname); + return NULL; + } + } + strcat(fname, "/"); + strcat(fname, file); + return fname; + } + return NULL; +} + +eurephiaVALUES *parse_config_line(const char *line) { + char *cp = NULL, *key = NULL, *val = NULL, *ptr = NULL;; + eurephiaVALUES *ret = NULL; + + if( *line == '#' ) { + return NULL; + } + + cp = strdup(line); + key = cp; + val = strpbrk(cp, "="); + if( val == NULL ) { + return NULL; + } + *val = '\0'; val++; + + // Discard comments at the end of a line + if( (ptr = strpbrk(val, "#")) != NULL ) { + *ptr = '\0'; + } + + // Left trim + while( ((*key == 0x20) || (*key == 0x0A) || (*key == 0x0D)) ) { + key++; + } + while( ((*val == 0x20) || (*val == 0x0A) || (*val == 0x0D)) ) { + val++; + } + + // Right trim + ptr = key + strlen_nullsafe(key) - 1; + while( ((*ptr == 0x20) || (*ptr == 0x0A) || (*ptr == 0x0D)) && (ptr > key) ) { + ptr--; + } + ptr++; + *ptr = '\0'; + + ptr = val + strlen_nullsafe(val) - 1; + while( ((*ptr == 0x20) || (*ptr == 0x0A) || (*ptr == 0x0D)) && (ptr > val) ) { + ptr--; + } + ptr++; + *ptr = '\0'; + + // Put key/value into a eurephiaVALUES struct and return it + ret = eCreate_value_space(NULL, 20); + ret->key = strdup(key); + ret->val = strdup(val); + + free_nullsafe(cp); + return ret; +} + + +eurephiaVALUES *ReadConfig(const char *env, const char *cfgname) { + char *fname = NULL; + FILE *fp = NULL; + char *buf = NULL; + eurephiaVALUES *cfg = NULL; + struct stat fi; + + fname = get_config_filename(env, cfgname); + if( fname == NULL ) { + fprintf(stderr, "Could not find a valid path for the config file\n"); + return NULL; + } + + if( stat(fname, &fi) == -1 ) { + fprintf(stderr, "Could not open the config file: %s\n", fname); + return NULL; + } + + if( (fp = fopen(fname, "r")) == NULL ) { + fprintf(stderr, "Could not open the config file: %s\n", fname); + return NULL; + } + + buf = (char *) malloc(fi.st_size+2); + memset(buf, 0, fi.st_size+2); + + cfg = eCreate_value_space(NULL, 20); + while( fgets(buf, fi.st_size, fp) != NULL ) { + eurephiaVALUES *prm = parse_config_line(buf); + if( prm != NULL ) { + eAdd_valuestruct(NULL, cfg, prm); + } + }; + free_nullsafe(buf); + + return cfg; +} diff --git a/eurephiadm/client_config.h b/eurephiadm/client_config.h new file mode 100644 index 0000000..76a0572 --- /dev/null +++ b/eurephiadm/client_config.h @@ -0,0 +1,29 @@ +/* client_config.h -- Handles reading and parsing of config files + * + * 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 EUREPHIA_CLIENT_CONFIG_H +#define EUREPHIA_CLIENT_CONFIG_H + +#include <eurephia_values.h> + +char *get_config_filename(const char *env, const char *file); +eurephiaVALUES *ReadConfig(const char *env, const char *cfgname); + +#endif diff --git a/eurephiadm/client_context.c b/eurephiadm/client_context.c new file mode 100644 index 0000000..adcabcf --- /dev/null +++ b/eurephiadm/client_context.c @@ -0,0 +1,76 @@ +/* context.c -- Handles eurephia contexts used by admin interfaces + * + * 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 <assert.h> + +#include <eurephia_nullsafe.h> +#include <eurephia_context.h> +#include <eurephiadb.h> + + +eurephiaCTX *eurephiaCTX_init(FILE *log, const int loglevel, const char *dbi) { + eurephiaCTX *ctx = NULL; + + ctx = (eurephiaCTX *) malloc(sizeof(eurephiaCTX)+2); + assert(ctx != NULL); + memset(ctx, 0, sizeof(eurephiaCTX)+2); + ctx->context_type = ECTX_ADMIN_CONSOLE; + + ctx->log = log; + ctx->loglevel = loglevel; + + if( !eDBlink_init(ctx, dbi, 2) ){ + eurephia_log(ctx, LOG_PANIC, 0, "Could not load the database driver"); + free_nullsafe(ctx); + return NULL; + } + return ctx; +} + +void eurephiaCTX_destroy(eurephiaCTX *ctx) { + if( ctx == NULL ) { + return; + } + + if( (ctx->dbc != NULL) && (ctx->dbc->dbhandle != NULL) ) { + eDBdisconnect(ctx); + } + + if( ctx->eurephia_driver != NULL ) { + eDBlink_close(ctx); + } + + if( ctx->log != NULL ) { + fflush(ctx->log); + + // Do not close log file if we're on stdout or stderr + if( (ctx->log != stderr) && (ctx->log != stdout) ) { + eurephia_log(ctx, LOG_INFO, 2, "Closing log file"); + fclose(ctx->log); + } + + ctx->log = NULL; + ctx->loglevel = 0; + } + free_nullsafe(ctx); +} diff --git a/eurephiadm/client_context.h b/eurephiadm/client_context.h new file mode 100644 index 0000000..ac0f9a1 --- /dev/null +++ b/eurephiadm/client_context.h @@ -0,0 +1,27 @@ +/* context.h -- Handles eurephia contexts used by admin interfaces + * + * 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 EUREPHIA_CLIENT_CONTEXT +#define EUREPHIA_CLIENT_CONTEXT + +eurephiaCTX *eurephiaCTX_init(FILE *log, const int loglevel, const char *dbi); +void eurephiaCTX_destroy(eurephiaCTX *ctx); + +#endif diff --git a/eurephiadm/client_session.c b/eurephiadm/client_session.c new file mode 100644 index 0000000..c4f9f17 --- /dev/null +++ b/eurephiadm/client_session.c @@ -0,0 +1,181 @@ +/* client_session.c -- Handles eurephia session in admin clients + * + * 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 <sys/stat.h> +#include <unistd.h> +#include <assert.h> + +#include <eurephia_nullsafe.h> +#include <eurephia_context.h> +#include <eurephiadb_session_struct.h> +#include <eurephiadb_session_common.h> +#include <eurephiadb_driver.h> +#include <eurephia_log.h> +#include <sha512.h> + +#include "client_config.h" + + +char *get_session_file() { + char *fname = NULL; + + fname = get_config_filename("EUREPHIADM_SESSION","/session"); + if( fname == NULL ) { + // If even default ~/.eurephia dir failed, create one in /tmp + snprintf(fname, 1024, "/tmp/.eurepia-%i.sess", getuid()); + return fname; + } + + return fname; +} + +char *read_session_file(eurephiaCTX *ctx) { + char *sesskey = NULL, *fname = NULL; + FILE *sfp = NULL; + + fname = get_session_file(); + assert(fname != NULL); + + if( (sfp = fopen(fname, "r")) == NULL ) { + return NULL; + } + + sesskey = (char *) malloc(256); + assert( sesskey != NULL ); + memset(sesskey, 0, 256); + if( (fgets(sesskey, 254, sfp) == NULL) || (strlen_nullsafe(sesskey) < 64) ) { + eurephia_log(ctx, LOG_PANIC, 0, + "Could not read session file (%s). Session value too short", fname); + return NULL; + } + fclose(sfp); + return sesskey; +} + +int write_session_file(eurephiaCTX *ctx, eurephiaSESSION *sess) { + struct stat fchk; + char *fname = NULL; + FILE *sfp = NULL; + + assert((ctx != NULL) && (sess != NULL) && (sess->sessionkey != NULL)); + + fname = get_session_file(); + assert(fname != NULL); + + if( stat(fname, &fchk) != -1 ) { + // session file exists, do not update it + return 1; + } + + if( (sfp = fopen(fname, "w")) == NULL ) { + eurephia_log(ctx, LOG_PANIC, 0, "Could not create session file (%s). Operation aborted.", fname); + return 0; + } + fprintf(sfp, "%s", sess->sessionkey); + fclose(sfp); + return 1; +} + +void remove_session_file(eurephiaCTX *ctx) { + const char *fname = NULL; + + fname = get_session_file(); + assert(fname != NULL); + if( unlink(fname) == -1) { + eurephia_log(ctx, LOG_ERROR, 0, + "Could not remove session file (%s). Following operations might fail", fname); + }; +} + +eurephiaSESSION *create_session(eurephiaCTX *ctx, const char *sesskey) { + eurephiaSESSION *new_sess = NULL; + int loop, uniqchk; + char *randdata = NULL; + unsigned char sha_res[SHA512_HASH_SIZE+2]; + SHA512Context sha; + + new_sess = (eurephiaSESSION *) malloc(sizeof(eurephiaSESSION) + 2); + assert(new_sess != NULL); + memset(new_sess, 0, sizeof(eurephiaSESSION) + 2); + + + if( sesskey == NULL ) { + + // Get data for a unique session key + randdata = (char *) malloc(514); + assert(randdata != NULL); + + do { + char *ptr = NULL; + int i = 0; + + memset(randdata, 0, 514); + if( !eDBsessionGetRandString(ctx, randdata, 512) ) { + eurephia_log(ctx, LOG_FATAL, 0, + "Could not generate enough random data for session"); + free_nullsafe(randdata); + free_nullsafe(new_sess); + return NULL; + } + + memset(&sha, 0, sizeof(SHA512Context)); + memset(&sha_res, 0, sizeof(sha_res)+2); + + free_nullsafe(new_sess->sessionkey); + new_sess->sessionkey = (char *) malloc((SHA512_HASH_SIZE*2) + 3); + assert(new_sess->sessionkey != NULL); + memset(new_sess->sessionkey, 0, (SHA512_HASH_SIZE*2) + 3); + + SHA512Init(&sha); + SHA512Update(&sha, randdata, 512); + SHA512Final(&sha, sha_res); + + ptr = new_sess->sessionkey; + for( i = 0; i < SHA512_HASH_SIZE; i++ ) { + sprintf(ptr, "%02x", sha_res[i]); + ptr++; + } + memset(&sha, 0, sizeof(SHA512Context)); + memset(&sha_res, 0, sizeof(sha_res)); + free_nullsafe(randdata); + + loop++; + uniqchk = eDBcheck_sessionkey_uniqueness(ctx, new_sess->sessionkey); + } while( (uniqchk == 0) && (loop < 11) ); + free_nullsafe(randdata); + + if( uniqchk == 0 ) { + eurephia_log(ctx, LOG_FATAL, 0, + "Did not manage to create a unique session key after %i attemtps. Aborting.", + loop-1); + free_nullsafe(new_sess->sessionkey); + free_nullsafe(new_sess); + return NULL; + } + } else { + new_sess->sessionkey = strdup(sesskey); + new_sess->sessvals = eDBload_sessiondata(ctx, new_sess->sessionkey); + } + // Return new session + return new_sess; +} diff --git a/eurephiadm/client_session.h b/eurephiadm/client_session.h new file mode 100644 index 0000000..335b074 --- /dev/null +++ b/eurephiadm/client_session.h @@ -0,0 +1,30 @@ +/* client_session.h -- Handles eurephia session in admin clients + * + * 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 EUREPHIA_CLIENT_SESSION_H +#define EUREPHIA_CLIENT_SESSION_H + +char *get_session_file(); +char *read_session_file(eurephiaCTX *ctx); +char *write_session_file(eurephiaCTX *ctx, eurephiaSESSION *sess); +char *remove_session_file(eurephiaCTX *ctx); +eurephiaSESSION *create_session(eurephiaCTX *ctx, const char *sesskey); + +#endif diff --git a/eurephiadm/eurephiadm.c b/eurephiadm/eurephiadm.c index 57becc6..551060e 100644 --- a/eurephiadm/eurephiadm.c +++ b/eurephiadm/eurephiadm.c @@ -21,70 +21,23 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <unistd.h> #include <assert.h> -#include <eurephia_context.h> #include <eurephia_nullsafe.h> +#include <eurephia_context.h> #include <eurephia_log.h> #include <eurephia_values.h> #include <eurephiadb_driver.h> #include <eurephiadb_session_common.h> #include <eurephiadb.h> -#include <sha512.h> +#include "client_context.h" +#include "client_config.h" +#include "client_session.h" #include "get_console_input.h" #define MAX_ARGUMENTS 64 -eurephiaCTX *eurephiaCTX_init(FILE *log, const int loglevel, const char *dbi) { - eurephiaCTX *ctx = NULL; - - ctx = (eurephiaCTX *) malloc(sizeof(eurephiaCTX)+2); - assert(ctx != NULL); - memset(ctx, 0, sizeof(eurephiaCTX)+2); - ctx->context_type = ECTX_ADMIN_CONSOLE; - - ctx->log = log; - ctx->loglevel = loglevel; - - if( !eDBlink_init(ctx, dbi, 2) ){ - eurephia_log(ctx, LOG_PANIC, 0, "Could not load the database driver"); - free_nullsafe(ctx); - return NULL; - } - return ctx; -} - -void eurephiaCTX_destroy(eurephiaCTX *ctx) { - if( ctx == NULL ) { - return; - } - - if( (ctx->dbc != NULL) && (ctx->dbc->dbhandle != NULL) ) { - eDBdisconnect(ctx); - } - - if( ctx->eurephia_driver != NULL ) { - eDBlink_close(ctx); - } - - if( ctx->log != NULL ) { - fflush(ctx->log); - - // Do not close log file if we're on stdout or stderr - if( (ctx->log != stderr) && (ctx->log != stdout) ) { - eurephia_log(ctx, LOG_INFO, 2, "Closing log file"); - fclose(ctx->log); - } - - ctx->log = NULL; - ctx->loglevel = 0; - } - free_nullsafe(ctx); -} int eurephia_ConnectDB(eurephiaCTX *ctx, const char *argstr) { char *delims = " "; @@ -116,298 +69,6 @@ int eurephia_ConnectDB(eurephiaCTX *ctx, const char *argstr) { return 1; } -char *get_config_file(const char *env, const char *file) { - struct stat chk; - static char fname[1026]; - char *ptr; - int flen = 1024 - strlen_nullsafe(file); - - assert( (file != NULL)); - memset(&fname, 0, 1026); - - if( env != NULL ) { - // Use the explicit eurephia session file, if set in environment - if( (ptr = getenv(env)) != NULL ) { - snprintf(fname, 1024, "%s", ptr); - return fname; - } - } - - // Use the explicit set eurephia session directory for a session file - if( ((ptr = getenv("EUREPHIA_DIR")) != NULL) && (strlen_nullsafe(ptr) <= 1016) ) { - strncat(fname, ptr, flen-1); - strcat(fname, "/"); - strcat(fname, file); - // Make sure we have this directory - if( stat(ptr, &chk) == -1 ) { - if( mkdir(ptr, S_IRWXU) == -1 ) { - fprintf(stderr, "Could not create eurephia directory: %s\n", ptr); - return NULL; - } - } - return fname; - } - - // Use default ~/.eurephia directory for session file - if( ((ptr = getenv("HOME")) != NULL) && (strlen_nullsafe(ptr) <= 1016) ) { - flen -= 10; - strncat(fname, ptr, flen); - strcat(fname, "/.eurephia"); - - // Make sure we have this directory - if( stat(fname, &chk) == -1 ) { - if( mkdir(fname, S_IRWXU) == -1 ) { - fprintf(stderr, "Could not create eurephia directory: %s\n", fname); - return NULL; - } - } - strcat(fname, "/"); - strcat(fname, file); - return fname; - } - return NULL; -} - -eurephiaVALUES *parse_config_line(const char *line) { - char *cp = NULL, *key = NULL, *val = NULL, *ptr = NULL;; - eurephiaVALUES *ret = NULL; - - if( *line == '#' ) { - return NULL; - } - - cp = strdup(line); - key = cp; - val = strpbrk(cp, "="); - if( val == NULL ) { - return NULL; - } - *val = '\0'; val++; - - // Discard comments at the end of a line - if( (ptr = strpbrk(val, "#")) != NULL ) { - *ptr = '\0'; - } - - // Left trim - while( ((*key == 0x20) || (*key == 0x0A) || (*key == 0x0D)) ) { - key++; - } - while( ((*val == 0x20) || (*val == 0x0A) || (*val == 0x0D)) ) { - val++; - } - - // Right trim - ptr = key + strlen_nullsafe(key) - 1; - while( ((*ptr == 0x20) || (*ptr == 0x0A) || (*ptr == 0x0D)) && (ptr > key) ) { - ptr--; - } - ptr++; - *ptr = '\0'; - - ptr = val + strlen_nullsafe(val) - 1; - while( ((*ptr == 0x20) || (*ptr == 0x0A) || (*ptr == 0x0D)) && (ptr > val) ) { - ptr--; - } - ptr++; - *ptr = '\0'; - - // Put key/value into a eurephiaVALUES struct and return it - ret = eCreate_value_space(NULL, 20); - ret->key = strdup(key); - ret->val = strdup(val); - - free_nullsafe(cp); - return ret; -} - - -eurephiaVALUES *ReadConfig(const char *env, const char *cfgname) { - char *fname = NULL; - FILE *fp = NULL; - char *buf = NULL; - eurephiaVALUES *cfg = NULL; - struct stat fi; - - fname = get_config_file(env, cfgname); - if( fname == NULL ) { - fprintf(stderr, "Could not find a valid path for the config file\n"); - return NULL; - } - - if( stat(fname, &fi) == -1 ) { - fprintf(stderr, "Could not open the config file: %s\n", fname); - return NULL; - } - - if( (fp = fopen(fname, "r")) == NULL ) { - fprintf(stderr, "Could not open the config file: %s\n", fname); - return NULL; - } - - buf = (char *) malloc(fi.st_size+2); - memset(buf, 0, fi.st_size+2); - - cfg = eCreate_value_space(NULL, 20); - while( fgets(buf, fi.st_size, fp) != NULL ) { - eurephiaVALUES *prm = parse_config_line(buf); - if( prm != NULL ) { - eAdd_valuestruct(NULL, cfg, prm); - } - }; - free_nullsafe(buf); - - return cfg; -} - - - - -char *get_session_file() { - char *fname = NULL; - - fname = get_config_file("EUREPHIADM_SESSION","/session"); - if( fname == NULL ) { - // If even default ~/.eurephia dir failed, create one in /tmp - snprintf(fname, 1024, "/tmp/.eurepia-%i.sess", getuid()); - return fname; - } - - return fname; -} - -char *read_session_file(eurephiaCTX *ctx) { - char *sesskey = NULL, *fname = NULL; - FILE *sfp = NULL; - - fname = get_session_file(); - assert(fname != NULL); - - if( (sfp = fopen(fname, "r")) == NULL ) { - return NULL; - } - - sesskey = (char *) malloc(256); - assert( sesskey != NULL ); - memset(sesskey, 0, 256); - if( (fgets(sesskey, 254, sfp) == NULL) || (strlen_nullsafe(sesskey) < 64) ) { - eurephia_log(ctx, LOG_PANIC, 0, - "Could not read session file (%s). Session value too short", fname); - return NULL; - } - fclose(sfp); - return sesskey; -} - -int write_session_file(eurephiaCTX *ctx, eurephiaSESSION *sess) { - struct stat fchk; - char *fname = NULL; - FILE *sfp = NULL; - - assert((ctx != NULL) && (sess != NULL) && (sess->sessionkey != NULL)); - - fname = get_session_file(); - assert(fname != NULL); - - if( stat(fname, &fchk) != -1 ) { - // session file exists, do not update it - return 1; - } - - if( (sfp = fopen(fname, "w")) == NULL ) { - eurephia_log(ctx, LOG_PANIC, 0, "Could not create session file (%s). Operation aborted.", fname); - return 0; - } - fprintf(sfp, "%s", sess->sessionkey); - fclose(sfp); - return 1; -} - -void remove_session_file(eurephiaCTX *ctx) { - const char *fname = NULL; - - fname = get_session_file(); - assert(fname != NULL); - if( unlink(fname) == -1) { - eurephia_log(ctx, LOG_ERROR, 0, - "Could not remove session file (%s). Following operations might fail", fname); - }; -} - -eurephiaSESSION *create_session(eurephiaCTX *ctx, const char *sesskey) { - eurephiaSESSION *new_sess = NULL; - int loop, uniqchk; - char *randdata = NULL; - unsigned char sha_res[SHA512_HASH_SIZE+2]; - SHA512Context sha; - - new_sess = (eurephiaSESSION *) malloc(sizeof(eurephiaSESSION) + 2); - assert(new_sess != NULL); - memset(new_sess, 0, sizeof(eurephiaSESSION) + 2); - - - if( sesskey == NULL ) { - - // Get data for a unique session key - randdata = (char *) malloc(514); - assert(randdata != NULL); - - do { - char *ptr = NULL; - int i = 0; - - memset(randdata, 0, 514); - if( !eDBsessionGetRandString(ctx, randdata, 512) ) { - eurephia_log(ctx, LOG_FATAL, 0, - "Could not generate enough random data for session"); - free_nullsafe(randdata); - free_nullsafe(new_sess); - return NULL; - } - - memset(&sha, 0, sizeof(SHA512Context)); - memset(&sha_res, 0, sizeof(sha_res)+2); - - free_nullsafe(new_sess->sessionkey); - new_sess->sessionkey = (char *) malloc((SHA512_HASH_SIZE*2) + 3); - assert(new_sess->sessionkey != NULL); - memset(new_sess->sessionkey, 0, (SHA512_HASH_SIZE*2) + 3); - - SHA512Init(&sha); - SHA512Update(&sha, randdata, 512); - SHA512Final(&sha, sha_res); - - ptr = new_sess->sessionkey; - for( i = 0; i < SHA512_HASH_SIZE; i++ ) { - sprintf(ptr, "%02x", sha_res[i]); - ptr++; - } - memset(&sha, 0, sizeof(SHA512Context)); - memset(&sha_res, 0, sizeof(sha_res)); - free_nullsafe(randdata); - - loop++; - uniqchk = eDBcheck_sessionkey_uniqueness(ctx, new_sess->sessionkey); - } while( (uniqchk == 0) && (loop < 11) ); - free_nullsafe(randdata); - - if( uniqchk == 0 ) { - eurephia_log(ctx, LOG_FATAL, 0, - "Did not manage to create a unique session key after %i attemtps. Aborting.", - loop-1); - free_nullsafe(new_sess->sessionkey); - free_nullsafe(new_sess); - return NULL; - } - } else { - new_sess->sessionkey = strdup(sesskey); - new_sess->sessvals = eDBload_sessiondata(ctx, new_sess->sessionkey); - } - // Return new session - return new_sess; -} - - eurephiaSESSION *do_login(eurephiaCTX *ctx) { eurephiaSESSION *session = NULL; char username[33], password[33]; |