17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
58548bf79Snr * Common Development and Distribution License (the "License").
68548bf79Snr * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
227c478bd9Sstevel@tonic-gate * ns_files.c
237c478bd9Sstevel@tonic-gate *
248548bf79Snr * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
257c478bd9Sstevel@tonic-gate * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate */
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <syslog.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <ctype.h>
337c478bd9Sstevel@tonic-gate #include <nsswitch.h>
347c478bd9Sstevel@tonic-gate #include <sys/stat.h>
357c478bd9Sstevel@tonic-gate #include <sys/param.h>
367c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
377c478bd9Sstevel@tonic-gate #include <rpcsvc/nfs_prot.h>
387c478bd9Sstevel@tonic-gate #include <thread.h>
397c478bd9Sstevel@tonic-gate #include <assert.h>
407c478bd9Sstevel@tonic-gate #include <errno.h>
417c478bd9Sstevel@tonic-gate #include <fcntl.h>
427c478bd9Sstevel@tonic-gate #include <unistd.h>
437c478bd9Sstevel@tonic-gate #include <synch.h>
447c478bd9Sstevel@tonic-gate #include <sys/types.h>
457c478bd9Sstevel@tonic-gate #include <sys/wait.h>
468548bf79Snr #include <strings.h>
477c478bd9Sstevel@tonic-gate #include "automount.h"
487c478bd9Sstevel@tonic-gate
49*bfbf29e2SToomas Soome int did_exec_map;
504dc70696SToomas Soome static int read_execout(char *, char **, char *, char *, int);
514dc70696SToomas Soome static int call_read_execout(char *, char *, char *, int);
527c478bd9Sstevel@tonic-gate static FILE *file_open(char *, char *, char **, char ***);
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate /*
557c478bd9Sstevel@tonic-gate * Initialize the stack
567c478bd9Sstevel@tonic-gate */
577c478bd9Sstevel@tonic-gate void
init_files(char ** stack,char *** stkptr)587c478bd9Sstevel@tonic-gate init_files(char **stack, char ***stkptr)
597c478bd9Sstevel@tonic-gate {
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate * The call is bogus for automountd since the stack is
627c478bd9Sstevel@tonic-gate * is more appropriately initialized in the thread-private
637c478bd9Sstevel@tonic-gate * routines
647c478bd9Sstevel@tonic-gate */
657c478bd9Sstevel@tonic-gate if (stack == NULL && stkptr == NULL)
667c478bd9Sstevel@tonic-gate return;
677c478bd9Sstevel@tonic-gate (void) stack_op(INIT, NULL, stack, stkptr);
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate int
getmapent_files(char * key,char * mapname,struct mapline * ml,char ** stack,char *** stkptr,bool_t * iswildcard,bool_t isrestricted)714dc70696SToomas Soome getmapent_files(char *key, char *mapname, struct mapline *ml,
724dc70696SToomas Soome char **stack, char ***stkptr, bool_t *iswildcard, bool_t isrestricted)
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate int nserr;
757c478bd9Sstevel@tonic-gate FILE *fp;
767c478bd9Sstevel@tonic-gate char word[MAXPATHLEN+1], wordq[MAXPATHLEN+1];
777c478bd9Sstevel@tonic-gate char linebuf[LINESZ], lineqbuf[LINESZ];
787c478bd9Sstevel@tonic-gate char *lp, *lq;
797c478bd9Sstevel@tonic-gate struct stat stbuf;
807c478bd9Sstevel@tonic-gate char fname[MAXFILENAMELEN]; /* /etc prepended to mapname if reqd */
817c478bd9Sstevel@tonic-gate int syntaxok = 1;
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate if (iswildcard)
847c478bd9Sstevel@tonic-gate *iswildcard = FALSE;
857c478bd9Sstevel@tonic-gate if ((fp = file_open(mapname, fname, stack, stkptr)) == NULL) {
867c478bd9Sstevel@tonic-gate nserr = __NSW_UNAVAIL;
877c478bd9Sstevel@tonic-gate goto done;
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate if (stat(fname, &stbuf) < 0) {
917c478bd9Sstevel@tonic-gate nserr = __NSW_UNAVAIL;
927c478bd9Sstevel@tonic-gate goto done;
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate * If the file has its execute bit on then
977c478bd9Sstevel@tonic-gate * assume it's an executable map.
987c478bd9Sstevel@tonic-gate * Execute it and pass the key as an argument.
997c478bd9Sstevel@tonic-gate * Expect to get a map entry on the stdout.
1007c478bd9Sstevel@tonic-gate * Ignore the "x" bit on restricted maps.
1017c478bd9Sstevel@tonic-gate */
1027c478bd9Sstevel@tonic-gate if (!isrestricted && (stbuf.st_mode & S_IXUSR)) {
1037c478bd9Sstevel@tonic-gate int rc;
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate if (trace > 1) {
1064dc70696SToomas Soome trace_prt(1, "\tExecutable map: map=%s key=%s\n",
1074dc70696SToomas Soome fname, key);
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate
11064ab3274SToomas Soome rc = call_read_execout(key, fname, ml->linebuf, LINESZ);
1117c478bd9Sstevel@tonic-gate
1127c478bd9Sstevel@tonic-gate if (rc != 0) {
1137c478bd9Sstevel@tonic-gate nserr = __NSW_UNAVAIL;
1147c478bd9Sstevel@tonic-gate goto done;
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate
1178548bf79Snr if (strlen(ml->linebuf) == 0) {
1187c478bd9Sstevel@tonic-gate nserr = __NSW_NOTFOUND;
1197c478bd9Sstevel@tonic-gate goto done;
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate unquote(ml->linebuf, ml->lineqbuf);
1237c478bd9Sstevel@tonic-gate nserr = __NSW_SUCCESS;
1247c478bd9Sstevel@tonic-gate goto done;
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate /*
1297c478bd9Sstevel@tonic-gate * It's just a normal map file.
1307c478bd9Sstevel@tonic-gate * Search for the entry with the required key.
1317c478bd9Sstevel@tonic-gate */
1327c478bd9Sstevel@tonic-gate for (;;) {
1337c478bd9Sstevel@tonic-gate lp = get_line(fp, fname, linebuf, sizeof (linebuf));
1347c478bd9Sstevel@tonic-gate if (lp == NULL) {
1357c478bd9Sstevel@tonic-gate nserr = __NSW_NOTFOUND;
1367c478bd9Sstevel@tonic-gate goto done;
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate if (verbose && syntaxok && isspace(*(uchar_t *)lp)) {
1397c478bd9Sstevel@tonic-gate syntaxok = 0;
1407c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
1414dc70696SToomas Soome "leading space in map entry \"%s\" in %s",
1424dc70696SToomas Soome lp, mapname);
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate lq = lineqbuf;
1457c478bd9Sstevel@tonic-gate unquote(lp, lq);
1467c478bd9Sstevel@tonic-gate if ((getword(word, wordq, &lp, &lq, ' ', sizeof (word))
1474dc70696SToomas Soome == -1) || (word[0] == '\0'))
1487c478bd9Sstevel@tonic-gate continue;
1497c478bd9Sstevel@tonic-gate if (strcmp(word, key) == 0)
1507c478bd9Sstevel@tonic-gate break;
1517c478bd9Sstevel@tonic-gate if (word[0] == '*' && word[1] == '\0') {
1527c478bd9Sstevel@tonic-gate if (iswildcard)
1537c478bd9Sstevel@tonic-gate *iswildcard = TRUE;
1547c478bd9Sstevel@tonic-gate break;
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate if (word[0] == '+') {
1577c478bd9Sstevel@tonic-gate nserr = getmapent(key, word+1, ml, stack, stkptr,
1584dc70696SToomas Soome iswildcard, isrestricted);
1597c478bd9Sstevel@tonic-gate if (nserr == __NSW_SUCCESS)
1607c478bd9Sstevel@tonic-gate goto done;
1617c478bd9Sstevel@tonic-gate continue;
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate
1647c478bd9Sstevel@tonic-gate /*
1657c478bd9Sstevel@tonic-gate * sanity check each map entry key against
1667c478bd9Sstevel@tonic-gate * the lookup key as the map is searched.
1677c478bd9Sstevel@tonic-gate */
1687c478bd9Sstevel@tonic-gate if (verbose && syntaxok) { /* sanity check entry */
1697c478bd9Sstevel@tonic-gate if (*key == '/') {
1707c478bd9Sstevel@tonic-gate if (*word != '/') {
1717c478bd9Sstevel@tonic-gate syntaxok = 0;
1724dc70696SToomas Soome syslog(LOG_ERR, "bad key \"%s\" in "
1734dc70696SToomas Soome "direct map %s\n", word, mapname);
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate } else {
1767c478bd9Sstevel@tonic-gate if (strchr(word, '/')) {
1777c478bd9Sstevel@tonic-gate syntaxok = 0;
1784dc70696SToomas Soome syslog(LOG_ERR, "bad key \"%s\" in "
1794dc70696SToomas Soome "indirect map %s\n", word, mapname);
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate
1857c478bd9Sstevel@tonic-gate (void) strcpy(ml->linebuf, lp);
1867c478bd9Sstevel@tonic-gate (void) strcpy(ml->lineqbuf, lq);
1877c478bd9Sstevel@tonic-gate nserr = __NSW_SUCCESS;
1887c478bd9Sstevel@tonic-gate done:
1897c478bd9Sstevel@tonic-gate if (fp) {
1907c478bd9Sstevel@tonic-gate (void) stack_op(POP, (char *)NULL, stack, stkptr);
1917c478bd9Sstevel@tonic-gate (void) fclose(fp);
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate
1947c478bd9Sstevel@tonic-gate
1957c478bd9Sstevel@tonic-gate return (nserr);
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate int
getmapkeys_files(char * mapname,struct dir_entry ** list,int * error,int * cache_time,char ** stack,char *** stkptr)1994dc70696SToomas Soome getmapkeys_files(char *mapname, struct dir_entry **list, int *error,
2004dc70696SToomas Soome int *cache_time, char **stack, char ***stkptr)
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate FILE *fp = NULL;
2037c478bd9Sstevel@tonic-gate char word[MAXPATHLEN+1], wordq[MAXPATHLEN+1];
2047c478bd9Sstevel@tonic-gate char linebuf[LINESZ], lineqbuf[LINESZ];
2057c478bd9Sstevel@tonic-gate char *lp, *lq;
2067c478bd9Sstevel@tonic-gate struct stat stbuf;
2077c478bd9Sstevel@tonic-gate char fname[MAXFILENAMELEN]; /* /etc prepended to mapname if reqd */
2087c478bd9Sstevel@tonic-gate int syntaxok = 1;
2097c478bd9Sstevel@tonic-gate int nserr;
2107c478bd9Sstevel@tonic-gate struct dir_entry *last = NULL;
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate if (trace > 1)
2137c478bd9Sstevel@tonic-gate trace_prt(1, "getmapkeys_files %s\n", mapname);
2147c478bd9Sstevel@tonic-gate
2157c478bd9Sstevel@tonic-gate *cache_time = RDDIR_CACHE_TIME;
2167c478bd9Sstevel@tonic-gate if ((fp = file_open(mapname, fname, stack, stkptr)) == NULL) {
2177c478bd9Sstevel@tonic-gate *error = ENOENT;
2187c478bd9Sstevel@tonic-gate nserr = __NSW_UNAVAIL;
2197c478bd9Sstevel@tonic-gate goto done;
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate if (fseek(fp, 0L, SEEK_SET) == -1) {
2227c478bd9Sstevel@tonic-gate *error = ENOENT;
2237c478bd9Sstevel@tonic-gate nserr = __NSW_UNAVAIL;
2247c478bd9Sstevel@tonic-gate goto done;
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate
2277c478bd9Sstevel@tonic-gate if (stat(fname, &stbuf) < 0) {
2287c478bd9Sstevel@tonic-gate *error = ENOENT;
2297c478bd9Sstevel@tonic-gate nserr = __NSW_UNAVAIL;
2307c478bd9Sstevel@tonic-gate goto done;
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate
2337c478bd9Sstevel@tonic-gate /*
2347c478bd9Sstevel@tonic-gate * If the file has its execute bit on then
2357c478bd9Sstevel@tonic-gate * assume it's an executable map.
2367c478bd9Sstevel@tonic-gate * I don't know how to list executable maps, return
2377c478bd9Sstevel@tonic-gate * an empty map.
2387c478bd9Sstevel@tonic-gate */
2397c478bd9Sstevel@tonic-gate if (stbuf.st_mode & S_IXUSR) {
2407c478bd9Sstevel@tonic-gate *error = 0;
2417c478bd9Sstevel@tonic-gate nserr = __NSW_SUCCESS;
2427c478bd9Sstevel@tonic-gate goto done;
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate /*
2457c478bd9Sstevel@tonic-gate * It's just a normal map file.
2467c478bd9Sstevel@tonic-gate * List entries one line at a time.
2477c478bd9Sstevel@tonic-gate */
2487c478bd9Sstevel@tonic-gate for (;;) {
2497c478bd9Sstevel@tonic-gate lp = get_line(fp, fname, linebuf, sizeof (linebuf));
2507c478bd9Sstevel@tonic-gate if (lp == NULL) {
2517c478bd9Sstevel@tonic-gate nserr = __NSW_SUCCESS;
2527c478bd9Sstevel@tonic-gate goto done;
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate if (syntaxok && isspace(*(uchar_t *)lp)) {
2557c478bd9Sstevel@tonic-gate syntaxok = 0;
2567c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
2574dc70696SToomas Soome "leading space in map entry \"%s\" in %s",
2584dc70696SToomas Soome lp, mapname);
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate lq = lineqbuf;
2617c478bd9Sstevel@tonic-gate unquote(lp, lq);
2627c478bd9Sstevel@tonic-gate if ((getword(word, wordq, &lp, &lq, ' ', MAXFILENAMELEN)
2634dc70696SToomas Soome == -1) || (word[0] == '\0'))
2647c478bd9Sstevel@tonic-gate continue;
2657c478bd9Sstevel@tonic-gate /*
2667c478bd9Sstevel@tonic-gate * Wildcard entries should be ignored and this should be
2677c478bd9Sstevel@tonic-gate * the last entry read to corroborate the search through
2687c478bd9Sstevel@tonic-gate * files, i.e., search for key until a wildcard is reached.
2697c478bd9Sstevel@tonic-gate */
2707c478bd9Sstevel@tonic-gate if (word[0] == '*' && word[1] == '\0')
2717c478bd9Sstevel@tonic-gate break;
2727c478bd9Sstevel@tonic-gate if (word[0] == '+') {
2737c478bd9Sstevel@tonic-gate /*
2747c478bd9Sstevel@tonic-gate * Name switch here
2757c478bd9Sstevel@tonic-gate */
2767c478bd9Sstevel@tonic-gate getmapkeys(word+1, list, error, cache_time,
2774dc70696SToomas Soome stack, stkptr, 0);
2787c478bd9Sstevel@tonic-gate /*
2797c478bd9Sstevel@tonic-gate * the list may have been updated, therefore
2807c478bd9Sstevel@tonic-gate * our 'last' may no longer be valid
2817c478bd9Sstevel@tonic-gate */
2827c478bd9Sstevel@tonic-gate last = NULL;
2837c478bd9Sstevel@tonic-gate continue;
2847c478bd9Sstevel@tonic-gate }
2857c478bd9Sstevel@tonic-gate
2867c478bd9Sstevel@tonic-gate if (add_dir_entry(word, list, &last) != 0) {
2877c478bd9Sstevel@tonic-gate *error = ENOMEM;
2887c478bd9Sstevel@tonic-gate goto done;
2897c478bd9Sstevel@tonic-gate }
2907c478bd9Sstevel@tonic-gate assert(last != NULL);
2917c478bd9Sstevel@tonic-gate }
2927c478bd9Sstevel@tonic-gate
2937c478bd9Sstevel@tonic-gate nserr = __NSW_SUCCESS;
2947c478bd9Sstevel@tonic-gate done:
2957c478bd9Sstevel@tonic-gate if (fp) {
2967c478bd9Sstevel@tonic-gate (void) stack_op(POP, (char *)NULL, stack, stkptr);
2977c478bd9Sstevel@tonic-gate (void) fclose(fp);
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate
3007c478bd9Sstevel@tonic-gate if (*list != NULL) {
3017c478bd9Sstevel@tonic-gate /*
3027c478bd9Sstevel@tonic-gate * list of entries found
3037c478bd9Sstevel@tonic-gate */
3047c478bd9Sstevel@tonic-gate *error = 0;
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate return (nserr);
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate
30911606941Sjwahlig int
loadmaster_files(char * mastermap,char * defopts,char ** stack,char *** stkptr)3104dc70696SToomas Soome loadmaster_files(char *mastermap, char *defopts, char **stack, char ***stkptr)
3117c478bd9Sstevel@tonic-gate {
3127c478bd9Sstevel@tonic-gate FILE *fp;
3137c478bd9Sstevel@tonic-gate int done = 0;
3147c478bd9Sstevel@tonic-gate char *line, *dir, *map, *opts;
3157c478bd9Sstevel@tonic-gate char linebuf[LINESZ];
3167c478bd9Sstevel@tonic-gate char lineq[LINESZ];
3177c478bd9Sstevel@tonic-gate char fname[MAXFILENAMELEN]; /* /etc prepended to mapname if reqd */
3187c478bd9Sstevel@tonic-gate
3197c478bd9Sstevel@tonic-gate
3207c478bd9Sstevel@tonic-gate if ((fp = file_open(mastermap, fname, stack, stkptr)) == NULL)
3217c478bd9Sstevel@tonic-gate return (__NSW_UNAVAIL);
3227c478bd9Sstevel@tonic-gate
3237c478bd9Sstevel@tonic-gate while ((line = get_line(fp, fname, linebuf,
3244dc70696SToomas Soome sizeof (linebuf))) != NULL) {
3257c478bd9Sstevel@tonic-gate unquote(line, lineq);
3267c478bd9Sstevel@tonic-gate if (macro_expand("", line, lineq, LINESZ)) {
3274dc70696SToomas Soome syslog(LOG_ERR, "map %s: line too long (max %d chars)",
3284dc70696SToomas Soome mastermap, LINESZ - 1);
3297c478bd9Sstevel@tonic-gate continue;
3307c478bd9Sstevel@tonic-gate }
3317c478bd9Sstevel@tonic-gate dir = line;
3327c478bd9Sstevel@tonic-gate while (*dir && isspace(*dir))
3337c478bd9Sstevel@tonic-gate dir++;
3347c478bd9Sstevel@tonic-gate if (*dir == '\0')
3357c478bd9Sstevel@tonic-gate continue;
3367c478bd9Sstevel@tonic-gate map = dir;
3377c478bd9Sstevel@tonic-gate
3387c478bd9Sstevel@tonic-gate while (*map && !isspace(*map)) map++;
3397c478bd9Sstevel@tonic-gate if (*map)
3407c478bd9Sstevel@tonic-gate *map++ = '\0';
3417c478bd9Sstevel@tonic-gate
3427c478bd9Sstevel@tonic-gate if (*dir == '+') {
3437c478bd9Sstevel@tonic-gate opts = map;
3447c478bd9Sstevel@tonic-gate while (*opts && isspace(*opts))
3457c478bd9Sstevel@tonic-gate opts++;
3467c478bd9Sstevel@tonic-gate if (*opts != '-')
3477c478bd9Sstevel@tonic-gate opts = defopts;
3487c478bd9Sstevel@tonic-gate else
3497c478bd9Sstevel@tonic-gate opts++;
3507c478bd9Sstevel@tonic-gate /*
3517c478bd9Sstevel@tonic-gate * Check for no embedded blanks.
3527c478bd9Sstevel@tonic-gate */
353*bfbf29e2SToomas Soome if (strcspn(opts, " \t") == strlen(opts)) {
3547c478bd9Sstevel@tonic-gate dir++;
3557c478bd9Sstevel@tonic-gate (void) loadmaster_map(dir, opts, stack, stkptr);
3567c478bd9Sstevel@tonic-gate } else {
3577c478bd9Sstevel@tonic-gate pr_msg("Warning: invalid entry for %s in %s ignored.\n", dir, fname);
3587c478bd9Sstevel@tonic-gate continue;
3597c478bd9Sstevel@tonic-gate }
3607c478bd9Sstevel@tonic-gate
3617c478bd9Sstevel@tonic-gate } else {
3627c478bd9Sstevel@tonic-gate while (*map && isspace(*map))
3637c478bd9Sstevel@tonic-gate map++;
3647c478bd9Sstevel@tonic-gate if (*map == '\0')
3657c478bd9Sstevel@tonic-gate continue;
3667c478bd9Sstevel@tonic-gate opts = map;
3677c478bd9Sstevel@tonic-gate while (*opts && !isspace(*opts))
3687c478bd9Sstevel@tonic-gate opts++;
3697c478bd9Sstevel@tonic-gate if (*opts) {
3707c478bd9Sstevel@tonic-gate *opts++ = '\0';
3717c478bd9Sstevel@tonic-gate while (*opts && isspace(*opts))
3727c478bd9Sstevel@tonic-gate opts++;
3737c478bd9Sstevel@tonic-gate }
3747c478bd9Sstevel@tonic-gate if (*opts != '-')
3757c478bd9Sstevel@tonic-gate opts = defopts;
3767c478bd9Sstevel@tonic-gate else
3777c478bd9Sstevel@tonic-gate opts++;
3787c478bd9Sstevel@tonic-gate /*
3797c478bd9Sstevel@tonic-gate * Check for no embedded blanks.
3807c478bd9Sstevel@tonic-gate */
381*bfbf29e2SToomas Soome if (strcspn(opts, " \t") == strlen(opts)) {
3827c478bd9Sstevel@tonic-gate dirinit(dir, map, opts, 0, stack, stkptr);
3837c478bd9Sstevel@tonic-gate } else {
3847c478bd9Sstevel@tonic-gate pr_msg("Warning: invalid entry for %s in %s ignored.\n", dir, fname);
3857c478bd9Sstevel@tonic-gate continue;
3867c478bd9Sstevel@tonic-gate }
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate done++;
3897c478bd9Sstevel@tonic-gate }
3907c478bd9Sstevel@tonic-gate
3917c478bd9Sstevel@tonic-gate (void) stack_op(POP, (char *)NULL, stack, stkptr);
3927c478bd9Sstevel@tonic-gate (void) fclose(fp);
3937c478bd9Sstevel@tonic-gate
3947c478bd9Sstevel@tonic-gate return (done ? __NSW_SUCCESS : __NSW_NOTFOUND);
3957c478bd9Sstevel@tonic-gate }
3967c478bd9Sstevel@tonic-gate
39711606941Sjwahlig int
loaddirect_files(char * map,char * local_map,char * opts,char ** stack,char *** stkptr)3984dc70696SToomas Soome loaddirect_files(char *map, char *local_map, char *opts,
3994dc70696SToomas Soome char **stack, char ***stkptr)
4007c478bd9Sstevel@tonic-gate {
4017c478bd9Sstevel@tonic-gate FILE *fp;
4027c478bd9Sstevel@tonic-gate int done = 0;
4037c478bd9Sstevel@tonic-gate char *line, *p1, *p2;
4047c478bd9Sstevel@tonic-gate char linebuf[LINESZ];
4057c478bd9Sstevel@tonic-gate char fname[MAXFILENAMELEN]; /* /etc prepended to mapname if reqd */
4067c478bd9Sstevel@tonic-gate
4077c478bd9Sstevel@tonic-gate if ((fp = file_open(map, fname, stack, stkptr)) == NULL)
4087c478bd9Sstevel@tonic-gate return (__NSW_UNAVAIL);
4097c478bd9Sstevel@tonic-gate
4107c478bd9Sstevel@tonic-gate while ((line = get_line(fp, fname, linebuf,
4114dc70696SToomas Soome sizeof (linebuf))) != NULL) {
4127c478bd9Sstevel@tonic-gate p1 = line;
4137c478bd9Sstevel@tonic-gate while (*p1 && isspace(*p1))
4147c478bd9Sstevel@tonic-gate p1++;
4157c478bd9Sstevel@tonic-gate if (*p1 == '\0')
4167c478bd9Sstevel@tonic-gate continue;
4177c478bd9Sstevel@tonic-gate p2 = p1;
4187c478bd9Sstevel@tonic-gate while (*p2 && !isspace(*p2))
4197c478bd9Sstevel@tonic-gate p2++;
4207c478bd9Sstevel@tonic-gate *p2 = '\0';
4217c478bd9Sstevel@tonic-gate if (*p1 == '+') {
4227c478bd9Sstevel@tonic-gate p1++;
4237c478bd9Sstevel@tonic-gate (void) loaddirect_map(p1, local_map, opts, stack,
4244dc70696SToomas Soome stkptr);
4257c478bd9Sstevel@tonic-gate } else {
4267c478bd9Sstevel@tonic-gate dirinit(p1, local_map, opts, 1, stack, stkptr);
4277c478bd9Sstevel@tonic-gate }
4287c478bd9Sstevel@tonic-gate done++;
4297c478bd9Sstevel@tonic-gate }
4307c478bd9Sstevel@tonic-gate
4317c478bd9Sstevel@tonic-gate (void) stack_op(POP, (char *)NULL, stack, stkptr);
4327c478bd9Sstevel@tonic-gate (void) fclose(fp);
4337c478bd9Sstevel@tonic-gate
4347c478bd9Sstevel@tonic-gate return (done ? __NSW_SUCCESS : __NSW_NOTFOUND);
4357c478bd9Sstevel@tonic-gate }
4367c478bd9Sstevel@tonic-gate
4377c478bd9Sstevel@tonic-gate /*
4387c478bd9Sstevel@tonic-gate * This procedure opens the file and pushes it onto the
4397c478bd9Sstevel@tonic-gate * the stack. Only if a file is opened successfully, is
4407c478bd9Sstevel@tonic-gate * it pushed onto the stack
4417c478bd9Sstevel@tonic-gate */
4427c478bd9Sstevel@tonic-gate static FILE *
file_open(char * map,char * fname,char ** stack,char *** stkptr)4434dc70696SToomas Soome file_open(char *map, char *fname, char **stack, char ***stkptr)
4447c478bd9Sstevel@tonic-gate {
4457c478bd9Sstevel@tonic-gate FILE *fp;
4467c478bd9Sstevel@tonic-gate
4477c478bd9Sstevel@tonic-gate if (*map != '/') {
4487c478bd9Sstevel@tonic-gate /* prepend an "/etc" */
4497c478bd9Sstevel@tonic-gate (void) strcpy(fname, "/etc/");
4507c478bd9Sstevel@tonic-gate (void) strcat(fname, map);
4514dc70696SToomas Soome } else {
4527c478bd9Sstevel@tonic-gate (void) strcpy(fname, map);
4534dc70696SToomas Soome }
4547c478bd9Sstevel@tonic-gate
4557c478bd9Sstevel@tonic-gate fp = fopen(fname, "r");
4567c478bd9Sstevel@tonic-gate
4577c478bd9Sstevel@tonic-gate if (fp != NULL) {
4587c478bd9Sstevel@tonic-gate if (!stack_op(PUSH, fname, stack, stkptr)) {
4597c478bd9Sstevel@tonic-gate (void) fclose(fp);
4607c478bd9Sstevel@tonic-gate return (NULL);
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate }
4637c478bd9Sstevel@tonic-gate return (fp);
4647c478bd9Sstevel@tonic-gate }
4657c478bd9Sstevel@tonic-gate
4667c478bd9Sstevel@tonic-gate /*
4677c478bd9Sstevel@tonic-gate * reimplemnted to be MT-HOT.
4687c478bd9Sstevel@tonic-gate */
4697c478bd9Sstevel@tonic-gate int
stack_op(int op,char * name,char ** stack,char *** stkptr)4704dc70696SToomas Soome stack_op(int op, char *name, char **stack, char ***stkptr)
4717c478bd9Sstevel@tonic-gate {
4727c478bd9Sstevel@tonic-gate char **ptr = NULL;
4737c478bd9Sstevel@tonic-gate char **stk_top = &stack[STACKSIZ - 1];
4747c478bd9Sstevel@tonic-gate
4757c478bd9Sstevel@tonic-gate /*
4767c478bd9Sstevel@tonic-gate * the stackptr points to the next empty slot
4777c478bd9Sstevel@tonic-gate * for PUSH: put the element and increment stkptr
4787c478bd9Sstevel@tonic-gate * for POP: decrement stkptr and free
4797c478bd9Sstevel@tonic-gate */
4807c478bd9Sstevel@tonic-gate
4817c478bd9Sstevel@tonic-gate switch (op) {
4827c478bd9Sstevel@tonic-gate case INIT:
4837c478bd9Sstevel@tonic-gate for (ptr = stack; ptr != stk_top; ptr++)
4847c478bd9Sstevel@tonic-gate *ptr = (char *)NULL;
4857c478bd9Sstevel@tonic-gate *stkptr = stack;
4867c478bd9Sstevel@tonic-gate return (1);
4877c478bd9Sstevel@tonic-gate case ERASE:
4887c478bd9Sstevel@tonic-gate for (ptr = stack; ptr != stk_top; ptr++)
4897c478bd9Sstevel@tonic-gate if (*ptr) {
4907c478bd9Sstevel@tonic-gate if (trace > 1)
4917c478bd9Sstevel@tonic-gate trace_prt(1, " ERASE %s\n", *ptr);
4927c478bd9Sstevel@tonic-gate free (*ptr);
4937c478bd9Sstevel@tonic-gate *ptr = (char *)NULL;
4947c478bd9Sstevel@tonic-gate }
4957c478bd9Sstevel@tonic-gate *stkptr = stack;
4967c478bd9Sstevel@tonic-gate return (1);
4977c478bd9Sstevel@tonic-gate case PUSH:
4987c478bd9Sstevel@tonic-gate if (*stkptr == stk_top)
4997c478bd9Sstevel@tonic-gate return (0);
5007c478bd9Sstevel@tonic-gate for (ptr = stack; ptr != *stkptr; ptr++)
5017c478bd9Sstevel@tonic-gate if (*ptr && (strcmp(*ptr, name) == 0)) {
5027c478bd9Sstevel@tonic-gate return (0);
5037c478bd9Sstevel@tonic-gate }
5047c478bd9Sstevel@tonic-gate if (trace > 1)
5057c478bd9Sstevel@tonic-gate trace_prt(1, " PUSH %s\n", name);
5067c478bd9Sstevel@tonic-gate if ((**stkptr = strdup(name)) == NULL) {
5077c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "stack_op: Memory alloc failed : %m");
5087c478bd9Sstevel@tonic-gate return (0);
5097c478bd9Sstevel@tonic-gate }
5107c478bd9Sstevel@tonic-gate (*stkptr)++;
5117c478bd9Sstevel@tonic-gate return (1);
5127c478bd9Sstevel@tonic-gate case POP:
5137c478bd9Sstevel@tonic-gate if (*stkptr != stack)
5147c478bd9Sstevel@tonic-gate (*stkptr)--;
5157c478bd9Sstevel@tonic-gate else
5167c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "Attempt to pop empty stack\n");
5177c478bd9Sstevel@tonic-gate
5187c478bd9Sstevel@tonic-gate if (*stkptr && **stkptr) {
5197c478bd9Sstevel@tonic-gate if (trace > 1)
5207c478bd9Sstevel@tonic-gate trace_prt(1, " POP %s\n", **stkptr);
5217c478bd9Sstevel@tonic-gate free (**stkptr);
5227c478bd9Sstevel@tonic-gate **stkptr = (char *)NULL;
5237c478bd9Sstevel@tonic-gate }
5247c478bd9Sstevel@tonic-gate return (1);
5257c478bd9Sstevel@tonic-gate default:
5267c478bd9Sstevel@tonic-gate return (0);
5277c478bd9Sstevel@tonic-gate }
5287c478bd9Sstevel@tonic-gate }
5297c478bd9Sstevel@tonic-gate
5307c478bd9Sstevel@tonic-gate #define READ_EXECOUT_ARGS 3
5317c478bd9Sstevel@tonic-gate
5327c478bd9Sstevel@tonic-gate /*
5337c478bd9Sstevel@tonic-gate * read_execout(char *key, char **lp, char *fname, char *line, int linesz)
5347c478bd9Sstevel@tonic-gate * A simpler, multithreaded implementation of popen(). Used due to
5357c478bd9Sstevel@tonic-gate * non multithreaded implementation of popen() (it calls vfork()) and a
5367c478bd9Sstevel@tonic-gate * significant bug in execl().
5377c478bd9Sstevel@tonic-gate * Returns 0 on OK or -1 on error.
5387c478bd9Sstevel@tonic-gate */
5397c478bd9Sstevel@tonic-gate static int
read_execout(char * key,char ** lp,char * fname,char * line,int linesz)5407c478bd9Sstevel@tonic-gate read_execout(char *key, char **lp, char *fname, char *line, int linesz)
5417c478bd9Sstevel@tonic-gate {
5427c478bd9Sstevel@tonic-gate int p[2];
5437c478bd9Sstevel@tonic-gate int status = 0;
5447c478bd9Sstevel@tonic-gate int child_pid;
5457c478bd9Sstevel@tonic-gate char *args[READ_EXECOUT_ARGS];
5467c478bd9Sstevel@tonic-gate FILE *fp0;
5477c478bd9Sstevel@tonic-gate
5487c478bd9Sstevel@tonic-gate if (pipe(p) < 0) {
5497c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "read_execout: Cannot create pipe");
5507c478bd9Sstevel@tonic-gate return (-1);
5517c478bd9Sstevel@tonic-gate }
5527c478bd9Sstevel@tonic-gate
5537c478bd9Sstevel@tonic-gate /* setup args for execv */
5547c478bd9Sstevel@tonic-gate if (((args[0] = strdup(fname)) == NULL) ||
5554dc70696SToomas Soome ((args[1] = strdup(key)) == NULL)) {
5567c478bd9Sstevel@tonic-gate if (args[0] != NULL)
5577c478bd9Sstevel@tonic-gate free(args[0]);
5587c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "read_execout: Memory allocation failed");
5597c478bd9Sstevel@tonic-gate return (-1);
5607c478bd9Sstevel@tonic-gate }
5617c478bd9Sstevel@tonic-gate args[2] = NULL;
5627c478bd9Sstevel@tonic-gate
5637c478bd9Sstevel@tonic-gate if (trace > 3)
5647c478bd9Sstevel@tonic-gate trace_prt(1, "\tread_execout: forking .....\n");
5657c478bd9Sstevel@tonic-gate
5667c478bd9Sstevel@tonic-gate switch ((child_pid = fork1())) {
5677c478bd9Sstevel@tonic-gate case -1:
5687c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "read_execout: Cannot fork");
5697c478bd9Sstevel@tonic-gate return (-1);
5707c478bd9Sstevel@tonic-gate case 0:
5717c478bd9Sstevel@tonic-gate /*
5727c478bd9Sstevel@tonic-gate * Child
5737c478bd9Sstevel@tonic-gate */
5747c478bd9Sstevel@tonic-gate close(p[0]);
5757c478bd9Sstevel@tonic-gate close(1);
5767c478bd9Sstevel@tonic-gate if (fcntl(p[1], F_DUPFD, 1) != 1) {
5777c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
5787c478bd9Sstevel@tonic-gate "read_execout: dup of stdout failed");
5797c478bd9Sstevel@tonic-gate _exit(-1);
5807c478bd9Sstevel@tonic-gate }
5817c478bd9Sstevel@tonic-gate close(p[1]);
5827c478bd9Sstevel@tonic-gate execv(fname, &args[0]);
5837c478bd9Sstevel@tonic-gate _exit(-1);
5847c478bd9Sstevel@tonic-gate default:
5857c478bd9Sstevel@tonic-gate /*
5867c478bd9Sstevel@tonic-gate * Parent
5877c478bd9Sstevel@tonic-gate */
5887c478bd9Sstevel@tonic-gate close(p[1]);
5897c478bd9Sstevel@tonic-gate
5907c478bd9Sstevel@tonic-gate /*
5917c478bd9Sstevel@tonic-gate * wait for child to complete. Note we read after the
5927c478bd9Sstevel@tonic-gate * child exits to guarantee a full pipe.
5937c478bd9Sstevel@tonic-gate */
5947c478bd9Sstevel@tonic-gate while (waitpid(child_pid, &status, 0) < 0) {
5957c478bd9Sstevel@tonic-gate /* if waitpid fails with EINTR, restart */
5967c478bd9Sstevel@tonic-gate if (errno != EINTR) {
5977c478bd9Sstevel@tonic-gate status = -1;
5987c478bd9Sstevel@tonic-gate break;
5997c478bd9Sstevel@tonic-gate }
6007c478bd9Sstevel@tonic-gate }
6017c478bd9Sstevel@tonic-gate if (status != -1) {
6027c478bd9Sstevel@tonic-gate if ((fp0 = fdopen(p[0], "r")) != NULL) {
6037c478bd9Sstevel@tonic-gate *lp = get_line(fp0, fname, line, linesz);
6047c478bd9Sstevel@tonic-gate fclose(fp0);
6057c478bd9Sstevel@tonic-gate } else {
6067c478bd9Sstevel@tonic-gate close(p[0]);
6077c478bd9Sstevel@tonic-gate status = -1;
6087c478bd9Sstevel@tonic-gate }
6097c478bd9Sstevel@tonic-gate } else {
6107c478bd9Sstevel@tonic-gate close(p[0]);
6117c478bd9Sstevel@tonic-gate }
6127c478bd9Sstevel@tonic-gate
6137c478bd9Sstevel@tonic-gate /* free args */
6147c478bd9Sstevel@tonic-gate free(args[0]);
6157c478bd9Sstevel@tonic-gate free(args[1]);
6167c478bd9Sstevel@tonic-gate
6174dc70696SToomas Soome if (trace > 3) {
6187c478bd9Sstevel@tonic-gate trace_prt(1, "\tread_execout: map=%s key=%s line=%s\n",
6194dc70696SToomas Soome fname, key, line);
6204dc70696SToomas Soome }
6217c478bd9Sstevel@tonic-gate
6227c478bd9Sstevel@tonic-gate return (status);
6237c478bd9Sstevel@tonic-gate }
6247c478bd9Sstevel@tonic-gate }
6258548bf79Snr
6268548bf79Snr void
automountd_do_exec_map(void * cookie,char * argp,size_t arg_size,door_desc_t * dfd,uint_t n_desc)6278548bf79Snr automountd_do_exec_map(void *cookie, char *argp, size_t arg_size,
62864ab3274SToomas Soome door_desc_t *dfd, uint_t n_desc)
6298548bf79Snr {
6308548bf79Snr command_t *command;
6318548bf79Snr char line[LINESZ];
6328548bf79Snr char *lp;
6338548bf79Snr int rc;
6348548bf79Snr
6358548bf79Snr command = (command_t *)argp;
6368548bf79Snr
6378548bf79Snr if (sizeof (*command) != arg_size) {
6388548bf79Snr rc = 0;
6398548bf79Snr syslog(LOG_ERR, "read_execout: invalid door arguments");
6408548bf79Snr door_return((char *)&rc, sizeof (rc), NULL, 0);
6418548bf79Snr }
6428548bf79Snr
6438548bf79Snr rc = read_execout(command->key, &lp, command->file, line, LINESZ);
6448548bf79Snr
6458548bf79Snr if (rc != 0) {
6468548bf79Snr /*
6478548bf79Snr * read_execout returned an error, return 0 to the door_client
6488548bf79Snr * to indicate failure
6498548bf79Snr */
6508548bf79Snr rc = 0;
6518548bf79Snr door_return((char *)&rc, sizeof (rc), NULL, 0);
6528548bf79Snr } else {
6538548bf79Snr door_return((char *)line, LINESZ, NULL, 0);
6548548bf79Snr }
6558548bf79Snr trace_prt(1, "automountd_do_exec_map, door return failed %s, %s\n",
6568548bf79Snr command->file, strerror(errno));
6578548bf79Snr door_return(NULL, 0, NULL, 0);
6588548bf79Snr }
6598548bf79Snr
66064ab3274SToomas Soome static int
call_read_execout(char * key,char * fname,char * line,int linesz)66164ab3274SToomas Soome call_read_execout(char *key, char *fname, char *line, int linesz)
6628548bf79Snr {
6638548bf79Snr command_t command;
6648548bf79Snr door_arg_t darg;
6658548bf79Snr int ret;
6668548bf79Snr
6678548bf79Snr bzero(&command, sizeof (command));
6688548bf79Snr (void) strlcpy(command.file, fname, MAXPATHLEN);
6698548bf79Snr (void) strlcpy(command.key, key, MAXOPTSLEN);
6708548bf79Snr
6718548bf79Snr if (trace >= 1)
6728548bf79Snr trace_prt(1, "call_read_execout %s %s\n", fname, key);
6738548bf79Snr darg.data_ptr = (char *)&command;
6748548bf79Snr darg.data_size = sizeof (command);
6758548bf79Snr darg.desc_ptr = NULL;
6768548bf79Snr darg.desc_num = 0;
6778548bf79Snr darg.rbuf = line;
6788548bf79Snr darg.rsize = linesz;
6798548bf79Snr
6808548bf79Snr ret = door_call(did_exec_map, &darg);
6818548bf79Snr
6828548bf79Snr return (ret);
6838548bf79Snr }
684