File:  [ELWIX - Embedded LightWeight unIX -] / libaitcfg / inc / aitpwd.h
Revision 1.3: download - view: text, annotated - select for diffs - revision graph
Thu Jan 17 13:27:39 2013 UTC (11 years, 4 months ago) by misho
Branches: MAIN
CVS tags: cfg7_0, HEAD, CFG6_1
version 6.1

    1: /*************************************************************************
    2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
    3: *  by Michael Pounov <misho@openbsd-bg.org>
    4: *
    5: * $Author: misho $
    6: * $Id: aitpwd.h,v 1.3 2013/01/17 13:27:39 misho Exp $
    7: *
    8: **************************************************************************
    9: The ELWIX and AITNET software is distributed under the following
   10: terms:
   11: 
   12: All of the documentation and software included in the ELWIX and AITNET
   13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
   14: 
   15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
   16: 	by Michael Pounov <misho@elwix.org>.  All rights reserved.
   17: 
   18: Redistribution and use in source and binary forms, with or without
   19: modification, are permitted provided that the following conditions
   20: are met:
   21: 1. Redistributions of source code must retain the above copyright
   22:    notice, this list of conditions and the following disclaimer.
   23: 2. Redistributions in binary form must reproduce the above copyright
   24:    notice, this list of conditions and the following disclaimer in the
   25:    documentation and/or other materials provided with the distribution.
   26: 3. All advertising materials mentioning features or use of this software
   27:    must display the following acknowledgement:
   28: This product includes software developed by Michael Pounov <misho@elwix.org>
   29: ELWIX - Embedded LightWeight unIX and its contributors.
   30: 4. Neither the name of AITNET nor the names of its contributors
   31:    may be used to endorse or promote products derived from this software
   32:    without specific prior written permission.
   33: 
   34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
   35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   44: SUCH DAMAGE.
   45: */
   46: #ifndef __AITPWD_H
   47: #define __AITPWD_H
   48: 
   49: 
   50: #include <time.h>
   51: #include <sys/types.h>
   52: #include <aitio.h>
   53: 
   54: 
   55: #define PWD_CRIT_NAME	0
   56: #define PWD_CRIT_UID	1
   57: #define PWD_CRIT_GID	2
   58: 
   59: 
   60: struct tagAcctDB {
   61: 	unsigned short	db_ver;
   62: 	unsigned char	db_lock;
   63: 	unsigned char	db_wrap;
   64: 
   65: 	unsigned int	db_rmin;
   66: 	unsigned int	db_rmax;
   67: 	uint64_t	db_rsize;
   68: 
   69: 	uint64_t	db_since;
   70: 
   71: 	int		db_h;		/* optional */
   72: } __packed;
   73: 
   74: /* Search callback function, compare to match argument to record match ... 
   75:  *  (return: -1 error, 0 not match or 1 match)
   76:  */
   77: typedef int (*cb_acct_f)(void * /*current db_record*/, void * /*argument*/);
   78: 
   79: 
   80: struct tagUser {
   81: 	int			usr_fields;
   82: 
   83: 	ait_val_t		usr_name;
   84: 	ait_val_t		usr_pass;
   85: 	ait_val_t		usr_uid;
   86: 	ait_val_t		usr_gid;
   87: 	ait_val_t		usr_class;
   88: 	ait_val_t		usr_change;
   89: 	ait_val_t		usr_expire;
   90: 	ait_val_t		usr_realm;
   91: 	ait_val_t		usr_home;
   92: 	ait_val_t		usr_shell;
   93: 
   94: 	SLIST_ENTRY(tagUser)	usr_next;
   95: 	RB_ENTRY(tagUser)	usr_node;
   96: };
   97: typedef struct tagPWD {
   98: 	pthread_mutex_t		pwd_mtx;
   99: 
  100: 	struct tagUser		*slh_first;
  101: 	struct tagUser		*rbh_root;
  102: } pwd_root_t;
  103: #define PWD_LOCK(x)	pthread_mutex_lock(&(x)->pwd_mtx)
  104: #define PWD_UNLOCK(x)	pthread_mutex_unlock(&(x)->pwd_mtx)
  105: 
  106: #define PWD_ISEMPTY(x)	RB_EMPTY((x))
  107: 
  108: typedef enum { ALL = -1, 
  109: 	Username, 
  110: 	Password, 
  111: 	UID, 
  112: 	GID, 
  113: 	Class, 
  114: 	Change, 
  115: 	Expire, 
  116: 	Realm, 
  117: 	Home, 
  118: 	Shell 
  119: } passwd_attr_t;
  120: 
  121: 
  122: /*
  123:  * cfgInitPasswd() - Init password root
  124:  *
  125:  * @pwd = Password root
  126:  * return: -1 error or 0 ok
  127:  */
  128: int cfgInitPasswd(pwd_root_t * __restrict pwd);
  129: /*
  130:  * cfgLoadPasswd() - Load passwords from file
  131:  *
  132:  * @pwdName = Passwords filename
  133:  * @pwd = Password root
  134:  * return: -1 error or 0 ok
  135:  */
  136: int cfgLoadPasswd(const char *pwdName, pwd_root_t * __restrict pwd);
  137: /*
  138:  * cfgClearPasswd() - Clear passwords and free resources
  139:  *
  140:  * @cfg = Password root
  141:  * return: none
  142:  */
  143: void cfgClearPasswd(pwd_root_t * __restrict pwd);
  144: /*
  145:  * cfgUnloadPasswd() - Unload passwords from memory and destroy resources
  146:  *
  147:  * @pwd = Password root
  148:  * return: none
  149:  */
  150: void cfgUnloadPasswd(pwd_root_t * __restrict pwd);
  151: /*
  152:  * cfgCreatePasswd() - Create password file from memory
  153:  *
  154:  * @pwdName = New password filename
  155:  * @pwd = Password root
  156:  * return: -1 error or 0 ok
  157:  */
  158: int cfgCreatePasswd(const char *pwdName, pwd_root_t * __restrict pwd);
  159: 
  160: /*
  161:  * cfgReadPasswd() - Read file and add new item at password root
  162:  *
  163:  * @f = File resource
  164:  * @pwd = Password root
  165:  * return: -1 error or 0 ok
  166:  */
  167: int cfgReadPasswd(FILE *f, pwd_root_t * __restrict pwd);
  168: /*
  169:  * cfgWritePasswd() - Write passwords from memory
  170:  *
  171:  * @f = File handle
  172:  * @pwd = Password root
  173:  * return: -1 error or 0 ok
  174:  */
  175: int cfgWritePasswd(FILE *f, pwd_root_t * __restrict pwd);
  176: /*
  177:  * cfgConcatPasswd() - Concat two password roots into one
  178:  *
  179:  * @pwd = Password root
  180:  * @add_pwd = Concated password root will be destroy after merge
  181:  * return: -1 error or 0 ok
  182:  */
  183: int cfgConcatPasswd(pwd_root_t * __restrict pwd, pwd_root_t * __restrict add_pwd);
  184: 
  185: /*
  186:  * cfgAuthPasswd() - Authenticate user against passwords db
  187:  *
  188:  * @pwd = Password root
  189:  * @csName = Username
  190:  * @csPass = Password
  191:  * return: =NULL deny or !=NULL allow
  192:  */
  193: const struct tagUser *cfgAuthPasswd(pwd_root_t * __restrict pwd, 
  194: 		const char *csName, const char *csPass);
  195: 
  196: /*
  197:  * cfg_findPasswdBy() - Find user by criteria position in list
  198:  *
  199:  * @pwd = Password root
  200:  * @criteria = Search criteria [PWD_CRIT_NAME|PWD_CRIT_UID|PWD_CRIT_GID]
  201:  * @arg1 = Username | UID | GID
  202:  * return: NULL not found item or error and !=NULL found item
  203:  */
  204: const struct tagUser *cfg_findPasswdBy(pwd_root_t * __restrict pwd, int criteria, ...);
  205: /*
  206:  * cfg_unsetPasswd() - Unset item from passwords and free resources
  207:  *
  208:  * @pwd = Password root
  209:  * @criteria = Search criteria [PWD_CRIT_NAME|PWD_CRIT_UID]
  210:  * @arg1 = Username | UID
  211:  * return: 0 item not found, -1 error or 1 removed item
  212:  */
  213: int cfg_unsetPasswd(pwd_root_t * __restrict pwd, int criteria, ...);
  214: /*
  215:  * cfg_setPasswd() - Set item in password or adding new item if not exists
  216:  *
  217:  * @cfg = Password root
  218:  * @fields = Following parameters are continuous to certain field
  219:  * @csName = Username
  220:  * @arg1 = Password
  221:  * @arg2 = UID
  222:  * @arg3 = GID
  223:  * @arg4 = Login class
  224:  * @arg5 = Chage date
  225:  * @arg6 = Expire date
  226:  * @arg7 = Realm
  227:  * @arg8 = Home dir
  228:  * @arg9 = Shell
  229:  * return: 0 nothing changed, -1 error, 1 found and updated item or 2 added new item
  230:  */
  231: int cfg_setPasswd(pwd_root_t * __restrict pwd, passwd_attr_t fields, const char *csName, ...);
  232: /*
  233:  * cfg_getPasswd() - Get item from passwords and return structure from it
  234:  *
  235:  * @pwd = Password root
  236:  * @criteria = Search criteria [PWD_CRIT_NAME|PWD_CRIT_UID]
  237:  * @arg1 = Username | UID
  238:  * return: NULL item not found, !=NULL structure found
  239:  */
  240: inline const struct tagUser *cfg_getPasswd(pwd_root_t * __restrict pwd, int criteria, ...);
  241: 
  242: 
  243: #endif

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>