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 "user" backends.
260Sstevel@tonic-gate * Much of this was taken directly from the files_common.c source.
270Sstevel@tonic-gate */
280Sstevel@tonic-gate
290Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
300Sstevel@tonic-gate
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate * An implementation that used mmap() sensibly would be a wonderful thing,
330Sstevel@tonic-gate * but this here is just yer standard fgets() thang.
340Sstevel@tonic-gate */
350Sstevel@tonic-gate
361914Scasper #include "user_common.h"
370Sstevel@tonic-gate #include <stdio.h>
380Sstevel@tonic-gate #include <stdlib.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 #include <string.h>
450Sstevel@tonic-gate
46*2830Sdjl /*ARGSUSED*/
470Sstevel@tonic-gate nss_status_t
_nss_user_setent(be,dummy)480Sstevel@tonic-gate _nss_user_setent(be, dummy)
490Sstevel@tonic-gate user_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
66*2830Sdjl /*ARGSUSED*/
670Sstevel@tonic-gate nss_status_t
_nss_user_endent(be,dummy)680Sstevel@tonic-gate _nss_user_endent(be, dummy)
690Sstevel@tonic-gate user_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_user_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
_nss_user_read_line(f,buffer,buflen)950Sstevel@tonic-gate _nss_user_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
103*2830Sdjl /*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 /*
1450Sstevel@tonic-gate * Could implement this as an iterator function on top of _nss_user_do_all(),
1460Sstevel@tonic-gate * but the shared code is small enough that it'd be pretty silly.
1470Sstevel@tonic-gate */
1480Sstevel@tonic-gate nss_status_t
_nss_user_XY_all(be,args,netdb,filter,check)1490Sstevel@tonic-gate _nss_user_XY_all(be, args, netdb, filter, check)
1500Sstevel@tonic-gate user_backend_ptr_t be;
1510Sstevel@tonic-gate nss_XbyY_args_t *args;
1520Sstevel@tonic-gate int netdb; /* whether it uses netdb */
1530Sstevel@tonic-gate /* format or not */
1540Sstevel@tonic-gate const char *filter; /* advisory, to speed up */
1550Sstevel@tonic-gate /* string search */
1560Sstevel@tonic-gate user_XY_check_func check; /* NULL means one-shot, for getXXent */
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate nss_status_t res;
1590Sstevel@tonic-gate int parsestat;
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate if (be->buf == 0 &&
1620Sstevel@tonic-gate (be->buf = malloc(be->minbuf)) == 0) {
1630Sstevel@tonic-gate return (NSS_UNAVAIL); /* really panic, malloc failed */
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate if (check != 0 || be->f == 0) {
1670Sstevel@tonic-gate if ((res = _nss_user_setent(be, 0)) != NSS_SUCCESS) {
1680Sstevel@tonic-gate return (res);
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate
1720Sstevel@tonic-gate res = NSS_NOTFOUND;
1730Sstevel@tonic-gate
174*2830Sdjl /*CONSTCOND*/
1750Sstevel@tonic-gate while (1) {
1760Sstevel@tonic-gate char *instr = be->buf;
1770Sstevel@tonic-gate int linelen;
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate if ((linelen = _nss_user_read_line(be->f, instr,
1800Sstevel@tonic-gate be->minbuf)) < 0) {
1810Sstevel@tonic-gate /* End of file */
1820Sstevel@tonic-gate args->returnval = 0;
1830Sstevel@tonic-gate args->erange = 0;
1840Sstevel@tonic-gate break;
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate if (filter != 0 && strstr(instr, filter) == 0) {
1870Sstevel@tonic-gate /*
1880Sstevel@tonic-gate * Optimization: if the entry doesn't contain the
1890Sstevel@tonic-gate * filter string then it can't be the entry we want,
1900Sstevel@tonic-gate * so don't bother looking more closely at it.
1910Sstevel@tonic-gate */
1920Sstevel@tonic-gate continue;
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate if (netdb) {
1950Sstevel@tonic-gate char *first;
1960Sstevel@tonic-gate char *last;
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate if ((last = strchr(instr, '#')) == 0) {
1990Sstevel@tonic-gate last = instr + linelen;
2000Sstevel@tonic-gate }
2010Sstevel@tonic-gate *last-- = '\0'; /* Nuke '\n' or #comment */
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate /*
2040Sstevel@tonic-gate * Skip leading whitespace. Normally there isn't
2050Sstevel@tonic-gate * any, so it's not worth calling strspn().
2060Sstevel@tonic-gate */
2070Sstevel@tonic-gate for (first = instr; isspace(*first); first++) {
2080Sstevel@tonic-gate ;
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate if (*first == '\0') {
2110Sstevel@tonic-gate continue;
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate /*
2140Sstevel@tonic-gate * Found something non-blank on the line. Skip back
2150Sstevel@tonic-gate * over any trailing whitespace; since we know
2160Sstevel@tonic-gate * there's non-whitespace earlier in the line,
2170Sstevel@tonic-gate * checking for termination is easy.
2180Sstevel@tonic-gate */
2190Sstevel@tonic-gate while (isspace(*last)) {
2200Sstevel@tonic-gate --last;
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate linelen = last - first + 1;
2240Sstevel@tonic-gate if (first != instr) {
2250Sstevel@tonic-gate instr = first;
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate args->returnval = 0;
2300Sstevel@tonic-gate parsestat = (*args->str2ent)(instr, linelen, args->buf.result,
2310Sstevel@tonic-gate args->buf.buffer, args->buf.buflen);
2320Sstevel@tonic-gate
2330Sstevel@tonic-gate if (parsestat == NSS_STR_PARSE_SUCCESS) {
2340Sstevel@tonic-gate args->returnval = args->buf.result;
2350Sstevel@tonic-gate if (check == 0 || (*check)(args)) {
2360Sstevel@tonic-gate res = NSS_SUCCESS;
2370Sstevel@tonic-gate break;
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate } else if (parsestat == NSS_STR_PARSE_ERANGE) {
2400Sstevel@tonic-gate args->erange = 1; /* should we just skip this */
2410Sstevel@tonic-gate /* one long line ?? */
2420Sstevel@tonic-gate } /* else if (parsestat == NSS_STR_PARSE_PARSE) don't care ! */
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate /*
2460Sstevel@tonic-gate * stayopen is set to 0 by default in order to close the opened
2470Sstevel@tonic-gate * file. Some applications may break if it is set to 1.
2480Sstevel@tonic-gate */
2490Sstevel@tonic-gate if (check != 0 && !args->stayopen) {
2500Sstevel@tonic-gate (void) _nss_user_endent(be, 0);
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate
2530Sstevel@tonic-gate return (res);
2540Sstevel@tonic-gate }
2550Sstevel@tonic-gate
2560Sstevel@tonic-gate
257*2830Sdjl /*ARGSUSED*/
2580Sstevel@tonic-gate nss_status_t
_nss_user_destr(be,dummy)2590Sstevel@tonic-gate _nss_user_destr(be, dummy)
2600Sstevel@tonic-gate user_backend_ptr_t be;
2610Sstevel@tonic-gate void *dummy;
2620Sstevel@tonic-gate {
2630Sstevel@tonic-gate if (be != 0) {
2640Sstevel@tonic-gate if (be->f != 0) {
265*2830Sdjl (void) _nss_user_endent(be, 0);
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate free((char *)be->filename);
2680Sstevel@tonic-gate free(be);
2690Sstevel@tonic-gate }
2700Sstevel@tonic-gate return (NSS_SUCCESS); /* In case anyone is dumb enough to check */
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate
2730Sstevel@tonic-gate nss_backend_t *
_nss_user_constr(ops,n_ops,filename,min_bufsize)2740Sstevel@tonic-gate _nss_user_constr(ops, n_ops, filename, min_bufsize)
2750Sstevel@tonic-gate user_backend_op_t ops[];
2760Sstevel@tonic-gate int n_ops;
2770Sstevel@tonic-gate const char *filename;
2780Sstevel@tonic-gate int min_bufsize;
2790Sstevel@tonic-gate {
2800Sstevel@tonic-gate user_backend_ptr_t be;
2810Sstevel@tonic-gate
2821914Scasper if ((be = (user_backend_ptr_t)malloc(sizeof (*be))) == 0) {
2830Sstevel@tonic-gate return (0);
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate be->ops = ops;
2860Sstevel@tonic-gate be->n_ops = n_ops;
2870Sstevel@tonic-gate if ((be->filename = strdup(filename)) == NULL) {
2880Sstevel@tonic-gate free(be);
2890Sstevel@tonic-gate return (NULL);
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate be->minbuf = min_bufsize;
2920Sstevel@tonic-gate be->f = 0;
2930Sstevel@tonic-gate be->buf = 0;
2940Sstevel@tonic-gate
2951914Scasper return ((nss_backend_t *)be);
2960Sstevel@tonic-gate }
297