File:  [ELWIX - Embedded LightWeight unIX -] / libaitcfg / src / pq.c
Revision 1.5: download - view: text, annotated - select for diffs - revision graph
Wed Jan 29 23:48:34 2014 UTC (10 years, 2 months ago) by misho
Branches: MAIN
CVS tags: cfg9_5, cfg9_4, cfg9_3, cfg9_2, cfg9_1, cfg8_2, cfg8_1, cfg8_0, cfg7_9, cfg7_8, cfg7_7, cfg7_6, cfg7_5, cfg7_4, cfg7_3, HEAD, CFG9_4, CFG9_3, CFG9_2, CFG9_1, CFG9_0, CFG8_1, CFG8_0, CFG7_9, CFG7_8, CFG7_7, CFG7_6, CFG7_5, CFG7_4, CFG7_3, CFG7_2
version 7.2

/*************************************************************************
* (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
*  by Michael Pounov <misho@openbsd-bg.org>
*
* $Author: misho $
* $Id: pq.c,v 1.5 2014/01/29 23:48:34 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 <info@elwix.org>

Copyright 2004 - 2014
	by Michael Pounov <misho@elwix.org>.  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 <misho@elwix.org>
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.
*/
#include "global.h"


static inline struct tagUser *
_selectPasswd(pwd_root_t * __restrict pwd, u_int uid, const char *csName)
{
	struct tagUser fu;

	if (!pwd)
		return NULL;
	else
		memset(&fu, 0, sizeof fu);

	if (csName) {
		ait_setlikeVar(&fu.usr_name, string, strlen(csName) + 1, csName);
		return RB_FIND(tagPWD, pwd, &fu);
	}

	return (struct tagUser*) cfg_findPasswdBy(pwd, PWD_CRIT_UID, uid);
}

/* --------------------------------------------------------------- */

/*
 * 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, ...)
{
	struct tagUser *u;
	va_list lst;
	ait_val_t v;

	if (!pwd)
		return NULL;
	else
		AIT_INIT_VAL(&v);

	va_start(lst, criteria);
	switch (criteria) {
		case PWD_CRIT_NAME:
			AIT_SET_STR(&v, va_arg(lst, char*));
			break;
		case PWD_CRIT_UID:
		case PWD_CRIT_GID:
			AIT_SET_U32(&v, va_arg(lst, u_int));
			break;
		default:
			return NULL;
	}
	va_end(lst);

	SLIST_FOREACH(u, pwd, usr_next)
		switch (criteria) {
			case PWD_CRIT_NAME:
				if (!ait_cmpVar(&u->usr_name, &v)) {
					AIT_FREE_VAL(&v);
					return u;
				}
				break;
			case PWD_CRIT_UID:
				if ((u_int) AIT_RAW(&u->usr_uid) == AIT_GET_U32(&v)) {
					AIT_FREE_VAL(&v);
					return u;
				}
				break;
			case PWD_CRIT_GID:
				if ((u_int) AIT_RAW(&u->usr_gid) == AIT_GET_U32(&v)) {
					AIT_FREE_VAL(&v);
					return u;
				}
				break;
		}

	AIT_FREE_VAL(&v);
	return NULL;
}

/*
 * 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, ...)
{
	struct tagUser *u, *u2;
	va_list lst;
	u_int n = 0;
	char *name = NULL;

	if (!pwd)
		return -1;

	va_start(lst, criteria);
	switch (criteria) {
		case PWD_CRIT_UID:
			n = va_arg(lst, u_int);
			break;
		case PWD_CRIT_NAME:
			name = va_arg(lst, char*);
			if (name)
				break;
		default:
			va_end(lst);
			return -1;
	}
	va_end(lst);

	u = _selectPasswd(pwd, n, name);
	if (!u)
		return 0;

	PWD_LOCK(pwd);
	RB_REMOVE(tagPWD, pwd, u);
	SLIST_REMOVE(pwd, u, tagUser, usr_next);

	/* if duplicates exists, then update r/b tree */
	SLIST_FOREACH(u2, pwd, usr_next)
		if (!AIT_ISEMPTY(&u2->usr_name) && 
				!strcmp(AIT_GET_STR(&u->usr_name), AIT_GET_STR(&u2->usr_name))) {
			RB_INSERT(tagPWD, pwd, u2);
			break;
		}
	PWD_UNLOCK(pwd);

	AIT_FREE_VAL(&u->usr_name);
	AIT_FREE_VAL(&u->usr_pass);
	AIT_FREE_VAL(&u->usr_uid);
	AIT_FREE_VAL(&u->usr_gid);
	AIT_FREE_VAL(&u->usr_class);
	AIT_FREE_VAL(&u->usr_change);
	AIT_FREE_VAL(&u->usr_expire);
	AIT_FREE_VAL(&u->usr_realm);
	AIT_FREE_VAL(&u->usr_home);
	AIT_FREE_VAL(&u->usr_shell);
	e_free(u);
	return 1;
}

