--- libaitcfg/src/aitcfg.c 2012/04/03 11:51:52 1.4.4.3 +++ libaitcfg/src/aitcfg.c 2012/09/18 13:24:50 1.7.6.2 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitcfg.c,v 1.4.4.3 2012/04/03 11:51:52 misho Exp $ +* $Id: aitcfg.c,v 1.7.6.2 2012/09/18 13:24:50 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -45,6 +45,7 @@ SUCH DAMAGE. */ #include "global.h" #include "aitcfg.h" +#include "aitpwd.h" #pragma GCC visibility push(hidden) @@ -53,6 +54,19 @@ int cfg_Errno; char cfg_Error[STRSIZ]; inline 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; +} + +inline int cfg_tree_cmp(struct tagCfg *a, struct tagCfg *b) { int ret; @@ -154,13 +168,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; @@ -174,22 +188,38 @@ cfgUnloadConfig(cfg_root_t * __restrict cfg) AIT_FREE_VAL(&av->cfg_val); AIT_FREE_VAL(&av->cfg_attr); AIT_FREE_VAL(&av->cfg_sec); - free(av); + io_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); } -#if 0 /* - * CreateConfig() Create config file from memory + * cfgCreateConfig() - Create config file from memory + * * @csConfigName = New config filename - * @cfg = Head list element - * return: 0 ok; -1 error:: can`t save new config -*/ -int CreateConfig(const char *csConfigName, sl_config * __restrict cfg) + * @cfg = Config root + * @whitespace = Additional whitespace characters to file + * return: -1 error or 0 ok + */ +int +cfgCreateConfig(const char *csConfigName, cfg_root_t * __restrict cfg, int whitespace) { FILE *f; int ret; @@ -197,43 +227,142 @@ int CreateConfig(const char *csConfigName, sl_config * if (!csConfigName || !cfg) return -1; - f = fopen(csConfigName, "wt"); + f = fopen(csConfigName, "w"); if (!f) { LOGERR; return -1; } - ret ^= ret; - ret = WriteConfig(f, cfg); + ret = cfgWriteConfig(f, cfg, whitespace); fclose(f); return ret; } /* - * cfg_CreateConfig() Create config file from memory without whitespaces! - * @csConfigName = New config filename - * @cfg = Head list element - * return: 0 ok; -1 error:: can`t save new config -*/ -int cfg_CreateConfig(const char *csConfigName, sl_config * __restrict cfg) + * cfgInitPasswd() - Init password root + * + * @pwd = Password root + * return: -1 error or 0 ok + */ +int +cfgInitPasswd(pwd_root_t * __restrict pwd) { + if (!pwd) + return -1; + + pthread_mutex_init(&pwd->pwd_mtx, NULL); + + SLIST_INIT(pwd); + RB_INIT(pwd); + return 0; +} + +/* + * 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 (!csConfigName || !cfg) + if (!pwdName || !pwd) { + cfg_SetErr(EINVAL, "Invalid parameter(s)"); return -1; + } else + cfgInitPasswd(pwd); - f = fopen(csConfigName, "wt"); + 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); + io_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 ^= ret; - ret = cfg_WriteConfig(f, cfg); + ret = cfgWritePasswd(f, pwd); fclose(f); return ret; } -#endif