xref: /titanic_52/usr/src/lib/libnisdb/ldap_attr.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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*a87701e9SGary Mills  * Copyright 2015 Gary Mills
247c478bd9Sstevel@tonic-gate  * Copyright 2001-2003 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
297c478bd9Sstevel@tonic-gate #include <strings.h>
307c478bd9Sstevel@tonic-gate #include <rpcsvc/nis.h>
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include "nis_parse_ldap_conf.h"
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include "ldap_attr.h"
357c478bd9Sstevel@tonic-gate #include "ldap_util.h"
367c478bd9Sstevel@tonic-gate #include "ldap_structs.h"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  * If 'name' doesn't end in a trailing dot, return a copy with the
417c478bd9Sstevel@tonic-gate  * value of "nisplusLDAPbaseDomain" appended. Otherwise, return a
427c478bd9Sstevel@tonic-gate  * copy of 'name'. If deallocate!=0, free 'name'.
437c478bd9Sstevel@tonic-gate  */
447c478bd9Sstevel@tonic-gate char *
457c478bd9Sstevel@tonic-gate fullObjName(int deallocate, char *name) {
467c478bd9Sstevel@tonic-gate 	int	l;
477c478bd9Sstevel@tonic-gate 	char	*full;
487c478bd9Sstevel@tonic-gate 	char	*myself = "fullObjName";
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 	if (name == 0)
517c478bd9Sstevel@tonic-gate 		return (sdup(myself, T, proxyInfo.default_nis_domain));
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 	l = strlen(name);
547c478bd9Sstevel@tonic-gate 	if (name[l-1] == '.') {
557c478bd9Sstevel@tonic-gate 		full = sdup(myself, T, name);
567c478bd9Sstevel@tonic-gate 	} else {
577c478bd9Sstevel@tonic-gate 		full = scat(myself, T, scat(myself, F, name, "."),
587c478bd9Sstevel@tonic-gate 			sdup(myself, T, proxyInfo.default_nis_domain));
597c478bd9Sstevel@tonic-gate 	}
607c478bd9Sstevel@tonic-gate 	if (deallocate)
617c478bd9Sstevel@tonic-gate 		free(name);
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	return (full);
647c478bd9Sstevel@tonic-gate }
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /*
677c478bd9Sstevel@tonic-gate  * Convert a domain name ("x.y.z.", say) to a "dc=..." type LDAP equivalent
687c478bd9Sstevel@tonic-gate  * ("dc=x,dc=y,dx=z"). The domain name supplied MUST be terminated by a
697c478bd9Sstevel@tonic-gate  * trailing dot. If 'domain' is NULL, the value of "nisplusLDAPbaseDomain"
707c478bd9Sstevel@tonic-gate  * is converted.
717c478bd9Sstevel@tonic-gate  */
727c478bd9Sstevel@tonic-gate char *
737c478bd9Sstevel@tonic-gate domain2base(char *domain) {
747c478bd9Sstevel@tonic-gate 	char	*base = 0;
757c478bd9Sstevel@tonic-gate 	int	l, i;
767c478bd9Sstevel@tonic-gate 	char	*myself = "domain2base";
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	if (domain == 0)
797c478bd9Sstevel@tonic-gate 		domain = sdup(myself, T, proxyInfo.default_nis_domain);
807c478bd9Sstevel@tonic-gate 	if (domain == 0)
817c478bd9Sstevel@tonic-gate 		return (0);
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	for (l = 0, i = 0; domain[i] != '\0'; i++) {
847c478bd9Sstevel@tonic-gate 		if (domain[i] == '.') {
857c478bd9Sstevel@tonic-gate 			domain[i] = '\0';
867c478bd9Sstevel@tonic-gate 			if (l != 0)
877c478bd9Sstevel@tonic-gate 				base = scat(myself, T, base,
887c478bd9Sstevel@tonic-gate 					scat(myself, F, ",dc=", &domain[l]));
897c478bd9Sstevel@tonic-gate 			else
907c478bd9Sstevel@tonic-gate 				base = scat(myself, T, base,
917c478bd9Sstevel@tonic-gate 					scat(myself, F, "dc=", &domain[l]));
927c478bd9Sstevel@tonic-gate 			l = i+1;
937c478bd9Sstevel@tonic-gate 		}
947c478bd9Sstevel@tonic-gate 	}
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	return (base);
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate /*
1007c478bd9Sstevel@tonic-gate  * If 'name' ends in a trailing comma, append the value of the
1017c478bd9Sstevel@tonic-gate  * "defaultSearchBase". If deallocate!=0, free 'name'.
1027c478bd9Sstevel@tonic-gate  */
1037c478bd9Sstevel@tonic-gate char *
1047c478bd9Sstevel@tonic-gate fullLDAPname(int deallocate, char *name) {
1057c478bd9Sstevel@tonic-gate 	int	err = 0;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	return (appendBase(name, proxyInfo.default_search_base, &err,
1087c478bd9Sstevel@tonic-gate 				deallocate));
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate  * If the 'item' string ends in a comma, append 'base', and return
1137c478bd9Sstevel@tonic-gate  * the result. On exit, '*err' will be zero if successful, non-zero
1147c478bd9Sstevel@tonic-gate  * otherwise. If 'dealloc' is non-zero, 'item' is freed; this happens
1157c478bd9Sstevel@tonic-gate  * even if an error status is returned.
1167c478bd9Sstevel@tonic-gate  *
1177c478bd9Sstevel@tonic-gate  * The return value is always allocated, and must be freed by the caller.
1187c478bd9Sstevel@tonic-gate  */
1197c478bd9Sstevel@tonic-gate char *
1207c478bd9Sstevel@tonic-gate appendBase(char *item, char *base, int *err, int dealloc) {
1217c478bd9Sstevel@tonic-gate 	char	*new;
1227c478bd9Sstevel@tonic-gate 	int	len, deferr;
1237c478bd9Sstevel@tonic-gate 	char	*myself = "appendBase";
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	/*
1267c478bd9Sstevel@tonic-gate 	 * Make sure that 'err' points to something valid, so that we can
1277c478bd9Sstevel@tonic-gate 	 * dispense with all those 'if (err != 0)'.
1287c478bd9Sstevel@tonic-gate 	 */
1297c478bd9Sstevel@tonic-gate 	if (err == 0)
1307c478bd9Sstevel@tonic-gate 		err = &deferr;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	/* Establish default (successful) error status */
1337c478bd9Sstevel@tonic-gate 	*err = 0;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	/* Trivial case 1: If 'item' is NULL, return a copy of 'base' */
1367c478bd9Sstevel@tonic-gate 	if (item == 0) {
1377c478bd9Sstevel@tonic-gate 		new = sdup(myself, T, base);
1387c478bd9Sstevel@tonic-gate 		if (new == 0)
1397c478bd9Sstevel@tonic-gate 			*err = -1;
1407c478bd9Sstevel@tonic-gate 		return (new);
1417c478bd9Sstevel@tonic-gate 	}
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	/* Trivial case 2: If 'base' is NULL, return a copy of 'item' */
1447c478bd9Sstevel@tonic-gate 	if (base == 0) {
1457c478bd9Sstevel@tonic-gate 		new = sdup(myself, T, item);
1467c478bd9Sstevel@tonic-gate 		if (new == 0)
1477c478bd9Sstevel@tonic-gate 			*err = -1;
1487c478bd9Sstevel@tonic-gate 		if (dealloc)
1497c478bd9Sstevel@tonic-gate 			free(item);
1507c478bd9Sstevel@tonic-gate 		return (new);
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	len = strlen(item);
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	/* If 'item' is the empty string, return a copy of 'base' */
1567c478bd9Sstevel@tonic-gate 	if (len <= 0) {
1577c478bd9Sstevel@tonic-gate 		new = sdup(myself, T, base);
1587c478bd9Sstevel@tonic-gate 		if (new == 0)
1597c478bd9Sstevel@tonic-gate 			*err = -1;
1607c478bd9Sstevel@tonic-gate 		if (dealloc)
1617c478bd9Sstevel@tonic-gate 			free(item);
1627c478bd9Sstevel@tonic-gate 		return (new);
1637c478bd9Sstevel@tonic-gate 	}
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	/*
1667c478bd9Sstevel@tonic-gate 	 * If 'item' ends in a comma, append 'base', and return a copy
1677c478bd9Sstevel@tonic-gate 	 * of the result. Otherwise, return a copy of 'item'.
1687c478bd9Sstevel@tonic-gate 	 */
1697c478bd9Sstevel@tonic-gate 	if (item[len-1] == ',') {
1707c478bd9Sstevel@tonic-gate 		int	blen = slen(base);
1717c478bd9Sstevel@tonic-gate 		new = am(myself, len + blen + 1);
1727c478bd9Sstevel@tonic-gate 		if (new != 0) {
1737c478bd9Sstevel@tonic-gate 			(void) memcpy(new, item, len);
1747c478bd9Sstevel@tonic-gate 			(void) memcpy(&new[len], base, blen);
1757c478bd9Sstevel@tonic-gate 		} else {
1767c478bd9Sstevel@tonic-gate 			*err = -1;
1777c478bd9Sstevel@tonic-gate 		}
1787c478bd9Sstevel@tonic-gate 	} else {
1797c478bd9Sstevel@tonic-gate 		new = sdup(myself, T, item);
1807c478bd9Sstevel@tonic-gate 		if (new == 0)
1817c478bd9Sstevel@tonic-gate 			*err = -1;
1827c478bd9Sstevel@tonic-gate 	}
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	if (dealloc)
1857c478bd9Sstevel@tonic-gate 		free(item);
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	return (new);
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate /*
1917c478bd9Sstevel@tonic-gate  * Despite its general-sounding name, this function only knows how to
1927c478bd9Sstevel@tonic-gate  * turn a list of attributes ("a,b,c") into an AND filter ("(&(a)(b)(c))").
1937c478bd9Sstevel@tonic-gate  */
1947c478bd9Sstevel@tonic-gate char *
1957c478bd9Sstevel@tonic-gate makeFilter(char *attr) {
1967c478bd9Sstevel@tonic-gate 	int	len, s, e, c;
1977c478bd9Sstevel@tonic-gate 	char	*str, *filter, *tmp;
1987c478bd9Sstevel@tonic-gate 	char	*myself = "makeFilter";
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	if (attr == 0 || (len = strlen(attr)) == 0)
2017c478bd9Sstevel@tonic-gate 		return (0);
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	/* Assume already of appropriate form if first char is '(' */
2047c478bd9Sstevel@tonic-gate 	if (len > 1 && attr[0] == '(' && attr[len-1] == ')')
2057c478bd9Sstevel@tonic-gate 		return (sdup(myself, T, attr));
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	str = sdup(myself, T, attr);
2087c478bd9Sstevel@tonic-gate 	if (str == 0)
2097c478bd9Sstevel@tonic-gate 		return (0);
2107c478bd9Sstevel@tonic-gate 	filter = sdup(myself, T, "(&");
2117c478bd9Sstevel@tonic-gate 	if (filter == 0) {
2127c478bd9Sstevel@tonic-gate 		free(str);
2137c478bd9Sstevel@tonic-gate 		return (0);
2147c478bd9Sstevel@tonic-gate 	}
2157c478bd9Sstevel@tonic-gate 	for (s = c = 0; s < len; s = e+1) {
2167c478bd9Sstevel@tonic-gate 		/* Skip blank space, if any */
217*a87701e9SGary Mills 		for (; str[s] == ' ' || str[s] == '\t'; s++);
2187c478bd9Sstevel@tonic-gate 		/* Find delimiter (comma) or end of string */
2197c478bd9Sstevel@tonic-gate 		for (e = s; str[e] != '\0' && str[e] != ','; e++);
2207c478bd9Sstevel@tonic-gate 		str[e] = '\0';
2217c478bd9Sstevel@tonic-gate 		tmp = scat(myself, T, sdup(myself, T, "("),
2227c478bd9Sstevel@tonic-gate 			scat(myself, F, &str[s], ")"));
2237c478bd9Sstevel@tonic-gate 		if (tmp == 0) {
2247c478bd9Sstevel@tonic-gate 			sfree(filter);
2257c478bd9Sstevel@tonic-gate 			return (0);
2267c478bd9Sstevel@tonic-gate 		}
2277c478bd9Sstevel@tonic-gate 		c++;
2287c478bd9Sstevel@tonic-gate 		filter = scat(myself, T, filter, tmp);
2297c478bd9Sstevel@tonic-gate 	}
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	/*
2327c478bd9Sstevel@tonic-gate 	 * If there's just one component, we return it as is. This
2337c478bd9Sstevel@tonic-gate 	 * means we avoid turning "objectClass=posixAccount" into
2347c478bd9Sstevel@tonic-gate 	 * "(&(objectClass=posixAccount))".
2357c478bd9Sstevel@tonic-gate 	 */
2367c478bd9Sstevel@tonic-gate 	if (c == 1) {
2377c478bd9Sstevel@tonic-gate 		sfree(filter);
2387c478bd9Sstevel@tonic-gate 		return (str);
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	/* Add the closing ')' */
2427c478bd9Sstevel@tonic-gate 	tmp = filter;
2437c478bd9Sstevel@tonic-gate 	filter = scat(myself, F, tmp, ")");
2447c478bd9Sstevel@tonic-gate 	sfree(tmp);
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	free(str);
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	return (filter);
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate /*
2527c478bd9Sstevel@tonic-gate  * Split an AND-filter string into components.
2537c478bd9Sstevel@tonic-gate  */
2547c478bd9Sstevel@tonic-gate char **
2557c478bd9Sstevel@tonic-gate makeFilterComp(char *filter, int *numComps) {
2567c478bd9Sstevel@tonic-gate 	int	nc = 0, s, e, i;
2577c478bd9Sstevel@tonic-gate 	char	**comp = 0, **new, *str;
2587c478bd9Sstevel@tonic-gate 	int	len;
2597c478bd9Sstevel@tonic-gate 	char	*myself = "makeFilterComp";
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	if ((len = slen(filter)) <= 0)
2627c478bd9Sstevel@tonic-gate 		return (0);
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	/* Is it just a plain "attr=val" string ? If so, return a copy */
2657c478bd9Sstevel@tonic-gate 	if (len <= 2 || filter[0] != '(') {
2667c478bd9Sstevel@tonic-gate 		comp = am(myself, 2 * sizeof (comp[0]));
2677c478bd9Sstevel@tonic-gate 		if (comp == 0)
2687c478bd9Sstevel@tonic-gate 			return (0);
2697c478bd9Sstevel@tonic-gate 		comp[0] = sdup(myself, T, filter);
2707c478bd9Sstevel@tonic-gate 		if (comp[0] == 0) {
2717c478bd9Sstevel@tonic-gate 			sfree(comp);
2727c478bd9Sstevel@tonic-gate 			return (0);
2737c478bd9Sstevel@tonic-gate 		}
2747c478bd9Sstevel@tonic-gate 		if (numComps != 0)
2757c478bd9Sstevel@tonic-gate 			*numComps = 1;
2767c478bd9Sstevel@tonic-gate 		return (comp);
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	if (filter != 0 && (len = strlen(filter)) != 0 && len > 2 &&
2807c478bd9Sstevel@tonic-gate 			filter[0] == '(' && filter[1] == '&' &&
2817c478bd9Sstevel@tonic-gate 			filter[len-1] == ')') {
2827c478bd9Sstevel@tonic-gate 		str = sdup(myself, T, filter);
2837c478bd9Sstevel@tonic-gate 		if (str == 0)
2847c478bd9Sstevel@tonic-gate 			return (0);
2857c478bd9Sstevel@tonic-gate 		for (s = 2; s < len; s = e+1) {
2867c478bd9Sstevel@tonic-gate 			/* Skip past the '(' */
287*a87701e9SGary Mills 			for (; s < len && str[s] != '('; s++);
2887c478bd9Sstevel@tonic-gate 			s++;
2897c478bd9Sstevel@tonic-gate 			if (s >= len)
2907c478bd9Sstevel@tonic-gate 				break;
2917c478bd9Sstevel@tonic-gate 			for (e = s; str[e] != '\0' && str[e] != ')'; e++);
2927c478bd9Sstevel@tonic-gate 			str[e] = '\0';
2937c478bd9Sstevel@tonic-gate 			new = realloc(comp, (nc+1) * sizeof (comp[nc]));
2947c478bd9Sstevel@tonic-gate 			if (new == 0) {
2957c478bd9Sstevel@tonic-gate 				if (comp != 0) {
2967c478bd9Sstevel@tonic-gate 					for (i = 0; i < nc; i++)
2977c478bd9Sstevel@tonic-gate 						sfree(comp[i]);
2987c478bd9Sstevel@tonic-gate 					free(comp);
2997c478bd9Sstevel@tonic-gate 					comp = 0;
3007c478bd9Sstevel@tonic-gate 				}
3017c478bd9Sstevel@tonic-gate 				nc = 0;
3027c478bd9Sstevel@tonic-gate 				break;
3037c478bd9Sstevel@tonic-gate 			}
3047c478bd9Sstevel@tonic-gate 			comp = new;
3057c478bd9Sstevel@tonic-gate 			comp[nc] = sdup(myself, T, &str[s]);
3067c478bd9Sstevel@tonic-gate 			if (comp[nc] == 0) {
3077c478bd9Sstevel@tonic-gate 				for (i = 0; i < nc; i++)
3087c478bd9Sstevel@tonic-gate 					sfree(comp[i]);
3097c478bd9Sstevel@tonic-gate 				sfree(comp);
3107c478bd9Sstevel@tonic-gate 				comp = 0;
3117c478bd9Sstevel@tonic-gate 				nc = 0;
3127c478bd9Sstevel@tonic-gate 				break;
3137c478bd9Sstevel@tonic-gate 			}
3147c478bd9Sstevel@tonic-gate 			nc++;
3157c478bd9Sstevel@tonic-gate 		}
3167c478bd9Sstevel@tonic-gate 		sfree(str);
3177c478bd9Sstevel@tonic-gate 	}
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	if (numComps != 0)
3207c478bd9Sstevel@tonic-gate 		*numComps = nc;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	return (comp);
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate void
3267c478bd9Sstevel@tonic-gate freeFilterComp(char **comp, int numComps) {
3277c478bd9Sstevel@tonic-gate 	int	i;
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	if (comp == 0)
3307c478bd9Sstevel@tonic-gate 		return;
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	for (i = 0; i < numComps; i++) {
3337c478bd9Sstevel@tonic-gate 		sfree(comp[i]);
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate 	free(comp);
3367c478bd9Sstevel@tonic-gate }
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate char **
3397c478bd9Sstevel@tonic-gate addFilterComp(char *new, char **comp, int *numComps) {
3407c478bd9Sstevel@tonic-gate 	char	**tmp, *str;
3417c478bd9Sstevel@tonic-gate 	char	*myself = "addFilterComp";
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	if (new == 0 || numComps == 0 || *numComps < 0)
3447c478bd9Sstevel@tonic-gate 		return (comp);
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	str = sdup(myself, T, new);
3477c478bd9Sstevel@tonic-gate 	if (str == 0)
3487c478bd9Sstevel@tonic-gate 		return (0);
3497c478bd9Sstevel@tonic-gate 	tmp = realloc(comp, ((*numComps)+1) * sizeof (comp[0]));
3507c478bd9Sstevel@tonic-gate 	if (tmp == 0) {
3517c478bd9Sstevel@tonic-gate 		sfree(str);
3527c478bd9Sstevel@tonic-gate 		return (0);
3537c478bd9Sstevel@tonic-gate 	}
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	comp = tmp;
3567c478bd9Sstevel@tonic-gate 	comp[*numComps] = str;
3577c478bd9Sstevel@tonic-gate 	*numComps += 1;
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	return (comp);
3607c478bd9Sstevel@tonic-gate }
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate char *
3637c478bd9Sstevel@tonic-gate concatenateFilterComps(int numComps, char **comp) {
3647c478bd9Sstevel@tonic-gate 	int		i;
3657c478bd9Sstevel@tonic-gate 	__nis_buffer_t	b = {0, 0};
3667c478bd9Sstevel@tonic-gate 	char		*myself = "concatenateFilterComps";
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	if (numComps == 0 || comp == 0)
3697c478bd9Sstevel@tonic-gate 		return (0);
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	bp2buf(myself, &b, "(&");
3727c478bd9Sstevel@tonic-gate 	for (i = 0; i < numComps; i++) {
3737c478bd9Sstevel@tonic-gate 		if (comp[i] == 0)
3747c478bd9Sstevel@tonic-gate 			continue;
3757c478bd9Sstevel@tonic-gate 		bp2buf(myself, &b, "(%s)", comp[i]);
3767c478bd9Sstevel@tonic-gate 	}
3777c478bd9Sstevel@tonic-gate 	bp2buf(myself, &b, ")");
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	return (b.buf);
3807c478bd9Sstevel@tonic-gate }
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate void
3837c478bd9Sstevel@tonic-gate freeDNs(char **dn, int numDN) {
3847c478bd9Sstevel@tonic-gate 	int	i;
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	if (dn == 0)
3877c478bd9Sstevel@tonic-gate 		return;
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	for (i = 0; i < numDN; i++) {
3907c478bd9Sstevel@tonic-gate 		sfree(dn[i]);
3917c478bd9Sstevel@tonic-gate 	}
3927c478bd9Sstevel@tonic-gate 	sfree(dn);
3937c478bd9Sstevel@tonic-gate }
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate /*
3967c478bd9Sstevel@tonic-gate  * Search the supplied rule-value structure array for any attributes called
3977c478bd9Sstevel@tonic-gate  * "dn", and return their values. If the "dn" value(s) end in a comma, they
3987c478bd9Sstevel@tonic-gate  * get the 'defBase' value appended.
3997c478bd9Sstevel@tonic-gate  */
4007c478bd9Sstevel@tonic-gate char **
4017c478bd9Sstevel@tonic-gate findDNs(char *msg, __nis_rule_value_t *rv, int nrv, char *defBase,
4027c478bd9Sstevel@tonic-gate 		int *numDN) {
4037c478bd9Sstevel@tonic-gate 	char	**dn;
4047c478bd9Sstevel@tonic-gate 	int	irv, iv, ndn;
4057c478bd9Sstevel@tonic-gate 	char	*myself = "findDNs";
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	if (rv == 0 || nrv <= 0 || numDN == 0)
4087c478bd9Sstevel@tonic-gate 		return (0);
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	if (msg == 0)
4117c478bd9Sstevel@tonic-gate 		msg = myself;
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	/* Avoid realloc() by pre-allocating 'dn' at maximum size */
4147c478bd9Sstevel@tonic-gate 	dn = am(msg, nrv * sizeof (dn[0]));
4157c478bd9Sstevel@tonic-gate 	if (dn == 0)
4167c478bd9Sstevel@tonic-gate 		return (0);
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	for (ndn = 0, irv = 0; irv < nrv; irv++) {
4197c478bd9Sstevel@tonic-gate 		for (iv = 0; iv < rv[irv].numAttrs; iv++) {
4207c478bd9Sstevel@tonic-gate 			/* Looking for string-valued attribute called "dn" */
4217c478bd9Sstevel@tonic-gate 			if (rv[irv].attrName[iv] != 0 &&
4227c478bd9Sstevel@tonic-gate 				rv[irv].attrVal[iv].type == vt_string &&
4237c478bd9Sstevel@tonic-gate 				rv[irv].attrVal[iv].numVals >= 1 &&
4247c478bd9Sstevel@tonic-gate 				strcasecmp("dn", rv[irv].attrName[iv]) == 0) {
4257c478bd9Sstevel@tonic-gate 				int	err = 0;
4267c478bd9Sstevel@tonic-gate 				dn[ndn] = appendBase(
4277c478bd9Sstevel@tonic-gate 					rv[irv].attrVal[iv].val[0].value,
4287c478bd9Sstevel@tonic-gate 					defBase, &err, 0);
4297c478bd9Sstevel@tonic-gate 				if (err != 0) {
4307c478bd9Sstevel@tonic-gate 					freeDNs(dn, ndn);
4317c478bd9Sstevel@tonic-gate 					return (0);
4327c478bd9Sstevel@tonic-gate 				}
4337c478bd9Sstevel@tonic-gate 				ndn++;
4347c478bd9Sstevel@tonic-gate 				break;
4357c478bd9Sstevel@tonic-gate 			}
4367c478bd9Sstevel@tonic-gate 		}
4377c478bd9Sstevel@tonic-gate 	}
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	*numDN = ndn;
4407c478bd9Sstevel@tonic-gate 	return (dn);
4417c478bd9Sstevel@tonic-gate }
442