xref: /onnv-gate/usr/src/lib/nsswitch/user/common/user_common.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  *
26*0Sstevel@tonic-gate  * Common code and structures used by name-service-switch "user" backends.
27*0Sstevel@tonic-gate  * Much of this was taken directly from the files_common.c source.
28*0Sstevel@tonic-gate  */
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate /*
33*0Sstevel@tonic-gate  * An implementation that used mmap() sensibly would be a wonderful thing,
34*0Sstevel@tonic-gate  *   but this here is just yer standard fgets() thang.
35*0Sstevel@tonic-gate  */
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #include <stdio.h>
38*0Sstevel@tonic-gate #include <stdlib.h>
39*0Sstevel@tonic-gate #include <ctype.h>
40*0Sstevel@tonic-gate #include <fcntl.h>
41*0Sstevel@tonic-gate #include <poll.h>
42*0Sstevel@tonic-gate #include <unistd.h>
43*0Sstevel@tonic-gate #include "user_common.h"
44*0Sstevel@tonic-gate #include <sys/stat.h>
45*0Sstevel@tonic-gate #include "../../../libnsl/include/nsl_stdio_prv.h"
46*0Sstevel@tonic-gate #include <string.h>
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate nss_status_t
49*0Sstevel@tonic-gate _nss_user_setent(be, dummy)
50*0Sstevel@tonic-gate 	user_backend_ptr_t	be;
51*0Sstevel@tonic-gate 	void			*dummy;
52*0Sstevel@tonic-gate {
53*0Sstevel@tonic-gate 	if (be->f == 0) {
54*0Sstevel@tonic-gate 		if (be->filename == 0) {
55*0Sstevel@tonic-gate 			/* Backend isn't initialized properly? */
56*0Sstevel@tonic-gate 			return (NSS_UNAVAIL);
57*0Sstevel@tonic-gate 		}
58*0Sstevel@tonic-gate 		if ((be->f = __nsl_fopen(be->filename, "r")) == 0) {
59*0Sstevel@tonic-gate 			return (NSS_UNAVAIL);
60*0Sstevel@tonic-gate 		}
61*0Sstevel@tonic-gate 	} else {
62*0Sstevel@tonic-gate 		__nsl_rewind(be->f);
63*0Sstevel@tonic-gate 	}
64*0Sstevel@tonic-gate 	return (NSS_SUCCESS);
65*0Sstevel@tonic-gate }
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate nss_status_t
68*0Sstevel@tonic-gate _nss_user_endent(be, dummy)
69*0Sstevel@tonic-gate 	user_backend_ptr_t	be;
70*0Sstevel@tonic-gate 	void			*dummy;
71*0Sstevel@tonic-gate {
72*0Sstevel@tonic-gate 	if (be->f != 0) {
73*0Sstevel@tonic-gate 		__nsl_fclose(be->f);
74*0Sstevel@tonic-gate 		be->f = 0;
75*0Sstevel@tonic-gate 	}
76*0Sstevel@tonic-gate 	if (be->buf != 0) {
77*0Sstevel@tonic-gate 		free(be->buf);
78*0Sstevel@tonic-gate 		be->buf = 0;
79*0Sstevel@tonic-gate 	}
80*0Sstevel@tonic-gate 	return (NSS_SUCCESS);
81*0Sstevel@tonic-gate }
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate /*
84*0Sstevel@tonic-gate  * This routine reads a line, including the processing of continuation
85*0Sstevel@tonic-gate  * characters.  It always leaves (or inserts) \n\0 at the end of the line.
86*0Sstevel@tonic-gate  * It returns the length of the line read, excluding the \n\0.  Who's idea
87*0Sstevel@tonic-gate  * was this?
88*0Sstevel@tonic-gate  * Returns -1 on EOF.
89*0Sstevel@tonic-gate  *
90*0Sstevel@tonic-gate  * Note that since each concurrent call to _nss_user_read_line has
91*0Sstevel@tonic-gate  * it's own FILE pointer, we can use getc_unlocked w/o difficulties,
92*0Sstevel@tonic-gate  * a substantial performance win.
93*0Sstevel@tonic-gate  */
94*0Sstevel@tonic-gate int
95*0Sstevel@tonic-gate _nss_user_read_line(f, buffer, buflen)
96*0Sstevel@tonic-gate 	__NSL_FILE		*f;
97*0Sstevel@tonic-gate 	char			*buffer;
98*0Sstevel@tonic-gate 	int			buflen;
99*0Sstevel@tonic-gate {
100*0Sstevel@tonic-gate 	int			linelen;	/* 1st unused slot in buffer */
101*0Sstevel@tonic-gate 	int			c;
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 	while (1) {
104*0Sstevel@tonic-gate 		linelen = 0;
105*0Sstevel@tonic-gate 		while (linelen < buflen - 1) {	/* "- 1" saves room for \n\0 */
106*0Sstevel@tonic-gate 			switch (c = __nsl_getc_unlocked(f)) {
107*0Sstevel@tonic-gate 			case EOF:
108*0Sstevel@tonic-gate 				if (linelen == 0 ||
109*0Sstevel@tonic-gate 				    buffer[linelen - 1] == '\\') {
110*0Sstevel@tonic-gate 					return (-1);
111*0Sstevel@tonic-gate 				} else {
112*0Sstevel@tonic-gate 					buffer[linelen    ] = '\n';
113*0Sstevel@tonic-gate 					buffer[linelen + 1] = '\0';
114*0Sstevel@tonic-gate 					return (linelen);
115*0Sstevel@tonic-gate 				}
116*0Sstevel@tonic-gate 			case '\n':
117*0Sstevel@tonic-gate 				if (linelen > 0 &&
118*0Sstevel@tonic-gate 				    buffer[linelen - 1] == '\\') {
119*0Sstevel@tonic-gate 					--linelen;  /* remove the '\\' */
120*0Sstevel@tonic-gate 				} else {
121*0Sstevel@tonic-gate 					buffer[linelen    ] = '\n';
122*0Sstevel@tonic-gate 					buffer[linelen + 1] = '\0';
123*0Sstevel@tonic-gate 					return (linelen);
124*0Sstevel@tonic-gate 				}
125*0Sstevel@tonic-gate 				break;
126*0Sstevel@tonic-gate 			default:
127*0Sstevel@tonic-gate 				buffer[linelen++] = c;
128*0Sstevel@tonic-gate 			}
129*0Sstevel@tonic-gate 		}
130*0Sstevel@tonic-gate 		/* Buffer overflow -- eat rest of line and loop again */
131*0Sstevel@tonic-gate 		/* ===> Should syslog() */
132*0Sstevel@tonic-gate 		do {
133*0Sstevel@tonic-gate 			c = __nsl_getc_unlocked(f);
134*0Sstevel@tonic-gate 			if (c == EOF) {
135*0Sstevel@tonic-gate 				return (-1);
136*0Sstevel@tonic-gate 			}
137*0Sstevel@tonic-gate 		} while (c != '\n');
138*0Sstevel@tonic-gate 	}
139*0Sstevel@tonic-gate 	/*NOTREACHED*/
140*0Sstevel@tonic-gate }
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate /*
144*0Sstevel@tonic-gate  * Could implement this as an iterator function on top of _nss_user_do_all(),
145*0Sstevel@tonic-gate  *   but the shared code is small enough that it'd be pretty silly.
146*0Sstevel@tonic-gate  */
147*0Sstevel@tonic-gate nss_status_t
148*0Sstevel@tonic-gate _nss_user_XY_all(be, args, netdb, filter, check)
149*0Sstevel@tonic-gate 	user_backend_ptr_t	be;
150*0Sstevel@tonic-gate 	nss_XbyY_args_t		*args;
151*0Sstevel@tonic-gate 	int			netdb;		/* whether it uses netdb */
152*0Sstevel@tonic-gate 						/* format or not */
153*0Sstevel@tonic-gate 	const char		*filter;	/* advisory, to speed up */
154*0Sstevel@tonic-gate 						/* string search */
155*0Sstevel@tonic-gate 	user_XY_check_func	check;	/* NULL means one-shot, for getXXent */
156*0Sstevel@tonic-gate {
157*0Sstevel@tonic-gate 	nss_status_t		res;
158*0Sstevel@tonic-gate 	int	parsestat;
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 	if (be->buf == 0 &&
161*0Sstevel@tonic-gate 		(be->buf = malloc(be->minbuf)) == 0) {
162*0Sstevel@tonic-gate 		return (NSS_UNAVAIL); /* really panic, malloc failed */
163*0Sstevel@tonic-gate 	}
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 	if (check != 0 || be->f == 0) {
166*0Sstevel@tonic-gate 		if ((res = _nss_user_setent(be, 0)) != NSS_SUCCESS) {
167*0Sstevel@tonic-gate 			return (res);
168*0Sstevel@tonic-gate 		}
169*0Sstevel@tonic-gate 	}
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate 	res = NSS_NOTFOUND;
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 	while (1) {
174*0Sstevel@tonic-gate 		char		*instr	= be->buf;
175*0Sstevel@tonic-gate 		int		linelen;
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 		if ((linelen = _nss_user_read_line(be->f, instr,
178*0Sstevel@tonic-gate 		    be->minbuf)) < 0) {
179*0Sstevel@tonic-gate 			/* End of file */
180*0Sstevel@tonic-gate 			args->returnval = 0;
181*0Sstevel@tonic-gate 			args->erange    = 0;
182*0Sstevel@tonic-gate 			break;
183*0Sstevel@tonic-gate 		}
184*0Sstevel@tonic-gate 		if (filter != 0 && strstr(instr, filter) == 0) {
185*0Sstevel@tonic-gate 			/*
186*0Sstevel@tonic-gate 			 * Optimization:  if the entry doesn't contain the
187*0Sstevel@tonic-gate 			 *   filter string then it can't be the entry we want,
188*0Sstevel@tonic-gate 			 *   so don't bother looking more closely at it.
189*0Sstevel@tonic-gate 			 */
190*0Sstevel@tonic-gate 			continue;
191*0Sstevel@tonic-gate 		}
192*0Sstevel@tonic-gate 		if (netdb) {
193*0Sstevel@tonic-gate 			char		*first;
194*0Sstevel@tonic-gate 			char		*last;
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate 			if ((last = strchr(instr, '#')) == 0) {
197*0Sstevel@tonic-gate 				last = instr + linelen;
198*0Sstevel@tonic-gate 			}
199*0Sstevel@tonic-gate 			*last-- = '\0';		/* Nuke '\n' or #comment */
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 			/*
202*0Sstevel@tonic-gate 			 * Skip leading whitespace.  Normally there isn't
203*0Sstevel@tonic-gate 			 *   any, so it's not worth calling strspn().
204*0Sstevel@tonic-gate 			 */
205*0Sstevel@tonic-gate 			for (first = instr;  isspace(*first);  first++) {
206*0Sstevel@tonic-gate 				;
207*0Sstevel@tonic-gate 			}
208*0Sstevel@tonic-gate 			if (*first == '\0') {
209*0Sstevel@tonic-gate 				continue;
210*0Sstevel@tonic-gate 			}
211*0Sstevel@tonic-gate 			/*
212*0Sstevel@tonic-gate 			 * Found something non-blank on the line.  Skip back
213*0Sstevel@tonic-gate 			 * over any trailing whitespace;  since we know
214*0Sstevel@tonic-gate 			 * there's non-whitespace earlier in the line,
215*0Sstevel@tonic-gate 			 * checking for termination is easy.
216*0Sstevel@tonic-gate 			 */
217*0Sstevel@tonic-gate 			while (isspace(*last)) {
218*0Sstevel@tonic-gate 				--last;
219*0Sstevel@tonic-gate 			}
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate 			linelen = last - first + 1;
222*0Sstevel@tonic-gate 			if (first != instr) {
223*0Sstevel@tonic-gate 					instr = first;
224*0Sstevel@tonic-gate 			}
225*0Sstevel@tonic-gate 		}
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 		args->returnval = 0;
228*0Sstevel@tonic-gate 		parsestat = (*args->str2ent)(instr, linelen, args->buf.result,
229*0Sstevel@tonic-gate 				args->buf.buffer, args->buf.buflen);
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate 		if (parsestat == NSS_STR_PARSE_SUCCESS) {
232*0Sstevel@tonic-gate 			args->returnval = args->buf.result;
233*0Sstevel@tonic-gate 			if (check == 0 || (*check)(args)) {
234*0Sstevel@tonic-gate 				res = NSS_SUCCESS;
235*0Sstevel@tonic-gate 				break;
236*0Sstevel@tonic-gate 			}
237*0Sstevel@tonic-gate 		} else if (parsestat == NSS_STR_PARSE_ERANGE) {
238*0Sstevel@tonic-gate 			args->erange = 1;	/* should we just skip this */
239*0Sstevel@tonic-gate 						/* one long line ?? */
240*0Sstevel@tonic-gate 		} /* else if (parsestat == NSS_STR_PARSE_PARSE) don't care ! */
241*0Sstevel@tonic-gate 	}
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate 	/*
244*0Sstevel@tonic-gate 	 * stayopen is set to 0 by default in order to close the opened
245*0Sstevel@tonic-gate 	 * file.  Some applications may break if it is set to 1.
246*0Sstevel@tonic-gate 	 */
247*0Sstevel@tonic-gate 	if (check != 0 && !args->stayopen) {
248*0Sstevel@tonic-gate 		(void) _nss_user_endent(be, 0);
249*0Sstevel@tonic-gate 	}
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate 	return (res);
252*0Sstevel@tonic-gate }
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate nss_status_t
256*0Sstevel@tonic-gate _nss_user_destr(be, dummy)
257*0Sstevel@tonic-gate 	user_backend_ptr_t	be;
258*0Sstevel@tonic-gate 	void			*dummy;
259*0Sstevel@tonic-gate {
260*0Sstevel@tonic-gate 	if (be != 0) {
261*0Sstevel@tonic-gate 		if (be->f != 0) {
262*0Sstevel@tonic-gate 			_nss_user_endent(be, 0);
263*0Sstevel@tonic-gate 		}
264*0Sstevel@tonic-gate 		free((char *)be->filename);
265*0Sstevel@tonic-gate 		free(be);
266*0Sstevel@tonic-gate 	}
267*0Sstevel@tonic-gate 	return (NSS_SUCCESS);	/* In case anyone is dumb enough to check */
268*0Sstevel@tonic-gate }
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate nss_backend_t *
271*0Sstevel@tonic-gate _nss_user_constr(ops, n_ops, filename, min_bufsize)
272*0Sstevel@tonic-gate 	user_backend_op_t	ops[];
273*0Sstevel@tonic-gate 	int			n_ops;
274*0Sstevel@tonic-gate 	const char		*filename;
275*0Sstevel@tonic-gate 	int			min_bufsize;
276*0Sstevel@tonic-gate {
277*0Sstevel@tonic-gate 	user_backend_ptr_t	be;
278*0Sstevel@tonic-gate 
279*0Sstevel@tonic-gate 	if ((be = (user_backend_ptr_t) malloc(sizeof (*be))) == 0) {
280*0Sstevel@tonic-gate 		return (0);
281*0Sstevel@tonic-gate 	}
282*0Sstevel@tonic-gate 	be->ops		= ops;
283*0Sstevel@tonic-gate 	be->n_ops	= n_ops;
284*0Sstevel@tonic-gate 	if ((be->filename = strdup(filename)) == NULL) {
285*0Sstevel@tonic-gate 		free(be);
286*0Sstevel@tonic-gate 		return (NULL);
287*0Sstevel@tonic-gate 	}
288*0Sstevel@tonic-gate 	be->minbuf	= min_bufsize;
289*0Sstevel@tonic-gate 	be->f		= 0;
290*0Sstevel@tonic-gate 	be->buf		= 0;
291*0Sstevel@tonic-gate 
292*0Sstevel@tonic-gate 	return ((nss_backend_t *) be);
293*0Sstevel@tonic-gate }
294