|
version 1.7.6.2, 2012/09/18 13:24:50
|
version 1.18, 2026/08/04 18:59:27
|
|
Line 12 terms:
|
Line 12 terms:
|
| All of the documentation and software included in the ELWIX and AITNET |
All of the documentation and software included in the ELWIX and AITNET |
| Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org> |
Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org> |
| |
|
| Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 | Copyright 2004 - 2026 |
| by Michael Pounov <misho@elwix.org>. All rights reserved. |
by Michael Pounov <misho@elwix.org>. All rights reserved. |
| |
|
| Redistribution and use in source and binary forms, with or without |
Redistribution and use in source and binary forms, with or without |
|
Line 44 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF TH
|
Line 44 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF TH
|
| SUCH DAMAGE. |
SUCH DAMAGE. |
| */ |
*/ |
| #include "global.h" |
#include "global.h" |
| #include "aitcfg.h" |
|
| #include "aitpwd.h" |
|
| |
|
| |
|
| #pragma GCC visibility push(hidden) |
#pragma GCC visibility push(hidden) |
|
Line 53 SUCH DAMAGE.
|
Line 51 SUCH DAMAGE.
|
| int cfg_Errno; |
int cfg_Errno; |
| char cfg_Error[STRSIZ]; |
char cfg_Error[STRSIZ]; |
| |
|
| inline int | int |
| cfg_Write(FILE *f, char *fmt, ...) |
cfg_Write(FILE *f, char *fmt, ...) |
| { |
{ |
| int ret = 0; |
int ret = 0; |
|
Line 66 cfg_Write(FILE *f, char *fmt, ...)
|
Line 64 cfg_Write(FILE *f, char *fmt, ...)
|
| return ret; |
return ret; |
| } |
} |
| |
|
| inline int | int |
| cfg_tree_cmp(struct tagCfg *a, struct tagCfg *b) |
cfg_tree_cmp(struct tagCfg *a, struct tagCfg *b) |
| { |
{ |
| int ret; |
int ret; |
| |
|
| assert(a && b); |
assert(a && b); |
| |
|
| ret = ((AIT_KEY(&a->cfg_sec) << 16) | AIT_KEY(&a->cfg_attr)) - | ret = ((AIT_KEY(&a->cfg_sec) << 15) | AIT_KEY(&a->cfg_attr)) - |
| ((AIT_KEY(&b->cfg_sec) << 16) | AIT_KEY(&b->cfg_attr)); | ((AIT_KEY(&b->cfg_sec) << 15) | AIT_KEY(&b->cfg_attr)); |
| |
|
| if (ret < 0) |
if (ret < 0) |
| return -1; |
return -1; |
|
Line 90 RB_GENERATE(tagRC, tagCfg, cfg_node, cfg_tree_cmp);
|
Line 88 RB_GENERATE(tagRC, tagCfg, cfg_node, cfg_tree_cmp);
|
| |
|
| |
|
| // cfg_GetErrno() Get error code of last operation |
// cfg_GetErrno() Get error code of last operation |
| inline int | int |
| cfg_GetErrno() |
cfg_GetErrno() |
| { |
{ |
| return cfg_Errno; |
return cfg_Errno; |
| } |
} |
| |
|
| // cfg_GetError() Get error text of last operation |
// cfg_GetError() Get error text of last operation |
| inline const char * | const char * |
| cfg_GetError() |
cfg_GetError() |
| { |
{ |
| return cfg_Error; |
return cfg_Error; |
| } |
} |
| |
|
| // cfg_SetErr() Set error to variables for internal use!!! |
// cfg_SetErr() Set error to variables for internal use!!! |
| inline void | void |
| cfg_SetErr(int eno, char *estr, ...) |
cfg_SetErr(int eno, char *estr, ...) |
| { |
{ |
| va_list lst; |
va_list lst; |
|
Line 120 cfg_SetErr(int eno, char *estr, ...)
|
Line 118 cfg_SetErr(int eno, char *estr, ...)
|
| /* |
/* |
| * cfgInitConfig() - Init config root |
* cfgInitConfig() - Init config root |
| * |
* |
| |
* return: NULL error or !=NULL allocated config root |
| |
*/ |
| |
cfg_root_t * |
| |
cfgInitConfig() |
| |
{ |
| |
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); |
| |
|
| |
TAILQ_INIT(cfg); |
| |
RB_INIT(cfg); |
| |
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; |
| |
} |
| |
} |
| |
|
| |
/* |
| |
* cfgInitConfigExt() - Init existed config root |
| |
* |
| * @cfg = Config root |
* @cfg = Config root |
| * return: -1 error or 0 ok | * return: -1 error or 0 inited config root |
| */ |
*/ |
| int |
int |
| cfgInitConfig(cfg_root_t * __restrict cfg) | cfgInitConfigExt(cfg_root_t * __restrict cfg) |
| { |
{ |
| if (!cfg) | if (!cfg) { |
| | cfg_SetErr(EINVAL, "Invalid parameter"); |
| return -1; |
return -1; |
| |
} |
| |
|
| |
memset(cfg, 0, sizeof(cfg_root_t)); |
| |
|
| pthread_mutex_init(&cfg->rc_mtx, NULL); |
pthread_mutex_init(&cfg->rc_mtx, NULL); |
| |
|
| SLIST_INIT(cfg); | TAILQ_INIT(cfg); |
| RB_INIT(cfg); |
RB_INIT(cfg); |
| return 0; |
return 0; |
| } |
} |
|
Line 149 cfgLoadConfig(const char *cfgName, cfg_root_t * __rest
|
Line 192 cfgLoadConfig(const char *cfgName, cfg_root_t * __rest
|
| FILE *f; |
FILE *f; |
| int ret; |
int ret; |
| |
|
| if (!cfgName || !cfg) { | if (!cfgName || cfgInitConfigExt(cfg)) { |
| cfg_SetErr(EINVAL, "Invalid parameter(s)"); |
cfg_SetErr(EINVAL, "Invalid parameter(s)"); |
| return -1; |
return -1; |
| } else | } |
| cfgInitConfig(cfg); | |
| |
|
| f = fopen(cfgName, "r"); |
f = fopen(cfgName, "r"); |
| if (!f) { |
if (!f) { |
|
Line 161 cfgLoadConfig(const char *cfgName, cfg_root_t * __rest
|
Line 203 cfgLoadConfig(const char *cfgName, cfg_root_t * __rest
|
| return -1; |
return -1; |
| } |
} |
| |
|
| ret = cfgReadConfig(f, cfg); | ret = cfg_ReadConfig(f, cfg, 1); |
| |
|
| fclose(f); |
fclose(f); |
| return ret; |
return ret; |
| } |
} |
| |
|
| /* |
/* |
| |
* cfgLoadConfigExt() - Load config from file with recursion level of includes |
| |
* |
| |
* @cfgName = Config filename |
| |
* @cfg = Config root |
| |
* @lvl = Level of include recursion |
| |
* return: -1 error or 0 ok |
| |
*/ |
| |
int |
| |
cfgLoadConfigExt(const char *cfgName, cfg_root_t * __restrict cfg, int lvl) |
| |
{ |
| |
FILE *f; |
| |
int ret; |
| |
|
| |
if (!cfgName || cfgInitConfigExt(cfg)) { |
| |
cfg_SetErr(EINVAL, "Invalid parameter(s)"); |
| |
return -1; |
| |
} |
| |
|
| |
f = fopen(cfgName, "r"); |
| |
if (!f) { |
| |
LOGERR; |
| |
return -1; |
| |
} |
| |
|
| |
ret = cfg_ReadConfig(f, cfg, lvl); |
| |
|
| |
fclose(f); |
| |
return ret; |
| |
} |
| |
/* |
| * cfgClearConfig() - Clear config and free resources |
* cfgClearConfig() - Clear config and free resources |
| * |
* |
| * @cfg = Config root |
* @cfg = Config root |
|
Line 182 cfgClearConfig(cfg_root_t * __restrict cfg)
|
Line 254 cfgClearConfig(cfg_root_t * __restrict cfg)
|
| return; |
return; |
| |
|
| CFG_RC_LOCK(cfg); |
CFG_RC_LOCK(cfg); |
| while ((av = SLIST_FIRST(cfg))) { | while ((av = TAILQ_FIRST(cfg))) { |
| SLIST_REMOVE_HEAD(cfg, cfg_next); | TAILQ_REMOVE(cfg, av, cfg_next); |
| |
|
| AIT_FREE_VAL(&av->cfg_val); |
AIT_FREE_VAL(&av->cfg_val); |
| AIT_FREE_VAL(&av->cfg_attr); |
AIT_FREE_VAL(&av->cfg_attr); |
| AIT_FREE_VAL(&av->cfg_sec); |
AIT_FREE_VAL(&av->cfg_sec); |
| io_free(av); | e_free(av); |
| } |
} |
| cfg->rbh_root = NULL; |
cfg->rbh_root = NULL; |
| CFG_RC_UNLOCK(cfg); |
CFG_RC_UNLOCK(cfg); |
|
Line 208 cfgUnloadConfig(cfg_root_t * __restrict cfg)
|
Line 280 cfgUnloadConfig(cfg_root_t * __restrict cfg)
|
| |
|
| cfgClearConfig(cfg); |
cfgClearConfig(cfg); |
| pthread_mutex_destroy(&cfg->rc_mtx); |
pthread_mutex_destroy(&cfg->rc_mtx); |
| |
memset(&cfg->rc_mtx, 0, sizeof cfg->rc_mtx); |
| } |
} |
| |
|
| /* |
/* |
|
Line 233 cfgCreateConfig(const char *csConfigName, cfg_root_t *
|
Line 306 cfgCreateConfig(const char *csConfigName, cfg_root_t *
|
| return -1; |
return -1; |
| } |
} |
| |
|
| ret = cfgWriteConfig(f, cfg, whitespace); | ret = cfgWriteConfigRaw(f, cfg, whitespace); |
| |
|
| fclose(f); |
fclose(f); |
| return ret; |
return ret; |
|
Line 242 cfgCreateConfig(const char *csConfigName, cfg_root_t *
|
Line 315 cfgCreateConfig(const char *csConfigName, cfg_root_t *
|
| /* |
/* |
| * cfgInitPasswd() - Init password root |
* cfgInitPasswd() - Init password root |
| * |
* |
| * @pwd = Password root | * return: NULL error or !=NULL allocated password root |
| * return: -1 error or 0 ok | |
| */ |
*/ |
| int | pwd_root_t * |
| cfgInitPasswd(pwd_root_t * __restrict pwd) | cfgInitPasswd() |
| { |
{ |
| if (!pwd) | pwd_root_t *pwd = NULL; |
| return -1; | |
| |
|
| |
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); |
pthread_mutex_init(&pwd->pwd_mtx, NULL); |
| |
|
| SLIST_INIT(pwd); |
SLIST_INIT(pwd); |
|
Line 259 cfgInitPasswd(pwd_root_t * __restrict pwd)
|
Line 337 cfgInitPasswd(pwd_root_t * __restrict pwd)
|
| } |
} |
| |
|
| /* |
/* |
| |
* 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 |
* cfgLoadPasswd() - Load passwords from file |
| * |
* |
| * @pwdName = Passwords filename |
* @pwdName = Passwords filename |
|
Line 274 cfgLoadPasswd(const char *pwdName, pwd_root_t * __rest
|
Line 369 cfgLoadPasswd(const char *pwdName, pwd_root_t * __rest
|
| if (!pwdName || !pwd) { |
if (!pwdName || !pwd) { |
| cfg_SetErr(EINVAL, "Invalid parameter(s)"); |
cfg_SetErr(EINVAL, "Invalid parameter(s)"); |
| return -1; |
return -1; |
| } else | } else { |
| cfgInitPasswd(pwd); | memset(pwd, 0, sizeof(pwd_root_t)); |
| |
|
| |
pthread_mutex_init(&pwd->pwd_mtx, NULL); |
| |
|
| |
SLIST_INIT(pwd); |
| |
RB_INIT(pwd); |
| |
} |
| |
|
| f = fopen(pwdName, "r"); |
f = fopen(pwdName, "r"); |
| if (!f) { |
if (!f) { |
| LOGERR; |
LOGERR; |
|
Line 317 cfgClearPasswd(pwd_root_t * __restrict pwd)
|
Line 418 cfgClearPasswd(pwd_root_t * __restrict pwd)
|
| AIT_FREE_VAL(&p->usr_realm); |
AIT_FREE_VAL(&p->usr_realm); |
| AIT_FREE_VAL(&p->usr_home); |
AIT_FREE_VAL(&p->usr_home); |
| AIT_FREE_VAL(&p->usr_shell); |
AIT_FREE_VAL(&p->usr_shell); |
| io_free(p); | e_free(p); |
| } |
} |
| pwd->rbh_root = NULL; |
pwd->rbh_root = NULL; |
| PWD_UNLOCK(pwd); |
PWD_UNLOCK(pwd); |