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 * Copyright (c) 2018, Joyent, Inc. 28 * Copyright 2019 Nexenta by DDN, Inc. All rights reserved. 29 */ 30 31 /* 32 * smbfs umount 33 */ 34 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include <string.h> 38 #include <stdarg.h> 39 #include <signal.h> 40 #include <unistd.h> 41 #include <kstat.h> 42 #include <rpc/rpc.h> 43 #include <sys/mnttab.h> 44 #include <sys/mount.h> 45 #include <sys/mntent.h> 46 #include <errno.h> 47 #include <locale.h> 48 #include <fslib.h> 49 #include <priv_utils.h> 50 51 #define RET_OK 0 52 #define RET_ERR 32 53 54 static void pr_err(const char *fmt, ...); 55 static void usage(); 56 static int smbfs_unmount(char *, int); 57 static struct extmnttab *mnttab_find(); 58 59 int 60 main(int argc, char *argv[]) 61 { 62 extern int optind; 63 int c; 64 int umnt_flag = 0; 65 66 (void) setlocale(LC_ALL, ""); 67 68 #if !defined(TEXT_DOMAIN) 69 #define TEXT_DOMAIN "SYS_TEST" 70 #endif 71 (void) textdomain(TEXT_DOMAIN); 72 73 /* 74 * Normal users are allowed to umount smbfs mounts they own. 75 * To allow that, this program has an exec_attr that adds 76 * SYS_MOUNT privilege. 77 * 78 * The __init_suid_priv call was designed for SUID programs, 79 * but also works for privileges granted via exec_attr with 80 * one difference: the added privileges are already effective 81 * when the program starts, and remain effective after the call. 82 * To make this work more like the SUID case we'll turn off the 83 * additional privileges with a __priv_bracket() call here. 84 * Later calls to __priv_bracket() make the extra privileges 85 * effective only when we need them. 86 */ 87 if (__init_suid_priv(0, PRIV_SYS_MOUNT, (char *)NULL) < 0) { 88 (void) fprintf(stderr, 89 gettext("Insufficient privileges, " 90 "%s should have sys_mount privilege via exec_attr\n"), 91 argv[0]); 92 exit(RET_ERR); 93 } 94 (void) __priv_bracket(PRIV_OFF); 95 96 /* 97 * Set options 98 */ 99 while ((c = getopt(argc, argv, "f")) != EOF) { 100 switch (c) { 101 case 'f': 102 umnt_flag |= MS_FORCE; /* forced unmount is desired */ 103 break; 104 default: 105 usage(); 106 exit(RET_ERR); 107 } 108 } 109 if (argc - optind != 1) { 110 usage(); 111 exit(RET_ERR); 112 } 113 114 return (smbfs_unmount(argv[optind], umnt_flag)); 115 } 116 117 static void 118 pr_err(const char *fmt, ...) 119 { 120 va_list ap; 121 122 va_start(ap, fmt); 123 (void) fprintf(stderr, "smbfs/umount: "); 124 (void) vfprintf(stderr, fmt, ap); 125 (void) fflush(stderr); 126 va_end(ap); 127 } 128 129 static void 130 usage() 131 { 132 (void) fprintf(stderr, 133 gettext("Usage: smbfs umount [-o opts] {//server/share | dir}\n")); 134 exit(RET_ERR); 135 } 136 137 static int 138 smbfs_unmount(char *pathname, int umnt_flag) 139 { 140 struct extmnttab *mntp; 141 int rc; 142 143 mntp = mnttab_find(pathname); 144 if (mntp) { 145 pathname = mntp->mnt_mountp; 146 } 147 148 /* Need sys_mount privilege for the umount call. */ 149 (void) __priv_bracket(PRIV_ON); 150 rc = umount2(pathname, umnt_flag); 151 (void) __priv_bracket(PRIV_OFF); 152 153 if (rc < 0) { 154 pr_err(gettext("%s: %s\n"), pathname, strerror(errno)); 155 return (RET_ERR); 156 } 157 158 return (RET_OK); 159 } 160 161 /* 162 * Find the mnttab entry that corresponds to "name". 163 * We're not sure what the name represents: either 164 * a mountpoint name, or a special name (server:/path). 165 * Return the last entry in the file that matches. 166 */ 167 static struct extmnttab * 168 mnttab_find(char *dirname) 169 { 170 FILE *fp; 171 struct extmnttab mnt; 172 struct extmnttab *res = NULL; 173 174 fp = fopen(MNTTAB, "r"); 175 if (fp == NULL) { 176 pr_err("%s: %s\n", MNTTAB, strerror(errno)); 177 return (NULL); 178 } 179 while (getextmntent(fp, &mnt, sizeof (struct extmnttab)) == 0) { 180 if (strcmp(mnt.mnt_mountp, dirname) == 0 || 181 strcmp(mnt.mnt_special, dirname) == 0) { 182 if (res) 183 fsfreemnttab(res); 184 res = fsdupmnttab(&mnt); 185 } 186 } 187 188 (void) fclose(fp); 189 return (res); 190 } 191