--- libaitcfg/src/aitcfg.c 2012/07/22 21:54:47 1.6 +++ libaitcfg/src/aitcfg.c 2016/05/18 15:18:10 1.13 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitcfg.c,v 1.6 2012/07/22 21:54:47 misho Exp $ +* $Id: aitcfg.c,v 1.13 2016/05/18 15:18:10 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -12,7 +12,7 @@ terms: All of the documentation and software included in the ELWIX and AITNET Releases is copyrighted by ELWIX - Sofia/Bulgaria -Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 +Copyright 2004 - 2016 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -44,7 +44,6 @@ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF TH SUCH DAMAGE. */ #include "global.h" -#include "aitcfg.h" #pragma GCC visibility push(hidden) @@ -52,7 +51,20 @@ SUCH DAMAGE. int cfg_Errno; char cfg_Error[STRSIZ]; -inline int +int +cfg_Write(FILE *f, char *fmt, ...) +{ + int ret = 0; + va_list lst; + + va_start(lst, fmt); + ret = vfprintf(f, fmt, lst); + va_end(lst); + + return ret; +} + +int cfg_tree_cmp(struct tagCfg *a, struct tagCfg *b) { int ret; @@ -76,21 +88,21 @@ RB_GENERATE(tagRC, tagCfg, cfg_node, cfg_tree_cmp); // cfg_GetErrno() Get error code of last operation -inline int +int cfg_GetErrno() { return cfg_Errno; } // cfg_GetError() Get error text of last operation -inline const char * +const char * cfg_GetError() { return cfg_Error; } // cfg_SetErr() Set error to variables for internal use!!! -inline void +void cfg_SetErr(int eno, char *estr, ...) { va_list lst; @@ -106,23 +118,45 @@ cfg_SetErr(int eno, char *estr, ...) /* * cfgInitConfig() - Init config root * - * @cfg = Config root - * return: -1 error or 0 ok + * return: NULL error or !=NULL allocated config root */ -int -cfgInitConfig(cfg_root_t * __restrict cfg) +cfg_root_t * +cfgInitConfig() { - if (!cfg) - return -1; + cfg_root_t *cfg = NULL; + cfg = e_malloc(sizeof(cfg_root_t)); + if (!cfg) { + cfg_SetErr(elwix_GetErrno(), "%s", elwix_GetError()); + return NULL; + } else + memset(cfg, 0, sizeof(cfg_root_t)); + pthread_mutex_init(&cfg->rc_mtx, NULL); - SLIST_INIT(cfg); + TAILQ_INIT(cfg); RB_INIT(cfg); - return 0; + return cfg; } /* + * cfgEndConfig() - Free resources & config root + * + * @pcfg = Config root + * return: none + */ +void +cfgEndConfig(cfg_root_t **pcfg) +{ + if (pcfg && *pcfg) { + cfgClearConfig(*pcfg); + pthread_mutex_destroy(&(*pcfg)->rc_mtx); + e_free(*pcfg); + *pcfg = NULL; + } +} + +/* * cfgLoadConfig() - Load config from file * * @cfgName = Config filename @@ -138,9 +172,13 @@ cfgLoadConfig(const char *cfgName, cfg_root_t * __rest if (!cfgName || !cfg) { cfg_SetErr(EINVAL, "Invalid parameter(s)"); return -1; - } else - cfgInitConfig(cfg); + } else { + pthread_mutex_init(&cfg->rc_mtx, NULL); + TAILQ_INIT(cfg); + RB_INIT(cfg); + } + f = fopen(cfgName, "r"); if (!f) { LOGERR; @@ -154,13 +192,13 @@ cfgLoadConfig(const char *cfgName, cfg_root_t * __rest } /* - * cfgUnloadConfig() - Unload config from memory and free resources + * cfgClearConfig() - Clear config and free resources * * @cfg = Config root * return: none */ void -cfgUnloadConfig(cfg_root_t * __restrict cfg) +cfgClearConfig(cfg_root_t * __restrict cfg) { struct tagCfg *av; @@ -168,17 +206,31 @@ cfgUnloadConfig(cfg_root_t * __restrict cfg) return; CFG_RC_LOCK(cfg); - while ((av = SLIST_FIRST(cfg))) { - SLIST_REMOVE_HEAD(cfg, cfg_next); + while ((av = TAILQ_FIRST(cfg))) { + TAILQ_REMOVE(cfg, av, cfg_next); AIT_FREE_VAL(&av->cfg_val); AIT_FREE_VAL(&av->cfg_attr); AIT_FREE_VAL(&av->cfg_sec); - io_free(av); + e_free(av); } cfg->rbh_root = NULL; CFG_RC_UNLOCK(cfg); +} +/* + * cfgUnloadConfig() - Unload config from memory and destroy resources + * + * @cfg = Config root + * return: none + */ +void +cfgUnloadConfig(cfg_root_t * __restrict cfg) +{ + if (!cfg) + return; + + cfgClearConfig(cfg); pthread_mutex_destroy(&cfg->rc_mtx); } @@ -206,6 +258,160 @@ cfgCreateConfig(const char *csConfigName, cfg_root_t * } ret = cfgWriteConfig(f, cfg, whitespace); + + fclose(f); + return ret; +} + +/* + * cfgInitPasswd() - Init password root + * + * return: NULL error or !=NULL allocated password root + */ +pwd_root_t * +cfgInitPasswd() +{ + pwd_root_t *pwd = NULL; + + pwd = e_malloc(sizeof(pwd_root_t)); + if (!pwd) { + cfg_SetErr(elwix_GetErrno(), "%s", elwix_GetError()); + return NULL; + } else + memset(pwd, 0, sizeof(pwd_root_t)); + + pthread_mutex_init(&pwd->pwd_mtx, NULL); + + SLIST_INIT(pwd); + RB_INIT(pwd); + return 0; +} + +/* + * cfgEndPasswd() - Free resources & password root + * + * @ppwd = Password root + * return: none + */ +void +cfgEndPasswd(pwd_root_t **ppwd) +{ + if (ppwd && *ppwd) { + cfgClearPasswd(*ppwd); + pthread_mutex_destroy(&(*ppwd)->pwd_mtx); + e_free(*ppwd); + *ppwd = NULL; + } +} + +/* + * cfgLoadPasswd() - Load passwords from file + * + * @pwdName = Passwords filename + * @pwd = Password root + * return: -1 error or 0 ok + */ +int +cfgLoadPasswd(const char *pwdName, pwd_root_t * __restrict pwd) +{ + FILE *f; + int ret; + + if (!pwdName || !pwd) { + cfg_SetErr(EINVAL, "Invalid parameter(s)"); + return -1; + } else { + pthread_mutex_init(&pwd->pwd_mtx, NULL); + + SLIST_INIT(pwd); + RB_INIT(pwd); + } + + f = fopen(pwdName, "r"); + if (!f) { + LOGERR; + return -1; + } + + ret = cfgReadPasswd(f, pwd); + + fclose(f); + return ret; +} + +/* + * cfgClearPasswd() - Clear passwords and free resources + * + * @cfg = Password root + * return: none + */ +void +cfgClearPasswd(pwd_root_t * __restrict pwd) +{ + struct tagUser *p; + + if (!pwd) + return; + + PWD_LOCK(pwd); + while ((p = SLIST_FIRST(pwd))) { + SLIST_REMOVE_HEAD(pwd, usr_next); + + AIT_FREE_VAL(&p->usr_name); + AIT_FREE_VAL(&p->usr_pass); + AIT_FREE_VAL(&p->usr_uid); + AIT_FREE_VAL(&p->usr_gid); + AIT_FREE_VAL(&p->usr_class); + AIT_FREE_VAL(&p->usr_change); + AIT_FREE_VAL(&p->usr_expire); + AIT_FREE_VAL(&p->usr_realm); + AIT_FREE_VAL(&p->usr_home); + AIT_FREE_VAL(&p->usr_shell); + e_free(p); + } + pwd->rbh_root = NULL; + PWD_UNLOCK(pwd); +} + +/* + * cfgUnloadPasswd() - Unload passwords from memory and destroy resources + * + * @pwd = Password root + * return: none + */ +void +cfgUnloadPasswd(pwd_root_t * __restrict pwd) +{ + if (!pwd) + return; + + cfgClearPasswd(pwd); + pthread_mutex_destroy(&pwd->pwd_mtx); +} + +/* + * cfgCreatePasswd() - Create password file from memory + * + * @pwdName = New password filename + * @pwd = Password root + * return: -1 error or 0 ok + */ +int +cfgCreatePasswd(const char *pwdName, pwd_root_t * __restrict pwd) +{ + FILE *f; + int ret; + + if (!pwdName || !pwd) + return -1; + + f = fopen(pwdName, "w"); + if (!f) { + LOGERR; + return -1; + } + + ret = cfgWritePasswd(f, pwd); fclose(f); return ret;