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 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 <ctype.h>
30*0Sstevel@tonic-gate #include <libintl.h>
31*0Sstevel@tonic-gate #include <strings.h>
32*0Sstevel@tonic-gate #include <stdio.h>
33*0Sstevel@tonic-gate #include "../../../lib/libsldap/common/ns_sldap.h"
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #define	MAXLINE	2000
37*0Sstevel@tonic-gate #define	SAME	0
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate struct mapping {
40*0Sstevel@tonic-gate 	char *database;
41*0Sstevel@tonic-gate 	char *def_type;
42*0Sstevel@tonic-gate 	char *objectclass;
43*0Sstevel@tonic-gate 	char *actual_db;
44*0Sstevel@tonic-gate };
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate #define	PUBLICKEY	0
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate static struct mapping maplist[] = {
49*0Sstevel@tonic-gate 	{"publickey", "uidnumber", "niskeyobject", "passwd"},
50*0Sstevel@tonic-gate 	{"publickey", "cn", "niskeyobject", "host"},
51*0Sstevel@tonic-gate 	{"bootparams", "cn", "bootableDevice", NULL},
52*0Sstevel@tonic-gate 	{"ethers", "cn", "ieee802Device", NULL},
53*0Sstevel@tonic-gate 	{"group", "cn", "posixgroup", NULL},
54*0Sstevel@tonic-gate 	{"hosts", "cn", "iphost", NULL},
55*0Sstevel@tonic-gate 	{"ipnodes", "cn", "iphost", NULL},
56*0Sstevel@tonic-gate 	{"netgroup", "cn", "nisnetgroup", NULL},
57*0Sstevel@tonic-gate 	{"netmasks", "ipnetworknumber", "ipnetwork", NULL},
58*0Sstevel@tonic-gate 	{"networks", "ipnetworknumber", "ipnetwork", NULL},
59*0Sstevel@tonic-gate 	{"passwd", "uid", "posixaccount", NULL},
60*0Sstevel@tonic-gate 	{"protocols", "cn", "ipprotocol", NULL},
61*0Sstevel@tonic-gate 	{"rpc", "cn", "oncrpc", NULL},
62*0Sstevel@tonic-gate 	{"services", "cn", "ipservice", NULL},
63*0Sstevel@tonic-gate 	{"aliases", "cn", "mailGroup", NULL},
64*0Sstevel@tonic-gate 	{"project", "SolarisProjectID", "SolarisProject", NULL},
65*0Sstevel@tonic-gate 	{"printers", "printer-uri", "sunPrinter", NULL},
66*0Sstevel@tonic-gate 	{"shadow", "uid", "shadowaccount", NULL},
67*0Sstevel@tonic-gate 	{"auth_attr", "cn", "SolarisAuthAttr", NULL},
68*0Sstevel@tonic-gate 	{"prof_attr", "cn", "SolarisProfAttr", NULL},
69*0Sstevel@tonic-gate 	{"exec_attr", "cn", "SolarisExecAttr", NULL},
70*0Sstevel@tonic-gate 	{"user_attr", "uid", "SolarisUserAttr", NULL},
71*0Sstevel@tonic-gate 	{"audit_user", "uid", "SolarisAuditUser", NULL},
72*0Sstevel@tonic-gate 	{NULL, NULL, NULL, NULL}
73*0Sstevel@tonic-gate };
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate void
77*0Sstevel@tonic-gate printMapping()
78*0Sstevel@tonic-gate {
79*0Sstevel@tonic-gate 	int	i;
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate 	fprintf(stdout,
82*0Sstevel@tonic-gate 		gettext("database       default type        objectclass\n"));
83*0Sstevel@tonic-gate 	fprintf(stdout,
84*0Sstevel@tonic-gate 		gettext("=============  =================   =============\n"));
85*0Sstevel@tonic-gate 	/* first dump auto_* and automount which are not in maplist[] */
86*0Sstevel@tonic-gate 	fprintf(stdout, "%-15s%-20s%s\n", "auto_*", "automountKey",
87*0Sstevel@tonic-gate 		"automount");
88*0Sstevel@tonic-gate 	fprintf(stdout, "%-15s%-20s%s\n", "automount", "automountMapName",
89*0Sstevel@tonic-gate 		"automountMap");
90*0Sstevel@tonic-gate 	for (i = 0; maplist[i].database != NULL; i++) {
91*0Sstevel@tonic-gate 	/* skip printing shadow */
92*0Sstevel@tonic-gate 	if (strcasecmp(maplist[i].database, "shadow") != 0)
93*0Sstevel@tonic-gate 		fprintf(stdout, "%-15s%-20s%s\n", maplist[i].database,
94*0Sstevel@tonic-gate 			maplist[i].def_type, maplist[i].objectclass);
95*0Sstevel@tonic-gate 	}
96*0Sstevel@tonic-gate }
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate char *
100*0Sstevel@tonic-gate set_keys(char **key, char *attrtype)
101*0Sstevel@tonic-gate {
102*0Sstevel@tonic-gate 	char	*keyeq = NULL;
103*0Sstevel@tonic-gate 	static char	keyfilter[MAXLINE];
104*0Sstevel@tonic-gate 	char	typeeq[100];
105*0Sstevel@tonic-gate 	char	buf[100];
106*0Sstevel@tonic-gate 	char	*k, **karray;
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate 	if (!key || !key[0])	/* should never contain NULL string */
109*0Sstevel@tonic-gate 		return (NULL);
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	if (attrtype) {
112*0Sstevel@tonic-gate 		strcpy(typeeq, attrtype);
113*0Sstevel@tonic-gate 		strcat(typeeq, "=");
114*0Sstevel@tonic-gate 	}
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 	keyfilter[0] = '\0';
117*0Sstevel@tonic-gate 	if (key[1])
118*0Sstevel@tonic-gate 		strcat(keyfilter, "(|");
119*0Sstevel@tonic-gate 	karray = key;
120*0Sstevel@tonic-gate 	while (k = *karray) {
121*0Sstevel@tonic-gate 		keyeq = strchr(k, '=');
122*0Sstevel@tonic-gate 		sprintf(buf, "(%s%s)", (keyeq ? "" : typeeq), k);
123*0Sstevel@tonic-gate 		if (strlen(buf) + strlen(keyfilter) >= MAXLINE) {
124*0Sstevel@tonic-gate 			fprintf(stdout,
125*0Sstevel@tonic-gate 				gettext("***ERROR: ldapfilter too long\n"));
126*0Sstevel@tonic-gate 			exit(2);
127*0Sstevel@tonic-gate 		}
128*0Sstevel@tonic-gate 		strcat(keyfilter, buf);
129*0Sstevel@tonic-gate 		karray++;
130*0Sstevel@tonic-gate 	}
131*0Sstevel@tonic-gate 	if (key[1])
132*0Sstevel@tonic-gate 		strcat(keyfilter, ")");
133*0Sstevel@tonic-gate 	return (keyfilter);
134*0Sstevel@tonic-gate }
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate /*
138*0Sstevel@tonic-gate  * A special set_key routine for to handle public keys.
139*0Sstevel@tonic-gate  * If the key starts with a digiti, view it as a user id.
140*0Sstevel@tonic-gate  * Otherwise, view it as a hostname.
141*0Sstevel@tonic-gate  * It returns: -1 no keys defined, 0 key defined but none for type
142*0Sstevel@tonic-gate  *		specified, n>0 number of matches found.
143*0Sstevel@tonic-gate  */
144*0Sstevel@tonic-gate int
145*0Sstevel@tonic-gate set_keys_publickey(char **key, char *attrtype, int type, char **ret)
146*0Sstevel@tonic-gate {
147*0Sstevel@tonic-gate 	char	*keyeq = NULL;
148*0Sstevel@tonic-gate 	static char	keyfilter[MAXLINE];
149*0Sstevel@tonic-gate 	char	pre_filter[MAXLINE];
150*0Sstevel@tonic-gate 	char	buf[100];
151*0Sstevel@tonic-gate 	char	*k, **karray;
152*0Sstevel@tonic-gate 	int	count = 0;
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 	if (!key || !key[0]) {	/* should never contain NULL string */
155*0Sstevel@tonic-gate 		*ret = NULL;
156*0Sstevel@tonic-gate 		return (-1);
157*0Sstevel@tonic-gate 	}
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 	keyfilter[0] = '\0';
160*0Sstevel@tonic-gate 	pre_filter[0] = '\0';
161*0Sstevel@tonic-gate 	karray = key;
162*0Sstevel@tonic-gate 	while (k = *karray) {
163*0Sstevel@tonic-gate 		keyeq = strchr(k, '=');
164*0Sstevel@tonic-gate 		if (keyeq)
165*0Sstevel@tonic-gate 			sprintf(buf, "(%s)", k);
166*0Sstevel@tonic-gate 		else {
167*0Sstevel@tonic-gate 			if (type == 0 && isdigit(*k)) {
168*0Sstevel@tonic-gate 				/* user type keys */
169*0Sstevel@tonic-gate 				sprintf(buf, "(%s=%s)", attrtype, k);
170*0Sstevel@tonic-gate 			} else if (type == 1 && (!isdigit(*k))) {
171*0Sstevel@tonic-gate 				/* hosts type keys */
172*0Sstevel@tonic-gate 				sprintf(buf, "(%s=%s)", attrtype, k);
173*0Sstevel@tonic-gate 			} else {
174*0Sstevel@tonic-gate 				karray++;
175*0Sstevel@tonic-gate 				continue;
176*0Sstevel@tonic-gate 			}
177*0Sstevel@tonic-gate 		}
178*0Sstevel@tonic-gate 		if (strlen(buf) + strlen(pre_filter) >= MAXLINE) {
179*0Sstevel@tonic-gate 			fprintf(stdout,
180*0Sstevel@tonic-gate 				gettext("***ERROR: ldapfilter too long\n"));
181*0Sstevel@tonic-gate 			exit(2);
182*0Sstevel@tonic-gate 		}
183*0Sstevel@tonic-gate 		strcat(pre_filter, buf);
184*0Sstevel@tonic-gate 		karray++;
185*0Sstevel@tonic-gate 		count++;
186*0Sstevel@tonic-gate 	}
187*0Sstevel@tonic-gate 	if (count > 1) {
188*0Sstevel@tonic-gate 		if (strlen(pre_filter) + 4 >= MAXLINE) {
189*0Sstevel@tonic-gate 			fprintf(stdout,
190*0Sstevel@tonic-gate 				gettext("***ERROR: ldapfilter too long\n"));
191*0Sstevel@tonic-gate 			exit(2);
192*0Sstevel@tonic-gate 		}
193*0Sstevel@tonic-gate 		strcat(keyfilter, "(|");
194*0Sstevel@tonic-gate 		strcat(keyfilter, pre_filter);
195*0Sstevel@tonic-gate 		strcat(keyfilter, ")");
196*0Sstevel@tonic-gate 		*ret = keyfilter;
197*0Sstevel@tonic-gate 	} else
198*0Sstevel@tonic-gate 		*ret = pre_filter;
199*0Sstevel@tonic-gate 	return (count);
200*0Sstevel@tonic-gate }
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate /*
203*0Sstevel@tonic-gate  * publickey specific set_filter
204*0Sstevel@tonic-gate  * type 0 -> check for user publickeys
205*0Sstevel@tonic-gate  * type 1 -> check for hosts publickeys
206*0Sstevel@tonic-gate  */
207*0Sstevel@tonic-gate char *
208*0Sstevel@tonic-gate set_filter_publickey(char **key, char *database, int type, char **udata)
209*0Sstevel@tonic-gate {
210*0Sstevel@tonic-gate 	char 	*filter;
211*0Sstevel@tonic-gate 	char 	*userdata;
212*0Sstevel@tonic-gate 	char	*keyfilter;
213*0Sstevel@tonic-gate 	int	rc;
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate 	if (!database) {
216*0Sstevel@tonic-gate 		return (NULL);
217*0Sstevel@tonic-gate 	}
218*0Sstevel@tonic-gate 	if (!udata) {
219*0Sstevel@tonic-gate 		return (NULL);
220*0Sstevel@tonic-gate 	}
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 	filter = (char *)malloc(MAXLINE);
223*0Sstevel@tonic-gate 	if (!filter) {
224*0Sstevel@tonic-gate 		return (NULL);
225*0Sstevel@tonic-gate 	}
226*0Sstevel@tonic-gate 	filter[0] = '\0';
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate 	userdata = (char *)malloc(MAXLINE);
229*0Sstevel@tonic-gate 	if (!userdata) {
230*0Sstevel@tonic-gate 		free(filter);
231*0Sstevel@tonic-gate 		return (NULL);
232*0Sstevel@tonic-gate 	}
233*0Sstevel@tonic-gate 	userdata[0] = '\0';
234*0Sstevel@tonic-gate 	*udata = userdata;
235*0Sstevel@tonic-gate 
236*0Sstevel@tonic-gate 	if (strcasecmp(database, maplist[PUBLICKEY].database) == SAME) {
237*0Sstevel@tonic-gate 		rc = set_keys_publickey(key,
238*0Sstevel@tonic-gate 				maplist[PUBLICKEY + type].def_type, type,
239*0Sstevel@tonic-gate 				&keyfilter);
240*0Sstevel@tonic-gate 		switch (rc) {
241*0Sstevel@tonic-gate 		case -1:
242*0Sstevel@tonic-gate 			sprintf(filter, "objectclass=%s",
243*0Sstevel@tonic-gate 				maplist[PUBLICKEY].objectclass);
244*0Sstevel@tonic-gate 			sprintf(userdata, "%%s");
245*0Sstevel@tonic-gate 			break;
246*0Sstevel@tonic-gate 		case 0:
247*0Sstevel@tonic-gate 			return (NULL);
248*0Sstevel@tonic-gate 		default:
249*0Sstevel@tonic-gate 			sprintf(filter, "(&(objectclass=%s)%s)",
250*0Sstevel@tonic-gate 				maplist[PUBLICKEY].objectclass, keyfilter);
251*0Sstevel@tonic-gate 			sprintf(userdata, "(&(%%s)%s)",
252*0Sstevel@tonic-gate 				keyfilter);
253*0Sstevel@tonic-gate 		}
254*0Sstevel@tonic-gate 	} else {
255*0Sstevel@tonic-gate 		if ((keyfilter = set_keys(key, "cn")) == NULL) {
256*0Sstevel@tonic-gate 			sprintf(filter, "objectclass=*");
257*0Sstevel@tonic-gate 			sprintf(userdata, "%%s");
258*0Sstevel@tonic-gate 		} else {
259*0Sstevel@tonic-gate 			sprintf(filter, "%s", keyfilter);
260*0Sstevel@tonic-gate 			sprintf(userdata, "(&(%%s)%s)", keyfilter);
261*0Sstevel@tonic-gate 		}
262*0Sstevel@tonic-gate 	}
263*0Sstevel@tonic-gate #ifdef DEBUG
264*0Sstevel@tonic-gate 	fprintf(stdout, "set_filter: filter=\"%s\"\n", filter);
265*0Sstevel@tonic-gate 	fprintf(stdout, "set_filter: userdata=\"%s\"\n", userdata);
266*0Sstevel@tonic-gate #endif /* DEBUG */
267*0Sstevel@tonic-gate 	return (filter);
268*0Sstevel@tonic-gate }
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate 
271*0Sstevel@tonic-gate /* generic set_filter, this function is not thread safe */
272*0Sstevel@tonic-gate char *
273*0Sstevel@tonic-gate set_filter(char **key, char *database, char **udata)
274*0Sstevel@tonic-gate {
275*0Sstevel@tonic-gate 	char 		*filter;
276*0Sstevel@tonic-gate 	char 		*userdata;
277*0Sstevel@tonic-gate 	char		*keyfilter;
278*0Sstevel@tonic-gate 	int		i;
279*0Sstevel@tonic-gate 	int		rc, v2 = 1;
280*0Sstevel@tonic-gate 	void		**paramVal = NULL;
281*0Sstevel@tonic-gate 	ns_ldap_error_t	*errorp = NULL;
282*0Sstevel@tonic-gate 
283*0Sstevel@tonic-gate 	if (!database) {
284*0Sstevel@tonic-gate 		return (NULL);
285*0Sstevel@tonic-gate 	}
286*0Sstevel@tonic-gate 	if (!udata) {
287*0Sstevel@tonic-gate 		return (NULL);
288*0Sstevel@tonic-gate 	}
289*0Sstevel@tonic-gate 
290*0Sstevel@tonic-gate 	filter = (char *)malloc(MAXLINE);
291*0Sstevel@tonic-gate 	if (!filter) {
292*0Sstevel@tonic-gate 		return (NULL);
293*0Sstevel@tonic-gate 	}
294*0Sstevel@tonic-gate 	filter[0] = '\0';
295*0Sstevel@tonic-gate 
296*0Sstevel@tonic-gate 	userdata = (char *)malloc(MAXLINE);
297*0Sstevel@tonic-gate 	if (!userdata) {
298*0Sstevel@tonic-gate 		free(filter);
299*0Sstevel@tonic-gate 		return (NULL);
300*0Sstevel@tonic-gate 	}
301*0Sstevel@tonic-gate 	userdata[0] = '\0';
302*0Sstevel@tonic-gate 	*udata = userdata;
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate 	/*
305*0Sstevel@tonic-gate 	 * Check for version of the profile the client is using
306*0Sstevel@tonic-gate 	 *
307*0Sstevel@tonic-gate 	 * For version 1 profiles we do use nisMap and nisObject schema
308*0Sstevel@tonic-gate 	 * for backward compatibility with Solaris 8 clients.
309*0Sstevel@tonic-gate 	 *
310*0Sstevel@tonic-gate 	 * For version 2 profiles we use automountMap and automount as
311*0Sstevel@tonic-gate 	 * default attributes (which can then be overridden in libsldap
312*0Sstevel@tonic-gate 	 * if schema mapping is configured in the profile).
313*0Sstevel@tonic-gate 	 *
314*0Sstevel@tonic-gate 	 * If profile version is not available, use version 2 as default.
315*0Sstevel@tonic-gate 	 */
316*0Sstevel@tonic-gate 	rc = __ns_ldap_getParam(NS_LDAP_FILE_VERSION_P, &paramVal, &errorp);
317*0Sstevel@tonic-gate 	if (rc != NS_LDAP_SUCCESS || !paramVal || !*paramVal) {
318*0Sstevel@tonic-gate 		/* should print a message here: using v2 defaults */
319*0Sstevel@tonic-gate 		(void) __ns_ldap_freeError(&errorp);
320*0Sstevel@tonic-gate 	} else {
321*0Sstevel@tonic-gate 		if (strcasecmp(*paramVal, NS_LDAP_VERSION_1) == 0)
322*0Sstevel@tonic-gate 			v2 = 0;
323*0Sstevel@tonic-gate 		(void) __ns_ldap_freeParam(&paramVal);
324*0Sstevel@tonic-gate 	}
325*0Sstevel@tonic-gate 
326*0Sstevel@tonic-gate 	/*
327*0Sstevel@tonic-gate 	 * starts at 2 to skip over publickey databases.
328*0Sstevel@tonic-gate 	 * These databases are handled separately.
329*0Sstevel@tonic-gate 	 */
330*0Sstevel@tonic-gate 	for (i = 2; maplist[i].database != NULL; i++) {
331*0Sstevel@tonic-gate 		if (strcasecmp(database, maplist[i].database) == SAME) {
332*0Sstevel@tonic-gate 			if ((keyfilter = set_keys(key, maplist[i].def_type))
333*0Sstevel@tonic-gate 							== NULL) {
334*0Sstevel@tonic-gate 				snprintf(filter, MAXLINE, "objectclass=%s",
335*0Sstevel@tonic-gate 					maplist[i].objectclass);
336*0Sstevel@tonic-gate 				sprintf(userdata, "%%s");
337*0Sstevel@tonic-gate 			} else {
338*0Sstevel@tonic-gate 				snprintf(filter, MAXLINE,
339*0Sstevel@tonic-gate 					"(&(objectclass=%s)%s)",
340*0Sstevel@tonic-gate 					maplist[i].objectclass, keyfilter);
341*0Sstevel@tonic-gate 				snprintf(userdata, MAXLINE, "(&(%%s)%s)",
342*0Sstevel@tonic-gate 					keyfilter);
343*0Sstevel@tonic-gate #ifdef DEBUG
344*0Sstevel@tonic-gate 	fprintf(stdout, "set_filter: filter=\"%s\"\n", filter);
345*0Sstevel@tonic-gate 	fprintf(stdout, "set_filter: userdata=\"%s\"\n", userdata);
346*0Sstevel@tonic-gate #endif /* DEBUG */
347*0Sstevel@tonic-gate 			}
348*0Sstevel@tonic-gate 			return (filter);
349*0Sstevel@tonic-gate 		}
350*0Sstevel@tonic-gate 	}
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate 	/* special cases for automounter and other services */
353*0Sstevel@tonic-gate 
354*0Sstevel@tonic-gate 	/* auto_* services */
355*0Sstevel@tonic-gate 	if (strncasecmp(database, "auto_", 5) == SAME) {
356*0Sstevel@tonic-gate 	    if (v2) {
357*0Sstevel@tonic-gate 		if ((keyfilter = set_keys(key, "automountKey"))
358*0Sstevel@tonic-gate 			!= NULL) {
359*0Sstevel@tonic-gate 			snprintf(filter, MAXLINE,
360*0Sstevel@tonic-gate 				"(&(objectclass=automount)%s)", keyfilter);
361*0Sstevel@tonic-gate 			snprintf(userdata, MAXLINE, "(&(%%s)%s)", keyfilter);
362*0Sstevel@tonic-gate 		} else {
363*0Sstevel@tonic-gate 			strcpy(filter, "objectclass=automount");
364*0Sstevel@tonic-gate 			strcpy(userdata, "%s");
365*0Sstevel@tonic-gate 		}
366*0Sstevel@tonic-gate 	    } else {
367*0Sstevel@tonic-gate 		if ((keyfilter = set_keys(key, "cn"))
368*0Sstevel@tonic-gate 			!= NULL) {
369*0Sstevel@tonic-gate 			snprintf(filter, MAXLINE,
370*0Sstevel@tonic-gate 				"(&(objectclass=nisObject)%s)", keyfilter);
371*0Sstevel@tonic-gate 			snprintf(userdata, MAXLINE, "(&(%%s)%s)", keyfilter);
372*0Sstevel@tonic-gate 		} else {
373*0Sstevel@tonic-gate 			strcpy(filter, "objectclass=nisObject");
374*0Sstevel@tonic-gate 			strcpy(userdata, "%s");
375*0Sstevel@tonic-gate 		}
376*0Sstevel@tonic-gate 	    }
377*0Sstevel@tonic-gate 	    goto done;
378*0Sstevel@tonic-gate 	}
379*0Sstevel@tonic-gate 
380*0Sstevel@tonic-gate 	/* automount service */
381*0Sstevel@tonic-gate 	if (strcasecmp(database, "automount") == SAME) {
382*0Sstevel@tonic-gate 	    if (v2) {
383*0Sstevel@tonic-gate 		if ((keyfilter = set_keys(key, "automountMapName"))
384*0Sstevel@tonic-gate 			!= NULL) {
385*0Sstevel@tonic-gate 			snprintf(filter, MAXLINE,
386*0Sstevel@tonic-gate 				"(&(objectclass=automountMap)%s)", keyfilter);
387*0Sstevel@tonic-gate 			snprintf(userdata, MAXLINE, "(&(%%s)%s)", keyfilter);
388*0Sstevel@tonic-gate 		} else {
389*0Sstevel@tonic-gate 			strcpy(filter, "objectclass=automountMap");
390*0Sstevel@tonic-gate 			strcpy(userdata, "%s");
391*0Sstevel@tonic-gate 		}
392*0Sstevel@tonic-gate 	    } else {
393*0Sstevel@tonic-gate 		if ((keyfilter = set_keys(key, "nisMapName"))
394*0Sstevel@tonic-gate 			!= NULL) {
395*0Sstevel@tonic-gate 			snprintf(filter, MAXLINE, "(&(objectclass=nisMap)%s)",
396*0Sstevel@tonic-gate 				keyfilter);
397*0Sstevel@tonic-gate 			snprintf(userdata, MAXLINE, "(&(%%s)%s)", keyfilter);
398*0Sstevel@tonic-gate 		} else {
399*0Sstevel@tonic-gate 			strcpy(filter, "objectclass=nisMap");
400*0Sstevel@tonic-gate 			strcpy(userdata, "%s");
401*0Sstevel@tonic-gate 		}
402*0Sstevel@tonic-gate 	    }
403*0Sstevel@tonic-gate 	    goto done;
404*0Sstevel@tonic-gate 	}
405*0Sstevel@tonic-gate 
406*0Sstevel@tonic-gate 	/* other services (catch all) */
407*0Sstevel@tonic-gate 	if ((keyfilter = set_keys(key, "cn")) == NULL) {
408*0Sstevel@tonic-gate 		snprintf(filter, MAXLINE, "objectclass=*");
409*0Sstevel@tonic-gate 		strcpy(userdata, "%s");
410*0Sstevel@tonic-gate 	} else {
411*0Sstevel@tonic-gate 		snprintf(filter, MAXLINE, "%s", keyfilter);
412*0Sstevel@tonic-gate 		snprintf(userdata, MAXLINE, "(&(%%s)(%s))", keyfilter);
413*0Sstevel@tonic-gate 	}
414*0Sstevel@tonic-gate 
415*0Sstevel@tonic-gate done:
416*0Sstevel@tonic-gate #ifdef DEBUG
417*0Sstevel@tonic-gate 	fprintf(stdout, "set_filter: filter=\"%s\"\n", filter);
418*0Sstevel@tonic-gate 	fprintf(stdout, "set_filter: userdata=\"%s\"\n", userdata);
419*0Sstevel@tonic-gate #endif /* DEBUG */
420*0Sstevel@tonic-gate 	return (filter);
421*0Sstevel@tonic-gate }
422