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

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.8     ! misho       6: * $Id: aitcfg.c,v 1.7.6.2 2012/09/18 13:24:50 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.5       misho      15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
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: #include "aitcfg.h"
1.8     ! misho      48: #include "aitpwd.h"
1.1       misho      49: 
                     50: 
                     51: #pragma GCC visibility push(hidden)
                     52: 
1.5       misho      53: int cfg_Errno;
                     54: char cfg_Error[STRSIZ];
                     55: 
                     56: inline int
1.8     ! misho      57: cfg_Write(FILE *f, char *fmt, ...)
        !            58: {
        !            59:        int ret = 0;
        !            60:        va_list lst;
        !            61: 
        !            62:        va_start(lst, fmt);
        !            63:        ret = vfprintf(f, fmt, lst);
        !            64:        va_end(lst);
        !            65: 
        !            66:        return ret;
        !            67: }
        !            68: 
        !            69: inline int
1.5       misho      70: cfg_tree_cmp(struct tagCfg *a, struct tagCfg *b)
                     71: {
                     72:        int ret;
                     73: 
                     74:        assert(a && b);
                     75: 
                     76:        ret = ((AIT_KEY(&a->cfg_sec) << 16) | AIT_KEY(&a->cfg_attr)) - 
                     77:                ((AIT_KEY(&b->cfg_sec) << 16) | AIT_KEY(&b->cfg_attr));
                     78: 
                     79:        if (ret < 0)
                     80:                return -1;
                     81:        else if (ret > 0)
                     82:                return 1;
                     83: 
                     84:        return ret;
                     85: }
                     86: 
                     87: RB_GENERATE(tagRC, tagCfg, cfg_node, cfg_tree_cmp);
1.1       misho      88: 
                     89: #pragma GCC visibility pop
                     90: 
                     91: 
1.5       misho      92: // cfg_GetErrno() Get error code of last operation
                     93: inline int
                     94: cfg_GetErrno()
                     95: {
                     96:        return cfg_Errno;
                     97: }
                     98: 
                     99: // cfg_GetError() Get error text of last operation
                    100: inline const char *
                    101: cfg_GetError()
                    102: {
                    103:        return cfg_Error;
                    104: }
                    105: 
                    106: // cfg_SetErr() Set error to variables for internal use!!!
                    107: inline void
                    108: cfg_SetErr(int eno, char *estr, ...)
                    109: {
                    110:        va_list lst;
                    111: 
                    112:        cfg_Errno = eno;
                    113:        memset(cfg_Error, 0, sizeof cfg_Error);
                    114:        va_start(lst, estr);
                    115:        vsnprintf(cfg_Error, sizeof cfg_Error, estr, lst);
                    116:        va_end(lst);
                    117: }
                    118: 
                    119: 
1.1       misho     120: /*
1.5       misho     121:  * cfgInitConfig() - Init config root
                    122:  *
                    123:  * @cfg = Config root
                    124:  * return: -1 error or 0 ok
                    125:  */
                    126: int
                    127: cfgInitConfig(cfg_root_t * __restrict cfg)
1.1       misho     128: {
                    129:        if (!cfg)
                    130:                return -1;
                    131: 
1.5       misho     132:        pthread_mutex_init(&cfg->rc_mtx, NULL);
                    133: 
                    134:        SLIST_INIT(cfg);
                    135:        RB_INIT(cfg);
1.2       misho     136:        return 0;
1.1       misho     137: }
                    138: 
                    139: /*
1.5       misho     140:  * cfgLoadConfig() - Load config from file
                    141:  *
                    142:  * @cfgName = Config filename
                    143:  * @cfg = Config root
                    144:  * return: -1 error or 0 ok
                    145:  */
                    146: int
                    147: cfgLoadConfig(const char *cfgName, cfg_root_t * __restrict cfg)
