xref: /illumos-gate/usr/src/cmd/fs.d/autofs/ns_files.c (revision 64ab3274db4bff1fa61f17076c4a40255cf17fb8)
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  *	ns_files.c
23  *
24  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <syslog.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <nsswitch.h>
34 #include <sys/stat.h>
35 #include <sys/param.h>
36 #include <rpc/rpc.h>
37 #include <rpcsvc/nfs_prot.h>
38 #include <thread.h>
39 #include <assert.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <unistd.h>
43 #include <synch.h>
44 #include <sys/types.h>
45 #include <sys/wait.h>
46 #include <strings.h>
47 #include "automount.h"
48 
49 static int read_execout(char *key, char **lp, char *fname, char *line,
50 			int linesz);
51 static int call_read_execout(char *key, char *fname, char *line, int linesz);
52 static FILE *file_open(char *, char *, char **, char ***);
53 
54 /*
55  * Initialize the stack
56  */
57 void
58 init_files(char **stack, char ***stkptr)
59 {
60 	/*
61 	 * The call is bogus for automountd since the stack is
62 	 * is more appropriately initialized in the thread-private
63 	 * routines
64 	 */
65 	if (stack == NULL && stkptr == NULL)
66 		return;
67 	(void) stack_op(INIT, NULL, stack, stkptr);
68 }
69 
70 int
71 getmapent_files(key, mapname, ml, stack, stkptr, iswildcard, isrestricted)
72 	char *key;
73 	char *mapname;
74 	struct mapline *ml;
75 	char **stack, ***stkptr;
76 	bool_t *iswildcard;
77 	bool_t isrestricted;
78 {
79 	int nserr;
80 	FILE *fp;
81 	char word[MAXPATHLEN+1], wordq[MAXPATHLEN+1];
82 	char linebuf[LINESZ], lineqbuf[LINESZ];
83 	char *lp, *lq;
84 	struct stat stbuf;
85 	char fname[MAXFILENAMELEN]; /* /etc prepended to mapname if reqd */
86 	int syntaxok = 1;
87 
88 	if (iswildcard)
89 		*iswildcard = FALSE;
90 	if ((fp = file_open(mapname, fname, stack, stkptr)) == NULL) {
91 		nserr = __NSW_UNAVAIL;
92 		goto done;
93 	}
94 
95 	if (stat(fname, &stbuf) < 0) {
96 		nserr = __NSW_UNAVAIL;
97 		goto done;
98 	}
99 
100 	/*
101 	 * If the file has its execute bit on then
102 	 * assume it's an executable map.
103 	 * Execute it and pass the key as an argument.
104 	 * Expect to get a map entry on the stdout.
105 	 * Ignore the "x" bit on restricted maps.
106 	 */
107 	if (!isrestricted && (stbuf.st_mode & S_IXUSR)) {
108 		int rc;
109 
110 		if (trace > 1) {
111 			trace_prt(1,
112 				"\tExecutable map: map=%s key=%s\n",
113 				fname, key);
114 		}
115 
116 		rc = call_read_execout(key, fname, ml->linebuf, LINESZ);
117 
118 		if (rc != 0) {
119 			nserr = __NSW_UNAVAIL;
120 			goto done;
121 		}
122 
123 		if (strlen(ml->linebuf) == 0) {
124 			nserr = __NSW_NOTFOUND;
125 			goto done;
126 		}
127 
128 		unquote(ml->linebuf, ml->lineqbuf);
129 		nserr = __NSW_SUCCESS;
130 		goto done;
131 	}
132 
133 
134 	/*
135 	 * It's just a normal map file.
136 	 * Search for the entry with the required key.
137 	 */
138 	for (;;) {
139 		lp = get_line(fp, fname, linebuf, sizeof (linebuf));
140 		if (lp == NULL) {
141 			nserr = __NSW_NOTFOUND;
142 			goto done;
143 		}
144 		if (verbose && syntaxok && isspace(*(uchar_t *)lp)) {
145 			syntaxok = 0;
146 			syslog(LOG_ERR,
147 				"leading space in map entry \"%s\" in %s",
148 				lp, mapname);
149 		}
150 		lq = lineqbuf;
151 		unquote(lp, lq);
152 		if ((getword(word, wordq, &lp, &lq, ' ', sizeof (word))
153 			== -1) || (word[0] == '\0'))
154 			continue;
155 		if (strcmp(word, key) == 0)
156 			break;
157 		if (word[0] == '*' && word[1] == '\0') {
158 			if (iswildcard)
159 				*iswildcard = TRUE;
160 			break;
161 		}
162 		if (word[0] == '+') {
163 			nserr = getmapent(key, word+1, ml, stack, stkptr,
164 						iswildcard, isrestricted);
165 			if (nserr == __NSW_SUCCESS)
166 				goto done;
167 			continue;
168 		}
169 
170 		/*
171 		 * sanity check each map entry key against
172 		 * the lookup key as the map is searched.
173 		 */
174 		if (verbose && syntaxok) { /* sanity check entry */
175 			if (*key == '/') {
176 				if (*word != '/') {
177 					syntaxok = 0;
178 					syslog(LOG_ERR,
179 					"bad key \"%s\" in direct map %s\n",
180 					word, mapname);
181 				}
182 			} else {
183 				if (strchr(word, '/')) {
184 					syntaxok = 0;
185 					syslog(LOG_ERR,
186 					"bad key \"%s\" in indirect map %s\n",
187 					word, mapname);
188 				}
189 			}
190 		}
191 	}
192 
193 	(void) strcpy(ml->linebuf, lp);
194 	(void) strcpy(ml->lineqbuf, lq);
195 	nserr = __NSW_SUCCESS;
196 done:
197 	if (fp) {
198 		(void) stack_op(POP, (char *)NULL, stack, stkptr);
199 		(void) fclose(fp);
200 	}
201 
202 
203 	return (nserr);
204 }
205 
206 int
207 getmapkeys_files(mapname, list, error, cache_time, stack, stkptr)
208 	char *mapname;
209 	struct dir_entry **list;
210 	int *error;
211 	int *cache_time;
212 	char **stack, ***stkptr;
213 {
214 	FILE *fp = NULL;
215 	char word[MAXPATHLEN+1], wordq[MAXPATHLEN+1];
216 	char linebuf[LINESZ], lineqbuf[LINESZ];
217 	char *lp, *lq;
218 	struct stat stbuf;
219 	char fname[MAXFILENAMELEN]; /* /etc prepended to mapname if reqd */
220 	int syntaxok = 1;
221 	int nserr;
222 	struct dir_entry *last = NULL;
223 
224 	if (trace > 1)
225 		trace_prt(1, "getmapkeys_files %s\n", mapname);
226 
227 	*cache_time = RDDIR_CACHE_TIME;
228 	if ((fp = file_open(mapname, fname, stack, stkptr)) == NULL) {
229 		*error = ENOENT;
230 		nserr = __NSW_UNAVAIL;
231 		goto done;
232 	}
233 	if (fseek(fp, 0L, SEEK_SET) == -1) {
234 		*error = ENOENT;
235 		nserr = __NSW_UNAVAIL;
236 		goto done;
237 	}
238 
239 	if (stat(fname, &stbuf) < 0) {
240 		*error = ENOENT;
241 		nserr = __NSW_UNAVAIL;
242 		goto done;
243 	}
244 
245 	/*
246 	 * If the file has its execute bit on then
247 	 * assume it's an executable map.
248 	 * I don't know how to list executable maps, return
249 	 * an empty map.
250 	 */
251 	if (stbuf.st_mode & S_IXUSR) {
252 		*error = 0;
253 		nserr = __NSW_SUCCESS;
254 		goto done;
255 	}
256 	/*
257 	 * It's just a normal map file.
258 	 * List entries one line at a time.
259 	 */
260 	for (;;) {
261 		lp = get_line(fp, fname, linebuf, sizeof (linebuf));
262 		if (lp == NULL) {
263 			nserr = __NSW_SUCCESS;
264 			goto done;
265 		}
266 		if (syntaxok && isspace(*(uchar_t *)lp)) {
267 			syntaxok = 0;
268 			syslog(LOG_ERR,
269 				"leading space in map entry \"%s\" in %s",
270 				lp, mapname);
271 		}
272 		lq = lineqbuf;
273 		unquote(lp, lq);
274 		if ((getword(word, wordq, &lp, &lq, ' ', MAXFILENAMELEN)
275 				== -1) || (word[0] == '\0'))
276 			continue;
277 		/*
278 		 * Wildcard entries should be ignored and this should be
279 		 * the last entry read to corroborate the search through
280 		 * files, i.e., search for key until a wildcard is reached.
281 		 */
282 		if (word[0] == '*' && word[1] == '\0')
283 			break;
284 		if (word[0] == '+') {
285 			/*
286 			 * Name switch here
287 			 */
288 			getmapkeys(word+1, list, error, cache_time,
289 				stack, stkptr, 0);
290 			/*
291 			 * the list may have been updated, therefore
292 			 * our 'last' may no longer be valid
293 			 */
294 			last = NULL;
295 			continue;
296 		}
297 
298 		if (add_dir_entry(word, list, &last) != 0) {
299 			*error = ENOMEM;
300 			goto done;
301 		}
302 		assert(last != NULL);
303 	}
304 
305 	nserr = __NSW_SUCCESS;
306 done:
307 	if (fp) {
308 		(void) stack_op(POP, (char *)NULL, stack, stkptr);
309 		(void) fclose(fp);
310 	}
311 
312 	if (*list != NULL) {
313 		/*
314 		 * list of entries found
315 		 */
316 		*error = 0;
317 	}
318 	return (nserr);
319 }
320 
321 int
322 loadmaster_files(mastermap, defopts, stack, stkptr)
323 	char *mastermap;
324 	char *defopts;
325 	char **stack, ***stkptr;
326 {
327 	FILE *fp;
328 	int done = 0;
329 	char *line, *dir, *map, *opts;
330 	char linebuf[LINESZ];
331 	char lineq[LINESZ];
332 	char fname[MAXFILENAMELEN]; /* /etc prepended to mapname if reqd */
333 
334 
335 	if ((fp = file_open(mastermap, fname, stack, stkptr)) == NULL)
336 		return (__NSW_UNAVAIL);
337 
338 	while ((line = get_line(fp, fname, linebuf,
339 				sizeof (linebuf))) != NULL) {
340 		unquote(line, lineq);
341 		if (macro_expand("", line, lineq, LINESZ)) {
342 			syslog(LOG_ERR,
343 				"map %s: line too long (max %d chars)",
344 				mastermap, LINESZ - 1);
345 			continue;
346 		}
347 		dir = line;
348 		while (*dir && isspace(*dir))
349 			dir++;
350 		if (*dir == '\0')
351 			continue;
352 		map = dir;
353 
354 		while (*map && !isspace(*map)) map++;
355 		if (*map)
356 			*map++ = '\0';
357 
358 		if (*dir == '+') {
359 			opts = map;
360 			while (*opts && isspace(*opts))
361 				opts++;
362 			if (*opts != '-')
363 				opts = defopts;
364 			else
365 				opts++;
366 			/*
367 			 * Check for no embedded blanks.
368 			 */
369 			if (strcspn(opts, " 	") == strlen(opts)) {
370 				dir++;
371 				(void) loadmaster_map(dir, opts, stack, stkptr);
372 			} else {
373 pr_msg("Warning: invalid entry for %s in %s ignored.\n", dir, fname);
374 				continue;
375 			}
376 
377 		} else {
378 			while (*map && isspace(*map))
379 				map++;
380 			if (*map == '\0')
381 				continue;
382 			opts = map;
383 			while (*opts && !isspace(*opts))
384 				opts++;
385 			if (*opts) {
386 				*opts++ = '\0';
387 				while (*opts && isspace(*opts))
388 					opts++;
389 			}
390 			if (*opts != '-')
391 				opts = defopts;
392 			else
393 				opts++;
394 			/*
395 			 * Check for no embedded blanks.
396 			 */
397 			if (strcspn(opts, " 	") == strlen(opts)) {
398 				dirinit(dir, map, opts, 0, stack, stkptr);
399 			} else {
400 pr_msg("Warning: invalid entry for %s in %s ignored.\n", dir, fname);
401 				continue;
402 			}
403 		}
404 		done++;
405 	}
406 
407 	(void) stack_op(POP, (char *)NULL, stack, stkptr);
408 	(void) fclose(fp);
409 
410 	return (done ? __NSW_SUCCESS : __NSW_NOTFOUND);
411 }
412 
413 int
414 loaddirect_files(map, local_map, opts, stack, stkptr)
415 	char *map, *local_map, *opts;
416 	char **stack, ***stkptr;
417 {
418 	FILE *fp;
419 	int done = 0;
420 	char *line, *p1, *p2;
421 	char linebuf[LINESZ];
422 	char fname[MAXFILENAMELEN]; /* /etc prepended to mapname if reqd */
423 
424 	if ((fp = file_open(map, fname, stack, stkptr)) == NULL)
425 		return (__NSW_UNAVAIL);
426 
427 	while ((line = get_line(fp, fname, linebuf,
428 				sizeof (linebuf))) != NULL) {
429 		p1 = line;
430 		while (*p1 && isspace(*p1))
431 			p1++;
432 		if (*p1 == '\0')
433 			continue;
434 		p2 = p1;
435 		while (*p2 && !isspace(*p2))
436 			p2++;
437 		*p2 = '\0';
438 		if (*p1 == '+') {
439 			p1++;
440 			(void) loaddirect_map(p1, local_map, opts, stack,
441 					stkptr);
442 		} else {
443 			dirinit(p1, local_map, opts, 1, stack, stkptr);
444 		}
445 		done++;
446 	}
447 
448 	(void) stack_op(POP, (char *)NULL, stack, stkptr);
449 	(void) fclose(fp);
450 
451 	return (done ? __NSW_SUCCESS : __NSW_NOTFOUND);
452 }
453 
454 /*
455  * This procedure opens the file and pushes it onto the
456  * the stack. Only if a file is opened successfully, is
457  * it pushed onto the stack
458  */
459 static FILE *
460 file_open(map, fname, stack, stkptr)
461 	char *map, *fname;
462 	char **stack, ***stkptr;
463 {
464 	FILE *fp;
465 
466 	if (*map != '/') {
467 		/* prepend an "/etc" */
468 		(void) strcpy(fname, "/etc/");
469 		(void) strcat(fname, map);
470 	} else
471 		(void) strcpy(fname, map);
472 
473 	fp = fopen(fname, "r");
474 
475 	if (fp != NULL) {
476 		if (!stack_op(PUSH, fname, stack, stkptr)) {
477 			(void) fclose(fp);
478 			return (NULL);
479 		}
480 	}
481 	return (fp);
482 }
483 
484 /*
485  * reimplemnted to be MT-HOT.
486  */
487 int
488 stack_op(op, name, stack, stkptr)
489 	int op;
490 	char *name;
491 	char **stack, ***stkptr;
492 {
493 	char **ptr = NULL;
494 	char **stk_top = &stack[STACKSIZ - 1];
495 
496 	/*
497 	 * the stackptr points to the next empty slot
498 	 * for PUSH: put the element and increment stkptr
499 	 * for POP: decrement stkptr and free
500 	 */
501 
502 	switch (op) {
503 	case INIT:
504 		for (ptr = stack; ptr != stk_top; ptr++)
505 			*ptr = (char *)NULL;
506 		*stkptr = stack;
507 		return (1);
508 	case ERASE:
509 		for (ptr = stack; ptr != stk_top; ptr++)
510 			if (*ptr) {
511 				if (trace > 1)
512 					trace_prt(1, "  ERASE %s\n", *ptr);
513 				free (*ptr);
514 				*ptr = (char *)NULL;
515 			}
516 		*stkptr = stack;
517 		return (1);
518 	case PUSH:
519 		if (*stkptr == stk_top)
520 			return (0);
521 		for (ptr = stack; ptr != *stkptr; ptr++)
522 			if (*ptr && (strcmp(*ptr, name) == 0)) {
523 				return (0);
524 			}
525 		if (trace > 1)
526 			trace_prt(1, "  PUSH %s\n", name);
527 		if ((**stkptr = strdup(name)) == NULL) {
528 			syslog(LOG_ERR, "stack_op: Memory alloc failed : %m");
529 			return (0);
530 		}
531 		(*stkptr)++;
532 		return (1);
533 	case POP:
534 		if (*stkptr != stack)
535 			(*stkptr)--;
536 		else
537 			syslog(LOG_ERR, "Attempt to pop empty stack\n");
538 
539 		if (*stkptr && **stkptr) {
540 			if (trace > 1)
541 				trace_prt(1, "  POP %s\n", **stkptr);
542 			free (**stkptr);
543 			**stkptr = (char *)NULL;
544 		}
545 		return (1);
546 	default:
547 		return (0);
548 	}
549 }
550 
551 #define	READ_EXECOUT_ARGS 3
552 
553 /*
554  * read_execout(char *key, char **lp, char *fname, char *line, int linesz)
555  * A simpler, multithreaded implementation of popen(). Used due to
556  * non multithreaded implementation of popen() (it calls vfork()) and a
557  * significant bug in execl().
558  * Returns 0 on OK or -1 on error.
559  */
560 static int
561 read_execout(char *key, char **lp, char *fname, char *line, int linesz)
562 {
563 	int p[2];
564 	int status = 0;
565 	int child_pid;
566 	char *args[READ_EXECOUT_ARGS];
567 	FILE *fp0;
568 
569 	if (pipe(p) < 0) {
570 		syslog(LOG_ERR, "read_execout: Cannot create pipe");
571 		return (-1);
572 	}
573 
574 	/* setup args for execv */
575 	if (((args[0] = strdup(fname)) == NULL) ||
576 		((args[1] = strdup(key)) == NULL)) {
577 		if (args[0] != NULL)
578 			free(args[0]);
579 		syslog(LOG_ERR, "read_execout: Memory allocation failed");
580 		return (-1);
581 	}
582 	args[2] = NULL;
583 
584 	if (trace > 3)
585 		trace_prt(1, "\tread_execout: forking .....\n");
586 
587 	switch ((child_pid = fork1())) {
588 	case -1:
589 		syslog(LOG_ERR, "read_execout: Cannot fork");
590 		return (-1);
591 	case 0:
592 		/*
593 		 * Child
594 		 */
595 		close(p[0]);
596 		close(1);
597 		if (fcntl(p[1], F_DUPFD, 1) != 1) {
598 			syslog(LOG_ERR,
599 			"read_execout: dup of stdout failed");
600 			_exit(-1);
601 		}
602 		close(p[1]);
603 		execv(fname, &args[0]);
604 		_exit(-1);
605 	default:
606 		/*
607 		 * Parent
608 		 */
609 		close(p[1]);
610 
611 		/*
612 		 * wait for child to complete. Note we read after the
613 		 * child exits to guarantee a full pipe.
614 		 */
615 		while (waitpid(child_pid, &status, 0) < 0) {
616 			/* if waitpid fails with EINTR, restart */
617 			if (errno != EINTR) {
618 				status = -1;
619 				break;
620 			}
621 		}
622 		if (status != -1) {
623 			if ((fp0 = fdopen(p[0], "r")) != NULL) {
624 				*lp = get_line(fp0, fname, line, linesz);
625 				fclose(fp0);
626 			} else {
627 				close(p[0]);
628 				status = -1;
629 			}
630 		} else {
631 			close(p[0]);
632 		}
633 
634 		/* free args */
635 		free(args[0]);
636 		free(args[1]);
637 
638 		if (trace > 3)
639 			trace_prt(1, "\tread_execout: map=%s key=%s line=%s\n",
640 			fname, key, line);
641 
642 		return (status);
643 	}
644 }
645 
646 void
647 automountd_do_exec_map(void *cookie, char *argp, size_t arg_size,
648     door_desc_t *dfd, uint_t n_desc)
649 {
650 	command_t	*command;
651 	char	line[LINESZ];
652 	char	*lp;
653 	int	rc;
654 
655 	command = (command_t *)argp;
656 
657 	if (sizeof (*command) != arg_size) {
658 		rc = 0;
659 		syslog(LOG_ERR, "read_execout: invalid door arguments");
660 		door_return((char *)&rc, sizeof (rc), NULL, 0);
661 	}
662 
663 	rc = read_execout(command->key, &lp, command->file, line, LINESZ);
664 
665 	if (rc != 0) {
666 		/*
667 		 * read_execout returned an error, return 0 to the door_client
668 		 * to indicate failure
669 		 */
670 		rc = 0;
671 		door_return((char *)&rc, sizeof (rc), NULL, 0);
672 	} else {
673 		door_return((char *)line, LINESZ, NULL, 0);
674 	}
675 	trace_prt(1, "automountd_do_exec_map, door return failed %s, %s\n",
676 	    command->file, strerror(errno));
677 	door_return(NULL, 0, NULL, 0);
678 }
679 
680 static int
681 call_read_execout(char *key, char *fname, char *line, int linesz)
682 {
683 	command_t command;
684 	door_arg_t darg;
685 	int ret;
686 
687 	bzero(&command, sizeof (command));
688 	(void) strlcpy(command.file, fname, MAXPATHLEN);
689 	(void) strlcpy(command.key, key, MAXOPTSLEN);
690 
691 	if (trace >= 1)
692 		trace_prt(1, "call_read_execout %s %s\n", fname, key);
693 	darg.data_ptr = (char *)&command;
694 	darg.data_size = sizeof (command);
695 	darg.desc_ptr = NULL;
696 	darg.desc_num = 0;
697 	darg.rbuf = line;
698 	darg.rsize = linesz;
699 
700 	ret = door_call(did_exec_map, &darg);
701 
702 	return (ret);
703 }
704