xref: /onnv-gate/usr/src/cmd/fs.d/autofs/mount.c (revision 249:6233bc9dd306)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  *
260Sstevel@tonic-gate  *	autofs mount.c
270Sstevel@tonic-gate  *
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <stdio.h>
330Sstevel@tonic-gate #include <unistd.h>
340Sstevel@tonic-gate #include <stdlib.h>
350Sstevel@tonic-gate #include <ctype.h>
360Sstevel@tonic-gate #include <sys/param.h>
370Sstevel@tonic-gate #include <sys/stat.h>
380Sstevel@tonic-gate #include <sys/mntent.h>
390Sstevel@tonic-gate #include <sys/mnttab.h>
400Sstevel@tonic-gate #include <sys/mount.h>
410Sstevel@tonic-gate #include <sys/utsname.h>
420Sstevel@tonic-gate #include <sys/tiuser.h>
430Sstevel@tonic-gate #include <string.h>
440Sstevel@tonic-gate #include <fslib.h>
450Sstevel@tonic-gate #include <errno.h>
460Sstevel@tonic-gate #include <rpcsvc/daemon_utils.h>
470Sstevel@tonic-gate #include "automount.h"
480Sstevel@tonic-gate 
490Sstevel@tonic-gate #define	MNTTAB_OPTS	"ignore,nest"
500Sstevel@tonic-gate 
510Sstevel@tonic-gate static void usage();
520Sstevel@tonic-gate static void process_opts(char *options, int *directp);
530Sstevel@tonic-gate static char *concat_opts(const char *opts1, const char *opts2);
540Sstevel@tonic-gate static int  ro_given(char *options);
550Sstevel@tonic-gate 
560Sstevel@tonic-gate /*
570Sstevel@tonic-gate  * list of support services needed
580Sstevel@tonic-gate  */
590Sstevel@tonic-gate static char	*service_list[] = { AUTOMOUNTD, NULL };
600Sstevel@tonic-gate 
61*249Sjwahlig int
main(int argc,char * argv[])62*249Sjwahlig main(int argc, char *argv[])
630Sstevel@tonic-gate {
640Sstevel@tonic-gate 	int error;
650Sstevel@tonic-gate 	int c;
660Sstevel@tonic-gate 	int mntflags = 0;
670Sstevel@tonic-gate 	int nmflg = 0;
680Sstevel@tonic-gate 	int roflg = 0;
690Sstevel@tonic-gate 	char *mntpnt, *mapname;
700Sstevel@tonic-gate 	struct utsname utsname;
710Sstevel@tonic-gate 	char autofs_addr[MAXADDRLEN];
720Sstevel@tonic-gate 	struct autofs_args fni;
730Sstevel@tonic-gate 	char *options = "";
740Sstevel@tonic-gate 	int mount_timeout = AUTOFS_MOUNT_TIMEOUT;
750Sstevel@tonic-gate 	char obuf[MAX_MNTOPT_STR];
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "o:mrq")) != EOF) {
780Sstevel@tonic-gate 		switch (c) {
790Sstevel@tonic-gate 		case '?':
800Sstevel@tonic-gate 			usage();
810Sstevel@tonic-gate 			exit(1);
820Sstevel@tonic-gate 			/* NOTREACHED */
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 		case 'o':
850Sstevel@tonic-gate 			options = optarg;
860Sstevel@tonic-gate 			break;
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 		case 'm':
890Sstevel@tonic-gate 			nmflg++;
900Sstevel@tonic-gate 			break;
910Sstevel@tonic-gate 		case 'r':	/* converted to -o ro always */
920Sstevel@tonic-gate 			roflg++;
930Sstevel@tonic-gate 			break;
940Sstevel@tonic-gate 		/*
950Sstevel@tonic-gate 		 *  The "quiet" flag can be ignored, since this
960Sstevel@tonic-gate 		 *  program never complains about invalid -o options
970Sstevel@tonic-gate 		 *  anyway.
980Sstevel@tonic-gate 		 */
990Sstevel@tonic-gate 		case 'q':
1000Sstevel@tonic-gate 			break;
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 		default:
1030Sstevel@tonic-gate 			usage();
1040Sstevel@tonic-gate 		}
1050Sstevel@tonic-gate 	}
1060Sstevel@tonic-gate 	if (argc - optind != 2)
1070Sstevel@tonic-gate 		usage();
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	mapname = argv[optind];
1100Sstevel@tonic-gate 	mntpnt  = argv[optind + 1];
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	if (strcmp(mntpnt, "/-") == 0) {
1130Sstevel@tonic-gate 		(void) fprintf(stderr, "invalid mountpoint: /-\n");
1140Sstevel@tonic-gate 		exit(1);
1150Sstevel@tonic-gate 	}
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	if (uname(&utsname) < 0) {
1180Sstevel@tonic-gate 		perror("uname");
1190Sstevel@tonic-gate 		exit(1);
1200Sstevel@tonic-gate 	}
1210Sstevel@tonic-gate 	(void) strcpy(autofs_addr, utsname.nodename);
1220Sstevel@tonic-gate 	(void) strcat(autofs_addr, ".autofs");
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	process_opts(options, &fni.direct);
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	if (roflg && !ro_given(options))
1270Sstevel@tonic-gate 		options = concat_opts(options, "ro");
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	fni.addr.buf	= autofs_addr;
1300Sstevel@tonic-gate 	fni.addr.len	= strlen(fni.addr.buf);
1310Sstevel@tonic-gate 	fni.addr.maxlen	= fni.addr.len;
1320Sstevel@tonic-gate 	fni.path	= mntpnt;
1330Sstevel@tonic-gate 	fni.opts	= options;
1340Sstevel@tonic-gate 	fni.map		= mapname;
1350Sstevel@tonic-gate 	fni.subdir	= "";
1360Sstevel@tonic-gate 	if (fni.direct)
1370Sstevel@tonic-gate 		fni.key = mntpnt;
1380Sstevel@tonic-gate 	else
1390Sstevel@tonic-gate 		fni.key	= "";
1400Sstevel@tonic-gate 	fni.mount_to	= mount_timeout;
1410Sstevel@tonic-gate 	fni.rpc_to	= AUTOFS_RPC_TIMEOUT;
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	strcpy(obuf, options);
1440Sstevel@tonic-gate 	if (*obuf != '\0')
1450Sstevel@tonic-gate 		strcat(obuf, ",");
1460Sstevel@tonic-gate 	strcat(obuf,
1470Sstevel@tonic-gate 		fni.direct ? MNTTAB_OPTS ",direct" : MNTTAB_OPTS ",indirect");
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	/*
1500Sstevel@tonic-gate 	 * enable services as needed.
1510Sstevel@tonic-gate 	 */
1520Sstevel@tonic-gate 	_check_services(service_list);
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	error = mount(fni.map, mntpnt, mntflags | MS_DATA | MS_OPTIONSTR,
1550Sstevel@tonic-gate 		MNTTYPE_AUTOFS, &fni, sizeof (fni), obuf, MAX_MNTOPT_STR);
1560Sstevel@tonic-gate 	if (error < 0) {
1570Sstevel@tonic-gate 		perror("autofs mount");
1580Sstevel@tonic-gate 		exit(1);
1590Sstevel@tonic-gate 	}
1600Sstevel@tonic-gate 	return (0);
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate static void
usage()1640Sstevel@tonic-gate usage()
1650Sstevel@tonic-gate {
1660Sstevel@tonic-gate 	(void) fprintf(stderr,
1670Sstevel@tonic-gate 	    "Usage: autofs mount [-r] [-o opts]  map  dir\n");
1680Sstevel@tonic-gate 	exit(1);
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate /*
1720Sstevel@tonic-gate  * Remove pseudo-options "direct", "indirect", "nest", and "ignore" from
1730Sstevel@tonic-gate  * option list.  Set *directp to 1 if "direct" is found, and 0 otherwise
1740Sstevel@tonic-gate  * (mounts are indirect by default).  If both "direct" and "indirect" are
1750Sstevel@tonic-gate  * found, the last one wins.
1760Sstevel@tonic-gate  */
1770Sstevel@tonic-gate static void
process_opts(char * options,int * directp)1780Sstevel@tonic-gate process_opts(char *options, int *directp)
1790Sstevel@tonic-gate {
1800Sstevel@tonic-gate 	char *opt;
1810Sstevel@tonic-gate 	char *opts;
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	if ((opts = strdup(options)) == NULL) {
1840Sstevel@tonic-gate 		(void) fprintf(stderr,
1850Sstevel@tonic-gate 				"autofs mount: memory allocation failed\n");
1860Sstevel@tonic-gate 		exit(1);
1870Sstevel@tonic-gate 	}
1880Sstevel@tonic-gate 	options[0] = '\0';
1890Sstevel@tonic-gate 	*directp = 0;
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	while ((opt = strtok(opts, ",")) != NULL) {
1920Sstevel@tonic-gate 		opts = NULL;
1930Sstevel@tonic-gate 		while (isspace(*opt)) {
1940Sstevel@tonic-gate 			opt++;
1950Sstevel@tonic-gate 		}
1960Sstevel@tonic-gate 		if (strcmp(opt, "direct") == 0) {
1970Sstevel@tonic-gate 			*directp = 1;
1980Sstevel@tonic-gate 		} else if (strcmp(opt, "indirect") == 0) {
1990Sstevel@tonic-gate 			*directp = 0;
2000Sstevel@tonic-gate 		} else if ((strcmp(opt, "nest") != 0) &&
2010Sstevel@tonic-gate 				(strcmp(opt, "ignore") != 0)) {
2020Sstevel@tonic-gate 			if (options[0] != '\0') {
2030Sstevel@tonic-gate 				(void) strcat(options, ",");
2040Sstevel@tonic-gate 			}
2050Sstevel@tonic-gate 			(void) strcat(options, opt);
2060Sstevel@tonic-gate 		}
2070Sstevel@tonic-gate 	};
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate /*
2110Sstevel@tonic-gate  * Concatenate two options strings, with a comma between them.
2120Sstevel@tonic-gate  */
2130Sstevel@tonic-gate static char *
concat_opts(const char * opts1,const char * opts2)2140Sstevel@tonic-gate concat_opts(const char *opts1, const char *opts2)
2150Sstevel@tonic-gate {
2160Sstevel@tonic-gate 	char *opts = malloc(strlen(opts1) + strlen(opts2) + 2);
2170Sstevel@tonic-gate 	if (opts == NULL) {
2180Sstevel@tonic-gate 		(void) fprintf(stderr,
2190Sstevel@tonic-gate 			"autofs mount: memory allocation failed\n");
2200Sstevel@tonic-gate 		exit(1);
2210Sstevel@tonic-gate 	}
2220Sstevel@tonic-gate 	strcpy(opts, opts1);
2230Sstevel@tonic-gate 	if (opts1[0] != '\0' && opts2[0] != '\0') {
2240Sstevel@tonic-gate 		strcat(opts, ",");
2250Sstevel@tonic-gate 	}
2260Sstevel@tonic-gate 	return (strcat(opts, opts2));
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate /*
2300Sstevel@tonic-gate  * check the options string for 'ro' options
2310Sstevel@tonic-gate  * if present returns 1 otherwise return 0;
2320Sstevel@tonic-gate  */
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate static int
ro_given(char * options)2350Sstevel@tonic-gate ro_given(char *options)
2360Sstevel@tonic-gate {
2370Sstevel@tonic-gate 	char	*op = options;
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	if (!*op)
2400Sstevel@tonic-gate 		return (0);
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	while (op != 0) {
2430Sstevel@tonic-gate 		if (*op == 'r' && *(op+1) == 'o' &&
2440Sstevel@tonic-gate 			(*(op+2) == ',' || *(op+2) == '\0'))
2450Sstevel@tonic-gate 			return (1);
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 		if ((op = strchr(op, ',')) != NULL)
2480Sstevel@tonic-gate 			op++;
2490Sstevel@tonic-gate 	}
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	return (0);
2530Sstevel@tonic-gate }
254