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