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