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