xref: /onnv-gate/usr/src/cmd/fs.d/autofs/mount.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  *
26*0Sstevel@tonic-gate  *	autofs mount.c
27*0Sstevel@tonic-gate  *
28*0Sstevel@tonic-gate  */
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #include <stdio.h>
33*0Sstevel@tonic-gate #include <unistd.h>
34*0Sstevel@tonic-gate #include <stdlib.h>
35*0Sstevel@tonic-gate #include <ctype.h>
36*0Sstevel@tonic-gate #include <sys/param.h>
37*0Sstevel@tonic-gate #include <sys/stat.h>
38*0Sstevel@tonic-gate #include <sys/mntent.h>
39*0Sstevel@tonic-gate #include <sys/mnttab.h>
40*0Sstevel@tonic-gate #include <sys/mount.h>
41*0Sstevel@tonic-gate #include <sys/utsname.h>
42*0Sstevel@tonic-gate #include <sys/tiuser.h>
43*0Sstevel@tonic-gate #include <string.h>
44*0Sstevel@tonic-gate #include <fslib.h>
45*0Sstevel@tonic-gate #include <errno.h>
46*0Sstevel@tonic-gate #include <rpcsvc/daemon_utils.h>
47*0Sstevel@tonic-gate #include "automount.h"
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate #define	MNTTAB_OPTS	"ignore,nest"
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate static void usage();
52*0Sstevel@tonic-gate static void process_opts(char *options, int *directp);
53*0Sstevel@tonic-gate static char *concat_opts(const char *opts1, const char *opts2);
54*0Sstevel@tonic-gate static int  ro_given(char *options);
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate /*
57*0Sstevel@tonic-gate  * list of support services needed
58*0Sstevel@tonic-gate  */
59*0Sstevel@tonic-gate static char	*service_list[] = { AUTOMOUNTD, NULL };
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate main(argc, argv)
62*0Sstevel@tonic-gate 	int argc;
63*0Sstevel@tonic-gate 	char **argv;
64*0Sstevel@tonic-gate {
65*0Sstevel@tonic-gate 	int error;
66*0Sstevel@tonic-gate 	int c;
67*0Sstevel@tonic-gate 	int mntflags = 0;
68*0Sstevel@tonic-gate 	int nmflg = 0;
69*0Sstevel@tonic-gate 	int roflg = 0;
70*0Sstevel@tonic-gate 	char *mntpnt, *mapname;
71*0Sstevel@tonic-gate 	struct utsname utsname;
72*0Sstevel@tonic-gate 	char autofs_addr[MAXADDRLEN];
73*0Sstevel@tonic-gate 	struct autofs_args fni;
74*0Sstevel@tonic-gate 	char *options = "";
75*0Sstevel@tonic-gate 	int mount_timeout = AUTOFS_MOUNT_TIMEOUT;
76*0Sstevel@tonic-gate 	char obuf[MAX_MNTOPT_STR];
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "o:mrq")) != EOF) {
79*0Sstevel@tonic-gate 		switch (c) {
80*0Sstevel@tonic-gate 		case '?':
81*0Sstevel@tonic-gate 			usage();
82*0Sstevel@tonic-gate 			exit(1);
83*0Sstevel@tonic-gate 			/* NOTREACHED */
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate 		case 'o':
86*0Sstevel@tonic-gate 			options = optarg;
87*0Sstevel@tonic-gate 			break;
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate 		case 'm':
90*0Sstevel@tonic-gate 			nmflg++;
91*0Sstevel@tonic-gate 			break;
92*0Sstevel@tonic-gate 		case 'r':	/* converted to -o ro always */
93*0Sstevel@tonic-gate 			roflg++;
94*0Sstevel@tonic-gate 			break;
95*0Sstevel@tonic-gate 		/*
96*0Sstevel@tonic-gate 		 *  The "quiet" flag can be ignored, since this
97*0Sstevel@tonic-gate 		 *  program never complains about invalid -o options
98*0Sstevel@tonic-gate 		 *  anyway.
99*0Sstevel@tonic-gate 		 */
100*0Sstevel@tonic-gate 		case 'q':
101*0Sstevel@tonic-gate 			break;
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 		default:
104*0Sstevel@tonic-gate 			usage();
105*0Sstevel@tonic-gate 		}
106*0Sstevel@tonic-gate 	}
107*0Sstevel@tonic-gate 	if (argc - optind != 2)
108*0Sstevel@tonic-gate 		usage();
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 	mapname = argv[optind];
111*0Sstevel@tonic-gate 	mntpnt  = argv[optind + 1];
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 	if (strcmp(mntpnt, "/-") == 0) {
114*0Sstevel@tonic-gate 		(void) fprintf(stderr, "invalid mountpoint: /-\n");
115*0Sstevel@tonic-gate 		exit(1);
116*0Sstevel@tonic-gate 	}
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate 	if (uname(&utsname) < 0) {
119*0Sstevel@tonic-gate 		perror("uname");
120*0Sstevel@tonic-gate 		exit(1);
121*0Sstevel@tonic-gate 	}
122*0Sstevel@tonic-gate 	(void) strcpy(autofs_addr, utsname.nodename);
123*0Sstevel@tonic-gate 	(void) strcat(autofs_addr, ".autofs");
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 	process_opts(options, &fni.direct);
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate 	if (roflg && !ro_given(options))
128*0Sstevel@tonic-gate 		options = concat_opts(options, "ro");
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate 	fni.addr.buf	= autofs_addr;
131*0Sstevel@tonic-gate 	fni.addr.len	= strlen(fni.addr.buf);
132*0Sstevel@tonic-gate 	fni.addr.maxlen	= fni.addr.len;
133*0Sstevel@tonic-gate 	fni.path	= mntpnt;
134*0Sstevel@tonic-gate 	fni.opts	= options;
135*0Sstevel@tonic-gate 	fni.map		= mapname;
136*0Sstevel@tonic-gate 	fni.subdir	= "";
137*0Sstevel@tonic-gate 	if (fni.direct)
138*0Sstevel@tonic-gate 		fni.key = mntpnt;
139*0Sstevel@tonic-gate 	else
140*0Sstevel@tonic-gate 		fni.key	= "";
141*0Sstevel@tonic-gate 	fni.mount_to	= mount_timeout;
142*0Sstevel@tonic-gate 	fni.rpc_to	= AUTOFS_RPC_TIMEOUT;
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	strcpy(obuf, options);
145*0Sstevel@tonic-gate 	if (*obuf != '\0')
146*0Sstevel@tonic-gate 		strcat(obuf, ",");
147*0Sstevel@tonic-gate 	strcat(obuf,
148*0Sstevel@tonic-gate 		fni.direct ? MNTTAB_OPTS ",direct" : MNTTAB_OPTS ",indirect");
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 	/*
151*0Sstevel@tonic-gate 	 * enable services as needed.
152*0Sstevel@tonic-gate 	 */
153*0Sstevel@tonic-gate 	_check_services(service_list);
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 	error = mount(fni.map, mntpnt, mntflags | MS_DATA | MS_OPTIONSTR,
156*0Sstevel@tonic-gate 		MNTTYPE_AUTOFS, &fni, sizeof (fni), obuf, MAX_MNTOPT_STR);
157*0Sstevel@tonic-gate 	if (error < 0) {
158*0Sstevel@tonic-gate 		perror("autofs mount");
159*0Sstevel@tonic-gate 		exit(1);
160*0Sstevel@tonic-gate 	}
161*0Sstevel@tonic-gate 	return (0);
162*0Sstevel@tonic-gate }
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate static void
165*0Sstevel@tonic-gate usage()
166*0Sstevel@tonic-gate {
167*0Sstevel@tonic-gate 	(void) fprintf(stderr,
168*0Sstevel@tonic-gate 	    "Usage: autofs mount [-r] [-o opts]  map  dir\n");
169*0Sstevel@tonic-gate 	exit(1);
170*0Sstevel@tonic-gate }
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate /*
173*0Sstevel@tonic-gate  * Remove pseudo-options "direct", "indirect", "nest", and "ignore" from
174*0Sstevel@tonic-gate  * option list.  Set *directp to 1 if "direct" is found, and 0 otherwise
175*0Sstevel@tonic-gate  * (mounts are indirect by default).  If both "direct" and "indirect" are
176*0Sstevel@tonic-gate  * found, the last one wins.
177*0Sstevel@tonic-gate  */
178*0Sstevel@tonic-gate static void
179*0Sstevel@tonic-gate process_opts(char *options, int *directp)
180*0Sstevel@tonic-gate {
181*0Sstevel@tonic-gate 	char *opt;
182*0Sstevel@tonic-gate 	char *opts;
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate 	if ((opts = strdup(options)) == NULL) {
185*0Sstevel@tonic-gate 		(void) fprintf(stderr,
186*0Sstevel@tonic-gate 				"autofs mount: memory allocation failed\n");
187*0Sstevel@tonic-gate 		exit(1);
188*0Sstevel@tonic-gate 	}
189*0Sstevel@tonic-gate 	options[0] = '\0';
190*0Sstevel@tonic-gate 	*directp = 0;
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 	while ((opt = strtok(opts, ",")) != NULL) {
193*0Sstevel@tonic-gate 		opts = NULL;
194*0Sstevel@tonic-gate 		while (isspace(*opt)) {
195*0Sstevel@tonic-gate 			opt++;
196*0Sstevel@tonic-gate 		}
197*0Sstevel@tonic-gate 		if (strcmp(opt, "direct") == 0) {
198*0Sstevel@tonic-gate 			*directp = 1;
199*0Sstevel@tonic-gate 		} else if (strcmp(opt, "indirect") == 0) {
200*0Sstevel@tonic-gate 			*directp = 0;
201*0Sstevel@tonic-gate 		} else if ((strcmp(opt, "nest") != 0) &&
202*0Sstevel@tonic-gate 				(strcmp(opt, "ignore") != 0)) {
203*0Sstevel@tonic-gate 			if (options[0] != '\0') {
204*0Sstevel@tonic-gate 				(void) strcat(options, ",");
205*0Sstevel@tonic-gate 			}
206*0Sstevel@tonic-gate 			(void) strcat(options, opt);
207*0Sstevel@tonic-gate 		}
208*0Sstevel@tonic-gate 	};
209*0Sstevel@tonic-gate }
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate /*
212*0Sstevel@tonic-gate  * Concatenate two options strings, with a comma between them.
213*0Sstevel@tonic-gate  */
214*0Sstevel@tonic-gate static char *
215*0Sstevel@tonic-gate concat_opts(const char *opts1, const char *opts2)
216*0Sstevel@tonic-gate {
217*0Sstevel@tonic-gate 	char *opts = malloc(strlen(opts1) + strlen(opts2) + 2);
218*0Sstevel@tonic-gate 	if (opts == NULL) {
219*0Sstevel@tonic-gate 		(void) fprintf(stderr,
220*0Sstevel@tonic-gate 			"autofs mount: memory allocation failed\n");
221*0Sstevel@tonic-gate 		exit(1);
222*0Sstevel@tonic-gate 	}
223*0Sstevel@tonic-gate 	strcpy(opts, opts1);
224*0Sstevel@tonic-gate 	if (opts1[0] != '\0' && opts2[0] != '\0') {
225*0Sstevel@tonic-gate 		strcat(opts, ",");
226*0Sstevel@tonic-gate 	}
227*0Sstevel@tonic-gate 	return (strcat(opts, opts2));
228*0Sstevel@tonic-gate }
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate /*
231*0Sstevel@tonic-gate  * check the options string for 'ro' options
232*0Sstevel@tonic-gate  * if present returns 1 otherwise return 0;
233*0Sstevel@tonic-gate  */
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate static int
236*0Sstevel@tonic-gate ro_given(char *options)
237*0Sstevel@tonic-gate {
238*0Sstevel@tonic-gate 	char	*op = options;
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate 	if (!*op)
241*0Sstevel@tonic-gate 		return (0);
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate 	while (op != 0) {
244*0Sstevel@tonic-gate 		if (*op == 'r' && *(op+1) == 'o' &&
245*0Sstevel@tonic-gate 			(*(op+2) == ',' || *(op+2) == '\0'))
246*0Sstevel@tonic-gate 			return (1);
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate 		if ((op = strchr(op, ',')) != NULL)
249*0Sstevel@tonic-gate 			op++;
250*0Sstevel@tonic-gate 	}
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate 
253*0Sstevel@tonic-gate 	return (0);
254*0Sstevel@tonic-gate }
255