Annotation of libaitcfg/src/pq.c, revision 1.1.2.2

1.1.2.1   misho       1: /*************************************************************************
                      2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
                      3: *  by Michael Pounov <misho@openbsd-bg.org>
                      4: *
                      5: * $Author: misho $
1.1.2.2 ! misho       6: * $Id: pq.c,v 1.1.2.1 2012/09/18 15:50:59 misho Exp $
1.1.2.1   misho       7: *
                      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: 
                     15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
                     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: */
                     46: #include "global.h"
                     47: 
                     48: 
                     49: static inline struct tagUser *
1.1.2.2 ! misho      50: _selectPasswd(pwd_root_t * __restrict pwd, u_int uid, const char *csName)
1.1.2.1   misho      51: {
                     52:        struct tagUser fu;
                     53: 
                     54:        if (!pwd)
                     55:                return NULL;
                     56:        else
                     57:                memset(&fu, 0, sizeof fu);
                     58: 
                     59:        if (csName) {
                     60:                io_setlikeVar(&fu.usr_name, string, strlen(csName) + 1, csName);
                     61:                return RB_FIND(tagPWD, pwd, &fu);
                     62:        }
                     63: 
                     64:        return cfg_findPasswdBy(pwd, PWD_CRIT_UID, uid);
                     65: }
                     66: 
                     67: /* --------------------------------------------------------------- */
                     68: 
                     69: /*
                     70:  * cfg_findPasswdBy() - Find user by criteria position in list
                     71:  *
                     72:  * @pwd = Password root
                     73:  * @criteria = Search criteria [PWD_CRIT_NAME|PWD_CRIT_UID|PWD_CRIT_GID]
                     74:  * @arg1 = Username | UID | GID
                     75:  * return: NULL not found item or error and !=NULL found item
                     76:  */
                     77: struct tagUser *
                     78: cfg_findPasswdBy(pwd_root_t * __restrict pwd, int criteria, ...)
                     79: {
                     80:        struct tagUser *u;
                     81:        va_list lst;
                     82:        ait_val_t v;
                     83: 
                     84:        if (!pwd)
                     85:                return NULL;
                     86: 
                     87:        va_start(lst, criteria);
                     88:        switch (criteria) {
                     89:                case PWD_CRIT_NAME:
                     90:                        AIT_SET_STR(&v, va_arg(lst, char*));
                     91:                        break;
                     92:                case PWD_CRIT_UID:
                     93:                case PWD_CRIT_GID:
                     94:                        AIT_SET_U32(&v, va_arg(lst, u_int));
                     95:                        break;
                     96:                default:
                     97:                        return NULL;
                     98:        }
                     99:        va_end(lst);
                    100: 
                    101:        SLIST_FOREACH(u, pwd, usr_next)
                    102:                switch (criteria) {
                    103:                        case PWD_CRIT_NAME:
                    104:                                if (!io_cmpVar(&u->usr_name, &v)) {
                    105:                                        AIT_FREE_VAL(&v);
                    106:                                        return u;
                    107:                                }
                    108:                                break;
                    109:                        case PWD_CRIT_UID:
                    110:                                if (AIT_GET_U32(&u->usr_uid) == AIT_GET_U32(&v)) {
                    111:                                        AIT_FREE_VAL(&v);
                    112:                                        return u;
                    113:                                }
                    114:                                break;
                    115:                        case PWD_CRIT_GID:
                    116:                                if (AIT_GET_U32(&u->usr_gid) == AIT_GET_U32(&v)) {
                    117:                                        AIT_FREE_VAL(&v);
                    118:                                        return u;
                    119:                                }
                    120:                                break;
                    121:                }
                    122: 
                    123:        AIT_FREE_VAL(&v);
                    124:        return NULL;
                    125: }
                    126: 
                    127: /*
                    128:  * cfg_unsetPasswd() - Unset item from passwords and free resources
                    129:  *
                    130:  * @pwd = Password root
                    131:  * @criteria = Search criteria [PWD_CRIT_NAME|PWD_CRIT_UID]
                    132:  * @arg1 = Username | UID
                    133:  * return: 0 item not found, -1 error or 1 removed item
                    134:  */
                    135: int
                    136: cfg_unsetPasswd(pwd_root_t * __restrict pwd, int criteria, ...)
                    137: {
                    138:        struct tagUser *u;
                    139:        va_list lst;
                    140: 
                    141:        if (!pwd)
                    142:                return -1;
                    143: 
                    144:        va_start(lst, criteria);
                    145:        switch (criteria) {
                    146:                case PWD_CRIT_NAME:
1.1.2.2 ! misho     147:                        u = _selectPasswd(pwd, 0, va_arg(lst, char*));
1.1.2.1   misho     148:                        break;
                    149:                case PWD_CRIT_UID:
1.1.2.2 ! misho     150:                        u = _selectPasswd(pwd, va_arg(lst, u_int), NULL);
1.1.2.1   misho     151:                        break;
                    152:                default:
                    153:                        va_end(lst);
                    154:                        return -1;
                    155:        }
                    156:        va_end(lst);
                    157:        if (!u)
                    158:                return 0;
                    159: 
                    160:        PWD_LOCK(pwd);
                    161:        RB_REMOVE(tagPWD, pwd, u);
                    162:        SLIST_REMOVE(pwd, u, tagUser, usr_next);
                    163:        PWD_UNLOCK(pwd);
                    164: 
                    165:        AIT_FREE_VAL(&u->usr_name);
                    166:        AIT_FREE_VAL(&u->usr_pass);
                    167:        AIT_FREE_VAL(&u->usr_uid);
                    168:        AIT_FREE_VAL(&u->usr_gid);
                    169:        AIT_FREE_VAL(&u->usr_class);
                    170:        AIT_FREE_VAL(&u->usr_change);
                    171:        AIT_FREE_VAL(&u->usr_expire);
                    172:        AIT_FREE_VAL(&u->usr_realm);
                    173:        AIT_FREE_VAL(&u->usr_home);
                    174:        AIT_FREE_VAL(&u->usr_shell);
                    175:        io_free(u);
                    176:        return 1;
                    177: }
                    178: 
                    179: /*
1.1.2.2 ! misho     180:  * cfg_setPasswd() - Set item in password or adding new item if not exists
1.1.2.1   misho     181:  *
1.1.2.2 ! misho     182:  * @cfg = Password root
        !           183:  * @csName = Username
        !           184:  * @csPass = Password
        !           185:  * @uid = UID
        !           186:  * @gid = GID
        !           187:  * @Class = Login class
        !           188:  * @change = Chage date
        !           189:  * @expire = Expire date
        !           190:  * @csRealm = Realm
        !           191:  * @csHome = Home dir
        !           192:  * @csShell = Shell
1.1.2.1   misho     193:  * return: 0 nothing changed, -1 error, 1 found and updated item or 2 added new item
                    194:  */
                    195: int
