Annotation of libaitcfg/src/aitcfg.c, revision 1.17.4.1

1.2       misho       1: /*************************************************************************
                      2: * (C) 2008 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
                      3: *  by Michael Pounov <misho@openbsd-bg.org>
                      4: *
                      5: * $Author: misho $
1.17.4.1! misho       6: * $Id: aitcfg.c,v 1.17 2025/08/19 11:43:53 misho Exp $
1.2       misho       7: *
1.4       misho       8: **************************************************************************
                      9: The ELWIX and AITNET software is distributed under the following
                     10: terms:
                     11: 
                     12: All of the documentation and software included in the ELWIX and AITNET
                     13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
                     14: 
1.17.4.1! misho      15: Copyright 2004 - 2026
1.4       misho      16:        by Michael Pounov <misho@elwix.org>.  All rights reserved.
                     17: 
                     18: Redistribution and use in source and binary forms, with or without
                     19: modification, are permitted provided that the following conditions
                     20: are met:
                     21: 1. Redistributions of source code must retain the above copyright
                     22:    notice, this list of conditions and the following disclaimer.
                     23: 2. Redistributions in binary form must reproduce the above copyright
                     24:    notice, this list of conditions and the following disclaimer in the
                     25:    documentation and/or other materials provided with the distribution.
                     26: 3. All advertising materials mentioning features or use of this software
                     27:    must display the following acknowledgement:
                     28: This product includes software developed by Michael Pounov <misho@elwix.org>
                     29: ELWIX - Embedded LightWeight unIX and its contributors.
                     30: 4. Neither the name of AITNET nor the names of its contributors
                     31:    may be used to endorse or promote products derived from this software
                     32:    without specific prior written permission.
                     33: 
                     34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
                     35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     44: SUCH DAMAGE.
                     45: */
1.1       misho      46: #include "global.h"
                     47: 
                     48: 
                     49: #pragma GCC visibility push(hidden)
                     50: 
1.5       misho      51: int cfg_Errno;
                     52: char cfg_Error[STRSIZ];
                     53: 
1.9       misho      54: int
1.8       misho      55: cfg_Write(FILE *f, char *fmt, ...)
                     56: {
                     57:        int ret = 0;
                     58:        va_list lst;
                     59: 
                     60:        va_start(lst, fmt);
                     61:        ret = vfprintf(f, fmt, lst);
                     62:        va_end(lst);
                     63: 
                     64:        return ret;
                     65: }
                     66: 
1.9       misho      67: int
1.5       misho      68: cfg_tree_cmp(struct tagCfg *a, struct tagCfg *b)
                     69: {
                     70:        int ret;
                     71: 
                     72:        assert(a && b);
                     73: 
1.14      misho      74:        ret = ((AIT_KEY(&a->cfg_sec) << 15) | AIT_KEY(&a->cfg_attr)) - 
                     75:                ((AIT_KEY(&b->cfg_sec) << 15) | AIT_KEY(&b->cfg_attr));
1.5       misho      76: 
                     77:        if (ret < 0)
                     78:                return -1;
                     79:        else if (ret > 0)
                     80:                return 1;
                     81: 
                     82:        return ret;
                     83: }
                     84: 
                     85: RB_GENERATE(tagRC, tagCfg, cfg_node, cfg_tree_cmp);
1.1       misho      86: 
                     87: #pragma GCC visibility pop
                     88: 
                     89: 
1.5       misho      90: // cfg_GetErrno() Get error code of last operation
1.9       misho      91: int
1.5       misho      92: cfg_GetErrno()
                     93: {
                     94:        return cfg_Errno;
                     95: }
                     96: 
                     97: // cfg_GetError() Get error text of last operation
1.9       misho      98: const char *
1.5       misho      99: cfg_GetError()
                    100: {
                    101:        return cfg_Error;
                    102: }
                    103: 
                    104: // cfg_SetErr() Set error to variables for internal use!!!
1.9       misho     105: void
1.5       misho     106: cfg_SetErr(int eno, char *estr, ...)
                    107: {
                    108:        va_list lst;
                    109: 
                    110:        cfg_Errno = eno;
                    111:        memset(cfg_Error, 0, sizeof cfg_Error);
                    112:        va_start(lst, estr);
                    113:        vsnprintf(cfg_Error, sizeof cfg_Error, estr, lst);
                    114:        va_end(lst);
                    115: }
                    116: 
                    117: 
