]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/nfsumount.c
mount.nfs: umount needs to pick up transport option from /etc/mtab
[nfs-utils.git] / utils / mount / nfsumount.c
1 /*
2  * nfsumount.c -- Linux NFS umount
3  * Copyright (C) 2006 Amit Gud <agud@redhat.com>
4  *
5  * - Basic code and wrapper around NFS umount code originally
6  *   in util-linux/mount/nfsmount.c
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2, or (at your option)
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  */
19
20 #include <unistd.h>
21 #include <stdio.h>
22 #include <errno.h>
23 #include <getopt.h>
24 #include <mntent.h>
25 #include <sys/mount.h>
26 #include <ctype.h>
27 #include <pwd.h>
28
29 #include "xcommon.h"
30 #include "fstab.h"
31 #include "nls.h"
32
33 #include "mount_constants.h"
34 #include "mount.h"
35 #include "error.h"
36 #include "network.h"
37
38 #if !defined(MNT_FORCE)
39 /* dare not try to include <linux/mount.h> -- lots of errors */
40 #define MNT_FORCE 1
41 #endif
42
43 #if !defined(MNT_DETACH)
44 #define MNT_DETACH 2
45 #endif
46
47 extern char *progname;
48 extern int nomtab;
49 extern int verbose;
50 int force;
51 int lazy;
52 int remount;
53
54 static int del_mtab(const char *spec, const char *node)
55 {
56         int umnt_err, res;
57
58         umnt_err = 0;
59         if (lazy) {
60                 res = umount2 (node, MNT_DETACH);
61                 if (res < 0)
62                         umnt_err = errno;
63                 goto writemtab;
64         }
65
66         if (force) {
67                 res = umount2 (node, MNT_FORCE);
68                 if (res == -1) {
69                         int errsv = errno;
70                         perror(_("umount2"));
71                         errno = errsv;
72                         if (errno == ENOSYS) {
73                                 if (verbose)
74                                         printf(_("no umount2, trying umount...\n"));
75                                 res = umount (node);
76                         }
77                 }
78         } else
79                 res = umount (node);
80
81         if (res < 0 && remount && errno == EBUSY && spec) {
82                 /* Umount failed - let us try a remount */
83                 res = mount(spec, node, NULL,
84                             MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
85                 if (res == 0) {
86                         struct mntent remnt;
87                         nfs_error(_("%s: %s busy - remounted read-only"),
88                                         progname, spec);
89                         remnt.mnt_type = remnt.mnt_fsname = NULL;
90                         remnt.mnt_dir = xstrdup(node);
91                         remnt.mnt_opts = xstrdup("ro");
92                         if (!nomtab)
93                                 update_mtab(node, &remnt);
94                         return 0;
95                 } else if (errno != EBUSY) {    /* hmm ... */
96                         perror(_("remount"));
97                         nfs_error(_("%s: could not remount %s read-only"),
98                                         progname, spec);
99                 }
100         }
101
102         if (res >= 0) {
103                 /* Umount succeeded */
104                 if (verbose)
105                         printf(_("%s umounted\n"), spec ? spec : node);
106         }
107
108  writemtab:
109         if (!nomtab &&
110             (umnt_err == 0 || umnt_err == EINVAL || umnt_err == ENOENT)) {
111                update_mtab(node, NULL);
112         }
113
114         if (res >= 0)
115                 return 0;
116
117         if (umnt_err)
118                 umount_error(umnt_err, node);
119         return EX_FILEIO;
120 }
121
122 /*
123  * Pick up certain mount options used during the original mount
124  * from /etc/mtab.  The basics include the server's IP address and
125  * the server pathname of the share to unregister.
126  *
127  * These options might also describe the mount port, mount protocol
128  * version, and transport protocol used to punch through a firewall.
129  * We will need this information to get through the firewall again
130  * to do the umount.
131  */
132 static int do_nfs_umount(const char *spec, char *opts)
133 {
134         char *hostname;
135         char *dirname;
136         clnt_addr_t mnt_server = { &hostname, };
137         struct mntent mnt = { .mnt_opts = opts };
138         struct pmap *pmap = &mnt_server.pmap;
139         char *p;
140
141         if (spec == NULL) {
142                 nfs_error(_("%s: No NFS export name was provided"),
143                                 progname);
144                 return EX_USAGE;
145         }
146         
147         p = strchr(spec, ':');
148         if (p == NULL) {
149                 nfs_error(_("%s: '%s' is not a legal NFS export name"),
150                                 progname, spec);
151                 return EX_USAGE;
152         }
153         hostname = xstrndup(spec, p - spec);
154         dirname = xstrdup(p + 1);
155 #ifdef NFS_MOUNT_DEBUG
156         printf(_("host: %s, directory: %s\n"), hostname, dirname);
157 #endif
158
159         if (opts && (p = strstr(opts, "addr="))) {
160                 char *q;
161
162                 free(hostname);
163                 p += 5;
164                 q = p;
165                 while (*q && *q != ',') q++;
166                 hostname = xstrndup(p,q-p);
167         }
168
169         if (opts && (p = strstr(opts, "mounthost="))) {
170                 char *q;
171
172                 free(hostname);
173                 p += 10;
174                 q = p;
175                 while (*q && *q != ',') q++;
176                 hostname = xstrndup(p,q-p);
177         }
178
179         pmap->pm_prog = MOUNTPROG;
180         pmap->pm_vers = MOUNTVERS_NFSV3;
181         if (opts && (p = strstr(opts, "mountprog=")) && isdigit(*(p+10)))
182                 pmap->pm_prog = atoi(p+10);
183         if (opts && (p = strstr(opts, "mountport=")) && isdigit(*(p+10)))
184                 pmap->pm_port = atoi(p+10);
185         if (opts && hasmntopt(&mnt, "v2"))
186                 pmap->pm_vers = nfsvers_to_mnt(2);
187         if (opts && hasmntopt(&mnt, "v3"))
188                 pmap->pm_vers = nfsvers_to_mnt(3);
189         if (opts && hasmntopt(&mnt, "v4"))
190                 pmap->pm_vers = nfsvers_to_mnt(4);
191         if (opts && (p = strstr(opts, "vers=")) && isdigit(*(p+5)))
192                 pmap->pm_vers = nfsvers_to_mnt(atoi(p+5));
193         if (opts && (p = strstr(opts, "mountvers=")) && isdigit(*(p+10)))
194                 pmap->pm_vers = atoi(p+10);
195         if (opts && (hasmntopt(&mnt, "udp") || hasmntopt(&mnt, "proto=udp")))
196                 pmap->pm_prot = IPPROTO_UDP;
197         if (opts && (hasmntopt(&mnt, "tcp") || hasmntopt(&mnt, "proto=tcp")))
198                 pmap->pm_prot = IPPROTO_TCP;
199
200         if (!nfs_gethostbyname(hostname, &mnt_server.saddr)) {
201                 nfs_error(_("%s: '%s' does not contain a recognized hostname"),
202                                 progname, spec);
203                 return EX_USAGE;
204         }
205
206         if (!nfs_call_umount(&mnt_server, &dirname)) {
207                 nfs_error(_("%s: Server failed to unmount '%s'"),
208                                 progname, spec);
209                 return EX_USAGE;
210         }
211
212         return 0;
213 }
214
215 static struct option umount_longopts[] =
216 {
217   { "force", 0, 0, 'f' },
218   { "help", 0, 0, 'h' },
219   { "no-mtab", 0, 0, 'n' },
220   { "verbose", 0, 0, 'v' },
221   { "read-only", 0, 0, 'r' },
222   { NULL, 0, 0, 0 }
223 };
224
225 static void umount_usage(void)
226 {
227         printf(_("usage: %s dir [-fvnrlh]\n"), progname);
228         printf(_("options:\n\t-f\t\tforce unmount\n"));
229         printf(_("\t-v\tverbose\n"));
230         printf(_("\t-n\tDo not update /etc/mtab\n"));
231         printf(_("\t-r\tremount\n"));
232         printf(_("\t-l\tlazy unmount\n"));
233         printf(_("\t-h\tprint this help\n\n"));
234 }
235
236 int nfsumount(int argc, char *argv[])
237 {
238         int c, ret;
239         char *spec;
240         struct mntentchn *mc;
241
242         if (argc < 2) {
243                 umount_usage();
244                 return EX_USAGE;
245         }
246
247         spec = argv[1];
248
249         argv += 1;
250         argc -= 1;
251
252         argv[0] = argv[-1]; /* So that getopt error messages are correct */
253         while ((c = getopt_long (argc, argv, "fvnrlh",
254                                 umount_longopts, NULL)) != -1) {
255
256                 switch (c) {
257                 case 'f':
258                         ++force;
259                         break;
260                 case 'v':
261                         ++verbose;
262                         break;
263                 case 'n':
264                         ++nomtab;
265                         break;
266                 case 'r':
267                         ++remount;
268                         break;
269                 case 'l':
270                         ++lazy;
271                         break;
272                 case 'h':
273                 default:
274                         umount_usage();
275                         return EX_USAGE;
276                 }
277         }
278         if (optind != argc) {
279                 umount_usage();
280                 return EX_USAGE;
281         }
282         
283         if (spec == NULL || (*spec != '/' && strchr(spec,':') == NULL)) {
284                 nfs_error(_("%s: %s: not found\n"), progname, spec);
285                 return EX_USAGE;
286         }
287
288         if (*spec == '/')
289                 mc = getmntdirbackward(spec, NULL);
290         else
291                 mc = getmntdevbackward(spec, NULL);
292         if (!mc && verbose)
293                 printf(_("Could not find %s in mtab\n"), spec);
294
295         if (mc && strcmp(mc->m.mnt_type, "nfs") != 0 &&
296             strcmp(mc->m.mnt_type, "nfs4") != 0) {
297                 nfs_error(_("%s: %s on %s is not an NFS filesystem"),
298                                 progname, mc->m.mnt_fsname, mc->m.mnt_dir);
299                 return EX_USAGE;
300         }
301
302         if (getuid() != 0) {
303                 /* only permitted if "user=" or "users" is in mount options */
304                 if (!mc) {
305                         /* umount might call us twice.  The second time there will
306                          * be no entry in mtab and we should just exit quietly
307                          */
308                         return 0;
309
310                 only_root:
311                         nfs_error(_("%s: You are not permitted to unmount %s"),
312                                         progname, spec);
313                         return EX_USAGE;
314                 }
315                 if (hasmntopt(&mc->m, "users") == NULL) {
316                         char *opt = hasmntopt(&mc->m, "user");
317                         struct passwd *pw;
318                         char *comma;
319                         int len;
320                         if (!opt)
321                                 goto only_root;
322                         if (opt[4] != '=')
323                                 goto only_root;
324                         comma = strchr(opt, ',');
325                         if (comma)
326                                 len = comma - (opt + 5);
327                         else
328                                 len = strlen(opt+5);
329                         pw = getpwuid(getuid());
330                         if (pw == NULL || strlen(pw->pw_name) != len
331                             || strncmp(pw->pw_name, opt+5, len) != 0)
332                                 goto only_root;
333                 }
334         }
335
336         ret = 0;
337         if (mc) {
338                 if (!lazy)
339                         ret = do_nfs_umount(mc->m.mnt_fsname, mc->m.mnt_opts);
340                 if (!ret || force)
341                         ret = del_mtab(mc->m.mnt_fsname, mc->m.mnt_dir);
342         } else if (*spec != '/') {
343                 if (!lazy)
344                         ret = do_nfs_umount(spec, "tcp,v3");
345         } else
346                 ret = del_mtab(NULL, spec);
347
348         return ret;
349 }