xref: /titanic_41/usr/src/cmd/nscd/nscd_switch.c (revision e37190e5b4531a897e4191a30b8f41678b582e25)
1cb5caa98Sdjl /*
2cb5caa98Sdjl  * CDDL HEADER START
3cb5caa98Sdjl  *
4cb5caa98Sdjl  * The contents of this file are subject to the terms of the
5cb5caa98Sdjl  * Common Development and Distribution License (the "License").
6cb5caa98Sdjl  * You may not use this file except in compliance with the License.
7cb5caa98Sdjl  *
8cb5caa98Sdjl  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9cb5caa98Sdjl  * or http://www.opensolaris.org/os/licensing.
10cb5caa98Sdjl  * See the License for the specific language governing permissions
11cb5caa98Sdjl  * and limitations under the License.
12cb5caa98Sdjl  *
13cb5caa98Sdjl  * When distributing Covered Code, include this CDDL HEADER in each
14cb5caa98Sdjl  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15cb5caa98Sdjl  * If applicable, add the following below this CDDL HEADER, with the
16cb5caa98Sdjl  * fields enclosed by brackets "[]" replaced with your own identifying
17cb5caa98Sdjl  * information: Portions Copyright [yyyy] [name of copyright owner]
18cb5caa98Sdjl  *
19cb5caa98Sdjl  * CDDL HEADER END
20cb5caa98Sdjl  */
21cb5caa98Sdjl /*
22cb5caa98Sdjl  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23cb5caa98Sdjl  * Use is subject to license terms.
24cb5caa98Sdjl  */
25cb5caa98Sdjl 
26cb5caa98Sdjl #pragma ident	"%Z%%M%	%I%	%E% SMI"
27cb5caa98Sdjl 
28cb5caa98Sdjl #include <stdlib.h>	/* getenv() */
29cb5caa98Sdjl #include <assert.h>
30cb5caa98Sdjl #include <unistd.h>
31cb5caa98Sdjl #include <string.h>
32cb5caa98Sdjl #include <dlfcn.h>
33cb5caa98Sdjl #include <nss_dbdefs.h>
34cb5caa98Sdjl #include <exec_attr.h>
35cb5caa98Sdjl #include <gssapi/gssapi.h>
36cb5caa98Sdjl #include "nscd_door.h"
37cb5caa98Sdjl #include "nscd_switch.h"
38cb5caa98Sdjl #include "nscd_log.h"
39cb5caa98Sdjl #include "nscd_frontend.h"
40cb5caa98Sdjl 
41cb5caa98Sdjl #pragma weak nss_search = _nss_search
42cb5caa98Sdjl #define	nss_search	_nss_search
43cb5caa98Sdjl 
44cb5caa98Sdjl extern rwlock_t nscd_smf_service_state_lock;
45cb5caa98Sdjl 
46cb5caa98Sdjl /* nscd id: main, forker, or child */
47cb5caa98Sdjl extern int _whoami;
48cb5caa98Sdjl 
49cb5caa98Sdjl static int
50cb5caa98Sdjl retry_test(nss_status_t res, int n, struct __nsw_lookup_v1 *lkp)
51cb5caa98Sdjl {
52cb5caa98Sdjl 	if (res != NSS_TRYAGAIN && res !=  NSS_NISSERVDNS_TRYAGAIN) {
53cb5caa98Sdjl 		if (res == NSS_SUCCESS) {
54cb5caa98Sdjl 			__NSW_UNPAUSE_ACTION(lkp->actions[__NSW_TRYAGAIN]);
55cb5caa98Sdjl 			__NSW_UNPAUSE_ACTION(
56cb5caa98Sdjl 				lkp->actions[__NSW_NISSERVDNS_TRYAGAIN]);
57cb5caa98Sdjl 		}
58cb5caa98Sdjl 		return (0);
59cb5caa98Sdjl 	}
60cb5caa98Sdjl 
61cb5caa98Sdjl 	if ((res == NSS_TRYAGAIN &&
62cb5caa98Sdjl 	    lkp->actions[__NSW_TRYAGAIN] == __NSW_TRYAGAIN_FOREVER) ||
63cb5caa98Sdjl 	    (res == NSS_NISSERVDNS_TRYAGAIN &&
64cb5caa98Sdjl 	    lkp->actions[__NSW_NISSERVDNS_TRYAGAIN] == __NSW_TRYAGAIN_FOREVER))
65cb5caa98Sdjl 		return (1);
66cb5caa98Sdjl 
67cb5caa98Sdjl 	if (res == NSS_TRYAGAIN &&
68cb5caa98Sdjl 	    lkp->actions[__NSW_TRYAGAIN] == __NSW_TRYAGAIN_NTIMES)
69cb5caa98Sdjl 		if (n <= lkp->max_retries)
70cb5caa98Sdjl 			return (1);
71cb5caa98Sdjl 		else {
72cb5caa98Sdjl 			lkp->actions[__NSW_TRYAGAIN] = __NSW_TRYAGAIN_PAUSED;
73cb5caa98Sdjl 			return (0);
74cb5caa98Sdjl 		}
75cb5caa98Sdjl 
76cb5caa98Sdjl 	if (res == NSS_NISSERVDNS_TRYAGAIN &&
77cb5caa98Sdjl 	    lkp->actions[__NSW_NISSERVDNS_TRYAGAIN] == __NSW_TRYAGAIN_NTIMES)
78cb5caa98Sdjl 		if (n <= lkp->max_retries)
79cb5caa98Sdjl 			return (1);
80cb5caa98Sdjl 		else {
81cb5caa98Sdjl 			lkp->actions[__NSW_NISSERVDNS_TRYAGAIN] =
82cb5caa98Sdjl 			    __NSW_TRYAGAIN_PAUSED;
83cb5caa98Sdjl 			return (0);
84cb5caa98Sdjl 		}
85cb5caa98Sdjl 
86cb5caa98Sdjl 	return (0);
87cb5caa98Sdjl }
88cb5caa98Sdjl 
89cb5caa98Sdjl static thread_key_t loopback_key;
90cb5caa98Sdjl static mutex_t loopback_key_lock = DEFAULTMUTEX;
91cb5caa98Sdjl static int loopback_key_created = 0;
92cb5caa98Sdjl typedef struct lb_key {
93cb5caa98Sdjl 	int		srci;
94cb5caa98Sdjl 	int		dbi;
95cb5caa98Sdjl 	int		fnum;
96cb5caa98Sdjl 	int		*lb_flagp;
97cb5caa98Sdjl } lb_key_t;
98cb5caa98Sdjl 
99cb5caa98Sdjl static int
100cb5caa98Sdjl set_loopback_key(lb_key_t *key) {
101cb5caa98Sdjl 
102cb5caa98Sdjl 	int		rc = 0;
103cb5caa98Sdjl 	lb_key_t	*k;
104cb5caa98Sdjl 
105cb5caa98Sdjl 	if (!loopback_key_created) {
106cb5caa98Sdjl 		(void) mutex_lock(&loopback_key_lock);
107cb5caa98Sdjl 		if (!loopback_key_created) {
108cb5caa98Sdjl 			if ((rc = thr_keycreate(&loopback_key,
109cb5caa98Sdjl 					NULL)) == 0)
110cb5caa98Sdjl 				loopback_key_created = 1;
111cb5caa98Sdjl 		}
112cb5caa98Sdjl 		(void) mutex_unlock(&loopback_key_lock);
113cb5caa98Sdjl 	}
114cb5caa98Sdjl 	if (rc == 0) {
115cb5caa98Sdjl 		/* set key if not already set */
116cb5caa98Sdjl 		if (thr_getspecific(loopback_key, (void **)&k) == 0 &&
117cb5caa98Sdjl 				k == NULL) {
118cb5caa98Sdjl 			rc = thr_setspecific(loopback_key, key);
119cb5caa98Sdjl 		}
120cb5caa98Sdjl 	}
121cb5caa98Sdjl 
122cb5caa98Sdjl 	return (rc);
123cb5caa98Sdjl }
124cb5caa98Sdjl 
125cb5caa98Sdjl static lb_key_t *
126cb5caa98Sdjl get_loopback_key(void) {
127cb5caa98Sdjl 
128cb5caa98Sdjl 	char		*me = "get_loopback_key";
129cb5caa98Sdjl 	int 		rc = 0;
130cb5caa98Sdjl 	lb_key_t	*k = NULL;
131cb5caa98Sdjl 
132cb5caa98Sdjl 	if (!loopback_key_created)
133cb5caa98Sdjl 		return (NULL);
134cb5caa98Sdjl 
135cb5caa98Sdjl 	rc = thr_getspecific(loopback_key, (void **)&k);
136cb5caa98Sdjl 
137cb5caa98Sdjl 	_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
138cb5caa98Sdjl 	(me, "get loopback key rc= %d, key = %p\n", rc, k);
139cb5caa98Sdjl 
140cb5caa98Sdjl 	if (rc == 0 && k != NULL)
141cb5caa98Sdjl 		return (k);
142cb5caa98Sdjl 
143cb5caa98Sdjl 	return (NULL);
144cb5caa98Sdjl }
145cb5caa98Sdjl 
146cb5caa98Sdjl static void
147cb5caa98Sdjl clear_loopback_key(lb_key_t *key) {
148cb5caa98Sdjl 
149cb5caa98Sdjl 	char		*me = "clear_loopback_key";
150cb5caa98Sdjl 
151cb5caa98Sdjl 	if (loopback_key_created && key != 0) {
152cb5caa98Sdjl 		/*
153cb5caa98Sdjl 		 * key->lb_flagp points to the location of the
154cb5caa98Sdjl 		 * flag, check_flag, in the stack where it was
155cb5caa98Sdjl 		 * first set; clearing the flag tells that
156cb5caa98Sdjl 		 * stack the loopback error has been resolved
157cb5caa98Sdjl 		 */
158cb5caa98Sdjl 		*key->lb_flagp = 0;
159cb5caa98Sdjl 		(void) thr_setspecific(loopback_key, NULL);
160cb5caa98Sdjl 	}
161cb5caa98Sdjl 
162cb5caa98Sdjl 	_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
163cb5caa98Sdjl 	(me, "key %p cleared\n", key);
164cb5caa98Sdjl }
165cb5caa98Sdjl 
166cb5caa98Sdjl static thread_key_t initf_key;
167cb5caa98Sdjl static mutex_t initf_key_lock = DEFAULTMUTEX;
168cb5caa98Sdjl static int initf_key_created = 0;
169cb5caa98Sdjl 
170cb5caa98Sdjl static int
171cb5caa98Sdjl set_initf_key(void *pbuf) {
172cb5caa98Sdjl 
173cb5caa98Sdjl 	int		rc = 0;
174cb5caa98Sdjl 
175cb5caa98Sdjl 	if (!initf_key_created) {
176cb5caa98Sdjl 		(void) mutex_lock(&initf_key_lock);
177cb5caa98Sdjl 		if (!initf_key_created) {
178cb5caa98Sdjl 			if ((rc = thr_keycreate(&initf_key, NULL)) == 0)
179cb5caa98Sdjl 				initf_key_created = 1;
180cb5caa98Sdjl 		}
181cb5caa98Sdjl 		(void) mutex_unlock(&initf_key_lock);
182cb5caa98Sdjl 	}
183cb5caa98Sdjl 	if (rc == 0)
184cb5caa98Sdjl 		rc = thr_setspecific(initf_key, pbuf);
185cb5caa98Sdjl 
186cb5caa98Sdjl 	return (rc);
187cb5caa98Sdjl }
188cb5caa98Sdjl 
189cb5caa98Sdjl static void *
190cb5caa98Sdjl get_initf_key(void) {
191cb5caa98Sdjl 
192cb5caa98Sdjl 	char		*me = "get_initf_key";
193cb5caa98Sdjl 	void		*pbuf;
194cb5caa98Sdjl 	int 		rc = 0;
195cb5caa98Sdjl 
196cb5caa98Sdjl 	if (!initf_key_created)
197cb5caa98Sdjl 		return (NULL);
198cb5caa98Sdjl 
199cb5caa98Sdjl 	rc = thr_getspecific(initf_key, (void **)&pbuf);
200cb5caa98Sdjl 
201cb5caa98Sdjl 	_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
202cb5caa98Sdjl 	(me, "got initf pbuf rc= %d, key = %p\n", rc, pbuf);
203cb5caa98Sdjl 
204cb5caa98Sdjl 	if (rc == 0 && pbuf != NULL)
205cb5caa98Sdjl 		return (pbuf);
206cb5caa98Sdjl 
207cb5caa98Sdjl 	return (NULL);
208cb5caa98Sdjl }
209cb5caa98Sdjl 
210cb5caa98Sdjl static void
211cb5caa98Sdjl clear_initf_key(void) {
212cb5caa98Sdjl 
213cb5caa98Sdjl 	char		*me = "clear_initf_key";
214cb5caa98Sdjl 
215cb5caa98Sdjl 	if (initf_key_created)
216cb5caa98Sdjl 		(void) thr_setspecific(initf_key, NULL);
217cb5caa98Sdjl 
218cb5caa98Sdjl 	_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
219cb5caa98Sdjl 	(me, "initf pbuf cleared\n");
220cb5caa98Sdjl }
221cb5caa98Sdjl 
222cb5caa98Sdjl /*
223cb5caa98Sdjl  * Call the input initf function to extract the
224cb5caa98Sdjl  * NSS front end parameters and examine them to
225cb5caa98Sdjl  * determine if an NSS lookup is to be performed
226cb5caa98Sdjl  * on a regular or a pseudo (called from compat
227cb5caa98Sdjl  * backend) database. Then set the necessary
228cb5caa98Sdjl  * parameters for later data structures creation
229cb5caa98Sdjl  * and processing.
230cb5caa98Sdjl  */
231cb5caa98Sdjl static nscd_rc_t
232cb5caa98Sdjl getparams(
233cb5caa98Sdjl 	int			search_fnum,
234cb5caa98Sdjl 	nss_db_initf_t		initf,
235cb5caa98Sdjl 	nscd_nsw_params_t	*params)
236cb5caa98Sdjl {
237cb5caa98Sdjl 
238cb5caa98Sdjl 	nscd_rc_t	rc = NSCD_SUCCESS;
239cb5caa98Sdjl 	nss_db_params_t	*p;
240cb5caa98Sdjl 	int		j;
241cb5caa98Sdjl 	char		*dbn;
242cb5caa98Sdjl 	const char	*n;
243cb5caa98Sdjl 
244cb5caa98Sdjl 	p = &params->p;
245cb5caa98Sdjl 	(void) memset(p, 0, sizeof (*p));
246cb5caa98Sdjl 	(*initf)(p);
247cb5caa98Sdjl 	params->dbi = -1;
248cb5caa98Sdjl 	params->cfgdbi = -1;
249cb5caa98Sdjl 	params->compati = -1;
250cb5caa98Sdjl 	params->dnsi = -1;
251cb5caa98Sdjl 
252cb5caa98Sdjl 	/* map database name to index */
253cb5caa98Sdjl 	n = p->name;
254cb5caa98Sdjl 	for (j = 0; j < NSCD_NUM_DB; j++) {
255cb5caa98Sdjl 		dbn = NSCD_NSW_DB_NAME(j);
256cb5caa98Sdjl 		if (*n != *dbn)
257cb5caa98Sdjl 			continue;
258cb5caa98Sdjl 		if (strcmp(n, dbn) == 0) {
259cb5caa98Sdjl 			params->dbi = j;
260cb5caa98Sdjl 			if (*n != 'h' && *n != 'i' && *n != 's' && *n != 'a')
261cb5caa98Sdjl 				break;
262cb5caa98Sdjl 			if (strcmp(n, NSS_DBNAM_HOSTS) == 0 &&
263cb5caa98Sdjl 				search_fnum == NSS_DBOP_HOSTS_BYNAME)
264cb5caa98Sdjl 				params->dnsi = 0;
265cb5caa98Sdjl 			else if (strcmp(n, NSS_DBNAM_IPNODES) == 0 &&
266cb5caa98Sdjl 				search_fnum == NSS_DBOP_IPNODES_BYNAME)
267cb5caa98Sdjl 				params->dnsi = 1;
268cb5caa98Sdjl 			else if (strcmp(n, NSS_DBNAM_SHADOW) == 0)
269cb5caa98Sdjl 				params->privdb = 1;
270cb5caa98Sdjl 			else if (strcmp(n, NSS_DBNAM_AUDITUSER) == 0)
271cb5caa98Sdjl 				params->privdb = 1;
272cb5caa98Sdjl 			break;
273cb5caa98Sdjl 		}
274cb5caa98Sdjl 	}
275cb5caa98Sdjl 
276cb5caa98Sdjl 	/*
277cb5caa98Sdjl 	 * use the switch policy for passwd_compat or
278cb5caa98Sdjl 	 * group_compat?
279cb5caa98Sdjl 	 */
280cb5caa98Sdjl 	if (p->config_name != NULL) {
281cb5caa98Sdjl 
282cb5caa98Sdjl 		n = p->config_name;
283cb5caa98Sdjl 		for (j = 0; j < NSCD_NUM_DB; j++) {
284cb5caa98Sdjl 			dbn = NSCD_NSW_DB_NAME(j);
285cb5caa98Sdjl 			if (*n == *dbn) {
286cb5caa98Sdjl 				if (strcmp(n, dbn) == 0) {
287cb5caa98Sdjl 					params->cfgdbi = j;
288cb5caa98Sdjl 					break;
289cb5caa98Sdjl 				}
290cb5caa98Sdjl 			}
291cb5caa98Sdjl 		}
292cb5caa98Sdjl 	}
293cb5caa98Sdjl 
294cb5caa98Sdjl 	/* map the database name to the pseudo database index */
295cb5caa98Sdjl 	if (params->cfgdbi != -1) {
296cb5caa98Sdjl 		if (strstr(p->config_name, "_compat") != NULL) {
297cb5caa98Sdjl 			n = p->name;
298cb5caa98Sdjl 			for (j = params->cfgdbi; j < NSCD_NUM_DB; j++) {
299cb5caa98Sdjl 				dbn = NSCD_NSW_DB_NAME(j);
300cb5caa98Sdjl 				if (*n == *dbn) {
301cb5caa98Sdjl 					if (strcmp(n, dbn) == 0) {
302cb5caa98Sdjl 						params->compati = j;
303cb5caa98Sdjl 						break;
304cb5caa98Sdjl 					}
305cb5caa98Sdjl 				}
306cb5caa98Sdjl 			}
307cb5caa98Sdjl 		}
308cb5caa98Sdjl 	}
309cb5caa98Sdjl 
310cb5caa98Sdjl 	assert(params->dbi != -1);
311cb5caa98Sdjl 	return (rc);
312cb5caa98Sdjl }
313cb5caa98Sdjl 
314cb5caa98Sdjl static void
315cb5caa98Sdjl nscd_initf(nss_db_params_t	*p)
316cb5caa98Sdjl {
317cb5caa98Sdjl 	nss_pheader_t		*pbuf;
318cb5caa98Sdjl 	nssuint_t		off;
319cb5caa98Sdjl 	nss_dbd_t		*pdbd;
320cb5caa98Sdjl 	char			*me = "nscd_initf";
321cb5caa98Sdjl 
322cb5caa98Sdjl 	pbuf = (nss_pheader_t *)get_initf_key();
323cb5caa98Sdjl 	if (pbuf == NULL) {
324cb5caa98Sdjl 		_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
325cb5caa98Sdjl 		(me, "ERROR: initf key not set\n");
326cb5caa98Sdjl 		return;
327cb5caa98Sdjl 	}
328cb5caa98Sdjl 
329cb5caa98Sdjl 	if (pbuf->dbd_len <= sizeof (nss_dbd_t)) {
330cb5caa98Sdjl 		_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
331cb5caa98Sdjl 		(me, "invalid db front params data ? dbd_len = %d\n",
332cb5caa98Sdjl 		pbuf->dbd_len);
333cb5caa98Sdjl 		return;
334cb5caa98Sdjl 	}
335cb5caa98Sdjl 
336cb5caa98Sdjl 	off = pbuf->dbd_off;
337cb5caa98Sdjl 	pdbd = (nss_dbd_t *)((void *)((char *)pbuf + off));
338cb5caa98Sdjl 
339cb5caa98Sdjl 	p->name = (char *)pdbd + pdbd->o_name;
340cb5caa98Sdjl 	p->config_name = (char *)pdbd + pdbd->o_config_name;
341cb5caa98Sdjl 	p->default_config = (char *)pdbd + pdbd->o_default_config;
342cb5caa98Sdjl 	p->flags = (enum nss_dbp_flags)pdbd->flags;
343cb5caa98Sdjl 	(void) memcpy(&p->private, &pbuf->nscdpriv, sizeof (p->private));
344cb5caa98Sdjl 
345cb5caa98Sdjl 	_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
346cb5caa98Sdjl 	(me, "db frontend params: name =%s, config_name = %s, "
347cb5caa98Sdjl 	"default_config = %s, flags = %x\n", p->name,
348cb5caa98Sdjl 	(p->config_name && *p->config_name != '\0' ?
349cb5caa98Sdjl 				p->config_name : "<NOT SPECIFIED>"),
350cb5caa98Sdjl 	(p->default_config && *p->default_config != '\0' ?
351cb5caa98Sdjl 				p->default_config : "<NOT SPECIFIED>"),
352cb5caa98Sdjl 	p->flags);
353cb5caa98Sdjl }
354cb5caa98Sdjl 
355cb5caa98Sdjl 
356cb5caa98Sdjl static void
357cb5caa98Sdjl trace_result(
358cb5caa98Sdjl 	int		dbi,
359cb5caa98Sdjl 	int		srci,
360cb5caa98Sdjl 	int		op,
361cb5caa98Sdjl 	nss_status_t	res,
362cb5caa98Sdjl 	nss_XbyY_args_t	*arg)
363cb5caa98Sdjl {
364cb5caa98Sdjl 	char	*res_str;
365cb5caa98Sdjl 	char	*src = "?";
366cb5caa98Sdjl 	char	*db = "?";
367cb5caa98Sdjl 	char	*me = "nss_search";
368cb5caa98Sdjl 
369cb5caa98Sdjl 	switch (res) {
370cb5caa98Sdjl 	case NSS_SUCCESS:
371cb5caa98Sdjl 		res_str = "NSS_SUCCESS";
372cb5caa98Sdjl 		break;
373cb5caa98Sdjl 	case NSS_NOTFOUND:
374cb5caa98Sdjl 		res_str = "NSS_NOTFOUND";
375cb5caa98Sdjl 		break;
376cb5caa98Sdjl 	case NSS_UNAVAIL:
377cb5caa98Sdjl 		res_str = "NSS_UNAVAIL";
378cb5caa98Sdjl 		break;
379cb5caa98Sdjl 	case NSS_TRYAGAIN:
380cb5caa98Sdjl 		res_str = "NSS_TRYAGAIN";
381cb5caa98Sdjl 		break;
382cb5caa98Sdjl 	case NSS_NISSERVDNS_TRYAGAIN:
383cb5caa98Sdjl 		res_str = "NSS_NISSERVDNS_TRYAGAIN";
384cb5caa98Sdjl 		break;
385cb5caa98Sdjl 	default:
386cb5caa98Sdjl 		res_str = "UNKNOWN STATUS";
387cb5caa98Sdjl 		break;
388cb5caa98Sdjl 	}
389cb5caa98Sdjl 
390cb5caa98Sdjl 	if (dbi != -1)
391cb5caa98Sdjl 		db = NSCD_NSW_DB_NAME(dbi);
392cb5caa98Sdjl 	if (srci != -1)
393cb5caa98Sdjl 		src = NSCD_NSW_SRC_NAME(srci);
394cb5caa98Sdjl 
395cb5caa98Sdjl 	if (res == NSS_SUCCESS) {
396cb5caa98Sdjl 		_nscd_logit(me,
397*e37190e5Smichen "%s: database: %s, operation: %d, source: %s returned >>%s<<, length = %d\n",
398cb5caa98Sdjl 		res_str, db, op, src, arg->buf.buffer, arg->returnlen);
399cb5caa98Sdjl 
400cb5caa98Sdjl 		return;
401cb5caa98Sdjl 	}
402cb5caa98Sdjl 
403cb5caa98Sdjl 	_nscd_logit(me,
404cb5caa98Sdjl "%s: database: %s, operation: %d, source: %s, erange= %d, errno: %s \n",
405cb5caa98Sdjl 		res_str, db, op, src, arg->erange, strerror(arg->h_errno));
406cb5caa98Sdjl }
407cb5caa98Sdjl 
408cb5caa98Sdjl /*
409cb5caa98Sdjl  * Determine if a request should be done locally in the getXbyY caller's
410cb5caa98Sdjl  * process. Return none zero if yes, 0 otherwise.
411cb5caa98Sdjl  * This function returnis 1 if:
412cb5caa98Sdjl  *   -- the database is exec_attr and the search_flag is GET_ALL
413cb5caa98Sdjl  */
414cb5caa98Sdjl static int
415cb5caa98Sdjl try_local(
416cb5caa98Sdjl 	int			dbi,
417cb5caa98Sdjl 	void			*arg)
418cb5caa98Sdjl {
419cb5caa98Sdjl 	struct nss_XbyY_args	*ap = (struct nss_XbyY_args *)arg;
420cb5caa98Sdjl 	_priv_execattr		*ep;
421cb5caa98Sdjl 	int			rc = 0;
422cb5caa98Sdjl 	char			*me = "try_local";
423cb5caa98Sdjl 
424cb5caa98Sdjl 	if (strcmp(NSCD_NSW_DB_NAME(dbi), NSS_DBNAM_EXECATTR) == 0) {
425cb5caa98Sdjl 		if ((ep = ap->key.attrp) != NULL &&
426cb5caa98Sdjl 				ep->search_flag == GET_ALL)
427cb5caa98Sdjl 			rc = 1;
428cb5caa98Sdjl 	}
429cb5caa98Sdjl 
430cb5caa98Sdjl 	if (rc != 0) {
431cb5caa98Sdjl 
432cb5caa98Sdjl 		_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
433cb5caa98Sdjl 		(me, "TRYLOCAL: exec_attr:GET_ALL\n");
434cb5caa98Sdjl 	}
435cb5caa98Sdjl 
436cb5caa98Sdjl 	return (rc);
437cb5caa98Sdjl }
438cb5caa98Sdjl 
439cb5caa98Sdjl static nscd_rc_t
440c70a8a3bSmichen get_gss_func(void **func_p)
441c70a8a3bSmichen {
442c70a8a3bSmichen 	char		*me = "get_gss_func";
443c70a8a3bSmichen 	static void	*handle = NULL;
444c70a8a3bSmichen 	static mutex_t	func_lock = DEFAULTMUTEX;
445c70a8a3bSmichen 	void		*sym;
446c70a8a3bSmichen 	char		*func_name = "gss_inquire_cred";
447c70a8a3bSmichen 	static void	*func = NULL;
448c70a8a3bSmichen 
449c70a8a3bSmichen 	if (handle != NULL && func_p != NULL && func != NULL) {
450c70a8a3bSmichen 		(void) memcpy(func_p, &func, sizeof (void *));
451c70a8a3bSmichen 		return (NSCD_SUCCESS);
452c70a8a3bSmichen 	}
453c70a8a3bSmichen 
454c70a8a3bSmichen 	(void) mutex_lock(&func_lock);
455c70a8a3bSmichen 
456c70a8a3bSmichen 	/* close the handle if requested */
457c70a8a3bSmichen 	if (func_p == NULL) {
458c70a8a3bSmichen 		if (handle != NULL) {
459c70a8a3bSmichen 			(void) dlclose(handle);
460c70a8a3bSmichen 			func = NULL;
461c70a8a3bSmichen 		}
462c70a8a3bSmichen 		(void) mutex_unlock(&func_lock);
463c70a8a3bSmichen 		return (NSCD_SUCCESS);
464c70a8a3bSmichen 	}
465c70a8a3bSmichen 
466c70a8a3bSmichen 	if (handle != NULL && func != NULL) {
467c70a8a3bSmichen 		(void) memcpy(func_p, &func, sizeof (void *));
468c70a8a3bSmichen 		(void) mutex_unlock(&func_lock);
469c70a8a3bSmichen 		return (NSCD_SUCCESS);
470c70a8a3bSmichen 	}
471c70a8a3bSmichen 
472c70a8a3bSmichen 	if (handle == NULL) {
473c70a8a3bSmichen 		handle = dlopen("libgss.so.1", RTLD_LAZY);
474c70a8a3bSmichen 		if (handle == NULL) {
475c70a8a3bSmichen 			_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE,
476c70a8a3bSmichen 				NSCD_LOG_LEVEL_ERROR)
477c70a8a3bSmichen 			(me, "unable to dlopen libgss.so.1\n");
478c70a8a3bSmichen 			(void) mutex_unlock(&func_lock);
479c70a8a3bSmichen 			return (NSCD_CFG_DLOPEN_ERROR);
480c70a8a3bSmichen 		}
481c70a8a3bSmichen 	}
482c70a8a3bSmichen 
483c70a8a3bSmichen 	if ((sym = dlsym(handle, func_name)) == NULL) {
484c70a8a3bSmichen 
485c70a8a3bSmichen 		_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_ERROR)
486c70a8a3bSmichen 		(me, "unable to find symbol %s\n", func_name);
487c70a8a3bSmichen 		(void) mutex_unlock(&func_lock);
488c70a8a3bSmichen 		return (NSCD_CFG_DLSYM_ERROR);
489c70a8a3bSmichen 	} else {
490c70a8a3bSmichen 		(void) memcpy(func_p, &sym, sizeof (void *));
491c70a8a3bSmichen 		(void) memcpy(&func, &sym, sizeof (void *));
492c70a8a3bSmichen 	}
493c70a8a3bSmichen 
494c70a8a3bSmichen 	(void) mutex_unlock(&func_lock);
495c70a8a3bSmichen 	return (NSCD_SUCCESS);
496c70a8a3bSmichen }
497c70a8a3bSmichen 
498c70a8a3bSmichen static nscd_rc_t
499cb5caa98Sdjl get_dns_funcs(int dnsi, void **func_p)
500cb5caa98Sdjl {
501cb5caa98Sdjl 	char		*me = "get_dns_funcs";
502cb5caa98Sdjl 	static void	*handle = NULL;
503cb5caa98Sdjl 	static mutex_t	func_lock = DEFAULTMUTEX;
504cb5caa98Sdjl 	void		*sym;
505cb5caa98Sdjl 	char		*func_name[2] = { "_nss_get_dns_hosts_name",
506cb5caa98Sdjl 				"_nss_get_dns_ipnodes_name" };
507cb5caa98Sdjl 	static void	*func[2] = {NULL, NULL};
508cb5caa98Sdjl 
509cb5caa98Sdjl 	if (handle != NULL && dnsi > 0 && func[dnsi] != NULL) {
510cb5caa98Sdjl 		(void) memcpy(func_p, &func[dnsi], sizeof (void *));
511cb5caa98Sdjl 		return (NSCD_SUCCESS);
512cb5caa98Sdjl 	}
513cb5caa98Sdjl 
514cb5caa98Sdjl 	(void) mutex_lock(&func_lock);
515cb5caa98Sdjl 
516cb5caa98Sdjl 	/* close the handle if requested */
517cb5caa98Sdjl 	if (dnsi < 0) {
518cb5caa98Sdjl 		if (handle != NULL) {
519cb5caa98Sdjl 			(void) dlclose(handle);
520cb5caa98Sdjl 			func[0] = NULL;
521cb5caa98Sdjl 			func[1] = NULL;
522cb5caa98Sdjl 		}
523cb5caa98Sdjl 		(void) mutex_unlock(&func_lock);
524cb5caa98Sdjl 		return (NSCD_SUCCESS);
525cb5caa98Sdjl 	}
526cb5caa98Sdjl 
527cb5caa98Sdjl 	if (handle != NULL && func[dnsi] != NULL) {
528cb5caa98Sdjl 		(void) memcpy(func_p, &func[dnsi], sizeof (void *));
529cb5caa98Sdjl 		(void) mutex_unlock(&func_lock);
530cb5caa98Sdjl 		return (NSCD_SUCCESS);
531cb5caa98Sdjl 	}
532cb5caa98Sdjl 
533cb5caa98Sdjl 	if (handle == NULL) {
534cb5caa98Sdjl 		handle = dlopen("nss_dns.so.1", RTLD_LAZY);
535cb5caa98Sdjl 		if (handle == NULL) {
536cb5caa98Sdjl 			_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE,
537cb5caa98Sdjl 				NSCD_LOG_LEVEL_ERROR)
538cb5caa98Sdjl 			(me, "unable to dlopen nss_dns.so.1\n");
539cb5caa98Sdjl 			(void) mutex_unlock(&func_lock);
540cb5caa98Sdjl 			return (NSCD_CFG_DLOPEN_ERROR);
541cb5caa98Sdjl 		}
542cb5caa98Sdjl 	}
543cb5caa98Sdjl 
544cb5caa98Sdjl 	if ((sym = dlsym(handle, func_name[dnsi])) == NULL) {
545cb5caa98Sdjl 
546cb5caa98Sdjl 		_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_ERROR)
547cb5caa98Sdjl 		(me, "unable to find symbol %s\n", func_name[dnsi]);
548cb5caa98Sdjl 		(void) mutex_unlock(&func_lock);
549cb5caa98Sdjl 		return (NSCD_CFG_DLSYM_ERROR);
550cb5caa98Sdjl 	} else {
551cb5caa98Sdjl 		(void) memcpy(func_p, &sym, sizeof (void *));
552cb5caa98Sdjl 		(void) memcpy(&func[dnsi], &sym, sizeof (void *));
553cb5caa98Sdjl 	}
554cb5caa98Sdjl 
555cb5caa98Sdjl 	(void) mutex_unlock(&func_lock);
556cb5caa98Sdjl 	return (NSCD_SUCCESS);
557cb5caa98Sdjl }
558cb5caa98Sdjl 
559cb5caa98Sdjl static nss_status_t
560cb5caa98Sdjl search_dns_withttl(nscd_sw_return_t *swret, char *srcname, int dnsi)
561cb5caa98Sdjl {
562cb5caa98Sdjl 	nss_status_t	(*func)();
563cb5caa98Sdjl 	nss_status_t	res = NSS_UNAVAIL;
564cb5caa98Sdjl 	nscd_rc_t	rc;
565cb5caa98Sdjl 
566cb5caa98Sdjl 	swret->noarg = 0;
567cb5caa98Sdjl 	if (strcmp(srcname, "dns") != 0)
568cb5caa98Sdjl 		return (NSS_ERROR);
569cb5caa98Sdjl 
570cb5caa98Sdjl 	rc = get_dns_funcs(dnsi, (void **)&func);
571cb5caa98Sdjl 	if (rc == NSCD_SUCCESS)
572cb5caa98Sdjl 		res = (func)(NULL, &swret->pbuf, &swret->pbufsiz);
573cb5caa98Sdjl 	return (res);
574cb5caa98Sdjl }
575cb5caa98Sdjl 
576cb5caa98Sdjl /*
577cb5caa98Sdjl  * Returns a flag to indicate if needs to fall back to the
578cb5caa98Sdjl  * main nscd when a per-user lookup failed with rc NSS_NOTFOUND.
579cb5caa98Sdjl  */
580cb5caa98Sdjl static int
581cb5caa98Sdjl set_fallback_flag(char *srcname, nss_status_t rc)
582cb5caa98Sdjl {
583cb5caa98Sdjl 	char	*me = "set_fallback_flag";
584cb5caa98Sdjl 	if (strcmp(srcname, "ldap") == 0 && rc == NSS_NOTFOUND) {
585cb5caa98Sdjl 		_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
586cb5caa98Sdjl 		(me, "NSS_NOTFOUND (ldap): fallback to main nscd "
587cb5caa98Sdjl 		"may be needed\n");
588cb5caa98Sdjl 		return (1);
589cb5caa98Sdjl 	}
590cb5caa98Sdjl 	return (0);
591cb5caa98Sdjl }
592cb5caa98Sdjl 
593cb5caa98Sdjl nss_status_t
594cb5caa98Sdjl nss_search(nss_db_root_t *rootp, nss_db_initf_t initf, int search_fnum,
595cb5caa98Sdjl 	void *search_args)
596cb5caa98Sdjl {
597cb5caa98Sdjl 	char			*me = "nss_search";
598cb5caa98Sdjl 	nss_status_t		res = NSS_UNAVAIL;
599cb5caa98Sdjl 	nscd_nsw_state_t	*s = NULL;
600cb5caa98Sdjl 	int			n_src;
601cb5caa98Sdjl 	unsigned int		status_vec = 0;
602cb5caa98Sdjl 	int			dbi, srci = -1;
603cb5caa98Sdjl 	int			check_loopback = 0;
604cb5caa98Sdjl 	int			state_thr = 0;
605cb5caa98Sdjl 	lb_key_t		key, *k = NULL;
606cb5caa98Sdjl 	nss_db_root_t		root_db;
607cb5caa98Sdjl 	nscd_nsw_params_t	params;
608cb5caa98Sdjl 	nscd_sw_return_t	*swret;
609cb5caa98Sdjl 
610cb5caa98Sdjl 	_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
611cb5caa98Sdjl 	(me, "rootp = %p, initf = %p, search_fnum = %d, "
612cb5caa98Sdjl 		"search_args = %p\n", rootp, initf,
613cb5caa98Sdjl 		search_fnum, search_args);
614cb5caa98Sdjl 
615cb5caa98Sdjl 	NSCD_SW_STATS_G.lookup_request_received_g++;
616cb5caa98Sdjl 	NSCD_SW_STATS_G.lookup_request_in_progress_g++;
617cb5caa98Sdjl 	NSCD_SW_STATS_G.lookup_request_queued_g++;
618cb5caa98Sdjl 
619cb5caa98Sdjl 	/* determine db index, cfg db index, etc */
620cb5caa98Sdjl 	(void) getparams(search_fnum, initf, &params);
621cb5caa98Sdjl 	dbi = params.dbi;
622cb5caa98Sdjl 
623cb5caa98Sdjl 	/* get address of the switch engine return data area */
624cb5caa98Sdjl 	if (initf == nscd_initf) {
625cb5caa98Sdjl 		swret = (nscd_sw_return_t *)params.p.private;
626cb5caa98Sdjl 		swret->srci = -1;
627cb5caa98Sdjl 	} else {
628cb5caa98Sdjl 		swret = NULL;
629cb5caa98Sdjl 		params.dnsi = -1;
630cb5caa98Sdjl 	}
631cb5caa98Sdjl 
632cb5caa98Sdjl 	/*
633cb5caa98Sdjl 	 * for request that should be processed by the client,
634cb5caa98Sdjl 	 * send it back with status NSS_TRYLOCAL
635cb5caa98Sdjl 	 */
636cb5caa98Sdjl 	if (try_local(dbi, search_args) == 1) {
637cb5caa98Sdjl 		res = NSS_TRYLOCAL;
638cb5caa98Sdjl 		goto error_exit;
639cb5caa98Sdjl 	}
640cb5caa98Sdjl 
641cb5caa98Sdjl 	NSCD_SW_STATS(dbi).lookup_request_received++;
642cb5caa98Sdjl 	NSCD_SW_STATS(dbi).lookup_request_in_progress++;
643cb5caa98Sdjl 	NSCD_SW_STATS(dbi).lookup_request_queued++;
644cb5caa98Sdjl 
645cb5caa98Sdjl 	/* if lookup not enabled, return NSS_UNAVAIL  */
646cb5caa98Sdjl 	if (!(NSCD_SW_CFG_G.enable_lookup_g == nscd_true &&
647cb5caa98Sdjl 		NSCD_SW_CFG(dbi).enable_lookup == nscd_true)) {
648cb5caa98Sdjl 
649cb5caa98Sdjl 		_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
650cb5caa98Sdjl 		(me, "lookup not enabled for %s\n", NSCD_NSW_DB_NAME(dbi));
651cb5caa98Sdjl 
652cb5caa98Sdjl 		goto error_exit;
653cb5caa98Sdjl 	}
654cb5caa98Sdjl 
655cb5caa98Sdjl 	/* determine if loopback checking is configured */
656cb5caa98Sdjl 	if (NSCD_SW_CFG_G.enable_loopback_checking_g == nscd_true &&
657cb5caa98Sdjl 		NSCD_SW_CFG(dbi).enable_loopback_checking == nscd_true) {
658cb5caa98Sdjl 		check_loopback = 1;
659cb5caa98Sdjl 
660cb5caa98Sdjl 		_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
661cb5caa98Sdjl 		(me, "loopback checking enabled for %s\n",
662cb5caa98Sdjl 		NSCD_NSW_DB_NAME(dbi));
663cb5caa98Sdjl 	}
664cb5caa98Sdjl 
665cb5caa98Sdjl 	if (check_loopback) {
666cb5caa98Sdjl 		k = get_loopback_key();
667cb5caa98Sdjl 		if (k != NULL) {
668cb5caa98Sdjl 			if (k->dbi != dbi || k->fnum != search_fnum) {
669cb5caa98Sdjl 				clear_loopback_key(k);
670cb5caa98Sdjl 				k = NULL;
671cb5caa98Sdjl 			}
672cb5caa98Sdjl 		}
673cb5caa98Sdjl 	}
674cb5caa98Sdjl 
675cb5caa98Sdjl 	if (s == 0) {
676cb5caa98Sdjl 		nscd_rc_t	rc;
677cb5caa98Sdjl 
678cb5caa98Sdjl 		if (check_loopback) {
679cb5caa98Sdjl 			rc = _nscd_get_nsw_state_thread(&root_db, &params);
680cb5caa98Sdjl 			state_thr = 1;
681cb5caa98Sdjl 		} else
682cb5caa98Sdjl 			rc = _nscd_get_nsw_state(&root_db, &params);
683cb5caa98Sdjl 
684cb5caa98Sdjl 		NSCD_SW_STATS_G.lookup_request_queued_g--;
685cb5caa98Sdjl 		NSCD_SW_STATS(dbi).lookup_request_queued--;
686cb5caa98Sdjl 
687cb5caa98Sdjl 		if (rc != NSCD_SUCCESS)
688cb5caa98Sdjl 				goto error_exit;
689cb5caa98Sdjl 
690cb5caa98Sdjl 		s = (nscd_nsw_state_t *)root_db.s;
691cb5caa98Sdjl 	}
692cb5caa98Sdjl 
693cb5caa98Sdjl 	_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
694*e37190e5Smichen 	(me, "database = %s, config = >>%s<<\n", NSCD_NSW_DB_NAME(dbi),
695cb5caa98Sdjl 	(*s->nsw_cfg_p)->nsw_cfg_str);
696cb5caa98Sdjl 
697cb5caa98Sdjl 	for (n_src = 0;  n_src < s->max_src;  n_src++) {
698cb5caa98Sdjl 		nss_backend_t		*be;
699cb5caa98Sdjl 		nss_backend_op_t	funcp;
700cb5caa98Sdjl 		struct __nsw_lookup_v1	*lkp;
701cb5caa98Sdjl 		int			smf_state;
702cb5caa98Sdjl 		int			n_loop = 0;
703cb5caa98Sdjl 		int			max_retry = 10;
704cb5caa98Sdjl 
705cb5caa98Sdjl 		res = NSS_UNAVAIL;
706cb5caa98Sdjl 
707cb5caa98Sdjl 		if (n_src == 0)
708cb5caa98Sdjl 			lkp = s->config->lookups;
709cb5caa98Sdjl 		else
710cb5caa98Sdjl 			lkp = lkp->next;
711cb5caa98Sdjl 
712cb5caa98Sdjl 		/* set the number of max. retries */
713cb5caa98Sdjl 		if (lkp->actions[__NSW_TRYAGAIN] == __NSW_TRYAGAIN_NTIMES)
714cb5caa98Sdjl 			max_retry = lkp->max_retries;
715cb5caa98Sdjl 
716cb5caa98Sdjl 		srci = (*s->nsw_cfg_p)->src_idx[n_src];
717cb5caa98Sdjl 		if (swret != NULL)
718cb5caa98Sdjl 			swret->srci = srci;
719cb5caa98Sdjl 
720cb5caa98Sdjl 		_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
721cb5caa98Sdjl 		(me, "nsw source = %s\n", NSCD_NSW_SRC_NAME(srci));
722cb5caa98Sdjl 
723cb5caa98Sdjl 		/* if no privilege to look up, skip */
724cb5caa98Sdjl 		if (params.privdb == 1 && swret != NULL &&
725cb5caa98Sdjl 			strcmp(NSCD_NSW_SRC_NAME(srci), "files") == 0 &&
726cb5caa98Sdjl 			_nscd_get_client_euid() != 0) {
727cb5caa98Sdjl 			_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE,
728cb5caa98Sdjl 				NSCD_LOG_LEVEL_DEBUG)
729cb5caa98Sdjl 			(me, "no privilege to look up, skip source\n");
730cb5caa98Sdjl 
731cb5caa98Sdjl 			goto next_src;
732cb5caa98Sdjl 		}
733cb5caa98Sdjl 
734cb5caa98Sdjl 		/* get state of the (backend) client service */
735cb5caa98Sdjl 		smf_state = _nscd_get_smf_state(srci, dbi, 0);
736cb5caa98Sdjl 
737cb5caa98Sdjl 		/* stop if the source is one that should be TRYLOCAL */
738cb5caa98Sdjl 		if (smf_state == NSCD_SVC_STATE_UNKNOWN_SRC) {
739cb5caa98Sdjl 			_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE,
740cb5caa98Sdjl 					NSCD_LOG_LEVEL_DEBUG)
741cb5caa98Sdjl 			(me, "returning TRYLOCAL ... \n");
742cb5caa98Sdjl 			res = NSS_TRYLOCAL;
743cb5caa98Sdjl 			goto free_nsw_state;
744cb5caa98Sdjl 		}
745cb5caa98Sdjl 
746cb5caa98Sdjl 		if (check_loopback && k != NULL) {
747cb5caa98Sdjl 
748cb5caa98Sdjl 			if (k->srci == srci && k->dbi == dbi)
749cb5caa98Sdjl 				if (k->fnum == search_fnum) {
750cb5caa98Sdjl 
751cb5caa98Sdjl 					_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE,
752cb5caa98Sdjl 						NSCD_LOG_LEVEL_DEBUG)
753cb5caa98Sdjl 					(me, "loopback detected: "
754cb5caa98Sdjl 					"source = %s, database = %s "
755cb5caa98Sdjl 					"search fnum = %d\n",
756cb5caa98Sdjl 					NSCD_NSW_SRC_NAME(srci),
757cb5caa98Sdjl 					NSCD_NSW_DB_NAME(dbi), search_fnum);
758cb5caa98Sdjl 
759cb5caa98Sdjl 				NSCD_SW_STATS_G.loopback_nsw_db_skipped_g++;
760cb5caa98Sdjl 				NSCD_SW_STATS(dbi).loopback_nsw_db_skipped++;
761cb5caa98Sdjl 					continue;
762cb5caa98Sdjl 				}
763cb5caa98Sdjl 		}
764cb5caa98Sdjl 
765cb5caa98Sdjl 		be = s->be[n_src];
766cb5caa98Sdjl 		if (be != NULL)
767cb5caa98Sdjl 			funcp = NSS_LOOKUP_DBOP(be, search_fnum);
768cb5caa98Sdjl 
769cb5caa98Sdjl 		if ((params.dnsi >= 0 && be == 0) || (params.dnsi  < 0 &&
770cb5caa98Sdjl 			(be == 0 || (smf_state != NSCD_SVC_STATE_UNINITED &&
771cb5caa98Sdjl 			smf_state < SCF_STATE_ONLINE) || funcp == 0))) {
772cb5caa98Sdjl 
773cb5caa98Sdjl 			_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE,
774cb5caa98Sdjl 					NSCD_LOG_LEVEL_DEBUG)
775cb5caa98Sdjl 			(me, "unable to look up source %s: be = %p, "
776cb5caa98Sdjl 			"smf state = %d, funcp = %p\n",
777cb5caa98Sdjl 			NSCD_NSW_SRC_NAME(srci), be, smf_state, funcp);
778cb5caa98Sdjl 
779cb5caa98Sdjl 			goto next_src;
780cb5caa98Sdjl 		}
781cb5caa98Sdjl 
782cb5caa98Sdjl 		do {
783cb5caa98Sdjl 			/*
784cb5caa98Sdjl 			 * we can only retry max_retry times,
785cb5caa98Sdjl 			 * otherwise threads may get stuck in this
786cb5caa98Sdjl 			 * do-while loop forever
787cb5caa98Sdjl 			 */
788cb5caa98Sdjl 			if (n_loop > max_retry) {
789cb5caa98Sdjl 				if (swret != NULL)
790cb5caa98Sdjl 					res = NSS_TRYLOCAL;
791cb5caa98Sdjl 				goto free_nsw_state;
792cb5caa98Sdjl 			}
793cb5caa98Sdjl 
794cb5caa98Sdjl 			/*
795cb5caa98Sdjl 			 * set up to prevent loopback
796cb5caa98Sdjl 			 */
797cb5caa98Sdjl 			if (check_loopback && k == NULL) {
798cb5caa98Sdjl 				key.srci = srci;
799cb5caa98Sdjl 				key.dbi = dbi;
800cb5caa98Sdjl 				key.fnum = search_fnum;
801cb5caa98Sdjl 				key.lb_flagp = &check_loopback;
802cb5caa98Sdjl 				(void) set_loopback_key(&key);
803cb5caa98Sdjl 				k = &key;
804cb5caa98Sdjl 			}
805cb5caa98Sdjl 
806cb5caa98Sdjl 			_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE,
807cb5caa98Sdjl 					NSCD_LOG_LEVEL_DEBUG)
808cb5caa98Sdjl 			(me, "looking up source = %s, loop# = %d \n",
809cb5caa98Sdjl 			NSCD_NSW_SRC_NAME(srci), n_loop);
810cb5caa98Sdjl 
811cb5caa98Sdjl 			/*
812cb5caa98Sdjl 			 * search the backend, if hosts lookups,
813cb5caa98Sdjl 			 * try to get the hosts data with ttl first
814cb5caa98Sdjl 			 */
815cb5caa98Sdjl 			if (params.dnsi >= 0) {
816cb5caa98Sdjl 				res = search_dns_withttl(swret,
817cb5caa98Sdjl 					NSCD_NSW_SRC_NAME(srci),
818cb5caa98Sdjl 					params.dnsi);
819cb5caa98Sdjl 				/*
820cb5caa98Sdjl 				 * if not able to get ttl, fall back
821cb5caa98Sdjl 				 * to the regular backend call
822cb5caa98Sdjl 				 */
823cb5caa98Sdjl 				if (res == NSS_ERROR)
824cb5caa98Sdjl 					res = (*funcp)(be, search_args);
825cb5caa98Sdjl 				else {
826cb5caa98Sdjl 					/*
827cb5caa98Sdjl 					 * status/result are in the
828cb5caa98Sdjl 					 * packed buffer, not
829cb5caa98Sdjl 					 * search_args
830cb5caa98Sdjl 					 */
831cb5caa98Sdjl 					swret->noarg = 1;
832cb5caa98Sdjl 				}
833cb5caa98Sdjl 			} else
834cb5caa98Sdjl 				res = (*funcp)(be, search_args);
835cb5caa98Sdjl 			if (swret != NULL)
836cb5caa98Sdjl 				swret->errnum = errno;
837cb5caa98Sdjl 
838cb5caa98Sdjl 			/*
839cb5caa98Sdjl 			 * backend is not up, check and update the
840cb5caa98Sdjl 			 * smf state table
841cb5caa98Sdjl 			 */
842cb5caa98Sdjl 			if (res == NSS_UNAVAIL)
843cb5caa98Sdjl 				(void) _nscd_get_smf_state(srci, dbi, 1);
844cb5caa98Sdjl 
845cb5caa98Sdjl 			/*
846cb5caa98Sdjl 			 * may need to fall back to use the main nscd
847cb5caa98Sdjl 			 * if per-user lookup
848cb5caa98Sdjl 			 */
849cb5caa98Sdjl 			if (_whoami == NSCD_CHILD && swret != NULL)
850cb5caa98Sdjl 				swret->fallback = set_fallback_flag(
851cb5caa98Sdjl 				NSCD_NSW_SRC_NAME(srci), res);
852cb5caa98Sdjl 
853cb5caa98Sdjl 			_NSCD_LOG_IF(NSCD_LOG_SWITCH_ENGINE,
854cb5caa98Sdjl 					NSCD_LOG_LEVEL_DEBUG) {
855cb5caa98Sdjl 
856cb5caa98Sdjl 				/*
857cb5caa98Sdjl 				 * set up to trace the result/status
858cb5caa98Sdjl 				 * of the dns/ttl lookup
859cb5caa98Sdjl 				 */
860cb5caa98Sdjl 				if (swret != NULL && swret->noarg == 1) {
861cb5caa98Sdjl 					nss_pheader_t *phdr;
862cb5caa98Sdjl 					struct nss_XbyY_args *arg;
863cb5caa98Sdjl 					arg = (struct nss_XbyY_args *)
864cb5caa98Sdjl 						search_args;
865cb5caa98Sdjl 					phdr = (nss_pheader_t *)swret->pbuf;
866cb5caa98Sdjl 					arg->buf.buffer = (char *)phdr +
867cb5caa98Sdjl 						phdr->data_off;
868cb5caa98Sdjl 					arg->returnlen = phdr->data_len;
869cb5caa98Sdjl 					if (phdr->p_errno == ERANGE)
870cb5caa98Sdjl 						arg->erange = 1;
871cb5caa98Sdjl 					arg->h_errno = phdr->p_herrno;
872cb5caa98Sdjl 				}
873cb5caa98Sdjl 
874cb5caa98Sdjl 				trace_result(dbi, srci, search_fnum, res,
875cb5caa98Sdjl 				(nss_XbyY_args_t *)search_args);
876cb5caa98Sdjl 			}
877cb5caa98Sdjl 
878cb5caa98Sdjl 			n_loop++;
879cb5caa98Sdjl 		} while (retry_test(res, n_loop, lkp));
880cb5caa98Sdjl 
881cb5caa98Sdjl 		next_src:
882cb5caa98Sdjl 
883cb5caa98Sdjl 		status_vec |= (1 << res);
884cb5caa98Sdjl 
885cb5caa98Sdjl 		if (__NSW_ACTION_V1(lkp, res) == __NSW_RETURN) {
886cb5caa98Sdjl 			break;
887cb5caa98Sdjl 		}
888cb5caa98Sdjl 	}
889cb5caa98Sdjl 
890cb5caa98Sdjl 	free_nsw_state:
891cb5caa98Sdjl 
892cb5caa98Sdjl 	if (state_thr == 1)
893cb5caa98Sdjl 		_nscd_put_nsw_state_thread(s);
894cb5caa98Sdjl 	else
895cb5caa98Sdjl 		_nscd_put_nsw_state(s);
896cb5caa98Sdjl 	if (check_loopback && k != NULL)
897cb5caa98Sdjl 		clear_loopback_key(k);
898cb5caa98Sdjl 
899cb5caa98Sdjl 	if (res != NSS_SUCCESS)
900cb5caa98Sdjl 		goto error_exit;
901cb5caa98Sdjl 
902cb5caa98Sdjl 	NSCD_SW_STATS_G.lookup_request_succeeded_g++;
903cb5caa98Sdjl 	NSCD_SW_STATS(dbi).lookup_request_succeeded++;
904cb5caa98Sdjl 	NSCD_SW_STATS_G.lookup_request_in_progress_g--;
905cb5caa98Sdjl 	NSCD_SW_STATS(dbi).lookup_request_in_progress--;
906cb5caa98Sdjl 
907cb5caa98Sdjl 	return (NSS_SUCCESS);
908cb5caa98Sdjl 
909cb5caa98Sdjl 	error_exit:
910cb5caa98Sdjl 
911cb5caa98Sdjl 	NSCD_SW_STATS_G.lookup_request_failed_g++;
912cb5caa98Sdjl 	NSCD_SW_STATS_G.lookup_request_in_progress_g--;
913cb5caa98Sdjl 	NSCD_SW_STATS(dbi).lookup_request_failed++;
914cb5caa98Sdjl 	NSCD_SW_STATS(dbi).lookup_request_in_progress--;
915cb5caa98Sdjl 
916cb5caa98Sdjl 	return (res);
917cb5caa98Sdjl }
918cb5caa98Sdjl 
919cb5caa98Sdjl 
920cb5caa98Sdjl /* ===> get/set/endent */
921cb5caa98Sdjl 
922cb5caa98Sdjl static void		nss_setent_u(nss_db_root_t *,
923cb5caa98Sdjl 				    nss_db_initf_t,
924cb5caa98Sdjl 				    nss_getent_t *);
925cb5caa98Sdjl static nss_status_t	nss_getent_u(nss_db_root_t *,
926cb5caa98Sdjl 				    nss_db_initf_t,
927cb5caa98Sdjl 				    nss_getent_t *,
928cb5caa98Sdjl 				    void *);
929cb5caa98Sdjl static void		nss_endent_u(nss_db_root_t *,
930cb5caa98Sdjl 				    nss_db_initf_t,
931cb5caa98Sdjl 				    nss_getent_t *);
932cb5caa98Sdjl 
933cb5caa98Sdjl void
934cb5caa98Sdjl nss_setent(nss_db_root_t *rootp, nss_db_initf_t initf,
935cb5caa98Sdjl 	nss_getent_t *contextpp)
936cb5caa98Sdjl {
937cb5caa98Sdjl 	if (contextpp == 0)
938cb5caa98Sdjl 		return;
939cb5caa98Sdjl 	nss_setent_u(rootp, initf, contextpp);
940cb5caa98Sdjl }
941cb5caa98Sdjl 
942cb5caa98Sdjl nss_status_t
943cb5caa98Sdjl nss_getent(nss_db_root_t *rootp, nss_db_initf_t initf, nss_getent_t *contextpp,
944cb5caa98Sdjl 	void *args)
945cb5caa98Sdjl {
946cb5caa98Sdjl 	nss_status_t		status;
947cb5caa98Sdjl 
948cb5caa98Sdjl 	if (contextpp == 0) {
949cb5caa98Sdjl 		return (NSS_UNAVAIL);
950cb5caa98Sdjl 	}
951cb5caa98Sdjl 	status = nss_getent_u(rootp, initf, contextpp, args);
952cb5caa98Sdjl 	return (status);
953cb5caa98Sdjl }
954cb5caa98Sdjl 
955cb5caa98Sdjl void
956cb5caa98Sdjl nss_endent(nss_db_root_t *rootp, nss_db_initf_t initf,
957cb5caa98Sdjl 	nss_getent_t *contextpp)
958cb5caa98Sdjl {
959cb5caa98Sdjl 	if (contextpp == 0)
960cb5caa98Sdjl 		return;
961cb5caa98Sdjl 	nss_endent_u(rootp, initf, contextpp);
962cb5caa98Sdjl }
963cb5caa98Sdjl 
964cb5caa98Sdjl /*ARGSUSED*/
965cb5caa98Sdjl static void
966cb5caa98Sdjl end_iter_u(nss_db_root_t *rootp, struct nss_getent_context *contextp)
967cb5caa98Sdjl {
968cb5caa98Sdjl 	nscd_getent_context_t	*ctx;
969cb5caa98Sdjl 	nscd_nsw_state_t	*s;
970cb5caa98Sdjl 	nss_backend_t		*be;
971cb5caa98Sdjl 	int			n_src;
972cb5caa98Sdjl 
973cb5caa98Sdjl 	ctx = (nscd_getent_context_t *)contextp;
974cb5caa98Sdjl 	s = ctx->nsw_state;
975cb5caa98Sdjl 	n_src = ctx->n_src;
976cb5caa98Sdjl 	be = ctx->be;
977cb5caa98Sdjl 
978cb5caa98Sdjl 	if (s != 0) {
979cb5caa98Sdjl 		if (n_src < s->max_src && be != 0) {
980cb5caa98Sdjl 			(void) NSS_INVOKE_DBOP(be, NSS_DBOP_ENDENT, 0);
981cb5caa98Sdjl 			ctx->be = 0;  /* Should be unnecessary, but hey */
982cb5caa98Sdjl 		}
983cb5caa98Sdjl 	}
984cb5caa98Sdjl 	ctx->n_src = 0;
985cb5caa98Sdjl }
986cb5caa98Sdjl 
987cb5caa98Sdjl static void
988cb5caa98Sdjl nss_setent_u(nss_db_root_t *rootp, nss_db_initf_t initf,
989cb5caa98Sdjl 	nss_getent_t *contextpp)
990cb5caa98Sdjl {
991cb5caa98Sdjl 	char			*me = "nss_setent_u";
992cb5caa98Sdjl 	nscd_nsw_state_t	*s;
993cb5caa98Sdjl 	nscd_getent_context_t	*contextp;
994cb5caa98Sdjl 	nscd_nsw_params_t	params;
995cb5caa98Sdjl 	nss_db_root_t		root;
996cb5caa98Sdjl 	nss_backend_t		*be;
997cb5caa98Sdjl 	int			n_src, i;
998cb5caa98Sdjl 	nscd_sw_return_t	*swret = NULL;
999cb5caa98Sdjl 
1000cb5caa98Sdjl 	_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
1001cb5caa98Sdjl 	(me, "rootp = %p, initf = %p, contextpp = %p \n",
1002cb5caa98Sdjl 		rootp, initf, contextpp);
1003cb5caa98Sdjl 
1004cb5caa98Sdjl 	/* get the nsw db index via the initf function */
1005cb5caa98Sdjl 	(void) getparams(-1, initf, &params);
1006cb5caa98Sdjl 
1007cb5caa98Sdjl 	/* get address of the switch engine return data area */
1008cb5caa98Sdjl 	if (initf == nscd_initf)
1009cb5caa98Sdjl 		swret = (nscd_sw_return_t *)params.p.private;
1010cb5caa98Sdjl 
1011cb5caa98Sdjl 	/* if no privilege to look up, return */
1012cb5caa98Sdjl 	if (params.privdb == 1 && swret != NULL &&
1013cb5caa98Sdjl 		((nss_pheader_t *)(swret->pbuf))->p_euid != 0) {
1014cb5caa98Sdjl 
1015cb5caa98Sdjl 		_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
1016cb5caa98Sdjl 		(me, "no privilege \n");
1017cb5caa98Sdjl 		return;
1018cb5caa98Sdjl 	}
1019cb5caa98Sdjl 
1020cb5caa98Sdjl 	if ((contextp = (nscd_getent_context_t *)contextpp->ctx) == 0) {
1021cb5caa98Sdjl 		if ((_nscd_get_getent_ctx(contextpp, &params)) !=
1022cb5caa98Sdjl 			NSCD_SUCCESS) {
1023cb5caa98Sdjl 			return;
1024cb5caa98Sdjl 		}
1025cb5caa98Sdjl 		contextp = (nscd_getent_context_t *)contextpp->ctx;
1026cb5caa98Sdjl 	}
1027cb5caa98Sdjl 	s = contextp->nsw_state;
1028cb5caa98Sdjl 
1029cb5caa98Sdjl 	if (s == 0) {
1030cb5caa98Sdjl 		if (_nscd_get_nsw_state(&root, &params) !=
1031cb5caa98Sdjl 				NSCD_SUCCESS) {
1032cb5caa98Sdjl 			return;
1033cb5caa98Sdjl 		}
1034cb5caa98Sdjl 		s = (nscd_nsw_state_t *)root.s;
1035cb5caa98Sdjl 		contextp->nsw_state = s;
1036cb5caa98Sdjl 
1037cb5caa98Sdjl 	} else {
1038cb5caa98Sdjl 		s	= contextp->nsw_state;
1039cb5caa98Sdjl 		n_src	= contextp->n_src;
1040cb5caa98Sdjl 		be	= contextp->be;
1041cb5caa98Sdjl 		if (n_src == 0 && be != 0) {
1042cb5caa98Sdjl 			/*
1043cb5caa98Sdjl 			 * Optimization:  don't do endent, don't change
1044cb5caa98Sdjl 			 *   backends, just do the setent.  Look Ma, no locks
1045cb5caa98Sdjl 			 *   (nor any context that needs updating).
1046cb5caa98Sdjl 			 */
1047cb5caa98Sdjl 			(void) NSS_INVOKE_DBOP(be, NSS_DBOP_SETENT, 0);
1048cb5caa98Sdjl 			return;
1049cb5caa98Sdjl 		}
1050cb5caa98Sdjl 		if (n_src < s->max_src && be != 0) {
1051cb5caa98Sdjl 			(void) NSS_INVOKE_DBOP(be, NSS_DBOP_ENDENT, 0);
1052cb5caa98Sdjl 			contextp->be = 0;	/* Play it safe */
1053cb5caa98Sdjl 		}
1054cb5caa98Sdjl 	}
1055cb5caa98Sdjl 	for (n_src = 0, be = 0; n_src < s->max_src &&
1056cb5caa98Sdjl 		(be = s->be[n_src]) == 0; n_src++) {
1057cb5caa98Sdjl 		;
1058cb5caa98Sdjl 	}
1059cb5caa98Sdjl 
1060cb5caa98Sdjl 	contextp->n_src	= n_src;
1061cb5caa98Sdjl 	contextp->be	= be;
1062cb5caa98Sdjl 
1063cb5caa98Sdjl 	if (be == 0) {
1064cb5caa98Sdjl 		/* Things are broken enough that we can't do setent/getent */
1065cb5caa98Sdjl 		nss_endent_u(rootp, initf, contextpp);
1066cb5caa98Sdjl 		return;
1067cb5caa98Sdjl 	}
1068cb5caa98Sdjl 
1069cb5caa98Sdjl 	/*
1070cb5caa98Sdjl 	 * make sure all the backends are supported
1071cb5caa98Sdjl 	 */
1072cb5caa98Sdjl 	for (i = 0; i < s->max_src; i++) {
1073cb5caa98Sdjl 		int	st, srci;
1074cb5caa98Sdjl 
1075cb5caa98Sdjl 		srci = (*s->nsw_cfg_p)->src_idx[i];
1076cb5caa98Sdjl 		st = _nscd_get_smf_state(srci, params.dbi, 1);
1077cb5caa98Sdjl 		if (st == NSCD_SVC_STATE_UNKNOWN_SRC ||
1078cb5caa98Sdjl 				st == NSCD_SVC_STATE_UNINITED) {
1079cb5caa98Sdjl 			nss_endent_u(rootp, initf, contextpp);
1080cb5caa98Sdjl 
1081cb5caa98Sdjl 			_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE,
1082cb5caa98Sdjl 				NSCD_LOG_LEVEL_DEBUG)
1083cb5caa98Sdjl 			(me, "backend (%s) not available (state = %d)\n",
1084cb5caa98Sdjl 			NSCD_NSW_SRC_NAME(srci), st);
1085cb5caa98Sdjl 
1086cb5caa98Sdjl 			return;
1087cb5caa98Sdjl 		}
1088cb5caa98Sdjl 	}
1089cb5caa98Sdjl 
1090cb5caa98Sdjl 	(void) NSS_INVOKE_DBOP(be, NSS_DBOP_SETENT, 0);
1091cb5caa98Sdjl }
1092cb5caa98Sdjl 
1093cb5caa98Sdjl nss_status_t
1094cb5caa98Sdjl nss_getent_u(nss_db_root_t *rootp, nss_db_initf_t initf,
1095cb5caa98Sdjl 	nss_getent_t *contextpp, void *args)
1096cb5caa98Sdjl {
1097cb5caa98Sdjl 	char			*me = "nss_getent_u";
1098cb5caa98Sdjl 	nscd_nsw_state_t	*s;
1099cb5caa98Sdjl 	nscd_getent_context_t	*contextp;
1100cb5caa98Sdjl 	int			n_src;
1101cb5caa98Sdjl 	nss_backend_t		*be;
1102cb5caa98Sdjl 
1103cb5caa98Sdjl 	_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
1104cb5caa98Sdjl 	(me, "rootp = %p, initf = %p, contextpp = %p, args = %p\n",
1105cb5caa98Sdjl 		rootp, initf, contextpp, args);
1106cb5caa98Sdjl 
1107cb5caa98Sdjl 	if ((contextp = (nscd_getent_context_t *)contextpp->ctx) == 0) {
1108cb5caa98Sdjl 		nss_setent_u(rootp, initf, contextpp);
1109cb5caa98Sdjl 		if ((contextp = (nscd_getent_context_t *)contextpp->ctx) == 0) {
1110cb5caa98Sdjl 			/* Give up */
1111cb5caa98Sdjl 			_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE,
1112cb5caa98Sdjl 				NSCD_LOG_LEVEL_ERROR)
1113cb5caa98Sdjl 			(me, "not able to obtain getent context ... give up\n");
1114cb5caa98Sdjl 
1115cb5caa98Sdjl 			return (NSS_UNAVAIL);
1116cb5caa98Sdjl 		}
1117cb5caa98Sdjl 	}
1118cb5caa98Sdjl 
1119cb5caa98Sdjl 	s	= contextp->nsw_state;
1120cb5caa98Sdjl 	n_src	= contextp->n_src;
1121cb5caa98Sdjl 	be	= contextp->be;
1122cb5caa98Sdjl 
1123cb5caa98Sdjl 	if (s == 0) {
1124cb5caa98Sdjl 		/*
1125cb5caa98Sdjl 		 * We've done an end_iter() and haven't done nss_setent()
1126cb5caa98Sdjl 		 * or nss_endent() since;  we should stick in this state
1127cb5caa98Sdjl 		 * until the caller invokes one of those two routines.
1128cb5caa98Sdjl 		 */
1129cb5caa98Sdjl 		return (NSS_SUCCESS);
1130cb5caa98Sdjl 	}
1131cb5caa98Sdjl 
1132cb5caa98Sdjl 	while (n_src < s->max_src) {
1133cb5caa98Sdjl 		nss_status_t		res;
1134cb5caa98Sdjl 		struct __nsw_lookup_v1	*lkp = NULL;
1135cb5caa98Sdjl 		int			n;
1136cb5caa98Sdjl 
1137cb5caa98Sdjl 		/* get the nsw config for the current source */
1138cb5caa98Sdjl 		lkp = s->config->lookups;
1139cb5caa98Sdjl 		for (n = 0; n < n_src; n++)
1140cb5caa98Sdjl 			lkp = lkp->next;
1141cb5caa98Sdjl 
1142cb5caa98Sdjl 		if (be == 0) {
1143cb5caa98Sdjl 			/* If it's null it's a bug, but let's play safe */
1144cb5caa98Sdjl 			res = NSS_UNAVAIL;
1145cb5caa98Sdjl 		} else {
1146cb5caa98Sdjl 			_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE,
1147cb5caa98Sdjl 					NSCD_LOG_LEVEL_DEBUG)
1148cb5caa98Sdjl 			(me, "database: %s, backend: %s, nsswitch config: %s\n",
1149cb5caa98Sdjl 				NSCD_NSW_DB_NAME(s->dbi),
1150cb5caa98Sdjl 				lkp->service_name,
1151cb5caa98Sdjl 				(*s->nsw_cfg_p)->nsw_cfg_str);
1152cb5caa98Sdjl 
1153cb5caa98Sdjl 			res = NSS_INVOKE_DBOP(be, NSS_DBOP_GETENT, args);
1154cb5caa98Sdjl 		}
1155cb5caa98Sdjl 
1156cb5caa98Sdjl 		if (__NSW_ACTION_V1(lkp, res) == __NSW_RETURN) {
1157cb5caa98Sdjl 			if (res != __NSW_SUCCESS) {
1158cb5caa98Sdjl 				end_iter_u(rootp,
1159cb5caa98Sdjl 					(struct nss_getent_context *)contextp);
1160cb5caa98Sdjl 			}
1161cb5caa98Sdjl 			return (res);
1162cb5caa98Sdjl 		}
1163cb5caa98Sdjl 		(void) NSS_INVOKE_DBOP(be, NSS_DBOP_ENDENT, 0);
1164cb5caa98Sdjl 		do {
1165cb5caa98Sdjl 			n_src++;
1166cb5caa98Sdjl 		} while (n_src < s->max_src &&
1167cb5caa98Sdjl 				(be = s->be[n_src]) == 0);
1168cb5caa98Sdjl 		if (be == 0) {
1169cb5caa98Sdjl 			/*
1170cb5caa98Sdjl 			 * This is the case where we failed to get the backend
1171cb5caa98Sdjl 			 * for the last source. We exhausted all sources.
1172cb5caa98Sdjl 			 */
1173cb5caa98Sdjl 			nss_endent_u(rootp, initf, contextpp);
1174cb5caa98Sdjl 			return (NSS_SUCCESS);
1175cb5caa98Sdjl 		}
1176cb5caa98Sdjl 		contextp->n_src	= n_src;
1177cb5caa98Sdjl 		contextp->be	= be;
1178cb5caa98Sdjl 		(void) NSS_INVOKE_DBOP(be, NSS_DBOP_SETENT, 0);
1179cb5caa98Sdjl 	}
1180cb5caa98Sdjl 	/* Got to the end of the sources without finding another entry */
1181cb5caa98Sdjl 	end_iter_u(rootp, (struct nss_getent_context *)contextp);
1182cb5caa98Sdjl 	return (NSS_SUCCESS);
1183cb5caa98Sdjl 	/* success is either a successful entry or end of the sources */
1184cb5caa98Sdjl }
1185cb5caa98Sdjl 
1186cb5caa98Sdjl /*ARGSUSED*/
1187cb5caa98Sdjl void
1188cb5caa98Sdjl nss_endent_u(nss_db_root_t *rootp, nss_db_initf_t initf,
1189cb5caa98Sdjl 	nss_getent_t *contextpp)
1190cb5caa98Sdjl {
1191cb5caa98Sdjl 	char			*me = "nss_endent_u";
1192cb5caa98Sdjl 	nscd_getent_context_t	*contextp;
1193cb5caa98Sdjl 
1194cb5caa98Sdjl 	_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
1195cb5caa98Sdjl 	(me, "rootp = %p, initf = %p, contextpp = %p \n",
1196cb5caa98Sdjl 		rootp, initf, contextpp);
1197cb5caa98Sdjl 
1198cb5caa98Sdjl 	if ((contextp = (nscd_getent_context_t *)contextpp->ctx) == 0) {
1199cb5caa98Sdjl 		/* nss_endent() on an unused context is a no-op */
1200cb5caa98Sdjl 		return;
1201cb5caa98Sdjl 	}
1202cb5caa98Sdjl 	end_iter_u(rootp, (struct nss_getent_context *)contextp);
1203cb5caa98Sdjl 	_nscd_put_getent_ctx(contextp);
1204cb5caa98Sdjl 	contextpp->ctx = NULL;
1205cb5caa98Sdjl }
1206cb5caa98Sdjl 
1207cb5caa98Sdjl /*
1208cb5caa98Sdjl  * _nss_db_state_destr() and nss_delete() do nothing in nscd
1209cb5caa98Sdjl  * but is needed to make the caller (below nscd) happy
1210cb5caa98Sdjl  */
1211cb5caa98Sdjl /*ARGSUSED*/
1212cb5caa98Sdjl void
1213cb5caa98Sdjl _nss_db_state_destr(struct nss_db_state *s)
1214cb5caa98Sdjl {
1215cb5caa98Sdjl 	/* nsw state in nscd is always reused, so do nothing here */
1216cb5caa98Sdjl }
1217cb5caa98Sdjl 
1218cb5caa98Sdjl /*ARGSUSED*/
1219cb5caa98Sdjl void
1220cb5caa98Sdjl nss_delete(nss_db_root_t *rootp)
1221cb5caa98Sdjl {
1222cb5caa98Sdjl 	/*
1223cb5caa98Sdjl 	 * the only resource kept tracked by the nss_db_root_t
1224cb5caa98Sdjl 	 * is the nsw state which is always reused and no need
1225cb5caa98Sdjl 	 * to be freed. So just return.
1226cb5caa98Sdjl 	 */
1227cb5caa98Sdjl }
1228cb5caa98Sdjl 
1229cb5caa98Sdjl /*
1230cb5caa98Sdjl  * Start of nss_psearch/nss_psetent()/nss_pgetent()/nss_pendent()
1231cb5caa98Sdjl  * buffers switch entry points
1232cb5caa98Sdjl  */
1233cb5caa98Sdjl 
1234cb5caa98Sdjl /*
1235cb5caa98Sdjl  * nss_psearch opens a packed structure header, assembles a local
1236cb5caa98Sdjl  * nss_XbyY_args_t structure and calls the local copy of nss_search.
1237cb5caa98Sdjl  * The return data is assembled in "files native format" in the
1238cb5caa98Sdjl  * return buffer location.  Status if packed back up with the buffer
1239cb5caa98Sdjl  * and the whole wad is returned to the cache or the client.
1240cb5caa98Sdjl  */
1241cb5caa98Sdjl 
1242cb5caa98Sdjl void
1243cb5caa98Sdjl nss_psearch(void *buffer, size_t length)
1244cb5caa98Sdjl {
1245cb5caa98Sdjl 	/* inputs */
1246cb5caa98Sdjl 	nss_db_initf_t		initf;
1247cb5caa98Sdjl 	int			dbop;
1248cb5caa98Sdjl 	int			rc;
1249cb5caa98Sdjl 	nss_XbyY_args_t		arg;
1250cb5caa98Sdjl 	nss_status_t		status;
1251cb5caa98Sdjl 	nscd_sw_return_t	swret = { 0 }, *swrp = &swret;
1252cb5caa98Sdjl 	nss_pheader_t		*pbuf = (nss_pheader_t *)buffer;
1253cb5caa98Sdjl 	char			*me = "nss_psearch";
1254cb5caa98Sdjl 
1255cb5caa98Sdjl 	if (buffer == NULL || length == 0) {
1256cb5caa98Sdjl 		NSCD_RETURN_STATUS(pbuf, NSS_ERROR, EFAULT);
1257cb5caa98Sdjl 	}
1258cb5caa98Sdjl 
1259cb5caa98Sdjl 	status = nss_packed_arg_init(buffer, length,
1260cb5caa98Sdjl 			NULL, &initf, &dbop, &arg);
1261cb5caa98Sdjl 	if (status != NSS_SUCCESS) {
1262cb5caa98Sdjl 		NSCD_RETURN_STATUS(pbuf, status, -1);
1263cb5caa98Sdjl 	}
1264cb5caa98Sdjl 
1265cb5caa98Sdjl 	/*
1266cb5caa98Sdjl 	 * pass the address of the return data area
1267cb5caa98Sdjl 	 * for the switch engine to return its own data
1268cb5caa98Sdjl 	 */
1269cb5caa98Sdjl 	(void) memcpy(&pbuf->nscdpriv, &swrp, sizeof (swrp));
1270cb5caa98Sdjl 	swret.pbuf = buffer;
1271cb5caa98Sdjl 	swret.pbufsiz = length;
1272cb5caa98Sdjl 
1273cb5caa98Sdjl 	/*
1274cb5caa98Sdjl 	 * use the generic nscd_initf for all database lookups
1275cb5caa98Sdjl 	 * (the TSD key is the pointer to the packed header)
1276cb5caa98Sdjl 	 */
1277cb5caa98Sdjl 	rc = set_initf_key(pbuf);
1278cb5caa98Sdjl 	if (rc != 0) {
1279cb5caa98Sdjl 		NSCD_RETURN_STATUS(pbuf, NSS_UNAVAIL, EINVAL);
1280cb5caa98Sdjl 	}
1281cb5caa98Sdjl 	initf = nscd_initf;
1282cb5caa98Sdjl 
1283cb5caa98Sdjl 	/* Perform local search and pack results into return buffer */
1284cb5caa98Sdjl 	/* nscd's search ignores db_root */
1285cb5caa98Sdjl 	status = nss_search(NULL, initf, dbop, &arg);
1286cb5caa98Sdjl 
1287cb5caa98Sdjl 	/*
1288cb5caa98Sdjl 	 * If status is NSS_NOTFOUND and ldap also returned
1289cb5caa98Sdjl 	 * NSS_NOTFOUND, it is possible that the user does
1290cb5caa98Sdjl 	 * not have a credential, so check and see if
1291cb5caa98Sdjl 	 * needs to return NSS_ALTRETRY to let the main
1292cb5caa98Sdjl 	 * nscd get a chance to process the lookup
1293cb5caa98Sdjl 	 */
1294cb5caa98Sdjl 	if (swret.fallback == 1 && status == NSS_NOTFOUND) {
1295c70a8a3bSmichen 		OM_uint32	(*func)();
1296cb5caa98Sdjl 		OM_uint32	stat;
1297c70a8a3bSmichen 		nscd_rc_t	rc;
1298cb5caa98Sdjl 
1299c70a8a3bSmichen 		rc = get_gss_func((void **)&func);
1300c70a8a3bSmichen 		if (rc == NSCD_SUCCESS) {
1301c70a8a3bSmichen 			if (func(&stat, GSS_C_NO_CREDENTIAL,
1302cb5caa98Sdjl 				NULL, NULL, NULL, NULL) != GSS_S_COMPLETE) {
1303cb5caa98Sdjl 
1304cb5caa98Sdjl 				_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE,
1305cb5caa98Sdjl 					NSCD_LOG_LEVEL_DEBUG)
1306cb5caa98Sdjl 			(me, "NSS_ALTRETRY: fallback to main nscd needed\n");
1307cb5caa98Sdjl 
1308cb5caa98Sdjl 				status = NSS_ALTRETRY;
1309cb5caa98Sdjl 			}
1310cb5caa98Sdjl 		}
1311c70a8a3bSmichen 	}
1312cb5caa98Sdjl 
1313cb5caa98Sdjl 	NSCD_SET_STATUS(pbuf, status, -1);
1314cb5caa98Sdjl 	errno = swret.errnum;
1315cb5caa98Sdjl 
1316cb5caa98Sdjl 	/*
1317cb5caa98Sdjl 	 * move result/status from args to packed buffer only if
1318cb5caa98Sdjl 	 * arg was being used
1319cb5caa98Sdjl 	 */
1320cb5caa98Sdjl 	if (!swret.noarg)
1321cb5caa98Sdjl 		nss_packed_set_status(buffer, length, status,  &arg);
1322cb5caa98Sdjl 
1323cb5caa98Sdjl 	_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
1324cb5caa98Sdjl 	(me, "switch engine result: source is %s, status %d, "
1325cb5caa98Sdjl 	"herrno is %d, errno is %s\n",
1326cb5caa98Sdjl 	(swret.srci != -1) ? NSCD_NSW_SRC_NAME(swret.srci) : "<NOTSET>",
1327cb5caa98Sdjl 	pbuf->p_status, pbuf->p_herrno, strerror(pbuf->p_errno));
1328cb5caa98Sdjl 
1329cb5caa98Sdjl 	/* clear the TSD key used by the generic initf */
1330cb5caa98Sdjl 	clear_initf_key();
1331cb5caa98Sdjl 	pbuf->nscdpriv = 0;
1332cb5caa98Sdjl }
1333cb5caa98Sdjl 
1334cb5caa98Sdjl static void
1335cb5caa98Sdjl nscd_map_contextp(void *buffer, nss_getent_t *contextp,
1336*e37190e5Smichen 	nssuint_t **cookie_num_p, nssuint_t **seqnum_p, int setent)
1337cb5caa98Sdjl {
1338cb5caa98Sdjl 	nss_pheader_t		*pbuf = (nss_pheader_t *)buffer;
1339cb5caa98Sdjl 	nssuint_t		off;
1340cb5caa98Sdjl 	nscd_getent_context_t	*ctx;
1341cb5caa98Sdjl 	char			*me = "nscd_map_contextp";
1342*e37190e5Smichen 	nscd_getent_p1_cookie_t	*cookie;
1343cb5caa98Sdjl 
1344cb5caa98Sdjl 	if (buffer == NULL) {
1345cb5caa98Sdjl 		NSCD_RETURN_STATUS(pbuf, NSS_ERROR, EFAULT);
1346cb5caa98Sdjl 	}
1347cb5caa98Sdjl 
1348cb5caa98Sdjl 	off = pbuf->key_off;
1349*e37190e5Smichen 	cookie = (nscd_getent_p1_cookie_t *)((void *)((char *)buffer + off));
1350cb5caa98Sdjl 	if (seqnum_p != NULL)
1351*e37190e5Smichen 		*seqnum_p = &cookie->p1_seqnum;
1352cb5caa98Sdjl 
1353cb5caa98Sdjl 	/*
1354*e37190e5Smichen 	 * if called by nss_psetent, and the passed in cookie number
1355*e37190e5Smichen 	 * is NSCD_NEW_COOKIE, then there is no cookie yet, return a
1356*e37190e5Smichen 	 * pointer pointing to where the cookie number will be stored.
1357*e37190e5Smichen 	 * Also because there is no cookie to validate, just return
1358*e37190e5Smichen 	 * success.
1359cb5caa98Sdjl 	 *
1360*e37190e5Smichen 	 * On the other hand, if a cookie number is passed in, we need
1361*e37190e5Smichen 	 * to validate the cookie number before returning.
1362cb5caa98Sdjl 	 */
1363*e37190e5Smichen 	if (cookie_num_p != NULL)
1364*e37190e5Smichen 		*cookie_num_p = &cookie->p1_cookie_num;
1365*e37190e5Smichen 	if (setent == 1 && cookie->p1_cookie_num == NSCD_NEW_COOKIE) {
1366*e37190e5Smichen 			NSCD_RETURN_STATUS_SUCCESS(pbuf);
1367*e37190e5Smichen 	}
1368*e37190e5Smichen 
1369*e37190e5Smichen 	/*
1370*e37190e5Smichen 	 * If the sequence number and start time match nscd's p0 cookie,
1371*e37190e5Smichen 	 * then either setent was done twice in a row or this is the
1372*e37190e5Smichen 	 * first getent after the setent, return success as well.
1373*e37190e5Smichen 	 */
1374*e37190e5Smichen 	if (cookie->p1_seqnum == NSCD_P0_COOKIE_SEQNUM) {
1375*e37190e5Smichen 		nscd_getent_p0_cookie_t *p0c =
1376*e37190e5Smichen 			(nscd_getent_p0_cookie_t *)cookie;
1377*e37190e5Smichen 		if (p0c->p0_time == _nscd_get_start_time())
1378cb5caa98Sdjl 			NSCD_RETURN_STATUS_SUCCESS(pbuf);
1379cb5caa98Sdjl 	}
1380cb5caa98Sdjl 
1381cb5caa98Sdjl 	_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
1382*e37190e5Smichen 	(me, "cookie # = %lld,  sequence # = %lld\n",
1383*e37190e5Smichen 		cookie->p1_cookie_num, cookie->p1_seqnum);
1384cb5caa98Sdjl 
1385*e37190e5Smichen 	ctx = _nscd_is_getent_ctx(cookie->p1_cookie_num);
1386cb5caa98Sdjl 
1387cb5caa98Sdjl 	if (ctx == NULL) {
1388cb5caa98Sdjl 		_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
1389*e37190e5Smichen 		(me, "invalid cookie # (%lld)\n", cookie->p1_cookie_num);
1390cb5caa98Sdjl 
1391cb5caa98Sdjl 		NSCD_RETURN_STATUS(pbuf, NSS_ERROR, EFAULT);
1392cb5caa98Sdjl 	}
1393cb5caa98Sdjl 
1394*e37190e5Smichen 	/* if not called by nss_psetent, verify sequence number */
1395*e37190e5Smichen 	if (setent != 1 && ctx->seq_num !=
1396*e37190e5Smichen 			(nscd_seq_num_t)cookie->p1_seqnum) {
1397cb5caa98Sdjl 		_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
1398*e37190e5Smichen 		(me, "invalid sequence # (%lld)\n", cookie->p1_seqnum);
1399cb5caa98Sdjl 
1400cb5caa98Sdjl 		NSCD_RETURN_STATUS(pbuf, NSS_ERROR, EFAULT);
1401cb5caa98Sdjl 	}
1402cb5caa98Sdjl 
1403cb5caa98Sdjl 	contextp->ctx = (struct nss_getent_context *)ctx;
1404cb5caa98Sdjl 
1405cb5caa98Sdjl 	NSCD_RETURN_STATUS_SUCCESS(pbuf);
1406cb5caa98Sdjl }
1407cb5caa98Sdjl 
1408cb5caa98Sdjl void
1409cb5caa98Sdjl nss_psetent(void *buffer, size_t length, pid_t pid)
1410cb5caa98Sdjl {
1411cb5caa98Sdjl 	nss_getent_t		context = { 0 };
1412cb5caa98Sdjl 	nss_getent_t		*contextp = &context;
1413*e37190e5Smichen 	nssuint_t		*cookie_num_p;
1414*e37190e5Smichen 	nssuint_t		*seqnum_p;
1415cb5caa98Sdjl 	nss_pheader_t		*pbuf = (nss_pheader_t *)buffer;
1416*e37190e5Smichen 	nscd_getent_p0_cookie_t *p0c;
1417cb5caa98Sdjl 	char			*me = "nss_psetent";
1418cb5caa98Sdjl 
1419cb5caa98Sdjl 	if (buffer == NULL || length == 0) {
1420cb5caa98Sdjl 		NSCD_RETURN_STATUS(pbuf, NSS_ERROR, EFAULT);
1421cb5caa98Sdjl 	}
1422cb5caa98Sdjl 
1423cb5caa98Sdjl 	/*
1424cb5caa98Sdjl 	 * If this is a per-user nscd, and the user does not have
1425cb5caa98Sdjl 	 * the necessary credential, return NSS_TRYLOCAL, so the
1426cb5caa98Sdjl 	 * setent/getent can be done locally in the process of the
1427cb5caa98Sdjl 	 * setent call
1428cb5caa98Sdjl 	 */
1429cb5caa98Sdjl 	if (_whoami == NSCD_CHILD) {
1430c70a8a3bSmichen 		OM_uint32	(*func)();
1431cb5caa98Sdjl 		OM_uint32	stat;
1432c70a8a3bSmichen 		nscd_rc_t	rc;
1433cb5caa98Sdjl 
1434c70a8a3bSmichen 		rc = get_gss_func((void **)&func);
1435c70a8a3bSmichen 		if (rc == NSCD_SUCCESS) {
1436c70a8a3bSmichen 			if (func(&stat, GSS_C_NO_CREDENTIAL,
1437cb5caa98Sdjl 				NULL, NULL, NULL, NULL) != GSS_S_COMPLETE) {
1438cb5caa98Sdjl 
1439cb5caa98Sdjl 				_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE,
1440cb5caa98Sdjl 					NSCD_LOG_LEVEL_DEBUG)
1441cb5caa98Sdjl 			(me, "NSS_TRYLOCAL: fallback to caller process\n");
1442cb5caa98Sdjl 				NSCD_RETURN_STATUS(pbuf, NSS_TRYLOCAL, 0);
1443cb5caa98Sdjl 			}
1444cb5caa98Sdjl 		}
1445c70a8a3bSmichen 	}
1446cb5caa98Sdjl 
1447*e37190e5Smichen 	/* check cookie number */
1448*e37190e5Smichen 	nscd_map_contextp(buffer, contextp, &cookie_num_p, &seqnum_p, 1);
1449cb5caa98Sdjl 	if (NSCD_STATUS_IS_NOT_OK(pbuf))
1450cb5caa98Sdjl 		return;
1451*e37190e5Smichen 
1452*e37190e5Smichen 	/* set cookie number and sequence number */
1453*e37190e5Smichen 	p0c = (nscd_getent_p0_cookie_t *)cookie_num_p;
1454*e37190e5Smichen 	if (contextp->ctx ==  NULL) {
1455*e37190e5Smichen 		/*
1456*e37190e5Smichen 		 * first setent (no existing getent context),
1457*e37190e5Smichen 		 * return a p0 cookie
1458*e37190e5Smichen 		 */
1459*e37190e5Smichen 		_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
1460*e37190e5Smichen 		(me, "first setent, no getent context yet\n");
1461*e37190e5Smichen 	} else {
1462*e37190e5Smichen 		/*
1463*e37190e5Smichen 		 * doing setent on an existing getent context,
1464*e37190e5Smichen 		 * release resources allocated and return a
1465*e37190e5Smichen 		 * p0 cookie
1466*e37190e5Smichen 		 */
1467*e37190e5Smichen 		_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
1468*e37190e5Smichen 		(me, "setent resetting sequence number = %lld\n",  *seqnum_p);
1469*e37190e5Smichen 
1470*e37190e5Smichen 		_nscd_put_getent_ctx((nscd_getent_context_t *)contextp->ctx);
1471*e37190e5Smichen 		contextp->ctx = NULL;
1472*e37190e5Smichen 	}
1473*e37190e5Smichen 
1474*e37190e5Smichen 	p0c->p0_pid = pid;
1475*e37190e5Smichen 	p0c->p0_time = _nscd_get_start_time();
1476*e37190e5Smichen 	p0c->p0_seqnum = NSCD_P0_COOKIE_SEQNUM;
1477*e37190e5Smichen 	_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
1478*e37190e5Smichen 	(me, "returning a p0 cookie: pid = %ld, time = %ld, seq #= %llx\n",
1479*e37190e5Smichen 		p0c->p0_pid, p0c->p0_time, p0c->p0_seqnum);
1480*e37190e5Smichen 
1481*e37190e5Smichen 	NSCD_RETURN_STATUS(pbuf, NSS_SUCCESS, 0);
1482*e37190e5Smichen }
1483*e37190e5Smichen 
1484*e37190e5Smichen static void
1485*e37190e5Smichen delayed_setent(nss_pheader_t *pbuf, nss_db_initf_t initf,
1486*e37190e5Smichen 	nss_getent_t *contextp, nssuint_t *cookie_num_p,
1487*e37190e5Smichen 	nssuint_t *seqnum_p, pid_t pid)
1488*e37190e5Smichen {
1489*e37190e5Smichen 	nscd_getent_context_t	*ctx;
1490*e37190e5Smichen 	nscd_sw_return_t	swret = { 0 }, *swrp = &swret;
1491*e37190e5Smichen 	char			*me = "delayed_setent";
1492*e37190e5Smichen 
1493*e37190e5Smichen 	/*
1494*e37190e5Smichen 	 * check credential
1495*e37190e5Smichen 	 */
1496*e37190e5Smichen 	_nscd_APP_check_cred(pbuf, &pid, "NSCD_DELAYED_SETENT",
1497*e37190e5Smichen 		NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_ERROR);
1498*e37190e5Smichen 	if (NSCD_STATUS_IS_NOT_OK(pbuf)) {
1499*e37190e5Smichen 		_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
1500*e37190e5Smichen 		(me, "invalid credential\n");
1501*e37190e5Smichen 		return;
1502*e37190e5Smichen 	}
1503*e37190e5Smichen 
1504cb5caa98Sdjl 	/*
1505cb5caa98Sdjl 	 * pass the packed header buffer pointer to nss_setent
1506cb5caa98Sdjl 	 */
1507cb5caa98Sdjl 	(void) memcpy(&pbuf->nscdpriv, &swrp, sizeof (swrp));
1508*e37190e5Smichen 	swret.pbuf = pbuf;
1509cb5caa98Sdjl 
1510cb5caa98Sdjl 	/* Perform local setent and set context */
1511cb5caa98Sdjl 	nss_setent(NULL, initf, contextp);
1512cb5caa98Sdjl 
1513*e37190e5Smichen 	/* insert cookie info into packed buffer header */
1514cb5caa98Sdjl 	ctx = (nscd_getent_context_t *)contextp->ctx;
1515cb5caa98Sdjl 	if (ctx != NULL) {
1516*e37190e5Smichen 		*cookie_num_p = ctx->cookie_num;
1517*e37190e5Smichen 		*seqnum_p = ctx->seq_num;
1518cb5caa98Sdjl 		ctx->pid = pid;
1519cb5caa98Sdjl 	} else {
1520cb5caa98Sdjl 		/*
1521cb5caa98Sdjl 		 * not able to allocate a getent context, the
1522cb5caa98Sdjl 		 * client should try the enumeration locally
1523cb5caa98Sdjl 		 */
1524*e37190e5Smichen 		*cookie_num_p = NSCD_LOCAL_COOKIE;
1525*e37190e5Smichen 		*seqnum_p = 0;
1526cb5caa98Sdjl 
1527cb5caa98Sdjl 		_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
1528*e37190e5Smichen 		(me, "NSS_TRYLOCAL: cookie # = %lld,  sequence # = %lld\n",
1529*e37190e5Smichen 		*cookie_num_p, *seqnum_p);
1530cb5caa98Sdjl 		NSCD_RETURN_STATUS(pbuf, NSS_TRYLOCAL, 0);
1531cb5caa98Sdjl 	}
1532*e37190e5Smichen 
1533*e37190e5Smichen 	_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
1534*e37190e5Smichen 	(me, "NSS_SUCCESS: cookie # = %lld,  sequence # = %lld\n",
1535*e37190e5Smichen 	ctx->cookie_num, ctx->seq_num);
1536*e37190e5Smichen 
1537*e37190e5Smichen 	NSCD_RETURN_STATUS(pbuf, NSS_SUCCESS, 0);
1538cb5caa98Sdjl }
1539cb5caa98Sdjl 
1540cb5caa98Sdjl void
1541cb5caa98Sdjl nss_pgetent(void *buffer, size_t length)
1542cb5caa98Sdjl {
1543cb5caa98Sdjl 	/* inputs */
1544cb5caa98Sdjl 	nss_db_initf_t		initf;
1545*e37190e5Smichen 	nss_getent_t		context = { 0 };
1546cb5caa98Sdjl 	nss_getent_t		*contextp = &context;
1547*e37190e5Smichen 	nss_XbyY_args_t		arg = { 0};
1548cb5caa98Sdjl 	nss_status_t		status;
1549*e37190e5Smichen 	nssuint_t		*cookie_num_p;
1550*e37190e5Smichen 	nssuint_t		*seqnum_p;
1551cb5caa98Sdjl 	nscd_getent_context_t	*ctx;
1552cb5caa98Sdjl 	int			rc;
1553cb5caa98Sdjl 	nss_pheader_t		*pbuf = (nss_pheader_t *)buffer;
1554cb5caa98Sdjl 	char			*me = "nss_pgetent";
1555cb5caa98Sdjl 
1556cb5caa98Sdjl 	if (buffer == NULL || length == 0) {
1557cb5caa98Sdjl 		NSCD_RETURN_STATUS(pbuf, NSS_ERROR, EFAULT);
1558cb5caa98Sdjl 	}
1559cb5caa98Sdjl 
1560*e37190e5Smichen 	/* verify the cookie passed in */
1561*e37190e5Smichen 	nscd_map_contextp(buffer, contextp, &cookie_num_p, &seqnum_p, 0);
1562*e37190e5Smichen 	if (NSCD_STATUS_IS_NOT_OK(pbuf))
1563*e37190e5Smichen 		return;
1564cb5caa98Sdjl 	/*
1565cb5caa98Sdjl 	 * use the generic nscd_initf for all the getent requests
1566cb5caa98Sdjl 	 * (the TSD key is the pointer to the packed header)
1567cb5caa98Sdjl 	 */
1568cb5caa98Sdjl 	rc = set_initf_key(pbuf);
1569cb5caa98Sdjl 	if (rc != 0) {
1570cb5caa98Sdjl 		NSCD_RETURN_STATUS(pbuf, NSS_UNAVAIL, EINVAL);
1571cb5caa98Sdjl 	}
1572cb5caa98Sdjl 	initf = nscd_initf;
1573cb5caa98Sdjl 
1574*e37190e5Smichen 	/* if no context yet, get one */
1575*e37190e5Smichen 	if (contextp->ctx ==  NULL) {
1576*e37190e5Smichen 		nscd_getent_p0_cookie_t *p0c =
1577*e37190e5Smichen 			(nscd_getent_p0_cookie_t *)cookie_num_p;
1578cb5caa98Sdjl 
1579*e37190e5Smichen 		delayed_setent(pbuf, initf, contextp, cookie_num_p,
1580*e37190e5Smichen 			seqnum_p, p0c->p0_pid);
1581*e37190e5Smichen 		if (NSCD_STATUS_IS_NOT_OK(pbuf)) {
1582*e37190e5Smichen 			clear_initf_key();
1583cb5caa98Sdjl 			return;
1584*e37190e5Smichen 		}
1585*e37190e5Smichen 	}
1586*e37190e5Smichen 
1587*e37190e5Smichen 	status = nss_packed_context_init(buffer, length,
1588*e37190e5Smichen 			NULL, &initf, &contextp, &arg);
1589*e37190e5Smichen 	if (status != NSS_SUCCESS) {
1590*e37190e5Smichen 		NSCD_RETURN_STATUS(pbuf, status, -1);
1591*e37190e5Smichen 	}
1592cb5caa98Sdjl 
1593cb5caa98Sdjl 	/* Perform local search and pack results into return buffer */
1594cb5caa98Sdjl 	status = nss_getent(NULL, initf, contextp, &arg);
1595cb5caa98Sdjl 	NSCD_SET_STATUS(pbuf, status, -1);
1596cb5caa98Sdjl 	nss_packed_set_status(buffer, length, status,  &arg);
1597cb5caa98Sdjl 
1598cb5caa98Sdjl 	/* increment sequence number in the buffer and nscd context */
1599cb5caa98Sdjl 	if (status == NSS_SUCCESS) {
1600cb5caa98Sdjl 		ctx = (nscd_getent_context_t *)contextp->ctx;
1601cb5caa98Sdjl 		ctx->seq_num++;
1602*e37190e5Smichen 		*seqnum_p = ctx->seq_num;
1603*e37190e5Smichen 		*cookie_num_p = ctx->cookie_num;
1604cb5caa98Sdjl 
1605cb5caa98Sdjl 		_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
1606*e37190e5Smichen 		(me, "getent OK, new sequence # = %lld, len = %lld,"
1607*e37190e5Smichen 		" data = >>%s<<\n", *seqnum_p,
1608cb5caa98Sdjl 		pbuf->data_len, (char *)buffer + pbuf->data_off);
1609cb5caa98Sdjl 	} else {
1610124771bbSmichen 		/* release the resources used */
1611cb5caa98Sdjl 		ctx = (nscd_getent_context_t *)contextp->ctx;
1612124771bbSmichen 		if (ctx != NULL) {
1613124771bbSmichen 			_nscd_put_getent_ctx(ctx);
1614124771bbSmichen 			contextp->ctx = NULL;
1615124771bbSmichen 		}
1616cb5caa98Sdjl 		_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
1617*e37190e5Smichen 		(me, "getent failed, status = %d, sequence # = %lld\n",
1618*e37190e5Smichen 			status, *seqnum_p);
1619cb5caa98Sdjl 	}
1620cb5caa98Sdjl 
1621cb5caa98Sdjl 	/* clear the TSD key used by the generic initf */
1622cb5caa98Sdjl 	clear_initf_key();
1623cb5caa98Sdjl }
1624cb5caa98Sdjl 
1625cb5caa98Sdjl void
1626cb5caa98Sdjl nss_pendent(void *buffer, size_t length)
1627cb5caa98Sdjl {
1628*e37190e5Smichen 	nss_getent_t		context = { 0 };
1629cb5caa98Sdjl 	nss_getent_t		*contextp = &context;
1630*e37190e5Smichen 	nssuint_t		*seqnum_p;
1631*e37190e5Smichen 	nssuint_t		*cookie_num_p;
1632cb5caa98Sdjl 	nss_pheader_t		*pbuf = (nss_pheader_t *)buffer;
1633cb5caa98Sdjl 	char			*me = "nss_pendent";
1634cb5caa98Sdjl 
1635cb5caa98Sdjl 	if (buffer == NULL || length == 0) {
1636cb5caa98Sdjl 		NSCD_RETURN_STATUS(pbuf, NSS_ERROR, EFAULT);
1637cb5caa98Sdjl 	}
1638cb5caa98Sdjl 
1639cb5caa98Sdjl 	/* map the contextp from the cookie information */
1640*e37190e5Smichen 	nscd_map_contextp(buffer, contextp, &cookie_num_p, &seqnum_p, 0);
1641cb5caa98Sdjl 	if (NSCD_STATUS_IS_NOT_OK(pbuf))
1642cb5caa98Sdjl 		return;
1643cb5caa98Sdjl 
1644*e37190e5Smichen 	if (contextp->ctx == NULL)
1645*e37190e5Smichen 		return;
1646*e37190e5Smichen 
1647cb5caa98Sdjl 	_NSCD_LOG(NSCD_LOG_SWITCH_ENGINE, NSCD_LOG_LEVEL_DEBUG)
1648*e37190e5Smichen 	(me, "endent, cookie = %lld, sequence # = %lld\n",
1649*e37190e5Smichen 		*cookie_num_p, *seqnum_p);
1650cb5caa98Sdjl 
1651cb5caa98Sdjl 	/* Perform local endent and reset context */
1652cb5caa98Sdjl 	nss_endent(NULL, NULL, contextp);
1653cb5caa98Sdjl 	NSCD_RETURN_STATUS(pbuf, NSS_SUCCESS, 0);
1654cb5caa98Sdjl }
1655cb5caa98Sdjl 
1656cb5caa98Sdjl /*ARGSUSED*/
1657cb5caa98Sdjl void
1658cb5caa98Sdjl nss_pdelete(void *buffer, size_t length)
1659cb5caa98Sdjl {
1660cb5caa98Sdjl 	nss_pheader_t	*pbuf = (nss_pheader_t *)buffer;
1661cb5caa98Sdjl 
1662cb5caa98Sdjl 	/* unnecessary, kept for completeness */
1663cb5caa98Sdjl 	NSCD_RETURN_STATUS_SUCCESS(pbuf);
1664cb5caa98Sdjl }
1665