1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* 27 * smbfs umount 28 */ 29 30 #include <stdio.h> 31 #include <stdlib.h> 32 #include <string.h> 33 #include <stdarg.h> 34 #include <signal.h> 35 #include <unistd.h> 36 #include <kstat.h> 37 #include <rpc/rpc.h> 38 #include <sys/mnttab.h> 39 #include <sys/mount.h> 40 #include <sys/mntent.h> 41 #include <errno.h> 42 #include <locale.h> 43 #include <fslib.h> 44 #include <priv_utils.h> 45 46 #define RET_OK 0 47 #define RET_ERR 32 48 49 static void pr_err(const char *fmt, ...); 50 static void usage(); 51 static int smbfs_unmount(char *, int); 52 static struct extmnttab *mnttab_find(); 53 54 static char *myname; 55 static char typename[64]; 56 57 int 58 main(int argc, char *argv[]) 59 { 60 extern int optind; 61 int c; 62 int umnt_flag = 0; 63 64 (void) setlocale(LC_ALL, ""); 65 66 #if !defined(TEXT_DOMAIN) 67 #define TEXT_DOMAIN "SYS_TEST" 68 #endif 69 (void) textdomain(TEXT_DOMAIN); 70 71 /* 72 * Normal users are allowed to umount smbfs mounts they own. 73 * To allow that, this program is installed setuid root, and 74 * it adds SYS_MOUNT privilege here (if needed), and then 75 * restores the user's normal privileges. 76 */ 77 if (__init_suid_priv(0, PRIV_SYS_MOUNT, (char *)NULL) < 0) { 78 (void) fprintf(stderr, 79 gettext("Insufficient privileges, " 80 "%s must be set-uid root\n"), argv[0]); 81 exit(RET_ERR); 82 } 83 84 myname = strrchr(argv[0], '/'); 85 myname = myname ? myname+1 : argv[0]; 86 (void) sprintf(typename, "smbfs %s", myname); 87 argv[0] = typename; 88 89 /* 90 * Set options 91 */ 92 while ((c = getopt(argc, argv, "f")) != EOF) { 93 switch (c) { 94 case 'f': 95 umnt_flag |= MS_FORCE; /* forced unmount is desired */ 96 break; 97 default: 98 usage(); 99 exit(RET_ERR); 100 } 101 } 102 if (argc - optind != 1) { 103 usage(); 104 exit(RET_ERR); 105 } 106 107 return (smbfs_unmount(argv[optind], umnt_flag)); 108 } 109 110 static void 111 pr_err(const char *fmt, ...) 112 { 113 va_list ap; 114 115 va_start(ap, fmt); 116 (void) fprintf(stderr, "%s: ", typename); 117 (void) vfprintf(stderr, fmt, ap); 118 (void) fflush(stderr); 119 va_end(ap); 120 } 121 122 static void 123 usage() 124 { 125 (void) fprintf(stderr, 126 gettext("Usage: smbfs umount [-o opts] {//server/share | dir}\n")); 127 exit(RET_ERR); 128 } 129 130 static int 131 smbfs_unmount(char *pathname, int umnt_flag) 132 { 133 struct extmnttab *mntp; 134 int rc; 135 136 mntp = mnttab_find(pathname); 137 if (mntp) { 138 pathname = mntp->mnt_mountp; 139 } 140 141 /* Need sys_mount privilege for the umount call. */ 142 (void) __priv_bracket(PRIV_ON); 143 rc = umount2(pathname, umnt_flag); 144 (void) __priv_bracket(PRIV_OFF); 145 146 if (rc < 0) { 147 pr_err(gettext("%s: %s\n"), pathname, strerror(errno)); 148 return (RET_ERR); 149 } 150 151 return (RET_OK); 152 } 153 154 /* 155 * Find the mnttab entry that corresponds to "name". 156 * We're not sure what the name represents: either 157 * a mountpoint name, or a special name (server:/path). 158 * Return the last entry in the file that matches. 159 */ 160 static struct extmnttab * 161 mnttab_find(dirname) 162 char *dirname; 163 { 164 FILE *fp; 165 struct extmnttab mnt; 166 struct extmnttab *res = NULL; 167 168 fp = fopen(MNTTAB, "r"); 169 if (fp == NULL) { 170 pr_err("%s: %s\n", MNTTAB, strerror(errno)); 171 return (NULL); 172 } 173 while (getextmntent(fp, &mnt, sizeof (struct extmnttab)) == 0) { 174 if (strcmp(mnt.mnt_mountp, dirname) == 0 || 175 strcmp(mnt.mnt_special, dirname) == 0) { 176 if (res) 177 fsfreemnttab(res); 178 res = fsdupmnttab(&mnt); 179 } 180 } 181 182 fclose(fp); 183 return (res); 184 } 185