File:  [ELWIX - Embedded LightWeight unIX -] / libaitcfg / src / aitcfg.c
Revision 1.8.4.1: download - view: text, annotated - select for diffs - revision graph
Thu Jan 17 13:42:10 2013 UTC (11 years, 3 months ago) by misho
Branches: cfg7_0
Diff to: branchpoint 1.8: preferred, unified
changed apis

    1: /*************************************************************************
    2: * (C) 2008 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
    3: *  by Michael Pounov <misho@openbsd-bg.org>
    4: *
    5: * $Author: misho $
    6: * $Id: aitcfg.c,v 1.8.4.1 2013/01/17 13:42:10 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, 2013
   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: #include "global.h"
   47: 
   48: 
   49: #pragma GCC visibility push(hidden)
   50: 
   51: int cfg_Errno;
   52: char cfg_Error[STRSIZ];
   53: 
   54: inline int
   55: cfg_Write(FILE *f, char *fmt, ...)
   56: {
   57: 	int ret = 0;
   58: 	va_list lst;
   59: 
   60: 	va_start(lst, fmt);
   61: 	ret = vfprintf(f, fmt, lst);
   62: 	va_end(lst);
   63: 
   64: 	return ret;
   65: }
   66: 
   67: inline int
   68: cfg_tree_cmp(struct tagCfg *a, struct tagCfg *b)
   69: {
   70: 	int ret;
   71: 
   72: 	assert(a && b);
   73: 
   74: 	ret = ((AIT_KEY(&a->cfg_sec) << 16) | AIT_KEY(&a->cfg_attr)) - 
   75: 		((AIT_KEY(&b->cfg_sec) << 16) | AIT_KEY(&b->cfg_attr));
   76: 
   77: 	if (ret < 0)
   78: 		return -1;
   79: 	else if (ret > 0)
   80: 		return 1;
   81: 
   82: 	return ret;
   83: }
   84: 
   85: RB_GENERATE(tagRC, tagCfg, cfg_node, cfg_tree_cmp);
   86: 
   87: #pragma GCC visibility pop
   88: 
   89: 
   90: // cfg_GetErrno() Get error code of last operation
   91: inline int
   92: cfg_GetErrno()
   93: {
   94: 	return cfg_Errno;
   95: }
   96: 
   97: // cfg_GetError() Get error text of last operation
   98: inline const char *
   99: cfg_GetError()
  100: {
  101: 	return cfg_Error;
  102: }
  103: 
  104: // cfg_SetErr() Set error to variables for internal use!!!
  105: inline void
  106: cfg_SetErr(int eno, char *estr, ...)
  107: {
  108: 	va_list lst;
  109: 
  110: 	cfg_Errno = eno;
  111: 	memset(cfg_Error, 0, sizeof cfg_Error);
  112: 	va_start(lst, estr);
  113: 	vsnprintf(cfg_Error, sizeof cfg_Error, estr, lst);
  114: 	va_end(lst);
  115: }
  116: 
  117: 
  118: /*
  119:  * cfgInitConfig() - Init config root
  120:  *
  121:  * @cfg = Config root
  122:  * return: -1 error or 0 ok
  123:  */
  124: int
  125: cfgInitConfig(cfg_root_t * __restrict cfg)
  126: {
  127: 	if (!cfg)
  128: 		return -1;
  129: 
  130: 	pthread_mutex_init(&cfg->rc_mtx, NULL);
  131: 
  132: 	SLIST_INIT(cfg);
  133: 	RB_INIT(cfg);
  134: 	return 0;
  135: }
  136: 
  137: /*
  138:  * cfgLoadConfig() - Load config from file
  139:  *
  140:  * @cfgName = Config filename
  141:  * @cfg = Config root
  142:  * return: -1 error or 0 ok
  143:  */
  144: int
  145: cfgLoadConfig(const char *cfgName, cfg_root_t * __restrict cfg)
  146: {
  147: 	FILE *f;
  148: 	int ret;
  149: 
  150: 	if (!cfgName || !cfg) {
  151: 		cfg_SetErr(EINVAL, "Invalid parameter(s)");
  152: 		return -1;
  153: 	} else
  154: 		cfgInitConfig(cfg);
  155: 
  156: 	f = fopen(cfgName, "r");
  157: 	if (!f) {
  158: 		LOGERR;
  159: 		return -1;
  160: 	}
  161: 
  162: 	ret = cfgReadConfig(f, cfg);
  163: 
  164: 	fclose(f);
  165: 	return ret;
  166: }
  167: 
  168: /*
  169:  * cfgClearConfig() - Clear config and free resources
  170:  *
  171:  * @cfg = Config root
  172:  * return: none
  173:  */
  174: void
  175: cfgClearConfig(cfg_root_t * __restrict cfg)
  176: {
  177: 	struct tagCfg *av;
  178: 
  179: 	if (!cfg)
  180: 		return;
  181: 
  182: 	CFG_RC_LOCK(cfg);
  183: 	while ((av = SLIST_FIRST(cfg))) {
  184: 		SLIST_REMOVE_HEAD(cfg, cfg_next);
  185: 
  186: 		AIT_FREE_VAL(&av->cfg_val);
  187: 		AIT_FREE_VAL(&av->cfg_attr);
  188: 		AIT_FREE_VAL(&av->cfg_sec);
  189: 		e_free(av);
  190: 	}
  191: 	cfg->rbh_root = NULL;
  192: 	CFG_RC_UNLOCK(cfg);
  193: }
  194: 
  195: /*
  196:  * cfgUnloadConfig() - Unload config from memory and destroy resources
  197:  *
  198:  * @cfg = Config root
  199:  * return: none
  200:  */
  201: void
  202: cfgUnloadConfig(cfg_root_t * __restrict cfg)
  203: {
  204: 	if (!cfg)
  205: 		return;
  206: 
  207: 	cfgClearConfig(cfg);
  208: 	pthread_mutex_destroy(&cfg->rc_mtx);
  209: }
  210: 
  211: /*
  212:  * cfgCreateConfig() - Create config file from memory
  213:  *
  214:  * @csConfigName = New config filename
  215:  * @cfg = Config root
  216:  * @whitespace = Additional whitespace characters to file
  217:  * return: -1 error or 0 ok
  218:  */
  219: int
  220: cfgCreateConfig(const char *csConfigName, cfg_root_t * __restrict cfg, int whitespace)
  221: {
  222: 	FILE *f;
  223: 	int ret;
  224: 
  225: 	if (!csConfigName || !cfg)
  226: 		return -1;
  227: 
  228: 	f = fopen(csConfigName, "w");
  229: 	if (!f) {
  230: 		LOGERR;
  231: 		return -1;
  232: 	}
  233: 	
  234: 	ret = cfgWriteConfig(f, cfg, whitespace);
  235: 
  236: 	fclose(f);
  237: 	return ret;
  238: }
  239: 
  240: /*
  241:  * cfgInitPasswd() - Init password root
  242:  *
  243:  * @pwd = Password root
  244:  * return: -1 error or 0 ok
  245:  */
  246: int
  247: cfgInitPasswd(pwd_root_t * __restrict pwd)
  248: {
  249: 	if (!pwd)
  250: 		return -1;
  251: 
  252: 	pthread_mutex_init(&pwd->pwd_mtx, NULL);
  253: 
  254: 	SLIST_INIT(pwd);
  255: 	RB_INIT(pwd);
  256: 	return 0;
  257: }
  258: 
  259: /*
  260:  * cfgLoadPasswd() - Load passwords from file
  261:  *
  262:  * @pwdName = Passwords filename
  263:  * @pwd = Password root
  264:  * return: -1 error or 0 ok
  265:  */
  266: int
  267: cfgLoadPasswd(const char *pwdName, pwd_root_t * __restrict pwd)
  268: {
  269: 	FILE *f;
  270: 	int ret;
  271: 
  272: 	if (!pwdName || !pwd) {
  273: 		cfg_SetErr(EINVAL, "Invalid parameter(s)");
  274: 		return -1;
  275: 	} else
  276: 		cfgInitPasswd(pwd);
  277: 
  278: 	f = fopen(pwdName, "r");
  279: 	if (!f) {
  280: 		LOGERR;
  281: 		return -1;
  282: 	}
  283: 
  284: 	ret = cfgReadPasswd(f, pwd);
  285: 
  286: 	fclose(f);
  287: 	return ret;
  288: }
  289: 
  290: /*
  291:  * cfgClearPasswd() - Clear passwords and free resources
  292:  *
  293:  * @cfg = Password root
  294:  * return: none
  295:  */
  296: void
  297: cfgClearPasswd(pwd_root_t * __restrict pwd)
  298: {
  299: 	struct tagUser *p;
  300: 
  301: 	if (!pwd)
  302: 		return;
  303: 
  304: 	PWD_LOCK(pwd);
  305: 	while ((p = SLIST_FIRST(pwd))) {
  306: 		SLIST_REMOVE_HEAD(pwd, usr_next);
  307: 
  308: 		AIT_FREE_VAL(&p->usr_name);
  309: 		AIT_FREE_VAL(&p->usr_pass);
  310: 		AIT_FREE_VAL(&p->usr_uid);
  311: 		AIT_FREE_VAL(&p->usr_gid);
  312: 		AIT_FREE_VAL(&p->usr_class);
  313: 		AIT_FREE_VAL(&p->usr_change);
  314: 		AIT_FREE_VAL(&p->usr_expire);
  315: 		AIT_FREE_VAL(&p->usr_realm);
  316: 		AIT_FREE_VAL(&p->usr_home);
  317: 		AIT_FREE_VAL(&p->usr_shell);
  318: 		e_free(p);
  319: 	}
  320: 	pwd->rbh_root = NULL;
  321: 	PWD_UNLOCK(pwd);
  322: }
  323: 
  324: /*
  325:  * cfgUnloadPasswd() - Unload passwords from memory and destroy resources
  326:  *
  327:  * @pwd = Password root
  328:  * return: none
  329:  */
  330: void
  331: cfgUnloadPasswd(pwd_root_t * __restrict pwd)
  332: {
  333: 	if (!pwd)
  334: 		return;
  335: 
  336: 	cfgClearPasswd(pwd);
  337: 	pthread_mutex_destroy(&pwd->pwd_mtx);
  338: }
  339: 
  340: /*
  341:  * cfgCreatePasswd() - Create password file from memory
  342:  *
  343:  * @pwdName = New password filename
  344:  * @pwd = Password root
  345:  * return: -1 error or 0 ok
  346:  */
  347: int
  348: cfgCreatePasswd(const char *pwdName, pwd_root_t * __restrict pwd)
  349: {
  350: 	FILE *f;
  351: 	int ret;
  352: 
  353: 	if (!pwdName || !pwd)
  354: 		return -1;
  355: 
  356: 	f = fopen(pwdName, "w");
  357: 	if (!f) {
  358: 		LOGERR;
  359: 		return -1;
  360: 	}
  361: 	
  362: 	ret = cfgWritePasswd(f, pwd);
  363: 
  364: 	fclose(f);
  365: 	return ret;
  366: }

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