]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/nfsumount.c
78a217876b55c8c2991e7c94c1efcd50ea1d868e
[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 "nfs_mount.h"
35 #include "mount.h"
36 #include "error.h"
37 #include "network.h"
38 #include "parse_opt.h"
39 #include "parse_dev.h"
40
41 #if !defined(MNT_FORCE)
42 /* dare not try to include <linux/mount.h> -- lots of errors */
43 #define MNT_FORCE 1
44 #endif
45
46 #if !defined(MNT_DETACH)
47 #define MNT_DETACH 2
48 #endif
49
50 extern char *progname;
51 extern int nomtab;
52 extern int verbose;
53 int force;
54 int lazy;
55 int remount;
56
57
58 static int try_remount(const char *spec, const char *node)
59 {
60         int res;
61
62         res = mount(spec, node, NULL,
63                     MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
64         if (res == 0) {
65                 struct mntent remnt;
66                 nfs_error(_("%s: %s busy - remounted read-only"),
67                                 progname, spec);
68                 remnt.mnt_type = remnt.mnt_fsname = NULL;
69                 remnt.mnt_dir = xstrdup(node);
70                 remnt.mnt_opts = xstrdup("ro");
71                 if (!nomtab)
72                         update_mtab(node, &remnt);
73         } else if (errno != EBUSY) {    /* hmm ... */
74                 perror(_("remount"));
75                 nfs_error(_("%s: could not remount %s read-only"),
76                                 progname, spec);
77         }
78         return res;
79 }
80
81 static int del_mtab(const char *spec, const char *node)
82 {
83         int umnt_err, res;
84
85         umnt_err = 0;
86         if (lazy) {
87                 res = umount2 (node, MNT_DETACH);
88                 if (res < 0)
89                         umnt_err = errno;
90                 goto writemtab;
91         }
92
93         if (force) {
94                 res = umount2 (node, MNT_FORCE);
95                 if (res == -1) {
96                         int errsv = errno;
97                         perror(_("umount2"));
98                         errno = errsv;
99                         if (errno == ENOSYS) {
100                                 if (verbose)
101                                         printf(_("no umount2, trying umount...\n"));
102                                 res = umount (node);
103                         }
104                 }
105         } else
106                 res = umount (node);
107
108         if (res < 0) {
109                 if (remount && errno == EBUSY && spec) {
110                         res = try_remount(spec, node);
111                         if (res)
112                                 goto writemtab;
113                         return 0;
114                 } else
115                         umnt_err = errno;
116         }
117
118         if (res >= 0) {
119                 /* Umount succeeded */
120                 if (verbose)
121                         printf(_("%s umounted\n"), spec ? spec : node);
122         }
123
124  writemtab:
125         if (!nomtab &&
126             (umnt_err == 0 || umnt_err == EINVAL || umnt_err == ENOENT)) {
127                 update_mtab(node, NULL);
128         }
129
130         if (res >= 0)
131                 return 0;
132
133         if (umnt_err)
134                 umount_error(umnt_err, node);
135         return EX_FILEIO;
136 }
137
138 /*
139  * Discover mount server's hostname/address by examining mount options
140  *
141  * Returns a pointer to a string that the caller must free, on
142  * success; otherwise NULL is returned.
143  */
144 static char *nfs_umount_hostname(struct mount_options *options,
145                                  char *hostname)
146 {
147         char *option;
148
149         option = po_get(options, "mountaddr");
150         if (option)
151                 goto out;
152         option = po_get(options, "mounthost");
153         if (option)
154                 goto out;
155         option = po_get(options, "addr");
156         if (option)
157                 goto out;
158
159         return hostname;
160
161 out:
162         free(hostname);
163         return strdup(option);
164 }
165
166 /*
167  * Returns EX_SUCCESS if mount options and device name have been
168  * parsed successfully; otherwise EX_FAIL.
169  */
170 static int nfs_umount_do_umnt(struct mount_options *options,
171                               char **hostname, char **dirname)
172 {
173         union {
174                 struct sockaddr         sa;
175                 struct sockaddr_in      s4;
176                 struct sockaddr_in6     s6;
177         } address;
178         struct sockaddr *sap = &address.sa;
179         socklen_t salen = sizeof(address);
180         struct pmap nfs_pmap, mnt_pmap;
181         sa_family_t family;
182
183         if (!nfs_options2pmap(options, &nfs_pmap, &mnt_pmap))
184                 return EX_FAIL;
185
186         /* Skip UMNT call for vers=4 mounts */
187         if (nfs_pmap.pm_vers == 4)
188                 return EX_SUCCESS;
189
190         *hostname = nfs_umount_hostname(options, *hostname);
191         if (!*hostname) {
192                 nfs_error(_("%s: out of memory"), progname);
193                 return EX_FAIL;
194         }
195
196         if (!nfs_mount_proto_family(options, &family))
197                 return 0;
198         if (!nfs_lookup(*hostname, family, sap, &salen))
199                 /* nfs_lookup reports any errors */
200                 return EX_FAIL;
201
202         if (nfs_advise_umount(sap, salen, &mnt_pmap, dirname) == 0)
203                 /* nfs_advise_umount reports any errors */
204                 return EX_FAIL;
205
206         return EX_SUCCESS;
207 }
208
209 /*
210  * Pick up certain mount options used during the original mount
211  * from /etc/mtab.  The basics include the server's IP address and
212  * the server pathname of the share to unregister.
213  *
214  * These options might also describe the mount port, mount protocol
215  * version, and transport protocol used to punch through a firewall.
216  * We will need this information to get through the firewall again
217  * to do the umount.
218  *
219  * Note that option parsing failures won't necessarily cause the
220  * umount request to fail.  Those values will be left zero in the
221  * pmap tuple.  If the GETPORT call later fails to disambiguate them,
222  * then we fail.
223  */
224 static int nfs_umount23(const char *devname, char *string)
225 {
226         char *hostname, *dirname;
227         struct mount_options *options;
228         int result = EX_FAIL;
229
230         if (!nfs_parse_devname(devname, &hostname, &dirname))
231                 return EX_USAGE;
232
233         options = po_split(string);
234         if (options) {
235                 result = nfs_umount_do_umnt(options, &hostname, &dirname);
236                 po_destroy(options);
237         } else
238                 nfs_error(_("%s: option parsing error"), progname);
239
240         free(hostname);
241         free(dirname);
242         return result;
243 }
244
245 static struct option umount_longopts[] =
246 {
247   { "force", 0, 0, 'f' },
248   { "help", 0, 0, 'h' },
249   { "no-mtab", 0, 0, 'n' },
250   { "verbose", 0, 0, 'v' },
251   { "read-only", 0, 0, 'r' },
252   { NULL, 0, 0, 0 }
253 };
254
255 static void umount_usage(void)
256 {
257         printf(_("usage: %s dir [-fvnrlh]\n"), progname);
258         printf(_("options:\n\t-f\t\tforce unmount\n"));
259         printf(_("\t-v\tverbose\n"));
260         printf(_("\t-n\tDo not update /etc/mtab\n"));
261         printf(_("\t-r\tremount\n"));
262         printf(_("\t-l\tlazy unmount\n"));
263         printf(_("\t-h\tprint this help\n\n"));
264 }
265
266 int nfsumount(int argc, char *argv[])
267 {
268         int c, ret;
269         char *spec;
270         struct mntentchn *mc;
271
272         if (argc < 2) {
273                 umount_usage();
274                 return EX_USAGE;
275         }
276
277         spec = argv[1];
278
279         argv += 1;
280         argc -= 1;
281
282         argv[0] = argv[-1]; /* So that getopt error messages are correct */
283         while ((c = getopt_long (argc, argv, "fvnrlh",
284                                 umount_longopts, NULL)) != -1) {
285
286                 switch (c) {
287                 case 'f':
288                         ++force;
289                         break;
290                 case 'v':
291                         ++verbose;
292                         break;
293                 case 'n':
294                         ++nomtab;
295                         break;
296                 case 'r':
297                         ++remount;
298                         break;
299                 case 'l':
300                         ++lazy;
301                         break;
302                 case 'h':
303                 default:
304                         umount_usage();
305                         return EX_USAGE;
306                 }
307         }
308         if (optind != argc) {
309                 umount_usage();
310                 return EX_USAGE;
311         }
312         
313         if (spec == NULL || (*spec != '/' && strchr(spec,':') == NULL)) {
314                 nfs_error(_("%s: %s: not found\n"), progname, spec);
315                 return EX_USAGE;
316         }
317
318         if (*spec == '/')
319                 mc = getmntdirbackward(spec, NULL);
320         else
321                 mc = getmntdevbackward(spec, NULL);
322         if (!mc && verbose)
323                 printf(_("Could not find %s in mtab\n"), spec);
324
325         if (mc && strcmp(mc->m.mnt_type, "nfs") != 0 &&
326             strcmp(mc->m.mnt_type, "nfs4") != 0) {
327                 nfs_error(_("%s: %s on %s is not an NFS filesystem"),
328                                 progname, mc->m.mnt_fsname, mc->m.mnt_dir);
329                 return EX_USAGE;
330         }
331
332         if (getuid() != 0) {
333                 /* only permitted if "user=" or "users" is in mount options */
334                 if (!mc) {
335                         /* umount might call us twice.  The second time there will
336                          * be no entry in mtab and we should just exit quietly
337                          */
338                         return EX_SUCCESS;
339
340                 only_root:
341                         nfs_error(_("%s: You are not permitted to unmount %s"),
342                                         progname, spec);
343                         return EX_USAGE;
344                 }
345                 if (hasmntopt(&mc->m, "users") == NULL) {
346                         char *opt = hasmntopt(&mc->m, "user");
347                         struct passwd *pw;
348                         char *comma;
349                         size_t len;
350                         if (!opt)
351                                 goto only_root;
352                         if (opt[4] != '=')
353                                 goto only_root;
354                         comma = strchr(opt, ',');
355                         if (comma)
356                                 len = comma - (opt + 5);
357                         else
358                                 len = strlen(opt+5);
359                         pw = getpwuid(getuid());
360                         if (pw == NULL || strlen(pw->pw_name) != len
361                             || strncmp(pw->pw_name, opt+5, len) != 0)
362                                 goto only_root;
363                 }
364         }
365
366         ret = 0;
367         if (mc) {
368                 if (!lazy && strcmp(mc->m.mnt_type, "nfs4") != 0)
369                         /* We ignore the error from nfs_umount23.
370                          * If the actual umount succeeds (in del_mtab),
371                          * we don't want to signal an error, as that
372                          * could cause /sbin/mount to retry!
373                          */
374                         nfs_umount23(mc->m.mnt_fsname, mc->m.mnt_opts);
375                 ret = del_mtab(mc->m.mnt_fsname, mc->m.mnt_dir) ?: ret;
376         } else if (*spec != '/') {
377                 if (!lazy)
378                         ret = nfs_umount23(spec, "tcp,v3");
379         } else
380                 ret = del_mtab(NULL, spec);
381
382         return ret;
383 }