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

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.6 ! misho       6: * $Id: pq.c,v 1.1.2.5 2012/09/19 12:23:12 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: 
1.1.2.3   misho      64:        return (struct tagUser*) cfg_findPasswdBy(pwd, PWD_CRIT_UID, uid);
1.1.2.1   misho      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:  */
1.1.2.3   misho      77: const struct tagUser *
1.1.2.1   misho      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;
1.1.2.6 ! misho      86:        else
        !            87:                AIT_INIT_VAL(&v);
1.1.2.1   misho      88: 
                     89:        va_start(lst, criteria);
                     90:        switch (criteria) {
                     91:                case PWD_CRIT_NAME:
                     92:                        AIT_SET_STR(&v, va_arg(lst, char*));
                     93:                        break;
                     94:                case PWD_CRIT_UID:
                     95:                case PWD_CRIT_GID:
                     96:                        AIT_SET_U32(&v, va_arg(lst, u_int));
                     97:                        break;
                     98:                default:
                     99:                        return NULL;
                    100:        }
                    101:        va_end(lst);
                    102: 
                    103:        SLIST_FOREACH(u, pwd, usr_next)
                    104:                switch (criteria) {
                    105:                        case PWD_CRIT_NAME:
                    106:                                if (!io_cmpVar(&u->usr_name, &v)) {
                    107:                                        AIT_FREE_VAL(&v);
                    108:                                        return u;
                    109:                                }
                    110:                                break;
                    111:                        case PWD_CRIT_UID:
1.1.2.4   misho     112:                                if (AIT_GET_LIKE(&u->usr_uid, u_int) == AIT_GET_U32(&v)) {
1.1.2.1   misho     113:                                        AIT_FREE_VAL(&v);
                    114:                                        return u;
                    115:                                }
                    116:                                break;
                    117:                        case PWD_CRIT_GID:
1.1.2.4   misho     118:                                if (AIT_GET_LIKE(&u->usr_gid, u_int) == AIT_GET_U32(&v)) {
1.1.2.1   misho     119:                                        AIT_FREE_VAL(&v);
                    120:                                        return u;
                    121:                                }
                    122:                                break;
                    123:                }
                    124: 
                    125:        AIT_FREE_VAL(&v);
                    126:        return NULL;
                    127: }
                    128: 
                    129: /*
                    130:  * cfg_unsetPasswd() - Unset item from passwords and free resources
                    131:  *
                    132:  * @pwd = Password root
                    133:  * @criteria = Search criteria [PWD_CRIT_NAME|PWD_CRIT_UID]
                    134:  * @arg1 = Username | UID
                    135:  * return: 0 item not found, -1 error or 1 removed item
                    136:  */
                    137: int
                    138: cfg_unsetPasswd(pwd_root_t * __restrict pwd, int criteria, ...)
                    139: {
                    140:        struct tagUser *u;
                    141:        va_list lst;
                    142: 
                    143:        if (!pwd)
                    144:                return -1;
                    145: 
                    146:        va_start(lst, criteria);
                    147:        switch (criteria) {
                    148:                case PWD_CRIT_NAME:
1.1.2.2   misho     149:                        u = _selectPasswd(pwd, 0, va_arg(lst, char*));
1.1.2.1   misho     150:                        break;
                    151:                case PWD_CRIT_UID:
1.1.2.2   misho     152:                        u = _selectPasswd(pwd, va_arg(lst, u_int), NULL);
1.1.2.1   misho     153:                        break;
                    154:                default:
                    155:                        va_end(lst);
                    156:                        return -1;
                    157:        }
                    158:        va_end(lst);
                    159:        if (!u)
                    160:                return 0;
                    161: 
                    162:        PWD_LOCK(pwd);
                    163:        RB_REMOVE(tagPWD, pwd, u);
                    164:        SLIST_REMOVE(pwd, u, tagUser, usr_next);
                    165:        PWD_UNLOCK(pwd);
                    166: 
                    167:        AIT_FREE_VAL(&u->usr_name);
                    168:        AIT_FREE_VAL(&u->usr_pass);
                    169:        AIT_FREE_VAL(&u->usr_uid);
                    170:        AIT_FREE_VAL(&u->usr_gid);
                    171:        AIT_FREE_VAL(&u->usr_class);
                    172:        AIT_FREE_VAL(&u->usr_change);
                    173:        AIT_FREE_VAL(&u->usr_expire);
                    174:        AIT_FREE_VAL(&u->usr_realm);
                    175:        AIT_FREE_VAL(&u->usr_home);
                    176:        AIT_FREE_VAL(&u->usr_shell);
                    177:        io_free(u);
                    178:        return 1;
                    179: }
                    180: 
                    181: /*
1.1.2.2   misho     182:  * cfg_setPasswd() - Set item in password or adding new item if not exists
1.1.2.1   misho     183:  *
1.1.2.2   misho     184:  * @cfg = Password root
                    185:  * @csName = Username
                    186:  * @csPass = Password
                    187:  * @uid = UID
                    188:  * @gid = GID
                    189:  * @Class = Login class
                    190:  * @change = Chage date
                    191:  * @expire = Expire date
                    192:  * @csRealm = Realm
                    193:  * @csHome = Home dir
                    194:  * @csShell = Shell
1.1.2.1   misho     195:  * return: 0 nothing changed, -1 error, 1 found and updated item or 2 added new item
                    196:  */
                    197: int
1.1.2.2   misho     198: cfg_setPasswd(pwd_root_t * __restrict pwd, const char *csName, const char *csPass, 
                    199:                u_int uid, u_int gid, const char *csClass, u_int change, u_int expire, 
                    200:                const char *csRealm, const char *csHome, const char *csShell)
