1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <stdio.h>
30*0Sstevel@tonic-gate #include <stdlib.h>
31*0Sstevel@tonic-gate #include <strings.h>
32*0Sstevel@tonic-gate #include <secdb.h>
33*0Sstevel@tonic-gate #include <ctype.h>
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate /* From libnsl */
36*0Sstevel@tonic-gate extern char *_strdup_null(char *);
37*0Sstevel@tonic-gate extern char *_strtok_escape(char *, char *, char **);
38*0Sstevel@tonic-gate extern char *_strpbrk_escape(char *, char *);
39*0Sstevel@tonic-gate extern char *_unescape(char *, char *);
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate char *_do_unescape(char *);
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate /*
45*0Sstevel@tonic-gate  * kva_match(): Given a key-value array and a key, return a pointer to the
46*0Sstevel@tonic-gate  * value that matches the key.
47*0Sstevel@tonic-gate  */
48*0Sstevel@tonic-gate char *
49*0Sstevel@tonic-gate kva_match(kva_t *kva, char *key)
50*0Sstevel@tonic-gate {
51*0Sstevel@tonic-gate 	int	i;
52*0Sstevel@tonic-gate 	kv_t	*data;
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate 	if (kva == NULL || key == NULL) {
55*0Sstevel@tonic-gate 		return ((char *)NULL);
56*0Sstevel@tonic-gate 	}
57*0Sstevel@tonic-gate 	data = kva->data;
58*0Sstevel@tonic-gate 	for (i = 0; i < kva->length; i++) {
59*0Sstevel@tonic-gate 		if (strcmp(data[i].key, key) == 0) {
60*0Sstevel@tonic-gate 			return (data[i].value);
61*0Sstevel@tonic-gate 		}
62*0Sstevel@tonic-gate 	}
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate 	return ((char *)NULL);
65*0Sstevel@tonic-gate }
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate /*
68*0Sstevel@tonic-gate  * _kva_free(): Free up memory.
69*0Sstevel@tonic-gate  */
70*0Sstevel@tonic-gate void
71*0Sstevel@tonic-gate _kva_free(kva_t *kva)
72*0Sstevel@tonic-gate {
73*0Sstevel@tonic-gate 	int	i;
74*0Sstevel@tonic-gate 	kv_t	*data;
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate 	if (kva == NULL) {
77*0Sstevel@tonic-gate 		return;
78*0Sstevel@tonic-gate 	}
79*0Sstevel@tonic-gate 	data = kva->data;
80*0Sstevel@tonic-gate 	for (i = 0; i < kva->length; i++) {
81*0Sstevel@tonic-gate 		if (data[i].key != NULL) {
82*0Sstevel@tonic-gate 			free(data[i].key);
83*0Sstevel@tonic-gate 			data[i].key = NULL;
84*0Sstevel@tonic-gate 		}
85*0Sstevel@tonic-gate 		if (data[i].value != NULL) {
86*0Sstevel@tonic-gate 			free(data[i].value);
87*0Sstevel@tonic-gate 			data[i].value = NULL;
88*0Sstevel@tonic-gate 		}
89*0Sstevel@tonic-gate 	}
90*0Sstevel@tonic-gate 	free(kva->data);
91*0Sstevel@tonic-gate 	free(kva);
92*0Sstevel@tonic-gate }
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate /*
95*0Sstevel@tonic-gate  * new_kva(): Allocate a key-value array.
96*0Sstevel@tonic-gate  */
97*0Sstevel@tonic-gate kva_t  *
98*0Sstevel@tonic-gate _new_kva(int size)
99*0Sstevel@tonic-gate {
100*0Sstevel@tonic-gate 	kva_t	*new_kva;
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 	if ((new_kva = (kva_t *)calloc(1, sizeof (kva_t))) == NULL) {
103*0Sstevel@tonic-gate 		return ((kva_t *)NULL);
104*0Sstevel@tonic-gate 	}
105*0Sstevel@tonic-gate 	if ((new_kva->data = (kv_t *)calloc(1, (size*sizeof (kv_t)))) == NULL) {
106*0Sstevel@tonic-gate 		free(new_kva);
107*0Sstevel@tonic-gate 		return ((kva_t *)NULL);
108*0Sstevel@tonic-gate 	}
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 	return (new_kva);
111*0Sstevel@tonic-gate }
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate /*
114*0Sstevel@tonic-gate  * _str2kva(): Given a string (s) of key-value pairs, separated by delimeter
115*0Sstevel@tonic-gate  * (del), place the values into the key value array (nkva).
116*0Sstevel@tonic-gate  */
117*0Sstevel@tonic-gate kva_t  *
118*0Sstevel@tonic-gate _str2kva(char *s, char *ass, char *del)
119*0Sstevel@tonic-gate {
120*0Sstevel@tonic-gate 	int	n = 0;
121*0Sstevel@tonic-gate 	int	m;
122*0Sstevel@tonic-gate 	int	size = KV_ADD_KEYS;
123*0Sstevel@tonic-gate 	char	*buf;
124*0Sstevel@tonic-gate 	char	*p;
125*0Sstevel@tonic-gate 	char	*pair;
126*0Sstevel@tonic-gate 	char	*key;
127*0Sstevel@tonic-gate 	char	*last_pair;
128*0Sstevel@tonic-gate 	char	*last_key;
129*0Sstevel@tonic-gate 	kv_t	*data;
130*0Sstevel@tonic-gate 	kva_t	*nkva;
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 	if (s == NULL ||
133*0Sstevel@tonic-gate 	    ass == NULL ||
134*0Sstevel@tonic-gate 	    del == NULL ||
135*0Sstevel@tonic-gate 	    *s == '\0' ||
136*0Sstevel@tonic-gate 	    *s == '\n' ||
137*0Sstevel@tonic-gate 	    (strlen(s) <= 1)) {
138*0Sstevel@tonic-gate 		return ((kva_t *)NULL);
139*0Sstevel@tonic-gate 	}
140*0Sstevel@tonic-gate 	p = s;
141*0Sstevel@tonic-gate 	while ((p = _strpbrk_escape(p, ass)) != NULL) {
142*0Sstevel@tonic-gate 		n++;
143*0Sstevel@tonic-gate 		p++;
144*0Sstevel@tonic-gate 	}
145*0Sstevel@tonic-gate 	if (n > size) {
146*0Sstevel@tonic-gate 		m = n/size;
147*0Sstevel@tonic-gate 		if (n%size) {
148*0Sstevel@tonic-gate 			++m;
149*0Sstevel@tonic-gate 		}
150*0Sstevel@tonic-gate 		size = m * KV_ADD_KEYS;
151*0Sstevel@tonic-gate 	}
152*0Sstevel@tonic-gate 	if ((nkva = _new_kva(size)) == NULL) {
153*0Sstevel@tonic-gate 		return ((kva_t *)NULL);
154*0Sstevel@tonic-gate 	}
155*0Sstevel@tonic-gate 	data = nkva->data;
156*0Sstevel@tonic-gate 	nkva->length = 0;
157*0Sstevel@tonic-gate 	if ((buf = strdup(s)) == NULL) {
158*0Sstevel@tonic-gate 		return ((kva_t *)NULL);
159*0Sstevel@tonic-gate 	}
160*0Sstevel@tonic-gate 	pair = _strtok_escape(buf, del, &last_pair);
161*0Sstevel@tonic-gate 	do {
162*0Sstevel@tonic-gate 		key = _strtok_escape(pair, ass, &last_key);
163*0Sstevel@tonic-gate 		if (key != NULL) {
164*0Sstevel@tonic-gate 			data[nkva->length].key = _do_unescape(key);
165*0Sstevel@tonic-gate 			data[nkva->length].value = _do_unescape(last_key);
166*0Sstevel@tonic-gate 			nkva->length++;
167*0Sstevel@tonic-gate 		}
168*0Sstevel@tonic-gate 	} while ((pair = _strtok_escape(NULL, del, &last_pair)) != NULL);
169*0Sstevel@tonic-gate 	free(buf);
170*0Sstevel@tonic-gate 	return (nkva);
171*0Sstevel@tonic-gate }
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate /*
174*0Sstevel@tonic-gate  * _kva2str(): Given an array of key-value pairs, place them into a string
175*0Sstevel@tonic-gate  * (buf). Use delimeter (del) to separate pairs.  Use assignment character
176*0Sstevel@tonic-gate  * (ass) to separate keys and values.
177*0Sstevel@tonic-gate  *
178*0Sstevel@tonic-gate  * Return Values: 0  Success 1  Buffer too small 2  Out of memory
179*0Sstevel@tonic-gate  */
180*0Sstevel@tonic-gate int
181*0Sstevel@tonic-gate _kva2str(kva_t *kva, char *buf, int buflen, char *ass, char *del)
182*0Sstevel@tonic-gate {
183*0Sstevel@tonic-gate 	int	i;
184*0Sstevel@tonic-gate 	int	length = 0;
185*0Sstevel@tonic-gate 	char	*tmp;
186*0Sstevel@tonic-gate 	kv_t	*data;
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 	if (kva == NULL) {
189*0Sstevel@tonic-gate 		return (0);
190*0Sstevel@tonic-gate 	}
191*0Sstevel@tonic-gate 	data = kva->data;
192*0Sstevel@tonic-gate 	for (i = 0; i < kva->length; i++) {
193*0Sstevel@tonic-gate 		if (data[i].value != NULL) {
194*0Sstevel@tonic-gate 			length += 2 + strlen(data[i].value);
195*0Sstevel@tonic-gate 		}
196*0Sstevel@tonic-gate 	}
197*0Sstevel@tonic-gate 	if (length > buflen) {
198*0Sstevel@tonic-gate 		return (1);
199*0Sstevel@tonic-gate 	}
200*0Sstevel@tonic-gate 	(void) memset(buf, 0, buflen);
201*0Sstevel@tonic-gate 	if ((tmp = (char *)malloc(buflen)) == NULL) {
202*0Sstevel@tonic-gate 		return (2);
203*0Sstevel@tonic-gate 	}
204*0Sstevel@tonic-gate 	for (i = 0; i < kva->length; i++) {
205*0Sstevel@tonic-gate 		if (data[i].value != NULL) {
206*0Sstevel@tonic-gate 			if (snprintf(tmp, buflen, "%s%s%s%s",
207*0Sstevel@tonic-gate 			    data[i].key, ass, data[i].value, del) >= buflen) {
208*0Sstevel@tonic-gate 				return (0);
209*0Sstevel@tonic-gate 			}
210*0Sstevel@tonic-gate 			(void) strcat(buf, tmp);
211*0Sstevel@tonic-gate 		}
212*0Sstevel@tonic-gate 	}
213*0Sstevel@tonic-gate 	return (0);
214*0Sstevel@tonic-gate }
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate int
217*0Sstevel@tonic-gate _insert2kva(kva_t *kva, char *key, char *value)
218*0Sstevel@tonic-gate {
219*0Sstevel@tonic-gate 	int	i;
220*0Sstevel@tonic-gate 	kv_t	*data;
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 	if (kva == NULL) {
223*0Sstevel@tonic-gate 		return (0);
224*0Sstevel@tonic-gate 	}
225*0Sstevel@tonic-gate 	data = kva->data;
226*0Sstevel@tonic-gate 	for (i = 0; i < kva->length; i++) {
227*0Sstevel@tonic-gate 		if (strcmp(data[i].key, key) == 0) {
228*0Sstevel@tonic-gate 			if (data[i].value != NULL)
229*0Sstevel@tonic-gate 				free(data[i].value);
230*0Sstevel@tonic-gate 			data[i].value = _strdup_null(value);
231*0Sstevel@tonic-gate 			return (0);
232*0Sstevel@tonic-gate 		}
233*0Sstevel@tonic-gate 	}
234*0Sstevel@tonic-gate 	return (1);
235*0Sstevel@tonic-gate }
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate kva_t  *
238*0Sstevel@tonic-gate _kva_dup(kva_t *old_kva)
239*0Sstevel@tonic-gate {
240*0Sstevel@tonic-gate 	int	i;
241*0Sstevel@tonic-gate 	int	size;
242*0Sstevel@tonic-gate 	kv_t	*old_data;
243*0Sstevel@tonic-gate 	kv_t	*new_data;
244*0Sstevel@tonic-gate 	kva_t 	*nkva = (kva_t *)NULL;
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate 	if (old_kva == NULL) {
247*0Sstevel@tonic-gate 		return ((kva_t *)NULL);
248*0Sstevel@tonic-gate 	}
249*0Sstevel@tonic-gate 	old_data = old_kva->data;
250*0Sstevel@tonic-gate 	size = old_kva->length;
251*0Sstevel@tonic-gate 	if ((nkva = _new_kva(size)) == NULL) {
252*0Sstevel@tonic-gate 		return ((kva_t *)NULL);
253*0Sstevel@tonic-gate 	}
254*0Sstevel@tonic-gate 	new_data = nkva->data;
255*0Sstevel@tonic-gate 	nkva->length = old_kva->length;
256*0Sstevel@tonic-gate 	for (i = 0; i <= nkva->length; i++) {
257*0Sstevel@tonic-gate 		new_data[i].key = _strdup_null(old_data[i].key);
258*0Sstevel@tonic-gate 		new_data[i].value = _strdup_null(old_data[i].value);
259*0Sstevel@tonic-gate 	}
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate 	return (nkva);
262*0Sstevel@tonic-gate }
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate static void
265*0Sstevel@tonic-gate strip_spaces(char **valuep)
266*0Sstevel@tonic-gate {
267*0Sstevel@tonic-gate 	char *p, *start;
268*0Sstevel@tonic-gate 
269*0Sstevel@tonic-gate 	/* Find first non-white space character and return pointer to it */
270*0Sstevel@tonic-gate 	for (p = *valuep; *p != '\0' && isspace((unsigned char)*p); p++)
271*0Sstevel@tonic-gate 		;
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate 	*valuep = start = p;
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate 	if (*p == '\0')
276*0Sstevel@tonic-gate 		return;
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate 	p = p + strlen(p) - 1;
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate 	/* Remove trailing spaces */
281*0Sstevel@tonic-gate 	while (p > start && isspace((unsigned char)*p))
282*0Sstevel@tonic-gate 		p--;
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate 	p[1] = '\0';
285*0Sstevel@tonic-gate }
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate char *
288*0Sstevel@tonic-gate _do_unescape(char *src)
289*0Sstevel@tonic-gate {
290*0Sstevel@tonic-gate 	char *tmp = NULL;
291*0Sstevel@tonic-gate 	char *dst = NULL;
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate 	if (src == NULL) {
294*0Sstevel@tonic-gate 		dst = _strdup_null(src);
295*0Sstevel@tonic-gate 	} else {
296*0Sstevel@tonic-gate 		strip_spaces(&src);
297*0Sstevel@tonic-gate 		tmp = _unescape(src, "=;:,\\");
298*0Sstevel@tonic-gate 		dst = (tmp == NULL) ? _strdup_null(src) : tmp;
299*0Sstevel@tonic-gate 	}
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate 	return (dst);
302*0Sstevel@tonic-gate }
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate 
305*0Sstevel@tonic-gate /*
306*0Sstevel@tonic-gate  * Some utilities for handling comma-separated lists.
307*0Sstevel@tonic-gate  */
308*0Sstevel@tonic-gate char *
309*0Sstevel@tonic-gate _argv_to_csl(char **strings)
310*0Sstevel@tonic-gate {
311*0Sstevel@tonic-gate 	int len = 0;
312*0Sstevel@tonic-gate 	int i = 0;
313*0Sstevel@tonic-gate 	char *newstr = (char *)NULL;
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate 	if (strings == NULL)
316*0Sstevel@tonic-gate 		return ((char *)NULL);
317*0Sstevel@tonic-gate 	for (i = 0; strings[i] != NULL; i++) {
318*0Sstevel@tonic-gate 		len += strlen(strings[i]) + 1;
319*0Sstevel@tonic-gate 	}
320*0Sstevel@tonic-gate 	if ((newstr = (char *)malloc(len + 1)) == NULL) {
321*0Sstevel@tonic-gate 		return ((char *)NULL);
322*0Sstevel@tonic-gate 	}
323*0Sstevel@tonic-gate 	(void) memset(newstr, 0, len);
324*0Sstevel@tonic-gate 	for (i = 0; strings[i] != NULL; i++) {
325*0Sstevel@tonic-gate 		(void) strcat(newstr, strings[i]);
326*0Sstevel@tonic-gate 		(void) strcat(newstr, ",");
327*0Sstevel@tonic-gate 	}
328*0Sstevel@tonic-gate 	newstr[len-1] = NULL;
329*0Sstevel@tonic-gate 	return (newstr);
330*0Sstevel@tonic-gate }
331*0Sstevel@tonic-gate 
332*0Sstevel@tonic-gate 
333*0Sstevel@tonic-gate char **
334*0Sstevel@tonic-gate _csl_to_argv(char *csl)
335*0Sstevel@tonic-gate {
336*0Sstevel@tonic-gate 	int len = 0;
337*0Sstevel@tonic-gate 	int ncommas = 0;
338*0Sstevel@tonic-gate 	int i = 0;
339*0Sstevel@tonic-gate 	char **spc = (char **)NULL;
340*0Sstevel@tonic-gate 	char *copy = (char *)NULL;
341*0Sstevel@tonic-gate 	char *pc;
342*0Sstevel@tonic-gate 	char *lasts = (char *)NULL;
343*0Sstevel@tonic-gate 
344*0Sstevel@tonic-gate 	len = strlen(csl);
345*0Sstevel@tonic-gate 	for (i = 0; i < len; i++) {
346*0Sstevel@tonic-gate 		if (csl[i] == ',')
347*0Sstevel@tonic-gate 			ncommas++;
348*0Sstevel@tonic-gate 	}
349*0Sstevel@tonic-gate 	if ((spc = (char **)malloc((ncommas + 2) * sizeof (char *))) == NULL) {
350*0Sstevel@tonic-gate 		return ((char **)NULL);
351*0Sstevel@tonic-gate 	}
352*0Sstevel@tonic-gate 	copy = strdup(csl);
353*0Sstevel@tonic-gate 	for (pc = strtok_r(copy, ",", &lasts), i = 0; pc != NULL;
354*0Sstevel@tonic-gate 	    pc = strtok_r(NULL, ",", &lasts), i++) {
355*0Sstevel@tonic-gate 		spc[i] = strdup(pc);
356*0Sstevel@tonic-gate 	}
357*0Sstevel@tonic-gate 	spc[i] = NULL;
358*0Sstevel@tonic-gate 	free(copy);
359*0Sstevel@tonic-gate 	return (spc);
360*0Sstevel@tonic-gate }
361*0Sstevel@tonic-gate 
362*0Sstevel@tonic-gate 
363*0Sstevel@tonic-gate void
364*0Sstevel@tonic-gate _free_argv(char **p_argv)
365*0Sstevel@tonic-gate {
366*0Sstevel@tonic-gate 	char **p_a;
367*0Sstevel@tonic-gate 
368*0Sstevel@tonic-gate 	for (p_a = p_argv; *p_a != NULL; p_a++)
369*0Sstevel@tonic-gate 		free(*p_a);
370*0Sstevel@tonic-gate 	free(p_argv);
371*0Sstevel@tonic-gate }
372*0Sstevel@tonic-gate 
373*0Sstevel@tonic-gate 
374*0Sstevel@tonic-gate #ifdef DEBUG
375*0Sstevel@tonic-gate void
376*0Sstevel@tonic-gate print_kva(kva_t *kva)
377*0Sstevel@tonic-gate {
378*0Sstevel@tonic-gate 	int	i;
379*0Sstevel@tonic-gate 	kv_t	*data;
380*0Sstevel@tonic-gate 
381*0Sstevel@tonic-gate 	if (kva == NULL) {
382*0Sstevel@tonic-gate 		printf("  (empty)\n");
383*0Sstevel@tonic-gate 		return;
384*0Sstevel@tonic-gate 	}
385*0Sstevel@tonic-gate 	data = kva->data;
386*0Sstevel@tonic-gate 	for (i = 0; i < kva->length; i++) {
387*0Sstevel@tonic-gate 		printf("  %s = %s\n", data[i].key, data[i].value);
388*0Sstevel@tonic-gate 	}
389*0Sstevel@tonic-gate }
390*0Sstevel@tonic-gate #endif  /* DEBUG */
391