1.1       misho     148: {
                    149:        FILE *f;
                    150:        int ret;
                    151: 
1.5       misho     152:        if (!cfgName || !cfg) {
                    153:                cfg_SetErr(EINVAL, "Invalid parameter(s)");
1.1       misho     154:                return -1;
1.5       misho     155:        } else
                    156:                cfgInitConfig(cfg);
1.1       misho     157: 
1.5       misho     158:        f = fopen(cfgName, "r");
                    159:        if (!f) {
1.1       misho     160:                LOGERR;
                    161:                return -1;
                    162:        }
                    163: 
1.5       misho     164:        ret = cfgReadConfig(f, cfg);
1.1       misho     165: 
                    166:        fclose(f);
                    167:        return ret;
                    168: }
                    169: 
                    170: /*
1.7       misho     171:  * cfgClearConfig() - Clear config and free resources
1.5       misho     172:  *
                    173:  * @cfg = Config root
                    174:  * return: none
                    175:  */
                    176: void
1.7       misho     177: cfgClearConfig(cfg_root_t * __restrict cfg)
1.1       misho     178: {
1.5       misho     179:        struct tagCfg *av;
1.1       misho     180: 
1.5       misho     181:        if (!cfg)
1.1       misho     182:                return;
                    183: 
1.5       misho     184:        CFG_RC_LOCK(cfg);
                    185:        while ((av = SLIST_FIRST(cfg))) {
                    186:                SLIST_REMOVE_HEAD(cfg, cfg_next);
                    187: 
                    188:                AIT_FREE_VAL(&av->cfg_val);
                    189:                AIT_FREE_VAL(&av->cfg_attr);
                    190:                AIT_FREE_VAL(&av->cfg_sec);
1.6       misho     191:                io_free(av);
1.1       misho     192:        }
1.5       misho     193:        cfg->rbh_root = NULL;
                    194:        CFG_RC_UNLOCK(cfg);
1.7       misho     195: }
                    196: 
                    197: /*
                    198:  * cfgUnloadConfig() - Unload config from memory and destroy resources
                    199:  *
                    200:  * @cfg = Config root
                    201:  * return: none
                    202:  */
                    203: void
                    204: cfgUnloadConfig(cfg_root_t * __restrict cfg)
                    205: {
                    206:        if (!cfg)
                    207:                return;
1.1       misho     208: 
1.7       misho     209:        cfgClearConfig(cfg);
1.5       misho     210:        pthread_mutex_destroy(&cfg->rc_mtx);
1.1       misho     211: }
                    212: 
1.3       misho     213: /*
1.5       misho     214:  * cfgCreateConfig() - Create config file from memory
                    215:  *
1.3       misho     216:  * @csConfigName = New config filename
1.5       misho     217:  * @cfg = Config root
                    218:  * @whitespace = Additional whitespace characters to file
                    219:  * return: -1 error or 0 ok
                    220:  */
                    221: int
                    222: cfgCreateConfig(const char *csConfigName, cfg_root_t * __restrict cfg, int whitespace)
1.3       misho     223: {
                    224:        FILE *f;
                    225:        int ret;
                    226: 
                    227:        if (!csConfigName || !cfg)
                    228:                return -1;
                    229: 
1.5       misho     230:        f = fopen(csConfigName, "w");
1.3       misho     231:        if (!f) {
                    232:                LOGERR;
                    233:                return -1;
                    234:        }
                    235:        
1.5       misho     236:        ret = cfgWriteConfig(f, cfg, whitespace);
1.3       misho     237: 
                    238:        fclose(f);
                    239:        return ret;
                    240: }
