xref: /titanic_50/usr/src/lib/nsswitch/files/common/getexecattr.c (revision a3470551d4852eb32a15cd435c98646b57a2c56a)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <stdlib.h>
29 #include "files_common.h"
30 #include <time.h>
31 #include <exec_attr.h>
32 #include <strings.h>
33 #include <sys/stat.h>
34 #include <sys/mman.h>
35 #include <ctype.h>
36 #include <synch.h>
37 #include <sys/types.h>
38 #include <sys/uio.h>
39 #include <unistd.h>
40 
41 /*
42  * files/getexecattr.c -- "files" backend for nsswitch "exec_attr" database
43  *
44  * _execattr_files_read_line and _execattr_files_XY_all code based on
45  * nss_files_read_line and nss_files_XY_all respectively, from files_common.c
46  */
47 
48 
49 /* externs from libnsl */
50 extern int _doexeclist(nss_XbyY_args_t *);
51 extern int _readbufline(char *, int, char *, int, int *);
52 extern char *_exec_wild_id(char *, const char *);
53 extern void _exec_cleanup(nss_status_t, nss_XbyY_args_t *);
54 
55 
56 /*
57  * check_match: returns 1 if matching entry found, else returns 0.
58  */
59 static int
60 check_match(nss_XbyY_args_t *argp, const char *line, int linelen)
61 {
62 	const char	*limit, *linep, *keyp;
63 	_priv_execattr	*_priv_exec = (_priv_execattr *)(argp->key.attrp);
64 	const char	*exec_field[6];
65 	int		i;
66 
67 	exec_field[0] = _priv_exec->name;	/* name */
68 	exec_field[1] = _priv_exec->policy;	/* policy */
69 	exec_field[2] = _priv_exec->type;	/* type */
70 	exec_field[3] = NULL;			/* res1 */
71 	exec_field[4] = NULL;			/* res2 */
72 	exec_field[5] = _priv_exec->id;		/* id */
73 	/* No need to check attr field */
74 
75 	linep = line;
76 	limit = line + linelen;
77 
78 	for (i = 0; i < 6; i++) {
79 		keyp = exec_field[i];
80 		if (keyp) {
81 			/* compare field */
82 			while (*keyp && linep < limit &&
83 				*linep != ':' && *keyp == *linep) {
84 				keyp++;
85 				linep++;
86 			}
87 			if (*keyp || linep == limit || *linep != ':')
88 				return (0);
89 		} else {
90 			/* skip field */
91 			while (linep < limit && *linep != ':')
92 				linep++;
93 		}
94 		linep++;
95 	}
96 	return (1);
97 }
98 
99 
100 static nss_status_t
101 _exec_files_XY_all(files_backend_ptr_t be,
102     nss_XbyY_args_t *argp,
103     int getby_flag)
104 {
105 	int		parse_stat = 0;
106 	int		lastlen = 0;
107 	int		exec_fd = 0;
108 	int		f_size = 0;
109 	time_t		f_time = 0;
110 	static time_t	read_time = 0;
111 	char		*first;
112 	char		*last;
113 	static char	*f_buf = NULL;
114 	struct stat	f_stat;
115 	nss_status_t	res = NSS_NOTFOUND;
116 	_priv_execattr	*_priv_exec = (_priv_execattr *)(argp->key.attrp);
117 	static rwlock_t	exec_lock;
118 
119 	if (((be->buf == NULL) &&
120 	    ((be->buf = (char *)calloc(1, be->minbuf)) == NULL)) ||
121 	    (be->filename == NULL) ||
122 	    (rw_rdlock(&exec_lock) != 0)) {
123 		return (NSS_UNAVAIL);
124 	}
125 
126 	/*
127 	 * check the size and the time stamp on the file
128 	 */
129 	if (stat(be->filename, &f_stat) != 0) {
130 		(void) _nss_files_endent(be, 0);
131 		(void) rw_unlock(&exec_lock);
132 		return (NSS_UNAVAIL);
133 	}
134 
135 	f_size = f_stat.st_size;
136 	f_time = f_stat.st_mtime;
137 
138 	while (f_time > read_time) {
139 		/*
140 		 * file has been modified since we last read it.
141 		 * read it into the buffer with rw lock.
142 		 */
143 		(void) rw_unlock(&exec_lock);
144 		if (rw_wrlock(&exec_lock) != 0) {
145 			(void) _nss_files_endent(be, 0);
146 			return (NSS_UNAVAIL);
147 		}
148 		if ((be->f = fopen(be->filename, "rF")) == 0) {
149 			(void) _nss_files_endent(be, 0);
150 			(void) rw_unlock(&exec_lock);
151 			return (NSS_UNAVAIL);
152 		}
153 		exec_fd = fileno(be->f);
154 		if (f_buf != NULL)
155 			free(f_buf);
156 		if ((f_buf = malloc(f_size)) == NULL) {
157 			(void) _nss_files_endent(be, 0);
158 			(void) rw_unlock(&exec_lock);
159 			return (NSS_UNAVAIL);
160 		}
161 		if (read(exec_fd, f_buf, f_size) < f_size) {
162 			free(f_buf);
163 			(void) _nss_files_endent(be, 0);
164 			(void) rw_unlock(&exec_lock);
165 			return (NSS_UNAVAIL);
166 		}
167 		read_time = f_time;
168 		(void) rw_unlock(&exec_lock);
169 		/*
170 		 * verify that the file did not change after
171 		 * we read it.
172 		 */
173 		if (rw_rdlock(&exec_lock) != 0) {
174 			free(f_buf);
175 			(void) _nss_files_endent(be, 0);
176 			return (NSS_UNAVAIL);
177 		}
178 		if (stat(be->filename, &f_stat) != 0) {
179 			free(f_buf);
180 			(void) _nss_files_endent(be, 0);
181 			(void) rw_unlock(&exec_lock);
182 			return (NSS_UNAVAIL);
183 		}
184 		f_size = f_stat.st_size;
185 		f_time = f_stat.st_mtime;
186 	}
187 
188 	res = NSS_NOTFOUND;
189 	/*CONSTCOND*/
190 	while (1) {
191 		int	linelen = 0;
192 		char	*instr = be->buf;
193 
194 		linelen = _readbufline(f_buf, f_size, instr, be->minbuf,
195 		    &lastlen);
196 		if (linelen < 0) {
197 			/* End of file */
198 			break;
199 		}
200 
201 		/*
202 		 * If the entry doesn't contain the filter string then
203 		 * it can't be the entry we want, so don't bother looking
204 		 * more closely at it.
205 		 */
206 		switch (getby_flag) {
207 		case NSS_DBOP_EXECATTR_BYNAME:
208 			if (strstr(instr, _priv_exec->name) == NULL)
209 				continue;
210 			break;
211 		case NSS_DBOP_EXECATTR_BYID:
212 			if (strstr(instr, _priv_exec->id) == NULL)
213 				continue;
214 			break;
215 		case NSS_DBOP_EXECATTR_BYNAMEID:
216 			if ((strstr(instr, _priv_exec->name) == NULL) ||
217 			    (strstr(instr, _priv_exec->id) == NULL))
218 				continue;
219 			break;
220 		default:
221 			break;
222 		}
223 		if ((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 (_priv_exec->search_flag == GET_ONE) {
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,
392 		NSS_LINELEN_EXECATTR,
393 		NULL));
394 }
395