version 1.1.2.1, 2012/09/18 08:17:59
|
version 1.1.2.7, 2012/09/19 11:53:52
|
Line 48 SUCH DAMAGE.
|
Line 48 SUCH DAMAGE.
|
|
|
|
|
#include <time.h> |
#include <time.h> |
|
#include <sys/types.h> |
|
#include <aitio.h> |
|
|
|
|
|
#define PWD_CRIT_NAME 0 |
|
#define PWD_CRIT_UID 1 |
|
#define PWD_CRIT_GID 2 |
|
|
|
|
struct tagAcctDB { |
struct tagAcctDB { |
unsigned short db_ver; |
unsigned short db_ver; |
unsigned char db_lock; |
unsigned char db_lock; |
Line 71 typedef int (*cb_acct_f)(void * /*current db_record*/,
|
Line 78 typedef int (*cb_acct_f)(void * /*current db_record*/,
|
|
|
|
|
struct tagUser { |
struct tagUser { |
|
int usr_fields; |
|
|
ait_val_t usr_name; |
ait_val_t usr_name; |
ait_val_t usr_pass; |
ait_val_t usr_pass; |
ait_val_t usr_uid; |
ait_val_t usr_uid; |
Line 82 struct tagUser {
|
Line 91 struct tagUser {
|
ait_val_t usr_home; |
ait_val_t usr_home; |
ait_val_t usr_shell; |
ait_val_t usr_shell; |
|
|
SLIST_ENTRY(tagCfg) usr_next; | SLIST_ENTRY(tagUser) usr_next; |
RB_ENTRY(tagCfg) usr_node; | RB_ENTRY(tagUser) usr_node; |
}; |
}; |
|
typedef struct tagPWD { |
|
pthread_mutex_t pwd_mtx; |
|
|
typedef struct tagUser * passwd_t; | struct tagUser *slh_first; |
| struct tagUser *rbh_root; |
| } pwd_root_t; |
| #define PWD_LOCK(x) pthread_mutex_lock(&(x)->pwd_mtx) |
| #define PWD_UNLOCK(x) pthread_mutex_unlock(&(x)->pwd_mtx) |
|
|
|
#define PWD_ISEMPTY(x) RB_EMPTY((x)) |
|
|
typedef enum { ALL = -1, |
typedef enum { ALL = -1, |
Username, |
Username, |
Password, |
Password, |
Line 102 typedef enum { ALL = -1,
|
Line 119 typedef enum { ALL = -1,
|
} passwd_attr_t; |
} passwd_attr_t; |
|
|
|
|
#if 0 |
|
/* |
/* |
* CreatePasswd() Create secrets from memory | * cfgInitPasswd() - Init password root |
* @csPasswdName = New filename for secrets | * |
* @ppwd = Users array element | * @pwd = Password root |
* return: 0 ok; -1 error:: can`t save new file | * return: -1 error or 0 ok |
*/ | */ |
int CreatePasswd(const char *csPasswdName, passwd_t * __restrict ppwd); | int cfgInitPasswd(pwd_root_t * __restrict pwd); |
/* |
/* |
* LoadPasswd() Load secrets from file | * cfgLoadPasswd() - Load passwords from file |
* @csPasswdName = Filename of secrets | * |
* @ppwd = Users array element | * @pwdName = Passwords filename |
* return: 0 ok; -1 error:: can`t load file | * @pwd = Password root |
*/ | * return: -1 error or 0 ok |
int LoadPasswd(const char *csPasswdName, passwd_t * __restrict ppwd); | */ |
| int cfgLoadPasswd(const char *pwdName, pwd_root_t * __restrict pwd); |
/* |
/* |
* UnloadPasswd() Unload secrets from memory and free resources | * cfgClearPasswd() - Clear passwords and free resources |
* @ppwd = Users array element | * |
*/ | * @cfg = Password root |
void UnloadPasswd(passwd_t * __restrict ppwd); | * return: none |
| */ |
| void cfgClearPasswd(pwd_root_t * __restrict pwd); |
// pwd_GetErrno() Get error code of last operation | |
inline int pwd_GetErrno(); | |
// pwd_GetError() Get error text of last operation | |
inline const char *pwd_GetError(); | |
// pwd_Delimiter() Operate with file delimiter, if delim == 0 only get current | |
inline unsigned char pwd_GetDelim(unsigned char delim); | |
| |
| |
/* |
/* |
* ReadPasswd() Read from file and add new item to user array | * cfgUnloadPasswd() - Unload passwords from memory and destroy resources |
* @f = file resource | * |
* @ppwd = User array | * @pwd = Password root |
* return: 0 ok; -1 error:: can`t allocate memory | |
*/ | |
int ReadPasswd(FILE *f, passwd_t * __restrict ppwd); | |
/* | |
* 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); | |
| |
| |
/* | |
* pwdCleanStr() Free strings in passwd_t value | |
* @pwd = User object | |
* return: none |
* return: none |
*/ | */ |
inline void pwdCleanStr(passwd_t __restrict pwd); | void cfgUnloadPasswd(pwd_root_t * __restrict pwd); |
/* |
/* |
* pwdFreeValue() Free passwd_t value | * cfgCreatePasswd() - Create password file from memory |
* @ppwd = User object | * |
* @idx = Index of attribute, if ALL == idx is free all structure | * @pwdName = New password filename |
* return: 0 ok, -1 error | * @pwd = Password root |
*/ | * return: -1 error or 0 ok |
inline int pwdFreeValue(passwd_t * __restrict ppwd, passwd_attr_t idx); | */ |
/* | int cfgCreatePasswd(const char *pwdName, pwd_root_t * __restrict pwd); |
* 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); | |
|
|
|
|
/* |
/* |
* pwd_FindAttribute() Find by attribute position in user array | * cfgReadPasswd() - Read file and add new item at password root |
* @pwd = User array | * |
* @idx = Index of attribute, if ALL == idx return number of user array elements | * @f = File resource |
* @csAttr = User attribute | * @pwd = Password root |
* return: 0 not found item; -1 error: null parameters; >0 position in array + 1 | * return: -1 error or 0 ok |
*/ | */ |
inline int pwd_FindAttribute(passwd_t __restrict pwd, passwd_attr_t idx, const unsigned char *csAttr); | int cfgReadPasswd(FILE *f, pwd_root_t * __restrict pwd); |
/* |
/* |
* pwd_SetAttribute() Set user in array or add new user if not exists | * cfgWritePasswd() - Write passwords from memory |
* @ppwd = User array | * |
* @idx = Index of attribute to set | * @f = File handle |
* @val = User structure for setup | * @pwd = Password root |
* return: 0 nothing changed, -1 error: not enough memory; 1 find and update item; 2 added new item | * return: -1 error or 0 ok |
*/ | */ |
int pwd_SetAttribute(passwd_t * __restrict ppwd, passwd_attr_t idx, passwd_t val); | int cfgWritePasswd(FILE *f, pwd_root_t * __restrict pwd); |
/* |
/* |
* pwd_GetAttribute() Get user from array, set username in val | * cfgConcatPasswd() - Concat two password roots into one |
* @pwd = User array | * |
* @idx = Index of attribute to get, if idx == ALL return full element items | * @pwd = Password root |
* @val = User structure | * @add_pwd = Concated password root will be destroy after merge |
* return: 0 not found, -1 error; >0 found at position in array + 1 | * return: -1 error or 0 ok |
*/ | */ |
int pwd_GetAttribute(passwd_t __restrict pwd, passwd_attr_t idx, passwd_t val); | int cfgConcatPasswd(pwd_root_t * __restrict pwd, pwd_root_t * __restrict add_pwd); |
/* | |
* 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); | |
|
|
|
|
/* |
/* |
* pwd_openAcct() Open accounting aDat binary database | * cfgAuthPasswd() - Authenticate user against passwords db |
* @csName - DB name | * |
* @minR - Minimum records in database | * @pwd = Password root |
* @maxR - Maximum records in database(round-robin db), if 0 unlimited grow | * @csName = Username |
* @recSize - Size of record element | * @csPass = Password |
* @wrapR - If maxR >0 give ability to wrap round-robin db | * return: -1 error, 1 deny or 0 allow |
* return: NULL error or !=NULL opened db handle | |
*/ |
*/ |
struct tagAcctDB *pwd_openAcct(const char *csName, unsigned int minR, unsigned int maxR, | int cfgAuthPasswd(pwd_root_t * __restrict pwd, const char *csName, const char *csPass); |
size_t recSize, unsigned char wrapR); | |
/* |
/* |
* pwd_closeAcct() Close accounting aDat binary database | * cfg_findPasswdBy() - Find user by criteria position in list |
* @db - DB handle | * |
* return: none | * @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 |
*/ |
*/ |
void pwd_closeAcct(struct tagAcctDB ** __restrict db); | struct tagUser *cfg_findPasswdBy(pwd_root_t * __restrict pwd, int criteria, ...); |
/* |
/* |
* pwd_writeAcct() Write accounting record to aDat binary database | * cfg_unsetPasswd() - Unset item from passwords and free resources |
* @db - DB handle | * |
* @posR - At position number, if db is unlimited grow (-1) write at last free record | * @pwd = Password root |
* @rec - Record data | * @criteria = Search criteria [PWD_CRIT_NAME|PWD_CRIT_UID] |
* return: -1 error or 0 ok | * @arg1 = Username | UID |
| * return: 0 item not found, -1 error or 1 removed item |
*/ |
*/ |
int pwd_writeAcct(struct tagAcctDB * __restrict db, unsigned int posR, void *rec); | int cfg_unsetPasswd(pwd_root_t * __restrict pwd, int criteria, ...); |
/* |
/* |
* pwd_readAcct() Read accounting record from aDat binary database | * cfg_setPasswd() - Set item in password or adding new item if not exists |
* @db - DB handle | * |
* @posR - From position number | * @cfg = Password root |
* @rec - Record data buffer | * @csName = Username |
* @recsize - Record data buffer size | * @csPass = Password |
* return: -1 error or 0 ok | * @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 pwd_readAcct(struct tagAcctDB * __restrict db, unsigned int posR, void *rec, int recsize); | int cfg_setPasswd(pwd_root_t * __restrict pwd, const char *csName, const char *csPass, |
| unsigned int uid, unsigned int gid, const char *csClass, |
| unsigned int change, unsigned int expire, const char *csRealm, |
| const char *csHome, const char *csShell); |
/* |
/* |
* pwd_findAcct() Find accounting record from aDat binary database | * cfg_getPasswd() - Get item from passwords and return structure from it |
* @db - DB handle | * |
* @from - From position | * @pwd = Password root |
* @to - To position, if 0 search to end-of-file | * @criteria = Search criteria [PWD_CRIT_NAME|PWD_CRIT_UID] |
* @func - Check for match callback function | * @arg1 = Username | UID |
* @arg - Argument passthrough to callback as argument for search | * return: NULL item not found, !=NULL structure found |
* 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, | inline const struct tagUser *cfg_getPasswd(pwd_root_t * __restrict pwd, int criteria, ...); |
cb_acct_f func, void *arg); | |
#endif | |
|
|
|
|
#endif |
#endif |