r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[kai/samba.git] / source3 / client / umount.cifs.c
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 3 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, see <http://www.gnu.org/licenses/>.  */
17
18 #ifndef _GNU_SOURCE
19 #define _GNU_SOURCE
20 #endif
21
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
37 #define UNMOUNT_CIFS_VERSION_MAJOR "0"
38 #define UNMOUNT_CIFS_VERSION_MINOR "5"
39
40 #ifndef UNMOUNT_CIFS_VENDOR_SUFFIX
41  #ifdef _SAMBA_BUILD_
42   #include "include/version.h"
43   #ifdef SAMBA_VERSION_VENDOR_SUFFIX
44    #define UNMOUNT_CIFS_VENDOR_SUFFIX "-"SAMBA_VERSION_OFFICIAL_STRING"-"SAMBA_VERSION_VENDOR_SUFFIX
45   #else
46    #define UNMOUNT_CIFS_VENDOR_SUFFIX "-"SAMBA_VERSION_OFFICIAL_STRING
47   #endif /* SAMBA_VERSION_OFFICIAL_STRING and SAMBA_VERSION_VENDOR_SUFFIX */
48  #else
49   #define UNMOUNT_CIFS_VENDOR_SUFFIX ""
50  #endif /* _SAMBA_BUILD_ */
51 #endif /* UNMOUNT_CIFS_VENDOR_SUFFIX */
52
53 #ifndef MNT_DETACH
54 #define MNT_DETACH 0x02
55 #endif
56
57 #ifndef MNT_EXPIRE
58 #define MNT_EXPIRE 0x04
59 #endif
60
61 #ifndef MOUNTED_LOCK
62 #define MOUNTED_LOCK    "/etc/mtab~"
63 #endif
64 #ifndef MOUNTED_TEMP
65 #define MOUNTED_TEMP    "/etc/mtab.tmp"
66 #endif
67
68 #define CIFS_IOC_CHECKUMOUNT _IO(0xCF, 2)
69 #define CIFS_MAGIC_NUMBER 0xFF534D42   /* the first four bytes of SMB PDU */
70    
71 static struct option longopts[] = {
72         { "all", 0, NULL, 'a' },
73         { "help",0, NULL, 'h' },
74         { "read-only", 0, NULL, 'r' },
75         { "ro", 0, NULL, 'r' },
76         { "verbose", 0, NULL, 'v' },
77         { "version", 0, NULL, 'V' },
78         { "expire", 0, NULL, 'e' },
79         { "force", 0, 0, 'f' },
80         { "lazy", 0, 0, 'l' },
81         { "no-mtab", 0, 0, 'n' },
82         { NULL, 0, NULL, 0 }
83 };
84
85 const char * thisprogram;
86 int verboseflg = 0;
87
88 static void umount_cifs_usage(void)
89 {
90         printf("\nUsage:  %s <remotetarget> <dir>\n", thisprogram);
91         printf("\nUnmount the specified directory\n");
92         printf("\nLess commonly used options:");
93         printf("\n\t-r\tIf mount fails, retry with readonly remount.");
94         printf("\n\t-n\tDo not write to mtab.");
95         printf("\n\t-f\tAttempt a forced unmount, even if the fs is busy.");
96         printf("\n\t-l\tAttempt lazy unmount, Unmount now, cleanup later.");
97         printf("\n\t-v\tEnable verbose mode (may be useful for debugging).");
98         printf("\n\t-h\tDisplay this help.");
99         printf("\n\nOptions are described in more detail in the manual page");
100         printf("\n\tman 8 umount.cifs\n");
101         printf("\nTo display the version number of the cifs umount utility:");
102         printf("\n\t%s -V\n",thisprogram);
103         printf("\nInvoking the umount utility on cifs mounts, can execute");
104         printf(" /sbin/umount.cifs (if present and umount -i is not specified.\n");
105 }
106
107 static int umount_check_perm(char * dir)
108 {
109         int fileid;
110         int rc;
111
112         /* allow root to unmount, no matter what */
113         if(getuid() == 0)
114                 return 0;
115
116         /* presumably can not chdir into the target as we do on mount */
117         fileid = open(dir, O_RDONLY | O_DIRECTORY | O_NOFOLLOW, 0);
118         if(fileid == -1) {
119                 if(verboseflg)
120                         printf("error opening mountpoint %d %s",errno,strerror(errno));
121                 return errno;
122         }
123
124         rc = ioctl(fileid, CIFS_IOC_CHECKUMOUNT, NULL);
125
126         if(verboseflg)
127                 printf("ioctl returned %d with errno %d %s\n",rc,errno,strerror(errno));
128
129         if(rc == ENOTTY) {
130                 printf("user unmounting via %s is an optional feature of",thisprogram);
131                 printf(" the cifs filesystem driver (cifs.ko)");
132                 printf("\n\tand requires cifs.ko version 1.32 or later\n");
133         } else if (rc > 0)
134                 printf("user unmount of %s failed with %d %s\n",dir,errno,strerror(errno));
135         close(fileid);
136
137         return rc;
138 }
139
140 static int lock_mtab(void)
141 {
142         int rc;
143         
144         rc = mknod(MOUNTED_LOCK , 0600, 0);
145         if(rc == -1)
146                 printf("\ngetting lock file %s failed with %s\n",MOUNTED_LOCK,
147                                 strerror(errno));
148                 
149         return rc;      
150         
151 }
152
153 static void unlock_mtab(void)
154 {
155         unlink(MOUNTED_LOCK);   
156 }
157
158 static int remove_from_mtab(char * mountpoint)
159 {
160         int rc;
161         int num_matches;
162         FILE * org_fd;
163         FILE * new_fd;
164         struct mntent * mount_entry;
165
166         /* Do we need to check if it is a symlink to e.g. /proc/mounts
167         in which case we probably do not want to update it? */
168
169         /* Do we first need to check if it is writable? */ 
170
171         if (lock_mtab()) {
172                 printf("Mount table locked\n");
173                 return -EACCES;
174         }
175         
176         if(verboseflg)
177                 printf("attempting to remove from mtab\n");
178
179         org_fd = setmntent(MOUNTED, "r");
180
181         if(org_fd == NULL) {
182                 printf("Can not open %s\n",MOUNTED);
183                 unlock_mtab();
184                 return -EIO;
185         }
186
187         new_fd = setmntent(MOUNTED_TEMP,"w");
188         if(new_fd == NULL) {
189                 printf("Can not open temp file %s", MOUNTED_TEMP);
190                 endmntent(org_fd);
191                 unlock_mtab();
192                 return -EIO;
193         }
194
195         /* BB fix so we only remove the last entry that matches BB */
196         num_matches = 0;
197         while((mount_entry = getmntent(org_fd)) != NULL) {
198                 if(strcmp(mount_entry->mnt_dir, mountpoint) == 0) {
199                         num_matches++;
200                 }
201         }       
202         if(verboseflg)
203                 printf("%d matching entries in mount table\n", num_matches);
204                 
205         /* Is there a better way to seek back to the first entry in mtab? */
206         endmntent(org_fd);
207         org_fd = setmntent(MOUNTED, "r");
208
209         if(org_fd == NULL) {
210                 printf("Can not open %s\n",MOUNTED);
211                 unlock_mtab();
212                 return -EIO;
213         }
214         
215         while((mount_entry = getmntent(org_fd)) != NULL) {
216                 if(strcmp(mount_entry->mnt_dir, mountpoint) != 0) {
217                         addmntent(new_fd, mount_entry);
218                 } else {
219                         if(num_matches != 1) {
220                                 addmntent(new_fd, mount_entry);
221                                 num_matches--;
222                         } else if(verboseflg)
223                                 printf("entry not copied (ie entry is removed)\n");
224                 }
225         }
226
227         if(verboseflg)
228                 printf("done updating tmp file\n");
229         rc = fchmod (fileno (new_fd), S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
230         if(rc < 0) {
231                 printf("error %s changing mode of %s\n", strerror(errno),
232                         MOUNTED_TEMP);
233         }
234         endmntent(new_fd);
235
236         rc = rename(MOUNTED_TEMP, MOUNTED);
237
238         if(rc < 0) {
239                 printf("failure %s renaming %s to %s\n",strerror(errno),
240                         MOUNTED_TEMP, MOUNTED);
241                 unlock_mtab();
242                 return -EIO;
243         }
244
245         unlock_mtab();
246         
247         return rc;
248 }
249
250 int main(int argc, char ** argv)
251 {
252         int c;
253         int rc;
254         int flags = 0;
255         int nomtab = 0;
256         int retry_remount = 0;
257         struct statfs statbuf;
258         char * mountpoint;
259
260         if(argc && argv) {
261                 thisprogram = argv[0];
262         } else {
263                 umount_cifs_usage();
264                 return -EINVAL;
265         }
266
267         if(argc < 2) {
268                 umount_cifs_usage();
269                 return -EINVAL;
270         }
271
272         if(thisprogram == NULL)
273                 thisprogram = "umount.cifs";
274
275         /* add sharename in opts string as unc= parm */
276
277         while ((c = getopt_long (argc, argv, "afhilnrvV",
278                          longopts, NULL)) != -1) {
279                 switch (c) {
280 /* No code to do the following  option yet */
281 /*              case 'a':              
282                         ++umount_all;
283                         break; */
284                 case '?':
285                 case 'h':   /* help */
286                         umount_cifs_usage();
287                         exit(1);
288                 case 'n':
289                         ++nomtab;
290                         break;
291                 case 'f':
292                         flags |= MNT_FORCE;
293                         break;
294                 case 'l':
295                         flags |= MNT_DETACH; /* lazy unmount */
296                         break;
297                 case 'e':
298                         flags |= MNT_EXPIRE; /* gradually timeout */
299                         break;
300                 case 'r':
301                         ++retry_remount;
302                         break;
303                 case 'v':
304                         ++verboseflg;
305                         break;
306                 case 'V':          
307                         printf ("umount.cifs version: %s.%s%s\n",
308                                 UNMOUNT_CIFS_VERSION_MAJOR,
309                                 UNMOUNT_CIFS_VERSION_MINOR,
310                                 UNMOUNT_CIFS_VENDOR_SUFFIX);
311                         exit (0);
312                 default:
313                         printf("unknown unmount option %c\n",c);
314                         umount_cifs_usage();
315                         exit(1);
316                 }
317         }
318
319         /* move past the umount options */
320         argv += optind;
321         argc -= optind;
322
323         mountpoint = argv[0];
324
325         if((argc < 1) || (argv[0] == NULL)) {
326                 printf("\nMissing name of unmount directory\n");
327                 umount_cifs_usage();
328                 return -EINVAL;
329         }
330
331         if(verboseflg)
332                 printf("optind %d unmount dir %s\n",optind, mountpoint);
333
334         /* check if running effectively root */
335         if(geteuid() != 0) {
336                 printf("Trying to unmount when %s not installed suid\n",thisprogram);
337                 if(verboseflg)
338                         printf("euid = %d\n",geteuid());
339                 return -EACCES;
340         }
341
342         /* fixup path if needed */
343
344         /* make sure that this is a cifs filesystem */
345         rc = statfs(mountpoint, &statbuf);
346         
347         if(rc || (statbuf.f_type != CIFS_MAGIC_NUMBER)) {
348                 printf("This utility only unmounts cifs filesystems.\n");
349                 return -EINVAL;
350         }
351
352         /* check if our uid was the one who mounted */
353         rc = umount_check_perm(mountpoint);
354         if (rc) {
355                 printf("Not permitted to unmount\n");
356                 return rc;
357         }
358
359         if(umount2(mountpoint, flags)) {
360         /* remember to kill daemon on error */
361                 switch (errno) {
362                 case 0:
363                         printf("unmount failed but no error number set\n");
364                         break;
365                 default:
366                         printf("unmount error %d = %s\n",errno,strerror(errno));
367                 }
368                 printf("Refer to the umount.cifs(8) manual page (man 8 umount.cifs)\n");
369                 return -1;
370         } else {
371                 if(verboseflg)
372                         printf("umount2 succeeded\n");
373                 if(nomtab == 0)
374                         remove_from_mtab(mountpoint);
375         }
376
377         return 0;
378 }
379