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 5*1914Scasper * Common Development and Distribution License (the "License"). 6*1914Scasper * 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 /* 22*1914Scasper * 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> 440Sstevel@tonic-gate 450Sstevel@tonic-gate /*ARGSUSED*/ 460Sstevel@tonic-gate nss_status_t 470Sstevel@tonic-gate _nss_files_setent(be, dummy) 480Sstevel@tonic-gate files_backend_ptr_t be; 490Sstevel@tonic-gate void *dummy; 500Sstevel@tonic-gate { 510Sstevel@tonic-gate if (be->f == 0) { 520Sstevel@tonic-gate if (be->filename == 0) { 530Sstevel@tonic-gate /* Backend isn't initialized properly? */ 540Sstevel@tonic-gate return (NSS_UNAVAIL); 550Sstevel@tonic-gate } 56*1914Scasper if ((be->f = fopen(be->filename, "rF")) == 0) { 570Sstevel@tonic-gate return (NSS_UNAVAIL); 580Sstevel@tonic-gate } 590Sstevel@tonic-gate } else { 60*1914Scasper rewind(be->f); 610Sstevel@tonic-gate } 620Sstevel@tonic-gate return (NSS_SUCCESS); 630Sstevel@tonic-gate } 640Sstevel@tonic-gate 650Sstevel@tonic-gate /*ARGSUSED*/ 660Sstevel@tonic-gate nss_status_t 670Sstevel@tonic-gate _nss_files_endent(be, dummy) 680Sstevel@tonic-gate files_backend_ptr_t be; 690Sstevel@tonic-gate void *dummy; 700Sstevel@tonic-gate { 710Sstevel@tonic-gate if (be->f != 0) { 72*1914Scasper fclose(be->f); 730Sstevel@tonic-gate be->f = 0; 740Sstevel@tonic-gate } 750Sstevel@tonic-gate if (be->buf != 0) { 760Sstevel@tonic-gate free(be->buf); 770Sstevel@tonic-gate be->buf = 0; 780Sstevel@tonic-gate } 790Sstevel@tonic-gate return (NSS_SUCCESS); 800Sstevel@tonic-gate } 810Sstevel@tonic-gate 820Sstevel@tonic-gate /* 830Sstevel@tonic-gate * This routine reads a line, including the processing of continuation 840Sstevel@tonic-gate * characters. It always leaves (or inserts) \n\0 at the end of the line. 850Sstevel@tonic-gate * It returns the length of the line read, excluding the \n\0. Who's idea 860Sstevel@tonic-gate * was this? 870Sstevel@tonic-gate * Returns -1 on EOF. 880Sstevel@tonic-gate * 890Sstevel@tonic-gate * Note that since each concurrent call to _nss_files_read_line has 900Sstevel@tonic-gate * it's own FILE pointer, we can use getc_unlocked w/o difficulties, 910Sstevel@tonic-gate * a substantial performance win. 920Sstevel@tonic-gate */ 930Sstevel@tonic-gate int 940Sstevel@tonic-gate _nss_files_read_line(f, buffer, buflen) 95*1914Scasper FILE *f; 960Sstevel@tonic-gate char *buffer; 970Sstevel@tonic-gate int buflen; 980Sstevel@tonic-gate { 990Sstevel@tonic-gate int linelen; /* 1st unused slot in buffer */ 1000Sstevel@tonic-gate int c; 1010Sstevel@tonic-gate 1020Sstevel@tonic-gate /*CONSTCOND*/ 1030Sstevel@tonic-gate while (1) { 1040Sstevel@tonic-gate linelen = 0; 1050Sstevel@tonic-gate while (linelen < buflen - 1) { /* "- 1" saves room for \n\0 */ 106*1914Scasper switch (c = getc_unlocked(f)) { 1070Sstevel@tonic-gate case EOF: 1080Sstevel@tonic-gate if (linelen == 0 || 1090Sstevel@tonic-gate buffer[linelen - 1] == '\\') { 1100Sstevel@tonic-gate return (-1); 1110Sstevel@tonic-gate } else { 1120Sstevel@tonic-gate buffer[linelen ] = '\n'; 1130Sstevel@tonic-gate buffer[linelen + 1] = '\0'; 1140Sstevel@tonic-gate return (linelen); 1150Sstevel@tonic-gate } 1160Sstevel@tonic-gate case '\n': 1170Sstevel@tonic-gate if (linelen > 0 && 1180Sstevel@tonic-gate buffer[linelen - 1] == '\\') { 1190Sstevel@tonic-gate --linelen; /* remove the '\\' */ 1200Sstevel@tonic-gate } else { 1210Sstevel@tonic-gate buffer[linelen ] = '\n'; 1220Sstevel@tonic-gate buffer[linelen + 1] = '\0'; 1230Sstevel@tonic-gate return (linelen); 1240Sstevel@tonic-gate } 1250Sstevel@tonic-gate break; 1260Sstevel@tonic-gate default: 1270Sstevel@tonic-gate buffer[linelen++] = c; 1280Sstevel@tonic-gate } 1290Sstevel@tonic-gate } 1300Sstevel@tonic-gate /* Buffer overflow -- eat rest of line and loop again */ 1310Sstevel@tonic-gate /* ===> Should syslog() */ 1320Sstevel@tonic-gate do { 133*1914Scasper c = getc_unlocked(f); 1340Sstevel@tonic-gate if (c == EOF) { 1350Sstevel@tonic-gate return (-1); 1360Sstevel@tonic-gate } 1370Sstevel@tonic-gate } while (c != '\n'); 1380Sstevel@tonic-gate } 1390Sstevel@tonic-gate /*NOTREACHED*/ 1400Sstevel@tonic-gate } 1410Sstevel@tonic-gate 1420Sstevel@tonic-gate /* 1430Sstevel@tonic-gate * used only for getgroupbymem() now. 1440Sstevel@tonic-gate */ 1450Sstevel@tonic-gate nss_status_t 1460Sstevel@tonic-gate _nss_files_do_all(be, args, filter, func) 1470Sstevel@tonic-gate files_backend_ptr_t be; 1480Sstevel@tonic-gate void *args; 1490Sstevel@tonic-gate const char *filter; 1500Sstevel@tonic-gate files_do_all_func_t func; 1510Sstevel@tonic-gate { 1520Sstevel@tonic-gate char *buffer; 1530Sstevel@tonic-gate int buflen; 1540Sstevel@tonic-gate nss_status_t res; 1550Sstevel@tonic-gate 1560Sstevel@tonic-gate if (be->buf == 0 && 1570Sstevel@tonic-gate (be->buf = malloc(be->minbuf)) == 0) { 1580Sstevel@tonic-gate return (NSS_UNAVAIL); 1590Sstevel@tonic-gate } 1600Sstevel@tonic-gate buffer = be->buf; 1610Sstevel@tonic-gate buflen = be->minbuf; 1620Sstevel@tonic-gate 1630Sstevel@tonic-gate if ((res = _nss_files_setent(be, 0)) != NSS_SUCCESS) { 1640Sstevel@tonic-gate return (res); 1650Sstevel@tonic-gate } 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate res = NSS_NOTFOUND; 1680Sstevel@tonic-gate 1690Sstevel@tonic-gate do { 1700Sstevel@tonic-gate int linelen; 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate if ((linelen = _nss_files_read_line(be->f, buffer, 1730Sstevel@tonic-gate buflen)) < 0) { 1740Sstevel@tonic-gate /* End of file */ 1750Sstevel@tonic-gate break; 1760Sstevel@tonic-gate } 1770Sstevel@tonic-gate if (filter != 0 && strstr(buffer, filter) == 0) { 1780Sstevel@tonic-gate /* 1790Sstevel@tonic-gate * Optimization: if the entry doesn't contain the 1800Sstevel@tonic-gate * filter string then it can't be the entry we want, 1810Sstevel@tonic-gate * so don't bother looking more closely at it. 1820Sstevel@tonic-gate */ 1830Sstevel@tonic-gate continue; 1840Sstevel@tonic-gate } 1850Sstevel@tonic-gate res = (*func)(buffer, linelen, args); 1860Sstevel@tonic-gate 1870Sstevel@tonic-gate } while (res == NSS_NOTFOUND); 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate _nss_files_endent(be, 0); 1900Sstevel@tonic-gate return (res); 1910Sstevel@tonic-gate } 1920Sstevel@tonic-gate 1930Sstevel@tonic-gate /* 1940Sstevel@tonic-gate * Could implement this as an iterator function on top of _nss_files_do_all(), 1950Sstevel@tonic-gate * but the shared code is small enough that it'd be pretty silly. 1960Sstevel@tonic-gate */ 1970Sstevel@tonic-gate nss_status_t 1980Sstevel@tonic-gate _nss_files_XY_all(be, args, netdb, filter, check) 1990Sstevel@tonic-gate files_backend_ptr_t be; 2000Sstevel@tonic-gate nss_XbyY_args_t *args; 2010Sstevel@tonic-gate int netdb; /* whether it uses netdb */ 2020Sstevel@tonic-gate /* format or not */ 2030Sstevel@tonic-gate const char *filter; /* advisory, to speed up */ 2040Sstevel@tonic-gate /* string search */ 2050Sstevel@tonic-gate files_XY_check_func check; /* NULL means one-shot, for getXXent */ 2060Sstevel@tonic-gate { 2070Sstevel@tonic-gate nss_status_t res; 2080Sstevel@tonic-gate int parsestat; 2090Sstevel@tonic-gate int (*func)(); 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate if (be->buf == 0 && 2120Sstevel@tonic-gate (be->buf = malloc(be->minbuf)) == 0) { 2130Sstevel@tonic-gate return (NSS_UNAVAIL); /* really panic, malloc failed */ 2140Sstevel@tonic-gate } 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate if (check != 0 || be->f == 0) { 2170Sstevel@tonic-gate if ((res = _nss_files_setent(be, 0)) != NSS_SUCCESS) { 2180Sstevel@tonic-gate return (res); 2190Sstevel@tonic-gate } 2200Sstevel@tonic-gate } 2210Sstevel@tonic-gate 2220Sstevel@tonic-gate res = NSS_NOTFOUND; 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate /*CONSTCOND*/ 2250Sstevel@tonic-gate while (1) { 2260Sstevel@tonic-gate char *instr = be->buf; 2270Sstevel@tonic-gate int linelen; 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate if ((linelen = _nss_files_read_line(be->f, instr, 2300Sstevel@tonic-gate be->minbuf)) < 0) { 2310Sstevel@tonic-gate /* End of file */ 2320Sstevel@tonic-gate args->returnval = 0; 2330Sstevel@tonic-gate args->erange = 0; 2340Sstevel@tonic-gate break; 2350Sstevel@tonic-gate } 2360Sstevel@tonic-gate if (filter != 0 && strstr(instr, filter) == 0) { 2370Sstevel@tonic-gate /* 2380Sstevel@tonic-gate * Optimization: if the entry doesn't contain the 2390Sstevel@tonic-gate * filter string then it can't be the entry we want, 2400Sstevel@tonic-gate * so don't bother looking more closely at it. 2410Sstevel@tonic-gate */ 2420Sstevel@tonic-gate continue; 2430Sstevel@tonic-gate } 2440Sstevel@tonic-gate if (netdb) { 2450Sstevel@tonic-gate char *first; 2460Sstevel@tonic-gate char *last; 2470Sstevel@tonic-gate 2480Sstevel@tonic-gate if ((last = strchr(instr, '#')) == 0) { 2490Sstevel@tonic-gate last = instr + linelen; 2500Sstevel@tonic-gate } 2510Sstevel@tonic-gate *last-- = '\0'; /* Nuke '\n' or #comment */ 2520Sstevel@tonic-gate 2530Sstevel@tonic-gate /* 2540Sstevel@tonic-gate * Skip leading whitespace. Normally there isn't 2550Sstevel@tonic-gate * any, so it's not worth calling strspn(). 2560Sstevel@tonic-gate */ 2570Sstevel@tonic-gate for (first = instr; isspace(*first); first++) { 2580Sstevel@tonic-gate ; 2590Sstevel@tonic-gate } 2600Sstevel@tonic-gate if (*first == '\0') { 2610Sstevel@tonic-gate continue; 2620Sstevel@tonic-gate } 2630Sstevel@tonic-gate /* 2640Sstevel@tonic-gate * Found something non-blank on the line. Skip back 2650Sstevel@tonic-gate * over any trailing whitespace; since we know 2660Sstevel@tonic-gate * there's non-whitespace earlier in the line, 2670Sstevel@tonic-gate * checking for termination is easy. 2680Sstevel@tonic-gate */ 2690Sstevel@tonic-gate while (isspace(*last)) { 2700Sstevel@tonic-gate --last; 2710Sstevel@tonic-gate } 2720Sstevel@tonic-gate 2730Sstevel@tonic-gate linelen = last - first + 1; 2740Sstevel@tonic-gate if (first != instr) { 2750Sstevel@tonic-gate instr = first; 2760Sstevel@tonic-gate } 2770Sstevel@tonic-gate } 2780Sstevel@tonic-gate 2790Sstevel@tonic-gate args->returnval = 0; 2800Sstevel@tonic-gate 2810Sstevel@tonic-gate func = args->str2ent; 2820Sstevel@tonic-gate parsestat = (*func)(instr, linelen, args->buf.result, 2830Sstevel@tonic-gate args->buf.buffer, args->buf.buflen); 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate if (parsestat == NSS_STR_PARSE_SUCCESS) { 2860Sstevel@tonic-gate args->returnval = args->buf.result; 2870Sstevel@tonic-gate if (check == 0 || (*check)(args)) { 2880Sstevel@tonic-gate res = NSS_SUCCESS; 2890Sstevel@tonic-gate break; 2900Sstevel@tonic-gate } 2910Sstevel@tonic-gate } else if (parsestat == NSS_STR_PARSE_ERANGE) { 2920Sstevel@tonic-gate args->erange = 1; 2930Sstevel@tonic-gate break; 2940Sstevel@tonic-gate } /* else if (parsestat == NSS_STR_PARSE_PARSE) don't care ! */ 2950Sstevel@tonic-gate } 2960Sstevel@tonic-gate 2970Sstevel@tonic-gate /* 2980Sstevel@tonic-gate * stayopen is set to 0 by default in order to close the opened 2990Sstevel@tonic-gate * file. Some applications may break if it is set to 1. 3000Sstevel@tonic-gate */ 3010Sstevel@tonic-gate if (check != 0 && !args->stayopen) { 3020Sstevel@tonic-gate (void) _nss_files_endent(be, 0); 3030Sstevel@tonic-gate } 3040Sstevel@tonic-gate 3050Sstevel@tonic-gate return (res); 3060Sstevel@tonic-gate } 3070Sstevel@tonic-gate 3080Sstevel@tonic-gate /* 3090Sstevel@tonic-gate * File hashing support. Critical for sites with large (e.g. 1000+ lines) 3100Sstevel@tonic-gate * /etc/passwd or /etc/group files. Currently only used by getpw*() and 3110Sstevel@tonic-gate * getgr*() routines, but any files backend can use this stuff. 3120Sstevel@tonic-gate */ 3130Sstevel@tonic-gate static void 3140Sstevel@tonic-gate _nss_files_hash_destroy(files_hash_t *fhp) 3150Sstevel@tonic-gate { 3160Sstevel@tonic-gate free(fhp->fh_table); 3170Sstevel@tonic-gate fhp->fh_table = NULL; 3180Sstevel@tonic-gate free(fhp->fh_line); 3190Sstevel@tonic-gate fhp->fh_line = NULL; 3200Sstevel@tonic-gate free(fhp->fh_file_start); 3210Sstevel@tonic-gate fhp->fh_file_start = NULL; 3220Sstevel@tonic-gate } 3230Sstevel@tonic-gate #ifdef PIC 3240Sstevel@tonic-gate /* 3250Sstevel@tonic-gate * It turns out the hashing stuff really needs to be disabled for processes 3260Sstevel@tonic-gate * other than the nscd; the consumption of swap space and memory is otherwise 3270Sstevel@tonic-gate * unacceptable when the nscd is killed w/ a large passwd file (4M) active. 3280Sstevel@tonic-gate * See 4031930 for details. 3290Sstevel@tonic-gate * So we just use this psuedo function to enable the hashing feature. Since 3300Sstevel@tonic-gate * this function name is private, we just create a function w/ the name 3310Sstevel@tonic-gate * __nss_use_files_hash in the nscd itself and everyone else uses the old 3320Sstevel@tonic-gate * interface. 3330Sstevel@tonic-gate * We also disable hashing for .a executables to avoid problems with large 3340Sstevel@tonic-gate * files.... 3350Sstevel@tonic-gate */ 3360Sstevel@tonic-gate 3370Sstevel@tonic-gate #pragma weak __nss_use_files_hash 3380Sstevel@tonic-gate 3390Sstevel@tonic-gate extern void __nss_use_files_hash(void); 3400Sstevel@tonic-gate #endif /* pic */ 3410Sstevel@tonic-gate 3420Sstevel@tonic-gate nss_status_t 3430Sstevel@tonic-gate _nss_files_XY_hash(files_backend_ptr_t be, nss_XbyY_args_t *args, 3440Sstevel@tonic-gate int netdb, files_hash_t *fhp, int hashop, files_XY_check_func check) 3450Sstevel@tonic-gate { 3460Sstevel@tonic-gate int fd, retries, ht; 3470Sstevel@tonic-gate uint_t hash, line, f; 3480Sstevel@tonic-gate files_hashent_t *hp, *htab; 3490Sstevel@tonic-gate char *cp, *first, *last; 3500Sstevel@tonic-gate nss_XbyY_args_t xargs; 3510Sstevel@tonic-gate struct stat64 st; 3520Sstevel@tonic-gate 3530Sstevel@tonic-gate #ifndef PIC 3540Sstevel@tonic-gate return (_nss_files_XY_all(be, args, netdb, 0, check)); 3550Sstevel@tonic-gate } 3560Sstevel@tonic-gate #else 3570Sstevel@tonic-gate if (__nss_use_files_hash == 0) 3580Sstevel@tonic-gate return (_nss_files_XY_all(be, args, netdb, 0, check)); 3590Sstevel@tonic-gate 3600Sstevel@tonic-gate mutex_lock(&fhp->fh_lock); 3610Sstevel@tonic-gate retry: 3620Sstevel@tonic-gate retries = 100; 3630Sstevel@tonic-gate while (stat64(be->filename, &st) < 0) { 3640Sstevel@tonic-gate /* 3650Sstevel@tonic-gate * On a healthy system this can't happen except during brief 3660Sstevel@tonic-gate * periods when the file is being modified/renamed. Keep 3670Sstevel@tonic-gate * trying until things settle down, but eventually give up. 3680Sstevel@tonic-gate */ 3690Sstevel@tonic-gate if (--retries == 0) 3700Sstevel@tonic-gate goto unavail; 3710Sstevel@tonic-gate poll(0, 0, 100); 3720Sstevel@tonic-gate } 3730Sstevel@tonic-gate 3740Sstevel@tonic-gate if (st.st_mtim.tv_sec == fhp->fh_mtime.tv_sec && 3750Sstevel@tonic-gate st.st_mtim.tv_nsec == fhp->fh_mtime.tv_nsec && 3760Sstevel@tonic-gate fhp->fh_table != NULL) { 3770Sstevel@tonic-gate htab = &fhp->fh_table[hashop * fhp->fh_size]; 3780Sstevel@tonic-gate hash = fhp->fh_hash_func[hashop](args, 1); 3790Sstevel@tonic-gate for (hp = htab[hash % fhp->fh_size].h_first; hp != NULL; 3800Sstevel@tonic-gate hp = hp->h_next) { 3810Sstevel@tonic-gate if (hp->h_hash != hash) 3820Sstevel@tonic-gate continue; 3830Sstevel@tonic-gate line = hp - htab; 3840Sstevel@tonic-gate if ((*args->str2ent)(fhp->fh_line[line].l_start, 3850Sstevel@tonic-gate fhp->fh_line[line].l_len, args->buf.result, 3860Sstevel@tonic-gate args->buf.buffer, args->buf.buflen) == 3870Sstevel@tonic-gate NSS_STR_PARSE_SUCCESS) { 3880Sstevel@tonic-gate args->returnval = args->buf.result; 3890Sstevel@tonic-gate if ((*check)(args)) { 3900Sstevel@tonic-gate mutex_unlock(&fhp->fh_lock); 3910Sstevel@tonic-gate return (NSS_SUCCESS); 3920Sstevel@tonic-gate } 3930Sstevel@tonic-gate } else { 3940Sstevel@tonic-gate args->erange = 1; 3950Sstevel@tonic-gate } 3960Sstevel@tonic-gate } 3970Sstevel@tonic-gate args->returnval = 0; 3980Sstevel@tonic-gate mutex_unlock(&fhp->fh_lock); 3990Sstevel@tonic-gate return (NSS_NOTFOUND); 4000Sstevel@tonic-gate } 4010Sstevel@tonic-gate 4020Sstevel@tonic-gate _nss_files_hash_destroy(fhp); 4030Sstevel@tonic-gate 4040Sstevel@tonic-gate if (st.st_size > SSIZE_MAX) 4050Sstevel@tonic-gate goto unavail; 4060Sstevel@tonic-gate 4070Sstevel@tonic-gate if ((fhp->fh_file_start = malloc((ssize_t)st.st_size + 1)) == NULL) 4080Sstevel@tonic-gate goto unavail; 4090Sstevel@tonic-gate 4100Sstevel@tonic-gate if ((fd = open(be->filename, O_RDONLY)) < 0) 4110Sstevel@tonic-gate goto unavail; 4120Sstevel@tonic-gate 4130Sstevel@tonic-gate if (read(fd, fhp->fh_file_start, (ssize_t)st.st_size) != 4140Sstevel@tonic-gate (ssize_t)st.st_size) { 4150Sstevel@tonic-gate close(fd); 4160Sstevel@tonic-gate goto retry; 4170Sstevel@tonic-gate } 4180Sstevel@tonic-gate 4190Sstevel@tonic-gate close(fd); 4200Sstevel@tonic-gate 4210Sstevel@tonic-gate fhp->fh_file_end = fhp->fh_file_start + (off_t)st.st_size; 4220Sstevel@tonic-gate *fhp->fh_file_end = '\n'; 4230Sstevel@tonic-gate fhp->fh_mtime = st.st_mtim; 4240Sstevel@tonic-gate 4250Sstevel@tonic-gate /* 4260Sstevel@tonic-gate * If the file changed since we read it, or if it's less than 4270Sstevel@tonic-gate * 1-2 seconds old, don't trust it; its modification may still 4280Sstevel@tonic-gate * be in progress. The latter is a heuristic hack to minimize 4290Sstevel@tonic-gate * the likelihood of damage if someone modifies /etc/mumble 4300Sstevel@tonic-gate * directly (as opposed to editing and renaming a temp file). 4310Sstevel@tonic-gate * 4320Sstevel@tonic-gate * Note: the cast to u_int is there in case (1) someone rdated 4330Sstevel@tonic-gate * the system backwards since the last modification of /etc/mumble 4340Sstevel@tonic-gate * or (2) this is a diskless client whose time is badly out of sync 4350Sstevel@tonic-gate * with its server. The 1-2 second age hack doesn't cover these 4360Sstevel@tonic-gate * cases -- oh well. 4370Sstevel@tonic-gate */ 4380Sstevel@tonic-gate if (stat64(be->filename, &st) < 0 || 4390Sstevel@tonic-gate st.st_mtim.tv_sec != fhp->fh_mtime.tv_sec || 4400Sstevel@tonic-gate st.st_mtim.tv_nsec != fhp->fh_mtime.tv_nsec || 4410Sstevel@tonic-gate (uint_t)(time(0) - st.st_mtim.tv_sec + 2) < 4) { 4420Sstevel@tonic-gate poll(0, 0, 1000); 4430Sstevel@tonic-gate goto retry; 4440Sstevel@tonic-gate } 4450Sstevel@tonic-gate 4460Sstevel@tonic-gate line = 1; 4470Sstevel@tonic-gate for (cp = fhp->fh_file_start; cp < fhp->fh_file_end; cp++) 4480Sstevel@tonic-gate if (*cp == '\n') 4490Sstevel@tonic-gate line++; 4500Sstevel@tonic-gate 4510Sstevel@tonic-gate for (f = 2; f * f <= line; f++) { /* find next largest prime */ 4520Sstevel@tonic-gate if (line % f == 0) { 4530Sstevel@tonic-gate f = 1; 4540Sstevel@tonic-gate line++; 4550Sstevel@tonic-gate } 4560Sstevel@tonic-gate } 4570Sstevel@tonic-gate 4580Sstevel@tonic-gate fhp->fh_size = line; 4590Sstevel@tonic-gate fhp->fh_line = malloc(line * sizeof (files_linetab_t)); 4600Sstevel@tonic-gate fhp->fh_table = calloc(line * fhp->fh_nhtab, sizeof (files_hashent_t)); 4610Sstevel@tonic-gate if (fhp->fh_line == NULL || fhp->fh_table == NULL) 4620Sstevel@tonic-gate goto unavail; 4630Sstevel@tonic-gate 4640Sstevel@tonic-gate xargs = *args; 4650Sstevel@tonic-gate xargs.buf.result = malloc(fhp->fh_resultsize + fhp->fh_bufsize); 4660Sstevel@tonic-gate if (xargs.buf.result == NULL) 4670Sstevel@tonic-gate goto unavail; 4680Sstevel@tonic-gate xargs.buf.buffer = (char *)xargs.buf.result + fhp->fh_resultsize; 4690Sstevel@tonic-gate xargs.buf.buflen = fhp->fh_bufsize; 4700Sstevel@tonic-gate xargs.returnval = xargs.buf.result; 4710Sstevel@tonic-gate 4720Sstevel@tonic-gate line = 0; 4730Sstevel@tonic-gate cp = fhp->fh_file_start; 4740Sstevel@tonic-gate while (cp < fhp->fh_file_end) { 4750Sstevel@tonic-gate first = cp; 4760Sstevel@tonic-gate while (*cp != '\n') 4770Sstevel@tonic-gate cp++; 4780Sstevel@tonic-gate if (cp > first && *(cp - 1) == '\\') { 4790Sstevel@tonic-gate memmove(first + 2, first, cp - first - 1); 4800Sstevel@tonic-gate cp = first + 2; 4810Sstevel@tonic-gate continue; 4820Sstevel@tonic-gate } 4830Sstevel@tonic-gate last = cp; 4840Sstevel@tonic-gate *cp++ = '\0'; 4850Sstevel@tonic-gate if (netdb) { 4860Sstevel@tonic-gate if ((last = strchr(first, '#')) == 0) 4870Sstevel@tonic-gate last = cp - 1; 4880Sstevel@tonic-gate *last-- = '\0'; /* nuke '\n' or #comment */ 4890Sstevel@tonic-gate while (isspace(*first)) /* nuke leading whitespace */ 4900Sstevel@tonic-gate first++; 4910Sstevel@tonic-gate if (*first == '\0') /* skip content-free lines */ 4920Sstevel@tonic-gate continue; 4930Sstevel@tonic-gate while (isspace(*last)) /* nuke trailing whitespace */ 4940Sstevel@tonic-gate --last; 4950Sstevel@tonic-gate *++last = '\0'; 4960Sstevel@tonic-gate } 4970Sstevel@tonic-gate if ((*xargs.str2ent)(first, last - first, 4980Sstevel@tonic-gate xargs.buf.result, xargs.buf.buffer, xargs.buf.buflen) != 4990Sstevel@tonic-gate NSS_STR_PARSE_SUCCESS) 5000Sstevel@tonic-gate continue; 5010Sstevel@tonic-gate for (ht = 0; ht < fhp->fh_nhtab; ht++) { 5020Sstevel@tonic-gate hp = &fhp->fh_table[ht * fhp->fh_size + line]; 5030Sstevel@tonic-gate hp->h_hash = fhp->fh_hash_func[ht](&xargs, 0); 5040Sstevel@tonic-gate } 5050Sstevel@tonic-gate fhp->fh_line[line].l_start = first; 5060Sstevel@tonic-gate fhp->fh_line[line++].l_len = last - first; 5070Sstevel@tonic-gate } 5080Sstevel@tonic-gate free(xargs.buf.result); 5090Sstevel@tonic-gate 5100Sstevel@tonic-gate /* 5110Sstevel@tonic-gate * Populate the hash tables in reverse order so that the hash chains 5120Sstevel@tonic-gate * end up in forward order. This ensures that hashed lookups find 5130Sstevel@tonic-gate * things in the same order that a linear search of the file would. 5140Sstevel@tonic-gate * This is essential in cases where there could be multiple matches. 5150Sstevel@tonic-gate * For example: until 2.7, root and smtp both had uid 0; but we 5160Sstevel@tonic-gate * certainly wouldn't want getpwuid(0) to return smtp. 5170Sstevel@tonic-gate */ 5180Sstevel@tonic-gate for (ht = 0; ht < fhp->fh_nhtab; ht++) { 5190Sstevel@tonic-gate htab = &fhp->fh_table[ht * fhp->fh_size]; 5200Sstevel@tonic-gate for (hp = &htab[line - 1]; hp >= htab; hp--) { 5210Sstevel@tonic-gate uint_t bucket = hp->h_hash % fhp->fh_size; 5220Sstevel@tonic-gate hp->h_next = htab[bucket].h_first; 5230Sstevel@tonic-gate htab[bucket].h_first = hp; 5240Sstevel@tonic-gate } 5250Sstevel@tonic-gate } 5260Sstevel@tonic-gate 5270Sstevel@tonic-gate goto retry; 5280Sstevel@tonic-gate 5290Sstevel@tonic-gate unavail: 5300Sstevel@tonic-gate _nss_files_hash_destroy(fhp); 5310Sstevel@tonic-gate mutex_unlock(&fhp->fh_lock); 5320Sstevel@tonic-gate return (NSS_UNAVAIL); 5330Sstevel@tonic-gate } 5340Sstevel@tonic-gate #endif /* PIC */ 5350Sstevel@tonic-gate 5360Sstevel@tonic-gate nss_status_t 5370Sstevel@tonic-gate _nss_files_getent_rigid(be, a) 5380Sstevel@tonic-gate files_backend_ptr_t be; 5390Sstevel@tonic-gate void *a; 5400Sstevel@tonic-gate { 5410Sstevel@tonic-gate nss_XbyY_args_t *args = (nss_XbyY_args_t *)a; 5420Sstevel@tonic-gate 5430Sstevel@tonic-gate return (_nss_files_XY_all(be, args, 0, 0, 0)); 5440Sstevel@tonic-gate } 5450Sstevel@tonic-gate 5460Sstevel@tonic-gate nss_status_t 5470Sstevel@tonic-gate _nss_files_getent_netdb(be, a) 5480Sstevel@tonic-gate files_backend_ptr_t be; 5490Sstevel@tonic-gate void *a; 5500Sstevel@tonic-gate { 5510Sstevel@tonic-gate nss_XbyY_args_t *args = (nss_XbyY_args_t *)a; 5520Sstevel@tonic-gate 5530Sstevel@tonic-gate return (_nss_files_XY_all(be, args, 1, 0, 0)); 5540Sstevel@tonic-gate } 5550Sstevel@tonic-gate 5560Sstevel@tonic-gate /*ARGSUSED*/ 5570Sstevel@tonic-gate nss_status_t 5580Sstevel@tonic-gate _nss_files_destr(be, dummy) 5590Sstevel@tonic-gate files_backend_ptr_t be; 5600Sstevel@tonic-gate void *dummy; 5610Sstevel@tonic-gate { 5620Sstevel@tonic-gate if (be != 0) { 5630Sstevel@tonic-gate if (be->f != 0) { 5640Sstevel@tonic-gate _nss_files_endent(be, 0); 5650Sstevel@tonic-gate } 5660Sstevel@tonic-gate if (be->hashinfo != NULL) { 5670Sstevel@tonic-gate mutex_lock(&be->hashinfo->fh_lock); 5680Sstevel@tonic-gate if (--be->hashinfo->fh_refcnt == 0) 5690Sstevel@tonic-gate _nss_files_hash_destroy(be->hashinfo); 5700Sstevel@tonic-gate mutex_unlock(&be->hashinfo->fh_lock); 5710Sstevel@tonic-gate } 5720Sstevel@tonic-gate free(be); 5730Sstevel@tonic-gate } 5740Sstevel@tonic-gate return (NSS_SUCCESS); /* In case anyone is dumb enough to check */ 5750Sstevel@tonic-gate } 5760Sstevel@tonic-gate 5770Sstevel@tonic-gate nss_backend_t * 5780Sstevel@tonic-gate _nss_files_constr(ops, n_ops, filename, min_bufsize, fhp) 5790Sstevel@tonic-gate files_backend_op_t ops[]; 5800Sstevel@tonic-gate int n_ops; 5810Sstevel@tonic-gate const char *filename; 5820Sstevel@tonic-gate int min_bufsize; 5830Sstevel@tonic-gate files_hash_t *fhp; 5840Sstevel@tonic-gate { 5850Sstevel@tonic-gate files_backend_ptr_t be; 5860Sstevel@tonic-gate 5870Sstevel@tonic-gate if ((be = (files_backend_ptr_t)malloc(sizeof (*be))) == 0) { 5880Sstevel@tonic-gate return (0); 5890Sstevel@tonic-gate } 5900Sstevel@tonic-gate be->ops = ops; 5910Sstevel@tonic-gate be->n_ops = n_ops; 5920Sstevel@tonic-gate be->filename = filename; 5930Sstevel@tonic-gate be->minbuf = min_bufsize; 5940Sstevel@tonic-gate be->f = 0; 5950Sstevel@tonic-gate be->buf = 0; 5960Sstevel@tonic-gate be->hashinfo = fhp; 5970Sstevel@tonic-gate 5980Sstevel@tonic-gate if (fhp != NULL) { 5990Sstevel@tonic-gate mutex_lock(&fhp->fh_lock); 6000Sstevel@tonic-gate fhp->fh_refcnt++; 6010Sstevel@tonic-gate mutex_unlock(&fhp->fh_lock); 6020Sstevel@tonic-gate } 6030Sstevel@tonic-gate 6040Sstevel@tonic-gate return ((nss_backend_t *)be); 6050Sstevel@tonic-gate } 606