1.1.2.2 ! misho     196: cfg_setPasswd(pwd_root_t * __restrict pwd, const char *csName, const char *csPass, 
        !           197:                u_int uid, u_int gid, const char *csClass, u_int change, u_int expire, 
        !           198:                const char *csRealm, const char *csHome, const char *csShell)
1.1.2.1   misho     199: {
1.1.2.2 ! misho     200:        struct tagUser *u;
1.1.2.1   misho     201: 
1.1.2.2 ! misho     202:        if (!pwd || !csName)
1.1.2.1   misho     203:                return -1;
                    204: 
1.1.2.2 ! misho     205:        u = _selectPasswd(pwd, 0, csName);
        !           206:        if (!u) {
1.1.2.1   misho     207:                /* adding new element */
1.1.2.2 ! misho     208:                u = io_malloc(sizeof(struct tagUser));
        !           209:                if (!u) {
        !           210:                        cfg_SetErr(io_GetErrno(), "%s", io_GetError());
1.1.2.1   misho     211:                        return -1;
                    212:                } else {
1.1.2.2 ! misho     213:                        memset(u, 0, sizeof(struct tagUser));
1.1.2.1   misho     214: 
1.1.2.2 ! misho     215:                        PWD_LOCK(pwd);
        !           216:                        SLIST_INSERT_HEAD(pwd, u, usr_next);
        !           217:                        PWD_UNLOCK(pwd);
1.1.2.1   misho     218:                }
                    219: 
1.1.2.2 ! misho     220:                AIT_SET_STR(&u->usr_name, csName);
        !           221:                AIT_SET_STR(&u->usr_pass, csPass);
        !           222:                AIT_SET_U32(&u->usr_uid, uid);
        !           223:                AIT_SET_U32(&u->usr_gid, gid);
        !           224:                AIT_SET_STR(&u->usr_class, csClass);
        !           225:                AIT_SET_U32(&u->usr_change, change);
        !           226:                AIT_SET_U32(&u->usr_expire, expire);
        !           227:                AIT_SET_STR(&u->usr_realm, csRealm);
        !           228:                AIT_SET_STR(&u->usr_home, csHome);
        !           229:                AIT_SET_STR(&u->usr_shell, csShell);
        !           230: 
        !           231:                AIT_KEY(&u->usr_name) = crcFletcher16(AIT_GET_LIKE(&u->usr_name, u_short*), 
        !           232:                                io_align(AIT_LEN(&u->usr_name) - 1, 2) / 2);
        !           233: 
        !           234:                PWD_LOCK(pwd);
        !           235:                RB_INSERT(tagPWD, pwd, u);
        !           236:                PWD_UNLOCK(pwd);
1.1.2.1   misho     237:                return 2;
1.1.2.2 ! misho     238:        } else {
        !           239:                AIT_FREE_VAL(&u->usr_pass);
        !           240:                AIT_FREE_VAL(&u->usr_uid);
        !           241:                AIT_FREE_VAL(&u->usr_gid);
        !           242:                AIT_FREE_VAL(&u->usr_class);
        !           243:                AIT_FREE_VAL(&u->usr_change);
        !           244:                AIT_FREE_VAL(&u->usr_expire);
        !           245:                AIT_FREE_VAL(&u->usr_realm);
        !           246:                AIT_FREE_VAL(&u->usr_home);
        !           247:                AIT_FREE_VAL(&u->usr_shell);
1.1.2.1   misho     248: 
                    249:                /* Update element */
1.1.2.2 ! misho     250:                AIT_SET_STR(&u->usr_pass, csPass);
        !           251:                AIT_SET_U32(&u->usr_uid, uid);
        !           252:                AIT_SET_U32(&u->usr_gid, gid);
        !           253:                AIT_SET_STR(&u->usr_class, csClass);
        !           254:                AIT_SET_U32(&u->usr_change, change);
        !           255:                AIT_SET_U32(&u->usr_expire, expire);
        !           256:                AIT_SET_STR(&u->usr_realm, csRealm);
        !           257:                AIT_SET_STR(&u->usr_home, csHome);
        !           258:                AIT_SET_STR(&u->usr_shell, csShell);
1.1.2.1   misho     259:                return 1;
                    260:        }
                    261: 
                    262:        /* Nothing happens ... found & values is equal! */
                    263:        return 0;
                    264: }
                    265: 
                    266: /*
1.1.2.2 ! misho     267:  * cfg_getPasswd() - Get item from passwords and return structure from it
1.1.2.1   misho     268:  *
1.1.2.2 ! misho     269:  * @pwd = Password root
        !           270:  * @criteria = Search criteria [PWD_CRIT_NAME|PWD_CRIT_UID]
        !           271:  * @arg1 = Username | UID
        !           272:  * return: NULL item not found, !=NULL structure found
1.1.2.1   misho     273:  */
1.1.2.2 ! misho     274: inline const struct tagUser *
        !           275: cfg_getPasswd(pwd_root_t * __restrict pwd, int criteria, ...)
1.1.2.1   misho     276: {
1.1.2.2 ! misho     277:        struct tagUser *u;
        !           278:        va_list lst;
1.1.2.1   misho     279: 
1.1.2.2 ! misho     280:        if (!pwd)
1.1.2.1   misho     281:                return NULL;
                    282: 
1.1.2.2 ! misho     283:        va_start(lst, criteria);
        !           284:        switch (criteria) {
        !           285:                case PWD_CRIT_NAME:
        !           286:                        u = _selectPasswd(pwd, 0, va_arg(lst, char*));
        !           287:                        break;
        !           288:                case PWD_CRIT_UID:
        !           289:                        u = _selectPasswd(pwd, va_arg(lst, u_int), NULL);
        !           290:                        break;
        !           291:                default:
        !           292:                        u = NULL;
        !           293:                        break;
1.1.2.1   misho     294:        }
1.1.2.2 ! misho     295:        va_end(lst);
1.1.2.1   misho     296: 
1.1.2.2 ! misho     297:        return u;
1.1.2.1   misho     298: }

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