xref: /onnv-gate/usr/src/cmd/fs.d/autofs/ns_fnmount.c (revision 11262:b7ebfbf2359e)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*11262SRajagopal.Andra@Sun.COM  * Common Development and Distribution License (the "License").
6*11262SRajagopal.Andra@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*11262SRajagopal.Andra@Sun.COM  * ns_fnmount.c
230Sstevel@tonic-gate  *
24*11262SRajagopal.Andra@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
25*11262SRajagopal.Andra@Sun.COM  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <stdio.h>
290Sstevel@tonic-gate #include <stdlib.h>
300Sstevel@tonic-gate #include <string.h>
310Sstevel@tonic-gate #include <ctype.h>
320Sstevel@tonic-gate #include <syslog.h>
330Sstevel@tonic-gate #include <rpc/rpc.h>
340Sstevel@tonic-gate #include <rpcsvc/nis.h>
350Sstevel@tonic-gate #include <xfn/xfn.h>
360Sstevel@tonic-gate #include "automount.h"
370Sstevel@tonic-gate #include "ns_fnutils.h"
380Sstevel@tonic-gate 
390Sstevel@tonic-gate 
400Sstevel@tonic-gate /*
410Sstevel@tonic-gate  * The maximum sizes of map names, key names, composite names, and status
420Sstevel@tonic-gate  * descriptions, including the trailing '\0'.
430Sstevel@tonic-gate  */
440Sstevel@tonic-gate #define	MAPNAMESZ	(size_t)(AUTOFS_MAXCOMPONENTLEN + 1)
450Sstevel@tonic-gate #define	KEYNAMESZ	(size_t)(AUTOFS_MAXCOMPONENTLEN + 1)
460Sstevel@tonic-gate #define	COMPNAMESZ	(size_t)(MAPNAMESZ - FNPREFIXLEN + KEYNAMESZ - 2)
470Sstevel@tonic-gate #define	DESCSZ		(size_t)512
480Sstevel@tonic-gate 
490Sstevel@tonic-gate typedef struct mapent	mapent;
500Sstevel@tonic-gate typedef struct mapline	mapline;
510Sstevel@tonic-gate 
520Sstevel@tonic-gate 
530Sstevel@tonic-gate /*
540Sstevel@tonic-gate  * The name of an attribute.
550Sstevel@tonic-gate  */
560Sstevel@tonic-gate static const FN_identifier_t attr_exported = {FN_ID_STRING, 8, "exported"};
570Sstevel@tonic-gate 
580Sstevel@tonic-gate 
590Sstevel@tonic-gate /*
600Sstevel@tonic-gate  * Given a request by a particular user to mount the name "key" under
610Sstevel@tonic-gate  * map/context "map", and a set of default mount options, return (in
620Sstevel@tonic-gate  * "res") either a list of mapents giving the mounts that need to be
630Sstevel@tonic-gate  * performed, or a symbolic link to be created for a user-relative
640Sstevel@tonic-gate  * context.  If "shallow" is true return, in place of the list of
650Sstevel@tonic-gate  * mapents, a single mapent representing an indirect mount point.
660Sstevel@tonic-gate  *
670Sstevel@tonic-gate  *	void
680Sstevel@tonic-gate  *	getmapent_fn(char *key, char *map, char *opts, uid_t uid,
690Sstevel@tonic-gate  *	      bool_t shallow, getmapent_fn_res *res);
700Sstevel@tonic-gate  */
710Sstevel@tonic-gate 
720Sstevel@tonic-gate /*
730Sstevel@tonic-gate  * Given a reference, its composite name, default mount options, and a
740Sstevel@tonic-gate  * mapent root, return a list of mapents to mount.  If "shallow" is
750Sstevel@tonic-gate  * true return, in place of the list of mapents, a single mapent
760Sstevel@tonic-gate  * representing an indirect mount point.  The map and key strings are
770Sstevel@tonic-gate  * pieces of the composite name such that:
780Sstevel@tonic-gate  * "FNPREFIX/cname" == "map/key".
790Sstevel@tonic-gate  */
800Sstevel@tonic-gate static mapent *
810Sstevel@tonic-gate process_ref(const FN_ref_t *ref, const char *cname, char *map, char *key,
820Sstevel@tonic-gate     char *opts, char *root, bool_t shallow, FN_status_t *status);
830Sstevel@tonic-gate 
840Sstevel@tonic-gate /*
850Sstevel@tonic-gate  * Traverse the namespace to find a frontier below ref along which
860Sstevel@tonic-gate  * future mounts may need to be triggered.  Add to mapents the
870Sstevel@tonic-gate  * corresponding direct autofs mount points.
880Sstevel@tonic-gate  *     map:	map name for ref
890Sstevel@tonic-gate  *     maplen:	strlen(map)
900Sstevel@tonic-gate  *     mntpnt:	suffix of map where the current mount request begins
910Sstevel@tonic-gate  *		(starts off as "", and grows as we traverse the namespace)
920Sstevel@tonic-gate  *     opts:	default mount options
930Sstevel@tonic-gate  *     status:	passed from above to avoid having to allocate one on each call
940Sstevel@tonic-gate  * Works by calling frontier_aux() on each name bound under ref.
950Sstevel@tonic-gate  * Return the new mapents, or free mapents and return NULL on failure.
960Sstevel@tonic-gate  */
970Sstevel@tonic-gate static mapent *
980Sstevel@tonic-gate frontier(mapent *mapents, const FN_ref_t *ref, char *map, size_t maplen,
990Sstevel@tonic-gate     char *mntpnt, char *opts, FN_status_t *status);
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate /*
1020Sstevel@tonic-gate  * Called by frontier(), once for each "name" that it finds.  map is
1030Sstevel@tonic-gate  * passed unchanged from frontier().  ref is the reference named by
1040Sstevel@tonic-gate  * "map/name".  If ref is found to be along the frontier, add the
1050Sstevel@tonic-gate  * corresponding direct autofs mount point to mapents.  Otherwise
1060Sstevel@tonic-gate  * continue traversing the namespace to find the frontier.  Other
1070Sstevel@tonic-gate  * arguments and the return value are as for frontier().
1080Sstevel@tonic-gate  */
1090Sstevel@tonic-gate static mapent *
1100Sstevel@tonic-gate frontier_aux(mapent *mapents, const FN_ref_t *ref, char *map, size_t maplen,
1110Sstevel@tonic-gate     char *mntpnt, const char *name, char *opts, FN_status_t *status);
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate /*
1140Sstevel@tonic-gate  * Given a reference with an address type of ADDR_HOST and its
1150Sstevel@tonic-gate  * composite name, check the attr_exported attribute to determine if
1160Sstevel@tonic-gate  * the corresponding directory is exported.  Return FALSE on error.
1170Sstevel@tonic-gate  */
1180Sstevel@tonic-gate static bool_t
1190Sstevel@tonic-gate exported(const FN_ref_t *ref, const char *cname, FN_status_t *status);
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate /*
1220Sstevel@tonic-gate  * Find a reference's address type and, if "data" is not NULL, its
1230Sstevel@tonic-gate  * data string.  If there is no address of a known type, set *typep to
1240Sstevel@tonic-gate  * NUM_ADDRTYPES; if there are several, stop after finding the first.
1250Sstevel@tonic-gate  * Return 0 on success.
1260Sstevel@tonic-gate  */
1270Sstevel@tonic-gate static int
1280Sstevel@tonic-gate addr_from_ref(const FN_ref_t *ref, const char *cname, addrtype_t *typep,
1290Sstevel@tonic-gate     char *data, size_t datasz);
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate /*
1320Sstevel@tonic-gate  * Decode an address's data into a string.  Return 0 on success.
1330Sstevel@tonic-gate  */
1340Sstevel@tonic-gate static int
1350Sstevel@tonic-gate str_from_addr(const char *cname, const FN_ref_addr_t *addr, char str[],
1360Sstevel@tonic-gate     size_t strsz);
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate /*
1390Sstevel@tonic-gate  * Given a map name and its current length, append "/name".  Return
1400Sstevel@tonic-gate  * the new length.  On error, syslog a warning and return 0.
1410Sstevel@tonic-gate  */
1420Sstevel@tonic-gate static size_t
1430Sstevel@tonic-gate append_mapname(char *map, size_t maplen, const char *name);
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate /*
1460Sstevel@tonic-gate  * Concatenate two strings using the given separator.  The result is a
1470Sstevel@tonic-gate  * newly-allocated string, or NULL on error.
1480Sstevel@tonic-gate  */
1490Sstevel@tonic-gate static char *
1500Sstevel@tonic-gate concat(const char *s1, char sep, const char *s2);
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate /*
1530Sstevel@tonic-gate  * Add the "nosuid" option to a mapent.  Also check for a sneaky
1540Sstevel@tonic-gate  * hacker trying to override this option by manually inserting a
1550Sstevel@tonic-gate  * multiple mount entry into the XFN namespace.  Return FALSE on error.
1560Sstevel@tonic-gate  */
1570Sstevel@tonic-gate static bool_t
1580Sstevel@tonic-gate safe_mapent(mapent *me);
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate /*
1610Sstevel@tonic-gate  * Append "nosuid" to a list of options.  The result is a
1620Sstevel@tonic-gate  * newly-allocated string, or NULL on error.
1630Sstevel@tonic-gate  */
1640Sstevel@tonic-gate static char *
1650Sstevel@tonic-gate safe_opts(const char *opts);
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate /*
1680Sstevel@tonic-gate  * Trim comments and trailing whitespace from ml->linebuf, then
1690Sstevel@tonic-gate  * unquote it and leave the result in ml.  Return 0 on success.
1700Sstevel@tonic-gate  */
1710Sstevel@tonic-gate static int
1720Sstevel@tonic-gate trim_line(mapline *ml);
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate /*
1750Sstevel@tonic-gate  * Determine whether ml contains an option string (such as "-ro") and
1760Sstevel@tonic-gate  * nothing else.
1770Sstevel@tonic-gate  */
1780Sstevel@tonic-gate static bool_t
1790Sstevel@tonic-gate opts_only(const mapline *ml);
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate /*
1820Sstevel@tonic-gate  * Allocate a new mapent structure.  The arguments must have been
1830Sstevel@tonic-gate  * malloc'ed, and are owned by the mapent; they are freed if
1840Sstevel@tonic-gate  * new_mapent() fails.  If any argument is NULL, the call fails and a
1850Sstevel@tonic-gate  * memory allocation failure is logged.  A root argument of 'noroot'
1860Sstevel@tonic-gate  * indicates that the map_root field does not need to be set (it's
1870Sstevel@tonic-gate  * only needed in the first of a list of mapents).
1880Sstevel@tonic-gate  */
1890Sstevel@tonic-gate static char *noroot = "[no root]";
1900Sstevel@tonic-gate static mapent *
1910Sstevel@tonic-gate new_mapent(char *root, char *mntpnt, char *fstype, char *mntopts, char *host,
1920Sstevel@tonic-gate     char *dir);
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate /*
1950Sstevel@tonic-gate  * Determine whether cname is a user-relative binding -- such as "myself" --
1960Sstevel@tonic-gate  * in the initial context.
1970Sstevel@tonic-gate  */
1980Sstevel@tonic-gate static bool_t
1990Sstevel@tonic-gate is_user_relative(const char *cname);
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate /*
2020Sstevel@tonic-gate  * Given the name of a user-relative binding, return an equivalent
2030Sstevel@tonic-gate  * name that is not user-relative.
2040Sstevel@tonic-gate  */
2050Sstevel@tonic-gate static char *
2060Sstevel@tonic-gate equiv_name(FN_ctx_t *, const char *cname, FN_status_t *);
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate void
getmapent_fn(char * key,char * map,char * opts,uid_t uid,bool_t shallow,getmapent_fn_res * res)2090Sstevel@tonic-gate getmapent_fn(char *key, char *map, char *opts, uid_t uid, bool_t shallow,
2100Sstevel@tonic-gate     getmapent_fn_res *res)
2110Sstevel@tonic-gate {
2120Sstevel@tonic-gate 	size_t			maplen;
2130Sstevel@tonic-gate 	FN_status_t		*status;
2140Sstevel@tonic-gate 	FN_ctx_t		*init_ctx = NULL;
2150Sstevel@tonic-gate 	int			statcode;
2160Sstevel@tonic-gate 	char			cname[COMPNAMESZ];
2170Sstevel@tonic-gate 	FN_composite_name_t	*compname;
2180Sstevel@tonic-gate 	FN_ref_t		*ref;
2190Sstevel@tonic-gate 	char			mapname[MAPNAMESZ];
2200Sstevel@tonic-gate 	char			*root;
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	res->type = FN_NONE;
2230Sstevel@tonic-gate 	res->m_or_l.mapents = NULL;
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	if (init_fn() != 0) {
2260Sstevel@tonic-gate 		return;
2270Sstevel@tonic-gate 	}
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	/*
2300Sstevel@tonic-gate 	 * For direct mounts, the key is the entire path, and the map
2310Sstevel@tonic-gate 	 * name already has the final key component appended.  Split
2320Sstevel@tonic-gate 	 * apart the map name and key.  The "root" of the mapent is
2330Sstevel@tonic-gate 	 * "/key" for indirect mounts, and "" for direct mounts.
2340Sstevel@tonic-gate 	 */
2350Sstevel@tonic-gate 	strcpy(mapname, map);
2360Sstevel@tonic-gate 	if (key[0] == '/') {
2370Sstevel@tonic-gate 		key = strrchr(key, '/') + 1;
2380Sstevel@tonic-gate 		*strrchr(mapname, '/') = '\0';
2390Sstevel@tonic-gate 		root = strdup("");
2400Sstevel@tonic-gate 	} else {
2410Sstevel@tonic-gate 		root = concat("", '/', key);
2420Sstevel@tonic-gate 	}
2430Sstevel@tonic-gate 	map = mapname;
2440Sstevel@tonic-gate 	maplen = strlen(map);
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	if ((maplen - FNPREFIXLEN + strlen(key)) >= COMPNAMESZ) {
2470Sstevel@tonic-gate 		if (verbose) {
2480Sstevel@tonic-gate 			syslog(LOG_ERR, "name %s/%s too long", map, key);
2490Sstevel@tonic-gate 		}
2500Sstevel@tonic-gate 		return;
2510Sstevel@tonic-gate 	}
2520Sstevel@tonic-gate 	if (maplen == FNPREFIXLEN) {
2530Sstevel@tonic-gate 		strcpy(cname, key);
2540Sstevel@tonic-gate 	} else {
2550Sstevel@tonic-gate 		sprintf(cname, "%s/%s", map + FNPREFIXLEN + 1, key);
2560Sstevel@tonic-gate 	}
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	status = fn_status_create();
2590Sstevel@tonic-gate 	if (status == NULL) {
2600Sstevel@tonic-gate 		if (verbose) {
2610Sstevel@tonic-gate 			syslog(LOG_ERR, "Could not create FNS status object");
2620Sstevel@tonic-gate 		}
2630Sstevel@tonic-gate 		return;
2640Sstevel@tonic-gate 	}
2650Sstevel@tonic-gate 	init_ctx = _fn_ctx_handle_from_initial_with_uid(uid, 0, status);
2660Sstevel@tonic-gate 	if (init_ctx == NULL) {
2670Sstevel@tonic-gate 		logstat(status, "", "No initial context");
2680Sstevel@tonic-gate 		goto done;
2690Sstevel@tonic-gate 	}
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate #ifndef XFN1ENV
2720Sstevel@tonic-gate 	if (is_user_relative(cname)) {
2730Sstevel@tonic-gate 		res->type = FN_SYMLINK;
2740Sstevel@tonic-gate 		res->m_or_l.symlink = equiv_name(init_ctx, cname, status);
2750Sstevel@tonic-gate 		goto done;
2760Sstevel@tonic-gate 	}
2770Sstevel@tonic-gate #endif
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	if ((compname = new_cname(cname)) == NULL) {
2800Sstevel@tonic-gate 		goto done;
2810Sstevel@tonic-gate 	}
2820Sstevel@tonic-gate 	ref = fn_ctx_lookup(init_ctx, compname, status);
2830Sstevel@tonic-gate 	statcode = fn_status_code(status);
2840Sstevel@tonic-gate 	fn_composite_name_destroy(compname);
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	if (trace > 1 && !shallow) {
2870Sstevel@tonic-gate 		trace_prt(1, "  FNS traversal: %s\n", cname);
2880Sstevel@tonic-gate 	}
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	if (ref == NULL) {
2910Sstevel@tonic-gate 		if ((statcode != FN_E_NAME_NOT_FOUND) &&
2920Sstevel@tonic-gate 		    (statcode != FN_E_NOT_A_CONTEXT)) {
2930Sstevel@tonic-gate 			logstat(status, "lookup failed on", cname);
2940Sstevel@tonic-gate 		}
2950Sstevel@tonic-gate 		goto done;
2960Sstevel@tonic-gate 	}
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	res->type = FN_MAPENTS;
2990Sstevel@tonic-gate 	res->m_or_l.mapents =
3000Sstevel@tonic-gate 	    process_ref(ref, cname, map, key, opts, root, shallow, status);
3010Sstevel@tonic-gate 	fn_ref_destroy(ref);
3020Sstevel@tonic-gate done:
3030Sstevel@tonic-gate 	fn_ctx_handle_destroy(init_ctx);
3040Sstevel@tonic-gate 	fn_status_destroy(status);
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate static mapent *
process_ref(const FN_ref_t * ref,const char * cname,char * map,char * key,char * opts,char * root,bool_t shallow,FN_status_t * status)3090Sstevel@tonic-gate process_ref(const FN_ref_t *ref, const char *cname, char *map, char *key,
3100Sstevel@tonic-gate     char *opts, char *root, bool_t shallow, FN_status_t *status)
3110Sstevel@tonic-gate {
3120Sstevel@tonic-gate 	addrtype_t	addrtype;
3130Sstevel@tonic-gate 	mapline		ml;
3140Sstevel@tonic-gate 	char		*addrdata = ml.linebuf;
3150Sstevel@tonic-gate 	mapent		*mapents;
3160Sstevel@tonic-gate 	bool_t		self;
3170Sstevel@tonic-gate 	char		*homedir;
3180Sstevel@tonic-gate 	size_t		maplen;
3190Sstevel@tonic-gate 	char		*colon;
3200Sstevel@tonic-gate 	char		*nfshost;
3210Sstevel@tonic-gate 	char		*nfsdir;
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	if ((reftype(ref) < NUM_REFTYPES) &&
3240Sstevel@tonic-gate 	    (addr_from_ref(ref, cname, &addrtype, addrdata, LINESZ) == 0)) {
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 		switch (addrtype) {
3270Sstevel@tonic-gate 		case ADDR_MOUNT:
3280Sstevel@tonic-gate 			if (trim_line(&ml) != 0) {
3290Sstevel@tonic-gate 				return (NULL);
3300Sstevel@tonic-gate 			}
3310Sstevel@tonic-gate 			if (opts_only(&ml)) {
3320Sstevel@tonic-gate 				/* parse_entry() can't handle such lines */
3330Sstevel@tonic-gate 				if (macro_expand("&", ml.linebuf,
3340Sstevel@tonic-gate 				    ml.lineqbuf, LINESZ)) {
3350Sstevel@tonic-gate 					syslog(LOG_ERR,
3360Sstevel@tonic-gate 					"%s/%s: opts too long (max %d chars)",
337*11262SRajagopal.Andra@Sun.COM 					    FNPREFIX, cname, LINESZ - 1);
3380Sstevel@tonic-gate 					return (NULL);
3390Sstevel@tonic-gate 				}
3400Sstevel@tonic-gate 				opts = ml.linebuf + 1;	/* skip '-' */
3410Sstevel@tonic-gate 				goto indirect;
3420Sstevel@tonic-gate 			}
3430Sstevel@tonic-gate 			mapents = parse_entry(key, map, opts, &ml, NULL, 0,
344*11262SRajagopal.Andra@Sun.COM 			    TRUE);
3450Sstevel@tonic-gate 			if (mapents == NULL || !safe_mapent(mapents)) {
3460Sstevel@tonic-gate 				free_mapent(mapents);
3470Sstevel@tonic-gate 				return (NULL);
3480Sstevel@tonic-gate 			}
3490Sstevel@tonic-gate 			free(mapents->map_root);
3500Sstevel@tonic-gate 			mapents->map_root = root;
3510Sstevel@tonic-gate 			break;
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 		case ADDR_HOST:
3540Sstevel@tonic-gate 			/*
3550Sstevel@tonic-gate 			 * Address is of the form "host:dir".
3560Sstevel@tonic-gate 			 * If "dir" is not supplied, it defaults to "/".
3570Sstevel@tonic-gate 			 */
3580Sstevel@tonic-gate 			colon = strchr(addrdata, ':');
3590Sstevel@tonic-gate 			if (colon == NULL || colon[1] == '\0') {
3600Sstevel@tonic-gate 				nfsdir = strdup("/");
3610Sstevel@tonic-gate 			} else {
3620Sstevel@tonic-gate 				*colon = '\0';
3630Sstevel@tonic-gate 				nfsdir = strdup(colon + 1);
3640Sstevel@tonic-gate 			}
3650Sstevel@tonic-gate 			nfshost = strdup(addrdata);
3660Sstevel@tonic-gate 			/*
3670Sstevel@tonic-gate 			 * If nfshost is the local host, the NFS mount
3680Sstevel@tonic-gate 			 * request will be converted to a loopback
3690Sstevel@tonic-gate 			 * mount.  Otherwise check that the file system
3700Sstevel@tonic-gate 			 * is exported.
3710Sstevel@tonic-gate 			 */
3720Sstevel@tonic-gate 			if (nfshost != NULL) {
3730Sstevel@tonic-gate 				self = self_check(nfshost);
3740Sstevel@tonic-gate 				if (!self && !exported(ref, cname, status)) {
3750Sstevel@tonic-gate 					if (transient(status)) {
3760Sstevel@tonic-gate 						return (NULL);
3770Sstevel@tonic-gate 					} else {
3780Sstevel@tonic-gate 						goto indirect;
3790Sstevel@tonic-gate 					}
3800Sstevel@tonic-gate 				}
3810Sstevel@tonic-gate 			}
3820Sstevel@tonic-gate 			mapents = new_mapent(root, strdup(""), strdup("nfs"),
383*11262SRajagopal.Andra@Sun.COM 			    safe_opts(opts), nfshost, nfsdir);
3840Sstevel@tonic-gate 			if (self && !shallow) {
3850Sstevel@tonic-gate 				return (mapents);
3860Sstevel@tonic-gate 			}
3870Sstevel@tonic-gate 			break;
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 		case ADDR_USER:
3900Sstevel@tonic-gate 			homedir = strdup(addrdata);
3910Sstevel@tonic-gate 			homedir[strcspn(homedir, " \t\r\n")] = '\0';
3920Sstevel@tonic-gate 			mapents = new_mapent(root, strdup(""), strdup("lofs"),
393*11262SRajagopal.Andra@Sun.COM 			    strdup(opts), strdup(""), homedir);
3940Sstevel@tonic-gate 			break;
3950Sstevel@tonic-gate 		}
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 		if (mapents == NULL) {
3980Sstevel@tonic-gate 			return (NULL);
3990Sstevel@tonic-gate 		}
4000Sstevel@tonic-gate 		if (shallow) {
4010Sstevel@tonic-gate 			mapents->map_root = NULL;	/* don't free "root" */
4020Sstevel@tonic-gate 			free_mapent(mapents);
4030Sstevel@tonic-gate 			goto indirect;
4040Sstevel@tonic-gate 		}
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 		/* "map" => "map/key" */
4070Sstevel@tonic-gate 		if ((maplen = append_mapname(map, strlen(map), key)) == 0) {
4080Sstevel@tonic-gate 			return (mapents);
4090Sstevel@tonic-gate 		}
4100Sstevel@tonic-gate 		return (frontier(mapents, ref, map, maplen, map + maplen,
411*11262SRajagopal.Andra@Sun.COM 		    opts, status));
4120Sstevel@tonic-gate 	}
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	/* Ref type wasn't recognized. */
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate indirect:
4170Sstevel@tonic-gate 	/* Install an indirect autofs mount point. */
4180Sstevel@tonic-gate 	return (new_mapent(root, strdup(""), strdup("autofs"), strdup(opts),
419*11262SRajagopal.Andra@Sun.COM 	    strdup(""), concat(map, '/', key)));
4200Sstevel@tonic-gate }
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate /*
4240Sstevel@tonic-gate  * All that this function really does is call frontier_aux() on every
4250Sstevel@tonic-gate  * name bound under ref.  The rest is error checking(!)
4260Sstevel@tonic-gate  *
4270Sstevel@tonic-gate  * The error handling strategy is to reject the entire mount request
4280Sstevel@tonic-gate  * (by freeing mapents) if any (potentially) transient error occurs,
4290Sstevel@tonic-gate  * and to treat nontransient errors as holes in the affected portions
4300Sstevel@tonic-gate  * of the namespace.
4310Sstevel@tonic-gate  */
4320Sstevel@tonic-gate static mapent *
frontier(mapent * mapents,const FN_ref_t * ref,char * map,size_t maplen,char * mntpnt,char * opts,FN_status_t * status)4330Sstevel@tonic-gate frontier(mapent *mapents, const FN_ref_t *ref, char *map, size_t maplen,
4340Sstevel@tonic-gate     char *mntpnt, char *opts, FN_status_t *status)
4350Sstevel@tonic-gate {
4360Sstevel@tonic-gate 	FN_ctx_t		*ctx;
4370Sstevel@tonic-gate 	FN_bindinglist_t	*bindings = NULL;
4380Sstevel@tonic-gate 	FN_ref_t		*child_ref;
4390Sstevel@tonic-gate 	FN_string_t		*child_s;
4400Sstevel@tonic-gate 	const char		*child;
4410Sstevel@tonic-gate 	unsigned int		statcode;
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	ctx = fn_ctx_handle_from_ref(ref, XFN2(0) status);
4440Sstevel@tonic-gate 	if (ctx == NULL) {
4450Sstevel@tonic-gate 		if (fn_status_code(status) != FN_E_NO_SUPPORTED_ADDRESS) {
4460Sstevel@tonic-gate 			logstat(status, "from_ref failed for", map);
4470Sstevel@tonic-gate 		}
4480Sstevel@tonic-gate 		goto checkerr_return;
4490Sstevel@tonic-gate 	}
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 	bindings = fn_ctx_list_bindings(ctx, empty_cname, status);
4520Sstevel@tonic-gate 	fn_ctx_handle_destroy(ctx);
4530Sstevel@tonic-gate 	if (bindings == NULL) {
4540Sstevel@tonic-gate 		logstat(status, "list_bindings failed for", map);
4550Sstevel@tonic-gate 		goto checkerr_return;
4560Sstevel@tonic-gate 	}
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	while ((child_s = fn_bindinglist_next(bindings, &child_ref, status))
459*11262SRajagopal.Andra@Sun.COM 	    != NULL) {
4600Sstevel@tonic-gate 		child = (const char *)fn_string_str(child_s, &statcode);
4610Sstevel@tonic-gate 		if (child == NULL) {
4620Sstevel@tonic-gate 			if (verbose) {
4630Sstevel@tonic-gate 				syslog(LOG_ERR,
464*11262SRajagopal.Andra@Sun.COM 				    "FNS string error listing %s", map);
4650Sstevel@tonic-gate 			}
4660Sstevel@tonic-gate 			fn_string_destroy(child_s);
4670Sstevel@tonic-gate 			goto err_return;
4680Sstevel@tonic-gate 		}
4690Sstevel@tonic-gate 		mapents = frontier_aux(mapents, child_ref, map, maplen,
470*11262SRajagopal.Andra@Sun.COM 		    mntpnt, child, opts, status);
4710Sstevel@tonic-gate 		fn_string_destroy(child_s);
4720Sstevel@tonic-gate 		fn_ref_destroy(child_ref);
4730Sstevel@tonic-gate 		if (mapents == NULL) {
4740Sstevel@tonic-gate 			goto noerr_return;
4750Sstevel@tonic-gate 		}
4760Sstevel@tonic-gate 	}
4770Sstevel@tonic-gate 	if (fn_status_is_success(status)) {
4780Sstevel@tonic-gate 		goto noerr_return;
4790Sstevel@tonic-gate 	} else {
4800Sstevel@tonic-gate 		logstat(status, "error while listing", map);
4810Sstevel@tonic-gate 		/* Fall through to checkerr_return. */
4820Sstevel@tonic-gate 	}
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate checkerr_return:
4850Sstevel@tonic-gate 	if (!transient(status)) {
4860Sstevel@tonic-gate 		goto noerr_return;
4870Sstevel@tonic-gate 	}
4880Sstevel@tonic-gate err_return:
4890Sstevel@tonic-gate 	free_mapent(mapents);
4900Sstevel@tonic-gate 	mapents = NULL;
4910Sstevel@tonic-gate noerr_return:
4920Sstevel@tonic-gate 	fn_bindinglist_destroy(bindings XFN1(status));
4930Sstevel@tonic-gate 	return (mapents);
4940Sstevel@tonic-gate }
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate static mapent *
frontier_aux(mapent * mapents,const FN_ref_t * ref,char * map,size_t maplen,char * mntpnt,const char * name,char * opts,FN_status_t * status)4980Sstevel@tonic-gate frontier_aux(mapent *mapents, const FN_ref_t *ref, char *map, size_t maplen,
4990Sstevel@tonic-gate     char *mntpnt, const char *name, char *opts, FN_status_t *status)
5000Sstevel@tonic-gate {
5010Sstevel@tonic-gate 	addrtype_t	addrtype;
5020Sstevel@tonic-gate 	bool_t		at_frontier;
5030Sstevel@tonic-gate 	mapent		*me;
5040Sstevel@tonic-gate 	size_t		maplen_save = maplen;
5050Sstevel@tonic-gate 	char		*cname = map + FNPREFIXLEN + 1;	/* for error msgs */
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	if (reftype(ref) >= NUM_REFTYPES) {
5080Sstevel@tonic-gate 		/*
5090Sstevel@tonic-gate 		 * We could instead install an indirect autofs mount point
5100Sstevel@tonic-gate 		 * here.  That would allow, for example, a user to be bound
5110Sstevel@tonic-gate 		 * beneath a file system.
5120Sstevel@tonic-gate 		 */
5130Sstevel@tonic-gate 		return (mapents);
5140Sstevel@tonic-gate 	}
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	/* "map" => "map/name" */
5170Sstevel@tonic-gate 	if ((maplen = append_mapname(map, maplen, name)) == 0) {
5180Sstevel@tonic-gate 		return (mapents);
5190Sstevel@tonic-gate 	}
5200Sstevel@tonic-gate 	if (trace > 1) {
5210Sstevel@tonic-gate 		trace_prt(1, "  FNS traversal: %s/\n", cname);
5220Sstevel@tonic-gate 	}
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 	/*
5250Sstevel@tonic-gate 	 * If this is an address type that we know how to mount, then
5260Sstevel@tonic-gate 	 * we have reached the frontier.
5270Sstevel@tonic-gate 	 */
5280Sstevel@tonic-gate 	at_frontier = (addr_from_ref(ref, cname, &addrtype, NULL, 0) == 0);
5290Sstevel@tonic-gate 	/*
5300Sstevel@tonic-gate 	 * For an ADDR_HOST address, treat a non-exported directory as
5310Sstevel@tonic-gate 	 * if the address type were not known:  continue searching for
5320Sstevel@tonic-gate 	 * exported subdirectories.
5330Sstevel@tonic-gate 	 */
5340Sstevel@tonic-gate 	if (at_frontier && (addrtype == ADDR_HOST)) {
5350Sstevel@tonic-gate 		if (!exported(ref, cname, status)) {
5360Sstevel@tonic-gate 			if (transient(status)) {
5370Sstevel@tonic-gate 				free_mapent(mapents);
5380Sstevel@tonic-gate 				return (NULL);
5390Sstevel@tonic-gate 			} else {
5400Sstevel@tonic-gate 				at_frontier = FALSE;
5410Sstevel@tonic-gate 			}
5420Sstevel@tonic-gate 		}
5430Sstevel@tonic-gate 	}
5440Sstevel@tonic-gate 	/*
5450Sstevel@tonic-gate 	 * If we have reached the frontier, install a direct autofs
5460Sstevel@tonic-gate 	 * mount point (which will trigger the actual mount if the
5470Sstevel@tonic-gate 	 * user steps on it later).  Otherwise, continue traversing
5480Sstevel@tonic-gate 	 * the namespace looking for known address types.
5490Sstevel@tonic-gate 	 */
5500Sstevel@tonic-gate 	if (at_frontier) {
5510Sstevel@tonic-gate 		opts = (opts[0] != '\0')
552*11262SRajagopal.Andra@Sun.COM 		    ? concat(opts, ',', "direct")
553*11262SRajagopal.Andra@Sun.COM 		    : strdup("direct");
5540Sstevel@tonic-gate 		me = new_mapent(noroot, strdup(mntpnt), strdup("autofs"), opts,
555*11262SRajagopal.Andra@Sun.COM 		    strdup(""), strdup(map));
5560Sstevel@tonic-gate 		if (me != NULL) {
5570Sstevel@tonic-gate 			/* Link new mapent into list (not at the head). */
5580Sstevel@tonic-gate 			me->map_next = mapents->map_next;
5590Sstevel@tonic-gate 			mapents->map_next = me;
5600Sstevel@tonic-gate 		} else {
5610Sstevel@tonic-gate 			free_mapent(mapents);
5620Sstevel@tonic-gate 			mapents = NULL;
5630Sstevel@tonic-gate 		}
5640Sstevel@tonic-gate 	} else {
5650Sstevel@tonic-gate 		mapents =
5660Sstevel@tonic-gate 		    frontier(mapents, ref, map, maplen, mntpnt, opts, status);
5670Sstevel@tonic-gate 	}
5680Sstevel@tonic-gate 	map[maplen_save] = '\0';	/* "map/name" => "map" */
5690Sstevel@tonic-gate 	return (mapents);
5700Sstevel@tonic-gate }
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate static bool_t
exported(const FN_ref_t * ref,const char * cname,FN_status_t * status)5740Sstevel@tonic-gate exported(const FN_ref_t *ref, const char *cname, FN_status_t *status)
5750Sstevel@tonic-gate {
5760Sstevel@tonic-gate 	FN_ctx_t		*ctx;
5770Sstevel@tonic-gate 	FN_attribute_t		*attr;
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	ctx = fn_ctx_handle_from_ref(ref, XFN2(0) status);
5800Sstevel@tonic-gate 	if (ctx == NULL) {
5810Sstevel@tonic-gate 		logstat(status, "from_ref failed for", cname);
5820Sstevel@tonic-gate 		return (FALSE);
5830Sstevel@tonic-gate 	}
5840Sstevel@tonic-gate 	attr = fn_attr_get(ctx, empty_cname, &attr_exported, XFN2(1) status);
5850Sstevel@tonic-gate 	fn_ctx_handle_destroy(ctx);
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	switch (fn_status_code(status)) {
5880Sstevel@tonic-gate 	case FN_SUCCESS:
5890Sstevel@tonic-gate 		fn_attribute_destroy(attr);
5900Sstevel@tonic-gate 		break;
5910Sstevel@tonic-gate 	case FN_E_NO_SUCH_ATTRIBUTE:
5920Sstevel@tonic-gate 		break;
5930Sstevel@tonic-gate 	default:
5940Sstevel@tonic-gate 		logstat(status, "could not get attributes for", cname);
5950Sstevel@tonic-gate 	}
5960Sstevel@tonic-gate 	return (attr != NULL);
5970Sstevel@tonic-gate }
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate static int
addr_from_ref(const FN_ref_t * ref,const char * cname,addrtype_t * typep,char * data,size_t datasz)6010Sstevel@tonic-gate addr_from_ref(const FN_ref_t *ref, const char *cname, addrtype_t *typep,
6020Sstevel@tonic-gate     char *data, size_t datasz)
6030Sstevel@tonic-gate {
6040Sstevel@tonic-gate 	const FN_ref_addr_t	*addr;
6050Sstevel@tonic-gate 	void			*iter_pos;
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 	addr = fn_ref_first(ref, &iter_pos);
6080Sstevel@tonic-gate 	if (addr == NULL) {
6090Sstevel@tonic-gate 		if (verbose) {
6100Sstevel@tonic-gate 			syslog(LOG_ERR, "FNS ref with no address: %s", cname);
6110Sstevel@tonic-gate 		}
6120Sstevel@tonic-gate 		return (-1);
6130Sstevel@tonic-gate 	}
6140Sstevel@tonic-gate 	while (addr != NULL) {
6150Sstevel@tonic-gate 		*typep = addrtype(addr);
6160Sstevel@tonic-gate 		if (*typep < NUM_ADDRTYPES) {
6170Sstevel@tonic-gate 			return ((data != NULL)
618*11262SRajagopal.Andra@Sun.COM 			    ? str_from_addr(cname, addr, data, datasz)
619*11262SRajagopal.Andra@Sun.COM 			    : 0);
6200Sstevel@tonic-gate 		}
6210Sstevel@tonic-gate 		addr = fn_ref_next(ref, &iter_pos);
6220Sstevel@tonic-gate 	}
6230Sstevel@tonic-gate 	return (-1);
6240Sstevel@tonic-gate }
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate static int
str_from_addr(const char * cname,const FN_ref_addr_t * addr,char str[],size_t strsz)6280Sstevel@tonic-gate str_from_addr(const char *cname, const FN_ref_addr_t *addr, char str[],
6290Sstevel@tonic-gate     size_t strsz)
6300Sstevel@tonic-gate {
6310Sstevel@tonic-gate 	XDR	xdr;
6320Sstevel@tonic-gate 	int	res;
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	xdrmem_create(&xdr, (caddr_t)fn_ref_addr_data(addr),
635*11262SRajagopal.Andra@Sun.COM 	    fn_ref_addr_length(addr), XDR_DECODE);
6360Sstevel@tonic-gate 	if (!xdr_string(&xdr, &str, strsz)) {
6370Sstevel@tonic-gate 		if (verbose) {
6380Sstevel@tonic-gate 			syslog(LOG_ERR,
639*11262SRajagopal.Andra@Sun.COM 			    "Could not decode FNS address for %s", cname);
6400Sstevel@tonic-gate 		}
6410Sstevel@tonic-gate 		res = -1;
6420Sstevel@tonic-gate 	} else {
6430Sstevel@tonic-gate 		res = 0;
6440Sstevel@tonic-gate 	}
6450Sstevel@tonic-gate 	xdr_destroy(&xdr);
6460Sstevel@tonic-gate 	return (res);
6470Sstevel@tonic-gate }
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate static size_t
append_mapname(char * map,size_t maplen,const char * name)6500Sstevel@tonic-gate append_mapname(char *map, size_t maplen, const char *name)
6510Sstevel@tonic-gate {
6520Sstevel@tonic-gate 	size_t namelen = strlen(name);
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	if (maplen + 1 + namelen >= MAPNAMESZ) {
6550Sstevel@tonic-gate 		if (verbose) {
6560Sstevel@tonic-gate 			syslog(LOG_ERR, "FNS name %s/%s too long",
657*11262SRajagopal.Andra@Sun.COM 			    map + FNPREFIXLEN + 1, name);
6580Sstevel@tonic-gate 		}
6590Sstevel@tonic-gate 		return (0);
6600Sstevel@tonic-gate 	}
6610Sstevel@tonic-gate 	sprintf(map + maplen, "/%s", name);
6620Sstevel@tonic-gate 	return (maplen + 1 + namelen);
6630Sstevel@tonic-gate }
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate static char *
concat(const char * s1,char sep,const char * s2)6670Sstevel@tonic-gate concat(const char *s1, char sep, const char *s2)
6680Sstevel@tonic-gate {
6690Sstevel@tonic-gate 	char *s = malloc(strlen(s1) + 1 + strlen(s2) + 1);
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 	if (s != NULL) {
6720Sstevel@tonic-gate 		sprintf(s, "%s%c%s", s1, sep, s2);
6730Sstevel@tonic-gate 	}
6740Sstevel@tonic-gate 	return (s);
6750Sstevel@tonic-gate }
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate static bool_t
safe_mapent(mapent * me)6790Sstevel@tonic-gate safe_mapent(mapent *me)
6800Sstevel@tonic-gate {
6810Sstevel@tonic-gate 	char	*opts;
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 	if (me->map_next != NULL) {
6840Sstevel@tonic-gate 		/* Multiple mounts don't belong in XFN namespace. */
6850Sstevel@tonic-gate 		return (NULL);
6860Sstevel@tonic-gate 	}
6870Sstevel@tonic-gate 	opts = me->map_mntopts;
6880Sstevel@tonic-gate 	me->map_mntopts = safe_opts(opts);
6890Sstevel@tonic-gate 	free(opts);
6900Sstevel@tonic-gate 	return (me->map_mntopts != NULL);
6910Sstevel@tonic-gate }
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate static char *
safe_opts(const char * opts)6950Sstevel@tonic-gate safe_opts(const char *opts)
6960Sstevel@tonic-gate {
6970Sstevel@tonic-gate 	char	*start;
6980Sstevel@tonic-gate 	size_t	len;
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	if (opts[0] == '\0') {
7010Sstevel@tonic-gate 		return (strdup(MNTOPT_NOSUID));
7020Sstevel@tonic-gate 	}
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 	/* A quick-and-dirty check to see if "nosuid" is already there. */
7050Sstevel@tonic-gate 	start = strstr(opts, MNTOPT_NOSUID);
7060Sstevel@tonic-gate 	len = sizeof (MNTOPT_NOSUID) - 1;	/* "-1" for trailing '\0' */
7070Sstevel@tonic-gate 	if (start != NULL) {
7080Sstevel@tonic-gate 		while (start > opts && isspace(*(start - 1))) {
7090Sstevel@tonic-gate 			start--;
7100Sstevel@tonic-gate 		}
7110Sstevel@tonic-gate 		if ((start == opts || *(start - 1) == ',') &&
7120Sstevel@tonic-gate 		    opts[len] == ',' || opts[len] == '\0') {
7130Sstevel@tonic-gate 			return (strdup(opts));
7140Sstevel@tonic-gate 		}
7150Sstevel@tonic-gate 	}
7160Sstevel@tonic-gate 	return (concat(opts, ',', MNTOPT_NOSUID));
7170Sstevel@tonic-gate }
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate static int
trim_line(mapline * ml)7210Sstevel@tonic-gate trim_line(mapline *ml)
7220Sstevel@tonic-gate {
7230Sstevel@tonic-gate 	char	*end;	/* pointer to '\0' at end of linebuf */
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 	end = ml->linebuf + strcspn(ml->linebuf, "#");
7260Sstevel@tonic-gate 	while ((end > ml->linebuf) && isspace(end[-1])) {
7270Sstevel@tonic-gate 		end--;
7280Sstevel@tonic-gate 	}
7290Sstevel@tonic-gate 	if (end <= ml->linebuf) {
7300Sstevel@tonic-gate 		return (-1);
7310Sstevel@tonic-gate 	}
7320Sstevel@tonic-gate 	*end = '\0';
7330Sstevel@tonic-gate 	unquote(ml->linebuf, ml->lineqbuf);
7340Sstevel@tonic-gate 	return (0);
7350Sstevel@tonic-gate }
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate static bool_t
opts_only(const mapline * ml)7390Sstevel@tonic-gate opts_only(const mapline *ml)
7400Sstevel@tonic-gate {
7410Sstevel@tonic-gate 	const char *s = ml->linebuf;
7420Sstevel@tonic-gate 	const char *q = ml->lineqbuf;
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	if (*s != '-') {
7450Sstevel@tonic-gate 		return (FALSE);
7460Sstevel@tonic-gate 	}
7470Sstevel@tonic-gate 	for (; *s != '\0'; s++, q++) {
7480Sstevel@tonic-gate 		if (isspace(*s) && (*q == ' ')) {
7490Sstevel@tonic-gate 			return (FALSE);
7500Sstevel@tonic-gate 		}
7510Sstevel@tonic-gate 	}
7520Sstevel@tonic-gate 	return (TRUE);
7530Sstevel@tonic-gate }
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate static mapent *
new_mapent(char * root,char * mntpnt,char * fstype,char * mntopts,char * host,char * dir)7570Sstevel@tonic-gate new_mapent(char *root, char *mntpnt, char *fstype, char *mntopts, char *host,
7580Sstevel@tonic-gate     char *dir)
7590Sstevel@tonic-gate {
7600Sstevel@tonic-gate 	mapent		*me;
7610Sstevel@tonic-gate 	struct mapfs	*mfs;
7620Sstevel@tonic-gate 	char		*mounter = NULL;
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	me = calloc(1, sizeof (*me));
7650Sstevel@tonic-gate 	mfs = calloc(1, sizeof (*mfs));
7660Sstevel@tonic-gate 	if (fstype != NULL) {
7670Sstevel@tonic-gate 		mounter = strdup(fstype);
7680Sstevel@tonic-gate 	}
7690Sstevel@tonic-gate 	if ((mntpnt == NULL) || (fstype == NULL) || (mntopts == NULL) ||
7700Sstevel@tonic-gate 	    (host == NULL) || (dir == NULL) || (me == NULL) || (mfs == NULL) ||
7710Sstevel@tonic-gate 	    (mounter == NULL) || (root == NULL)) {
7720Sstevel@tonic-gate 		log_mem_failure();
7730Sstevel@tonic-gate 		free(me);
7740Sstevel@tonic-gate 		free(mfs);
7750Sstevel@tonic-gate 		free(mounter);
7760Sstevel@tonic-gate 		free(root);
7770Sstevel@tonic-gate 		free(mntpnt);
7780Sstevel@tonic-gate 		free(fstype);
7790Sstevel@tonic-gate 		free(mntopts);
7800Sstevel@tonic-gate 		free(host);
7810Sstevel@tonic-gate 		free(dir);
7820Sstevel@tonic-gate 		return (NULL);
7830Sstevel@tonic-gate 	}
7840Sstevel@tonic-gate 	me->map_root	= (root != noroot) ? root : NULL;
7850Sstevel@tonic-gate 	me->map_fstype	= fstype;
7860Sstevel@tonic-gate 	me->map_mounter	= mounter;
7870Sstevel@tonic-gate 	me->map_mntpnt	= mntpnt;
7880Sstevel@tonic-gate 	me->map_mntopts	= mntopts;
7890Sstevel@tonic-gate 	me->map_fsw	= NULL;
7900Sstevel@tonic-gate 	me->map_fswq    = NULL;
7910Sstevel@tonic-gate 	me->map_fs	= mfs;
7920Sstevel@tonic-gate 	mfs->mfs_host	= host;
7930Sstevel@tonic-gate 	mfs->mfs_dir	= dir;
7940Sstevel@tonic-gate 	me->map_mntlevel = -1;
7950Sstevel@tonic-gate 	me->map_modified = FALSE;
7960Sstevel@tonic-gate 	me->map_faked = FALSE;
7970Sstevel@tonic-gate 	me->map_err = 0;		/* MAPENT_NOERR */
7980Sstevel@tonic-gate 	return (me);
7990Sstevel@tonic-gate }
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate #ifndef XFN1ENV
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate /*
8050Sstevel@tonic-gate  * User-relative bindings in the initial context, and the leading components
8060Sstevel@tonic-gate  * of their non-user-relative equivalents.  Leading components are listed in
8070Sstevel@tonic-gate  * the order in which they should be tried.  Each list is NULL-terminated
8080Sstevel@tonic-gate  * (the compiler generously does this for us).
8090Sstevel@tonic-gate  * For "myorgunit", for example, we first check if it is equivalent to
8100Sstevel@tonic-gate  * "thisorgunit".  If not, we translate it into "org/<something>".
8110Sstevel@tonic-gate  */
8120Sstevel@tonic-gate #define	MAX_LEADS 3
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate static struct {
8150Sstevel@tonic-gate 	const char	*binding;
8160Sstevel@tonic-gate 	const char	*leads[MAX_LEADS + 1];
8170Sstevel@tonic-gate } user_rel[] = {
8180Sstevel@tonic-gate 	{"thisuser",	{"user", "thisorgunit", "org"}},
8190Sstevel@tonic-gate 	{"myself",	{"user", "thisorgunit", "org"}},
8200Sstevel@tonic-gate 	{"_myself",	{"_user", "_thisorgunit", "_orgunit"}},
8210Sstevel@tonic-gate 	{"myorgunit",	{"thisorgunit", "org"}},
8220Sstevel@tonic-gate 	{"_myorgunit",	{"_thisorgunit", "_orgunit"}},
8230Sstevel@tonic-gate 	{"myens",	{"thisens"}},
8240Sstevel@tonic-gate 	{"_myens",	{"_thisens"}}
8250Sstevel@tonic-gate };
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate static bool_t
is_user_relative(const char * cname)8290Sstevel@tonic-gate is_user_relative(const char *cname)
8300Sstevel@tonic-gate {
8310Sstevel@tonic-gate 	int	i;
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 	for (i = 0; i < sizeof (user_rel) / sizeof (user_rel[0]); i++) {
8340Sstevel@tonic-gate 		if (strcmp(cname, user_rel[i].binding) == 0) {
8350Sstevel@tonic-gate 			return (TRUE);
8360Sstevel@tonic-gate 		}
8370Sstevel@tonic-gate 	}
8380Sstevel@tonic-gate 	return (FALSE);
8390Sstevel@tonic-gate }
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate static char *
equiv_name(FN_ctx_t * ctx,const char * cname,FN_status_t * status)8430Sstevel@tonic-gate equiv_name(FN_ctx_t *ctx, const char *cname, FN_status_t *status)
8440Sstevel@tonic-gate {
8450Sstevel@tonic-gate 	FN_composite_name_t	*name;
8460Sstevel@tonic-gate 	FN_string_t		*leading_name;
8470Sstevel@tonic-gate 	FN_composite_name_t	*equiv;
8480Sstevel@tonic-gate 	FN_string_t		*equiv_string;
8490Sstevel@tonic-gate 	const char		*equiv_str;
8500Sstevel@tonic-gate 	char			*equiv_str_dup;
8510Sstevel@tonic-gate 	const char		**leads;
8520Sstevel@tonic-gate 	unsigned int		stat;
8530Sstevel@tonic-gate 	int			i;
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 	for (i = 0; i < sizeof (user_rel) / sizeof (user_rel[0]); i++) {
8560Sstevel@tonic-gate 		if (strcmp(cname, user_rel[i].binding) == 0) {
8570Sstevel@tonic-gate 			break;
8580Sstevel@tonic-gate 		}
8590Sstevel@tonic-gate 	}
8600Sstevel@tonic-gate 	if ((name = new_cname(cname)) == NULL) {
8610Sstevel@tonic-gate 		return (NULL);
8620Sstevel@tonic-gate 	}
8630Sstevel@tonic-gate 	leads = user_rel[i].leads;	/* array of leading names to try */
8640Sstevel@tonic-gate 	do {
8650Sstevel@tonic-gate 		leading_name = fn_string_from_str((unsigned char *)*leads);
8660Sstevel@tonic-gate 		if (leading_name == NULL) {
8670Sstevel@tonic-gate 			log_mem_failure();
8680Sstevel@tonic-gate 			fn_composite_name_destroy(name);
8690Sstevel@tonic-gate 			return (NULL);
8700Sstevel@tonic-gate 		}
8710Sstevel@tonic-gate 		equiv = prelim_fn_ctx_equivalent_name(ctx, name, leading_name,
8720Sstevel@tonic-gate 		    status);
8730Sstevel@tonic-gate 		fn_string_destroy(leading_name);
8740Sstevel@tonic-gate 	} while (equiv == NULL && *++leads != NULL);
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 	fn_composite_name_destroy(name);
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 	if (equiv == NULL) {
8790Sstevel@tonic-gate 		if (transient(status)) {
8800Sstevel@tonic-gate 			logstat(status, "could not find equivalent of", cname);
8810Sstevel@tonic-gate 		}
8820Sstevel@tonic-gate 		return (NULL);
8830Sstevel@tonic-gate 	}
8840Sstevel@tonic-gate 	equiv_string = fn_string_from_composite_name(equiv, &stat);
8850Sstevel@tonic-gate 	fn_composite_name_destroy(equiv);
8860Sstevel@tonic-gate 	if (equiv_string == NULL) {
8870Sstevel@tonic-gate 		log_mem_failure();
8880Sstevel@tonic-gate 		return (NULL);
8890Sstevel@tonic-gate 	}
8900Sstevel@tonic-gate 	equiv_str = (const char *)fn_string_str(equiv_string, &stat);
8910Sstevel@tonic-gate 	if (equiv_str == NULL ||
8920Sstevel@tonic-gate 	    (equiv_str_dup = strdup(equiv_str)) == NULL) {
8930Sstevel@tonic-gate 		log_mem_failure();
8940Sstevel@tonic-gate 		fn_string_destroy(equiv_string);
8950Sstevel@tonic-gate 		return (NULL);
8960Sstevel@tonic-gate 	}
8970Sstevel@tonic-gate 	fn_string_destroy(equiv_string);
8980Sstevel@tonic-gate 	return (equiv_str_dup);
8990Sstevel@tonic-gate }
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate #endif	/* XFN1ENV */
902