10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*3930Snr123932 * Common Development and Distribution License (the "License").
6*3930Snr123932 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
220Sstevel@tonic-gate * ns_files.c
230Sstevel@tonic-gate *
24*3930Snr123932 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
250Sstevel@tonic-gate * Use is subject to license terms.
260Sstevel@tonic-gate */
270Sstevel@tonic-gate
280Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
290Sstevel@tonic-gate
300Sstevel@tonic-gate #include <stdio.h>
310Sstevel@tonic-gate #include <stdlib.h>
320Sstevel@tonic-gate #include <syslog.h>
330Sstevel@tonic-gate #include <string.h>
340Sstevel@tonic-gate #include <ctype.h>
350Sstevel@tonic-gate #include <nsswitch.h>
360Sstevel@tonic-gate #include <sys/stat.h>
370Sstevel@tonic-gate #include <sys/param.h>
380Sstevel@tonic-gate #include <rpc/rpc.h>
390Sstevel@tonic-gate #include <rpcsvc/nfs_prot.h>
400Sstevel@tonic-gate #include <thread.h>
410Sstevel@tonic-gate #include <assert.h>
420Sstevel@tonic-gate #include <errno.h>
430Sstevel@tonic-gate #include <fcntl.h>
440Sstevel@tonic-gate #include <unistd.h>
450Sstevel@tonic-gate #include <synch.h>
460Sstevel@tonic-gate #include <sys/types.h>
470Sstevel@tonic-gate #include <sys/wait.h>
48*3930Snr123932 #include <strings.h>
490Sstevel@tonic-gate #include "automount.h"
500Sstevel@tonic-gate
510Sstevel@tonic-gate static int read_execout(char *key, char **lp, char *fname, char *line,
520Sstevel@tonic-gate int linesz);
53*3930Snr123932 static int call_read_execout(char *key, char **lp, char *fname, char *line,
54*3930Snr123932 int linesz);
550Sstevel@tonic-gate static FILE *file_open(char *, char *, char **, char ***);
560Sstevel@tonic-gate
570Sstevel@tonic-gate /*
580Sstevel@tonic-gate * Initialize the stack
590Sstevel@tonic-gate */
600Sstevel@tonic-gate void
init_files(char ** stack,char *** stkptr)610Sstevel@tonic-gate init_files(char **stack, char ***stkptr)
620Sstevel@tonic-gate {
630Sstevel@tonic-gate /*
640Sstevel@tonic-gate * The call is bogus for automountd since the stack is
650Sstevel@tonic-gate * is more appropriately initialized in the thread-private
660Sstevel@tonic-gate * routines
670Sstevel@tonic-gate */
680Sstevel@tonic-gate if (stack == NULL && stkptr == NULL)
690Sstevel@tonic-gate return;
700Sstevel@tonic-gate (void) stack_op(INIT, NULL, stack, stkptr);
710Sstevel@tonic-gate }
720Sstevel@tonic-gate
730Sstevel@tonic-gate int
getmapent_files(key,mapname,ml,stack,stkptr,iswildcard,isrestricted)740Sstevel@tonic-gate getmapent_files(key, mapname, ml, stack, stkptr, iswildcard, isrestricted)
750Sstevel@tonic-gate char *key;
760Sstevel@tonic-gate char *mapname;
770Sstevel@tonic-gate struct mapline *ml;
780Sstevel@tonic-gate char **stack, ***stkptr;
790Sstevel@tonic-gate bool_t *iswildcard;
800Sstevel@tonic-gate bool_t isrestricted;
810Sstevel@tonic-gate {
820Sstevel@tonic-gate int nserr;
830Sstevel@tonic-gate FILE *fp;
840Sstevel@tonic-gate char word[MAXPATHLEN+1], wordq[MAXPATHLEN+1];
850Sstevel@tonic-gate char linebuf[LINESZ], lineqbuf[LINESZ];
860Sstevel@tonic-gate char *lp, *lq;
870Sstevel@tonic-gate struct stat stbuf;
880Sstevel@tonic-gate char fname[MAXFILENAMELEN]; /* /etc prepended to mapname if reqd */
890Sstevel@tonic-gate int syntaxok = 1;
900Sstevel@tonic-gate
910Sstevel@tonic-gate if (iswildcard)
920Sstevel@tonic-gate *iswildcard = FALSE;
930Sstevel@tonic-gate if ((fp = file_open(mapname, fname, stack, stkptr)) == NULL) {
940Sstevel@tonic-gate nserr = __NSW_UNAVAIL;
950Sstevel@tonic-gate goto done;
960Sstevel@tonic-gate }
970Sstevel@tonic-gate
980Sstevel@tonic-gate if (stat(fname, &stbuf) < 0) {
990Sstevel@tonic-gate nserr = __NSW_UNAVAIL;
1000Sstevel@tonic-gate goto done;
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate * If the file has its execute bit on then
1050Sstevel@tonic-gate * assume it's an executable map.
1060Sstevel@tonic-gate * Execute it and pass the key as an argument.
1070Sstevel@tonic-gate * Expect to get a map entry on the stdout.
1080Sstevel@tonic-gate * Ignore the "x" bit on restricted maps.
1090Sstevel@tonic-gate */
1100Sstevel@tonic-gate if (!isrestricted && (stbuf.st_mode & S_IXUSR)) {
1110Sstevel@tonic-gate int rc;
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate if (trace > 1) {
1140Sstevel@tonic-gate trace_prt(1,
1150Sstevel@tonic-gate "\tExecutable map: map=%s key=%s\n",
1160Sstevel@tonic-gate fname, key);
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate
119*3930Snr123932 rc = call_read_execout(key, &lp, fname, ml->linebuf, LINESZ);
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate if (rc != 0) {
1220Sstevel@tonic-gate nserr = __NSW_UNAVAIL;
1230Sstevel@tonic-gate goto done;
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate
126*3930Snr123932 if (strlen(ml->linebuf) == 0) {
1270Sstevel@tonic-gate nserr = __NSW_NOTFOUND;
1280Sstevel@tonic-gate goto done;
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate unquote(ml->linebuf, ml->lineqbuf);
1320Sstevel@tonic-gate nserr = __NSW_SUCCESS;
1330Sstevel@tonic-gate goto done;
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate
1370Sstevel@tonic-gate /*
1380Sstevel@tonic-gate * It's just a normal map file.
1390Sstevel@tonic-gate * Search for the entry with the required key.
1400Sstevel@tonic-gate */
1410Sstevel@tonic-gate for (;;) {
1420Sstevel@tonic-gate lp = get_line(fp, fname, linebuf, sizeof (linebuf));
1430Sstevel@tonic-gate if (lp == NULL) {
1440Sstevel@tonic-gate nserr = __NSW_NOTFOUND;
1450Sstevel@tonic-gate goto done;
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate if (verbose && syntaxok && isspace(*(uchar_t *)lp)) {
1480Sstevel@tonic-gate syntaxok = 0;
1490Sstevel@tonic-gate syslog(LOG_ERR,
1500Sstevel@tonic-gate "leading space in map entry \"%s\" in %s",
1510Sstevel@tonic-gate lp, mapname);
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate lq = lineqbuf;
1540Sstevel@tonic-gate unquote(lp, lq);
1550Sstevel@tonic-gate if ((getword(word, wordq, &lp, &lq, ' ', sizeof (word))
1560Sstevel@tonic-gate == -1) || (word[0] == '\0'))
1570Sstevel@tonic-gate continue;
1580Sstevel@tonic-gate if (strcmp(word, key) == 0)
1590Sstevel@tonic-gate break;
1600Sstevel@tonic-gate if (word[0] == '*' && word[1] == '\0') {
1610Sstevel@tonic-gate if (iswildcard)
1620Sstevel@tonic-gate *iswildcard = TRUE;
1630Sstevel@tonic-gate break;
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate if (word[0] == '+') {
1660Sstevel@tonic-gate nserr = getmapent(key, word+1, ml, stack, stkptr,
1670Sstevel@tonic-gate iswildcard, isrestricted);
1680Sstevel@tonic-gate if (nserr == __NSW_SUCCESS)
1690Sstevel@tonic-gate goto done;
1700Sstevel@tonic-gate continue;
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate /*
1740Sstevel@tonic-gate * sanity check each map entry key against
1750Sstevel@tonic-gate * the lookup key as the map is searched.
1760Sstevel@tonic-gate */
1770Sstevel@tonic-gate if (verbose && syntaxok) { /* sanity check entry */
1780Sstevel@tonic-gate if (*key == '/') {
1790Sstevel@tonic-gate if (*word != '/') {
1800Sstevel@tonic-gate syntaxok = 0;
1810Sstevel@tonic-gate syslog(LOG_ERR,
1820Sstevel@tonic-gate "bad key \"%s\" in direct map %s\n",
1830Sstevel@tonic-gate word, mapname);
1840Sstevel@tonic-gate }
1850Sstevel@tonic-gate } else {
1860Sstevel@tonic-gate if (strchr(word, '/')) {
1870Sstevel@tonic-gate syntaxok = 0;
1880Sstevel@tonic-gate syslog(LOG_ERR,
1890Sstevel@tonic-gate "bad key \"%s\" in indirect map %s\n",
1900Sstevel@tonic-gate word, mapname);
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate
1960Sstevel@tonic-gate (void) strcpy(ml->linebuf, lp);
1970Sstevel@tonic-gate (void) strcpy(ml->lineqbuf, lq);
1980Sstevel@tonic-gate nserr = __NSW_SUCCESS;
1990Sstevel@tonic-gate done:
2000Sstevel@tonic-gate if (fp) {
2010Sstevel@tonic-gate (void) stack_op(POP, (char *)NULL, stack, stkptr);
2020Sstevel@tonic-gate (void) fclose(fp);
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate
2060Sstevel@tonic-gate return (nserr);
2070Sstevel@tonic-gate }
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate int
getmapkeys_files(mapname,list,error,cache_time,stack,stkptr)2100Sstevel@tonic-gate getmapkeys_files(mapname, list, error, cache_time, stack, stkptr)
2110Sstevel@tonic-gate char *mapname;
2120Sstevel@tonic-gate struct dir_entry **list;
2130Sstevel@tonic-gate int *error;
2140Sstevel@tonic-gate int *cache_time;
2150Sstevel@tonic-gate char **stack, ***stkptr;
2160Sstevel@tonic-gate {
2170Sstevel@tonic-gate FILE *fp = NULL;
2180Sstevel@tonic-gate char word[MAXPATHLEN+1], wordq[MAXPATHLEN+1];
2190Sstevel@tonic-gate char linebuf[LINESZ], lineqbuf[LINESZ];
2200Sstevel@tonic-gate char *lp, *lq;
2210Sstevel@tonic-gate struct stat stbuf;
2220Sstevel@tonic-gate char fname[MAXFILENAMELEN]; /* /etc prepended to mapname if reqd */
2230Sstevel@tonic-gate int syntaxok = 1;
2240Sstevel@tonic-gate int nserr;
2250Sstevel@tonic-gate struct dir_entry *last = NULL;
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate if (trace > 1)
2280Sstevel@tonic-gate trace_prt(1, "getmapkeys_files %s\n", mapname);
2290Sstevel@tonic-gate
2300Sstevel@tonic-gate *cache_time = RDDIR_CACHE_TIME;
2310Sstevel@tonic-gate if ((fp = file_open(mapname, fname, stack, stkptr)) == NULL) {
2320Sstevel@tonic-gate *error = ENOENT;
2330Sstevel@tonic-gate nserr = __NSW_UNAVAIL;
2340Sstevel@tonic-gate goto done;
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate if (fseek(fp, 0L, SEEK_SET) == -1) {
2370Sstevel@tonic-gate *error = ENOENT;
2380Sstevel@tonic-gate nserr = __NSW_UNAVAIL;
2390Sstevel@tonic-gate goto done;
2400Sstevel@tonic-gate }
2410Sstevel@tonic-gate
2420Sstevel@tonic-gate if (stat(fname, &stbuf) < 0) {
2430Sstevel@tonic-gate *error = ENOENT;
2440Sstevel@tonic-gate nserr = __NSW_UNAVAIL;
2450Sstevel@tonic-gate goto done;
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate
2480Sstevel@tonic-gate /*
2490Sstevel@tonic-gate * If the file has its execute bit on then
2500Sstevel@tonic-gate * assume it's an executable map.
2510Sstevel@tonic-gate * I don't know how to list executable maps, return
2520Sstevel@tonic-gate * an empty map.
2530Sstevel@tonic-gate */
2540Sstevel@tonic-gate if (stbuf.st_mode & S_IXUSR) {
2550Sstevel@tonic-gate *error = 0;
2560Sstevel@tonic-gate nserr = __NSW_SUCCESS;
2570Sstevel@tonic-gate goto done;
2580Sstevel@tonic-gate }
2590Sstevel@tonic-gate /*
2600Sstevel@tonic-gate * It's just a normal map file.
2610Sstevel@tonic-gate * List entries one line at a time.
2620Sstevel@tonic-gate */
2630Sstevel@tonic-gate for (;;) {
2640Sstevel@tonic-gate lp = get_line(fp, fname, linebuf, sizeof (linebuf));
2650Sstevel@tonic-gate if (lp == NULL) {
2660Sstevel@tonic-gate nserr = __NSW_SUCCESS;
2670Sstevel@tonic-gate goto done;
2680Sstevel@tonic-gate }
2690Sstevel@tonic-gate if (syntaxok && isspace(*(uchar_t *)lp)) {
2700Sstevel@tonic-gate syntaxok = 0;
2710Sstevel@tonic-gate syslog(LOG_ERR,
2720Sstevel@tonic-gate "leading space in map entry \"%s\" in %s",
2730Sstevel@tonic-gate lp, mapname);
2740Sstevel@tonic-gate }
2750Sstevel@tonic-gate lq = lineqbuf;
2760Sstevel@tonic-gate unquote(lp, lq);
2770Sstevel@tonic-gate if ((getword(word, wordq, &lp, &lq, ' ', MAXFILENAMELEN)
2780Sstevel@tonic-gate == -1) || (word[0] == '\0'))
2790Sstevel@tonic-gate continue;
2800Sstevel@tonic-gate /*
2810Sstevel@tonic-gate * Wildcard entries should be ignored and this should be
2820Sstevel@tonic-gate * the last entry read to corroborate the search through
2830Sstevel@tonic-gate * files, i.e., search for key until a wildcard is reached.
2840Sstevel@tonic-gate */
2850Sstevel@tonic-gate if (word[0] == '*' && word[1] == '\0')
2860Sstevel@tonic-gate break;
2870Sstevel@tonic-gate if (word[0] == '+') {
2880Sstevel@tonic-gate /*
2890Sstevel@tonic-gate * Name switch here
2900Sstevel@tonic-gate */
2910Sstevel@tonic-gate getmapkeys(word+1, list, error, cache_time,
2920Sstevel@tonic-gate stack, stkptr, 0);
2930Sstevel@tonic-gate /*
2940Sstevel@tonic-gate * the list may have been updated, therefore
2950Sstevel@tonic-gate * our 'last' may no longer be valid
2960Sstevel@tonic-gate */
2970Sstevel@tonic-gate last = NULL;
2980Sstevel@tonic-gate continue;
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate
3010Sstevel@tonic-gate if (add_dir_entry(word, list, &last) != 0) {
3020Sstevel@tonic-gate *error = ENOMEM;
3030Sstevel@tonic-gate goto done;
3040Sstevel@tonic-gate }
3050Sstevel@tonic-gate assert(last != NULL);
3060Sstevel@tonic-gate }
3070Sstevel@tonic-gate
3080Sstevel@tonic-gate nserr = __NSW_SUCCESS;
3090Sstevel@tonic-gate done:
3100Sstevel@tonic-gate if (fp) {
3110Sstevel@tonic-gate (void) stack_op(POP, (char *)NULL, stack, stkptr);
3120Sstevel@tonic-gate (void) fclose(fp);
3130Sstevel@tonic-gate }
3140Sstevel@tonic-gate
3150Sstevel@tonic-gate if (*list != NULL) {
3160Sstevel@tonic-gate /*
3170Sstevel@tonic-gate * list of entries found
3180Sstevel@tonic-gate */
3190Sstevel@tonic-gate *error = 0;
3200Sstevel@tonic-gate }
3210Sstevel@tonic-gate return (nserr);
3220Sstevel@tonic-gate }
3230Sstevel@tonic-gate
324249Sjwahlig int
loadmaster_files(mastermap,defopts,stack,stkptr)3250Sstevel@tonic-gate loadmaster_files(mastermap, defopts, stack, stkptr)
3260Sstevel@tonic-gate char *mastermap;
3270Sstevel@tonic-gate char *defopts;
3280Sstevel@tonic-gate char **stack, ***stkptr;
3290Sstevel@tonic-gate {
3300Sstevel@tonic-gate FILE *fp;
3310Sstevel@tonic-gate int done = 0;
3320Sstevel@tonic-gate char *line, *dir, *map, *opts;
3330Sstevel@tonic-gate char linebuf[LINESZ];
3340Sstevel@tonic-gate char lineq[LINESZ];
3350Sstevel@tonic-gate char fname[MAXFILENAMELEN]; /* /etc prepended to mapname if reqd */
3360Sstevel@tonic-gate
3370Sstevel@tonic-gate
3380Sstevel@tonic-gate if ((fp = file_open(mastermap, fname, stack, stkptr)) == NULL)
3390Sstevel@tonic-gate return (__NSW_UNAVAIL);
3400Sstevel@tonic-gate
3410Sstevel@tonic-gate while ((line = get_line(fp, fname, linebuf,
3420Sstevel@tonic-gate sizeof (linebuf))) != NULL) {
3430Sstevel@tonic-gate unquote(line, lineq);
3440Sstevel@tonic-gate if (macro_expand("", line, lineq, LINESZ)) {
3450Sstevel@tonic-gate syslog(LOG_ERR,
3460Sstevel@tonic-gate "map %s: line too long (max %d chars)",
3470Sstevel@tonic-gate mastermap, LINESZ - 1);
3480Sstevel@tonic-gate continue;
3490Sstevel@tonic-gate }
3500Sstevel@tonic-gate dir = line;
3510Sstevel@tonic-gate while (*dir && isspace(*dir))
3520Sstevel@tonic-gate dir++;
3530Sstevel@tonic-gate if (*dir == '\0')
3540Sstevel@tonic-gate continue;
3550Sstevel@tonic-gate map = dir;
3560Sstevel@tonic-gate
3570Sstevel@tonic-gate while (*map && !isspace(*map)) map++;
3580Sstevel@tonic-gate if (*map)
3590Sstevel@tonic-gate *map++ = '\0';
3600Sstevel@tonic-gate
3610Sstevel@tonic-gate if (*dir == '+') {
3620Sstevel@tonic-gate opts = map;
3630Sstevel@tonic-gate while (*opts && isspace(*opts))
3640Sstevel@tonic-gate opts++;
3650Sstevel@tonic-gate if (*opts != '-')
3660Sstevel@tonic-gate opts = defopts;
3670Sstevel@tonic-gate else
3680Sstevel@tonic-gate opts++;
3690Sstevel@tonic-gate /*
3700Sstevel@tonic-gate * Check for no embedded blanks.
3710Sstevel@tonic-gate */
3720Sstevel@tonic-gate if (strcspn(opts, " ") == strlen(opts)) {
3730Sstevel@tonic-gate dir++;
3740Sstevel@tonic-gate (void) loadmaster_map(dir, opts, stack, stkptr);
3750Sstevel@tonic-gate } else {
3760Sstevel@tonic-gate pr_msg("Warning: invalid entry for %s in %s ignored.\n", dir, fname);
3770Sstevel@tonic-gate continue;
3780Sstevel@tonic-gate }
3790Sstevel@tonic-gate
3800Sstevel@tonic-gate } else {
3810Sstevel@tonic-gate while (*map && isspace(*map))
3820Sstevel@tonic-gate map++;
3830Sstevel@tonic-gate if (*map == '\0')
3840Sstevel@tonic-gate continue;
3850Sstevel@tonic-gate opts = map;
3860Sstevel@tonic-gate while (*opts && !isspace(*opts))
3870Sstevel@tonic-gate opts++;
3880Sstevel@tonic-gate if (*opts) {
3890Sstevel@tonic-gate *opts++ = '\0';
3900Sstevel@tonic-gate while (*opts && isspace(*opts))
3910Sstevel@tonic-gate opts++;
3920Sstevel@tonic-gate }
3930Sstevel@tonic-gate if (*opts != '-')
3940Sstevel@tonic-gate opts = defopts;
3950Sstevel@tonic-gate else
3960Sstevel@tonic-gate opts++;
3970Sstevel@tonic-gate /*
3980Sstevel@tonic-gate * Check for no embedded blanks.
3990Sstevel@tonic-gate */
4000Sstevel@tonic-gate if (strcspn(opts, " ") == strlen(opts)) {
4010Sstevel@tonic-gate dirinit(dir, map, opts, 0, stack, stkptr);
4020Sstevel@tonic-gate } else {
4030Sstevel@tonic-gate pr_msg("Warning: invalid entry for %s in %s ignored.\n", dir, fname);
4040Sstevel@tonic-gate continue;
4050Sstevel@tonic-gate }
4060Sstevel@tonic-gate }
4070Sstevel@tonic-gate done++;
4080Sstevel@tonic-gate }
4090Sstevel@tonic-gate
4100Sstevel@tonic-gate (void) stack_op(POP, (char *)NULL, stack, stkptr);
4110Sstevel@tonic-gate (void) fclose(fp);
4120Sstevel@tonic-gate
4130Sstevel@tonic-gate return (done ? __NSW_SUCCESS : __NSW_NOTFOUND);
4140Sstevel@tonic-gate }
4150Sstevel@tonic-gate
416249Sjwahlig int
loaddirect_files(map,local_map,opts,stack,stkptr)4170Sstevel@tonic-gate loaddirect_files(map, local_map, opts, stack, stkptr)
4180Sstevel@tonic-gate char *map, *local_map, *opts;
4190Sstevel@tonic-gate char **stack, ***stkptr;
4200Sstevel@tonic-gate {
4210Sstevel@tonic-gate FILE *fp;
4220Sstevel@tonic-gate int done = 0;
4230Sstevel@tonic-gate char *line, *p1, *p2;
4240Sstevel@tonic-gate char linebuf[LINESZ];
4250Sstevel@tonic-gate char fname[MAXFILENAMELEN]; /* /etc prepended to mapname if reqd */
4260Sstevel@tonic-gate
4270Sstevel@tonic-gate if ((fp = file_open(map, fname, stack, stkptr)) == NULL)
4280Sstevel@tonic-gate return (__NSW_UNAVAIL);
4290Sstevel@tonic-gate
4300Sstevel@tonic-gate while ((line = get_line(fp, fname, linebuf,
4310Sstevel@tonic-gate sizeof (linebuf))) != NULL) {
4320Sstevel@tonic-gate p1 = line;
4330Sstevel@tonic-gate while (*p1 && isspace(*p1))
4340Sstevel@tonic-gate p1++;
4350Sstevel@tonic-gate if (*p1 == '\0')
4360Sstevel@tonic-gate continue;
4370Sstevel@tonic-gate p2 = p1;
4380Sstevel@tonic-gate while (*p2 && !isspace(*p2))
4390Sstevel@tonic-gate p2++;
4400Sstevel@tonic-gate *p2 = '\0';
4410Sstevel@tonic-gate if (*p1 == '+') {
4420Sstevel@tonic-gate p1++;
4430Sstevel@tonic-gate (void) loaddirect_map(p1, local_map, opts, stack,
4440Sstevel@tonic-gate stkptr);
4450Sstevel@tonic-gate } else {
4460Sstevel@tonic-gate dirinit(p1, local_map, opts, 1, stack, stkptr);
4470Sstevel@tonic-gate }
4480Sstevel@tonic-gate done++;
4490Sstevel@tonic-gate }
4500Sstevel@tonic-gate
4510Sstevel@tonic-gate (void) stack_op(POP, (char *)NULL, stack, stkptr);
4520Sstevel@tonic-gate (void) fclose(fp);
4530Sstevel@tonic-gate
4540Sstevel@tonic-gate return (done ? __NSW_SUCCESS : __NSW_NOTFOUND);
4550Sstevel@tonic-gate }
4560Sstevel@tonic-gate
4570Sstevel@tonic-gate /*
4580Sstevel@tonic-gate * This procedure opens the file and pushes it onto the
4590Sstevel@tonic-gate * the stack. Only if a file is opened successfully, is
4600Sstevel@tonic-gate * it pushed onto the stack
4610Sstevel@tonic-gate */
4620Sstevel@tonic-gate static FILE *
file_open(map,fname,stack,stkptr)4630Sstevel@tonic-gate file_open(map, fname, stack, stkptr)
4640Sstevel@tonic-gate char *map, *fname;
4650Sstevel@tonic-gate char **stack, ***stkptr;
4660Sstevel@tonic-gate {
4670Sstevel@tonic-gate FILE *fp;
4680Sstevel@tonic-gate
4690Sstevel@tonic-gate if (*map != '/') {
4700Sstevel@tonic-gate /* prepend an "/etc" */
4710Sstevel@tonic-gate (void) strcpy(fname, "/etc/");
4720Sstevel@tonic-gate (void) strcat(fname, map);
4730Sstevel@tonic-gate } else
4740Sstevel@tonic-gate (void) strcpy(fname, map);
4750Sstevel@tonic-gate
4760Sstevel@tonic-gate fp = fopen(fname, "r");
4770Sstevel@tonic-gate
4780Sstevel@tonic-gate if (fp != NULL) {
4790Sstevel@tonic-gate if (!stack_op(PUSH, fname, stack, stkptr)) {
4800Sstevel@tonic-gate (void) fclose(fp);
4810Sstevel@tonic-gate return (NULL);
4820Sstevel@tonic-gate }
4830Sstevel@tonic-gate }
4840Sstevel@tonic-gate return (fp);
4850Sstevel@tonic-gate }
4860Sstevel@tonic-gate
4870Sstevel@tonic-gate /*
4880Sstevel@tonic-gate * reimplemnted to be MT-HOT.
4890Sstevel@tonic-gate */
4900Sstevel@tonic-gate int
stack_op(op,name,stack,stkptr)4910Sstevel@tonic-gate stack_op(op, name, stack, stkptr)
4920Sstevel@tonic-gate int op;
4930Sstevel@tonic-gate char *name;
4940Sstevel@tonic-gate char **stack, ***stkptr;
4950Sstevel@tonic-gate {
4960Sstevel@tonic-gate char **ptr = NULL;
4970Sstevel@tonic-gate char **stk_top = &stack[STACKSIZ - 1];
4980Sstevel@tonic-gate
4990Sstevel@tonic-gate /*
5000Sstevel@tonic-gate * the stackptr points to the next empty slot
5010Sstevel@tonic-gate * for PUSH: put the element and increment stkptr
5020Sstevel@tonic-gate * for POP: decrement stkptr and free
5030Sstevel@tonic-gate */
5040Sstevel@tonic-gate
5050Sstevel@tonic-gate switch (op) {
5060Sstevel@tonic-gate case INIT:
5070Sstevel@tonic-gate for (ptr = stack; ptr != stk_top; ptr++)
5080Sstevel@tonic-gate *ptr = (char *)NULL;
5090Sstevel@tonic-gate *stkptr = stack;
5100Sstevel@tonic-gate return (1);
5110Sstevel@tonic-gate case ERASE:
5120Sstevel@tonic-gate for (ptr = stack; ptr != stk_top; ptr++)
5130Sstevel@tonic-gate if (*ptr) {
5140Sstevel@tonic-gate if (trace > 1)
5150Sstevel@tonic-gate trace_prt(1, " ERASE %s\n", *ptr);
5160Sstevel@tonic-gate free (*ptr);
5170Sstevel@tonic-gate *ptr = (char *)NULL;
5180Sstevel@tonic-gate }
5190Sstevel@tonic-gate *stkptr = stack;
5200Sstevel@tonic-gate return (1);
5210Sstevel@tonic-gate case PUSH:
5220Sstevel@tonic-gate if (*stkptr == stk_top)
5230Sstevel@tonic-gate return (0);
5240Sstevel@tonic-gate for (ptr = stack; ptr != *stkptr; ptr++)
5250Sstevel@tonic-gate if (*ptr && (strcmp(*ptr, name) == 0)) {
5260Sstevel@tonic-gate return (0);
5270Sstevel@tonic-gate }
5280Sstevel@tonic-gate if (trace > 1)
5290Sstevel@tonic-gate trace_prt(1, " PUSH %s\n", name);
5300Sstevel@tonic-gate if ((**stkptr = strdup(name)) == NULL) {
5310Sstevel@tonic-gate syslog(LOG_ERR, "stack_op: Memory alloc failed : %m");
5320Sstevel@tonic-gate return (0);
5330Sstevel@tonic-gate }
5340Sstevel@tonic-gate (*stkptr)++;
5350Sstevel@tonic-gate return (1);
5360Sstevel@tonic-gate case POP:
5370Sstevel@tonic-gate if (*stkptr != stack)
5380Sstevel@tonic-gate (*stkptr)--;
5390Sstevel@tonic-gate else
5400Sstevel@tonic-gate syslog(LOG_ERR, "Attempt to pop empty stack\n");
5410Sstevel@tonic-gate
5420Sstevel@tonic-gate if (*stkptr && **stkptr) {
5430Sstevel@tonic-gate if (trace > 1)
5440Sstevel@tonic-gate trace_prt(1, " POP %s\n", **stkptr);
5450Sstevel@tonic-gate free (**stkptr);
5460Sstevel@tonic-gate **stkptr = (char *)NULL;
5470Sstevel@tonic-gate }
5480Sstevel@tonic-gate return (1);
5490Sstevel@tonic-gate default:
5500Sstevel@tonic-gate return (0);
5510Sstevel@tonic-gate }
5520Sstevel@tonic-gate }
5530Sstevel@tonic-gate
5540Sstevel@tonic-gate #define READ_EXECOUT_ARGS 3
5550Sstevel@tonic-gate
5560Sstevel@tonic-gate /*
5570Sstevel@tonic-gate * read_execout(char *key, char **lp, char *fname, char *line, int linesz)
5580Sstevel@tonic-gate * A simpler, multithreaded implementation of popen(). Used due to
5590Sstevel@tonic-gate * non multithreaded implementation of popen() (it calls vfork()) and a
5600Sstevel@tonic-gate * significant bug in execl().
5610Sstevel@tonic-gate * Returns 0 on OK or -1 on error.
5620Sstevel@tonic-gate */
5630Sstevel@tonic-gate static int
read_execout(char * key,char ** lp,char * fname,char * line,int linesz)5640Sstevel@tonic-gate read_execout(char *key, char **lp, char *fname, char *line, int linesz)
5650Sstevel@tonic-gate {
5660Sstevel@tonic-gate int p[2];
5670Sstevel@tonic-gate int status = 0;
5680Sstevel@tonic-gate int child_pid;
5690Sstevel@tonic-gate char *args[READ_EXECOUT_ARGS];
5700Sstevel@tonic-gate FILE *fp0;
5710Sstevel@tonic-gate
5720Sstevel@tonic-gate if (pipe(p) < 0) {
5730Sstevel@tonic-gate syslog(LOG_ERR, "read_execout: Cannot create pipe");
5740Sstevel@tonic-gate return (-1);
5750Sstevel@tonic-gate }
5760Sstevel@tonic-gate
5770Sstevel@tonic-gate /* setup args for execv */
5780Sstevel@tonic-gate if (((args[0] = strdup(fname)) == NULL) ||
5790Sstevel@tonic-gate ((args[1] = strdup(key)) == NULL)) {
5800Sstevel@tonic-gate if (args[0] != NULL)
5810Sstevel@tonic-gate free(args[0]);
5820Sstevel@tonic-gate syslog(LOG_ERR, "read_execout: Memory allocation failed");
5830Sstevel@tonic-gate return (-1);
5840Sstevel@tonic-gate }
5850Sstevel@tonic-gate args[2] = NULL;
5860Sstevel@tonic-gate
5870Sstevel@tonic-gate if (trace > 3)
5880Sstevel@tonic-gate trace_prt(1, "\tread_execout: forking .....\n");
5890Sstevel@tonic-gate
5900Sstevel@tonic-gate switch ((child_pid = fork1())) {
5910Sstevel@tonic-gate case -1:
5920Sstevel@tonic-gate syslog(LOG_ERR, "read_execout: Cannot fork");
5930Sstevel@tonic-gate return (-1);
5940Sstevel@tonic-gate case 0:
5950Sstevel@tonic-gate /*
5960Sstevel@tonic-gate * Child
5970Sstevel@tonic-gate */
5980Sstevel@tonic-gate close(p[0]);
5990Sstevel@tonic-gate close(1);
6000Sstevel@tonic-gate if (fcntl(p[1], F_DUPFD, 1) != 1) {
6010Sstevel@tonic-gate syslog(LOG_ERR,
6020Sstevel@tonic-gate "read_execout: dup of stdout failed");
6030Sstevel@tonic-gate _exit(-1);
6040Sstevel@tonic-gate }
6050Sstevel@tonic-gate close(p[1]);
6060Sstevel@tonic-gate execv(fname, &args[0]);
6070Sstevel@tonic-gate _exit(-1);
6080Sstevel@tonic-gate default:
6090Sstevel@tonic-gate /*
6100Sstevel@tonic-gate * Parent
6110Sstevel@tonic-gate */
6120Sstevel@tonic-gate close(p[1]);
6130Sstevel@tonic-gate
6140Sstevel@tonic-gate /*
6150Sstevel@tonic-gate * wait for child to complete. Note we read after the
6160Sstevel@tonic-gate * child exits to guarantee a full pipe.
6170Sstevel@tonic-gate */
6180Sstevel@tonic-gate while (waitpid(child_pid, &status, 0) < 0) {
6190Sstevel@tonic-gate /* if waitpid fails with EINTR, restart */
6200Sstevel@tonic-gate if (errno != EINTR) {
6210Sstevel@tonic-gate status = -1;
6220Sstevel@tonic-gate break;
6230Sstevel@tonic-gate }
6240Sstevel@tonic-gate }
6250Sstevel@tonic-gate if (status != -1) {
6260Sstevel@tonic-gate if ((fp0 = fdopen(p[0], "r")) != NULL) {
6270Sstevel@tonic-gate *lp = get_line(fp0, fname, line, linesz);
6280Sstevel@tonic-gate fclose(fp0);
6290Sstevel@tonic-gate } else {
6300Sstevel@tonic-gate close(p[0]);
6310Sstevel@tonic-gate status = -1;
6320Sstevel@tonic-gate }
6330Sstevel@tonic-gate } else {
6340Sstevel@tonic-gate close(p[0]);
6350Sstevel@tonic-gate }
6360Sstevel@tonic-gate
6370Sstevel@tonic-gate /* free args */
6380Sstevel@tonic-gate free(args[0]);
6390Sstevel@tonic-gate free(args[1]);
6400Sstevel@tonic-gate
6410Sstevel@tonic-gate if (trace > 3)
6420Sstevel@tonic-gate trace_prt(1, "\tread_execout: map=%s key=%s line=%s\n",
6430Sstevel@tonic-gate fname, key, line);
6440Sstevel@tonic-gate
6450Sstevel@tonic-gate return (status);
6460Sstevel@tonic-gate }
6470Sstevel@tonic-gate }
648*3930Snr123932
649*3930Snr123932 void
automountd_do_exec_map(void * cookie,char * argp,size_t arg_size,door_desc_t * dfd,uint_t n_desc)650*3930Snr123932 automountd_do_exec_map(void *cookie, char *argp, size_t arg_size,
651*3930Snr123932 door_desc_t *dfd, uint_t n_desc)
652*3930Snr123932 {
653*3930Snr123932 command_t *command;
654*3930Snr123932 char line[LINESZ];
655*3930Snr123932 char *lp;
656*3930Snr123932 int rc;
657*3930Snr123932
658*3930Snr123932 command = (command_t *)argp;
659*3930Snr123932
660*3930Snr123932 if (sizeof (*command) != arg_size) {
661*3930Snr123932 rc = 0;
662*3930Snr123932 syslog(LOG_ERR, "read_execout: invalid door arguments");
663*3930Snr123932 door_return((char *)&rc, sizeof (rc), NULL, 0);
664*3930Snr123932 }
665*3930Snr123932
666*3930Snr123932 rc = read_execout(command->key, &lp, command->file, line, LINESZ);
667*3930Snr123932
668*3930Snr123932 if (rc != 0) {
669*3930Snr123932 /*
670*3930Snr123932 * read_execout returned an error, return 0 to the door_client
671*3930Snr123932 * to indicate failure
672*3930Snr123932 */
673*3930Snr123932 rc = 0;
674*3930Snr123932 door_return((char *)&rc, sizeof (rc), NULL, 0);
675*3930Snr123932 } else {
676*3930Snr123932 door_return((char *)line, LINESZ, NULL, 0);
677*3930Snr123932 }
678*3930Snr123932 trace_prt(1, "automountd_do_exec_map, door return failed %s, %s\n",
679*3930Snr123932 command->file, strerror(errno));
680*3930Snr123932 door_return(NULL, 0, NULL, 0);
681*3930Snr123932 }
682*3930Snr123932
683*3930Snr123932 int
call_read_execout(char * key,char ** lp,char * fname,char * line,int linesz)684*3930Snr123932 call_read_execout(char *key, char **lp, char *fname, char *line,
685*3930Snr123932 int linesz)
686*3930Snr123932 {
687*3930Snr123932 command_t command;
688*3930Snr123932 door_arg_t darg;
689*3930Snr123932 int ret;
690*3930Snr123932
691*3930Snr123932 bzero(&command, sizeof (command));
692*3930Snr123932 (void) strlcpy(command.file, fname, MAXPATHLEN);
693*3930Snr123932 (void) strlcpy(command.key, key, MAXOPTSLEN);
694*3930Snr123932
695*3930Snr123932 if (trace >= 1)
696*3930Snr123932 trace_prt(1, "call_read_execout %s %s\n", fname, key);
697*3930Snr123932 darg.data_ptr = (char *)&command;
698*3930Snr123932 darg.data_size = sizeof (command);
699*3930Snr123932 darg.desc_ptr = NULL;
700*3930Snr123932 darg.desc_num = 0;
701*3930Snr123932 darg.rbuf = line;
702*3930Snr123932 darg.rsize = linesz;
703*3930Snr123932
704*3930Snr123932 ret = door_call(did_exec_map, &darg);
705*3930Snr123932
706*3930Snr123932 lp = &line;
707*3930Snr123932 return (ret);
708*3930Snr123932 }
709