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