[312] | 1 | /*
|
---|
| 2 | Unmount utility program for Linux CIFS VFS (virtual filesystem) client
|
---|
| 3 | Copyright (C) 2005 Steve French (sfrench@us.ibm.com)
|
---|
| 4 |
|
---|
| 5 | This program is free software; you can redistribute it and/or modify
|
---|
| 6 | it under the terms of the GNU General Public License as published by
|
---|
| 7 | the Free Software Foundation; either version 2 of the License, or
|
---|
| 8 | (at your option) any later version.
|
---|
| 9 |
|
---|
| 10 | This program is distributed in the hope that it will be useful,
|
---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 13 | GNU General Public License for more details.
|
---|
| 14 |
|
---|
| 15 | You should have received a copy of the GNU General Public License
|
---|
| 16 | along with this program; if not, write to the Free Software
|
---|
| 17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
|
---|
| 18 |
|
---|
| 19 | #ifndef _GNU_SOURCE
|
---|
| 20 | #define _GNU_SOURCE
|
---|
| 21 | #endif
|
---|
| 22 |
|
---|
| 23 | #include <stdlib.h>
|
---|
| 24 | #include <stdio.h>
|
---|
| 25 | #include <unistd.h>
|
---|
| 26 | #include <ctype.h>
|
---|
| 27 | #include <sys/types.h>
|
---|
| 28 | #include <sys/mount.h>
|
---|
| 29 | #include <sys/ioctl.h>
|
---|
| 30 | #include <sys/stat.h>
|
---|
| 31 | #include <sys/vfs.h>
|
---|
| 32 | #include <fcntl.h>
|
---|
| 33 | #include <getopt.h>
|
---|
| 34 | #include <errno.h>
|
---|
| 35 | #include <string.h>
|
---|
| 36 | #include <mntent.h>
|
---|
| 37 | #include <limits.h>
|
---|
| 38 | #include "mount.h"
|
---|
| 39 |
|
---|
| 40 | #define UNMOUNT_CIFS_VERSION_MAJOR "0"
|
---|
| 41 | #define UNMOUNT_CIFS_VERSION_MINOR "6"
|
---|
| 42 |
|
---|
| 43 | #ifndef UNMOUNT_CIFS_VENDOR_SUFFIX
|
---|
| 44 | #ifdef _SAMBA_BUILD_
|
---|
| 45 | #include "include/version.h"
|
---|
| 46 | #ifdef SAMBA_VERSION_VENDOR_SUFFIX
|
---|
| 47 | #define UNMOUNT_CIFS_VENDOR_SUFFIX "-"SAMBA_VERSION_OFFICIAL_STRING"-"SAMBA_VERSION_VENDOR_SUFFIX
|
---|
| 48 | #else
|
---|
| 49 | #define UNMOUNT_CIFS_VENDOR_SUFFIX "-"SAMBA_VERSION_OFFICIAL_STRING
|
---|
| 50 | #endif /* SAMBA_VERSION_OFFICIAL_STRING and SAMBA_VERSION_VENDOR_SUFFIX */
|
---|
| 51 | #else
|
---|
| 52 | #define UNMOUNT_CIFS_VENDOR_SUFFIX ""
|
---|
| 53 | #endif /* _SAMBA_BUILD_ */
|
---|
| 54 | #endif /* UNMOUNT_CIFS_VENDOR_SUFFIX */
|
---|
| 55 |
|
---|
| 56 | #ifndef MNT_DETACH
|
---|
| 57 | #define MNT_DETACH 0x02
|
---|
| 58 | #endif
|
---|
| 59 |
|
---|
| 60 | #ifndef MNT_EXPIRE
|
---|
| 61 | #define MNT_EXPIRE 0x04
|
---|
| 62 | #endif
|
---|
| 63 |
|
---|
| 64 | #ifndef MOUNTED_LOCK
|
---|
| 65 | #define MOUNTED_LOCK "/etc/mtab~"
|
---|
| 66 | #endif
|
---|
| 67 | #ifndef MOUNTED_TEMP
|
---|
| 68 | #define MOUNTED_TEMP "/etc/mtab.tmp"
|
---|
| 69 | #endif
|
---|
| 70 |
|
---|
| 71 | #define CIFS_IOC_CHECKUMOUNT _IO(0xCF, 2)
|
---|
| 72 | #define CIFS_MAGIC_NUMBER 0xFF534D42 /* the first four bytes of SMB PDU */
|
---|
| 73 |
|
---|
| 74 | static struct option longopts[] = {
|
---|
| 75 | { "all", 0, NULL, 'a' },
|
---|
| 76 | { "help",0, NULL, 'h' },
|
---|
| 77 | { "read-only", 0, NULL, 'r' },
|
---|
| 78 | { "ro", 0, NULL, 'r' },
|
---|
| 79 | { "verbose", 0, NULL, 'v' },
|
---|
| 80 | { "version", 0, NULL, 'V' },
|
---|
| 81 | { "expire", 0, NULL, 'e' },
|
---|
| 82 | { "force", 0, 0, 'f' },
|
---|
| 83 | { "lazy", 0, 0, 'l' },
|
---|
| 84 | { "no-mtab", 0, 0, 'n' },
|
---|
| 85 | { NULL, 0, NULL, 0 }
|
---|
| 86 | };
|
---|
| 87 |
|
---|
| 88 | const char * thisprogram;
|
---|
| 89 | int verboseflg = 0;
|
---|
| 90 |
|
---|
| 91 | static void umount_cifs_usage(void)
|
---|
| 92 | {
|
---|
| 93 | printf("\nUsage: %s <remotetarget> <dir>\n", thisprogram);
|
---|
| 94 | printf("\nUnmount the specified directory\n");
|
---|
| 95 | printf("\nLess commonly used options:");
|
---|
| 96 | printf("\n\t-r\tIf mount fails, retry with readonly remount.");
|
---|
| 97 | printf("\n\t-n\tDo not write to mtab.");
|
---|
| 98 | printf("\n\t-f\tAttempt a forced unmount, even if the fs is busy.");
|
---|
| 99 | printf("\n\t-l\tAttempt lazy unmount, Unmount now, cleanup later.");
|
---|
| 100 | printf("\n\t-v\tEnable verbose mode (may be useful for debugging).");
|
---|
| 101 | printf("\n\t-h\tDisplay this help.");
|
---|
| 102 | printf("\n\nOptions are described in more detail in the manual page");
|
---|
| 103 | printf("\n\tman 8 umount.cifs\n");
|
---|
| 104 | printf("\nTo display the version number of the cifs umount utility:");
|
---|
| 105 | printf("\n\t%s -V\n",thisprogram);
|
---|
| 106 | printf("\nInvoking the umount utility on cifs mounts, can execute");
|
---|
| 107 | printf(" /sbin/umount.cifs (if present and umount -i is not specified.\n");
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | static int umount_check_perm(char * dir)
|
---|
| 111 | {
|
---|
| 112 | int fileid;
|
---|
| 113 | int rc;
|
---|
| 114 |
|
---|
| 115 | /* allow root to unmount, no matter what */
|
---|
| 116 | if(getuid() == 0)
|
---|
| 117 | return 0;
|
---|
| 118 |
|
---|
| 119 | /* presumably can not chdir into the target as we do on mount */
|
---|
| 120 | fileid = open(dir, O_RDONLY | O_DIRECTORY | O_NOFOLLOW, 0);
|
---|
| 121 | if(fileid == -1) {
|
---|
| 122 | if(verboseflg)
|
---|
| 123 | printf("error opening mountpoint %d %s",errno,strerror(errno));
|
---|
| 124 | return errno;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | rc = ioctl(fileid, CIFS_IOC_CHECKUMOUNT, NULL);
|
---|
| 128 |
|
---|
| 129 | if(verboseflg)
|
---|
| 130 | printf("ioctl returned %d with errno %d %s\n",rc,errno,strerror(errno));
|
---|
| 131 |
|
---|
| 132 | if(rc == ENOTTY) {
|
---|
| 133 | printf("user unmounting via %s is an optional feature of",thisprogram);
|
---|
| 134 | printf(" the cifs filesystem driver (cifs.ko)");
|
---|
| 135 | printf("\n\tand requires cifs.ko version 1.32 or later\n");
|
---|
| 136 | } else if (rc != 0)
|
---|
| 137 | printf("user unmount of %s failed with %d %s\n",dir,errno,strerror(errno));
|
---|
| 138 | close(fileid);
|
---|
| 139 |
|
---|
| 140 | return rc;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | static int remove_from_mtab(char * mountpoint)
|
---|
| 144 | {
|
---|
| 145 | int rc;
|
---|
| 146 | int num_matches;
|
---|
| 147 | FILE * org_fd;
|
---|
| 148 | FILE * new_fd;
|
---|
| 149 | struct mntent * mount_entry;
|
---|
| 150 |
|
---|
| 151 | /* Do we need to check if it is a symlink to e.g. /proc/mounts
|
---|
| 152 | in which case we probably do not want to update it? */
|
---|
| 153 |
|
---|
| 154 | /* Do we first need to check if it is writable? */
|
---|
| 155 |
|
---|
| 156 | atexit(unlock_mtab);
|
---|
| 157 | if (lock_mtab()) {
|
---|
| 158 | printf("Mount table locked\n");
|
---|
| 159 | return -EACCES;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | if(verboseflg)
|
---|
| 163 | printf("attempting to remove from mtab\n");
|
---|
| 164 |
|
---|
| 165 | org_fd = setmntent(MOUNTED, "r");
|
---|
| 166 |
|
---|
| 167 | if(org_fd == NULL) {
|
---|
| 168 | printf("Can not open %s\n",MOUNTED);
|
---|
| 169 | unlock_mtab();
|
---|
| 170 | return -EIO;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | new_fd = setmntent(MOUNTED_TEMP,"w");
|
---|
| 174 | if(new_fd == NULL) {
|
---|
| 175 | printf("Can not open temp file %s", MOUNTED_TEMP);
|
---|
| 176 | endmntent(org_fd);
|
---|
| 177 | unlock_mtab();
|
---|
| 178 | return -EIO;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | /* BB fix so we only remove the last entry that matches BB */
|
---|
| 182 | num_matches = 0;
|
---|
| 183 | while((mount_entry = getmntent(org_fd)) != NULL) {
|
---|
| 184 | if(strcmp(mount_entry->mnt_dir, mountpoint) == 0) {
|
---|
| 185 | num_matches++;
|
---|
| 186 | }
|
---|
| 187 | }
|
---|
| 188 | if(verboseflg)
|
---|
| 189 | printf("%d matching entries in mount table\n", num_matches);
|
---|
| 190 |
|
---|
| 191 | /* Is there a better way to seek back to the first entry in mtab? */
|
---|
| 192 | endmntent(org_fd);
|
---|
| 193 | org_fd = setmntent(MOUNTED, "r");
|
---|
| 194 |
|
---|
| 195 | if(org_fd == NULL) {
|
---|
| 196 | printf("Can not open %s\n",MOUNTED);
|
---|
| 197 | unlock_mtab();
|
---|
| 198 | return -EIO;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | while((mount_entry = getmntent(org_fd)) != NULL) {
|
---|
| 202 | if(strcmp(mount_entry->mnt_dir, mountpoint) != 0) {
|
---|
| 203 | addmntent(new_fd, mount_entry);
|
---|
| 204 | } else {
|
---|
| 205 | if(num_matches != 1) {
|
---|
| 206 | addmntent(new_fd, mount_entry);
|
---|
| 207 | num_matches--;
|
---|
| 208 | } else if(verboseflg)
|
---|
| 209 | printf("entry not copied (ie entry is removed)\n");
|
---|
| 210 | }
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | if(verboseflg)
|
---|
| 214 | printf("done updating tmp file\n");
|
---|
| 215 | rc = fchmod (fileno (new_fd), S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
|
---|
| 216 | if(rc < 0) {
|
---|
| 217 | printf("error %s changing mode of %s\n", strerror(errno),
|
---|
| 218 | MOUNTED_TEMP);
|
---|
| 219 | }
|
---|
| 220 | endmntent(new_fd);
|
---|
| 221 |
|
---|
| 222 | rc = rename(MOUNTED_TEMP, MOUNTED);
|
---|
| 223 |
|
---|
| 224 | if(rc < 0) {
|
---|
| 225 | printf("failure %s renaming %s to %s\n",strerror(errno),
|
---|
| 226 | MOUNTED_TEMP, MOUNTED);
|
---|
| 227 | unlock_mtab();
|
---|
| 228 | return -EIO;
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | unlock_mtab();
|
---|
| 232 |
|
---|
| 233 | return rc;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | /* Make a canonical pathname from PATH. Returns a freshly malloced string.
|
---|
| 237 | It is up the *caller* to ensure that the PATH is sensible. i.e.
|
---|
| 238 | canonicalize ("/dev/fd0/.") returns "/dev/fd0" even though ``/dev/fd0/.''
|
---|
| 239 | is not a legal pathname for ``/dev/fd0'' Anything we cannot parse
|
---|
| 240 | we return unmodified. */
|
---|
| 241 | static char *
|
---|
| 242 | canonicalize(char *path)
|
---|
| 243 | {
|
---|
| 244 | char *canonical = malloc (PATH_MAX + 1);
|
---|
| 245 |
|
---|
| 246 | if (!canonical) {
|
---|
| 247 | fprintf(stderr, "Error! Not enough memory!\n");
|
---|
| 248 | return NULL;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | if (strlen(path) > PATH_MAX) {
|
---|
| 252 | fprintf(stderr, "Mount point string too long\n");
|
---|
| 253 | return NULL;
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | if (path == NULL)
|
---|
| 257 | return NULL;
|
---|
| 258 |
|
---|
| 259 | if (realpath (path, canonical))
|
---|
| 260 | return canonical;
|
---|
| 261 |
|
---|
| 262 | strncpy (canonical, path, PATH_MAX);
|
---|
| 263 | canonical[PATH_MAX] = '\0';
|
---|
| 264 | return canonical;
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | int main(int argc, char ** argv)
|
---|
| 268 | {
|
---|
| 269 | int c;
|
---|
| 270 | int rc;
|
---|
| 271 | int flags = 0;
|
---|
| 272 | int nomtab = 0;
|
---|
| 273 | int retry_remount = 0;
|
---|
| 274 | struct statfs statbuf;
|
---|
| 275 | char * mountpoint;
|
---|
| 276 |
|
---|
| 277 | if(argc && argv) {
|
---|
| 278 | thisprogram = argv[0];
|
---|
| 279 | } else {
|
---|
| 280 | umount_cifs_usage();
|
---|
| 281 | return -EINVAL;
|
---|
| 282 | }
|
---|
| 283 |
|
---|
| 284 | if(argc < 2) {
|
---|
| 285 | umount_cifs_usage();
|
---|
| 286 | return -EINVAL;
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | if(thisprogram == NULL)
|
---|
| 290 | thisprogram = "umount.cifs";
|
---|
| 291 |
|
---|
| 292 | /* add sharename in opts string as unc= parm */
|
---|
| 293 |
|
---|
| 294 | while ((c = getopt_long (argc, argv, "afhilnrvV",
|
---|
| 295 | longopts, NULL)) != -1) {
|
---|
| 296 | switch (c) {
|
---|
| 297 | /* No code to do the following option yet */
|
---|
| 298 | /* case 'a':
|
---|
| 299 | ++umount_all;
|
---|
| 300 | break; */
|
---|
| 301 | case '?':
|
---|
| 302 | case 'h': /* help */
|
---|
| 303 | umount_cifs_usage();
|
---|
| 304 | exit(1);
|
---|
| 305 | case 'n':
|
---|
| 306 | ++nomtab;
|
---|
| 307 | break;
|
---|
| 308 | case 'f':
|
---|
| 309 | flags |= MNT_FORCE;
|
---|
| 310 | break;
|
---|
| 311 | case 'l':
|
---|
| 312 | flags |= MNT_DETACH; /* lazy unmount */
|
---|
| 313 | break;
|
---|
| 314 | case 'e':
|
---|
| 315 | flags |= MNT_EXPIRE; /* gradually timeout */
|
---|
| 316 | break;
|
---|
| 317 | case 'r':
|
---|
| 318 | ++retry_remount;
|
---|
| 319 | break;
|
---|
| 320 | case 'v':
|
---|
| 321 | ++verboseflg;
|
---|
| 322 | break;
|
---|
| 323 | case 'V':
|
---|
| 324 | printf ("umount.cifs version: %s.%s%s\n",
|
---|
| 325 | UNMOUNT_CIFS_VERSION_MAJOR,
|
---|
| 326 | UNMOUNT_CIFS_VERSION_MINOR,
|
---|
| 327 | UNMOUNT_CIFS_VENDOR_SUFFIX);
|
---|
| 328 | exit (0);
|
---|
| 329 | default:
|
---|
| 330 | printf("unknown unmount option %c\n",c);
|
---|
| 331 | umount_cifs_usage();
|
---|
| 332 | exit(1);
|
---|
| 333 | }
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 | /* move past the umount options */
|
---|
| 337 | argv += optind;
|
---|
| 338 | argc -= optind;
|
---|
| 339 |
|
---|
| 340 | mountpoint = canonicalize(argv[0]);
|
---|
| 341 |
|
---|
| 342 | if((argc < 1) || (argv[0] == NULL)) {
|
---|
| 343 | printf("\nMissing name of unmount directory\n");
|
---|
| 344 | umount_cifs_usage();
|
---|
| 345 | return -EINVAL;
|
---|
| 346 | }
|
---|
| 347 |
|
---|
| 348 | if(verboseflg)
|
---|
| 349 | printf("optind %d unmount dir %s\n",optind, mountpoint);
|
---|
| 350 |
|
---|
| 351 | /* check if running effectively root */
|
---|
| 352 | if(geteuid() != 0) {
|
---|
| 353 | printf("Trying to unmount when %s not installed suid\n",thisprogram);
|
---|
| 354 | if(verboseflg)
|
---|
| 355 | printf("euid = %d\n",geteuid());
|
---|
| 356 | return -EACCES;
|
---|
| 357 | }
|
---|
| 358 |
|
---|
| 359 | /* fixup path if needed */
|
---|
| 360 |
|
---|
| 361 | /* Trim any trailing slashes */
|
---|
| 362 | while ((strlen(mountpoint) > 1) &&
|
---|
| 363 | (mountpoint[strlen(mountpoint)-1] == '/'))
|
---|
| 364 | {
|
---|
| 365 | mountpoint[strlen(mountpoint)-1] = '\0';
|
---|
| 366 | }
|
---|
| 367 |
|
---|
| 368 | /* make sure that this is a cifs filesystem */
|
---|
| 369 | rc = statfs(mountpoint, &statbuf);
|
---|
| 370 |
|
---|
| 371 | if(rc || (statbuf.f_type != CIFS_MAGIC_NUMBER)) {
|
---|
| 372 | printf("This utility only unmounts cifs filesystems.\n");
|
---|
| 373 | return -EINVAL;
|
---|
| 374 | }
|
---|
| 375 |
|
---|
| 376 | /* check if our uid was the one who mounted */
|
---|
| 377 | rc = umount_check_perm(mountpoint);
|
---|
| 378 | if (rc) {
|
---|
| 379 | printf("Not permitted to unmount\n");
|
---|
| 380 | return rc;
|
---|
| 381 | }
|
---|
| 382 |
|
---|
| 383 | if(umount2(mountpoint, flags)) {
|
---|
| 384 | /* remember to kill daemon on error */
|
---|
| 385 | switch (errno) {
|
---|
| 386 | case 0:
|
---|
| 387 | printf("unmount failed but no error number set\n");
|
---|
| 388 | break;
|
---|
| 389 | default:
|
---|
| 390 | printf("unmount error %d = %s\n",errno,strerror(errno));
|
---|
| 391 | }
|
---|
| 392 | printf("Refer to the umount.cifs(8) manual page (man 8 umount.cifs)\n");
|
---|
| 393 | return -1;
|
---|
| 394 | } else {
|
---|
| 395 | if(verboseflg)
|
---|
| 396 | printf("umount2 succeeded\n");
|
---|
| 397 | if(nomtab == 0)
|
---|
| 398 | remove_from_mtab(mountpoint);
|
---|
| 399 | }
|
---|
| 400 |
|
---|
| 401 | return 0;
|
---|
| 402 | }
|
---|
| 403 |
|
---|