xref: /onnv-gate/usr/src/cmd/fs.d/autofs/autod_parse.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  *	autod_parse.c
24*0Sstevel@tonic-gate  *
25*0Sstevel@tonic-gate  *	Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
26*0Sstevel@tonic-gate  *	Use is subject to license terms.
27*0Sstevel@tonic-gate  */
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #include <stdio.h>
32*0Sstevel@tonic-gate #include <ctype.h>
33*0Sstevel@tonic-gate #include <string.h>
34*0Sstevel@tonic-gate #include <syslog.h>
35*0Sstevel@tonic-gate #include <sys/types.h>
36*0Sstevel@tonic-gate #include <sys/stat.h>
37*0Sstevel@tonic-gate #include <sys/param.h>
38*0Sstevel@tonic-gate #include <errno.h>
39*0Sstevel@tonic-gate #include <pwd.h>
40*0Sstevel@tonic-gate #include <netinet/in.h>
41*0Sstevel@tonic-gate #include <netdb.h>
42*0Sstevel@tonic-gate #include <sys/tiuser.h>
43*0Sstevel@tonic-gate #include <locale.h>
44*0Sstevel@tonic-gate #include <stdlib.h>
45*0Sstevel@tonic-gate #include <unistd.h>
46*0Sstevel@tonic-gate #include <thread.h>
47*0Sstevel@tonic-gate #include <rpc/rpc.h>
48*0Sstevel@tonic-gate #include <rpcsvc/mount.h>
49*0Sstevel@tonic-gate #include <fcntl.h>
50*0Sstevel@tonic-gate #include <limits.h>
51*0Sstevel@tonic-gate #include "automount.h"
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate /*
54*0Sstevel@tonic-gate  * This structure is used to determine the hierarchical
55*0Sstevel@tonic-gate  * relationship between directories
56*0Sstevel@tonic-gate  */
57*0Sstevel@tonic-gate static struct _hiernode
58*0Sstevel@tonic-gate {
59*0Sstevel@tonic-gate 	char dirname[MAXFILENAMELEN+1];
60*0Sstevel@tonic-gate 	struct _hiernode *subdir;
61*0Sstevel@tonic-gate 	struct _hiernode *leveldir;
62*0Sstevel@tonic-gate 	struct mapent *mapent;
63*0Sstevel@tonic-gate };
64*0Sstevel@tonic-gate typedef struct _hiernode hiernode;
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate void free_mapent(struct mapent *);
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate static int mapline_to_mapent(struct mapent **, struct mapline *, char *, char *,
69*0Sstevel@tonic-gate 				char *, char *, uint_t);
70*0Sstevel@tonic-gate static int hierarchical_sort(struct mapent *, hiernode **, char *, char *);
71*0Sstevel@tonic-gate static int push_options(hiernode *, char *, char *, int);
72*0Sstevel@tonic-gate static int set_mapent_opts(struct mapent *, char *, char *, char *);
73*0Sstevel@tonic-gate static void get_opts(char *, char *, char *, bool_t *);
74*0Sstevel@tonic-gate static int fstype_opts(struct mapent *, char *, char *, char *);
75*0Sstevel@tonic-gate static int modify_mapents(struct mapent **, char *, char *, char *, hiernode *,
76*0Sstevel@tonic-gate 			char *, uint_t, bool_t);
77*0Sstevel@tonic-gate static int set_and_fake_mapent_mntlevel(hiernode *, char *, char *, char *,
78*0Sstevel@tonic-gate 				struct mapent **, uint_t, char *, bool_t);
79*0Sstevel@tonic-gate static int mark_level1_root(hiernode *, char *);
80*0Sstevel@tonic-gate static int mark_and_fake_level1_noroot(hiernode *, char *, char *, char *,
81*0Sstevel@tonic-gate 				    struct mapent **, uint_t i, char *);
82*0Sstevel@tonic-gate static int convert_mapent_to_automount(struct mapent *, char *, char *);
83*0Sstevel@tonic-gate static int automount_opts(char **, char *);
84*0Sstevel@tonic-gate static int parse_fsinfo(char *, struct mapent *);
85*0Sstevel@tonic-gate static int parse_nfs(char *, struct mapent *, char *, char *, char **, char **,
86*0Sstevel@tonic-gate 				int);
87*0Sstevel@tonic-gate static int parse_special(struct mapent *, char *, char *, char **, char **,
88*0Sstevel@tonic-gate 				int);
89*0Sstevel@tonic-gate static int get_dir_from_path(char *, char **, int);
90*0Sstevel@tonic-gate static int alloc_hiernode(hiernode **, char *);
91*0Sstevel@tonic-gate static void free_hiernode(hiernode *);
92*0Sstevel@tonic-gate static void trace_mapents(char *, struct mapent *);
93*0Sstevel@tonic-gate static void trace_hierarchy(hiernode *, int);
94*0Sstevel@tonic-gate static struct mapent *do_mapent_hosts(char *, char *, uint_t);
95*0Sstevel@tonic-gate static void freeex_ent(struct exportnode *);
96*0Sstevel@tonic-gate static void freeex(struct exportnode *);
97*0Sstevel@tonic-gate static void dump_mapent_err(struct mapent *, char *, char *);
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate #define	PARSE_OK	0
100*0Sstevel@tonic-gate #define	PARSE_ERROR	-1
101*0Sstevel@tonic-gate #define	MAX_FSLEN	32
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate /*
104*0Sstevel@tonic-gate  * mapentry error type defininitions
105*0Sstevel@tonic-gate  */
106*0Sstevel@tonic-gate #define	MAPENT_NOERR	0
107*0Sstevel@tonic-gate #define	MAPENT_UATFS	1
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate /*
110*0Sstevel@tonic-gate  * parse_entry(char *key, char *mapname, char *mapopts, struct mapline *ml,
111*0Sstevel@tonic-gate  *			char *subdir, uint_t isdirect, bool_t mount_access)
112*0Sstevel@tonic-gate  * Parses the data in ml to build a mapentry list containing the information
113*0Sstevel@tonic-gate  * for the mounts/lookups to be performed. Builds an intermediate mapentry list
114*0Sstevel@tonic-gate  * by processing ml, hierarchically sorts (builds a tree of) the list according
115*0Sstevel@tonic-gate  * to mountpoint. Then pushes options down the hierarchy, and fills in the mount
116*0Sstevel@tonic-gate  * file system. Finally, modifies the intermediate list depending on how far
117*0Sstevel@tonic-gate  * in the hierarchy the current request is (uses subdir). Deals with special
118*0Sstevel@tonic-gate  * case of /net map parsing.
119*0Sstevel@tonic-gate  * Returns a pointer to the head of the mapentry list.
120*0Sstevel@tonic-gate  */
121*0Sstevel@tonic-gate struct mapent *
122*0Sstevel@tonic-gate parse_entry(char *key, char *mapname, char *mapopts, struct mapline *ml,
123*0Sstevel@tonic-gate 			char *subdir, uint_t isdirect, bool_t mount_access)
124*0Sstevel@tonic-gate {
125*0Sstevel@tonic-gate 	char *p;
126*0Sstevel@tonic-gate 	char defaultopts[AUTOFS_MAXOPTSLEN];
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 	struct mapent *mapents = NULL;
129*0Sstevel@tonic-gate 	hiernode *rootnode = NULL;
130*0Sstevel@tonic-gate 	char *lp = ml->linebuf;
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 	if (trace > 1)
133*0Sstevel@tonic-gate 		trace_prt(1, "  mapline: %s\n", ml->linebuf);
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate 	/*
136*0Sstevel@tonic-gate 	 * Assure the key is only one token long.
137*0Sstevel@tonic-gate 	 * This prevents options from sneaking in through the
138*0Sstevel@tonic-gate 	 * command line or corruption of /etc/mnttab.
139*0Sstevel@tonic-gate 	 */
140*0Sstevel@tonic-gate 	for (p = key; *p != '\0'; p++) {
141*0Sstevel@tonic-gate 		if (isspace(*p)) {
142*0Sstevel@tonic-gate 			syslog(LOG_ERR,
143*0Sstevel@tonic-gate 			"parse_entry: bad key in map %s: %s", mapname, key);
144*0Sstevel@tonic-gate 			return ((struct mapent *)NULL);
145*0Sstevel@tonic-gate 		}
146*0Sstevel@tonic-gate 	}
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	/*
149*0Sstevel@tonic-gate 	 * select the appropriate parser, and build the mapentry list
150*0Sstevel@tonic-gate 	 */
151*0Sstevel@tonic-gate 	if (strcmp(lp, "-hosts") == 0) {
152*0Sstevel@tonic-gate 		/*
153*0Sstevel@tonic-gate 		 * the /net parser - uses do_mapent_hosts to build mapents.
154*0Sstevel@tonic-gate 		 * The mapopts are considered default for every entry, so we
155*0Sstevel@tonic-gate 		 * don't push options down hierarchies.
156*0Sstevel@tonic-gate 		 */
157*0Sstevel@tonic-gate 		mapents = do_mapent_hosts(mapopts, key, isdirect);
158*0Sstevel@tonic-gate 		if (mapents == NULL)		/* nothing to free */
159*0Sstevel@tonic-gate 			return (mapents);
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 		if (trace > 3)
162*0Sstevel@tonic-gate 			trace_mapents("do_mapent_hosts:(return)", mapents);
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 		if (hierarchical_sort(mapents, &rootnode, key, mapname)
165*0Sstevel@tonic-gate 				!= PARSE_OK)
166*0Sstevel@tonic-gate 			goto parse_error;
167*0Sstevel@tonic-gate 	} else {
168*0Sstevel@tonic-gate 		/*
169*0Sstevel@tonic-gate 		 * all other parsing
170*0Sstevel@tonic-gate 		 */
171*0Sstevel@tonic-gate 		if (mapline_to_mapent(&mapents, ml, key, mapname,
172*0Sstevel@tonic-gate 		    mapopts, defaultopts, isdirect) != PARSE_OK)
173*0Sstevel@tonic-gate 			goto parse_error;
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 		if (mapents == NULL)
176*0Sstevel@tonic-gate 			return (mapents);
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate 		if (hierarchical_sort(mapents, &rootnode, key, mapname)
179*0Sstevel@tonic-gate 		    != PARSE_OK)
180*0Sstevel@tonic-gate 			goto parse_error;
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 		if (push_options(rootnode, defaultopts, mapopts,
183*0Sstevel@tonic-gate 		    MAPENT_NOERR) != PARSE_OK)
184*0Sstevel@tonic-gate 			goto parse_error;
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate 		if (trace > 3) {
187*0Sstevel@tonic-gate 			trace_prt(1, "\n\tpush_options (return)\n");
188*0Sstevel@tonic-gate 			trace_prt(0, "\tdefault options=%s\n", defaultopts);
189*0Sstevel@tonic-gate 			trace_hierarchy(rootnode, 0);
190*0Sstevel@tonic-gate 		};
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 		if (parse_fsinfo(mapname, mapents) != PARSE_OK)
193*0Sstevel@tonic-gate 			goto parse_error;
194*0Sstevel@tonic-gate 	}
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate 	/*
197*0Sstevel@tonic-gate 	 * Modify the mapentry list. We *must* do this only after
198*0Sstevel@tonic-gate 	 * the mapentry list is completely built (since we need to
199*0Sstevel@tonic-gate 	 * have parse_fsinfo called first).
200*0Sstevel@tonic-gate 	 */
201*0Sstevel@tonic-gate 	if (modify_mapents(&mapents, mapname, mapopts, subdir,
202*0Sstevel@tonic-gate 	    rootnode, key, isdirect, mount_access) != PARSE_OK)
203*0Sstevel@tonic-gate 		goto parse_error;
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate 	/*
206*0Sstevel@tonic-gate 	 * XXX: its dangerous to use rootnode after modify mapents as
207*0Sstevel@tonic-gate 	 * it may be pointing to mapents that have been freed
208*0Sstevel@tonic-gate 	 */
209*0Sstevel@tonic-gate 	if (rootnode != NULL)
210*0Sstevel@tonic-gate 		free_hiernode(rootnode);
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 	return (mapents);
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate parse_error:
215*0Sstevel@tonic-gate 	syslog(LOG_ERR, "parse_entry: mapentry parse error: map=%s key=%s",
216*0Sstevel@tonic-gate 	    mapname, key);
217*0Sstevel@tonic-gate 	free_mapent(mapents);
218*0Sstevel@tonic-gate 	if (rootnode != NULL)
219*0Sstevel@tonic-gate 		free_hiernode(rootnode);
220*0Sstevel@tonic-gate 	return ((struct mapent *)NULL);
221*0Sstevel@tonic-gate }
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate /*
225*0Sstevel@tonic-gate  * mapline_to_mapent(struct mapent **mapents, struct mapline *ml,
226*0Sstevel@tonic-gate  *		char *key, char *mapname, char *mapopts, char *defaultopts,
227*0Sstevel@tonic-gate  *              uint_t isdirect)
228*0Sstevel@tonic-gate  * Parses the mapline information in ml word by word to build an intermediate
229*0Sstevel@tonic-gate  * mapentry list, which is passed back to the caller. The mapentries may have
230*0Sstevel@tonic-gate  * holes (example no options), as they are completed only later. The logic is
231*0Sstevel@tonic-gate  * awkward, but needed to provide the supported flexibility in the map entries.
232*0Sstevel@tonic-gate  * (especially the first line). Note that the key is the full pathname of the
233*0Sstevel@tonic-gate  * directory to be mounted in a direct map, and ml is the mapentry beyond key.
234*0Sstevel@tonic-gate  * Returns PARSE_OK or an appropriate error value.
235*0Sstevel@tonic-gate  */
236*0Sstevel@tonic-gate static int
237*0Sstevel@tonic-gate mapline_to_mapent(struct mapent **mapents, struct mapline *ml, char *key,
238*0Sstevel@tonic-gate 		char *mapname, char *mapopts, char *defaultopts,
239*0Sstevel@tonic-gate 		uint_t isdirect)
240*0Sstevel@tonic-gate {
241*0Sstevel@tonic-gate 	struct mapent *me = NULL;
242*0Sstevel@tonic-gate 	struct mapent *mp;
243*0Sstevel@tonic-gate 	char w[MAXPATHLEN];
244*0Sstevel@tonic-gate 	char wq[MAXPATHLEN];
245*0Sstevel@tonic-gate 	char w1[MAXPATHLEN];
246*0Sstevel@tonic-gate 	int implied;
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate 	char *lp = ml->linebuf;
249*0Sstevel@tonic-gate 	char *lq = ml->lineqbuf;
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate 	/* do any macro expansions that are required to complete ml */
252*0Sstevel@tonic-gate 	if (macro_expand(key, lp, lq, LINESZ)) {
253*0Sstevel@tonic-gate 		syslog(LOG_ERR,
254*0Sstevel@tonic-gate 		"mapline_to_mapent: map %s: line too long (max %d chars)",
255*0Sstevel@tonic-gate 			mapname, LINESZ - 1);
256*0Sstevel@tonic-gate 		return (PARSE_ERROR);
257*0Sstevel@tonic-gate 	}
258*0Sstevel@tonic-gate 	if (trace > 3 && (strcmp(ml->linebuf, lp) != 0))
259*0Sstevel@tonic-gate 		trace_prt(1,
260*0Sstevel@tonic-gate 			"  mapline_to_mapent: (expanded) mapline (%s,%s)\n",
261*0Sstevel@tonic-gate 			ml->linebuf, ml->lineqbuf);
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate 	/* init the head of mapentry list to null */
264*0Sstevel@tonic-gate 	*mapents = NULL;
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate 	/*
267*0Sstevel@tonic-gate 	 * Get the first word - its either a '-' if default options provided,
268*0Sstevel@tonic-gate 	 * a '/', if the mountroot is implicitly provided, or a mount filesystem
269*0Sstevel@tonic-gate 	 * if the mountroot is implicit. Note that if the first word begins with
270*0Sstevel@tonic-gate 	 * a '-' then the second must be read and it must be a mountpoint or a
271*0Sstevel@tonic-gate 	 * mount filesystem. Use mapopts if no default opts are provided.
272*0Sstevel@tonic-gate 	 */
273*0Sstevel@tonic-gate 	if (getword(w, wq, &lp, &lq, ' ', sizeof (w)) == -1)
274*0Sstevel@tonic-gate 		return (PARSE_ERROR);
275*0Sstevel@tonic-gate 	if (*w == '-') {
276*0Sstevel@tonic-gate 		strcpy(defaultopts, w);
277*0Sstevel@tonic-gate 		if (getword(w, wq, &lp, &lq, ' ', sizeof (w)) == -1)
278*0Sstevel@tonic-gate 			return (PARSE_ERROR);
279*0Sstevel@tonic-gate 	} else
280*0Sstevel@tonic-gate 		strcpy(defaultopts, mapopts);
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate 	implied = *w != '/'; /* implied is 1 only if '/' is implicit */
283*0Sstevel@tonic-gate 	while (*w == '/' || implied) {
284*0Sstevel@tonic-gate 		mp = me;
285*0Sstevel@tonic-gate 		if ((me = (struct mapent *)malloc(sizeof (*me))) == NULL)
286*0Sstevel@tonic-gate 			goto alloc_failed;
287*0Sstevel@tonic-gate 		(void) memset((char *)me, 0, sizeof (*me));
288*0Sstevel@tonic-gate 		if (*mapents == NULL)	/* special case of head */
289*0Sstevel@tonic-gate 			*mapents = me;
290*0Sstevel@tonic-gate 		else
291*0Sstevel@tonic-gate 			mp->map_next = me;
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate 		/*
294*0Sstevel@tonic-gate 		 * direct maps get an empty string as root - to be filled
295*0Sstevel@tonic-gate 		 * by the entire path later. Indirect maps get /key as the
296*0Sstevel@tonic-gate 		 * map root. Note that xfn maps don't care about the root
297*0Sstevel@tonic-gate 		 * - they override it in getmapent_fn().
298*0Sstevel@tonic-gate 		 */
299*0Sstevel@tonic-gate 		if (isdirect) {
300*0Sstevel@tonic-gate 			*w1 = '\0';
301*0Sstevel@tonic-gate 		} else {
302*0Sstevel@tonic-gate 			strcpy(w1, "/");
303*0Sstevel@tonic-gate 			strcat(w1, key);
304*0Sstevel@tonic-gate 		}
305*0Sstevel@tonic-gate 		if ((me->map_root = strdup(w1)) == NULL)
306*0Sstevel@tonic-gate 			goto alloc_failed;
307*0Sstevel@tonic-gate 
308*0Sstevel@tonic-gate 		/* mntpnt is empty for the mount root */
309*0Sstevel@tonic-gate 		if (strcmp(w, "/") == 0 || implied)
310*0Sstevel@tonic-gate 			me->map_mntpnt = strdup("");
311*0Sstevel@tonic-gate 		else
312*0Sstevel@tonic-gate 			me->map_mntpnt = strdup(w);
313*0Sstevel@tonic-gate 		if (me->map_mntpnt == NULL)
314*0Sstevel@tonic-gate 			goto alloc_failed;
315*0Sstevel@tonic-gate 
316*0Sstevel@tonic-gate 		/*
317*0Sstevel@tonic-gate 		 * If implied, the word must be a mount filesystem,
318*0Sstevel@tonic-gate 		 * and its already read in; also turn off implied - its
319*0Sstevel@tonic-gate 		 * not applicable except for the mount root. Else,
320*0Sstevel@tonic-gate 		 * read another (or two) words depending on if there's
321*0Sstevel@tonic-gate 		 * an option.
322*0Sstevel@tonic-gate 		 */
323*0Sstevel@tonic-gate 		if (implied)   /* must be a mount filesystem */
324*0Sstevel@tonic-gate 			implied = 0;
325*0Sstevel@tonic-gate 		else {
326*0Sstevel@tonic-gate 			if (getword(w, wq, &lp, &lq, ' ', sizeof (w)) == -1)
327*0Sstevel@tonic-gate 				return (PARSE_ERROR);
328*0Sstevel@tonic-gate 			if (w[0] == '-') {
329*0Sstevel@tonic-gate 				/* mount options */
330*0Sstevel@tonic-gate 				if ((me->map_mntopts = strdup(w)) == NULL)
331*0Sstevel@tonic-gate 					goto alloc_failed;
332*0Sstevel@tonic-gate 				if (getword(w, wq, &lp, &lq, ' ',
333*0Sstevel@tonic-gate 				    sizeof (w)) == -1)
334*0Sstevel@tonic-gate 					return (PARSE_ERROR);
335*0Sstevel@tonic-gate 			}
336*0Sstevel@tonic-gate 		}
337*0Sstevel@tonic-gate 
338*0Sstevel@tonic-gate 		/*
339*0Sstevel@tonic-gate 		 * must be a mount filesystem or a set of filesystems at
340*0Sstevel@tonic-gate 		 * this point.
341*0Sstevel@tonic-gate 		 */
342*0Sstevel@tonic-gate 		if (w[0] == '\0' || w[0] == '-') {
343*0Sstevel@tonic-gate 			syslog(LOG_ERR,
344*0Sstevel@tonic-gate 			"mapline_to_mapent: bad location=%s map=%s key=%s",
345*0Sstevel@tonic-gate 				w, mapname, key);
346*0Sstevel@tonic-gate 			return (PARSE_ERROR);
347*0Sstevel@tonic-gate 		}
348*0Sstevel@tonic-gate 
349*0Sstevel@tonic-gate 		/*
350*0Sstevel@tonic-gate 		 * map_fsw and map_fswq hold information which will be
351*0Sstevel@tonic-gate 		 * used to determine filesystem information at a later
352*0Sstevel@tonic-gate 		 * point. This is required since we can only find out
353*0Sstevel@tonic-gate 		 * about the mount file system after the directories
354*0Sstevel@tonic-gate 		 * are hierarchically sorted and options have been pushed
355*0Sstevel@tonic-gate 		 * down the hierarchies.
356*0Sstevel@tonic-gate 		 */
357*0Sstevel@tonic-gate 		if (((me->map_fsw = strdup(w)) == NULL) ||
358*0Sstevel@tonic-gate 		    ((me->map_fswq = strdup(wq)) == NULL))
359*0Sstevel@tonic-gate 			goto alloc_failed;
360*0Sstevel@tonic-gate 
361*0Sstevel@tonic-gate 		/*
362*0Sstevel@tonic-gate 		 * the next word, if any, is either another mount point or a
363*0Sstevel@tonic-gate 		 * mount filesystem if more than one server is listed.
364*0Sstevel@tonic-gate 		 */
365*0Sstevel@tonic-gate 		if (getword(w, wq, &lp, &lq, ' ', sizeof (w)) == -1)
366*0Sstevel@tonic-gate 			return (PARSE_ERROR);
367*0Sstevel@tonic-gate 		while (*w && *w != '/') {	/* more than 1 server listed */
368*0Sstevel@tonic-gate 			int len;
369*0Sstevel@tonic-gate 			char *fsw, *fswq;
370*0Sstevel@tonic-gate 			len = strlen(me->map_fsw) + strlen(w) + 4;
371*0Sstevel@tonic-gate 			if ((fsw = (char *)malloc(len)) == NULL)
372*0Sstevel@tonic-gate 				goto alloc_failed;
373*0Sstevel@tonic-gate 			sprintf(fsw, "%s   %s", me->map_fsw, w);
374*0Sstevel@tonic-gate 			free(me->map_fsw);
375*0Sstevel@tonic-gate 			me->map_fsw = fsw;
376*0Sstevel@tonic-gate 			len = strlen(me->map_fswq) + strlen(wq) + 4;
377*0Sstevel@tonic-gate 			if ((fswq = (char *)malloc(len)) == NULL)
378*0Sstevel@tonic-gate 				goto alloc_failed;
379*0Sstevel@tonic-gate 			sprintf(fswq, "%s   %s", me->map_fswq, wq);
380*0Sstevel@tonic-gate 			free(me->map_fswq);
381*0Sstevel@tonic-gate 			me->map_fswq = fswq;
382*0Sstevel@tonic-gate 			if (getword(w, wq, &lp, &lq, ' ', sizeof (w)) == -1)
383*0Sstevel@tonic-gate 				return (PARSE_ERROR);
384*0Sstevel@tonic-gate 		}
385*0Sstevel@tonic-gate 
386*0Sstevel@tonic-gate 		/* initialize flags */
387*0Sstevel@tonic-gate 		me->map_mntlevel = -1;
388*0Sstevel@tonic-gate 		me->map_modified = FALSE;
389*0Sstevel@tonic-gate 		me->map_faked = FALSE;
390*0Sstevel@tonic-gate 		me->map_err = MAPENT_NOERR;
391*0Sstevel@tonic-gate 
392*0Sstevel@tonic-gate 		me->map_next = NULL;
393*0Sstevel@tonic-gate 	}
394*0Sstevel@tonic-gate 
395*0Sstevel@tonic-gate 	if (*mapents == NULL || w[0] != '\0') {	/* sanity check */
396*0Sstevel@tonic-gate 		if (verbose) {
397*0Sstevel@tonic-gate 			if (*mapents == NULL)
398*0Sstevel@tonic-gate 				syslog(LOG_ERR,
399*0Sstevel@tonic-gate 				"mapline_to_mapent: parsed with null mapents");
400*0Sstevel@tonic-gate 			else
401*0Sstevel@tonic-gate 				syslog(LOG_ERR,
402*0Sstevel@tonic-gate 				"mapline_to_mapent: parsed nononempty w=%s", w);
403*0Sstevel@tonic-gate 		}
404*0Sstevel@tonic-gate 		return (PARSE_ERROR);
405*0Sstevel@tonic-gate 	}
406*0Sstevel@tonic-gate 
407*0Sstevel@tonic-gate 	if (trace > 3)
408*0Sstevel@tonic-gate 		trace_mapents("mapline_to_mapent:", *mapents);
409*0Sstevel@tonic-gate 
410*0Sstevel@tonic-gate 	return (PARSE_OK);
411*0Sstevel@tonic-gate 
412*0Sstevel@tonic-gate alloc_failed:
413*0Sstevel@tonic-gate 	syslog(LOG_ERR, "mapline_to_mapent: Memory allocation failed");
414*0Sstevel@tonic-gate 	return (ENOMEM);
415*0Sstevel@tonic-gate }
416*0Sstevel@tonic-gate 
417*0Sstevel@tonic-gate /*
418*0Sstevel@tonic-gate  * hierarchical_sort(struct mapent *mapents, hiernode **rootnode, char *key
419*0Sstevel@tonic-gate  *                   char *mapname)
420*0Sstevel@tonic-gate  * sorts the mntpnts in each mapent to build a hierarchy of nodes, with
421*0Sstevel@tonic-gate  * with the rootnode being the mount root. The hierarchy is setup as
422*0Sstevel@tonic-gate  * levels, and subdirs below each level. Provides a link from node to
423*0Sstevel@tonic-gate  * the relevant mapentry.
424*0Sstevel@tonic-gate  * Returns PARSE_OK or appropriate error value
425*0Sstevel@tonic-gate  */
426*0Sstevel@tonic-gate static int
427*0Sstevel@tonic-gate hierarchical_sort(struct mapent *mapents, hiernode **rootnode, char *key,
428*0Sstevel@tonic-gate 	char *mapname)
429*0Sstevel@tonic-gate {
430*0Sstevel@tonic-gate 	hiernode *prevnode, *currnode, *newnode;
431*0Sstevel@tonic-gate 	char *path;
432*0Sstevel@tonic-gate 	char dirname[MAXFILENAMELEN];
433*0Sstevel@tonic-gate 
434*0Sstevel@tonic-gate 	int rc = PARSE_OK;
435*0Sstevel@tonic-gate 	struct mapent *me = mapents;
436*0Sstevel@tonic-gate 
437*0Sstevel@tonic-gate 	/* allocate the rootnode with a default path of "" */
438*0Sstevel@tonic-gate 	*rootnode = NULL;
439*0Sstevel@tonic-gate 	if ((rc = alloc_hiernode(rootnode, "")) != PARSE_OK)
440*0Sstevel@tonic-gate 		return (rc);
441*0Sstevel@tonic-gate 
442*0Sstevel@tonic-gate 	/*
443*0Sstevel@tonic-gate 	 * walk through mapents - for each mapent, locate the position
444*0Sstevel@tonic-gate 	 * within the hierarchy by walking across leveldirs, and
445*0Sstevel@tonic-gate 	 * subdirs of matched leveldirs. Starts one level below
446*0Sstevel@tonic-gate 	 * the root (assumes an implicit match with rootnode).
447*0Sstevel@tonic-gate 	 * XXX - this could probably be done more cleanly using recursion.
448*0Sstevel@tonic-gate 	 */
449*0Sstevel@tonic-gate 	while (me != NULL) {
450*0Sstevel@tonic-gate 
451*0Sstevel@tonic-gate 		path = me->map_mntpnt;
452*0Sstevel@tonic-gate 
453*0Sstevel@tonic-gate 		if ((rc = get_dir_from_path(dirname, &path,
454*0Sstevel@tonic-gate 		    sizeof (dirname))) != PARSE_OK)
455*0Sstevel@tonic-gate 			return (rc);
456*0Sstevel@tonic-gate 
457*0Sstevel@tonic-gate 		prevnode = *rootnode;
458*0Sstevel@tonic-gate 		currnode = (*rootnode)->subdir;
459*0Sstevel@tonic-gate 
460*0Sstevel@tonic-gate 		while (dirname[0] != '\0') {
461*0Sstevel@tonic-gate 			if (currnode != NULL) {
462*0Sstevel@tonic-gate 				if (strcmp(currnode->dirname, dirname) == 0) {
463*0Sstevel@tonic-gate 					/*
464*0Sstevel@tonic-gate 					 * match found - mntpnt is a child of
465*0Sstevel@tonic-gate 					 * this node
466*0Sstevel@tonic-gate 					 */
467*0Sstevel@tonic-gate 					prevnode = currnode;
468*0Sstevel@tonic-gate 					currnode = currnode->subdir;
469*0Sstevel@tonic-gate 				} else {
470*0Sstevel@tonic-gate 					prevnode = currnode;
471*0Sstevel@tonic-gate 					currnode = currnode->leveldir;
472*0Sstevel@tonic-gate 
473*0Sstevel@tonic-gate 					if (currnode == NULL) {
474*0Sstevel@tonic-gate 						/*
475*0Sstevel@tonic-gate 						 * No more leveldirs to match.
476*0Sstevel@tonic-gate 						 * Add a new one
477*0Sstevel@tonic-gate 						 */
478*0Sstevel@tonic-gate 						if ((rc = alloc_hiernode
479*0Sstevel@tonic-gate 							(&newnode, dirname))
480*0Sstevel@tonic-gate 							!= PARSE_OK)
481*0Sstevel@tonic-gate 							return (rc);
482*0Sstevel@tonic-gate 						prevnode->leveldir = newnode;
483*0Sstevel@tonic-gate 						prevnode = newnode;
484*0Sstevel@tonic-gate 						currnode = newnode->subdir;
485*0Sstevel@tonic-gate 					} else {
486*0Sstevel@tonic-gate 						/* try this leveldir */
487*0Sstevel@tonic-gate 						continue;
488*0Sstevel@tonic-gate 					}
489*0Sstevel@tonic-gate 				}
490*0Sstevel@tonic-gate 			} else {
491*0Sstevel@tonic-gate 				/* no more subdirs to match. Add a new one */
492*0Sstevel@tonic-gate 				if ((rc = alloc_hiernode(&newnode,
493*0Sstevel@tonic-gate 				    dirname)) != PARSE_OK)
494*0Sstevel@tonic-gate 					return (rc);
495*0Sstevel@tonic-gate 				prevnode->subdir = newnode;
496*0Sstevel@tonic-gate 				prevnode = newnode;
497*0Sstevel@tonic-gate 				currnode = newnode->subdir;
498*0Sstevel@tonic-gate 			}
499*0Sstevel@tonic-gate 			if ((rc = get_dir_from_path(dirname, &path,
500*0Sstevel@tonic-gate 			    sizeof (dirname))) != PARSE_OK)
501*0Sstevel@tonic-gate 				return (rc);
502*0Sstevel@tonic-gate 		}
503*0Sstevel@tonic-gate 
504*0Sstevel@tonic-gate 		if (prevnode->mapent != NULL) {
505*0Sstevel@tonic-gate 			/* duplicate mntpoint found */
506*0Sstevel@tonic-gate 			syslog(LOG_ERR,
507*0Sstevel@tonic-gate 			"hierarchical_sort: duplicate mntpnt map=%s key=%s",
508*0Sstevel@tonic-gate 				mapname, key);
509*0Sstevel@tonic-gate 			return (PARSE_ERROR);
510*0Sstevel@tonic-gate 		}
511*0Sstevel@tonic-gate 
512*0Sstevel@tonic-gate 		/* provide a pointer from node to mapent */
513*0Sstevel@tonic-gate 		prevnode->mapent = me;
514*0Sstevel@tonic-gate 		me = me->map_next;
515*0Sstevel@tonic-gate 	}
516*0Sstevel@tonic-gate 
517*0Sstevel@tonic-gate 	if (trace > 3) {
518*0Sstevel@tonic-gate 		trace_prt(1, "\n\thierarchical_sort:\n");
519*0Sstevel@tonic-gate 		trace_hierarchy(*rootnode, 0);	/* 0 is rootnode's level */
520*0Sstevel@tonic-gate 	}
521*0Sstevel@tonic-gate 
522*0Sstevel@tonic-gate 	return (rc);
523*0Sstevel@tonic-gate }
524*0Sstevel@tonic-gate 
525*0Sstevel@tonic-gate /*
526*0Sstevel@tonic-gate  * push_options(hiernode *node, char *opts, char *mapopts, int err)
527*0Sstevel@tonic-gate  * Pushes the options down a hierarchical structure. Works recursively from the
528*0Sstevel@tonic-gate  * root, which is passed in on the first call. Uses a replacement policy.
529*0Sstevel@tonic-gate  * If a node points to a mapentry, and it has an option, then thats the option
530*0Sstevel@tonic-gate  * for that mapentry. Else, the node's mapent inherits the option from the
531*0Sstevel@tonic-gate  * default (which may be the global option for the entry or mapopts).
532*0Sstevel@tonic-gate  * err is useful in flagging entries with errors in pushing options.
533*0Sstevel@tonic-gate  * returns PARSE_OK or appropriate error value.
534*0Sstevel@tonic-gate  */
535*0Sstevel@tonic-gate static int
536*0Sstevel@tonic-gate push_options(hiernode *node, char *defaultopts, char *mapopts, int err)
537*0Sstevel@tonic-gate {
538*0Sstevel@tonic-gate 	int rc = PARSE_OK;
539*0Sstevel@tonic-gate 	struct mapent *me = NULL;
540*0Sstevel@tonic-gate 
541*0Sstevel@tonic-gate 	/* ensure that all the dirs at a level are passed the default options */
542*0Sstevel@tonic-gate 	while (node != NULL) {
543*0Sstevel@tonic-gate 		me = node->mapent;
544*0Sstevel@tonic-gate 		if (me != NULL) {	/* not all nodes point to a mapentry */
545*0Sstevel@tonic-gate 			me->map_err = err;
546*0Sstevel@tonic-gate 			if ((rc = set_mapent_opts(me, me->map_mntopts,
547*0Sstevel@tonic-gate 				defaultopts, mapopts)) != PARSE_OK)
548*0Sstevel@tonic-gate 				return (rc);
549*0Sstevel@tonic-gate 		}
550*0Sstevel@tonic-gate 
551*0Sstevel@tonic-gate 		/* push the options to subdirs */
552*0Sstevel@tonic-gate 		if (node->subdir != NULL) {
553*0Sstevel@tonic-gate 			if (node->mapent && strcmp(node->mapent->map_fstype,
554*0Sstevel@tonic-gate 				MNTTYPE_AUTOFS) == 0)
555*0Sstevel@tonic-gate 				err = MAPENT_UATFS;
556*0Sstevel@tonic-gate 			if ((rc = push_options(node->subdir, defaultopts,
557*0Sstevel@tonic-gate 				mapopts, err)) != PARSE_OK)
558*0Sstevel@tonic-gate 				return (rc);
559*0Sstevel@tonic-gate 		}
560*0Sstevel@tonic-gate 		node = node->leveldir;
561*0Sstevel@tonic-gate 	}
562*0Sstevel@tonic-gate 	return (rc);
563*0Sstevel@tonic-gate }
564*0Sstevel@tonic-gate 
565*0Sstevel@tonic-gate #define	BACKFSTYPE "backfstype" /* used in cachefs options */
566*0Sstevel@tonic-gate #define	BACKFSTYPE_EQ "backfstype="
567*0Sstevel@tonic-gate #define	FSTYPE "fstype"
568*0Sstevel@tonic-gate #define	FSTYPE_EQ "fstype="
569*0Sstevel@tonic-gate #define	NO_OPTS ""
570*0Sstevel@tonic-gate 
571*0Sstevel@tonic-gate /*
572*0Sstevel@tonic-gate  * set_mapent_opts(struct mapent *me, char *opts, char *defaultopts,
573*0Sstevel@tonic-gate  *			char *mapopts)
574*0Sstevel@tonic-gate  * sets the mapentry's options, fstype and mounter fields by separating
575*0Sstevel@tonic-gate  * out the fstype part from the opts. Use default options if opts is NULL.
576*0Sstevel@tonic-gate  * Note taht defaultopts may be the same as mapopts.
577*0Sstevel@tonic-gate  * Returns PARSE_OK or appropriate error value.
578*0Sstevel@tonic-gate  */
579*0Sstevel@tonic-gate static int
580*0Sstevel@tonic-gate set_mapent_opts(struct mapent *me, char *opts, char *defaultopts,
581*0Sstevel@tonic-gate 		char *mapopts)
582*0Sstevel@tonic-gate {
583*0Sstevel@tonic-gate 	char entryopts[AUTOFS_MAXOPTSLEN];
584*0Sstevel@tonic-gate 	char fstype[MAX_FSLEN], mounter[MAX_FSLEN];
585*0Sstevel@tonic-gate 	int rc = PARSE_OK;
586*0Sstevel@tonic-gate 	bool_t fstype_opt = FALSE;
587*0Sstevel@tonic-gate 
588*0Sstevel@tonic-gate 	strcpy(fstype, MNTTYPE_NFS);		/* default */
589*0Sstevel@tonic-gate 
590*0Sstevel@tonic-gate 	/* set options to default options, if none exist for this entry */
591*0Sstevel@tonic-gate 	if (opts == NULL) {
592*0Sstevel@tonic-gate 		opts = defaultopts;
593*0Sstevel@tonic-gate 		if (defaultopts == NULL) { /* NULL opts for entry */
594*0Sstevel@tonic-gate 			strcpy(mounter, fstype);
595*0Sstevel@tonic-gate 			goto done;
596*0Sstevel@tonic-gate 		}
597*0Sstevel@tonic-gate 	}
598*0Sstevel@tonic-gate 	if (*opts == '-')
599*0Sstevel@tonic-gate 		opts++;
600*0Sstevel@tonic-gate 
601*0Sstevel@tonic-gate 	/* separate opts into fstype and (other) entrypopts */
602*0Sstevel@tonic-gate 	get_opts(opts,	entryopts, fstype, &fstype_opt);
603*0Sstevel@tonic-gate 
604*0Sstevel@tonic-gate 	/* replace any existing opts */
605*0Sstevel@tonic-gate 	if (me->map_mntopts != NULL)
606*0Sstevel@tonic-gate 		free(me->map_mntopts);
607*0Sstevel@tonic-gate 	if ((me->map_mntopts = strdup(entryopts)) == NULL)
608*0Sstevel@tonic-gate 		return (ENOMEM);
609*0Sstevel@tonic-gate 	strcpy(mounter,	fstype);
610*0Sstevel@tonic-gate 
611*0Sstevel@tonic-gate 	/*
612*0Sstevel@tonic-gate 	 * The following ugly chunk of code crept in as a result of
613*0Sstevel@tonic-gate 	 * cachefs.  If it's a cachefs mount of an nfs filesystem, then
614*0Sstevel@tonic-gate 	 * it's important to parse the nfs special field.  Otherwise,
615*0Sstevel@tonic-gate 	 * just hand the special field to the fs-specific mount
616*0Sstevel@tonic-gate 	 */
617*0Sstevel@tonic-gate 	if (strcmp(fstype, MNTTYPE_CACHEFS) ==  0) {
618*0Sstevel@tonic-gate 		struct mnttab m;
619*0Sstevel@tonic-gate 		char *p;
620*0Sstevel@tonic-gate 
621*0Sstevel@tonic-gate 		m.mnt_mntopts = entryopts;
622*0Sstevel@tonic-gate 		if ((p = hasmntopt(&m, BACKFSTYPE)) != NULL) {
623*0Sstevel@tonic-gate 			int len = strlen(MNTTYPE_NFS);
624*0Sstevel@tonic-gate 
625*0Sstevel@tonic-gate 			p += strlen(BACKFSTYPE_EQ);
626*0Sstevel@tonic-gate 
627*0Sstevel@tonic-gate 			if (strncmp(p, MNTTYPE_NFS, len) ==  0 &&
628*0Sstevel@tonic-gate 				(p[len] == '\0' || p[len] == ',')) {
629*0Sstevel@tonic-gate 				/*
630*0Sstevel@tonic-gate 				 * Cached nfs mount
631*0Sstevel@tonic-gate 				 */
632*0Sstevel@tonic-gate 				(void) strcpy(fstype, MNTTYPE_NFS);
633*0Sstevel@tonic-gate 				(void) strcpy(mounter, MNTTYPE_CACHEFS);
634*0Sstevel@tonic-gate 			}
635*0Sstevel@tonic-gate 		}
636*0Sstevel@tonic-gate 	}
637*0Sstevel@tonic-gate 
638*0Sstevel@tonic-gate 	/*
639*0Sstevel@tonic-gate 	 * child options are exactly fstype = somefs, we need to do some
640*0Sstevel@tonic-gate 	 * more option pushing work.
641*0Sstevel@tonic-gate 	 */
642*0Sstevel@tonic-gate 	if (fstype_opt == TRUE &&
643*0Sstevel@tonic-gate 		(strcmp(me->map_mntopts, NO_OPTS) == 0)) {
644*0Sstevel@tonic-gate 		free(me->map_mntopts);
645*0Sstevel@tonic-gate 		if ((rc = fstype_opts(me, opts, defaultopts,
646*0Sstevel@tonic-gate 		    mapopts)) != PARSE_OK)
647*0Sstevel@tonic-gate 			return (rc);
648*0Sstevel@tonic-gate 	}
649*0Sstevel@tonic-gate 
650*0Sstevel@tonic-gate done:
651*0Sstevel@tonic-gate 	if (((me->map_fstype = strdup(fstype)) == NULL) ||
652*0Sstevel@tonic-gate 		((me->map_mounter = strdup(mounter)) == NULL)) {
653*0Sstevel@tonic-gate 		if (me->map_fstype != NULL)
654*0Sstevel@tonic-gate 			free(me->map_fstype);
655*0Sstevel@tonic-gate 		syslog(LOG_ERR, "set_mapent_opts: No memory");
656*0Sstevel@tonic-gate 		return (ENOMEM);
657*0Sstevel@tonic-gate 	}
658*0Sstevel@tonic-gate 
659*0Sstevel@tonic-gate 	return (rc);
660*0Sstevel@tonic-gate }
661*0Sstevel@tonic-gate 
662*0Sstevel@tonic-gate /*
663*0Sstevel@tonic-gate  * Check the option string for an "fstype"
664*0Sstevel@tonic-gate  * option.  If found, return the fstype
665*0Sstevel@tonic-gate  * and the option string with the fstype
666*0Sstevel@tonic-gate  * option removed, e.g.
667*0Sstevel@tonic-gate  *
668*0Sstevel@tonic-gate  *  input:  "fstype=cachefs,ro,nosuid"
669*0Sstevel@tonic-gate  *  opts:   "ro,nosuid"
670*0Sstevel@tonic-gate  *  fstype: "cachefs"
671*0Sstevel@tonic-gate  *
672*0Sstevel@tonic-gate  * Also indicates if the fstype option was present
673*0Sstevel@tonic-gate  * by setting a flag, if the pointer to the flag
674*0Sstevel@tonic-gate  * is not NULL.
675*0Sstevel@tonic-gate  */
676*0Sstevel@tonic-gate static void
677*0Sstevel@tonic-gate get_opts(input, opts, fstype, fstype_opt)
678*0Sstevel@tonic-gate 	char *input;
679*0Sstevel@tonic-gate 	char *opts; 	/* output */
680*0Sstevel@tonic-gate 	char *fstype;   /* output */
681*0Sstevel@tonic-gate 	bool_t *fstype_opt;
682*0Sstevel@tonic-gate {
683*0Sstevel@tonic-gate 	char *p, *pb;
684*0Sstevel@tonic-gate 	char buf[MAXOPTSLEN];
685*0Sstevel@tonic-gate 	char *placeholder;
686*0Sstevel@tonic-gate 
687*0Sstevel@tonic-gate 	*opts = '\0';
688*0Sstevel@tonic-gate 	(void) strcpy(buf, input);
689*0Sstevel@tonic-gate 	pb = buf;
690*0Sstevel@tonic-gate 	while (p = (char *)strtok_r(pb, ",", &placeholder)) {
691*0Sstevel@tonic-gate 		pb = NULL;
692*0Sstevel@tonic-gate 		if (strncmp(p, FSTYPE_EQ, 7) == 0) {
693*0Sstevel@tonic-gate 			if (fstype_opt != NULL)
694*0Sstevel@tonic-gate 				*fstype_opt = TRUE;
695*0Sstevel@tonic-gate 			(void) strcpy(fstype, p + 7);
696*0Sstevel@tonic-gate 		} else {
697*0Sstevel@tonic-gate 			if (*opts)
698*0Sstevel@tonic-gate 				(void) strcat(opts, ",");
699*0Sstevel@tonic-gate 			(void) strcat(opts, p);
700*0Sstevel@tonic-gate 		}
701*0Sstevel@tonic-gate 	}
702*0Sstevel@tonic-gate }
703*0Sstevel@tonic-gate 
704*0Sstevel@tonic-gate /*
705*0Sstevel@tonic-gate  * fstype_opts(struct mapent *me, char *opts, char *defaultopts,
706*0Sstevel@tonic-gate  *				char *mapopts)
707*0Sstevel@tonic-gate  * We need to push global options to the child entry if it is exactly
708*0Sstevel@tonic-gate  * fstype=somefs.
709*0Sstevel@tonic-gate  */
710*0Sstevel@tonic-gate static int
711*0Sstevel@tonic-gate fstype_opts(struct mapent *me, char *opts, char *defaultopts,
712*0Sstevel@tonic-gate 				char *mapopts)
713*0Sstevel@tonic-gate {
714*0Sstevel@tonic-gate 	char pushopts[AUTOFS_MAXOPTSLEN];
715*0Sstevel@tonic-gate 	char pushentryopts[AUTOFS_MAXOPTSLEN];
716*0Sstevel@tonic-gate 	char pushfstype[MAX_FSLEN];
717*0Sstevel@tonic-gate 
718*0Sstevel@tonic-gate 	if (defaultopts && *defaultopts == '-')
719*0Sstevel@tonic-gate 		defaultopts++;
720*0Sstevel@tonic-gate 
721*0Sstevel@tonic-gate 	/*
722*0Sstevel@tonic-gate 	 * the options to push are the global defaults for the entry,
723*0Sstevel@tonic-gate 	 * if they exist, or mapopts, if the global defaults for the
724*0Sstevel@tonic-gate 	 * entry does not exist.
725*0Sstevel@tonic-gate 	 */
726*0Sstevel@tonic-gate 	if (strcmp(defaultopts, opts) == 0) {
727*0Sstevel@tonic-gate 		if (*mapopts == '-')
728*0Sstevel@tonic-gate 			mapopts++;
729*0Sstevel@tonic-gate 		get_opts(mapopts, pushentryopts, pushfstype, NULL);
730*0Sstevel@tonic-gate 		strcpy(pushopts, mapopts);
731*0Sstevel@tonic-gate 	} else {
732*0Sstevel@tonic-gate 		get_opts(defaultopts, pushentryopts, pushfstype, NULL);
733*0Sstevel@tonic-gate 		strcpy(pushopts, defaultopts);
734*0Sstevel@tonic-gate 	}
735*0Sstevel@tonic-gate 
736*0Sstevel@tonic-gate 	if (strcmp(pushfstype, MNTTYPE_CACHEFS) == 0)
737*0Sstevel@tonic-gate 		me->map_mntopts = strdup(pushopts);
738*0Sstevel@tonic-gate 	else
739*0Sstevel@tonic-gate 		me->map_mntopts = strdup(pushentryopts);
740*0Sstevel@tonic-gate 
741*0Sstevel@tonic-gate 	if (!me->map_mntopts) {
742*0Sstevel@tonic-gate 		syslog(LOG_ERR, "fstype_opts: No memory");
743*0Sstevel@tonic-gate 		return (ENOMEM);
744*0Sstevel@tonic-gate 	}
745*0Sstevel@tonic-gate 
746*0Sstevel@tonic-gate 	return (PARSE_OK);
747*0Sstevel@tonic-gate }
748*0Sstevel@tonic-gate 
749*0Sstevel@tonic-gate /*
750*0Sstevel@tonic-gate  * modify_mapents(struct mapent **mapents, char *mapname,
751*0Sstevel@tonic-gate  *			char *mapopts, char *subdir, hiernode *rootnode,
752*0Sstevel@tonic-gate  * 			char *key, uint_t isdirect, bool_t mount_access)
753*0Sstevel@tonic-gate  * modifies the intermediate mapentry list into the final one, and passes
754*0Sstevel@tonic-gate  * back a pointer to it. The final list may contain faked mapentries for
755*0Sstevel@tonic-gate  * hiernodes that do not point to a mapentry, or converted mapentries, if
756*0Sstevel@tonic-gate  * hiernodes that point to a mapentry need to be converted from nfs to autofs.
757*0Sstevel@tonic-gate  * mounts. Entries that are not directly 1 level below the subdir are removed.
758*0Sstevel@tonic-gate  * Returns PARSE_OK or PARSE_ERROR
759*0Sstevel@tonic-gate  */
760*0Sstevel@tonic-gate static int
761*0Sstevel@tonic-gate modify_mapents(struct mapent **mapents, char *mapname,
762*0Sstevel@tonic-gate 			char *mapopts, char *subdir, hiernode *rootnode,
763*0Sstevel@tonic-gate 			char *key, uint_t isdirect, bool_t mount_access)
764*0Sstevel@tonic-gate {
765*0Sstevel@tonic-gate 	struct mapent *mp = NULL;
766*0Sstevel@tonic-gate 	char w[MAXPATHLEN];
767*0Sstevel@tonic-gate 
768*0Sstevel@tonic-gate 	struct mapent *me;
769*0Sstevel@tonic-gate 	int rc = PARSE_OK;
770*0Sstevel@tonic-gate 	struct mapent *faked_mapents = NULL;
771*0Sstevel@tonic-gate 
772*0Sstevel@tonic-gate 	/*
773*0Sstevel@tonic-gate 	 * correct the mapentry mntlevel from default -1 to level depending on
774*0Sstevel@tonic-gate 	 * position in hierarchy, and build any faked mapentries, if required
775*0Sstevel@tonic-gate 	 * at one level below the rootnode given by subdir.
776*0Sstevel@tonic-gate 	 */
777*0Sstevel@tonic-gate 	if ((rc = set_and_fake_mapent_mntlevel(rootnode, subdir, key, mapname,
778*0Sstevel@tonic-gate 		&faked_mapents, isdirect, mapopts, mount_access)) != PARSE_OK)
779*0Sstevel@tonic-gate 		return (rc);
780*0Sstevel@tonic-gate 
781*0Sstevel@tonic-gate 	/*
782*0Sstevel@tonic-gate 	 * attaches faked mapents to real mapents list. Assumes mapents
783*0Sstevel@tonic-gate 	 * is not NULL.
784*0Sstevel@tonic-gate 	 */
785*0Sstevel@tonic-gate 	me = *mapents;
786*0Sstevel@tonic-gate 	while (me->map_next != NULL)
787*0Sstevel@tonic-gate 		me = me->map_next;
788*0Sstevel@tonic-gate 	me->map_next = faked_mapents;
789*0Sstevel@tonic-gate 
790*0Sstevel@tonic-gate 	/*
791*0Sstevel@tonic-gate 	 * get rid of nodes marked at level -1
792*0Sstevel@tonic-gate 	 */
793*0Sstevel@tonic-gate 	me = *mapents;
794*0Sstevel@tonic-gate 	while (me != NULL) {
795*0Sstevel@tonic-gate 		if ((me->map_mntlevel ==  -1) || (me->map_err) ||
796*0Sstevel@tonic-gate 			(mount_access == FALSE && me->map_mntlevel == 0)) {
797*0Sstevel@tonic-gate 			/*
798*0Sstevel@tonic-gate 			 * syslog any errors and free entry
799*0Sstevel@tonic-gate 			 */
800*0Sstevel@tonic-gate 			if (me->map_err)
801*0Sstevel@tonic-gate 				dump_mapent_err(me, key, mapname);
802*0Sstevel@tonic-gate 
803*0Sstevel@tonic-gate 			if (me ==  (*mapents)) {
804*0Sstevel@tonic-gate 				/* special case when head has to be freed */
805*0Sstevel@tonic-gate 				*mapents = me->map_next;
806*0Sstevel@tonic-gate 				if ((*mapents) ==  NULL) {
807*0Sstevel@tonic-gate 					/* something wierd happened */
808*0Sstevel@tonic-gate 					if (verbose)
809*0Sstevel@tonic-gate 						syslog(LOG_ERR,
810*0Sstevel@tonic-gate 						"modify_mapents: level error");
811*0Sstevel@tonic-gate 					return (PARSE_ERROR);
812*0Sstevel@tonic-gate 				}
813*0Sstevel@tonic-gate 
814*0Sstevel@tonic-gate 				/* separate out the node */
815*0Sstevel@tonic-gate 				me->map_next = NULL;
816*0Sstevel@tonic-gate 				free_mapent(me);
817*0Sstevel@tonic-gate 				me = *mapents;
818*0Sstevel@tonic-gate 			} else {
819*0Sstevel@tonic-gate 				mp->map_next = me->map_next;
820*0Sstevel@tonic-gate 				me->map_next = NULL;
821*0Sstevel@tonic-gate 				free_mapent(me);
822*0Sstevel@tonic-gate 				me = mp->map_next;
823*0Sstevel@tonic-gate 			}
824*0Sstevel@tonic-gate 			continue;
825*0Sstevel@tonic-gate 		}
826*0Sstevel@tonic-gate 
827*0Sstevel@tonic-gate 		/*
828*0Sstevel@tonic-gate 		 * convert level 1 mapents that are not already autonodes
829*0Sstevel@tonic-gate 		 * to autonodes
830*0Sstevel@tonic-gate 		 */
831*0Sstevel@tonic-gate 		if (me->map_mntlevel == 1 &&
832*0Sstevel@tonic-gate 			(strcmp(me->map_fstype, MNTTYPE_AUTOFS) != 0) &&
833*0Sstevel@tonic-gate 			(me->map_faked != TRUE)) {
834*0Sstevel@tonic-gate 			if ((rc = convert_mapent_to_automount(me, mapname,
835*0Sstevel@tonic-gate 			    mapopts)) != PARSE_OK)
836*0Sstevel@tonic-gate 				return (rc);
837*0Sstevel@tonic-gate 		}
838*0Sstevel@tonic-gate 		strcpy(w, (me->map_mntpnt+strlen(subdir)));
839*0Sstevel@tonic-gate 		strcpy(me->map_mntpnt, w);
840*0Sstevel@tonic-gate 		mp = me;
841*0Sstevel@tonic-gate 		me = me->map_next;
842*0Sstevel@tonic-gate 	}
843*0Sstevel@tonic-gate 
844*0Sstevel@tonic-gate 	if (trace > 3)
845*0Sstevel@tonic-gate 		trace_mapents("modify_mapents:", *mapents);
846*0Sstevel@tonic-gate 
847*0Sstevel@tonic-gate 	return (PARSE_OK);
848*0Sstevel@tonic-gate }
849*0Sstevel@tonic-gate 
850*0Sstevel@tonic-gate /*
851*0Sstevel@tonic-gate  * set_and_fake_mapent_mntlevel(hiernode *rootnode, char *subdir, char *key,
852*0Sstevel@tonic-gate  *			char *mapname, struct mapent **faked_mapents,
853*0Sstevel@tonic-gate  *			uint_t isdirect, char *mapopts, bool_t mount_access)
854*0Sstevel@tonic-gate  * sets the mapentry mount levels (depths) with respect to the subdir.
855*0Sstevel@tonic-gate  * Assigns a value of 0 to the new root. Finds the level1 directories by
856*0Sstevel@tonic-gate  * calling mark_*_level1_*(). Also cleans off extra /'s in level0 and
857*0Sstevel@tonic-gate  * level1 map_mntpnts. Note that one level below the new root is an existing
858*0Sstevel@tonic-gate  * mapentry if there's a mapentry (nfs mount) corresponding to the root,
859*0Sstevel@tonic-gate  * and the direct subdir set for the root, if there's no mapentry corresponding
860*0Sstevel@tonic-gate  * to the root (we install autodirs). Returns PARSE_OK or error value.
861*0Sstevel@tonic-gate  */
862*0Sstevel@tonic-gate static int
863*0Sstevel@tonic-gate set_and_fake_mapent_mntlevel(hiernode *rootnode, char *subdir, char *key,
864*0Sstevel@tonic-gate 		char *mapname, struct mapent **faked_mapents,
865*0Sstevel@tonic-gate 		uint_t isdirect, char *mapopts, bool_t mount_access)
866*0Sstevel@tonic-gate {
867*0Sstevel@tonic-gate 	char dirname[MAXFILENAMELEN];
868*0Sstevel@tonic-gate 	char traversed_path[MAXPATHLEN]; /* used in building fake mapentries */
869*0Sstevel@tonic-gate 
870*0Sstevel@tonic-gate 	char *subdir_child = subdir;
871*0Sstevel@tonic-gate 	hiernode *prevnode = rootnode;
872*0Sstevel@tonic-gate 	hiernode *currnode = rootnode->subdir;
873*0Sstevel@tonic-gate 	int rc = PARSE_OK;
874*0Sstevel@tonic-gate 	traversed_path[0] = '\0';
875*0Sstevel@tonic-gate 
876*0Sstevel@tonic-gate 	/*
877*0Sstevel@tonic-gate 	 * find and mark the root by tracing down subdir. Use traversed_path
878*0Sstevel@tonic-gate 	 * to keep track of how far we go, while guaranteeing that it
879*0Sstevel@tonic-gate 	 * contains no '/' at the end. Took some mucking to get that right.
880*0Sstevel@tonic-gate 	 */
881*0Sstevel@tonic-gate 	if ((rc = get_dir_from_path(dirname, &subdir_child, sizeof (dirname)))
882*0Sstevel@tonic-gate 				!= PARSE_OK)
883*0Sstevel@tonic-gate 		return (rc);
884*0Sstevel@tonic-gate 
885*0Sstevel@tonic-gate 	if (dirname[0] != '\0')
886*0Sstevel@tonic-gate 		sprintf(traversed_path, "%s/%s", traversed_path, dirname);
887*0Sstevel@tonic-gate 
888*0Sstevel@tonic-gate 	prevnode = rootnode;
889*0Sstevel@tonic-gate 	currnode = rootnode->subdir;
890*0Sstevel@tonic-gate 	while (dirname[0] != '\0' && currnode != NULL) {
891*0Sstevel@tonic-gate 		if (strcmp(currnode->dirname, dirname) == 0) {
892*0Sstevel@tonic-gate 
893*0Sstevel@tonic-gate 			/* subdir is a child of currnode */
894*0Sstevel@tonic-gate 			prevnode = currnode;
895*0Sstevel@tonic-gate 			currnode = currnode->subdir;
896*0Sstevel@tonic-gate 
897*0Sstevel@tonic-gate 			if ((rc = get_dir_from_path(dirname, &subdir_child,
898*0Sstevel@tonic-gate 			    sizeof (dirname))) != PARSE_OK)
899*0Sstevel@tonic-gate 				return (rc);
900*0Sstevel@tonic-gate 			if (dirname[0] != '\0')
901*0Sstevel@tonic-gate 				sprintf(traversed_path, "%s/%s",
902*0Sstevel@tonic-gate 					traversed_path, dirname);
903*0Sstevel@tonic-gate 
904*0Sstevel@tonic-gate 		} else {
905*0Sstevel@tonic-gate 			/* try next leveldir */
906*0Sstevel@tonic-gate 			prevnode = currnode;
907*0Sstevel@tonic-gate 			currnode = currnode->leveldir;
908*0Sstevel@tonic-gate 		}
909*0Sstevel@tonic-gate 	}
910*0Sstevel@tonic-gate 
911*0Sstevel@tonic-gate 	if (dirname[0] != '\0') {
912*0Sstevel@tonic-gate 		if (verbose)
913*0Sstevel@tonic-gate 			syslog(LOG_ERR,
914*0Sstevel@tonic-gate 			"set_and_fake_mapent_mntlevel: subdir=%s error: map=%s",
915*0Sstevel@tonic-gate 			    subdir, mapname);
916*0Sstevel@tonic-gate 		return (PARSE_ERROR);
917*0Sstevel@tonic-gate 	}
918*0Sstevel@tonic-gate 
919*0Sstevel@tonic-gate 	/*
920*0Sstevel@tonic-gate 	 * see if level of root really points to a mapent and if
921*0Sstevel@tonic-gate 	 * have access to that filessystem - call appropriate
922*0Sstevel@tonic-gate 	 * routine to mark level 1 nodes, and build faked entries
923*0Sstevel@tonic-gate 	 */
924*0Sstevel@tonic-gate 	if (prevnode->mapent != NULL && mount_access == TRUE) {
925*0Sstevel@tonic-gate 		if (trace > 3)
926*0Sstevel@tonic-gate 			trace_prt(1, "  node mountpoint %s\t travpath=%s\n",
927*0Sstevel@tonic-gate 				prevnode->mapent->map_mntpnt, traversed_path);
928*0Sstevel@tonic-gate 
929*0Sstevel@tonic-gate 		/*
930*0Sstevel@tonic-gate 		 * Copy traversed path map_mntpnt to get rid of any extra
931*0Sstevel@tonic-gate 		 * '/' the map entry may contain.
932*0Sstevel@tonic-gate 		 */
933*0Sstevel@tonic-gate 		if (strlen(prevnode->mapent->map_mntpnt) <
934*0Sstevel@tonic-gate 				strlen(traversed_path)) { /* sanity check */
935*0Sstevel@tonic-gate 			if (verbose)
936*0Sstevel@tonic-gate 				syslog(LOG_ERR,
937*0Sstevel@tonic-gate 				"set_and_fake_mapent_mntlevel: path=%s error",
938*0Sstevel@tonic-gate 				    traversed_path);
939*0Sstevel@tonic-gate 			return (PARSE_ERROR);
940*0Sstevel@tonic-gate 		}
941*0Sstevel@tonic-gate 		if (strcmp(prevnode->mapent->map_mntpnt, traversed_path) != 0)
942*0Sstevel@tonic-gate 			strcpy(prevnode->mapent->map_mntpnt, traversed_path);
943*0Sstevel@tonic-gate 
944*0Sstevel@tonic-gate 		prevnode->mapent->map_mntlevel = 0; /* root level is 0 */
945*0Sstevel@tonic-gate 		if (currnode != NULL) {
946*0Sstevel@tonic-gate 			if ((rc = mark_level1_root(currnode,
947*0Sstevel@tonic-gate 			    traversed_path)) != PARSE_OK)
948*0Sstevel@tonic-gate 				return (rc);
949*0Sstevel@tonic-gate 		}
950*0Sstevel@tonic-gate 	} else if (currnode != NULL) {
951*0Sstevel@tonic-gate 		if (trace > 3)
952*0Sstevel@tonic-gate 			trace_prt(1, "  No rootnode, travpath=%s\n",
953*0Sstevel@tonic-gate 				traversed_path);
954*0Sstevel@tonic-gate 		if ((rc = mark_and_fake_level1_noroot(currnode,
955*0Sstevel@tonic-gate 		    traversed_path, key, mapname, faked_mapents, isdirect,
956*0Sstevel@tonic-gate 		    mapopts)) != PARSE_OK)
957*0Sstevel@tonic-gate 			return (rc);
958*0Sstevel@tonic-gate 	}
959*0Sstevel@tonic-gate 
960*0Sstevel@tonic-gate 	if (trace > 3) {
961*0Sstevel@tonic-gate 		trace_prt(1, "\n\tset_and_fake_mapent_mntlevel\n");
962*0Sstevel@tonic-gate 		trace_hierarchy(rootnode, 0);
963*0Sstevel@tonic-gate 	}
964*0Sstevel@tonic-gate 
965*0Sstevel@tonic-gate 	return (rc);
966*0Sstevel@tonic-gate }
967*0Sstevel@tonic-gate 
968*0Sstevel@tonic-gate 
969*0Sstevel@tonic-gate /*
970*0Sstevel@tonic-gate  * mark_level1_root(hiernode *node, char *traversed_path)
971*0Sstevel@tonic-gate  * marks nodes upto one level below the rootnode given by subdir
972*0Sstevel@tonic-gate  * recursively. Called if rootnode points to a mapent.
973*0Sstevel@tonic-gate  * In this routine, a level 1 node is considered to be the 1st existing
974*0Sstevel@tonic-gate  * mapentry below the root node, so there's no faking involved.
975*0Sstevel@tonic-gate  * Returns PARSE_OK or error value
976*0Sstevel@tonic-gate  */
977*0Sstevel@tonic-gate static int
978*0Sstevel@tonic-gate mark_level1_root(hiernode *node, char *traversed_path)
979*0Sstevel@tonic-gate {
980*0Sstevel@tonic-gate 	/* ensure we touch all leveldirs */
981*0Sstevel@tonic-gate 	while (node) {
982*0Sstevel@tonic-gate 		/*
983*0Sstevel@tonic-gate 		 * mark node level as 1, if one exists - else walk down
984*0Sstevel@tonic-gate 		 * subdirs until we find one.
985*0Sstevel@tonic-gate 		 */
986*0Sstevel@tonic-gate 		if (node->mapent ==  NULL) {
987*0Sstevel@tonic-gate 			char w[MAXPATHLEN];
988*0Sstevel@tonic-gate 
989*0Sstevel@tonic-gate 			if (node->subdir != NULL) {
990*0Sstevel@tonic-gate 				sprintf(w, "%s/%s", traversed_path,
991*0Sstevel@tonic-gate 				    node->dirname);
992*0Sstevel@tonic-gate 				if (mark_level1_root(node->subdir, w)
993*0Sstevel@tonic-gate 						== PARSE_ERROR)
994*0Sstevel@tonic-gate 					return (PARSE_ERROR);
995*0Sstevel@tonic-gate 			} else {
996*0Sstevel@tonic-gate 				if (verbose) {
997*0Sstevel@tonic-gate 					syslog(LOG_ERR,
998*0Sstevel@tonic-gate 					"mark_level1_root: hierarchy error");
999*0Sstevel@tonic-gate 				}
1000*0Sstevel@tonic-gate 				return (PARSE_ERROR);
1001*0Sstevel@tonic-gate 			}
1002*0Sstevel@tonic-gate 		} else {
1003*0Sstevel@tonic-gate 			char w[MAXPATHLEN];
1004*0Sstevel@tonic-gate 
1005*0Sstevel@tonic-gate 			sprintf(w, "%s/%s", traversed_path, node->dirname);
1006*0Sstevel@tonic-gate 			if (trace > 3)
1007*0Sstevel@tonic-gate 				trace_prt(1, "  node mntpnt %s\t travpath %s\n",
1008*0Sstevel@tonic-gate 				    node->mapent->map_mntpnt, w);
1009*0Sstevel@tonic-gate 
1010*0Sstevel@tonic-gate 			/* replace mntpnt with travpath to clean extra '/' */
1011*0Sstevel@tonic-gate 			if (strlen(node->mapent->map_mntpnt) < strlen(w)) {
1012*0Sstevel@tonic-gate 				if (verbose) {
1013*0Sstevel@tonic-gate 					syslog(LOG_ERR,
1014*0Sstevel@tonic-gate 					"mark_level1_root: path=%s error",
1015*0Sstevel@tonic-gate 					    traversed_path);
1016*0Sstevel@tonic-gate 				}
1017*0Sstevel@tonic-gate 				return (PARSE_ERROR);
1018*0Sstevel@tonic-gate 			}
1019*0Sstevel@tonic-gate 			if (strcmp(node->mapent->map_mntpnt, w) != 0)
1020*0Sstevel@tonic-gate 				strcpy(node->mapent->map_mntpnt, w);
1021*0Sstevel@tonic-gate 			node->mapent->map_mntlevel = 1;
1022*0Sstevel@tonic-gate 		}
1023*0Sstevel@tonic-gate 		node = node->leveldir;
1024*0Sstevel@tonic-gate 	}
1025*0Sstevel@tonic-gate 	return (PARSE_OK);
1026*0Sstevel@tonic-gate }
1027*0Sstevel@tonic-gate 
1028*0Sstevel@tonic-gate /*
1029*0Sstevel@tonic-gate  * mark_and_fake_level1_noroot(hiernode *node, char *traversed_path,
1030*0Sstevel@tonic-gate  * 			char *key,char *mapname, struct mapent **faked_mapents,
1031*0Sstevel@tonic-gate  *			uint_t isdirect, char *mapopts)
1032*0Sstevel@tonic-gate  * Called if the root of the hierarchy does not point to a mapent. marks nodes
1033*0Sstevel@tonic-gate  * upto one physical level below the rootnode given by subdir. checks if
1034*0Sstevel@tonic-gate  * there's a real mapentry. If not, it builds a faked one (autonode) at that
1035*0Sstevel@tonic-gate  * point. The faked autonode is direct, with the map being the same as the
1036*0Sstevel@tonic-gate  * original one from which the call originated. Options are same as that of
1037*0Sstevel@tonic-gate  * the map and assigned in automount_opts(). Returns PARSE_OK or error value.
1038*0Sstevel@tonic-gate  */
1039*0Sstevel@tonic-gate static int
1040*0Sstevel@tonic-gate mark_and_fake_level1_noroot(hiernode *node, char *traversed_path,
1041*0Sstevel@tonic-gate 			char *key, char *mapname, struct mapent **faked_mapents,
1042*0Sstevel@tonic-gate 			uint_t isdirect, char *mapopts)
1043*0Sstevel@tonic-gate {
1044*0Sstevel@tonic-gate 	struct mapent *me;
1045*0Sstevel@tonic-gate 	int rc = 0;
1046*0Sstevel@tonic-gate 	char faked_map_mntpnt[MAXPATHLEN];
1047*0Sstevel@tonic-gate 	char w1[MAXPATHLEN];
1048*0Sstevel@tonic-gate 	char w[MAXPATHLEN];
1049*0Sstevel@tonic-gate 
1050*0Sstevel@tonic-gate 	while (node != NULL) {
1051*0Sstevel@tonic-gate 		if (node->mapent != NULL) {
1052*0Sstevel@tonic-gate 			/*
1053*0Sstevel@tonic-gate 			 * existing mapentry at level 1 - copy travpath to
1054*0Sstevel@tonic-gate 			 * get rid of extra '/' in mntpnt
1055*0Sstevel@tonic-gate 			 */
1056*0Sstevel@tonic-gate 			sprintf(w, "%s/%s", traversed_path, node->dirname);
1057*0Sstevel@tonic-gate 			if (trace > 3)
1058*0Sstevel@tonic-gate 				trace_prt(1, "  node mntpnt=%s\t travpath=%s\n",
1059*0Sstevel@tonic-gate 				    node->mapent->map_mntpnt, w);
1060*0Sstevel@tonic-gate 			if (strlen(node->mapent->map_mntpnt) < strlen(w)) {
1061*0Sstevel@tonic-gate 				/* sanity check */
1062*0Sstevel@tonic-gate 				if (verbose)
1063*0Sstevel@tonic-gate 					syslog(LOG_ERR,
1064*0Sstevel@tonic-gate 					"mark_fake_level1_noroot:path=%s error",
1065*0Sstevel@tonic-gate 					    traversed_path);
1066*0Sstevel@tonic-gate 				return (PARSE_ERROR);
1067*0Sstevel@tonic-gate 			}
1068*0Sstevel@tonic-gate 			if (strcmp(node->mapent->map_mntpnt, w) != 0)
1069*0Sstevel@tonic-gate 				strcpy(node->mapent->map_mntpnt, w);
1070*0Sstevel@tonic-gate 			node->mapent->map_mntlevel = 1;
1071*0Sstevel@tonic-gate 		} else {
1072*0Sstevel@tonic-gate 			/*
1073*0Sstevel@tonic-gate 			 * build the faked autonode
1074*0Sstevel@tonic-gate 			 */
1075*0Sstevel@tonic-gate 			if ((me = (struct mapent *)malloc(sizeof (*me)))
1076*0Sstevel@tonic-gate 			    == NULL) {
1077*0Sstevel@tonic-gate 				syslog(LOG_ERR,
1078*0Sstevel@tonic-gate 				"mark_and_fake_level1_noroot: out of memory");
1079*0Sstevel@tonic-gate 				return (ENOMEM);
1080*0Sstevel@tonic-gate 			}
1081*0Sstevel@tonic-gate 			(void) memset((char *)me, 0, sizeof (*me));
1082*0Sstevel@tonic-gate 
1083*0Sstevel@tonic-gate 			if ((me->map_fs = (struct mapfs *)
1084*0Sstevel@tonic-gate 				malloc(sizeof (struct mapfs))) == NULL)
1085*0Sstevel@tonic-gate 				return (ENOMEM);
1086*0Sstevel@tonic-gate 			(void) memset(me->map_fs, 0, sizeof (struct mapfs));
1087*0Sstevel@tonic-gate 
1088*0Sstevel@tonic-gate 			if (isdirect) {
1089*0Sstevel@tonic-gate 				*w1 = '\0';
1090*0Sstevel@tonic-gate 			} else {
1091*0Sstevel@tonic-gate 				strcpy(w1, "/");
1092*0Sstevel@tonic-gate 				strcat(w1, key);
1093*0Sstevel@tonic-gate 			}
1094*0Sstevel@tonic-gate 			me->map_root = strdup(w1);
1095*0Sstevel@tonic-gate 
1096*0Sstevel@tonic-gate 			sprintf(faked_map_mntpnt, "%s/%s", traversed_path,
1097*0Sstevel@tonic-gate 				node->dirname);
1098*0Sstevel@tonic-gate 			me->map_mntpnt = strdup(faked_map_mntpnt);
1099*0Sstevel@tonic-gate 			me->map_fstype = strdup(MNTTYPE_AUTOFS);
1100*0Sstevel@tonic-gate 			me->map_mounter = strdup(MNTTYPE_AUTOFS);
1101*0Sstevel@tonic-gate 
1102*0Sstevel@tonic-gate 			/* set options */
1103*0Sstevel@tonic-gate 			if ((rc = automount_opts(&me->map_mntopts, mapopts))
1104*0Sstevel@tonic-gate 				!= PARSE_OK)
1105*0Sstevel@tonic-gate 				return (rc);
1106*0Sstevel@tonic-gate 			me->map_fs->mfs_dir = strdup(mapname);
1107*0Sstevel@tonic-gate 			me->map_mntlevel = 1;
1108*0Sstevel@tonic-gate 			me->map_modified = FALSE;
1109*0Sstevel@tonic-gate 			me->map_faked = TRUE;   /* mark as faked */
1110*0Sstevel@tonic-gate 			if (me->map_root == NULL ||
1111*0Sstevel@tonic-gate 			    me->map_mntpnt == NULL ||
1112*0Sstevel@tonic-gate 			    me->map_fstype == NULL ||
1113*0Sstevel@tonic-gate 			    me->map_mounter == NULL ||
1114*0Sstevel@tonic-gate 			    me->map_mntopts == NULL ||
1115*0Sstevel@tonic-gate 			    me->map_fs->mfs_dir == NULL) {
1116*0Sstevel@tonic-gate 				syslog(LOG_ERR,
1117*0Sstevel@tonic-gate 				"mark_and_fake_level1_noroot: out of memory");
1118*0Sstevel@tonic-gate 				free_mapent(*faked_mapents);
1119*0Sstevel@tonic-gate 				return (ENOMEM);
1120*0Sstevel@tonic-gate 			}
1121*0Sstevel@tonic-gate 
1122*0Sstevel@tonic-gate 			if (*faked_mapents == NULL)
1123*0Sstevel@tonic-gate 				*faked_mapents = me;
1124*0Sstevel@tonic-gate 			else {			/* attach to the head */
1125*0Sstevel@tonic-gate 				me->map_next = *faked_mapents;
1126*0Sstevel@tonic-gate 				*faked_mapents = me;
1127*0Sstevel@tonic-gate 			}
1128*0Sstevel@tonic-gate 			node->mapent = me;
1129*0Sstevel@tonic-gate 		}
1130*0Sstevel@tonic-gate 		node = node->leveldir;
1131*0Sstevel@tonic-gate 	}
1132*0Sstevel@tonic-gate 	return (rc);
1133*0Sstevel@tonic-gate }
1134*0Sstevel@tonic-gate 
1135*0Sstevel@tonic-gate /*
1136*0Sstevel@tonic-gate  * convert_mapent_to_automount(struct mapent *me, char *mapname,
1137*0Sstevel@tonic-gate  *				char *mapopts)
1138*0Sstevel@tonic-gate  * change the mapentry me to an automount - free fields first and NULL them
1139*0Sstevel@tonic-gate  * to avoid freeing again, while freeing the mapentry at a later stage.
1140*0Sstevel@tonic-gate  * Could have avoided freeing entries here as we don't really look at them.
1141*0Sstevel@tonic-gate  * Give the converted mapent entry the options that came with the map using
1142*0Sstevel@tonic-gate  * automount_opts(). Returns PARSE_OK or appropriate error value.
1143*0Sstevel@tonic-gate  */
1144*0Sstevel@tonic-gate static int
1145*0Sstevel@tonic-gate convert_mapent_to_automount(struct mapent *me, char *mapname,
1146*0Sstevel@tonic-gate 				char *mapopts)
1147*0Sstevel@tonic-gate {
1148*0Sstevel@tonic-gate 	struct mapfs *mfs = me->map_fs;		/* assumes it exists */
1149*0Sstevel@tonic-gate 	int rc = PARSE_OK;
1150*0Sstevel@tonic-gate 
1151*0Sstevel@tonic-gate 	/* free relevant entries */
1152*0Sstevel@tonic-gate 	if (mfs->mfs_host) {
1153*0Sstevel@tonic-gate 		free(mfs->mfs_host);
1154*0Sstevel@tonic-gate 		mfs->mfs_host = NULL;
1155*0Sstevel@tonic-gate 	}
1156*0Sstevel@tonic-gate 	while (me->map_fs->mfs_next != NULL) {
1157*0Sstevel@tonic-gate 		mfs = me->map_fs->mfs_next;
1158*0Sstevel@tonic-gate 		if (mfs->mfs_host)
1159*0Sstevel@tonic-gate 			free(mfs->mfs_host);
1160*0Sstevel@tonic-gate 		if (mfs->mfs_dir)
1161*0Sstevel@tonic-gate 			free(mfs->mfs_dir);
1162*0Sstevel@tonic-gate 		me->map_fs->mfs_next = mfs->mfs_next;	/* nulls eventually */
1163*0Sstevel@tonic-gate 		free((void*)mfs);
1164*0Sstevel@tonic-gate 	}
1165*0Sstevel@tonic-gate 
1166*0Sstevel@tonic-gate 	/* replace relevant entries */
1167*0Sstevel@tonic-gate 	if (me->map_fstype)
1168*0Sstevel@tonic-gate 		free(me->map_fstype);
1169*0Sstevel@tonic-gate 	if ((me->map_fstype = strdup(MNTTYPE_AUTOFS)) == NULL)
1170*0Sstevel@tonic-gate 		goto alloc_failed;
1171*0Sstevel@tonic-gate 
1172*0Sstevel@tonic-gate 	if (me->map_mounter)
1173*0Sstevel@tonic-gate 		free(me->map_mounter);
1174*0Sstevel@tonic-gate 	if ((me->map_mounter = strdup(me->map_fstype)) == NULL)
1175*0Sstevel@tonic-gate 		goto alloc_failed;
1176*0Sstevel@tonic-gate 
1177*0Sstevel@tonic-gate 	if (me->map_fs->mfs_dir)
1178*0Sstevel@tonic-gate 		free(me->map_fs->mfs_dir);
1179*0Sstevel@tonic-gate 	if ((me->map_fs->mfs_dir = strdup(mapname)) == NULL)
1180*0Sstevel@tonic-gate 		goto alloc_failed;
1181*0Sstevel@tonic-gate 
1182*0Sstevel@tonic-gate 	/* set options */
1183*0Sstevel@tonic-gate 	if (me->map_mntopts)
1184*0Sstevel@tonic-gate 		free(me->map_mntopts);
1185*0Sstevel@tonic-gate 	if ((rc = automount_opts(&me->map_mntopts, mapopts)) != PARSE_OK)
1186*0Sstevel@tonic-gate 		return (rc);
1187*0Sstevel@tonic-gate 
1188*0Sstevel@tonic-gate 	/* mucked with this entry, set the map_modified field to TRUE */
1189*0Sstevel@tonic-gate 	me->map_modified = TRUE;
1190*0Sstevel@tonic-gate 
1191*0Sstevel@tonic-gate 	return (rc);
1192*0Sstevel@tonic-gate 
1193*0Sstevel@tonic-gate alloc_failed:
1194*0Sstevel@tonic-gate 	syslog(LOG_ERR,
1195*0Sstevel@tonic-gate 		"convert_mapent_to_automount: Memory allocation failed");
1196*0Sstevel@tonic-gate 	return (ENOMEM);
1197*0Sstevel@tonic-gate }
1198*0Sstevel@tonic-gate 
1199*0Sstevel@tonic-gate /*
1200*0Sstevel@tonic-gate  * automount_opts(char **map_mntopts, char *mapopts)
1201*0Sstevel@tonic-gate  * modifies automount opts - gets rid of all "indirect" and "direct" strings
1202*0Sstevel@tonic-gate  * if they exist, and then adds a direct string to force a direct automount.
1203*0Sstevel@tonic-gate  * Rest of the mapopts stay intact. Returns PARSE_OK or appropriate error.
1204*0Sstevel@tonic-gate  */
1205*0Sstevel@tonic-gate static int
1206*0Sstevel@tonic-gate automount_opts(char **map_mntopts, char *mapopts)
1207*0Sstevel@tonic-gate {
1208*0Sstevel@tonic-gate 	char *opts;
1209*0Sstevel@tonic-gate 	char *opt;
1210*0Sstevel@tonic-gate 	int len;
1211*0Sstevel@tonic-gate 	char *placeholder;
1212*0Sstevel@tonic-gate 	char buf[AUTOFS_MAXOPTSLEN];
1213*0Sstevel@tonic-gate 
1214*0Sstevel@tonic-gate 	char *addopt = "direct";
1215*0Sstevel@tonic-gate 
1216*0Sstevel@tonic-gate 	len = strlen(mapopts)+ strlen(addopt)+2;	/* +2 for ",", '\0' */
1217*0Sstevel@tonic-gate 	if (len > AUTOFS_MAXOPTSLEN) {
1218*0Sstevel@tonic-gate 		syslog(LOG_ERR,
1219*0Sstevel@tonic-gate 		"option string %s too long (max=%d)", mapopts,
1220*0Sstevel@tonic-gate 			AUTOFS_MAXOPTSLEN-8);
1221*0Sstevel@tonic-gate 		return (PARSE_ERROR);
1222*0Sstevel@tonic-gate 	}
1223*0Sstevel@tonic-gate 
1224*0Sstevel@tonic-gate 	if (((*map_mntopts) = ((char *)malloc(len))) == NULL) {
1225*0Sstevel@tonic-gate 		syslog(LOG_ERR,	"automount_opts: Memory allocation failed");
1226*0Sstevel@tonic-gate 		return (ENOMEM);
1227*0Sstevel@tonic-gate 	}
1228*0Sstevel@tonic-gate 	memset(*map_mntopts, 0, len);
1229*0Sstevel@tonic-gate 
1230*0Sstevel@tonic-gate 	strcpy(buf, mapopts);
1231*0Sstevel@tonic-gate 	opts = buf;
1232*0Sstevel@tonic-gate 	while ((opt = strtok_r(opts, ",", &placeholder)) != NULL) {
1233*0Sstevel@tonic-gate 		opts = NULL;
1234*0Sstevel@tonic-gate 
1235*0Sstevel@tonic-gate 		/* remove trailing and leading spaces */
1236*0Sstevel@tonic-gate 		while (isspace(*opt))
1237*0Sstevel@tonic-gate 			opt++;
1238*0Sstevel@tonic-gate 		len = strlen(opt)-1;
1239*0Sstevel@tonic-gate 		while (isspace(opt[len]))
1240*0Sstevel@tonic-gate 			opt[len--] = '\0';
1241*0Sstevel@tonic-gate 
1242*0Sstevel@tonic-gate 		/*
1243*0Sstevel@tonic-gate 		 * if direct or indirect found, get rid of it, else put it
1244*0Sstevel@tonic-gate 		 * back
1245*0Sstevel@tonic-gate 		 */
1246*0Sstevel@tonic-gate 		if ((strcmp(opt, "indirect") == 0) ||
1247*0Sstevel@tonic-gate 		    (strcmp(opt, "direct") == 0))
1248*0Sstevel@tonic-gate 			continue;
1249*0Sstevel@tonic-gate 		if (*map_mntopts[0] != '\0')
1250*0Sstevel@tonic-gate 			strcat(*map_mntopts, ",");
1251*0Sstevel@tonic-gate 		strcat(*map_mntopts, opt);
1252*0Sstevel@tonic-gate 	}
1253*0Sstevel@tonic-gate 
1254*0Sstevel@tonic-gate 	/* add the direct string at the end */
1255*0Sstevel@tonic-gate 	if (*map_mntopts[0] != '\0')
1256*0Sstevel@tonic-gate 		strcat(*map_mntopts,	",");
1257*0Sstevel@tonic-gate 	strcat(*map_mntopts, addopt);
1258*0Sstevel@tonic-gate 
1259*0Sstevel@tonic-gate 	return (PARSE_OK);
1260*0Sstevel@tonic-gate }
1261*0Sstevel@tonic-gate 
1262*0Sstevel@tonic-gate /*
1263*0Sstevel@tonic-gate  * parse_fsinfo(char *mapname, struct mapent *mapents)
1264*0Sstevel@tonic-gate  * parses the filesystem information stored in me->map_fsw and me->map_fswq
1265*0Sstevel@tonic-gate  * and calls appropriate filesystem parser.
1266*0Sstevel@tonic-gate  * Returns PARSE_OK or an appropriate error value.
1267*0Sstevel@tonic-gate  */
1268*0Sstevel@tonic-gate static int
1269*0Sstevel@tonic-gate parse_fsinfo(char *mapname, struct mapent *mapents)
1270*0Sstevel@tonic-gate {
1271*0Sstevel@tonic-gate 	struct mapent *me = mapents;
1272*0Sstevel@tonic-gate 	char *bufp;
1273*0Sstevel@tonic-gate 	char *bufq;
1274*0Sstevel@tonic-gate 	int wordsz = MAXPATHLEN;
1275*0Sstevel@tonic-gate 	int err = 0;
1276*0Sstevel@tonic-gate 
1277*0Sstevel@tonic-gate 	while (me != NULL) {
1278*0Sstevel@tonic-gate 		bufp = "";
1279*0Sstevel@tonic-gate 		bufq = "";
1280*0Sstevel@tonic-gate 		if (strcmp(me->map_fstype, MNTTYPE_NFS) == 0) {
1281*0Sstevel@tonic-gate 			err = parse_nfs(mapname, me, me->map_fsw,
1282*0Sstevel@tonic-gate 				me->map_fswq, &bufp, &bufq, wordsz);
1283*0Sstevel@tonic-gate 		} else {
1284*0Sstevel@tonic-gate 			err = parse_special(me, me->map_fsw, me->map_fswq,
1285*0Sstevel@tonic-gate 				&bufp, &bufq, wordsz);
1286*0Sstevel@tonic-gate 		}
1287*0Sstevel@tonic-gate 
1288*0Sstevel@tonic-gate 		if (err != PARSE_OK || *me->map_fsw != '\0' ||
1289*0Sstevel@tonic-gate 		    *me->map_fswq != '\0') {
1290*0Sstevel@tonic-gate 			/* sanity check */
1291*0Sstevel@tonic-gate 			if (verbose)
1292*0Sstevel@tonic-gate 				syslog(LOG_ERR,
1293*0Sstevel@tonic-gate 				"parse_fsinfo: mount location error %s",
1294*0Sstevel@tonic-gate 				    me->map_fsw);
1295*0Sstevel@tonic-gate 			return (PARSE_ERROR);
1296*0Sstevel@tonic-gate 		}
1297*0Sstevel@tonic-gate 
1298*0Sstevel@tonic-gate 		me = me->map_next;
1299*0Sstevel@tonic-gate 	}
1300*0Sstevel@tonic-gate 
1301*0Sstevel@tonic-gate 	if (trace > 3) {
1302*0Sstevel@tonic-gate 		trace_mapents("parse_fsinfo:", mapents);
1303*0Sstevel@tonic-gate 	}
1304*0Sstevel@tonic-gate 
1305*0Sstevel@tonic-gate 	return (PARSE_OK);
1306*0Sstevel@tonic-gate }
1307*0Sstevel@tonic-gate 
1308*0Sstevel@tonic-gate /*
1309*0Sstevel@tonic-gate  * This function parses the map entry for a nfs type file system
1310*0Sstevel@tonic-gate  * The input is the string lp (and lq) which can be one of the
1311*0Sstevel@tonic-gate  * following forms:
1312*0Sstevel@tonic-gate  * a) host[(penalty)][,host[(penalty)]]... :/directory
1313*0Sstevel@tonic-gate  * b) host[(penalty)]:/directory[ host[(penalty)]:/directory]...
1314*0Sstevel@tonic-gate  * This routine constructs a mapfs link-list for each of
1315*0Sstevel@tonic-gate  * the hosts and the corresponding file system. The list
1316*0Sstevel@tonic-gate  * is then attatched to the mapent struct passed in.
1317*0Sstevel@tonic-gate  */
1318*0Sstevel@tonic-gate parse_nfs(mapname, me, fsw, fswq, lp, lq, wsize)
1319*0Sstevel@tonic-gate 	struct mapent *me;
1320*0Sstevel@tonic-gate 	char *mapname, *fsw, *fswq, **lp, **lq;
1321*0Sstevel@tonic-gate 	int wsize;
1322*0Sstevel@tonic-gate {
1323*0Sstevel@tonic-gate 	struct mapfs *mfs, **mfsp;
1324*0Sstevel@tonic-gate 	char *wlp, *wlq;
1325*0Sstevel@tonic-gate 	char *hl, hostlist[1024], *hlq, hostlistq[1024];
1326*0Sstevel@tonic-gate 	char hostname_and_penalty[MXHOSTNAMELEN+5];
1327*0Sstevel@tonic-gate 	char *hn, *hnq, hostname[MXHOSTNAMELEN+1];
1328*0Sstevel@tonic-gate 	char dirname[MAXPATHLEN+1], subdir[MAXPATHLEN+1];
1329*0Sstevel@tonic-gate 	char qbuff[MAXPATHLEN+1], qbuff1[MAXPATHLEN+1];
1330*0Sstevel@tonic-gate 	char pbuff[10], pbuffq[10];
1331*0Sstevel@tonic-gate 	int penalty;
1332*0Sstevel@tonic-gate 	char w[MAXPATHLEN];
1333*0Sstevel@tonic-gate 	char wq[MAXPATHLEN];
1334*0Sstevel@tonic-gate 	int host_cnt;
1335*0Sstevel@tonic-gate 
1336*0Sstevel@tonic-gate 	mfsp = &me->map_fs;
1337*0Sstevel@tonic-gate 	*mfsp = NULL;
1338*0Sstevel@tonic-gate 
1339*0Sstevel@tonic-gate 	/*
1340*0Sstevel@tonic-gate 	 * there may be more than one entry in the map list. Get the
1341*0Sstevel@tonic-gate 	 * first one. Use temps to handle the word information and
1342*0Sstevel@tonic-gate 	 * copy back into fsw and fswq fields when done.
1343*0Sstevel@tonic-gate 	 */
1344*0Sstevel@tonic-gate 	*lp = fsw;
1345*0Sstevel@tonic-gate 	*lq = fswq;
1346*0Sstevel@tonic-gate 	if (getword(w, wq, lp, lq, ' ', wsize) == -1)
1347*0Sstevel@tonic-gate 		return (PARSE_ERROR);
1348*0Sstevel@tonic-gate 	while (*w && *w != '/') {
1349*0Sstevel@tonic-gate 		bool_t maybe_url;
1350*0Sstevel@tonic-gate 
1351*0Sstevel@tonic-gate 		maybe_url = TRUE;
1352*0Sstevel@tonic-gate 
1353*0Sstevel@tonic-gate 		wlp = w; wlq = wq;
1354*0Sstevel@tonic-gate 		if (getword(hostlist, hostlistq, &wlp, &wlq, ':',
1355*0Sstevel@tonic-gate 			    sizeof (hostlist)) == -1)
1356*0Sstevel@tonic-gate 			return (PARSE_ERROR);
1357*0Sstevel@tonic-gate 		if (!*hostlist)
1358*0Sstevel@tonic-gate 			goto bad_entry;
1359*0Sstevel@tonic-gate 
1360*0Sstevel@tonic-gate 		if (strcmp(hostlist, "nfs") != 0)
1361*0Sstevel@tonic-gate 			maybe_url = FALSE;
1362*0Sstevel@tonic-gate 
1363*0Sstevel@tonic-gate 		if (getword(dirname, qbuff, &wlp, &wlq, ':',
1364*0Sstevel@tonic-gate 					sizeof (dirname)) == -1)
1365*0Sstevel@tonic-gate 			return (PARSE_ERROR);
1366*0Sstevel@tonic-gate 		if (*dirname == '\0')
1367*0Sstevel@tonic-gate 			goto bad_entry;
1368*0Sstevel@tonic-gate 
1369*0Sstevel@tonic-gate 		if (maybe_url == TRUE && strncmp(dirname, "//", 2) != 0)
1370*0Sstevel@tonic-gate 			maybe_url = FALSE;
1371*0Sstevel@tonic-gate 
1372*0Sstevel@tonic-gate 		/*
1373*0Sstevel@tonic-gate 		 * See the next block comment ("Once upon a time ...") to
1374*0Sstevel@tonic-gate 		 * understand this. It turns the deprecated concept
1375*0Sstevel@tonic-gate 		 * of "subdir mounts" produced some useful code for handling
1376*0Sstevel@tonic-gate 		 * the possibility of a ":port#" in the URL.
1377*0Sstevel@tonic-gate 		 */
1378*0Sstevel@tonic-gate 		if (maybe_url == FALSE)
1379*0Sstevel@tonic-gate 			*subdir = '/';
1380*0Sstevel@tonic-gate 		else
1381*0Sstevel@tonic-gate 			*subdir = ':';
1382*0Sstevel@tonic-gate 
1383*0Sstevel@tonic-gate 		*qbuff = ' ';
1384*0Sstevel@tonic-gate 
1385*0Sstevel@tonic-gate 		/*
1386*0Sstevel@tonic-gate 		 * Once upon time, before autofs, there was support for
1387*0Sstevel@tonic-gate 		 * "subdir mounts". The idea was to "economize" the
1388*0Sstevel@tonic-gate 		 * number of mounts, so if you had a number of entries
1389*0Sstevel@tonic-gate 		 * all referring to a common subdirectory, e.g.
1390*0Sstevel@tonic-gate 		 *
1391*0Sstevel@tonic-gate 		 *	carol    seasons:/export/home11/carol
1392*0Sstevel@tonic-gate 		 *	ted	 seasons:/export/home11/ted
1393*0Sstevel@tonic-gate 		 *	alice	 seasons:/export/home11/alice
1394*0Sstevel@tonic-gate 		 *
1395*0Sstevel@tonic-gate 		 * then you could tell the automounter to mount a
1396*0Sstevel@tonic-gate 		 * common mountpoint which was delimited by the second
1397*0Sstevel@tonic-gate 		 * colon:
1398*0Sstevel@tonic-gate 		 *
1399*0Sstevel@tonic-gate 		 *	carol    seasons:/export/home11:carol
1400*0Sstevel@tonic-gate 		 *	ted	 seasons:/export/home11:ted
1401*0Sstevel@tonic-gate 		 *	alice	 seasons:/export/home11:alice
1402*0Sstevel@tonic-gate 		 *
1403*0Sstevel@tonic-gate 		 * The automounter would mount seasons:/export/home11
1404*0Sstevel@tonic-gate 		 * then for any other map entry that referenced the same
1405*0Sstevel@tonic-gate 		 * directory it would build a symbolic link that
1406*0Sstevel@tonic-gate 		 * appended the remainder of the path after the second
1407*0Sstevel@tonic-gate 		 * colon, i.e.  once the common subdir was mounted, then
1408*0Sstevel@tonic-gate 		 * other directories could be accessed just by link
1409*0Sstevel@tonic-gate 		 * building - no further mounts required.
1410*0Sstevel@tonic-gate 		 *
1411*0Sstevel@tonic-gate 		 * In theory the "mount saving" idea sounded good. In
1412*0Sstevel@tonic-gate 		 * practice the saving didn't amount to much and the
1413*0Sstevel@tonic-gate 		 * symbolic links confused people because the common
1414*0Sstevel@tonic-gate 		 * mountpoint had to have a pseudonym.
1415*0Sstevel@tonic-gate 		 *
1416*0Sstevel@tonic-gate 		 * To remain backward compatible with the existing
1417*0Sstevel@tonic-gate 		 * maps, we interpret a second colon as a slash.
1418*0Sstevel@tonic-gate 		 */
1419*0Sstevel@tonic-gate 		if (getword(subdir+1, qbuff+1, &wlp, &wlq, ':',
1420*0Sstevel@tonic-gate 				sizeof (subdir)) == -1)
1421*0Sstevel@tonic-gate 			return (PARSE_ERROR);
1422*0Sstevel@tonic-gate 
1423*0Sstevel@tonic-gate 		if (*(subdir+1))
1424*0Sstevel@tonic-gate 			(void) strcat(dirname, subdir);
1425*0Sstevel@tonic-gate 
1426*0Sstevel@tonic-gate 		hl = hostlist; hlq = hostlistq;
1427*0Sstevel@tonic-gate 
1428*0Sstevel@tonic-gate 		host_cnt = 0;
1429*0Sstevel@tonic-gate 		for (;;) {
1430*0Sstevel@tonic-gate 
1431*0Sstevel@tonic-gate 			if (getword(hostname_and_penalty, qbuff, &hl, &hlq, ',',
1432*0Sstevel@tonic-gate 				sizeof (hostname_and_penalty)) == -1)
1433*0Sstevel@tonic-gate 				return (PARSE_ERROR);
1434*0Sstevel@tonic-gate 			if (!*hostname_and_penalty)
1435*0Sstevel@tonic-gate 				break;
1436*0Sstevel@tonic-gate 
1437*0Sstevel@tonic-gate 			host_cnt++;
1438*0Sstevel@tonic-gate 			if (host_cnt > 1)
1439*0Sstevel@tonic-gate 				maybe_url = FALSE;
1440*0Sstevel@tonic-gate 
1441*0Sstevel@tonic-gate 			hn = hostname_and_penalty;
1442*0Sstevel@tonic-gate 			hnq = qbuff;
1443*0Sstevel@tonic-gate 			if (getword(hostname, qbuff1, &hn, &hnq, '(',
1444*0Sstevel@tonic-gate 				sizeof (hostname)) == -1)
1445*0Sstevel@tonic-gate 				return (PARSE_ERROR);
1446*0Sstevel@tonic-gate 			if (hostname[0] == '\0')
1447*0Sstevel@tonic-gate 				goto bad_entry;
1448*0Sstevel@tonic-gate 
1449*0Sstevel@tonic-gate 			if (strcmp(hostname, hostname_and_penalty) == 0) {
1450*0Sstevel@tonic-gate 				penalty = 0;
1451*0Sstevel@tonic-gate 			} else {
1452*0Sstevel@tonic-gate 				maybe_url = FALSE;
1453*0Sstevel@tonic-gate 				hn++; hnq++;
1454*0Sstevel@tonic-gate 				if (getword(pbuff, pbuffq, &hn, &hnq, ')',
1455*0Sstevel@tonic-gate 					sizeof (pbuff)) == -1)
1456*0Sstevel@tonic-gate 					return (PARSE_ERROR);
1457*0Sstevel@tonic-gate 				if (!*pbuff)
1458*0Sstevel@tonic-gate 					penalty = 0;
1459*0Sstevel@tonic-gate 				else
1460*0Sstevel@tonic-gate 					penalty = atoi(pbuff);
1461*0Sstevel@tonic-gate 			}
1462*0Sstevel@tonic-gate 			mfs = (struct mapfs *)malloc(sizeof (*mfs));
1463*0Sstevel@tonic-gate 			if (mfs == NULL) {
1464*0Sstevel@tonic-gate 				syslog(LOG_ERR,
1465*0Sstevel@tonic-gate 				"parse_nfs: Memory allocation failed");
1466*0Sstevel@tonic-gate 				return (PARSE_ERROR);
1467*0Sstevel@tonic-gate 			}
1468*0Sstevel@tonic-gate 			(void) memset(mfs, 0, sizeof (*mfs));
1469*0Sstevel@tonic-gate 			*mfsp = mfs;
1470*0Sstevel@tonic-gate 			mfsp = &mfs->mfs_next;
1471*0Sstevel@tonic-gate 
1472*0Sstevel@tonic-gate 			if (maybe_url == TRUE) {
1473*0Sstevel@tonic-gate 				char *host;
1474*0Sstevel@tonic-gate 				char *path;
1475*0Sstevel@tonic-gate 				char *sport;
1476*0Sstevel@tonic-gate 
1477*0Sstevel@tonic-gate 				host = dirname+2;
1478*0Sstevel@tonic-gate 				path = strchr(host, '/');
1479*0Sstevel@tonic-gate 				if (path == NULL) {
1480*0Sstevel@tonic-gate 					syslog(LOG_ERR,
1481*0Sstevel@tonic-gate 					"parse_nfs: illegal nfs url syntax: %s",
1482*0Sstevel@tonic-gate 					host);
1483*0Sstevel@tonic-gate 
1484*0Sstevel@tonic-gate 					return (PARSE_ERROR);
1485*0Sstevel@tonic-gate 				}
1486*0Sstevel@tonic-gate 				*path = '\0';
1487*0Sstevel@tonic-gate 				sport =  strchr(host, ':');
1488*0Sstevel@tonic-gate 
1489*0Sstevel@tonic-gate 				if (sport != NULL && sport < path) {
1490*0Sstevel@tonic-gate 					*sport = '\0';
1491*0Sstevel@tonic-gate 					mfs->mfs_port = atoi(sport+1);
1492*0Sstevel@tonic-gate 
1493*0Sstevel@tonic-gate 					if (mfs->mfs_port > USHRT_MAX) {
1494*0Sstevel@tonic-gate 						syslog(LOG_ERR,
1495*0Sstevel@tonic-gate 							"parse_nfs: invalid "
1496*0Sstevel@tonic-gate 							"port number (%d) in "
1497*0Sstevel@tonic-gate 							"NFS URL",
1498*0Sstevel@tonic-gate 							mfs->mfs_port);
1499*0Sstevel@tonic-gate 
1500*0Sstevel@tonic-gate 						return (PARSE_ERROR);
1501*0Sstevel@tonic-gate 					}
1502*0Sstevel@tonic-gate 
1503*0Sstevel@tonic-gate 				}
1504*0Sstevel@tonic-gate 
1505*0Sstevel@tonic-gate 				path++;
1506*0Sstevel@tonic-gate 				if (*path == '\0')
1507*0Sstevel@tonic-gate 					path = ".";
1508*0Sstevel@tonic-gate 
1509*0Sstevel@tonic-gate 				mfs->mfs_flags |= MFS_URL;
1510*0Sstevel@tonic-gate 
1511*0Sstevel@tonic-gate 				mfs->mfs_host = strdup(host);
1512*0Sstevel@tonic-gate 				mfs->mfs_dir = strdup(path);
1513*0Sstevel@tonic-gate 			} else {
1514*0Sstevel@tonic-gate 				mfs->mfs_host = strdup(hostname);
1515*0Sstevel@tonic-gate 				mfs->mfs_dir = strdup(dirname);
1516*0Sstevel@tonic-gate 			}
1517*0Sstevel@tonic-gate 
1518*0Sstevel@tonic-gate 			mfs->mfs_penalty = penalty;
1519*0Sstevel@tonic-gate 			if (mfs->mfs_host == NULL || mfs->mfs_dir == NULL) {
1520*0Sstevel@tonic-gate 				syslog(LOG_ERR,
1521*0Sstevel@tonic-gate 				"parse_nfs: Memory allocation failed");
1522*0Sstevel@tonic-gate 				return (PARSE_ERROR);
1523*0Sstevel@tonic-gate 			}
1524*0Sstevel@tonic-gate 		}
1525*0Sstevel@tonic-gate 		/*
1526*0Sstevel@tonic-gate 		 * We check host_cnt to make sure we haven't parsed an entry
1527*0Sstevel@tonic-gate 		 * with no host information.
1528*0Sstevel@tonic-gate 		 */
1529*0Sstevel@tonic-gate 		if (host_cnt == 0) {
1530*0Sstevel@tonic-gate 			syslog(LOG_ERR,
1531*0Sstevel@tonic-gate 			"parse_nfs: invalid host specified - bad entry "
1532*0Sstevel@tonic-gate 			"in map %s \"%s\"",
1533*0Sstevel@tonic-gate 			mapname, w);
1534*0Sstevel@tonic-gate 			return (PARSE_ERROR);
1535*0Sstevel@tonic-gate 		}
1536*0Sstevel@tonic-gate 		if (getword(w, wq, lp, lq, ' ', wsize) == -1)
1537*0Sstevel@tonic-gate 			return (PARSE_ERROR);
1538*0Sstevel@tonic-gate 	}
1539*0Sstevel@tonic-gate 
1540*0Sstevel@tonic-gate 	strcpy(fsw, w);
1541*0Sstevel@tonic-gate 	strcpy(fswq, wq);
1542*0Sstevel@tonic-gate 
1543*0Sstevel@tonic-gate 	return (PARSE_OK);
1544*0Sstevel@tonic-gate 
1545*0Sstevel@tonic-gate bad_entry:
1546*0Sstevel@tonic-gate 	syslog(LOG_ERR, "parse_nfs: bad entry in map %s \"%s\"", mapname, w);
1547*0Sstevel@tonic-gate 	return (PARSE_ERROR);
1548*0Sstevel@tonic-gate }
1549*0Sstevel@tonic-gate 
1550*0Sstevel@tonic-gate static int
1551*0Sstevel@tonic-gate parse_special(me, w, wq, lp, lq, wsize)
1552*0Sstevel@tonic-gate 	struct mapent *me;
1553*0Sstevel@tonic-gate 	char *w, *wq, **lp, **lq;
1554*0Sstevel@tonic-gate 	int wsize;
1555*0Sstevel@tonic-gate {
1556*0Sstevel@tonic-gate 	char devname[MAXPATHLEN + 1], qbuf[MAXPATHLEN + 1];
1557*0Sstevel@tonic-gate 	char *wlp, *wlq;
1558*0Sstevel@tonic-gate 	struct mapfs *mfs;
1559*0Sstevel@tonic-gate 
1560*0Sstevel@tonic-gate 	wlp = w;
1561*0Sstevel@tonic-gate 	wlq = wq;
1562*0Sstevel@tonic-gate 	if (getword(devname, qbuf, &wlp, &wlq, ' ', sizeof (devname)) == -1)
1563*0Sstevel@tonic-gate 		return (PARSE_ERROR);
1564*0Sstevel@tonic-gate 	if (devname[0] == '\0')
1565*0Sstevel@tonic-gate 		return (PARSE_ERROR);
1566*0Sstevel@tonic-gate 
1567*0Sstevel@tonic-gate 	mfs = (struct mapfs *)malloc(sizeof (struct mapfs));
1568*0Sstevel@tonic-gate 	if (mfs == NULL)
1569*0Sstevel@tonic-gate 		return (PARSE_ERROR);
1570*0Sstevel@tonic-gate 	(void) memset(mfs, 0, sizeof (*mfs));
1571*0Sstevel@tonic-gate 
1572*0Sstevel@tonic-gate 	/*
1573*0Sstevel@tonic-gate 	 * A device name that begins with a slash could
1574*0Sstevel@tonic-gate 	 * be confused with a mountpoint path, hence use
1575*0Sstevel@tonic-gate 	 * a colon to escape a device string that begins
1576*0Sstevel@tonic-gate 	 * with a slash, e.g.
1577*0Sstevel@tonic-gate 	 *
1578*0Sstevel@tonic-gate 	 *	foo  -ro  /bar  foo:/bar
1579*0Sstevel@tonic-gate 	 * and
1580*0Sstevel@tonic-gate 	 *	foo  -ro  /dev/sr0
1581*0Sstevel@tonic-gate 	 *
1582*0Sstevel@tonic-gate 	 * would confuse the parser.  The second instance
1583*0Sstevel@tonic-gate 	 * must use a colon:
1584*0Sstevel@tonic-gate 	 *
1585*0Sstevel@tonic-gate 	 *	foo  -ro  :/dev/sr0
1586*0Sstevel@tonic-gate 	 */
1587*0Sstevel@tonic-gate 	mfs->mfs_dir = strdup(&devname[devname[0] == ':']);
1588*0Sstevel@tonic-gate 	if (mfs->mfs_dir == NULL)
1589*0Sstevel@tonic-gate 		return (PARSE_ERROR);
1590*0Sstevel@tonic-gate 	me->map_fs = mfs;
1591*0Sstevel@tonic-gate 	if (getword(w, wq, lp, lq, ' ', wsize) == -1)
1592*0Sstevel@tonic-gate 		return (PARSE_ERROR);
1593*0Sstevel@tonic-gate 	return (0);
1594*0Sstevel@tonic-gate }
1595*0Sstevel@tonic-gate 
1596*0Sstevel@tonic-gate /*
1597*0Sstevel@tonic-gate  * get_dir_from_path(char *dir, char **path, int dirsz)
1598*0Sstevel@tonic-gate  * gets the directory name dir from path for max string of length dirsz.
1599*0Sstevel@tonic-gate  * A modification of the getword routine. Assumes the delimiter is '/'
1600*0Sstevel@tonic-gate  * and that excess /'s are redundant.
1601*0Sstevel@tonic-gate  * Returns PARSE_OK or PARSE_ERROR
1602*0Sstevel@tonic-gate  */
1603*0Sstevel@tonic-gate static int
1604*0Sstevel@tonic-gate get_dir_from_path(char *dir, char **path, int dirsz)
1605*0Sstevel@tonic-gate {
1606*0Sstevel@tonic-gate 	char *tmp = dir;
1607*0Sstevel@tonic-gate 	int count = dirsz;
1608*0Sstevel@tonic-gate 
1609*0Sstevel@tonic-gate 	if (dirsz <= 0) {
1610*0Sstevel@tonic-gate 		if (verbose)
1611*0Sstevel@tonic-gate 			syslog(LOG_ERR,
1612*0Sstevel@tonic-gate 			"get_dir_from_path: invalid directory size %d", dirsz);
1613*0Sstevel@tonic-gate 		return (PARSE_ERROR);
1614*0Sstevel@tonic-gate 	}
1615*0Sstevel@tonic-gate 
1616*0Sstevel@tonic-gate 	/* get rid of leading /'s in path */
1617*0Sstevel@tonic-gate 	while (**path == '/')
1618*0Sstevel@tonic-gate 		(*path)++;
1619*0Sstevel@tonic-gate 
1620*0Sstevel@tonic-gate 	/* now at a word or at the end of path */
1621*0Sstevel@tonic-gate 	while ((**path) && ((**path) != '/')) {
1622*0Sstevel@tonic-gate 		if (--count <= 0) {
1623*0Sstevel@tonic-gate 			*tmp = '\0';
1624*0Sstevel@tonic-gate 			syslog(LOG_ERR,
1625*0Sstevel@tonic-gate 			"get_dir_from_path: max pathlength exceeded %d", dirsz);
1626*0Sstevel@tonic-gate 			return (PARSE_ERROR);
1627*0Sstevel@tonic-gate 		}
1628*0Sstevel@tonic-gate 		*dir++ = *(*path)++;
1629*0Sstevel@tonic-gate 	}
1630*0Sstevel@tonic-gate 
1631*0Sstevel@tonic-gate 	*dir = '\0';
1632*0Sstevel@tonic-gate 
1633*0Sstevel@tonic-gate 	/* get rid of trailing /'s in path */
1634*0Sstevel@tonic-gate 	while (**path == '/')
1635*0Sstevel@tonic-gate 		(*path)++;
1636*0Sstevel@tonic-gate 
1637*0Sstevel@tonic-gate 	return (PARSE_OK);
1638*0Sstevel@tonic-gate }
1639*0Sstevel@tonic-gate 
1640*0Sstevel@tonic-gate /*
1641*0Sstevel@tonic-gate  * alloc_hiernode(hiernode **newnode, char *dirname)
1642*0Sstevel@tonic-gate  * allocates a new hiernode corresponding to a new directory entry
1643*0Sstevel@tonic-gate  * in the hierarchical structure, and passes a pointer to it back
1644*0Sstevel@tonic-gate  * to the calling program.
1645*0Sstevel@tonic-gate  * Returns PARSE_OK or appropriate error value.
1646*0Sstevel@tonic-gate  */
1647*0Sstevel@tonic-gate static int
1648*0Sstevel@tonic-gate alloc_hiernode(hiernode **newnode, char *dirname)
1649*0Sstevel@tonic-gate {
1650*0Sstevel@tonic-gate 	if ((*newnode = (hiernode *)malloc(sizeof (hiernode))) == NULL) {
1651*0Sstevel@tonic-gate 		syslog(LOG_ERR,	"alloc_hiernode: Memory allocation failed");
1652*0Sstevel@tonic-gate 		return (ENOMEM);
1653*0Sstevel@tonic-gate 	}
1654*0Sstevel@tonic-gate 
1655*0Sstevel@tonic-gate 	memset(((char *)*newnode), 0, sizeof (hiernode));
1656*0Sstevel@tonic-gate 	strcpy(((*newnode)->dirname), dirname);
1657*0Sstevel@tonic-gate 	return (PARSE_OK);
1658*0Sstevel@tonic-gate }
1659*0Sstevel@tonic-gate 
1660*0Sstevel@tonic-gate /*
1661*0Sstevel@tonic-gate  * free_hiernode(hiernode *node)
1662*0Sstevel@tonic-gate  * frees the allocated hiernode given the head of the structure
1663*0Sstevel@tonic-gate  * recursively calls itself until it frees entire structure.
1664*0Sstevel@tonic-gate  * Returns nothing.
1665*0Sstevel@tonic-gate  */
1666*0Sstevel@tonic-gate static void
1667*0Sstevel@tonic-gate free_hiernode(hiernode *node)
1668*0Sstevel@tonic-gate {
1669*0Sstevel@tonic-gate 	hiernode *currnode = node;
1670*0Sstevel@tonic-gate 	hiernode *prevnode = NULL;
1671*0Sstevel@tonic-gate 
1672*0Sstevel@tonic-gate 	while (currnode != NULL) {
1673*0Sstevel@tonic-gate 		if (currnode->subdir != NULL)
1674*0Sstevel@tonic-gate 			free_hiernode(currnode->subdir);
1675*0Sstevel@tonic-gate 		prevnode = currnode;
1676*0Sstevel@tonic-gate 		currnode = currnode->leveldir;
1677*0Sstevel@tonic-gate 		free((void*)prevnode);
1678*0Sstevel@tonic-gate 	}
1679*0Sstevel@tonic-gate }
1680*0Sstevel@tonic-gate 
1681*0Sstevel@tonic-gate /*
1682*0Sstevel@tonic-gate  * free_mapent(struct mapent *)
1683*0Sstevel@tonic-gate  * free the mapentry and its fields
1684*0Sstevel@tonic-gate  */
1685*0Sstevel@tonic-gate void
1686*0Sstevel@tonic-gate free_mapent(me)
1687*0Sstevel@tonic-gate 	struct mapent *me;
1688*0Sstevel@tonic-gate {
1689*0Sstevel@tonic-gate 	struct mapfs *mfs;
1690*0Sstevel@tonic-gate 	struct mapent *m;
1691*0Sstevel@tonic-gate 
1692*0Sstevel@tonic-gate 	while (me) {
1693*0Sstevel@tonic-gate 		while (me->map_fs) {
1694*0Sstevel@tonic-gate 			mfs = me->map_fs;
1695*0Sstevel@tonic-gate 			if (mfs->mfs_host)
1696*0Sstevel@tonic-gate 				free(mfs->mfs_host);
1697*0Sstevel@tonic-gate 			if (mfs->mfs_dir)
1698*0Sstevel@tonic-gate 				free(mfs->mfs_dir);
1699*0Sstevel@tonic-gate 			if (mfs->mfs_args)
1700*0Sstevel@tonic-gate 				free(mfs->mfs_args);
1701*0Sstevel@tonic-gate 			if (mfs->mfs_nconf)
1702*0Sstevel@tonic-gate 				freenetconfigent(mfs->mfs_nconf);
1703*0Sstevel@tonic-gate 			me->map_fs = mfs->mfs_next;
1704*0Sstevel@tonic-gate 			free((char *)mfs);
1705*0Sstevel@tonic-gate 		}
1706*0Sstevel@tonic-gate 
1707*0Sstevel@tonic-gate 		if (me->map_root)
1708*0Sstevel@tonic-gate 			free(me->map_root);
1709*0Sstevel@tonic-gate 		if (me->map_mntpnt)
1710*0Sstevel@tonic-gate 			free(me->map_mntpnt);
1711*0Sstevel@tonic-gate 		if (me->map_mntopts)
1712*0Sstevel@tonic-gate 			free(me->map_mntopts);
1713*0Sstevel@tonic-gate 		if (me->map_fstype)
1714*0Sstevel@tonic-gate 			free(me->map_fstype);
1715*0Sstevel@tonic-gate 		if (me->map_mounter)
1716*0Sstevel@tonic-gate 			free(me->map_mounter);
1717*0Sstevel@tonic-gate 		if (me->map_fsw)
1718*0Sstevel@tonic-gate 			free(me->map_fsw);
1719*0Sstevel@tonic-gate 		if (me->map_fswq)
1720*0Sstevel@tonic-gate 			free(me->map_fswq);
1721*0Sstevel@tonic-gate 
1722*0Sstevel@tonic-gate 		m = me;
1723*0Sstevel@tonic-gate 		me = me->map_next;
1724*0Sstevel@tonic-gate 		free((char *)m);
1725*0Sstevel@tonic-gate 	}
1726*0Sstevel@tonic-gate }
1727*0Sstevel@tonic-gate 
1728*0Sstevel@tonic-gate /*
1729*0Sstevel@tonic-gate  * trace_mapents(struct mapent *mapents)
1730*0Sstevel@tonic-gate  * traces through the mapentry structure and prints it element by element
1731*0Sstevel@tonic-gate  * returns nothing
1732*0Sstevel@tonic-gate  */
1733*0Sstevel@tonic-gate static void
1734*0Sstevel@tonic-gate trace_mapents(char *s, struct mapent *mapents)
1735*0Sstevel@tonic-gate {
1736*0Sstevel@tonic-gate 	struct mapfs  *mfs;
1737*0Sstevel@tonic-gate 	struct mapent *me;
1738*0Sstevel@tonic-gate 
1739*0Sstevel@tonic-gate 	trace_prt(1, "\n\t%s\n", s);
1740*0Sstevel@tonic-gate 	for (me = mapents; me; me = me->map_next) {
1741*0Sstevel@tonic-gate 		trace_prt(1, "  (%s,%s)\t %s%s -%s\n",
1742*0Sstevel@tonic-gate 			me->map_fstype ? me->map_fstype : "",
1743*0Sstevel@tonic-gate 			me->map_mounter ? me->map_mounter : "",
1744*0Sstevel@tonic-gate 			me->map_root  ? me->map_root : "",
1745*0Sstevel@tonic-gate 			me->map_mntpnt ? me->map_mntpnt : "",
1746*0Sstevel@tonic-gate 			me->map_mntopts ? me->map_mntopts : "");
1747*0Sstevel@tonic-gate 		for (mfs = me->map_fs; mfs; mfs = mfs->mfs_next)
1748*0Sstevel@tonic-gate 			trace_prt(0, "\t\t%s:%s\n",
1749*0Sstevel@tonic-gate 				mfs->mfs_host ? mfs->mfs_host: "",
1750*0Sstevel@tonic-gate 				mfs->mfs_dir ? mfs->mfs_dir : "");
1751*0Sstevel@tonic-gate 
1752*0Sstevel@tonic-gate 		trace_prt(1, "\tme->map_fsw=%s\n",
1753*0Sstevel@tonic-gate 			me->map_fsw ? me->map_fsw:"",
1754*0Sstevel@tonic-gate 			me->map_fswq ? me->map_fsw:"");
1755*0Sstevel@tonic-gate 		trace_prt(1, "\t mntlevel=%d\t%s\t%s err=%d\n",
1756*0Sstevel@tonic-gate 			me->map_mntlevel,
1757*0Sstevel@tonic-gate 			me->map_modified ? "modify=TRUE":"modify=FALSE",
1758*0Sstevel@tonic-gate 			me->map_faked ? "faked=TRUE":"faked=FALSE",
1759*0Sstevel@tonic-gate 			me->map_err);
1760*0Sstevel@tonic-gate 	}
1761*0Sstevel@tonic-gate }
1762*0Sstevel@tonic-gate 
1763*0Sstevel@tonic-gate /*
1764*0Sstevel@tonic-gate  * trace_hierarchy(hiernode *node)
1765*0Sstevel@tonic-gate  * traces the allocated hiernode given the head of the structure
1766*0Sstevel@tonic-gate  * recursively calls itself until it traces entire structure.
1767*0Sstevel@tonic-gate  * the first call made at the root is made with a zero level.
1768*0Sstevel@tonic-gate  * nodelevel is simply used to print tab and make the tracing clean.
1769*0Sstevel@tonic-gate  * Returns nothing.
1770*0Sstevel@tonic-gate  */
1771*0Sstevel@tonic-gate static void
1772*0Sstevel@tonic-gate trace_hierarchy(hiernode *node, int nodelevel)
1773*0Sstevel@tonic-gate {
1774*0Sstevel@tonic-gate 	hiernode *currnode = node;
1775*0Sstevel@tonic-gate 	int i;
1776*0Sstevel@tonic-gate 
1777*0Sstevel@tonic-gate 	while (currnode != NULL) {
1778*0Sstevel@tonic-gate 		if (currnode->subdir != NULL) {
1779*0Sstevel@tonic-gate 			for (i = 0; i < nodelevel; i++)
1780*0Sstevel@tonic-gate 				trace_prt(0, "\t");
1781*0Sstevel@tonic-gate 			trace_prt(0, "\t(%s, ",
1782*0Sstevel@tonic-gate 				currnode->dirname ? currnode->dirname :"");
1783*0Sstevel@tonic-gate 			if (currnode->mapent) {
1784*0Sstevel@tonic-gate 				trace_prt(0, "%d, %s)\n",
1785*0Sstevel@tonic-gate 					currnode->mapent->map_mntlevel,
1786*0Sstevel@tonic-gate 					currnode->mapent->map_mntopts ?
1787*0Sstevel@tonic-gate 					currnode->mapent->map_mntopts:"");
1788*0Sstevel@tonic-gate 			}
1789*0Sstevel@tonic-gate 			else
1790*0Sstevel@tonic-gate 				trace_prt(0, " ,)\n");
1791*0Sstevel@tonic-gate 			nodelevel++;
1792*0Sstevel@tonic-gate 			trace_hierarchy(currnode->subdir, nodelevel);
1793*0Sstevel@tonic-gate 		} else {
1794*0Sstevel@tonic-gate 			for (i = 0; i < nodelevel; i++)
1795*0Sstevel@tonic-gate 				trace_prt(0, "\t");
1796*0Sstevel@tonic-gate 			trace_prt(0, "\t(%s, ",
1797*0Sstevel@tonic-gate 				currnode->dirname ? currnode->dirname :"");
1798*0Sstevel@tonic-gate 			if (currnode->mapent) {
1799*0Sstevel@tonic-gate 				trace_prt(0, "%d, %s)\n",
1800*0Sstevel@tonic-gate 					currnode->mapent->map_mntlevel,
1801*0Sstevel@tonic-gate 					currnode->mapent->map_mntopts ?
1802*0Sstevel@tonic-gate 					currnode->mapent->map_mntopts:"");
1803*0Sstevel@tonic-gate 			}
1804*0Sstevel@tonic-gate 			else
1805*0Sstevel@tonic-gate 				trace_prt(0, ", )\n");
1806*0Sstevel@tonic-gate 		}
1807*0Sstevel@tonic-gate 		currnode = currnode->leveldir;
1808*0Sstevel@tonic-gate 	}
1809*0Sstevel@tonic-gate }
1810*0Sstevel@tonic-gate 
1811*0Sstevel@tonic-gate struct mapent *
1812*0Sstevel@tonic-gate do_mapent_hosts(mapopts, host, isdirect)
1813*0Sstevel@tonic-gate 	char *mapopts, *host;
1814*0Sstevel@tonic-gate 	uint_t isdirect;
1815*0Sstevel@tonic-gate {
1816*0Sstevel@tonic-gate 	CLIENT *cl;
1817*0Sstevel@tonic-gate 	struct mapent *me, *ms, *mp;
1818*0Sstevel@tonic-gate 	struct mapfs *mfs;
1819*0Sstevel@tonic-gate 	struct exportnode *ex = NULL;
1820*0Sstevel@tonic-gate 	struct exportnode *exlist, *texlist, **texp, *exnext;
1821*0Sstevel@tonic-gate 	struct timeval timeout;
1822*0Sstevel@tonic-gate 	enum clnt_stat clnt_stat;
1823*0Sstevel@tonic-gate 	char name[MAXPATHLEN];
1824*0Sstevel@tonic-gate 	char entryopts[MAXOPTSLEN];
1825*0Sstevel@tonic-gate 	char fstype[32], mounter[32];
1826*0Sstevel@tonic-gate 	int exlen, duplicate;
1827*0Sstevel@tonic-gate 	struct mnttab mb;	/* needed for hasmntopt() to get nfs version */
1828*0Sstevel@tonic-gate 	rpcvers_t nfsvers;	/* version in map options, 0 if not there */
1829*0Sstevel@tonic-gate 	rpcvers_t vers, versmin; /* used to negotiate nfs vers in pingnfs() */
1830*0Sstevel@tonic-gate 	int retries, delay;
1831*0Sstevel@tonic-gate 	int foundvers;
1832*0Sstevel@tonic-gate 
1833*0Sstevel@tonic-gate 	if (trace > 1)
1834*0Sstevel@tonic-gate 		trace_prt(1, "  do_mapent_hosts: host %s\n", host);
1835*0Sstevel@tonic-gate 
1836*0Sstevel@tonic-gate 	/* check for special case: host is me */
1837*0Sstevel@tonic-gate 
1838*0Sstevel@tonic-gate 	if (self_check(host)) {
1839*0Sstevel@tonic-gate 		ms = (struct mapent *)malloc(sizeof (*ms));
1840*0Sstevel@tonic-gate 		if (ms == NULL)
1841*0Sstevel@tonic-gate 			goto alloc_failed;
1842*0Sstevel@tonic-gate 		(void) memset((char *)ms, 0, sizeof (*ms));
1843*0Sstevel@tonic-gate 		(void) strcpy(fstype, MNTTYPE_NFS);
1844*0Sstevel@tonic-gate 		get_opts(mapopts, entryopts, fstype, NULL);
1845*0Sstevel@tonic-gate 		ms->map_mntopts = strdup(entryopts);
1846*0Sstevel@tonic-gate 		if (ms->map_mntopts == NULL)
1847*0Sstevel@tonic-gate 			goto alloc_failed;
1848*0Sstevel@tonic-gate 		ms->map_mounter = strdup(fstype);
1849*0Sstevel@tonic-gate 		if (ms->map_mounter == NULL)
1850*0Sstevel@tonic-gate 			goto alloc_failed;
1851*0Sstevel@tonic-gate 		ms->map_fstype = strdup(MNTTYPE_NFS);
1852*0Sstevel@tonic-gate 		if (ms->map_fstype == NULL)
1853*0Sstevel@tonic-gate 			goto alloc_failed;
1854*0Sstevel@tonic-gate 
1855*0Sstevel@tonic-gate 		if (isdirect)
1856*0Sstevel@tonic-gate 			name[0] = '\0';
1857*0Sstevel@tonic-gate 		else {
1858*0Sstevel@tonic-gate 			(void) strcpy(name, "/");
1859*0Sstevel@tonic-gate 			(void) strcat(name, host);
1860*0Sstevel@tonic-gate 		}
1861*0Sstevel@tonic-gate 		ms->map_root = strdup(name);
1862*0Sstevel@tonic-gate 		if (ms->map_root == NULL)
1863*0Sstevel@tonic-gate 			goto alloc_failed;
1864*0Sstevel@tonic-gate 		ms->map_mntpnt = strdup("");
1865*0Sstevel@tonic-gate 		if (ms->map_mntpnt == NULL)
1866*0Sstevel@tonic-gate 			goto alloc_failed;
1867*0Sstevel@tonic-gate 		mfs = (struct mapfs *)malloc(sizeof (*mfs));
1868*0Sstevel@tonic-gate 		if (mfs == NULL)
1869*0Sstevel@tonic-gate 			goto alloc_failed;
1870*0Sstevel@tonic-gate 		(void) memset((char *)mfs, 0, sizeof (*mfs));
1871*0Sstevel@tonic-gate 		ms->map_fs = mfs;
1872*0Sstevel@tonic-gate 		mfs->mfs_host = strdup(host);
1873*0Sstevel@tonic-gate 		if (mfs->mfs_host == NULL)
1874*0Sstevel@tonic-gate 			goto alloc_failed;
1875*0Sstevel@tonic-gate 		mfs->mfs_dir  = strdup("/");
1876*0Sstevel@tonic-gate 		if (mfs->mfs_dir == NULL)
1877*0Sstevel@tonic-gate 			goto alloc_failed;
1878*0Sstevel@tonic-gate 
1879*0Sstevel@tonic-gate 		/* initialize mntlevel and modify */
1880*0Sstevel@tonic-gate 		ms->map_mntlevel = -1;
1881*0Sstevel@tonic-gate 		ms->map_modified = FALSE;
1882*0Sstevel@tonic-gate 		ms->map_faked = FALSE;
1883*0Sstevel@tonic-gate 
1884*0Sstevel@tonic-gate 		if (trace > 1)
1885*0Sstevel@tonic-gate 			trace_prt(1,
1886*0Sstevel@tonic-gate 			"  do_mapent_hosts: self-host %s OK\n", host);
1887*0Sstevel@tonic-gate 
1888*0Sstevel@tonic-gate 		return (ms);
1889*0Sstevel@tonic-gate 	}
1890*0Sstevel@tonic-gate 
1891*0Sstevel@tonic-gate 	/*
1892*0Sstevel@tonic-gate 	 * Call pingnfs. Note that we can't have replicated hosts in /net.
1893*0Sstevel@tonic-gate 	 * XXX - we would like to avoid duplicating the across the wire calls
1894*0Sstevel@tonic-gate 	 * made here in nfsmount(). The pingnfs cache should help avoid it.
1895*0Sstevel@tonic-gate 	 */
1896*0Sstevel@tonic-gate 	mb.mnt_mntopts = mapopts;
1897*0Sstevel@tonic-gate 	foundvers = nopt(&mb, MNTOPT_VERS, (int *)&nfsvers);
1898*0Sstevel@tonic-gate 	if (!foundvers)
1899*0Sstevel@tonic-gate 		nfsvers = 0;
1900*0Sstevel@tonic-gate 	if (set_versrange(nfsvers, &vers, &versmin) != 0) {
1901*0Sstevel@tonic-gate 		syslog(LOG_ERR, "Incorrect NFS version specified for %s", host);
1902*0Sstevel@tonic-gate 		return ((struct mapent *)NULL);
1903*0Sstevel@tonic-gate 	}
1904*0Sstevel@tonic-gate 	if (pingnfs(host, get_retry(mapopts) + 1, &vers, versmin, 0, FALSE,
1905*0Sstevel@tonic-gate 	    NULL, NULL) != RPC_SUCCESS)
1906*0Sstevel@tonic-gate 		return ((struct mapent *)NULL);
1907*0Sstevel@tonic-gate 
1908*0Sstevel@tonic-gate 	retries = get_retry(mapopts);
1909*0Sstevel@tonic-gate 	delay = INITDELAY;
1910*0Sstevel@tonic-gate retry:
1911*0Sstevel@tonic-gate 	/* get export list of host */
1912*0Sstevel@tonic-gate 	cl = clnt_create(host, MOUNTPROG, MOUNTVERS, "circuit_v");
1913*0Sstevel@tonic-gate 	if (cl == NULL) {
1914*0Sstevel@tonic-gate 		cl = clnt_create(host, MOUNTPROG, MOUNTVERS, "datagram_v");
1915*0Sstevel@tonic-gate 		if (cl == NULL) {
1916*0Sstevel@tonic-gate 			syslog(LOG_ERR,
1917*0Sstevel@tonic-gate 			"do_mapent_hosts: %s %s", host, clnt_spcreateerror(""));
1918*0Sstevel@tonic-gate 			return ((struct mapent *)NULL);
1919*0Sstevel@tonic-gate 		}
1920*0Sstevel@tonic-gate 
1921*0Sstevel@tonic-gate 	}
1922*0Sstevel@tonic-gate #ifdef MALLOC_DEBUG
1923*0Sstevel@tonic-gate 	add_alloc("CLNT_HANDLE", cl, 0, __FILE__, __LINE__);
1924*0Sstevel@tonic-gate 	add_alloc("AUTH_HANDLE", cl->cl_auth, 0,
1925*0Sstevel@tonic-gate 		__FILE__, __LINE__);
1926*0Sstevel@tonic-gate #endif
1927*0Sstevel@tonic-gate 
1928*0Sstevel@tonic-gate 	timeout.tv_usec = 0;
1929*0Sstevel@tonic-gate 	timeout.tv_sec  = 25;
1930*0Sstevel@tonic-gate 	if (clnt_stat = clnt_call(cl, MOUNTPROC_EXPORT, xdr_void, 0,
1931*0Sstevel@tonic-gate 				xdr_exports, (caddr_t)&ex, timeout)) {
1932*0Sstevel@tonic-gate 
1933*0Sstevel@tonic-gate 		if (retries-- > 0) {
1934*0Sstevel@tonic-gate 			clnt_destroy(cl);
1935*0Sstevel@tonic-gate 			DELAY(delay);
1936*0Sstevel@tonic-gate 			goto retry;
1937*0Sstevel@tonic-gate 		}
1938*0Sstevel@tonic-gate 
1939*0Sstevel@tonic-gate 		syslog(LOG_ERR,
1940*0Sstevel@tonic-gate 			"do_mapent_hosts: %s: export list: %s",
1941*0Sstevel@tonic-gate 			host, clnt_sperrno(clnt_stat));
1942*0Sstevel@tonic-gate #ifdef MALLOC_DEBUG
1943*0Sstevel@tonic-gate 		drop_alloc("CLNT_HANDLE", cl, __FILE__, __LINE__);
1944*0Sstevel@tonic-gate 		drop_alloc("AUTH_HANDLE", cl->cl_auth,
1945*0Sstevel@tonic-gate 			__FILE__, __LINE__);
1946*0Sstevel@tonic-gate #endif
1947*0Sstevel@tonic-gate 		clnt_destroy(cl);
1948*0Sstevel@tonic-gate 		return ((struct mapent *)NULL);
1949*0Sstevel@tonic-gate 	}
1950*0Sstevel@tonic-gate 
1951*0Sstevel@tonic-gate #ifdef MALLOC_DEBUG
1952*0Sstevel@tonic-gate 	drop_alloc("CLNT_HANDLE", cl, __FILE__, __LINE__);
1953*0Sstevel@tonic-gate 	drop_alloc("AUTH_HANDLE", cl->cl_auth,
1954*0Sstevel@tonic-gate 		__FILE__, __LINE__);
1955*0Sstevel@tonic-gate #endif
1956*0Sstevel@tonic-gate 	clnt_destroy(cl);
1957*0Sstevel@tonic-gate 
1958*0Sstevel@tonic-gate 	if (ex == NULL) {
1959*0Sstevel@tonic-gate 		if (trace > 1)
1960*0Sstevel@tonic-gate 			trace_prt(1,
1961*0Sstevel@tonic-gate 			    gettext("  getmapent_hosts: null export list\n"));
1962*0Sstevel@tonic-gate 		return ((struct mapent *)NULL);
1963*0Sstevel@tonic-gate 	}
1964*0Sstevel@tonic-gate 
1965*0Sstevel@tonic-gate 	/* now sort by length of names - to get mount order right */
1966*0Sstevel@tonic-gate 	exlist = ex;
1967*0Sstevel@tonic-gate 	texlist = NULL;
1968*0Sstevel@tonic-gate #ifdef lint
1969*0Sstevel@tonic-gate 	exnext = NULL;
1970*0Sstevel@tonic-gate #endif
1971*0Sstevel@tonic-gate 	for (; ex; ex = exnext) {
1972*0Sstevel@tonic-gate 		exnext = ex->ex_next;
1973*0Sstevel@tonic-gate 		exlen = strlen(ex->ex_dir);
1974*0Sstevel@tonic-gate 		duplicate = 0;
1975*0Sstevel@tonic-gate 		for (texp = &texlist; *texp; texp = &((*texp)->ex_next)) {
1976*0Sstevel@tonic-gate 			if (exlen < (int)strlen((*texp)->ex_dir))
1977*0Sstevel@tonic-gate 				break;
1978*0Sstevel@tonic-gate 			duplicate = (strcmp(ex->ex_dir, (*texp)->ex_dir) == 0);
1979*0Sstevel@tonic-gate 			if (duplicate) {
1980*0Sstevel@tonic-gate 				/* disregard duplicate entry */
1981*0Sstevel@tonic-gate 				freeex_ent(ex);
1982*0Sstevel@tonic-gate 				break;
1983*0Sstevel@tonic-gate 			}
1984*0Sstevel@tonic-gate 		}
1985*0Sstevel@tonic-gate 		if (!duplicate) {
1986*0Sstevel@tonic-gate 			ex->ex_next = *texp;
1987*0Sstevel@tonic-gate 			*texp = ex;
1988*0Sstevel@tonic-gate 		}
1989*0Sstevel@tonic-gate 	}
1990*0Sstevel@tonic-gate 	exlist = texlist;
1991*0Sstevel@tonic-gate 
1992*0Sstevel@tonic-gate 	/*
1993*0Sstevel@tonic-gate 	 * The following ugly chunk of code crept in as
1994*0Sstevel@tonic-gate 	 * a result of cachefs.  If it's a cachefs mount
1995*0Sstevel@tonic-gate 	 * of an nfs filesystem, then have it handled as
1996*0Sstevel@tonic-gate 	 * an nfs mount but have cachefs do the mount.
1997*0Sstevel@tonic-gate 	 */
1998*0Sstevel@tonic-gate 	(void) strcpy(fstype, MNTTYPE_NFS);
1999*0Sstevel@tonic-gate 	get_opts(mapopts, entryopts, fstype, NULL);
2000*0Sstevel@tonic-gate 	(void) strcpy(mounter, fstype);
2001*0Sstevel@tonic-gate 	if (strcmp(fstype, MNTTYPE_CACHEFS) == 0) {
2002*0Sstevel@tonic-gate 		struct mnttab m;
2003*0Sstevel@tonic-gate 		char *p;
2004*0Sstevel@tonic-gate 
2005*0Sstevel@tonic-gate 		m.mnt_mntopts = entryopts;
2006*0Sstevel@tonic-gate 		if ((p = hasmntopt(&m, "backfstype")) != NULL) {
2007*0Sstevel@tonic-gate 			int len = strlen(MNTTYPE_NFS);
2008*0Sstevel@tonic-gate 
2009*0Sstevel@tonic-gate 			p += 11;
2010*0Sstevel@tonic-gate 			if (strncmp(p, MNTTYPE_NFS, len) == 0 &&
2011*0Sstevel@tonic-gate 			    (p[len] == '\0' || p[len] == ',')) {
2012*0Sstevel@tonic-gate 				/*
2013*0Sstevel@tonic-gate 				 * Cached nfs mount
2014*0Sstevel@tonic-gate 				 */
2015*0Sstevel@tonic-gate 				(void) strcpy(fstype, MNTTYPE_NFS);
2016*0Sstevel@tonic-gate 				(void) strcpy(mounter, MNTTYPE_CACHEFS);
2017*0Sstevel@tonic-gate 			}
2018*0Sstevel@tonic-gate 		}
2019*0Sstevel@tonic-gate 	}
2020*0Sstevel@tonic-gate 
2021*0Sstevel@tonic-gate 	/* Now create a mapent from the export list */
2022*0Sstevel@tonic-gate 	ms = NULL;
2023*0Sstevel@tonic-gate 	me = NULL;
2024*0Sstevel@tonic-gate 
2025*0Sstevel@tonic-gate 	for (ex = exlist; ex; ex = ex->ex_next) {
2026*0Sstevel@tonic-gate 		mp = me;
2027*0Sstevel@tonic-gate 		me = (struct mapent *)malloc(sizeof (*me));
2028*0Sstevel@tonic-gate 		if (me == NULL)
2029*0Sstevel@tonic-gate 			goto alloc_failed;
2030*0Sstevel@tonic-gate 		(void) memset((char *)me, 0, sizeof (*me));
2031*0Sstevel@tonic-gate 
2032*0Sstevel@tonic-gate 		if (ms == NULL)
2033*0Sstevel@tonic-gate 			ms = me;
2034*0Sstevel@tonic-gate 		else
2035*0Sstevel@tonic-gate 			mp->map_next = me;
2036*0Sstevel@tonic-gate 
2037*0Sstevel@tonic-gate 		if (isdirect)
2038*0Sstevel@tonic-gate 			name[0] = '\0';
2039*0Sstevel@tonic-gate 		else {
2040*0Sstevel@tonic-gate 			(void) strcpy(name, "/");
2041*0Sstevel@tonic-gate 			(void) strcat(name, host);
2042*0Sstevel@tonic-gate 		}
2043*0Sstevel@tonic-gate 		me->map_root = strdup(name);
2044*0Sstevel@tonic-gate 		if (me->map_root == NULL)
2045*0Sstevel@tonic-gate 			goto alloc_failed;
2046*0Sstevel@tonic-gate 
2047*0Sstevel@tonic-gate 		*name = '\0';
2048*0Sstevel@tonic-gate 		if (strcmp(ex->ex_dir, "/") != 0) {
2049*0Sstevel@tonic-gate 			if (*(ex->ex_dir) != '/')
2050*0Sstevel@tonic-gate 				(void) strcpy(name, "/");
2051*0Sstevel@tonic-gate 			(void) strcat(name, ex->ex_dir);
2052*0Sstevel@tonic-gate 		}
2053*0Sstevel@tonic-gate 		me->map_mntpnt = strdup(name);
2054*0Sstevel@tonic-gate 		if (me->map_mntpnt == NULL)
2055*0Sstevel@tonic-gate 			goto alloc_failed;
2056*0Sstevel@tonic-gate 
2057*0Sstevel@tonic-gate 		me->map_fstype = strdup(fstype);
2058*0Sstevel@tonic-gate 		if (me->map_fstype == NULL)
2059*0Sstevel@tonic-gate 			goto alloc_failed;
2060*0Sstevel@tonic-gate 		me->map_mounter = strdup(mounter);
2061*0Sstevel@tonic-gate 		if (me->map_mounter == NULL)
2062*0Sstevel@tonic-gate 			goto alloc_failed;
2063*0Sstevel@tonic-gate 		me->map_mntopts = strdup(entryopts);
2064*0Sstevel@tonic-gate 		if (me->map_mntopts == NULL)
2065*0Sstevel@tonic-gate 			goto alloc_failed;
2066*0Sstevel@tonic-gate 
2067*0Sstevel@tonic-gate 		mfs = (struct mapfs *)malloc(sizeof (*mfs));
2068*0Sstevel@tonic-gate 		if (mfs == NULL)
2069*0Sstevel@tonic-gate 			goto alloc_failed;
2070*0Sstevel@tonic-gate 		(void) memset((char *)mfs, 0, sizeof (*mfs));
2071*0Sstevel@tonic-gate 		me->map_fs = mfs;
2072*0Sstevel@tonic-gate 		mfs->mfs_host = strdup(host);
2073*0Sstevel@tonic-gate 		if (mfs->mfs_host == NULL)
2074*0Sstevel@tonic-gate 			goto alloc_failed;
2075*0Sstevel@tonic-gate 		mfs->mfs_dir = strdup(ex->ex_dir);
2076*0Sstevel@tonic-gate 		if (mfs->mfs_dir == NULL)
2077*0Sstevel@tonic-gate 			goto alloc_failed;
2078*0Sstevel@tonic-gate 
2079*0Sstevel@tonic-gate 		/* initialize mntlevel and modify values */
2080*0Sstevel@tonic-gate 		me->map_mntlevel = -1;
2081*0Sstevel@tonic-gate 		me->map_modified = FALSE;
2082*0Sstevel@tonic-gate 		me->map_faked = FALSE;
2083*0Sstevel@tonic-gate 	}
2084*0Sstevel@tonic-gate 	freeex(exlist);
2085*0Sstevel@tonic-gate 
2086*0Sstevel@tonic-gate 	if (trace > 1)
2087*0Sstevel@tonic-gate 		trace_prt(1, "  do_mapent_hosts: host %s OK\n", host);
2088*0Sstevel@tonic-gate 
2089*0Sstevel@tonic-gate 	return (ms);
2090*0Sstevel@tonic-gate 
2091*0Sstevel@tonic-gate alloc_failed:
2092*0Sstevel@tonic-gate 	syslog(LOG_ERR, "do_mapent_hosts: Memory allocation failed");
2093*0Sstevel@tonic-gate 	free_mapent(ms);
2094*0Sstevel@tonic-gate 	freeex(exlist);
2095*0Sstevel@tonic-gate 	return ((struct mapent *)NULL);
2096*0Sstevel@tonic-gate }
2097*0Sstevel@tonic-gate 
2098*0Sstevel@tonic-gate 
2099*0Sstevel@tonic-gate static void
2100*0Sstevel@tonic-gate freeex_ent(ex)
2101*0Sstevel@tonic-gate 	struct exportnode *ex;
2102*0Sstevel@tonic-gate {
2103*0Sstevel@tonic-gate 	struct groupnode *groups, *tmpgroups;
2104*0Sstevel@tonic-gate 
2105*0Sstevel@tonic-gate 	free(ex->ex_dir);
2106*0Sstevel@tonic-gate 	groups = ex->ex_groups;
2107*0Sstevel@tonic-gate 	while (groups) {
2108*0Sstevel@tonic-gate 		free(groups->gr_name);
2109*0Sstevel@tonic-gate 		tmpgroups = groups->gr_next;
2110*0Sstevel@tonic-gate 		free((char *)groups);
2111*0Sstevel@tonic-gate 		groups = tmpgroups;
2112*0Sstevel@tonic-gate 	}
2113*0Sstevel@tonic-gate 	free((char *)ex);
2114*0Sstevel@tonic-gate }
2115*0Sstevel@tonic-gate 
2116*0Sstevel@tonic-gate static void
2117*0Sstevel@tonic-gate freeex(ex)
2118*0Sstevel@tonic-gate 	struct exportnode *ex;
2119*0Sstevel@tonic-gate {
2120*0Sstevel@tonic-gate 	struct exportnode *tmpex;
2121*0Sstevel@tonic-gate 
2122*0Sstevel@tonic-gate 	while (ex) {
2123*0Sstevel@tonic-gate 		tmpex = ex->ex_next;
2124*0Sstevel@tonic-gate 		freeex_ent(ex);
2125*0Sstevel@tonic-gate 		ex = tmpex;
2126*0Sstevel@tonic-gate 	}
2127*0Sstevel@tonic-gate }
2128*0Sstevel@tonic-gate 
2129*0Sstevel@tonic-gate static const char uatfs_err[] = "submount under fstype=autofs not supported";
2130*0Sstevel@tonic-gate /*
2131*0Sstevel@tonic-gate  * dump_mapent_err(struct mapent *me, char *key, char *mapname)
2132*0Sstevel@tonic-gate  * syslog appropriate error in mapentries.
2133*0Sstevel@tonic-gate  */
2134*0Sstevel@tonic-gate static void dump_mapent_err(struct mapent *me, char *key, char *mapname)
2135*0Sstevel@tonic-gate {
2136*0Sstevel@tonic-gate 	switch (me->map_err) {
2137*0Sstevel@tonic-gate 	case MAPENT_NOERR:
2138*0Sstevel@tonic-gate 		if (verbose)
2139*0Sstevel@tonic-gate 			syslog(LOG_ERR,
2140*0Sstevel@tonic-gate 			"map=%s key=%s mntpnt=%s: no error");
2141*0Sstevel@tonic-gate 		break;
2142*0Sstevel@tonic-gate 	case MAPENT_UATFS:
2143*0Sstevel@tonic-gate 		syslog(LOG_ERR,
2144*0Sstevel@tonic-gate 		"mountpoint %s in map %s key %s not mounted: %s",
2145*0Sstevel@tonic-gate 		    me->map_mntpnt, mapname, key, uatfs_err);
2146*0Sstevel@tonic-gate 		break;
2147*0Sstevel@tonic-gate 	default:
2148*0Sstevel@tonic-gate 		if (verbose)
2149*0Sstevel@tonic-gate 			syslog(LOG_ERR,
2150*0Sstevel@tonic-gate 			"map=%s key=%s mntpnt=%s: unknown mapentry error");
2151*0Sstevel@tonic-gate 	}
2152*0Sstevel@tonic-gate }
2153