version 1.1, 2012/09/18 15:50:59
|
version 1.1.2.1, 2012/09/18 15:50:59
|
Line 0
|
Line 1
|
|
/************************************************************************* |
|
* (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com> |
|
* by Michael Pounov <misho@openbsd-bg.org> |
|
* |
|
* $Author$ |
|
* $Id$ |
|
* |
|
************************************************************************** |
|
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 <info@elwix.org> |
|
|
|
Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 |
|
by Michael Pounov <misho@elwix.org>. 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 <misho@elwix.org> |
|
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 * |
|
_selectAttribute(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 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 |
|
*/ |
|
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; |
|
|
|
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 (AIT_GET_U32(&u->usr_uid) == 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)) { |
|
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; |
|
va_list lst; |
|
|
|
if (!pwd) |
|
return -1; |
|
|
|
va_start(lst, criteria); |
|
switch (criteria) { |
|
case PWD_CRIT_NAME: |
|
u = _selectAttribute(pwd, 0, va_arg(lst, char*)); |
|
break; |
|
case PWD_CRIT_UID: |
|
u = _selectAttribute(pwd, va_arg(lst, u_int), NULL); |
|
break; |
|
default: |
|
va_end(lst); |
|
return -1; |
|
} |
|
va_end(lst); |
|
if (!u) |
|
return 0; |
|
|
|
PWD_LOCK(pwd); |
|
RB_REMOVE(tagPWD, pwd, u); |
|
SLIST_REMOVE(pwd, u, tagUser, usr_next); |
|
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; |
|
} |
|
|
|
#if 0 |
|
/* |
|
* cfg_setAttribute() - Set item in config 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 |
|
* 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) |
|
{ |
|
struct tagCfg *av, *section; |
|
|
|
if (!cfg || !csAttr) |
|
return -1; |
|
|
|
av = _selectAttribute(cfg, csSec, csAttr); |
|
if (!av) { |
|
/* adding new element */ |
|
section = _selectAttribute(cfg, csSec, NULL); |
|
|
|
av = io_malloc(sizeof(struct tagCfg)); |
|
if (!av) { |
|
LOGERR; |
|
return -1; |
|
} else { |
|
memset(av, 0, sizeof(struct tagCfg)); |
|
|
|
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); |
|
} |
|
|
|
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); |
|
|
|
CFG_RC_LOCK(cfg); |
|
RB_INSERT(tagRC, cfg, av); |
|
CFG_RC_UNLOCK(cfg); |
|
return 2; |
|
} |
|
|
|
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); |
|
return 1; |
|
} |
|
|
|
/* Nothing happens ... found & values is equal! */ |
|
return 0; |
|
} |
|
|
|
/* |
|
* cfg_getAttribute() - Get item from config and return value 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 |
|
*/ |
|
inline const char * |
|
cfg_getAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr) |
|
{ |
|
struct tagCfg *av; |
|
|
|
if (!cfg || !csAttr) |
|
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; |
|
} |
|
|
|
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; |
|
} |
|
#endif |