summaryrefslogtreecommitdiff
path: root/usr/src/cmd/nscd/getuser.c
blob: fe654ef07098b81d36c2a1b1251f01d3bb862407 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * Routines to handle getuser* calls in nscd
 */

#include <assert.h>
#include <errno.h>
#include <memory.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/door.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <thread.h>
#include <unistd.h>
#include <ucred.h>
#include <nss_common.h>

#include <user_attr.h>

#include <getxby_door.h>
#include "server_door.h"
#include "nscd.h"

extern userstr_t *_getusernam(const char *, userstr_t *, char *, int, int *);

static hash_t *nam_hash;
static mutex_t  db_lock = DEFAULTMUTEX;
static waiter_t db_wait;

static void getuser_namekeepalive(int keep, int interval);
static void update_user_bucket(nsc_bucket_t **old, nsc_bucket_t *new,
    int callnumber);
static nsc_bucket_t *fixbuffer(nsc_return_t *in, int maxlen);
static void do_findgnams(nsc_bucket_t *ptr, int *table, char *gnam);
static void do_invalidate(nsc_bucket_t **ptr, int callnumber);
static void getuser_invalidate_unlocked(void);

void
getuser_init(void)
{
	nam_hash = make_hash(current_admin.user.nsc_suggestedsize);
}

static void
do_invalidate(nsc_bucket_t ** ptr, int callnumber)
{
	if (*ptr != NULL && *ptr != (nsc_bucket_t *)-1) {
		/* leave pending calls alone */
		update_user_bucket(ptr, NULL, callnumber);
	}
}

static void
do_findgnams(nsc_bucket_t *ptr, int *table, char *gnam)
{

	/*
	 * be careful with ptr - it may be -1 or NULL.
	 */

	if (ptr != NULL && ptr != (nsc_bucket_t *)-1) {
		char *tmp = (char *)insertn(table, ptr->nsc_hits,
		    (int)strdup(gnam));
		if (tmp != (char *)-1)
			free(tmp);
	}
}

void
getuser_revalidate(void)
{
	for (;;) {
		int slp;
		int interval;
		int count;

		slp = current_admin.user.nsc_pos_ttl;

		if (slp < 60) {
			slp = 60;
		}

		if ((count = current_admin.user.nsc_keephot) != 0) {
			interval = (slp / 2)/count;
			if (interval == 0) interval = 1;
			sleep(slp * 2 / 3);
			getuser_namekeepalive(count, interval);
		} else {
			sleep(slp);
		}
	}
}

static void
getuser_namekeepalive(int keep, int interval)
{
	int *table;
	union {
		nsc_data_t  ping;
		char space[sizeof (nsc_data_t) + NSCDMAXNAMELEN];
	} u;

	int i;

	if (!keep)
		return;

	table = maken(keep);
	mutex_lock(&db_lock);
	operate_hash(nam_hash, do_findgnams, (char *)table);
	mutex_unlock(&db_lock);

	for (i = 1; i <= keep; i++) {
		char *tmp;
		u.ping.nsc_call.nsc_callnumber = GETUSERNAM;

		if ((tmp = (char *)table[keep + 1 + i]) == (char *)-1)
			continue; /* unused slot in table */

		strcpy(u.ping.nsc_call.nsc_u.name, tmp);

		launch_update(&u.ping.nsc_call);
		sleep(interval);
	}

	for (i = 1; i <= keep; i++) {
		char *tmp;
		if ((tmp = (char *)table[keep + 1 + i]) != (char *)-1)
			free(tmp);
	}

	free(table);
}


/*
 *   This routine marks all entries as invalid
 *
 */

void
getuser_invalidate()
{
	mutex_lock(&db_lock);
	getuser_invalidate_unlocked();
	mutex_unlock(&db_lock);
}

static void
getuser_invalidate_unlocked()
{
	operate_hash_addr(nam_hash, do_invalidate, (char *)GETUSERNAM);
	current_admin.user.nsc_invalidate_count++;
}

