xref: /illumos-gate/usr/src/lib/nsswitch/files/common/getexecattr.c (revision 004388ebfdfe2ed7dfd2d153a876dfcc22d2c006)
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 typedef int (*_exec_XY_check_func) (nss_XbyY_args_t *);
56 
57 
58 /*
59  * check_match: returns 1 if matching entry found, else returns 0.
60  */
61 static int
62 check_match(nss_XbyY_args_t *argp)
63 {
64 	_priv_execattr	*_priv_exec = (_priv_execattr *)(argp->key.attrp);
65 	const char	*name = _priv_exec->name;
66 	const char	*type = _priv_exec->type;
67 	const char	*id = _priv_exec->id;
68 	const char	*policy = _priv_exec->policy;
69 	execstr_t	*exec = (execstr_t *)argp->returnval;
70 
71 	if ((policy && exec->policy && (strcmp(policy, exec->policy) != 0)) ||
72 	    (name && exec->name && (strcmp(name, exec->name) != 0)) ||
73 	    (type && exec->type && (strcmp(type, exec->type) != 0)) ||
74 	    (id && exec->id && (strcmp(id, exec->id) != 0))) {
75 		return (0);
76 	}
77 
78 	return (1);
79 }
80 
81 
82 static nss_status_t
83 _exec_files_XY_all(files_backend_ptr_t be,
84     nss_XbyY_args_t *argp,
85     int getby_flag)
86 {
87 	int		parse_stat = 0;
88 	int		lastlen = 0;
89 	int		exec_fd = 0;
90 	int		f_size = 0;
91 	time_t		f_time = 0;
92 	static time_t	read_time = 0;
93 	char		*key = NULL;
94 	char		*first;
95 	char		*last;
96 	static char	*f_buf = NULL;
97 	struct stat	f_stat;
98 	nss_status_t	res = NSS_NOTFOUND;
99 	_priv_execattr	*_priv_exec = (_priv_execattr *)(argp->key.attrp);
100 	static rwlock_t	exec_lock;
101 
102 	if (((be->buf == NULL) &&
103 	    ((be->buf = (char *)calloc(1, be->minbuf)) == NULL)) ||
104 	    (be->filename == NULL) ||
105 	    (rw_rdlock(&exec_lock) != 0)) {
106 		return (NSS_UNAVAIL);
107 	}
108 
109 	/*
110 	 * check the size and the time stamp on the file
111 	 */
112 	if (stat(be->filename, &f_stat) != 0) {
113 		(void) _nss_files_endent(be, 0);
114 		(void) rw_unlock(&exec_lock);
115 		return (NSS_UNAVAIL);
116 	}
117 
118 	f_size = f_stat.st_size;
119 	f_time = f_stat.st_mtime;
120 
121 	while (f_time > read_time) {
122 		/*
123 		 * file has been modified since we last read it.
124 		 * read it into the buffer with rw lock.
125 		 */
126 		(void) rw_unlock(&exec_lock);
127 		if (rw_wrlock(&exec_lock) != 0) {
128 			(void) _nss_files_endent(be, 0);
129 			return (NSS_UNAVAIL);
130 		}
131 		if ((be->f = fopen(be->filename, "rF")) == 0) {
132 			(void) _nss_files_endent(be, 0);
133 			(void) rw_unlock(&exec_lock);
134 			return (NSS_UNAVAIL);
135 		}
136 		exec_fd = fileno(be->f);
137 		if (f_buf != NULL)
138 			free(f_buf);
139 		if ((f_buf = malloc(f_size)) == NULL) {
140 			(void) _nss_files_endent(be, 0);
141 			(void) rw_unlock(&exec_lock);
142 			return (NSS_UNAVAIL);
143 		}
144 		if (read(exec_fd, f_buf, f_size) < f_size) {
145 			free(f_buf);
146 			(void) _nss_files_endent(be, 0);
147 			(void) rw_unlock(&exec_lock);
148 			return (NSS_UNAVAIL);
149 		}
150 		read_time = f_time;
151 		(void) rw_unlock(&exec_lock);
152 		/*
153 		 * verify that the file did not change after
154 		 * we read it.
155 		 */
156 		if (rw_rdlock(&exec_lock) != 0) {
157 			free(f_buf);
158 			(void) _nss_files_endent(be, 0);
159 			return (NSS_UNAVAIL);
160 		}
161 		if (stat(be->filename, &f_stat) != 0) {
162 			free(f_buf);
163 			(void) _nss_files_endent(be, 0);
164 			(void) rw_unlock(&exec_lock);
165 			return (NSS_UNAVAIL);
166 		}
167 		f_size = f_stat.st_size;
168 		f_time = f_stat.st_mtime;
169 	}
170 
171 	res = NSS_NOTFOUND;
172 	while (1) {
173 		int	linelen = 0;
174 		int	check_stat = 0;
175 		char	*instr = be->buf;
176 
177 		linelen = _readbufline(f_buf, f_size, instr, be->minbuf,
178 		    &lastlen);
179 		if (linelen < 0) {
180 			/* End of file */
181 			argp->erange = 0;
182 			break;
183 		}
184 
185 		/*
186 		 * If the entry doesn't contain the filter string then
187 		 * it can't be the entry we want, so don't bother looking
188 		 * more closely at it.
189 		 */
190 		switch (getby_flag) {
191 		case NSS_DBOP_EXECATTR_BYNAME:
192 			if (strstr(instr, _priv_exec->name) == NULL)
193 				continue;
194 			break;
195 		case NSS_DBOP_EXECATTR_BYID:
196 			if (strstr(instr, _priv_exec->id) == NULL)
197 				continue;
198 			break;
199 		case NSS_DBOP_EXECATTR_BYNAMEID:
200 			if ((strstr(instr, _priv_exec->name) == NULL) ||
201 			    (strstr(instr, _priv_exec->id) == NULL))
202 				continue;
203 			break;
204 		default:
205 			break;
206 		}
207 		if ((strstr(instr, _priv_exec->policy) == NULL) ||
208 		    ((_priv_exec->type != NULL) &&
209 		    (strstr(instr, _priv_exec->type) == NULL)))
210 				continue;
211 
212 		/*
213 		 * Get rid of white spaces, comments etc.
214 		 */
215 		if ((last = strchr(instr, '#')) == NULL)
216 			last = instr + linelen;
217 		*last-- = '\0';	/* Nuke '\n' or #comment */
218 		/*
219 		 * Skip leading whitespace.  Normally there isn't any,
220 		 * so it's not worth calling strspn().
221 		 */
222 		for (first = instr; isspace(*first); first++)
223 			;
224 		if (*first == '\0')
225 			continue;
226 		/*
227 		 * Found something non-blank on the line.  Skip back
228 		 * over any trailing whitespace;  since we know there's
229 		 * non-whitespace earlier in the line, checking for
230 		 * termination is easy.
231 		 */
232 		while (isspace(*last))
233 			--last;
234 		linelen = last - first + 1;
235 		if (first != instr)
236 			instr = first;
237 
238 		/*
239 		 * Parse the entry.
240 		 */
241 		argp->returnval = NULL;
242 		parse_stat = (*argp->str2ent)(instr, linelen, argp->buf.result,
243 		    argp->buf.buffer, argp->buf.buflen);
244 		if (parse_stat == NSS_STR_PARSE_SUCCESS) {
245 			argp->returnval = argp->buf.result;
246 			if (check_match(argp)) {
247 				res = NSS_SUCCESS;
248 				if (_priv_exec->search_flag == GET_ONE) {
249 					break;
250 				} else if (_doexeclist(argp) == 0) {
251 					res = NSS_UNAVAIL;
252 					break;
253 				}
254 			} else {
255 				argp->returnval = NULL;
256 				memset(argp->buf.buffer, NULL,
257 				    argp->buf.buflen);
258 			}
259 		} else if (parse_stat == NSS_STR_PARSE_ERANGE) {
260 			argp->erange = 1;
261 			break;
262 		} /* else if (parse_stat == NSS_STR_PARSE_PARSE) don't care ! */
263 	}
264 
265 	(void) _nss_files_endent(be, 0);
266 	(void) rw_unlock(&exec_lock);
267 
268 	return (res);
269 }
270 
271 
272 /*
273  * If search for exact match for id failed, get_wild checks if we have
274  * a wild-card entry for that id.
275  */
276 static nss_status_t
277 get_wild(files_backend_ptr_t be, nss_XbyY_args_t *argp, int getby_flag)
278 {
279 	char		*orig_id = NULL;
280 	char		*old_id = NULL;
281 	char		*wild_id = NULL;
282 	nss_status_t	res = NSS_NOTFOUND;
283 	_priv_execattr	*_priv_exec = (_priv_execattr *)(argp->key.attrp);
284 
285 	orig_id = strdup(_priv_exec->id);
286 	old_id = strdup(_priv_exec->id);
287 	wild_id = old_id;
288 	while ((wild_id = _exec_wild_id(wild_id, _priv_exec->type)) != NULL) {
289 		_priv_exec->id = wild_id;
290 		res = _exec_files_XY_all(be, argp, getby_flag);
291 		if (res == NSS_SUCCESS)
292 			break;
293 	}
294 	_priv_exec->id = orig_id;
295 	if (old_id)
296 		free(old_id);
297 
298 	return (res);
299 }
300 
301 
302 static nss_status_t
303 getbynam(files_backend_ptr_t be, void *a)
304 {
305 	nss_status_t	res;
306 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
307 
308 	res =  _exec_files_XY_all(be, argp, NSS_DBOP_EXECATTR_BYNAME);
309 
310 	_exec_cleanup(res, argp);
311 
312 	return (res);
313 }
314 
315 
316 static nss_status_t
317 getbyid(files_backend_ptr_t be, void *a)
318 {
319 	nss_status_t	res;
320 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
321 	_priv_execattr	*_priv_exec = (_priv_execattr *)(argp->key.attrp);
322 
323 	res = _exec_files_XY_all(be, argp, NSS_DBOP_EXECATTR_BYID);
324 
325 	if (res != NSS_SUCCESS)
326 		res = get_wild(be, argp, NSS_DBOP_EXECATTR_BYID);
327 
328 	_exec_cleanup(res, argp);
329 
330 	return (res);
331 }
332 
333 
334 static nss_status_t
335 getbynameid(files_backend_ptr_t be, void *a)
336 {
337 	nss_status_t	res;
338 	nss_XbyY_args_t	*argp = (nss_XbyY_args_t *)a;
339 	_priv_execattr	*_priv_exec = (_priv_execattr *)(argp->key.attrp);
340 
341 	res = _exec_files_XY_all(be, argp, NSS_DBOP_EXECATTR_BYNAMEID);
342 
343 	if (res != NSS_SUCCESS)
344 		res = get_wild(be, argp, NSS_DBOP_EXECATTR_BYNAMEID);
345 
346 	_exec_cleanup(res, argp);
347 
348 	return (res);
349 }
350 
351 
352 static files_backend_op_t execattr_ops[] = {
353 	_nss_files_destr,
354 	_nss_files_endent,
355 	_nss_files_setent,
356 	_nss_files_getent_netdb,
357 	getbynam,
358 	getbyid,
359 	getbynameid
360 };
361 
362 nss_backend_t  *
363 _nss_files_exec_attr_constr(const char *dummy1,
364     const char *dummy2,
365     const char *dummy3,
366     const char *dummy4,
367     const char *dummy5,
368     const char *dummy6,
369     const char *dummy7)
370 {
371 	return (_nss_files_constr(execattr_ops,
372 		sizeof (execattr_ops)/sizeof (execattr_ops[0]),
373 		EXECATTR_FILENAME,
374 		NSS_LINELEN_EXECATTR,
375 		NULL));
376 }
377