xref: /titanic_53/usr/src/cmd/nscd/gethost.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 gethost* 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 <stdlib.h>
38*7c478bd9Sstevel@tonic-gate #include <stdio.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 "getxby_door.h"
51*7c478bd9Sstevel@tonic-gate #include "server_door.h"
52*7c478bd9Sstevel@tonic-gate #include "nscd.h"
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate static hash_t *addr_hash;
55*7c478bd9Sstevel@tonic-gate static hash_t *hnam_hash;
56*7c478bd9Sstevel@tonic-gate static mutex_t  host_lock = DEFAULTMUTEX;
57*7c478bd9Sstevel@tonic-gate static waiter_t host_wait;
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate static void gethost_addrkeepalive(int keep, int interval);
60*7c478bd9Sstevel@tonic-gate static void gethost_invalidate_unlocked(void);
61*7c478bd9Sstevel@tonic-gate static void gethost_namekeepalive(int keep, int interval);
62*7c478bd9Sstevel@tonic-gate static int addr_to_int(char *addr);
63*7c478bd9Sstevel@tonic-gate static int int_to_addr(int h);
64*7c478bd9Sstevel@tonic-gate static void update_host_bucket(nsc_bucket_t **old, nsc_bucket_t *new,
65*7c478bd9Sstevel@tonic-gate     int callnumber);
66*7c478bd9Sstevel@tonic-gate static nsc_bucket_t *fixbuffer(nsc_return_t *in, int maxlen);
67*7c478bd9Sstevel@tonic-gate static void do_findhaddrs(nsc_bucket_t *ptr, int *table, int intaddr);
68*7c478bd9Sstevel@tonic-gate static void do_findhnams(nsc_bucket_t *ptr, int *table, char *name);
69*7c478bd9Sstevel@tonic-gate static void do_invalidate(nsc_bucket_t **ptr, int callnumber);
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate static int
72*7c478bd9Sstevel@tonic-gate addr_to_int(char *addr)
73*7c478bd9Sstevel@tonic-gate {
74*7c478bd9Sstevel@tonic-gate 	union {
75*7c478bd9Sstevel@tonic-gate 		char data[4];
76*7c478bd9Sstevel@tonic-gate 		int  hashval;
77*7c478bd9Sstevel@tonic-gate 	} u;
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate /*
80*7c478bd9Sstevel@tonic-gate  * following code is byte order dependant, but since all we use this for
81*7c478bd9Sstevel@tonic-gate  * is hashing this works out just fine.
82*7c478bd9Sstevel@tonic-gate  */
83*7c478bd9Sstevel@tonic-gate 	u.data[0] = *addr++;
84*7c478bd9Sstevel@tonic-gate 	u.data[1] = *addr++;
85*7c478bd9Sstevel@tonic-gate 	u.data[2] = *addr++;
86*7c478bd9Sstevel@tonic-gate 	u.data[3] = *addr++;
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate 	return (u.hashval);
89*7c478bd9Sstevel@tonic-gate }
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate static int
92*7c478bd9Sstevel@tonic-gate int_to_addr(int h)
93*7c478bd9Sstevel@tonic-gate {
94*7c478bd9Sstevel@tonic-gate 	union {
95*7c478bd9Sstevel@tonic-gate 		char data[4];
96*7c478bd9Sstevel@tonic-gate 		int  hashval;
97*7c478bd9Sstevel@tonic-gate 	} u;
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate /*
100*7c478bd9Sstevel@tonic-gate  * following code is byte order dependant, but since all we use this for
101*7c478bd9Sstevel@tonic-gate  * is hashing this works out just fine.
102*7c478bd9Sstevel@tonic-gate  */
103*7c478bd9Sstevel@tonic-gate 	u.hashval = h;
104*7c478bd9Sstevel@tonic-gate 	return (* ((int *)u.data));
105*7c478bd9Sstevel@tonic-gate }
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate void
108*7c478bd9Sstevel@tonic-gate gethost_init(void)
109*7c478bd9Sstevel@tonic-gate {
110*7c478bd9Sstevel@tonic-gate 	addr_hash = make_ihash(current_admin.host.nsc_suggestedsize);
111*7c478bd9Sstevel@tonic-gate 	hnam_hash = make_hash(current_admin.host.nsc_suggestedsize);
112*7c478bd9Sstevel@tonic-gate }
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate static void
115*7c478bd9Sstevel@tonic-gate do_invalidate(nsc_bucket_t ** ptr, int callnumber)
116*7c478bd9Sstevel@tonic-gate {
117*7c478bd9Sstevel@tonic-gate 	if (*ptr != NULL && *ptr != (nsc_bucket_t *)-1) {
118*7c478bd9Sstevel@tonic-gate 		/* leave pending calls alone */
119*7c478bd9Sstevel@tonic-gate 		update_host_bucket(ptr, NULL, callnumber);
120*7c478bd9Sstevel@tonic-gate 	}
121*7c478bd9Sstevel@tonic-gate }
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate static void
124*7c478bd9Sstevel@tonic-gate do_findhnams(nsc_bucket_t *ptr, int *table, char *name)
125*7c478bd9Sstevel@tonic-gate {
126*7c478bd9Sstevel@tonic-gate 	/*
127*7c478bd9Sstevel@tonic-gate 	 * be careful with ptr - it may be -1 or NULL.
128*7c478bd9Sstevel@tonic-gate 	 */
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	if (ptr != NULL && ptr != (nsc_bucket_t *)-1) {
131*7c478bd9Sstevel@tonic-gate 		/* leave pending calls alone */
132*7c478bd9Sstevel@tonic-gate 		char *tmp = (char *)insertn(table, ptr->nsc_hits,
133*7c478bd9Sstevel@tonic-gate 			(int)strdup(name));
134*7c478bd9Sstevel@tonic-gate 		if (tmp != (char *)-1)
135*7c478bd9Sstevel@tonic-gate 			free(tmp);
136*7c478bd9Sstevel@tonic-gate 	}
137*7c478bd9Sstevel@tonic-gate }
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate static void
140*7c478bd9Sstevel@tonic-gate do_findhaddrs(nsc_bucket_t *ptr, int *table, int intaddr)
141*7c478bd9Sstevel@tonic-gate {
142*7c478bd9Sstevel@tonic-gate 	if (ptr != NULL && ptr != (nsc_bucket_t *)-1) {
143*7c478bd9Sstevel@tonic-gate 		/* leave pending calls alone */
144*7c478bd9Sstevel@tonic-gate 		insertn(table, ptr->nsc_hits, int_to_addr(intaddr));
145*7c478bd9Sstevel@tonic-gate 	}
146*7c478bd9Sstevel@tonic-gate }
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate void
149*7c478bd9Sstevel@tonic-gate gethost_revalidate(void)
150*7c478bd9Sstevel@tonic-gate {
151*7c478bd9Sstevel@tonic-gate 	for (;;) {
152*7c478bd9Sstevel@tonic-gate 		int slp;
153*7c478bd9Sstevel@tonic-gate 		int interval;
154*7c478bd9Sstevel@tonic-gate 		int count;
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 		slp = current_admin.host.nsc_pos_ttl;
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate 		if (slp < 60)
159*7c478bd9Sstevel@tonic-gate 			slp = 60;
160*7c478bd9Sstevel@tonic-gate 		count = current_admin.host.nsc_keephot;
161*7c478bd9Sstevel@tonic-gate 		if (count != 0) {
162*7c478bd9Sstevel@tonic-gate 			interval = (slp/2)/count;
163*7c478bd9Sstevel@tonic-gate 			if (interval == 0) interval = 1;
164*7c478bd9Sstevel@tonic-gate 			sleep(slp*2/3);
165*7c478bd9Sstevel@tonic-gate 			gethost_namekeepalive(count, interval);
166*7c478bd9Sstevel@tonic-gate 			gethost_addrkeepalive(count, interval);
167*7c478bd9Sstevel@tonic-gate 		} else {
168*7c478bd9Sstevel@tonic-gate 			sleep(slp);
169*7c478bd9Sstevel@tonic-gate 		}
170*7c478bd9Sstevel@tonic-gate 	}
171*7c478bd9Sstevel@tonic-gate }
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate static void
174*7c478bd9Sstevel@tonic-gate gethost_namekeepalive(int keep, int interval)
175*7c478bd9Sstevel@tonic-gate {
176*7c478bd9Sstevel@tonic-gate 	int *table;
177*7c478bd9Sstevel@tonic-gate 	union {
178*7c478bd9Sstevel@tonic-gate 		nsc_data_t  ping;
179*7c478bd9Sstevel@tonic-gate 		char space[sizeof (nsc_data_t) + NSCDMAXNAMELEN];
180*7c478bd9Sstevel@tonic-gate 	} u;
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate 	int i;
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 	if (!keep)
185*7c478bd9Sstevel@tonic-gate 		return;
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate 	table = maken(keep);
188*7c478bd9Sstevel@tonic-gate 	mutex_lock(&host_lock);
189*7c478bd9Sstevel@tonic-gate 	operate_hash(hnam_hash, do_findhnams, (char *)table);
190*7c478bd9Sstevel@tonic-gate 	mutex_unlock(&host_lock);
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate 	for (i = 1; i <= keep; i++) {
193*7c478bd9Sstevel@tonic-gate 		char *tmp;
194*7c478bd9Sstevel@tonic-gate 		u.ping.nsc_call.nsc_callnumber = GETHOSTBYNAME;
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate 		if ((tmp = (char *)table[keep + 1 + i]) == (char *)-1)
197*7c478bd9Sstevel@tonic-gate 			continue; /* unused slot in table */
198*7c478bd9Sstevel@tonic-gate 		if (current_admin.debug_level >= DBG_ALL)
199*7c478bd9Sstevel@tonic-gate 			logit("keepalive: reviving host %s\n", tmp);
200*7c478bd9Sstevel@tonic-gate 		strcpy(u.ping.nsc_call.nsc_u.name, tmp);
201*7c478bd9Sstevel@tonic-gate 
202*7c478bd9Sstevel@tonic-gate 		launch_update(&u.ping.nsc_call);
203*7c478bd9Sstevel@tonic-gate 		sleep(interval);
204*7c478bd9Sstevel@tonic-gate 	}
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	for (i = 1; i <= keep; i++) {
207*7c478bd9Sstevel@tonic-gate 		char *tmp;
208*7c478bd9Sstevel@tonic-gate 		if ((tmp = (char *)table[keep + 1 + i]) != (char *)-1)
209*7c478bd9Sstevel@tonic-gate 			free(tmp);
210*7c478bd9Sstevel@tonic-gate 	}
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate 	free(table);
213*7c478bd9Sstevel@tonic-gate }
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate static void
216*7c478bd9Sstevel@tonic-gate gethost_addrkeepalive(int keep, int interval)
217*7c478bd9Sstevel@tonic-gate {
218*7c478bd9Sstevel@tonic-gate 	int *table;
219*7c478bd9Sstevel@tonic-gate 	union {
220*7c478bd9Sstevel@tonic-gate 		nsc_data_t  ping;
221*7c478bd9Sstevel@tonic-gate 		char space[sizeof (nsc_data_t) + 80];
222*7c478bd9Sstevel@tonic-gate 	} u;
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate 	int i;
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate 	if (!keep)
227*7c478bd9Sstevel@tonic-gate 		return;
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate 	table = maken(keep);
230*7c478bd9Sstevel@tonic-gate 	mutex_lock(&host_lock);
231*7c478bd9Sstevel@tonic-gate 	operate_hash(addr_hash, do_findhaddrs, (char *)table);
232*7c478bd9Sstevel@tonic-gate 	mutex_unlock(&host_lock);
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate 	for (i = 1; i <= keep; i++) {
235*7c478bd9Sstevel@tonic-gate 		int tmp;
236*7c478bd9Sstevel@tonic-gate 		u.ping.nsc_call.nsc_callnumber = GETHOSTBYADDR;
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate 		if ((tmp = table[keep + 1 + i]) == -1)
239*7c478bd9Sstevel@tonic-gate 			continue; /* unused slot in table */
240*7c478bd9Sstevel@tonic-gate 		u.ping.nsc_call.nsc_u.addr.a_type = AF_INET;
241*7c478bd9Sstevel@tonic-gate 		u.ping.nsc_call.nsc_u.addr.a_length = sizeof (int);
242*7c478bd9Sstevel@tonic-gate 		memcpy(u.ping.nsc_call.nsc_u.addr.a_data, &tmp, sizeof (int));
243*7c478bd9Sstevel@tonic-gate 		launch_update(&u.ping.nsc_call);
244*7c478bd9Sstevel@tonic-gate 		sleep(interval);
245*7c478bd9Sstevel@tonic-gate 	}
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate 	free(table);
248*7c478bd9Sstevel@tonic-gate }
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate /*
251*7c478bd9Sstevel@tonic-gate  *   This routine marks all entries as invalid
252*7c478bd9Sstevel@tonic-gate  *
253*7c478bd9Sstevel@tonic-gate  */
254*7c478bd9Sstevel@tonic-gate void
255*7c478bd9Sstevel@tonic-gate gethost_invalidate(void)
256*7c478bd9Sstevel@tonic-gate {
257*7c478bd9Sstevel@tonic-gate 	mutex_lock(&host_lock);
258*7c478bd9Sstevel@tonic-gate 	gethost_invalidate_unlocked();
259*7c478bd9Sstevel@tonic-gate 	mutex_unlock(&host_lock);
260*7c478bd9Sstevel@tonic-gate }
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate static void
263*7c478bd9Sstevel@tonic-gate gethost_invalidate_unlocked(void)
264*7c478bd9Sstevel@tonic-gate {
265*7c478bd9Sstevel@tonic-gate 	operate_hash_addr(hnam_hash, do_invalidate, (char *)GETHOSTBYNAME);
266*7c478bd9Sstevel@tonic-gate 	operate_hash_addr(addr_hash, do_invalidate, (char *)GETHOSTBYADDR);
267*7c478bd9Sstevel@tonic-gate 	current_admin.host.nsc_invalidate_count++;
268*7c478bd9Sstevel@tonic-gate }
269*7c478bd9Sstevel@tonic-gate 
270*7c478bd9Sstevel@tonic-gate void
271*7c478bd9Sstevel@tonic-gate gethost_lookup(nsc_return_t *out, int maxsize, nsc_call_t *in, time_t now)
272*7c478bd9Sstevel@tonic-gate {
273*7c478bd9Sstevel@tonic-gate 	int		out_of_date;
274*7c478bd9Sstevel@tonic-gate 	nsc_bucket_t	*retb;
275*7c478bd9Sstevel@tonic-gate 	char 		**bucket;
276*7c478bd9Sstevel@tonic-gate 
277*7c478bd9Sstevel@tonic-gate 	static time_t	lastmod;
278*7c478bd9Sstevel@tonic-gate 
279*7c478bd9Sstevel@tonic-gate 	int bufferspace = maxsize - sizeof (nsc_return_t);
280*7c478bd9Sstevel@tonic-gate 
281*7c478bd9Sstevel@tonic-gate 	if (current_admin.host.nsc_enabled == 0) {
282*7c478bd9Sstevel@tonic-gate 		out->nsc_return_code = NOSERVER;
283*7c478bd9Sstevel@tonic-gate 		out->nsc_bufferbytesused = sizeof (*out);
284*7c478bd9Sstevel@tonic-gate 		return;
285*7c478bd9Sstevel@tonic-gate 	}
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate 	mutex_lock(&host_lock);
288*7c478bd9Sstevel@tonic-gate 
289*7c478bd9Sstevel@tonic-gate 	if (current_admin.host.nsc_check_files) {
290*7c478bd9Sstevel@tonic-gate 		struct stat buf;
291*7c478bd9Sstevel@tonic-gate 
292*7c478bd9Sstevel@tonic-gate 		if (stat("/etc/hosts", &buf) < 0) {
293*7c478bd9Sstevel@tonic-gate 			/*EMPTY*/;
294*7c478bd9Sstevel@tonic-gate 		} else if (lastmod == 0) {
295*7c478bd9Sstevel@tonic-gate 			lastmod = buf.st_mtime;
296*7c478bd9Sstevel@tonic-gate 		} else if (lastmod < buf.st_mtime) {
297*7c478bd9Sstevel@tonic-gate 			gethost_invalidate_unlocked();
298*7c478bd9Sstevel@tonic-gate 			lastmod = buf.st_mtime;
299*7c478bd9Sstevel@tonic-gate 		}
300*7c478bd9Sstevel@tonic-gate 	}
301*7c478bd9Sstevel@tonic-gate 
302*7c478bd9Sstevel@tonic-gate 
303*7c478bd9Sstevel@tonic-gate 	if (current_admin.debug_level >= DBG_ALL) {
304*7c478bd9Sstevel@tonic-gate 		if (MASKUPDATEBIT(in->nsc_callnumber) == GETHOSTBYADDR) {
305*7c478bd9Sstevel@tonic-gate 			logit("gethost_lookup: looking for address %s\n",
306*7c478bd9Sstevel@tonic-gate 			inet_ntoa(*((struct in_addr *)in->nsc_u.addr.a_data)));
307*7c478bd9Sstevel@tonic-gate 		} else {
308*7c478bd9Sstevel@tonic-gate 			logit("gethost_lookup: looking for hostname %s\n",
309*7c478bd9Sstevel@tonic-gate 				in->nsc_u.name);
310*7c478bd9Sstevel@tonic-gate 		}
311*7c478bd9Sstevel@tonic-gate 	}
312*7c478bd9Sstevel@tonic-gate 
313*7c478bd9Sstevel@tonic-gate 	for (;;) {
314*7c478bd9Sstevel@tonic-gate 		if (MASKUPDATEBIT(in->nsc_callnumber) == GETHOSTBYADDR) {
315*7c478bd9Sstevel@tonic-gate 			bucket = get_hash(addr_hash,
316*7c478bd9Sstevel@tonic-gate 				(char *)addr_to_int(in->nsc_u.addr.a_data));
317*7c478bd9Sstevel@tonic-gate 		} else { /* bounce excessively long requests */
318*7c478bd9Sstevel@tonic-gate 			if (strlen(in->nsc_u.name) > NSCDMAXNAMELEN) {
319*7c478bd9Sstevel@tonic-gate 				ucred_t *uc = NULL;
320*7c478bd9Sstevel@tonic-gate 
321*7c478bd9Sstevel@tonic-gate 				if (door_ucred(&uc) != 0) {
322*7c478bd9Sstevel@tonic-gate 					logit("gethost_lookup: Name too long, "
323*7c478bd9Sstevel@tonic-gate 					    "but no user credential: %s\n",
324*7c478bd9Sstevel@tonic-gate 					    strerror(errno));
325*7c478bd9Sstevel@tonic-gate 				} else {
326*7c478bd9Sstevel@tonic-gate 					logit("gethost_lookup: Name too long "
327*7c478bd9Sstevel@tonic-gate 					    "from pid %d uid %d\n",
328*7c478bd9Sstevel@tonic-gate 					    ucred_getpid(uc),
329*7c478bd9Sstevel@tonic-gate 					    ucred_getruid(uc));
330*7c478bd9Sstevel@tonic-gate 					ucred_free(uc);
331*7c478bd9Sstevel@tonic-gate 				}
332*7c478bd9Sstevel@tonic-gate 
333*7c478bd9Sstevel@tonic-gate 				out->nsc_errno = NSS_NOTFOUND;
334*7c478bd9Sstevel@tonic-gate 				out->nsc_return_code = NOTFOUND;
335*7c478bd9Sstevel@tonic-gate 				out->nsc_bufferbytesused = sizeof (*out);
336*7c478bd9Sstevel@tonic-gate 				goto getout;
337*7c478bd9Sstevel@tonic-gate 			}
338*7c478bd9Sstevel@tonic-gate 			bucket = get_hash(hnam_hash, in->nsc_u.name);
339*7c478bd9Sstevel@tonic-gate 		}
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate 		if (*bucket == (char *)-1) {	/* pending lookup */
342*7c478bd9Sstevel@tonic-gate 			if (get_clearance(in->nsc_callnumber) != 0) {
343*7c478bd9Sstevel@tonic-gate 				/* no threads available */
344*7c478bd9Sstevel@tonic-gate 				out->nsc_return_code = NOSERVER;
345*7c478bd9Sstevel@tonic-gate 				/* cannot process now */
346*7c478bd9Sstevel@tonic-gate 				out->nsc_bufferbytesused = sizeof (*out);
347*7c478bd9Sstevel@tonic-gate 				current_admin.host.nsc_throttle_count++;
348*7c478bd9Sstevel@tonic-gate 				goto getout;
349*7c478bd9Sstevel@tonic-gate 			}
350*7c478bd9Sstevel@tonic-gate 			nscd_wait(&host_wait, &host_lock, bucket);
351*7c478bd9Sstevel@tonic-gate 			release_clearance(in->nsc_callnumber);
352*7c478bd9Sstevel@tonic-gate 			continue; /* go back and relookup hash bucket */
353*7c478bd9Sstevel@tonic-gate 		}
354*7c478bd9Sstevel@tonic-gate 		break;
355*7c478bd9Sstevel@tonic-gate 	}
356*7c478bd9Sstevel@tonic-gate 
357*7c478bd9Sstevel@tonic-gate 	/*
358*7c478bd9Sstevel@tonic-gate 	 * check for no name_service mode
359*7c478bd9Sstevel@tonic-gate 	 */
360*7c478bd9Sstevel@tonic-gate 
361*7c478bd9Sstevel@tonic-gate 	if (*bucket == NULL && current_admin.avoid_nameservice) {
362*7c478bd9Sstevel@tonic-gate 		out->nsc_return_code = NOTFOUND;
363*7c478bd9Sstevel@tonic-gate 		out->nsc_bufferbytesused = sizeof (*out);
364*7c478bd9Sstevel@tonic-gate 	} else if ((*bucket == NULL) ||	/* New entry in name service */
365*7c478bd9Sstevel@tonic-gate 	    (in->nsc_callnumber & UPDATEBIT) || /* needs updating */
366*7c478bd9Sstevel@tonic-gate 	    (out_of_date = (!current_admin.avoid_nameservice &&
367*7c478bd9Sstevel@tonic-gate 		(current_admin.host.nsc_old_data_ok == 0) &&
368*7c478bd9Sstevel@tonic-gate 		(((nsc_bucket_t *)*bucket)->nsc_timestamp < now)))) {
369*7c478bd9Sstevel@tonic-gate 		/* time has expired */
370*7c478bd9Sstevel@tonic-gate 		int saved_errno;
371*7c478bd9Sstevel@tonic-gate 		int saved_hits = 0;
372*7c478bd9Sstevel@tonic-gate 		struct hostent *p;
373*7c478bd9Sstevel@tonic-gate 
374*7c478bd9Sstevel@tonic-gate 		if (get_clearance(in->nsc_callnumber) != 0) {
375*7c478bd9Sstevel@tonic-gate 			/* no threads available */
376*7c478bd9Sstevel@tonic-gate 			out->nsc_return_code = NOSERVER;
377*7c478bd9Sstevel@tonic-gate 			/* cannot process now */
378*7c478bd9Sstevel@tonic-gate 			out->nsc_bufferbytesused = sizeof (*out);
379*7c478bd9Sstevel@tonic-gate 			current_admin.host.nsc_throttle_count++;
380*7c478bd9Sstevel@tonic-gate 			goto getout;
381*7c478bd9Sstevel@tonic-gate 		}
382*7c478bd9Sstevel@tonic-gate 
383*7c478bd9Sstevel@tonic-gate 		if (*bucket != NULL) {
384*7c478bd9Sstevel@tonic-gate 			saved_hits = ((nsc_bucket_t *)*bucket)->nsc_hits;
385*7c478bd9Sstevel@tonic-gate 		}
386*7c478bd9Sstevel@tonic-gate 
387*7c478bd9Sstevel@tonic-gate 		/*
388*7c478bd9Sstevel@tonic-gate 		 * block any threads accessing this bucket if data is
389*7c478bd9Sstevel@tonic-gate 		 * non-existent or out of date
390*7c478bd9Sstevel@tonic-gate 		 */
391*7c478bd9Sstevel@tonic-gate 
392*7c478bd9Sstevel@tonic-gate 		if (*bucket == NULL || out_of_date) {
393*7c478bd9Sstevel@tonic-gate 			update_host_bucket((nsc_bucket_t **)bucket,
394*7c478bd9Sstevel@tonic-gate 						(nsc_bucket_t *)-1,
395*7c478bd9Sstevel@tonic-gate 						in->nsc_callnumber);
396*7c478bd9Sstevel@tonic-gate 		} else {
397*7c478bd9Sstevel@tonic-gate 		/*
398*7c478bd9Sstevel@tonic-gate 		 * if still not -1 bucket we are doing update... mark
399*7c478bd9Sstevel@tonic-gate 		 * to prevent pileups of threads if the name service
400*7c478bd9Sstevel@tonic-gate 		 * is hanging....
401*7c478bd9Sstevel@tonic-gate 		 */
402*7c478bd9Sstevel@tonic-gate 			((nsc_bucket_t *)(*bucket))->nsc_status |=
403*7c478bd9Sstevel@tonic-gate 						ST_UPDATE_PENDING;
404*7c478bd9Sstevel@tonic-gate 			/* cleared by deletion of old data */
405*7c478bd9Sstevel@tonic-gate 		}
406*7c478bd9Sstevel@tonic-gate 		mutex_unlock(&host_lock);
407*7c478bd9Sstevel@tonic-gate 
408*7c478bd9Sstevel@tonic-gate 		if (MASKUPDATEBIT(in->nsc_callnumber) == GETHOSTBYADDR) {
409*7c478bd9Sstevel@tonic-gate 			p = _uncached_gethostbyaddr_r(in->nsc_u.addr.a_data,
410*7c478bd9Sstevel@tonic-gate 				in->nsc_u.addr.a_length,
411*7c478bd9Sstevel@tonic-gate 				in->nsc_u.addr.a_type,
412*7c478bd9Sstevel@tonic-gate 				&out->nsc_u.hst,
413*7c478bd9Sstevel@tonic-gate 				out->nsc_u.buff+sizeof (struct hostent),
414*7c478bd9Sstevel@tonic-gate 						bufferspace,
415*7c478bd9Sstevel@tonic-gate 						&saved_errno);
416*7c478bd9Sstevel@tonic-gate 		} else {
417*7c478bd9Sstevel@tonic-gate 			p = _uncached_gethostbyname_r(in->nsc_u.name,
418*7c478bd9Sstevel@tonic-gate 				&out->nsc_u.hst,
419*7c478bd9Sstevel@tonic-gate 				out->nsc_u.buff+sizeof (struct hostent),
420*7c478bd9Sstevel@tonic-gate 						bufferspace,
421*7c478bd9Sstevel@tonic-gate 						&saved_errno);
422*7c478bd9Sstevel@tonic-gate 		}
423*7c478bd9Sstevel@tonic-gate 
424*7c478bd9Sstevel@tonic-gate 		mutex_lock(&host_lock);
425*7c478bd9Sstevel@tonic-gate 
426*7c478bd9Sstevel@tonic-gate 		release_clearance(in->nsc_callnumber);
427*7c478bd9Sstevel@tonic-gate 
428*7c478bd9Sstevel@tonic-gate 		if (p == NULL) { /* data not found */
429*7c478bd9Sstevel@tonic-gate 			if (current_admin.debug_level >= DBG_CANT_FIND) {
430*7c478bd9Sstevel@tonic-gate 				if (MASKUPDATEBIT(in->nsc_callnumber) ==
431*7c478bd9Sstevel@tonic-gate 					GETHOSTBYADDR) {
432*7c478bd9Sstevel@tonic-gate 		logit("gethost_lookup: nscd COULDN'T FIND address %s\n",
433*7c478bd9Sstevel@tonic-gate 			inet_ntoa(*((struct in_addr *)in->nsc_u.addr.a_data)));
434*7c478bd9Sstevel@tonic-gate 				} else {
435*7c478bd9Sstevel@tonic-gate 		logit("gethost_lookup: nscd COULDN'T FIND host name %s\n",
436*7c478bd9Sstevel@tonic-gate 						in->nsc_u.name);
437*7c478bd9Sstevel@tonic-gate 				}
438*7c478bd9Sstevel@tonic-gate 			}
439*7c478bd9Sstevel@tonic-gate 
440*7c478bd9Sstevel@tonic-gate 			if (!(UPDATEBIT & in->nsc_callnumber))
441*7c478bd9Sstevel@tonic-gate 				current_admin.host.nsc_neg_cache_misses++;
442*7c478bd9Sstevel@tonic-gate 
443*7c478bd9Sstevel@tonic-gate 			retb = (nsc_bucket_t *)malloc(sizeof (nsc_bucket_t));
444*7c478bd9Sstevel@tonic-gate 
445*7c478bd9Sstevel@tonic-gate 			retb->nsc_refcount = 1;
446*7c478bd9Sstevel@tonic-gate 			retb->nsc_data.nsc_return_code = NOTFOUND;
447*7c478bd9Sstevel@tonic-gate 			retb->nsc_data.nsc_bufferbytesused =
448*7c478bd9Sstevel@tonic-gate 				sizeof (nsc_return_t);
449*7c478bd9Sstevel@tonic-gate 			retb->nsc_data.nsc_errno = saved_errno;
450*7c478bd9Sstevel@tonic-gate 			memcpy(out, &(retb->nsc_data),
451*7c478bd9Sstevel@tonic-gate 				retb->nsc_data.nsc_bufferbytesused);
452*7c478bd9Sstevel@tonic-gate 			update_host_bucket((nsc_bucket_t **)bucket, retb,
453*7c478bd9Sstevel@tonic-gate 				in->nsc_callnumber);
454*7c478bd9Sstevel@tonic-gate 			goto getout;
455*7c478bd9Sstevel@tonic-gate 		} else {
456*7c478bd9Sstevel@tonic-gate 			if (current_admin.debug_level >= DBG_ALL) {
457*7c478bd9Sstevel@tonic-gate 				if (MASKUPDATEBIT(in->nsc_callnumber) ==
458*7c478bd9Sstevel@tonic-gate 						GETHOSTBYADDR) {
459*7c478bd9Sstevel@tonic-gate 				logit("gethost_lookup: nscd FOUND addr %s\n",
460*7c478bd9Sstevel@tonic-gate 			inet_ntoa(*((struct in_addr *)in->nsc_u.addr.a_data)));
461*7c478bd9Sstevel@tonic-gate 				} else {
462*7c478bd9Sstevel@tonic-gate 			logit("gethost_lookup: nscd FOUND host name %s\n",
463*7c478bd9Sstevel@tonic-gate 						in->nsc_u.name);
464*7c478bd9Sstevel@tonic-gate 				}
465*7c478bd9Sstevel@tonic-gate 			}
466*7c478bd9Sstevel@tonic-gate 			if (!(UPDATEBIT & in->nsc_callnumber))
467*7c478bd9Sstevel@tonic-gate 				current_admin.host.nsc_pos_cache_misses++;
468*7c478bd9Sstevel@tonic-gate 
469*7c478bd9Sstevel@tonic-gate 			retb = fixbuffer(out, bufferspace);
470*7c478bd9Sstevel@tonic-gate 
471*7c478bd9Sstevel@tonic-gate 			update_host_bucket((nsc_bucket_t **)bucket, retb,
472*7c478bd9Sstevel@tonic-gate 				in->nsc_callnumber);
473*7c478bd9Sstevel@tonic-gate 			if (saved_hits)
474*7c478bd9Sstevel@tonic-gate 				retb->nsc_hits = saved_hits;
475*7c478bd9Sstevel@tonic-gate 		}
476*7c478bd9Sstevel@tonic-gate 	} else { 	/* found entry in cache */
477*7c478bd9Sstevel@tonic-gate 		retb = (nsc_bucket_t *)*bucket;
478*7c478bd9Sstevel@tonic-gate 
479*7c478bd9Sstevel@tonic-gate 		retb->nsc_hits++;
480*7c478bd9Sstevel@tonic-gate 
481*7c478bd9Sstevel@tonic-gate 		memcpy(out, &(retb->nsc_data),
482*7c478bd9Sstevel@tonic-gate 			retb->nsc_data.nsc_bufferbytesused);
483*7c478bd9Sstevel@tonic-gate 
484*7c478bd9Sstevel@tonic-gate 		if (out->nsc_return_code == SUCCESS) {
485*7c478bd9Sstevel@tonic-gate 			if (!(UPDATEBIT & in->nsc_callnumber))
486*7c478bd9Sstevel@tonic-gate 			    current_admin.host.nsc_pos_cache_hits++;
487*7c478bd9Sstevel@tonic-gate 			if (current_admin.debug_level >= DBG_ALL) {
488*7c478bd9Sstevel@tonic-gate 				if (MASKUPDATEBIT(in->nsc_callnumber) ==
489*7c478bd9Sstevel@tonic-gate 					GETHOSTBYADDR) {
490*7c478bd9Sstevel@tonic-gate 			logit("gethost_lookup: found address %s in cache\n",
491*7c478bd9Sstevel@tonic-gate 			inet_ntoa(*((struct in_addr *)in->nsc_u.addr.a_data)));
492*7c478bd9Sstevel@tonic-gate 				} else {
493*7c478bd9Sstevel@tonic-gate 			logit("gethost_lookup: found host name %s in cache\n",
494*7c478bd9Sstevel@tonic-gate 						in->nsc_u.name);
495*7c478bd9Sstevel@tonic-gate 				}
496*7c478bd9Sstevel@tonic-gate 			}
497*7c478bd9Sstevel@tonic-gate 		} else {
498*7c478bd9Sstevel@tonic-gate 			if (!(UPDATEBIT & in->nsc_callnumber))
499*7c478bd9Sstevel@tonic-gate 			    current_admin.host.nsc_neg_cache_hits++;
500*7c478bd9Sstevel@tonic-gate 			if (current_admin.debug_level >= DBG_ALL) {
501*7c478bd9Sstevel@tonic-gate 				if (MASKUPDATEBIT(in->nsc_callnumber) ==
502*7c478bd9Sstevel@tonic-gate 					GETHOSTBYADDR) {
503*7c478bd9Sstevel@tonic-gate 		logit("gethost_lookup: %s marked as NOT FOUND in cache.\n",
504*7c478bd9Sstevel@tonic-gate 			inet_ntoa(*((struct in_addr *)in->nsc_u.addr.a_data)));
505*7c478bd9Sstevel@tonic-gate 				} else {
506*7c478bd9Sstevel@tonic-gate 		logit("gethost_lookup: %s marked as NOT FOUND in cache.\n",
507*7c478bd9Sstevel@tonic-gate 						in->nsc_u.name);
508*7c478bd9Sstevel@tonic-gate 				}
509*7c478bd9Sstevel@tonic-gate 			}
510*7c478bd9Sstevel@tonic-gate 		}
511*7c478bd9Sstevel@tonic-gate 
512*7c478bd9Sstevel@tonic-gate 		if ((retb->nsc_timestamp < now) &&
513*7c478bd9Sstevel@tonic-gate 			!(in->nsc_callnumber & UPDATEBIT) &&
514*7c478bd9Sstevel@tonic-gate 			!(retb->nsc_status & ST_UPDATE_PENDING)) {
515*7c478bd9Sstevel@tonic-gate 		logit("launch update since time = %d\n", retb->nsc_timestamp);
516*7c478bd9Sstevel@tonic-gate 			/* cleared by deletion of old data */
517*7c478bd9Sstevel@tonic-gate 			retb->nsc_status |= ST_UPDATE_PENDING;
518*7c478bd9Sstevel@tonic-gate 			launch_update(in);
519*7c478bd9Sstevel@tonic-gate 		}
520*7c478bd9Sstevel@tonic-gate 	}
521*7c478bd9Sstevel@tonic-gate 
522*7c478bd9Sstevel@tonic-gate getout:
523*7c478bd9Sstevel@tonic-gate 
524*7c478bd9Sstevel@tonic-gate 	mutex_unlock(&host_lock);
525*7c478bd9Sstevel@tonic-gate }
526*7c478bd9Sstevel@tonic-gate 
527*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
528*7c478bd9Sstevel@tonic-gate static void
529*7c478bd9Sstevel@tonic-gate update_host_bucket(nsc_bucket_t **old, nsc_bucket_t *new, int callnumber)
530*7c478bd9Sstevel@tonic-gate {
531*7c478bd9Sstevel@tonic-gate 	if (*old != NULL && *old != (nsc_bucket_t *)-1) {
532*7c478bd9Sstevel@tonic-gate 		/* old data exists */
533*7c478bd9Sstevel@tonic-gate 		free(*old);
534*7c478bd9Sstevel@tonic-gate 		current_admin.host.nsc_entries--;
535*7c478bd9Sstevel@tonic-gate 	}
536*7c478bd9Sstevel@tonic-gate 
537*7c478bd9Sstevel@tonic-gate 	/*
538*7c478bd9Sstevel@tonic-gate 	 *  we can do this before reseting *old since we're holding the lock
539*7c478bd9Sstevel@tonic-gate 	 */
540*7c478bd9Sstevel@tonic-gate 
541*7c478bd9Sstevel@tonic-gate 	else if (*old == (nsc_bucket_t *)-1) {
542*7c478bd9Sstevel@tonic-gate 		nscd_signal(&host_wait, (char **)old);
543*7c478bd9Sstevel@tonic-gate 	}
544*7c478bd9Sstevel@tonic-gate 
545*7c478bd9Sstevel@tonic-gate 
546*7c478bd9Sstevel@tonic-gate 
547*7c478bd9Sstevel@tonic-gate 	*old = new;
548*7c478bd9Sstevel@tonic-gate 
549*7c478bd9Sstevel@tonic-gate 	if ((new != NULL) &&
550*7c478bd9Sstevel@tonic-gate 		(new != (nsc_bucket_t *)-1)) {
551*7c478bd9Sstevel@tonic-gate 		/* real data, not just update pending or invalidate */
552*7c478bd9Sstevel@tonic-gate 
553*7c478bd9Sstevel@tonic-gate 		new->nsc_hits = 1;
554*7c478bd9Sstevel@tonic-gate 		new->nsc_status = 0;
555*7c478bd9Sstevel@tonic-gate 		new->nsc_refcount = 1;
556*7c478bd9Sstevel@tonic-gate 		current_admin.host.nsc_entries++;
557*7c478bd9Sstevel@tonic-gate 
558*7c478bd9Sstevel@tonic-gate 		if (new->nsc_data.nsc_return_code == SUCCESS) {
559*7c478bd9Sstevel@tonic-gate 			new->nsc_timestamp = time(NULL) +
560*7c478bd9Sstevel@tonic-gate 				current_admin.host.nsc_pos_ttl;
561*7c478bd9Sstevel@tonic-gate 		} else {
562*7c478bd9Sstevel@tonic-gate 			new->nsc_timestamp = time(NULL) +
563*7c478bd9Sstevel@tonic-gate 				current_admin.host.nsc_neg_ttl;
564*7c478bd9Sstevel@tonic-gate 		}
565*7c478bd9Sstevel@tonic-gate 	}
566*7c478bd9Sstevel@tonic-gate }
567*7c478bd9Sstevel@tonic-gate 
568*7c478bd9Sstevel@tonic-gate 
569*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
570*7c478bd9Sstevel@tonic-gate static nsc_bucket_t *
571*7c478bd9Sstevel@tonic-gate fixbuffer(nsc_return_t *in, int maxlen)
572*7c478bd9Sstevel@tonic-gate {
573*7c478bd9Sstevel@tonic-gate 	nsc_return_t *out;
574*7c478bd9Sstevel@tonic-gate 	nsc_bucket_t *retb;
575*7c478bd9Sstevel@tonic-gate 	char *dest;
576*7c478bd9Sstevel@tonic-gate 	char **aliaseslist;
577*7c478bd9Sstevel@tonic-gate 	char **addrlist;
578*7c478bd9Sstevel@tonic-gate 	int offset;
579*7c478bd9Sstevel@tonic-gate 	int strs;
580*7c478bd9Sstevel@tonic-gate 	int i;
581*7c478bd9Sstevel@tonic-gate 	int numaliases;
582*7c478bd9Sstevel@tonic-gate 	int numaddrs;
583*7c478bd9Sstevel@tonic-gate 
584*7c478bd9Sstevel@tonic-gate 	/*
585*7c478bd9Sstevel@tonic-gate 	 *  find out the size of the data block we're going to need
586*7c478bd9Sstevel@tonic-gate 	 */
587*7c478bd9Sstevel@tonic-gate 
588*7c478bd9Sstevel@tonic-gate 	strs = 1 + strlen(in->nsc_u.hst.h_name);
589*7c478bd9Sstevel@tonic-gate 	for (numaliases = 0; in->nsc_u.hst.h_aliases[numaliases]; numaliases++)
590*7c478bd9Sstevel@tonic-gate 		strs += 1 + strlen(in->nsc_u.hst.h_aliases[numaliases]);
591*7c478bd9Sstevel@tonic-gate 	strs += sizeof (char *) * (numaliases+1);
592*7c478bd9Sstevel@tonic-gate 	for (numaddrs = 0; in->nsc_u.hst.h_addr_list[numaddrs]; numaddrs++)
593*7c478bd9Sstevel@tonic-gate 		strs += in->nsc_u.hst.h_length;
594*7c478bd9Sstevel@tonic-gate 	strs += sizeof (char *) * (numaddrs+1+3);
595*7c478bd9Sstevel@tonic-gate 
596*7c478bd9Sstevel@tonic-gate 	/*
597*7c478bd9Sstevel@tonic-gate 	 * allocate it and copy it in
598*7c478bd9Sstevel@tonic-gate 	 * code doesn't assume packing order in original buffer
599*7c478bd9Sstevel@tonic-gate 	 */
600*7c478bd9Sstevel@tonic-gate 
601*7c478bd9Sstevel@tonic-gate 	if ((retb = (nsc_bucket_t *)malloc(sizeof (*retb) + strs)) == NULL) {
602*7c478bd9Sstevel@tonic-gate 		return (NULL);
603*7c478bd9Sstevel@tonic-gate 	}
604*7c478bd9Sstevel@tonic-gate 
605*7c478bd9Sstevel@tonic-gate 	out = &(retb->nsc_data);
606*7c478bd9Sstevel@tonic-gate 	out->nsc_bufferbytesused = sizeof (*in) + strs;
607*7c478bd9Sstevel@tonic-gate 	out->nsc_return_code 	= SUCCESS;
608*7c478bd9Sstevel@tonic-gate 	out->nsc_errno 		= 0;
609*7c478bd9Sstevel@tonic-gate 
610*7c478bd9Sstevel@tonic-gate 
611*7c478bd9Sstevel@tonic-gate 	dest = retb->nsc_data.nsc_u.buff + sizeof (struct hostent);
612*7c478bd9Sstevel@tonic-gate 
613*7c478bd9Sstevel@tonic-gate 	offset = (int)dest;
614*7c478bd9Sstevel@tonic-gate 
615*7c478bd9Sstevel@tonic-gate 	/*
616*7c478bd9Sstevel@tonic-gate 	 * allocat the h_aliases list and the h_addr_list first to align 'em.
617*7c478bd9Sstevel@tonic-gate 	 */
618*7c478bd9Sstevel@tonic-gate 	aliaseslist = (char **)dest;
619*7c478bd9Sstevel@tonic-gate 
620*7c478bd9Sstevel@tonic-gate 	dest += sizeof (char *) * (numaliases+1);
621*7c478bd9Sstevel@tonic-gate 
622*7c478bd9Sstevel@tonic-gate 	addrlist = (char **)dest;
623*7c478bd9Sstevel@tonic-gate 
624*7c478bd9Sstevel@tonic-gate 	dest += sizeof (char *) * (numaddrs+1);
625*7c478bd9Sstevel@tonic-gate 
626*7c478bd9Sstevel@tonic-gate 	strcpy(dest, in->nsc_u.hst.h_name);
627*7c478bd9Sstevel@tonic-gate 	strs = 1 + strlen(in->nsc_u.hst.h_name);
628*7c478bd9Sstevel@tonic-gate 	out->nsc_u.hst.h_name = dest - offset;
629*7c478bd9Sstevel@tonic-gate 	dest += strs;
630*7c478bd9Sstevel@tonic-gate 
631*7c478bd9Sstevel@tonic-gate 
632*7c478bd9Sstevel@tonic-gate 	/*
633*7c478bd9Sstevel@tonic-gate 	 * fill out the h_aliases list
634*7c478bd9Sstevel@tonic-gate 	 */
635*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < numaliases; i++) {
636*7c478bd9Sstevel@tonic-gate 		strcpy(dest, in->nsc_u.hst.h_aliases[i]);
637*7c478bd9Sstevel@tonic-gate 		strs = 1 + strlen(in->nsc_u.hst.h_aliases[i]);
638*7c478bd9Sstevel@tonic-gate 		aliaseslist[i] = dest - offset;
639*7c478bd9Sstevel@tonic-gate 		dest += strs;
640*7c478bd9Sstevel@tonic-gate 	}
641*7c478bd9Sstevel@tonic-gate 	aliaseslist[i] = 0;	/* null term ptr chain */
642*7c478bd9Sstevel@tonic-gate 
643*7c478bd9Sstevel@tonic-gate 	out->nsc_u.hst.h_aliases = (char **)((int)aliaseslist-offset);
644*7c478bd9Sstevel@tonic-gate 
645*7c478bd9Sstevel@tonic-gate 	/*
646*7c478bd9Sstevel@tonic-gate 	 * fill out the h_addr list
647*7c478bd9Sstevel@tonic-gate 	 */
648*7c478bd9Sstevel@tonic-gate 
649*7c478bd9Sstevel@tonic-gate 	dest = (char *)(((int)dest + 3) & ~3);
650*7c478bd9Sstevel@tonic-gate 
651*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < numaddrs; i++) {
652*7c478bd9Sstevel@tonic-gate 		memcpy(dest, in->nsc_u.hst.h_addr_list[i],
653*7c478bd9Sstevel@tonic-gate 			in->nsc_u.hst.h_length);
654*7c478bd9Sstevel@tonic-gate 		strs = in->nsc_u.hst.h_length;
655*7c478bd9Sstevel@tonic-gate 		addrlist[i] = dest - offset;
656*7c478bd9Sstevel@tonic-gate 		dest += strs;
657*7c478bd9Sstevel@tonic-gate 		dest = (char *)(((int)dest + 3) & ~3);
658*7c478bd9Sstevel@tonic-gate 	}
659*7c478bd9Sstevel@tonic-gate 
660*7c478bd9Sstevel@tonic-gate 	addrlist[i] = 0;	/* null term ptr chain */
661*7c478bd9Sstevel@tonic-gate 
662*7c478bd9Sstevel@tonic-gate 	out->nsc_u.hst.h_addr_list = (char **)((int)addrlist-offset);
663*7c478bd9Sstevel@tonic-gate 
664*7c478bd9Sstevel@tonic-gate 	out->nsc_u.hst.h_length = in->nsc_u.hst.h_length;
665*7c478bd9Sstevel@tonic-gate 	out->nsc_u.hst.h_addrtype = in->nsc_u.hst.h_addrtype;
666*7c478bd9Sstevel@tonic-gate 
667*7c478bd9Sstevel@tonic-gate 	memcpy(in, &(retb->nsc_data), retb->nsc_data.nsc_bufferbytesused);
668*7c478bd9Sstevel@tonic-gate 
669*7c478bd9Sstevel@tonic-gate 	return (retb);
670*7c478bd9Sstevel@tonic-gate 
671*7c478bd9Sstevel@tonic-gate }
672*7c478bd9Sstevel@tonic-gate 
673*7c478bd9Sstevel@tonic-gate void
674*7c478bd9Sstevel@tonic-gate gethost_nam_reaper()
675*7c478bd9Sstevel@tonic-gate {
676*7c478bd9Sstevel@tonic-gate 	nsc_reaper("gethost_nam", hnam_hash, &current_admin.host, &host_lock);
677*7c478bd9Sstevel@tonic-gate }
678*7c478bd9Sstevel@tonic-gate 
679*7c478bd9Sstevel@tonic-gate void
680*7c478bd9Sstevel@tonic-gate gethost_addr_reaper()
681*7c478bd9Sstevel@tonic-gate {
682*7c478bd9Sstevel@tonic-gate 	nsc_reaper("gethost_addr", addr_hash, &current_admin.host, &host_lock);
683*7c478bd9Sstevel@tonic-gate }
684