void
getuser_lookup(nsc_return_t *out, int maxsize, nsc_call_t *in, time_t now)
{
	int		out_of_date;
	nsc_bucket_t	*retb;
	char 		**bucket;

	static time_t	lastmod;

	int bufferspace = maxsize - sizeof (nsc_return_t);

	if (current_admin.user.nsc_enabled == 0) {
		out->nsc_return_code = NOSERVER;
		out->nsc_bufferbytesused = sizeof (*out);
		return;
	}

	mutex_lock(&db_lock);

	if (current_admin.user.nsc_check_files) {
		struct stat buf;

		if (stat(USERATTR_FILENAME, &buf) < 0) {
			/*EMPTY*/;
		} else if (lastmod == 0) {
			lastmod = buf.st_mtime;
		} else if (lastmod < buf.st_mtime) {
			getuser_invalidate_unlocked();
			lastmod = buf.st_mtime;
		}
	}

	if (current_admin.debug_level >= DBG_ALL) {
		logit("getuser_lookup: looking for name %s\n",
				in->nsc_u.name);
	}

	for (;;) {
		if (attr_strlen(in->nsc_u.name) > NSCDMAXNAMELEN) {
			ucred_t *uc = NULL;

			if (door_ucred(&uc) != 0) {
				logit("getuser_lookup: Name too long, "
				    "but no user credential: %s\n",
				    strerror(errno));
			} else {
				logit("getuser_lookup: Name too long "
				    "from pid %d uid %d\n",
				    ucred_getpid(uc),
				    ucred_getruid(uc));
				ucred_free(uc);
			}

			out->nsc_errno = NSS_NOTFOUND;
			out->nsc_return_code = NOTFOUND;
			out->nsc_bufferbytesused = sizeof (*out);
			goto getout;
		}
		bucket = get_hash(nam_hash, in->nsc_u.name);

		if (*bucket == (char *)-1) {	/* pending lookup */
			if (get_clearance(in->nsc_callnumber) != 0) {
			    /* no threads available */
				out->nsc_return_code = NOSERVER;
				    /* cannot process now */
				out->nsc_bufferbytesused =
				    sizeof (*out);
				current_admin.user.nsc_throttle_count++;
				goto getout;
			}
			nscd_wait(&db_wait, &db_lock, bucket);
			release_clearance(in->nsc_callnumber);
			continue; /* go back and relookup hash bucket */
		}
		break;
	}

	/*
	 * check for no name_service mode
	 */

	if (*bucket == NULL && current_admin.avoid_nameservice) {
		out->nsc_return_code = NOTFOUND;
		out->nsc_bufferbytesused = sizeof (*out);
	} else if ((*bucket == NULL) || /* New entry in name service */
	    (in->nsc_callnumber & UPDATEBIT) || /* needs updating */
	    (out_of_date = (!current_admin.avoid_nameservice &&
	    (current_admin.user.nsc_old_data_ok == 0) &&
	    (((nsc_bucket_t *)*bucket)->nsc_timestamp < now)))) {
		/* time has expired */
		int saved_errno;
		int saved_hits = 0;
		userstr_t *p;

		if (get_clearance(in->nsc_callnumber) != 0) {
		    /* no threads available */
			out->nsc_return_code = NOSERVER;
			    /* cannot process now */
			out->nsc_bufferbytesused = sizeof (*out);
			current_admin.user.nsc_throttle_count++;
			goto getout;
		}

		if (*bucket != NULL) {
			saved_hits =
			    ((nsc_bucket_t *)*bucket)->nsc_hits;
		}

		/*
		 *  block any threads accessing this bucket if data is
		 *  non-existent out of date
		 */

		if (*bucket == NULL || out_of_date) {
			update_user_bucket((nsc_bucket_t **)bucket,
			    (nsc_bucket_t *)-1, in->nsc_callnumber);
		} else {
		/*
		 * if still not -1 bucket we are doing update...
		 * mark to prevent
		 * pileups of threads if the name service is hanging....
		 */
			((nsc_bucket_t *)(*bucket))->nsc_status |=
			    ST_UPDATE_PENDING;
			/* cleared by deletion of old data */
		}
		mutex_unlock(&db_lock);

		/*
		 * Call non-caching version in libnsl.
		 */
		p = _getusernam(in->nsc_u.name, &out->nsc_u.user,
		    out->nsc_u.buff + sizeof (userstr_t),
		    bufferspace, &errno);
		saved_errno = errno;

		mutex_lock(&db_lock);

		release_clearance(in->nsc_callnumber);

		if (p == NULL) { /* data not found */

			if (current_admin.debug_level >= DBG_CANT_FIND) {
		logit("getuser_lookup: nscd COULDN'T FIND user_attr name %s\n",
						in->nsc_u.name);
			}


			if (!(UPDATEBIT & in->nsc_callnumber))
			    current_admin.user.nsc_neg_cache_misses++;

			retb = (nsc_bucket_t *)malloc(sizeof (nsc_bucket_t));

			retb->nsc_refcount = 1;
			retb->nsc_data.nsc_bufferbytesused =
				sizeof (nsc_return_t);
			retb->nsc_data.nsc_return_code = NOTFOUND;
			retb->nsc_data.nsc_errno = saved_errno;
			memcpy(out, &retb->nsc_data,
			    retb->nsc_data.nsc_bufferbytesused);
			update_user_bucket((nsc_bucket_t **)bucket,
			    retb, in->nsc_callnumber);
			goto getout;
		} else {
			if (current_admin.debug_level >= DBG_ALL) {
		logit("getuser_lookup: nscd FOUND user_attr name %s\n",
						in->nsc_u.name);
			}
			if (!(UPDATEBIT & in->nsc_callnumber))
			    current_admin.user.nsc_pos_cache_misses++;

			retb = fixbuffer(out, bufferspace);
			update_user_bucket((nsc_bucket_t **)bucket,
			    retb, in->nsc_callnumber);
			if (saved_hits)
				retb->nsc_hits = saved_hits;
		}
	} else { 	/* found entry in cache */
		retb = (nsc_bucket_t *)*bucket;

		retb->nsc_hits++;

		memcpy(out, &(retb->nsc_data),
		    retb->nsc_data.nsc_bufferbytesused);

		if (out->nsc_return_code == SUCCESS) {
			if (!(UPDATEBIT & in->nsc_callnumber))
			    current_admin.user.nsc_pos_cache_hits++;
			if (current_admin.debug_level >= DBG_ALL) {
			logit("getuser_lookup: found name %s in cache\n",
						in->nsc_u.name);
			}
		} else {
			if (!(UPDATEBIT & in->nsc_callnumber))
			    current_admin.user.nsc_neg_cache_hits++;
			if (current_admin.debug_level >= DBG_ALL) {
		logit("getuser_lookup: %s marked as NOT FOUND in cache.\n",
						in->nsc_u.name);
			}
		}

		if ((retb->nsc_timestamp < now) &&
		    !(in->nsc_callnumber & UPDATEBIT) &&
		    !(retb->nsc_status & ST_UPDATE_PENDING)) {
		logit("launch update since time = %d\n", retb->nsc_timestamp);
			retb->nsc_status |= ST_UPDATE_PENDING;
			/* cleared by deletion of old data */
			launch_update(in);
		}
	}

getout:
	mutex_unlock(&db_lock);
}

