docs: Improve description of the share commands in man smb.conf.
[Samba/gebeck_regimport.git] / source3 / client / umount.cifs.c
blobaff7cea39746b34f04196a954c0b4fd29a109dad
1 /*
2 Unmount utility program for Linux CIFS VFS (virtual filesystem) client
3 Copyright (C) 2005 Steve French (sfrench@us.ibm.com)
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 3 of the License, or
8 (at your option) any later version.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 #ifndef _GNU_SOURCE
19 #define _GNU_SOURCE
20 #endif
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <ctype.h>
26 #include <sys/types.h>
27 #include <sys/mount.h>
28 #include <sys/ioctl.h>
29 #include <sys/stat.h>
30 #include <sys/vfs.h>
31 #include <fcntl.h>
32 #include <getopt.h>
33 #include <errno.h>
34 #include <string.h>
35 #include <mntent.h>
36 #include "mount.h"
38 #define UNMOUNT_CIFS_VERSION_MAJOR "0"
39 #define UNMOUNT_CIFS_VERSION_MINOR "6"
41 #ifndef UNMOUNT_CIFS_VENDOR_SUFFIX
42 #ifdef _SAMBA_BUILD_
43 #include "include/version.h"
44 #ifdef SAMBA_VERSION_VENDOR_SUFFIX
45 #define UNMOUNT_CIFS_VENDOR_SUFFIX "-"SAMBA_VERSION_OFFICIAL_STRING"-"SAMBA_VERSION_VENDOR_SUFFIX
46 #else
47 #define UNMOUNT_CIFS_VENDOR_SUFFIX "-"SAMBA_VERSION_OFFICIAL_STRING
48 #endif /* SAMBA_VERSION_OFFICIAL_STRING and SAMBA_VERSION_VENDOR_SUFFIX */
49 #else
50 #define UNMOUNT_CIFS_VENDOR_SUFFIX ""
51 #endif /* _SAMBA_BUILD_ */
52 #endif /* UNMOUNT_CIFS_VENDOR_SUFFIX */
54 #ifndef MNT_DETACH
55 #define MNT_DETACH 0x02
56 #endif
58 #ifndef MNT_EXPIRE
59 #define MNT_EXPIRE 0x04
60 #endif
62 #ifndef MOUNTED_LOCK
63 #define MOUNTED_LOCK "/etc/mtab~"
64 #endif
65 #ifndef MOUNTED_TEMP
66 #define MOUNTED_TEMP "/etc/mtab.tmp"
67 #endif
69 #define CIFS_IOC_CHECKUMOUNT _IO(0xCF, 2)
70 #define CIFS_MAGIC_NUMBER 0xFF534D42 /* the first four bytes of SMB PDU */
72 static struct option longopts[] = {
73 { "all", 0, NULL, 'a' },
74 { "help",0, NULL, 'h' },
75 { "read-only", 0, NULL, 'r' },
76 { "ro", 0, NULL, 'r' },
77 { "verbose", 0, NULL, 'v' },
78 { "version", 0, NULL, 'V' },
79 { "expire", 0, NULL, 'e' },
80 { "force", 0, 0, 'f' },
81 { "lazy", 0, 0, 'l' },
82 { "no-mtab", 0, 0, 'n' },
83 { NULL, 0, NULL, 0 }
86 const char * thisprogram;
87 int verboseflg = 0;
89 static void umount_cifs_usage(void)
91 printf("\nUsage: %s <remotetarget> <dir>\n", thisprogram);
92 printf("\nUnmount the specified directory\n");
93 printf("\nLess commonly used options:");
94 printf("\n\t-r\tIf mount fails, retry with readonly remount.");
95 printf("\n\t-n\tDo not write to mtab.");
96 printf("\n\t-f\tAttempt a forced unmount, even if the fs is busy.");
97 printf("\n\t-l\tAttempt lazy unmount, Unmount now, cleanup later.");
98 printf("\n\t-v\tEnable verbose mode (may be useful for debugging).");
99 printf("\n\t-h\tDisplay this help.");
100 printf("\n\nOptions are described in more detail in the manual page");
101 printf("\n\tman 8 umount.cifs\n");
102 printf("\nTo display the version number of the cifs umount utility:");
103 printf("\n\t%s -V\n",thisprogram);
104 printf("\nInvoking the umount utility on cifs mounts, can execute");
105 printf(" /sbin/umount.cifs (if present and umount -i is not specified.\n");
108 static int umount_check_perm(char * dir)
110 int fileid;
111 int rc;
113 /* allow root to unmount, no matter what */
114 if(getuid() == 0)
115 return 0;
117 /* presumably can not chdir into the target as we do on mount */
118 fileid = open(dir, O_RDONLY | O_DIRECTORY | O_NOFOLLOW, 0);
119 if(fileid == -1) {
120 if(verboseflg)
121 printf("error opening mountpoint %d %s",errno,strerror(errno));
122 return errno;
125 rc = ioctl(fileid, CIFS_IOC_CHECKUMOUNT, NULL);
127 if(verboseflg)
128 printf("ioctl returned %d with errno %d %s\n",rc,errno,strerror(errno));
130 if(rc == ENOTTY) {
131 printf("user unmounting via %s is an optional feature of",thisprogram);
132 printf(" the cifs filesystem driver (cifs.ko)");
133 printf("\n\tand requires cifs.ko version 1.32 or later\n");
134 } else if (rc != 0)
135 printf("user unmount of %s failed with %d %s\n",dir,errno,strerror(errno));
136 close(fileid);
138 return rc;
141 static int remove_from_mtab(char * mountpoint)
143 int rc;
144 int num_matches;
145 FILE * org_fd;
146 FILE * new_fd;
147 struct mntent * mount_entry;
149 /* Do we need to check if it is a symlink to e.g. /proc/mounts
150 in which case we probably do not want to update it? */
152 /* Do we first need to check if it is writable? */
154 atexit(unlock_mtab);
155 if (lock_mtab()) {
156 printf("Mount table locked\n");
157 return -EACCES;
160 if(verboseflg)
161 printf("attempting to remove from mtab\n");
163 org_fd = setmntent(MOUNTED, "r");
165 if(org_fd == NULL) {
166 printf("Can not open %s\n",MOUNTED);
167 unlock_mtab();
168 return -EIO;
171 new_fd = setmntent(MOUNTED_TEMP,"w");
172 if(new_fd == NULL) {
173 printf("Can not open temp file %s", MOUNTED_TEMP);
174 endmntent(org_fd);
175 unlock_mtab();
176 return -EIO;
179 /* BB fix so we only remove the last entry that matches BB */
180 num_matches = 0;
181 while((mount_entry = getmntent(org_fd)) != NULL) {
182 if(strcmp(mount_entry->mnt_dir, mountpoint) == 0) {
183 num_matches++;
186 if(verboseflg)
187 printf("%d matching entries in mount table\n", num_matches);
189 /* Is there a better way to seek back to the first entry in mtab? */
190 endmntent(org_fd);
191 org_fd = setmntent(MOUNTED, "r");
193 if(org_fd == NULL) {
194 printf("Can not open %s\n",MOUNTED);
195 unlock_mtab();
196 return -EIO;
199 while((mount_entry = getmntent(org_fd)) != NULL) {
200 if(strcmp(mount_entry->mnt_dir, mountpoint) != 0) {
201 addmntent(new_fd, mount_entry);
202 } else {
203 if(num_matches != 1) {
204 addmntent(new_fd, mount_entry);
205 num_matches--;
206 } else if(verboseflg)
207 printf("entry not copied (ie entry is removed)\n");
211 if(verboseflg)
212 printf("done updating tmp file\n");
213 rc = fchmod (fileno (new_fd), S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
214 if(rc < 0) {
215 printf("error %s changing mode of %s\n", strerror(errno),
216 MOUNTED_TEMP);
218 endmntent(new_fd);
220 rc = rename(MOUNTED_TEMP, MOUNTED);
222 if(rc < 0) {
223 printf("failure %s renaming %s to %s\n",strerror(errno),
224 MOUNTED_TEMP, MOUNTED);
225 unlock_mtab();
226 return -EIO;
229 unlock_mtab();
231 return rc;
234 int main(int argc, char ** argv)
236 int c;
237 int rc;
238 int flags = 0;
239 int nomtab = 0;
240 int retry_remount = 0;
241 struct statfs statbuf;
242 char * mountpoint;
244 if(argc && argv) {
245 thisprogram = argv[0];
246 } else {
247 umount_cifs_usage();
248 return -EINVAL;
251 if(argc < 2) {
252 umount_cifs_usage();
253 return -EINVAL;
256 if(thisprogram == NULL)
257 thisprogram = "umount.cifs";
259 /* add sharename in opts string as unc= parm */
261 while ((c = getopt_long (argc, argv, "afhilnrvV",
262 longopts, NULL)) != -1) {
263 switch (c) {
264 /* No code to do the following option yet */
265 /* case 'a':
266 ++umount_all;
267 break; */
268 case '?':
269 case 'h': /* help */
270 umount_cifs_usage();
271 exit(1);
272 case 'n':
273 ++nomtab;
274 break;
275 case 'f':
276 flags |= MNT_FORCE;
277 break;
278 case 'l':
279 flags |= MNT_DETACH; /* lazy unmount */
280 break;
281 case 'e':
282 flags |= MNT_EXPIRE; /* gradually timeout */
283 break;
284 case 'r':
285 ++retry_remount;
286 break;
287 case 'v':
288 ++verboseflg;
289 break;
290 case 'V':
291 printf ("umount.cifs version: %s.%s%s\n",
292 UNMOUNT_CIFS_VERSION_MAJOR,
293 UNMOUNT_CIFS_VERSION_MINOR,
294 UNMOUNT_CIFS_VENDOR_SUFFIX);
295 exit (0);
296 default:
297 printf("unknown unmount option %c\n",c);
298 umount_cifs_usage();
299 exit(1);
303 /* move past the umount options */
304 argv += optind;
305 argc -= optind;
307 mountpoint = argv[0];
309 if((argc < 1) || (argv[0] == NULL)) {
310 printf("\nMissing name of unmount directory\n");
311 umount_cifs_usage();
312 return -EINVAL;
315 if(verboseflg)
316 printf("optind %d unmount dir %s\n",optind, mountpoint);
318 /* check if running effectively root */
319 if(geteuid() != 0) {
320 printf("Trying to unmount when %s not installed suid\n",thisprogram);
321 if(verboseflg)
322 printf("euid = %d\n",geteuid());
323 return -EACCES;
326 /* fixup path if needed */
328 /* Trim any trailing slashes */
329 while ((strlen(mountpoint) > 1) &&
330 (mountpoint[strlen(mountpoint)-1] == '/'))
332 mountpoint[strlen(mountpoint)-1] = '\0';
335 /* make sure that this is a cifs filesystem */
336 rc = statfs(mountpoint, &statbuf);
338 if(rc || (statbuf.f_type != CIFS_MAGIC_NUMBER)) {
339 printf("This utility only unmounts cifs filesystems.\n");
340 return -EINVAL;
343 /* check if our uid was the one who mounted */
344 rc = umount_check_perm(mountpoint);
345 if (rc) {
346 printf("Not permitted to unmount\n");
347 return rc;
350 if(umount2(mountpoint, flags)) {
351 /* remember to kill daemon on error */
352 switch (errno) {
353 case 0:
354 printf("unmount failed but no error number set\n");
355 break;
356 default:
357 printf("unmount error %d = %s\n",errno,strerror(errno));
359 printf("Refer to the umount.cifs(8) manual page (man 8 umount.cifs)\n");
360 return -1;
361 } else {
362 if(verboseflg)
363 printf("umount2 succeeded\n");
364 if(nomtab == 0)
365 remove_from_mtab(mountpoint);
368 return 0;