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
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate *
267c478bd9Sstevel@tonic-gate * autofs mount.c
277c478bd9Sstevel@tonic-gate *
287c478bd9Sstevel@tonic-gate */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <ctype.h>
347c478bd9Sstevel@tonic-gate #include <sys/param.h>
357c478bd9Sstevel@tonic-gate #include <sys/stat.h>
367c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
377c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
387c478bd9Sstevel@tonic-gate #include <sys/mount.h>
397c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
407c478bd9Sstevel@tonic-gate #include <sys/tiuser.h>
417c478bd9Sstevel@tonic-gate #include <string.h>
427c478bd9Sstevel@tonic-gate #include <fslib.h>
437c478bd9Sstevel@tonic-gate #include <errno.h>
447c478bd9Sstevel@tonic-gate #include <rpcsvc/daemon_utils.h>
457c478bd9Sstevel@tonic-gate #include "automount.h"
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate #define MNTTAB_OPTS "ignore,nest"
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate static void usage();
507c478bd9Sstevel@tonic-gate static void process_opts(char *options, int *directp);
517c478bd9Sstevel@tonic-gate static char *concat_opts(const char *opts1, const char *opts2);
527c478bd9Sstevel@tonic-gate static int ro_given(char *options);
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate /*
557c478bd9Sstevel@tonic-gate * list of support services needed
567c478bd9Sstevel@tonic-gate */
577c478bd9Sstevel@tonic-gate static char *service_list[] = { AUTOMOUNTD, NULL };
587c478bd9Sstevel@tonic-gate
59*11606941Sjwahlig int
main(int argc,char * argv[])60*11606941Sjwahlig main(int argc, char *argv[])
617c478bd9Sstevel@tonic-gate {
627c478bd9Sstevel@tonic-gate int error;
637c478bd9Sstevel@tonic-gate int c;
647c478bd9Sstevel@tonic-gate int mntflags = 0;
657c478bd9Sstevel@tonic-gate int nmflg = 0;
667c478bd9Sstevel@tonic-gate int roflg = 0;
677c478bd9Sstevel@tonic-gate char *mntpnt, *mapname;
687c478bd9Sstevel@tonic-gate struct utsname utsname;
697c478bd9Sstevel@tonic-gate char autofs_addr[MAXADDRLEN];
707c478bd9Sstevel@tonic-gate struct autofs_args fni;
717c478bd9Sstevel@tonic-gate char *options = "";
727c478bd9Sstevel@tonic-gate int mount_timeout = AUTOFS_MOUNT_TIMEOUT;
737c478bd9Sstevel@tonic-gate char obuf[MAX_MNTOPT_STR];
747c478bd9Sstevel@tonic-gate
757c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "o:mrq")) != EOF) {
767c478bd9Sstevel@tonic-gate switch (c) {
777c478bd9Sstevel@tonic-gate case '?':
787c478bd9Sstevel@tonic-gate usage();
797c478bd9Sstevel@tonic-gate exit(1);
807c478bd9Sstevel@tonic-gate /* NOTREACHED */
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate case 'o':
837c478bd9Sstevel@tonic-gate options = optarg;
847c478bd9Sstevel@tonic-gate break;
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate case 'm':
877c478bd9Sstevel@tonic-gate nmflg++;
887c478bd9Sstevel@tonic-gate break;
897c478bd9Sstevel@tonic-gate case 'r': /* converted to -o ro always */
907c478bd9Sstevel@tonic-gate roflg++;
917c478bd9Sstevel@tonic-gate break;
927c478bd9Sstevel@tonic-gate /*
937c478bd9Sstevel@tonic-gate * The "quiet" flag can be ignored, since this
947c478bd9Sstevel@tonic-gate * program never complains about invalid -o options
957c478bd9Sstevel@tonic-gate * anyway.
967c478bd9Sstevel@tonic-gate */
977c478bd9Sstevel@tonic-gate case 'q':
987c478bd9Sstevel@tonic-gate break;
997c478bd9Sstevel@tonic-gate
1007c478bd9Sstevel@tonic-gate default:
1017c478bd9Sstevel@tonic-gate usage();
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate if (argc - optind != 2)
1057c478bd9Sstevel@tonic-gate usage();
1067c478bd9Sstevel@tonic-gate
1077c478bd9Sstevel@tonic-gate mapname = argv[optind];
1087c478bd9Sstevel@tonic-gate mntpnt = argv[optind + 1];
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate if (strcmp(mntpnt, "/-") == 0) {
1117c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "invalid mountpoint: /-\n");
1127c478bd9Sstevel@tonic-gate exit(1);
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate if (uname(&utsname) < 0) {
1167c478bd9Sstevel@tonic-gate perror("uname");
1177c478bd9Sstevel@tonic-gate exit(1);
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate (void) strcpy(autofs_addr, utsname.nodename);
1207c478bd9Sstevel@tonic-gate (void) strcat(autofs_addr, ".autofs");
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate process_opts(options, &fni.direct);
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate if (roflg && !ro_given(options))
1257c478bd9Sstevel@tonic-gate options = concat_opts(options, "ro");
1267c478bd9Sstevel@tonic-gate
1277c478bd9Sstevel@tonic-gate fni.addr.buf = autofs_addr;
1287c478bd9Sstevel@tonic-gate fni.addr.len = strlen(fni.addr.buf);
1297c478bd9Sstevel@tonic-gate fni.addr.maxlen = fni.addr.len;
1307c478bd9Sstevel@tonic-gate fni.path = mntpnt;
1317c478bd9Sstevel@tonic-gate fni.opts = options;
1327c478bd9Sstevel@tonic-gate fni.map = mapname;
1337c478bd9Sstevel@tonic-gate fni.subdir = "";
1347c478bd9Sstevel@tonic-gate if (fni.direct)
1357c478bd9Sstevel@tonic-gate fni.key = mntpnt;
1367c478bd9Sstevel@tonic-gate else
1377c478bd9Sstevel@tonic-gate fni.key = "";
1387c478bd9Sstevel@tonic-gate fni.mount_to = mount_timeout;
1397c478bd9Sstevel@tonic-gate fni.rpc_to = AUTOFS_RPC_TIMEOUT;
1407c478bd9Sstevel@tonic-gate
1417c478bd9Sstevel@tonic-gate strcpy(obuf, options);
1427c478bd9Sstevel@tonic-gate if (*obuf != '\0')
1437c478bd9Sstevel@tonic-gate strcat(obuf, ",");
1447c478bd9Sstevel@tonic-gate strcat(obuf,
1457c478bd9Sstevel@tonic-gate fni.direct ? MNTTAB_OPTS ",direct" : MNTTAB_OPTS ",indirect");
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate * enable services as needed.
1497c478bd9Sstevel@tonic-gate */
1507c478bd9Sstevel@tonic-gate _check_services(service_list);
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate error = mount(fni.map, mntpnt, mntflags | MS_DATA | MS_OPTIONSTR,
1537c478bd9Sstevel@tonic-gate MNTTYPE_AUTOFS, &fni, sizeof (fni), obuf, MAX_MNTOPT_STR);
1547c478bd9Sstevel@tonic-gate if (error < 0) {
1557c478bd9Sstevel@tonic-gate perror("autofs mount");
1567c478bd9Sstevel@tonic-gate exit(1);
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate return (0);
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate
1617c478bd9Sstevel@tonic-gate static void
usage()1627c478bd9Sstevel@tonic-gate usage()
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1657c478bd9Sstevel@tonic-gate "Usage: autofs mount [-r] [-o opts] map dir\n");
1667c478bd9Sstevel@tonic-gate exit(1);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate
1697c478bd9Sstevel@tonic-gate /*
1707c478bd9Sstevel@tonic-gate * Remove pseudo-options "direct", "indirect", "nest", and "ignore" from
1717c478bd9Sstevel@tonic-gate * option list. Set *directp to 1 if "direct" is found, and 0 otherwise
1727c478bd9Sstevel@tonic-gate * (mounts are indirect by default). If both "direct" and "indirect" are
1737c478bd9Sstevel@tonic-gate * found, the last one wins.
1747c478bd9Sstevel@tonic-gate */
1757c478bd9Sstevel@tonic-gate static void
process_opts(char * options,int * directp)1767c478bd9Sstevel@tonic-gate process_opts(char *options, int *directp)
1777c478bd9Sstevel@tonic-gate {
1787c478bd9Sstevel@tonic-gate char *opt;
1797c478bd9Sstevel@tonic-gate char *opts;
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate if ((opts = strdup(options)) == NULL) {
1827c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1837c478bd9Sstevel@tonic-gate "autofs mount: memory allocation failed\n");
1847c478bd9Sstevel@tonic-gate exit(1);
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate options[0] = '\0';
1877c478bd9Sstevel@tonic-gate *directp = 0;
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate while ((opt = strtok(opts, ",")) != NULL) {
1907c478bd9Sstevel@tonic-gate opts = NULL;
1917c478bd9Sstevel@tonic-gate while (isspace(*opt)) {
1927c478bd9Sstevel@tonic-gate opt++;
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate if (strcmp(opt, "direct") == 0) {
1957c478bd9Sstevel@tonic-gate *directp = 1;
1967c478bd9Sstevel@tonic-gate } else if (strcmp(opt, "indirect") == 0) {
1977c478bd9Sstevel@tonic-gate *directp = 0;
1987c478bd9Sstevel@tonic-gate } else if ((strcmp(opt, "nest") != 0) &&
1997c478bd9Sstevel@tonic-gate (strcmp(opt, "ignore") != 0)) {
2007c478bd9Sstevel@tonic-gate if (options[0] != '\0') {
2017c478bd9Sstevel@tonic-gate (void) strcat(options, ",");
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate (void) strcat(options, opt);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate };
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate
2087c478bd9Sstevel@tonic-gate /*
2097c478bd9Sstevel@tonic-gate * Concatenate two options strings, with a comma between them.
2107c478bd9Sstevel@tonic-gate */
2117c478bd9Sstevel@tonic-gate static char *
concat_opts(const char * opts1,const char * opts2)2127c478bd9Sstevel@tonic-gate concat_opts(const char *opts1, const char *opts2)
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate char *opts = malloc(strlen(opts1) + strlen(opts2) + 2);
2157c478bd9Sstevel@tonic-gate if (opts == NULL) {
2167c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
2177c478bd9Sstevel@tonic-gate "autofs mount: memory allocation failed\n");
2187c478bd9Sstevel@tonic-gate exit(1);
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate strcpy(opts, opts1);
2217c478bd9Sstevel@tonic-gate if (opts1[0] != '\0' && opts2[0] != '\0') {
2227c478bd9Sstevel@tonic-gate strcat(opts, ",");
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate return (strcat(opts, opts2));
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate
2277c478bd9Sstevel@tonic-gate /*
2287c478bd9Sstevel@tonic-gate * check the options string for 'ro' options
2297c478bd9Sstevel@tonic-gate * if present returns 1 otherwise return 0;
2307c478bd9Sstevel@tonic-gate */
2317c478bd9Sstevel@tonic-gate
2327c478bd9Sstevel@tonic-gate static int
ro_given(char * options)2337c478bd9Sstevel@tonic-gate ro_given(char *options)
2347c478bd9Sstevel@tonic-gate {
2357c478bd9Sstevel@tonic-gate char *op = options;
2367c478bd9Sstevel@tonic-gate
2377c478bd9Sstevel@tonic-gate if (!*op)
2387c478bd9Sstevel@tonic-gate return (0);
2397c478bd9Sstevel@tonic-gate
2407c478bd9Sstevel@tonic-gate while (op != 0) {
2417c478bd9Sstevel@tonic-gate if (*op == 'r' && *(op+1) == 'o' &&
2427c478bd9Sstevel@tonic-gate (*(op+2) == ',' || *(op+2) == '\0'))
2437c478bd9Sstevel@tonic-gate return (1);
2447c478bd9Sstevel@tonic-gate
2457c478bd9Sstevel@tonic-gate if ((op = strchr(op, ',')) != NULL)
2467c478bd9Sstevel@tonic-gate op++;
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate
2497c478bd9Sstevel@tonic-gate
2507c478bd9Sstevel@tonic-gate return (0);
2517c478bd9Sstevel@tonic-gate }
252