--- libaitcfg/src/pq.c 2012/09/18 15:50:59 1.1.2.1 +++ libaitcfg/src/pq.c 2012/09/19 11:47:39 1.1.2.2 @@ -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.2 2012/09/19 11:47:39 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; @@ -144,10 +144,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 +176,86 @@ 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 + * @csName = Username + * @csPass = Password + * @uid = UID + * @gid = GID + * @Class = Login class + * @change = Chage date + * @expire = Expire date + * @csRealm = Realm + * @csHome = Home dir + * @csShell = 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, const char *csName, const char *csPass, + u_int uid, u_int gid, const char *csClass, u_int change, u_int expire, + const char *csRealm, const char *csHome, const char *csShell) { - struct tagCfg *av, *section; + struct tagUser *u; - 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)); + memset(u, 0, sizeof(struct tagUser)); - CFG_RC_LOCK(cfg); - if (!section) - SLIST_INSERT_HEAD(cfg, av, cfg_next); - else - SLIST_INSERT_AFTER(section, av, cfg_next); - CFG_RC_UNLOCK(cfg); + PWD_LOCK(pwd); + SLIST_INSERT_HEAD(pwd, u, usr_next); + PWD_UNLOCK(pwd); } - 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); - } - 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); + AIT_SET_STR(&u->usr_name, csName); + AIT_SET_STR(&u->usr_pass, csPass); + AIT_SET_U32(&u->usr_uid, uid); + AIT_SET_U32(&u->usr_gid, gid); + AIT_SET_STR(&u->usr_class, csClass); + AIT_SET_U32(&u->usr_change, change); + AIT_SET_U32(&u->usr_expire, expire); + AIT_SET_STR(&u->usr_realm, csRealm); + AIT_SET_STR(&u->usr_home, csHome); + AIT_SET_STR(&u->usr_shell, csShell); - CFG_RC_LOCK(cfg); - RB_INSERT(tagRC, cfg, av); - CFG_RC_UNLOCK(cfg); + 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); + AIT_SET_STR(&u->usr_pass, csPass); + AIT_SET_U32(&u->usr_uid, uid); + AIT_SET_U32(&u->usr_gid, gid); + AIT_SET_STR(&u->usr_class, csClass); + AIT_SET_U32(&u->usr_change, change); + AIT_SET_U32(&u->usr_expire, expire); + AIT_SET_STR(&u->usr_realm, csRealm); + AIT_SET_STR(&u->usr_home, csHome); + AIT_SET_STR(&u->usr_shell, csShell); return 1; } @@ -243,76 +264,35 @@ 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; - 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: + u = _selectPasswd(pwd, 0, va_arg(lst, char*)); + 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