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