--- libaitcfg/src/pq.c 2012/09/18 15:50:59 1.1 +++ libaitcfg/src/pq.c 2012/09/19 15:22:32 1.2 @@ -0,0 +1,371 @@ +/************************************************************************* +* (C) 2010 AITNET ltd - Sofia/Bulgaria - +* by Michael Pounov +* +* $Author: misho $ +* $Id: pq.c,v 1.2 2012/09/19 15:22:32 misho Exp $ +* +************************************************************************** +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 + +Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 + by Michael Pounov . 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 +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" + + +static inline struct tagUser * +_selectPasswd(pwd_root_t * __restrict pwd, u_int uid, const char *csName) +{ + struct tagUser fu; + + if (!pwd) + return NULL; + else + memset(&fu, 0, sizeof fu); + + if (csName) { + io_setlikeVar(&fu.usr_name, string, strlen(csName) + 1, csName); + return RB_FIND(tagPWD, pwd, &fu); + } + + return (struct tagUser*) cfg_findPasswdBy(pwd, PWD_CRIT_UID, uid); +} + +/* --------------------------------------------------------------- */ + +/* + * cfg_findPasswdBy() - Find user by criteria position in list + * + * @pwd = Password root + * @criteria = Search criteria [PWD_CRIT_NAME|PWD_CRIT_UID|PWD_CRIT_GID] + * @arg1 = Username | UID | GID + * return: NULL not found item or error and !=NULL found item + */ +const struct tagUser * +cfg_findPasswdBy(pwd_root_t * __restrict pwd, int criteria, ...) +{ + struct tagUser *u; + va_list lst; + ait_val_t v; + + if (!pwd) + return NULL; + else + AIT_INIT_VAL(&v); + + va_start(lst, criteria); + switch (criteria) { + case PWD_CRIT_NAME: + AIT_SET_STR(&v, va_arg(lst, char*)); + break; + case PWD_CRIT_UID: + case PWD_CRIT_GID: + AIT_SET_U32(&v, va_arg(lst, u_int)); + break; + default: + return NULL; + } + va_end(lst); + + SLIST_FOREACH(u, pwd, usr_next) + switch (criteria) { + case PWD_CRIT_NAME: + if (!io_cmpVar(&u->usr_name, &v)) { + AIT_FREE_VAL(&v); + return u; + } + break; + case PWD_CRIT_UID: + if ((u_int) AIT_RAW(&u->usr_uid) == AIT_GET_U32(&v)) { + AIT_FREE_VAL(&v); + return u; + } + break; + case PWD_CRIT_GID: + if ((u_int) AIT_RAW(&u->usr_gid) == AIT_GET_U32(&v)) { + AIT_FREE_VAL(&v); + return u; + } + break; + } + + AIT_FREE_VAL(&v); + return NULL; +} + +/* + * cfg_unsetPasswd() - Unset item from passwords and free resources + * + * @pwd = Password root + * @criteria = Search criteria [PWD_CRIT_NAME|PWD_CRIT_UID] + * @arg1 = Username | UID + * return: 0 item not found, -1 error or 1 removed item + */ +int +cfg_unsetPasswd(pwd_root_t * __restrict pwd, int criteria, ...) +{ + struct tagUser *u, *u2; + va_list lst; + u_int n = 0; + char *name = NULL; + + if (!pwd) + return -1; + + va_start(lst, criteria); + switch (criteria) { + case PWD_CRIT_UID: + n = va_arg(lst, u_int); + break; + case PWD_CRIT_NAME: + name = va_arg(lst, char*); + if (name) + break; + default: + va_end(lst); + return -1; + } + va_end(lst); + + u = _selectPasswd(pwd, n, name); + if (!u) + return 0; + + PWD_LOCK(pwd); + RB_REMOVE(tagPWD, pwd, u); + SLIST_REMOVE(pwd, u, tagUser, usr_next); + + /* if duplicates exists, then update r/b tree */ + SLIST_FOREACH(u2, pwd, usr_next) + if (!AIT_ISEMPTY(&u2->usr_name) && + !strcmp(AIT_GET_STR(&u->usr_name), AIT_GET_STR(&u2->usr_name))) { + RB_INSERT(tagPWD, pwd, u2); + break; + } + PWD_UNLOCK(pwd); + + AIT_FREE_VAL(&u->usr_name); + AIT_FREE_VAL(&u->usr_pass); + AIT_FREE_VAL(&u->usr_uid); + AIT_FREE_VAL(&u->usr_gid); + AIT_FREE_VAL(&u->usr_class); + AIT_FREE_VAL(&u->usr_change); + AIT_FREE_VAL(&u->usr_expire); + AIT_FREE_VAL(&u->usr_realm); + AIT_FREE_VAL(&u->usr_home); + AIT_FREE_VAL(&u->usr_shell); + io_free(u); + return 1; +} + +/* + * cfg_setPasswd() - Set item in password or adding new item if not exists + * + * @cfg = Password root + * @fields = Meaning continuous field + * @csName = Username + * @arg1 = Password + * @arg2 = UID + * @arg3 = GID + * @arg4 = Login class + * @arg5 = Chage date + * @arg6 = Expire date + * @arg7 = Realm + * @arg8 = Home dir + * @arg9 = Shell + * return: 0 nothing changed, -1 error, 1 found and updated item or 2 added new item + */ +int +cfg_setPasswd(pwd_root_t * __restrict pwd, u_char fields, const char *csName, ...) +{ + struct tagUser *u; + register int i; + va_list lst; + + if (!pwd || !csName) + return -1; + + u = _selectPasswd(pwd, 0, csName); + if (!u) { + /* adding new element */ + u = io_malloc(sizeof(struct tagUser)); + if (!u) { + cfg_SetErr(io_GetErrno(), "%s", io_GetError()); + return -1; + } else { + memset(u, 0, sizeof(struct tagUser)); + if (fields && fields < PWD_MAX_FIELDS) + u->usr_fields = fields; + else + u->usr_fields = PWD_MAX_FIELDS - 1; + + PWD_LOCK(pwd); + SLIST_INSERT_HEAD(pwd, u, usr_next); + PWD_UNLOCK(pwd); + } + + va_start(lst, csName); + for (i = 0; i < (u->usr_fields + 1); i++) + switch (i) { + case 0: + AIT_SET_STR(&u->usr_name, csName); + break; + case 1: + AIT_SET_STR(&u->usr_pass, va_arg(lst, char*)); + break; + case 2: + AIT_SET_U32(&u->usr_uid, va_arg(lst, u_int)); + break; + case 3: + AIT_SET_U32(&u->usr_gid, va_arg(lst, u_int)); + break; + case 4: + AIT_SET_STR(&u->usr_class, va_arg(lst, char*)); + break; + case 5: + AIT_SET_U32(&u->usr_change, va_arg(lst, u_int)); + break; + case 6: + AIT_SET_U32(&u->usr_expire, va_arg(lst, u_int)); + break; + case 7: + AIT_SET_STR(&u->usr_realm, va_arg(lst, char*)); + break; + case 8: + AIT_SET_STR(&u->usr_home, va_arg(lst, char*)); + break; + case 9: + AIT_SET_STR(&u->usr_shell, va_arg(lst, char*)); + break; + } + va_end(lst); + + AIT_KEY(&u->usr_name) = crcFletcher16(AIT_GET_LIKE(&u->usr_name, u_short*), + io_align(AIT_LEN(&u->usr_name) - 1, 2) / 2); + + PWD_LOCK(pwd); + RB_INSERT(tagPWD, pwd, u); + PWD_UNLOCK(pwd); + return 2; + } else { + AIT_FREE_VAL(&u->usr_pass); + AIT_FREE_VAL(&u->usr_uid); + AIT_FREE_VAL(&u->usr_gid); + AIT_FREE_VAL(&u->usr_class); + AIT_FREE_VAL(&u->usr_change); + AIT_FREE_VAL(&u->usr_expire); + AIT_FREE_VAL(&u->usr_realm); + AIT_FREE_VAL(&u->usr_home); + AIT_FREE_VAL(&u->usr_shell); + + /* Update element */ + va_start(lst, csName); + for (i = 1; i < (u->usr_fields + 1); i++) + switch (i) { + case 1: + AIT_SET_STR(&u->usr_pass, va_arg(lst, char*)); + break; + case 2: + AIT_SET_U32(&u->usr_uid, va_arg(lst, u_int)); + break; + case 3: + AIT_SET_U32(&u->usr_gid, va_arg(lst, u_int)); + break; + case 4: + AIT_SET_STR(&u->usr_class, va_arg(lst, char*)); + break; + case 5: + AIT_SET_U32(&u->usr_change, va_arg(lst, u_int)); + break; + case 6: + AIT_SET_U32(&u->usr_expire, va_arg(lst, u_int)); + break; + case 7: + AIT_SET_STR(&u->usr_realm, va_arg(lst, char*)); + break; + case 8: + AIT_SET_STR(&u->usr_home, va_arg(lst, char*)); + break; + case 9: + AIT_SET_STR(&u->usr_shell, va_arg(lst, char*)); + break; + } + va_end(lst); + return 1; + } + + /* Nothing happens ... found & values is equal! */ + return 0; +} + +/* + * cfg_getPasswd() - Get item from passwords and return structure from it + * + * @pwd = Password root + * @criteria = Search criteria [PWD_CRIT_NAME|PWD_CRIT_UID] + * @arg1 = Username | UID + * return: NULL item not found, !=NULL structure found + */ +inline const struct tagUser * +cfg_getPasswd(pwd_root_t * __restrict pwd, int criteria, ...) +{ + struct tagUser *u; + va_list lst; + char *str; + + if (!pwd) + return NULL; + + va_start(lst, criteria); + switch (criteria) { + case PWD_CRIT_NAME: + str = va_arg(lst, char*); + if (!str) + u = NULL; + else + u = _selectPasswd(pwd, 0, str); + break; + case PWD_CRIT_UID: + u = _selectPasswd(pwd, va_arg(lst, u_int), NULL); + break; + default: + u = NULL; + break; + } + va_end(lst); + + return u; +}