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