/************************************************************************* * (C) 2010 AITNET ltd - Sofia/Bulgaria - * by Michael Pounov * * $Author: misho $ * $Id: aitpwd.h,v 1.1.2.2 2012/09/18 08:44:19 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 tagPWD { pthread_mutex_t pwd_mtx; 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, Username, Password, UID, GID, Class, Change, Expire, Realm, Home, Shell } passwd_attr_t; /* * cfgInitPasswd() - Init password root * * @pwd = Password root * return: -1 error or 0 ok */ int cfgInitPasswd(pwd_root_t * __restrict pwd); /* * cfgLoadPasswd() - Load passwords from file * * @pwdName = Passwords filename * @pwd = Password root * return: -1 error or 0 ok */ int cfgLoadPasswd(const char *pwdName, pwd_root_t * __restrict pwd); /* * cfgClearPasswd() - Clear passwords and free resources * * @cfg = Password root * return: none */ void cfgClearPasswd(pwd_root_t * __restrict pwd); /* * cfgUnloadPasswd() - Unload passwords from memory and destroy resources * * @pwd = Password root * return: none */ void cfgUnloadPasswd(pwd_root_t * __restrict pwd); /* * cfgCreatePasswd() - Create password file from memory * * @pwdName = New password filename * @pwd = Password root * return: -1 error or 0 ok */ 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); /* * 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