1.1       misho     118: /*
1.5       misho     119:  * cfgInitConfig() - Init config root
                    120:  *
1.12      misho     121:  * return: NULL error or !=NULL allocated config root
1.5       misho     122:  */
1.12      misho     123: cfg_root_t *
                    124: cfgInitConfig()
1.1       misho     125: {
1.12      misho     126:        cfg_root_t *cfg = NULL;
                    127: 
                    128:        cfg = e_malloc(sizeof(cfg_root_t));
                    129:        if (!cfg) {
                    130:                cfg_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
                    131:                return NULL;
                    132:        } else
                    133:                memset(cfg, 0, sizeof(cfg_root_t));
1.1       misho     134: 
1.5       misho     135:        pthread_mutex_init(&cfg->rc_mtx, NULL);
                    136: 
1.11      misho     137:        TAILQ_INIT(cfg);
1.5       misho     138:        RB_INIT(cfg);
1.12      misho     139:        return cfg;
                    140: }
                    141: 
                    142: /*
                    143:  * cfgEndConfig() - Free resources & config root
                    144:  *
                    145:  * @pcfg = Config root
                    146:  * return: none
                    147:  */
                    148: void
                    149: cfgEndConfig(cfg_root_t **pcfg)
                    150: {
                    151:        if (pcfg && *pcfg) {
                    152:                cfgClearConfig(*pcfg);
                    153:                pthread_mutex_destroy(&(*pcfg)->rc_mtx);
                    154:                e_free(*pcfg);
                    155:                *pcfg = NULL;
                    156:        }
1.1       misho     157: }
                    158: 
                    159: /*
1.17      misho     160:  * cfgInitConfigExt() - Init existed config root
                    161:  *
                    162:  * @cfg = Config root
                    163:  * return: -1 error or 0 inited config root
                    164:  */
                    165: int
                    166: cfgInitConfigExt(cfg_root_t * __restrict cfg)
                    167: {
                    168:        if (!cfg) {
                    169:                cfg_SetErr(EINVAL, "Invalid parameter");
                    170:                return -1;
                    171:        }
                    172: 
                    173:        memset(cfg, 0, sizeof(cfg_root_t));
                    174: 
                    175:        pthread_mutex_init(&cfg->rc_mtx, NULL);
                    176: 
                    177:        TAILQ_INIT(cfg);
                    178:        RB_INIT(cfg);
                    179:        return 0;
                    180: }
                    181: 
                    182: /*
1.5       misho     183:  * cfgLoadConfig() - Load config from file
                    184:  *
                    185:  * @cfgName = Config filename
                    186:  * @cfg = Config root
                    187:  * return: -1 error or 0 ok
                    188:  */
                    189: int
                    190: cfgLoadConfig(const char *cfgName, cfg_root_t * __restrict cfg)
1.1       misho     191: {
                    192:        FILE *f;
                    193:        int ret;
                    194: 
1.17      misho     195:        if (!cfgName || cfgInitConfigExt(cfg)) {
1.5       misho     196:                cfg_SetErr(EINVAL, "Invalid parameter(s)");
1.1       misho     197:                return -1;
1.12      misho     198:        }
1.1       misho     199: 
1.5       misho     200:        f = fopen(cfgName, "r");
                    201:        if (!f) {
1.1       misho     202:                LOGERR;
                    203:                return -1;
                    204:        }
                    205: 
1.17.4.1! misho     206:        ret = cfg_ReadConfig(f, cfg, 1);
1.1       misho     207: 
                    208:        fclose(f);
                    209:        return ret;
                    210: }
                    211: 
                    212: /*
1.17.4.1! misho     213:  * cfgLoadConfigExt() - Load config from file with recursion level of includes
        !           214:  *
        !           215:  * @cfgName = Config filename
        !           216:  * @cfg = Config root
        !           217:  * @lvl = Level of include recursion
        !           218:  * return: -1 error or 0 ok
        !           219:  */
        !           220: int
        !           221: cfgLoadConfigExt(const char *cfgName, cfg_root_t * __restrict cfg, int lvl)
        !           222: {
        !           223:        FILE *f;
        !           224:        int ret;
        !           225: 
        !           226:        if (!cfgName || cfgInitConfigExt(cfg)) {
        !           227:                cfg_SetErr(EINVAL, "Invalid parameter(s)");
        !           228:                return -1;
        !           229:        }
        !           230: 
        !           231:        f = fopen(cfgName, "r");
        !           232:        if (!f) {
        !           233:                LOGERR;
        !           234:                return -1;
        !           235:        }
        !           236: 
        !           237:        ret = cfg_ReadConfig(f, cfg, lvl);
        !           238: 
        !           239:        fclose(f);
        !           240:        return ret;
        !           241: }
        !           242: /*
1.7       misho     243:  * cfgClearConfig() - Clear config and free resources
1.5       misho     244:  *
                    245:  * @cfg = Config root
                    246:  * return: none
                    247:  */
                    248: void
