xref: /illumos-gate/usr/src/lib/nsswitch/user/common/user_common.c (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5004388ebScasper  * Common Development and Distribution License (the "License").
6004388ebScasper  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22004388ebScasper  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  * Common code and structures used by name-service-switch "user" backends.
267c478bd9Sstevel@tonic-gate  * Much of this was taken directly from the files_common.c source.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * An implementation that used mmap() sensibly would be a wonderful thing,
317c478bd9Sstevel@tonic-gate  *   but this here is just yer standard fgets() thang.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
34004388ebScasper #include "user_common.h"
357c478bd9Sstevel@tonic-gate #include <stdio.h>
367c478bd9Sstevel@tonic-gate #include <stdlib.h>
377c478bd9Sstevel@tonic-gate #include <ctype.h>
387c478bd9Sstevel@tonic-gate #include <fcntl.h>
397c478bd9Sstevel@tonic-gate #include <poll.h>
407c478bd9Sstevel@tonic-gate #include <unistd.h>
417c478bd9Sstevel@tonic-gate #include <sys/stat.h>
427c478bd9Sstevel@tonic-gate #include <string.h>
437c478bd9Sstevel@tonic-gate 
44*cb5caa98Sdjl /*ARGSUSED*/
457c478bd9Sstevel@tonic-gate nss_status_t
_nss_user_setent(be,dummy)467c478bd9Sstevel@tonic-gate _nss_user_setent(be, dummy)
477c478bd9Sstevel@tonic-gate 	user_backend_ptr_t	be;
487c478bd9Sstevel@tonic-gate 	void			*dummy;
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate 	if (be->f == 0) {
517c478bd9Sstevel@tonic-gate 		if (be->filename == 0) {
527c478bd9Sstevel@tonic-gate 			/* Backend isn't initialized properly? */
537c478bd9Sstevel@tonic-gate 			return (NSS_UNAVAIL);
547c478bd9Sstevel@tonic-gate 		}
55004388ebScasper 		if ((be->f = fopen(be->filename, "rF")) == 0) {
567c478bd9Sstevel@tonic-gate 			return (NSS_UNAVAIL);
577c478bd9Sstevel@tonic-gate 		}
587c478bd9Sstevel@tonic-gate 	} else {
59004388ebScasper 		rewind(be->f);
607c478bd9Sstevel@tonic-gate 	}
617c478bd9Sstevel@tonic-gate 	return (NSS_SUCCESS);
627c478bd9Sstevel@tonic-gate }
637c478bd9Sstevel@tonic-gate 
64*cb5caa98Sdjl /*ARGSUSED*/
657c478bd9Sstevel@tonic-gate nss_status_t
_nss_user_endent(be,dummy)667c478bd9Sstevel@tonic-gate _nss_user_endent(be, dummy)
677c478bd9Sstevel@tonic-gate 	user_backend_ptr_t	be;
687c478bd9Sstevel@tonic-gate 	void			*dummy;
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate 	if (be->f != 0) {
71*cb5caa98Sdjl 		(void) fclose(be->f);
727c478bd9Sstevel@tonic-gate 		be->f = 0;
737c478bd9Sstevel@tonic-gate 	}
747c478bd9Sstevel@tonic-gate 	if (be->buf != 0) {
757c478bd9Sstevel@tonic-gate 		free(be->buf);
767c478bd9Sstevel@tonic-gate 		be->buf = 0;
777c478bd9Sstevel@tonic-gate 	}
787c478bd9Sstevel@tonic-gate 	return (NSS_SUCCESS);
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate /*
827c478bd9Sstevel@tonic-gate  * This routine reads a line, including the processing of continuation
837c478bd9Sstevel@tonic-gate  * characters.  It always leaves (or inserts) \n\0 at the end of the line.
847c478bd9Sstevel@tonic-gate  * It returns the length of the line read, excluding the \n\0.  Who's idea
857c478bd9Sstevel@tonic-gate  * was this?
867c478bd9Sstevel@tonic-gate  * Returns -1 on EOF.
877c478bd9Sstevel@tonic-gate  *
887c478bd9Sstevel@tonic-gate  * Note that since each concurrent call to _nss_user_read_line has
897c478bd9Sstevel@tonic-gate  * it's own FILE pointer, we can use getc_unlocked w/o difficulties,
907c478bd9Sstevel@tonic-gate  * a substantial performance win.
917c478bd9Sstevel@tonic-gate  */
927c478bd9Sstevel@tonic-gate int
_nss_user_read_line(f,buffer,buflen)937c478bd9Sstevel@tonic-gate _nss_user_read_line(f, buffer, buflen)
94004388ebScasper 	FILE			*f;
957c478bd9Sstevel@tonic-gate 	char			*buffer;
967c478bd9Sstevel@tonic-gate 	int			buflen;
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate 	int			linelen;	/* 1st unused slot in buffer */
997c478bd9Sstevel@tonic-gate 	int			c;
1007c478bd9Sstevel@tonic-gate 
101*cb5caa98Sdjl 	/*CONSTCOND*/
1027c478bd9Sstevel@tonic-gate 	while (1) {
1037c478bd9Sstevel@tonic-gate 		linelen = 0;
1047c478bd9Sstevel@tonic-gate 		while (linelen < buflen - 1) {	/* "- 1" saves room for \n\0 */
105004388ebScasper 			switch (c = getc_unlocked(f)) {
1067c478bd9Sstevel@tonic-gate 			case EOF:
1077c478bd9Sstevel@tonic-gate 				if (linelen == 0 ||
1087c478bd9Sstevel@tonic-gate 				    buffer[linelen - 1] == '\\') {
1097c478bd9Sstevel@tonic-gate 					return (-1);
1107c478bd9Sstevel@tonic-gate 				} else {
1117c478bd9Sstevel@tonic-gate 					buffer[linelen    ] = '\n';
1127c478bd9Sstevel@tonic-gate 					buffer[linelen + 1] = '\0';
1137c478bd9Sstevel@tonic-gate 					return (linelen);
1147c478bd9Sstevel@tonic-gate 				}
1157c478bd9Sstevel@tonic-gate 			case '\n':
1167c478bd9Sstevel@tonic-gate 				if (linelen > 0 &&
1177c478bd9Sstevel@tonic-gate 				    buffer[linelen - 1] == '\\') {
1187c478bd9Sstevel@tonic-gate 					--linelen;  /* remove the '\\' */
1197c478bd9Sstevel@tonic-gate 				} else {
1207c478bd9Sstevel@tonic-gate 					buffer[linelen    ] = '\n';
1217c478bd9Sstevel@tonic-gate 					buffer[linelen + 1] = '\0';
1227c478bd9Sstevel@tonic-gate 					return (linelen);
1237c478bd9Sstevel@tonic-gate 				}
1247c478bd9Sstevel@tonic-gate 				break;
1257c478bd9Sstevel@tonic-gate 			default:
1267c478bd9Sstevel@tonic-gate 				buffer[linelen++] = c;
1277c478bd9Sstevel@tonic-gate 			}
1287c478bd9Sstevel@tonic-gate 		}
1297c478bd9Sstevel@tonic-gate 		/* Buffer overflow -- eat rest of line and loop again */
1307c478bd9Sstevel@tonic-gate 		/* ===> Should syslog() */
1317c478bd9Sstevel@tonic-gate 		do {
132004388ebScasper 			c = getc_unlocked(f);
1337c478bd9Sstevel@tonic-gate 			if (c == EOF) {
1347c478bd9Sstevel@tonic-gate 				return (-1);
1357c478bd9Sstevel@tonic-gate 			}
1367c478bd9Sstevel@tonic-gate 		} while (c != '\n');
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate /*
1437c478bd9Sstevel@tonic-gate  * Could implement this as an iterator function on top of _nss_user_do_all(),
1447c478bd9Sstevel@tonic-gate  *   but the shared code is small enough that it'd be pretty silly.
1457c478bd9Sstevel@tonic-gate  */
1467c478bd9Sstevel@tonic-gate nss_status_t
_nss_user_XY_all(be,args,netdb,filter,check)1477c478bd9Sstevel@tonic-gate _nss_user_XY_all(be, args, netdb, filter, check)
1487c478bd9Sstevel@tonic-gate 	user_backend_ptr_t	be;
1497c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t		*args;
1507c478bd9Sstevel@tonic-gate 	int			netdb;		/* whether it uses netdb */
1517c478bd9Sstevel@tonic-gate 						/* format or not */
1527c478bd9Sstevel@tonic-gate 	const char		*filter;	/* advisory, to speed up */
1537c478bd9Sstevel@tonic-gate 						/* string search */
1547c478bd9Sstevel@tonic-gate 	user_XY_check_func	check;	/* NULL means one-shot, for getXXent */
1557c478bd9Sstevel@tonic-gate {
1567c478bd9Sstevel@tonic-gate 	nss_status_t		res;
1577c478bd9Sstevel@tonic-gate 	int	parsestat;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	if (be->buf == 0 &&
1607c478bd9Sstevel@tonic-gate 		(be->buf = malloc(be->minbuf)) == 0) {
1617c478bd9Sstevel@tonic-gate 		return (NSS_UNAVAIL); /* really panic, malloc failed */
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	if (check != 0 || be->f == 0) {
1657c478bd9Sstevel@tonic-gate 		if ((res = _nss_user_setent(be, 0)) != NSS_SUCCESS) {
1667c478bd9Sstevel@tonic-gate 			return (res);
1677c478bd9Sstevel@tonic-gate 		}
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	res = NSS_NOTFOUND;
1717c478bd9Sstevel@tonic-gate 
172*cb5caa98Sdjl 	/*CONSTCOND*/
1737c478bd9Sstevel@tonic-gate 	while (1) {
1747c478bd9Sstevel@tonic-gate 		char		*instr	= be->buf;
1757c478bd9Sstevel@tonic-gate 		int		linelen;
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 		if ((linelen = _nss_user_read_line(be->f, instr,
1787c478bd9Sstevel@tonic-gate 		    be->minbuf)) < 0) {
1797c478bd9Sstevel@tonic-gate 			/* End of file */
1807c478bd9Sstevel@tonic-gate 			args->returnval = 0;
1817c478bd9Sstevel@tonic-gate 			args->erange    = 0;
1827c478bd9Sstevel@tonic-gate 			break;
1837c478bd9Sstevel@tonic-gate 		}
1847c478bd9Sstevel@tonic-gate 		if (filter != 0 && strstr(instr, filter) == 0) {
1857c478bd9Sstevel@tonic-gate 			/*
1867c478bd9Sstevel@tonic-gate 			 * Optimization:  if the entry doesn't contain the
1877c478bd9Sstevel@tonic-gate 			 *   filter string then it can't be the entry we want,
1887c478bd9Sstevel@tonic-gate 			 *   so don't bother looking more closely at it.
1897c478bd9Sstevel@tonic-gate 			 */
1907c478bd9Sstevel@tonic-gate 			continue;
1917c478bd9Sstevel@tonic-gate 		}
1927c478bd9Sstevel@tonic-gate 		if (netdb) {
1937c478bd9Sstevel@tonic-gate 			char		*first;
1947c478bd9Sstevel@tonic-gate 			char		*last;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 			if ((last = strchr(instr, '#')) == 0) {
1977c478bd9Sstevel@tonic-gate 				last = instr + linelen;
1987c478bd9Sstevel@tonic-gate 			}
1997c478bd9Sstevel@tonic-gate 			*last-- = '\0';		/* Nuke '\n' or #comment */
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 			/*
2027c478bd9Sstevel@tonic-gate 			 * Skip leading whitespace.  Normally there isn't
2037c478bd9Sstevel@tonic-gate 			 *   any, so it's not worth calling strspn().
2047c478bd9Sstevel@tonic-gate 			 */
2057c478bd9Sstevel@tonic-gate 			for (first = instr;  isspace(*first);  first++) {
2067c478bd9Sstevel@tonic-gate 				;
2077c478bd9Sstevel@tonic-gate 			}
2087c478bd9Sstevel@tonic-gate 			if (*first == '\0') {
2097c478bd9Sstevel@tonic-gate 				continue;
2107c478bd9Sstevel@tonic-gate 			}
2117c478bd9Sstevel@tonic-gate 			/*
2127c478bd9Sstevel@tonic-gate 			 * Found something non-blank on the line.  Skip back
2137c478bd9Sstevel@tonic-gate 			 * over any trailing whitespace;  since we know
2147c478bd9Sstevel@tonic-gate 			 * there's non-whitespace earlier in the line,
2157c478bd9Sstevel@tonic-gate 			 * checking for termination is easy.
2167c478bd9Sstevel@tonic-gate 			 */
2177c478bd9Sstevel@tonic-gate 			while (isspace(*last)) {
2187c478bd9Sstevel@tonic-gate 				--last;
2197c478bd9Sstevel@tonic-gate 			}
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 			linelen = last - first + 1;
2227c478bd9Sstevel@tonic-gate 			if (first != instr) {
2237c478bd9Sstevel@tonic-gate 					instr = first;
2247c478bd9Sstevel@tonic-gate 			}
2257c478bd9Sstevel@tonic-gate 		}
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 		args->returnval = 0;
2287c478bd9Sstevel@tonic-gate 		parsestat = (*args->str2ent)(instr, linelen, args->buf.result,
2297c478bd9Sstevel@tonic-gate 				args->buf.buffer, args->buf.buflen);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 		if (parsestat == NSS_STR_PARSE_SUCCESS) {
2327c478bd9Sstevel@tonic-gate 			args->returnval = args->buf.result;
2337c478bd9Sstevel@tonic-gate 			if (check == 0 || (*check)(args)) {
2347c478bd9Sstevel@tonic-gate 				res = NSS_SUCCESS;
2357c478bd9Sstevel@tonic-gate 				break;
2367c478bd9Sstevel@tonic-gate 			}
2377c478bd9Sstevel@tonic-gate 		} else if (parsestat == NSS_STR_PARSE_ERANGE) {
2387c478bd9Sstevel@tonic-gate 			args->erange = 1;	/* should we just skip this */
2397c478bd9Sstevel@tonic-gate 						/* one long line ?? */
2407c478bd9Sstevel@tonic-gate 		} /* else if (parsestat == NSS_STR_PARSE_PARSE) don't care ! */
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	/*
2447c478bd9Sstevel@tonic-gate 	 * stayopen is set to 0 by default in order to close the opened
2457c478bd9Sstevel@tonic-gate 	 * file.  Some applications may break if it is set to 1.
2467c478bd9Sstevel@tonic-gate 	 */
2477c478bd9Sstevel@tonic-gate 	if (check != 0 && !args->stayopen) {
2487c478bd9Sstevel@tonic-gate 		(void) _nss_user_endent(be, 0);
2497c478bd9Sstevel@tonic-gate 	}
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	return (res);
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 
255*cb5caa98Sdjl /*ARGSUSED*/
2567c478bd9Sstevel@tonic-gate nss_status_t
_nss_user_destr(be,dummy)2577c478bd9Sstevel@tonic-gate _nss_user_destr(be, dummy)
2587c478bd9Sstevel@tonic-gate 	user_backend_ptr_t	be;
2597c478bd9Sstevel@tonic-gate 	void			*dummy;
2607c478bd9Sstevel@tonic-gate {
2617c478bd9Sstevel@tonic-gate 	if (be != 0) {
2627c478bd9Sstevel@tonic-gate 		if (be->f != 0) {
263*cb5caa98Sdjl 			(void) _nss_user_endent(be, 0);
2647c478bd9Sstevel@tonic-gate 		}
2657c478bd9Sstevel@tonic-gate 		free((char *)be->filename);
2667c478bd9Sstevel@tonic-gate 		free(be);
2677c478bd9Sstevel@tonic-gate 	}
2687c478bd9Sstevel@tonic-gate 	return (NSS_SUCCESS);	/* In case anyone is dumb enough to check */
2697c478bd9Sstevel@tonic-gate }
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate nss_backend_t *
_nss_user_constr(ops,n_ops,filename,min_bufsize)2727c478bd9Sstevel@tonic-gate _nss_user_constr(ops, n_ops, filename, min_bufsize)
2737c478bd9Sstevel@tonic-gate 	user_backend_op_t	ops[];
2747c478bd9Sstevel@tonic-gate 	int			n_ops;
2757c478bd9Sstevel@tonic-gate 	const char		*filename;
2767c478bd9Sstevel@tonic-gate 	int			min_bufsize;
2777c478bd9Sstevel@tonic-gate {
2787c478bd9Sstevel@tonic-gate 	user_backend_ptr_t	be;
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	if ((be = (user_backend_ptr_t)malloc(sizeof (*be))) == 0) {
2817c478bd9Sstevel@tonic-gate 		return (0);
2827c478bd9Sstevel@tonic-gate 	}
2837c478bd9Sstevel@tonic-gate 	be->ops		= ops;
2847c478bd9Sstevel@tonic-gate 	be->n_ops	= n_ops;
2857c478bd9Sstevel@tonic-gate 	if ((be->filename = strdup(filename)) == NULL) {
2867c478bd9Sstevel@tonic-gate 		free(be);
2877c478bd9Sstevel@tonic-gate 		return (NULL);
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate 	be->minbuf	= min_bufsize;
2907c478bd9Sstevel@tonic-gate 	be->f		= 0;
2917c478bd9Sstevel@tonic-gate 	be->buf		= 0;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	return ((nss_backend_t *)be);
2947c478bd9Sstevel@tonic-gate }
295