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 #pragma ident "%Z%%M% %I% %E% SMI" 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate #include <stdio.h> 337c478bd9Sstevel@tonic-gate #include <unistd.h> 347c478bd9Sstevel@tonic-gate #include <stdlib.h> 357c478bd9Sstevel@tonic-gate #include <ctype.h> 367c478bd9Sstevel@tonic-gate #include <sys/param.h> 377c478bd9Sstevel@tonic-gate #include <sys/stat.h> 387c478bd9Sstevel@tonic-gate #include <sys/mntent.h> 397c478bd9Sstevel@tonic-gate #include <sys/mnttab.h> 407c478bd9Sstevel@tonic-gate #include <sys/mount.h> 417c478bd9Sstevel@tonic-gate #include <sys/utsname.h> 427c478bd9Sstevel@tonic-gate #include <sys/tiuser.h> 437c478bd9Sstevel@tonic-gate #include <string.h> 447c478bd9Sstevel@tonic-gate #include <fslib.h> 457c478bd9Sstevel@tonic-gate #include <errno.h> 467c478bd9Sstevel@tonic-gate #include <rpcsvc/daemon_utils.h> 477c478bd9Sstevel@tonic-gate #include "automount.h" 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate #define MNTTAB_OPTS "ignore,nest" 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate static void usage(); 527c478bd9Sstevel@tonic-gate static void process_opts(char *options, int *directp); 537c478bd9Sstevel@tonic-gate static char *concat_opts(const char *opts1, const char *opts2); 547c478bd9Sstevel@tonic-gate static int ro_given(char *options); 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate /* 577c478bd9Sstevel@tonic-gate * list of support services needed 587c478bd9Sstevel@tonic-gate */ 597c478bd9Sstevel@tonic-gate static char *service_list[] = { AUTOMOUNTD, NULL }; 607c478bd9Sstevel@tonic-gate 61*11606941Sjwahlig int 62*11606941Sjwahlig main(int argc, char *argv[]) 637c478bd9Sstevel@tonic-gate { 647c478bd9Sstevel@tonic-gate int error; 657c478bd9Sstevel@tonic-gate int c; 667c478bd9Sstevel@tonic-gate int mntflags = 0; 677c478bd9Sstevel@tonic-gate int nmflg = 0; 687c478bd9Sstevel@tonic-gate int roflg = 0; 697c478bd9Sstevel@tonic-gate char *mntpnt, *mapname; 707c478bd9Sstevel@tonic-gate struct utsname utsname; 717c478bd9Sstevel@tonic-gate char autofs_addr[MAXADDRLEN]; 727c478bd9Sstevel@tonic-gate struct autofs_args fni; 737c478bd9Sstevel@tonic-gate char *options = ""; 747c478bd9Sstevel@tonic-gate int mount_timeout = AUTOFS_MOUNT_TIMEOUT; 757c478bd9Sstevel@tonic-gate char obuf[MAX_MNTOPT_STR]; 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "o:mrq")) != EOF) { 787c478bd9Sstevel@tonic-gate switch (c) { 797c478bd9Sstevel@tonic-gate case '?': 807c478bd9Sstevel@tonic-gate usage(); 817c478bd9Sstevel@tonic-gate exit(1); 827c478bd9Sstevel@tonic-gate /* NOTREACHED */ 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate case 'o': 857c478bd9Sstevel@tonic-gate options = optarg; 867c478bd9Sstevel@tonic-gate break; 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate case 'm': 897c478bd9Sstevel@tonic-gate nmflg++; 907c478bd9Sstevel@tonic-gate break; 917c478bd9Sstevel@tonic-gate case 'r': /* converted to -o ro always */ 927c478bd9Sstevel@tonic-gate roflg++; 937c478bd9Sstevel@tonic-gate break; 947c478bd9Sstevel@tonic-gate /* 957c478bd9Sstevel@tonic-gate * The "quiet" flag can be ignored, since this 967c478bd9Sstevel@tonic-gate * program never complains about invalid -o options 977c478bd9Sstevel@tonic-gate * anyway. 987c478bd9Sstevel@tonic-gate */ 997c478bd9Sstevel@tonic-gate case 'q': 1007c478bd9Sstevel@tonic-gate break; 1017c478bd9Sstevel@tonic-gate 1027c478bd9Sstevel@tonic-gate default: 1037c478bd9Sstevel@tonic-gate usage(); 1047c478bd9Sstevel@tonic-gate } 1057c478bd9Sstevel@tonic-gate } 1067c478bd9Sstevel@tonic-gate if (argc - optind != 2) 1077c478bd9Sstevel@tonic-gate usage(); 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate mapname = argv[optind]; 1107c478bd9Sstevel@tonic-gate mntpnt = argv[optind + 1]; 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate if (strcmp(mntpnt, "/-") == 0) { 1137c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "invalid mountpoint: /-\n"); 1147c478bd9Sstevel@tonic-gate exit(1); 1157c478bd9Sstevel@tonic-gate } 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate if (uname(&utsname) < 0) { 1187c478bd9Sstevel@tonic-gate perror("uname"); 1197c478bd9Sstevel@tonic-gate exit(1); 1207c478bd9Sstevel@tonic-gate } 1217c478bd9Sstevel@tonic-gate (void) strcpy(autofs_addr, utsname.nodename); 1227c478bd9Sstevel@tonic-gate (void) strcat(autofs_addr, ".autofs"); 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate process_opts(options, &fni.direct); 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate if (roflg && !ro_given(options)) 1277c478bd9Sstevel@tonic-gate options = concat_opts(options, "ro"); 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate fni.addr.buf = autofs_addr; 1307c478bd9Sstevel@tonic-gate fni.addr.len = strlen(fni.addr.buf); 1317c478bd9Sstevel@tonic-gate fni.addr.maxlen = fni.addr.len; 1327c478bd9Sstevel@tonic-gate fni.path = mntpnt; 1337c478bd9Sstevel@tonic-gate fni.opts = options; 1347c478bd9Sstevel@tonic-gate fni.map = mapname; 1357c478bd9Sstevel@tonic-gate fni.subdir = ""; 1367c478bd9Sstevel@tonic-gate if (fni.direct) 1377c478bd9Sstevel@tonic-gate fni.key = mntpnt; 1387c478bd9Sstevel@tonic-gate else 1397c478bd9Sstevel@tonic-gate fni.key = ""; 1407c478bd9Sstevel@tonic-gate fni.mount_to = mount_timeout; 1417c478bd9Sstevel@tonic-gate fni.rpc_to = AUTOFS_RPC_TIMEOUT; 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate strcpy(obuf, options); 1447c478bd9Sstevel@tonic-gate if (*obuf != '\0') 1457c478bd9Sstevel@tonic-gate strcat(obuf, ","); 1467c478bd9Sstevel@tonic-gate strcat(obuf, 1477c478bd9Sstevel@tonic-gate fni.direct ? MNTTAB_OPTS ",direct" : MNTTAB_OPTS ",indirect"); 1487c478bd9Sstevel@tonic-gate 1497c478bd9Sstevel@tonic-gate /* 1507c478bd9Sstevel@tonic-gate * enable services as needed. 1517c478bd9Sstevel@tonic-gate */ 1527c478bd9Sstevel@tonic-gate _check_services(service_list); 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate error = mount(fni.map, mntpnt, mntflags | MS_DATA | MS_OPTIONSTR, 1557c478bd9Sstevel@tonic-gate MNTTYPE_AUTOFS, &fni, sizeof (fni), obuf, MAX_MNTOPT_STR); 1567c478bd9Sstevel@tonic-gate if (error < 0) { 1577c478bd9Sstevel@tonic-gate perror("autofs mount"); 1587c478bd9Sstevel@tonic-gate exit(1); 1597c478bd9Sstevel@tonic-gate } 1607c478bd9Sstevel@tonic-gate return (0); 1617c478bd9Sstevel@tonic-gate } 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate static void 1647c478bd9Sstevel@tonic-gate usage() 1657c478bd9Sstevel@tonic-gate { 1667c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1677c478bd9Sstevel@tonic-gate "Usage: autofs mount [-r] [-o opts] map dir\n"); 1687c478bd9Sstevel@tonic-gate exit(1); 1697c478bd9Sstevel@tonic-gate } 1707c478bd9Sstevel@tonic-gate 1717c478bd9Sstevel@tonic-gate /* 1727c478bd9Sstevel@tonic-gate * Remove pseudo-options "direct", "indirect", "nest", and "ignore" from 1737c478bd9Sstevel@tonic-gate * option list. Set *directp to 1 if "direct" is found, and 0 otherwise 1747c478bd9Sstevel@tonic-gate * (mounts are indirect by default). If both "direct" and "indirect" are 1757c478bd9Sstevel@tonic-gate * found, the last one wins. 1767c478bd9Sstevel@tonic-gate */ 1777c478bd9Sstevel@tonic-gate static void 1787c478bd9Sstevel@tonic-gate process_opts(char *options, int *directp) 1797c478bd9Sstevel@tonic-gate { 1807c478bd9Sstevel@tonic-gate char *opt; 1817c478bd9Sstevel@tonic-gate char *opts; 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate if ((opts = strdup(options)) == NULL) { 1847c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1857c478bd9Sstevel@tonic-gate "autofs mount: memory allocation failed\n"); 1867c478bd9Sstevel@tonic-gate exit(1); 1877c478bd9Sstevel@tonic-gate } 1887c478bd9Sstevel@tonic-gate options[0] = '\0'; 1897c478bd9Sstevel@tonic-gate *directp = 0; 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate while ((opt = strtok(opts, ",")) != NULL) { 1927c478bd9Sstevel@tonic-gate opts = NULL; 1937c478bd9Sstevel@tonic-gate while (isspace(*opt)) { 1947c478bd9Sstevel@tonic-gate opt++; 1957c478bd9Sstevel@tonic-gate } 1967c478bd9Sstevel@tonic-gate if (strcmp(opt, "direct") == 0) { 1977c478bd9Sstevel@tonic-gate *directp = 1; 1987c478bd9Sstevel@tonic-gate } else if (strcmp(opt, "indirect") == 0) { 1997c478bd9Sstevel@tonic-gate *directp = 0; 2007c478bd9Sstevel@tonic-gate } else if ((strcmp(opt, "nest") != 0) && 2017c478bd9Sstevel@tonic-gate (strcmp(opt, "ignore") != 0)) { 2027c478bd9Sstevel@tonic-gate if (options[0] != '\0') { 2037c478bd9Sstevel@tonic-gate (void) strcat(options, ","); 2047c478bd9Sstevel@tonic-gate } 2057c478bd9Sstevel@tonic-gate (void) strcat(options, opt); 2067c478bd9Sstevel@tonic-gate } 2077c478bd9Sstevel@tonic-gate }; 2087c478bd9Sstevel@tonic-gate } 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate /* 2117c478bd9Sstevel@tonic-gate * Concatenate two options strings, with a comma between them. 2127c478bd9Sstevel@tonic-gate */ 2137c478bd9Sstevel@tonic-gate static char * 2147c478bd9Sstevel@tonic-gate concat_opts(const char *opts1, const char *opts2) 2157c478bd9Sstevel@tonic-gate { 2167c478bd9Sstevel@tonic-gate char *opts = malloc(strlen(opts1) + strlen(opts2) + 2); 2177c478bd9Sstevel@tonic-gate if (opts == NULL) { 2187c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 2197c478bd9Sstevel@tonic-gate "autofs mount: memory allocation failed\n"); 2207c478bd9Sstevel@tonic-gate exit(1); 2217c478bd9Sstevel@tonic-gate } 2227c478bd9Sstevel@tonic-gate strcpy(opts, opts1); 2237c478bd9Sstevel@tonic-gate if (opts1[0] != '\0' && opts2[0] != '\0') { 2247c478bd9Sstevel@tonic-gate strcat(opts, ","); 2257c478bd9Sstevel@tonic-gate } 2267c478bd9Sstevel@tonic-gate return (strcat(opts, opts2)); 2277c478bd9Sstevel@tonic-gate } 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate /* 2307c478bd9Sstevel@tonic-gate * check the options string for 'ro' options 2317c478bd9Sstevel@tonic-gate * if present returns 1 otherwise return 0; 2327c478bd9Sstevel@tonic-gate */ 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate static int 2357c478bd9Sstevel@tonic-gate ro_given(char *options) 2367c478bd9Sstevel@tonic-gate { 2377c478bd9Sstevel@tonic-gate char *op = options; 2387c478bd9Sstevel@tonic-gate 2397c478bd9Sstevel@tonic-gate if (!*op) 2407c478bd9Sstevel@tonic-gate return (0); 2417c478bd9Sstevel@tonic-gate 2427c478bd9Sstevel@tonic-gate while (op != 0) { 2437c478bd9Sstevel@tonic-gate if (*op == 'r' && *(op+1) == 'o' && 2447c478bd9Sstevel@tonic-gate (*(op+2) == ',' || *(op+2) == '\0')) 2457c478bd9Sstevel@tonic-gate return (1); 2467c478bd9Sstevel@tonic-gate 2477c478bd9Sstevel@tonic-gate if ((op = strchr(op, ',')) != NULL) 2487c478bd9Sstevel@tonic-gate op++; 2497c478bd9Sstevel@tonic-gate } 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate 2527c478bd9Sstevel@tonic-gate return (0); 2537c478bd9Sstevel@tonic-gate } 254