1.7       misho     249: cfgClearConfig(cfg_root_t * __restrict cfg)
1.1       misho     250: {
1.5       misho     251:        struct tagCfg *av;
1.1       misho     252: 
1.5       misho     253:        if (!cfg)
1.1       misho     254:                return;
                    255: 
1.5       misho     256:        CFG_RC_LOCK(cfg);
1.11      misho     257:        while ((av = TAILQ_FIRST(cfg))) {
                    258:                TAILQ_REMOVE(cfg, av, cfg_next);
1.5       misho     259: 
                    260:                AIT_FREE_VAL(&av->cfg_val);
                    261:                AIT_FREE_VAL(&av->cfg_attr);
                    262:                AIT_FREE_VAL(&av->cfg_sec);
1.9       misho     263:                e_free(av);
1.1       misho     264:        }
1.5       misho     265:        cfg->rbh_root = NULL;
                    266:        CFG_RC_UNLOCK(cfg);
1.7       misho     267: }
                    268: 
                    269: /*
                    270:  * cfgUnloadConfig() - Unload config from memory and destroy resources
                    271:  *
                    272:  * @cfg = Config root
                    273:  * return: none
                    274:  */
                    275: void
                    276: cfgUnloadConfig(cfg_root_t * __restrict cfg)
                    277: {
                    278:        if (!cfg)
                    279:                return;
1.1       misho     280: 
1.7       misho     281:        cfgClearConfig(cfg);
1.5       misho     282:        pthread_mutex_destroy(&cfg->rc_mtx);
1.17      misho     283:        memset(&cfg->rc_mtx, 0, sizeof cfg->rc_mtx);
1.1       misho     284: }
                    285: 
1.3       misho     286: /*
1.5       misho     287:  * cfgCreateConfig() - Create config file from memory
                    288:  *
1.3       misho     289:  * @csConfigName = New config filename
1.5       misho     290:  * @cfg = Config root
                    291:  * @whitespace = Additional whitespace characters to file
                    292:  * return: -1 error or 0 ok
                    293:  */
                    294: int
                    295: cfgCreateConfig(const char *csConfigName, cfg_root_t * __restrict cfg, int whitespace)
1.3       misho     296: {
                    297:        FILE *f;
                    298:        int ret;
                    299: 
                    300:        if (!csConfigName || !cfg)
                    301:                return -1;
                    302: 
1.5       misho     303:        f = fopen(csConfigName, "w");
1.3       misho     304:        if (!f) {
                    305:                LOGERR;
                    306:                return -1;
                    307:        }
                    308:        
1.16      misho     309:        ret = cfgWriteConfigRaw(f, cfg, whitespace);
1.3       misho     310: 
                    311:        fclose(f);
                    312:        return ret;
                    313: }
1.8       misho     314: 
                    315: /*
                    316:  * cfgInitPasswd() - Init password root
                    317:  *
1.12      misho     318:  * return: NULL error or !=NULL allocated password root
1.8       misho     319:  */
1.12      misho     320: pwd_root_t *
                    321: cfgInitPasswd()
