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 /* 22*12152SJan.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 * 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) { 520Sstevel@tonic-gate return ((char *)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 610Sstevel@tonic-gate return ((char *)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 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 /* 920Sstevel@tonic-gate * new_kva(): Allocate a key-value array. 930Sstevel@tonic-gate */ 940Sstevel@tonic-gate kva_t * 950Sstevel@tonic-gate _new_kva(int size) 960Sstevel@tonic-gate { 970Sstevel@tonic-gate kva_t *new_kva; 980Sstevel@tonic-gate 990Sstevel@tonic-gate if ((new_kva = (kva_t *)calloc(1, sizeof (kva_t))) == NULL) { 1000Sstevel@tonic-gate return ((kva_t *)NULL); 1010Sstevel@tonic-gate } 1020Sstevel@tonic-gate if ((new_kva->data = (kv_t *)calloc(1, (size*sizeof (kv_t)))) == NULL) { 1030Sstevel@tonic-gate free(new_kva); 1040Sstevel@tonic-gate return ((kva_t *)NULL); 1050Sstevel@tonic-gate } 1060Sstevel@tonic-gate 1070Sstevel@tonic-gate return (new_kva); 1080Sstevel@tonic-gate } 1090Sstevel@tonic-gate 1100Sstevel@tonic-gate /* 1110Sstevel@tonic-gate * _str2kva(): Given a string (s) of key-value pairs, separated by delimeter 1120Sstevel@tonic-gate * (del), place the values into the key value array (nkva). 1130Sstevel@tonic-gate */ 1140Sstevel@tonic-gate kva_t * 1150Sstevel@tonic-gate _str2kva(char *s, char *ass, char *del) 1160Sstevel@tonic-gate { 1170Sstevel@tonic-gate int n = 0; 1180Sstevel@tonic-gate int m; 1190Sstevel@tonic-gate int size = KV_ADD_KEYS; 1200Sstevel@tonic-gate char *buf; 1210Sstevel@tonic-gate char *p; 1220Sstevel@tonic-gate char *pair; 1230Sstevel@tonic-gate char *key; 1240Sstevel@tonic-gate char *last_pair; 1250Sstevel@tonic-gate char *last_key; 1260Sstevel@tonic-gate kv_t *data; 1270Sstevel@tonic-gate kva_t *nkva; 1280Sstevel@tonic-gate 1290Sstevel@tonic-gate if (s == NULL || 1300Sstevel@tonic-gate ass == NULL || 1310Sstevel@tonic-gate del == NULL || 1320Sstevel@tonic-gate *s == '\0' || 1330Sstevel@tonic-gate *s == '\n' || 1340Sstevel@tonic-gate (strlen(s) <= 1)) { 1350Sstevel@tonic-gate return ((kva_t *)NULL); 1360Sstevel@tonic-gate } 1370Sstevel@tonic-gate p = s; 1380Sstevel@tonic-gate while ((p = _strpbrk_escape(p, ass)) != NULL) { 1390Sstevel@tonic-gate n++; 1400Sstevel@tonic-gate p++; 1410Sstevel@tonic-gate } 1420Sstevel@tonic-gate if (n > size) { 1430Sstevel@tonic-gate m = n/size; 1440Sstevel@tonic-gate if (n%size) { 1450Sstevel@tonic-gate ++m; 1460Sstevel@tonic-gate } 1470Sstevel@tonic-gate size = m * KV_ADD_KEYS; 1480Sstevel@tonic-gate } 1490Sstevel@tonic-gate if ((nkva = _new_kva(size)) == NULL) { 1500Sstevel@tonic-gate return ((kva_t *)NULL); 1510Sstevel@tonic-gate } 1520Sstevel@tonic-gate data = nkva->data; 1530Sstevel@tonic-gate nkva->length = 0; 1540Sstevel@tonic-gate if ((buf = strdup(s)) == NULL) { 1550Sstevel@tonic-gate return ((kva_t *)NULL); 1560Sstevel@tonic-gate } 1570Sstevel@tonic-gate pair = _strtok_escape(buf, del, &last_pair); 1580Sstevel@tonic-gate do { 1590Sstevel@tonic-gate key = _strtok_escape(pair, ass, &last_key); 1600Sstevel@tonic-gate if (key != NULL) { 1610Sstevel@tonic-gate data[nkva->length].key = _do_unescape(key); 1620Sstevel@tonic-gate data[nkva->length].value = _do_unescape(last_key); 1630Sstevel@tonic-gate nkva->length++; 1640Sstevel@tonic-gate } 1650Sstevel@tonic-gate } while ((pair = _strtok_escape(NULL, del, &last_pair)) != NULL); 1660Sstevel@tonic-gate free(buf); 1670Sstevel@tonic-gate return (nkva); 1680Sstevel@tonic-gate } 1690Sstevel@tonic-gate 1700Sstevel@tonic-gate /* 1710Sstevel@tonic-gate * _kva2str(): Given an array of key-value pairs, place them into a string 1720Sstevel@tonic-gate * (buf). Use delimeter (del) to separate pairs. Use assignment character 1730Sstevel@tonic-gate * (ass) to separate keys and values. 1740Sstevel@tonic-gate * 1750Sstevel@tonic-gate * Return Values: 0 Success 1 Buffer too small 2 Out of memory 1760Sstevel@tonic-gate */ 1770Sstevel@tonic-gate int 1780Sstevel@tonic-gate _kva2str(kva_t *kva, char *buf, int buflen, char *ass, char *del) 1790Sstevel@tonic-gate { 1800Sstevel@tonic-gate int i; 1810Sstevel@tonic-gate int length = 0; 1820Sstevel@tonic-gate char *tmp; 1830Sstevel@tonic-gate kv_t *data; 1840Sstevel@tonic-gate 1850Sstevel@tonic-gate if (kva == NULL) { 1860Sstevel@tonic-gate return (0); 1870Sstevel@tonic-gate } 1880Sstevel@tonic-gate data = kva->data; 1890Sstevel@tonic-gate for (i = 0; i < kva->length; i++) { 1900Sstevel@tonic-gate if (data[i].value != NULL) { 1910Sstevel@tonic-gate length += 2 + strlen(data[i].value); 1920Sstevel@tonic-gate } 1930Sstevel@tonic-gate } 1940Sstevel@tonic-gate if (length > buflen) { 1950Sstevel@tonic-gate return (1); 1960Sstevel@tonic-gate } 1970Sstevel@tonic-gate (void) memset(buf, 0, buflen); 1980Sstevel@tonic-gate if ((tmp = (char *)malloc(buflen)) == NULL) { 1990Sstevel@tonic-gate return (2); 2000Sstevel@tonic-gate } 2010Sstevel@tonic-gate for (i = 0; i < kva->length; i++) { 2020Sstevel@tonic-gate if (data[i].value != NULL) { 2030Sstevel@tonic-gate if (snprintf(tmp, buflen, "%s%s%s%s", 2040Sstevel@tonic-gate data[i].key, ass, data[i].value, del) >= buflen) { 205*12152SJan.Friedel@Sun.COM free((void *)tmp); 2060Sstevel@tonic-gate return (0); 2070Sstevel@tonic-gate } 2080Sstevel@tonic-gate (void) strcat(buf, tmp); 2090Sstevel@tonic-gate } 2100Sstevel@tonic-gate } 211*12152SJan.Friedel@Sun.COM free((void *)tmp); 2120Sstevel@tonic-gate return (0); 2130Sstevel@tonic-gate } 2140Sstevel@tonic-gate 2150Sstevel@tonic-gate int 2160Sstevel@tonic-gate _insert2kva(kva_t *kva, char *key, char *value) 2170Sstevel@tonic-gate { 2180Sstevel@tonic-gate int i; 2190Sstevel@tonic-gate kv_t *data; 2200Sstevel@tonic-gate 2210Sstevel@tonic-gate if (kva == NULL) { 2220Sstevel@tonic-gate return (0); 2230Sstevel@tonic-gate } 2240Sstevel@tonic-gate data = kva->data; 2250Sstevel@tonic-gate for (i = 0; i < kva->length; i++) { 2260Sstevel@tonic-gate if (strcmp(data[i].key, key) == 0) { 2270Sstevel@tonic-gate if (data[i].value != NULL) 2280Sstevel@tonic-gate free(data[i].value); 2290Sstevel@tonic-gate data[i].value = _strdup_null(value); 2300Sstevel@tonic-gate return (0); 2310Sstevel@tonic-gate } 2320Sstevel@tonic-gate } 2330Sstevel@tonic-gate return (1); 2340Sstevel@tonic-gate } 2350Sstevel@tonic-gate 2360Sstevel@tonic-gate kva_t * 2370Sstevel@tonic-gate _kva_dup(kva_t *old_kva) 2380Sstevel@tonic-gate { 2390Sstevel@tonic-gate int i; 2400Sstevel@tonic-gate int size; 2410Sstevel@tonic-gate kv_t *old_data; 2420Sstevel@tonic-gate kv_t *new_data; 2430Sstevel@tonic-gate kva_t *nkva = (kva_t *)NULL; 2440Sstevel@tonic-gate 2450Sstevel@tonic-gate if (old_kva == NULL) { 2460Sstevel@tonic-gate return ((kva_t *)NULL); 2470Sstevel@tonic-gate } 2480Sstevel@tonic-gate old_data = old_kva->data; 2490Sstevel@tonic-gate size = old_kva->length; 2500Sstevel@tonic-gate if ((nkva = _new_kva(size)) == NULL) { 2510Sstevel@tonic-gate return ((kva_t *)NULL); 2520Sstevel@tonic-gate } 2530Sstevel@tonic-gate new_data = nkva->data; 2540Sstevel@tonic-gate nkva->length = old_kva->length; 2558247SJan.Friedel@Sun.COM for (i = 0; i < nkva->length; i++) { 2560Sstevel@tonic-gate new_data[i].key = _strdup_null(old_data[i].key); 2570Sstevel@tonic-gate new_data[i].value = _strdup_null(old_data[i].value); 2580Sstevel@tonic-gate } 2590Sstevel@tonic-gate 2600Sstevel@tonic-gate return (nkva); 2610Sstevel@tonic-gate } 2620Sstevel@tonic-gate 2630Sstevel@tonic-gate static void 2640Sstevel@tonic-gate strip_spaces(char **valuep) 2650Sstevel@tonic-gate { 2660Sstevel@tonic-gate char *p, *start; 2670Sstevel@tonic-gate 2680Sstevel@tonic-gate /* Find first non-white space character and return pointer to it */ 2690Sstevel@tonic-gate for (p = *valuep; *p != '\0' && isspace((unsigned char)*p); p++) 2700Sstevel@tonic-gate ; 2710Sstevel@tonic-gate 2720Sstevel@tonic-gate *valuep = start = p; 2730Sstevel@tonic-gate 2740Sstevel@tonic-gate if (*p == '\0') 2750Sstevel@tonic-gate return; 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate p = p + strlen(p) - 1; 2780Sstevel@tonic-gate 2790Sstevel@tonic-gate /* Remove trailing spaces */ 2800Sstevel@tonic-gate while (p > start && isspace((unsigned char)*p)) 2810Sstevel@tonic-gate p--; 2820Sstevel@tonic-gate 2830Sstevel@tonic-gate p[1] = '\0'; 2840Sstevel@tonic-gate } 2850Sstevel@tonic-gate 2860Sstevel@tonic-gate char * 2870Sstevel@tonic-gate _do_unescape(char *src) 2880Sstevel@tonic-gate { 2890Sstevel@tonic-gate char *tmp = NULL; 2900Sstevel@tonic-gate char *dst = NULL; 2910Sstevel@tonic-gate 2920Sstevel@tonic-gate if (src == NULL) { 2930Sstevel@tonic-gate dst = _strdup_null(src); 2940Sstevel@tonic-gate } else { 2950Sstevel@tonic-gate strip_spaces(&src); 2960Sstevel@tonic-gate tmp = _unescape(src, "=;:,\\"); 2970Sstevel@tonic-gate dst = (tmp == NULL) ? _strdup_null(src) : tmp; 2980Sstevel@tonic-gate } 2990Sstevel@tonic-gate 3000Sstevel@tonic-gate return (dst); 3010Sstevel@tonic-gate } 3020Sstevel@tonic-gate 3030Sstevel@tonic-gate 3040Sstevel@tonic-gate /* 3050Sstevel@tonic-gate * Some utilities for handling comma-separated lists. 3060Sstevel@tonic-gate */ 3070Sstevel@tonic-gate char * 3080Sstevel@tonic-gate _argv_to_csl(char **strings) 3090Sstevel@tonic-gate { 3100Sstevel@tonic-gate int len = 0; 3110Sstevel@tonic-gate int i = 0; 3120Sstevel@tonic-gate char *newstr = (char *)NULL; 3130Sstevel@tonic-gate 3140Sstevel@tonic-gate if (strings == NULL) 3150Sstevel@tonic-gate return ((char *)NULL); 3160Sstevel@tonic-gate for (i = 0; strings[i] != NULL; i++) { 3170Sstevel@tonic-gate len += strlen(strings[i]) + 1; 3180Sstevel@tonic-gate } 3198061STon.Nguyen@Sun.COM if ((len > 0) && ((newstr = (char *)malloc(len + 1)) != NULL)) { 3208061STon.Nguyen@Sun.COM (void) memset(newstr, 0, len); 3218061STon.Nguyen@Sun.COM for (i = 0; strings[i] != NULL; i++) { 3228061STon.Nguyen@Sun.COM (void) strcat(newstr, strings[i]); 3238061STon.Nguyen@Sun.COM (void) strcat(newstr, ","); 3248061STon.Nguyen@Sun.COM } 3258061STon.Nguyen@Sun.COM newstr[len-1] = NULL; 3268061STon.Nguyen@Sun.COM return (newstr); 3278061STon.Nguyen@Sun.COM } else 3280Sstevel@tonic-gate return ((char *)NULL); 3290Sstevel@tonic-gate } 3300Sstevel@tonic-gate 3310Sstevel@tonic-gate 3320Sstevel@tonic-gate char ** 3330Sstevel@tonic-gate _csl_to_argv(char *csl) 3340Sstevel@tonic-gate { 3350Sstevel@tonic-gate int len = 0; 3360Sstevel@tonic-gate int ncommas = 0; 3370Sstevel@tonic-gate int i = 0; 3380Sstevel@tonic-gate char **spc = (char **)NULL; 3390Sstevel@tonic-gate char *copy = (char *)NULL; 3400Sstevel@tonic-gate char *pc; 3410Sstevel@tonic-gate char *lasts = (char *)NULL; 3420Sstevel@tonic-gate 3430Sstevel@tonic-gate len = strlen(csl); 3440Sstevel@tonic-gate for (i = 0; i < len; i++) { 3450Sstevel@tonic-gate if (csl[i] == ',') 3460Sstevel@tonic-gate ncommas++; 3470Sstevel@tonic-gate } 3480Sstevel@tonic-gate if ((spc = (char **)malloc((ncommas + 2) * sizeof (char *))) == NULL) { 3490Sstevel@tonic-gate return ((char **)NULL); 3500Sstevel@tonic-gate } 3510Sstevel@tonic-gate copy = strdup(csl); 3520Sstevel@tonic-gate for (pc = strtok_r(copy, ",", &lasts), i = 0; pc != NULL; 3530Sstevel@tonic-gate pc = strtok_r(NULL, ",", &lasts), i++) { 3540Sstevel@tonic-gate spc[i] = strdup(pc); 3550Sstevel@tonic-gate } 3560Sstevel@tonic-gate spc[i] = NULL; 3570Sstevel@tonic-gate free(copy); 3580Sstevel@tonic-gate return (spc); 3590Sstevel@tonic-gate } 3600Sstevel@tonic-gate 3610Sstevel@tonic-gate 3620Sstevel@tonic-gate void 3630Sstevel@tonic-gate _free_argv(char **p_argv) 3640Sstevel@tonic-gate { 3650Sstevel@tonic-gate char **p_a; 3660Sstevel@tonic-gate 3670Sstevel@tonic-gate for (p_a = p_argv; *p_a != NULL; p_a++) 3680Sstevel@tonic-gate free(*p_a); 3690Sstevel@tonic-gate free(p_argv); 3700Sstevel@tonic-gate } 3710Sstevel@tonic-gate 3720Sstevel@tonic-gate 3730Sstevel@tonic-gate #ifdef DEBUG 3740Sstevel@tonic-gate void 3750Sstevel@tonic-gate print_kva(kva_t *kva) 3760Sstevel@tonic-gate { 3770Sstevel@tonic-gate int i; 3780Sstevel@tonic-gate kv_t *data; 3790Sstevel@tonic-gate 3800Sstevel@tonic-gate if (kva == NULL) { 3810Sstevel@tonic-gate printf(" (empty)\n"); 3820Sstevel@tonic-gate return; 3830Sstevel@tonic-gate } 3840Sstevel@tonic-gate data = kva->data; 3850Sstevel@tonic-gate for (i = 0; i < kva->length; i++) { 3860Sstevel@tonic-gate printf(" %s = %s\n", data[i].key, data[i].value); 3870Sstevel@tonic-gate } 3880Sstevel@tonic-gate } 3890Sstevel@tonic-gate #endif /* DEBUG */ 390