File:  [ELWIX - Embedded LightWeight unIX -] / libaitcfg / inc / aitpwd.h
Revision 1.1.2.4: download - view: text, annotated - select for diffs - revision graph
Tue Sep 18 13:32:48 2012 UTC (11 years, 8 months ago) by misho
Branches: cfg5_5
added some fixes

    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.1.2.4 2012/09/18 13:32:48 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: 
   52: 
   53: struct tagAcctDB {
   54: 	unsigned short	db_ver;
   55: 	unsigned char	db_lock;
   56: 	unsigned char	db_wrap;
   57: 
   58: 	unsigned int	db_rmin;
   59: 	unsigned int	db_rmax;
   60: 	uint64_t	db_rsize;
   61: 
   62: 	uint64_t	db_since;
   63: 
   64: 	int		db_h;		/* optional */
   65: } __packed;
   66: 
   67: /* Search callback function, compare to match argument to record match ... 
   68:  *  (return: -1 error, 0 not match or 1 match)
   69:  */
   70: typedef int (*cb_acct_f)(void * /*current db_record*/, void * /*argument*/);
   71: 
   72: 
   73: struct tagUser {
   74: 	int			usr_fields;
   75: 
   76: 	ait_val_t		usr_name;
   77: 	ait_val_t		usr_pass;
   78: 	ait_val_t		usr_uid;
   79: 	ait_val_t		usr_gid;
   80: 	ait_val_t		usr_class;
   81: 	ait_val_t		usr_change;
   82: 	ait_val_t		usr_expire;
   83: 	ait_val_t		usr_realm;
   84: 	ait_val_t		usr_home;
   85: 	ait_val_t		usr_shell;
   86: 
   87: 	SLIST_ENTRY(tagUser)	usr_next;
   88: 	RB_ENTRY(tagUser)	usr_node;
   89: };
   90: typedef struct tagPWD {
   91: 	pthread_mutex_t		pwd_mtx;
   92: 
   93: 	struct tagUser		*slh_first;
   94: 	struct tagUser		*rbh_root;
   95: } pwd_root_t;
   96: #define PWD_LOCK(x)	pthread_mutex_lock(&(x)->pwd_mtx)
   97: #define PWD_UNLOCK(x)	pthread_mutex_unlock(&(x)->pwd_mtx)
   98: 
   99: #define PWD_ISEMPTY(x)	RB_EMPTY((x))
  100: 
  101: typedef enum { ALL = -1, 
  102: 	Username, 
  103: 	Password, 
  104: 	UID, 
  105: 	GID, 
  106: 	Class, 
  107: 	Change, 
  108: 	Expire, 
  109: 	Realm, 
  110: 	Home, 
  111: 	Shell 
  112: } passwd_attr_t;
  113: 
  114: 
  115: /*
  116:  * cfgInitPasswd() - Init password root
  117:  *
  118:  * @pwd = Password root
  119:  * return: -1 error or 0 ok
  120:  */
  121: int cfgInitPasswd(pwd_root_t * __restrict pwd);
  122: /*
  123:  * cfgLoadPasswd() - Load passwords from file
  124:  *
  125:  * @pwdName = Passwords filename
  126:  * @pwd = Password root
  127:  * return: -1 error or 0 ok
  128:  */
  129: int cfgLoadPasswd(const char *pwdName, pwd_root_t * __restrict pwd);
  130: /*
  131:  * cfgClearPasswd() - Clear passwords and free resources
  132:  *
  133:  * @cfg = Password root
  134:  * return: none
  135:  */
  136: void cfgClearPasswd(pwd_root_t * __restrict pwd);
  137: /*
  138:  * cfgUnloadPasswd() - Unload passwords from memory and destroy resources
  139:  *
  140:  * @pwd = Password root
  141:  * return: none
  142:  */
  143: void cfgUnloadPasswd(pwd_root_t * __restrict pwd);
  144: /*
  145:  * cfgCreatePasswd() - Create password file from memory
  146:  *
  147:  * @pwdName = New password filename
  148:  * @pwd = Password root
  149:  * return: -1 error or 0 ok
  150:  */
  151: int cfgCreatePasswd(const char *pwdName, pwd_root_t * __restrict pwd);
  152: 
  153: /*
  154:  * cfgReadPasswd() - Read file and add new item at password root
  155:  *
  156:  * @f = File resource
  157:  * @pwd = Password root
  158:  * return: -1 error or 0 ok
  159:  */
  160: int cfgReadPasswd(FILE *f, pwd_root_t * __restrict pwd);
  161: /*
  162:  * cfgWritePasswd() - Write passwords from memory
  163:  *
  164:  * @f = File handle
  165:  * @pwd = Password root
  166:  * return: -1 error or 0 ok
  167:  */
  168: int cfgWritePasswd(FILE *f, pwd_root_t * __restrict pwd);
  169: /*
  170:  * cfgConcatPasswd() - Concat two password roots into one
  171:  *
  172:  * @pwd = Password root
  173:  * @add_pwd = Concated password root will be destroy after merge
  174:  * return: -1 error or 0 ok
  175:  */
  176: int cfgConcatPasswd(pwd_root_t * __restrict pwd, pwd_root_t * __restrict add_pwd);
  177: 
  178: #if 0
  179: /*
  180:  * pwdFreeValue() Free passwd_t value
  181:  * @ppwd = User object
  182:  * @idx = Index of attribute, if ALL == idx is free all structure
  183:  * return: 0 ok, -1 error
  184: */
  185: inline int pwdFreeValue(passwd_t * __restrict ppwd, passwd_attr_t idx);
  186: /*
  187:  * pwdSetValue() Set passwd_t value
  188:  * @ppwd = User object
  189:  * @idx = Index of attribute, if ALL == idx clean User object and set username!
  190:  * @Value = Value to set
  191:  * return: number of fields in passwd_t; -1 error:: can`t set passwd_t
  192: */
  193: inline int pwdSetValue(passwd_t * __restrict ppwd, passwd_attr_t idx, unsigned char *Value);
  194: /*
  195:  * pwdGetValue() Get passwd_t value
  196:  * @pwd = User object
  197:  * @idx = Index of attribute, if ALL == idx is error!
  198:  * @Value = Value to get
  199:  * @valLen = Size of Value string
  200:  * return: number of fields in passwd_t; -1 error:: can`t get passwd_t
  201: */
  202: inline int pwdGetValue(passwd_t __restrict pwd, passwd_attr_t idx, unsigned char *Value, int valLen);
  203: 
  204: 
  205: /*
  206:  * pwd_FindAttribute() Find by attribute position in user array
  207:  * @pwd = User array
  208:  * @idx = Index of attribute, if ALL == idx return number of user array elements
  209:  * @csAttr = User attribute
  210:  * return: 0 not found item; -1 error: null parameters; >0 position in array + 1
  211: */
  212: inline int pwd_FindAttribute(passwd_t __restrict pwd, passwd_attr_t idx, const unsigned char *csAttr);
  213: /*
  214:  * pwd_SetAttribute() Set user in array or add new user if not exists
  215:  * @ppwd = User array
  216:  * @idx = Index of attribute to set
  217:  * @val = User structure for setup
  218:  * return: 0 nothing changed, -1 error: not enough memory; 1 find and update item; 2 added new item
  219: */
  220: int pwd_SetAttribute(passwd_t * __restrict ppwd, passwd_attr_t idx, passwd_t val);
  221: /*
  222:  * pwd_GetAttribute() Get user from array, set username in val
  223:  * @pwd = User array
  224:  * @idx = Index of attribute to get, if idx == ALL return full element items
  225:  * @val = User structure
  226:  * return: 0 not found, -1 error; >0 found at position in array + 1
  227: */
  228: int pwd_GetAttribute(passwd_t __restrict pwd, passwd_attr_t idx, passwd_t val);
  229: /*
  230:  * pwd_UnsetAttribute() Unset user from array and free resources!
  231:  * @ppwd = User array
  232:  * @idx = Index of attribute to unset, if idx == ALL unset(delete) all items in element
  233:  * @val = User structure
  234:  * return: 0 item not found, -1 error: null parameters; >0 number of elements leave in array
  235: */
  236: int pwd_UnsetAttribute(passwd_t * __restrict ppwd, passwd_attr_t idx, passwd_t val);
  237: /*
  238:  * pwd_Authenticate() Authenticate user, set username and password in val
  239:  * @pwd = User array
  240:  * @val = User structure for authenticate, set username & password 
  241:  	after authentication fill other fields if exists
  242:  * return: 0 not found, -1 error; >0 found at position in array + 1
  243: */
  244: int pwd_Authenticate(passwd_t __restrict pwd, passwd_t val);
  245: 
  246: 
  247: /*
  248:  * pwd_openAcct() Open accounting aDat binary database
  249:  * @csName - DB name
  250:  * @minR - Minimum records in database
  251:  * @maxR - Maximum records in database(round-robin db), if 0 unlimited grow
  252:  * @recSize - Size of record element
  253:  * @wrapR - If maxR >0 give ability to wrap round-robin db
  254:  * return: NULL error or !=NULL opened db handle
  255:  */
  256: struct tagAcctDB *pwd_openAcct(const char *csName, unsigned int minR, unsigned int maxR, 
  257: 		size_t recSize, unsigned char wrapR);
  258: /*
  259:  * pwd_closeAcct() Close accounting aDat binary database
  260:  * @db - DB handle
  261:  * return: none
  262:  */
  263: void pwd_closeAcct(struct tagAcctDB ** __restrict db);
  264: /*
  265:  * pwd_writeAcct() Write accounting record to aDat binary database
  266:  * @db - DB handle
  267:  * @posR - At position number, if db is unlimited grow (-1) write at last free record
  268:  * @rec - Record data
  269:  * return: -1 error or 0 ok
  270:  */
  271: int pwd_writeAcct(struct tagAcctDB * __restrict db, unsigned int posR, void *rec);
  272: /*
  273:  * pwd_readAcct() Read accounting record from aDat binary database
  274:  * @db - DB handle
  275:  * @posR - From position number
  276:  * @rec - Record data buffer
  277:  * @recsize - Record data buffer size
  278:  * return: -1 error or 0 ok
  279:  */
  280: int pwd_readAcct(struct tagAcctDB * __restrict db, unsigned int posR, void *rec, int recsize);
  281: 
  282: /*
  283:  * pwd_findAcct() Find accounting record from aDat binary database
  284:  * @db - DB handle
  285:  * @from - From position
  286:  * @to - To position, if 0 search to end-of-file
  287:  * @func - Check for match callback function
  288:  * @arg - Argument passthrough to callback as argument for search
  289:  * return: -1 error, 0 not found or >1 founded at position! (p.s:start from 1)
  290:  */
  291: int64_t pwd_findAcct(struct tagAcctDB * __restrict db, unsigned int from, unsigned int to, 
  292: 		cb_acct_f func, void *arg);
  293: #endif
  294: 
  295: 
  296: #endif

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