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