10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*8061STon.Nguyen@Sun.COM  * Common Development and Distribution License (the "License").
6*8061STon.Nguyen@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*8061STon.Nguyen@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <stdio.h>
280Sstevel@tonic-gate #include <stdlib.h>
290Sstevel@tonic-gate #include <strings.h>
300Sstevel@tonic-gate #include <secdb.h>
310Sstevel@tonic-gate #include <ctype.h>
320Sstevel@tonic-gate 
330Sstevel@tonic-gate /* From libnsl */
340Sstevel@tonic-gate extern char *_strdup_null(char *);
350Sstevel@tonic-gate extern char *_strtok_escape(char *, char *, char **);
360Sstevel@tonic-gate extern char *_strpbrk_escape(char *, char *);
370Sstevel@tonic-gate extern char *_unescape(char *, char *);
380Sstevel@tonic-gate 
390Sstevel@tonic-gate char *_do_unescape(char *);
400Sstevel@tonic-gate 
410Sstevel@tonic-gate 
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate  * kva_match(): Given a key-value array and a key, return a pointer to the
440Sstevel@tonic-gate  * value that matches the key.
450Sstevel@tonic-gate  */
460Sstevel@tonic-gate char *
470Sstevel@tonic-gate kva_match(kva_t *kva, char *key)
480Sstevel@tonic-gate {
490Sstevel@tonic-gate 	int	i;
500Sstevel@tonic-gate 	kv_t	*data;
510Sstevel@tonic-gate 
520Sstevel@tonic-gate 	if (kva == NULL || key == NULL) {
530Sstevel@tonic-gate 		return ((char *)NULL);
540Sstevel@tonic-gate 	}
550Sstevel@tonic-gate 	data = kva->data;
560Sstevel@tonic-gate 	for (i = 0; i < kva->length; i++) {
570Sstevel@tonic-gate 		if (strcmp(data[i].key, key) == 0) {
580Sstevel@tonic-gate 			return (data[i].value);
590Sstevel@tonic-gate 		}
600Sstevel@tonic-gate 	}
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 	return ((char *)NULL);
630Sstevel@tonic-gate }
640Sstevel@tonic-gate 
650Sstevel@tonic-gate /*
660Sstevel@tonic-gate  * _kva_free(): Free up memory.
670Sstevel@tonic-gate  */
680Sstevel@tonic-gate void
690Sstevel@tonic-gate _kva_free(kva_t *kva)
700Sstevel@tonic-gate {
710Sstevel@tonic-gate 	int	i;
720Sstevel@tonic-gate 	kv_t	*data;
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 	if (kva == NULL) {
750Sstevel@tonic-gate 		return;
760Sstevel@tonic-gate 	}
770Sstevel@tonic-gate 	data = kva->data;
780Sstevel@tonic-gate 	for (i = 0; i < kva->length; i++) {
790Sstevel@tonic-gate 		if (data[i].key != NULL) {
800Sstevel@tonic-gate 			free(data[i].key);
810Sstevel@tonic-gate 			data[i].key = NULL;
820Sstevel@tonic-gate 		}
830Sstevel@tonic-gate 		if (data[i].value != NULL) {
840Sstevel@tonic-gate 			free(data[i].value);
850Sstevel@tonic-gate 			data[i].value = NULL;
860Sstevel@tonic-gate 		}
870Sstevel@tonic-gate 	}
880Sstevel@tonic-gate 	free(kva->data);
890Sstevel@tonic-gate 	free(kva);
900Sstevel@tonic-gate }
910Sstevel@tonic-gate 
920Sstevel@tonic-gate /*
930Sstevel@tonic-gate  * new_kva(): Allocate a key-value array.
940Sstevel@tonic-gate  */
950Sstevel@tonic-gate kva_t  *
960Sstevel@tonic-gate _new_kva(int size)
970Sstevel@tonic-gate {
980Sstevel@tonic-gate 	kva_t	*new_kva;
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	if ((new_kva = (kva_t *)calloc(1, sizeof (kva_t))) == NULL) {
1010Sstevel@tonic-gate 		return ((kva_t *)NULL);
1020Sstevel@tonic-gate 	}
1030Sstevel@tonic-gate 	if ((new_kva->data = (kv_t *)calloc(1, (size*sizeof (kv_t)))) == NULL) {
1040Sstevel@tonic-gate 		free(new_kva);
1050Sstevel@tonic-gate 		return ((kva_t *)NULL);
1060Sstevel@tonic-gate 	}
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	return (new_kva);
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate /*
1120Sstevel@tonic-gate  * _str2kva(): Given a string (s) of key-value pairs, separated by delimeter
1130Sstevel@tonic-gate  * (del), place the values into the key value array (nkva).
1140Sstevel@tonic-gate  */
1150Sstevel@tonic-gate kva_t  *
1160Sstevel@tonic-gate _str2kva(char *s, char *ass, char *del)
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate 	int	n = 0;
1190Sstevel@tonic-gate 	int	m;
1200Sstevel@tonic-gate 	int	size = KV_ADD_KEYS;
1210Sstevel@tonic-gate 	char	*buf;
1220Sstevel@tonic-gate 	char	*p;
1230Sstevel@tonic-gate 	char	*pair;
1240Sstevel@tonic-gate 	char	*key;
1250Sstevel@tonic-gate 	char	*last_pair;
1260Sstevel@tonic-gate 	char	*last_key;
1270Sstevel@tonic-gate 	kv_t	*data;
1280Sstevel@tonic-gate 	kva_t	*nkva;
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	if (s == NULL ||
1310Sstevel@tonic-gate 	    ass == NULL ||
1320Sstevel@tonic-gate 	    del == NULL ||
1330Sstevel@tonic-gate 	    *s == '\0' ||
1340Sstevel@tonic-gate 	    *s == '\n' ||
1350Sstevel@tonic-gate 	    (strlen(s) <= 1)) {
1360Sstevel@tonic-gate 		return ((kva_t *)NULL);
1370Sstevel@tonic-gate 	}
1380Sstevel@tonic-gate 	p = s;
1390Sstevel@tonic-gate 	while ((p = _strpbrk_escape(p, ass)) != NULL) {
1400Sstevel@tonic-gate 		n++;
1410Sstevel@tonic-gate 		p++;
1420Sstevel@tonic-gate 	}
1430Sstevel@tonic-gate 	if (n > size) {
1440Sstevel@tonic-gate 		m = n/size;
1450Sstevel@tonic-gate 		if (n%size) {
1460Sstevel@tonic-gate 			++m;
1470Sstevel@tonic-gate 		}
1480Sstevel@tonic-gate 		size = m * KV_ADD_KEYS;
1490Sstevel@tonic-gate 	}
1500Sstevel@tonic-gate 	if ((nkva = _new_kva(size)) == NULL) {
1510Sstevel@tonic-gate 		return ((kva_t *)NULL);
1520Sstevel@tonic-gate 	}
1530Sstevel@tonic-gate 	data = nkva->data;
1540Sstevel@tonic-gate 	nkva->length = 0;
1550Sstevel@tonic-gate 	if ((buf = strdup(s)) == NULL) {
1560Sstevel@tonic-gate 		return ((kva_t *)NULL);
1570Sstevel@tonic-gate 	}
1580Sstevel@tonic-gate 	pair = _strtok_escape(buf, del, &last_pair);
1590Sstevel@tonic-gate 	do {
1600Sstevel@tonic-gate 		key = _strtok_escape(pair, ass, &last_key);
1610Sstevel@tonic-gate 		if (key != NULL) {
1620Sstevel@tonic-gate 			data[nkva->length].key = _do_unescape(key);
1630Sstevel@tonic-gate 			data[nkva->length].value = _do_unescape(last_key);
1640Sstevel@tonic-gate 			nkva->length++;
1650Sstevel@tonic-gate 		}
1660Sstevel@tonic-gate 	} while ((pair = _strtok_escape(NULL, del, &last_pair)) != NULL);
1670Sstevel@tonic-gate 	free(buf);
1680Sstevel@tonic-gate 	return (nkva);
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate /*
1720Sstevel@tonic-gate  * _kva2str(): Given an array of key-value pairs, place them into a string
1730Sstevel@tonic-gate  * (buf). Use delimeter (del) to separate pairs.  Use assignment character
1740Sstevel@tonic-gate  * (ass) to separate keys and values.
1750Sstevel@tonic-gate  *
1760Sstevel@tonic-gate  * Return Values: 0  Success 1  Buffer too small 2  Out of memory
1770Sstevel@tonic-gate  */
1780Sstevel@tonic-gate int
1790Sstevel@tonic-gate _kva2str(kva_t *kva, char *buf, int buflen, char *ass, char *del)
1800Sstevel@tonic-gate {
1810Sstevel@tonic-gate 	int	i;
1820Sstevel@tonic-gate 	int	length = 0;
1830Sstevel@tonic-gate 	char	*tmp;
1840Sstevel@tonic-gate 	kv_t	*data;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	if (kva == NULL) {
1870Sstevel@tonic-gate 		return (0);
1880Sstevel@tonic-gate 	}
1890Sstevel@tonic-gate 	data = kva->data;
1900Sstevel@tonic-gate 	for (i = 0; i < kva->length; i++) {
1910Sstevel@tonic-gate 		if (data[i].value != NULL) {
1920Sstevel@tonic-gate 			length += 2 + strlen(data[i].value);
1930Sstevel@tonic-gate 		}
1940Sstevel@tonic-gate 	}
1950Sstevel@tonic-gate 	if (length > buflen) {
1960Sstevel@tonic-gate 		return (1);
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate 	(void) memset(buf, 0, buflen);
1990Sstevel@tonic-gate 	if ((tmp = (char *)malloc(buflen)) == NULL) {
2000Sstevel@tonic-gate 		return (2);
2010Sstevel@tonic-gate 	}
2020Sstevel@tonic-gate 	for (i = 0; i < kva->length; i++) {
2030Sstevel@tonic-gate 		if (data[i].value != NULL) {
2040Sstevel@tonic-gate 			if (snprintf(tmp, buflen, "%s%s%s%s",
2050Sstevel@tonic-gate 			    data[i].key, ass, data[i].value, del) >= buflen) {
2060Sstevel@tonic-gate 				return (0);
2070Sstevel@tonic-gate 			}
2080Sstevel@tonic-gate 			(void) strcat(buf, tmp);
2090Sstevel@tonic-gate 		}
2100Sstevel@tonic-gate 	}
2110Sstevel@tonic-gate 	return (0);
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate int
2150Sstevel@tonic-gate _insert2kva(kva_t *kva, char *key, char *value)
2160Sstevel@tonic-gate {
2170Sstevel@tonic-gate 	int	i;
2180Sstevel@tonic-gate 	kv_t	*data;
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	if (kva == NULL) {
2210Sstevel@tonic-gate 		return (0);
2220Sstevel@tonic-gate 	}
2230Sstevel@tonic-gate 	data = kva->data;
2240Sstevel@tonic-gate 	for (i = 0; i < kva->length; i++) {
2250Sstevel@tonic-gate 		if (strcmp(data[i].key, key) == 0) {
2260Sstevel@tonic-gate 			if (data[i].value != NULL)
2270Sstevel@tonic-gate 				free(data[i].value);
2280Sstevel@tonic-gate 			data[i].value = _strdup_null(value);
2290Sstevel@tonic-gate 			return (0);
2300Sstevel@tonic-gate 		}
2310Sstevel@tonic-gate 	}
2320Sstevel@tonic-gate 	return (1);
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate kva_t  *
2360Sstevel@tonic-gate _kva_dup(kva_t *old_kva)
2370Sstevel@tonic-gate {
2380Sstevel@tonic-gate 	int	i;
2390Sstevel@tonic-gate 	int	size;
2400Sstevel@tonic-gate 	kv_t	*old_data;
2410Sstevel@tonic-gate 	kv_t	*new_data;
2420Sstevel@tonic-gate 	kva_t 	*nkva = (kva_t *)NULL;
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 	if (old_kva == NULL) {
2450Sstevel@tonic-gate 		return ((kva_t *)NULL);
2460Sstevel@tonic-gate 	}
2470Sstevel@tonic-gate 	old_data = old_kva->data;
2480Sstevel@tonic-gate 	size = old_kva->length;
2490Sstevel@tonic-gate 	if ((nkva = _new_kva(size)) == NULL) {
2500Sstevel@tonic-gate 		return ((kva_t *)NULL);
2510Sstevel@tonic-gate 	}
2520Sstevel@tonic-gate 	new_data = nkva->data;
2530Sstevel@tonic-gate 	nkva->length = old_kva->length;
2540Sstevel@tonic-gate 	for (i = 0; i <= nkva->length; i++) {
2550Sstevel@tonic-gate 		new_data[i].key = _strdup_null(old_data[i].key);
2560Sstevel@tonic-gate 		new_data[i].value = _strdup_null(old_data[i].value);
2570Sstevel@tonic-gate 	}
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	return (nkva);
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate static void
2630Sstevel@tonic-gate strip_spaces(char **valuep)
2640Sstevel@tonic-gate {
2650Sstevel@tonic-gate 	char *p, *start;
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	/* Find first non-white space character and return pointer to it */
2680Sstevel@tonic-gate 	for (p = *valuep; *p != '\0' && isspace((unsigned char)*p); p++)
2690Sstevel@tonic-gate 		;
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	*valuep = start = p;
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 	if (*p == '\0')
2740Sstevel@tonic-gate 		return;
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	p = p + strlen(p) - 1;
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	/* Remove trailing spaces */
2790Sstevel@tonic-gate 	while (p > start && isspace((unsigned char)*p))
2800Sstevel@tonic-gate 		p--;
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	p[1] = '\0';
2830Sstevel@tonic-gate }
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate char *
2860Sstevel@tonic-gate _do_unescape(char *src)
2870Sstevel@tonic-gate {
2880Sstevel@tonic-gate 	char *tmp = NULL;
2890Sstevel@tonic-gate 	char *dst = NULL;
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	if (src == NULL) {
2920Sstevel@tonic-gate 		dst = _strdup_null(src);
2930Sstevel@tonic-gate 	} else {
2940Sstevel@tonic-gate 		strip_spaces(&src);
2950Sstevel@tonic-gate 		tmp = _unescape(src, "=;:,\\");
2960Sstevel@tonic-gate 		dst = (tmp == NULL) ? _strdup_null(src) : tmp;
2970Sstevel@tonic-gate 	}
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	return (dst);
3000Sstevel@tonic-gate }
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate /*
3040Sstevel@tonic-gate  * Some utilities for handling comma-separated lists.
3050Sstevel@tonic-gate  */
3060Sstevel@tonic-gate char *
3070Sstevel@tonic-gate _argv_to_csl(char **strings)
3080Sstevel@tonic-gate {
3090Sstevel@tonic-gate 	int len = 0;
3100Sstevel@tonic-gate 	int i = 0;
3110Sstevel@tonic-gate 	char *newstr = (char *)NULL;
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	if (strings == NULL)
3140Sstevel@tonic-gate 		return ((char *)NULL);
3150Sstevel@tonic-gate 	for (i = 0; strings[i] != NULL; i++) {
3160Sstevel@tonic-gate 		len += strlen(strings[i]) + 1;
3170Sstevel@tonic-gate 	}
318*8061STon.Nguyen@Sun.COM 	if ((len > 0) && ((newstr = (char *)malloc(len + 1)) != NULL)) {
319*8061STon.Nguyen@Sun.COM 		(void) memset(newstr, 0, len);
320*8061STon.Nguyen@Sun.COM 		for (i = 0; strings[i] != NULL; i++) {
321*8061STon.Nguyen@Sun.COM 			(void) strcat(newstr, strings[i]);
322*8061STon.Nguyen@Sun.COM 			(void) strcat(newstr, ",");
323*8061STon.Nguyen@Sun.COM 		}
324*8061STon.Nguyen@Sun.COM 		newstr[len-1] = NULL;
325*8061STon.Nguyen@Sun.COM 		return (newstr);
326*8061STon.Nguyen@Sun.COM 	} else
3270Sstevel@tonic-gate 		return ((char *)NULL);
3280Sstevel@tonic-gate }
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate char **
3320Sstevel@tonic-gate _csl_to_argv(char *csl)
3330Sstevel@tonic-gate {
3340Sstevel@tonic-gate 	int len = 0;
3350Sstevel@tonic-gate 	int ncommas = 0;
3360Sstevel@tonic-gate 	int i = 0;
3370Sstevel@tonic-gate 	char **spc = (char **)NULL;
3380Sstevel@tonic-gate 	char *copy = (char *)NULL;
3390Sstevel@tonic-gate 	char *pc;
3400Sstevel@tonic-gate 	char *lasts = (char *)NULL;
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	len = strlen(csl);
3430Sstevel@tonic-gate 	for (i = 0; i < len; i++) {
3440Sstevel@tonic-gate 		if (csl[i] == ',')
3450Sstevel@tonic-gate 			ncommas++;
3460Sstevel@tonic-gate 	}
3470Sstevel@tonic-gate 	if ((spc = (char **)malloc((ncommas + 2) * sizeof (char *))) == NULL) {
3480Sstevel@tonic-gate 		return ((char **)NULL);
3490Sstevel@tonic-gate 	}
3500Sstevel@tonic-gate 	copy = strdup(csl);
3510Sstevel@tonic-gate 	for (pc = strtok_r(copy, ",", &lasts), i = 0; pc != NULL;
3520Sstevel@tonic-gate 	    pc = strtok_r(NULL, ",", &lasts), i++) {
3530Sstevel@tonic-gate 		spc[i] = strdup(pc);
3540Sstevel@tonic-gate 	}
3550Sstevel@tonic-gate 	spc[i] = NULL;
3560Sstevel@tonic-gate 	free(copy);
3570Sstevel@tonic-gate 	return (spc);
3580Sstevel@tonic-gate }
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate void
3620Sstevel@tonic-gate _free_argv(char **p_argv)
3630Sstevel@tonic-gate {
3640Sstevel@tonic-gate 	char **p_a;
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	for (p_a = p_argv; *p_a != NULL; p_a++)
3670Sstevel@tonic-gate 		free(*p_a);
3680Sstevel@tonic-gate 	free(p_argv);
3690Sstevel@tonic-gate }
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate #ifdef DEBUG
3730Sstevel@tonic-gate void
3740Sstevel@tonic-gate print_kva(kva_t *kva)
3750Sstevel@tonic-gate {
3760Sstevel@tonic-gate 	int	i;
3770Sstevel@tonic-gate 	kv_t	*data;
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	if (kva == NULL) {
3800Sstevel@tonic-gate 		printf("  (empty)\n");
3810Sstevel@tonic-gate 		return;
3820Sstevel@tonic-gate 	}
3830Sstevel@tonic-gate 	data = kva->data;
3840Sstevel@tonic-gate 	for (i = 0; i < kva->length; i++) {
3850Sstevel@tonic-gate 		printf("  %s = %s\n", data[i].key, data[i].value);
3860Sstevel@tonic-gate 	}
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate #endif  /* DEBUG */
389