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
58061STon.Nguyen@Sun.COM * Common Development and Distribution License (the "License").
68061STon.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 /*
2212152SJan.Friedel@Sun.COM * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include <stdio.h>
270Sstevel@tonic-gate #include <stdlib.h>
280Sstevel@tonic-gate #include <strings.h>
290Sstevel@tonic-gate #include <secdb.h>
300Sstevel@tonic-gate #include <ctype.h>
310Sstevel@tonic-gate
320Sstevel@tonic-gate /* From libnsl */
330Sstevel@tonic-gate extern char *_strdup_null(char *);
340Sstevel@tonic-gate extern char *_strtok_escape(char *, char *, char **);
350Sstevel@tonic-gate extern char *_strpbrk_escape(char *, char *);
360Sstevel@tonic-gate extern char *_unescape(char *, char *);
370Sstevel@tonic-gate
380Sstevel@tonic-gate char *_do_unescape(char *);
390Sstevel@tonic-gate
400Sstevel@tonic-gate
410Sstevel@tonic-gate /*
420Sstevel@tonic-gate * kva_match(): Given a key-value array and a key, return a pointer to the
430Sstevel@tonic-gate * value that matches the key.
440Sstevel@tonic-gate */
450Sstevel@tonic-gate char *
kva_match(kva_t * kva,char * key)460Sstevel@tonic-gate kva_match(kva_t *kva, char *key)
470Sstevel@tonic-gate {
480Sstevel@tonic-gate int i;
490Sstevel@tonic-gate kv_t *data;
500Sstevel@tonic-gate
510Sstevel@tonic-gate if (kva == NULL || key == NULL) {
52*12918SJan.Friedel@Sun.COM return (NULL);
530Sstevel@tonic-gate }
540Sstevel@tonic-gate data = kva->data;
550Sstevel@tonic-gate for (i = 0; i < kva->length; i++) {
560Sstevel@tonic-gate if (strcmp(data[i].key, key) == 0) {
570Sstevel@tonic-gate return (data[i].value);
580Sstevel@tonic-gate }
590Sstevel@tonic-gate }
600Sstevel@tonic-gate
61*12918SJan.Friedel@Sun.COM return (NULL);
620Sstevel@tonic-gate }
630Sstevel@tonic-gate
640Sstevel@tonic-gate /*
650Sstevel@tonic-gate * _kva_free(): Free up memory.
660Sstevel@tonic-gate */
670Sstevel@tonic-gate void
_kva_free(kva_t * kva)680Sstevel@tonic-gate _kva_free(kva_t *kva)
690Sstevel@tonic-gate {
700Sstevel@tonic-gate int i;
710Sstevel@tonic-gate kv_t *data;
720Sstevel@tonic-gate
730Sstevel@tonic-gate if (kva == NULL) {
740Sstevel@tonic-gate return;
750Sstevel@tonic-gate }
760Sstevel@tonic-gate data = kva->data;
770Sstevel@tonic-gate for (i = 0; i < kva->length; i++) {
780Sstevel@tonic-gate if (data[i].key != NULL) {
790Sstevel@tonic-gate free(data[i].key);
800Sstevel@tonic-gate data[i].key = NULL;
810Sstevel@tonic-gate }
820Sstevel@tonic-gate if (data[i].value != NULL) {
830Sstevel@tonic-gate free(data[i].value);
840Sstevel@tonic-gate data[i].value = NULL;
850Sstevel@tonic-gate }
860Sstevel@tonic-gate }
870Sstevel@tonic-gate free(kva->data);
880Sstevel@tonic-gate free(kva);
890Sstevel@tonic-gate }
900Sstevel@tonic-gate
910Sstevel@tonic-gate /*
92*12918SJan.Friedel@Sun.COM * _kva_free_value(): Free up memory (value) for all the occurrences of
93*12918SJan.Friedel@Sun.COM * the given key.
94*12918SJan.Friedel@Sun.COM */
95*12918SJan.Friedel@Sun.COM void
_kva_free_value(kva_t * kva,char * key)96*12918SJan.Friedel@Sun.COM _kva_free_value(kva_t *kva, char *key)
97*12918SJan.Friedel@Sun.COM {
98*12918SJan.Friedel@Sun.COM int ctr;
99*12918SJan.Friedel@Sun.COM kv_t *data;
100*12918SJan.Friedel@Sun.COM
101*12918SJan.Friedel@Sun.COM if (kva == NULL) {
102*12918SJan.Friedel@Sun.COM return;
103*12918SJan.Friedel@Sun.COM }
104*12918SJan.Friedel@Sun.COM
105*12918SJan.Friedel@Sun.COM ctr = kva->length;
106*12918SJan.Friedel@Sun.COM data = kva->data;
107*12918SJan.Friedel@Sun.COM
108*12918SJan.Friedel@Sun.COM while (ctr--) {
109*12918SJan.Friedel@Sun.COM if (strcmp(data->key, key) == 0 && data->value != NULL) {
110*12918SJan.Friedel@Sun.COM free(data->value);
111*12918SJan.Friedel@Sun.COM data->value = NULL;
112*12918SJan.Friedel@Sun.COM }
113*12918SJan.Friedel@Sun.COM data++;
114*12918SJan.Friedel@Sun.COM }
115*12918SJan.Friedel@Sun.COM }
116*12918SJan.Friedel@Sun.COM
117*12918SJan.Friedel@Sun.COM /*
1180Sstevel@tonic-gate * new_kva(): Allocate a key-value array.
1190Sstevel@tonic-gate */
1200Sstevel@tonic-gate kva_t *
_new_kva(int size)1210Sstevel@tonic-gate _new_kva(int size)
1220Sstevel@tonic-gate {
1230Sstevel@tonic-gate kva_t *new_kva;
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate if ((new_kva = (kva_t *)calloc(1, sizeof (kva_t))) == NULL) {
126*12918SJan.Friedel@Sun.COM return (NULL);
1270Sstevel@tonic-gate }
1280Sstevel@tonic-gate if ((new_kva->data = (kv_t *)calloc(1, (size*sizeof (kv_t)))) == NULL) {
1290Sstevel@tonic-gate free(new_kva);
130*12918SJan.Friedel@Sun.COM return (NULL);
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate return (new_kva);
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate /*
1370Sstevel@tonic-gate * _str2kva(): Given a string (s) of key-value pairs, separated by delimeter
1380Sstevel@tonic-gate * (del), place the values into the key value array (nkva).
1390Sstevel@tonic-gate */
1400Sstevel@tonic-gate kva_t *
_str2kva(char * s,char * ass,char * del)1410Sstevel@tonic-gate _str2kva(char *s, char *ass, char *del)
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate int n = 0;
1440Sstevel@tonic-gate int m;
1450Sstevel@tonic-gate int size = KV_ADD_KEYS;
1460Sstevel@tonic-gate char *buf;
1470Sstevel@tonic-gate char *p;
1480Sstevel@tonic-gate char *pair;
1490Sstevel@tonic-gate char *key;
1500Sstevel@tonic-gate char *last_pair;
1510Sstevel@tonic-gate char *last_key;
1520Sstevel@tonic-gate kv_t *data;
1530Sstevel@tonic-gate kva_t *nkva;
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate if (s == NULL ||
1560Sstevel@tonic-gate ass == NULL ||
1570Sstevel@tonic-gate del == NULL ||
1580Sstevel@tonic-gate *s == '\0' ||
1590Sstevel@tonic-gate *s == '\n' ||
1600Sstevel@tonic-gate (strlen(s) <= 1)) {
161*12918SJan.Friedel@Sun.COM return (NULL);
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate p = s;
1640Sstevel@tonic-gate while ((p = _strpbrk_escape(p, ass)) != NULL) {
1650Sstevel@tonic-gate n++;
1660Sstevel@tonic-gate p++;
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate if (n > size) {
1690Sstevel@tonic-gate m = n/size;
1700Sstevel@tonic-gate if (n%size) {
1710Sstevel@tonic-gate ++m;
1720Sstevel@tonic-gate }
1730Sstevel@tonic-gate size = m * KV_ADD_KEYS;
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate if ((nkva = _new_kva(size)) == NULL) {
176*12918SJan.Friedel@Sun.COM return (NULL);
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate data = nkva->data;
1790Sstevel@tonic-gate nkva->length = 0;
1800Sstevel@tonic-gate if ((buf = strdup(s)) == NULL) {
181*12918SJan.Friedel@Sun.COM return (NULL);
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate pair = _strtok_escape(buf, del, &last_pair);
1840Sstevel@tonic-gate do {
1850Sstevel@tonic-gate key = _strtok_escape(pair, ass, &last_key);
1860Sstevel@tonic-gate if (key != NULL) {
1870Sstevel@tonic-gate data[nkva->length].key = _do_unescape(key);
1880Sstevel@tonic-gate data[nkva->length].value = _do_unescape(last_key);
1890Sstevel@tonic-gate nkva->length++;
1900Sstevel@tonic-gate }
1910Sstevel@tonic-gate } while ((pair = _strtok_escape(NULL, del, &last_pair)) != NULL);
1920Sstevel@tonic-gate free(buf);
1930Sstevel@tonic-gate return (nkva);
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate
1960Sstevel@tonic-gate /*
1970Sstevel@tonic-gate * _kva2str(): Given an array of key-value pairs, place them into a string
1980Sstevel@tonic-gate * (buf). Use delimeter (del) to separate pairs. Use assignment character
1990Sstevel@tonic-gate * (ass) to separate keys and values.
2000Sstevel@tonic-gate *
201*12918SJan.Friedel@Sun.COM * Return Values: 0 Success 1 Buffer too small
2020Sstevel@tonic-gate */
2030Sstevel@tonic-gate int
_kva2str(kva_t * kva,char * buf,int buflen,char * ass,char * del)2040Sstevel@tonic-gate _kva2str(kva_t *kva, char *buf, int buflen, char *ass, char *del)
2050Sstevel@tonic-gate {
2060Sstevel@tonic-gate int i;
207*12918SJan.Friedel@Sun.COM int len;
208*12918SJan.Friedel@Sun.COM int off = 0;
2090Sstevel@tonic-gate kv_t *data;
2100Sstevel@tonic-gate
2110Sstevel@tonic-gate if (kva == NULL) {
2120Sstevel@tonic-gate return (0);
2130Sstevel@tonic-gate }
214*12918SJan.Friedel@Sun.COM
215*12918SJan.Friedel@Sun.COM buf[0] = '\0';
2160Sstevel@tonic-gate data = kva->data;
217*12918SJan.Friedel@Sun.COM
2180Sstevel@tonic-gate for (i = 0; i < kva->length; i++) {
2190Sstevel@tonic-gate if (data[i].value != NULL) {
220*12918SJan.Friedel@Sun.COM len = snprintf(buf + off, buflen - off, "%s%s%s%s",
221*12918SJan.Friedel@Sun.COM data[i].key, ass, data[i].value, del);
222*12918SJan.Friedel@Sun.COM if (len < 0 || len + off >= buflen) {
223*12918SJan.Friedel@Sun.COM return (1);
224*12918SJan.Friedel@Sun.COM }
225*12918SJan.Friedel@Sun.COM off += len;
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate }
228*12918SJan.Friedel@Sun.COM
2290Sstevel@tonic-gate return (0);
2300Sstevel@tonic-gate }
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate int
_insert2kva(kva_t * kva,char * key,char * value)2330Sstevel@tonic-gate _insert2kva(kva_t *kva, char *key, char *value)
2340Sstevel@tonic-gate {
2350Sstevel@tonic-gate int i;
2360Sstevel@tonic-gate kv_t *data;
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate if (kva == NULL) {
2390Sstevel@tonic-gate return (0);
2400Sstevel@tonic-gate }
2410Sstevel@tonic-gate data = kva->data;
2420Sstevel@tonic-gate for (i = 0; i < kva->length; i++) {
2430Sstevel@tonic-gate if (strcmp(data[i].key, key) == 0) {
2440Sstevel@tonic-gate if (data[i].value != NULL)
2450Sstevel@tonic-gate free(data[i].value);
2460Sstevel@tonic-gate data[i].value = _strdup_null(value);
2470Sstevel@tonic-gate return (0);
2480Sstevel@tonic-gate }
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate return (1);
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate
2530Sstevel@tonic-gate kva_t *
_kva_dup(kva_t * old_kva)2540Sstevel@tonic-gate _kva_dup(kva_t *old_kva)
2550Sstevel@tonic-gate {
2560Sstevel@tonic-gate int i;
2570Sstevel@tonic-gate int size;
2580Sstevel@tonic-gate kv_t *old_data;
2590Sstevel@tonic-gate kv_t *new_data;
260*12918SJan.Friedel@Sun.COM kva_t *nkva = NULL;
2610Sstevel@tonic-gate
2620Sstevel@tonic-gate if (old_kva == NULL) {
263*12918SJan.Friedel@Sun.COM return (NULL);
2640Sstevel@tonic-gate }
2650Sstevel@tonic-gate old_data = old_kva->data;
2660Sstevel@tonic-gate size = old_kva->length;
2670Sstevel@tonic-gate if ((nkva = _new_kva(size)) == NULL) {
268*12918SJan.Friedel@Sun.COM return (NULL);
2690Sstevel@tonic-gate }
2700Sstevel@tonic-gate new_data = nkva->data;
2710Sstevel@tonic-gate nkva->length = old_kva->length;
2728247SJan.Friedel@Sun.COM for (i = 0; i < nkva->length; i++) {
2730Sstevel@tonic-gate new_data[i].key = _strdup_null(old_data[i].key);
2740Sstevel@tonic-gate new_data[i].value = _strdup_null(old_data[i].value);
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate
2770Sstevel@tonic-gate return (nkva);
2780Sstevel@tonic-gate }
2790Sstevel@tonic-gate
2800Sstevel@tonic-gate static void
strip_spaces(char ** valuep)2810Sstevel@tonic-gate strip_spaces(char **valuep)
2820Sstevel@tonic-gate {
2830Sstevel@tonic-gate char *p, *start;
2840Sstevel@tonic-gate
2850Sstevel@tonic-gate /* Find first non-white space character and return pointer to it */
2860Sstevel@tonic-gate for (p = *valuep; *p != '\0' && isspace((unsigned char)*p); p++)
2870Sstevel@tonic-gate ;
2880Sstevel@tonic-gate
2890Sstevel@tonic-gate *valuep = start = p;
2900Sstevel@tonic-gate
2910Sstevel@tonic-gate if (*p == '\0')
2920Sstevel@tonic-gate return;
2930Sstevel@tonic-gate
2940Sstevel@tonic-gate p = p + strlen(p) - 1;
2950Sstevel@tonic-gate
2960Sstevel@tonic-gate /* Remove trailing spaces */
2970Sstevel@tonic-gate while (p > start && isspace((unsigned char)*p))
2980Sstevel@tonic-gate p--;
2990Sstevel@tonic-gate
3000Sstevel@tonic-gate p[1] = '\0';
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate
3030Sstevel@tonic-gate char *
_do_unescape(char * src)3040Sstevel@tonic-gate _do_unescape(char *src)
3050Sstevel@tonic-gate {
3060Sstevel@tonic-gate char *tmp = NULL;
3070Sstevel@tonic-gate char *dst = NULL;
3080Sstevel@tonic-gate
3090Sstevel@tonic-gate if (src == NULL) {
3100Sstevel@tonic-gate dst = _strdup_null(src);
3110Sstevel@tonic-gate } else {
3120Sstevel@tonic-gate strip_spaces(&src);
3130Sstevel@tonic-gate tmp = _unescape(src, "=;:,\\");
3140Sstevel@tonic-gate dst = (tmp == NULL) ? _strdup_null(src) : tmp;
3150Sstevel@tonic-gate }
3160Sstevel@tonic-gate
3170Sstevel@tonic-gate return (dst);
3180Sstevel@tonic-gate }
3190Sstevel@tonic-gate
3200Sstevel@tonic-gate
3210Sstevel@tonic-gate /*
3220Sstevel@tonic-gate * Some utilities for handling comma-separated lists.
3230Sstevel@tonic-gate */
3240Sstevel@tonic-gate char *
_argv_to_csl(char ** strings)3250Sstevel@tonic-gate _argv_to_csl(char **strings)
3260Sstevel@tonic-gate {
3270Sstevel@tonic-gate int len = 0;
3280Sstevel@tonic-gate int i = 0;
329*12918SJan.Friedel@Sun.COM char *newstr = NULL;
3300Sstevel@tonic-gate
3310Sstevel@tonic-gate if (strings == NULL)
332*12918SJan.Friedel@Sun.COM return (NULL);
3330Sstevel@tonic-gate for (i = 0; strings[i] != NULL; i++) {
3340Sstevel@tonic-gate len += strlen(strings[i]) + 1;
3350Sstevel@tonic-gate }
3368061STon.Nguyen@Sun.COM if ((len > 0) && ((newstr = (char *)malloc(len + 1)) != NULL)) {
3378061STon.Nguyen@Sun.COM (void) memset(newstr, 0, len);
3388061STon.Nguyen@Sun.COM for (i = 0; strings[i] != NULL; i++) {
3398061STon.Nguyen@Sun.COM (void) strcat(newstr, strings[i]);
3408061STon.Nguyen@Sun.COM (void) strcat(newstr, ",");
3418061STon.Nguyen@Sun.COM }
3428061STon.Nguyen@Sun.COM newstr[len-1] = NULL;
3438061STon.Nguyen@Sun.COM return (newstr);
3448061STon.Nguyen@Sun.COM } else
345*12918SJan.Friedel@Sun.COM return (NULL);
3460Sstevel@tonic-gate }
3470Sstevel@tonic-gate
3480Sstevel@tonic-gate
3490Sstevel@tonic-gate char **
_csl_to_argv(char * csl)3500Sstevel@tonic-gate _csl_to_argv(char *csl)
3510Sstevel@tonic-gate {
3520Sstevel@tonic-gate int len = 0;
3530Sstevel@tonic-gate int ncommas = 0;
3540Sstevel@tonic-gate int i = 0;
355*12918SJan.Friedel@Sun.COM char **spc = NULL;
356*12918SJan.Friedel@Sun.COM char *copy = NULL;
3570Sstevel@tonic-gate char *pc;
358*12918SJan.Friedel@Sun.COM char *lasts = NULL;
3590Sstevel@tonic-gate
3600Sstevel@tonic-gate len = strlen(csl);
3610Sstevel@tonic-gate for (i = 0; i < len; i++) {
3620Sstevel@tonic-gate if (csl[i] == ',')
3630Sstevel@tonic-gate ncommas++;
3640Sstevel@tonic-gate }
3650Sstevel@tonic-gate if ((spc = (char **)malloc((ncommas + 2) * sizeof (char *))) == NULL) {
366*12918SJan.Friedel@Sun.COM return (NULL);
3670Sstevel@tonic-gate }
3680Sstevel@tonic-gate copy = strdup(csl);
3690Sstevel@tonic-gate for (pc = strtok_r(copy, ",", &lasts), i = 0; pc != NULL;
3700Sstevel@tonic-gate pc = strtok_r(NULL, ",", &lasts), i++) {
3710Sstevel@tonic-gate spc[i] = strdup(pc);
3720Sstevel@tonic-gate }
3730Sstevel@tonic-gate spc[i] = NULL;
3740Sstevel@tonic-gate free(copy);
3750Sstevel@tonic-gate return (spc);
3760Sstevel@tonic-gate }
3770Sstevel@tonic-gate
3780Sstevel@tonic-gate
3790Sstevel@tonic-gate void
_free_argv(char ** p_argv)3800Sstevel@tonic-gate _free_argv(char **p_argv)
3810Sstevel@tonic-gate {
3820Sstevel@tonic-gate char **p_a;
3830Sstevel@tonic-gate
3840Sstevel@tonic-gate for (p_a = p_argv; *p_a != NULL; p_a++)
3850Sstevel@tonic-gate free(*p_a);
3860Sstevel@tonic-gate free(p_argv);
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate
3900Sstevel@tonic-gate #ifdef DEBUG
3910Sstevel@tonic-gate void
print_kva(kva_t * kva)3920Sstevel@tonic-gate print_kva(kva_t *kva)
3930Sstevel@tonic-gate {
3940Sstevel@tonic-gate int i;
3950Sstevel@tonic-gate kv_t *data;
3960Sstevel@tonic-gate
3970Sstevel@tonic-gate if (kva == NULL) {
398*12918SJan.Friedel@Sun.COM (void) printf(" (empty)\n");
3990Sstevel@tonic-gate return;
4000Sstevel@tonic-gate }
4010Sstevel@tonic-gate data = kva->data;
4020Sstevel@tonic-gate for (i = 0; i < kva->length; i++) {
403*12918SJan.Friedel@Sun.COM (void) printf(" %s = %s\n",
404*12918SJan.Friedel@Sun.COM data[i].key != NULL ? data[i].key : "NULL",
405*12918SJan.Friedel@Sun.COM data[i].value != NULL ? data[i].value : "NULL");
4060Sstevel@tonic-gate }
4070Sstevel@tonic-gate }
4080Sstevel@tonic-gate #endif /* DEBUG */
409