xref: /titanic_51/usr/src/lib/nsswitch/files/common/getexecattr.c (revision 2bc987325e3ded1865bff043128661815c4690b9)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 
25 #include <stdlib.h>
26 #include "files_common.h"
27 #include <time.h>
28 #include <exec_attr.h>
29 #include <strings.h>
30 #include <sys/stat.h>
31 #include <sys/mman.h>
32 #include <ctype.h>
33 #include <synch.h>
34 #include <sys/types.h>
35 #include <sys/uio.h>
36 #include <unistd.h>
37 
38 /*
39  * files/getexecattr.c -- "files" backend for nsswitch "exec_attr" database
40  *
41  * _execattr_files_read_line and _execattr_files_XY_all code based on
42  * nss_files_read_line and nss_files_XY_all respectively, from files_common.c
43  */
44 
45 
46 /* externs from libnsl */
47 extern int _doexeclist(nss_XbyY_args_t *);
48 extern int _readbufline(char *, int, char *, int, int *);
49 extern char *_exec_wild_id(char *, const char *);
50 extern void _exec_cleanup(nss_status_t, nss_XbyY_args_t *);
51 
52 
53 /*
54  * check_match: returns 1 if matching entry found, else returns 0.
55  */
56 static int
57 check_match(nss_XbyY_args_t *argp, const char *line, int linelen)
58 {
59 	const char	*limit, *linep, *keyp;
60 	_priv_execattr	*_priv_exec = (_priv_execattr *)(argp->key.attrp);
61 	const char	*exec_field[6];
62 	int		i;
63 
64 	exec_field[0] = _priv_exec->name;	/* name */
65 	exec_field[1] = _priv_exec->policy;	/* policy */
66 	exec_field[2] = _priv_exec->type;	/* type */
67 	exec_field[3] = NULL;			/* res1 */
68 	exec_field[4] = NULL;			/* res2 */
69 	exec_field[5] = _priv_exec->id;		/* id */
70 	/* No need to check attr field */
71 
72 	linep = line;
73 	limit = line + linelen;
74 
75 	for (i = 0; i < 6; i++) {
76 		keyp = exec_field[i];
77 		if (keyp) {
78 			/* compare field */
79 			while (*keyp && linep < limit &&
80 			    *linep != ':' && *keyp == *linep) {
81 				keyp++;
82 				linep++;
83 			}
84 			if (*keyp || linep == limit || *linep != ':')
85 				return (0);
86 		} else {
87 			/* skip field */
88 			while (linep < limit && *linep != ':')
89 				linep++;
90 		}
91 		linep++;
92 	}
93 	return (1);
94 }
95 
96 
97 static nss_status_t
98 _exec_files_XY_all(files_backend_ptr_t be,
99     nss_XbyY_args_t *argp,
100     int getby_flag)
101 {
102 	int		parse_stat = 0;
103 	int		lastlen = 0;
104 	int		exec_fd = 0;
105 	int		f_size = 0;
106 	time_t		f_time = 0;
107 	static time_t	read_time = 0;
108 	char		*first;
109 	char		*last;
110 	static char	*f_buf = NULL;
111 	struct stat	f_stat;
112 	nss_status_t	res = NSS_NOTFOUND;
113 	_priv_execattr	*_priv_exec = (_priv_execattr *)(argp->key.attrp);
114 	static rwlock_t	exec_lock;
115 
116 	if (((be->buf == NULL) &&
117 	    ((be->buf = (char *)calloc(1, be->minbuf)) == NULL)) ||
118 	    (be->filename == NULL) ||
119 	    (rw_rdlock(&exec_lock) != 0)) {
120 		return (NSS_UNAVAIL);
121 	}
122 
123 	/*
124 	 * check the size and the time stamp on the file
125 	 */
126 	if (stat(be->filename, &f_stat) != 0) {
127 		(void) _nss_files_endent(be, 0);
128 		(void) rw_unlock(&exec_lock);
129 		return (NSS_UNAVAIL);
130 	}
131 
132 	f_size = f_stat.st_size;
133 	f_time = f_stat.st_mtime;
134 
135 	while (f_time > read_time || f_buf == NULL) {
136 		/*
137 		 * file has been modified since we last read it
138 		 * or we never read it or memory allocation
139 		 * failed before.
140 		 * read it into the buffer with rw lock.
141 		 */
142 		(void) rw_unlock(&exec_lock);
143 		if (rw_wrlock(&exec_lock) != 0) {
144 			(void) _nss_files_endent(be, 0);
145 			return (NSS_UNAVAIL);
146 		}
147 		if ((be->f = fopen(be->filename, "rF")) == 0) {
148 			(void) _nss_files_endent(be, 0);
149 			(void) rw_unlock(&exec_lock);
150 			return (NSS_UNAVAIL);
151 		}
152 		exec_fd = fileno(be->f);
153 		if (f_buf != NULL)
154 			free(f_buf);
155 		if ((f_buf = malloc(f_size)) == NULL) {
156 			(void) _nss_files_endent(be, 0);
157 			(void) rw_unlock(&exec_lock);
158 			return (NSS_UNAVAIL);
159 		}
160 		if (read(exec_fd, f_buf, f_size) < f_size) {
161 			free(f_buf);
162 			(void) _nss_files_endent(be, 0);
163 			(void) rw_unlock(&exec_lock);
164 			return (NSS_UNAVAIL);
165 		}
166 		read_time = f_time;
167 		(void) rw_unlock(&exec_lock);
168 		/*
169 		 * verify that the file did not change after
170 		 * we read it.
171 		 */
172 		if (rw_rdlock(&exec_lock) != 0) {
173 			free(f_buf);
174 			(void) _nss_files_endent(be, 0);
175 			return (NSS_UNAVAIL);
176 		}
177 		if (stat(be->filename, &f_stat) != 0) {
178 			free(f_buf);
179 			(void) _nss_files_endent(be, 0);
180 			(void) rw_unlock(&exec_lock);
181 			return (NSS_UNAVAIL);
182 		}
183 		f_size = f_stat.st_size;
184 		f_time = f_stat.st_mtime;
185 	}
186 
187 	res = NSS_NOTFOUND;
188 	/*CONSTCOND*/
189 	while (1) {
190 		int	linelen = 0;
191 		char	*instr = be->buf;
192 
193 		linelen = _readbufline(f_buf, f_size, instr, be->minbuf,
194 		    &lastlen);
195 		if (linelen < 0) {
196 			/* End of file */
197 			break;
198 		}
199 
200 		/*
201 		 * If the entry doesn't contain the filter string then
202 		 * it can't be the entry we want, so don't bother looking
203 		 * more closely at it.
204 		 */
205 		switch (getby_flag) {
206 		case NSS_DBOP_EXECATTR_BYNAME:
207 			if (strstr(instr, _priv_exec->name) == NULL)
208 				continue;
209 			break;
210 		case NSS_DBOP_EXECATTR_BYID:
211 			if (strstr(instr, _priv_exec->id) == NULL)
212 				continue;
213 			break;
214 		case NSS_DBOP_EXECATTR_BYNAMEID:
215 			if ((strstr(instr, _priv_exec->name) == NULL) ||
216 			    (strstr(instr, _priv_exec->id) == NULL))
217 				continue;
218 			break;
219 		default:
220 			break;
221 		}
222 		if (((_priv_exec->policy != NULL) &&
223 		    (strstr(instr, _priv_exec->policy) == NULL)) ||
224 		    ((_priv_exec->type != NULL) &&
225 		    (strstr(instr, _priv_exec->type) == NULL)))
226 				continue;
227 
228 		/*
229 		 * Get rid of white spaces, comments etc.
230 		 */
231 		if ((last = strchr(instr, '#')) == NULL)
232 			last = instr + linelen;
233 		*last-- = '\0';	/* Nuke '\n' or #comment */
234 		/*
235 		 * Skip leading whitespace.  Normally there isn't any,
236 		 * so it's not worth calling strspn().
237 		 */
238 		for (first = instr; isspace(*first); first++)
239 			;
240 		if (*first == '\0')
241 			continue;
242 		/*
243 		 * Found something non-blank on the line.  Skip back
244 		 * over any trailing whitespace;  since we know there's
245 		 * non-whitespace earlier in the line, checking for
246 		 * termination is easy.
247 		 */
248 		while (isspace(*last))
249 			--last;
250 		linelen = last - first + 1;
251 		if (first != instr)
252 			instr = first;
253 
254 		/* Check the entry */
255 		argp->returnval = NULL;
256 		argp->returnlen = 0;
257 		if (check_match(argp, instr, linelen) == 0)
258 			continue;
259 
260 		/* Marshall the data */
261 		parse_stat = (*argp->str2ent)(instr, linelen, argp->buf.result,
262 		    argp->buf.buffer, argp->buf.buflen);
263 		if (parse_stat == NSS_STR_PARSE_SUCCESS) {
264 			argp->returnval = (argp->buf.result != NULL)?
265 			    argp->buf.result : argp->buf.buffer;
266 			argp->returnlen = linelen;
267 			res = NSS_SUCCESS;
268 			if (IS_GET_ONE(_priv_exec->search_flag)) {
269 				break;
270 			} else if (_doexeclist(argp) == 0) {
271 				res = NSS_UNAVAIL;
272 				break;
273 			}
274 		} else if (parse_stat == NSS_STR_PARSE_ERANGE) {
275 			argp->erange = 1;
276 			break;
277 		} /* else if (parse_stat == NSS_STR_PARSE_PARSE) don't care ! */
278 	}
279 
280 	(void) _nss_files_endent(be, 0);
281 	(void) rw_unlock(&exec_lock);
282 
283 	return (res);
284 }
285 
286 
287 /*
288  * If search for exact match for id failed, get_wild checks if we have
289  * a wild-card entry for that id.
290  */
291 static nss_status_t
292 get_wild(files_backend_ptr_t be, nss_XbyY_args_t *argp, int getby_flag)
293 {
294 	const char	*orig_id = NULL;
295 	char		*old_id = NULL;
296 	char		*wild_id = NULL;
297 	nss_status_t	res = NSS_NOTFOUND;
298 	_priv_execattr	*_priv_exec = (_priv_execattr *)(argp->key.attrp);
299 
300 	orig_id = _priv_exec->id;
301 	old_id = strdup(_priv_exec->id);
302 	wild_id = old_id;
303 	while ((wild_id = _exec_wild_id(wild_id, _priv_exec->type)) != NULL) {
304 		_priv_exec->id = wild_id;
305 		res = _exec_files_XY_all(be, argp, getby_flag);
306 		if (res == NSS_SUCCESS)
307 			break;
308 	}
309 	_priv_exec->id = orig_id;
310 	if (old_id)
311 		free(old_id);
312 
313 	return (res);
314 }
315 
316 
317 static nss_status_t
318 getbynam(files_backend_ptr_t be, void *a)
319 {
320 	nss_status_t	res;
321 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
322 
323 	res =  _exec_files_XY_all(be, argp, NSS_DBOP_EXECATTR_BYNAME);
324 
325 	_exec_cleanup(res, argp);
326 
327 	return (res);
328 }
329 
330 
331 static nss_status_t
332 getbyid(files_backend_ptr_t be, void *a)
333 {
334 	nss_status_t	res;
335 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
336 	/*LINTED*/
337 	_priv_execattr	*_priv_exec = (_priv_execattr *)(argp->key.attrp);
338 
339 	res = _exec_files_XY_all(be, argp, NSS_DBOP_EXECATTR_BYID);
340 
341 	if (res != NSS_SUCCESS)
342 		res = get_wild(be, argp, NSS_DBOP_EXECATTR_BYID);
343 
344 	_exec_cleanup(res, argp);
345 
346 	return (res);
347 }
348 
349 
350 static nss_status_t
351 getbynameid(files_backend_ptr_t be, void *a)
352 {
353 	nss_status_t	res;
354 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
355 	/*LINTED*/
356 	_priv_execattr	*_priv_exec = (_priv_execattr *)(argp->key.attrp);
357 
358 	res = _exec_files_XY_all(be, argp, NSS_DBOP_EXECATTR_BYNAMEID);
359 
360 	if (res != NSS_SUCCESS)
361 		res = get_wild(be, argp, NSS_DBOP_EXECATTR_BYNAMEID);
362 
363 	_exec_cleanup(res, argp);
364 
365 	return (res);
366 }
367 
368 
369 static files_backend_op_t execattr_ops[] = {
370 	_nss_files_destr,
371 	_nss_files_endent,
372 	_nss_files_setent,
373 	_nss_files_getent_netdb,
374 	getbynam,
375 	getbyid,
376 	getbynameid
377 };
378 
379 /*ARGSUSED*/
380 nss_backend_t  *
381 _nss_files_exec_attr_constr(const char *dummy1,
382     const char *dummy2,
383     const char *dummy3,
384     const char *dummy4,
385     const char *dummy5,
386     const char *dummy6,
387     const char *dummy7)
388 {
389 	return (_nss_files_constr(execattr_ops,
390 	    sizeof (execattr_ops)/sizeof (execattr_ops[0]),
391 	    EXECATTR_FILENAME, NSS_LINELEN_EXECATTR, NULL));
392 }
393