xref: /titanic_51/usr/src/cmd/nscd/getuser.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * Routines to handle getuser* calls in nscd
31*7c478bd9Sstevel@tonic-gate  */
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <assert.h>
34*7c478bd9Sstevel@tonic-gate #include <errno.h>
35*7c478bd9Sstevel@tonic-gate #include <memory.h>
36*7c478bd9Sstevel@tonic-gate #include <signal.h>
37*7c478bd9Sstevel@tonic-gate #include <stdio.h>
38*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
39*7c478bd9Sstevel@tonic-gate #include <string.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/door.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/wait.h>
45*7c478bd9Sstevel@tonic-gate #include <thread.h>
46*7c478bd9Sstevel@tonic-gate #include <unistd.h>
47*7c478bd9Sstevel@tonic-gate #include <ucred.h>
48*7c478bd9Sstevel@tonic-gate #include <nss_common.h>
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate #include <user_attr.h>
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate #include <getxby_door.h>
53*7c478bd9Sstevel@tonic-gate #include "server_door.h"
54*7c478bd9Sstevel@tonic-gate #include "nscd.h"
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate extern userstr_t *_getusernam(const char *, userstr_t *, char *, int, int *);
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate static hash_t *nam_hash;
59*7c478bd9Sstevel@tonic-gate static mutex_t  db_lock = DEFAULTMUTEX;
60*7c478bd9Sstevel@tonic-gate static waiter_t db_wait;
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate static void getuser_namekeepalive(int keep, int interval);
63*7c478bd9Sstevel@tonic-gate static void update_user_bucket(nsc_bucket_t **old, nsc_bucket_t *new,
64*7c478bd9Sstevel@tonic-gate     int callnumber);
65*7c478bd9Sstevel@tonic-gate static nsc_bucket_t *fixbuffer(nsc_return_t *in, int maxlen);
66*7c478bd9Sstevel@tonic-gate static void do_findgnams(nsc_bucket_t *ptr, int *table, char *gnam);
67*7c478bd9Sstevel@tonic-gate static void do_invalidate(nsc_bucket_t **ptr, int callnumber);
68*7c478bd9Sstevel@tonic-gate static void getuser_invalidate_unlocked(void);
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate void
71*7c478bd9Sstevel@tonic-gate getuser_init(void)
72*7c478bd9Sstevel@tonic-gate {
73*7c478bd9Sstevel@tonic-gate 	nam_hash = make_hash(current_admin.user.nsc_suggestedsize);
74*7c478bd9Sstevel@tonic-gate }
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate static void
77*7c478bd9Sstevel@tonic-gate do_invalidate(nsc_bucket_t ** ptr, int callnumber)
78*7c478bd9Sstevel@tonic-gate {
79*7c478bd9Sstevel@tonic-gate 	if (*ptr != NULL && *ptr != (nsc_bucket_t *)-1) {
80*7c478bd9Sstevel@tonic-gate 		/* leave pending calls alone */
81*7c478bd9Sstevel@tonic-gate 		update_user_bucket(ptr, NULL, callnumber);
82*7c478bd9Sstevel@tonic-gate 	}
83*7c478bd9Sstevel@tonic-gate }
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate static void
86*7c478bd9Sstevel@tonic-gate do_findgnams(nsc_bucket_t *ptr, int *table, char *gnam)
87*7c478bd9Sstevel@tonic-gate {
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	/*
90*7c478bd9Sstevel@tonic-gate 	 * be careful with ptr - it may be -1 or NULL.
91*7c478bd9Sstevel@tonic-gate 	 */
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 	if (ptr != NULL && ptr != (nsc_bucket_t *)-1) {
94*7c478bd9Sstevel@tonic-gate 		char *tmp = (char *)insertn(table, ptr->nsc_hits,
95*7c478bd9Sstevel@tonic-gate 		    (int)strdup(gnam));
96*7c478bd9Sstevel@tonic-gate 		if (tmp != (char *)-1)
97*7c478bd9Sstevel@tonic-gate 			free(tmp);
98*7c478bd9Sstevel@tonic-gate 	}
99*7c478bd9Sstevel@tonic-gate }
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate void
102*7c478bd9Sstevel@tonic-gate getuser_revalidate(void)
103*7c478bd9Sstevel@tonic-gate {
104*7c478bd9Sstevel@tonic-gate 	for (;;) {
105*7c478bd9Sstevel@tonic-gate 		int slp;
106*7c478bd9Sstevel@tonic-gate 		int interval;
107*7c478bd9Sstevel@tonic-gate 		int count;
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 		slp = current_admin.user.nsc_pos_ttl;
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 		if (slp < 60) {
112*7c478bd9Sstevel@tonic-gate 			slp = 60;
113*7c478bd9Sstevel@tonic-gate 		}
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 		if ((count = current_admin.user.nsc_keephot) != 0) {
116*7c478bd9Sstevel@tonic-gate 			interval = (slp / 2)/count;
117*7c478bd9Sstevel@tonic-gate 			if (interval == 0) interval = 1;
118*7c478bd9Sstevel@tonic-gate 			sleep(slp * 2 / 3);
119*7c478bd9Sstevel@tonic-gate 			getuser_namekeepalive(count, interval);
120*7c478bd9Sstevel@tonic-gate 		} else {
121*7c478bd9Sstevel@tonic-gate 			sleep(slp);
122*7c478bd9Sstevel@tonic-gate 		}
123*7c478bd9Sstevel@tonic-gate 	}
124*7c478bd9Sstevel@tonic-gate }
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate static void
127*7c478bd9Sstevel@tonic-gate getuser_namekeepalive(int keep, int interval)
128*7c478bd9Sstevel@tonic-gate {
129*7c478bd9Sstevel@tonic-gate 	int *table;
130*7c478bd9Sstevel@tonic-gate 	union {
131*7c478bd9Sstevel@tonic-gate 		nsc_data_t  ping;
132*7c478bd9Sstevel@tonic-gate 		char space[sizeof (nsc_data_t) + NSCDMAXNAMELEN];
133*7c478bd9Sstevel@tonic-gate 	} u;
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	int i;
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 	if (!keep)
138*7c478bd9Sstevel@tonic-gate 		return;
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 	table = maken(keep);
141*7c478bd9Sstevel@tonic-gate 	mutex_lock(&db_lock);
142*7c478bd9Sstevel@tonic-gate 	operate_hash(nam_hash, do_findgnams, (char *)table);
143*7c478bd9Sstevel@tonic-gate 	mutex_unlock(&db_lock);
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 	for (i = 1; i <= keep; i++) {
146*7c478bd9Sstevel@tonic-gate 		char *tmp;
147*7c478bd9Sstevel@tonic-gate 		u.ping.nsc_call.nsc_callnumber = GETUSERNAM;
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 		if ((tmp = (char *)table[keep + 1 + i]) == (char *)-1)
150*7c478bd9Sstevel@tonic-gate 			continue; /* unused slot in table */
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 		strcpy(u.ping.nsc_call.nsc_u.name, tmp);
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate 		launch_update(&u.ping.nsc_call);
155*7c478bd9Sstevel@tonic-gate 		sleep(interval);
156*7c478bd9Sstevel@tonic-gate 	}
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate 	for (i = 1; i <= keep; i++) {
159*7c478bd9Sstevel@tonic-gate 		char *tmp;
160*7c478bd9Sstevel@tonic-gate 		if ((tmp = (char *)table[keep + 1 + i]) != (char *)-1)
161*7c478bd9Sstevel@tonic-gate 			free(tmp);
162*7c478bd9Sstevel@tonic-gate 	}
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate 	free(table);
165*7c478bd9Sstevel@tonic-gate }
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate /*
169*7c478bd9Sstevel@tonic-gate  *   This routine marks all entries as invalid
170*7c478bd9Sstevel@tonic-gate  *
171*7c478bd9Sstevel@tonic-gate  */
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate void
174*7c478bd9Sstevel@tonic-gate getuser_invalidate()
175*7c478bd9Sstevel@tonic-gate {
176*7c478bd9Sstevel@tonic-gate 	mutex_lock(&db_lock);
177*7c478bd9Sstevel@tonic-gate 	getuser_invalidate_unlocked();
178*7c478bd9Sstevel@tonic-gate 	mutex_unlock(&db_lock);
179*7c478bd9Sstevel@tonic-gate }
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate static void
182*7c478bd9Sstevel@tonic-gate getuser_invalidate_unlocked()
183*7c478bd9Sstevel@tonic-gate {
184*7c478bd9Sstevel@tonic-gate 	operate_hash_addr(nam_hash, do_invalidate, (char *)GETUSERNAM);
185*7c478bd9Sstevel@tonic-gate 	current_admin.user.nsc_invalidate_count++;
186*7c478bd9Sstevel@tonic-gate }
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate void
189*7c478bd9Sstevel@tonic-gate getuser_lookup(nsc_return_t *out, int maxsize, nsc_call_t *in, time_t now)
190*7c478bd9Sstevel@tonic-gate {
191*7c478bd9Sstevel@tonic-gate 	int		out_of_date;
192*7c478bd9Sstevel@tonic-gate 	nsc_bucket_t	*retb;
193*7c478bd9Sstevel@tonic-gate 	char 		**bucket;
194*7c478bd9Sstevel@tonic-gate 
195*7c478bd9Sstevel@tonic-gate 	static time_t	lastmod;
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 	int bufferspace = maxsize - sizeof (nsc_return_t);
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate 	if (current_admin.user.nsc_enabled == 0) {
200*7c478bd9Sstevel@tonic-gate 		out->nsc_return_code = NOSERVER;
201*7c478bd9Sstevel@tonic-gate 		out->nsc_bufferbytesused = sizeof (*out);
202*7c478bd9Sstevel@tonic-gate 		return;
203*7c478bd9Sstevel@tonic-gate 	}
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate 	mutex_lock(&db_lock);
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate 	if (current_admin.user.nsc_check_files) {
208*7c478bd9Sstevel@tonic-gate 		struct stat buf;
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate 		if (stat(USERATTR_FILENAME, &buf) < 0) {
211*7c478bd9Sstevel@tonic-gate 			/*EMPTY*/;
212*7c478bd9Sstevel@tonic-gate 		} else if (lastmod == 0) {
213*7c478bd9Sstevel@tonic-gate 			lastmod = buf.st_mtime;
214*7c478bd9Sstevel@tonic-gate 		} else if (lastmod < buf.st_mtime) {
215*7c478bd9Sstevel@tonic-gate 			getuser_invalidate_unlocked();
216*7c478bd9Sstevel@tonic-gate 			lastmod = buf.st_mtime;
217*7c478bd9Sstevel@tonic-gate 		}
218*7c478bd9Sstevel@tonic-gate 	}
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_ALL) {
221*7c478bd9Sstevel@tonic-gate 		logit("getuser_lookup: looking for name %s\n",
222*7c478bd9Sstevel@tonic-gate 				in->nsc_u.name);
223*7c478bd9Sstevel@tonic-gate 	}
224*7c478bd9Sstevel@tonic-gate 
225*7c478bd9Sstevel@tonic-gate 	for (;;) {
226*7c478bd9Sstevel@tonic-gate 		if (attr_strlen(in->nsc_u.name) > NSCDMAXNAMELEN) {
227*7c478bd9Sstevel@tonic-gate 			ucred_t *uc = NULL;
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate 			if (door_ucred(&uc) != 0) {
230*7c478bd9Sstevel@tonic-gate 				logit("getuser_lookup: Name too long, "
231*7c478bd9Sstevel@tonic-gate 				    "but no user credential: %s\n",
232*7c478bd9Sstevel@tonic-gate 				    strerror(errno));
233*7c478bd9Sstevel@tonic-gate 			} else {
234*7c478bd9Sstevel@tonic-gate 				logit("getuser_lookup: Name too long "
235*7c478bd9Sstevel@tonic-gate 				    "from pid %d uid %d\n",
236*7c478bd9Sstevel@tonic-gate 				    ucred_getpid(uc),
237*7c478bd9Sstevel@tonic-gate 				    ucred_getruid(uc));
238*7c478bd9Sstevel@tonic-gate 				ucred_free(uc);
239*7c478bd9Sstevel@tonic-gate 			}
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate 			out->nsc_errno = NSS_NOTFOUND;
242*7c478bd9Sstevel@tonic-gate 			out->nsc_return_code = NOTFOUND;
243*7c478bd9Sstevel@tonic-gate 			out->nsc_bufferbytesused = sizeof (*out);
244*7c478bd9Sstevel@tonic-gate 			goto getout;
245*7c478bd9Sstevel@tonic-gate 		}
246*7c478bd9Sstevel@tonic-gate 		bucket = get_hash(nam_hash, in->nsc_u.name);
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate 		if (*bucket == (char *)-1) {	/* pending lookup */
249*7c478bd9Sstevel@tonic-gate 			if (get_clearance(in->nsc_callnumber) != 0) {
250*7c478bd9Sstevel@tonic-gate 			    /* no threads available */
251*7c478bd9Sstevel@tonic-gate 				out->nsc_return_code = NOSERVER;
252*7c478bd9Sstevel@tonic-gate 				    /* cannot process now */
253*7c478bd9Sstevel@tonic-gate 				out->nsc_bufferbytesused =
254*7c478bd9Sstevel@tonic-gate 				    sizeof (*out);
255*7c478bd9Sstevel@tonic-gate 				current_admin.user.nsc_throttle_count++;
256*7c478bd9Sstevel@tonic-gate 				goto getout;
257*7c478bd9Sstevel@tonic-gate 			}
258*7c478bd9Sstevel@tonic-gate 			nscd_wait(&db_wait, &db_lock, bucket);
259*7c478bd9Sstevel@tonic-gate 			release_clearance(in->nsc_callnumber);
260*7c478bd9Sstevel@tonic-gate 			continue; /* go back and relookup hash bucket */
261*7c478bd9Sstevel@tonic-gate 		}
262*7c478bd9Sstevel@tonic-gate 		break;
263*7c478bd9Sstevel@tonic-gate 	}
264*7c478bd9Sstevel@tonic-gate 
265*7c478bd9Sstevel@tonic-gate 	/*
266*7c478bd9Sstevel@tonic-gate 	 * check for no name_service mode
267*7c478bd9Sstevel@tonic-gate 	 */
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate 	if (*bucket == NULL && current_admin.avoid_nameservice) {
270*7c478bd9Sstevel@tonic-gate 		out->nsc_return_code = NOTFOUND;
271*7c478bd9Sstevel@tonic-gate 		out->nsc_bufferbytesused = sizeof (*out);
272*7c478bd9Sstevel@tonic-gate 	} else if ((*bucket == NULL) || /* New entry in name service */
273*7c478bd9Sstevel@tonic-gate 	    (in->nsc_callnumber & UPDATEBIT) || /* needs updating */
274*7c478bd9Sstevel@tonic-gate 	    (out_of_date = (!current_admin.avoid_nameservice &&
275*7c478bd9Sstevel@tonic-gate 	    (current_admin.user.nsc_old_data_ok == 0) &&
276*7c478bd9Sstevel@tonic-gate 	    (((nsc_bucket_t *)*bucket)->nsc_timestamp < now)))) {
277*7c478bd9Sstevel@tonic-gate 		/* time has expired */
278*7c478bd9Sstevel@tonic-gate 		int saved_errno;
279*7c478bd9Sstevel@tonic-gate 		int saved_hits = 0;
280*7c478bd9Sstevel@tonic-gate 		userstr_t *p;
281*7c478bd9Sstevel@tonic-gate 
282*7c478bd9Sstevel@tonic-gate 		if (get_clearance(in->nsc_callnumber) != 0) {
283*7c478bd9Sstevel@tonic-gate 		    /* no threads available */
284*7c478bd9Sstevel@tonic-gate 			out->nsc_return_code = NOSERVER;
285*7c478bd9Sstevel@tonic-gate 			    /* cannot process now */
286*7c478bd9Sstevel@tonic-gate 			out->nsc_bufferbytesused = sizeof (*out);
287*7c478bd9Sstevel@tonic-gate 			current_admin.user.nsc_throttle_count++;
288*7c478bd9Sstevel@tonic-gate 			goto getout;
289*7c478bd9Sstevel@tonic-gate 		}
290*7c478bd9Sstevel@tonic-gate 
291*7c478bd9Sstevel@tonic-gate 		if (*bucket != NULL) {
292*7c478bd9Sstevel@tonic-gate 			saved_hits =
293*7c478bd9Sstevel@tonic-gate 			    ((nsc_bucket_t *)*bucket)->nsc_hits;
294*7c478bd9Sstevel@tonic-gate 		}
295*7c478bd9Sstevel@tonic-gate 
296*7c478bd9Sstevel@tonic-gate 		/*
297*7c478bd9Sstevel@tonic-gate 		 *  block any threads accessing this bucket if data is
298*7c478bd9Sstevel@tonic-gate 		 *  non-existent out of date
299*7c478bd9Sstevel@tonic-gate 		 */
300*7c478bd9Sstevel@tonic-gate 
301*7c478bd9Sstevel@tonic-gate 		if (*bucket == NULL || out_of_date) {
302*7c478bd9Sstevel@tonic-gate 			update_user_bucket((nsc_bucket_t **)bucket,
303*7c478bd9Sstevel@tonic-gate 			    (nsc_bucket_t *)-1, in->nsc_callnumber);
304*7c478bd9Sstevel@tonic-gate 		} else {
305*7c478bd9Sstevel@tonic-gate 		/*
306*7c478bd9Sstevel@tonic-gate 		 * if still not -1 bucket we are doing update...
307*7c478bd9Sstevel@tonic-gate 		 * mark to prevent
308*7c478bd9Sstevel@tonic-gate 		 * pileups of threads if the name service is hanging....
309*7c478bd9Sstevel@tonic-gate 		 */
310*7c478bd9Sstevel@tonic-gate 			((nsc_bucket_t *)(*bucket))->nsc_status |=
311*7c478bd9Sstevel@tonic-gate 			    ST_UPDATE_PENDING;
312*7c478bd9Sstevel@tonic-gate 			/* cleared by deletion of old data */
313*7c478bd9Sstevel@tonic-gate 		}
314*7c478bd9Sstevel@tonic-gate 		mutex_unlock(&db_lock);
315*7c478bd9Sstevel@tonic-gate 
316*7c478bd9Sstevel@tonic-gate 		/*
317*7c478bd9Sstevel@tonic-gate 		 * Call non-caching version in libnsl.
318*7c478bd9Sstevel@tonic-gate 		 */
319*7c478bd9Sstevel@tonic-gate 		p = _getusernam(in->nsc_u.name, &out->nsc_u.user,
320*7c478bd9Sstevel@tonic-gate 		    out->nsc_u.buff + sizeof (userstr_t),
321*7c478bd9Sstevel@tonic-gate 		    bufferspace, &errno);
322*7c478bd9Sstevel@tonic-gate 		saved_errno = errno;
323*7c478bd9Sstevel@tonic-gate 
324*7c478bd9Sstevel@tonic-gate 		mutex_lock(&db_lock);
325*7c478bd9Sstevel@tonic-gate 
326*7c478bd9Sstevel@tonic-gate 		release_clearance(in->nsc_callnumber);
327*7c478bd9Sstevel@tonic-gate 
328*7c478bd9Sstevel@tonic-gate 		if (p == NULL) { /* data not found */
329*7c478bd9Sstevel@tonic-gate 
330*7c478bd9Sstevel@tonic-gate 			if (current_admin.debug_level >= DBG_CANT_FIND) {
331*7c478bd9Sstevel@tonic-gate 		logit("getuser_lookup: nscd COULDN'T FIND user_attr name %s\n",
332*7c478bd9Sstevel@tonic-gate 						in->nsc_u.name);
333*7c478bd9Sstevel@tonic-gate 			}
334*7c478bd9Sstevel@tonic-gate 
335*7c478bd9Sstevel@tonic-gate 
336*7c478bd9Sstevel@tonic-gate 			if (!(UPDATEBIT & in->nsc_callnumber))
337*7c478bd9Sstevel@tonic-gate 			    current_admin.user.nsc_neg_cache_misses++;
338*7c478bd9Sstevel@tonic-gate 
339*7c478bd9Sstevel@tonic-gate 			retb = (nsc_bucket_t *)malloc(sizeof (nsc_bucket_t));
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate 			retb->nsc_refcount = 1;
342*7c478bd9Sstevel@tonic-gate 			retb->nsc_data.nsc_bufferbytesused =
343*7c478bd9Sstevel@tonic-gate 				sizeof (nsc_return_t);
344*7c478bd9Sstevel@tonic-gate 			retb->nsc_data.nsc_return_code = NOTFOUND;
345*7c478bd9Sstevel@tonic-gate 			retb->nsc_data.nsc_errno = saved_errno;
346*7c478bd9Sstevel@tonic-gate 			memcpy(out, &retb->nsc_data,
347*7c478bd9Sstevel@tonic-gate 			    retb->nsc_data.nsc_bufferbytesused);
348*7c478bd9Sstevel@tonic-gate 			update_user_bucket((nsc_bucket_t **)bucket,
349*7c478bd9Sstevel@tonic-gate 			    retb, in->nsc_callnumber);
350*7c478bd9Sstevel@tonic-gate 			goto getout;
351*7c478bd9Sstevel@tonic-gate 		} else {
352*7c478bd9Sstevel@tonic-gate 			if (current_admin.debug_level >= DBG_ALL) {
353*7c478bd9Sstevel@tonic-gate 		logit("getuser_lookup: nscd FOUND user_attr name %s\n",
354*7c478bd9Sstevel@tonic-gate 						in->nsc_u.name);
355*7c478bd9Sstevel@tonic-gate 			}
356*7c478bd9Sstevel@tonic-gate 			if (!(UPDATEBIT & in->nsc_callnumber))
357*7c478bd9Sstevel@tonic-gate 			    current_admin.user.nsc_pos_cache_misses++;
358*7c478bd9Sstevel@tonic-gate 
359*7c478bd9Sstevel@tonic-gate 			retb = fixbuffer(out, bufferspace);
360*7c478bd9Sstevel@tonic-gate 			update_user_bucket((nsc_bucket_t **)bucket,
361*7c478bd9Sstevel@tonic-gate 			    retb, in->nsc_callnumber);
362*7c478bd9Sstevel@tonic-gate 			if (saved_hits)
363*7c478bd9Sstevel@tonic-gate 				retb->nsc_hits = saved_hits;
364*7c478bd9Sstevel@tonic-gate 		}
365*7c478bd9Sstevel@tonic-gate 	} else { 	/* found entry in cache */
366*7c478bd9Sstevel@tonic-gate 		retb = (nsc_bucket_t *)*bucket;
367*7c478bd9Sstevel@tonic-gate 
368*7c478bd9Sstevel@tonic-gate 		retb->nsc_hits++;
369*7c478bd9Sstevel@tonic-gate 
370*7c478bd9Sstevel@tonic-gate 		memcpy(out, &(retb->nsc_data),
371*7c478bd9Sstevel@tonic-gate 		    retb->nsc_data.nsc_bufferbytesused);
372*7c478bd9Sstevel@tonic-gate 
373*7c478bd9Sstevel@tonic-gate 		if (out->nsc_return_code == SUCCESS) {
374*7c478bd9Sstevel@tonic-gate 			if (!(UPDATEBIT & in->nsc_callnumber))
375*7c478bd9Sstevel@tonic-gate 			    current_admin.user.nsc_pos_cache_hits++;
376*7c478bd9Sstevel@tonic-gate 			if (current_admin.debug_level >= DBG_ALL) {
377*7c478bd9Sstevel@tonic-gate 			logit("getuser_lookup: found name %s in cache\n",
378*7c478bd9Sstevel@tonic-gate 						in->nsc_u.name);
379*7c478bd9Sstevel@tonic-gate 			}
380*7c478bd9Sstevel@tonic-gate 		} else {
381*7c478bd9Sstevel@tonic-gate 			if (!(UPDATEBIT & in->nsc_callnumber))
382*7c478bd9Sstevel@tonic-gate 			    current_admin.user.nsc_neg_cache_hits++;
383*7c478bd9Sstevel@tonic-gate 			if (current_admin.debug_level >= DBG_ALL) {
384*7c478bd9Sstevel@tonic-gate 		logit("getuser_lookup: %s marked as NOT FOUND in cache.\n",
385*7c478bd9Sstevel@tonic-gate 						in->nsc_u.name);
386*7c478bd9Sstevel@tonic-gate 			}
387*7c478bd9Sstevel@tonic-gate 		}
388*7c478bd9Sstevel@tonic-gate 
389*7c478bd9Sstevel@tonic-gate 		if ((retb->nsc_timestamp < now) &&
390*7c478bd9Sstevel@tonic-gate 		    !(in->nsc_callnumber & UPDATEBIT) &&
391*7c478bd9Sstevel@tonic-gate 		    !(retb->nsc_status & ST_UPDATE_PENDING)) {
392*7c478bd9Sstevel@tonic-gate 		logit("launch update since time = %d\n", retb->nsc_timestamp);
393*7c478bd9Sstevel@tonic-gate 			retb->nsc_status |= ST_UPDATE_PENDING;
394*7c478bd9Sstevel@tonic-gate 			/* cleared by deletion of old data */
395*7c478bd9Sstevel@tonic-gate 			launch_update(in);
396*7c478bd9Sstevel@tonic-gate 		}
397*7c478bd9Sstevel@tonic-gate 	}
398*7c478bd9Sstevel@tonic-gate 
399*7c478bd9Sstevel@tonic-gate getout:
400*7c478bd9Sstevel@tonic-gate 	mutex_unlock(&db_lock);
401*7c478bd9Sstevel@tonic-gate }
402*7c478bd9Sstevel@tonic-gate 
403*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
404*7c478bd9Sstevel@tonic-gate static void
405*7c478bd9Sstevel@tonic-gate update_user_bucket(nsc_bucket_t **old, nsc_bucket_t *new, int callnumber)
406*7c478bd9Sstevel@tonic-gate {
407*7c478bd9Sstevel@tonic-gate 	if (*old != NULL && *old != (nsc_bucket_t *)-1) { /* old data exists */
408*7c478bd9Sstevel@tonic-gate 		free(*old);
409*7c478bd9Sstevel@tonic-gate 		current_admin.user.nsc_entries--;
410*7c478bd9Sstevel@tonic-gate 	}
411*7c478bd9Sstevel@tonic-gate 
412*7c478bd9Sstevel@tonic-gate 	/*
413*7c478bd9Sstevel@tonic-gate 	 *  we can do this before reseting *old since we're holding the lock
414*7c478bd9Sstevel@tonic-gate 	 */
415*7c478bd9Sstevel@tonic-gate 
416*7c478bd9Sstevel@tonic-gate 	else if (*old == (nsc_bucket_t *)-1) {
417*7c478bd9Sstevel@tonic-gate 		nscd_signal(&db_wait, (char **)old);
418*7c478bd9Sstevel@tonic-gate 	}
419*7c478bd9Sstevel@tonic-gate 
420*7c478bd9Sstevel@tonic-gate 	*old = new;
421*7c478bd9Sstevel@tonic-gate 
422*7c478bd9Sstevel@tonic-gate 	if ((new != NULL) &&
423*7c478bd9Sstevel@tonic-gate 	    (new != (nsc_bucket_t *)-1)) {
424*7c478bd9Sstevel@tonic-gate 		/* real data, not just update pending or invalidate */
425*7c478bd9Sstevel@tonic-gate 		new->nsc_hits = 1;
426*7c478bd9Sstevel@tonic-gate 		new->nsc_status = 0;
427*7c478bd9Sstevel@tonic-gate 		new->nsc_refcount = 1;
428*7c478bd9Sstevel@tonic-gate 		current_admin.user.nsc_entries++;
429*7c478bd9Sstevel@tonic-gate 
430*7c478bd9Sstevel@tonic-gate 		if (new->nsc_data.nsc_return_code == SUCCESS) {
431*7c478bd9Sstevel@tonic-gate 			new->nsc_timestamp = time(NULL) +
432*7c478bd9Sstevel@tonic-gate 			    current_admin.user.nsc_pos_ttl;
433*7c478bd9Sstevel@tonic-gate 		} else {
434*7c478bd9Sstevel@tonic-gate 			new->nsc_timestamp = time(NULL) +
435*7c478bd9Sstevel@tonic-gate 			    current_admin.user.nsc_neg_ttl;
436*7c478bd9Sstevel@tonic-gate 		}
437*7c478bd9Sstevel@tonic-gate 	}
438*7c478bd9Sstevel@tonic-gate }
439*7c478bd9Sstevel@tonic-gate 
440*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
441*7c478bd9Sstevel@tonic-gate static nsc_bucket_t *
442*7c478bd9Sstevel@tonic-gate fixbuffer(nsc_return_t *in, int maxlen)
443*7c478bd9Sstevel@tonic-gate {
444*7c478bd9Sstevel@tonic-gate 	nsc_bucket_t *retb;
445*7c478bd9Sstevel@tonic-gate 	nsc_return_t *out;
446*7c478bd9Sstevel@tonic-gate 	char 	*dest;
447*7c478bd9Sstevel@tonic-gate 	int 	offset;
448*7c478bd9Sstevel@tonic-gate 	int 	strs;
449*7c478bd9Sstevel@tonic-gate 
450*7c478bd9Sstevel@tonic-gate 	/*
451*7c478bd9Sstevel@tonic-gate 	 * find out the size of the data block we're going to need
452*7c478bd9Sstevel@tonic-gate 	 */
453*7c478bd9Sstevel@tonic-gate 
454*7c478bd9Sstevel@tonic-gate 	strs = attr_strlen(in->nsc_u.user.name) +
455*7c478bd9Sstevel@tonic-gate 	    attr_strlen(in->nsc_u.user.qualifier) +
456*7c478bd9Sstevel@tonic-gate 	    attr_strlen(in->nsc_u.user.res1) +
457*7c478bd9Sstevel@tonic-gate 	    attr_strlen(in->nsc_u.user.res2) +
458*7c478bd9Sstevel@tonic-gate 	    attr_strlen(in->nsc_u.user.attr) + USERATTR_DB_NCOL;
459*7c478bd9Sstevel@tonic-gate 
460*7c478bd9Sstevel@tonic-gate 	/*
461*7c478bd9Sstevel@tonic-gate 	 * allocate it and copy it in
462*7c478bd9Sstevel@tonic-gate 	 * code doesn't assume packing order in original buffer
463*7c478bd9Sstevel@tonic-gate 	 */
464*7c478bd9Sstevel@tonic-gate 
465*7c478bd9Sstevel@tonic-gate 	if ((retb = (nsc_bucket_t *)malloc(sizeof (*retb) + strs)) == NULL) {
466*7c478bd9Sstevel@tonic-gate 		return (NULL);
467*7c478bd9Sstevel@tonic-gate 	}
468*7c478bd9Sstevel@tonic-gate 
469*7c478bd9Sstevel@tonic-gate 	out = &(retb->nsc_data);
470*7c478bd9Sstevel@tonic-gate 	out->nsc_bufferbytesused = strs + ((int)&out->nsc_u.user - (int)out) +
471*7c478bd9Sstevel@tonic-gate 	    sizeof (userstr_t);
472*7c478bd9Sstevel@tonic-gate 	out->nsc_return_code 	= SUCCESS;
473*7c478bd9Sstevel@tonic-gate 	out->nsc_errno 		= 0;
474*7c478bd9Sstevel@tonic-gate 
475*7c478bd9Sstevel@tonic-gate 	dest = retb->nsc_data.nsc_u.buff + sizeof (userstr_t);
476*7c478bd9Sstevel@tonic-gate 	offset = (int)dest;
477*7c478bd9Sstevel@tonic-gate 
478*7c478bd9Sstevel@tonic-gate 	attr_strcpy(dest, in->nsc_u.user.name);
479*7c478bd9Sstevel@tonic-gate 	strs = 1 + attr_strlen(in->nsc_u.user.name);
480*7c478bd9Sstevel@tonic-gate 	out->nsc_u.user.name = dest - offset;
481*7c478bd9Sstevel@tonic-gate 	dest += strs;
482*7c478bd9Sstevel@tonic-gate 
483*7c478bd9Sstevel@tonic-gate 	attr_strcpy(dest, in->nsc_u.user.qualifier);
484*7c478bd9Sstevel@tonic-gate 	strs = 1 + attr_strlen(in->nsc_u.user.qualifier);
485*7c478bd9Sstevel@tonic-gate 	out->nsc_u.user.qualifier = dest - offset;
486*7c478bd9Sstevel@tonic-gate 	dest += strs;
487*7c478bd9Sstevel@tonic-gate 
488*7c478bd9Sstevel@tonic-gate 	attr_strcpy(dest, in->nsc_u.user.res1);
489*7c478bd9Sstevel@tonic-gate 	strs = 1 + attr_strlen(in->nsc_u.user.res1);
490*7c478bd9Sstevel@tonic-gate 	out->nsc_u.user.res1 = dest - offset;
491*7c478bd9Sstevel@tonic-gate 	dest += strs;
492*7c478bd9Sstevel@tonic-gate 
493*7c478bd9Sstevel@tonic-gate 	attr_strcpy(dest, in->nsc_u.user.res2);
494*7c478bd9Sstevel@tonic-gate 	strs = 1 + attr_strlen(in->nsc_u.user.res2);
495*7c478bd9Sstevel@tonic-gate 	out->nsc_u.user.res2 = dest - offset;
496*7c478bd9Sstevel@tonic-gate 	dest += strs;
497*7c478bd9Sstevel@tonic-gate 
498*7c478bd9Sstevel@tonic-gate 	attr_strcpy(dest, in->nsc_u.user.attr);
499*7c478bd9Sstevel@tonic-gate 	out->nsc_u.user.attr = dest - offset;
500*7c478bd9Sstevel@tonic-gate 
501*7c478bd9Sstevel@tonic-gate 	memcpy(in, out, out->nsc_bufferbytesused);
502*7c478bd9Sstevel@tonic-gate 
503*7c478bd9Sstevel@tonic-gate 	return (retb);
504*7c478bd9Sstevel@tonic-gate }
505*7c478bd9Sstevel@tonic-gate 
506*7c478bd9Sstevel@tonic-gate void
507*7c478bd9Sstevel@tonic-gate getuser_reaper(void)
508*7c478bd9Sstevel@tonic-gate {
509*7c478bd9Sstevel@tonic-gate 	nsc_reaper("getuser", nam_hash, &current_admin.user, &db_lock);
510*7c478bd9Sstevel@tonic-gate }
511