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