version 1.1.2.1, 2012/09/18 15:50:59
|
version 1.1.2.6, 2012/09/19 12:25:16
|
Line 47 SUCH DAMAGE.
|
Line 47 SUCH DAMAGE.
|
|
|
|
|
static inline struct tagUser * |
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; |
struct tagUser fu; |
|
|
Line 61 _selectAttribute(pwd_root_t * __restrict pwd, u_int ui
|
Line 61 _selectAttribute(pwd_root_t * __restrict pwd, u_int ui
|
return RB_FIND(tagPWD, pwd, &fu); |
return RB_FIND(tagPWD, pwd, &fu); |
} |
} |
|
|
return cfg_findPasswdBy(pwd, PWD_CRIT_UID, uid); | return (struct tagUser*) cfg_findPasswdBy(pwd, PWD_CRIT_UID, uid); |
} |
} |
|
|
/* --------------------------------------------------------------- */ |
/* --------------------------------------------------------------- */ |
Line 74 _selectAttribute(pwd_root_t * __restrict pwd, u_int ui
|
Line 74 _selectAttribute(pwd_root_t * __restrict pwd, u_int ui
|
* @arg1 = Username | UID | GID |
* @arg1 = Username | UID | GID |
* return: NULL not found item or error and !=NULL found item |
* 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, ...) |
cfg_findPasswdBy(pwd_root_t * __restrict pwd, int criteria, ...) |
{ |
{ |
struct tagUser *u; |
struct tagUser *u; |
Line 83 cfg_findPasswdBy(pwd_root_t * __restrict pwd, int crit
|
Line 83 cfg_findPasswdBy(pwd_root_t * __restrict pwd, int crit
|
|
|
if (!pwd) |
if (!pwd) |
return NULL; |
return NULL; |
|
else |
|
AIT_INIT_VAL(&v); |
|
|
va_start(lst, criteria); |
va_start(lst, criteria); |
switch (criteria) { |
switch (criteria) { |
Line 107 cfg_findPasswdBy(pwd_root_t * __restrict pwd, int crit
|
Line 109 cfg_findPasswdBy(pwd_root_t * __restrict pwd, int crit
|
} |
} |
break; |
break; |
case PWD_CRIT_UID: |
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); |
AIT_FREE_VAL(&v); |
return u; |
return u; |
} |
} |
break; |
break; |
case PWD_CRIT_GID: |
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); |
AIT_FREE_VAL(&v); |
return u; |
return u; |
} |
} |
Line 144 cfg_unsetPasswd(pwd_root_t * __restrict pwd, int crite
|
Line 146 cfg_unsetPasswd(pwd_root_t * __restrict pwd, int crite
|
va_start(lst, criteria); |
va_start(lst, criteria); |
switch (criteria) { |
switch (criteria) { |
case PWD_CRIT_NAME: |
case PWD_CRIT_NAME: |
u = _selectAttribute(pwd, 0, va_arg(lst, char*)); | u = _selectPasswd(pwd, 0, va_arg(lst, char*)); |
break; |
break; |
case PWD_CRIT_UID: |
case PWD_CRIT_UID: |
u = _selectAttribute(pwd, va_arg(lst, u_int), NULL); | u = _selectPasswd(pwd, va_arg(lst, u_int), NULL); |
break; |
break; |
default: |
default: |
va_end(lst); |
va_end(lst); |
Line 176 cfg_unsetPasswd(pwd_root_t * __restrict pwd, int crite
|
Line 178 cfg_unsetPasswd(pwd_root_t * __restrict pwd, int crite
|
return 1; |
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 | * @cfg = Password root |
* @csSec = Config section //[{csSec}], if NULL set in *default* section | * @csName = Username |
* @csAttr = Config attribute //{csAttr} = ... | * @csPass = Password |
* @csVal = Config value //... = {csVal} to setup | * @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 |
* return: 0 nothing changed, -1 error, 1 found and updated item or 2 added new item |
*/ |
*/ |
int |
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; |
return -1; |
|
|
av = _selectAttribute(cfg, csSec, csAttr); | u = _selectPasswd(pwd, 0, csName); |
if (!av) { | if (!u) { |
/* adding new element */ |
/* adding new element */ |
section = _selectAttribute(cfg, csSec, NULL); | u = io_malloc(sizeof(struct tagUser)); |
| if (!u) { |
av = io_malloc(sizeof(struct tagCfg)); | cfg_SetErr(io_GetErrno(), "%s", io_GetError()); |
if (!av) { | |
LOGERR; | |
return -1; |
return -1; |
} else { |
} else { |
memset(av, 0, sizeof(struct tagCfg)); | memset(u, 0, sizeof(struct tagUser)); |
|
|
CFG_RC_LOCK(cfg); | PWD_LOCK(pwd); |
if (!section) | SLIST_INSERT_HEAD(pwd, u, usr_next); |
SLIST_INSERT_HEAD(cfg, av, cfg_next); | PWD_UNLOCK(pwd); |
else | |
SLIST_INSERT_AFTER(section, av, cfg_next); | |
CFG_RC_UNLOCK(cfg); | |
} |
} |
|
|
if (csSec && *csSec) { | AIT_SET_STR(&u->usr_name, csName); |
AIT_SET_STR(&av->cfg_sec, csSec); | AIT_SET_STR(&u->usr_pass, csPass); |
AIT_KEY(&av->cfg_sec) = crcFletcher16(AIT_GET_LIKE(&av->cfg_sec, u_short*), | AIT_SET_U32(&u->usr_uid, uid); |
io_align(AIT_LEN(&av->cfg_sec) - 1, 2) / 2); | AIT_SET_U32(&u->usr_gid, gid); |
} | AIT_SET_STR(&u->usr_class, csClass); |
AIT_SET_STR(&av->cfg_val, csVal ? csVal : ""); | AIT_SET_U32(&u->usr_change, change); |
AIT_SET_STR(&av->cfg_attr, csAttr); | AIT_SET_U32(&u->usr_expire, expire); |
AIT_KEY(&av->cfg_attr) = crcFletcher16(AIT_GET_LIKE(&av->cfg_attr, u_short*), | AIT_SET_STR(&u->usr_realm, csRealm); |
io_align(AIT_LEN(&av->cfg_attr) - 1, 2) / 2); | AIT_SET_STR(&u->usr_home, csHome); |
| AIT_SET_STR(&u->usr_shell, csShell); |
|
|
CFG_RC_LOCK(cfg); | AIT_KEY(&u->usr_name) = crcFletcher16(AIT_GET_LIKE(&u->usr_name, u_short*), |
RB_INSERT(tagRC, cfg, av); | io_align(AIT_LEN(&u->usr_name) - 1, 2) / 2); |
CFG_RC_UNLOCK(cfg); | |
| PWD_LOCK(pwd); |
| RB_INSERT(tagPWD, pwd, u); |
| PWD_UNLOCK(pwd); |
return 2; |
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 */ |
/* Update element */ |
AIT_FREE_VAL(&av->cfg_val); | AIT_SET_STR(&u->usr_pass, csPass); |
AIT_SET_STR(&av->cfg_val, csVal); | 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; |
return 1; |
} |
} |
|
|
Line 243 cfg_setAttribute(cfg_root_t * __restrict cfg, const ch
|
Line 266 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 | * @pwd = Password root |
* @csSec = Config section //[{csSec}], if NULL unset in *default* section | * @criteria = Search criteria [PWD_CRIT_NAME|PWD_CRIT_UID] |
* @csAttr = Config attribute //{csAttr} = ..., if NULL unset as *any* attribute | * @arg1 = Username | UID |
* return: NULL item not found or null parameters, !=NULL value const string | * return: NULL item not found, !=NULL structure found |
*/ |
*/ |
inline const char * | inline const struct tagUser * |
cfg_getAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr) | 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; |
return NULL; |
|
|
av = _selectAttribute(cfg, csSec, csAttr); | va_start(lst, criteria); |
if (!av) | switch (criteria) { |
return NULL; | case PWD_CRIT_NAME: |
| str = va_arg(lst, char*); |
return AIT_GET_STR(&av->cfg_val); | if (!str) |
} | u = NULL; |
| else |
/* | u = _selectPasswd(pwd, 0, str); |
* cfg_loadAttribute() - Get guarded attribute, if not found item return *default value* | break; |
* | case PWD_CRIT_UID: |
* @cfg = Config root | u = _selectPasswd(pwd, va_arg(lst, u_int), NULL); |
* @csSec = Config section //[{csSec}], if NULL unset in *default* section | break; |
* @csAttr = Config attribute //{csAttr} = ... | default: |
* @val = Return buffer for item Value //... = {val} | u = NULL; |
* @csDefValue = *Default Value* for return in //{val}, if not found item in config | break; |
* 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_end(lst); |
|
|
AIT_INIT_VAL(val); | return u; |
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; | |
} |
} |
#endif |
|