1.8       misho     322: {
1.12      misho     323:        pwd_root_t *pwd = NULL;
                    324: 
                    325:        pwd = e_malloc(sizeof(pwd_root_t));
                    326:        if (!pwd) {
                    327:                cfg_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
                    328:                return NULL;
                    329:        } else
                    330:                memset(pwd, 0, sizeof(pwd_root_t));
1.8       misho     331: 
                    332:        pthread_mutex_init(&pwd->pwd_mtx, NULL);
                    333: 
                    334:        SLIST_INIT(pwd);
                    335:        RB_INIT(pwd);
                    336:        return 0;
                    337: }
                    338: 
                    339: /*
1.12      misho     340:  * cfgEndPasswd() - Free resources & password root
                    341:  *
                    342:  * @ppwd = Password root
                    343:  * return: none
                    344:  */
                    345: void
                    346: cfgEndPasswd(pwd_root_t **ppwd)
                    347: {
                    348:        if (ppwd && *ppwd) {
                    349:                cfgClearPasswd(*ppwd);
                    350:                pthread_mutex_destroy(&(*ppwd)->pwd_mtx);
                    351:                e_free(*ppwd);
                    352:                *ppwd = NULL;
                    353:        }
                    354: }
                    355: 
                    356: /*
1.8       misho     357:  * cfgLoadPasswd() - Load passwords from file
                    358:  *
                    359:  * @pwdName = Passwords filename
                    360:  * @pwd = Password root
                    361:  * return: -1 error or 0 ok
                    362:  */
                    363: int
                    364: cfgLoadPasswd(const char *pwdName, pwd_root_t * __restrict pwd)
                    365: {
                    366:        FILE *f;
                    367:        int ret;
                    368: 
                    369:        if (!pwdName || !pwd) {
                    370:                cfg_SetErr(EINVAL, "Invalid parameter(s)");
                    371:                return -1;
1.12      misho     372:        } else {
1.15      misho     373:                memset(pwd, 0, sizeof(pwd_root_t));
                    374: 
1.12      misho     375:                pthread_mutex_init(&pwd->pwd_mtx, NULL);
                    376: 
                    377:                SLIST_INIT(pwd);
                    378:                RB_INIT(pwd);
                    379:        }
1.8       misho     380: 
                    381:        f = fopen(pwdName, "r");
                    382:        if (!f) {
                    383:                LOGERR;
                    384:                return -1;
                    385:        }
                    386: 
                    387:        ret = cfgReadPasswd(f, pwd);
                    388: 
                    389:        fclose(f);
                    390:        return ret;
                    391: }
                    392: 
                    393: /*
                    394:  * cfgClearPasswd() - Clear passwords and free resources
                    395:  *
                    396:  * @cfg = Password root
                    397:  * return: none
                    398:  */
                    399: void
                    400: cfgClearPasswd(pwd_root_t * __restrict pwd)
                    401: {
                    402:        struct tagUser *p;
                    403: 
                    404:        if (!pwd)
                    405:                return;
                    406: 
                    407:        PWD_LOCK(pwd);
                    408:        while ((p = SLIST_FIRST(pwd))) {
                    409:                SLIST_REMOVE_HEAD(pwd, usr_next);
                    410: 
                    411:                AIT_FREE_VAL(&p->usr_name);
                    412:                AIT_FREE_VAL(&p->usr_pass);
                    413:                AIT_FREE_VAL(&p->usr_uid);
                    414:                AIT_FREE_VAL(&p->usr_gid);
                    415:                AIT_FREE_VAL(&p->usr_class);
                    416:                AIT_FREE_VAL(&p->usr_change);
                    417:                AIT_FREE_VAL(&p->usr_expire);
                    418:                AIT_FREE_VAL(&p->usr_realm);
                    419:                AIT_FREE_VAL(&p->usr_home);
                    420:                AIT_FREE_VAL(&p->usr_shell);
1.9       misho     421:                e_free(p);
1.8       misho     422:        }
                    423:        pwd->rbh_root = NULL;
                    424:        PWD_UNLOCK(pwd);
                    425: }
                    426: 
                    427: /*
                    428:  * cfgUnloadPasswd() - Unload passwords from memory and destroy resources
                    429:  *
                    430:  * @pwd = Password root
                    431:  * return: none
                    432:  */
                    433: void
                    434: cfgUnloadPasswd(pwd_root_t * __restrict pwd)
                    435: {
                    436:        if (!pwd)
                    437:                return;
                    438: 
                    439:        cfgClearPasswd(pwd);
                    440:        pthread_mutex_destroy(&pwd->pwd_mtx);
                    441: }
                    442: 
                    443: /*
                    444:  * cfgCreatePasswd() - Create password file from memory
                    445:  *
                    446:  * @pwdName = New password filename
                    447:  * @pwd = Password root
                    448:  * return: -1 error or 0 ok
                    449:  */
                    450: int
                    451: cfgCreatePasswd(const char *pwdName, pwd_root_t * __restrict pwd)
                    452: {
                    453:        FILE *f;
                    454:        int ret;
                    455: 
                    456:        if (!pwdName || !pwd)
                    457:                return -1;
                    458: 
                    459:        f = fopen(pwdName, "w");
                    460:        if (!f) {
                    461:                LOGERR;
                    462:                return -1;
                    463:        }
                    464:        
                    465:        ret = cfgWritePasswd(f, pwd);
                    466: 
                    467:        fclose(f);
                    468:        return ret;
                    469: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>