--- libaitcfg/inc/aitpwd.h 2012/09/18 08:17:59 1.1 +++ libaitcfg/inc/aitpwd.h 2012/09/18 08:17:59 1.1.2.1 @@ -0,0 +1,274 @@ +/************************************************************************* +* (C) 2010 AITNET ltd - Sofia/Bulgaria - +* by Michael Pounov +* +* $Author: misho $ +* $Id: aitpwd.h,v 1.1.2.1 2012/09/18 08:17:59 misho Exp $ +* +************************************************************************** +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 + +Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 + by Michael Pounov . 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 +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. +*/ +#ifndef __AITPWD_H +#define __AITPWD_H + + +#include + + +struct tagAcctDB { + unsigned short db_ver; + unsigned char db_lock; + unsigned char db_wrap; + + unsigned int db_rmin; + unsigned int db_rmax; + uint64_t db_rsize; + + uint64_t db_since; + + int db_h; /* optional */ +} __packed; + +/* Search callback function, compare to match argument to record match ... + * (return: -1 error, 0 not match or 1 match) + */ +typedef int (*cb_acct_f)(void * /*current db_record*/, void * /*argument*/); + + +struct tagUser { + ait_val_t usr_name; + ait_val_t usr_pass; + ait_val_t usr_uid; + ait_val_t usr_gid; + ait_val_t usr_class; + ait_val_t usr_change; + ait_val_t usr_expire; + ait_val_t usr_realm; + ait_val_t usr_home; + ait_val_t usr_shell; + + SLIST_ENTRY(tagCfg) usr_next; + RB_ENTRY(tagCfg) usr_node; +}; + +typedef struct tagUser * passwd_t; + +typedef enum { ALL = -1, + Username, + Password, + UID, + GID, + Class, + Change, + Expire, + Realm, + Home, + Shell +} passwd_attr_t; + + +#if 0 +/* + * CreatePasswd() Create secrets from memory + * @csPasswdName = New filename for secrets + * @ppwd = Users array element + * return: 0 ok; -1 error:: can`t save new file +*/ +int CreatePasswd(const char *csPasswdName, passwd_t * __restrict ppwd); +/* + * LoadPasswd() Load secrets from file + * @csPasswdName = Filename of secrets + * @ppwd = Users array element + * return: 0 ok; -1 error:: can`t load file +*/ +int LoadPasswd(const char *csPasswdName, passwd_t * __restrict ppwd); +/* + * UnloadPasswd() Unload secrets from memory and free resources + * @ppwd = Users array element +*/ +void UnloadPasswd(passwd_t * __restrict ppwd); + + +// 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 + * @f = file resource + * @ppwd = User array + * 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 +*/ +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); + + +/* + * 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); + + +/* + * 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 + */ +struct tagAcctDB *pwd_openAcct(const char *csName, unsigned int minR, unsigned int maxR, + size_t recSize, unsigned char wrapR); +/* + * pwd_closeAcct() Close accounting aDat binary database + * @db - DB handle + * return: none + */ +void pwd_closeAcct(struct tagAcctDB ** __restrict db); +/* + * 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 + */ +int pwd_writeAcct(struct tagAcctDB * __restrict db, unsigned int posR, void *rec); +/* + * 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 + */ +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 + + +#endif