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