--- libaitcfg/src/pq.c 2012/09/18 15:50:59 1.1.2.1 +++ libaitcfg/src/pq.c 2012/09/19 13:02:58 1.1.2.8 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: pq.c,v 1.1.2.1 2012/09/18 15:50:59 misho Exp $ +* $Id: pq.c,v 1.1.2.8 2012/09/19 13:02:58 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -47,7 +47,7 @@ SUCH DAMAGE. static inline struct tagUser * -_selectAttribute(pwd_root_t * __restrict pwd, u_int uid, const char *csName) +_selectPasswd(pwd_root_t * __restrict pwd, u_int uid, const char *csName) { struct tagUser fu; @@ -61,7 +61,7 @@ _selectAttribute(pwd_root_t * __restrict pwd, u_int ui return RB_FIND(tagPWD, pwd, &fu); } - return cfg_findPasswdBy(pwd, PWD_CRIT_UID, uid); + return (struct tagUser*) cfg_findPasswdBy(pwd, PWD_CRIT_UID, uid); } /* --------------------------------------------------------------- */ @@ -74,7 +74,7 @@ _selectAttribute(pwd_root_t * __restrict pwd, u_int ui * @arg1 = Username | UID | GID * return: NULL not found item or error and !=NULL found item */ -struct tagUser * +const struct tagUser * cfg_findPasswdBy(pwd_root_t * __restrict pwd, int criteria, ...) { struct tagUser *u; @@ -83,6 +83,8 @@ cfg_findPasswdBy(pwd_root_t * __restrict pwd, int crit if (!pwd) return NULL; + else + AIT_INIT_VAL(&v); va_start(lst, criteria); switch (criteria) { @@ -107,13 +109,13 @@ cfg_findPasswdBy(pwd_root_t * __restrict pwd, int crit } break; case PWD_CRIT_UID: - if (AIT_GET_U32(&u->usr_uid) == AIT_GET_U32(&v)) { + if (AIT_GET_LIKE(&u->usr_uid, u_int) == AIT_GET_U32(&v)) { AIT_FREE_VAL(&v); return u; } break; case PWD_CRIT_GID: - if (AIT_GET_U32(&u->usr_gid) == AIT_GET_U32(&v)) { + if (AIT_GET_LIKE(&u->usr_gid, u_int) == AIT_GET_U32(&v)) { AIT_FREE_VAL(&v); return u; } @@ -144,10 +146,10 @@ cfg_unsetPasswd(pwd_root_t * __restrict pwd, int crite va_start(lst, criteria); switch (criteria) { case PWD_CRIT_NAME: - u = _selectAttribute(pwd, 0, va_arg(lst, char*)); + u = _selectPasswd(pwd, 0, va_arg(lst, char*)); break; case PWD_CRIT_UID: - u = _selectAttribute(pwd, va_arg(lst, u_int), NULL); + u = _selectPasswd(pwd, va_arg(lst, u_int), NULL); break; default: va_end(lst); @@ -176,65 +178,139 @@ cfg_unsetPasswd(pwd_root_t * __restrict pwd, int crite return 1; } -#if 0 /* - * cfg_setAttribute() - Set item in config or adding new item if not exists + * cfg_setPasswd() - Set item in password or adding new item if not exists * - * @cfg = Config root - * @csSec = Config section //[{csSec}], if NULL set in *default* section - * @csAttr = Config attribute //{csAttr} = ... - * @csVal = Config value //... = {csVal} to setup + * @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_setAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr, const char *csVal) +cfg_setPasswd(pwd_root_t * __restrict pwd, u_char fields, const char *csName, ...) { - struct tagCfg *av, *section; + struct tagUser *u; + register int i; + va_list lst; - if (!cfg || !csAttr) + if (!pwd || !csName) return -1; - av = _selectAttribute(cfg, csSec, csAttr); - if (!av) { + u = _selectPasswd(pwd, 0, csName); + if (!u) { /* adding new element */ - section = _selectAttribute(cfg, csSec, NULL); - - av = io_malloc(sizeof(struct tagCfg)); - if (!av) { - LOGERR; + u = io_malloc(sizeof(struct tagUser)); + if (!u) { + cfg_SetErr(io_GetErrno(), "%s", io_GetError()); return -1; } else { - memset(av, 0, sizeof(struct tagCfg)); - - CFG_RC_LOCK(cfg); - if (!section) - SLIST_INSERT_HEAD(cfg, av, cfg_next); + memset(u, 0, sizeof(struct tagUser)); + if (fields && fields < PWD_MAX_FIELDS) + u->usr_fields = fields; else - SLIST_INSERT_AFTER(section, av, cfg_next); - CFG_RC_UNLOCK(cfg); - } + u->usr_fields = PWD_MAX_FIELDS - 1; - if (csSec && *csSec) { - AIT_SET_STR(&av->cfg_sec, csSec); - AIT_KEY(&av->cfg_sec) = crcFletcher16(AIT_GET_LIKE(&av->cfg_sec, u_short*), - io_align(AIT_LEN(&av->cfg_sec) - 1, 2) / 2); + PWD_LOCK(pwd); + SLIST_INSERT_HEAD(pwd, u, usr_next); + PWD_UNLOCK(pwd); } - AIT_SET_STR(&av->cfg_val, csVal ? csVal : ""); - AIT_SET_STR(&av->cfg_attr, csAttr); - AIT_KEY(&av->cfg_attr) = crcFletcher16(AIT_GET_LIKE(&av->cfg_attr, u_short*), - io_align(AIT_LEN(&av->cfg_attr) - 1, 2) / 2); - CFG_RC_LOCK(cfg); - RB_INSERT(tagRC, cfg, av); - CFG_RC_UNLOCK(cfg); + va_start(lst, csName); + for (i = 0; i < (u->usr_fields + 1); i++) + switch (i) { + case 0: + AIT_SET_STR(&u->usr_name, va_arg(lst, char*)); + 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); - if (csVal && AIT_ADDR(&av->cfg_val) && - strcmp((char*) csVal, (char*) AIT_GET_STR(&av->cfg_val))) { /* Update element */ - AIT_FREE_VAL(&av->cfg_val); - AIT_SET_STR(&av->cfg_val, csVal); + 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; } @@ -243,76 +319,40 @@ cfg_setAttribute(cfg_root_t * __restrict cfg, const ch } /* - * cfg_getAttribute() - Get item from config and return value from it + * cfg_getPasswd() - Get item from passwords and return structure from it * - * @cfg = Config root - * @csSec = Config section //[{csSec}], if NULL unset in *default* section - * @csAttr = Config attribute //{csAttr} = ..., if NULL unset as *any* attribute - * return: NULL item not found or null parameters, !=NULL value const string + * @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 char * -cfg_getAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr) +inline const struct tagUser * +cfg_getPasswd(pwd_root_t * __restrict pwd, int criteria, ...) { - struct tagCfg *av; + struct tagUser *u; + va_list lst; + char *str; - if (!cfg || !csAttr) + if (!pwd) return NULL; - av = _selectAttribute(cfg, csSec, csAttr); - if (!av) - return NULL; - - return AIT_GET_STR(&av->cfg_val); -} - -/* - * cfg_loadAttribute() - Get guarded attribute, if not found item return *default value* - * - * @cfg = Config root - * @csSec = Config section //[{csSec}], if NULL unset in *default* section - * @csAttr = Config attribute //{csAttr} = ... - * @val = Return buffer for item Value //... = {val} - * @csDefValue = *Default Value* for return in //{val}, if not found item in config - * return: 0 item not found, -1 error or >0 number of copied bytes in //{val} - */ -int -cfg_loadAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr, - ait_val_t * __restrict val, const char *csDefValue) -{ - struct tagCfg *av; - int ret = 0; - - if (!cfg || !csAttr || !val) { - cfg_SetErr(EINVAL, "Invalid argument(s)"); - return -1; + 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); - AIT_INIT_VAL(val); - av = _selectAttribute(cfg, csSec, csAttr); - if (!av) { - /* not found item */ - if (csDefValue) { - AIT_SET_STR(val, csDefValue); - ret = AIT_LEN(val); - } else - AIT_INIT_VAL(val); - return ret; - } - - if (AIT_ISEMPTY(&av->cfg_val) || !AIT_ADDR(&av->cfg_val) || - !*AIT_GET_LIKE(&av->cfg_val, char*)) { - /* empty value */ - if (csDefValue) { - AIT_SET_STR(val, csDefValue); - ret = AIT_LEN(val); - } else - AIT_INIT_VAL(val); - } else { - /* copy value */ - AIT_SET_STR(val, AIT_GET_STR(&av->cfg_val)); - ret = AIT_LEN(val); - } - - return ret; + return u; } -#endif