xref: /titanic_53/usr/src/lib/libnisdb/ldap_attr.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2001-2003 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
30*7c478bd9Sstevel@tonic-gate #include <strings.h>
31*7c478bd9Sstevel@tonic-gate #include <rpcsvc/nis.h>
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include "nis_parse_ldap_conf.h"
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #include "ldap_attr.h"
36*7c478bd9Sstevel@tonic-gate #include "ldap_util.h"
37*7c478bd9Sstevel@tonic-gate #include "ldap_structs.h"
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate /*
41*7c478bd9Sstevel@tonic-gate  * If 'name' doesn't end in a trailing dot, return a copy with the
42*7c478bd9Sstevel@tonic-gate  * value of "nisplusLDAPbaseDomain" appended. Otherwise, return a
43*7c478bd9Sstevel@tonic-gate  * copy of 'name'. If deallocate!=0, free 'name'.
44*7c478bd9Sstevel@tonic-gate  */
45*7c478bd9Sstevel@tonic-gate char *
46*7c478bd9Sstevel@tonic-gate fullObjName(int deallocate, char *name) {
47*7c478bd9Sstevel@tonic-gate 	int	l;
48*7c478bd9Sstevel@tonic-gate 	char	*full;
49*7c478bd9Sstevel@tonic-gate 	char	*myself = "fullObjName";
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate 	if (name == 0)
52*7c478bd9Sstevel@tonic-gate 		return (sdup(myself, T, proxyInfo.default_nis_domain));
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate 	l = strlen(name);
55*7c478bd9Sstevel@tonic-gate 	if (name[l-1] == '.') {
56*7c478bd9Sstevel@tonic-gate 		full = sdup(myself, T, name);
57*7c478bd9Sstevel@tonic-gate 	} else {
58*7c478bd9Sstevel@tonic-gate 		full = scat(myself, T, scat(myself, F, name, "."),
59*7c478bd9Sstevel@tonic-gate 			sdup(myself, T, proxyInfo.default_nis_domain));
60*7c478bd9Sstevel@tonic-gate 	}
61*7c478bd9Sstevel@tonic-gate 	if (deallocate)
62*7c478bd9Sstevel@tonic-gate 		free(name);
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate 	return (full);
65*7c478bd9Sstevel@tonic-gate }
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate /*
68*7c478bd9Sstevel@tonic-gate  * Convert a domain name ("x.y.z.", say) to a "dc=..." type LDAP equivalent
69*7c478bd9Sstevel@tonic-gate  * ("dc=x,dc=y,dx=z"). The domain name supplied MUST be terminated by a
70*7c478bd9Sstevel@tonic-gate  * trailing dot. If 'domain' is NULL, the value of "nisplusLDAPbaseDomain"
71*7c478bd9Sstevel@tonic-gate  * is converted.
72*7c478bd9Sstevel@tonic-gate  */
73*7c478bd9Sstevel@tonic-gate char *
74*7c478bd9Sstevel@tonic-gate domain2base(char *domain) {
75*7c478bd9Sstevel@tonic-gate 	char	*base = 0;
76*7c478bd9Sstevel@tonic-gate 	int	l, i;
77*7c478bd9Sstevel@tonic-gate 	char	*myself = "domain2base";
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate 	if (domain == 0)
80*7c478bd9Sstevel@tonic-gate 		domain = sdup(myself, T, proxyInfo.default_nis_domain);
81*7c478bd9Sstevel@tonic-gate 	if (domain == 0)
82*7c478bd9Sstevel@tonic-gate 		return (0);
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 	for (l = 0, i = 0; domain[i] != '\0'; i++) {
85*7c478bd9Sstevel@tonic-gate 		if (domain[i] == '.') {
86*7c478bd9Sstevel@tonic-gate 			domain[i] = '\0';
87*7c478bd9Sstevel@tonic-gate 			if (l != 0)
88*7c478bd9Sstevel@tonic-gate 				base = scat(myself, T, base,
89*7c478bd9Sstevel@tonic-gate 					scat(myself, F, ",dc=", &domain[l]));
90*7c478bd9Sstevel@tonic-gate 			else
91*7c478bd9Sstevel@tonic-gate 				base = scat(myself, T, base,
92*7c478bd9Sstevel@tonic-gate 					scat(myself, F, "dc=", &domain[l]));
93*7c478bd9Sstevel@tonic-gate 			l = i+1;
94*7c478bd9Sstevel@tonic-gate 		}
95*7c478bd9Sstevel@tonic-gate 	}
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 	return (base);
98*7c478bd9Sstevel@tonic-gate }
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate /*
101*7c478bd9Sstevel@tonic-gate  * If 'name' ends in a trailing comma, append the value of the
102*7c478bd9Sstevel@tonic-gate  * "defaultSearchBase". If deallocate!=0, free 'name'.
103*7c478bd9Sstevel@tonic-gate  */
104*7c478bd9Sstevel@tonic-gate char *
105*7c478bd9Sstevel@tonic-gate fullLDAPname(int deallocate, char *name) {
106*7c478bd9Sstevel@tonic-gate 	int	err = 0;
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 	return (appendBase(name, proxyInfo.default_search_base, &err,
109*7c478bd9Sstevel@tonic-gate 				deallocate));
110*7c478bd9Sstevel@tonic-gate }
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate /*
113*7c478bd9Sstevel@tonic-gate  * If the 'item' string ends in a comma, append 'base', and return
114*7c478bd9Sstevel@tonic-gate  * the result. On exit, '*err' will be zero if successful, non-zero
115*7c478bd9Sstevel@tonic-gate  * otherwise. If 'dealloc' is non-zero, 'item' is freed; this happens
116*7c478bd9Sstevel@tonic-gate  * even if an error status is returned.
117*7c478bd9Sstevel@tonic-gate  *
118*7c478bd9Sstevel@tonic-gate  * The return value is always allocated, and must be freed by the caller.
119*7c478bd9Sstevel@tonic-gate  */
120*7c478bd9Sstevel@tonic-gate char *
121*7c478bd9Sstevel@tonic-gate appendBase(char *item, char *base, int *err, int dealloc) {
122*7c478bd9Sstevel@tonic-gate 	char	*new;
123*7c478bd9Sstevel@tonic-gate 	int	len, deferr;
124*7c478bd9Sstevel@tonic-gate 	char	*myself = "appendBase";
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	/*
127*7c478bd9Sstevel@tonic-gate 	 * Make sure that 'err' points to something valid, so that we can
128*7c478bd9Sstevel@tonic-gate 	 * dispense with all those 'if (err != 0)'.
129*7c478bd9Sstevel@tonic-gate 	 */
130*7c478bd9Sstevel@tonic-gate 	if (err == 0)
131*7c478bd9Sstevel@tonic-gate 		err = &deferr;
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	/* Establish default (successful) error status */
134*7c478bd9Sstevel@tonic-gate 	*err = 0;
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 	/* Trivial case 1: If 'item' is NULL, return a copy of 'base' */
137*7c478bd9Sstevel@tonic-gate 	if (item == 0) {
138*7c478bd9Sstevel@tonic-gate 		new = sdup(myself, T, base);
139*7c478bd9Sstevel@tonic-gate 		if (new == 0)
140*7c478bd9Sstevel@tonic-gate 			*err = -1;
141*7c478bd9Sstevel@tonic-gate 		return (new);
142*7c478bd9Sstevel@tonic-gate 	}
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate 	/* Trivial case 2: If 'base' is NULL, return a copy of 'item' */
145*7c478bd9Sstevel@tonic-gate 	if (base == 0) {
146*7c478bd9Sstevel@tonic-gate 		new = sdup(myself, T, item);
147*7c478bd9Sstevel@tonic-gate 		if (new == 0)
148*7c478bd9Sstevel@tonic-gate 			*err = -1;
149*7c478bd9Sstevel@tonic-gate 		if (dealloc)
150*7c478bd9Sstevel@tonic-gate 			free(item);
151*7c478bd9Sstevel@tonic-gate 		return (new);
152*7c478bd9Sstevel@tonic-gate 	}
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate 	len = strlen(item);
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	/* If 'item' is the empty string, return a copy of 'base' */
157*7c478bd9Sstevel@tonic-gate 	if (len <= 0) {
158*7c478bd9Sstevel@tonic-gate 		new = sdup(myself, T, base);
159*7c478bd9Sstevel@tonic-gate 		if (new == 0)
160*7c478bd9Sstevel@tonic-gate 			*err = -1;
161*7c478bd9Sstevel@tonic-gate 		if (dealloc)
162*7c478bd9Sstevel@tonic-gate 			free(item);
163*7c478bd9Sstevel@tonic-gate 		return (new);
164*7c478bd9Sstevel@tonic-gate 	}
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate 	/*
167*7c478bd9Sstevel@tonic-gate 	 * If 'item' ends in a comma, append 'base', and return a copy
168*7c478bd9Sstevel@tonic-gate 	 * of the result. Otherwise, return a copy of 'item'.
169*7c478bd9Sstevel@tonic-gate 	 */
170*7c478bd9Sstevel@tonic-gate 	if (item[len-1] == ',') {
171*7c478bd9Sstevel@tonic-gate 		int	blen = slen(base);
172*7c478bd9Sstevel@tonic-gate 		new = am(myself, len + blen + 1);
173*7c478bd9Sstevel@tonic-gate 		if (new != 0) {
174*7c478bd9Sstevel@tonic-gate 			(void) memcpy(new, item, len);
175*7c478bd9Sstevel@tonic-gate 			(void) memcpy(&new[len], base, blen);
176*7c478bd9Sstevel@tonic-gate 		} else {
177*7c478bd9Sstevel@tonic-gate 			*err = -1;
178*7c478bd9Sstevel@tonic-gate 		}
179*7c478bd9Sstevel@tonic-gate 	} else {
180*7c478bd9Sstevel@tonic-gate 		new = sdup(myself, T, item);
181*7c478bd9Sstevel@tonic-gate 		if (new == 0)
182*7c478bd9Sstevel@tonic-gate 			*err = -1;
183*7c478bd9Sstevel@tonic-gate 	}
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate 	if (dealloc)
186*7c478bd9Sstevel@tonic-gate 		free(item);
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 	return (new);
189*7c478bd9Sstevel@tonic-gate }
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate /*
192*7c478bd9Sstevel@tonic-gate  * Despite its general-sounding name, this function only knows how to
193*7c478bd9Sstevel@tonic-gate  * turn a list of attributes ("a,b,c") into an AND filter ("(&(a)(b)(c))").
194*7c478bd9Sstevel@tonic-gate  */
195*7c478bd9Sstevel@tonic-gate char *
196*7c478bd9Sstevel@tonic-gate makeFilter(char *attr) {
197*7c478bd9Sstevel@tonic-gate 	int	len, s, e, c;
198*7c478bd9Sstevel@tonic-gate 	char	*str, *filter, *tmp;
199*7c478bd9Sstevel@tonic-gate 	char	*myself = "makeFilter";
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate 	if (attr == 0 || (len = strlen(attr)) == 0)
202*7c478bd9Sstevel@tonic-gate 		return (0);
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate 	/* Assume already of appropriate form if first char is '(' */
205*7c478bd9Sstevel@tonic-gate 	if (len > 1 && attr[0] == '(' && attr[len-1] == ')')
206*7c478bd9Sstevel@tonic-gate 		return (sdup(myself, T, attr));
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 	str = sdup(myself, T, attr);
209*7c478bd9Sstevel@tonic-gate 	if (str == 0)
210*7c478bd9Sstevel@tonic-gate 		return (0);
211*7c478bd9Sstevel@tonic-gate 	filter = sdup(myself, T, "(&");
212*7c478bd9Sstevel@tonic-gate 	if (filter == 0) {
213*7c478bd9Sstevel@tonic-gate 		free(str);
214*7c478bd9Sstevel@tonic-gate 		return (0);
215*7c478bd9Sstevel@tonic-gate 	}
216*7c478bd9Sstevel@tonic-gate 	for (s = c = 0; s < len; s = e+1) {
217*7c478bd9Sstevel@tonic-gate 		/* Skip blank space, if any */
218*7c478bd9Sstevel@tonic-gate 		for (0; str[s] == ' ' || str[s] == '\t'; s++);
219*7c478bd9Sstevel@tonic-gate 		/* Find delimiter (comma) or end of string */
220*7c478bd9Sstevel@tonic-gate 		for (e = s; str[e] != '\0' && str[e] != ','; e++);
221*7c478bd9Sstevel@tonic-gate 		str[e] = '\0';
222*7c478bd9Sstevel@tonic-gate 		tmp = scat(myself, T, sdup(myself, T, "("),
223*7c478bd9Sstevel@tonic-gate 			scat(myself, F, &str[s], ")"));
224*7c478bd9Sstevel@tonic-gate 		if (tmp == 0) {
225*7c478bd9Sstevel@tonic-gate 			sfree(filter);
226*7c478bd9Sstevel@tonic-gate 			return (0);
227*7c478bd9Sstevel@tonic-gate 		}
228*7c478bd9Sstevel@tonic-gate 		c++;
229*7c478bd9Sstevel@tonic-gate 		filter = scat(myself, T, filter, tmp);
230*7c478bd9Sstevel@tonic-gate 	}
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate 	/*
233*7c478bd9Sstevel@tonic-gate 	 * If there's just one component, we return it as is. This
234*7c478bd9Sstevel@tonic-gate 	 * means we avoid turning "objectClass=posixAccount" into
235*7c478bd9Sstevel@tonic-gate 	 * "(&(objectClass=posixAccount))".
236*7c478bd9Sstevel@tonic-gate 	 */
237*7c478bd9Sstevel@tonic-gate 	if (c == 1) {
238*7c478bd9Sstevel@tonic-gate 		sfree(filter);
239*7c478bd9Sstevel@tonic-gate 		return (str);
240*7c478bd9Sstevel@tonic-gate 	}
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate 	/* Add the closing ')' */
243*7c478bd9Sstevel@tonic-gate 	tmp = filter;
244*7c478bd9Sstevel@tonic-gate 	filter = scat(myself, F, tmp, ")");
245*7c478bd9Sstevel@tonic-gate 	sfree(tmp);
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate 	free(str);
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate 	return (filter);
250*7c478bd9Sstevel@tonic-gate }
251*7c478bd9Sstevel@tonic-gate 
252*7c478bd9Sstevel@tonic-gate /*
253*7c478bd9Sstevel@tonic-gate  * Split an AND-filter string into components.
254*7c478bd9Sstevel@tonic-gate  */
255*7c478bd9Sstevel@tonic-gate char **
256*7c478bd9Sstevel@tonic-gate makeFilterComp(char *filter, int *numComps) {
257*7c478bd9Sstevel@tonic-gate 	int	nc = 0, s, e, i;
258*7c478bd9Sstevel@tonic-gate 	char	**comp = 0, **new, *str;
259*7c478bd9Sstevel@tonic-gate 	int	len;
260*7c478bd9Sstevel@tonic-gate 	char	*myself = "makeFilterComp";
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate 	if ((len = slen(filter)) <= 0)
263*7c478bd9Sstevel@tonic-gate 		return (0);
264*7c478bd9Sstevel@tonic-gate 
265*7c478bd9Sstevel@tonic-gate 	/* Is it just a plain "attr=val" string ? If so, return a copy */
266*7c478bd9Sstevel@tonic-gate 	if (len <= 2 || filter[0] != '(') {
267*7c478bd9Sstevel@tonic-gate 		comp = am(myself, 2 * sizeof (comp[0]));
268*7c478bd9Sstevel@tonic-gate 		if (comp == 0)
269*7c478bd9Sstevel@tonic-gate 			return (0);
270*7c478bd9Sstevel@tonic-gate 		comp[0] = sdup(myself, T, filter);
271*7c478bd9Sstevel@tonic-gate 		if (comp[0] == 0) {
272*7c478bd9Sstevel@tonic-gate 			sfree(comp);
273*7c478bd9Sstevel@tonic-gate 			return (0);
274*7c478bd9Sstevel@tonic-gate 		}
275*7c478bd9Sstevel@tonic-gate 		if (numComps != 0)
276*7c478bd9Sstevel@tonic-gate 			*numComps = 1;
277*7c478bd9Sstevel@tonic-gate 		return (comp);
278*7c478bd9Sstevel@tonic-gate 	}
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate 	if (filter != 0 && (len = strlen(filter)) != 0 && len > 2 &&
281*7c478bd9Sstevel@tonic-gate 			filter[0] == '(' && filter[1] == '&' &&
282*7c478bd9Sstevel@tonic-gate 			filter[len-1] == ')') {
283*7c478bd9Sstevel@tonic-gate 		str = sdup(myself, T, filter);
284*7c478bd9Sstevel@tonic-gate 		if (str == 0)
285*7c478bd9Sstevel@tonic-gate 			return (0);
286*7c478bd9Sstevel@tonic-gate 		for (s = 2; s < len; s = e+1) {
287*7c478bd9Sstevel@tonic-gate 			/* Skip past the '(' */
288*7c478bd9Sstevel@tonic-gate 			for (0; s < len && str[s] != '('; s++);
289*7c478bd9Sstevel@tonic-gate 			s++;
290*7c478bd9Sstevel@tonic-gate 			if (s >= len)
291*7c478bd9Sstevel@tonic-gate 				break;
292*7c478bd9Sstevel@tonic-gate 			for (e = s; str[e] != '\0' && str[e] != ')'; e++);
293*7c478bd9Sstevel@tonic-gate 			str[e] = '\0';
294*7c478bd9Sstevel@tonic-gate 			new = realloc(comp, (nc+1) * sizeof (comp[nc]));
295*7c478bd9Sstevel@tonic-gate 			if (new == 0) {
296*7c478bd9Sstevel@tonic-gate 				if (comp != 0) {
297*7c478bd9Sstevel@tonic-gate 					for (i = 0; i < nc; i++)
298*7c478bd9Sstevel@tonic-gate 						sfree(comp[i]);
299*7c478bd9Sstevel@tonic-gate 					free(comp);
300*7c478bd9Sstevel@tonic-gate 					comp = 0;
301*7c478bd9Sstevel@tonic-gate 				}
302*7c478bd9Sstevel@tonic-gate 				nc = 0;
303*7c478bd9Sstevel@tonic-gate 				break;
304*7c478bd9Sstevel@tonic-gate 			}
305*7c478bd9Sstevel@tonic-gate 			comp = new;
306*7c478bd9Sstevel@tonic-gate 			comp[nc] = sdup(myself, T, &str[s]);
307*7c478bd9Sstevel@tonic-gate 			if (comp[nc] == 0) {
308*7c478bd9Sstevel@tonic-gate 				for (i = 0; i < nc; i++)
309*7c478bd9Sstevel@tonic-gate 					sfree(comp[i]);
310*7c478bd9Sstevel@tonic-gate 				sfree(comp);
311*7c478bd9Sstevel@tonic-gate 				comp = 0;
312*7c478bd9Sstevel@tonic-gate 				nc = 0;
313*7c478bd9Sstevel@tonic-gate 				break;
314*7c478bd9Sstevel@tonic-gate 			}
315*7c478bd9Sstevel@tonic-gate 			nc++;
316*7c478bd9Sstevel@tonic-gate 		}
317*7c478bd9Sstevel@tonic-gate 		sfree(str);
318*7c478bd9Sstevel@tonic-gate 	}
319*7c478bd9Sstevel@tonic-gate 
320*7c478bd9Sstevel@tonic-gate 	if (numComps != 0)
321*7c478bd9Sstevel@tonic-gate 		*numComps = nc;
322*7c478bd9Sstevel@tonic-gate 
323*7c478bd9Sstevel@tonic-gate 	return (comp);
324*7c478bd9Sstevel@tonic-gate }
325*7c478bd9Sstevel@tonic-gate 
326*7c478bd9Sstevel@tonic-gate void
327*7c478bd9Sstevel@tonic-gate freeFilterComp(char **comp, int numComps) {
328*7c478bd9Sstevel@tonic-gate 	int	i;
329*7c478bd9Sstevel@tonic-gate 
330*7c478bd9Sstevel@tonic-gate 	if (comp == 0)
331*7c478bd9Sstevel@tonic-gate 		return;
332*7c478bd9Sstevel@tonic-gate 
333*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < numComps; i++) {
334*7c478bd9Sstevel@tonic-gate 		sfree(comp[i]);
335*7c478bd9Sstevel@tonic-gate 	}
336*7c478bd9Sstevel@tonic-gate 	free(comp);
337*7c478bd9Sstevel@tonic-gate }
338*7c478bd9Sstevel@tonic-gate 
339*7c478bd9Sstevel@tonic-gate char **
340*7c478bd9Sstevel@tonic-gate addFilterComp(char *new, char **comp, int *numComps) {
341*7c478bd9Sstevel@tonic-gate 	char	**tmp, *str;
342*7c478bd9Sstevel@tonic-gate 	char	*myself = "addFilterComp";
343*7c478bd9Sstevel@tonic-gate 
344*7c478bd9Sstevel@tonic-gate 	if (new == 0 || numComps == 0 || *numComps < 0)
345*7c478bd9Sstevel@tonic-gate 		return (comp);
346*7c478bd9Sstevel@tonic-gate 
347*7c478bd9Sstevel@tonic-gate 	str = sdup(myself, T, new);
348*7c478bd9Sstevel@tonic-gate 	if (str == 0)
349*7c478bd9Sstevel@tonic-gate 		return (0);
350*7c478bd9Sstevel@tonic-gate 	tmp = realloc(comp, ((*numComps)+1) * sizeof (comp[0]));
351*7c478bd9Sstevel@tonic-gate 	if (tmp == 0) {
352*7c478bd9Sstevel@tonic-gate 		sfree(str);
353*7c478bd9Sstevel@tonic-gate 		return (0);
354*7c478bd9Sstevel@tonic-gate 	}
355*7c478bd9Sstevel@tonic-gate 
356*7c478bd9Sstevel@tonic-gate 	comp = tmp;
357*7c478bd9Sstevel@tonic-gate 	comp[*numComps] = str;
358*7c478bd9Sstevel@tonic-gate 	*numComps += 1;
359*7c478bd9Sstevel@tonic-gate 
360*7c478bd9Sstevel@tonic-gate 	return (comp);
361*7c478bd9Sstevel@tonic-gate }
362*7c478bd9Sstevel@tonic-gate 
363*7c478bd9Sstevel@tonic-gate char *
364*7c478bd9Sstevel@tonic-gate concatenateFilterComps(int numComps, char **comp) {
365*7c478bd9Sstevel@tonic-gate 	int		i;
366*7c478bd9Sstevel@tonic-gate 	__nis_buffer_t	b = {0, 0};
367*7c478bd9Sstevel@tonic-gate 	char		*myself = "concatenateFilterComps";
368*7c478bd9Sstevel@tonic-gate 
369*7c478bd9Sstevel@tonic-gate 	if (numComps == 0 || comp == 0)
370*7c478bd9Sstevel@tonic-gate 		return (0);
371*7c478bd9Sstevel@tonic-gate 
372*7c478bd9Sstevel@tonic-gate 	bp2buf(myself, &b, "(&");
373*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < numComps; i++) {
374*7c478bd9Sstevel@tonic-gate 		if (comp[i] == 0)
375*7c478bd9Sstevel@tonic-gate 			continue;
376*7c478bd9Sstevel@tonic-gate 		bp2buf(myself, &b, "(%s)", comp[i]);
377*7c478bd9Sstevel@tonic-gate 	}
378*7c478bd9Sstevel@tonic-gate 	bp2buf(myself, &b, ")");
379*7c478bd9Sstevel@tonic-gate 
380*7c478bd9Sstevel@tonic-gate 	return (b.buf);
381*7c478bd9Sstevel@tonic-gate }
382*7c478bd9Sstevel@tonic-gate 
383*7c478bd9Sstevel@tonic-gate void
384*7c478bd9Sstevel@tonic-gate freeDNs(char **dn, int numDN) {
385*7c478bd9Sstevel@tonic-gate 	int	i;
386*7c478bd9Sstevel@tonic-gate 
387*7c478bd9Sstevel@tonic-gate 	if (dn == 0)
388*7c478bd9Sstevel@tonic-gate 		return;
389*7c478bd9Sstevel@tonic-gate 
390*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < numDN; i++) {
391*7c478bd9Sstevel@tonic-gate 		sfree(dn[i]);
392*7c478bd9Sstevel@tonic-gate 	}
393*7c478bd9Sstevel@tonic-gate 	sfree(dn);
394*7c478bd9Sstevel@tonic-gate }
395*7c478bd9Sstevel@tonic-gate 
396*7c478bd9Sstevel@tonic-gate /*
397*7c478bd9Sstevel@tonic-gate  * Search the supplied rule-value structure array for any attributes called
398*7c478bd9Sstevel@tonic-gate  * "dn", and return their values. If the "dn" value(s) end in a comma, they
399*7c478bd9Sstevel@tonic-gate  * get the 'defBase' value appended.
400*7c478bd9Sstevel@tonic-gate  */
401*7c478bd9Sstevel@tonic-gate char **
402*7c478bd9Sstevel@tonic-gate findDNs(char *msg, __nis_rule_value_t *rv, int nrv, char *defBase,
403*7c478bd9Sstevel@tonic-gate 		int *numDN) {
404*7c478bd9Sstevel@tonic-gate 	char	**dn;
405*7c478bd9Sstevel@tonic-gate 	int	irv, iv, ndn;
406*7c478bd9Sstevel@tonic-gate 	char	*myself = "findDNs";
407*7c478bd9Sstevel@tonic-gate 
408*7c478bd9Sstevel@tonic-gate 	if (rv == 0 || nrv <= 0 || numDN == 0)
409*7c478bd9Sstevel@tonic-gate 		return (0);
410*7c478bd9Sstevel@tonic-gate 
411*7c478bd9Sstevel@tonic-gate 	if (msg == 0)
412*7c478bd9Sstevel@tonic-gate 		msg = myself;
413*7c478bd9Sstevel@tonic-gate 
414*7c478bd9Sstevel@tonic-gate 	/* Avoid realloc() by pre-allocating 'dn' at maximum size */
415*7c478bd9Sstevel@tonic-gate 	dn = am(msg, nrv * sizeof (dn[0]));
416*7c478bd9Sstevel@tonic-gate 	if (dn == 0)
417*7c478bd9Sstevel@tonic-gate 		return (0);
418*7c478bd9Sstevel@tonic-gate 
419*7c478bd9Sstevel@tonic-gate 	for (ndn = 0, irv = 0; irv < nrv; irv++) {
420*7c478bd9Sstevel@tonic-gate 		for (iv = 0; iv < rv[irv].numAttrs; iv++) {
421*7c478bd9Sstevel@tonic-gate 			/* Looking for string-valued attribute called "dn" */
422*7c478bd9Sstevel@tonic-gate 			if (rv[irv].attrName[iv] != 0 &&
423*7c478bd9Sstevel@tonic-gate 				rv[irv].attrVal[iv].type == vt_string &&
424*7c478bd9Sstevel@tonic-gate 				rv[irv].attrVal[iv].numVals >= 1 &&
425*7c478bd9Sstevel@tonic-gate 				strcasecmp("dn", rv[irv].attrName[iv]) == 0) {
426*7c478bd9Sstevel@tonic-gate 				int	err = 0;
427*7c478bd9Sstevel@tonic-gate 				dn[ndn] = appendBase(
428*7c478bd9Sstevel@tonic-gate 					rv[irv].attrVal[iv].val[0].value,
429*7c478bd9Sstevel@tonic-gate 					defBase, &err, 0);
430*7c478bd9Sstevel@tonic-gate 				if (err != 0) {
431*7c478bd9Sstevel@tonic-gate 					freeDNs(dn, ndn);
432*7c478bd9Sstevel@tonic-gate 					return (0);
433*7c478bd9Sstevel@tonic-gate 				}
434*7c478bd9Sstevel@tonic-gate 				ndn++;
435*7c478bd9Sstevel@tonic-gate 				break;
436*7c478bd9Sstevel@tonic-gate 			}
437*7c478bd9Sstevel@tonic-gate 		}
438*7c478bd9Sstevel@tonic-gate 	}
439*7c478bd9Sstevel@tonic-gate 
440*7c478bd9Sstevel@tonic-gate 	*numDN = ndn;
441*7c478bd9Sstevel@tonic-gate 	return (dn);
442*7c478bd9Sstevel@tonic-gate }
443