1.8     ! misho     241: 
        !           242: /*
        !           243:  * cfgInitPasswd() - Init password root
        !           244:  *
        !           245:  * @pwd = Password root
        !           246:  * return: -1 error or 0 ok
        !           247:  */
        !           248: int
        !           249: cfgInitPasswd(pwd_root_t * __restrict pwd)
        !           250: {
        !           251:        if (!pwd)
        !           252:                return -1;
        !           253: 
        !           254:        pthread_mutex_init(&pwd->pwd_mtx, NULL);
        !           255: 
        !           256:        SLIST_INIT(pwd);
        !           257:        RB_INIT(pwd);
        !           258:        return 0;
        !           259: }
        !           260: 
        !           261: /*
        !           262:  * cfgLoadPasswd() - Load passwords from file
        !           263:  *
        !           264:  * @pwdName = Passwords filename
        !           265:  * @pwd = Password root
        !           266:  * return: -1 error or 0 ok
        !           267:  */
        !           268: int
        !           269: cfgLoadPasswd(const char *pwdName, pwd_root_t * __restrict pwd)
        !           270: {
        !           271:        FILE *f;
        !           272:        int ret;
        !           273: 
        !           274:        if (!pwdName || !pwd) {
        !           275:                cfg_SetErr(EINVAL, "Invalid parameter(s)");
        !           276:                return -1;
        !           277:        } else
        !           278:                cfgInitPasswd(pwd);
        !           279: 
        !           280:        f = fopen(pwdName, "r");
        !           281:        if (!f) {
        !           282:                LOGERR;
        !           283:                return -1;
        !           284:        }
        !           285: 
        !           286:        ret = cfgReadPasswd(f, pwd);
        !           287: 
        !           288:        fclose(f);
        !           289:        return ret;
        !           290: }
        !           291: 
        !           292: /*
        !           293:  * cfgClearPasswd() - Clear passwords and free resources
        !           294:  *
        !           295:  * @cfg = Password root
        !           296:  * return: none
        !           297:  */
        !           298: void
        !           299: cfgClearPasswd(pwd_root_t * __restrict pwd)
        !           300: {
        !           301:        struct tagUser *p;
        !           302: 
        !           303:        if (!pwd)
        !           304:                return;
        !           305: 
        !           306:        PWD_LOCK(pwd);
        !           307:        while ((p = SLIST_FIRST(pwd))) {
        !           308:                SLIST_REMOVE_HEAD(pwd, usr_next);
        !           309: 
        !           310:                AIT_FREE_VAL(&p->usr_name);
        !           311:                AIT_FREE_VAL(&p->usr_pass);
        !           312:                AIT_FREE_VAL(&p->usr_uid);
        !           313:                AIT_FREE_VAL(&p->usr_gid);
        !           314:                AIT_FREE_VAL(&p->usr_class);
        !           315:                AIT_FREE_VAL(&p->usr_change);
        !           316:                AIT_FREE_VAL(&p->usr_expire);
        !           317:                AIT_FREE_VAL(&p->usr_realm);
        !           318:                AIT_FREE_VAL(&p->usr_home);
        !           319:                AIT_FREE_VAL(&p->usr_shell);
        !           320:                io_free(p);
        !           321:        }
        !           322:        pwd->rbh_root = NULL;
        !           323:        PWD_UNLOCK(pwd);
        !           324: }
        !           325: 
        !           326: /*
        !           327:  * cfgUnloadPasswd() - Unload passwords from memory and destroy resources
        !           328:  *
        !           329:  * @pwd = Password root
        !           330:  * return: none
        !           331:  */
        !           332: void
        !           333: cfgUnloadPasswd(pwd_root_t * __restrict pwd)
        !           334: {
        !           335:        if (!pwd)
        !           336:                return;
        !           337: 
        !           338:        cfgClearPasswd(pwd);
        !           339:        pthread_mutex_destroy(&pwd->pwd_mtx);
        !           340: }
        !           341: 
        !           342: /*
        !           343:  * cfgCreatePasswd() - Create password file from memory
        !           344:  *
        !           345:  * @pwdName = New password filename
        !           346:  * @pwd = Password root
        !           347:  * return: -1 error or 0 ok
        !           348:  */
        !           349: int
        !           350: cfgCreatePasswd(const char *pwdName, pwd_root_t * __restrict pwd)
        !           351: {
        !           352:        FILE *f;
        !           353:        int ret;
        !           354: 
        !           355:        if (!pwdName || !pwd)
        !           356:                return -1;
        !           357: 
        !           358:        f = fopen(pwdName, "w");
        !           359:        if (!f) {
        !           360:                LOGERR;
        !           361:                return -1;
        !           362:        }
        !           363:        
        !           364:        ret = cfgWritePasswd(f, pwd);
        !           365: 
        !           366:        fclose(f);
        !           367:        return ret;
        !           368: }

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