xref: /titanic_51/usr/src/cmd/fs.d/autofs/ns_fnmount.c (revision 36e852a172cba914383d7341c988128b2c667fbd)
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
5*36e852a1SRaja Andra  * Common Development and Distribution License (the "License").
6*36e852a1SRaja Andra  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
227c478bd9Sstevel@tonic-gate  * ns_fnmount.c
237c478bd9Sstevel@tonic-gate  *
24*36e852a1SRaja Andra  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
25*36e852a1SRaja Andra  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <string.h>
317c478bd9Sstevel@tonic-gate #include <ctype.h>
327c478bd9Sstevel@tonic-gate #include <syslog.h>
337c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
347c478bd9Sstevel@tonic-gate #include <rpcsvc/nis.h>
357c478bd9Sstevel@tonic-gate #include <xfn/xfn.h>
367c478bd9Sstevel@tonic-gate #include "automount.h"
377c478bd9Sstevel@tonic-gate #include "ns_fnutils.h"
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * The maximum sizes of map names, key names, composite names, and status
427c478bd9Sstevel@tonic-gate  * descriptions, including the trailing '\0'.
437c478bd9Sstevel@tonic-gate  */
447c478bd9Sstevel@tonic-gate #define	MAPNAMESZ	(size_t)(AUTOFS_MAXCOMPONENTLEN + 1)
457c478bd9Sstevel@tonic-gate #define	KEYNAMESZ	(size_t)(AUTOFS_MAXCOMPONENTLEN + 1)
467c478bd9Sstevel@tonic-gate #define	COMPNAMESZ	(size_t)(MAPNAMESZ - FNPREFIXLEN + KEYNAMESZ - 2)
477c478bd9Sstevel@tonic-gate #define	DESCSZ		(size_t)512
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate typedef struct mapent	mapent;
507c478bd9Sstevel@tonic-gate typedef struct mapline	mapline;
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /*
547c478bd9Sstevel@tonic-gate  * The name of an attribute.
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate static const FN_identifier_t attr_exported = {FN_ID_STRING, 8, "exported"};
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate  * Given a request by a particular user to mount the name "key" under
617c478bd9Sstevel@tonic-gate  * map/context "map", and a set of default mount options, return (in
627c478bd9Sstevel@tonic-gate  * "res") either a list of mapents giving the mounts that need to be
637c478bd9Sstevel@tonic-gate  * performed, or a symbolic link to be created for a user-relative
647c478bd9Sstevel@tonic-gate  * context.  If "shallow" is true return, in place of the list of
657c478bd9Sstevel@tonic-gate  * mapents, a single mapent representing an indirect mount point.
667c478bd9Sstevel@tonic-gate  *
677c478bd9Sstevel@tonic-gate  *	void
687c478bd9Sstevel@tonic-gate  *	getmapent_fn(char *key, char *map, char *opts, uid_t uid,
697c478bd9Sstevel@tonic-gate  *	      bool_t shallow, getmapent_fn_res *res);
707c478bd9Sstevel@tonic-gate  */
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /*
737c478bd9Sstevel@tonic-gate  * Given a reference, its composite name, default mount options, and a
747c478bd9Sstevel@tonic-gate  * mapent root, return a list of mapents to mount.  If "shallow" is
757c478bd9Sstevel@tonic-gate  * true return, in place of the list of mapents, a single mapent
767c478bd9Sstevel@tonic-gate  * representing an indirect mount point.  The map and key strings are
777c478bd9Sstevel@tonic-gate  * pieces of the composite name such that:
787c478bd9Sstevel@tonic-gate  * "FNPREFIX/cname" == "map/key".
797c478bd9Sstevel@tonic-gate  */
807c478bd9Sstevel@tonic-gate static mapent *
817c478bd9Sstevel@tonic-gate process_ref(const FN_ref_t *ref, const char *cname, char *map, char *key,
827c478bd9Sstevel@tonic-gate     char *opts, char *root, bool_t shallow, FN_status_t *status);
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate /*
857c478bd9Sstevel@tonic-gate  * Traverse the namespace to find a frontier below ref along which
867c478bd9Sstevel@tonic-gate  * future mounts may need to be triggered.  Add to mapents the
877c478bd9Sstevel@tonic-gate  * corresponding direct autofs mount points.
887c478bd9Sstevel@tonic-gate  *     map:	map name for ref
897c478bd9Sstevel@tonic-gate  *     maplen:	strlen(map)
907c478bd9Sstevel@tonic-gate  *     mntpnt:	suffix of map where the current mount request begins
917c478bd9Sstevel@tonic-gate  *		(starts off as "", and grows as we traverse the namespace)
927c478bd9Sstevel@tonic-gate  *     opts:	default mount options
937c478bd9Sstevel@tonic-gate  *     status:	passed from above to avoid having to allocate one on each call
947c478bd9Sstevel@tonic-gate  * Works by calling frontier_aux() on each name bound under ref.
957c478bd9Sstevel@tonic-gate  * Return the new mapents, or free mapents and return NULL on failure.
967c478bd9Sstevel@tonic-gate  */
977c478bd9Sstevel@tonic-gate static mapent *
987c478bd9Sstevel@tonic-gate frontier(mapent *mapents, const FN_ref_t *ref, char *map, size_t maplen,
997c478bd9Sstevel@tonic-gate     char *mntpnt, char *opts, FN_status_t *status);
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate /*
1027c478bd9Sstevel@tonic-gate  * Called by frontier(), once for each "name" that it finds.  map is
1037c478bd9Sstevel@tonic-gate  * passed unchanged from frontier().  ref is the reference named by
1047c478bd9Sstevel@tonic-gate  * "map/name".  If ref is found to be along the frontier, add the
1057c478bd9Sstevel@tonic-gate  * corresponding direct autofs mount point to mapents.  Otherwise
1067c478bd9Sstevel@tonic-gate  * continue traversing the namespace to find the frontier.  Other
1077c478bd9Sstevel@tonic-gate  * arguments and the return value are as for frontier().
1087c478bd9Sstevel@tonic-gate  */
1097c478bd9Sstevel@tonic-gate static mapent *
1107c478bd9Sstevel@tonic-gate frontier_aux(mapent *mapents, const FN_ref_t *ref, char *map, size_t maplen,
1117c478bd9Sstevel@tonic-gate     char *mntpnt, const char *name, char *opts, FN_status_t *status);
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate  * Given a reference with an address type of ADDR_HOST and its
1157c478bd9Sstevel@tonic-gate  * composite name, check the attr_exported attribute to determine if
1167c478bd9Sstevel@tonic-gate  * the corresponding directory is exported.  Return FALSE on error.
1177c478bd9Sstevel@tonic-gate  */
1187c478bd9Sstevel@tonic-gate static bool_t
1197c478bd9Sstevel@tonic-gate exported(const FN_ref_t *ref, const char *cname, FN_status_t *status);
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate  * Find a reference's address type and, if "data" is not NULL, its
1237c478bd9Sstevel@tonic-gate  * data string.  If there is no address of a known type, set *typep to
1247c478bd9Sstevel@tonic-gate  * NUM_ADDRTYPES; if there are several, stop after finding the first.
1257c478bd9Sstevel@tonic-gate  * Return 0 on success.
1267c478bd9Sstevel@tonic-gate  */
1277c478bd9Sstevel@tonic-gate static int
1287c478bd9Sstevel@tonic-gate addr_from_ref(const FN_ref_t *ref, const char *cname, addrtype_t *typep,
1297c478bd9Sstevel@tonic-gate     char *data, size_t datasz);
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate /*
1327c478bd9Sstevel@tonic-gate  * Decode an address's data into a string.  Return 0 on success.
1337c478bd9Sstevel@tonic-gate  */
1347c478bd9Sstevel@tonic-gate static int
1357c478bd9Sstevel@tonic-gate str_from_addr(const char *cname, const FN_ref_addr_t *addr, char str[],
1367c478bd9Sstevel@tonic-gate     size_t strsz);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate /*
1397c478bd9Sstevel@tonic-gate  * Given a map name and its current length, append "/name".  Return
1407c478bd9Sstevel@tonic-gate  * the new length.  On error, syslog a warning and return 0.
1417c478bd9Sstevel@tonic-gate  */
1427c478bd9Sstevel@tonic-gate static size_t
1437c478bd9Sstevel@tonic-gate append_mapname(char *map, size_t maplen, const char *name);
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate /*
1467c478bd9Sstevel@tonic-gate  * Concatenate two strings using the given separator.  The result is a
1477c478bd9Sstevel@tonic-gate  * newly-allocated string, or NULL on error.
1487c478bd9Sstevel@tonic-gate  */
1497c478bd9Sstevel@tonic-gate static char *
1507c478bd9Sstevel@tonic-gate concat(const char *s1, char sep, const char *s2);
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate /*
1537c478bd9Sstevel@tonic-gate  * Add the "nosuid" option to a mapent.  Also check for a sneaky
1547c478bd9Sstevel@tonic-gate  * hacker trying to override this option by manually inserting a
1557c478bd9Sstevel@tonic-gate  * multiple mount entry into the XFN namespace.  Return FALSE on error.
1567c478bd9Sstevel@tonic-gate  */
1577c478bd9Sstevel@tonic-gate static bool_t
1587c478bd9Sstevel@tonic-gate safe_mapent(mapent *me);
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate /*
1617c478bd9Sstevel@tonic-gate  * Append "nosuid" to a list of options.  The result is a
1627c478bd9Sstevel@tonic-gate  * newly-allocated string, or NULL on error.
1637c478bd9Sstevel@tonic-gate  */
1647c478bd9Sstevel@tonic-gate static char *
1657c478bd9Sstevel@tonic-gate safe_opts(const char *opts);
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate /*
1687c478bd9Sstevel@tonic-gate  * Trim comments and trailing whitespace from ml->linebuf, then
1697c478bd9Sstevel@tonic-gate  * unquote it and leave the result in ml.  Return 0 on success.
1707c478bd9Sstevel@tonic-gate  */
1717c478bd9Sstevel@tonic-gate static int
1727c478bd9Sstevel@tonic-gate trim_line(mapline *ml);
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate /*
1757c478bd9Sstevel@tonic-gate  * Determine whether ml contains an option string (such as "-ro") and
1767c478bd9Sstevel@tonic-gate  * nothing else.
1777c478bd9Sstevel@tonic-gate  */
1787c478bd9Sstevel@tonic-gate static bool_t
1797c478bd9Sstevel@tonic-gate opts_only(const mapline *ml);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate /*
1827c478bd9Sstevel@tonic-gate  * Allocate a new mapent structure.  The arguments must have been
1837c478bd9Sstevel@tonic-gate  * malloc'ed, and are owned by the mapent; they are freed if
1847c478bd9Sstevel@tonic-gate  * new_mapent() fails.  If any argument is NULL, the call fails and a
1857c478bd9Sstevel@tonic-gate  * memory allocation failure is logged.  A root argument of 'noroot'
1867c478bd9Sstevel@tonic-gate  * indicates that the map_root field does not need to be set (it's
1877c478bd9Sstevel@tonic-gate  * only needed in the first of a list of mapents).
1887c478bd9Sstevel@tonic-gate  */
1897c478bd9Sstevel@tonic-gate static char *noroot = "[no root]";
1907c478bd9Sstevel@tonic-gate static mapent *
1917c478bd9Sstevel@tonic-gate new_mapent(char *root, char *mntpnt, char *fstype, char *mntopts, char *host,
1927c478bd9Sstevel@tonic-gate     char *dir);
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate /*
1957c478bd9Sstevel@tonic-gate  * Determine whether cname is a user-relative binding -- such as "myself" --
1967c478bd9Sstevel@tonic-gate  * in the initial context.
1977c478bd9Sstevel@tonic-gate  */
1987c478bd9Sstevel@tonic-gate static bool_t
1997c478bd9Sstevel@tonic-gate is_user_relative(const char *cname);
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate /*
2027c478bd9Sstevel@tonic-gate  * Given the name of a user-relative binding, return an equivalent
2037c478bd9Sstevel@tonic-gate  * name that is not user-relative.
2047c478bd9Sstevel@tonic-gate  */
2057c478bd9Sstevel@tonic-gate static char *
2067c478bd9Sstevel@tonic-gate equiv_name(FN_ctx_t *, const char *cname, FN_status_t *);
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate void
2097c478bd9Sstevel@tonic-gate getmapent_fn(char *key, char *map, char *opts, uid_t uid, bool_t shallow,
2107c478bd9Sstevel@tonic-gate     getmapent_fn_res *res)
2117c478bd9Sstevel@tonic-gate {
2127c478bd9Sstevel@tonic-gate 	size_t			maplen;
2137c478bd9Sstevel@tonic-gate 	FN_status_t		*status;
2147c478bd9Sstevel@tonic-gate 	FN_ctx_t		*init_ctx = NULL;
2157c478bd9Sstevel@tonic-gate 	int			statcode;
2167c478bd9Sstevel@tonic-gate 	char			cname[COMPNAMESZ];
2177c478bd9Sstevel@tonic-gate 	FN_composite_name_t	*compname;
2187c478bd9Sstevel@tonic-gate 	FN_ref_t		*ref;
2197c478bd9Sstevel@tonic-gate 	char			mapname[MAPNAMESZ];
2207c478bd9Sstevel@tonic-gate 	char			*root;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	res->type = FN_NONE;
2237c478bd9Sstevel@tonic-gate 	res->m_or_l.mapents = NULL;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	if (init_fn() != 0) {
2267c478bd9Sstevel@tonic-gate 		return;
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	/*
2307c478bd9Sstevel@tonic-gate 	 * For direct mounts, the key is the entire path, and the map
2317c478bd9Sstevel@tonic-gate 	 * name already has the final key component appended.  Split
2327c478bd9Sstevel@tonic-gate 	 * apart the map name and key.  The "root" of the mapent is
2337c478bd9Sstevel@tonic-gate 	 * "/key" for indirect mounts, and "" for direct mounts.
2347c478bd9Sstevel@tonic-gate 	 */
2357c478bd9Sstevel@tonic-gate 	strcpy(mapname, map);
2367c478bd9Sstevel@tonic-gate 	if (key[0] == '/') {
2377c478bd9Sstevel@tonic-gate 		key = strrchr(key, '/') + 1;
2387c478bd9Sstevel@tonic-gate 		*strrchr(mapname, '/') = '\0';
2397c478bd9Sstevel@tonic-gate 		root = strdup("");
2407c478bd9Sstevel@tonic-gate 	} else {
2417c478bd9Sstevel@tonic-gate 		root = concat("", '/', key);
2427c478bd9Sstevel@tonic-gate 	}
2437c478bd9Sstevel@tonic-gate 	map = mapname;
2447c478bd9Sstevel@tonic-gate 	maplen = strlen(map);
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	if ((maplen - FNPREFIXLEN + strlen(key)) >= COMPNAMESZ) {
2477c478bd9Sstevel@tonic-gate 		if (verbose) {
2487c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "name %s/%s too long", map, key);
2497c478bd9Sstevel@tonic-gate 		}
2507c478bd9Sstevel@tonic-gate 		return;
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate 	if (maplen == FNPREFIXLEN) {
2537c478bd9Sstevel@tonic-gate 		strcpy(cname, key);
2547c478bd9Sstevel@tonic-gate 	} else {
2557c478bd9Sstevel@tonic-gate 		sprintf(cname, "%s/%s", map + FNPREFIXLEN + 1, key);
2567c478bd9Sstevel@tonic-gate 	}
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	status = fn_status_create();
2597c478bd9Sstevel@tonic-gate 	if (status == NULL) {
2607c478bd9Sstevel@tonic-gate 		if (verbose) {
2617c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "Could not create FNS status object");
2627c478bd9Sstevel@tonic-gate 		}
2637c478bd9Sstevel@tonic-gate 		return;
2647c478bd9Sstevel@tonic-gate 	}
2657c478bd9Sstevel@tonic-gate 	init_ctx = _fn_ctx_handle_from_initial_with_uid(uid, 0, status);
2667c478bd9Sstevel@tonic-gate 	if (init_ctx == NULL) {
2677c478bd9Sstevel@tonic-gate 		logstat(status, "", "No initial context");
2687c478bd9Sstevel@tonic-gate 		goto done;
2697c478bd9Sstevel@tonic-gate 	}
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate #ifndef XFN1ENV
2727c478bd9Sstevel@tonic-gate 	if (is_user_relative(cname)) {
2737c478bd9Sstevel@tonic-gate 		res->type = FN_SYMLINK;
2747c478bd9Sstevel@tonic-gate 		res->m_or_l.symlink = equiv_name(init_ctx, cname, status);
2757c478bd9Sstevel@tonic-gate 		goto done;
2767c478bd9Sstevel@tonic-gate 	}
2777c478bd9Sstevel@tonic-gate #endif
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	if ((compname = new_cname(cname)) == NULL) {
2807c478bd9Sstevel@tonic-gate 		goto done;
2817c478bd9Sstevel@tonic-gate 	}
2827c478bd9Sstevel@tonic-gate 	ref = fn_ctx_lookup(init_ctx, compname, status);
2837c478bd9Sstevel@tonic-gate 	statcode = fn_status_code(status);
2847c478bd9Sstevel@tonic-gate 	fn_composite_name_destroy(compname);
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	if (trace > 1 && !shallow) {
2877c478bd9Sstevel@tonic-gate 		trace_prt(1, "  FNS traversal: %s\n", cname);
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	if (ref == NULL) {
2917c478bd9Sstevel@tonic-gate 		if ((statcode != FN_E_NAME_NOT_FOUND) &&
2927c478bd9Sstevel@tonic-gate 		    (statcode != FN_E_NOT_A_CONTEXT)) {
2937c478bd9Sstevel@tonic-gate 			logstat(status, "lookup failed on", cname);
2947c478bd9Sstevel@tonic-gate 		}
2957c478bd9Sstevel@tonic-gate 		goto done;
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	res->type = FN_MAPENTS;
2997c478bd9Sstevel@tonic-gate 	res->m_or_l.mapents =
3007c478bd9Sstevel@tonic-gate 	    process_ref(ref, cname, map, key, opts, root, shallow, status);
3017c478bd9Sstevel@tonic-gate 	fn_ref_destroy(ref);
3027c478bd9Sstevel@tonic-gate done:
3037c478bd9Sstevel@tonic-gate 	fn_ctx_handle_destroy(init_ctx);
3047c478bd9Sstevel@tonic-gate 	fn_status_destroy(status);
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate static mapent *
3097c478bd9Sstevel@tonic-gate process_ref(const FN_ref_t *ref, const char *cname, char *map, char *key,
3107c478bd9Sstevel@tonic-gate     char *opts, char *root, bool_t shallow, FN_status_t *status)
3117c478bd9Sstevel@tonic-gate {
3127c478bd9Sstevel@tonic-gate 	addrtype_t	addrtype;
3137c478bd9Sstevel@tonic-gate 	mapline		ml;
3147c478bd9Sstevel@tonic-gate 	char		*addrdata = ml.linebuf;
3157c478bd9Sstevel@tonic-gate 	mapent		*mapents;
3167c478bd9Sstevel@tonic-gate 	bool_t		self;
3177c478bd9Sstevel@tonic-gate 	char		*homedir;
3187c478bd9Sstevel@tonic-gate 	size_t		maplen;
3197c478bd9Sstevel@tonic-gate 	char		*colon;
3207c478bd9Sstevel@tonic-gate 	char		*nfshost;
3217c478bd9Sstevel@tonic-gate 	char		*nfsdir;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	if ((reftype(ref) < NUM_REFTYPES) &&
3247c478bd9Sstevel@tonic-gate 	    (addr_from_ref(ref, cname, &addrtype, addrdata, LINESZ) == 0)) {
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 		switch (addrtype) {
3277c478bd9Sstevel@tonic-gate 		case ADDR_MOUNT:
3287c478bd9Sstevel@tonic-gate 			if (trim_line(&ml) != 0) {
3297c478bd9Sstevel@tonic-gate 				return (NULL);
3307c478bd9Sstevel@tonic-gate 			}
3317c478bd9Sstevel@tonic-gate 			if (opts_only(&ml)) {
3327c478bd9Sstevel@tonic-gate 				/* parse_entry() can't handle such lines */
3337c478bd9Sstevel@tonic-gate 				if (macro_expand("&", ml.linebuf,
3347c478bd9Sstevel@tonic-gate 				    ml.lineqbuf, LINESZ)) {
3357c478bd9Sstevel@tonic-gate 					syslog(LOG_ERR,
3367c478bd9Sstevel@tonic-gate 					"%s/%s: opts too long (max %d chars)",
3377c478bd9Sstevel@tonic-gate 					    FNPREFIX, cname, LINESZ - 1);
3387c478bd9Sstevel@tonic-gate 					return (NULL);
3397c478bd9Sstevel@tonic-gate 				}
3407c478bd9Sstevel@tonic-gate 				opts = ml.linebuf + 1;	/* skip '-' */
3417c478bd9Sstevel@tonic-gate 				goto indirect;
3427c478bd9Sstevel@tonic-gate 			}
3437c478bd9Sstevel@tonic-gate 			mapents = parse_entry(key, map, opts, &ml, NULL, 0,
3447c478bd9Sstevel@tonic-gate 			    TRUE);
3457c478bd9Sstevel@tonic-gate 			if (mapents == NULL || !safe_mapent(mapents)) {
3467c478bd9Sstevel@tonic-gate 				free_mapent(mapents);
3477c478bd9Sstevel@tonic-gate 				return (NULL);
3487c478bd9Sstevel@tonic-gate 			}
3497c478bd9Sstevel@tonic-gate 			free(mapents->map_root);
3507c478bd9Sstevel@tonic-gate 			mapents->map_root = root;
3517c478bd9Sstevel@tonic-gate 			break;
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 		case ADDR_HOST:
3547c478bd9Sstevel@tonic-gate 			/*
3557c478bd9Sstevel@tonic-gate 			 * Address is of the form "host:dir".
3567c478bd9Sstevel@tonic-gate 			 * If "dir" is not supplied, it defaults to "/".
3577c478bd9Sstevel@tonic-gate 			 */
3587c478bd9Sstevel@tonic-gate 			colon = strchr(addrdata, ':');
3597c478bd9Sstevel@tonic-gate 			if (colon == NULL || colon[1] == '\0') {
3607c478bd9Sstevel@tonic-gate 				nfsdir = strdup("/");
3617c478bd9Sstevel@tonic-gate 			} else {
3627c478bd9Sstevel@tonic-gate 				*colon = '\0';
3637c478bd9Sstevel@tonic-gate 				nfsdir = strdup(colon + 1);
3647c478bd9Sstevel@tonic-gate 			}
3657c478bd9Sstevel@tonic-gate 			nfshost = strdup(addrdata);
3667c478bd9Sstevel@tonic-gate 			/*
3677c478bd9Sstevel@tonic-gate 			 * If nfshost is the local host, the NFS mount
3687c478bd9Sstevel@tonic-gate 			 * request will be converted to a loopback
3697c478bd9Sstevel@tonic-gate 			 * mount.  Otherwise check that the file system
3707c478bd9Sstevel@tonic-gate 			 * is exported.
3717c478bd9Sstevel@tonic-gate 			 */
3727c478bd9Sstevel@tonic-gate 			if (nfshost != NULL) {
3737c478bd9Sstevel@tonic-gate 				self = self_check(nfshost);
3747c478bd9Sstevel@tonic-gate 				if (!self && !exported(ref, cname, status)) {
3757c478bd9Sstevel@tonic-gate 					if (transient(status)) {
3767c478bd9Sstevel@tonic-gate 						return (NULL);
3777c478bd9Sstevel@tonic-gate 					} else {
3787c478bd9Sstevel@tonic-gate 						goto indirect;
3797c478bd9Sstevel@tonic-gate 					}
3807c478bd9Sstevel@tonic-gate 				}
3817c478bd9Sstevel@tonic-gate 			}
3827c478bd9Sstevel@tonic-gate 			mapents = new_mapent(root, strdup(""), strdup("nfs"),
3837c478bd9Sstevel@tonic-gate 			    safe_opts(opts), nfshost, nfsdir);
3847c478bd9Sstevel@tonic-gate 			if (self && !shallow) {
3857c478bd9Sstevel@tonic-gate 				return (mapents);
3867c478bd9Sstevel@tonic-gate 			}
3877c478bd9Sstevel@tonic-gate 			break;
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 		case ADDR_USER:
3907c478bd9Sstevel@tonic-gate 			homedir = strdup(addrdata);
3917c478bd9Sstevel@tonic-gate 			homedir[strcspn(homedir, " \t\r\n")] = '\0';
3927c478bd9Sstevel@tonic-gate 			mapents = new_mapent(root, strdup(""), strdup("lofs"),
3937c478bd9Sstevel@tonic-gate 			    strdup(opts), strdup(""), homedir);
3947c478bd9Sstevel@tonic-gate 			break;
3957c478bd9Sstevel@tonic-gate 		}
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 		if (mapents == NULL) {
3987c478bd9Sstevel@tonic-gate 			return (NULL);
3997c478bd9Sstevel@tonic-gate 		}
4007c478bd9Sstevel@tonic-gate 		if (shallow) {
4017c478bd9Sstevel@tonic-gate 			mapents->map_root = NULL;	/* don't free "root" */
4027c478bd9Sstevel@tonic-gate 			free_mapent(mapents);
4037c478bd9Sstevel@tonic-gate 			goto indirect;
4047c478bd9Sstevel@tonic-gate 		}
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 		/* "map" => "map/key" */
4077c478bd9Sstevel@tonic-gate 		if ((maplen = append_mapname(map, strlen(map), key)) == 0) {
4087c478bd9Sstevel@tonic-gate 			return (mapents);
4097c478bd9Sstevel@tonic-gate 		}
4107c478bd9Sstevel@tonic-gate 		return (frontier(mapents, ref, map, maplen, map + maplen,
4117c478bd9Sstevel@tonic-gate 		    opts, status));
4127c478bd9Sstevel@tonic-gate 	}
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	/* Ref type wasn't recognized. */
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate indirect:
4177c478bd9Sstevel@tonic-gate 	/* Install an indirect autofs mount point. */
4187c478bd9Sstevel@tonic-gate 	return (new_mapent(root, strdup(""), strdup("autofs"), strdup(opts),
4197c478bd9Sstevel@tonic-gate 	    strdup(""), concat(map, '/', key)));
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate /*
4247c478bd9Sstevel@tonic-gate  * All that this function really does is call frontier_aux() on every
4257c478bd9Sstevel@tonic-gate  * name bound under ref.  The rest is error checking(!)
4267c478bd9Sstevel@tonic-gate  *
4277c478bd9Sstevel@tonic-gate  * The error handling strategy is to reject the entire mount request
4287c478bd9Sstevel@tonic-gate  * (by freeing mapents) if any (potentially) transient error occurs,
4297c478bd9Sstevel@tonic-gate  * and to treat nontransient errors as holes in the affected portions
4307c478bd9Sstevel@tonic-gate  * of the namespace.
4317c478bd9Sstevel@tonic-gate  */
4327c478bd9Sstevel@tonic-gate static mapent *
4337c478bd9Sstevel@tonic-gate frontier(mapent *mapents, const FN_ref_t *ref, char *map, size_t maplen,
4347c478bd9Sstevel@tonic-gate     char *mntpnt, char *opts, FN_status_t *status)
4357c478bd9Sstevel@tonic-gate {
4367c478bd9Sstevel@tonic-gate 	FN_ctx_t		*ctx;
4377c478bd9Sstevel@tonic-gate 	FN_bindinglist_t	*bindings = NULL;
4387c478bd9Sstevel@tonic-gate 	FN_ref_t		*child_ref;
4397c478bd9Sstevel@tonic-gate 	FN_string_t		*child_s;
4407c478bd9Sstevel@tonic-gate 	const char		*child;
4417c478bd9Sstevel@tonic-gate 	unsigned int		statcode;
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	ctx = fn_ctx_handle_from_ref(ref, XFN2(0) status);
4447c478bd9Sstevel@tonic-gate 	if (ctx == NULL) {
4457c478bd9Sstevel@tonic-gate 		if (fn_status_code(status) != FN_E_NO_SUPPORTED_ADDRESS) {
4467c478bd9Sstevel@tonic-gate 			logstat(status, "from_ref failed for", map);
4477c478bd9Sstevel@tonic-gate 		}
4487c478bd9Sstevel@tonic-gate 		goto checkerr_return;
4497c478bd9Sstevel@tonic-gate 	}
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	bindings = fn_ctx_list_bindings(ctx, empty_cname, status);
4527c478bd9Sstevel@tonic-gate 	fn_ctx_handle_destroy(ctx);
4537c478bd9Sstevel@tonic-gate 	if (bindings == NULL) {
4547c478bd9Sstevel@tonic-gate 		logstat(status, "list_bindings failed for", map);
4557c478bd9Sstevel@tonic-gate 		goto checkerr_return;
4567c478bd9Sstevel@tonic-gate 	}
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	while ((child_s = fn_bindinglist_next(bindings, &child_ref, status))
4597c478bd9Sstevel@tonic-gate 	    != NULL) {
4607c478bd9Sstevel@tonic-gate 		child = (const char *)fn_string_str(child_s, &statcode);
4617c478bd9Sstevel@tonic-gate 		if (child == NULL) {
4627c478bd9Sstevel@tonic-gate 			if (verbose) {
4637c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
4647c478bd9Sstevel@tonic-gate 				    "FNS string error listing %s", map);
4657c478bd9Sstevel@tonic-gate 			}
4667c478bd9Sstevel@tonic-gate 			fn_string_destroy(child_s);
4677c478bd9Sstevel@tonic-gate 			goto err_return;
4687c478bd9Sstevel@tonic-gate 		}
4697c478bd9Sstevel@tonic-gate 		mapents = frontier_aux(mapents, child_ref, map, maplen,
4707c478bd9Sstevel@tonic-gate 		    mntpnt, child, opts, status);
4717c478bd9Sstevel@tonic-gate 		fn_string_destroy(child_s);
4727c478bd9Sstevel@tonic-gate 		fn_ref_destroy(child_ref);
4737c478bd9Sstevel@tonic-gate 		if (mapents == NULL) {
4747c478bd9Sstevel@tonic-gate 			goto noerr_return;
4757c478bd9Sstevel@tonic-gate 		}
4767c478bd9Sstevel@tonic-gate 	}
4777c478bd9Sstevel@tonic-gate 	if (fn_status_is_success(status)) {
4787c478bd9Sstevel@tonic-gate 		goto noerr_return;
4797c478bd9Sstevel@tonic-gate 	} else {
4807c478bd9Sstevel@tonic-gate 		logstat(status, "error while listing", map);
4817c478bd9Sstevel@tonic-gate 		/* Fall through to checkerr_return. */
4827c478bd9Sstevel@tonic-gate 	}
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate checkerr_return:
4857c478bd9Sstevel@tonic-gate 	if (!transient(status)) {
4867c478bd9Sstevel@tonic-gate 		goto noerr_return;
4877c478bd9Sstevel@tonic-gate 	}
4887c478bd9Sstevel@tonic-gate err_return:
4897c478bd9Sstevel@tonic-gate 	free_mapent(mapents);
4907c478bd9Sstevel@tonic-gate 	mapents = NULL;
4917c478bd9Sstevel@tonic-gate noerr_return:
4927c478bd9Sstevel@tonic-gate 	fn_bindinglist_destroy(bindings XFN1(status));
4937c478bd9Sstevel@tonic-gate 	return (mapents);
4947c478bd9Sstevel@tonic-gate }
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate static mapent *
4987c478bd9Sstevel@tonic-gate frontier_aux(mapent *mapents, const FN_ref_t *ref, char *map, size_t maplen,
4997c478bd9Sstevel@tonic-gate     char *mntpnt, const char *name, char *opts, FN_status_t *status)
5007c478bd9Sstevel@tonic-gate {
5017c478bd9Sstevel@tonic-gate 	addrtype_t	addrtype;
5027c478bd9Sstevel@tonic-gate 	bool_t		at_frontier;
5037c478bd9Sstevel@tonic-gate 	mapent		*me;
5047c478bd9Sstevel@tonic-gate 	size_t		maplen_save = maplen;
5057c478bd9Sstevel@tonic-gate 	char		*cname = map + FNPREFIXLEN + 1;	/* for error msgs */
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	if (reftype(ref) >= NUM_REFTYPES) {
5087c478bd9Sstevel@tonic-gate 		/*
5097c478bd9Sstevel@tonic-gate 		 * We could instead install an indirect autofs mount point
5107c478bd9Sstevel@tonic-gate 		 * here.  That would allow, for example, a user to be bound
5117c478bd9Sstevel@tonic-gate 		 * beneath a file system.
5127c478bd9Sstevel@tonic-gate 		 */
5137c478bd9Sstevel@tonic-gate 		return (mapents);
5147c478bd9Sstevel@tonic-gate 	}
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	/* "map" => "map/name" */
5177c478bd9Sstevel@tonic-gate 	if ((maplen = append_mapname(map, maplen, name)) == 0) {
5187c478bd9Sstevel@tonic-gate 		return (mapents);
5197c478bd9Sstevel@tonic-gate 	}
5207c478bd9Sstevel@tonic-gate 	if (trace > 1) {
5217c478bd9Sstevel@tonic-gate 		trace_prt(1, "  FNS traversal: %s/\n", cname);
5227c478bd9Sstevel@tonic-gate 	}
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	/*
5257c478bd9Sstevel@tonic-gate 	 * If this is an address type that we know how to mount, then
5267c478bd9Sstevel@tonic-gate 	 * we have reached the frontier.
5277c478bd9Sstevel@tonic-gate 	 */
5287c478bd9Sstevel@tonic-gate 	at_frontier = (addr_from_ref(ref, cname, &addrtype, NULL, 0) == 0);
5297c478bd9Sstevel@tonic-gate 	/*
5307c478bd9Sstevel@tonic-gate 	 * For an ADDR_HOST address, treat a non-exported directory as
5317c478bd9Sstevel@tonic-gate 	 * if the address type were not known:  continue searching for
5327c478bd9Sstevel@tonic-gate 	 * exported subdirectories.
5337c478bd9Sstevel@tonic-gate 	 */
5347c478bd9Sstevel@tonic-gate 	if (at_frontier && (addrtype == ADDR_HOST)) {
5357c478bd9Sstevel@tonic-gate 		if (!exported(ref, cname, status)) {
5367c478bd9Sstevel@tonic-gate 			if (transient(status)) {
5377c478bd9Sstevel@tonic-gate 				free_mapent(mapents);
5387c478bd9Sstevel@tonic-gate 				return (NULL);
5397c478bd9Sstevel@tonic-gate 			} else {
5407c478bd9Sstevel@tonic-gate 				at_frontier = FALSE;
5417c478bd9Sstevel@tonic-gate 			}
5427c478bd9Sstevel@tonic-gate 		}
5437c478bd9Sstevel@tonic-gate 	}
5447c478bd9Sstevel@tonic-gate 	/*
5457c478bd9Sstevel@tonic-gate 	 * If we have reached the frontier, install a direct autofs
5467c478bd9Sstevel@tonic-gate 	 * mount point (which will trigger the actual mount if the
5477c478bd9Sstevel@tonic-gate 	 * user steps on it later).  Otherwise, continue traversing
5487c478bd9Sstevel@tonic-gate 	 * the namespace looking for known address types.
5497c478bd9Sstevel@tonic-gate 	 */
5507c478bd9Sstevel@tonic-gate 	if (at_frontier) {
5517c478bd9Sstevel@tonic-gate 		opts = (opts[0] != '\0')
5527c478bd9Sstevel@tonic-gate 		    ? concat(opts, ',', "direct")
5537c478bd9Sstevel@tonic-gate 		    : strdup("direct");
5547c478bd9Sstevel@tonic-gate 		me = new_mapent(noroot, strdup(mntpnt), strdup("autofs"), opts,
5557c478bd9Sstevel@tonic-gate 		    strdup(""), strdup(map));
5567c478bd9Sstevel@tonic-gate 		if (me != NULL) {
5577c478bd9Sstevel@tonic-gate 			/* Link new mapent into list (not at the head). */
5587c478bd9Sstevel@tonic-gate 			me->map_next = mapents->map_next;
5597c478bd9Sstevel@tonic-gate 			mapents->map_next = me;
5607c478bd9Sstevel@tonic-gate 		} else {
5617c478bd9Sstevel@tonic-gate 			free_mapent(mapents);
5627c478bd9Sstevel@tonic-gate 			mapents = NULL;
5637c478bd9Sstevel@tonic-gate 		}
5647c478bd9Sstevel@tonic-gate 	} else {
5657c478bd9Sstevel@tonic-gate 		mapents =
5667c478bd9Sstevel@tonic-gate 		    frontier(mapents, ref, map, maplen, mntpnt, opts, status);
5677c478bd9Sstevel@tonic-gate 	}
5687c478bd9Sstevel@tonic-gate 	map[maplen_save] = '\0';	/* "map/name" => "map" */
5697c478bd9Sstevel@tonic-gate 	return (mapents);
5707c478bd9Sstevel@tonic-gate }
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate static bool_t
5747c478bd9Sstevel@tonic-gate exported(const FN_ref_t *ref, const char *cname, FN_status_t *status)
5757c478bd9Sstevel@tonic-gate {
5767c478bd9Sstevel@tonic-gate 	FN_ctx_t		*ctx;
5777c478bd9Sstevel@tonic-gate 	FN_attribute_t		*attr;
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	ctx = fn_ctx_handle_from_ref(ref, XFN2(0) status);
5807c478bd9Sstevel@tonic-gate 	if (ctx == NULL) {
5817c478bd9Sstevel@tonic-gate 		logstat(status, "from_ref failed for", cname);
5827c478bd9Sstevel@tonic-gate 		return (FALSE);
5837c478bd9Sstevel@tonic-gate 	}
5847c478bd9Sstevel@tonic-gate 	attr = fn_attr_get(ctx, empty_cname, &attr_exported, XFN2(1) status);
5857c478bd9Sstevel@tonic-gate 	fn_ctx_handle_destroy(ctx);
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	switch (fn_status_code(status)) {
5887c478bd9Sstevel@tonic-gate 	case FN_SUCCESS:
5897c478bd9Sstevel@tonic-gate 		fn_attribute_destroy(attr);
5907c478bd9Sstevel@tonic-gate 		break;
5917c478bd9Sstevel@tonic-gate 	case FN_E_NO_SUCH_ATTRIBUTE:
5927c478bd9Sstevel@tonic-gate 		break;
5937c478bd9Sstevel@tonic-gate 	default:
5947c478bd9Sstevel@tonic-gate 		logstat(status, "could not get attributes for", cname);
5957c478bd9Sstevel@tonic-gate 	}
5967c478bd9Sstevel@tonic-gate 	return (attr != NULL);
5977c478bd9Sstevel@tonic-gate }
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate static int
6017c478bd9Sstevel@tonic-gate addr_from_ref(const FN_ref_t *ref, const char *cname, addrtype_t *typep,
6027c478bd9Sstevel@tonic-gate     char *data, size_t datasz)
6037c478bd9Sstevel@tonic-gate {
6047c478bd9Sstevel@tonic-gate 	const FN_ref_addr_t	*addr;
6057c478bd9Sstevel@tonic-gate 	void			*iter_pos;
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	addr = fn_ref_first(ref, &iter_pos);
6087c478bd9Sstevel@tonic-gate 	if (addr == NULL) {
6097c478bd9Sstevel@tonic-gate 		if (verbose) {
6107c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "FNS ref with no address: %s", cname);
6117c478bd9Sstevel@tonic-gate 		}
6127c478bd9Sstevel@tonic-gate 		return (-1);
6137c478bd9Sstevel@tonic-gate 	}
6147c478bd9Sstevel@tonic-gate 	while (addr != NULL) {
6157c478bd9Sstevel@tonic-gate 		*typep = addrtype(addr);
6167c478bd9Sstevel@tonic-gate 		if (*typep < NUM_ADDRTYPES) {
6177c478bd9Sstevel@tonic-gate 			return ((data != NULL)
6187c478bd9Sstevel@tonic-gate 			    ? str_from_addr(cname, addr, data, datasz)
6197c478bd9Sstevel@tonic-gate 			    : 0);
6207c478bd9Sstevel@tonic-gate 		}
6217c478bd9Sstevel@tonic-gate 		addr = fn_ref_next(ref, &iter_pos);
6227c478bd9Sstevel@tonic-gate 	}
6237c478bd9Sstevel@tonic-gate 	return (-1);
6247c478bd9Sstevel@tonic-gate }
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate static int
6287c478bd9Sstevel@tonic-gate str_from_addr(const char *cname, const FN_ref_addr_t *addr, char str[],
6297c478bd9Sstevel@tonic-gate     size_t strsz)
6307c478bd9Sstevel@tonic-gate {
6317c478bd9Sstevel@tonic-gate 	XDR	xdr;
6327c478bd9Sstevel@tonic-gate 	int	res;
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	xdrmem_create(&xdr, (caddr_t)fn_ref_addr_data(addr),
6357c478bd9Sstevel@tonic-gate 	    fn_ref_addr_length(addr), XDR_DECODE);
6367c478bd9Sstevel@tonic-gate 	if (!xdr_string(&xdr, &str, strsz)) {
6377c478bd9Sstevel@tonic-gate 		if (verbose) {
6387c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
6397c478bd9Sstevel@tonic-gate 			    "Could not decode FNS address for %s", cname);
6407c478bd9Sstevel@tonic-gate 		}
6417c478bd9Sstevel@tonic-gate 		res = -1;
6427c478bd9Sstevel@tonic-gate 	} else {
6437c478bd9Sstevel@tonic-gate 		res = 0;
6447c478bd9Sstevel@tonic-gate 	}
6457c478bd9Sstevel@tonic-gate 	xdr_destroy(&xdr);
6467c478bd9Sstevel@tonic-gate 	return (res);
6477c478bd9Sstevel@tonic-gate }
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate static size_t
6507c478bd9Sstevel@tonic-gate append_mapname(char *map, size_t maplen, const char *name)
6517c478bd9Sstevel@tonic-gate {
6527c478bd9Sstevel@tonic-gate 	size_t namelen = strlen(name);
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	if (maplen + 1 + namelen >= MAPNAMESZ) {
6557c478bd9Sstevel@tonic-gate 		if (verbose) {
6567c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "FNS name %s/%s too long",
6577c478bd9Sstevel@tonic-gate 			    map + FNPREFIXLEN + 1, name);
6587c478bd9Sstevel@tonic-gate 		}
6597c478bd9Sstevel@tonic-gate 		return (0);
6607c478bd9Sstevel@tonic-gate 	}
6617c478bd9Sstevel@tonic-gate 	sprintf(map + maplen, "/%s", name);
6627c478bd9Sstevel@tonic-gate 	return (maplen + 1 + namelen);
6637c478bd9Sstevel@tonic-gate }
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate static char *
6677c478bd9Sstevel@tonic-gate concat(const char *s1, char sep, const char *s2)
6687c478bd9Sstevel@tonic-gate {
6697c478bd9Sstevel@tonic-gate 	char *s = malloc(strlen(s1) + 1 + strlen(s2) + 1);
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	if (s != NULL) {
6727c478bd9Sstevel@tonic-gate 		sprintf(s, "%s%c%s", s1, sep, s2);
6737c478bd9Sstevel@tonic-gate 	}
6747c478bd9Sstevel@tonic-gate 	return (s);
6757c478bd9Sstevel@tonic-gate }
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate static bool_t
6797c478bd9Sstevel@tonic-gate safe_mapent(mapent *me)
6807c478bd9Sstevel@tonic-gate {
6817c478bd9Sstevel@tonic-gate 	char	*opts;
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	if (me->map_next != NULL) {
6847c478bd9Sstevel@tonic-gate 		/* Multiple mounts don't belong in XFN namespace. */
6857c478bd9Sstevel@tonic-gate 		return (NULL);
6867c478bd9Sstevel@tonic-gate 	}
6877c478bd9Sstevel@tonic-gate 	opts = me->map_mntopts;
6887c478bd9Sstevel@tonic-gate 	me->map_mntopts = safe_opts(opts);
6897c478bd9Sstevel@tonic-gate 	free(opts);
6907c478bd9Sstevel@tonic-gate 	return (me->map_mntopts != NULL);
6917c478bd9Sstevel@tonic-gate }
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate static char *
6957c478bd9Sstevel@tonic-gate safe_opts(const char *opts)
6967c478bd9Sstevel@tonic-gate {
6977c478bd9Sstevel@tonic-gate 	char	*start;
6987c478bd9Sstevel@tonic-gate 	size_t	len;
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	if (opts[0] == '\0') {
7017c478bd9Sstevel@tonic-gate 		return (strdup(MNTOPT_NOSUID));
7027c478bd9Sstevel@tonic-gate 	}
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	/* A quick-and-dirty check to see if "nosuid" is already there. */
7057c478bd9Sstevel@tonic-gate 	start = strstr(opts, MNTOPT_NOSUID);
7067c478bd9Sstevel@tonic-gate 	len = sizeof (MNTOPT_NOSUID) - 1;	/* "-1" for trailing '\0' */
7077c478bd9Sstevel@tonic-gate 	if (start != NULL) {
7087c478bd9Sstevel@tonic-gate 		while (start > opts && isspace(*(start - 1))) {
7097c478bd9Sstevel@tonic-gate 			start--;
7107c478bd9Sstevel@tonic-gate 		}
7117c478bd9Sstevel@tonic-gate 		if ((start == opts || *(start - 1) == ',') &&
7127c478bd9Sstevel@tonic-gate 		    opts[len] == ',' || opts[len] == '\0') {
7137c478bd9Sstevel@tonic-gate 			return (strdup(opts));
7147c478bd9Sstevel@tonic-gate 		}
7157c478bd9Sstevel@tonic-gate 	}
7167c478bd9Sstevel@tonic-gate 	return (concat(opts, ',', MNTOPT_NOSUID));
7177c478bd9Sstevel@tonic-gate }
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate static int
7217c478bd9Sstevel@tonic-gate trim_line(mapline *ml)
7227c478bd9Sstevel@tonic-gate {
7237c478bd9Sstevel@tonic-gate 	char	*end;	/* pointer to '\0' at end of linebuf */
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	end = ml->linebuf + strcspn(ml->linebuf, "#");
7267c478bd9Sstevel@tonic-gate 	while ((end > ml->linebuf) && isspace(end[-1])) {
7277c478bd9Sstevel@tonic-gate 		end--;
7287c478bd9Sstevel@tonic-gate 	}
7297c478bd9Sstevel@tonic-gate 	if (end <= ml->linebuf) {
7307c478bd9Sstevel@tonic-gate 		return (-1);
7317c478bd9Sstevel@tonic-gate 	}
7327c478bd9Sstevel@tonic-gate 	*end = '\0';
7337c478bd9Sstevel@tonic-gate 	unquote(ml->linebuf, ml->lineqbuf);
7347c478bd9Sstevel@tonic-gate 	return (0);
7357c478bd9Sstevel@tonic-gate }
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate static bool_t
7397c478bd9Sstevel@tonic-gate opts_only(const mapline *ml)
7407c478bd9Sstevel@tonic-gate {
7417c478bd9Sstevel@tonic-gate 	const char *s = ml->linebuf;
7427c478bd9Sstevel@tonic-gate 	const char *q = ml->lineqbuf;
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	if (*s != '-') {
7457c478bd9Sstevel@tonic-gate 		return (FALSE);
7467c478bd9Sstevel@tonic-gate 	}
7477c478bd9Sstevel@tonic-gate 	for (; *s != '\0'; s++, q++) {
7487c478bd9Sstevel@tonic-gate 		if (isspace(*s) && (*q == ' ')) {
7497c478bd9Sstevel@tonic-gate 			return (FALSE);
7507c478bd9Sstevel@tonic-gate 		}
7517c478bd9Sstevel@tonic-gate 	}
7527c478bd9Sstevel@tonic-gate 	return (TRUE);
7537c478bd9Sstevel@tonic-gate }
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate static mapent *
7577c478bd9Sstevel@tonic-gate new_mapent(char *root, char *mntpnt, char *fstype, char *mntopts, char *host,
7587c478bd9Sstevel@tonic-gate     char *dir)
7597c478bd9Sstevel@tonic-gate {
7607c478bd9Sstevel@tonic-gate 	mapent		*me;
7617c478bd9Sstevel@tonic-gate 	struct mapfs	*mfs;
7627c478bd9Sstevel@tonic-gate 	char		*mounter = NULL;
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	me = calloc(1, sizeof (*me));
7657c478bd9Sstevel@tonic-gate 	mfs = calloc(1, sizeof (*mfs));
7667c478bd9Sstevel@tonic-gate 	if (fstype != NULL) {
7677c478bd9Sstevel@tonic-gate 		mounter = strdup(fstype);
7687c478bd9Sstevel@tonic-gate 	}
7697c478bd9Sstevel@tonic-gate 	if ((mntpnt == NULL) || (fstype == NULL) || (mntopts == NULL) ||
7707c478bd9Sstevel@tonic-gate 	    (host == NULL) || (dir == NULL) || (me == NULL) || (mfs == NULL) ||
7717c478bd9Sstevel@tonic-gate 	    (mounter == NULL) || (root == NULL)) {
7727c478bd9Sstevel@tonic-gate 		log_mem_failure();
7737c478bd9Sstevel@tonic-gate 		free(me);
7747c478bd9Sstevel@tonic-gate 		free(mfs);
7757c478bd9Sstevel@tonic-gate 		free(mounter);
7767c478bd9Sstevel@tonic-gate 		free(root);
7777c478bd9Sstevel@tonic-gate 		free(mntpnt);
7787c478bd9Sstevel@tonic-gate 		free(fstype);
7797c478bd9Sstevel@tonic-gate 		free(mntopts);
7807c478bd9Sstevel@tonic-gate 		free(host);
7817c478bd9Sstevel@tonic-gate 		free(dir);
7827c478bd9Sstevel@tonic-gate 		return (NULL);
7837c478bd9Sstevel@tonic-gate 	}
7847c478bd9Sstevel@tonic-gate 	me->map_root	= (root != noroot) ? root : NULL;
7857c478bd9Sstevel@tonic-gate 	me->map_fstype	= fstype;
7867c478bd9Sstevel@tonic-gate 	me->map_mounter	= mounter;
7877c478bd9Sstevel@tonic-gate 	me->map_mntpnt	= mntpnt;
7887c478bd9Sstevel@tonic-gate 	me->map_mntopts	= mntopts;
7897c478bd9Sstevel@tonic-gate 	me->map_fsw	= NULL;
7907c478bd9Sstevel@tonic-gate 	me->map_fswq    = NULL;
7917c478bd9Sstevel@tonic-gate 	me->map_fs	= mfs;
7927c478bd9Sstevel@tonic-gate 	mfs->mfs_host	= host;
7937c478bd9Sstevel@tonic-gate 	mfs->mfs_dir	= dir;
7947c478bd9Sstevel@tonic-gate 	me->map_mntlevel = -1;
7957c478bd9Sstevel@tonic-gate 	me->map_modified = FALSE;
7967c478bd9Sstevel@tonic-gate 	me->map_faked = FALSE;
7977c478bd9Sstevel@tonic-gate 	me->map_err = 0;		/* MAPENT_NOERR */
7987c478bd9Sstevel@tonic-gate 	return (me);
7997c478bd9Sstevel@tonic-gate }
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate #ifndef XFN1ENV
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate /*
8057c478bd9Sstevel@tonic-gate  * User-relative bindings in the initial context, and the leading components
8067c478bd9Sstevel@tonic-gate  * of their non-user-relative equivalents.  Leading components are listed in
8077c478bd9Sstevel@tonic-gate  * the order in which they should be tried.  Each list is NULL-terminated
8087c478bd9Sstevel@tonic-gate  * (the compiler generously does this for us).
8097c478bd9Sstevel@tonic-gate  * For "myorgunit", for example, we first check if it is equivalent to
8107c478bd9Sstevel@tonic-gate  * "thisorgunit".  If not, we translate it into "org/<something>".
8117c478bd9Sstevel@tonic-gate  */
8127c478bd9Sstevel@tonic-gate #define	MAX_LEADS 3
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate static struct {
8157c478bd9Sstevel@tonic-gate 	const char	*binding;
8167c478bd9Sstevel@tonic-gate 	const char	*leads[MAX_LEADS + 1];
8177c478bd9Sstevel@tonic-gate } user_rel[] = {
8187c478bd9Sstevel@tonic-gate 	{"thisuser",	{"user", "thisorgunit", "org"}},
8197c478bd9Sstevel@tonic-gate 	{"myself",	{"user", "thisorgunit", "org"}},
8207c478bd9Sstevel@tonic-gate 	{"_myself",	{"_user", "_thisorgunit", "_orgunit"}},
8217c478bd9Sstevel@tonic-gate 	{"myorgunit",	{"thisorgunit", "org"}},
8227c478bd9Sstevel@tonic-gate 	{"_myorgunit",	{"_thisorgunit", "_orgunit"}},
8237c478bd9Sstevel@tonic-gate 	{"myens",	{"thisens"}},
8247c478bd9Sstevel@tonic-gate 	{"_myens",	{"_thisens"}}
8257c478bd9Sstevel@tonic-gate };
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate static bool_t
8297c478bd9Sstevel@tonic-gate is_user_relative(const char *cname)
8307c478bd9Sstevel@tonic-gate {
8317c478bd9Sstevel@tonic-gate 	int	i;
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (user_rel) / sizeof (user_rel[0]); i++) {
8347c478bd9Sstevel@tonic-gate 		if (strcmp(cname, user_rel[i].binding) == 0) {
8357c478bd9Sstevel@tonic-gate 			return (TRUE);
8367c478bd9Sstevel@tonic-gate 		}
8377c478bd9Sstevel@tonic-gate 	}
8387c478bd9Sstevel@tonic-gate 	return (FALSE);
8397c478bd9Sstevel@tonic-gate }
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate static char *
8437c478bd9Sstevel@tonic-gate equiv_name(FN_ctx_t *ctx, const char *cname, FN_status_t *status)
8447c478bd9Sstevel@tonic-gate {
8457c478bd9Sstevel@tonic-gate 	FN_composite_name_t	*name;
8467c478bd9Sstevel@tonic-gate 	FN_string_t		*leading_name;
8477c478bd9Sstevel@tonic-gate 	FN_composite_name_t	*equiv;
8487c478bd9Sstevel@tonic-gate 	FN_string_t		*equiv_string;
8497c478bd9Sstevel@tonic-gate 	const char		*equiv_str;
8507c478bd9Sstevel@tonic-gate 	char			*equiv_str_dup;
8517c478bd9Sstevel@tonic-gate 	const char		**leads;
8527c478bd9Sstevel@tonic-gate 	unsigned int		stat;
8537c478bd9Sstevel@tonic-gate 	int			i;
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (user_rel) / sizeof (user_rel[0]); i++) {
8567c478bd9Sstevel@tonic-gate 		if (strcmp(cname, user_rel[i].binding) == 0) {
8577c478bd9Sstevel@tonic-gate 			break;
8587c478bd9Sstevel@tonic-gate 		}
8597c478bd9Sstevel@tonic-gate 	}
8607c478bd9Sstevel@tonic-gate 	if ((name = new_cname(cname)) == NULL) {
8617c478bd9Sstevel@tonic-gate 		return (NULL);
8627c478bd9Sstevel@tonic-gate 	}
8637c478bd9Sstevel@tonic-gate 	leads = user_rel[i].leads;	/* array of leading names to try */
8647c478bd9Sstevel@tonic-gate 	do {
8657c478bd9Sstevel@tonic-gate 		leading_name = fn_string_from_str((unsigned char *)*leads);
8667c478bd9Sstevel@tonic-gate 		if (leading_name == NULL) {
8677c478bd9Sstevel@tonic-gate 			log_mem_failure();
8687c478bd9Sstevel@tonic-gate 			fn_composite_name_destroy(name);
8697c478bd9Sstevel@tonic-gate 			return (NULL);
8707c478bd9Sstevel@tonic-gate 		}
8717c478bd9Sstevel@tonic-gate 		equiv = prelim_fn_ctx_equivalent_name(ctx, name, leading_name,
8727c478bd9Sstevel@tonic-gate 		    status);
8737c478bd9Sstevel@tonic-gate 		fn_string_destroy(leading_name);
8747c478bd9Sstevel@tonic-gate 	} while (equiv == NULL && *++leads != NULL);
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 	fn_composite_name_destroy(name);
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 	if (equiv == NULL) {
8797c478bd9Sstevel@tonic-gate 		if (transient(status)) {
8807c478bd9Sstevel@tonic-gate 			logstat(status, "could not find equivalent of", cname);
8817c478bd9Sstevel@tonic-gate 		}
8827c478bd9Sstevel@tonic-gate 		return (NULL);
8837c478bd9Sstevel@tonic-gate 	}
8847c478bd9Sstevel@tonic-gate 	equiv_string = fn_string_from_composite_name(equiv, &stat);
8857c478bd9Sstevel@tonic-gate 	fn_composite_name_destroy(equiv);
8867c478bd9Sstevel@tonic-gate 	if (equiv_string == NULL) {
8877c478bd9Sstevel@tonic-gate 		log_mem_failure();
8887c478bd9Sstevel@tonic-gate 		return (NULL);
8897c478bd9Sstevel@tonic-gate 	}
8907c478bd9Sstevel@tonic-gate 	equiv_str = (const char *)fn_string_str(equiv_string, &stat);
8917c478bd9Sstevel@tonic-gate 	if (equiv_str == NULL ||
8927c478bd9Sstevel@tonic-gate 	    (equiv_str_dup = strdup(equiv_str)) == NULL) {
8937c478bd9Sstevel@tonic-gate 		log_mem_failure();
8947c478bd9Sstevel@tonic-gate 		fn_string_destroy(equiv_string);
8957c478bd9Sstevel@tonic-gate 		return (NULL);
8967c478bd9Sstevel@tonic-gate 	}
8977c478bd9Sstevel@tonic-gate 	fn_string_destroy(equiv_string);
8987c478bd9Sstevel@tonic-gate 	return (equiv_str_dup);
8997c478bd9Sstevel@tonic-gate }
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate #endif	/* XFN1ENV */
902