10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51914Scasper  * Common Development and Distribution License (the "License").
61914Scasper  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
221914Scasper  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  *
250Sstevel@tonic-gate  * Common code and structures used by name-service-switch "files" backends.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * An implementation that used mmap() sensibly would be a wonderful thing,
320Sstevel@tonic-gate  *   but this here is just yer standard fgets() thang.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #include "files_common.h"
360Sstevel@tonic-gate #include <stdio.h>
370Sstevel@tonic-gate #include <stdlib.h>
380Sstevel@tonic-gate #include <string.h>
390Sstevel@tonic-gate #include <ctype.h>
400Sstevel@tonic-gate #include <fcntl.h>
410Sstevel@tonic-gate #include <poll.h>
420Sstevel@tonic-gate #include <unistd.h>
430Sstevel@tonic-gate #include <sys/stat.h>
44*2830Sdjl #include <sys/mman.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate /*ARGSUSED*/
470Sstevel@tonic-gate nss_status_t
480Sstevel@tonic-gate _nss_files_setent(be, dummy)
490Sstevel@tonic-gate 	files_backend_ptr_t	be;
500Sstevel@tonic-gate 	void			*dummy;
510Sstevel@tonic-gate {
520Sstevel@tonic-gate 	if (be->f == 0) {
530Sstevel@tonic-gate 		if (be->filename == 0) {
540Sstevel@tonic-gate 			/* Backend isn't initialized properly? */
550Sstevel@tonic-gate 			return (NSS_UNAVAIL);
560Sstevel@tonic-gate 		}
571914Scasper 		if ((be->f = fopen(be->filename, "rF")) == 0) {
580Sstevel@tonic-gate 			return (NSS_UNAVAIL);
590Sstevel@tonic-gate 		}
600Sstevel@tonic-gate 	} else {
611914Scasper 		rewind(be->f);
620Sstevel@tonic-gate 	}
630Sstevel@tonic-gate 	return (NSS_SUCCESS);
640Sstevel@tonic-gate }
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /*ARGSUSED*/
670Sstevel@tonic-gate nss_status_t
680Sstevel@tonic-gate _nss_files_endent(be, dummy)
690Sstevel@tonic-gate 	files_backend_ptr_t	be;
700Sstevel@tonic-gate 	void			*dummy;
710Sstevel@tonic-gate {
720Sstevel@tonic-gate 	if (be->f != 0) {
73*2830Sdjl 		(void) fclose(be->f);
740Sstevel@tonic-gate 		be->f = 0;
750Sstevel@tonic-gate 	}
760Sstevel@tonic-gate 	if (be->buf != 0) {
770Sstevel@tonic-gate 		free(be->buf);
780Sstevel@tonic-gate 		be->buf = 0;
790Sstevel@tonic-gate 	}
800Sstevel@tonic-gate 	return (NSS_SUCCESS);
810Sstevel@tonic-gate }
820Sstevel@tonic-gate 
830Sstevel@tonic-gate /*
840Sstevel@tonic-gate  * This routine reads a line, including the processing of continuation
850Sstevel@tonic-gate  * characters.  It always leaves (or inserts) \n\0 at the end of the line.
860Sstevel@tonic-gate  * It returns the length of the line read, excluding the \n\0.  Who's idea
870Sstevel@tonic-gate  * was this?
880Sstevel@tonic-gate  * Returns -1 on EOF.
890Sstevel@tonic-gate  *
900Sstevel@tonic-gate  * Note that since each concurrent call to _nss_files_read_line has
910Sstevel@tonic-gate  * it's own FILE pointer, we can use getc_unlocked w/o difficulties,
920Sstevel@tonic-gate  * a substantial performance win.
930Sstevel@tonic-gate  */
940Sstevel@tonic-gate int
950Sstevel@tonic-gate _nss_files_read_line(f, buffer, buflen)
961914Scasper 	FILE			*f;
970Sstevel@tonic-gate 	char			*buffer;
980Sstevel@tonic-gate 	int			buflen;
990Sstevel@tonic-gate {
1000Sstevel@tonic-gate 	int			linelen;	/* 1st unused slot in buffer */
1010Sstevel@tonic-gate 	int			c;
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	/*CONSTCOND*/
1040Sstevel@tonic-gate 	while (1) {
1050Sstevel@tonic-gate 		linelen = 0;
1060Sstevel@tonic-gate 		while (linelen < buflen - 1) {	/* "- 1" saves room for \n\0 */
1071914Scasper 			switch (c = getc_unlocked(f)) {
1080Sstevel@tonic-gate 			case EOF:
1090Sstevel@tonic-gate 				if (linelen == 0 ||
1100Sstevel@tonic-gate 				    buffer[linelen - 1] == '\\') {
1110Sstevel@tonic-gate 					return (-1);
1120Sstevel@tonic-gate 				} else {
1130Sstevel@tonic-gate 					buffer[linelen    ] = '\n';
1140Sstevel@tonic-gate 					buffer[linelen + 1] = '\0';
1150Sstevel@tonic-gate 					return (linelen);
1160Sstevel@tonic-gate 				}
1170Sstevel@tonic-gate 			case '\n':
1180Sstevel@tonic-gate 				if (linelen > 0 &&
1190Sstevel@tonic-gate 				    buffer[linelen - 1] == '\\') {
1200Sstevel@tonic-gate 					--linelen;  /* remove the '\\' */
1210Sstevel@tonic-gate 				} else {
1220Sstevel@tonic-gate 					buffer[linelen    ] = '\n';
1230Sstevel@tonic-gate 					buffer[linelen + 1] = '\0';
1240Sstevel@tonic-gate 					return (linelen);
1250Sstevel@tonic-gate 				}
1260Sstevel@tonic-gate 				break;
1270Sstevel@tonic-gate 			default:
1280Sstevel@tonic-gate 				buffer[linelen++] = c;
1290Sstevel@tonic-gate 			}
1300Sstevel@tonic-gate 		}
1310Sstevel@tonic-gate 		/* Buffer overflow -- eat rest of line and loop again */
1320Sstevel@tonic-gate 		/* ===> Should syslog() */
1330Sstevel@tonic-gate 		do {
1341914Scasper 			c = getc_unlocked(f);
1350Sstevel@tonic-gate 			if (c == EOF) {
1360Sstevel@tonic-gate 				return (-1);
1370Sstevel@tonic-gate 			}
1380Sstevel@tonic-gate 		} while (c != '\n');
1390Sstevel@tonic-gate 	}
1400Sstevel@tonic-gate 	/*NOTREACHED*/
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate /*
1440Sstevel@tonic-gate  * used only for getgroupbymem() now.
1450Sstevel@tonic-gate  */
1460Sstevel@tonic-gate nss_status_t
1470Sstevel@tonic-gate _nss_files_do_all(be, args, filter, func)
1480Sstevel@tonic-gate 	files_backend_ptr_t	be;
1490Sstevel@tonic-gate 	void			*args;
1500Sstevel@tonic-gate 	const char		*filter;
1510Sstevel@tonic-gate 	files_do_all_func_t	func;
1520Sstevel@tonic-gate {
1530Sstevel@tonic-gate 	char			*buffer;
1540Sstevel@tonic-gate 	int			buflen;
1550Sstevel@tonic-gate 	nss_status_t		res;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	if (be->buf == 0 &&
1580Sstevel@tonic-gate 		(be->buf = malloc(be->minbuf)) == 0) {
1590Sstevel@tonic-gate 		return (NSS_UNAVAIL);
1600Sstevel@tonic-gate 	}
1610Sstevel@tonic-gate 	buffer = be->buf;
1620Sstevel@tonic-gate 	buflen = be->minbuf;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	if ((res = _nss_files_setent(be, 0)) != NSS_SUCCESS) {
1650Sstevel@tonic-gate 		return (res);
1660Sstevel@tonic-gate 	}
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	res = NSS_NOTFOUND;
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	do {
1710Sstevel@tonic-gate 		int		linelen;
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 		if ((linelen = _nss_files_read_line(be->f, buffer,
1740Sstevel@tonic-gate 		    buflen)) < 0) {
1750Sstevel@tonic-gate 			/* End of file */
1760Sstevel@tonic-gate 			break;
1770Sstevel@tonic-gate 		}
1780Sstevel@tonic-gate 		if (filter != 0 && strstr(buffer, filter) == 0) {
1790Sstevel@tonic-gate 			/*
1800Sstevel@tonic-gate 			 * Optimization:  if the entry doesn't contain the
1810Sstevel@tonic-gate 			 *   filter string then it can't be the entry we want,
1820Sstevel@tonic-gate 			 *   so don't bother looking more closely at it.
1830Sstevel@tonic-gate 			 */
1840Sstevel@tonic-gate 			continue;
1850Sstevel@tonic-gate 		}
1860Sstevel@tonic-gate 		res = (*func)(buffer, linelen, args);
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	} while (res == NSS_NOTFOUND);
1890Sstevel@tonic-gate 
190*2830Sdjl 	(void) _nss_files_endent(be, 0);
1910Sstevel@tonic-gate 	return (res);
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate /*
1950Sstevel@tonic-gate  * Could implement this as an iterator function on top of _nss_files_do_all(),
1960Sstevel@tonic-gate  *   but the shared code is small enough that it'd be pretty silly.
1970Sstevel@tonic-gate  */
1980Sstevel@tonic-gate nss_status_t
1990Sstevel@tonic-gate _nss_files_XY_all(be, args, netdb, filter, check)
2000Sstevel@tonic-gate 	files_backend_ptr_t	be;
2010Sstevel@tonic-gate 	nss_XbyY_args_t		*args;
2020Sstevel@tonic-gate 	int			netdb;		/* whether it uses netdb */
2030Sstevel@tonic-gate 						/* format or not */
2040Sstevel@tonic-gate 	const char		*filter;	/* advisory, to speed up */
2050Sstevel@tonic-gate 						/* string search */
2060Sstevel@tonic-gate 	files_XY_check_func	check;	/* NULL means one-shot, for getXXent */
2070Sstevel@tonic-gate {
2080Sstevel@tonic-gate 	nss_status_t		res;
2090Sstevel@tonic-gate 	int	parsestat;
2100Sstevel@tonic-gate 	int (*func)();
2110Sstevel@tonic-gate 
212*2830Sdjl 	if (filter != NULL && *filter == '\0')
213*2830Sdjl 		return (NSS_NOTFOUND);
2140Sstevel@tonic-gate 	if (be->buf == 0 &&
2150Sstevel@tonic-gate 		(be->buf = malloc(be->minbuf)) == 0) {
2160Sstevel@tonic-gate 		return (NSS_UNAVAIL); /* really panic, malloc failed */
2170Sstevel@tonic-gate 	}
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	if (check != 0 || be->f == 0) {
2200Sstevel@tonic-gate 		if ((res = _nss_files_setent(be, 0)) != NSS_SUCCESS) {
2210Sstevel@tonic-gate 			return (res);
2220Sstevel@tonic-gate 		}
2230Sstevel@tonic-gate 	}
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	res = NSS_NOTFOUND;
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	/*CONSTCOND*/
2280Sstevel@tonic-gate 	while (1) {
2290Sstevel@tonic-gate 		char		*instr	= be->buf;
2300Sstevel@tonic-gate 		int		linelen;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 		if ((linelen = _nss_files_read_line(be->f, instr,
2330Sstevel@tonic-gate 		    be->minbuf)) < 0) {
2340Sstevel@tonic-gate 			/* End of file */
2350Sstevel@tonic-gate 			args->returnval = 0;
236*2830Sdjl 			args->returnlen = 0;
2370Sstevel@tonic-gate 			break;
2380Sstevel@tonic-gate 		}
2390Sstevel@tonic-gate 		if (filter != 0 && strstr(instr, filter) == 0) {
2400Sstevel@tonic-gate 			/*
2410Sstevel@tonic-gate 			 * Optimization:  if the entry doesn't contain the
2420Sstevel@tonic-gate 			 *   filter string then it can't be the entry we want,
2430Sstevel@tonic-gate 			 *   so don't bother looking more closely at it.
2440Sstevel@tonic-gate 			 */
2450Sstevel@tonic-gate 			continue;
2460Sstevel@tonic-gate 		}
2470Sstevel@tonic-gate 		if (netdb) {
2480Sstevel@tonic-gate 			char		*first;
2490Sstevel@tonic-gate 			char		*last;
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 			if ((last = strchr(instr, '#')) == 0) {
2520Sstevel@tonic-gate 				last = instr + linelen;
2530Sstevel@tonic-gate 			}
2540Sstevel@tonic-gate 			*last-- = '\0';		/* Nuke '\n' or #comment */
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 			/*
2570Sstevel@tonic-gate 			 * Skip leading whitespace.  Normally there isn't
2580Sstevel@tonic-gate 			 *   any, so it's not worth calling strspn().
2590Sstevel@tonic-gate 			 */
2600Sstevel@tonic-gate 			for (first = instr;  isspace(*first);  first++) {
2610Sstevel@tonic-gate 				;
2620Sstevel@tonic-gate 			}
2630Sstevel@tonic-gate 			if (*first == '\0') {
2640Sstevel@tonic-gate 				continue;
2650Sstevel@tonic-gate 			}
2660Sstevel@tonic-gate 			/*
2670Sstevel@tonic-gate 			 * Found something non-blank on the line.  Skip back
2680Sstevel@tonic-gate 			 * over any trailing whitespace;  since we know
2690Sstevel@tonic-gate 			 * there's non-whitespace earlier in the line,
2700Sstevel@tonic-gate 			 * checking for termination is easy.
2710Sstevel@tonic-gate 			 */
2720Sstevel@tonic-gate 			while (isspace(*last)) {
2730Sstevel@tonic-gate 				--last;
2740Sstevel@tonic-gate 			}
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 			linelen = last - first + 1;
2770Sstevel@tonic-gate 			if (first != instr) {
2780Sstevel@tonic-gate 					instr = first;
2790Sstevel@tonic-gate 			}
2800Sstevel@tonic-gate 		}
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 		args->returnval = 0;
283*2830Sdjl 		args->returnlen = 0;
284*2830Sdjl 
285*2830Sdjl 		if (check != NULL && (*check)(args, instr, linelen) == 0)
286*2830Sdjl 			continue;
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 		func = args->str2ent;
2890Sstevel@tonic-gate 		parsestat = (*func)(instr, linelen, args->buf.result,
2900Sstevel@tonic-gate 					args->buf.buffer, args->buf.buflen);
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 		if (parsestat == NSS_STR_PARSE_SUCCESS) {
293*2830Sdjl 			args->returnval = (args->buf.result != NULL)?
294*2830Sdjl 					args->buf.result : args->buf.buffer;
295*2830Sdjl 			args->returnlen = linelen;
296*2830Sdjl 			res = NSS_SUCCESS;
297*2830Sdjl 			break;
2980Sstevel@tonic-gate 		} else if (parsestat == NSS_STR_PARSE_ERANGE) {
2990Sstevel@tonic-gate 			args->erange = 1;
3000Sstevel@tonic-gate 			break;
3010Sstevel@tonic-gate 		} /* else if (parsestat == NSS_STR_PARSE_PARSE) don't care ! */
3020Sstevel@tonic-gate 	}
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	/*
3050Sstevel@tonic-gate 	 * stayopen is set to 0 by default in order to close the opened
3060Sstevel@tonic-gate 	 * file.  Some applications may break if it is set to 1.
3070Sstevel@tonic-gate 	 */
3080Sstevel@tonic-gate 	if (check != 0 && !args->stayopen) {
3090Sstevel@tonic-gate 		(void) _nss_files_endent(be, 0);
3100Sstevel@tonic-gate 	}
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	return (res);
3130Sstevel@tonic-gate }
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate /*
3160Sstevel@tonic-gate  * File hashing support.  Critical for sites with large (e.g. 1000+ lines)
3170Sstevel@tonic-gate  * /etc/passwd or /etc/group files.  Currently only used by getpw*() and
3180Sstevel@tonic-gate  * getgr*() routines, but any files backend can use this stuff.
3190Sstevel@tonic-gate  */
3200Sstevel@tonic-gate static void
3210Sstevel@tonic-gate _nss_files_hash_destroy(files_hash_t *fhp)
3220Sstevel@tonic-gate {
3230Sstevel@tonic-gate 	free(fhp->fh_table);
3240Sstevel@tonic-gate 	fhp->fh_table = NULL;
3250Sstevel@tonic-gate 	free(fhp->fh_line);
3260Sstevel@tonic-gate 	fhp->fh_line = NULL;
3270Sstevel@tonic-gate 	free(fhp->fh_file_start);
3280Sstevel@tonic-gate 	fhp->fh_file_start = NULL;
3290Sstevel@tonic-gate }
3300Sstevel@tonic-gate #ifdef PIC
3310Sstevel@tonic-gate /*
3320Sstevel@tonic-gate  * It turns out the hashing stuff really needs to be disabled for processes
3330Sstevel@tonic-gate  * other than the nscd; the consumption of swap space and memory is otherwise
3340Sstevel@tonic-gate  * unacceptable when the nscd is killed w/ a large passwd file (4M) active.
3350Sstevel@tonic-gate  * See 4031930 for details.
3360Sstevel@tonic-gate  * So we just use this psuedo function to enable the hashing feature.  Since
3370Sstevel@tonic-gate  * this function name is private, we just create a function w/ the name
3380Sstevel@tonic-gate  *  __nss_use_files_hash in the nscd itself and everyone else uses the old
3390Sstevel@tonic-gate  * interface.
3400Sstevel@tonic-gate  * We also disable hashing for .a executables to avoid problems with large
3410Sstevel@tonic-gate  * files....
3420Sstevel@tonic-gate  */
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate #pragma weak __nss_use_files_hash
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate extern void  __nss_use_files_hash(void);
3470Sstevel@tonic-gate #endif /* pic */
3480Sstevel@tonic-gate 
349*2830Sdjl /*ARGSUSED*/
3500Sstevel@tonic-gate nss_status_t
3510Sstevel@tonic-gate _nss_files_XY_hash(files_backend_ptr_t be, nss_XbyY_args_t *args,
3520Sstevel@tonic-gate 	int netdb, files_hash_t *fhp, int hashop, files_XY_check_func check)
3530Sstevel@tonic-gate {
354*2830Sdjl 	/* LINTED E_FUNC_VAR_UNUSED */
3550Sstevel@tonic-gate 	int fd, retries, ht;
356*2830Sdjl 	/* LINTED E_FUNC_VAR_UNUSED */
3570Sstevel@tonic-gate 	uint_t hash, line, f;
358*2830Sdjl 	/* LINTED E_FUNC_VAR_UNUSED */
3590Sstevel@tonic-gate 	files_hashent_t *hp, *htab;
360*2830Sdjl 	/* LINTED E_FUNC_VAR_UNUSED */
3610Sstevel@tonic-gate 	char *cp, *first, *last;
362*2830Sdjl 	/* LINTED E_FUNC_VAR_UNUSED */
3630Sstevel@tonic-gate 	nss_XbyY_args_t xargs;
364*2830Sdjl 	/* LINTED E_FUNC_VAR_UNUSED */
3650Sstevel@tonic-gate 	struct stat64 st;
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate #ifndef PIC
3680Sstevel@tonic-gate 	return (_nss_files_XY_all(be, args, netdb, 0, check));
3690Sstevel@tonic-gate }
3700Sstevel@tonic-gate #else
3710Sstevel@tonic-gate 	if (__nss_use_files_hash == 0)
3720Sstevel@tonic-gate 		return (_nss_files_XY_all(be, args, netdb, 0, check));
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	mutex_lock(&fhp->fh_lock);
3750Sstevel@tonic-gate retry:
3760Sstevel@tonic-gate 	retries = 100;
3770Sstevel@tonic-gate 	while (stat64(be->filename, &st) < 0) {
3780Sstevel@tonic-gate 		/*
3790Sstevel@tonic-gate 		 * On a healthy system this can't happen except during brief
3800Sstevel@tonic-gate 		 * periods when the file is being modified/renamed.  Keep
3810Sstevel@tonic-gate 		 * trying until things settle down, but eventually give up.
3820Sstevel@tonic-gate 		 */
3830Sstevel@tonic-gate 		if (--retries == 0)
3840Sstevel@tonic-gate 			goto unavail;
3850Sstevel@tonic-gate 		poll(0, 0, 100);
3860Sstevel@tonic-gate 	}
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	if (st.st_mtim.tv_sec == fhp->fh_mtime.tv_sec &&
3890Sstevel@tonic-gate 	    st.st_mtim.tv_nsec == fhp->fh_mtime.tv_nsec &&
3900Sstevel@tonic-gate 	    fhp->fh_table != NULL) {
3910Sstevel@tonic-gate 		htab = &fhp->fh_table[hashop * fhp->fh_size];
392*2830Sdjl 		hash = fhp->fh_hash_func[hashop](args, 1, NULL, 0);
3930Sstevel@tonic-gate 		for (hp = htab[hash % fhp->fh_size].h_first; hp != NULL;
3940Sstevel@tonic-gate 		    hp = hp->h_next) {
3950Sstevel@tonic-gate 			if (hp->h_hash != hash)
3960Sstevel@tonic-gate 				continue;
3970Sstevel@tonic-gate 			line = hp - htab;
398*2830Sdjl 			if ((*check)(args, fhp->fh_line[line].l_start,
399*2830Sdjl 					fhp->fh_line[line].l_len) == 0)
400*2830Sdjl 				continue;
4010Sstevel@tonic-gate 			if ((*args->str2ent)(fhp->fh_line[line].l_start,
4020Sstevel@tonic-gate 			    fhp->fh_line[line].l_len, args->buf.result,
4030Sstevel@tonic-gate 			    args->buf.buffer, args->buf.buflen) ==
4040Sstevel@tonic-gate 			    NSS_STR_PARSE_SUCCESS) {
405*2830Sdjl 				args->returnval = (args->buf.result)?
406*2830Sdjl 					args->buf.result:args->buf.buffer;
407*2830Sdjl 				args->returnlen = fhp->fh_line[line].l_len;
408*2830Sdjl 				mutex_unlock(&fhp->fh_lock);
409*2830Sdjl 				return (NSS_SUCCESS);
4100Sstevel@tonic-gate 			} else {
4110Sstevel@tonic-gate 				args->erange = 1;
4120Sstevel@tonic-gate 			}
4130Sstevel@tonic-gate 		}
4140Sstevel@tonic-gate 		args->returnval = 0;
415*2830Sdjl 		args->returnlen = 0;
4160Sstevel@tonic-gate 		mutex_unlock(&fhp->fh_lock);
4170Sstevel@tonic-gate 		return (NSS_NOTFOUND);
4180Sstevel@tonic-gate 	}
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	_nss_files_hash_destroy(fhp);
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	if (st.st_size > SSIZE_MAX)
4230Sstevel@tonic-gate 		goto unavail;
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	if ((fhp->fh_file_start = malloc((ssize_t)st.st_size + 1)) == NULL)
4260Sstevel@tonic-gate 		goto unavail;
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	if ((fd = open(be->filename, O_RDONLY)) < 0)
4290Sstevel@tonic-gate 		goto unavail;
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	if (read(fd, fhp->fh_file_start, (ssize_t)st.st_size) !=
4320Sstevel@tonic-gate 	    (ssize_t)st.st_size) {
4330Sstevel@tonic-gate 		close(fd);
4340Sstevel@tonic-gate 		goto retry;
4350Sstevel@tonic-gate 	}
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	close(fd);
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	fhp->fh_file_end = fhp->fh_file_start + (off_t)st.st_size;
4400Sstevel@tonic-gate 	*fhp->fh_file_end = '\n';
4410Sstevel@tonic-gate 	fhp->fh_mtime = st.st_mtim;
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	/*
4440Sstevel@tonic-gate 	 * If the file changed since we read it, or if it's less than
4450Sstevel@tonic-gate 	 * 1-2 seconds old, don't trust it; its modification may still
4460Sstevel@tonic-gate 	 * be in progress.  The latter is a heuristic hack to minimize
4470Sstevel@tonic-gate 	 * the likelihood of damage if someone modifies /etc/mumble
4480Sstevel@tonic-gate 	 * directly (as opposed to editing and renaming a temp file).
4490Sstevel@tonic-gate 	 *
4500Sstevel@tonic-gate 	 * Note: the cast to u_int is there in case (1) someone rdated
4510Sstevel@tonic-gate 	 * the system backwards since the last modification of /etc/mumble
4520Sstevel@tonic-gate 	 * or (2) this is a diskless client whose time is badly out of sync
4530Sstevel@tonic-gate 	 * with its server.  The 1-2 second age hack doesn't cover these
4540Sstevel@tonic-gate 	 * cases -- oh well.
4550Sstevel@tonic-gate 	 */
4560Sstevel@tonic-gate 	if (stat64(be->filename, &st) < 0 ||
4570Sstevel@tonic-gate 	    st.st_mtim.tv_sec != fhp->fh_mtime.tv_sec ||
4580Sstevel@tonic-gate 	    st.st_mtim.tv_nsec != fhp->fh_mtime.tv_nsec ||
4590Sstevel@tonic-gate 	    (uint_t)(time(0) - st.st_mtim.tv_sec + 2) < 4) {
4600Sstevel@tonic-gate 		poll(0, 0, 1000);
4610Sstevel@tonic-gate 		goto retry;
4620Sstevel@tonic-gate 	}
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate 	line = 1;
4650Sstevel@tonic-gate 	for (cp = fhp->fh_file_start; cp < fhp->fh_file_end; cp++)
4660Sstevel@tonic-gate 		if (*cp == '\n')
4670Sstevel@tonic-gate 			line++;
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	for (f = 2; f * f <= line; f++) {	/* find next largest prime */
4700Sstevel@tonic-gate 		if (line % f == 0) {
4710Sstevel@tonic-gate 			f = 1;
4720Sstevel@tonic-gate 			line++;
4730Sstevel@tonic-gate 		}
4740Sstevel@tonic-gate 	}
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	fhp->fh_size = line;
4770Sstevel@tonic-gate 	fhp->fh_line = malloc(line * sizeof (files_linetab_t));
4780Sstevel@tonic-gate 	fhp->fh_table = calloc(line * fhp->fh_nhtab, sizeof (files_hashent_t));
4790Sstevel@tonic-gate 	if (fhp->fh_line == NULL || fhp->fh_table == NULL)
4800Sstevel@tonic-gate 		goto unavail;
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	line = 0;
4830Sstevel@tonic-gate 	cp = fhp->fh_file_start;
4840Sstevel@tonic-gate 	while (cp < fhp->fh_file_end) {
4850Sstevel@tonic-gate 		first = cp;
4860Sstevel@tonic-gate 		while (*cp != '\n')
4870Sstevel@tonic-gate 			cp++;
4880Sstevel@tonic-gate 		if (cp > first && *(cp - 1) == '\\') {
4890Sstevel@tonic-gate 			memmove(first + 2, first, cp - first - 1);
4900Sstevel@tonic-gate 			cp = first + 2;
4910Sstevel@tonic-gate 			continue;
4920Sstevel@tonic-gate 		}
4930Sstevel@tonic-gate 		last = cp;
4940Sstevel@tonic-gate 		*cp++ = '\0';
4950Sstevel@tonic-gate 		if (netdb) {
4960Sstevel@tonic-gate 			if ((last = strchr(first, '#')) == 0)
4970Sstevel@tonic-gate 				last = cp - 1;
4980Sstevel@tonic-gate 			*last-- = '\0';		/* nuke '\n' or #comment */
4990Sstevel@tonic-gate 			while (isspace(*first))	/* nuke leading whitespace */
5000Sstevel@tonic-gate 				first++;
5010Sstevel@tonic-gate 			if (*first == '\0')	/* skip content-free lines */
5020Sstevel@tonic-gate 				continue;
5030Sstevel@tonic-gate 			while (isspace(*last))	/* nuke trailing whitespace */
5040Sstevel@tonic-gate 				--last;
5050Sstevel@tonic-gate 			*++last = '\0';
5060Sstevel@tonic-gate 		}
5070Sstevel@tonic-gate 		for (ht = 0; ht < fhp->fh_nhtab; ht++) {
5080Sstevel@tonic-gate 			hp = &fhp->fh_table[ht * fhp->fh_size + line];
509*2830Sdjl 			hp->h_hash = fhp->fh_hash_func[ht](&xargs, 0, first,
510*2830Sdjl 					last - first);
5110Sstevel@tonic-gate 		}
5120Sstevel@tonic-gate 		fhp->fh_line[line].l_start = first;
5130Sstevel@tonic-gate 		fhp->fh_line[line++].l_len = last - first;
5140Sstevel@tonic-gate 	}
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	/*
5170Sstevel@tonic-gate 	 * Populate the hash tables in reverse order so that the hash chains
5180Sstevel@tonic-gate 	 * end up in forward order.  This ensures that hashed lookups find
5190Sstevel@tonic-gate 	 * things in the same order that a linear search of the file would.
5200Sstevel@tonic-gate 	 * This is essential in cases where there could be multiple matches.
5210Sstevel@tonic-gate 	 * For example: until 2.7, root and smtp both had uid 0; but we
5220Sstevel@tonic-gate 	 * certainly wouldn't want getpwuid(0) to return smtp.
5230Sstevel@tonic-gate 	 */
5240Sstevel@tonic-gate 	for (ht = 0; ht < fhp->fh_nhtab; ht++) {
5250Sstevel@tonic-gate 		htab = &fhp->fh_table[ht * fhp->fh_size];
5260Sstevel@tonic-gate 		for (hp = &htab[line - 1]; hp >= htab; hp--) {
5270Sstevel@tonic-gate 			uint_t bucket = hp->h_hash % fhp->fh_size;
5280Sstevel@tonic-gate 			hp->h_next = htab[bucket].h_first;
5290Sstevel@tonic-gate 			htab[bucket].h_first = hp;
5300Sstevel@tonic-gate 		}
5310Sstevel@tonic-gate 	}
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	goto retry;
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate unavail:
5360Sstevel@tonic-gate 	_nss_files_hash_destroy(fhp);
5370Sstevel@tonic-gate 	mutex_unlock(&fhp->fh_lock);
5380Sstevel@tonic-gate 	return (NSS_UNAVAIL);
5390Sstevel@tonic-gate }
5400Sstevel@tonic-gate #endif /* PIC */
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate nss_status_t
5430Sstevel@tonic-gate _nss_files_getent_rigid(be, a)
5440Sstevel@tonic-gate 	files_backend_ptr_t	be;
5450Sstevel@tonic-gate 	void			*a;
5460Sstevel@tonic-gate {
5470Sstevel@tonic-gate 	nss_XbyY_args_t		*args = (nss_XbyY_args_t *)a;
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 	return (_nss_files_XY_all(be, args, 0, 0, 0));
5500Sstevel@tonic-gate }
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate nss_status_t
5530Sstevel@tonic-gate _nss_files_getent_netdb(be, a)
5540Sstevel@tonic-gate 	files_backend_ptr_t	be;
5550Sstevel@tonic-gate 	void			*a;
5560Sstevel@tonic-gate {
5570Sstevel@tonic-gate 	nss_XbyY_args_t		*args = (nss_XbyY_args_t *)a;
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 	return (_nss_files_XY_all(be, args, 1, 0, 0));
5600Sstevel@tonic-gate }
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate /*ARGSUSED*/
5630Sstevel@tonic-gate nss_status_t
5640Sstevel@tonic-gate _nss_files_destr(be, dummy)
5650Sstevel@tonic-gate 	files_backend_ptr_t	be;
5660Sstevel@tonic-gate 	void			*dummy;
5670Sstevel@tonic-gate {
5680Sstevel@tonic-gate 	if (be != 0) {
5690Sstevel@tonic-gate 		if (be->f != 0) {
570*2830Sdjl 			(void) _nss_files_endent(be, 0);
5710Sstevel@tonic-gate 		}
5720Sstevel@tonic-gate 		if (be->hashinfo != NULL) {
573*2830Sdjl 			(void) mutex_lock(&be->hashinfo->fh_lock);
5740Sstevel@tonic-gate 			if (--be->hashinfo->fh_refcnt == 0)
5750Sstevel@tonic-gate 				_nss_files_hash_destroy(be->hashinfo);
576*2830Sdjl 			(void) mutex_unlock(&be->hashinfo->fh_lock);
5770Sstevel@tonic-gate 		}
5780Sstevel@tonic-gate 		free(be);
5790Sstevel@tonic-gate 	}
5800Sstevel@tonic-gate 	return (NSS_SUCCESS);	/* In case anyone is dumb enough to check */
5810Sstevel@tonic-gate }
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate nss_backend_t *
5840Sstevel@tonic-gate _nss_files_constr(ops, n_ops, filename, min_bufsize, fhp)
5850Sstevel@tonic-gate 	files_backend_op_t	ops[];
5860Sstevel@tonic-gate 	int			n_ops;
5870Sstevel@tonic-gate 	const char		*filename;
5880Sstevel@tonic-gate 	int			min_bufsize;
5890Sstevel@tonic-gate 	files_hash_t		*fhp;
5900Sstevel@tonic-gate {
5910Sstevel@tonic-gate 	files_backend_ptr_t	be;
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 	if ((be = (files_backend_ptr_t)malloc(sizeof (*be))) == 0) {
5940Sstevel@tonic-gate 		return (0);
5950Sstevel@tonic-gate 	}
5960Sstevel@tonic-gate 	be->ops		= ops;
5970Sstevel@tonic-gate 	be->n_ops	= n_ops;
5980Sstevel@tonic-gate 	be->filename	= filename;
5990Sstevel@tonic-gate 	be->minbuf	= min_bufsize;
6000Sstevel@tonic-gate 	be->f		= 0;
6010Sstevel@tonic-gate 	be->buf		= 0;
6020Sstevel@tonic-gate 	be->hashinfo	= fhp;
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 	if (fhp != NULL) {
605*2830Sdjl 		(void) mutex_lock(&fhp->fh_lock);
6060Sstevel@tonic-gate 		fhp->fh_refcnt++;
607*2830Sdjl 		(void) mutex_unlock(&fhp->fh_lock);
6080Sstevel@tonic-gate 	}
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	return ((nss_backend_t *)be);
6110Sstevel@tonic-gate }
612*2830Sdjl 
613*2830Sdjl int
614*2830Sdjl _nss_files_check_name_colon(nss_XbyY_args_t *argp, const char *line,
615*2830Sdjl 	int linelen)
616*2830Sdjl {
617*2830Sdjl 	const char	*linep, *limit;
618*2830Sdjl 	const char	*keyp = argp->key.name;
619*2830Sdjl 
620*2830Sdjl 	linep = line;
621*2830Sdjl 	limit = line + linelen;
622*2830Sdjl 	while (*keyp && linep < limit && *keyp == *linep) {
623*2830Sdjl 		keyp++;
624*2830Sdjl 		linep++;
625*2830Sdjl 	}
626*2830Sdjl 	return (linep < limit && *keyp == '\0' && *linep == ':');
627*2830Sdjl }
628*2830Sdjl 
629*2830Sdjl /*
630*2830Sdjl  * This routine is used to parse lines of the form:
631*2830Sdjl  * 	name number aliases
632*2830Sdjl  * It returns 1 if the key in argp matches any one of the
633*2830Sdjl  * names in the line, otherwise 0
634*2830Sdjl  * Used by rpc, networks, protocols
635*2830Sdjl  */
636*2830Sdjl int
637*2830Sdjl _nss_files_check_name_aliases(nss_XbyY_args_t *argp, const char *line,
638*2830Sdjl 	int linelen)
639*2830Sdjl {
640*2830Sdjl 	const char	*limit, *linep, *keyp;
641*2830Sdjl 
642*2830Sdjl 	linep = line;
643*2830Sdjl 	limit = line + linelen;
644*2830Sdjl 	keyp = argp->key.name;
645*2830Sdjl 
646*2830Sdjl 	/* compare name */
647*2830Sdjl 	while (*keyp && linep < limit && !isspace(*linep) && *keyp == *linep) {
648*2830Sdjl 		keyp++;
649*2830Sdjl 		linep++;
650*2830Sdjl 	}
651*2830Sdjl 	if (*keyp == '\0' && linep < limit && isspace(*linep))
652*2830Sdjl 		return (1);
653*2830Sdjl 	/* skip remainder of the name, if any */
654*2830Sdjl 	while (linep < limit && !isspace(*linep))
655*2830Sdjl 		linep++;
656*2830Sdjl 	/* skip the delimiting spaces */
657*2830Sdjl 	while (linep < limit && isspace(*linep))
658*2830Sdjl 		linep++;
659*2830Sdjl 	/* compare with the aliases */
660*2830Sdjl 	while (linep < limit) {
661*2830Sdjl 		/*
662*2830Sdjl 		 * 1st pass: skip number
663*2830Sdjl 		 * Other passes: skip remainder of the alias name, if any
664*2830Sdjl 		 */
665*2830Sdjl 		while (linep < limit && !isspace(*linep))
666*2830Sdjl 			linep++;
667*2830Sdjl 		/* skip the delimiting spaces */
668*2830Sdjl 		while (linep < limit && isspace(*linep))
669*2830Sdjl 			linep++;
670*2830Sdjl 		/* compare with the alias name */
671*2830Sdjl 		keyp = argp->key.name;
672*2830Sdjl 		while (*keyp && linep < limit && !isspace(*linep) &&
673*2830Sdjl 				*keyp == *linep) {
674*2830Sdjl 			keyp++;
675*2830Sdjl 			linep++;
676*2830Sdjl 		}
677*2830Sdjl 		if (*keyp == '\0' && (linep == limit || isspace(*linep)))
678*2830Sdjl 			return (1);
679*2830Sdjl 	}
680*2830Sdjl 	return (0);
681*2830Sdjl }
682