xref: /titanic_52/usr/src/lib/libnisdb/ldap_val.c (revision a87701e9837f8a9ee9e4c4d3186295c0e29f743f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
536e852a1SRaja Andra  * Common Development and Distribution License (the "License").
636e852a1SRaja Andra  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*a87701e9SGary Mills  * Copyright 2015 Gary Mills
2336e852a1SRaja Andra  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <lber.h>
297c478bd9Sstevel@tonic-gate #include <ldap.h>
307c478bd9Sstevel@tonic-gate #include <strings.h>
317c478bd9Sstevel@tonic-gate #include <errno.h>
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include "nisdb_mt.h"
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include "ldap_util.h"
367c478bd9Sstevel@tonic-gate #include "ldap_op.h"
377c478bd9Sstevel@tonic-gate #include "ldap_ruleval.h"
387c478bd9Sstevel@tonic-gate #include "ldap_attr.h"
397c478bd9Sstevel@tonic-gate #include "ldap_val.h"
407c478bd9Sstevel@tonic-gate #include "ldap_ldap.h"
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate extern int yp2ldap;
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate __nis_mapping_format_t *
467c478bd9Sstevel@tonic-gate cloneMappingFormat(__nis_mapping_format_t *m) {
477c478bd9Sstevel@tonic-gate 	__nis_mapping_format_t	*new;
487c478bd9Sstevel@tonic-gate 	int			i, nf, err;
497c478bd9Sstevel@tonic-gate 	char			*myself = "cloneMappingFormat";
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate 	if (m == 0)
527c478bd9Sstevel@tonic-gate 		return (0);
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 	for (nf = 0; m[nf].type != mmt_end; nf++);
557c478bd9Sstevel@tonic-gate 	nf++;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	new = am(myself, nf * sizeof (new[0]));
587c478bd9Sstevel@tonic-gate 	if (new == 0)
597c478bd9Sstevel@tonic-gate 		return (0);
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	/* Copy the whole array */
627c478bd9Sstevel@tonic-gate 	memcpy(new, m, nf * sizeof (new[0]));
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	/* Make copies of allocated stuff */
657c478bd9Sstevel@tonic-gate 	for (i = 0, err = 0; i < nf; i++) {
667c478bd9Sstevel@tonic-gate 		switch (m[i].type) {
677c478bd9Sstevel@tonic-gate 		case mmt_string:
687c478bd9Sstevel@tonic-gate 			new[i].match.string = sdup(myself, T,
697c478bd9Sstevel@tonic-gate 							m[i].match.string);
707c478bd9Sstevel@tonic-gate 			if (new[i].match.string == 0 && m[i].match.string != 0)
717c478bd9Sstevel@tonic-gate 				err++;
727c478bd9Sstevel@tonic-gate 			break;
737c478bd9Sstevel@tonic-gate 		case mmt_single:
747c478bd9Sstevel@tonic-gate 			new[i].match.single.lo =
757c478bd9Sstevel@tonic-gate 				am(myself, m[i].match.single.numRange *
767c478bd9Sstevel@tonic-gate 					sizeof (new[i].match.single.lo[0]));
777c478bd9Sstevel@tonic-gate 			new[i].match.single.hi =
787c478bd9Sstevel@tonic-gate 				am(myself, m[i].match.single.numRange *
797c478bd9Sstevel@tonic-gate 					sizeof (new[i].match.single.hi[0]));
807c478bd9Sstevel@tonic-gate 			if (new[i].match.single.lo != 0)
817c478bd9Sstevel@tonic-gate 				memcpy(new[i].match.single.lo,
827c478bd9Sstevel@tonic-gate 					m[i].match.single.lo,
837c478bd9Sstevel@tonic-gate 					m[i].match.single.numRange);
847c478bd9Sstevel@tonic-gate 			else if (m[i].match.single.lo != 0)
857c478bd9Sstevel@tonic-gate 				err++;
867c478bd9Sstevel@tonic-gate 			if (new[i].match.single.hi != 0)
877c478bd9Sstevel@tonic-gate 				memcpy(new[i].match.single.hi,
887c478bd9Sstevel@tonic-gate 					m[i].match.single.hi,
897c478bd9Sstevel@tonic-gate 					m[i].match.single.numRange);
907c478bd9Sstevel@tonic-gate 			else if (m[i].match.single.hi != 0)
917c478bd9Sstevel@tonic-gate 				err++;
927c478bd9Sstevel@tonic-gate 			break;
937c478bd9Sstevel@tonic-gate 		case mmt_berstring:
947c478bd9Sstevel@tonic-gate 			new[i].match.berString = sdup(myself, T,
957c478bd9Sstevel@tonic-gate 							m[i].match.berString);
967c478bd9Sstevel@tonic-gate 			if (new[i].match.berString == 0 &&
977c478bd9Sstevel@tonic-gate 					m[i].match.berString != 0)
987c478bd9Sstevel@tonic-gate 				err++;
997c478bd9Sstevel@tonic-gate 			break;
1007c478bd9Sstevel@tonic-gate 		case mmt_item:
1017c478bd9Sstevel@tonic-gate 		case mmt_limit:
1027c478bd9Sstevel@tonic-gate 		case mmt_any:
1037c478bd9Sstevel@tonic-gate 		case mmt_begin:
1047c478bd9Sstevel@tonic-gate 		case mmt_end:
1057c478bd9Sstevel@tonic-gate 		default:
1067c478bd9Sstevel@tonic-gate 			break;
1077c478bd9Sstevel@tonic-gate 		}
1087c478bd9Sstevel@tonic-gate 	}
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	/* If there were memory allocation errors, free the copy */
1117c478bd9Sstevel@tonic-gate 	if (err > 0) {
1127c478bd9Sstevel@tonic-gate 		freeMappingFormat(new);
1137c478bd9Sstevel@tonic-gate 		new = 0;
1147c478bd9Sstevel@tonic-gate 	}
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	return (new);
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate void
1207c478bd9Sstevel@tonic-gate freeMappingFormat(__nis_mapping_format_t *m) {
1217c478bd9Sstevel@tonic-gate 	int	i;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	if (m == 0)
1247c478bd9Sstevel@tonic-gate 		return;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	for (i = 0; m[i].type != mmt_end; i++) {
1277c478bd9Sstevel@tonic-gate 		switch (m[i].type) {
1287c478bd9Sstevel@tonic-gate 		case mmt_string:
1297c478bd9Sstevel@tonic-gate 			sfree(m[i].match.string);
1307c478bd9Sstevel@tonic-gate 			break;
1317c478bd9Sstevel@tonic-gate 		case mmt_single:
1327c478bd9Sstevel@tonic-gate 			sfree(m[i].match.single.lo);
1337c478bd9Sstevel@tonic-gate 			sfree(m[i].match.single.hi);
1347c478bd9Sstevel@tonic-gate 			break;
1357c478bd9Sstevel@tonic-gate 		case mmt_berstring:
1367c478bd9Sstevel@tonic-gate 			sfree(m[i].match.berString);
1377c478bd9Sstevel@tonic-gate 			break;
1387c478bd9Sstevel@tonic-gate 		case mmt_item:
1397c478bd9Sstevel@tonic-gate 		case mmt_limit:
1407c478bd9Sstevel@tonic-gate 		case mmt_any:
1417c478bd9Sstevel@tonic-gate 		case mmt_begin:
1427c478bd9Sstevel@tonic-gate 		case mmt_end:
1437c478bd9Sstevel@tonic-gate 		default:
1447c478bd9Sstevel@tonic-gate 			break;
1457c478bd9Sstevel@tonic-gate 		}
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	free(m);
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate void
1537c478bd9Sstevel@tonic-gate copyIndex(__nis_index_t *old, __nis_index_t *new, int *err) {
1547c478bd9Sstevel@tonic-gate 	int	i;
1557c478bd9Sstevel@tonic-gate 	char	*myself = "copyIndex";
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	if (old == 0 || new == 0) {
1587c478bd9Sstevel@tonic-gate 		*err = EINVAL;
1597c478bd9Sstevel@tonic-gate 		return;
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	for (i = 0; i < old->numIndexes; i++) {
1637c478bd9Sstevel@tonic-gate 		new->name[i] = sdup(myself, T, old->name[i]);
1647c478bd9Sstevel@tonic-gate 		if (new->name[i] == 0 && old->name[i] != 0) {
1657c478bd9Sstevel@tonic-gate 			*err = ENOMEM;
1667c478bd9Sstevel@tonic-gate 			return;
1677c478bd9Sstevel@tonic-gate 		}
1687c478bd9Sstevel@tonic-gate 		new->value[i] = cloneMappingFormat(old->value[i]);
1697c478bd9Sstevel@tonic-gate 		if (new->value[i] == 0 && old->value[i] != 0) {
1707c478bd9Sstevel@tonic-gate 			*err = ENOMEM;
1717c478bd9Sstevel@tonic-gate 			return;
1727c478bd9Sstevel@tonic-gate 		}
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	new->numIndexes = old->numIndexes;
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate __nis_index_t *
1797c478bd9Sstevel@tonic-gate cloneIndex(__nis_index_t *old) {
1807c478bd9Sstevel@tonic-gate 	char		*myself = "cloneIndex";
1817c478bd9Sstevel@tonic-gate 	int		err = 0;
1827c478bd9Sstevel@tonic-gate 	__nis_index_t	*new = am(myself, sizeof (*new));
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	if (old == 0)
1857c478bd9Sstevel@tonic-gate 		return (0);
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	if (new != 0) {
1887c478bd9Sstevel@tonic-gate 		copyIndex(old, new, &err);
1897c478bd9Sstevel@tonic-gate 		if (err != 0) {
1907c478bd9Sstevel@tonic-gate 			freeIndex(new, 1);
1917c478bd9Sstevel@tonic-gate 			new = 0;
1927c478bd9Sstevel@tonic-gate 		}
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	return (new);
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate void
1997c478bd9Sstevel@tonic-gate freeIndex(__nis_index_t *old, bool_t doFree) {
2007c478bd9Sstevel@tonic-gate 	int	i;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	if (old == 0)
2037c478bd9Sstevel@tonic-gate 		return;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	for (i = 0; i < old->numIndexes; i++) {
2067c478bd9Sstevel@tonic-gate 		sfree(old->name[i]);
2077c478bd9Sstevel@tonic-gate 		freeMappingFormat(old->value[i]);
2087c478bd9Sstevel@tonic-gate 	}
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	if (doFree)
2117c478bd9Sstevel@tonic-gate 		free(old);
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate char **
2157c478bd9Sstevel@tonic-gate cloneName(char **name, int numNames) {
2167c478bd9Sstevel@tonic-gate 	char	**new;
2177c478bd9Sstevel@tonic-gate 	int	i;
2187c478bd9Sstevel@tonic-gate 	char	*myself = "cloneName";
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	if (name == 0 || numNames <= 0)
2217c478bd9Sstevel@tonic-gate 		return (0);
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	new = am(myself, numNames * sizeof (new[0]));
2247c478bd9Sstevel@tonic-gate 	if (new == 0)
2257c478bd9Sstevel@tonic-gate 		return (0);
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	for (i = 0; i < numNames; i++) {
2287c478bd9Sstevel@tonic-gate 		if (name[i] != 0) {
2297c478bd9Sstevel@tonic-gate 			new[i] = sdup(myself, T, name[i]);
2307c478bd9Sstevel@tonic-gate 			if (new[i] == 0) {
2317c478bd9Sstevel@tonic-gate 				for (i--; i >= 0; i--) {
2327c478bd9Sstevel@tonic-gate 					sfree(new[i]);
2337c478bd9Sstevel@tonic-gate 				}
2347c478bd9Sstevel@tonic-gate 				sfree(new);
2357c478bd9Sstevel@tonic-gate 				return (0);
2367c478bd9Sstevel@tonic-gate 			}
2377c478bd9Sstevel@tonic-gate 		} else {
2387c478bd9Sstevel@tonic-gate 			new[i] = 0;
2397c478bd9Sstevel@tonic-gate 		}
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	return (new);
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate void
2467c478bd9Sstevel@tonic-gate freeValue(__nis_value_t *val, int count) {
2477c478bd9Sstevel@tonic-gate 	int	c, i;
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	if (val == 0)
2507c478bd9Sstevel@tonic-gate 		return;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	for (c = 0; c < count; c++) {
2537c478bd9Sstevel@tonic-gate 		if (val[c].val != 0) {
2547c478bd9Sstevel@tonic-gate 			for (i = 0; i < val[c].numVals; i++) {
2557c478bd9Sstevel@tonic-gate 				sfree(val[c].val[i].value);
2567c478bd9Sstevel@tonic-gate 			}
2577c478bd9Sstevel@tonic-gate 			free(val[c].val);
2587c478bd9Sstevel@tonic-gate 		}
2597c478bd9Sstevel@tonic-gate 	}
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	free(val);
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate __nis_value_t *
2657c478bd9Sstevel@tonic-gate cloneValue(__nis_value_t *val, int count) {
2667c478bd9Sstevel@tonic-gate 	__nis_value_t	*n;
2677c478bd9Sstevel@tonic-gate 	int		c, i;
2687c478bd9Sstevel@tonic-gate 	char		*myself = "cloneValue";
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	if (count <= 0 || val == 0)
2717c478bd9Sstevel@tonic-gate 		return (0);
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	n = am(myself, count * sizeof (*n));
2747c478bd9Sstevel@tonic-gate 	if (n == 0)
2757c478bd9Sstevel@tonic-gate 		return (0);
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	for (c = 0; c < count; c++) {
2787c478bd9Sstevel@tonic-gate 		n[c].type = val[c].type;
2797c478bd9Sstevel@tonic-gate 		n[c].repeat = val[c].repeat;
2807c478bd9Sstevel@tonic-gate 		n[c].numVals = val[c].numVals;
2817c478bd9Sstevel@tonic-gate 		if (n[c].numVals > 0) {
2827c478bd9Sstevel@tonic-gate 			n[c].val = am(myself, n[c].numVals *
2837c478bd9Sstevel@tonic-gate 						sizeof (n[c].val[0]));
2847c478bd9Sstevel@tonic-gate 			if (n[c].val == 0) {
2857c478bd9Sstevel@tonic-gate 				freeValue(n, c);
2867c478bd9Sstevel@tonic-gate 				return (0);
2877c478bd9Sstevel@tonic-gate 			}
2887c478bd9Sstevel@tonic-gate 		} else {
2897c478bd9Sstevel@tonic-gate 			n[c].val = 0;
2907c478bd9Sstevel@tonic-gate 		}
2917c478bd9Sstevel@tonic-gate 		for (i = 0; i < n[c].numVals; i++) {
2927c478bd9Sstevel@tonic-gate 			int	amlen = val[c].val[i].length;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 			/*
2957c478bd9Sstevel@tonic-gate 			 * The functions that create string values try to
2967c478bd9Sstevel@tonic-gate 			 * make sure that there's a NUL at the end. However,
2977c478bd9Sstevel@tonic-gate 			 * both NIS+ and LDAP have a tendency to store strings
2987c478bd9Sstevel@tonic-gate 			 * without a NUL, so the value length may not include
2997c478bd9Sstevel@tonic-gate 			 * the NUL (even though it's there). In order to
3007c478bd9Sstevel@tonic-gate 			 * preserve that NUL, we add a byte to the length if
3017c478bd9Sstevel@tonic-gate 			 * the type is vt_string, and there isn't already a
3027c478bd9Sstevel@tonic-gate 			 * NUL at the end. The memory allocation function
3037c478bd9Sstevel@tonic-gate 			 * (am()) will take care of actually putting the NUL
3047c478bd9Sstevel@tonic-gate 			 * in place, since it allocates zero-initialized
3057c478bd9Sstevel@tonic-gate 			 * memory.
3067c478bd9Sstevel@tonic-gate 			 */
3077c478bd9Sstevel@tonic-gate 			n[c].val[i].length = val[c].val[i].length;
3087c478bd9Sstevel@tonic-gate 			if (n[c].type == vt_string && amlen > 0 &&
3097c478bd9Sstevel@tonic-gate 				((char *)val[c].val[i].value)[amlen-1] !=
3107c478bd9Sstevel@tonic-gate 					'\0') {
3117c478bd9Sstevel@tonic-gate 				amlen++;
3127c478bd9Sstevel@tonic-gate 			}
3137c478bd9Sstevel@tonic-gate 			n[c].val[i].value = am(myself, amlen);
3147c478bd9Sstevel@tonic-gate 			if (amlen > 0 && n[c].val[i].value == 0) {
3157c478bd9Sstevel@tonic-gate 				freeValue(n, c);
3167c478bd9Sstevel@tonic-gate 				return (0);
3177c478bd9Sstevel@tonic-gate 			}
3187c478bd9Sstevel@tonic-gate 			memcpy(n[c].val[i].value, val[c].val[i].value,
3197c478bd9Sstevel@tonic-gate 				n[c].val[i].length);
3207c478bd9Sstevel@tonic-gate 		}
3217c478bd9Sstevel@tonic-gate 	}
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	return (n);
3247c478bd9Sstevel@tonic-gate }
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate /* Define LBER_USE_DER per ber_decode(3LDAP) */
3277c478bd9Sstevel@tonic-gate #ifndef	LBER_USE_DER
3287c478bd9Sstevel@tonic-gate #define	LBER_USE_DER	0x01
3297c478bd9Sstevel@tonic-gate #endif	/* LBER_USE_DER */
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate /*
3327c478bd9Sstevel@tonic-gate  * Return a copy of 'valIn' where each value has been replaced by the
3337c478bd9Sstevel@tonic-gate  * BER encoded equivalent specified by 'berstring'. 'valIn' is unchanged.
3347c478bd9Sstevel@tonic-gate  */
3357c478bd9Sstevel@tonic-gate __nis_value_t *
3367c478bd9Sstevel@tonic-gate berEncode(__nis_value_t *valIn, char *berstring) {
3377c478bd9Sstevel@tonic-gate 	char		*myself = "berEncode";
3387c478bd9Sstevel@tonic-gate 	__nis_value_t	*val;
3397c478bd9Sstevel@tonic-gate 	int		i;
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	if (valIn == 0 || berstring == 0)
3427c478bd9Sstevel@tonic-gate 		return (0);
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	val = cloneValue(valIn, 1);
3457c478bd9Sstevel@tonic-gate 	if (val == 0)
3467c478bd9Sstevel@tonic-gate 		return (0);
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	for (i = 0; i < val->numVals; i++) {
3497c478bd9Sstevel@tonic-gate 		BerElement	*ber = ber_alloc();
3507c478bd9Sstevel@tonic-gate 		struct berval	*bv = 0;
3517c478bd9Sstevel@tonic-gate 		int		ret;
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 		if (ber == 0) {
3547c478bd9Sstevel@tonic-gate 			logmsg(MSG_NOMEM, LOG_ERR, "%s: ber_alloc() => NULL",
3557c478bd9Sstevel@tonic-gate 				myself);
3567c478bd9Sstevel@tonic-gate 			freeValue(val, 1);
3577c478bd9Sstevel@tonic-gate 			return (0);
3587c478bd9Sstevel@tonic-gate 		}
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 		if ((strcmp("b", berstring) == 0 ||
3617c478bd9Sstevel@tonic-gate 				strcmp("i", berstring) == 0)) {
3627c478bd9Sstevel@tonic-gate 			if (val->val[i].length >= sizeof (int)) {
3637c478bd9Sstevel@tonic-gate 				ret = ber_printf(ber, berstring,
3647c478bd9Sstevel@tonic-gate 					*((int *)(val->val[i].value)));
3657c478bd9Sstevel@tonic-gate 			} else {
3667c478bd9Sstevel@tonic-gate 				ret = -1;
3677c478bd9Sstevel@tonic-gate 			}
3687c478bd9Sstevel@tonic-gate 		} else if (strcmp("B", berstring) == 0) {
3697c478bd9Sstevel@tonic-gate 			ret = ber_printf(ber, berstring,
3707c478bd9Sstevel@tonic-gate 				val->val[i].value,
3717c478bd9Sstevel@tonic-gate 				val->val[i].length * 8);
3727c478bd9Sstevel@tonic-gate 		} else if (strcmp("n", berstring) == 0) {
3737c478bd9Sstevel@tonic-gate 			ret = ber_printf(ber, berstring);
3747c478bd9Sstevel@tonic-gate 		} else if (strcmp("o", berstring) == 0) {
3757c478bd9Sstevel@tonic-gate 			ret = ber_printf(ber, berstring,
3767c478bd9Sstevel@tonic-gate 				val->val[i].value, val->val[i].length);
3777c478bd9Sstevel@tonic-gate 		} else if (strcmp("s", berstring) == 0) {
3787c478bd9Sstevel@tonic-gate 			char	*str = am(myself, val->val[i].length + 1);
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 			if (str != 0) {
3817c478bd9Sstevel@tonic-gate 				ret = ber_printf(ber, berstring, str);
3827c478bd9Sstevel@tonic-gate 				free(str);
3837c478bd9Sstevel@tonic-gate 			} else {
3847c478bd9Sstevel@tonic-gate 				ret = -1;
3857c478bd9Sstevel@tonic-gate 			}
3867c478bd9Sstevel@tonic-gate 		} else {
3877c478bd9Sstevel@tonic-gate 			ret = -1;
3887c478bd9Sstevel@tonic-gate 		}
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 		if (ret == -1) {
3917c478bd9Sstevel@tonic-gate 			reportError(NPL_BERENCODE, "%s: BER encoding error",
3927c478bd9Sstevel@tonic-gate 					myself);
3937c478bd9Sstevel@tonic-gate 			ber_free(ber, 1);
3947c478bd9Sstevel@tonic-gate 			freeValue(val, 1);
3957c478bd9Sstevel@tonic-gate 			return (0);
3967c478bd9Sstevel@tonic-gate 		}
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 		if (ber_flatten(ber, &bv) != 0 || bv == 0) {
3997c478bd9Sstevel@tonic-gate 			reportError(NPL_BERENCODE, "%s: ber_flatten() error",
4007c478bd9Sstevel@tonic-gate 					myself);
4017c478bd9Sstevel@tonic-gate 			ber_free(ber, 1);
4027c478bd9Sstevel@tonic-gate 			freeValue(val, 1);
4037c478bd9Sstevel@tonic-gate 			return (0);
4047c478bd9Sstevel@tonic-gate 		}
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 		sfree(val->val[i].value);
4077c478bd9Sstevel@tonic-gate 		val->val[i].length = bv->bv_len;
4087c478bd9Sstevel@tonic-gate 		val->val[i].value = bv->bv_val;
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 		ber_free(ber, 1);
4117c478bd9Sstevel@tonic-gate 	}
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	val->type = vt_ber;
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	return (val);
4167c478bd9Sstevel@tonic-gate }
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate __nis_value_t *
4197c478bd9Sstevel@tonic-gate berDecode(__nis_value_t *valIn, char *berstring) {
4207c478bd9Sstevel@tonic-gate 	__nis_value_t	*val;
4217c478bd9Sstevel@tonic-gate 	int		i;
4227c478bd9Sstevel@tonic-gate 	char		*myself = "berDecode";
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	if (valIn == 0 || berstring == 0)
4257c478bd9Sstevel@tonic-gate 		return (0);
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	val = cloneValue(valIn, 1);
4287c478bd9Sstevel@tonic-gate 	if (val == 0)
4297c478bd9Sstevel@tonic-gate 		return (0);
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	for (i = 0; i < val->numVals; i++) {
4327c478bd9Sstevel@tonic-gate 		void		*v = 0;
4337c478bd9Sstevel@tonic-gate 		int		ret, len = 0;
4347c478bd9Sstevel@tonic-gate 		struct berval	bv;
4357c478bd9Sstevel@tonic-gate 		BerElement	*ber;
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 		if (val->val[i].value == 0 || val->val[i].length <= 0)
4387c478bd9Sstevel@tonic-gate 			continue;
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 		bv.bv_val = val->val[i].value;
4417c478bd9Sstevel@tonic-gate 		bv.bv_len = val->val[i].length;
4427c478bd9Sstevel@tonic-gate 		ber = ber_init(&bv);
4437c478bd9Sstevel@tonic-gate 		if (ber == 0) {
4447c478bd9Sstevel@tonic-gate 			reportError(NPL_BERDECODE, "%s: ber_init() error",
4457c478bd9Sstevel@tonic-gate 				myself);
4467c478bd9Sstevel@tonic-gate 			freeValue(val, 1);
4477c478bd9Sstevel@tonic-gate 			return (0);
4487c478bd9Sstevel@tonic-gate 		}
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 		if ((strcmp("b", berstring) == 0 ||
4517c478bd9Sstevel@tonic-gate 				strcmp("i", berstring) == 0)) {
4527c478bd9Sstevel@tonic-gate 			len = sizeof (int);
4537c478bd9Sstevel@tonic-gate 			v = am(myself, len);
4547c478bd9Sstevel@tonic-gate 			if (v != 0) {
4557c478bd9Sstevel@tonic-gate 				ret = ber_scanf(ber, berstring, v);
4567c478bd9Sstevel@tonic-gate 			} else {
4577c478bd9Sstevel@tonic-gate 				ret = -1;
4587c478bd9Sstevel@tonic-gate 			}
4597c478bd9Sstevel@tonic-gate 		} else if (strcmp("B", berstring) == 0) {
4607c478bd9Sstevel@tonic-gate 			long	llen;
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 			ret = ber_scanf(ber, berstring, &v, &llen);
4637c478bd9Sstevel@tonic-gate 			if (ret != -1) {
4647c478bd9Sstevel@tonic-gate 				len = llen/8;
4657c478bd9Sstevel@tonic-gate 			}
4667c478bd9Sstevel@tonic-gate 		} else if (strcmp("n", berstring) == 0) {
4677c478bd9Sstevel@tonic-gate 			ret = 0;
4687c478bd9Sstevel@tonic-gate 		} else if (strcmp("o", berstring) == 0) {
4697c478bd9Sstevel@tonic-gate 			struct berval	*bv = am(myself, sizeof (*bv));
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 			if (bv != 0) {
4727c478bd9Sstevel@tonic-gate 				ret = ber_scanf(ber, "O", &bv);
4737c478bd9Sstevel@tonic-gate 				if (ret != -1 && bv != 0) {
4747c478bd9Sstevel@tonic-gate 					v = bv->bv_val;
4757c478bd9Sstevel@tonic-gate 					len = bv->bv_len;
4767c478bd9Sstevel@tonic-gate 				} else {
4777c478bd9Sstevel@tonic-gate 					ret = -1;
4787c478bd9Sstevel@tonic-gate 				}
4797c478bd9Sstevel@tonic-gate 				/* Only free 'bv' itself */
4807c478bd9Sstevel@tonic-gate 				free(bv);
4817c478bd9Sstevel@tonic-gate 			} else {
4827c478bd9Sstevel@tonic-gate 				ret = -1;
4837c478bd9Sstevel@tonic-gate 			}
4847c478bd9Sstevel@tonic-gate 		} else if (strcmp("s", berstring) == 0) {
4857c478bd9Sstevel@tonic-gate 			ret = ber_scanf(ber, "a", &v);
4867c478bd9Sstevel@tonic-gate 			if (ret != -1) {
4877c478bd9Sstevel@tonic-gate 				len = slen(v);
4887c478bd9Sstevel@tonic-gate 			}
4897c478bd9Sstevel@tonic-gate 		} else {
4907c478bd9Sstevel@tonic-gate 			ret = -1;
4917c478bd9Sstevel@tonic-gate 		}
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 		if (ret == -1) {
4947c478bd9Sstevel@tonic-gate 			reportError(NPL_BERDECODE, "%s: BER decoding error",
4957c478bd9Sstevel@tonic-gate 					myself);
4967c478bd9Sstevel@tonic-gate 			freeValue(val, 1);
4977c478bd9Sstevel@tonic-gate 			return (0);
4987c478bd9Sstevel@tonic-gate 		}
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 		/* Free the old value, and replace it with the decoded one */
5017c478bd9Sstevel@tonic-gate 		sfree(val->val[i].value);
5027c478bd9Sstevel@tonic-gate 		val->val[i].value = v;
5037c478bd9Sstevel@tonic-gate 		val->val[i].length = len;
5047c478bd9Sstevel@tonic-gate 	}
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	return (val);
5077c478bd9Sstevel@tonic-gate }
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate /*
5107c478bd9Sstevel@tonic-gate  * Return the value of the specified item.
5117c478bd9Sstevel@tonic-gate  */
5127c478bd9Sstevel@tonic-gate __nis_value_t *
5137c478bd9Sstevel@tonic-gate getMappingItemVal(__nis_mapping_item_t *item, __nis_mapping_item_type_t native,
5147c478bd9Sstevel@tonic-gate 		__nis_rule_value_t *rv, char *berstring, int *np_ldap_stat) {
5157c478bd9Sstevel@tonic-gate 	__nis_value_t				*val = 0, *nameVal, *exVal = 0;
5167c478bd9Sstevel@tonic-gate 	int					numName, caseInsens, cmp;
5177c478bd9Sstevel@tonic-gate 	int					i, j, k;
5187c478bd9Sstevel@tonic-gate 	char					**name;
5197c478bd9Sstevel@tonic-gate 	enum {rvOnly, rvThenLookup, lookupOnly}	check;
5207c478bd9Sstevel@tonic-gate 	unsigned char				fromldap = '\0';
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	if (item == 0)
5237c478bd9Sstevel@tonic-gate 		return (0);
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	/*
5267c478bd9Sstevel@tonic-gate 	 * First, we decide if we should look for the value in 'rv',
5277c478bd9Sstevel@tonic-gate 	 * directly from NIS+/LDAP, or both.
5287c478bd9Sstevel@tonic-gate 	 */
5297c478bd9Sstevel@tonic-gate 	switch (item->type) {
5307c478bd9Sstevel@tonic-gate 	case mit_nisplus:
5317c478bd9Sstevel@tonic-gate 		/* Do we have a valid index/object spec ? */
5327c478bd9Sstevel@tonic-gate 		if (item->searchSpec.obj.index.numIndexes <= 0 &&
5337c478bd9Sstevel@tonic-gate 				item->searchSpec.obj.name == 0) {
5347c478bd9Sstevel@tonic-gate 			/*
5357c478bd9Sstevel@tonic-gate 			 * No valid index/object. If we have a rule-value,
5367c478bd9Sstevel@tonic-gate 			 * use it. Otherwise, return error.
5377c478bd9Sstevel@tonic-gate 			 */
5387c478bd9Sstevel@tonic-gate 			if (rv != 0) {
5397c478bd9Sstevel@tonic-gate 				name = rv->colName;
5407c478bd9Sstevel@tonic-gate 				nameVal = rv->colVal;
5417c478bd9Sstevel@tonic-gate 				numName = rv->numColumns;
5427c478bd9Sstevel@tonic-gate 				caseInsens = 0;
5437c478bd9Sstevel@tonic-gate 				check = rvOnly;
5447c478bd9Sstevel@tonic-gate 			} else {
5457c478bd9Sstevel@tonic-gate 				return (0);
5467c478bd9Sstevel@tonic-gate 			}
5477c478bd9Sstevel@tonic-gate 		} else {
5487c478bd9Sstevel@tonic-gate 			/*
5497c478bd9Sstevel@tonic-gate 			 * Valid index, so skip the rule-value and do
5507c478bd9Sstevel@tonic-gate 			 * a direct NIS+ lookup.
5517c478bd9Sstevel@tonic-gate 			 */
5527c478bd9Sstevel@tonic-gate 			check = lookupOnly;
5537c478bd9Sstevel@tonic-gate 		}
5547c478bd9Sstevel@tonic-gate 		break;
5557c478bd9Sstevel@tonic-gate 	case mit_ldap:
5567c478bd9Sstevel@tonic-gate 		if (rv != 0) {
5577c478bd9Sstevel@tonic-gate 			name = rv->attrName;
5587c478bd9Sstevel@tonic-gate 			nameVal = rv->attrVal;
5597c478bd9Sstevel@tonic-gate 			numName = rv->numAttrs;
5607c478bd9Sstevel@tonic-gate 			caseInsens = 1;
5617c478bd9Sstevel@tonic-gate 			fromldap = '1';
5627c478bd9Sstevel@tonic-gate 		}
5637c478bd9Sstevel@tonic-gate 		/* Do we have a valid triple ? */
5647c478bd9Sstevel@tonic-gate 		if (item->searchSpec.triple.scope == LDAP_SCOPE_UNKNOWN) {
5657c478bd9Sstevel@tonic-gate 			/*
5667c478bd9Sstevel@tonic-gate 			 * No valid triple. If we have a rule-value, use it.
5677c478bd9Sstevel@tonic-gate 			 * Otherwise, return error.
5687c478bd9Sstevel@tonic-gate 			 */
5697c478bd9Sstevel@tonic-gate 			if (rv != 0) {
5707c478bd9Sstevel@tonic-gate 				check = rvOnly;
5717c478bd9Sstevel@tonic-gate 			} else {
5727c478bd9Sstevel@tonic-gate 				return (0);
5737c478bd9Sstevel@tonic-gate 			}
5747c478bd9Sstevel@tonic-gate 		} else if (item->searchSpec.triple.base == 0 &&
5757c478bd9Sstevel@tonic-gate 				item->searchSpec.triple.scope ==
5767c478bd9Sstevel@tonic-gate 					LDAP_SCOPE_ONELEVEL &&
5777c478bd9Sstevel@tonic-gate 				item->searchSpec.triple.attrs == 0 &&
5787c478bd9Sstevel@tonic-gate 				item->searchSpec.triple.element == 0) {
5797c478bd9Sstevel@tonic-gate 			/*
5807c478bd9Sstevel@tonic-gate 			 * We have a valid triple, but it points to the
5817c478bd9Sstevel@tonic-gate 			 * current LDAP container. Thus, first look in
5827c478bd9Sstevel@tonic-gate 			 * the rule-value; if that fails, perform a direct
5837c478bd9Sstevel@tonic-gate 			 * LDAP lookup.
5847c478bd9Sstevel@tonic-gate 			 */
5857c478bd9Sstevel@tonic-gate 			if (rv != 0) {
5867c478bd9Sstevel@tonic-gate 				check = rvThenLookup;
5877c478bd9Sstevel@tonic-gate 			} else {
5887c478bd9Sstevel@tonic-gate 				check = lookupOnly;
5897c478bd9Sstevel@tonic-gate 			}
5907c478bd9Sstevel@tonic-gate 		} else {
5917c478bd9Sstevel@tonic-gate 			/*
5927c478bd9Sstevel@tonic-gate 			 * Valid triple, and it's not the current container
5937c478bd9Sstevel@tonic-gate 			 * (at least not in the trivial sense). Hence, do
5947c478bd9Sstevel@tonic-gate 			 * a direct LDAP lookup.
5957c478bd9Sstevel@tonic-gate 			 */
5967c478bd9Sstevel@tonic-gate 			check = lookupOnly;
5977c478bd9Sstevel@tonic-gate 		}
5987c478bd9Sstevel@tonic-gate 		break;
5997c478bd9Sstevel@tonic-gate 	default:
6007c478bd9Sstevel@tonic-gate 		return (0);
6017c478bd9Sstevel@tonic-gate 	}
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	/* Check the rule-value */
6047c478bd9Sstevel@tonic-gate 	if (check == rvOnly || check == rvThenLookup) {
6057c478bd9Sstevel@tonic-gate 		for (i = 0; i < numName; i++) {
6067c478bd9Sstevel@tonic-gate 			if (caseInsens)
6077c478bd9Sstevel@tonic-gate 				cmp = strcasecmp(item->name, name[i]);
6087c478bd9Sstevel@tonic-gate 			else
6097c478bd9Sstevel@tonic-gate 				cmp = strcmp(item->name, name[i]);
6107c478bd9Sstevel@tonic-gate 			if (cmp == 0) {
6117c478bd9Sstevel@tonic-gate 				if (nameVal[i].numVals <= 0)
6127c478bd9Sstevel@tonic-gate 					break;
6137c478bd9Sstevel@tonic-gate 				if (berstring == 0) {
6147c478bd9Sstevel@tonic-gate 					val = cloneValue(&nameVal[i], 1);
6157c478bd9Sstevel@tonic-gate 				} else if (yp2ldap && berstring[0] == 'a') {
6167c478bd9Sstevel@tonic-gate 					val = cloneValue(&nameVal[i], 1);
6177c478bd9Sstevel@tonic-gate 				} else {
6187c478bd9Sstevel@tonic-gate 					val = berDecode(&nameVal[i],
6197c478bd9Sstevel@tonic-gate 						berstring);
6207c478bd9Sstevel@tonic-gate 				}
6217c478bd9Sstevel@tonic-gate 				if (val != 0) {
6227c478bd9Sstevel@tonic-gate 					val->repeat = item->repeat;
6237c478bd9Sstevel@tonic-gate 					/*
6247c478bd9Sstevel@tonic-gate 					 * If value for nis+ column is
6257c478bd9Sstevel@tonic-gate 					 * passed with value, val is
6267c478bd9Sstevel@tonic-gate 					 * manipulated in cloneValue().
6277c478bd9Sstevel@tonic-gate 					 * To decide whether there are
6287c478bd9Sstevel@tonic-gate 					 * enough nis+ column values
6297c478bd9Sstevel@tonic-gate 					 * for rule to produce a value,
6307c478bd9Sstevel@tonic-gate 					 * we need nis+ column values
6317c478bd9Sstevel@tonic-gate 					 * as well as nis_mapping_element
6327c478bd9Sstevel@tonic-gate 					 * from the rule. If we are here,
6337c478bd9Sstevel@tonic-gate 					 * it indicates that the 'val has
6347c478bd9Sstevel@tonic-gate 					 * an valid value for the column
6357c478bd9Sstevel@tonic-gate 					 * item-> name. So set
6367c478bd9Sstevel@tonic-gate 					 * NP_LDAP_MAP_SUCCESS
6377c478bd9Sstevel@tonic-gate 					 * to np_ldap-stat.
6387c478bd9Sstevel@tonic-gate 					 */
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 					if (np_ldap_stat != NULL)
6417c478bd9Sstevel@tonic-gate 						*np_ldap_stat =
6427c478bd9Sstevel@tonic-gate 							NP_LDAP_MAP_SUCCESS;
6437c478bd9Sstevel@tonic-gate 				}
6447c478bd9Sstevel@tonic-gate 				break;
6457c478bd9Sstevel@tonic-gate 			}
6467c478bd9Sstevel@tonic-gate 		}
6477c478bd9Sstevel@tonic-gate 	}
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	/* Do a direct lookup ? */
6507c478bd9Sstevel@tonic-gate 	if (val == 0 && (check == rvThenLookup || check == lookupOnly)) {
65136e852a1SRaja Andra 		if (item->type == mit_ldap) {
6527c478bd9Sstevel@tonic-gate 			int	err = 0;
6537c478bd9Sstevel@tonic-gate 			__nis_search_triple_t	triple;
6547c478bd9Sstevel@tonic-gate 			char			*baseDN;
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 			/*
6577c478bd9Sstevel@tonic-gate 			 * If item->searchSpec.triple.base is NULL, or ends
6587c478bd9Sstevel@tonic-gate 			 * in a comma, append the current search base from
6597c478bd9Sstevel@tonic-gate 			 * the TSD (put there by an upper layer).
6607c478bd9Sstevel@tonic-gate 			 *
6617c478bd9Sstevel@tonic-gate 			 * Special case for N2L mode:
6627c478bd9Sstevel@tonic-gate 			 * if item->searchSpec.triple.base ends in a comma,
6637c478bd9Sstevel@tonic-gate 			 * the current domain Context is used.
6647c478bd9Sstevel@tonic-gate 			 */
6657c478bd9Sstevel@tonic-gate 			if (yp2ldap && item->searchSpec.triple.base &&
6667c478bd9Sstevel@tonic-gate 				strlen(item->searchSpec.triple.base) > 0) {
6677c478bd9Sstevel@tonic-gate 				baseDN = __nisdb_get_tsd()->domainContext;
6687c478bd9Sstevel@tonic-gate 			} else {
6697c478bd9Sstevel@tonic-gate 				baseDN = __nisdb_get_tsd()->searchBase;
6707c478bd9Sstevel@tonic-gate 			}
6717c478bd9Sstevel@tonic-gate 			triple.base = appendBase(item->searchSpec.triple.base,
6727c478bd9Sstevel@tonic-gate 				baseDN, &err, 0);
6737c478bd9Sstevel@tonic-gate 			if (err == 0) {
6747c478bd9Sstevel@tonic-gate 				triple.scope = item->searchSpec.triple.scope;
6757c478bd9Sstevel@tonic-gate 				triple.attrs = item->searchSpec.triple.attrs;
6767c478bd9Sstevel@tonic-gate 				triple.element =
6777c478bd9Sstevel@tonic-gate 					item->searchSpec.triple.element;
6787c478bd9Sstevel@tonic-gate 				val = lookupLDAP(&triple, item->name, rv, 0,
6797c478bd9Sstevel@tonic-gate 					np_ldap_stat);
6807c478bd9Sstevel@tonic-gate 				fromldap = '1';
6817c478bd9Sstevel@tonic-gate 			} else {
6827c478bd9Sstevel@tonic-gate 				val = 0;
6837c478bd9Sstevel@tonic-gate 			}
6847c478bd9Sstevel@tonic-gate 			sfree(triple.base);
6857c478bd9Sstevel@tonic-gate 		}
6867c478bd9Sstevel@tonic-gate 	}
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	/* Special processing for NIS to LDAP mode */
6907c478bd9Sstevel@tonic-gate 	if (yp2ldap && val != 0) {
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 		/*
6937c478bd9Sstevel@tonic-gate 		 * Escape special chars from dn before sending to DIT,
6947c478bd9Sstevel@tonic-gate 		 * provided val is not ldap-based
6957c478bd9Sstevel@tonic-gate 		 */
6967c478bd9Sstevel@tonic-gate 		if (fromldap == '\0' && __nisdb_get_tsd()->escapeFlag == '1') {
6977c478bd9Sstevel@tonic-gate 			if (escapeSpecialChars(val) < 0) {
6987c478bd9Sstevel@tonic-gate 				freeValue(val, 1);
6997c478bd9Sstevel@tonic-gate 				return (0);
7007c478bd9Sstevel@tonic-gate 			}
7017c478bd9Sstevel@tonic-gate 		} else if (__nisdb_get_tsd()->escapeFlag == '2') {
7027c478bd9Sstevel@tonic-gate 			/* Remove escape chars from data received from DIT */
7037c478bd9Sstevel@tonic-gate 			(void) removeEscapeChars(val);
7047c478bd9Sstevel@tonic-gate 		}
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 		/*
7077c478bd9Sstevel@tonic-gate 		 * Remove from 'val', any values obtained using
7087c478bd9Sstevel@tonic-gate 		 * the 'removespec' syntax
7097c478bd9Sstevel@tonic-gate 		 */
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 		/* Obtain exVal */
7127c478bd9Sstevel@tonic-gate 		if (item->exItem)
7137c478bd9Sstevel@tonic-gate 			exVal = getMappingItemVal(item->exItem, native, rv,
7147c478bd9Sstevel@tonic-gate 			    berstring, NULL);
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 		/* delete */
7177c478bd9Sstevel@tonic-gate 		if (exVal != 0) {
7187c478bd9Sstevel@tonic-gate 			for (i = 0; i < val->numVals; ) {
7197c478bd9Sstevel@tonic-gate 				for (j = 0; j < exVal->numVals; j++) {
7207c478bd9Sstevel@tonic-gate 					if (sstrncmp(val->val[i].value,
7217c478bd9Sstevel@tonic-gate 							exVal->val[j].value,
7227c478bd9Sstevel@tonic-gate 							MAX(val->val[i].length,
7237c478bd9Sstevel@tonic-gate 							exVal->val[j].length))
7247c478bd9Sstevel@tonic-gate 							== 0)
7257c478bd9Sstevel@tonic-gate 						break;
7267c478bd9Sstevel@tonic-gate 				}
7277c478bd9Sstevel@tonic-gate 				if (j < exVal->numVals) {
7287c478bd9Sstevel@tonic-gate 					sfree(val->val[i].value);
7297c478bd9Sstevel@tonic-gate 					val->val[i].value = 0;
7307c478bd9Sstevel@tonic-gate 					val->val[i].length = 0;
7317c478bd9Sstevel@tonic-gate 					for (k = i; k < val->numVals - 1; k++) {
7327c478bd9Sstevel@tonic-gate 						val->val[k] = val->val[k + 1];
7337c478bd9Sstevel@tonic-gate 						val->val[k + 1].value = 0;
7347c478bd9Sstevel@tonic-gate 						val->val[k + 1].length = 0;
7357c478bd9Sstevel@tonic-gate 					}
7367c478bd9Sstevel@tonic-gate 					val->numVals--;
7377c478bd9Sstevel@tonic-gate 				} else
7387c478bd9Sstevel@tonic-gate 					i++;
7397c478bd9Sstevel@tonic-gate 			}
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 			freeValue(exVal, 1);
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 			/*
7447c478bd9Sstevel@tonic-gate 			 * If val->numVals <= 0, then we have no val to
7457c478bd9Sstevel@tonic-gate 			 * return. So free up stuff.
7467c478bd9Sstevel@tonic-gate 			 */
7477c478bd9Sstevel@tonic-gate 			if (val->numVals <= 0) {
7487c478bd9Sstevel@tonic-gate 				free(val->val);
7497c478bd9Sstevel@tonic-gate 				val->val = 0;
7507c478bd9Sstevel@tonic-gate 				free(val);
7517c478bd9Sstevel@tonic-gate 				return (0);
7527c478bd9Sstevel@tonic-gate 			}
7537c478bd9Sstevel@tonic-gate 		}
7547c478bd9Sstevel@tonic-gate 	}
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	return (val);
7577c478bd9Sstevel@tonic-gate }
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate __nis_value_t *
7607c478bd9Sstevel@tonic-gate getMappingFormat(__nis_mapping_format_t *f, __nis_rule_value_t *rv,
7617c478bd9Sstevel@tonic-gate 			__nis_format_arg_t at, void *a, int *numArg) {
7627c478bd9Sstevel@tonic-gate 	char		*myself = "getMappingFormat";
7637c478bd9Sstevel@tonic-gate 	__nis_value_t	*val = 0;
7647c478bd9Sstevel@tonic-gate 	__nis_buffer_t	b = {0, 0};
7657c478bd9Sstevel@tonic-gate 	int		i;
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	if (f == 0)
7687c478bd9Sstevel@tonic-gate 		return (0);
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	if (rv == 0) {
7717c478bd9Sstevel@tonic-gate 		val = am(myself, sizeof (*val));
7727c478bd9Sstevel@tonic-gate 		if (val == 0)
7737c478bd9Sstevel@tonic-gate 			return (0);
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 		switch (f->type) {
7767c478bd9Sstevel@tonic-gate 		case mmt_item:
7777c478bd9Sstevel@tonic-gate 			bp2buf(myself, &b, "%%s");
7787c478bd9Sstevel@tonic-gate 			break;
7797c478bd9Sstevel@tonic-gate 		case mmt_string:
7807c478bd9Sstevel@tonic-gate 			bp2buf(myself, &b, "%s", NIL(f->match.string));
7817c478bd9Sstevel@tonic-gate 			break;
7827c478bd9Sstevel@tonic-gate 		case mmt_single:
7837c478bd9Sstevel@tonic-gate 			bp2buf(myself, &b, "[");
7847c478bd9Sstevel@tonic-gate 			for (i = 0; i < f->match.single.numRange; i++) {
7857c478bd9Sstevel@tonic-gate 				if (f->match.single.lo[i] ==
7867c478bd9Sstevel@tonic-gate 						f->match.single.hi[i])
7877c478bd9Sstevel@tonic-gate 					bp2buf(myself, &b, "%c",
7887c478bd9Sstevel@tonic-gate 						f->match.single.lo[i]);
7897c478bd9Sstevel@tonic-gate 				else
7907c478bd9Sstevel@tonic-gate 					bp2buf(myself, &b, "%c-%c",
7917c478bd9Sstevel@tonic-gate 						f->match.single.lo[i],
7927c478bd9Sstevel@tonic-gate 						f->match.single.hi[i]);
7937c478bd9Sstevel@tonic-gate 			}
7947c478bd9Sstevel@tonic-gate 			bp2buf(myself, &b, "]");
7957c478bd9Sstevel@tonic-gate 			break;
7967c478bd9Sstevel@tonic-gate 		case mmt_limit:
7977c478bd9Sstevel@tonic-gate 			break;
7987c478bd9Sstevel@tonic-gate 		case mmt_any:
7997c478bd9Sstevel@tonic-gate 			bp2buf(myself, &b, "*");
8007c478bd9Sstevel@tonic-gate 			break;
8017c478bd9Sstevel@tonic-gate 		case mmt_berstring:
8027c478bd9Sstevel@tonic-gate 			bp2buf(myself, &b, "%s", NIL(f->match.berString));
8037c478bd9Sstevel@tonic-gate 			break;
8047c478bd9Sstevel@tonic-gate 		case mmt_begin:
8057c478bd9Sstevel@tonic-gate 		case mmt_end:
8067c478bd9Sstevel@tonic-gate 			bp2buf(myself, &b, "\"");
8077c478bd9Sstevel@tonic-gate 			break;
8087c478bd9Sstevel@tonic-gate 		default:
8097c478bd9Sstevel@tonic-gate 			bp2buf(myself, &b, "<unknown>");
8107c478bd9Sstevel@tonic-gate 		}
8117c478bd9Sstevel@tonic-gate 		val->type = vt_string;
8127c478bd9Sstevel@tonic-gate 		val->numVals = 1;
8137c478bd9Sstevel@tonic-gate 		val->val = am(myself, sizeof (val->val[0]));
8147c478bd9Sstevel@tonic-gate 		if (val->val == 0) {
8157c478bd9Sstevel@tonic-gate 			sfree(val);
8167c478bd9Sstevel@tonic-gate 			return (0);
8177c478bd9Sstevel@tonic-gate 		}
8187c478bd9Sstevel@tonic-gate 		val->val[0].value = b.buf;
8197c478bd9Sstevel@tonic-gate 		val->val[0].length = b.len;
8207c478bd9Sstevel@tonic-gate 	} else {
8217c478bd9Sstevel@tonic-gate 		switch (f->type) {
8227c478bd9Sstevel@tonic-gate 		case mmt_item:
8237c478bd9Sstevel@tonic-gate 		case mmt_berstring:
8247c478bd9Sstevel@tonic-gate 			if (a != 0) {
8257c478bd9Sstevel@tonic-gate 				if (at == fa_item) {
8267c478bd9Sstevel@tonic-gate 					val = getMappingItemVal(
8277c478bd9Sstevel@tonic-gate 						(__nis_mapping_item_t *)a,
8287c478bd9Sstevel@tonic-gate 						mit_any, rv,
8297c478bd9Sstevel@tonic-gate 		(f->type == mmt_berstring) ? f->match.berString : 0, NULL);
8307c478bd9Sstevel@tonic-gate 					if (numArg != 0)
8317c478bd9Sstevel@tonic-gate 						(*numArg)++;
8327c478bd9Sstevel@tonic-gate 				} else {
8337c478bd9Sstevel@tonic-gate 					val = cloneValue(
8347c478bd9Sstevel@tonic-gate 						(__nis_value_t *)a, 1);
8357c478bd9Sstevel@tonic-gate 					if (numArg != 0)
8367c478bd9Sstevel@tonic-gate 						(*numArg)++;
8377c478bd9Sstevel@tonic-gate 				}
8387c478bd9Sstevel@tonic-gate 			}
8397c478bd9Sstevel@tonic-gate 			break;
8407c478bd9Sstevel@tonic-gate 		case mmt_string:
8417c478bd9Sstevel@tonic-gate 			val = am(myself, sizeof (*val));
8427c478bd9Sstevel@tonic-gate 			if (val == 0)
8437c478bd9Sstevel@tonic-gate 				return (0);
8447c478bd9Sstevel@tonic-gate 			val->type = vt_string;
8457c478bd9Sstevel@tonic-gate 			val->numVals = 1;
8467c478bd9Sstevel@tonic-gate 			val->val = am(myself, sizeof (val->val[0]));
8477c478bd9Sstevel@tonic-gate 			if (val->val == 0) {
8487c478bd9Sstevel@tonic-gate 				sfree(val);
8497c478bd9Sstevel@tonic-gate 				return (0);
8507c478bd9Sstevel@tonic-gate 			}
8517c478bd9Sstevel@tonic-gate 			val->val[0].value = sdup(myself, T, f->match.string);
8527c478bd9Sstevel@tonic-gate 			val->val[0].length = strlen(val->val[0].value);
8537c478bd9Sstevel@tonic-gate 			break;
8547c478bd9Sstevel@tonic-gate 		case mmt_single:
8557c478bd9Sstevel@tonic-gate 		case mmt_limit:
8567c478bd9Sstevel@tonic-gate 		case mmt_any:
8577c478bd9Sstevel@tonic-gate 		case mmt_begin:
8587c478bd9Sstevel@tonic-gate 		case mmt_end:
8597c478bd9Sstevel@tonic-gate 			/* Not an error, so return an empty value */
8607c478bd9Sstevel@tonic-gate 			val = am(myself, sizeof (*val));
8617c478bd9Sstevel@tonic-gate 			if (val == 0)
8627c478bd9Sstevel@tonic-gate 				return (0);
8637c478bd9Sstevel@tonic-gate 			val->type = vt_string;
8647c478bd9Sstevel@tonic-gate 			val->numVals = 0;
8657c478bd9Sstevel@tonic-gate 			val->val = 0;
8667c478bd9Sstevel@tonic-gate 			break;
8677c478bd9Sstevel@tonic-gate 		default:
8687c478bd9Sstevel@tonic-gate 			/* Do nothing */
8697c478bd9Sstevel@tonic-gate 			val = 0;
8707c478bd9Sstevel@tonic-gate 			break;
8717c478bd9Sstevel@tonic-gate 		}
8727c478bd9Sstevel@tonic-gate 	}
8737c478bd9Sstevel@tonic-gate 	return (val);
8747c478bd9Sstevel@tonic-gate }
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate /*
8777c478bd9Sstevel@tonic-gate  * Used when evaluating an expression. Typically, the value of the
8787c478bd9Sstevel@tonic-gate  * expression so far will be kept in 'v1', and 'v2' is the value
8797c478bd9Sstevel@tonic-gate  * of the current component of the expression. In the general case,
8807c478bd9Sstevel@tonic-gate  * both will be multi-valued, and the result is an "explosion"
8817c478bd9Sstevel@tonic-gate  * resulting in N*M new values (if 'v1' had N values, and 'v2'
8827c478bd9Sstevel@tonic-gate  * M ditto).
8837c478bd9Sstevel@tonic-gate  *
8847c478bd9Sstevel@tonic-gate  * For example, if v1 = {"ab", "cd", "ef"}, and v2 = {"gh", "ij", "kl"},
8857c478bd9Sstevel@tonic-gate  * the result will be {"abgh", "abij", "abkl", "cdgh", "cdij", "cdkl",
8867c478bd9Sstevel@tonic-gate  * "efgh", "efij", "efkl"}.
8877c478bd9Sstevel@tonic-gate  *
8887c478bd9Sstevel@tonic-gate  * There are special cases when v1->repeat and/or v2->repeat are set.
8897c478bd9Sstevel@tonic-gate  * Repeat mostly makes sense with single values; for example, if
8907c478bd9Sstevel@tonic-gate  * v1 = {"x="} with repeat on, and v2 = {"1", "2", "3"}, the result
8917c478bd9Sstevel@tonic-gate  * is {"x=1", "x=2", "x=3"}.
8927c478bd9Sstevel@tonic-gate  *
8937c478bd9Sstevel@tonic-gate  * The result if v2 also had repeat on would be {"x=1x=2x=3"}. It's
8947c478bd9Sstevel@tonic-gate  * not clear if there's a useful application for this, but the code's
8957c478bd9Sstevel@tonic-gate  * there for the sake of orthogonality.
8967c478bd9Sstevel@tonic-gate  */
8977c478bd9Sstevel@tonic-gate __nis_value_t *
8987c478bd9Sstevel@tonic-gate explodeValues(__nis_value_t *v1, __nis_value_t *v2) {
8997c478bd9Sstevel@tonic-gate 	int		i1, i2, n, nv;
9007c478bd9Sstevel@tonic-gate 	__nis_value_t	*v;
9017c478bd9Sstevel@tonic-gate 	__nis_buffer_t	b = {0, 0};
9027c478bd9Sstevel@tonic-gate 	char		*myself = "explodeValues";
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	if (v1 == 0 || v1->numVals <= 0)
9057c478bd9Sstevel@tonic-gate 		return (cloneValue(v2, 1));
9067c478bd9Sstevel@tonic-gate 	if (v2 == 0 || v2->numVals <= 0)
9077c478bd9Sstevel@tonic-gate 		return (cloneValue(v1, 1));
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 	/*
9107c478bd9Sstevel@tonic-gate 	 * XXX What should we do if (v1->type != v2->type) ?
9117c478bd9Sstevel@tonic-gate 	 * Policy: Just explode anyway, even though the result is
9127c478bd9Sstevel@tonic-gate 	 * unlikely to be very useful.
9137c478bd9Sstevel@tonic-gate 	 */
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 	v = am(myself, sizeof (*v));
9167c478bd9Sstevel@tonic-gate 	if (v == 0)
9177c478bd9Sstevel@tonic-gate 		return (0);
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 	if (!v1->repeat && !v2->repeat)
9207c478bd9Sstevel@tonic-gate 		nv = v1->numVals * v2->numVals;
9217c478bd9Sstevel@tonic-gate 	else if (v1->repeat && !v2->repeat)
9227c478bd9Sstevel@tonic-gate 		nv = v2->numVals;
9237c478bd9Sstevel@tonic-gate 	else if (!v1->repeat && v2->repeat)
9247c478bd9Sstevel@tonic-gate 		nv = v1->numVals;
9257c478bd9Sstevel@tonic-gate 	else /* v1->repeat && v2->repeat */
9267c478bd9Sstevel@tonic-gate 		nv = 1;
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	v->val = am(myself, nv * sizeof (v->val[0]));
9297c478bd9Sstevel@tonic-gate 	if (v->val == 0) {
9307c478bd9Sstevel@tonic-gate 		free(v);
9317c478bd9Sstevel@tonic-gate 		return (0);
9327c478bd9Sstevel@tonic-gate 	}
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 	/*
9357c478bd9Sstevel@tonic-gate 	 * Four different cases, depending on the 'repeat' flags.
9367c478bd9Sstevel@tonic-gate 	 */
9377c478bd9Sstevel@tonic-gate 	if (!v1->repeat && !v2->repeat) {
9387c478bd9Sstevel@tonic-gate 		for (i1 = 0, n = 0; i1 < v1->numVals; i1++) {
9397c478bd9Sstevel@tonic-gate 			for (i2 = 0; i2 < v2->numVals; i2++) {
9407c478bd9Sstevel@tonic-gate 				if (v1->type == vt_string)
9417c478bd9Sstevel@tonic-gate 					sbc2buf(myself, v1->val[i1].value,
9427c478bd9Sstevel@tonic-gate 						v1->val[i1].length,
9437c478bd9Sstevel@tonic-gate 						&b);
9447c478bd9Sstevel@tonic-gate 				else
9457c478bd9Sstevel@tonic-gate 					bc2buf(myself, v1->val[i1].value,
9467c478bd9Sstevel@tonic-gate 						v1->val[i1].length,
9477c478bd9Sstevel@tonic-gate 						&b);
9487c478bd9Sstevel@tonic-gate 				if (v2->type == vt_string)
9497c478bd9Sstevel@tonic-gate 					sbc2buf(myself, v2->val[i2].value,
9507c478bd9Sstevel@tonic-gate 						v2->val[i2].length,
9517c478bd9Sstevel@tonic-gate 						&b);
9527c478bd9Sstevel@tonic-gate 				else
9537c478bd9Sstevel@tonic-gate 					bc2buf(myself, v2->val[i2].value,
9547c478bd9Sstevel@tonic-gate 						v2->val[i2].length,
9557c478bd9Sstevel@tonic-gate 						&b);
9567c478bd9Sstevel@tonic-gate 				v->val[n].value = b.buf;
9577c478bd9Sstevel@tonic-gate 				v->val[n].length = b.len;
9587c478bd9Sstevel@tonic-gate 				n++;
9597c478bd9Sstevel@tonic-gate 				b.buf = 0;
9607c478bd9Sstevel@tonic-gate 				b.len = 0;
9617c478bd9Sstevel@tonic-gate 			}
9627c478bd9Sstevel@tonic-gate 		}
9637c478bd9Sstevel@tonic-gate 	} else if (v1->repeat && !v2->repeat) {
9647c478bd9Sstevel@tonic-gate 		for (i2 = 0; i2 < v2->numVals; i2++) {
9657c478bd9Sstevel@tonic-gate 			for (i1 = 0, n = 0; i1 < v1->numVals; i1++) {
9667c478bd9Sstevel@tonic-gate 				if (v1->type == vt_string)
9677c478bd9Sstevel@tonic-gate 					sbc2buf(myself, v1->val[i1].value,
9687c478bd9Sstevel@tonic-gate 						v1->val[i1].length,
9697c478bd9Sstevel@tonic-gate 						&b);
9707c478bd9Sstevel@tonic-gate 				else
9717c478bd9Sstevel@tonic-gate 					bc2buf(myself, v1->val[i1].value,
9727c478bd9Sstevel@tonic-gate 						v1->val[i1].length,
9737c478bd9Sstevel@tonic-gate 						&b);
9747c478bd9Sstevel@tonic-gate 				if (v2->type == vt_string)
9757c478bd9Sstevel@tonic-gate 					sbc2buf(myself, v2->val[i2].value,
9767c478bd9Sstevel@tonic-gate 						v2->val[i2].length,
9777c478bd9Sstevel@tonic-gate 						&b);
9787c478bd9Sstevel@tonic-gate 				else
9797c478bd9Sstevel@tonic-gate 					bc2buf(myself, v2->val[i2].value,
9807c478bd9Sstevel@tonic-gate 						v2->val[i2].length,
9817c478bd9Sstevel@tonic-gate 						&b);
9827c478bd9Sstevel@tonic-gate 			}
9837c478bd9Sstevel@tonic-gate 			v->val[n].value = b.buf;
9847c478bd9Sstevel@tonic-gate 			v->val[n].length = b.len;
9857c478bd9Sstevel@tonic-gate 			n++;
9867c478bd9Sstevel@tonic-gate 			b.buf = 0;
9877c478bd9Sstevel@tonic-gate 			b.len = 0;
9887c478bd9Sstevel@tonic-gate 		}
9897c478bd9Sstevel@tonic-gate 	} else if (!v1->repeat && v2->repeat) {
9907c478bd9Sstevel@tonic-gate 		for (i1 = 0, n = 0; i1 < v1->numVals; i1++) {
9917c478bd9Sstevel@tonic-gate 			for (i2 = 0; i2 < v2->numVals; i2++) {
9927c478bd9Sstevel@tonic-gate 				if (v1->type == vt_string)
9937c478bd9Sstevel@tonic-gate 					sbc2buf(myself, v1->val[i1].value,
9947c478bd9Sstevel@tonic-gate 						v1->val[i1].length,
9957c478bd9Sstevel@tonic-gate 						&b);
9967c478bd9Sstevel@tonic-gate 				else
9977c478bd9Sstevel@tonic-gate 					bc2buf(myself, v1->val[i1].value,
9987c478bd9Sstevel@tonic-gate 						v1->val[i1].length,
9997c478bd9Sstevel@tonic-gate 						&b);
10007c478bd9Sstevel@tonic-gate 				if (v2->type == vt_string)
10017c478bd9Sstevel@tonic-gate 					sbc2buf(myself, v2->val[i2].value,
10027c478bd9Sstevel@tonic-gate 						v2->val[i2].length,
10037c478bd9Sstevel@tonic-gate 						&b);
10047c478bd9Sstevel@tonic-gate 				else
10057c478bd9Sstevel@tonic-gate 					bc2buf(myself, v2->val[i2].value,
10067c478bd9Sstevel@tonic-gate 						v2->val[i2].length,
10077c478bd9Sstevel@tonic-gate 						&b);
10087c478bd9Sstevel@tonic-gate 			}
10097c478bd9Sstevel@tonic-gate 			v->val[n].value = b.buf;
10107c478bd9Sstevel@tonic-gate 			v->val[n].length = b.len;
10117c478bd9Sstevel@tonic-gate 			n++;
10127c478bd9Sstevel@tonic-gate 			b.buf = 0;
10137c478bd9Sstevel@tonic-gate 			b.len = 0;
10147c478bd9Sstevel@tonic-gate 		}
10157c478bd9Sstevel@tonic-gate 	} else { /* v1->repeat && v2->repeat */
10167c478bd9Sstevel@tonic-gate 		for (i1 = 0, n = 0; i1 < v1->numVals; i1++) {
10177c478bd9Sstevel@tonic-gate 			for (i2 = 0; i2 < v2->numVals; i2++) {
10187c478bd9Sstevel@tonic-gate 				if (v1->type == vt_string)
10197c478bd9Sstevel@tonic-gate 					sbc2buf(myself, v1->val[i1].value,
10207c478bd9Sstevel@tonic-gate 						v1->val[i1].length,
10217c478bd9Sstevel@tonic-gate 						&b);
10227c478bd9Sstevel@tonic-gate 				else
10237c478bd9Sstevel@tonic-gate 					bc2buf(myself, v1->val[i1].value,
10247c478bd9Sstevel@tonic-gate 						v1->val[i1].length,
10257c478bd9Sstevel@tonic-gate 						&b);
10267c478bd9Sstevel@tonic-gate 				if (v2->type == vt_string)
10277c478bd9Sstevel@tonic-gate 					sbc2buf(myself, v2->val[i2].value,
10287c478bd9Sstevel@tonic-gate 						v2->val[i2].length,
10297c478bd9Sstevel@tonic-gate 						&b);
10307c478bd9Sstevel@tonic-gate 				else
10317c478bd9Sstevel@tonic-gate 					bc2buf(myself, v2->val[i2].value,
10327c478bd9Sstevel@tonic-gate 						v2->val[i2].length,
10337c478bd9Sstevel@tonic-gate 						&b);
10347c478bd9Sstevel@tonic-gate 			}
10357c478bd9Sstevel@tonic-gate 		}
10367c478bd9Sstevel@tonic-gate 		v->val[n].value = b.buf;
10377c478bd9Sstevel@tonic-gate 		v->val[n].length = b.len;
10387c478bd9Sstevel@tonic-gate 		n++;
10397c478bd9Sstevel@tonic-gate 		b.buf = 0;
10407c478bd9Sstevel@tonic-gate 		b.len = 0;
10417c478bd9Sstevel@tonic-gate 	}
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate #ifdef	NISDB_LDAP_DEBUG
10447c478bd9Sstevel@tonic-gate 	/* Sanity check */
10457c478bd9Sstevel@tonic-gate 	if (n != nv)
10467c478bd9Sstevel@tonic-gate 		abort();
10477c478bd9Sstevel@tonic-gate #endif	/* NISD__LDAP_DEBUG */
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate 	v->type = (v1->type == vt_string) ?
10507c478bd9Sstevel@tonic-gate 			((v2->type == vt_string) ?
10517c478bd9Sstevel@tonic-gate 				vt_string : vt_ber) : vt_ber;
10527c478bd9Sstevel@tonic-gate 	v->repeat = 0;
10537c478bd9Sstevel@tonic-gate 	v->numVals = n;
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 	return (v);
10567c478bd9Sstevel@tonic-gate }
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate __nis_value_t *
10597c478bd9Sstevel@tonic-gate getMappingFormatArray(__nis_mapping_format_t *a, __nis_rule_value_t *rv,
10607c478bd9Sstevel@tonic-gate 			__nis_format_arg_t at, int numArgs, void *arg) {
10617c478bd9Sstevel@tonic-gate 	int			i, ia = 0;
10627c478bd9Sstevel@tonic-gate 	__nis_value_t		*val, *v = 0;
10637c478bd9Sstevel@tonic-gate 	bool_t			moreFormat = (a != 0);
10647c478bd9Sstevel@tonic-gate 	bool_t			moreArgs = (numArgs > 0);
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 	while (moreFormat && (arg == 0 || ia < numArgs)) {
10677c478bd9Sstevel@tonic-gate 		for (i = 0; moreFormat; i++) {
10687c478bd9Sstevel@tonic-gate 			moreFormat = (a[i].type != mmt_end);
10697c478bd9Sstevel@tonic-gate 			if (at == fa_item) {
10707c478bd9Sstevel@tonic-gate 				__nis_mapping_item_t *item = arg;
10717c478bd9Sstevel@tonic-gate 				val = getMappingFormat(&a[i], rv, at,
10727c478bd9Sstevel@tonic-gate 					((item != 0) ? &item[ia] : 0), &ia);
10737c478bd9Sstevel@tonic-gate 			} else {
10747c478bd9Sstevel@tonic-gate 				__nis_value_t **ival = arg;
10757c478bd9Sstevel@tonic-gate 				val = getMappingFormat(&a[i], rv, at,
10767c478bd9Sstevel@tonic-gate 					((ival != 0) ? ival[ia] : 0), &ia);
10777c478bd9Sstevel@tonic-gate 			}
10787c478bd9Sstevel@tonic-gate 			if (val != 0) {
10797c478bd9Sstevel@tonic-gate 				__nis_value_t	*new = explodeValues(v, val);
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate 				freeValue(v, 1);
10827c478bd9Sstevel@tonic-gate 				freeValue(val, 1);
10837c478bd9Sstevel@tonic-gate 				if (new == 0)
10847c478bd9Sstevel@tonic-gate 					return (0);
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 				v = new;
10877c478bd9Sstevel@tonic-gate 			} else {
10887c478bd9Sstevel@tonic-gate 				freeValue(v, 1);
10897c478bd9Sstevel@tonic-gate 				return (0);
10907c478bd9Sstevel@tonic-gate 			}
10917c478bd9Sstevel@tonic-gate 			/*
10927c478bd9Sstevel@tonic-gate 			 * If we run out of arguments, but still have format
10937c478bd9Sstevel@tonic-gate 			 * remaining, repeat the last argument. Keep track of
10947c478bd9Sstevel@tonic-gate 			 * the fact that we've really consumed all arguments.
10957c478bd9Sstevel@tonic-gate 			 */
10967c478bd9Sstevel@tonic-gate 			if (moreFormat && ia >= numArgs) {
10977c478bd9Sstevel@tonic-gate 				ia = (numArgs > 0) ? numArgs - 1 : 0;
10987c478bd9Sstevel@tonic-gate 				moreArgs = FALSE;
10997c478bd9Sstevel@tonic-gate 			}
11007c478bd9Sstevel@tonic-gate 		}
11017c478bd9Sstevel@tonic-gate 		/*
11027c478bd9Sstevel@tonic-gate 		 * We've run out of format, so if we still have arguments
11037c478bd9Sstevel@tonic-gate 		 * left, start over on the format.
11047c478bd9Sstevel@tonic-gate 		 */
11057c478bd9Sstevel@tonic-gate 		if (ia < numArgs && moreArgs) {
11067c478bd9Sstevel@tonic-gate 			/*
11077c478bd9Sstevel@tonic-gate 			 * However, if we didn't consume any arguments going
11087c478bd9Sstevel@tonic-gate 			 * through the format once, abort to avoid an infinite
11097c478bd9Sstevel@tonic-gate 			 * loop.
11107c478bd9Sstevel@tonic-gate 			 */
11117c478bd9Sstevel@tonic-gate 			if (numArgs > 0 && ia <= 0) {
11127c478bd9Sstevel@tonic-gate 				freeValue(v, 1);
11137c478bd9Sstevel@tonic-gate 				return (0);
11147c478bd9Sstevel@tonic-gate 			}
11157c478bd9Sstevel@tonic-gate 			moreFormat = 1;
11167c478bd9Sstevel@tonic-gate 		}
11177c478bd9Sstevel@tonic-gate 	}
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 	return (v);
11207c478bd9Sstevel@tonic-gate }
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate /*
11237c478bd9Sstevel@tonic-gate  * Returns a string representation (such as "[name=foo, value=bar]")
11247c478bd9Sstevel@tonic-gate  * of a nis_index_t.
11257c478bd9Sstevel@tonic-gate  */
11267c478bd9Sstevel@tonic-gate char *
11277c478bd9Sstevel@tonic-gate getIndex(__nis_index_t *i, int *len) {
11287c478bd9Sstevel@tonic-gate 	int		n;
11297c478bd9Sstevel@tonic-gate 	__nis_buffer_t	b = {0, 0};
11307c478bd9Sstevel@tonic-gate 	char		*myself = "getIndex";
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate 	if (i == 0)
11337c478bd9Sstevel@tonic-gate 		return (0);
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 	if (i->numIndexes > 0) {
11367c478bd9Sstevel@tonic-gate 		bp2buf(myself, &b, "[");
11377c478bd9Sstevel@tonic-gate 		for (n = 0; n < i->numIndexes; n++) {
11387c478bd9Sstevel@tonic-gate 			__nis_value_t	*val;
11397c478bd9Sstevel@tonic-gate 			int		j;
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 			val = getMappingFormatArray(i->value[n],
11427c478bd9Sstevel@tonic-gate 						0, fa_any, 0, 0);
11437c478bd9Sstevel@tonic-gate 			if (n > 0)
11447c478bd9Sstevel@tonic-gate 				bp2buf(myself, &b, ", ");
11457c478bd9Sstevel@tonic-gate 			bp2buf(myself, &b, "%s=", i->name[n]);
11467c478bd9Sstevel@tonic-gate 			if (val != 0) {
11477c478bd9Sstevel@tonic-gate 				for (j = 0; j < val->numVals; j++) {
11487c478bd9Sstevel@tonic-gate 					bc2buf(myself, val->val[j].value,
11497c478bd9Sstevel@tonic-gate 						val->val[j].length, &b);
11507c478bd9Sstevel@tonic-gate 				}
11517c478bd9Sstevel@tonic-gate 			} else {
11527c478bd9Sstevel@tonic-gate 				bp2buf(myself, &b, "<no-vals>");
11537c478bd9Sstevel@tonic-gate 			}
11547c478bd9Sstevel@tonic-gate 			freeValue(val, 1);
11557c478bd9Sstevel@tonic-gate 		}
11567c478bd9Sstevel@tonic-gate 		bp2buf(myself, &b, "]");
11577c478bd9Sstevel@tonic-gate 	}
11587c478bd9Sstevel@tonic-gate 	if (len != 0)
11597c478bd9Sstevel@tonic-gate 		*len = b.len;
11607c478bd9Sstevel@tonic-gate 	return (b.buf);
11617c478bd9Sstevel@tonic-gate }
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate char *
11647c478bd9Sstevel@tonic-gate getObjSpec(__nis_obj_spec_t *o, int *len) {
11657c478bd9Sstevel@tonic-gate 	__nis_buffer_t	b = {0, 0};
11667c478bd9Sstevel@tonic-gate 	char		*myself = "getObjSpec";
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 	if (o == 0)
11697c478bd9Sstevel@tonic-gate 		return (0);
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate 	b.buf = getIndex(&o->index, &b.len);
11727c478bd9Sstevel@tonic-gate 	sbc2buf(myself, o->name, slen(o->name), &b);
11737c478bd9Sstevel@tonic-gate 	if (len != 0)
11747c478bd9Sstevel@tonic-gate 		*len = b.len;
11757c478bd9Sstevel@tonic-gate 	return (b.buf);
11767c478bd9Sstevel@tonic-gate }
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate /*
11797c478bd9Sstevel@tonic-gate  * Returns a string representation of the LDAP scope. Note that the
11807c478bd9Sstevel@tonic-gate  * returned value is a static entity, and must be copied by the
11817c478bd9Sstevel@tonic-gate  * caller (but, obviously, must not be freed).
11827c478bd9Sstevel@tonic-gate  */
11837c478bd9Sstevel@tonic-gate char *
11847c478bd9Sstevel@tonic-gate getScope(int scope) {
11857c478bd9Sstevel@tonic-gate 	switch (scope) {
11867c478bd9Sstevel@tonic-gate 	case LDAP_SCOPE_BASE:
11877c478bd9Sstevel@tonic-gate 		return ("base");
11887c478bd9Sstevel@tonic-gate 	case LDAP_SCOPE_ONELEVEL:
11897c478bd9Sstevel@tonic-gate 		return ("one");
11907c478bd9Sstevel@tonic-gate 	case LDAP_SCOPE_SUBTREE:
11917c478bd9Sstevel@tonic-gate 		return ("sub");
11927c478bd9Sstevel@tonic-gate 	default:
11937c478bd9Sstevel@tonic-gate 		return ("one");
11947c478bd9Sstevel@tonic-gate 	}
11957c478bd9Sstevel@tonic-gate }
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate /*
11987c478bd9Sstevel@tonic-gate  * Return a string representation of an LDAP search triple (such as
11997c478bd9Sstevel@tonic-gate  * "ou=Hosts,dc=eng,dc=sun,dc=com?one?cn=xyzzy").
12007c478bd9Sstevel@tonic-gate  */
12017c478bd9Sstevel@tonic-gate char *
12027c478bd9Sstevel@tonic-gate getSearchTriple(__nis_search_triple_t *s, int *len) {
12037c478bd9Sstevel@tonic-gate 	__nis_buffer_t	b = {0, 0};
12047c478bd9Sstevel@tonic-gate 	char		*a;
12057c478bd9Sstevel@tonic-gate 	int		l;
12067c478bd9Sstevel@tonic-gate 	char		*myself = "getSearchTriple";
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 	/* If the scope is LDAP_SCOPE_UNKNOWN, the search triple is unused */
12097c478bd9Sstevel@tonic-gate 	if (s == 0 || s->scope == LDAP_SCOPE_UNKNOWN) {
12107c478bd9Sstevel@tonic-gate 		if (len != 0)
12117c478bd9Sstevel@tonic-gate 			*len = 0;
12127c478bd9Sstevel@tonic-gate 		return (0);
12137c478bd9Sstevel@tonic-gate 	}
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 	if (s->base != 0)
12167c478bd9Sstevel@tonic-gate 		sbc2buf(myself, s->base, slen(s->base), &b);
12177c478bd9Sstevel@tonic-gate 	if (!(s->scope == LDAP_SCOPE_ONELEVEL &&
12187c478bd9Sstevel@tonic-gate 			(s->base == 0 || s->base[0] == '\0'))) {
12197c478bd9Sstevel@tonic-gate 		bp2buf(myself, &b, "?%s?", getScope(s->scope));
12207c478bd9Sstevel@tonic-gate 	}
12217c478bd9Sstevel@tonic-gate 	if ((l = slen(s->attrs)) > 0) {
12227c478bd9Sstevel@tonic-gate 		/*
12237c478bd9Sstevel@tonic-gate 		 * Remove white space from the filter/attribute list.
12247c478bd9Sstevel@tonic-gate 		 * The parser usually keeps any white space from the
12257c478bd9Sstevel@tonic-gate 		 * config file (or LDAP/command line), but we don't
12267c478bd9Sstevel@tonic-gate 		 * want it.
12277c478bd9Sstevel@tonic-gate 		 */
12287c478bd9Sstevel@tonic-gate 		a = am(myself, l+1);
12297c478bd9Sstevel@tonic-gate 		if (a != 0) {
12307c478bd9Sstevel@tonic-gate 			int	i, la;
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 			for (i = 0, la = 0; i < l; i++) {
12337c478bd9Sstevel@tonic-gate 				if (s->attrs[i] != ' ' &&
12347c478bd9Sstevel@tonic-gate 						s->attrs[i] != '\t')
12357c478bd9Sstevel@tonic-gate 					a[la++] = s->attrs[i];
12367c478bd9Sstevel@tonic-gate 			}
12377c478bd9Sstevel@tonic-gate 			sbc2buf(myself, a, la, &b);
12387c478bd9Sstevel@tonic-gate 			sfree(a);
12397c478bd9Sstevel@tonic-gate 		} else {
12407c478bd9Sstevel@tonic-gate 			sbc2buf(myself, s->attrs, slen(s->attrs), &b);
12417c478bd9Sstevel@tonic-gate 		}
12427c478bd9Sstevel@tonic-gate 	}
12437c478bd9Sstevel@tonic-gate 
12447c478bd9Sstevel@tonic-gate 	if (len != 0)
12457c478bd9Sstevel@tonic-gate 		*len = b.len;
12467c478bd9Sstevel@tonic-gate 	return (b.buf);
12477c478bd9Sstevel@tonic-gate }
12487c478bd9Sstevel@tonic-gate 
12497c478bd9Sstevel@tonic-gate __nis_value_t *
12507c478bd9Sstevel@tonic-gate getMappingItem(__nis_mapping_item_t *i, __nis_mapping_item_type_t native,
12517c478bd9Sstevel@tonic-gate 		__nis_rule_value_t *rv, char *berstring, int *np_ldap_stat) {
12527c478bd9Sstevel@tonic-gate 	char		*myself = "getMappingItem";
12537c478bd9Sstevel@tonic-gate 	__nis_value_t	*val = 0;
12547c478bd9Sstevel@tonic-gate 	__nis_buffer_t	b = {0, 0};
12557c478bd9Sstevel@tonic-gate 	int		len = 0;
12567c478bd9Sstevel@tonic-gate 	char		*buf;
12577c478bd9Sstevel@tonic-gate 
12587c478bd9Sstevel@tonic-gate 	if (i == 0)
12597c478bd9Sstevel@tonic-gate 		return (0);
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate 	if (rv != 0)
12627c478bd9Sstevel@tonic-gate 		return (getMappingItemVal(i, native, rv, berstring,
12637c478bd9Sstevel@tonic-gate 			np_ldap_stat));
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate 	val = am(myself, sizeof (*val));
12667c478bd9Sstevel@tonic-gate 	if (val == 0)
12677c478bd9Sstevel@tonic-gate 		return (0);
12687c478bd9Sstevel@tonic-gate 
12697c478bd9Sstevel@tonic-gate 	switch (i->type) {
12707c478bd9Sstevel@tonic-gate 	case mit_nisplus:
12717c478bd9Sstevel@tonic-gate 		if (native != mit_nisplus)
12727c478bd9Sstevel@tonic-gate 			bp2buf(myself, &b, "nis+:");
12737c478bd9Sstevel@tonic-gate 		bp2buf(myself, &b, "%s", NIL(i->name));
12747c478bd9Sstevel@tonic-gate 		buf = getObjSpec(&i->searchSpec.obj, &len);
12757c478bd9Sstevel@tonic-gate 		if (buf != 0 && len > 0) {
12767c478bd9Sstevel@tonic-gate 			bc2buf(myself, ":", 1, &b);
12777c478bd9Sstevel@tonic-gate 			sbc2buf(myself, buf, len, &b);
12787c478bd9Sstevel@tonic-gate 		}
12797c478bd9Sstevel@tonic-gate 		sfree(buf);
12807c478bd9Sstevel@tonic-gate 		val->type = vt_string;
12817c478bd9Sstevel@tonic-gate 		val->repeat = i->repeat;
12827c478bd9Sstevel@tonic-gate 		val->numVals = 1;
12837c478bd9Sstevel@tonic-gate 		val->val = am(myself, sizeof (val->val[0]));
12847c478bd9Sstevel@tonic-gate 		if (val->val == 0) {
12857c478bd9Sstevel@tonic-gate 			sfree(b.buf);
12867c478bd9Sstevel@tonic-gate 			free(val);
12877c478bd9Sstevel@tonic-gate 			return (0);
12887c478bd9Sstevel@tonic-gate 		}
12897c478bd9Sstevel@tonic-gate 		val->val[0].value = b.buf;
12907c478bd9Sstevel@tonic-gate 		val->val[0].length = b.len;
12917c478bd9Sstevel@tonic-gate 		break;
12927c478bd9Sstevel@tonic-gate 	case mit_ldap:
12937c478bd9Sstevel@tonic-gate 		if (native != mit_ldap)
12947c478bd9Sstevel@tonic-gate 			bp2buf(myself, &b, "ldap:");
12957c478bd9Sstevel@tonic-gate 		bp2buf(myself, &b, "%s", NIL(i->name));
12967c478bd9Sstevel@tonic-gate 		buf = getSearchTriple(&i->searchSpec.triple, &len);
12977c478bd9Sstevel@tonic-gate 		if (buf != 0 && len > 0) {
12987c478bd9Sstevel@tonic-gate 			bc2buf(myself, ":", 1, &b);
12997c478bd9Sstevel@tonic-gate 			sbc2buf(myself, buf, len, &b);
13007c478bd9Sstevel@tonic-gate 		}
13017c478bd9Sstevel@tonic-gate 		sfree(buf);
13027c478bd9Sstevel@tonic-gate 		val->type = vt_string;
13037c478bd9Sstevel@tonic-gate 		val->repeat = i->repeat;
13047c478bd9Sstevel@tonic-gate 		val->numVals = 1;
13057c478bd9Sstevel@tonic-gate 		val->val = am(myself, sizeof (val->val[0]));
13067c478bd9Sstevel@tonic-gate 		if (val->val == 0) {
13077c478bd9Sstevel@tonic-gate 			sfree(b.buf);
13087c478bd9Sstevel@tonic-gate 			free(val);
13097c478bd9Sstevel@tonic-gate 			return (0);
13107c478bd9Sstevel@tonic-gate 		}
13117c478bd9Sstevel@tonic-gate 		val->val[0].value = b.buf;
13127c478bd9Sstevel@tonic-gate 		val->val[0].length = b.len;
13137c478bd9Sstevel@tonic-gate 		break;
13147c478bd9Sstevel@tonic-gate 	default:
13157c478bd9Sstevel@tonic-gate 		p2buf(myself, "<unknown>:");
13167c478bd9Sstevel@tonic-gate 		p2buf(myself, "%s", NIL(i->name));
13177c478bd9Sstevel@tonic-gate 		break;
13187c478bd9Sstevel@tonic-gate 	}
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 	return (val);
13217c478bd9Sstevel@tonic-gate }
13227c478bd9Sstevel@tonic-gate 
13237c478bd9Sstevel@tonic-gate void
13247c478bd9Sstevel@tonic-gate copyObjSpec(__nis_obj_spec_t *old, __nis_obj_spec_t *new, int *err) {
13257c478bd9Sstevel@tonic-gate 	char	*myself = "copyObjSpec";
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate 	if (old == 0 || new == 0) {
13287c478bd9Sstevel@tonic-gate 		*err = EINVAL;
13297c478bd9Sstevel@tonic-gate 		return;
13307c478bd9Sstevel@tonic-gate 	}
13317c478bd9Sstevel@tonic-gate 
13327c478bd9Sstevel@tonic-gate 	if (new->index.name == 0) {
13337c478bd9Sstevel@tonic-gate 		new->index.name = am(myself, old->index.numIndexes *
13347c478bd9Sstevel@tonic-gate 						sizeof (new->index.name[0]));
13357c478bd9Sstevel@tonic-gate 		if (old->index.numIndexes > 0 && new->index.name == 0) {
13367c478bd9Sstevel@tonic-gate 			*err = ENOMEM;
13377c478bd9Sstevel@tonic-gate 			return;
13387c478bd9Sstevel@tonic-gate 		}
13397c478bd9Sstevel@tonic-gate 		new->index.value = am(myself, old->index.numIndexes *
13407c478bd9Sstevel@tonic-gate 						sizeof (new->index.value[0]));
13417c478bd9Sstevel@tonic-gate 		if (old->index.numIndexes > 0 && new->index.value == 0) {
13427c478bd9Sstevel@tonic-gate 			*err = ENOMEM;
13437c478bd9Sstevel@tonic-gate 			return;
13447c478bd9Sstevel@tonic-gate 		}
13457c478bd9Sstevel@tonic-gate 	}
13467c478bd9Sstevel@tonic-gate 	new->name = sdup(myself, T, old->name);
13477c478bd9Sstevel@tonic-gate 	if (new->name == 0 && old->name != 0) {
13487c478bd9Sstevel@tonic-gate 		*err = ENOMEM;
13497c478bd9Sstevel@tonic-gate 		return;
13507c478bd9Sstevel@tonic-gate 	}
13517c478bd9Sstevel@tonic-gate 	copyIndex(&old->index, &new->index, err);
13527c478bd9Sstevel@tonic-gate }
13537c478bd9Sstevel@tonic-gate 
13547c478bd9Sstevel@tonic-gate __nis_obj_spec_t *
13557c478bd9Sstevel@tonic-gate cloneObjSpec(__nis_obj_spec_t *old) {
13567c478bd9Sstevel@tonic-gate 	char			*myself = "cloneObjSpec";
13577c478bd9Sstevel@tonic-gate 	int			err = 0;
13587c478bd9Sstevel@tonic-gate 	__nis_obj_spec_t	*new = am(myself, sizeof (*new));
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate 	if (new != 0) {
13617c478bd9Sstevel@tonic-gate 		copyObjSpec(old, new, &err);
13627c478bd9Sstevel@tonic-gate 		if (err != 0) {
13637c478bd9Sstevel@tonic-gate 			freeObjSpec(new, 1);
13647c478bd9Sstevel@tonic-gate 			new = 0;
13657c478bd9Sstevel@tonic-gate 		}
13667c478bd9Sstevel@tonic-gate 	}
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate 	return (new);
13697c478bd9Sstevel@tonic-gate }
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate void
13727c478bd9Sstevel@tonic-gate freeObjSpec(__nis_obj_spec_t *old, bool_t doFree) {
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate 	if (old == 0)
13757c478bd9Sstevel@tonic-gate 		return;
13767c478bd9Sstevel@tonic-gate 
13777c478bd9Sstevel@tonic-gate 	sfree(old->name);
13787c478bd9Sstevel@tonic-gate 	freeIndex(&old->index, FALSE);
13797c478bd9Sstevel@tonic-gate 	if (doFree)
13807c478bd9Sstevel@tonic-gate 		free(old);
13817c478bd9Sstevel@tonic-gate }
13827c478bd9Sstevel@tonic-gate 
13837c478bd9Sstevel@tonic-gate void
13847c478bd9Sstevel@tonic-gate copySearchTriple(__nis_search_triple_t *old, __nis_search_triple_t *new,
13857c478bd9Sstevel@tonic-gate 		int *err) {
13867c478bd9Sstevel@tonic-gate 	char			*myself = "copySearchTriple";
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 	*err = 0;
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate 	if (old == 0 || new == 0) {
13917c478bd9Sstevel@tonic-gate 		*err = EINVAL;
13927c478bd9Sstevel@tonic-gate 		return;
13937c478bd9Sstevel@tonic-gate 	}
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate 	if (old->base != NULL)
13967c478bd9Sstevel@tonic-gate 		new->base = sdup(myself, T, old->base);
13977c478bd9Sstevel@tonic-gate 	else
13987c478bd9Sstevel@tonic-gate 		new->base = NULL;
13997c478bd9Sstevel@tonic-gate 	if (old->attrs != NULL)
14007c478bd9Sstevel@tonic-gate 		new->attrs = sdup(myself, T, old->attrs);
14017c478bd9Sstevel@tonic-gate 	else
14027c478bd9Sstevel@tonic-gate 		new->attrs = NULL;
14037c478bd9Sstevel@tonic-gate 	if ((new->base == 0 && old->base != 0) ||
14047c478bd9Sstevel@tonic-gate 			(new->attrs == 0 && old->attrs != 0)) {
14057c478bd9Sstevel@tonic-gate 		sfree(new->base);
14067c478bd9Sstevel@tonic-gate 		new->base = 0;
14077c478bd9Sstevel@tonic-gate 		sfree(new->attrs);
14087c478bd9Sstevel@tonic-gate 		new->attrs = 0;
14097c478bd9Sstevel@tonic-gate 		*err = ENOMEM;
14107c478bd9Sstevel@tonic-gate 		return;
14117c478bd9Sstevel@tonic-gate 	}
14127c478bd9Sstevel@tonic-gate 	new->scope = old->scope;
14137c478bd9Sstevel@tonic-gate 	/*
14147c478bd9Sstevel@tonic-gate 	 * XXX Really should have a cloneMappingElement() function.
14157c478bd9Sstevel@tonic-gate 	 * However, since whatever the 'element' field points to
14167c478bd9Sstevel@tonic-gate 	 * is allocated at parse time, and never is freed or modified,
14177c478bd9Sstevel@tonic-gate 	 * it's sufficient to copy the pointer value.
14187c478bd9Sstevel@tonic-gate 	 */
14197c478bd9Sstevel@tonic-gate 	new->element = old->element;
14207c478bd9Sstevel@tonic-gate }
14217c478bd9Sstevel@tonic-gate 
14227c478bd9Sstevel@tonic-gate __nis_search_triple_t *
14237c478bd9Sstevel@tonic-gate cloneSearchTriple(__nis_search_triple_t *old) {
14247c478bd9Sstevel@tonic-gate 	char			*myself = "cloneSearchTriple";
14257c478bd9Sstevel@tonic-gate 	int			err = 0;
14267c478bd9Sstevel@tonic-gate 	__nis_search_triple_t	*new = am(myself, sizeof (*new));
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate 	if (new != 0) {
14297c478bd9Sstevel@tonic-gate 		copySearchTriple(old, new, &err);
14307c478bd9Sstevel@tonic-gate 		if (err != 0) {
14317c478bd9Sstevel@tonic-gate 			freeSearchTriple(new, 1);
14327c478bd9Sstevel@tonic-gate 			new = 0;
14337c478bd9Sstevel@tonic-gate 		}
14347c478bd9Sstevel@tonic-gate 	}
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate 	return (new);
14377c478bd9Sstevel@tonic-gate }
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate void
14407c478bd9Sstevel@tonic-gate freeSearchTriple(__nis_search_triple_t *old, bool_t doFree) {
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate 	if (old == 0)
14437c478bd9Sstevel@tonic-gate 		return;
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate 	sfree(old->base);
14467c478bd9Sstevel@tonic-gate 	sfree(old->attrs);
14477c478bd9Sstevel@tonic-gate 	/*
14487c478bd9Sstevel@tonic-gate 	 * Since we only copied the element pointer when this structure
14497c478bd9Sstevel@tonic-gate 	 * was created, we don't free old->element.
14507c478bd9Sstevel@tonic-gate 	 */
14517c478bd9Sstevel@tonic-gate 	if (doFree)
14527c478bd9Sstevel@tonic-gate 		free(old);
14537c478bd9Sstevel@tonic-gate }
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate void
14567c478bd9Sstevel@tonic-gate copyTripleOrObj(__nis_mapping_item_type_t type,
14577c478bd9Sstevel@tonic-gate 		__nis_triple_or_obj_t *old, __nis_triple_or_obj_t *new,
14587c478bd9Sstevel@tonic-gate 		int *err) {
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate 	*err = 0;
14617c478bd9Sstevel@tonic-gate 
14627c478bd9Sstevel@tonic-gate 	if (old == 0 || new == 0) {
14637c478bd9Sstevel@tonic-gate 		*err = EINVAL;
14647c478bd9Sstevel@tonic-gate 		return;
14657c478bd9Sstevel@tonic-gate 	}
14667c478bd9Sstevel@tonic-gate 
14677c478bd9Sstevel@tonic-gate 	if (type == mit_nisplus) {
14687c478bd9Sstevel@tonic-gate 		copyObjSpec(&old->obj, &new->obj, err);
14697c478bd9Sstevel@tonic-gate 	} else if (type == mit_ldap) {
14707c478bd9Sstevel@tonic-gate 		copySearchTriple(&old->triple, &new->triple, err);
14717c478bd9Sstevel@tonic-gate 	}
14727c478bd9Sstevel@tonic-gate }
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate __nis_triple_or_obj_t *
14757c478bd9Sstevel@tonic-gate cloneTripleOrObj(__nis_mapping_item_type_t type, __nis_triple_or_obj_t *old) {
14767c478bd9Sstevel@tonic-gate 	char			*myself = "cloneTripleOrObj";
14777c478bd9Sstevel@tonic-gate 	int			err = 0;
14787c478bd9Sstevel@tonic-gate 	__nis_triple_or_obj_t	*new = am(myself, sizeof (*new));
14797c478bd9Sstevel@tonic-gate 
14807c478bd9Sstevel@tonic-gate 	if (new != 0) {
14817c478bd9Sstevel@tonic-gate 		copyTripleOrObj(type, old, new, &err);
14827c478bd9Sstevel@tonic-gate 		if (err != 0) {
14837c478bd9Sstevel@tonic-gate 			freeTripleOrObj(type, new, 1);
14847c478bd9Sstevel@tonic-gate 			new = 0;
14857c478bd9Sstevel@tonic-gate 		}
14867c478bd9Sstevel@tonic-gate 	}
14877c478bd9Sstevel@tonic-gate 
14887c478bd9Sstevel@tonic-gate 	return (new);
14897c478bd9Sstevel@tonic-gate }
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate void
14927c478bd9Sstevel@tonic-gate freeTripleOrObj(__nis_mapping_item_type_t type, __nis_triple_or_obj_t *old,
14937c478bd9Sstevel@tonic-gate 		bool_t doFree) {
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate 	if (old == 0)
14967c478bd9Sstevel@tonic-gate 		return;
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate 	if (type == mit_nisplus)
14997c478bd9Sstevel@tonic-gate 		freeObjSpec(&old->obj, doFree);
15007c478bd9Sstevel@tonic-gate 	else if (type == mit_ldap)
15017c478bd9Sstevel@tonic-gate 		freeSearchTriple(&old->triple, doFree);
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate 	if (doFree)
15047c478bd9Sstevel@tonic-gate 		free(old);
15057c478bd9Sstevel@tonic-gate }
15067c478bd9Sstevel@tonic-gate 
15077c478bd9Sstevel@tonic-gate void
15087c478bd9Sstevel@tonic-gate copyItem(__nis_mapping_item_t *old, __nis_mapping_item_t *new, int *err) {
15097c478bd9Sstevel@tonic-gate 
15107c478bd9Sstevel@tonic-gate 	*err = 0;
15117c478bd9Sstevel@tonic-gate 
15127c478bd9Sstevel@tonic-gate 	if (old == 0 || new == 0) {
15137c478bd9Sstevel@tonic-gate 		*err = EINVAL;
15147c478bd9Sstevel@tonic-gate 		return;
15157c478bd9Sstevel@tonic-gate 	}
15167c478bd9Sstevel@tonic-gate 
15177c478bd9Sstevel@tonic-gate 	new->type = old->type;
15187c478bd9Sstevel@tonic-gate 	new->repeat = old->repeat;
15197c478bd9Sstevel@tonic-gate 	if (old->name != 0) {
15207c478bd9Sstevel@tonic-gate 		new->name = strdup(old->name);
15217c478bd9Sstevel@tonic-gate 		if (new->name == 0) {
15227c478bd9Sstevel@tonic-gate 			*err = ENOMEM;
15237c478bd9Sstevel@tonic-gate 			return;
15247c478bd9Sstevel@tonic-gate 		}
15257c478bd9Sstevel@tonic-gate 	} else {
15267c478bd9Sstevel@tonic-gate 		new->name = 0;
15277c478bd9Sstevel@tonic-gate 	}
15287c478bd9Sstevel@tonic-gate 	if (old->type == mit_nisplus || old->type == mit_ldap)
15297c478bd9Sstevel@tonic-gate 		copyTripleOrObj(old->type, &old->searchSpec, &new->searchSpec,
15307c478bd9Sstevel@tonic-gate 				err);
15317c478bd9Sstevel@tonic-gate 	else
15327c478bd9Sstevel@tonic-gate 		memset(&new->searchSpec, 0, sizeof (new->searchSpec));
15337c478bd9Sstevel@tonic-gate }
15347c478bd9Sstevel@tonic-gate 
15357c478bd9Sstevel@tonic-gate __nis_mapping_item_t *
15367c478bd9Sstevel@tonic-gate cloneItem(__nis_mapping_item_t *old) {
15377c478bd9Sstevel@tonic-gate 	__nis_mapping_item_t	*new;
15387c478bd9Sstevel@tonic-gate 	int			err = 0;
15397c478bd9Sstevel@tonic-gate 	char			*myself = "cloneItem";
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate 	if (old == 0)
15427c478bd9Sstevel@tonic-gate 		return (0);
15437c478bd9Sstevel@tonic-gate 
15447c478bd9Sstevel@tonic-gate 	new = am(myself, sizeof (*new));
15457c478bd9Sstevel@tonic-gate 	if (new == 0)
15467c478bd9Sstevel@tonic-gate 		return (0);
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate 	copyItem(old, new, &err);
15497c478bd9Sstevel@tonic-gate 	if (err != 0) {
15507c478bd9Sstevel@tonic-gate 		freeMappingItem(new, 1);
15517c478bd9Sstevel@tonic-gate 		return (0);
15527c478bd9Sstevel@tonic-gate 	}
15537c478bd9Sstevel@tonic-gate 
15547c478bd9Sstevel@tonic-gate 	return (new);
15557c478bd9Sstevel@tonic-gate }
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate void
15587c478bd9Sstevel@tonic-gate freeMappingItem(__nis_mapping_item_t *item, int numItems) {
15597c478bd9Sstevel@tonic-gate 	int	i;
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate 	if (item == 0)
15627c478bd9Sstevel@tonic-gate 		return;
15637c478bd9Sstevel@tonic-gate 
15647c478bd9Sstevel@tonic-gate 	for (i = 0; i < numItems; i++) {
15657c478bd9Sstevel@tonic-gate 		sfree(item[i].name);
15667c478bd9Sstevel@tonic-gate 		freeTripleOrObj(item[i].type, &item[i].searchSpec, FALSE);
15677c478bd9Sstevel@tonic-gate 	}
15687c478bd9Sstevel@tonic-gate 	sfree(item);
15697c478bd9Sstevel@tonic-gate }
15707c478bd9Sstevel@tonic-gate 
15717c478bd9Sstevel@tonic-gate __nis_mapping_item_t *
15727c478bd9Sstevel@tonic-gate concatenateMappingItem(__nis_mapping_item_t *old, int numItems,
15737c478bd9Sstevel@tonic-gate 		__nis_mapping_item_t *cat) {
15747c478bd9Sstevel@tonic-gate 	__nis_mapping_item_t	*new;
15757c478bd9Sstevel@tonic-gate 	int			i, err = 0;
15767c478bd9Sstevel@tonic-gate 	char			*myself = "concatenateMappingItem";
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate 	if (old == 0 || numItems < 1)
15797c478bd9Sstevel@tonic-gate 		return (cloneItem(cat));
15807c478bd9Sstevel@tonic-gate 
15817c478bd9Sstevel@tonic-gate 	new = am(myself, (numItems + 1) * sizeof (*new));
15827c478bd9Sstevel@tonic-gate 	if (new == 0)
15837c478bd9Sstevel@tonic-gate 		return (0);
15847c478bd9Sstevel@tonic-gate 
15857c478bd9Sstevel@tonic-gate 	for (i = 0; i < numItems; i++) {
15867c478bd9Sstevel@tonic-gate 		copyItem(&old[i], &new[i], &err);
15877c478bd9Sstevel@tonic-gate 		if (err != 0) {
15887c478bd9Sstevel@tonic-gate 			freeMappingItem(new, i);
15897c478bd9Sstevel@tonic-gate 			return (0);
15907c478bd9Sstevel@tonic-gate 		}
15917c478bd9Sstevel@tonic-gate 	}
15927c478bd9Sstevel@tonic-gate 	copyItem(cat, &new[numItems], &err);
15937c478bd9Sstevel@tonic-gate 	if (err != 0) {
15947c478bd9Sstevel@tonic-gate 		freeMappingItem(new, numItems);
15957c478bd9Sstevel@tonic-gate 		new = 0;
15967c478bd9Sstevel@tonic-gate 	}
15977c478bd9Sstevel@tonic-gate 
15987c478bd9Sstevel@tonic-gate 	return (new);
15997c478bd9Sstevel@tonic-gate }
16007c478bd9Sstevel@tonic-gate 
16017c478bd9Sstevel@tonic-gate __nis_value_t *
16027c478bd9Sstevel@tonic-gate concatenateValues(__nis_value_t *v1, __nis_value_t *v2) {
16037c478bd9Sstevel@tonic-gate 	int		i, n, a;
16047c478bd9Sstevel@tonic-gate 	__nis_value_t	*v;
16057c478bd9Sstevel@tonic-gate 	char		*myself = "concatenateValues";
16067c478bd9Sstevel@tonic-gate 
16077c478bd9Sstevel@tonic-gate 	if (v1 == 0 || v1->numVals <= 0)
16087c478bd9Sstevel@tonic-gate 		return (cloneValue(v2, 1));
16097c478bd9Sstevel@tonic-gate 	if (v2 == 0 || v2->numVals <= 0)
16107c478bd9Sstevel@tonic-gate 		return (cloneValue(v1, 1));
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate 	if (v1->type != v2->type)
16137c478bd9Sstevel@tonic-gate 		return (0);
16147c478bd9Sstevel@tonic-gate 
16157c478bd9Sstevel@tonic-gate 	n = v1->numVals + v2->numVals;
16167c478bd9Sstevel@tonic-gate 	v = am(myself, sizeof (*v));
16177c478bd9Sstevel@tonic-gate 	if (v == 0)
16187c478bd9Sstevel@tonic-gate 		return (0);
16197c478bd9Sstevel@tonic-gate 	v->val = am(myself, n * sizeof (v->val[0]));
16207c478bd9Sstevel@tonic-gate 	if (v->val == 0) {
16217c478bd9Sstevel@tonic-gate 		free(v);
16227c478bd9Sstevel@tonic-gate 		return (0);
16237c478bd9Sstevel@tonic-gate 	}
16247c478bd9Sstevel@tonic-gate 	v->type = v1->type;
16257c478bd9Sstevel@tonic-gate 	v->numVals = 0;
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate 	for (a = 0; a < 2; a++) {
16287c478bd9Sstevel@tonic-gate 		__nis_single_value_t	*val = (a == 0) ? v1->val : v2->val;
16297c478bd9Sstevel@tonic-gate 		int			numv = (a == 0) ? v1->numVals :
16307c478bd9Sstevel@tonic-gate 							v2->numVals;
16317c478bd9Sstevel@tonic-gate 		for (i = 0; i < numv; i++) {
16327c478bd9Sstevel@tonic-gate 			int	clen, alen = val[i].length;
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate 			clen = alen;
16357c478bd9Sstevel@tonic-gate 
16367c478bd9Sstevel@tonic-gate 			/*
16377c478bd9Sstevel@tonic-gate 			 * Make sure there's a NUL at the end of a string,
16387c478bd9Sstevel@tonic-gate 			 * but avoid adding to the allocated length if there's
16397c478bd9Sstevel@tonic-gate 			 * already a NUL at the end.
16407c478bd9Sstevel@tonic-gate 			 */
16417c478bd9Sstevel@tonic-gate 			if (alen > 0 && v->type == vt_string &&
16427c478bd9Sstevel@tonic-gate 					((char *)val[i].value)[alen-1] != '\0')
16437c478bd9Sstevel@tonic-gate 				alen += 1;
16447c478bd9Sstevel@tonic-gate 			v->val[v->numVals].value = am(myself, alen);
16457c478bd9Sstevel@tonic-gate 			if (v->val[v->numVals].value == 0) {
16467c478bd9Sstevel@tonic-gate 				freeValue(v, 1);
16477c478bd9Sstevel@tonic-gate 				return (0);
16487c478bd9Sstevel@tonic-gate 			}
16497c478bd9Sstevel@tonic-gate 			memcpy(v->val[v->numVals].value, val[i].value, clen);
16507c478bd9Sstevel@tonic-gate 			v->val[v->numVals].length = val[i].length;
16517c478bd9Sstevel@tonic-gate 			v->numVals++;
16527c478bd9Sstevel@tonic-gate 		}
16537c478bd9Sstevel@tonic-gate 	}
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate 	return (v);
16567c478bd9Sstevel@tonic-gate }
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate __nis_value_t *
16597c478bd9Sstevel@tonic-gate splitMappingItem(__nis_mapping_item_t *item, char delim,
16607c478bd9Sstevel@tonic-gate 		__nis_rule_value_t *rv) {
16617c478bd9Sstevel@tonic-gate 	__nis_value_t		*val = getMappingItem(item, mit_any,
16627c478bd9Sstevel@tonic-gate 			rv, 0, NULL);
16637c478bd9Sstevel@tonic-gate 	__nis_single_value_t	*nval;
16647c478bd9Sstevel@tonic-gate 	int			i, n, nv;
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 	if (val == 0)
16677c478bd9Sstevel@tonic-gate 		return (0);
16687c478bd9Sstevel@tonic-gate 	else if (delim == 0 || val->val == 0 || val->numVals <= 0 ||
16697c478bd9Sstevel@tonic-gate 			val->type != vt_string) {
16707c478bd9Sstevel@tonic-gate 		freeValue(val, 1);
16717c478bd9Sstevel@tonic-gate 		return (0);
16727c478bd9Sstevel@tonic-gate 	}
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 	nval = val->val;
16757c478bd9Sstevel@tonic-gate 	nv = val->numVals;
16767c478bd9Sstevel@tonic-gate 	val->repeat = FALSE;
16777c478bd9Sstevel@tonic-gate 	val->val = 0;
16787c478bd9Sstevel@tonic-gate 	val->numVals = 0;
16797c478bd9Sstevel@tonic-gate 
16807c478bd9Sstevel@tonic-gate 	/* In N2L, space and tab delimiters are treated the same */
16817c478bd9Sstevel@tonic-gate 	if (yp2ldap && delim == '\t')
16827c478bd9Sstevel@tonic-gate 		delim = ' ';
16837c478bd9Sstevel@tonic-gate 
16847c478bd9Sstevel@tonic-gate 	/* If the item has multiple values, we split each one independently */
16857c478bd9Sstevel@tonic-gate 	for (i = 0; i < nv; i++) {
16867c478bd9Sstevel@tonic-gate 		char			*str;
16877c478bd9Sstevel@tonic-gate 		int			s, e;
16887c478bd9Sstevel@tonic-gate 		char			*newstr;
16897c478bd9Sstevel@tonic-gate 		__nis_single_value_t	*newval;
16907c478bd9Sstevel@tonic-gate 
16917c478bd9Sstevel@tonic-gate 		if (yp2ldap && delim == ' ')
16927c478bd9Sstevel@tonic-gate 			nval[i].value = trimWhiteSpaces(nval[i].value,
16937c478bd9Sstevel@tonic-gate 							&nval[i].length, 1);
16947c478bd9Sstevel@tonic-gate 
16957c478bd9Sstevel@tonic-gate 		str = nval[i].value;
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate 		if (nval[i].value == 0)
16987c478bd9Sstevel@tonic-gate 			continue;
16997c478bd9Sstevel@tonic-gate 
17007c478bd9Sstevel@tonic-gate 		for (s = 0; s < nval[i].length; s = e+1) {
17017c478bd9Sstevel@tonic-gate 			/* Find the next delimiter, or end-of-string */
17027c478bd9Sstevel@tonic-gate 			for (e = s; str[e] != '\0' && str[e] != delim; e++);
17037c478bd9Sstevel@tonic-gate 			/*
17047c478bd9Sstevel@tonic-gate 			 * 'str[e]' is either a delimiter, or the concluding
17057c478bd9Sstevel@tonic-gate 			 * NUL. Make sure it's NUL.
17067c478bd9Sstevel@tonic-gate 			 */
17077c478bd9Sstevel@tonic-gate 			str[e] = '\0';
17087c478bd9Sstevel@tonic-gate 			/* Add to val->val */
17097c478bd9Sstevel@tonic-gate 			newstr = strdup(&str[s]);
17107c478bd9Sstevel@tonic-gate 			newval = realloc(val->val,
17117c478bd9Sstevel@tonic-gate 					(val->numVals+1) *
17127c478bd9Sstevel@tonic-gate 						sizeof (val->val[0]));
17137c478bd9Sstevel@tonic-gate 			if (newval != 0)
17147c478bd9Sstevel@tonic-gate 				val->val = newval;
17157c478bd9Sstevel@tonic-gate 			if (newstr == 0 || newval == 0) {
17167c478bd9Sstevel@tonic-gate 				freeValue(val, 1);
17177c478bd9Sstevel@tonic-gate 				for (n = i; n < nv; n++) {
17187c478bd9Sstevel@tonic-gate 					sfree(nval[n].value);
17197c478bd9Sstevel@tonic-gate 				}
17207c478bd9Sstevel@tonic-gate 				free(nval);
17217c478bd9Sstevel@tonic-gate 				sfree(newstr);
17227c478bd9Sstevel@tonic-gate 				return (0);
17237c478bd9Sstevel@tonic-gate 			}
17247c478bd9Sstevel@tonic-gate 			val->val[val->numVals].value = newstr;
17257c478bd9Sstevel@tonic-gate 			val->val[val->numVals].length = strlen(newstr) + 1;
17267c478bd9Sstevel@tonic-gate 			val->numVals++;
17277c478bd9Sstevel@tonic-gate 		}
17287c478bd9Sstevel@tonic-gate 		free(nval[i].value);
17297c478bd9Sstevel@tonic-gate 		nval[i].value = 0;
17307c478bd9Sstevel@tonic-gate 	}
17317c478bd9Sstevel@tonic-gate 	/* Already freed the nval[i].value's as we traversed nval */
17327c478bd9Sstevel@tonic-gate 	free(nval);
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate 	return (val);
17357c478bd9Sstevel@tonic-gate }
17367c478bd9Sstevel@tonic-gate 
17377c478bd9Sstevel@tonic-gate /*
17387c478bd9Sstevel@tonic-gate  * Match the format spec 'f[curf]' to the input value string 'str'.
17397c478bd9Sstevel@tonic-gate  *
17407c478bd9Sstevel@tonic-gate  * If successful, returns the updated position in the value string 'str'.
17417c478bd9Sstevel@tonic-gate  * Otherwise, NULL is returned.
17427c478bd9Sstevel@tonic-gate  *
17437c478bd9Sstevel@tonic-gate  * curf		Current index (i.e., the one we should look at) in 'f'
17447c478bd9Sstevel@tonic-gate  * nf		Number of elements in 'f', including 'mmt_end'
17457c478bd9Sstevel@tonic-gate  * str		The value string we're scanning
17467c478bd9Sstevel@tonic-gate  * val		Pointer to where an item value (if any) should be returned
17477c478bd9Sstevel@tonic-gate  *		Set to NULL if not an 'mmt_item'.
17487c478bd9Sstevel@tonic-gate  * fmtstart	If non-zero on entry, skip characters in 'str' until we find
17497c478bd9Sstevel@tonic-gate  *		the f[curf].type data, if doing so makes any sense. On exit,
17507c478bd9Sstevel@tonic-gate  *		set to the start of the fmt element data (which will be 'str',
17517c478bd9Sstevel@tonic-gate  *		unless we did skip characters)
17527c478bd9Sstevel@tonic-gate  * sepset	List of separators
17537c478bd9Sstevel@tonic-gate  */
17547c478bd9Sstevel@tonic-gate char *
17557c478bd9Sstevel@tonic-gate scanMappingFormat(__nis_mapping_format_t *f, int curf, int nf, char *str,
17567c478bd9Sstevel@tonic-gate 		char **val, char **fmtstart, char *sepset) {
17577c478bd9Sstevel@tonic-gate 	char	*mstr, *next, *start = 0, *tmpstr;
17587c478bd9Sstevel@tonic-gate 	int	i, len;
17597c478bd9Sstevel@tonic-gate 	bool_t	match;
17607c478bd9Sstevel@tonic-gate 	char	*myself = "scanMappingFormat";
17617c478bd9Sstevel@tonic-gate 	/* N2L variables */
17627c478bd9Sstevel@tonic-gate 	int	af, skipspaces = 0;
17637c478bd9Sstevel@tonic-gate 	bool_t	ipaddr = FALSE;
17647c478bd9Sstevel@tonic-gate 	char	*spacestr = " ", *emptystr = "";
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate 
17677c478bd9Sstevel@tonic-gate 	if (f == 0 || curf < 0 || nf <= 0 || str == 0)
17687c478bd9Sstevel@tonic-gate 		return (0);
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate 	/*
17717c478bd9Sstevel@tonic-gate 	 * If separator list is NULL (which will be the case for
17727c478bd9Sstevel@tonic-gate 	 * nis+2ldap), then simply use empty string
17737c478bd9Sstevel@tonic-gate 	 */
17747c478bd9Sstevel@tonic-gate 	if (sepset == 0)
17757c478bd9Sstevel@tonic-gate 		sepset = emptystr;
17767c478bd9Sstevel@tonic-gate 
17777c478bd9Sstevel@tonic-gate 	if (curf >= nf) {
17787c478bd9Sstevel@tonic-gate 		/* OK if the string also is exhausted */
17797c478bd9Sstevel@tonic-gate 		if (strchr(sepset, *str) != 0)
17807c478bd9Sstevel@tonic-gate 			return (str);
17817c478bd9Sstevel@tonic-gate 		else
17827c478bd9Sstevel@tonic-gate 			return (0);
17837c478bd9Sstevel@tonic-gate 	}
17847c478bd9Sstevel@tonic-gate 
17857c478bd9Sstevel@tonic-gate 	switch (f[curf].type) {
17867c478bd9Sstevel@tonic-gate 	case mmt_berstring:
17877c478bd9Sstevel@tonic-gate 		if (f[curf].match.berString[0] != 'a') {
17887c478bd9Sstevel@tonic-gate 			/* Not a matchable element */
17897c478bd9Sstevel@tonic-gate 			return (0);
17907c478bd9Sstevel@tonic-gate 		}
17917c478bd9Sstevel@tonic-gate 
17927c478bd9Sstevel@tonic-gate 		/*
17937c478bd9Sstevel@tonic-gate 		 * If here, it means it's an IP address (N2L case)
17947c478bd9Sstevel@tonic-gate 		 * So continue processing as if it was mmt_item
17957c478bd9Sstevel@tonic-gate 		 */
17967c478bd9Sstevel@tonic-gate 		ipaddr = TRUE;
17977c478bd9Sstevel@tonic-gate 
17987c478bd9Sstevel@tonic-gate 	case mmt_item:
17997c478bd9Sstevel@tonic-gate 		/*
18007c478bd9Sstevel@tonic-gate 		 * In order to find the end of the item value, we must look
18017c478bd9Sstevel@tonic-gate 		 * ahead and determine the start of the next formatting element.
18027c478bd9Sstevel@tonic-gate 		 * If successful, 'next' will be the start of the fmt element
18037c478bd9Sstevel@tonic-gate 		 * after the next one; we don't care about that, other than to
18047c478bd9Sstevel@tonic-gate 		 * check for error.
18057c478bd9Sstevel@tonic-gate 		 *
18067c478bd9Sstevel@tonic-gate 		 * Since an item match is somewhat like an any match, in that
18077c478bd9Sstevel@tonic-gate 		 * we don't know a priori if the first occurence of the next
18087c478bd9Sstevel@tonic-gate 		 * element really is the one we want, we have to scan ahead
18097c478bd9Sstevel@tonic-gate 		 * until we've reached the end.
18107c478bd9Sstevel@tonic-gate 		 */
18117c478bd9Sstevel@tonic-gate 		tmpstr = str;
18127c478bd9Sstevel@tonic-gate 		while ((next = scanMappingFormat(f, curf+1, nf, tmpstr, 0,
18137c478bd9Sstevel@tonic-gate 				&start, sepset)) != 0) {
18147c478bd9Sstevel@tonic-gate 			char	*tmp = next;
18157c478bd9Sstevel@tonic-gate 			int	cf;
18167c478bd9Sstevel@tonic-gate 
18177c478bd9Sstevel@tonic-gate 			for (cf = curf+2; cf < nf; cf++) {
18187c478bd9Sstevel@tonic-gate 				tmp = scanMappingFormat(f, cf, nf, tmp, 0,
18197c478bd9Sstevel@tonic-gate 					0, sepset);
18207c478bd9Sstevel@tonic-gate 				if (tmp == 0)
18217c478bd9Sstevel@tonic-gate 					break;
18227c478bd9Sstevel@tonic-gate 			}
18237c478bd9Sstevel@tonic-gate 			if (tmp == 0) {
18247c478bd9Sstevel@tonic-gate 				tmpstr = next;
18257c478bd9Sstevel@tonic-gate 			} else if (strchr(sepset, *tmp) != 0) {
18267c478bd9Sstevel@tonic-gate 				break;
18277c478bd9Sstevel@tonic-gate 			} else {
18287c478bd9Sstevel@tonic-gate 				return (0);
18297c478bd9Sstevel@tonic-gate 			}
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate 		}
18327c478bd9Sstevel@tonic-gate 		if (next == 0 || start == 0)
18337c478bd9Sstevel@tonic-gate 			return (0);
18347c478bd9Sstevel@tonic-gate 
18357c478bd9Sstevel@tonic-gate 		if (val != 0) {
18367c478bd9Sstevel@tonic-gate 			len = (int)((long)start - (long)str);
18377c478bd9Sstevel@tonic-gate 			*val = am(myself, len + 1);
18387c478bd9Sstevel@tonic-gate 			if (*val == 0)
18397c478bd9Sstevel@tonic-gate 				return (0);
18407c478bd9Sstevel@tonic-gate 			memcpy(*val, str, len);
18417c478bd9Sstevel@tonic-gate 			(*val)[len] = '\0';
18427c478bd9Sstevel@tonic-gate 
18437c478bd9Sstevel@tonic-gate 			if (ipaddr == TRUE) {
18447c478bd9Sstevel@tonic-gate 				/*
18457c478bd9Sstevel@tonic-gate 				 * In N2L, we need to check if *val is truly an
18467c478bd9Sstevel@tonic-gate 				 * IP address
18477c478bd9Sstevel@tonic-gate 				 */
18487c478bd9Sstevel@tonic-gate 				af = checkIPaddress(*val, len, &tmpstr);
18497c478bd9Sstevel@tonic-gate 
18507c478bd9Sstevel@tonic-gate 				if (af == -2) {
18517c478bd9Sstevel@tonic-gate 					logmsg(MSG_NOTIMECHECK, LOG_WARNING,
18527c478bd9Sstevel@tonic-gate 						"%s:Internal error while "
18537c478bd9Sstevel@tonic-gate 						"processing IPaddress %s",
18547c478bd9Sstevel@tonic-gate 						myself, *val);
18557c478bd9Sstevel@tonic-gate 					sfree(*val);
18567c478bd9Sstevel@tonic-gate 					return (0);
18577c478bd9Sstevel@tonic-gate 				} else if (af == -1) {
18587c478bd9Sstevel@tonic-gate 					logmsg(MSG_NOTIMECHECK, LOG_WARNING,
18597c478bd9Sstevel@tonic-gate 						"%s:%s is not an IP address",
18607c478bd9Sstevel@tonic-gate 						myself, *val);
18617c478bd9Sstevel@tonic-gate 					sfree(*val);
18627c478bd9Sstevel@tonic-gate 					return (0);
18637c478bd9Sstevel@tonic-gate 				} else if (af == 0) {
18647c478bd9Sstevel@tonic-gate 					logmsg(MSG_NOTIMECHECK, LOG_WARNING,
18657c478bd9Sstevel@tonic-gate 						"%s:IP address %s is not "
18667c478bd9Sstevel@tonic-gate 						"supported by rfc2307bis",
18677c478bd9Sstevel@tonic-gate 						myself, *val);
18687c478bd9Sstevel@tonic-gate 					sfree(*val);
18697c478bd9Sstevel@tonic-gate 					return (0);
18707c478bd9Sstevel@tonic-gate 				} else if (sstrncmp(*val, tmpstr, len) != 0) {
18717c478bd9Sstevel@tonic-gate 					logmsg(MSG_NOTIMECHECK, LOG_WARNING,
18727c478bd9Sstevel@tonic-gate 						"%s:IPaddress %s converted "
18737c478bd9Sstevel@tonic-gate 						"to %s", myself, *val, tmpstr);
18747c478bd9Sstevel@tonic-gate 				}
18757c478bd9Sstevel@tonic-gate 
18767c478bd9Sstevel@tonic-gate 				sfree(*val);
18777c478bd9Sstevel@tonic-gate 				*val = tmpstr;
18787c478bd9Sstevel@tonic-gate 			}
18797c478bd9Sstevel@tonic-gate 		}
18807c478bd9Sstevel@tonic-gate 
18817c478bd9Sstevel@tonic-gate 		if (fmtstart != 0)
18827c478bd9Sstevel@tonic-gate 			*fmtstart = str;
18837c478bd9Sstevel@tonic-gate 
18847c478bd9Sstevel@tonic-gate 		return (start);
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate 	case mmt_string:
18877c478bd9Sstevel@tonic-gate 		if ((mstr = f[curf].match.string) == 0 || *mstr == '\0') {
18887c478bd9Sstevel@tonic-gate 			/*
18897c478bd9Sstevel@tonic-gate 			 * Count this as a successful match of an empty
18907c478bd9Sstevel@tonic-gate 			 * string.
18917c478bd9Sstevel@tonic-gate 			 */
18927c478bd9Sstevel@tonic-gate 			if (fmtstart != 0)
18937c478bd9Sstevel@tonic-gate 				*fmtstart = str;
18947c478bd9Sstevel@tonic-gate 			return (str);
18957c478bd9Sstevel@tonic-gate 		}
18967c478bd9Sstevel@tonic-gate 
18977c478bd9Sstevel@tonic-gate 		/*
18987c478bd9Sstevel@tonic-gate 		 * In N2L, if the format string 'mstr' contains only
18997c478bd9Sstevel@tonic-gate 		 * whitespaces (spaces and tabs), then it should
19007c478bd9Sstevel@tonic-gate 		 * match one or more whitespaces from the input
19017c478bd9Sstevel@tonic-gate 		 * string 'str'.
19027c478bd9Sstevel@tonic-gate 		 */
19037c478bd9Sstevel@tonic-gate 		if (yp2ldap && strspn(mstr, " \t") == strlen(mstr)) {
19047c478bd9Sstevel@tonic-gate 				mstr = spacestr;
19057c478bd9Sstevel@tonic-gate 				skipspaces = 1;
19067c478bd9Sstevel@tonic-gate 				next = str + strcspn(str, " \t");
19077c478bd9Sstevel@tonic-gate 				/*
19087c478bd9Sstevel@tonic-gate 				 * Even if there is no whitespace in 'str',
19097c478bd9Sstevel@tonic-gate 				 * it's OK. This is to allow formats like
19107c478bd9Sstevel@tonic-gate 				 * "%s %s %s" to match inputs like "foo bar".
19117c478bd9Sstevel@tonic-gate 				 */
19127c478bd9Sstevel@tonic-gate 				if (*next == '\0')
19137c478bd9Sstevel@tonic-gate 					mstr = emptystr;
19147c478bd9Sstevel@tonic-gate 		} else {
19157c478bd9Sstevel@tonic-gate 			/* No match string in 'str' => failure */
19167c478bd9Sstevel@tonic-gate 			if ((next = strstr(str, mstr)) == 0)
19177c478bd9Sstevel@tonic-gate 				return (0);
19187c478bd9Sstevel@tonic-gate 		}
19197c478bd9Sstevel@tonic-gate 
19207c478bd9Sstevel@tonic-gate 		/* If 'fmtstart' == 0, we require 'next' == 'str' */
19217c478bd9Sstevel@tonic-gate 		if (fmtstart == 0 && next != str)
19227c478bd9Sstevel@tonic-gate 			return (0);
19237c478bd9Sstevel@tonic-gate 		/* Success; save start of match string if requested */
19247c478bd9Sstevel@tonic-gate 		if (fmtstart != 0)
19257c478bd9Sstevel@tonic-gate 			*fmtstart = next;
19267c478bd9Sstevel@tonic-gate 		/* Update position in the value string */
19277c478bd9Sstevel@tonic-gate 		str = (char *)((long)next + (long)strlen(mstr));
19287c478bd9Sstevel@tonic-gate 
19297c478bd9Sstevel@tonic-gate 		/* Skip whitespaces for N2L */
19307c478bd9Sstevel@tonic-gate 		if (skipspaces == 1)
19317c478bd9Sstevel@tonic-gate 			for (; *str == ' ' || *str == '\t'; str++);
19327c478bd9Sstevel@tonic-gate 
19337c478bd9Sstevel@tonic-gate 		return (str);
19347c478bd9Sstevel@tonic-gate 
19357c478bd9Sstevel@tonic-gate 	case mmt_single:
19367c478bd9Sstevel@tonic-gate 		if (fmtstart != 0) {
19377c478bd9Sstevel@tonic-gate 			match = FALSE;
19387c478bd9Sstevel@tonic-gate 			/* Skip ahead until we match */
19397c478bd9Sstevel@tonic-gate 			for (next = str; *next != '\0'; next++) {
19407c478bd9Sstevel@tonic-gate 				unsigned char	*lo = f[curf].match.single.lo;
19417c478bd9Sstevel@tonic-gate 				unsigned char	*hi = f[curf].match.single.hi;
19427c478bd9Sstevel@tonic-gate 
19437c478bd9Sstevel@tonic-gate 				for (i = 0; i < f[curf].match.single.numRange;
19447c478bd9Sstevel@tonic-gate 						i++) {
19457c478bd9Sstevel@tonic-gate 					if (*next >= lo[i] && *next <= hi[i]) {
19467c478bd9Sstevel@tonic-gate 						match = TRUE;
19477c478bd9Sstevel@tonic-gate 						break;
19487c478bd9Sstevel@tonic-gate 					}
19497c478bd9Sstevel@tonic-gate 				}
19507c478bd9Sstevel@tonic-gate 				if (match)
19517c478bd9Sstevel@tonic-gate 					break;
19527c478bd9Sstevel@tonic-gate 			}
19537c478bd9Sstevel@tonic-gate 			if (!match)
19547c478bd9Sstevel@tonic-gate 				return (0);
19557c478bd9Sstevel@tonic-gate 			*fmtstart = next;
19567c478bd9Sstevel@tonic-gate 			str = next;
19577c478bd9Sstevel@tonic-gate 		} else {
19587c478bd9Sstevel@tonic-gate 			match = FALSE;
19597c478bd9Sstevel@tonic-gate 			for (i = 0; i < f[curf].match.single.numRange; i++) {
19607c478bd9Sstevel@tonic-gate 				if (*str >= f[curf].match.single.lo[i] &&
19617c478bd9Sstevel@tonic-gate 					*str <= f[curf].match.single.hi[i]) {
19627c478bd9Sstevel@tonic-gate 					match = TRUE;
19637c478bd9Sstevel@tonic-gate 					break;
19647c478bd9Sstevel@tonic-gate 				}
19657c478bd9Sstevel@tonic-gate 			}
19667c478bd9Sstevel@tonic-gate 			if (!match)
19677c478bd9Sstevel@tonic-gate 				return (0);
19687c478bd9Sstevel@tonic-gate 		}
19697c478bd9Sstevel@tonic-gate 		/* Step over the matched character */
19707c478bd9Sstevel@tonic-gate 		str++;
19717c478bd9Sstevel@tonic-gate 		return (str);
19727c478bd9Sstevel@tonic-gate 
19737c478bd9Sstevel@tonic-gate 	case mmt_any:
19747c478bd9Sstevel@tonic-gate 		/*
19757c478bd9Sstevel@tonic-gate 		 * Look ahead to find the beginning of the next element.
19767c478bd9Sstevel@tonic-gate 		 * Because a wildcard-match isn't necessarily uniquely
19777c478bd9Sstevel@tonic-gate 		 * determined until we've reached the end, we then continue
19787c478bd9Sstevel@tonic-gate 		 * to scan ahead.
19797c478bd9Sstevel@tonic-gate 		 */
19807c478bd9Sstevel@tonic-gate 		while ((next = scanMappingFormat(f, curf+1, nf, str, 0,
19817c478bd9Sstevel@tonic-gate 						&start, sepset)) != 0) {
19827c478bd9Sstevel@tonic-gate 			char	*tmp = next;
19837c478bd9Sstevel@tonic-gate 			int	cf;
19847c478bd9Sstevel@tonic-gate 
19857c478bd9Sstevel@tonic-gate 			for (cf = curf+2; cf < nf; cf++) {
19867c478bd9Sstevel@tonic-gate 				tmp = scanMappingFormat(f, cf, nf, tmp, 0,
19877c478bd9Sstevel@tonic-gate 					0, sepset);
19887c478bd9Sstevel@tonic-gate 				if (tmp == 0)
19897c478bd9Sstevel@tonic-gate 					break;
19907c478bd9Sstevel@tonic-gate 			}
19917c478bd9Sstevel@tonic-gate 			if (tmp == 0) {
19927c478bd9Sstevel@tonic-gate 				str = next;
19937c478bd9Sstevel@tonic-gate 			} else if (*tmp == '\0') {
19947c478bd9Sstevel@tonic-gate 				break;
19957c478bd9Sstevel@tonic-gate 			} else {
19967c478bd9Sstevel@tonic-gate 				return (0);
19977c478bd9Sstevel@tonic-gate 			}
19987c478bd9Sstevel@tonic-gate 		}
19997c478bd9Sstevel@tonic-gate 		if (next == 0 || start == 0)
20007c478bd9Sstevel@tonic-gate 			return (0);
20017c478bd9Sstevel@tonic-gate 
20027c478bd9Sstevel@tonic-gate 		if (fmtstart != 0)
20037c478bd9Sstevel@tonic-gate 			*fmtstart = str;
20047c478bd9Sstevel@tonic-gate 
20057c478bd9Sstevel@tonic-gate 		return (start);
20067c478bd9Sstevel@tonic-gate 
20077c478bd9Sstevel@tonic-gate 	case mmt_limit:
20087c478bd9Sstevel@tonic-gate 		if (f[curf].match.limit == eos) {
20097c478bd9Sstevel@tonic-gate 			if (fmtstart != 0) {
20107c478bd9Sstevel@tonic-gate 				/* Skip to the end */
20117c478bd9Sstevel@tonic-gate 				str = str + strcspn(str, sepset);
20127c478bd9Sstevel@tonic-gate 				*fmtstart = str;
20137c478bd9Sstevel@tonic-gate 			} else if (strchr(sepset, *str) == 0) {
20147c478bd9Sstevel@tonic-gate 				return (0);
20157c478bd9Sstevel@tonic-gate 			}
20167c478bd9Sstevel@tonic-gate 		}
20177c478bd9Sstevel@tonic-gate 		return (str);
20187c478bd9Sstevel@tonic-gate 
20197c478bd9Sstevel@tonic-gate 	case mmt_begin:
20207c478bd9Sstevel@tonic-gate 		if (fmtstart != 0)
20217c478bd9Sstevel@tonic-gate 			*fmtstart = str;
20227c478bd9Sstevel@tonic-gate 		return (str);
20237c478bd9Sstevel@tonic-gate 
20247c478bd9Sstevel@tonic-gate 	case mmt_end:
20257c478bd9Sstevel@tonic-gate 		if (fmtstart != 0) {
20267c478bd9Sstevel@tonic-gate 			/* Skip to the end */
20277c478bd9Sstevel@tonic-gate 			str = str + strcspn(str, sepset);
20287c478bd9Sstevel@tonic-gate 			*fmtstart = str;
20297c478bd9Sstevel@tonic-gate 			return (str);
20307c478bd9Sstevel@tonic-gate 		}
20317c478bd9Sstevel@tonic-gate 		/* No skipping, so we must be at the end of the value */
20327c478bd9Sstevel@tonic-gate 		if (strchr(sepset, *str) == 0)
20337c478bd9Sstevel@tonic-gate 			return (0);
20347c478bd9Sstevel@tonic-gate 		return (str);
20357c478bd9Sstevel@tonic-gate 
20367c478bd9Sstevel@tonic-gate 	default:
20377c478bd9Sstevel@tonic-gate 		break;
20387c478bd9Sstevel@tonic-gate 	}
20397c478bd9Sstevel@tonic-gate 
20407c478bd9Sstevel@tonic-gate 	return (0);
20417c478bd9Sstevel@tonic-gate }
20427c478bd9Sstevel@tonic-gate 
20437c478bd9Sstevel@tonic-gate /*
20447c478bd9Sstevel@tonic-gate  * Verify that the string 'str' matches the mapping format array 'f'.
20457c478bd9Sstevel@tonic-gate  * Returns 1 in case of a match, 0 otherwise.
20467c478bd9Sstevel@tonic-gate  */
20477c478bd9Sstevel@tonic-gate int
20487c478bd9Sstevel@tonic-gate verifyMappingMatch(__nis_mapping_format_t *f, char *str) {
20497c478bd9Sstevel@tonic-gate 	int			n, nf;
20507c478bd9Sstevel@tonic-gate 	__nis_mapping_format_t	*ftmp;
20517c478bd9Sstevel@tonic-gate 
20527c478bd9Sstevel@tonic-gate 	/* Count the number of format elements in the format */
20537c478bd9Sstevel@tonic-gate 	for (nf = 0, ftmp = f; ftmp->type != mmt_end; ftmp++) {
20547c478bd9Sstevel@tonic-gate 		nf++;
20557c478bd9Sstevel@tonic-gate 	}
20567c478bd9Sstevel@tonic-gate 	/* Count the mmt_end as well */
20577c478bd9Sstevel@tonic-gate 	nf++;
20587c478bd9Sstevel@tonic-gate 
20597c478bd9Sstevel@tonic-gate 	for (n = 0; n < nf; n++) {
20607c478bd9Sstevel@tonic-gate 		str = scanMappingFormat(f, n, nf, str, 0, 0, 0);
20617c478bd9Sstevel@tonic-gate 		if (str == 0)
20627c478bd9Sstevel@tonic-gate 			break;
20637c478bd9Sstevel@tonic-gate 	}
20647c478bd9Sstevel@tonic-gate 
20657c478bd9Sstevel@tonic-gate 	return ((str != 0) ? 1 : 0);
20667c478bd9Sstevel@tonic-gate }
20677c478bd9Sstevel@tonic-gate 
20687c478bd9Sstevel@tonic-gate /*
20697c478bd9Sstevel@tonic-gate  * Perform a match operation. For example, given the rule
20707c478bd9Sstevel@tonic-gate  *	("{%s}%s", auth_name, public_data)=nisPublicKey
20717c478bd9Sstevel@tonic-gate  * and assuming that 'nisPublicKey' has the value "{dh640-0}abcdef12345",
20727c478bd9Sstevel@tonic-gate  * assign "dh640-0" to 'auth_name' and "abcdef12345" to 'public_data'.
20737c478bd9Sstevel@tonic-gate  *
20747c478bd9Sstevel@tonic-gate  * Note that this function doesn't perform the actual assignment. Rather,
20757c478bd9Sstevel@tonic-gate  * it returns an array of __nis_value_t's, with element zero of the value
20767c478bd9Sstevel@tonic-gate  * array being the new value of the first matched item, element one the
20777c478bd9Sstevel@tonic-gate  * value of the second matched item, etc. In the example above, we'd
20787c478bd9Sstevel@tonic-gate  * return a value array with two elements.
20797c478bd9Sstevel@tonic-gate  *
20807c478bd9Sstevel@tonic-gate  * If there is more than one input value (inVal->numVals > 1), the
20817c478bd9Sstevel@tonic-gate  * output array elements will also be multi-valued.
20827c478bd9Sstevel@tonic-gate  *
20837c478bd9Sstevel@tonic-gate  * f		The match format
20847c478bd9Sstevel@tonic-gate  * inVal	Input value(s)
20857c478bd9Sstevel@tonic-gate  * numVal	Number of elements in the output value array
20867c478bd9Sstevel@tonic-gate  * sepset	List of separators
20877c478bd9Sstevel@tonic-gate  * outstr	Points to the updated position upto which the
20887c478bd9Sstevel@tonic-gate  *		input string has been matched
20897c478bd9Sstevel@tonic-gate  */
20907c478bd9Sstevel@tonic-gate __nis_value_t **
20917c478bd9Sstevel@tonic-gate matchMappingItem(__nis_mapping_format_t *f, __nis_value_t *inVal,
20927c478bd9Sstevel@tonic-gate 		int *numVals, char *sepset, char **outstr) {
20937c478bd9Sstevel@tonic-gate 	__nis_value_t		**v = 0;
2094*a87701e9SGary Mills 	int			i, n, ni, numItems, nf;
20957c478bd9Sstevel@tonic-gate 	char			*str, *valstr;
20967c478bd9Sstevel@tonic-gate 	__nis_mapping_format_t	*ftmp;
20977c478bd9Sstevel@tonic-gate 	char			*myself = "matchMappingItem";
20987c478bd9Sstevel@tonic-gate 
20997c478bd9Sstevel@tonic-gate 	if (f == 0 ||
21007c478bd9Sstevel@tonic-gate 		inVal == 0 || inVal->numVals < 1 || inVal->type != vt_string)
21017c478bd9Sstevel@tonic-gate 		return (0);
21027c478bd9Sstevel@tonic-gate 
21037c478bd9Sstevel@tonic-gate 	/* Count the number of format elements and items in the format */
21047c478bd9Sstevel@tonic-gate 	for (nf = numItems = 0, ftmp = f; ftmp->type != mmt_end; ftmp++) {
21057c478bd9Sstevel@tonic-gate 		nf++;
21067c478bd9Sstevel@tonic-gate 
21077c478bd9Sstevel@tonic-gate 		/*
21087c478bd9Sstevel@tonic-gate 		 * Count mmt_item and mmt_berstring (used by N2L to
21097c478bd9Sstevel@tonic-gate 		 * represent address %a)
21107c478bd9Sstevel@tonic-gate 		 */
21117c478bd9Sstevel@tonic-gate 		if (ftmp->type == mmt_item)
21127c478bd9Sstevel@tonic-gate 				numItems++;
21137c478bd9Sstevel@tonic-gate 		else if (ftmp->type == mmt_berstring && ftmp->match.berString &&
21147c478bd9Sstevel@tonic-gate 				ftmp->match.berString[0] == 'a')
21157c478bd9Sstevel@tonic-gate 				numItems++;
21167c478bd9Sstevel@tonic-gate 	}
21177c478bd9Sstevel@tonic-gate 	/* Count the mmt_end as well */
21187c478bd9Sstevel@tonic-gate 	nf++;
21197c478bd9Sstevel@tonic-gate 
21207c478bd9Sstevel@tonic-gate 	/*
21217c478bd9Sstevel@tonic-gate 	 * If no items, there will be no values. This isn't exactly an error
21227c478bd9Sstevel@tonic-gate 	 * from the limited point of view of this function, so we return a
21237c478bd9Sstevel@tonic-gate 	 * __nis_value_t with zero values.
21247c478bd9Sstevel@tonic-gate 	 */
21257c478bd9Sstevel@tonic-gate 	if (numItems <= 0) {
21267c478bd9Sstevel@tonic-gate 		v = am(myself, sizeof (v[0]));
21277c478bd9Sstevel@tonic-gate 		if (v == 0)
21287c478bd9Sstevel@tonic-gate 			return (0);
21297c478bd9Sstevel@tonic-gate 		v[0] = am(myself, sizeof (*v[0]));
21307c478bd9Sstevel@tonic-gate 		if (v[0] == 0) {
21317c478bd9Sstevel@tonic-gate 			sfree(v);
21327c478bd9Sstevel@tonic-gate 			return (0);
21337c478bd9Sstevel@tonic-gate 		}
21347c478bd9Sstevel@tonic-gate 		v[0]->type = vt_string;
21357c478bd9Sstevel@tonic-gate 		v[0]->numVals = 0;
21367c478bd9Sstevel@tonic-gate 		v[0]->val = 0;
21377c478bd9Sstevel@tonic-gate 		if (numVals != 0)
21387c478bd9Sstevel@tonic-gate 			*numVals = 1;
21397c478bd9Sstevel@tonic-gate 		return (v);
21407c478bd9Sstevel@tonic-gate 	}
21417c478bd9Sstevel@tonic-gate 
21427c478bd9Sstevel@tonic-gate 	/* Allocate and initialize the return array */
21437c478bd9Sstevel@tonic-gate 	v = am(myself, numItems * sizeof (v[0]));
21447c478bd9Sstevel@tonic-gate 	if (v == 0)
21457c478bd9Sstevel@tonic-gate 		return (0);
21467c478bd9Sstevel@tonic-gate 	for (n = 0; n < numItems; n++) {
21477c478bd9Sstevel@tonic-gate 		v[n] = am(myself, sizeof (*v[n]));
21487c478bd9Sstevel@tonic-gate 		if (v[n] == 0) {
21497c478bd9Sstevel@tonic-gate 			int	j;
21507c478bd9Sstevel@tonic-gate 
21517c478bd9Sstevel@tonic-gate 			for (j = 0; j < n; j++)
21527c478bd9Sstevel@tonic-gate 				freeValue(v[j], 1);
21537c478bd9Sstevel@tonic-gate 			sfree(v);
21547c478bd9Sstevel@tonic-gate 			return (0);
21557c478bd9Sstevel@tonic-gate 		}
21567c478bd9Sstevel@tonic-gate 		v[n]->type = vt_string;
21577c478bd9Sstevel@tonic-gate 		v[n]->numVals = 0;
21587c478bd9Sstevel@tonic-gate 		v[n]->val = am(myself, inVal->numVals * sizeof (v[n]->val[0]));
21597c478bd9Sstevel@tonic-gate 		if (v[n]->val == 0) {
21607c478bd9Sstevel@tonic-gate 			int	j;
21617c478bd9Sstevel@tonic-gate 
21627c478bd9Sstevel@tonic-gate 			for (j = 0; j < n; j++)
21637c478bd9Sstevel@tonic-gate 				freeValue(v[j], 1);
21647c478bd9Sstevel@tonic-gate 			sfree(v);
21657c478bd9Sstevel@tonic-gate 			return (0);
21667c478bd9Sstevel@tonic-gate 		}
21677c478bd9Sstevel@tonic-gate 		for (i = 0; i < inVal->numVals; i++) {
21687c478bd9Sstevel@tonic-gate 			v[n]->val[i].length = 0;
21697c478bd9Sstevel@tonic-gate 			v[n]->val[i].value = 0;
21707c478bd9Sstevel@tonic-gate 		}
21717c478bd9Sstevel@tonic-gate 	}
21727c478bd9Sstevel@tonic-gate 
21737c478bd9Sstevel@tonic-gate 	/* For each input value, perform the match operation */
21747c478bd9Sstevel@tonic-gate 	for (i = 0; i < inVal->numVals; i++) {
21757c478bd9Sstevel@tonic-gate 		str = inVal->val[i].value;
21767c478bd9Sstevel@tonic-gate 		if (str == 0)
21777c478bd9Sstevel@tonic-gate 			continue;
21787c478bd9Sstevel@tonic-gate 		for (n = 0, ni = 0; n < nf; n++) {
21797c478bd9Sstevel@tonic-gate 			valstr = 0;
21807c478bd9Sstevel@tonic-gate 			str = scanMappingFormat(f, n, nf, str, &valstr,
21817c478bd9Sstevel@tonic-gate 				0, sepset);
21827c478bd9Sstevel@tonic-gate 			if (str == 0)
21837c478bd9Sstevel@tonic-gate 				break;
21847c478bd9Sstevel@tonic-gate 			if (valstr != 0 && ni < numItems &&
21857c478bd9Sstevel@tonic-gate 					v[ni]->numVals < inVal->numVals) {
21867c478bd9Sstevel@tonic-gate 				v[ni]->val[v[ni]->numVals].value = valstr;
21877c478bd9Sstevel@tonic-gate 				v[ni]->val[v[ni]->numVals].length =
21887c478bd9Sstevel@tonic-gate 							strlen(valstr) + 1;
21897c478bd9Sstevel@tonic-gate 				v[ni]->numVals++;
21907c478bd9Sstevel@tonic-gate 				ni++;
21917c478bd9Sstevel@tonic-gate 			} else if (valstr != 0) {
21927c478bd9Sstevel@tonic-gate 				sfree(valstr);
21937c478bd9Sstevel@tonic-gate 			}
21947c478bd9Sstevel@tonic-gate 		}
21957c478bd9Sstevel@tonic-gate 		if (str == 0) {
21967c478bd9Sstevel@tonic-gate 			for (n = 0; n < numItems; n++)
21977c478bd9Sstevel@tonic-gate 				freeValue(v[n], 1);
21987c478bd9Sstevel@tonic-gate 			sfree(v);
21997c478bd9Sstevel@tonic-gate 			return (0);
22007c478bd9Sstevel@tonic-gate 		}
22017c478bd9Sstevel@tonic-gate 	}
22027c478bd9Sstevel@tonic-gate 
22037c478bd9Sstevel@tonic-gate 	if (numVals != 0)
22047c478bd9Sstevel@tonic-gate 		*numVals = numItems;
22057c478bd9Sstevel@tonic-gate 
22067c478bd9Sstevel@tonic-gate 	/*
22077c478bd9Sstevel@tonic-gate 	 * Update the return string upto the point it has been matched
22087c478bd9Sstevel@tonic-gate 	 * This string will be used by the N2L code in its next call
22097c478bd9Sstevel@tonic-gate 	 * to this function
22107c478bd9Sstevel@tonic-gate 	 */
22117c478bd9Sstevel@tonic-gate 	if (outstr != 0)
22127c478bd9Sstevel@tonic-gate 		*outstr = str;
22137c478bd9Sstevel@tonic-gate 
22147c478bd9Sstevel@tonic-gate 	return (v);
22157c478bd9Sstevel@tonic-gate }
22167c478bd9Sstevel@tonic-gate 
22177c478bd9Sstevel@tonic-gate /*
22187c478bd9Sstevel@tonic-gate  * Perform an extract operation. For example, given the expression
22197c478bd9Sstevel@tonic-gate  *	(name, "%s.*")
22207c478bd9Sstevel@tonic-gate  * and assuming 'name' is an item with the value "some.thing", the
22217c478bd9Sstevel@tonic-gate  * value returned by the extract is "some".
22227c478bd9Sstevel@tonic-gate  */
22237c478bd9Sstevel@tonic-gate __nis_value_t *
22247c478bd9Sstevel@tonic-gate extractMappingItem(__nis_mapping_item_t *item, __nis_mapping_format_t *f,
22257c478bd9Sstevel@tonic-gate 		__nis_rule_value_t *rv, int *stat) {
22267c478bd9Sstevel@tonic-gate 	__nis_value_t		*val = getMappingItem(item, mit_any,
22277c478bd9Sstevel@tonic-gate 			rv, 0, stat);
22287c478bd9Sstevel@tonic-gate 	__nis_single_value_t	*nval;
22297c478bd9Sstevel@tonic-gate 	int			i, n, nv, nf;
22307c478bd9Sstevel@tonic-gate 	__nis_mapping_format_t	*ftmp;
22317c478bd9Sstevel@tonic-gate 
22327c478bd9Sstevel@tonic-gate 	if (val == 0)
22337c478bd9Sstevel@tonic-gate 		return (0);
22347c478bd9Sstevel@tonic-gate 	else if (f == 0 || rv == 0 || val->val == 0 ||
22357c478bd9Sstevel@tonic-gate 			val->numVals <= 0 || val->type != vt_string) {
22367c478bd9Sstevel@tonic-gate 		freeValue(val, 1);
22377c478bd9Sstevel@tonic-gate 		return (0);
22387c478bd9Sstevel@tonic-gate 	}
22397c478bd9Sstevel@tonic-gate 
22407c478bd9Sstevel@tonic-gate 	/* Sanity check the format; it must have one and only one mmt_item */
22417c478bd9Sstevel@tonic-gate 	{
22427c478bd9Sstevel@tonic-gate 		int	numitem;
22437c478bd9Sstevel@tonic-gate 
22447c478bd9Sstevel@tonic-gate 		for (nf = numitem = 0, ftmp = f; ftmp->type != mmt_end;
22457c478bd9Sstevel@tonic-gate 				ftmp++) {
22467c478bd9Sstevel@tonic-gate 			nf++;
22477c478bd9Sstevel@tonic-gate 			if (ftmp->type == mmt_item)
22487c478bd9Sstevel@tonic-gate 				numitem++;
22497c478bd9Sstevel@tonic-gate 		}
22507c478bd9Sstevel@tonic-gate 		/* Count the mmt_end as well */
22517c478bd9Sstevel@tonic-gate 		nf++;
22527c478bd9Sstevel@tonic-gate 		if (numitem != 1) {
22537c478bd9Sstevel@tonic-gate 			freeValue(val, 1);
22547c478bd9Sstevel@tonic-gate 			return (0);
22557c478bd9Sstevel@tonic-gate 		}
22567c478bd9Sstevel@tonic-gate 	}
22577c478bd9Sstevel@tonic-gate 
22587c478bd9Sstevel@tonic-gate 	nval = val->val;
22597c478bd9Sstevel@tonic-gate 	nv = val->numVals;
22607c478bd9Sstevel@tonic-gate 	val->repeat = FALSE;
22617c478bd9Sstevel@tonic-gate 	val->val = 0;
22627c478bd9Sstevel@tonic-gate 	val->numVals = 0;
22637c478bd9Sstevel@tonic-gate 
22647c478bd9Sstevel@tonic-gate 	/* If the item has multiple values, we extract each one independently */
22657c478bd9Sstevel@tonic-gate 	for (i = 0; i < nv; i++) {
22667c478bd9Sstevel@tonic-gate 		char			*str = nval[i].value;
22677c478bd9Sstevel@tonic-gate 		char			*newstr = 0;
22687c478bd9Sstevel@tonic-gate 		__nis_single_value_t	*newval;
22697c478bd9Sstevel@tonic-gate 
22707c478bd9Sstevel@tonic-gate 		if (nval[i].value == 0)
22717c478bd9Sstevel@tonic-gate 			continue;
22727c478bd9Sstevel@tonic-gate 
22737c478bd9Sstevel@tonic-gate 		/*
22747c478bd9Sstevel@tonic-gate 		 * We match the whole string, even if we find a value for
22757c478bd9Sstevel@tonic-gate 		 * the item before exhausting all format elements. By doing
22767c478bd9Sstevel@tonic-gate 		 * this, we ensure that the string really matches the complete
22777c478bd9Sstevel@tonic-gate 		 * format specification.
22787c478bd9Sstevel@tonic-gate 		 */
22797c478bd9Sstevel@tonic-gate 		for (n = 0; n < nf; n++) {
22807c478bd9Sstevel@tonic-gate 			str = scanMappingFormat(f, n, nf, str, &newstr, 0, 0);
22817c478bd9Sstevel@tonic-gate 			if (str == 0)
22827c478bd9Sstevel@tonic-gate 				break;
22837c478bd9Sstevel@tonic-gate 		}
22847c478bd9Sstevel@tonic-gate 
22857c478bd9Sstevel@tonic-gate 		/*
22867c478bd9Sstevel@tonic-gate 		 * *str should now be NUL, meaning we've reached the end of
22877c478bd9Sstevel@tonic-gate 		 * the string (value), and it completely matched the format.
22887c478bd9Sstevel@tonic-gate 		 * If 'str' is NULL, there was an error, and if 'newstr' is
22897c478bd9Sstevel@tonic-gate 		 * 0, we somehow failed to obtain a value.
22907c478bd9Sstevel@tonic-gate 		 */
22917c478bd9Sstevel@tonic-gate 		if (str == 0 || *str != '\0' || newstr == 0 ||
22927c478bd9Sstevel@tonic-gate 				(newval = realloc(val->val,
22937c478bd9Sstevel@tonic-gate 					(val->numVals+1) *
22947c478bd9Sstevel@tonic-gate 					sizeof (val->val[0]))) == 0) {
22957c478bd9Sstevel@tonic-gate 			freeValue(val, 1);
22967c478bd9Sstevel@tonic-gate 			for (n = 0; n < nv; n++) {
22977c478bd9Sstevel@tonic-gate 				sfree(nval[n].value);
22987c478bd9Sstevel@tonic-gate 			}
22997c478bd9Sstevel@tonic-gate 			free(nval);
23007c478bd9Sstevel@tonic-gate 			sfree(newstr);
23017c478bd9Sstevel@tonic-gate 			return (0);
23027c478bd9Sstevel@tonic-gate 		}
23037c478bd9Sstevel@tonic-gate 
23047c478bd9Sstevel@tonic-gate 		val->val = newval;
23057c478bd9Sstevel@tonic-gate 		val->val[val->numVals].value = newstr;
23067c478bd9Sstevel@tonic-gate 		val->val[val->numVals].length = strlen(newstr) + 1;
23077c478bd9Sstevel@tonic-gate 		val->numVals++;
23087c478bd9Sstevel@tonic-gate 
23097c478bd9Sstevel@tonic-gate 		free(nval[i].value);
23107c478bd9Sstevel@tonic-gate 		nval[i].value = 0;
23117c478bd9Sstevel@tonic-gate 	}
23127c478bd9Sstevel@tonic-gate 	free(nval);
23137c478bd9Sstevel@tonic-gate 
23147c478bd9Sstevel@tonic-gate 	return (val);
23157c478bd9Sstevel@tonic-gate }
23167c478bd9Sstevel@tonic-gate 
23177c478bd9Sstevel@tonic-gate /*
23187c478bd9Sstevel@tonic-gate  * For each value in 'val', remove the last character, provided that
23197c478bd9Sstevel@tonic-gate  * it matches 'elide'.
23207c478bd9Sstevel@tonic-gate  */
23217c478bd9Sstevel@tonic-gate void
23227c478bd9Sstevel@tonic-gate stringElide(__nis_value_t *val, char elide) {
23237c478bd9Sstevel@tonic-gate 
23247c478bd9Sstevel@tonic-gate 	if (val != 0 && val->type == vt_string) {
23257c478bd9Sstevel@tonic-gate 		int	i;
23267c478bd9Sstevel@tonic-gate 
23277c478bd9Sstevel@tonic-gate 		for (i = 0; i < val->numVals; i++) {
23287c478bd9Sstevel@tonic-gate 			int	end = val->val[i].length;
23297c478bd9Sstevel@tonic-gate 			char	*str = val->val[i].value;
23307c478bd9Sstevel@tonic-gate 
23317c478bd9Sstevel@tonic-gate 			if (str == 0 || end <= 0)
23327c478bd9Sstevel@tonic-gate 				continue;
23337c478bd9Sstevel@tonic-gate 
23347c478bd9Sstevel@tonic-gate 			/*
23357c478bd9Sstevel@tonic-gate 			 * If the NUL was counted in the length, step back
23367c478bd9Sstevel@tonic-gate 			 * over it.
23377c478bd9Sstevel@tonic-gate 			 */
23387c478bd9Sstevel@tonic-gate 			if (str[end-1] == '\0')
23397c478bd9Sstevel@tonic-gate 				end--;
23407c478bd9Sstevel@tonic-gate 			if (end > 0 && str[end-1] == elide) {
23417c478bd9Sstevel@tonic-gate 				str[end-1] = '\0';
23427c478bd9Sstevel@tonic-gate 				val->val[i].length--;
23437c478bd9Sstevel@tonic-gate 			}
23447c478bd9Sstevel@tonic-gate 		}
23457c478bd9Sstevel@tonic-gate 	}
23467c478bd9Sstevel@tonic-gate }
23477c478bd9Sstevel@tonic-gate 
23487c478bd9Sstevel@tonic-gate /*
23497c478bd9Sstevel@tonic-gate  * Obtain the value for the mapping sub-element 'e', given the input
23507c478bd9Sstevel@tonic-gate  * rule-value 'rv'.
23517c478bd9Sstevel@tonic-gate  */
23527c478bd9Sstevel@tonic-gate __nis_value_t *
23537c478bd9Sstevel@tonic-gate getMappingSubElement(__nis_mapping_sub_element_t *e,
23547c478bd9Sstevel@tonic-gate 	__nis_rule_value_t *rv, int *np_ldap_stat) {
23557c478bd9Sstevel@tonic-gate 	__nis_value_t	*val;
23567c478bd9Sstevel@tonic-gate 
23577c478bd9Sstevel@tonic-gate 	if (e == 0)
23587c478bd9Sstevel@tonic-gate 		return (0);
23597c478bd9Sstevel@tonic-gate 
23607c478bd9Sstevel@tonic-gate 	switch (e->type) {
23617c478bd9Sstevel@tonic-gate 	case me_item:
23627c478bd9Sstevel@tonic-gate 		val = getMappingItem(&e->element.item, mit_any, rv, 0,
23637c478bd9Sstevel@tonic-gate 			np_ldap_stat);
23647c478bd9Sstevel@tonic-gate 		break;
23657c478bd9Sstevel@tonic-gate 	case me_print:
23667c478bd9Sstevel@tonic-gate 		val = getMappingFormatArray(e->element.print.fmt, rv,
23677c478bd9Sstevel@tonic-gate 						fa_item,
23687c478bd9Sstevel@tonic-gate 						e->element.print.numItems,
23697c478bd9Sstevel@tonic-gate 						e->element.print.item);
23707c478bd9Sstevel@tonic-gate 		if (e->element.print.doElide)
23717c478bd9Sstevel@tonic-gate 			stringElide(val, e->element.print.elide);
23727c478bd9Sstevel@tonic-gate 		break;
23737c478bd9Sstevel@tonic-gate 	case me_split:
23747c478bd9Sstevel@tonic-gate 		val = splitMappingItem(&e->element.split.item,
23757c478bd9Sstevel@tonic-gate 					e->element.split.delim,
23767c478bd9Sstevel@tonic-gate 					rv);
23777c478bd9Sstevel@tonic-gate 		break;
23787c478bd9Sstevel@tonic-gate 	case me_extract:
23797c478bd9Sstevel@tonic-gate 		val = extractMappingItem(&e->element.extract.item,
23807c478bd9Sstevel@tonic-gate 					e->element.extract.fmt,
23817c478bd9Sstevel@tonic-gate 					rv, np_ldap_stat);
23827c478bd9Sstevel@tonic-gate 		break;
23837c478bd9Sstevel@tonic-gate 	case me_match:
23847c478bd9Sstevel@tonic-gate 	default:
23857c478bd9Sstevel@tonic-gate 		val = 0;
23867c478bd9Sstevel@tonic-gate 		break;
23877c478bd9Sstevel@tonic-gate 	}
23887c478bd9Sstevel@tonic-gate 
23897c478bd9Sstevel@tonic-gate 	return (val);
23907c478bd9Sstevel@tonic-gate }
23917c478bd9Sstevel@tonic-gate 
23927c478bd9Sstevel@tonic-gate /*
23937c478bd9Sstevel@tonic-gate  * Obtain the value of the mapping element 'e', given the input rule-
23947c478bd9Sstevel@tonic-gate  * value 'rv'. The 'native' mapping type is used when 'rv' is NULL,
23957c478bd9Sstevel@tonic-gate  * and the result is a string representation of the mapping element;
23967c478bd9Sstevel@tonic-gate  * in that case, items of the 'native' type are printed without their
23977c478bd9Sstevel@tonic-gate  * type designation ("nis+" or "ldap").
23987c478bd9Sstevel@tonic-gate  */
23997c478bd9Sstevel@tonic-gate __nis_value_t *
24007c478bd9Sstevel@tonic-gate getMappingElement(__nis_mapping_element_t *e, __nis_mapping_item_type_t native,
24017c478bd9Sstevel@tonic-gate 		__nis_rule_value_t *rv, int *stat) {
24027c478bd9Sstevel@tonic-gate 	__nis_value_t	*val, **tv;
24037c478bd9Sstevel@tonic-gate 	int		i, success = 0, novalue = 0;
24047c478bd9Sstevel@tonic-gate 	int *np_ldap_stat;
24057c478bd9Sstevel@tonic-gate 	char		*myself = "getMappingElement";
24067c478bd9Sstevel@tonic-gate 
24077c478bd9Sstevel@tonic-gate 	switch (e->type) {
24087c478bd9Sstevel@tonic-gate 	case me_item:
24097c478bd9Sstevel@tonic-gate 		val = getMappingItem(&e->element.item, native, rv, 0, NULL);
24107c478bd9Sstevel@tonic-gate 		break;
24117c478bd9Sstevel@tonic-gate 	case me_print:
24127c478bd9Sstevel@tonic-gate 		tv = am(myself, e->element.print.numSubElements *
24137c478bd9Sstevel@tonic-gate 			sizeof (tv[0]));
24147c478bd9Sstevel@tonic-gate 		np_ldap_stat = am(myself,
24157c478bd9Sstevel@tonic-gate 			e->element.print.numSubElements * sizeof (int));
24167c478bd9Sstevel@tonic-gate 		if ((e->element.print.numSubElements > 0) &&
24177c478bd9Sstevel@tonic-gate 				(tv == 0 || np_ldap_stat == 0)) {
24187c478bd9Sstevel@tonic-gate 			val = 0;
24197c478bd9Sstevel@tonic-gate 			sfree(tv);
24207c478bd9Sstevel@tonic-gate 			sfree(np_ldap_stat);
24217c478bd9Sstevel@tonic-gate 			break;
24227c478bd9Sstevel@tonic-gate 		}
24237c478bd9Sstevel@tonic-gate 		for (i = 0; i < e->element.print.numSubElements; i++) {
24247c478bd9Sstevel@tonic-gate 			np_ldap_stat[i] = 0;
24257c478bd9Sstevel@tonic-gate 			tv[i] = getMappingSubElement(
24267c478bd9Sstevel@tonic-gate 				&e->element.print.subElement[i],
24277c478bd9Sstevel@tonic-gate 				rv, &np_ldap_stat[i]);
24287c478bd9Sstevel@tonic-gate 		}
24297c478bd9Sstevel@tonic-gate 		/*
24307c478bd9Sstevel@tonic-gate 		 * if we get NP_LDAP_NO_VALUE to any of the subelement
24317c478bd9Sstevel@tonic-gate 		 * and we get NP_LDAP_MAP_SUCCESS to all other subelement
24327c478bd9Sstevel@tonic-gate 		 * then we had enough nis+ column values which can
24337c478bd9Sstevel@tonic-gate 		 * produce value for this rule, but didn't. So return
24347c478bd9Sstevel@tonic-gate 		 * NP_LDAP_RULES_NO_VALUE to indicate to proceed to
24357c478bd9Sstevel@tonic-gate 		 * next database id.
24367c478bd9Sstevel@tonic-gate 		 */
24377c478bd9Sstevel@tonic-gate 		for (i = 0; i < e->element.print.numSubElements; i++) {
24387c478bd9Sstevel@tonic-gate 			if (np_ldap_stat[i] == NP_LDAP_MAP_SUCCESS)
24397c478bd9Sstevel@tonic-gate 				success++;
24407c478bd9Sstevel@tonic-gate 			if (np_ldap_stat[i] == NP_LDAP_NO_VALUE)
24417c478bd9Sstevel@tonic-gate 				novalue++;
24427c478bd9Sstevel@tonic-gate 		}
24437c478bd9Sstevel@tonic-gate 		if (stat != NULL && novalue > 0 &&
24447c478bd9Sstevel@tonic-gate 				((novalue+success) ==
24457c478bd9Sstevel@tonic-gate 					e->element.print.numSubElements))
24467c478bd9Sstevel@tonic-gate 					    *stat = NP_LDAP_RULES_NO_VALUE;
24477c478bd9Sstevel@tonic-gate 		val = getMappingFormatArray(e->element.print.fmt, rv,
24487c478bd9Sstevel@tonic-gate 						fa_value,
24497c478bd9Sstevel@tonic-gate 						e->element.print.numSubElements,
24507c478bd9Sstevel@tonic-gate 						tv);
24517c478bd9Sstevel@tonic-gate 		for (i = 0; i < e->element.print.numSubElements; i++) {
24527c478bd9Sstevel@tonic-gate 			freeValue(tv[i], 1);
24537c478bd9Sstevel@tonic-gate 		}
24547c478bd9Sstevel@tonic-gate 		sfree(tv);
24557c478bd9Sstevel@tonic-gate 		sfree(np_ldap_stat);
24567c478bd9Sstevel@tonic-gate 		if (e->element.print.doElide)
24577c478bd9Sstevel@tonic-gate 			stringElide(val, e->element.print.elide);
24587c478bd9Sstevel@tonic-gate 		break;
24597c478bd9Sstevel@tonic-gate 	case me_split:
24607c478bd9Sstevel@tonic-gate 		val = splitMappingItem(&e->element.split.item,
24617c478bd9Sstevel@tonic-gate 					e->element.split.delim,
24627c478bd9Sstevel@tonic-gate 					rv);
24637c478bd9Sstevel@tonic-gate 		break;
24647c478bd9Sstevel@tonic-gate 	case me_match:
24657c478bd9Sstevel@tonic-gate 		/*
24667c478bd9Sstevel@tonic-gate 		 * A match doesn't produce an assignable value per se,
24677c478bd9Sstevel@tonic-gate 		 * so we shouldn't get one here.
24687c478bd9Sstevel@tonic-gate 		 */
24697c478bd9Sstevel@tonic-gate 		val = 0;
24707c478bd9Sstevel@tonic-gate 		break;
24717c478bd9Sstevel@tonic-gate 	case me_extract:
24727c478bd9Sstevel@tonic-gate 		val = extractMappingItem(&e->element.extract.item,
24737c478bd9Sstevel@tonic-gate 					e->element.extract.fmt,
24747c478bd9Sstevel@tonic-gate 					rv, NULL);
24757c478bd9Sstevel@tonic-gate 		break;
24767c478bd9Sstevel@tonic-gate 	default:
24777c478bd9Sstevel@tonic-gate 		val = 0;
24787c478bd9Sstevel@tonic-gate 		break;
24797c478bd9Sstevel@tonic-gate 	}
24807c478bd9Sstevel@tonic-gate 
24817c478bd9Sstevel@tonic-gate 	return (val);
24827c478bd9Sstevel@tonic-gate }
2483