--- libaitcfg/inc/aitpwd.h 2012/09/18 08:17:59 1.1 +++ libaitcfg/inc/aitpwd.h 2012/09/19 15:22:32 1.2 @@ -0,0 +1,243 @@ +/************************************************************************* +* (C) 2010 AITNET ltd - Sofia/Bulgaria - +* by Michael Pounov +* +* $Author: misho $ +* $Id: aitpwd.h,v 1.2 2012/09/19 15:22:32 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 +#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; + 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 { + int usr_fields; + + 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(tagUser) usr_next; + RB_ENTRY(tagUser) 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); + +/* + * 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); +/* + * 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); +/* + * 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); + +/* + * cfgAuthPasswd() - Authenticate user against passwords db + * + * @pwd = Password root + * @csName = Username + * @csPass = Password + * return: =NULL deny or !=NULL allow + */ +const struct tagUser *cfgAuthPasswd(pwd_root_t * __restrict pwd, + const char *csName, const char *csPass); + +/* + * 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 + */ +const struct tagUser *cfg_findPasswdBy(pwd_root_t * __restrict pwd, int criteria, ...); +/* + * 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, ...); +/* + * 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 cfg_setPasswd(pwd_root_t * __restrict pwd, u_char fields, const char *csName, ...); +/* + * 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 + */ +inline const struct tagUser *cfg_getPasswd(pwd_root_t * __restrict pwd, int criteria, ...); + + +#endif