17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
54bff34e3Sthurlow * Common Development and Distribution License (the "License").
64bff34e3Sthurlow * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
227c478bd9Sstevel@tonic-gate * autod_parse.c
237c478bd9Sstevel@tonic-gate *
244bff34e3Sthurlow * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
257c478bd9Sstevel@tonic-gate * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate */
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <ctype.h>
307c478bd9Sstevel@tonic-gate #include <string.h>
317c478bd9Sstevel@tonic-gate #include <syslog.h>
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/stat.h>
347c478bd9Sstevel@tonic-gate #include <sys/param.h>
357c478bd9Sstevel@tonic-gate #include <errno.h>
367c478bd9Sstevel@tonic-gate #include <pwd.h>
377c478bd9Sstevel@tonic-gate #include <netinet/in.h>
387c478bd9Sstevel@tonic-gate #include <netdb.h>
397c478bd9Sstevel@tonic-gate #include <sys/tiuser.h>
407c478bd9Sstevel@tonic-gate #include <locale.h>
417c478bd9Sstevel@tonic-gate #include <stdlib.h>
427c478bd9Sstevel@tonic-gate #include <unistd.h>
437c478bd9Sstevel@tonic-gate #include <thread.h>
447c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
457c478bd9Sstevel@tonic-gate #include <rpcsvc/mount.h>
467c478bd9Sstevel@tonic-gate #include <fcntl.h>
477c478bd9Sstevel@tonic-gate #include <limits.h>
487c478bd9Sstevel@tonic-gate #include "automount.h"
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate * This structure is used to determine the hierarchical
527c478bd9Sstevel@tonic-gate * relationship between directories
537c478bd9Sstevel@tonic-gate */
5411606941Sjwahlig typedef struct _hiernode {
557c478bd9Sstevel@tonic-gate char dirname[MAXFILENAMELEN+1];
567c478bd9Sstevel@tonic-gate struct _hiernode *subdir;
577c478bd9Sstevel@tonic-gate struct _hiernode *leveldir;
587c478bd9Sstevel@tonic-gate struct mapent *mapent;
5911606941Sjwahlig } hiernode;
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate void free_mapent(struct mapent *);
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate static int mapline_to_mapent(struct mapent **, struct mapline *, char *, char *,
647c478bd9Sstevel@tonic-gate char *, char *, uint_t);
657c478bd9Sstevel@tonic-gate static int hierarchical_sort(struct mapent *, hiernode **, char *, char *);
667c478bd9Sstevel@tonic-gate static int push_options(hiernode *, char *, char *, int);
677c478bd9Sstevel@tonic-gate static int set_mapent_opts(struct mapent *, char *, char *, char *);
687c478bd9Sstevel@tonic-gate static void get_opts(char *, char *, char *, bool_t *);
697c478bd9Sstevel@tonic-gate static int fstype_opts(struct mapent *, char *, char *, char *);
707c478bd9Sstevel@tonic-gate static int modify_mapents(struct mapent **, char *, char *, char *, hiernode *,
717c478bd9Sstevel@tonic-gate char *, uint_t, bool_t);
727c478bd9Sstevel@tonic-gate static int set_and_fake_mapent_mntlevel(hiernode *, char *, char *, char *,
737c478bd9Sstevel@tonic-gate struct mapent **, uint_t, char *, bool_t);
747c478bd9Sstevel@tonic-gate static int mark_level1_root(hiernode *, char *);
757c478bd9Sstevel@tonic-gate static int mark_and_fake_level1_noroot(hiernode *, char *, char *, char *,
767c478bd9Sstevel@tonic-gate struct mapent **, uint_t i, char *);
777c478bd9Sstevel@tonic-gate static int convert_mapent_to_automount(struct mapent *, char *, char *);
787c478bd9Sstevel@tonic-gate static int automount_opts(char **, char *);
797c478bd9Sstevel@tonic-gate static int parse_fsinfo(char *, struct mapent *);
807c478bd9Sstevel@tonic-gate static int parse_nfs(char *, struct mapent *, char *, char *, char **, char **,
817c478bd9Sstevel@tonic-gate int);
827c478bd9Sstevel@tonic-gate static int parse_special(struct mapent *, char *, char *, char **, char **,
837c478bd9Sstevel@tonic-gate int);
847c478bd9Sstevel@tonic-gate static int get_dir_from_path(char *, char **, int);
857c478bd9Sstevel@tonic-gate static int alloc_hiernode(hiernode **, char *);
867c478bd9Sstevel@tonic-gate static void free_hiernode(hiernode *);
877c478bd9Sstevel@tonic-gate static void trace_mapents(char *, struct mapent *);
887c478bd9Sstevel@tonic-gate static void trace_hierarchy(hiernode *, int);
897c478bd9Sstevel@tonic-gate static struct mapent *do_mapent_hosts(char *, char *, uint_t);
907c478bd9Sstevel@tonic-gate static void freeex_ent(struct exportnode *);
917c478bd9Sstevel@tonic-gate static void freeex(struct exportnode *);
927c478bd9Sstevel@tonic-gate static void dump_mapent_err(struct mapent *, char *, char *);
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate #define PARSE_OK 0
957c478bd9Sstevel@tonic-gate #define PARSE_ERROR -1
967c478bd9Sstevel@tonic-gate #define MAX_FSLEN 32
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate * mapentry error type defininitions
1007c478bd9Sstevel@tonic-gate */
1017c478bd9Sstevel@tonic-gate #define MAPENT_NOERR 0
1027c478bd9Sstevel@tonic-gate #define MAPENT_UATFS 1
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate * parse_entry(char *key, char *mapname, char *mapopts, struct mapline *ml,
1067c478bd9Sstevel@tonic-gate * char *subdir, uint_t isdirect, bool_t mount_access)
1077c478bd9Sstevel@tonic-gate * Parses the data in ml to build a mapentry list containing the information
1087c478bd9Sstevel@tonic-gate * for the mounts/lookups to be performed. Builds an intermediate mapentry list
1097c478bd9Sstevel@tonic-gate * by processing ml, hierarchically sorts (builds a tree of) the list according
1107c478bd9Sstevel@tonic-gate * to mountpoint. Then pushes options down the hierarchy, and fills in the mount
1117c478bd9Sstevel@tonic-gate * file system. Finally, modifies the intermediate list depending on how far
1127c478bd9Sstevel@tonic-gate * in the hierarchy the current request is (uses subdir). Deals with special
1137c478bd9Sstevel@tonic-gate * case of /net map parsing.
1147c478bd9Sstevel@tonic-gate * Returns a pointer to the head of the mapentry list.
1157c478bd9Sstevel@tonic-gate */
1167c478bd9Sstevel@tonic-gate struct mapent *
parse_entry(char * key,char * mapname,char * mapopts,struct mapline * ml,char * subdir,uint_t isdirect,bool_t mount_access)1177c478bd9Sstevel@tonic-gate parse_entry(char *key, char *mapname, char *mapopts, struct mapline *ml,
1187c478bd9Sstevel@tonic-gate char *subdir, uint_t isdirect, bool_t mount_access)
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate char *p;
1217c478bd9Sstevel@tonic-gate char defaultopts[AUTOFS_MAXOPTSLEN];
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate struct mapent *mapents = NULL;
1247c478bd9Sstevel@tonic-gate hiernode *rootnode = NULL;
1257c478bd9Sstevel@tonic-gate char *lp = ml->linebuf;
1267c478bd9Sstevel@tonic-gate
1277c478bd9Sstevel@tonic-gate if (trace > 1)
1287c478bd9Sstevel@tonic-gate trace_prt(1, " mapline: %s\n", ml->linebuf);
1297c478bd9Sstevel@tonic-gate
1307c478bd9Sstevel@tonic-gate /*
1317c478bd9Sstevel@tonic-gate * Assure the key is only one token long.
1327c478bd9Sstevel@tonic-gate * This prevents options from sneaking in through the
1337c478bd9Sstevel@tonic-gate * command line or corruption of /etc/mnttab.
1347c478bd9Sstevel@tonic-gate */
1357c478bd9Sstevel@tonic-gate for (p = key; *p != '\0'; p++) {
1367c478bd9Sstevel@tonic-gate if (isspace(*p)) {
1377c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
1387c478bd9Sstevel@tonic-gate "parse_entry: bad key in map %s: %s", mapname, key);
1397c478bd9Sstevel@tonic-gate return ((struct mapent *)NULL);
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate
1437c478bd9Sstevel@tonic-gate /*
1447c478bd9Sstevel@tonic-gate * select the appropriate parser, and build the mapentry list
1457c478bd9Sstevel@tonic-gate */
1467c478bd9Sstevel@tonic-gate if (strcmp(lp, "-hosts") == 0) {
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate * the /net parser - uses do_mapent_hosts to build mapents.
1497c478bd9Sstevel@tonic-gate * The mapopts are considered default for every entry, so we
1507c478bd9Sstevel@tonic-gate * don't push options down hierarchies.
1517c478bd9Sstevel@tonic-gate */
1527c478bd9Sstevel@tonic-gate mapents = do_mapent_hosts(mapopts, key, isdirect);
1537c478bd9Sstevel@tonic-gate if (mapents == NULL) /* nothing to free */
1547c478bd9Sstevel@tonic-gate return (mapents);
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate if (trace > 3)
1577c478bd9Sstevel@tonic-gate trace_mapents("do_mapent_hosts:(return)", mapents);
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate if (hierarchical_sort(mapents, &rootnode, key, mapname)
1607c478bd9Sstevel@tonic-gate != PARSE_OK)
1617c478bd9Sstevel@tonic-gate goto parse_error;
1627c478bd9Sstevel@tonic-gate } else {
1637c478bd9Sstevel@tonic-gate /*
1647c478bd9Sstevel@tonic-gate * all other parsing
1657c478bd9Sstevel@tonic-gate */
1667c478bd9Sstevel@tonic-gate if (mapline_to_mapent(&mapents, ml, key, mapname,
1677c478bd9Sstevel@tonic-gate mapopts, defaultopts, isdirect) != PARSE_OK)
1687c478bd9Sstevel@tonic-gate goto parse_error;
1697c478bd9Sstevel@tonic-gate
1707c478bd9Sstevel@tonic-gate if (mapents == NULL)
1717c478bd9Sstevel@tonic-gate return (mapents);
1727c478bd9Sstevel@tonic-gate
1737c478bd9Sstevel@tonic-gate if (hierarchical_sort(mapents, &rootnode, key, mapname)
1747c478bd9Sstevel@tonic-gate != PARSE_OK)
1757c478bd9Sstevel@tonic-gate goto parse_error;
1767c478bd9Sstevel@tonic-gate
1777c478bd9Sstevel@tonic-gate if (push_options(rootnode, defaultopts, mapopts,
1787c478bd9Sstevel@tonic-gate MAPENT_NOERR) != PARSE_OK)
1797c478bd9Sstevel@tonic-gate goto parse_error;
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate if (trace > 3) {
1827c478bd9Sstevel@tonic-gate trace_prt(1, "\n\tpush_options (return)\n");
1837c478bd9Sstevel@tonic-gate trace_prt(0, "\tdefault options=%s\n", defaultopts);
1847c478bd9Sstevel@tonic-gate trace_hierarchy(rootnode, 0);
1857c478bd9Sstevel@tonic-gate };
1867c478bd9Sstevel@tonic-gate
1877c478bd9Sstevel@tonic-gate if (parse_fsinfo(mapname, mapents) != PARSE_OK)
1887c478bd9Sstevel@tonic-gate goto parse_error;
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate /*
1927c478bd9Sstevel@tonic-gate * Modify the mapentry list. We *must* do this only after
1937c478bd9Sstevel@tonic-gate * the mapentry list is completely built (since we need to
1947c478bd9Sstevel@tonic-gate * have parse_fsinfo called first).
1957c478bd9Sstevel@tonic-gate */
1967c478bd9Sstevel@tonic-gate if (modify_mapents(&mapents, mapname, mapopts, subdir,
1977c478bd9Sstevel@tonic-gate rootnode, key, isdirect, mount_access) != PARSE_OK)
1987c478bd9Sstevel@tonic-gate goto parse_error;
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate /*
2017c478bd9Sstevel@tonic-gate * XXX: its dangerous to use rootnode after modify mapents as
2027c478bd9Sstevel@tonic-gate * it may be pointing to mapents that have been freed
2037c478bd9Sstevel@tonic-gate */
2047c478bd9Sstevel@tonic-gate if (rootnode != NULL)
2057c478bd9Sstevel@tonic-gate free_hiernode(rootnode);
2067c478bd9Sstevel@tonic-gate
2077c478bd9Sstevel@tonic-gate return (mapents);
2087c478bd9Sstevel@tonic-gate
2097c478bd9Sstevel@tonic-gate parse_error:
2107c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "parse_entry: mapentry parse error: map=%s key=%s",
2117c478bd9Sstevel@tonic-gate mapname, key);
2127c478bd9Sstevel@tonic-gate free_mapent(mapents);
2137c478bd9Sstevel@tonic-gate if (rootnode != NULL)
2147c478bd9Sstevel@tonic-gate free_hiernode(rootnode);
2157c478bd9Sstevel@tonic-gate return ((struct mapent *)NULL);
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate
2197c478bd9Sstevel@tonic-gate /*
2207c478bd9Sstevel@tonic-gate * mapline_to_mapent(struct mapent **mapents, struct mapline *ml,
2217c478bd9Sstevel@tonic-gate * char *key, char *mapname, char *mapopts, char *defaultopts,
2227c478bd9Sstevel@tonic-gate * uint_t isdirect)
2237c478bd9Sstevel@tonic-gate * Parses the mapline information in ml word by word to build an intermediate
2247c478bd9Sstevel@tonic-gate * mapentry list, which is passed back to the caller. The mapentries may have
2257c478bd9Sstevel@tonic-gate * holes (example no options), as they are completed only later. The logic is
2267c478bd9Sstevel@tonic-gate * awkward, but needed to provide the supported flexibility in the map entries.
2277c478bd9Sstevel@tonic-gate * (especially the first line). Note that the key is the full pathname of the
2287c478bd9Sstevel@tonic-gate * directory to be mounted in a direct map, and ml is the mapentry beyond key.
2297c478bd9Sstevel@tonic-gate * Returns PARSE_OK or an appropriate error value.
2307c478bd9Sstevel@tonic-gate */
2317c478bd9Sstevel@tonic-gate static int
mapline_to_mapent(struct mapent ** mapents,struct mapline * ml,char * key,char * mapname,char * mapopts,char * defaultopts,uint_t isdirect)2327c478bd9Sstevel@tonic-gate mapline_to_mapent(struct mapent **mapents, struct mapline *ml, char *key,
2337c478bd9Sstevel@tonic-gate char *mapname, char *mapopts, char *defaultopts,
2347c478bd9Sstevel@tonic-gate uint_t isdirect)
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate struct mapent *me = NULL;
2377c478bd9Sstevel@tonic-gate struct mapent *mp;
2387c478bd9Sstevel@tonic-gate char w[MAXPATHLEN];
2397c478bd9Sstevel@tonic-gate char wq[MAXPATHLEN];
2407c478bd9Sstevel@tonic-gate char w1[MAXPATHLEN];
2417c478bd9Sstevel@tonic-gate int implied;
2427c478bd9Sstevel@tonic-gate
2437c478bd9Sstevel@tonic-gate char *lp = ml->linebuf;
2447c478bd9Sstevel@tonic-gate char *lq = ml->lineqbuf;
2457c478bd9Sstevel@tonic-gate
2467c478bd9Sstevel@tonic-gate /* do any macro expansions that are required to complete ml */
2477c478bd9Sstevel@tonic-gate if (macro_expand(key, lp, lq, LINESZ)) {
2487c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
2497c478bd9Sstevel@tonic-gate "mapline_to_mapent: map %s: line too long (max %d chars)",
2507c478bd9Sstevel@tonic-gate mapname, LINESZ - 1);
2517c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate if (trace > 3 && (strcmp(ml->linebuf, lp) != 0))
2547c478bd9Sstevel@tonic-gate trace_prt(1,
2557c478bd9Sstevel@tonic-gate " mapline_to_mapent: (expanded) mapline (%s,%s)\n",
2567c478bd9Sstevel@tonic-gate ml->linebuf, ml->lineqbuf);
2577c478bd9Sstevel@tonic-gate
2587c478bd9Sstevel@tonic-gate /* init the head of mapentry list to null */
2597c478bd9Sstevel@tonic-gate *mapents = NULL;
2607c478bd9Sstevel@tonic-gate
2617c478bd9Sstevel@tonic-gate /*
2627c478bd9Sstevel@tonic-gate * Get the first word - its either a '-' if default options provided,
2637c478bd9Sstevel@tonic-gate * a '/', if the mountroot is implicitly provided, or a mount filesystem
2647c478bd9Sstevel@tonic-gate * if the mountroot is implicit. Note that if the first word begins with
2657c478bd9Sstevel@tonic-gate * a '-' then the second must be read and it must be a mountpoint or a
2667c478bd9Sstevel@tonic-gate * mount filesystem. Use mapopts if no default opts are provided.
2677c478bd9Sstevel@tonic-gate */
2687c478bd9Sstevel@tonic-gate if (getword(w, wq, &lp, &lq, ' ', sizeof (w)) == -1)
2697c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
2707c478bd9Sstevel@tonic-gate if (*w == '-') {
2717c478bd9Sstevel@tonic-gate strcpy(defaultopts, w);
2727c478bd9Sstevel@tonic-gate if (getword(w, wq, &lp, &lq, ' ', sizeof (w)) == -1)
2737c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
2747c478bd9Sstevel@tonic-gate } else
2757c478bd9Sstevel@tonic-gate strcpy(defaultopts, mapopts);
2767c478bd9Sstevel@tonic-gate
2774bff34e3Sthurlow /*
278*24101b26SRobert Thurlow * implied is true if there is no '/'
279*24101b26SRobert Thurlow * We need the same code path if we have an smbfs mount.
2804bff34e3Sthurlow */
281*24101b26SRobert Thurlow implied = (*w != '/') || (strstr(defaultopts, "fstype=smbfs") != NULL);
2827c478bd9Sstevel@tonic-gate while (*w == '/' || implied) {
2837c478bd9Sstevel@tonic-gate mp = me;
2847c478bd9Sstevel@tonic-gate if ((me = (struct mapent *)malloc(sizeof (*me))) == NULL)
2857c478bd9Sstevel@tonic-gate goto alloc_failed;
2867c478bd9Sstevel@tonic-gate (void) memset((char *)me, 0, sizeof (*me));
2877c478bd9Sstevel@tonic-gate if (*mapents == NULL) /* special case of head */
2887c478bd9Sstevel@tonic-gate *mapents = me;
2897c478bd9Sstevel@tonic-gate else
2907c478bd9Sstevel@tonic-gate mp->map_next = me;
2917c478bd9Sstevel@tonic-gate
2927c478bd9Sstevel@tonic-gate /*
2937c478bd9Sstevel@tonic-gate * direct maps get an empty string as root - to be filled
2947c478bd9Sstevel@tonic-gate * by the entire path later. Indirect maps get /key as the
2957c478bd9Sstevel@tonic-gate * map root. Note that xfn maps don't care about the root
2967c478bd9Sstevel@tonic-gate * - they override it in getmapent_fn().
2977c478bd9Sstevel@tonic-gate */
2987c478bd9Sstevel@tonic-gate if (isdirect) {
2997c478bd9Sstevel@tonic-gate *w1 = '\0';
3007c478bd9Sstevel@tonic-gate } else {
3017c478bd9Sstevel@tonic-gate strcpy(w1, "/");
3027c478bd9Sstevel@tonic-gate strcat(w1, key);
3037c478bd9Sstevel@tonic-gate }
3047c478bd9Sstevel@tonic-gate if ((me->map_root = strdup(w1)) == NULL)
3057c478bd9Sstevel@tonic-gate goto alloc_failed;
3067c478bd9Sstevel@tonic-gate
3077c478bd9Sstevel@tonic-gate /* mntpnt is empty for the mount root */
3087c478bd9Sstevel@tonic-gate if (strcmp(w, "/") == 0 || implied)
3097c478bd9Sstevel@tonic-gate me->map_mntpnt = strdup("");
3107c478bd9Sstevel@tonic-gate else
3117c478bd9Sstevel@tonic-gate me->map_mntpnt = strdup(w);
3127c478bd9Sstevel@tonic-gate if (me->map_mntpnt == NULL)
3137c478bd9Sstevel@tonic-gate goto alloc_failed;
3147c478bd9Sstevel@tonic-gate
3157c478bd9Sstevel@tonic-gate /*
3167c478bd9Sstevel@tonic-gate * If implied, the word must be a mount filesystem,
3177c478bd9Sstevel@tonic-gate * and its already read in; also turn off implied - its
3187c478bd9Sstevel@tonic-gate * not applicable except for the mount root. Else,
3197c478bd9Sstevel@tonic-gate * read another (or two) words depending on if there's
3207c478bd9Sstevel@tonic-gate * an option.
3217c478bd9Sstevel@tonic-gate */
3227c478bd9Sstevel@tonic-gate if (implied) /* must be a mount filesystem */
3237c478bd9Sstevel@tonic-gate implied = 0;
3247c478bd9Sstevel@tonic-gate else {
3257c478bd9Sstevel@tonic-gate if (getword(w, wq, &lp, &lq, ' ', sizeof (w)) == -1)
3267c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
3277c478bd9Sstevel@tonic-gate if (w[0] == '-') {
3287c478bd9Sstevel@tonic-gate /* mount options */
3297c478bd9Sstevel@tonic-gate if ((me->map_mntopts = strdup(w)) == NULL)
3307c478bd9Sstevel@tonic-gate goto alloc_failed;
3317c478bd9Sstevel@tonic-gate if (getword(w, wq, &lp, &lq, ' ',
3327c478bd9Sstevel@tonic-gate sizeof (w)) == -1)
3337c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
3347c478bd9Sstevel@tonic-gate }
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate
3377c478bd9Sstevel@tonic-gate /*
3387c478bd9Sstevel@tonic-gate * must be a mount filesystem or a set of filesystems at
3397c478bd9Sstevel@tonic-gate * this point.
3407c478bd9Sstevel@tonic-gate */
3417c478bd9Sstevel@tonic-gate if (w[0] == '\0' || w[0] == '-') {
3427c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
3437c478bd9Sstevel@tonic-gate "mapline_to_mapent: bad location=%s map=%s key=%s",
3447c478bd9Sstevel@tonic-gate w, mapname, key);
3457c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
3467c478bd9Sstevel@tonic-gate }
3477c478bd9Sstevel@tonic-gate
3487c478bd9Sstevel@tonic-gate /*
3497c478bd9Sstevel@tonic-gate * map_fsw and map_fswq hold information which will be
3507c478bd9Sstevel@tonic-gate * used to determine filesystem information at a later
3517c478bd9Sstevel@tonic-gate * point. This is required since we can only find out
3527c478bd9Sstevel@tonic-gate * about the mount file system after the directories
3537c478bd9Sstevel@tonic-gate * are hierarchically sorted and options have been pushed
3547c478bd9Sstevel@tonic-gate * down the hierarchies.
3557c478bd9Sstevel@tonic-gate */
3567c478bd9Sstevel@tonic-gate if (((me->map_fsw = strdup(w)) == NULL) ||
3577c478bd9Sstevel@tonic-gate ((me->map_fswq = strdup(wq)) == NULL))
3587c478bd9Sstevel@tonic-gate goto alloc_failed;
3597c478bd9Sstevel@tonic-gate
3607c478bd9Sstevel@tonic-gate /*
3617c478bd9Sstevel@tonic-gate * the next word, if any, is either another mount point or a
3627c478bd9Sstevel@tonic-gate * mount filesystem if more than one server is listed.
3637c478bd9Sstevel@tonic-gate */
3647c478bd9Sstevel@tonic-gate if (getword(w, wq, &lp, &lq, ' ', sizeof (w)) == -1)
3657c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
3667c478bd9Sstevel@tonic-gate while (*w && *w != '/') { /* more than 1 server listed */
3677c478bd9Sstevel@tonic-gate int len;
3687c478bd9Sstevel@tonic-gate char *fsw, *fswq;
3697c478bd9Sstevel@tonic-gate len = strlen(me->map_fsw) + strlen(w) + 4;
3707c478bd9Sstevel@tonic-gate if ((fsw = (char *)malloc(len)) == NULL)
3717c478bd9Sstevel@tonic-gate goto alloc_failed;
3727c478bd9Sstevel@tonic-gate sprintf(fsw, "%s %s", me->map_fsw, w);
3737c478bd9Sstevel@tonic-gate free(me->map_fsw);
3747c478bd9Sstevel@tonic-gate me->map_fsw = fsw;
3757c478bd9Sstevel@tonic-gate len = strlen(me->map_fswq) + strlen(wq) + 4;
3767c478bd9Sstevel@tonic-gate if ((fswq = (char *)malloc(len)) == NULL)
3777c478bd9Sstevel@tonic-gate goto alloc_failed;
3787c478bd9Sstevel@tonic-gate sprintf(fswq, "%s %s", me->map_fswq, wq);
3797c478bd9Sstevel@tonic-gate free(me->map_fswq);
3807c478bd9Sstevel@tonic-gate me->map_fswq = fswq;
3817c478bd9Sstevel@tonic-gate if (getword(w, wq, &lp, &lq, ' ', sizeof (w)) == -1)
3827c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
3837c478bd9Sstevel@tonic-gate }
3847c478bd9Sstevel@tonic-gate
3857c478bd9Sstevel@tonic-gate /* initialize flags */
3867c478bd9Sstevel@tonic-gate me->map_mntlevel = -1;
3877c478bd9Sstevel@tonic-gate me->map_modified = FALSE;
3887c478bd9Sstevel@tonic-gate me->map_faked = FALSE;
3897c478bd9Sstevel@tonic-gate me->map_err = MAPENT_NOERR;
3907c478bd9Sstevel@tonic-gate
3917c478bd9Sstevel@tonic-gate me->map_next = NULL;
3927c478bd9Sstevel@tonic-gate }
3937c478bd9Sstevel@tonic-gate
3947c478bd9Sstevel@tonic-gate if (*mapents == NULL || w[0] != '\0') { /* sanity check */
3957c478bd9Sstevel@tonic-gate if (verbose) {
3967c478bd9Sstevel@tonic-gate if (*mapents == NULL)
3977c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
3987c478bd9Sstevel@tonic-gate "mapline_to_mapent: parsed with null mapents");
3997c478bd9Sstevel@tonic-gate else
4007c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
4017c478bd9Sstevel@tonic-gate "mapline_to_mapent: parsed nononempty w=%s", w);
4027c478bd9Sstevel@tonic-gate }
4037c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
4047c478bd9Sstevel@tonic-gate }
4057c478bd9Sstevel@tonic-gate
4067c478bd9Sstevel@tonic-gate if (trace > 3)
4077c478bd9Sstevel@tonic-gate trace_mapents("mapline_to_mapent:", *mapents);
4087c478bd9Sstevel@tonic-gate
4097c478bd9Sstevel@tonic-gate return (PARSE_OK);
4107c478bd9Sstevel@tonic-gate
4117c478bd9Sstevel@tonic-gate alloc_failed:
4127c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "mapline_to_mapent: Memory allocation failed");
4137c478bd9Sstevel@tonic-gate return (ENOMEM);
4147c478bd9Sstevel@tonic-gate }
4157c478bd9Sstevel@tonic-gate
4167c478bd9Sstevel@tonic-gate /*
4177c478bd9Sstevel@tonic-gate * hierarchical_sort(struct mapent *mapents, hiernode **rootnode, char *key
4187c478bd9Sstevel@tonic-gate * char *mapname)
4197c478bd9Sstevel@tonic-gate * sorts the mntpnts in each mapent to build a hierarchy of nodes, with
4207c478bd9Sstevel@tonic-gate * with the rootnode being the mount root. The hierarchy is setup as
4217c478bd9Sstevel@tonic-gate * levels, and subdirs below each level. Provides a link from node to
4227c478bd9Sstevel@tonic-gate * the relevant mapentry.
4237c478bd9Sstevel@tonic-gate * Returns PARSE_OK or appropriate error value
4247c478bd9Sstevel@tonic-gate */
4257c478bd9Sstevel@tonic-gate static int
hierarchical_sort(struct mapent * mapents,hiernode ** rootnode,char * key,char * mapname)4267c478bd9Sstevel@tonic-gate hierarchical_sort(struct mapent *mapents, hiernode **rootnode, char *key,
4277c478bd9Sstevel@tonic-gate char *mapname)
4287c478bd9Sstevel@tonic-gate {
4297c478bd9Sstevel@tonic-gate hiernode *prevnode, *currnode, *newnode;
4307c478bd9Sstevel@tonic-gate char *path;
4317c478bd9Sstevel@tonic-gate char dirname[MAXFILENAMELEN];
4327c478bd9Sstevel@tonic-gate
4337c478bd9Sstevel@tonic-gate int rc = PARSE_OK;
4347c478bd9Sstevel@tonic-gate struct mapent *me = mapents;
4357c478bd9Sstevel@tonic-gate
4367c478bd9Sstevel@tonic-gate /* allocate the rootnode with a default path of "" */
4377c478bd9Sstevel@tonic-gate *rootnode = NULL;
4387c478bd9Sstevel@tonic-gate if ((rc = alloc_hiernode(rootnode, "")) != PARSE_OK)
4397c478bd9Sstevel@tonic-gate return (rc);
4407c478bd9Sstevel@tonic-gate
4417c478bd9Sstevel@tonic-gate /*
4427c478bd9Sstevel@tonic-gate * walk through mapents - for each mapent, locate the position
4437c478bd9Sstevel@tonic-gate * within the hierarchy by walking across leveldirs, and
4447c478bd9Sstevel@tonic-gate * subdirs of matched leveldirs. Starts one level below
4457c478bd9Sstevel@tonic-gate * the root (assumes an implicit match with rootnode).
4467c478bd9Sstevel@tonic-gate * XXX - this could probably be done more cleanly using recursion.
4477c478bd9Sstevel@tonic-gate */
4487c478bd9Sstevel@tonic-gate while (me != NULL) {
4497c478bd9Sstevel@tonic-gate
4507c478bd9Sstevel@tonic-gate path = me->map_mntpnt;
4517c478bd9Sstevel@tonic-gate
4527c478bd9Sstevel@tonic-gate if ((rc = get_dir_from_path(dirname, &path,
4537c478bd9Sstevel@tonic-gate sizeof (dirname))) != PARSE_OK)
4547c478bd9Sstevel@tonic-gate return (rc);
4557c478bd9Sstevel@tonic-gate
4567c478bd9Sstevel@tonic-gate prevnode = *rootnode;
4577c478bd9Sstevel@tonic-gate currnode = (*rootnode)->subdir;
4587c478bd9Sstevel@tonic-gate
4597c478bd9Sstevel@tonic-gate while (dirname[0] != '\0') {
4607c478bd9Sstevel@tonic-gate if (currnode != NULL) {
4617c478bd9Sstevel@tonic-gate if (strcmp(currnode->dirname, dirname) == 0) {
4627c478bd9Sstevel@tonic-gate /*
4637c478bd9Sstevel@tonic-gate * match found - mntpnt is a child of
4647c478bd9Sstevel@tonic-gate * this node
4657c478bd9Sstevel@tonic-gate */
4667c478bd9Sstevel@tonic-gate prevnode = currnode;
4677c478bd9Sstevel@tonic-gate currnode = currnode->subdir;
4687c478bd9Sstevel@tonic-gate } else {
4697c478bd9Sstevel@tonic-gate prevnode = currnode;
4707c478bd9Sstevel@tonic-gate currnode = currnode->leveldir;
4717c478bd9Sstevel@tonic-gate
4727c478bd9Sstevel@tonic-gate if (currnode == NULL) {
4737c478bd9Sstevel@tonic-gate /*
4747c478bd9Sstevel@tonic-gate * No more leveldirs to match.
4757c478bd9Sstevel@tonic-gate * Add a new one
4767c478bd9Sstevel@tonic-gate */
4777c478bd9Sstevel@tonic-gate if ((rc = alloc_hiernode
4787c478bd9Sstevel@tonic-gate (&newnode, dirname))
4797c478bd9Sstevel@tonic-gate != PARSE_OK)
4807c478bd9Sstevel@tonic-gate return (rc);
4817c478bd9Sstevel@tonic-gate prevnode->leveldir = newnode;
4827c478bd9Sstevel@tonic-gate prevnode = newnode;
4837c478bd9Sstevel@tonic-gate currnode = newnode->subdir;
4847c478bd9Sstevel@tonic-gate } else {
4857c478bd9Sstevel@tonic-gate /* try this leveldir */
4867c478bd9Sstevel@tonic-gate continue;
4877c478bd9Sstevel@tonic-gate }
4887c478bd9Sstevel@tonic-gate }
4897c478bd9Sstevel@tonic-gate } else {
4907c478bd9Sstevel@tonic-gate /* no more subdirs to match. Add a new one */
4917c478bd9Sstevel@tonic-gate if ((rc = alloc_hiernode(&newnode,
4927c478bd9Sstevel@tonic-gate dirname)) != PARSE_OK)
4937c478bd9Sstevel@tonic-gate return (rc);
4947c478bd9Sstevel@tonic-gate prevnode->subdir = newnode;
4957c478bd9Sstevel@tonic-gate prevnode = newnode;
4967c478bd9Sstevel@tonic-gate currnode = newnode->subdir;
4977c478bd9Sstevel@tonic-gate }
4987c478bd9Sstevel@tonic-gate if ((rc = get_dir_from_path(dirname, &path,
4997c478bd9Sstevel@tonic-gate sizeof (dirname))) != PARSE_OK)
5007c478bd9Sstevel@tonic-gate return (rc);
5017c478bd9Sstevel@tonic-gate }
5027c478bd9Sstevel@tonic-gate
5037c478bd9Sstevel@tonic-gate if (prevnode->mapent != NULL) {
5047c478bd9Sstevel@tonic-gate /* duplicate mntpoint found */
5057c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
5067c478bd9Sstevel@tonic-gate "hierarchical_sort: duplicate mntpnt map=%s key=%s",
5077c478bd9Sstevel@tonic-gate mapname, key);
5087c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
5097c478bd9Sstevel@tonic-gate }
5107c478bd9Sstevel@tonic-gate
5117c478bd9Sstevel@tonic-gate /* provide a pointer from node to mapent */
5127c478bd9Sstevel@tonic-gate prevnode->mapent = me;
5137c478bd9Sstevel@tonic-gate me = me->map_next;
5147c478bd9Sstevel@tonic-gate }
5157c478bd9Sstevel@tonic-gate
5167c478bd9Sstevel@tonic-gate if (trace > 3) {
5177c478bd9Sstevel@tonic-gate trace_prt(1, "\n\thierarchical_sort:\n");
5187c478bd9Sstevel@tonic-gate trace_hierarchy(*rootnode, 0); /* 0 is rootnode's level */
5197c478bd9Sstevel@tonic-gate }
5207c478bd9Sstevel@tonic-gate
5217c478bd9Sstevel@tonic-gate return (rc);
5227c478bd9Sstevel@tonic-gate }
5237c478bd9Sstevel@tonic-gate
5247c478bd9Sstevel@tonic-gate /*
5257c478bd9Sstevel@tonic-gate * push_options(hiernode *node, char *opts, char *mapopts, int err)
5267c478bd9Sstevel@tonic-gate * Pushes the options down a hierarchical structure. Works recursively from the
5277c478bd9Sstevel@tonic-gate * root, which is passed in on the first call. Uses a replacement policy.
5287c478bd9Sstevel@tonic-gate * If a node points to a mapentry, and it has an option, then thats the option
5297c478bd9Sstevel@tonic-gate * for that mapentry. Else, the node's mapent inherits the option from the
5307c478bd9Sstevel@tonic-gate * default (which may be the global option for the entry or mapopts).
5317c478bd9Sstevel@tonic-gate * err is useful in flagging entries with errors in pushing options.
5327c478bd9Sstevel@tonic-gate * returns PARSE_OK or appropriate error value.
5337c478bd9Sstevel@tonic-gate */
5347c478bd9Sstevel@tonic-gate static int
push_options(hiernode * node,char * defaultopts,char * mapopts,int err)5357c478bd9Sstevel@tonic-gate push_options(hiernode *node, char *defaultopts, char *mapopts, int err)
5367c478bd9Sstevel@tonic-gate {
5377c478bd9Sstevel@tonic-gate int rc = PARSE_OK;
5387c478bd9Sstevel@tonic-gate struct mapent *me = NULL;
5397c478bd9Sstevel@tonic-gate
5407c478bd9Sstevel@tonic-gate /* ensure that all the dirs at a level are passed the default options */
5417c478bd9Sstevel@tonic-gate while (node != NULL) {
5427c478bd9Sstevel@tonic-gate me = node->mapent;
5437c478bd9Sstevel@tonic-gate if (me != NULL) { /* not all nodes point to a mapentry */
5447c478bd9Sstevel@tonic-gate me->map_err = err;
5457c478bd9Sstevel@tonic-gate if ((rc = set_mapent_opts(me, me->map_mntopts,
5467c478bd9Sstevel@tonic-gate defaultopts, mapopts)) != PARSE_OK)
5477c478bd9Sstevel@tonic-gate return (rc);
5487c478bd9Sstevel@tonic-gate }
5497c478bd9Sstevel@tonic-gate
5507c478bd9Sstevel@tonic-gate /* push the options to subdirs */
5517c478bd9Sstevel@tonic-gate if (node->subdir != NULL) {
5527c478bd9Sstevel@tonic-gate if (node->mapent && strcmp(node->mapent->map_fstype,
5537c478bd9Sstevel@tonic-gate MNTTYPE_AUTOFS) == 0)
5547c478bd9Sstevel@tonic-gate err = MAPENT_UATFS;
5557c478bd9Sstevel@tonic-gate if ((rc = push_options(node->subdir, defaultopts,
5567c478bd9Sstevel@tonic-gate mapopts, err)) != PARSE_OK)
5577c478bd9Sstevel@tonic-gate return (rc);
5587c478bd9Sstevel@tonic-gate }
5597c478bd9Sstevel@tonic-gate node = node->leveldir;
5607c478bd9Sstevel@tonic-gate }
5617c478bd9Sstevel@tonic-gate return (rc);
5627c478bd9Sstevel@tonic-gate }
5637c478bd9Sstevel@tonic-gate
5647c478bd9Sstevel@tonic-gate #define BACKFSTYPE "backfstype" /* used in cachefs options */
5657c478bd9Sstevel@tonic-gate #define BACKFSTYPE_EQ "backfstype="
5667c478bd9Sstevel@tonic-gate #define FSTYPE "fstype"
5677c478bd9Sstevel@tonic-gate #define FSTYPE_EQ "fstype="
5687c478bd9Sstevel@tonic-gate #define NO_OPTS ""
5697c478bd9Sstevel@tonic-gate
5707c478bd9Sstevel@tonic-gate /*
5717c478bd9Sstevel@tonic-gate * set_mapent_opts(struct mapent *me, char *opts, char *defaultopts,
5727c478bd9Sstevel@tonic-gate * char *mapopts)
5737c478bd9Sstevel@tonic-gate * sets the mapentry's options, fstype and mounter fields by separating
5747c478bd9Sstevel@tonic-gate * out the fstype part from the opts. Use default options if opts is NULL.
5757c478bd9Sstevel@tonic-gate * Note taht defaultopts may be the same as mapopts.
5767c478bd9Sstevel@tonic-gate * Returns PARSE_OK or appropriate error value.
5777c478bd9Sstevel@tonic-gate */
5787c478bd9Sstevel@tonic-gate static int
set_mapent_opts(struct mapent * me,char * opts,char * defaultopts,char * mapopts)5797c478bd9Sstevel@tonic-gate set_mapent_opts(struct mapent *me, char *opts, char *defaultopts,
5807c478bd9Sstevel@tonic-gate char *mapopts)
5817c478bd9Sstevel@tonic-gate {
5827c478bd9Sstevel@tonic-gate char entryopts[AUTOFS_MAXOPTSLEN];
5837c478bd9Sstevel@tonic-gate char fstype[MAX_FSLEN], mounter[MAX_FSLEN];
5847c478bd9Sstevel@tonic-gate int rc = PARSE_OK;
5857c478bd9Sstevel@tonic-gate bool_t fstype_opt = FALSE;
5867c478bd9Sstevel@tonic-gate
5877c478bd9Sstevel@tonic-gate strcpy(fstype, MNTTYPE_NFS); /* default */
5887c478bd9Sstevel@tonic-gate
5897c478bd9Sstevel@tonic-gate /* set options to default options, if none exist for this entry */
5907c478bd9Sstevel@tonic-gate if (opts == NULL) {
5917c478bd9Sstevel@tonic-gate opts = defaultopts;
5927c478bd9Sstevel@tonic-gate if (defaultopts == NULL) { /* NULL opts for entry */
5937c478bd9Sstevel@tonic-gate strcpy(mounter, fstype);
5947c478bd9Sstevel@tonic-gate goto done;
5957c478bd9Sstevel@tonic-gate }
5967c478bd9Sstevel@tonic-gate }
5977c478bd9Sstevel@tonic-gate if (*opts == '-')
5987c478bd9Sstevel@tonic-gate opts++;
5997c478bd9Sstevel@tonic-gate
6007c478bd9Sstevel@tonic-gate /* separate opts into fstype and (other) entrypopts */
6017c478bd9Sstevel@tonic-gate get_opts(opts, entryopts, fstype, &fstype_opt);
6027c478bd9Sstevel@tonic-gate
6037c478bd9Sstevel@tonic-gate /* replace any existing opts */
6047c478bd9Sstevel@tonic-gate if (me->map_mntopts != NULL)
6057c478bd9Sstevel@tonic-gate free(me->map_mntopts);
6067c478bd9Sstevel@tonic-gate if ((me->map_mntopts = strdup(entryopts)) == NULL)
6077c478bd9Sstevel@tonic-gate return (ENOMEM);
6087c478bd9Sstevel@tonic-gate strcpy(mounter, fstype);
6097c478bd9Sstevel@tonic-gate
6107c478bd9Sstevel@tonic-gate /*
6117c478bd9Sstevel@tonic-gate * The following ugly chunk of code crept in as a result of
6127c478bd9Sstevel@tonic-gate * cachefs. If it's a cachefs mount of an nfs filesystem, then
6137c478bd9Sstevel@tonic-gate * it's important to parse the nfs special field. Otherwise,
6147c478bd9Sstevel@tonic-gate * just hand the special field to the fs-specific mount
6157c478bd9Sstevel@tonic-gate */
6167c478bd9Sstevel@tonic-gate if (strcmp(fstype, MNTTYPE_CACHEFS) == 0) {
6177c478bd9Sstevel@tonic-gate struct mnttab m;
6187c478bd9Sstevel@tonic-gate char *p;
6197c478bd9Sstevel@tonic-gate
6207c478bd9Sstevel@tonic-gate m.mnt_mntopts = entryopts;
6217c478bd9Sstevel@tonic-gate if ((p = hasmntopt(&m, BACKFSTYPE)) != NULL) {
6227c478bd9Sstevel@tonic-gate int len = strlen(MNTTYPE_NFS);
6237c478bd9Sstevel@tonic-gate
6247c478bd9Sstevel@tonic-gate p += strlen(BACKFSTYPE_EQ);
6257c478bd9Sstevel@tonic-gate
6267c478bd9Sstevel@tonic-gate if (strncmp(p, MNTTYPE_NFS, len) == 0 &&
6277c478bd9Sstevel@tonic-gate (p[len] == '\0' || p[len] == ',')) {
6287c478bd9Sstevel@tonic-gate /*
6297c478bd9Sstevel@tonic-gate * Cached nfs mount
6307c478bd9Sstevel@tonic-gate */
6317c478bd9Sstevel@tonic-gate (void) strcpy(fstype, MNTTYPE_NFS);
6327c478bd9Sstevel@tonic-gate (void) strcpy(mounter, MNTTYPE_CACHEFS);
6337c478bd9Sstevel@tonic-gate }
6347c478bd9Sstevel@tonic-gate }
6357c478bd9Sstevel@tonic-gate }
6367c478bd9Sstevel@tonic-gate
6377c478bd9Sstevel@tonic-gate /*
6387c478bd9Sstevel@tonic-gate * child options are exactly fstype = somefs, we need to do some
6397c478bd9Sstevel@tonic-gate * more option pushing work.
6407c478bd9Sstevel@tonic-gate */
6417c478bd9Sstevel@tonic-gate if (fstype_opt == TRUE &&
6427c478bd9Sstevel@tonic-gate (strcmp(me->map_mntopts, NO_OPTS) == 0)) {
6437c478bd9Sstevel@tonic-gate free(me->map_mntopts);
6447c478bd9Sstevel@tonic-gate if ((rc = fstype_opts(me, opts, defaultopts,
6457c478bd9Sstevel@tonic-gate mapopts)) != PARSE_OK)
6467c478bd9Sstevel@tonic-gate return (rc);
6477c478bd9Sstevel@tonic-gate }
6487c478bd9Sstevel@tonic-gate
6497c478bd9Sstevel@tonic-gate done:
6507c478bd9Sstevel@tonic-gate if (((me->map_fstype = strdup(fstype)) == NULL) ||
6517c478bd9Sstevel@tonic-gate ((me->map_mounter = strdup(mounter)) == NULL)) {
6527c478bd9Sstevel@tonic-gate if (me->map_fstype != NULL)
6537c478bd9Sstevel@tonic-gate free(me->map_fstype);
6547c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "set_mapent_opts: No memory");
6557c478bd9Sstevel@tonic-gate return (ENOMEM);
6567c478bd9Sstevel@tonic-gate }
6577c478bd9Sstevel@tonic-gate
6587c478bd9Sstevel@tonic-gate return (rc);
6597c478bd9Sstevel@tonic-gate }
6607c478bd9Sstevel@tonic-gate
6617c478bd9Sstevel@tonic-gate /*
6627c478bd9Sstevel@tonic-gate * Check the option string for an "fstype"
6637c478bd9Sstevel@tonic-gate * option. If found, return the fstype
6647c478bd9Sstevel@tonic-gate * and the option string with the fstype
6657c478bd9Sstevel@tonic-gate * option removed, e.g.
6667c478bd9Sstevel@tonic-gate *
6677c478bd9Sstevel@tonic-gate * input: "fstype=cachefs,ro,nosuid"
6687c478bd9Sstevel@tonic-gate * opts: "ro,nosuid"
6697c478bd9Sstevel@tonic-gate * fstype: "cachefs"
6707c478bd9Sstevel@tonic-gate *
6717c478bd9Sstevel@tonic-gate * Also indicates if the fstype option was present
6727c478bd9Sstevel@tonic-gate * by setting a flag, if the pointer to the flag
6737c478bd9Sstevel@tonic-gate * is not NULL.
6747c478bd9Sstevel@tonic-gate */
6757c478bd9Sstevel@tonic-gate static void
get_opts(input,opts,fstype,fstype_opt)6767c478bd9Sstevel@tonic-gate get_opts(input, opts, fstype, fstype_opt)
6777c478bd9Sstevel@tonic-gate char *input;
6787c478bd9Sstevel@tonic-gate char *opts; /* output */
6797c478bd9Sstevel@tonic-gate char *fstype; /* output */
6807c478bd9Sstevel@tonic-gate bool_t *fstype_opt;
6817c478bd9Sstevel@tonic-gate {
6827c478bd9Sstevel@tonic-gate char *p, *pb;
6837c478bd9Sstevel@tonic-gate char buf[MAXOPTSLEN];
6847c478bd9Sstevel@tonic-gate char *placeholder;
6857c478bd9Sstevel@tonic-gate
6867c478bd9Sstevel@tonic-gate *opts = '\0';
6877c478bd9Sstevel@tonic-gate (void) strcpy(buf, input);
6887c478bd9Sstevel@tonic-gate pb = buf;
6897c478bd9Sstevel@tonic-gate while (p = (char *)strtok_r(pb, ",", &placeholder)) {
6907c478bd9Sstevel@tonic-gate pb = NULL;
6917c478bd9Sstevel@tonic-gate if (strncmp(p, FSTYPE_EQ, 7) == 0) {
6927c478bd9Sstevel@tonic-gate if (fstype_opt != NULL)
6937c478bd9Sstevel@tonic-gate *fstype_opt = TRUE;
6947c478bd9Sstevel@tonic-gate (void) strcpy(fstype, p + 7);
6957c478bd9Sstevel@tonic-gate } else {
6967c478bd9Sstevel@tonic-gate if (*opts)
6977c478bd9Sstevel@tonic-gate (void) strcat(opts, ",");
6987c478bd9Sstevel@tonic-gate (void) strcat(opts, p);
6997c478bd9Sstevel@tonic-gate }
7007c478bd9Sstevel@tonic-gate }
7017c478bd9Sstevel@tonic-gate }
7027c478bd9Sstevel@tonic-gate
7037c478bd9Sstevel@tonic-gate /*
7047c478bd9Sstevel@tonic-gate * fstype_opts(struct mapent *me, char *opts, char *defaultopts,
7057c478bd9Sstevel@tonic-gate * char *mapopts)
7067c478bd9Sstevel@tonic-gate * We need to push global options to the child entry if it is exactly
7077c478bd9Sstevel@tonic-gate * fstype=somefs.
7087c478bd9Sstevel@tonic-gate */
7097c478bd9Sstevel@tonic-gate static int
fstype_opts(struct mapent * me,char * opts,char * defaultopts,char * mapopts)7107c478bd9Sstevel@tonic-gate fstype_opts(struct mapent *me, char *opts, char *defaultopts,
7117c478bd9Sstevel@tonic-gate char *mapopts)
7127c478bd9Sstevel@tonic-gate {
7137c478bd9Sstevel@tonic-gate char pushopts[AUTOFS_MAXOPTSLEN];
7147c478bd9Sstevel@tonic-gate char pushentryopts[AUTOFS_MAXOPTSLEN];
7157c478bd9Sstevel@tonic-gate char pushfstype[MAX_FSLEN];
7167c478bd9Sstevel@tonic-gate
7177c478bd9Sstevel@tonic-gate if (defaultopts && *defaultopts == '-')
7187c478bd9Sstevel@tonic-gate defaultopts++;
7197c478bd9Sstevel@tonic-gate
7207c478bd9Sstevel@tonic-gate /*
7217c478bd9Sstevel@tonic-gate * the options to push are the global defaults for the entry,
7227c478bd9Sstevel@tonic-gate * if they exist, or mapopts, if the global defaults for the
7237c478bd9Sstevel@tonic-gate * entry does not exist.
7247c478bd9Sstevel@tonic-gate */
7257c478bd9Sstevel@tonic-gate if (strcmp(defaultopts, opts) == 0) {
7267c478bd9Sstevel@tonic-gate if (*mapopts == '-')
7277c478bd9Sstevel@tonic-gate mapopts++;
7287c478bd9Sstevel@tonic-gate get_opts(mapopts, pushentryopts, pushfstype, NULL);
7297c478bd9Sstevel@tonic-gate strcpy(pushopts, mapopts);
7307c478bd9Sstevel@tonic-gate } else {
7317c478bd9Sstevel@tonic-gate get_opts(defaultopts, pushentryopts, pushfstype, NULL);
7327c478bd9Sstevel@tonic-gate strcpy(pushopts, defaultopts);
7337c478bd9Sstevel@tonic-gate }
7347c478bd9Sstevel@tonic-gate
7357c478bd9Sstevel@tonic-gate if (strcmp(pushfstype, MNTTYPE_CACHEFS) == 0)
7367c478bd9Sstevel@tonic-gate me->map_mntopts = strdup(pushopts);
7377c478bd9Sstevel@tonic-gate else
7387c478bd9Sstevel@tonic-gate me->map_mntopts = strdup(pushentryopts);
7397c478bd9Sstevel@tonic-gate
7407c478bd9Sstevel@tonic-gate if (!me->map_mntopts) {
7417c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "fstype_opts: No memory");
7427c478bd9Sstevel@tonic-gate return (ENOMEM);
7437c478bd9Sstevel@tonic-gate }
7447c478bd9Sstevel@tonic-gate
7457c478bd9Sstevel@tonic-gate return (PARSE_OK);
7467c478bd9Sstevel@tonic-gate }
7477c478bd9Sstevel@tonic-gate
7487c478bd9Sstevel@tonic-gate /*
7497c478bd9Sstevel@tonic-gate * modify_mapents(struct mapent **mapents, char *mapname,
7507c478bd9Sstevel@tonic-gate * char *mapopts, char *subdir, hiernode *rootnode,
7517c478bd9Sstevel@tonic-gate * char *key, uint_t isdirect, bool_t mount_access)
7527c478bd9Sstevel@tonic-gate * modifies the intermediate mapentry list into the final one, and passes
7537c478bd9Sstevel@tonic-gate * back a pointer to it. The final list may contain faked mapentries for
7547c478bd9Sstevel@tonic-gate * hiernodes that do not point to a mapentry, or converted mapentries, if
7557c478bd9Sstevel@tonic-gate * hiernodes that point to a mapentry need to be converted from nfs to autofs.
7567c478bd9Sstevel@tonic-gate * mounts. Entries that are not directly 1 level below the subdir are removed.
7577c478bd9Sstevel@tonic-gate * Returns PARSE_OK or PARSE_ERROR
7587c478bd9Sstevel@tonic-gate */
7597c478bd9Sstevel@tonic-gate static int
modify_mapents(struct mapent ** mapents,char * mapname,char * mapopts,char * subdir,hiernode * rootnode,char * key,uint_t isdirect,bool_t mount_access)7607c478bd9Sstevel@tonic-gate modify_mapents(struct mapent **mapents, char *mapname,
7617c478bd9Sstevel@tonic-gate char *mapopts, char *subdir, hiernode *rootnode,
7627c478bd9Sstevel@tonic-gate char *key, uint_t isdirect, bool_t mount_access)
7637c478bd9Sstevel@tonic-gate {
7647c478bd9Sstevel@tonic-gate struct mapent *mp = NULL;
7657c478bd9Sstevel@tonic-gate char w[MAXPATHLEN];
7667c478bd9Sstevel@tonic-gate
7677c478bd9Sstevel@tonic-gate struct mapent *me;
7687c478bd9Sstevel@tonic-gate int rc = PARSE_OK;
7697c478bd9Sstevel@tonic-gate struct mapent *faked_mapents = NULL;
7707c478bd9Sstevel@tonic-gate
7717c478bd9Sstevel@tonic-gate /*
7727c478bd9Sstevel@tonic-gate * correct the mapentry mntlevel from default -1 to level depending on
7737c478bd9Sstevel@tonic-gate * position in hierarchy, and build any faked mapentries, if required
7747c478bd9Sstevel@tonic-gate * at one level below the rootnode given by subdir.
7757c478bd9Sstevel@tonic-gate */
7767c478bd9Sstevel@tonic-gate if ((rc = set_and_fake_mapent_mntlevel(rootnode, subdir, key, mapname,
7777c478bd9Sstevel@tonic-gate &faked_mapents, isdirect, mapopts, mount_access)) != PARSE_OK)
7787c478bd9Sstevel@tonic-gate return (rc);
7797c478bd9Sstevel@tonic-gate
7807c478bd9Sstevel@tonic-gate /*
7817c478bd9Sstevel@tonic-gate * attaches faked mapents to real mapents list. Assumes mapents
7827c478bd9Sstevel@tonic-gate * is not NULL.
7837c478bd9Sstevel@tonic-gate */
7847c478bd9Sstevel@tonic-gate me = *mapents;
7857c478bd9Sstevel@tonic-gate while (me->map_next != NULL)
7867c478bd9Sstevel@tonic-gate me = me->map_next;
7877c478bd9Sstevel@tonic-gate me->map_next = faked_mapents;
7887c478bd9Sstevel@tonic-gate
7897c478bd9Sstevel@tonic-gate /*
7907c478bd9Sstevel@tonic-gate * get rid of nodes marked at level -1
7917c478bd9Sstevel@tonic-gate */
7927c478bd9Sstevel@tonic-gate me = *mapents;
7937c478bd9Sstevel@tonic-gate while (me != NULL) {
7947c478bd9Sstevel@tonic-gate if ((me->map_mntlevel == -1) || (me->map_err) ||
7957c478bd9Sstevel@tonic-gate (mount_access == FALSE && me->map_mntlevel == 0)) {
7967c478bd9Sstevel@tonic-gate /*
7977c478bd9Sstevel@tonic-gate * syslog any errors and free entry
7987c478bd9Sstevel@tonic-gate */
7997c478bd9Sstevel@tonic-gate if (me->map_err)
8007c478bd9Sstevel@tonic-gate dump_mapent_err(me, key, mapname);
8017c478bd9Sstevel@tonic-gate
8027c478bd9Sstevel@tonic-gate if (me == (*mapents)) {
8037c478bd9Sstevel@tonic-gate /* special case when head has to be freed */
8047c478bd9Sstevel@tonic-gate *mapents = me->map_next;
8057c478bd9Sstevel@tonic-gate if ((*mapents) == NULL) {
8067c478bd9Sstevel@tonic-gate /* something wierd happened */
8077c478bd9Sstevel@tonic-gate if (verbose)
8087c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
8097c478bd9Sstevel@tonic-gate "modify_mapents: level error");
8107c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
8117c478bd9Sstevel@tonic-gate }
8127c478bd9Sstevel@tonic-gate
8137c478bd9Sstevel@tonic-gate /* separate out the node */
8147c478bd9Sstevel@tonic-gate me->map_next = NULL;
8157c478bd9Sstevel@tonic-gate free_mapent(me);
8167c478bd9Sstevel@tonic-gate me = *mapents;
8177c478bd9Sstevel@tonic-gate } else {
8187c478bd9Sstevel@tonic-gate mp->map_next = me->map_next;
8197c478bd9Sstevel@tonic-gate me->map_next = NULL;
8207c478bd9Sstevel@tonic-gate free_mapent(me);
8217c478bd9Sstevel@tonic-gate me = mp->map_next;
8227c478bd9Sstevel@tonic-gate }
8237c478bd9Sstevel@tonic-gate continue;
8247c478bd9Sstevel@tonic-gate }
8257c478bd9Sstevel@tonic-gate
8267c478bd9Sstevel@tonic-gate /*
8277c478bd9Sstevel@tonic-gate * convert level 1 mapents that are not already autonodes
8287c478bd9Sstevel@tonic-gate * to autonodes
8297c478bd9Sstevel@tonic-gate */
8307c478bd9Sstevel@tonic-gate if (me->map_mntlevel == 1 &&
8317c478bd9Sstevel@tonic-gate (strcmp(me->map_fstype, MNTTYPE_AUTOFS) != 0) &&
8327c478bd9Sstevel@tonic-gate (me->map_faked != TRUE)) {
8337c478bd9Sstevel@tonic-gate if ((rc = convert_mapent_to_automount(me, mapname,
8347c478bd9Sstevel@tonic-gate mapopts)) != PARSE_OK)
8357c478bd9Sstevel@tonic-gate return (rc);
8367c478bd9Sstevel@tonic-gate }
8377c478bd9Sstevel@tonic-gate strcpy(w, (me->map_mntpnt+strlen(subdir)));
8387c478bd9Sstevel@tonic-gate strcpy(me->map_mntpnt, w);
8397c478bd9Sstevel@tonic-gate mp = me;
8407c478bd9Sstevel@tonic-gate me = me->map_next;
8417c478bd9Sstevel@tonic-gate }
8427c478bd9Sstevel@tonic-gate
8437c478bd9Sstevel@tonic-gate if (trace > 3)
8447c478bd9Sstevel@tonic-gate trace_mapents("modify_mapents:", *mapents);
8457c478bd9Sstevel@tonic-gate
8467c478bd9Sstevel@tonic-gate return (PARSE_OK);
8477c478bd9Sstevel@tonic-gate }
8487c478bd9Sstevel@tonic-gate
8497c478bd9Sstevel@tonic-gate /*
8507c478bd9Sstevel@tonic-gate * set_and_fake_mapent_mntlevel(hiernode *rootnode, char *subdir, char *key,
8517c478bd9Sstevel@tonic-gate * char *mapname, struct mapent **faked_mapents,
8527c478bd9Sstevel@tonic-gate * uint_t isdirect, char *mapopts, bool_t mount_access)
8537c478bd9Sstevel@tonic-gate * sets the mapentry mount levels (depths) with respect to the subdir.
8547c478bd9Sstevel@tonic-gate * Assigns a value of 0 to the new root. Finds the level1 directories by
8557c478bd9Sstevel@tonic-gate * calling mark_*_level1_*(). Also cleans off extra /'s in level0 and
8567c478bd9Sstevel@tonic-gate * level1 map_mntpnts. Note that one level below the new root is an existing
8577c478bd9Sstevel@tonic-gate * mapentry if there's a mapentry (nfs mount) corresponding to the root,
8587c478bd9Sstevel@tonic-gate * and the direct subdir set for the root, if there's no mapentry corresponding
8597c478bd9Sstevel@tonic-gate * to the root (we install autodirs). Returns PARSE_OK or error value.
8607c478bd9Sstevel@tonic-gate */
8617c478bd9Sstevel@tonic-gate static int
set_and_fake_mapent_mntlevel(hiernode * rootnode,char * subdir,char * key,char * mapname,struct mapent ** faked_mapents,uint_t isdirect,char * mapopts,bool_t mount_access)8627c478bd9Sstevel@tonic-gate set_and_fake_mapent_mntlevel(hiernode *rootnode, char *subdir, char *key,
8637c478bd9Sstevel@tonic-gate char *mapname, struct mapent **faked_mapents,
8647c478bd9Sstevel@tonic-gate uint_t isdirect, char *mapopts, bool_t mount_access)
8657c478bd9Sstevel@tonic-gate {
8667c478bd9Sstevel@tonic-gate char dirname[MAXFILENAMELEN];
8677c478bd9Sstevel@tonic-gate char traversed_path[MAXPATHLEN]; /* used in building fake mapentries */
8687c478bd9Sstevel@tonic-gate
8697c478bd9Sstevel@tonic-gate char *subdir_child = subdir;
8707c478bd9Sstevel@tonic-gate hiernode *prevnode = rootnode;
8717c478bd9Sstevel@tonic-gate hiernode *currnode = rootnode->subdir;
8727c478bd9Sstevel@tonic-gate int rc = PARSE_OK;
8737c478bd9Sstevel@tonic-gate traversed_path[0] = '\0';
8747c478bd9Sstevel@tonic-gate
8757c478bd9Sstevel@tonic-gate /*
8767c478bd9Sstevel@tonic-gate * find and mark the root by tracing down subdir. Use traversed_path
8777c478bd9Sstevel@tonic-gate * to keep track of how far we go, while guaranteeing that it
8787c478bd9Sstevel@tonic-gate * contains no '/' at the end. Took some mucking to get that right.
8797c478bd9Sstevel@tonic-gate */
8807c478bd9Sstevel@tonic-gate if ((rc = get_dir_from_path(dirname, &subdir_child, sizeof (dirname)))
8817c478bd9Sstevel@tonic-gate != PARSE_OK)
8827c478bd9Sstevel@tonic-gate return (rc);
8837c478bd9Sstevel@tonic-gate
8847c478bd9Sstevel@tonic-gate if (dirname[0] != '\0')
8857c478bd9Sstevel@tonic-gate sprintf(traversed_path, "%s/%s", traversed_path, dirname);
8867c478bd9Sstevel@tonic-gate
8877c478bd9Sstevel@tonic-gate prevnode = rootnode;
8887c478bd9Sstevel@tonic-gate currnode = rootnode->subdir;
8897c478bd9Sstevel@tonic-gate while (dirname[0] != '\0' && currnode != NULL) {
8907c478bd9Sstevel@tonic-gate if (strcmp(currnode->dirname, dirname) == 0) {
8917c478bd9Sstevel@tonic-gate
8927c478bd9Sstevel@tonic-gate /* subdir is a child of currnode */
8937c478bd9Sstevel@tonic-gate prevnode = currnode;
8947c478bd9Sstevel@tonic-gate currnode = currnode->subdir;
8957c478bd9Sstevel@tonic-gate
8967c478bd9Sstevel@tonic-gate if ((rc = get_dir_from_path(dirname, &subdir_child,
8977c478bd9Sstevel@tonic-gate sizeof (dirname))) != PARSE_OK)
8987c478bd9Sstevel@tonic-gate return (rc);
8997c478bd9Sstevel@tonic-gate if (dirname[0] != '\0')
9007c478bd9Sstevel@tonic-gate sprintf(traversed_path, "%s/%s",
9017c478bd9Sstevel@tonic-gate traversed_path, dirname);
9027c478bd9Sstevel@tonic-gate
9037c478bd9Sstevel@tonic-gate } else {
9047c478bd9Sstevel@tonic-gate /* try next leveldir */
9057c478bd9Sstevel@tonic-gate prevnode = currnode;
9067c478bd9Sstevel@tonic-gate currnode = currnode->leveldir;
9077c478bd9Sstevel@tonic-gate }
9087c478bd9Sstevel@tonic-gate }
9097c478bd9Sstevel@tonic-gate
9107c478bd9Sstevel@tonic-gate if (dirname[0] != '\0') {
9117c478bd9Sstevel@tonic-gate if (verbose)
9127c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
9137c478bd9Sstevel@tonic-gate "set_and_fake_mapent_mntlevel: subdir=%s error: map=%s",
9147c478bd9Sstevel@tonic-gate subdir, mapname);
9157c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
9167c478bd9Sstevel@tonic-gate }
9177c478bd9Sstevel@tonic-gate
9187c478bd9Sstevel@tonic-gate /*
9197c478bd9Sstevel@tonic-gate * see if level of root really points to a mapent and if
9207c478bd9Sstevel@tonic-gate * have access to that filessystem - call appropriate
9217c478bd9Sstevel@tonic-gate * routine to mark level 1 nodes, and build faked entries
9227c478bd9Sstevel@tonic-gate */
9237c478bd9Sstevel@tonic-gate if (prevnode->mapent != NULL && mount_access == TRUE) {
9247c478bd9Sstevel@tonic-gate if (trace > 3)
9257c478bd9Sstevel@tonic-gate trace_prt(1, " node mountpoint %s\t travpath=%s\n",
9267c478bd9Sstevel@tonic-gate prevnode->mapent->map_mntpnt, traversed_path);
9277c478bd9Sstevel@tonic-gate
9287c478bd9Sstevel@tonic-gate /*
9297c478bd9Sstevel@tonic-gate * Copy traversed path map_mntpnt to get rid of any extra
9307c478bd9Sstevel@tonic-gate * '/' the map entry may contain.
9317c478bd9Sstevel@tonic-gate */
9327c478bd9Sstevel@tonic-gate if (strlen(prevnode->mapent->map_mntpnt) <
9337c478bd9Sstevel@tonic-gate strlen(traversed_path)) { /* sanity check */
9347c478bd9Sstevel@tonic-gate if (verbose)
9357c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
9367c478bd9Sstevel@tonic-gate "set_and_fake_mapent_mntlevel: path=%s error",
9377c478bd9Sstevel@tonic-gate traversed_path);
9387c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
9397c478bd9Sstevel@tonic-gate }
9407c478bd9Sstevel@tonic-gate if (strcmp(prevnode->mapent->map_mntpnt, traversed_path) != 0)
9417c478bd9Sstevel@tonic-gate strcpy(prevnode->mapent->map_mntpnt, traversed_path);
9427c478bd9Sstevel@tonic-gate
9437c478bd9Sstevel@tonic-gate prevnode->mapent->map_mntlevel = 0; /* root level is 0 */
9447c478bd9Sstevel@tonic-gate if (currnode != NULL) {
9457c478bd9Sstevel@tonic-gate if ((rc = mark_level1_root(currnode,
9467c478bd9Sstevel@tonic-gate traversed_path)) != PARSE_OK)
9477c478bd9Sstevel@tonic-gate return (rc);
9487c478bd9Sstevel@tonic-gate }
9497c478bd9Sstevel@tonic-gate } else if (currnode != NULL) {
9507c478bd9Sstevel@tonic-gate if (trace > 3)
9517c478bd9Sstevel@tonic-gate trace_prt(1, " No rootnode, travpath=%s\n",
9527c478bd9Sstevel@tonic-gate traversed_path);
9537c478bd9Sstevel@tonic-gate if ((rc = mark_and_fake_level1_noroot(currnode,
9547c478bd9Sstevel@tonic-gate traversed_path, key, mapname, faked_mapents, isdirect,
9557c478bd9Sstevel@tonic-gate mapopts)) != PARSE_OK)
9567c478bd9Sstevel@tonic-gate return (rc);
9577c478bd9Sstevel@tonic-gate }
9587c478bd9Sstevel@tonic-gate
9597c478bd9Sstevel@tonic-gate if (trace > 3) {
9607c478bd9Sstevel@tonic-gate trace_prt(1, "\n\tset_and_fake_mapent_mntlevel\n");
9617c478bd9Sstevel@tonic-gate trace_hierarchy(rootnode, 0);
9627c478bd9Sstevel@tonic-gate }
9637c478bd9Sstevel@tonic-gate
9647c478bd9Sstevel@tonic-gate return (rc);
9657c478bd9Sstevel@tonic-gate }
9667c478bd9Sstevel@tonic-gate
9677c478bd9Sstevel@tonic-gate
9687c478bd9Sstevel@tonic-gate /*
9697c478bd9Sstevel@tonic-gate * mark_level1_root(hiernode *node, char *traversed_path)
9707c478bd9Sstevel@tonic-gate * marks nodes upto one level below the rootnode given by subdir
9717c478bd9Sstevel@tonic-gate * recursively. Called if rootnode points to a mapent.
9727c478bd9Sstevel@tonic-gate * In this routine, a level 1 node is considered to be the 1st existing
9737c478bd9Sstevel@tonic-gate * mapentry below the root node, so there's no faking involved.
9747c478bd9Sstevel@tonic-gate * Returns PARSE_OK or error value
9757c478bd9Sstevel@tonic-gate */
9767c478bd9Sstevel@tonic-gate static int
mark_level1_root(hiernode * node,char * traversed_path)9777c478bd9Sstevel@tonic-gate mark_level1_root(hiernode *node, char *traversed_path)
9787c478bd9Sstevel@tonic-gate {
9797c478bd9Sstevel@tonic-gate /* ensure we touch all leveldirs */
9807c478bd9Sstevel@tonic-gate while (node) {
9817c478bd9Sstevel@tonic-gate /*
9827c478bd9Sstevel@tonic-gate * mark node level as 1, if one exists - else walk down
9837c478bd9Sstevel@tonic-gate * subdirs until we find one.
9847c478bd9Sstevel@tonic-gate */
9857c478bd9Sstevel@tonic-gate if (node->mapent == NULL) {
9867c478bd9Sstevel@tonic-gate char w[MAXPATHLEN];
9877c478bd9Sstevel@tonic-gate
9887c478bd9Sstevel@tonic-gate if (node->subdir != NULL) {
9897c478bd9Sstevel@tonic-gate sprintf(w, "%s/%s", traversed_path,
9907c478bd9Sstevel@tonic-gate node->dirname);
9917c478bd9Sstevel@tonic-gate if (mark_level1_root(node->subdir, w)
9927c478bd9Sstevel@tonic-gate == PARSE_ERROR)
9937c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
9947c478bd9Sstevel@tonic-gate } else {
9957c478bd9Sstevel@tonic-gate if (verbose) {
9967c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
9977c478bd9Sstevel@tonic-gate "mark_level1_root: hierarchy error");
9987c478bd9Sstevel@tonic-gate }
9997c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
10007c478bd9Sstevel@tonic-gate }
10017c478bd9Sstevel@tonic-gate } else {
10027c478bd9Sstevel@tonic-gate char w[MAXPATHLEN];
10037c478bd9Sstevel@tonic-gate
10047c478bd9Sstevel@tonic-gate sprintf(w, "%s/%s", traversed_path, node->dirname);
10057c478bd9Sstevel@tonic-gate if (trace > 3)
10067c478bd9Sstevel@tonic-gate trace_prt(1, " node mntpnt %s\t travpath %s\n",
10077c478bd9Sstevel@tonic-gate node->mapent->map_mntpnt, w);
10087c478bd9Sstevel@tonic-gate
10097c478bd9Sstevel@tonic-gate /* replace mntpnt with travpath to clean extra '/' */
10107c478bd9Sstevel@tonic-gate if (strlen(node->mapent->map_mntpnt) < strlen(w)) {
10117c478bd9Sstevel@tonic-gate if (verbose) {
10127c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
10137c478bd9Sstevel@tonic-gate "mark_level1_root: path=%s error",
10147c478bd9Sstevel@tonic-gate traversed_path);
10157c478bd9Sstevel@tonic-gate }
10167c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
10177c478bd9Sstevel@tonic-gate }
10187c478bd9Sstevel@tonic-gate if (strcmp(node->mapent->map_mntpnt, w) != 0)
10197c478bd9Sstevel@tonic-gate strcpy(node->mapent->map_mntpnt, w);
10207c478bd9Sstevel@tonic-gate node->mapent->map_mntlevel = 1;
10217c478bd9Sstevel@tonic-gate }
10227c478bd9Sstevel@tonic-gate node = node->leveldir;
10237c478bd9Sstevel@tonic-gate }
10247c478bd9Sstevel@tonic-gate return (PARSE_OK);
10257c478bd9Sstevel@tonic-gate }
10267c478bd9Sstevel@tonic-gate
10277c478bd9Sstevel@tonic-gate /*
10287c478bd9Sstevel@tonic-gate * mark_and_fake_level1_noroot(hiernode *node, char *traversed_path,
10297c478bd9Sstevel@tonic-gate * char *key,char *mapname, struct mapent **faked_mapents,
10307c478bd9Sstevel@tonic-gate * uint_t isdirect, char *mapopts)
10317c478bd9Sstevel@tonic-gate * Called if the root of the hierarchy does not point to a mapent. marks nodes
10327c478bd9Sstevel@tonic-gate * upto one physical level below the rootnode given by subdir. checks if
10337c478bd9Sstevel@tonic-gate * there's a real mapentry. If not, it builds a faked one (autonode) at that
10347c478bd9Sstevel@tonic-gate * point. The faked autonode is direct, with the map being the same as the
10357c478bd9Sstevel@tonic-gate * original one from which the call originated. Options are same as that of
10367c478bd9Sstevel@tonic-gate * the map and assigned in automount_opts(). Returns PARSE_OK or error value.
10377c478bd9Sstevel@tonic-gate */
10387c478bd9Sstevel@tonic-gate static int
mark_and_fake_level1_noroot(hiernode * node,char * traversed_path,char * key,char * mapname,struct mapent ** faked_mapents,uint_t isdirect,char * mapopts)10397c478bd9Sstevel@tonic-gate mark_and_fake_level1_noroot(hiernode *node, char *traversed_path,
10407c478bd9Sstevel@tonic-gate char *key, char *mapname, struct mapent **faked_mapents,
10417c478bd9Sstevel@tonic-gate uint_t isdirect, char *mapopts)
10427c478bd9Sstevel@tonic-gate {
10437c478bd9Sstevel@tonic-gate struct mapent *me;
10447c478bd9Sstevel@tonic-gate int rc = 0;
10457c478bd9Sstevel@tonic-gate char faked_map_mntpnt[MAXPATHLEN];
10467c478bd9Sstevel@tonic-gate char w1[MAXPATHLEN];
10477c478bd9Sstevel@tonic-gate char w[MAXPATHLEN];
10487c478bd9Sstevel@tonic-gate
10497c478bd9Sstevel@tonic-gate while (node != NULL) {
10507c478bd9Sstevel@tonic-gate if (node->mapent != NULL) {
10517c478bd9Sstevel@tonic-gate /*
10527c478bd9Sstevel@tonic-gate * existing mapentry at level 1 - copy travpath to
10537c478bd9Sstevel@tonic-gate * get rid of extra '/' in mntpnt
10547c478bd9Sstevel@tonic-gate */
10557c478bd9Sstevel@tonic-gate sprintf(w, "%s/%s", traversed_path, node->dirname);
10567c478bd9Sstevel@tonic-gate if (trace > 3)
10577c478bd9Sstevel@tonic-gate trace_prt(1, " node mntpnt=%s\t travpath=%s\n",
10587c478bd9Sstevel@tonic-gate node->mapent->map_mntpnt, w);
10597c478bd9Sstevel@tonic-gate if (strlen(node->mapent->map_mntpnt) < strlen(w)) {
10607c478bd9Sstevel@tonic-gate /* sanity check */
10617c478bd9Sstevel@tonic-gate if (verbose)
10627c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
10637c478bd9Sstevel@tonic-gate "mark_fake_level1_noroot:path=%s error",
10647c478bd9Sstevel@tonic-gate traversed_path);
10657c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
10667c478bd9Sstevel@tonic-gate }
10677c478bd9Sstevel@tonic-gate if (strcmp(node->mapent->map_mntpnt, w) != 0)
10687c478bd9Sstevel@tonic-gate strcpy(node->mapent->map_mntpnt, w);
10697c478bd9Sstevel@tonic-gate node->mapent->map_mntlevel = 1;
10707c478bd9Sstevel@tonic-gate } else {
10717c478bd9Sstevel@tonic-gate /*
10727c478bd9Sstevel@tonic-gate * build the faked autonode
10737c478bd9Sstevel@tonic-gate */
10747c478bd9Sstevel@tonic-gate if ((me = (struct mapent *)malloc(sizeof (*me)))
10757c478bd9Sstevel@tonic-gate == NULL) {
10767c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
10777c478bd9Sstevel@tonic-gate "mark_and_fake_level1_noroot: out of memory");
10787c478bd9Sstevel@tonic-gate return (ENOMEM);
10797c478bd9Sstevel@tonic-gate }
10807c478bd9Sstevel@tonic-gate (void) memset((char *)me, 0, sizeof (*me));
10817c478bd9Sstevel@tonic-gate
10827c478bd9Sstevel@tonic-gate if ((me->map_fs = (struct mapfs *)
10837c478bd9Sstevel@tonic-gate malloc(sizeof (struct mapfs))) == NULL)
10847c478bd9Sstevel@tonic-gate return (ENOMEM);
10857c478bd9Sstevel@tonic-gate (void) memset(me->map_fs, 0, sizeof (struct mapfs));
10867c478bd9Sstevel@tonic-gate
10877c478bd9Sstevel@tonic-gate if (isdirect) {
10887c478bd9Sstevel@tonic-gate *w1 = '\0';
10897c478bd9Sstevel@tonic-gate } else {
10907c478bd9Sstevel@tonic-gate strcpy(w1, "/");
10917c478bd9Sstevel@tonic-gate strcat(w1, key);
10927c478bd9Sstevel@tonic-gate }
10937c478bd9Sstevel@tonic-gate me->map_root = strdup(w1);
10947c478bd9Sstevel@tonic-gate
10957c478bd9Sstevel@tonic-gate sprintf(faked_map_mntpnt, "%s/%s", traversed_path,
10967c478bd9Sstevel@tonic-gate node->dirname);
10977c478bd9Sstevel@tonic-gate me->map_mntpnt = strdup(faked_map_mntpnt);
10987c478bd9Sstevel@tonic-gate me->map_fstype = strdup(MNTTYPE_AUTOFS);
10997c478bd9Sstevel@tonic-gate me->map_mounter = strdup(MNTTYPE_AUTOFS);
11007c478bd9Sstevel@tonic-gate
11017c478bd9Sstevel@tonic-gate /* set options */
11027c478bd9Sstevel@tonic-gate if ((rc = automount_opts(&me->map_mntopts, mapopts))
11037c478bd9Sstevel@tonic-gate != PARSE_OK)
11047c478bd9Sstevel@tonic-gate return (rc);
11057c478bd9Sstevel@tonic-gate me->map_fs->mfs_dir = strdup(mapname);
11067c478bd9Sstevel@tonic-gate me->map_mntlevel = 1;
11077c478bd9Sstevel@tonic-gate me->map_modified = FALSE;
11087c478bd9Sstevel@tonic-gate me->map_faked = TRUE; /* mark as faked */
11097c478bd9Sstevel@tonic-gate if (me->map_root == NULL ||
11107c478bd9Sstevel@tonic-gate me->map_mntpnt == NULL ||
11117c478bd9Sstevel@tonic-gate me->map_fstype == NULL ||
11127c478bd9Sstevel@tonic-gate me->map_mounter == NULL ||
11137c478bd9Sstevel@tonic-gate me->map_mntopts == NULL ||
11147c478bd9Sstevel@tonic-gate me->map_fs->mfs_dir == NULL) {
11157c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
11167c478bd9Sstevel@tonic-gate "mark_and_fake_level1_noroot: out of memory");
11177c478bd9Sstevel@tonic-gate free_mapent(*faked_mapents);
11187c478bd9Sstevel@tonic-gate return (ENOMEM);
11197c478bd9Sstevel@tonic-gate }
11207c478bd9Sstevel@tonic-gate
11217c478bd9Sstevel@tonic-gate if (*faked_mapents == NULL)
11227c478bd9Sstevel@tonic-gate *faked_mapents = me;
11237c478bd9Sstevel@tonic-gate else { /* attach to the head */
11247c478bd9Sstevel@tonic-gate me->map_next = *faked_mapents;
11257c478bd9Sstevel@tonic-gate *faked_mapents = me;
11267c478bd9Sstevel@tonic-gate }
11277c478bd9Sstevel@tonic-gate node->mapent = me;
11287c478bd9Sstevel@tonic-gate }
11297c478bd9Sstevel@tonic-gate node = node->leveldir;
11307c478bd9Sstevel@tonic-gate }
11317c478bd9Sstevel@tonic-gate return (rc);
11327c478bd9Sstevel@tonic-gate }
11337c478bd9Sstevel@tonic-gate
11347c478bd9Sstevel@tonic-gate /*
11357c478bd9Sstevel@tonic-gate * convert_mapent_to_automount(struct mapent *me, char *mapname,
11367c478bd9Sstevel@tonic-gate * char *mapopts)
11377c478bd9Sstevel@tonic-gate * change the mapentry me to an automount - free fields first and NULL them
11387c478bd9Sstevel@tonic-gate * to avoid freeing again, while freeing the mapentry at a later stage.
11397c478bd9Sstevel@tonic-gate * Could have avoided freeing entries here as we don't really look at them.
11407c478bd9Sstevel@tonic-gate * Give the converted mapent entry the options that came with the map using
11417c478bd9Sstevel@tonic-gate * automount_opts(). Returns PARSE_OK or appropriate error value.
11427c478bd9Sstevel@tonic-gate */
11437c478bd9Sstevel@tonic-gate static int
convert_mapent_to_automount(struct mapent * me,char * mapname,char * mapopts)11447c478bd9Sstevel@tonic-gate convert_mapent_to_automount(struct mapent *me, char *mapname,
11457c478bd9Sstevel@tonic-gate char *mapopts)
11467c478bd9Sstevel@tonic-gate {
11477c478bd9Sstevel@tonic-gate struct mapfs *mfs = me->map_fs; /* assumes it exists */
11487c478bd9Sstevel@tonic-gate int rc = PARSE_OK;
11497c478bd9Sstevel@tonic-gate
11507c478bd9Sstevel@tonic-gate /* free relevant entries */
11517c478bd9Sstevel@tonic-gate if (mfs->mfs_host) {
11527c478bd9Sstevel@tonic-gate free(mfs->mfs_host);
11537c478bd9Sstevel@tonic-gate mfs->mfs_host = NULL;
11547c478bd9Sstevel@tonic-gate }
11557c478bd9Sstevel@tonic-gate while (me->map_fs->mfs_next != NULL) {
11567c478bd9Sstevel@tonic-gate mfs = me->map_fs->mfs_next;
11577c478bd9Sstevel@tonic-gate if (mfs->mfs_host)
11587c478bd9Sstevel@tonic-gate free(mfs->mfs_host);
11597c478bd9Sstevel@tonic-gate if (mfs->mfs_dir)
11607c478bd9Sstevel@tonic-gate free(mfs->mfs_dir);
11617c478bd9Sstevel@tonic-gate me->map_fs->mfs_next = mfs->mfs_next; /* nulls eventually */
11627c478bd9Sstevel@tonic-gate free((void*)mfs);
11637c478bd9Sstevel@tonic-gate }
11647c478bd9Sstevel@tonic-gate
11657c478bd9Sstevel@tonic-gate /* replace relevant entries */
11667c478bd9Sstevel@tonic-gate if (me->map_fstype)
11677c478bd9Sstevel@tonic-gate free(me->map_fstype);
11687c478bd9Sstevel@tonic-gate if ((me->map_fstype = strdup(MNTTYPE_AUTOFS)) == NULL)
11697c478bd9Sstevel@tonic-gate goto alloc_failed;
11707c478bd9Sstevel@tonic-gate
11717c478bd9Sstevel@tonic-gate if (me->map_mounter)
11727c478bd9Sstevel@tonic-gate free(me->map_mounter);
11737c478bd9Sstevel@tonic-gate if ((me->map_mounter = strdup(me->map_fstype)) == NULL)
11747c478bd9Sstevel@tonic-gate goto alloc_failed;
11757c478bd9Sstevel@tonic-gate
11767c478bd9Sstevel@tonic-gate if (me->map_fs->mfs_dir)
11777c478bd9Sstevel@tonic-gate free(me->map_fs->mfs_dir);
11787c478bd9Sstevel@tonic-gate if ((me->map_fs->mfs_dir = strdup(mapname)) == NULL)
11797c478bd9Sstevel@tonic-gate goto alloc_failed;
11807c478bd9Sstevel@tonic-gate
11817c478bd9Sstevel@tonic-gate /* set options */
11827c478bd9Sstevel@tonic-gate if (me->map_mntopts)
11837c478bd9Sstevel@tonic-gate free(me->map_mntopts);
11847c478bd9Sstevel@tonic-gate if ((rc = automount_opts(&me->map_mntopts, mapopts)) != PARSE_OK)
11857c478bd9Sstevel@tonic-gate return (rc);
11867c478bd9Sstevel@tonic-gate
11877c478bd9Sstevel@tonic-gate /* mucked with this entry, set the map_modified field to TRUE */
11887c478bd9Sstevel@tonic-gate me->map_modified = TRUE;
11897c478bd9Sstevel@tonic-gate
11907c478bd9Sstevel@tonic-gate return (rc);
11917c478bd9Sstevel@tonic-gate
11927c478bd9Sstevel@tonic-gate alloc_failed:
11937c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
11947c478bd9Sstevel@tonic-gate "convert_mapent_to_automount: Memory allocation failed");
11957c478bd9Sstevel@tonic-gate return (ENOMEM);
11967c478bd9Sstevel@tonic-gate }
11977c478bd9Sstevel@tonic-gate
11987c478bd9Sstevel@tonic-gate /*
11997c478bd9Sstevel@tonic-gate * automount_opts(char **map_mntopts, char *mapopts)
12007c478bd9Sstevel@tonic-gate * modifies automount opts - gets rid of all "indirect" and "direct" strings
12017c478bd9Sstevel@tonic-gate * if they exist, and then adds a direct string to force a direct automount.
12027c478bd9Sstevel@tonic-gate * Rest of the mapopts stay intact. Returns PARSE_OK or appropriate error.
12037c478bd9Sstevel@tonic-gate */
12047c478bd9Sstevel@tonic-gate static int
automount_opts(char ** map_mntopts,char * mapopts)12057c478bd9Sstevel@tonic-gate automount_opts(char **map_mntopts, char *mapopts)
12067c478bd9Sstevel@tonic-gate {
12077c478bd9Sstevel@tonic-gate char *opts;
12087c478bd9Sstevel@tonic-gate char *opt;
12097c478bd9Sstevel@tonic-gate int len;
12107c478bd9Sstevel@tonic-gate char *placeholder;
12117c478bd9Sstevel@tonic-gate char buf[AUTOFS_MAXOPTSLEN];
12127c478bd9Sstevel@tonic-gate
12137c478bd9Sstevel@tonic-gate char *addopt = "direct";
12147c478bd9Sstevel@tonic-gate
12157c478bd9Sstevel@tonic-gate len = strlen(mapopts)+ strlen(addopt)+2; /* +2 for ",", '\0' */
12167c478bd9Sstevel@tonic-gate if (len > AUTOFS_MAXOPTSLEN) {
12177c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
12187c478bd9Sstevel@tonic-gate "option string %s too long (max=%d)", mapopts,
12197c478bd9Sstevel@tonic-gate AUTOFS_MAXOPTSLEN-8);
12207c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
12217c478bd9Sstevel@tonic-gate }
12227c478bd9Sstevel@tonic-gate
12237c478bd9Sstevel@tonic-gate if (((*map_mntopts) = ((char *)malloc(len))) == NULL) {
12247c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "automount_opts: Memory allocation failed");
12257c478bd9Sstevel@tonic-gate return (ENOMEM);
12267c478bd9Sstevel@tonic-gate }
12277c478bd9Sstevel@tonic-gate memset(*map_mntopts, 0, len);
12287c478bd9Sstevel@tonic-gate
12297c478bd9Sstevel@tonic-gate strcpy(buf, mapopts);
12307c478bd9Sstevel@tonic-gate opts = buf;
12317c478bd9Sstevel@tonic-gate while ((opt = strtok_r(opts, ",", &placeholder)) != NULL) {
12327c478bd9Sstevel@tonic-gate opts = NULL;
12337c478bd9Sstevel@tonic-gate
12347c478bd9Sstevel@tonic-gate /* remove trailing and leading spaces */
12357c478bd9Sstevel@tonic-gate while (isspace(*opt))
12367c478bd9Sstevel@tonic-gate opt++;
12377c478bd9Sstevel@tonic-gate len = strlen(opt)-1;
12387c478bd9Sstevel@tonic-gate while (isspace(opt[len]))
12397c478bd9Sstevel@tonic-gate opt[len--] = '\0';
12407c478bd9Sstevel@tonic-gate
12417c478bd9Sstevel@tonic-gate /*
12427c478bd9Sstevel@tonic-gate * if direct or indirect found, get rid of it, else put it
12437c478bd9Sstevel@tonic-gate * back
12447c478bd9Sstevel@tonic-gate */
12457c478bd9Sstevel@tonic-gate if ((strcmp(opt, "indirect") == 0) ||
12467c478bd9Sstevel@tonic-gate (strcmp(opt, "direct") == 0))
12477c478bd9Sstevel@tonic-gate continue;
12487c478bd9Sstevel@tonic-gate if (*map_mntopts[0] != '\0')
12497c478bd9Sstevel@tonic-gate strcat(*map_mntopts, ",");
12507c478bd9Sstevel@tonic-gate strcat(*map_mntopts, opt);
12517c478bd9Sstevel@tonic-gate }
12527c478bd9Sstevel@tonic-gate
12537c478bd9Sstevel@tonic-gate /* add the direct string at the end */
12547c478bd9Sstevel@tonic-gate if (*map_mntopts[0] != '\0')
12557c478bd9Sstevel@tonic-gate strcat(*map_mntopts, ",");
12567c478bd9Sstevel@tonic-gate strcat(*map_mntopts, addopt);
12577c478bd9Sstevel@tonic-gate
12587c478bd9Sstevel@tonic-gate return (PARSE_OK);
12597c478bd9Sstevel@tonic-gate }
12607c478bd9Sstevel@tonic-gate
12617c478bd9Sstevel@tonic-gate /*
12627c478bd9Sstevel@tonic-gate * parse_fsinfo(char *mapname, struct mapent *mapents)
12637c478bd9Sstevel@tonic-gate * parses the filesystem information stored in me->map_fsw and me->map_fswq
12647c478bd9Sstevel@tonic-gate * and calls appropriate filesystem parser.
12657c478bd9Sstevel@tonic-gate * Returns PARSE_OK or an appropriate error value.
12667c478bd9Sstevel@tonic-gate */
12677c478bd9Sstevel@tonic-gate static int
parse_fsinfo(char * mapname,struct mapent * mapents)12687c478bd9Sstevel@tonic-gate parse_fsinfo(char *mapname, struct mapent *mapents)
12697c478bd9Sstevel@tonic-gate {
12707c478bd9Sstevel@tonic-gate struct mapent *me = mapents;
12717c478bd9Sstevel@tonic-gate char *bufp;
12727c478bd9Sstevel@tonic-gate char *bufq;
12737c478bd9Sstevel@tonic-gate int wordsz = MAXPATHLEN;
12747c478bd9Sstevel@tonic-gate int err = 0;
12757c478bd9Sstevel@tonic-gate
12767c478bd9Sstevel@tonic-gate while (me != NULL) {
12777c478bd9Sstevel@tonic-gate bufp = "";
12787c478bd9Sstevel@tonic-gate bufq = "";
12797c478bd9Sstevel@tonic-gate if (strcmp(me->map_fstype, MNTTYPE_NFS) == 0) {
12807c478bd9Sstevel@tonic-gate err = parse_nfs(mapname, me, me->map_fsw,
12817c478bd9Sstevel@tonic-gate me->map_fswq, &bufp, &bufq, wordsz);
12827c478bd9Sstevel@tonic-gate } else {
12837c478bd9Sstevel@tonic-gate err = parse_special(me, me->map_fsw, me->map_fswq,
12847c478bd9Sstevel@tonic-gate &bufp, &bufq, wordsz);
12857c478bd9Sstevel@tonic-gate }
12867c478bd9Sstevel@tonic-gate
12877c478bd9Sstevel@tonic-gate if (err != PARSE_OK || *me->map_fsw != '\0' ||
12887c478bd9Sstevel@tonic-gate *me->map_fswq != '\0') {
12897c478bd9Sstevel@tonic-gate /* sanity check */
12907c478bd9Sstevel@tonic-gate if (verbose)
12917c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
12927c478bd9Sstevel@tonic-gate "parse_fsinfo: mount location error %s",
12937c478bd9Sstevel@tonic-gate me->map_fsw);
12947c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
12957c478bd9Sstevel@tonic-gate }
12967c478bd9Sstevel@tonic-gate
12977c478bd9Sstevel@tonic-gate me = me->map_next;
12987c478bd9Sstevel@tonic-gate }
12997c478bd9Sstevel@tonic-gate
13007c478bd9Sstevel@tonic-gate if (trace > 3) {
13017c478bd9Sstevel@tonic-gate trace_mapents("parse_fsinfo:", mapents);
13027c478bd9Sstevel@tonic-gate }
13037c478bd9Sstevel@tonic-gate
13047c478bd9Sstevel@tonic-gate return (PARSE_OK);
13057c478bd9Sstevel@tonic-gate }
13067c478bd9Sstevel@tonic-gate
13077c478bd9Sstevel@tonic-gate /*
13087c478bd9Sstevel@tonic-gate * This function parses the map entry for a nfs type file system
13097c478bd9Sstevel@tonic-gate * The input is the string lp (and lq) which can be one of the
13107c478bd9Sstevel@tonic-gate * following forms:
13117c478bd9Sstevel@tonic-gate * a) host[(penalty)][,host[(penalty)]]... :/directory
13127c478bd9Sstevel@tonic-gate * b) host[(penalty)]:/directory[ host[(penalty)]:/directory]...
13137c478bd9Sstevel@tonic-gate * This routine constructs a mapfs link-list for each of
13147c478bd9Sstevel@tonic-gate * the hosts and the corresponding file system. The list
13157c478bd9Sstevel@tonic-gate * is then attatched to the mapent struct passed in.
13167c478bd9Sstevel@tonic-gate */
131711606941Sjwahlig int
parse_nfs(mapname,me,fsw,fswq,lp,lq,wsize)13187c478bd9Sstevel@tonic-gate parse_nfs(mapname, me, fsw, fswq, lp, lq, wsize)
13197c478bd9Sstevel@tonic-gate struct mapent *me;
13207c478bd9Sstevel@tonic-gate char *mapname, *fsw, *fswq, **lp, **lq;
13217c478bd9Sstevel@tonic-gate int wsize;
13227c478bd9Sstevel@tonic-gate {
13237c478bd9Sstevel@tonic-gate struct mapfs *mfs, **mfsp;
13247c478bd9Sstevel@tonic-gate char *wlp, *wlq;
13257c478bd9Sstevel@tonic-gate char *hl, hostlist[1024], *hlq, hostlistq[1024];
13267c478bd9Sstevel@tonic-gate char hostname_and_penalty[MXHOSTNAMELEN+5];
13277c478bd9Sstevel@tonic-gate char *hn, *hnq, hostname[MXHOSTNAMELEN+1];
13287c478bd9Sstevel@tonic-gate char dirname[MAXPATHLEN+1], subdir[MAXPATHLEN+1];
13297c478bd9Sstevel@tonic-gate char qbuff[MAXPATHLEN+1], qbuff1[MAXPATHLEN+1];
13307c478bd9Sstevel@tonic-gate char pbuff[10], pbuffq[10];
13317c478bd9Sstevel@tonic-gate int penalty;
13327c478bd9Sstevel@tonic-gate char w[MAXPATHLEN];
13337c478bd9Sstevel@tonic-gate char wq[MAXPATHLEN];
13347c478bd9Sstevel@tonic-gate int host_cnt;
13357c478bd9Sstevel@tonic-gate
13367c478bd9Sstevel@tonic-gate mfsp = &me->map_fs;
13377c478bd9Sstevel@tonic-gate *mfsp = NULL;
13387c478bd9Sstevel@tonic-gate
13397c478bd9Sstevel@tonic-gate /*
13407c478bd9Sstevel@tonic-gate * there may be more than one entry in the map list. Get the
13417c478bd9Sstevel@tonic-gate * first one. Use temps to handle the word information and
13427c478bd9Sstevel@tonic-gate * copy back into fsw and fswq fields when done.
13437c478bd9Sstevel@tonic-gate */
13447c478bd9Sstevel@tonic-gate *lp = fsw;
13457c478bd9Sstevel@tonic-gate *lq = fswq;
13467c478bd9Sstevel@tonic-gate if (getword(w, wq, lp, lq, ' ', wsize) == -1)
13477c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
13487c478bd9Sstevel@tonic-gate while (*w && *w != '/') {
13497c478bd9Sstevel@tonic-gate bool_t maybe_url;
13507c478bd9Sstevel@tonic-gate
13517c478bd9Sstevel@tonic-gate maybe_url = TRUE;
13527c478bd9Sstevel@tonic-gate
13537c478bd9Sstevel@tonic-gate wlp = w; wlq = wq;
13547c478bd9Sstevel@tonic-gate if (getword(hostlist, hostlistq, &wlp, &wlq, ':',
13557c478bd9Sstevel@tonic-gate sizeof (hostlist)) == -1)
13567c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
13577c478bd9Sstevel@tonic-gate if (!*hostlist)
13587c478bd9Sstevel@tonic-gate goto bad_entry;
13597c478bd9Sstevel@tonic-gate
13607c478bd9Sstevel@tonic-gate if (strcmp(hostlist, "nfs") != 0)
13617c478bd9Sstevel@tonic-gate maybe_url = FALSE;
13627c478bd9Sstevel@tonic-gate
13637c478bd9Sstevel@tonic-gate if (getword(dirname, qbuff, &wlp, &wlq, ':',
13647c478bd9Sstevel@tonic-gate sizeof (dirname)) == -1)
13657c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
13667c478bd9Sstevel@tonic-gate if (*dirname == '\0')
13677c478bd9Sstevel@tonic-gate goto bad_entry;
13687c478bd9Sstevel@tonic-gate
13697c478bd9Sstevel@tonic-gate if (maybe_url == TRUE && strncmp(dirname, "//", 2) != 0)
13707c478bd9Sstevel@tonic-gate maybe_url = FALSE;
13717c478bd9Sstevel@tonic-gate
13727c478bd9Sstevel@tonic-gate /*
13737c478bd9Sstevel@tonic-gate * See the next block comment ("Once upon a time ...") to
13747c478bd9Sstevel@tonic-gate * understand this. It turns the deprecated concept
13757c478bd9Sstevel@tonic-gate * of "subdir mounts" produced some useful code for handling
13767c478bd9Sstevel@tonic-gate * the possibility of a ":port#" in the URL.
13777c478bd9Sstevel@tonic-gate */
13787c478bd9Sstevel@tonic-gate if (maybe_url == FALSE)
13797c478bd9Sstevel@tonic-gate *subdir = '/';
13807c478bd9Sstevel@tonic-gate else
13817c478bd9Sstevel@tonic-gate *subdir = ':';
13827c478bd9Sstevel@tonic-gate
13837c478bd9Sstevel@tonic-gate *qbuff = ' ';
13847c478bd9Sstevel@tonic-gate
13857c478bd9Sstevel@tonic-gate /*
13867c478bd9Sstevel@tonic-gate * Once upon time, before autofs, there was support for
13877c478bd9Sstevel@tonic-gate * "subdir mounts". The idea was to "economize" the
13887c478bd9Sstevel@tonic-gate * number of mounts, so if you had a number of entries
13897c478bd9Sstevel@tonic-gate * all referring to a common subdirectory, e.g.
13907c478bd9Sstevel@tonic-gate *
13917c478bd9Sstevel@tonic-gate * carol seasons:/export/home11/carol
13927c478bd9Sstevel@tonic-gate * ted seasons:/export/home11/ted
13937c478bd9Sstevel@tonic-gate * alice seasons:/export/home11/alice
13947c478bd9Sstevel@tonic-gate *
13957c478bd9Sstevel@tonic-gate * then you could tell the automounter to mount a
13967c478bd9Sstevel@tonic-gate * common mountpoint which was delimited by the second
13977c478bd9Sstevel@tonic-gate * colon:
13987c478bd9Sstevel@tonic-gate *
13997c478bd9Sstevel@tonic-gate * carol seasons:/export/home11:carol
14007c478bd9Sstevel@tonic-gate * ted seasons:/export/home11:ted
14017c478bd9Sstevel@tonic-gate * alice seasons:/export/home11:alice
14027c478bd9Sstevel@tonic-gate *
14037c478bd9Sstevel@tonic-gate * The automounter would mount seasons:/export/home11
14047c478bd9Sstevel@tonic-gate * then for any other map entry that referenced the same
14057c478bd9Sstevel@tonic-gate * directory it would build a symbolic link that
14067c478bd9Sstevel@tonic-gate * appended the remainder of the path after the second
14077c478bd9Sstevel@tonic-gate * colon, i.e. once the common subdir was mounted, then
14087c478bd9Sstevel@tonic-gate * other directories could be accessed just by link
14097c478bd9Sstevel@tonic-gate * building - no further mounts required.
14107c478bd9Sstevel@tonic-gate *
14117c478bd9Sstevel@tonic-gate * In theory the "mount saving" idea sounded good. In
14127c478bd9Sstevel@tonic-gate * practice the saving didn't amount to much and the
14137c478bd9Sstevel@tonic-gate * symbolic links confused people because the common
14147c478bd9Sstevel@tonic-gate * mountpoint had to have a pseudonym.
14157c478bd9Sstevel@tonic-gate *
14167c478bd9Sstevel@tonic-gate * To remain backward compatible with the existing
14177c478bd9Sstevel@tonic-gate * maps, we interpret a second colon as a slash.
14187c478bd9Sstevel@tonic-gate */
14197c478bd9Sstevel@tonic-gate if (getword(subdir+1, qbuff+1, &wlp, &wlq, ':',
14207c478bd9Sstevel@tonic-gate sizeof (subdir)) == -1)
14217c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
14227c478bd9Sstevel@tonic-gate
14237c478bd9Sstevel@tonic-gate if (*(subdir+1))
14247c478bd9Sstevel@tonic-gate (void) strcat(dirname, subdir);
14257c478bd9Sstevel@tonic-gate
14267c478bd9Sstevel@tonic-gate hl = hostlist; hlq = hostlistq;
14277c478bd9Sstevel@tonic-gate
14287c478bd9Sstevel@tonic-gate host_cnt = 0;
14297c478bd9Sstevel@tonic-gate for (;;) {
14307c478bd9Sstevel@tonic-gate
14317c478bd9Sstevel@tonic-gate if (getword(hostname_and_penalty, qbuff, &hl, &hlq, ',',
14327c478bd9Sstevel@tonic-gate sizeof (hostname_and_penalty)) == -1)
14337c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
14347c478bd9Sstevel@tonic-gate if (!*hostname_and_penalty)
14357c478bd9Sstevel@tonic-gate break;
14367c478bd9Sstevel@tonic-gate
14377c478bd9Sstevel@tonic-gate host_cnt++;
14387c478bd9Sstevel@tonic-gate if (host_cnt > 1)
14397c478bd9Sstevel@tonic-gate maybe_url = FALSE;
14407c478bd9Sstevel@tonic-gate
14417c478bd9Sstevel@tonic-gate hn = hostname_and_penalty;
14427c478bd9Sstevel@tonic-gate hnq = qbuff;
14437c478bd9Sstevel@tonic-gate if (getword(hostname, qbuff1, &hn, &hnq, '(',
14447c478bd9Sstevel@tonic-gate sizeof (hostname)) == -1)
14457c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
14467c478bd9Sstevel@tonic-gate if (hostname[0] == '\0')
14477c478bd9Sstevel@tonic-gate goto bad_entry;
14487c478bd9Sstevel@tonic-gate
14497c478bd9Sstevel@tonic-gate if (strcmp(hostname, hostname_and_penalty) == 0) {
14507c478bd9Sstevel@tonic-gate penalty = 0;
14517c478bd9Sstevel@tonic-gate } else {
14527c478bd9Sstevel@tonic-gate maybe_url = FALSE;
14537c478bd9Sstevel@tonic-gate hn++; hnq++;
14547c478bd9Sstevel@tonic-gate if (getword(pbuff, pbuffq, &hn, &hnq, ')',
14557c478bd9Sstevel@tonic-gate sizeof (pbuff)) == -1)
14567c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
14577c478bd9Sstevel@tonic-gate if (!*pbuff)
14587c478bd9Sstevel@tonic-gate penalty = 0;
14597c478bd9Sstevel@tonic-gate else
14607c478bd9Sstevel@tonic-gate penalty = atoi(pbuff);
14617c478bd9Sstevel@tonic-gate }
14627c478bd9Sstevel@tonic-gate mfs = (struct mapfs *)malloc(sizeof (*mfs));
14637c478bd9Sstevel@tonic-gate if (mfs == NULL) {
14647c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
14657c478bd9Sstevel@tonic-gate "parse_nfs: Memory allocation failed");
14667c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
14677c478bd9Sstevel@tonic-gate }
14687c478bd9Sstevel@tonic-gate (void) memset(mfs, 0, sizeof (*mfs));
14697c478bd9Sstevel@tonic-gate *mfsp = mfs;
14707c478bd9Sstevel@tonic-gate mfsp = &mfs->mfs_next;
14717c478bd9Sstevel@tonic-gate
14727c478bd9Sstevel@tonic-gate if (maybe_url == TRUE) {
14737c478bd9Sstevel@tonic-gate char *host;
14747c478bd9Sstevel@tonic-gate char *path;
14757c478bd9Sstevel@tonic-gate char *sport;
14767c478bd9Sstevel@tonic-gate
14777c478bd9Sstevel@tonic-gate host = dirname+2;
14787c478bd9Sstevel@tonic-gate path = strchr(host, '/');
14797c478bd9Sstevel@tonic-gate if (path == NULL) {
14807c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
14817c478bd9Sstevel@tonic-gate "parse_nfs: illegal nfs url syntax: %s",
14827c478bd9Sstevel@tonic-gate host);
14837c478bd9Sstevel@tonic-gate
14847c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
14857c478bd9Sstevel@tonic-gate }
14867c478bd9Sstevel@tonic-gate *path = '\0';
14877c478bd9Sstevel@tonic-gate sport = strchr(host, ':');
14887c478bd9Sstevel@tonic-gate
14897c478bd9Sstevel@tonic-gate if (sport != NULL && sport < path) {
14907c478bd9Sstevel@tonic-gate *sport = '\0';
14917c478bd9Sstevel@tonic-gate mfs->mfs_port = atoi(sport+1);
14927c478bd9Sstevel@tonic-gate
14937c478bd9Sstevel@tonic-gate if (mfs->mfs_port > USHRT_MAX) {
14947c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
14957c478bd9Sstevel@tonic-gate "parse_nfs: invalid "
14967c478bd9Sstevel@tonic-gate "port number (%d) in "
14977c478bd9Sstevel@tonic-gate "NFS URL",
14987c478bd9Sstevel@tonic-gate mfs->mfs_port);
14997c478bd9Sstevel@tonic-gate
15007c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
15017c478bd9Sstevel@tonic-gate }
15027c478bd9Sstevel@tonic-gate
15037c478bd9Sstevel@tonic-gate }
15047c478bd9Sstevel@tonic-gate
15057c478bd9Sstevel@tonic-gate path++;
15067c478bd9Sstevel@tonic-gate if (*path == '\0')
15077c478bd9Sstevel@tonic-gate path = ".";
15087c478bd9Sstevel@tonic-gate
15097c478bd9Sstevel@tonic-gate mfs->mfs_flags |= MFS_URL;
15107c478bd9Sstevel@tonic-gate
15117c478bd9Sstevel@tonic-gate mfs->mfs_host = strdup(host);
15127c478bd9Sstevel@tonic-gate mfs->mfs_dir = strdup(path);
15137c478bd9Sstevel@tonic-gate } else {
15147c478bd9Sstevel@tonic-gate mfs->mfs_host = strdup(hostname);
15157c478bd9Sstevel@tonic-gate mfs->mfs_dir = strdup(dirname);
15167c478bd9Sstevel@tonic-gate }
15177c478bd9Sstevel@tonic-gate
15187c478bd9Sstevel@tonic-gate mfs->mfs_penalty = penalty;
15197c478bd9Sstevel@tonic-gate if (mfs->mfs_host == NULL || mfs->mfs_dir == NULL) {
15207c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
15217c478bd9Sstevel@tonic-gate "parse_nfs: Memory allocation failed");
15227c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
15237c478bd9Sstevel@tonic-gate }
15247c478bd9Sstevel@tonic-gate }
15257c478bd9Sstevel@tonic-gate /*
15267c478bd9Sstevel@tonic-gate * We check host_cnt to make sure we haven't parsed an entry
15277c478bd9Sstevel@tonic-gate * with no host information.
15287c478bd9Sstevel@tonic-gate */
15297c478bd9Sstevel@tonic-gate if (host_cnt == 0) {
15307c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
15317c478bd9Sstevel@tonic-gate "parse_nfs: invalid host specified - bad entry "
15327c478bd9Sstevel@tonic-gate "in map %s \"%s\"",
15337c478bd9Sstevel@tonic-gate mapname, w);
15347c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
15357c478bd9Sstevel@tonic-gate }
15367c478bd9Sstevel@tonic-gate if (getword(w, wq, lp, lq, ' ', wsize) == -1)
15377c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
15387c478bd9Sstevel@tonic-gate }
15397c478bd9Sstevel@tonic-gate
15407c478bd9Sstevel@tonic-gate strcpy(fsw, w);
15417c478bd9Sstevel@tonic-gate strcpy(fswq, wq);
15427c478bd9Sstevel@tonic-gate
15437c478bd9Sstevel@tonic-gate return (PARSE_OK);
15447c478bd9Sstevel@tonic-gate
15457c478bd9Sstevel@tonic-gate bad_entry:
15467c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "parse_nfs: bad entry in map %s \"%s\"", mapname, w);
15477c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
15487c478bd9Sstevel@tonic-gate }
15497c478bd9Sstevel@tonic-gate
15507c478bd9Sstevel@tonic-gate static int
parse_special(me,w,wq,lp,lq,wsize)15517c478bd9Sstevel@tonic-gate parse_special(me, w, wq, lp, lq, wsize)
15527c478bd9Sstevel@tonic-gate struct mapent *me;
15537c478bd9Sstevel@tonic-gate char *w, *wq, **lp, **lq;
15547c478bd9Sstevel@tonic-gate int wsize;
15557c478bd9Sstevel@tonic-gate {
15567c478bd9Sstevel@tonic-gate char devname[MAXPATHLEN + 1], qbuf[MAXPATHLEN + 1];
15577c478bd9Sstevel@tonic-gate char *wlp, *wlq;
15587c478bd9Sstevel@tonic-gate struct mapfs *mfs;
15597c478bd9Sstevel@tonic-gate
15607c478bd9Sstevel@tonic-gate wlp = w;
15617c478bd9Sstevel@tonic-gate wlq = wq;
15627c478bd9Sstevel@tonic-gate if (getword(devname, qbuf, &wlp, &wlq, ' ', sizeof (devname)) == -1)
15637c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
15647c478bd9Sstevel@tonic-gate if (devname[0] == '\0')
15657c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
15667c478bd9Sstevel@tonic-gate
15677c478bd9Sstevel@tonic-gate mfs = (struct mapfs *)malloc(sizeof (struct mapfs));
15687c478bd9Sstevel@tonic-gate if (mfs == NULL)
15697c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
15707c478bd9Sstevel@tonic-gate (void) memset(mfs, 0, sizeof (*mfs));
15717c478bd9Sstevel@tonic-gate
15727c478bd9Sstevel@tonic-gate /*
15737c478bd9Sstevel@tonic-gate * A device name that begins with a slash could
15747c478bd9Sstevel@tonic-gate * be confused with a mountpoint path, hence use
15757c478bd9Sstevel@tonic-gate * a colon to escape a device string that begins
15767c478bd9Sstevel@tonic-gate * with a slash, e.g.
15777c478bd9Sstevel@tonic-gate *
15787c478bd9Sstevel@tonic-gate * foo -ro /bar foo:/bar
15797c478bd9Sstevel@tonic-gate * and
15807c478bd9Sstevel@tonic-gate * foo -ro /dev/sr0
15817c478bd9Sstevel@tonic-gate *
15827c478bd9Sstevel@tonic-gate * would confuse the parser. The second instance
15837c478bd9Sstevel@tonic-gate * must use a colon:
15847c478bd9Sstevel@tonic-gate *
15857c478bd9Sstevel@tonic-gate * foo -ro :/dev/sr0
15867c478bd9Sstevel@tonic-gate */
15877c478bd9Sstevel@tonic-gate mfs->mfs_dir = strdup(&devname[devname[0] == ':']);
15887c478bd9Sstevel@tonic-gate if (mfs->mfs_dir == NULL)
15897c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
15907c478bd9Sstevel@tonic-gate me->map_fs = mfs;
15917c478bd9Sstevel@tonic-gate if (getword(w, wq, lp, lq, ' ', wsize) == -1)
15927c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
15937c478bd9Sstevel@tonic-gate return (0);
15947c478bd9Sstevel@tonic-gate }
15957c478bd9Sstevel@tonic-gate
15967c478bd9Sstevel@tonic-gate /*
15977c478bd9Sstevel@tonic-gate * get_dir_from_path(char *dir, char **path, int dirsz)
15987c478bd9Sstevel@tonic-gate * gets the directory name dir from path for max string of length dirsz.
15997c478bd9Sstevel@tonic-gate * A modification of the getword routine. Assumes the delimiter is '/'
16007c478bd9Sstevel@tonic-gate * and that excess /'s are redundant.
16017c478bd9Sstevel@tonic-gate * Returns PARSE_OK or PARSE_ERROR
16027c478bd9Sstevel@tonic-gate */
16037c478bd9Sstevel@tonic-gate static int
get_dir_from_path(char * dir,char ** path,int dirsz)16047c478bd9Sstevel@tonic-gate get_dir_from_path(char *dir, char **path, int dirsz)
16057c478bd9Sstevel@tonic-gate {
16067c478bd9Sstevel@tonic-gate char *tmp = dir;
16077c478bd9Sstevel@tonic-gate int count = dirsz;
16087c478bd9Sstevel@tonic-gate
16097c478bd9Sstevel@tonic-gate if (dirsz <= 0) {
16107c478bd9Sstevel@tonic-gate if (verbose)
16117c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
16127c478bd9Sstevel@tonic-gate "get_dir_from_path: invalid directory size %d", dirsz);
16137c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
16147c478bd9Sstevel@tonic-gate }
16157c478bd9Sstevel@tonic-gate
16167c478bd9Sstevel@tonic-gate /* get rid of leading /'s in path */
16177c478bd9Sstevel@tonic-gate while (**path == '/')
16187c478bd9Sstevel@tonic-gate (*path)++;
16197c478bd9Sstevel@tonic-gate
16207c478bd9Sstevel@tonic-gate /* now at a word or at the end of path */
16217c478bd9Sstevel@tonic-gate while ((**path) && ((**path) != '/')) {
16227c478bd9Sstevel@tonic-gate if (--count <= 0) {
16237c478bd9Sstevel@tonic-gate *tmp = '\0';
16247c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
16257c478bd9Sstevel@tonic-gate "get_dir_from_path: max pathlength exceeded %d", dirsz);
16267c478bd9Sstevel@tonic-gate return (PARSE_ERROR);
16277c478bd9Sstevel@tonic-gate }
16287c478bd9Sstevel@tonic-gate *dir++ = *(*path)++;
16297c478bd9Sstevel@tonic-gate }
16307c478bd9Sstevel@tonic-gate
16317c478bd9Sstevel@tonic-gate *dir = '\0';
16327c478bd9Sstevel@tonic-gate
16337c478bd9Sstevel@tonic-gate /* get rid of trailing /'s in path */
16347c478bd9Sstevel@tonic-gate while (**path == '/')
16357c478bd9Sstevel@tonic-gate (*path)++;
16367c478bd9Sstevel@tonic-gate
16377c478bd9Sstevel@tonic-gate return (PARSE_OK);
16387c478bd9Sstevel@tonic-gate }
16397c478bd9Sstevel@tonic-gate
16407c478bd9Sstevel@tonic-gate /*
16417c478bd9Sstevel@tonic-gate * alloc_hiernode(hiernode **newnode, char *dirname)
16427c478bd9Sstevel@tonic-gate * allocates a new hiernode corresponding to a new directory entry
16437c478bd9Sstevel@tonic-gate * in the hierarchical structure, and passes a pointer to it back
16447c478bd9Sstevel@tonic-gate * to the calling program.
16457c478bd9Sstevel@tonic-gate * Returns PARSE_OK or appropriate error value.
16467c478bd9Sstevel@tonic-gate */
16477c478bd9Sstevel@tonic-gate static int
alloc_hiernode(hiernode ** newnode,char * dirname)16487c478bd9Sstevel@tonic-gate alloc_hiernode(hiernode **newnode, char *dirname)
16497c478bd9Sstevel@tonic-gate {
16507c478bd9Sstevel@tonic-gate if ((*newnode = (hiernode *)malloc(sizeof (hiernode))) == NULL) {
16517c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "alloc_hiernode: Memory allocation failed");
16527c478bd9Sstevel@tonic-gate return (ENOMEM);
16537c478bd9Sstevel@tonic-gate }
16547c478bd9Sstevel@tonic-gate
16557c478bd9Sstevel@tonic-gate memset(((char *)*newnode), 0, sizeof (hiernode));
16567c478bd9Sstevel@tonic-gate strcpy(((*newnode)->dirname), dirname);
16577c478bd9Sstevel@tonic-gate return (PARSE_OK);
16587c478bd9Sstevel@tonic-gate }
16597c478bd9Sstevel@tonic-gate
16607c478bd9Sstevel@tonic-gate /*
16617c478bd9Sstevel@tonic-gate * free_hiernode(hiernode *node)
16627c478bd9Sstevel@tonic-gate * frees the allocated hiernode given the head of the structure
16637c478bd9Sstevel@tonic-gate * recursively calls itself until it frees entire structure.
16647c478bd9Sstevel@tonic-gate * Returns nothing.
16657c478bd9Sstevel@tonic-gate */
16667c478bd9Sstevel@tonic-gate static void
free_hiernode(hiernode * node)16677c478bd9Sstevel@tonic-gate free_hiernode(hiernode *node)
16687c478bd9Sstevel@tonic-gate {
16697c478bd9Sstevel@tonic-gate hiernode *currnode = node;
16707c478bd9Sstevel@tonic-gate hiernode *prevnode = NULL;
16717c478bd9Sstevel@tonic-gate
16727c478bd9Sstevel@tonic-gate while (currnode != NULL) {
16737c478bd9Sstevel@tonic-gate if (currnode->subdir != NULL)
16747c478bd9Sstevel@tonic-gate free_hiernode(currnode->subdir);
16757c478bd9Sstevel@tonic-gate prevnode = currnode;
16767c478bd9Sstevel@tonic-gate currnode = currnode->leveldir;
16777c478bd9Sstevel@tonic-gate free((void*)prevnode);
16787c478bd9Sstevel@tonic-gate }
16797c478bd9Sstevel@tonic-gate }
16807c478bd9Sstevel@tonic-gate
16817c478bd9Sstevel@tonic-gate /*
16827c478bd9Sstevel@tonic-gate * free_mapent(struct mapent *)
16837c478bd9Sstevel@tonic-gate * free the mapentry and its fields
16847c478bd9Sstevel@tonic-gate */
16857c478bd9Sstevel@tonic-gate void
free_mapent(me)16867c478bd9Sstevel@tonic-gate free_mapent(me)
16877c478bd9Sstevel@tonic-gate struct mapent *me;
16887c478bd9Sstevel@tonic-gate {
16897c478bd9Sstevel@tonic-gate struct mapfs *mfs;
16907c478bd9Sstevel@tonic-gate struct mapent *m;
16917c478bd9Sstevel@tonic-gate
16927c478bd9Sstevel@tonic-gate while (me) {
16937c478bd9Sstevel@tonic-gate while (me->map_fs) {
16947c478bd9Sstevel@tonic-gate mfs = me->map_fs;
16957c478bd9Sstevel@tonic-gate if (mfs->mfs_host)
16967c478bd9Sstevel@tonic-gate free(mfs->mfs_host);
16977c478bd9Sstevel@tonic-gate if (mfs->mfs_dir)
16987c478bd9Sstevel@tonic-gate free(mfs->mfs_dir);
16997c478bd9Sstevel@tonic-gate if (mfs->mfs_args)
17007c478bd9Sstevel@tonic-gate free(mfs->mfs_args);
17017c478bd9Sstevel@tonic-gate if (mfs->mfs_nconf)
17027c478bd9Sstevel@tonic-gate freenetconfigent(mfs->mfs_nconf);
17037c478bd9Sstevel@tonic-gate me->map_fs = mfs->mfs_next;
17047c478bd9Sstevel@tonic-gate free((char *)mfs);
17057c478bd9Sstevel@tonic-gate }
17067c478bd9Sstevel@tonic-gate
17077c478bd9Sstevel@tonic-gate if (me->map_root)
17087c478bd9Sstevel@tonic-gate free(me->map_root);
17097c478bd9Sstevel@tonic-gate if (me->map_mntpnt)
17107c478bd9Sstevel@tonic-gate free(me->map_mntpnt);
17117c478bd9Sstevel@tonic-gate if (me->map_mntopts)
17127c478bd9Sstevel@tonic-gate free(me->map_mntopts);
17137c478bd9Sstevel@tonic-gate if (me->map_fstype)
17147c478bd9Sstevel@tonic-gate free(me->map_fstype);
17157c478bd9Sstevel@tonic-gate if (me->map_mounter)
17167c478bd9Sstevel@tonic-gate free(me->map_mounter);
17177c478bd9Sstevel@tonic-gate if (me->map_fsw)
17187c478bd9Sstevel@tonic-gate free(me->map_fsw);
17197c478bd9Sstevel@tonic-gate if (me->map_fswq)
17207c478bd9Sstevel@tonic-gate free(me->map_fswq);
17217c478bd9Sstevel@tonic-gate
17227c478bd9Sstevel@tonic-gate m = me;
17237c478bd9Sstevel@tonic-gate me = me->map_next;
17247c478bd9Sstevel@tonic-gate free((char *)m);
17257c478bd9Sstevel@tonic-gate }
17267c478bd9Sstevel@tonic-gate }
17277c478bd9Sstevel@tonic-gate
17287c478bd9Sstevel@tonic-gate /*
17297c478bd9Sstevel@tonic-gate * trace_mapents(struct mapent *mapents)
17307c478bd9Sstevel@tonic-gate * traces through the mapentry structure and prints it element by element
17317c478bd9Sstevel@tonic-gate * returns nothing
17327c478bd9Sstevel@tonic-gate */
17337c478bd9Sstevel@tonic-gate static void
trace_mapents(char * s,struct mapent * mapents)17347c478bd9Sstevel@tonic-gate trace_mapents(char *s, struct mapent *mapents)
17357c478bd9Sstevel@tonic-gate {
17367c478bd9Sstevel@tonic-gate struct mapfs *mfs;
17377c478bd9Sstevel@tonic-gate struct mapent *me;
17387c478bd9Sstevel@tonic-gate
17397c478bd9Sstevel@tonic-gate trace_prt(1, "\n\t%s\n", s);
17407c478bd9Sstevel@tonic-gate for (me = mapents; me; me = me->map_next) {
17417c478bd9Sstevel@tonic-gate trace_prt(1, " (%s,%s)\t %s%s -%s\n",
17427c478bd9Sstevel@tonic-gate me->map_fstype ? me->map_fstype : "",
17437c478bd9Sstevel@tonic-gate me->map_mounter ? me->map_mounter : "",
17447c478bd9Sstevel@tonic-gate me->map_root ? me->map_root : "",
17457c478bd9Sstevel@tonic-gate me->map_mntpnt ? me->map_mntpnt : "",
17467c478bd9Sstevel@tonic-gate me->map_mntopts ? me->map_mntopts : "");
17477c478bd9Sstevel@tonic-gate for (mfs = me->map_fs; mfs; mfs = mfs->mfs_next)
17487c478bd9Sstevel@tonic-gate trace_prt(0, "\t\t%s:%s\n",
17497c478bd9Sstevel@tonic-gate mfs->mfs_host ? mfs->mfs_host: "",
17507c478bd9Sstevel@tonic-gate mfs->mfs_dir ? mfs->mfs_dir : "");
17517c478bd9Sstevel@tonic-gate
17527c478bd9Sstevel@tonic-gate trace_prt(1, "\tme->map_fsw=%s\n",
17537c478bd9Sstevel@tonic-gate me->map_fsw ? me->map_fsw:"",
17547c478bd9Sstevel@tonic-gate me->map_fswq ? me->map_fsw:"");
17557c478bd9Sstevel@tonic-gate trace_prt(1, "\t mntlevel=%d\t%s\t%s err=%d\n",
17567c478bd9Sstevel@tonic-gate me->map_mntlevel,
17577c478bd9Sstevel@tonic-gate me->map_modified ? "modify=TRUE":"modify=FALSE",
17587c478bd9Sstevel@tonic-gate me->map_faked ? "faked=TRUE":"faked=FALSE",
17597c478bd9Sstevel@tonic-gate me->map_err);
17607c478bd9Sstevel@tonic-gate }
17617c478bd9Sstevel@tonic-gate }
17627c478bd9Sstevel@tonic-gate
17637c478bd9Sstevel@tonic-gate /*
17647c478bd9Sstevel@tonic-gate * trace_hierarchy(hiernode *node)
17657c478bd9Sstevel@tonic-gate * traces the allocated hiernode given the head of the structure
17667c478bd9Sstevel@tonic-gate * recursively calls itself until it traces entire structure.
17677c478bd9Sstevel@tonic-gate * the first call made at the root is made with a zero level.
17687c478bd9Sstevel@tonic-gate * nodelevel is simply used to print tab and make the tracing clean.
17697c478bd9Sstevel@tonic-gate * Returns nothing.
17707c478bd9Sstevel@tonic-gate */
17717c478bd9Sstevel@tonic-gate static void
trace_hierarchy(hiernode * node,int nodelevel)17727c478bd9Sstevel@tonic-gate trace_hierarchy(hiernode *node, int nodelevel)
17737c478bd9Sstevel@tonic-gate {
17747c478bd9Sstevel@tonic-gate hiernode *currnode = node;
17757c478bd9Sstevel@tonic-gate int i;
17767c478bd9Sstevel@tonic-gate
17777c478bd9Sstevel@tonic-gate while (currnode != NULL) {
17787c478bd9Sstevel@tonic-gate if (currnode->subdir != NULL) {
17797c478bd9Sstevel@tonic-gate for (i = 0; i < nodelevel; i++)
17807c478bd9Sstevel@tonic-gate trace_prt(0, "\t");
17817c478bd9Sstevel@tonic-gate trace_prt(0, "\t(%s, ",
17827c478bd9Sstevel@tonic-gate currnode->dirname ? currnode->dirname :"");
17837c478bd9Sstevel@tonic-gate if (currnode->mapent) {
17847c478bd9Sstevel@tonic-gate trace_prt(0, "%d, %s)\n",
17857c478bd9Sstevel@tonic-gate currnode->mapent->map_mntlevel,
17867c478bd9Sstevel@tonic-gate currnode->mapent->map_mntopts ?
17877c478bd9Sstevel@tonic-gate currnode->mapent->map_mntopts:"");
17887c478bd9Sstevel@tonic-gate }
17897c478bd9Sstevel@tonic-gate else
17907c478bd9Sstevel@tonic-gate trace_prt(0, " ,)\n");
17917c478bd9Sstevel@tonic-gate nodelevel++;
17927c478bd9Sstevel@tonic-gate trace_hierarchy(currnode->subdir, nodelevel);
17937c478bd9Sstevel@tonic-gate } else {
17947c478bd9Sstevel@tonic-gate for (i = 0; i < nodelevel; i++)
17957c478bd9Sstevel@tonic-gate trace_prt(0, "\t");
17967c478bd9Sstevel@tonic-gate trace_prt(0, "\t(%s, ",
17977c478bd9Sstevel@tonic-gate currnode->dirname ? currnode->dirname :"");
17987c478bd9Sstevel@tonic-gate if (currnode->mapent) {
17997c478bd9Sstevel@tonic-gate trace_prt(0, "%d, %s)\n",
18007c478bd9Sstevel@tonic-gate currnode->mapent->map_mntlevel,
18017c478bd9Sstevel@tonic-gate currnode->mapent->map_mntopts ?
18027c478bd9Sstevel@tonic-gate currnode->mapent->map_mntopts:"");
18037c478bd9Sstevel@tonic-gate }
18047c478bd9Sstevel@tonic-gate else
18057c478bd9Sstevel@tonic-gate trace_prt(0, ", )\n");
18067c478bd9Sstevel@tonic-gate }
18077c478bd9Sstevel@tonic-gate currnode = currnode->leveldir;
18087c478bd9Sstevel@tonic-gate }
18097c478bd9Sstevel@tonic-gate }
18107c478bd9Sstevel@tonic-gate
18117c478bd9Sstevel@tonic-gate struct mapent *
do_mapent_hosts(mapopts,host,isdirect)18127c478bd9Sstevel@tonic-gate do_mapent_hosts(mapopts, host, isdirect)
18137c478bd9Sstevel@tonic-gate char *mapopts, *host;
18147c478bd9Sstevel@tonic-gate uint_t isdirect;
18157c478bd9Sstevel@tonic-gate {
18167c478bd9Sstevel@tonic-gate CLIENT *cl;
18177c478bd9Sstevel@tonic-gate struct mapent *me, *ms, *mp;
18187c478bd9Sstevel@tonic-gate struct mapfs *mfs;
18197c478bd9Sstevel@tonic-gate struct exportnode *ex = NULL;
18207c478bd9Sstevel@tonic-gate struct exportnode *exlist, *texlist, **texp, *exnext;
18217c478bd9Sstevel@tonic-gate struct timeval timeout;
18227c478bd9Sstevel@tonic-gate enum clnt_stat clnt_stat;
18237c478bd9Sstevel@tonic-gate char name[MAXPATHLEN];
18247c478bd9Sstevel@tonic-gate char entryopts[MAXOPTSLEN];
18257c478bd9Sstevel@tonic-gate char fstype[32], mounter[32];
18267c478bd9Sstevel@tonic-gate int exlen, duplicate;
18277c478bd9Sstevel@tonic-gate struct mnttab mb; /* needed for hasmntopt() to get nfs version */
18287c478bd9Sstevel@tonic-gate rpcvers_t nfsvers; /* version in map options, 0 if not there */
18297c478bd9Sstevel@tonic-gate rpcvers_t vers, versmin; /* used to negotiate nfs vers in pingnfs() */
18307c478bd9Sstevel@tonic-gate int retries, delay;
18317c478bd9Sstevel@tonic-gate int foundvers;
18327c478bd9Sstevel@tonic-gate
18337c478bd9Sstevel@tonic-gate if (trace > 1)
18347c478bd9Sstevel@tonic-gate trace_prt(1, " do_mapent_hosts: host %s\n", host);
18357c478bd9Sstevel@tonic-gate
18367c478bd9Sstevel@tonic-gate /* check for special case: host is me */
18377c478bd9Sstevel@tonic-gate
18387c478bd9Sstevel@tonic-gate if (self_check(host)) {
18397c478bd9Sstevel@tonic-gate ms = (struct mapent *)malloc(sizeof (*ms));
18407c478bd9Sstevel@tonic-gate if (ms == NULL)
18417c478bd9Sstevel@tonic-gate goto alloc_failed;
18427c478bd9Sstevel@tonic-gate (void) memset((char *)ms, 0, sizeof (*ms));
18437c478bd9Sstevel@tonic-gate (void) strcpy(fstype, MNTTYPE_NFS);
18447c478bd9Sstevel@tonic-gate get_opts(mapopts, entryopts, fstype, NULL);
18457c478bd9Sstevel@tonic-gate ms->map_mntopts = strdup(entryopts);
18467c478bd9Sstevel@tonic-gate if (ms->map_mntopts == NULL)
18477c478bd9Sstevel@tonic-gate goto alloc_failed;
18487c478bd9Sstevel@tonic-gate ms->map_mounter = strdup(fstype);
18497c478bd9Sstevel@tonic-gate if (ms->map_mounter == NULL)
18507c478bd9Sstevel@tonic-gate goto alloc_failed;
18517c478bd9Sstevel@tonic-gate ms->map_fstype = strdup(MNTTYPE_NFS);
18527c478bd9Sstevel@tonic-gate if (ms->map_fstype == NULL)
18537c478bd9Sstevel@tonic-gate goto alloc_failed;
18547c478bd9Sstevel@tonic-gate
18557c478bd9Sstevel@tonic-gate if (isdirect)
18567c478bd9Sstevel@tonic-gate name[0] = '\0';
18577c478bd9Sstevel@tonic-gate else {
18587c478bd9Sstevel@tonic-gate (void) strcpy(name, "/");
18597c478bd9Sstevel@tonic-gate (void) strcat(name, host);
18607c478bd9Sstevel@tonic-gate }
18617c478bd9Sstevel@tonic-gate ms->map_root = strdup(name);
18627c478bd9Sstevel@tonic-gate if (ms->map_root == NULL)
18637c478bd9Sstevel@tonic-gate goto alloc_failed;
18647c478bd9Sstevel@tonic-gate ms->map_mntpnt = strdup("");
18657c478bd9Sstevel@tonic-gate if (ms->map_mntpnt == NULL)
18667c478bd9Sstevel@tonic-gate goto alloc_failed;
18677c478bd9Sstevel@tonic-gate mfs = (struct mapfs *)malloc(sizeof (*mfs));
18687c478bd9Sstevel@tonic-gate if (mfs == NULL)
18697c478bd9Sstevel@tonic-gate goto alloc_failed;
18707c478bd9Sstevel@tonic-gate (void) memset((char *)mfs, 0, sizeof (*mfs));
18717c478bd9Sstevel@tonic-gate ms->map_fs = mfs;
18727c478bd9Sstevel@tonic-gate mfs->mfs_host = strdup(host);
18737c478bd9Sstevel@tonic-gate if (mfs->mfs_host == NULL)
18747c478bd9Sstevel@tonic-gate goto alloc_failed;
18757c478bd9Sstevel@tonic-gate mfs->mfs_dir = strdup("/");
18767c478bd9Sstevel@tonic-gate if (mfs->mfs_dir == NULL)
18777c478bd9Sstevel@tonic-gate goto alloc_failed;
18787c478bd9Sstevel@tonic-gate
18797c478bd9Sstevel@tonic-gate /* initialize mntlevel and modify */
18807c478bd9Sstevel@tonic-gate ms->map_mntlevel = -1;
18817c478bd9Sstevel@tonic-gate ms->map_modified = FALSE;
18827c478bd9Sstevel@tonic-gate ms->map_faked = FALSE;
18837c478bd9Sstevel@tonic-gate
18847c478bd9Sstevel@tonic-gate if (trace > 1)
18857c478bd9Sstevel@tonic-gate trace_prt(1,
18867c478bd9Sstevel@tonic-gate " do_mapent_hosts: self-host %s OK\n", host);
18877c478bd9Sstevel@tonic-gate
18887c478bd9Sstevel@tonic-gate return (ms);
18897c478bd9Sstevel@tonic-gate }
18907c478bd9Sstevel@tonic-gate
18917c478bd9Sstevel@tonic-gate /*
18927c478bd9Sstevel@tonic-gate * Call pingnfs. Note that we can't have replicated hosts in /net.
18937c478bd9Sstevel@tonic-gate * XXX - we would like to avoid duplicating the across the wire calls
18947c478bd9Sstevel@tonic-gate * made here in nfsmount(). The pingnfs cache should help avoid it.
18957c478bd9Sstevel@tonic-gate */
18967c478bd9Sstevel@tonic-gate mb.mnt_mntopts = mapopts;
18977c478bd9Sstevel@tonic-gate foundvers = nopt(&mb, MNTOPT_VERS, (int *)&nfsvers);
18987c478bd9Sstevel@tonic-gate if (!foundvers)
18997c478bd9Sstevel@tonic-gate nfsvers = 0;
19007c478bd9Sstevel@tonic-gate if (set_versrange(nfsvers, &vers, &versmin) != 0) {
19017c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "Incorrect NFS version specified for %s", host);
19027c478bd9Sstevel@tonic-gate return ((struct mapent *)NULL);
19037c478bd9Sstevel@tonic-gate }
19047c478bd9Sstevel@tonic-gate if (pingnfs(host, get_retry(mapopts) + 1, &vers, versmin, 0, FALSE,
19057c478bd9Sstevel@tonic-gate NULL, NULL) != RPC_SUCCESS)
19067c478bd9Sstevel@tonic-gate return ((struct mapent *)NULL);
19077c478bd9Sstevel@tonic-gate
19087c478bd9Sstevel@tonic-gate retries = get_retry(mapopts);
19097c478bd9Sstevel@tonic-gate delay = INITDELAY;
19107c478bd9Sstevel@tonic-gate retry:
19117c478bd9Sstevel@tonic-gate /* get export list of host */
19127c478bd9Sstevel@tonic-gate cl = clnt_create(host, MOUNTPROG, MOUNTVERS, "circuit_v");
19137c478bd9Sstevel@tonic-gate if (cl == NULL) {
19147c478bd9Sstevel@tonic-gate cl = clnt_create(host, MOUNTPROG, MOUNTVERS, "datagram_v");
19157c478bd9Sstevel@tonic-gate if (cl == NULL) {
19167c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
19177c478bd9Sstevel@tonic-gate "do_mapent_hosts: %s %s", host, clnt_spcreateerror(""));
19187c478bd9Sstevel@tonic-gate return ((struct mapent *)NULL);
19197c478bd9Sstevel@tonic-gate }
19207c478bd9Sstevel@tonic-gate
19217c478bd9Sstevel@tonic-gate }
19227c478bd9Sstevel@tonic-gate #ifdef MALLOC_DEBUG
19237c478bd9Sstevel@tonic-gate add_alloc("CLNT_HANDLE", cl, 0, __FILE__, __LINE__);
19247c478bd9Sstevel@tonic-gate add_alloc("AUTH_HANDLE", cl->cl_auth, 0,
19257c478bd9Sstevel@tonic-gate __FILE__, __LINE__);
19267c478bd9Sstevel@tonic-gate #endif
19277c478bd9Sstevel@tonic-gate
19287c478bd9Sstevel@tonic-gate timeout.tv_usec = 0;
19297c478bd9Sstevel@tonic-gate timeout.tv_sec = 25;
19307c478bd9Sstevel@tonic-gate if (clnt_stat = clnt_call(cl, MOUNTPROC_EXPORT, xdr_void, 0,
19317c478bd9Sstevel@tonic-gate xdr_exports, (caddr_t)&ex, timeout)) {
19327c478bd9Sstevel@tonic-gate
19337c478bd9Sstevel@tonic-gate if (retries-- > 0) {
19347c478bd9Sstevel@tonic-gate clnt_destroy(cl);
19357c478bd9Sstevel@tonic-gate DELAY(delay);
19367c478bd9Sstevel@tonic-gate goto retry;
19377c478bd9Sstevel@tonic-gate }
19387c478bd9Sstevel@tonic-gate
19397c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
19407c478bd9Sstevel@tonic-gate "do_mapent_hosts: %s: export list: %s",
19417c478bd9Sstevel@tonic-gate host, clnt_sperrno(clnt_stat));
19427c478bd9Sstevel@tonic-gate #ifdef MALLOC_DEBUG
19437c478bd9Sstevel@tonic-gate drop_alloc("CLNT_HANDLE", cl, __FILE__, __LINE__);
19447c478bd9Sstevel@tonic-gate drop_alloc("AUTH_HANDLE", cl->cl_auth,
19457c478bd9Sstevel@tonic-gate __FILE__, __LINE__);
19467c478bd9Sstevel@tonic-gate #endif
19477c478bd9Sstevel@tonic-gate clnt_destroy(cl);
19487c478bd9Sstevel@tonic-gate return ((struct mapent *)NULL);
19497c478bd9Sstevel@tonic-gate }
19507c478bd9Sstevel@tonic-gate
19517c478bd9Sstevel@tonic-gate #ifdef MALLOC_DEBUG
19527c478bd9Sstevel@tonic-gate drop_alloc("CLNT_HANDLE", cl, __FILE__, __LINE__);
19537c478bd9Sstevel@tonic-gate drop_alloc("AUTH_HANDLE", cl->cl_auth,
19547c478bd9Sstevel@tonic-gate __FILE__, __LINE__);
19557c478bd9Sstevel@tonic-gate #endif
19567c478bd9Sstevel@tonic-gate clnt_destroy(cl);
19577c478bd9Sstevel@tonic-gate
19587c478bd9Sstevel@tonic-gate if (ex == NULL) {
19597c478bd9Sstevel@tonic-gate if (trace > 1)
19607c478bd9Sstevel@tonic-gate trace_prt(1,
19617c478bd9Sstevel@tonic-gate gettext(" getmapent_hosts: null export list\n"));
19627c478bd9Sstevel@tonic-gate return ((struct mapent *)NULL);
19637c478bd9Sstevel@tonic-gate }
19647c478bd9Sstevel@tonic-gate
19657c478bd9Sstevel@tonic-gate /* now sort by length of names - to get mount order right */
19667c478bd9Sstevel@tonic-gate exlist = ex;
19677c478bd9Sstevel@tonic-gate texlist = NULL;
19687c478bd9Sstevel@tonic-gate #ifdef lint
19697c478bd9Sstevel@tonic-gate exnext = NULL;
19707c478bd9Sstevel@tonic-gate #endif
19717c478bd9Sstevel@tonic-gate for (; ex; ex = exnext) {
19727c478bd9Sstevel@tonic-gate exnext = ex->ex_next;
19737c478bd9Sstevel@tonic-gate exlen = strlen(ex->ex_dir);
19747c478bd9Sstevel@tonic-gate duplicate = 0;
19757c478bd9Sstevel@tonic-gate for (texp = &texlist; *texp; texp = &((*texp)->ex_next)) {
19767c478bd9Sstevel@tonic-gate if (exlen < (int)strlen((*texp)->ex_dir))
19777c478bd9Sstevel@tonic-gate break;
19787c478bd9Sstevel@tonic-gate duplicate = (strcmp(ex->ex_dir, (*texp)->ex_dir) == 0);
19797c478bd9Sstevel@tonic-gate if (duplicate) {
19807c478bd9Sstevel@tonic-gate /* disregard duplicate entry */
19817c478bd9Sstevel@tonic-gate freeex_ent(ex);
19827c478bd9Sstevel@tonic-gate break;
19837c478bd9Sstevel@tonic-gate }
19847c478bd9Sstevel@tonic-gate }
19857c478bd9Sstevel@tonic-gate if (!duplicate) {
19867c478bd9Sstevel@tonic-gate ex->ex_next = *texp;
19877c478bd9Sstevel@tonic-gate *texp = ex;
19887c478bd9Sstevel@tonic-gate }
19897c478bd9Sstevel@tonic-gate }
19907c478bd9Sstevel@tonic-gate exlist = texlist;
19917c478bd9Sstevel@tonic-gate
19927c478bd9Sstevel@tonic-gate /*
19937c478bd9Sstevel@tonic-gate * The following ugly chunk of code crept in as
19947c478bd9Sstevel@tonic-gate * a result of cachefs. If it's a cachefs mount
19957c478bd9Sstevel@tonic-gate * of an nfs filesystem, then have it handled as
19967c478bd9Sstevel@tonic-gate * an nfs mount but have cachefs do the mount.
19977c478bd9Sstevel@tonic-gate */
19987c478bd9Sstevel@tonic-gate (void) strcpy(fstype, MNTTYPE_NFS);
19997c478bd9Sstevel@tonic-gate get_opts(mapopts, entryopts, fstype, NULL);
20007c478bd9Sstevel@tonic-gate (void) strcpy(mounter, fstype);
20017c478bd9Sstevel@tonic-gate if (strcmp(fstype, MNTTYPE_CACHEFS) == 0) {
20027c478bd9Sstevel@tonic-gate struct mnttab m;
20037c478bd9Sstevel@tonic-gate char *p;
20047c478bd9Sstevel@tonic-gate
20057c478bd9Sstevel@tonic-gate m.mnt_mntopts = entryopts;
20067c478bd9Sstevel@tonic-gate if ((p = hasmntopt(&m, "backfstype")) != NULL) {
20077c478bd9Sstevel@tonic-gate int len = strlen(MNTTYPE_NFS);
20087c478bd9Sstevel@tonic-gate
20097c478bd9Sstevel@tonic-gate p += 11;
20107c478bd9Sstevel@tonic-gate if (strncmp(p, MNTTYPE_NFS, len) == 0 &&
20117c478bd9Sstevel@tonic-gate (p[len] == '\0' || p[len] == ',')) {
20127c478bd9Sstevel@tonic-gate /*
20137c478bd9Sstevel@tonic-gate * Cached nfs mount
20147c478bd9Sstevel@tonic-gate */
20157c478bd9Sstevel@tonic-gate (void) strcpy(fstype, MNTTYPE_NFS);
20167c478bd9Sstevel@tonic-gate (void) strcpy(mounter, MNTTYPE_CACHEFS);
20177c478bd9Sstevel@tonic-gate }
20187c478bd9Sstevel@tonic-gate }
20197c478bd9Sstevel@tonic-gate }
20207c478bd9Sstevel@tonic-gate
20217c478bd9Sstevel@tonic-gate /* Now create a mapent from the export list */
20227c478bd9Sstevel@tonic-gate ms = NULL;
20237c478bd9Sstevel@tonic-gate me = NULL;
20247c478bd9Sstevel@tonic-gate
20257c478bd9Sstevel@tonic-gate for (ex = exlist; ex; ex = ex->ex_next) {
20267c478bd9Sstevel@tonic-gate mp = me;
20277c478bd9Sstevel@tonic-gate me = (struct mapent *)malloc(sizeof (*me));
20287c478bd9Sstevel@tonic-gate if (me == NULL)
20297c478bd9Sstevel@tonic-gate goto alloc_failed;
20307c478bd9Sstevel@tonic-gate (void) memset((char *)me, 0, sizeof (*me));
20317c478bd9Sstevel@tonic-gate
20327c478bd9Sstevel@tonic-gate if (ms == NULL)
20337c478bd9Sstevel@tonic-gate ms = me;
20347c478bd9Sstevel@tonic-gate else
20357c478bd9Sstevel@tonic-gate mp->map_next = me;
20367c478bd9Sstevel@tonic-gate
20377c478bd9Sstevel@tonic-gate if (isdirect)
20387c478bd9Sstevel@tonic-gate name[0] = '\0';
20397c478bd9Sstevel@tonic-gate else {
20407c478bd9Sstevel@tonic-gate (void) strcpy(name, "/");
20417c478bd9Sstevel@tonic-gate (void) strcat(name, host);
20427c478bd9Sstevel@tonic-gate }
20437c478bd9Sstevel@tonic-gate me->map_root = strdup(name);
20447c478bd9Sstevel@tonic-gate if (me->map_root == NULL)
20457c478bd9Sstevel@tonic-gate goto alloc_failed;
20467c478bd9Sstevel@tonic-gate
20477c478bd9Sstevel@tonic-gate *name = '\0';
20487c478bd9Sstevel@tonic-gate if (strcmp(ex->ex_dir, "/") != 0) {
20497c478bd9Sstevel@tonic-gate if (*(ex->ex_dir) != '/')
20507c478bd9Sstevel@tonic-gate (void) strcpy(name, "/");
20517c478bd9Sstevel@tonic-gate (void) strcat(name, ex->ex_dir);
20527c478bd9Sstevel@tonic-gate }
20537c478bd9Sstevel@tonic-gate me->map_mntpnt = strdup(name);
20547c478bd9Sstevel@tonic-gate if (me->map_mntpnt == NULL)
20557c478bd9Sstevel@tonic-gate goto alloc_failed;
20567c478bd9Sstevel@tonic-gate
20577c478bd9Sstevel@tonic-gate me->map_fstype = strdup(fstype);
20587c478bd9Sstevel@tonic-gate if (me->map_fstype == NULL)
20597c478bd9Sstevel@tonic-gate goto alloc_failed;
20607c478bd9Sstevel@tonic-gate me->map_mounter = strdup(mounter);
20617c478bd9Sstevel@tonic-gate if (me->map_mounter == NULL)
20627c478bd9Sstevel@tonic-gate goto alloc_failed;
20637c478bd9Sstevel@tonic-gate me->map_mntopts = strdup(entryopts);
20647c478bd9Sstevel@tonic-gate if (me->map_mntopts == NULL)
20657c478bd9Sstevel@tonic-gate goto alloc_failed;
20667c478bd9Sstevel@tonic-gate
20677c478bd9Sstevel@tonic-gate mfs = (struct mapfs *)malloc(sizeof (*mfs));
20687c478bd9Sstevel@tonic-gate if (mfs == NULL)
20697c478bd9Sstevel@tonic-gate goto alloc_failed;
20707c478bd9Sstevel@tonic-gate (void) memset((char *)mfs, 0, sizeof (*mfs));
20717c478bd9Sstevel@tonic-gate me->map_fs = mfs;
20727c478bd9Sstevel@tonic-gate mfs->mfs_host = strdup(host);
20737c478bd9Sstevel@tonic-gate if (mfs->mfs_host == NULL)
20747c478bd9Sstevel@tonic-gate goto alloc_failed;
20757c478bd9Sstevel@tonic-gate mfs->mfs_dir = strdup(ex->ex_dir);
20767c478bd9Sstevel@tonic-gate if (mfs->mfs_dir == NULL)
20777c478bd9Sstevel@tonic-gate goto alloc_failed;
20787c478bd9Sstevel@tonic-gate
20797c478bd9Sstevel@tonic-gate /* initialize mntlevel and modify values */
20807c478bd9Sstevel@tonic-gate me->map_mntlevel = -1;
20817c478bd9Sstevel@tonic-gate me->map_modified = FALSE;
20827c478bd9Sstevel@tonic-gate me->map_faked = FALSE;
20837c478bd9Sstevel@tonic-gate }
20847c478bd9Sstevel@tonic-gate freeex(exlist);
20857c478bd9Sstevel@tonic-gate
20867c478bd9Sstevel@tonic-gate if (trace > 1)
20877c478bd9Sstevel@tonic-gate trace_prt(1, " do_mapent_hosts: host %s OK\n", host);
20887c478bd9Sstevel@tonic-gate
20897c478bd9Sstevel@tonic-gate return (ms);
20907c478bd9Sstevel@tonic-gate
20917c478bd9Sstevel@tonic-gate alloc_failed:
20927c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "do_mapent_hosts: Memory allocation failed");
20937c478bd9Sstevel@tonic-gate free_mapent(ms);
20947c478bd9Sstevel@tonic-gate freeex(exlist);
20957c478bd9Sstevel@tonic-gate return ((struct mapent *)NULL);
20967c478bd9Sstevel@tonic-gate }
20977c478bd9Sstevel@tonic-gate
20987c478bd9Sstevel@tonic-gate
20997c478bd9Sstevel@tonic-gate static void
freeex_ent(ex)21007c478bd9Sstevel@tonic-gate freeex_ent(ex)
21017c478bd9Sstevel@tonic-gate struct exportnode *ex;
21027c478bd9Sstevel@tonic-gate {
21037c478bd9Sstevel@tonic-gate struct groupnode *groups, *tmpgroups;
21047c478bd9Sstevel@tonic-gate
21057c478bd9Sstevel@tonic-gate free(ex->ex_dir);
21067c478bd9Sstevel@tonic-gate groups = ex->ex_groups;
21077c478bd9Sstevel@tonic-gate while (groups) {
21087c478bd9Sstevel@tonic-gate free(groups->gr_name);
21097c478bd9Sstevel@tonic-gate tmpgroups = groups->gr_next;
21107c478bd9Sstevel@tonic-gate free((char *)groups);
21117c478bd9Sstevel@tonic-gate groups = tmpgroups;
21127c478bd9Sstevel@tonic-gate }
21137c478bd9Sstevel@tonic-gate free((char *)ex);
21147c478bd9Sstevel@tonic-gate }
21157c478bd9Sstevel@tonic-gate
21167c478bd9Sstevel@tonic-gate static void
freeex(ex)21177c478bd9Sstevel@tonic-gate freeex(ex)
21187c478bd9Sstevel@tonic-gate struct exportnode *ex;
21197c478bd9Sstevel@tonic-gate {
21207c478bd9Sstevel@tonic-gate struct exportnode *tmpex;
21217c478bd9Sstevel@tonic-gate
21227c478bd9Sstevel@tonic-gate while (ex) {
21237c478bd9Sstevel@tonic-gate tmpex = ex->ex_next;
21247c478bd9Sstevel@tonic-gate freeex_ent(ex);
21257c478bd9Sstevel@tonic-gate ex = tmpex;
21267c478bd9Sstevel@tonic-gate }
21277c478bd9Sstevel@tonic-gate }
21287c478bd9Sstevel@tonic-gate
21297c478bd9Sstevel@tonic-gate static const char uatfs_err[] = "submount under fstype=autofs not supported";
21307c478bd9Sstevel@tonic-gate /*
21317c478bd9Sstevel@tonic-gate * dump_mapent_err(struct mapent *me, char *key, char *mapname)
21327c478bd9Sstevel@tonic-gate * syslog appropriate error in mapentries.
21337c478bd9Sstevel@tonic-gate */
dump_mapent_err(struct mapent * me,char * key,char * mapname)21347c478bd9Sstevel@tonic-gate static void dump_mapent_err(struct mapent *me, char *key, char *mapname)
21357c478bd9Sstevel@tonic-gate {
21367c478bd9Sstevel@tonic-gate switch (me->map_err) {
21377c478bd9Sstevel@tonic-gate case MAPENT_NOERR:
21387c478bd9Sstevel@tonic-gate if (verbose)
21397c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
21407c478bd9Sstevel@tonic-gate "map=%s key=%s mntpnt=%s: no error");
21417c478bd9Sstevel@tonic-gate break;
21427c478bd9Sstevel@tonic-gate case MAPENT_UATFS:
21437c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
21447c478bd9Sstevel@tonic-gate "mountpoint %s in map %s key %s not mounted: %s",
21457c478bd9Sstevel@tonic-gate me->map_mntpnt, mapname, key, uatfs_err);
21467c478bd9Sstevel@tonic-gate break;
21477c478bd9Sstevel@tonic-gate default:
21487c478bd9Sstevel@tonic-gate if (verbose)
21497c478bd9Sstevel@tonic-gate syslog(LOG_ERR,
21507c478bd9Sstevel@tonic-gate "map=%s key=%s mntpnt=%s: unknown mapentry error");
21517c478bd9Sstevel@tonic-gate }
21527c478bd9Sstevel@tonic-gate }
2153