/*
 * cfg_setPasswd() - Set item in password or adding new item if not exists
 *
 * @cfg = Password root
 * @fields = Following parameters are continuous to certain field
 * @csName = Username
 * @arg1 = Password
 * @arg2 = UID
 * @arg3 = GID
 * @arg4 = Login class
 * @arg5 = Change 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, passwd_attr_t fields, const char *csName, ...) 
{
	struct tagUser *u;
	register int i;
	va_list lst;

	if (!pwd || !csName)
		return -1;

	u = _selectPasswd(pwd, 0, csName);
	if (!u) {
		/* adding new element */
		u = e_malloc(sizeof(struct tagUser));
		if (!u) {
			cfg_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
			return -1;
		} else {
			memset(u, 0, sizeof(struct tagUser));
			if (fields >= Username && fields <= Shell)
				u->usr_fields = (int) fields;
			else
				u->usr_fields = PWD_MAX_FIELDS - 1;

			PWD_LOCK(pwd);
			SLIST_INSERT_HEAD(pwd, u, usr_next);
			PWD_UNLOCK(pwd);
		}

		va_start(lst, csName);
		for (i = 0; i < (u->usr_fields + 1); i++)
			switch (i) {
				case 0:
					AIT_SET_STR(&u->usr_name, csName);
					break;
				case 1:
					AIT_SET_STR(&u->usr_pass, va_arg(lst, char*));
					break;
				case 2:
					AIT_SET_U32(&u->usr_uid, va_arg(lst, u_int));
					break;
				case 3:
					AIT_SET_U32(&u->usr_gid, va_arg(lst, u_int));
					break;
				case 4:
					AIT_SET_STR(&u->usr_class, va_arg(lst, char*));
					break;
				case 5:
					AIT_SET_U32(&u->usr_change, va_arg(lst, u_int));
					break;
				case 6:
					AIT_SET_U32(&u->usr_expire, va_arg(lst, u_int));
					break;
				case 7:
					AIT_SET_STR(&u->usr_realm, va_arg(lst, char*));
					break;
				case 8:
					AIT_SET_STR(&u->usr_home, va_arg(lst, char*));
					break;
				case 9:
					AIT_SET_STR(&u->usr_shell, va_arg(lst, char*));
					break;
			}
		va_end(lst);

		AIT_KEY(&u->usr_name) = crcFletcher16(AIT_GET_LIKE(&u->usr_name, u_short*), 
				E_ALIGN(AIT_LEN(&u->usr_name) - 1, 2) / 2);

		PWD_LOCK(pwd);
		RB_INSERT(tagPWD, pwd, u);
		PWD_UNLOCK(pwd);
		return 2;
	} else {
		AIT_FREE_VAL(&u->usr_pass);
		AIT_FREE_VAL(&u->usr_uid);
		AIT_FREE_VAL(&u->usr_gid);
		AIT_FREE_VAL(&u->usr_class);
		AIT_FREE_VAL(&u->usr_change);
		AIT_FREE_VAL(&u->usr_expire);
		AIT_FREE_VAL(&u->usr_realm);
		AIT_FREE_VAL(&u->usr_home);
		AIT_FREE_VAL(&u->usr_shell);

		/* Update element */
		va_start(lst, csName);
		for (i = 1; i < (u->usr_fields + 1); i++)
			switch (i) {
				case 1:
					AIT_SET_STR(&u->usr_pass, va_arg(lst, char*));
					break;
				case 2:
					AIT_SET_U32(&u->usr_uid, va_arg(lst, u_int));
					break;
				case 3:
					AIT_SET_U32(&u->usr_gid, va_arg(lst, u_int));
					break;
				case 4:
					AIT_SET_STR(&u->usr_class, va_arg(lst, char*));
					break;
				case 5:
					AIT_SET_U32(&u->usr_change, va_arg(lst, u_int));
					break;
				case 6:
					AIT_SET_U32(&u->usr_expire, va_arg(lst, u_int));
					break;
				case 7:
					AIT_SET_STR(&u->usr_realm, va_arg(lst, char*));
					break;
				case 8:
					AIT_SET_STR(&u->usr_home, va_arg(lst, char*));
					break;
				case 9:
					AIT_SET_STR(&u->usr_shell, va_arg(lst, char*));
					break;
			}
		va_end(lst);
		return 1;
	}

	/* Nothing happens ... found & values is equal! */
	return 0;
}

/*
 * 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
 */
const struct tagUser *
cfg_getPasswd(pwd_root_t * __restrict pwd, int criteria, ...)
{
	struct tagUser *u;
	va_list lst;
	char *str;

	if (!pwd)
		return NULL;

	va_start(lst, criteria);
	switch (criteria) {
		case PWD_CRIT_NAME:
			str = va_arg(lst, char*);
			if (!str)
				u = NULL;
			else
				u = _selectPasswd(pwd, 0, str);
			break;
		case PWD_CRIT_UID:
			u = _selectPasswd(pwd, va_arg(lst, u_int), NULL);
			break;
		default:
			u = NULL;
			break;
	}
	va_end(lst);

	return u;
}

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