version 1.1, 2008/08/28 13:17:41
|
version 1.15, 2021/11/26 01:15:03
|
Line 1
|
Line 1
|
|
/************************************************************************* |
|
* (C) 2008 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com> |
|
* by Michael Pounov <misho@openbsd-bg.org> |
|
* |
|
* $Author$ |
|
* $Id$ |
|
* |
|
************************************************************************** |
|
The ELWIX and AITNET software is distributed under the following |
|
terms: |
|
|
|
All of the documentation and software included in the ELWIX and AITNET |
|
Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org> |
|
|
|
Copyright 2004 - 2021 |
|
by Michael Pounov <misho@elwix.org>. All rights reserved. |
|
|
|
Redistribution and use in source and binary forms, with or without |
|
modification, are permitted provided that the following conditions |
|
are met: |
|
1. Redistributions of source code must retain the above copyright |
|
notice, this list of conditions and the following disclaimer. |
|
2. Redistributions in binary form must reproduce the above copyright |
|
notice, this list of conditions and the following disclaimer in the |
|
documentation and/or other materials provided with the distribution. |
|
3. All advertising materials mentioning features or use of this software |
|
must display the following acknowledgement: |
|
This product includes software developed by Michael Pounov <misho@elwix.org> |
|
ELWIX - Embedded LightWeight unIX and its contributors. |
|
4. Neither the name of AITNET nor the names of its contributors |
|
may be used to endorse or promote products derived from this software |
|
without specific prior written permission. |
|
|
|
THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND |
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
|
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
|
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
|
SUCH DAMAGE. |
|
*/ |
#include "global.h" |
#include "global.h" |
#include "aitcfg.h" |
|
|
|
|
|
#pragma GCC visibility push(hidden) |
#pragma GCC visibility push(hidden) |
|
|
int cfgErrno; | int cfg_Errno; |
char cfgError[MAX_STR + 1]; | char cfg_Error[STRSIZ]; |
|
|
|
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; |
|
|
|
assert(a && b); |
|
|
|
ret = ((AIT_KEY(&a->cfg_sec) << 15) | AIT_KEY(&a->cfg_attr)) - |
|
((AIT_KEY(&b->cfg_sec) << 15) | AIT_KEY(&b->cfg_attr)); |
|
|
|
if (ret < 0) |
|
return -1; |
|
else if (ret > 0) |
|
return 1; |
|
|
|
return ret; |
|
} |
|
|
|
RB_GENERATE(tagRC, tagCfg, cfg_node, cfg_tree_cmp); |
|
|
#pragma GCC visibility pop |
#pragma GCC visibility pop |
|
|
|
|
|
// cfg_GetErrno() Get error code of last operation |
|
int |
|
cfg_GetErrno() |
|
{ |
|
return cfg_Errno; |
|
} |
|
|
|
// cfg_GetError() Get error text of last operation |
|
const char * |
|
cfg_GetError() |
|
{ |
|
return cfg_Error; |
|
} |
|
|
|
// cfg_SetErr() Set error to variables for internal use!!! |
|
void |
|
cfg_SetErr(int eno, char *estr, ...) |
|
{ |
|
va_list lst; |
|
|
|
cfg_Errno = eno; |
|
memset(cfg_Error, 0, sizeof cfg_Error); |
|
va_start(lst, estr); |
|
vsnprintf(cfg_Error, sizeof cfg_Error, estr, lst); |
|
va_end(lst); |
|
} |
|
|
|
|
/* |
/* |
* InitConfig() Head initializing function for config | * cfgInitConfig() - Init config root |
* @cfg = New head element for init | * |
* return: 0 ok; -1 error:: new head element is null | * return: NULL error or !=NULL allocated config root |
*/ | */ |
inline int InitConfig(sl_config * __restrict cfg) | cfg_root_t * |
| cfgInitConfig() |
{ |
{ |
if (!cfg) | cfg_root_t *cfg = NULL; |
return -1; | |
|
|
return (int) (cfg->slh_first = 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; |
} |
} |
|
|
/* |
/* |
* LoadConfig() Load config from file | * cfgEndConfig() - Free resources & config root |
* @csConfigName = Filename of config | * |
* @cfg = Head list element | * @pcfg = Config root |
* return: 0 ok; -1 error:: can`t load config | * return: none |
*/ | */ |
int LoadConfig(const char *csConfigName, sl_config * __restrict cfg) | 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 |
|
* @cfg = Config root |
|
* return: -1 error or 0 ok |
|
*/ |
|
int |
|
cfgLoadConfig(const char *cfgName, cfg_root_t * __restrict cfg) |
|
{ |
FILE *f; |
FILE *f; |
int ret; |
int ret; |
|
|
if (!csConfigName || !cfg) | if (!cfgName || !cfg) { |
| cfg_SetErr(EINVAL, "Invalid parameter(s)"); |
return -1; |
return -1; |
|
} else { |
|
memset(cfg, 0, sizeof(cfg_root_t)); |
|
|
InitConfig(cfg); | pthread_mutex_init(&cfg->rc_mtx, NULL); |
if (access(csConfigName, R_OK)) { | |
LOGERR; | TAILQ_INIT(cfg); |
return -1; | RB_INIT(cfg); |
} |
} |
|
|
f = fopen(csConfigName, "rt"); | f = fopen(cfgName, "r"); |
if (!f) { |
if (!f) { |
LOGERR; |
LOGERR; |
return -1; |
return -1; |
} |
} |
|
|
ret ^= ret; |
|
ret = ReadConfig(f, cfg); |
|
|
|
|
ret = cfgReadConfig(f, cfg); |
|
|
fclose(f); |
fclose(f); |
return ret; |
return ret; |
} |
} |
|
|
/* |
/* |
* UnloadConfig() Unload config from memory and free resources | * cfgClearConfig() - Clear config and free resources |
* @cfg = Head list element | * |
*/ | * @cfg = Config root |
void UnloadConfig(sl_config * __restrict cfg) | * return: none |
| */ |
| void |
| cfgClearConfig(cfg_root_t * __restrict cfg) |
{ |
{ |
struct tagPair *av; | struct tagCfg *av; |
|
|
if (!cfg->slh_first) | if (!cfg) |
return; |
return; |
|
|
for (av = cfg->slh_first; av; av = av->sle_next) { | CFG_RC_LOCK(cfg); |
cfg->slh_first = cfg->slh_first->sle_next; | while ((av = TAILQ_FIRST(cfg))) { |
| TAILQ_REMOVE(cfg, av, cfg_next); |
|
|
if (av->psValue) | AIT_FREE_VAL(&av->cfg_val); |
free(av->psValue); | AIT_FREE_VAL(&av->cfg_attr); |
if (av->psAttribute) | AIT_FREE_VAL(&av->cfg_sec); |
free(av->psAttribute); | e_free(av); |
if (av->psSection) | |
free(av->psSection); | |
free(av); | |
} |
} |
|
cfg->rbh_root = NULL; |
|
CFG_RC_UNLOCK(cfg); |
} |
} |
|
|
/* |
/* |
* CreateConfig() Create config file from memory | * 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); |
| } |
| |
| /* |
| * cfgCreateConfig() - Create config file from memory |
| * |
* @csConfigName = New config filename |
* @csConfigName = New config filename |
* @cfg = Head list element | * @cfg = Config root |
* return: 0 ok; -1 error:: can`t save new config | * @whitespace = Additional whitespace characters to file |
*/ | * return: -1 error or 0 ok |
int CreateConfig(const char *csConfigName, sl_config * __restrict cfg) | */ |
| int |
| cfgCreateConfig(const char *csConfigName, cfg_root_t * __restrict cfg, int whitespace) |
{ |
{ |
FILE *f; |
FILE *f; |
int ret; |
int ret; |
Line 94 int CreateConfig(const char *csConfigName, sl_config *
|
Line 253 int CreateConfig(const char *csConfigName, sl_config *
|
if (!csConfigName || !cfg) |
if (!csConfigName || !cfg) |
return -1; |
return -1; |
|
|
f = fopen(csConfigName, "wt"); | f = fopen(csConfigName, "w"); |
if (!f) { |
if (!f) { |
LOGERR; |
LOGERR; |
return -1; |
return -1; |
} |
} |
|
|
ret ^= ret; | ret = cfgWriteConfig(f, cfg, whitespace); |
ret = WriteConfig(f, cfg); | |
|
|
fclose(f); |
fclose(f); |
return ret; |
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)); |
// Error maintenance functions ... | if (!pwd) { |
// | cfg_SetErr(elwix_GetErrno(), "%s", elwix_GetError()); |
| return NULL; |
| } else |
| memset(pwd, 0, sizeof(pwd_root_t)); |
|
|
// cfg_GetErrno() Get error code of last operation | pthread_mutex_init(&pwd->pwd_mtx, NULL); |
inline int cfg_GetErrno() | |
| SLIST_INIT(pwd); |
| RB_INIT(pwd); |
| return 0; |
| } |
| |
| /* |
| * cfgEndPasswd() - Free resources & password root |
| * |
| * @ppwd = Password root |
| * return: none |
| */ |
| void |
| cfgEndPasswd(pwd_root_t **ppwd) |
{ |
{ |
return cfgErrno; | if (ppwd && *ppwd) { |
| cfgClearPasswd(*ppwd); |
| pthread_mutex_destroy(&(*ppwd)->pwd_mtx); |
| e_free(*ppwd); |
| *ppwd = NULL; |
| } |
} |
} |
|
|
// cfg_GetError() Get error text of last operation | /* |
inline const char *cfg_GetError() | * 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) |
{ |
{ |
return cfgError; | FILE *f; |
| int ret; |
| |
| if (!pwdName || !pwd) { |
| cfg_SetErr(EINVAL, "Invalid parameter(s)"); |
| return -1; |
| } else { |
| memset(pwd, 0, sizeof(pwd_root_t)); |
| |
| 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; |
} |
} |