1.1.2.1   misho     201: {
1.1.2.2   misho     202:        struct tagUser *u;
1.1.2.1   misho     203: 
1.1.2.2   misho     204:        if (!pwd || !csName)
1.1.2.1   misho     205:                return -1;
                    206: 
1.1.2.2   misho     207:        u = _selectPasswd(pwd, 0, csName);
                    208:        if (!u) {
1.1.2.1   misho     209:                /* adding new element */
1.1.2.2   misho     210:                u = io_malloc(sizeof(struct tagUser));
                    211:                if (!u) {
                    212:                        cfg_SetErr(io_GetErrno(), "%s", io_GetError());
1.1.2.1   misho     213:                        return -1;
                    214:                } else {
1.1.2.2   misho     215:                        memset(u, 0, sizeof(struct tagUser));
1.1.2.1   misho     216: 
1.1.2.2   misho     217:                        PWD_LOCK(pwd);
                    218:                        SLIST_INSERT_HEAD(pwd, u, usr_next);
                    219:                        PWD_UNLOCK(pwd);
1.1.2.1   misho     220:                }
                    221: 
1.1.2.2   misho     222:                AIT_SET_STR(&u->usr_name, csName);
                    223:                AIT_SET_STR(&u->usr_pass, csPass);
                    224:                AIT_SET_U32(&u->usr_uid, uid);
                    225:                AIT_SET_U32(&u->usr_gid, gid);
                    226:                AIT_SET_STR(&u->usr_class, csClass);
                    227:                AIT_SET_U32(&u->usr_change, change);
                    228:                AIT_SET_U32(&u->usr_expire, expire);
                    229:                AIT_SET_STR(&u->usr_realm, csRealm);
                    230:                AIT_SET_STR(&u->usr_home, csHome);
                    231:                AIT_SET_STR(&u->usr_shell, csShell);
                    232: 
                    233:                AIT_KEY(&u->usr_name) = crcFletcher16(AIT_GET_LIKE(&u->usr_name, u_short*), 
                    234:                                io_align(AIT_LEN(&u->usr_name) - 1, 2) / 2);
                    235: 
                    236:                PWD_LOCK(pwd);
                    237:                RB_INSERT(tagPWD, pwd, u);
                    238:                PWD_UNLOCK(pwd);
1.1.2.1   misho     239:                return 2;
1.1.2.2   misho     240:        } else {
                    241:                AIT_FREE_VAL(&u->usr_pass);
                    242:                AIT_FREE_VAL(&u->usr_uid);
                    243:                AIT_FREE_VAL(&u->usr_gid);
                    244:                AIT_FREE_VAL(&u->usr_class);
                    245:                AIT_FREE_VAL(&u->usr_change);
                    246:                AIT_FREE_VAL(&u->usr_expire);
                    247:                AIT_FREE_VAL(&u->usr_realm);
                    248:                AIT_FREE_VAL(&u->usr_home);
                    249:                AIT_FREE_VAL(&u->usr_shell);
1.1.2.1   misho     250: 
                    251:                /* Update element */
1.1.2.2   misho     252:                AIT_SET_STR(&u->usr_pass, csPass);
                    253:                AIT_SET_U32(&u->usr_uid, uid);
                    254:                AIT_SET_U32(&u->usr_gid, gid);
                    255:                AIT_SET_STR(&u->usr_class, csClass);
                    256:                AIT_SET_U32(&u->usr_change, change);
                    257:                AIT_SET_U32(&u->usr_expire, expire);
                    258:                AIT_SET_STR(&u->usr_realm, csRealm);
                    259:                AIT_SET_STR(&u->usr_home, csHome);
                    260:                AIT_SET_STR(&u->usr_shell, csShell);
1.1.2.1   misho     261:                return 1;
                    262:        }
                    263: 
                    264:        /* Nothing happens ... found & values is equal! */
                    265:        return 0;
                    266: }
                    267: 
                    268: /*
1.1.2.2   misho     269:  * cfg_getPasswd() - Get item from passwords and return structure from it
1.1.2.1   misho     270:  *
1.1.2.2   misho     271:  * @pwd = Password root
                    272:  * @criteria = Search criteria [PWD_CRIT_NAME|PWD_CRIT_UID]
                    273:  * @arg1 = Username | UID
                    274:  * return: NULL item not found, !=NULL structure found
1.1.2.1   misho     275:  */
1.1.2.2   misho     276: inline const struct tagUser *
                    277: cfg_getPasswd(pwd_root_t * __restrict pwd, int criteria, ...)
1.1.2.1   misho     278: {
1.1.2.2   misho     279:        struct tagUser *u;
                    280:        va_list lst;
1.1.2.5   misho     281:        char *str;
1.1.2.1   misho     282: 
1.1.2.2   misho     283:        if (!pwd)
1.1.2.1   misho     284:                return NULL;
                    285: 
1.1.2.2   misho     286:        va_start(lst, criteria);
                    287:        switch (criteria) {
                    288:                case PWD_CRIT_NAME:
1.1.2.5   misho     289:                        str = va_arg(lst, char*);
                    290:                        if (!str)
                    291:                                u = NULL;
                    292:                        else
                    293:                                u = _selectPasswd(pwd, 0, str);
1.1.2.2   misho     294:                        break;
                    295:                case PWD_CRIT_UID:
                    296:                        u = _selectPasswd(pwd, va_arg(lst, u_int), NULL);
                    297:                        break;
                    298:                default:
                    299:                        u = NULL;
                    300:                        break;
1.1.2.1   misho     301:        }
1.1.2.2   misho     302:        va_end(lst);
1.1.2.1   misho     303: 
1.1.2.2   misho     304:        return u;
1.1.2.1   misho     305: }

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