/*ARGSUSED*/
static void
update_user_bucket(nsc_bucket_t **old, nsc_bucket_t *new, int callnumber)
{
	if (*old != NULL && *old != (nsc_bucket_t *)-1) { /* old data exists */
		free(*old);
		current_admin.user.nsc_entries--;
	}

	/*
	 *  we can do this before reseting *old since we're holding the lock
	 */

	else if (*old == (nsc_bucket_t *)-1) {
		nscd_signal(&db_wait, (char **)old);
	}

	*old = new;

	if ((new != NULL) &&
	    (new != (nsc_bucket_t *)-1)) {
		/* real data, not just update pending or invalidate */
		new->nsc_hits = 1;
		new->nsc_status = 0;
		new->nsc_refcount = 1;
		current_admin.user.nsc_entries++;

		if (new->nsc_data.nsc_return_code == SUCCESS) {
			new->nsc_timestamp = time(NULL) +
			    current_admin.user.nsc_pos_ttl;
		} else {
			new->nsc_timestamp = time(NULL) +
			    current_admin.user.nsc_neg_ttl;
		}
	}
}

/*ARGSUSED*/
static nsc_bucket_t *
fixbuffer(nsc_return_t *in, int maxlen)
{
	nsc_bucket_t *retb;
	nsc_return_t *out;
	char 	*dest;
	int 	offset;
	int 	strs;

	/*
	 * find out the size of the data block we're going to need
	 */

	strs = attr_strlen(in->nsc_u.user.name) +
	    attr_strlen(in->nsc_u.user.qualifier) +
	    attr_strlen(in->nsc_u.user.res1) +
	    attr_strlen(in->nsc_u.user.res2) +
	    attr_strlen(in->nsc_u.user.attr) + USERATTR_DB_NCOL;

	/*
	 * allocate it and copy it in
	 * code doesn't assume packing order in original buffer
	 */

	if ((retb = (nsc_bucket_t *)malloc(sizeof (*retb) + strs)) == NULL) {
		return (NULL);
	}

	out = &(retb->nsc_data);
	out->nsc_bufferbytesused = strs + ((int)&out->nsc_u.user - (int)out) +
	    sizeof (userstr_t);
	out->nsc_return_code 	= SUCCESS;
	out->nsc_errno 		= 0;

	dest = retb->nsc_data.nsc_u.buff + sizeof (userstr_t);
	offset = (int)dest;

	attr_strcpy(dest, in->nsc_u.user.name);
	strs = 1 + attr_strlen(in->nsc_u.user.name);
	out->nsc_u.user.name = dest - offset;
	dest += strs;

	attr_strcpy(dest, in->nsc_u.user.qualifier);
	strs = 1 + attr_strlen(in->nsc_u.user.qualifier);
	out->nsc_u.user.qualifier = dest - offset;
	dest += strs;

	attr_strcpy(dest, in->nsc_u.user.res1);
	strs = 1 + attr_strlen(in->nsc_u.user.res1);
	out->nsc_u.user.res1 = dest - offset;
	dest += strs;

	attr_strcpy(dest, in->nsc_u.user.res2);
	strs = 1 + attr_strlen(in->nsc_u.user.res2);
	out->nsc_u.user.res2 = dest - offset;
	dest += strs;

	attr_strcpy(dest, in->nsc_u.user.attr);
	out->nsc_u.user.attr = dest - offset;

	memcpy(in, out, out->nsc_bufferbytesused);

	return (retb);
}

void
getuser_reaper(void)
{
	nsc_reaper("getuser", nam_hash, &current_admin.user, &db_lock);
}