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