--- libaitcfg/inc/aitpwd.h 2012/09/18 08:44:19 1.1.2.2 +++ libaitcfg/inc/aitpwd.h 2012/09/19 13:02:58 1.1.2.10 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitpwd.h,v 1.1.2.2 2012/09/18 08:44:19 misho Exp $ +* $Id: aitpwd.h,v 1.1.2.10 2012/09/19 13:02:58 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -48,8 +48,15 @@ SUCH DAMAGE. #include +#include +#include +#define PWD_CRIT_NAME 0 +#define PWD_CRIT_UID 1 +#define PWD_CRIT_GID 2 + + struct tagAcctDB { unsigned short db_ver; unsigned char db_lock; @@ -71,6 +78,8 @@ typedef int (*cb_acct_f)(void * /*current db_record*/, struct tagUser { + int usr_fields; + ait_val_t usr_name; ait_val_t usr_pass; ait_val_t usr_uid; @@ -82,8 +91,8 @@ struct tagUser { ait_val_t usr_home; ait_val_t usr_shell; - SLIST_ENTRY(tagCfg) usr_next; - RB_ENTRY(tagCfg) usr_node; + SLIST_ENTRY(tagUser) usr_next; + RB_ENTRY(tagUser) usr_node; }; typedef struct tagPWD { pthread_mutex_t pwd_mtx; @@ -148,144 +157,86 @@ void cfgUnloadPasswd(pwd_root_t * __restrict pwd); */ int cfgCreatePasswd(const char *pwdName, pwd_root_t * __restrict pwd); -#if 0 /* - * ReadPasswd() Read from file and add new item to user array - * @f = file resource - * @ppwd = User array - * return: 0 ok; -1 error:: can`t allocate memory -*/ -int ReadPasswd(FILE *f, passwd_t * __restrict ppwd); + * cfgReadPasswd() - Read file and add new item at password root + * + * @f = File resource + * @pwd = Password root + * return: -1 error or 0 ok + */ +int cfgReadPasswd(FILE *f, pwd_root_t * __restrict pwd); /* - * WritePasswd() Write to file from items in array - * @f = file resource - * @ppwd = User array - * return: 0 ok; -1 error:: can`t write to file -*/ -int WritePasswd(FILE *f, passwd_t * __restrict ppwd); - - + * cfgWritePasswd() - Write passwords from memory + * + * @f = File handle + * @pwd = Password root + * return: -1 error or 0 ok + */ +int cfgWritePasswd(FILE *f, pwd_root_t * __restrict pwd); /* - * pwdCleanStr() Free strings in passwd_t value - * @pwd = User object - * return: none -*/ -inline void pwdCleanStr(passwd_t __restrict pwd); -/* - * pwdFreeValue() Free passwd_t value - * @ppwd = User object - * @idx = Index of attribute, if ALL == idx is free all structure - * return: 0 ok, -1 error -*/ -inline int pwdFreeValue(passwd_t * __restrict ppwd, passwd_attr_t idx); -/* - * pwdSetValue() Set passwd_t value - * @ppwd = User object - * @idx = Index of attribute, if ALL == idx clean User object and set username! - * @Value = Value to set - * return: number of fields in passwd_t; -1 error:: can`t set passwd_t -*/ -inline int pwdSetValue(passwd_t * __restrict ppwd, passwd_attr_t idx, unsigned char *Value); -/* - * pwdGetValue() Get passwd_t value - * @pwd = User object - * @idx = Index of attribute, if ALL == idx is error! - * @Value = Value to get - * @valLen = Size of Value string - * return: number of fields in passwd_t; -1 error:: can`t get passwd_t -*/ -inline int pwdGetValue(passwd_t __restrict pwd, passwd_attr_t idx, unsigned char *Value, int valLen); + * cfgConcatPasswd() - Concat two password roots into one + * + * @pwd = Password root + * @add_pwd = Concated password root will be destroy after merge + * return: -1 error or 0 ok + */ +int cfgConcatPasswd(pwd_root_t * __restrict pwd, pwd_root_t * __restrict add_pwd); - /* - * pwd_FindAttribute() Find by attribute position in user array - * @pwd = User array - * @idx = Index of attribute, if ALL == idx return number of user array elements - * @csAttr = User attribute - * return: 0 not found item; -1 error: null parameters; >0 position in array + 1 -*/ -inline int pwd_FindAttribute(passwd_t __restrict pwd, passwd_attr_t idx, const unsigned char *csAttr); -/* - * pwd_SetAttribute() Set user in array or add new user if not exists - * @ppwd = User array - * @idx = Index of attribute to set - * @val = User structure for setup - * return: 0 nothing changed, -1 error: not enough memory; 1 find and update item; 2 added new item -*/ -int pwd_SetAttribute(passwd_t * __restrict ppwd, passwd_attr_t idx, passwd_t val); -/* - * pwd_GetAttribute() Get user from array, set username in val - * @pwd = User array - * @idx = Index of attribute to get, if idx == ALL return full element items - * @val = User structure - * return: 0 not found, -1 error; >0 found at position in array + 1 -*/ -int pwd_GetAttribute(passwd_t __restrict pwd, passwd_attr_t idx, passwd_t val); -/* - * pwd_UnsetAttribute() Unset user from array and free resources! - * @ppwd = User array - * @idx = Index of attribute to unset, if idx == ALL unset(delete) all items in element - * @val = User structure - * return: 0 item not found, -1 error: null parameters; >0 number of elements leave in array -*/ -int pwd_UnsetAttribute(passwd_t * __restrict ppwd, passwd_attr_t idx, passwd_t val); -/* - * pwd_Authenticate() Authenticate user, set username and password in val - * @pwd = User array - * @val = User structure for authenticate, set username & password - after authentication fill other fields if exists - * return: 0 not found, -1 error; >0 found at position in array + 1 -*/ -int pwd_Authenticate(passwd_t __restrict pwd, passwd_t val); + * cfgAuthPasswd() - Authenticate user against passwords db + * + * @pwd = Password root + * @csName = Username + * @csPass = Password + * return: -1 error, 1 deny or 0 allow + */ +int cfgAuthPasswd(pwd_root_t * __restrict pwd, const char *csName, const char *csPass); - /* - * pwd_openAcct() Open accounting aDat binary database - * @csName - DB name - * @minR - Minimum records in database - * @maxR - Maximum records in database(round-robin db), if 0 unlimited grow - * @recSize - Size of record element - * @wrapR - If maxR >0 give ability to wrap round-robin db - * return: NULL error or !=NULL opened db handle + * 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 tagAcctDB *pwd_openAcct(const char *csName, unsigned int minR, unsigned int maxR, - size_t recSize, unsigned char wrapR); +const struct tagUser *cfg_findPasswdBy(pwd_root_t * __restrict pwd, int criteria, ...); /* - * pwd_closeAcct() Close accounting aDat binary database - * @db - DB handle - * return: none + * 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 */ -void pwd_closeAcct(struct tagAcctDB ** __restrict db); +int cfg_unsetPasswd(pwd_root_t * __restrict pwd, int criteria, ...); /* - * pwd_writeAcct() Write accounting record to aDat binary database - * @db - DB handle - * @posR - At position number, if db is unlimited grow (-1) write at last free record - * @rec - Record data - * return: -1 error or 0 ok + * 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 pwd_writeAcct(struct tagAcctDB * __restrict db, unsigned int posR, void *rec); +int cfg_setPasswd(pwd_root_t * __restrict pwd, u_char fields, const char *csName, ...); /* - * pwd_readAcct() Read accounting record from aDat binary database - * @db - DB handle - * @posR - From position number - * @rec - Record data buffer - * @recsize - Record data buffer size - * return: -1 error or 0 ok + * 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 */ -int pwd_readAcct(struct tagAcctDB * __restrict db, unsigned int posR, void *rec, int recsize); - -/* - * pwd_findAcct() Find accounting record from aDat binary database - * @db - DB handle - * @from - From position - * @to - To position, if 0 search to end-of-file - * @func - Check for match callback function - * @arg - Argument passthrough to callback as argument for search - * return: -1 error, 0 not found or >1 founded at position! (p.s:start from 1) - */ -int64_t pwd_findAcct(struct tagAcctDB * __restrict db, unsigned int from, unsigned int to, - cb_acct_f func, void *arg); -#endif +inline const struct tagUser *cfg_getPasswd(pwd_root_t * __restrict pwd, int criteria, ...); #endif