psshfs.c revision 1.60 1 /* $NetBSD: psshfs.c,v 1.60 2010/02/17 15:50:54 pooka Exp $ */
2
3 /*
4 * Copyright (c) 2006-2009 Antti Kantee. All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 /*
29 * psshfs: puffs sshfs
30 *
31 * psshfs implements sshfs functionality on top of puffs making it
32 * possible to mount a filesystme through the sftp service.
33 *
34 * psshfs can execute multiple operations in "parallel" by using the
35 * puffs_cc framework for continuations.
36 *
37 * Concurrency control is handled currently by vnode locking (this
38 * will change in the future). Context switch locations are easy to
39 * find by grepping for puffs_framebuf_enqueue_cc().
40 */
41
42 #include <sys/cdefs.h>
43 #ifndef lint
44 __RCSID("$NetBSD: psshfs.c,v 1.60 2010/02/17 15:50:54 pooka Exp $");
45 #endif /* !lint */
46
47 #include <sys/types.h>
48 #include <sys/wait.h>
49
50 #include <assert.h>
51 #include <err.h>
52 #include <errno.h>
53 #include <mntopts.h>
54 #include <paths.h>
55 #include <poll.h>
56 #include <puffs.h>
57 #include <signal.h>
58 #include <stdlib.h>
59 #include <util.h>
60 #include <unistd.h>
61
62 #include "psshfs.h"
63
64 static int pssh_connect(struct puffs_usermount *, int);
65 static void psshfs_loopfn(struct puffs_usermount *);
66 static void usage(void);
67 static void add_ssharg(char ***, int *, const char *);
68 static void psshfs_notify(struct puffs_usermount *, int, int);
69
70 #define SSH_PATH "/usr/bin/ssh"
71
72 unsigned int max_reads;
73 static int sighup;
74
75 static void
76 add_ssharg(char ***sshargs, int *nargs, const char *arg)
77 {
78
79 *sshargs = realloc(*sshargs, (*nargs + 2) * sizeof(char*));
80 if (!*sshargs)
81 err(1, "realloc");
82 (*sshargs)[(*nargs)++] = estrdup(arg);
83 (*sshargs)[*nargs] = NULL;
84 }
85
86 static void
87 usage()
88 {
89
90 fprintf(stderr, "usage: %s "
91 "[-ceprst] [-F configfile] [-O sshopt=value] [-o opts] "
92 "user@host:path mountpath\n",
93 getprogname());
94 exit(1);
95 }
96
97 static void
98 takehup(int sig)
99 {
100
101 sighup = 1;
102 }
103
104 int
105 main(int argc, char *argv[])
106 {
107 struct psshfs_ctx pctx;
108 struct puffs_usermount *pu;
109 struct puffs_ops *pops;
110 struct psshfs_node *root = &pctx.psn_root;
111 struct puffs_node *pn_root;
112 puffs_framev_fdnotify_fn notfn;
113 struct vattr *rva;
114 mntoptparse_t mp;
115 char **sshargs;
116 char *userhost;
117 char *hostpath;
118 int mntflags, pflags, ch;
119 int detach;
120 int exportfs, refreshival, numconnections;
121 int nargs;
122
123 setprogname(argv[0]);
124 puffs_unmountonsignal(SIGINT, true);
125 puffs_unmountonsignal(SIGTERM, true);
126
127 if (argc < 3)
128 usage();
129
130 memset(&pctx, 0, sizeof(pctx));
131 mntflags = pflags = exportfs = nargs = 0;
132 numconnections = 1;
133 detach = 1;
134 refreshival = DEFAULTREFRESH;
135 notfn = puffs_framev_unmountonclose;
136 sshargs = NULL;
137 add_ssharg(&sshargs, &nargs, SSH_PATH);
138 add_ssharg(&sshargs, &nargs, "-axs");
139 add_ssharg(&sshargs, &nargs, "-oClearAllForwardings=yes");
140
141 while ((ch = getopt(argc, argv, "c:eF:g:o:O:pr:st:u:")) != -1) {
142 switch (ch) {
143 case 'c':
144 numconnections = atoi(optarg);
145 if (numconnections < 1 || numconnections > 2) {
146 fprintf(stderr, "%s: only 1 or 2 connections "
147 "permitted currently\n", getprogname());
148 usage();
149 /*NOTREACHED*/
150 }
151 break;
152 case 'e':
153 exportfs = 1;
154 break;
155 case 'F':
156 add_ssharg(&sshargs, &nargs, "-F");
157 add_ssharg(&sshargs, &nargs, optarg);
158 break;
159 case 'g':
160 pctx.domanglegid = 1;
161 pctx.manglegid = atoi(optarg);
162 if (pctx.manglegid == (gid_t)-1)
163 errx(1, "-1 not allowed for -g");
164 pctx.mygid = getegid();
165 break;
166 case 'O':
167 add_ssharg(&sshargs, &nargs, "-o");
168 add_ssharg(&sshargs, &nargs, optarg);
169 break;
170 case 'o':
171 mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
172 if (mp == NULL)
173 err(1, "getmntopts");
174 freemntopts(mp);
175 break;
176 case 'p':
177 notfn = psshfs_notify;
178 break;
179 case 'r':
180 max_reads = atoi(optarg);
181 break;
182 case 's':
183 detach = 0;
184 break;
185 case 't':
186 refreshival = atoi(optarg);
187 if (refreshival < 0 && refreshival != -1)
188 errx(1, "invalid timeout %d", refreshival);
189 break;
190 case 'u':
191 pctx.domangleuid = 1;
192 pctx.mangleuid = atoi(optarg);
193 if (pctx.mangleuid == (uid_t)-1)
194 errx(1, "-1 not allowed for -u");
195 pctx.myuid = geteuid();
196 break;
197 default:
198 usage();
199 /*NOTREACHED*/
200 }
201 }
202 argc -= optind;
203 argv += optind;
204
205 if (pflags & PUFFS_FLAG_OPDUMP)
206 detach = 0;
207 pflags |= PUFFS_FLAG_BUILDPATH;
208 pflags |= PUFFS_KFLAG_WTCACHE | PUFFS_KFLAG_IAONDEMAND;
209
210 if (argc != 2)
211 usage();
212
213 PUFFSOP_INIT(pops);
214
215 PUFFSOP_SET(pops, psshfs, fs, unmount);
216 PUFFSOP_SETFSNOP(pops, sync); /* XXX */
217 PUFFSOP_SET(pops, psshfs, fs, statvfs);
218 PUFFSOP_SET(pops, psshfs, fs, nodetofh);
219 PUFFSOP_SET(pops, psshfs, fs, fhtonode);
220
221 PUFFSOP_SET(pops, psshfs, node, lookup);
222 PUFFSOP_SET(pops, psshfs, node, create);
223 PUFFSOP_SET(pops, psshfs, node, open);
224 PUFFSOP_SET(pops, psshfs, node, inactive);
225 PUFFSOP_SET(pops, psshfs, node, readdir);
226 PUFFSOP_SET(pops, psshfs, node, getattr);
227 PUFFSOP_SET(pops, psshfs, node, setattr);
228 PUFFSOP_SET(pops, psshfs, node, mkdir);
229 PUFFSOP_SET(pops, psshfs, node, remove);
230 PUFFSOP_SET(pops, psshfs, node, readlink);
231 PUFFSOP_SET(pops, psshfs, node, rmdir);
232 PUFFSOP_SET(pops, psshfs, node, symlink);
233 PUFFSOP_SET(pops, psshfs, node, rename);
234 PUFFSOP_SET(pops, psshfs, node, read);
235 PUFFSOP_SET(pops, psshfs, node, write);
236 PUFFSOP_SET(pops, psshfs, node, reclaim);
237
238 pu = puffs_init(pops, argv[0], "psshfs", &pctx, pflags);
239 if (pu == NULL)
240 err(1, "puffs_init");
241
242 pctx.mounttime = time(NULL);
243 pctx.refreshival = refreshival;
244 pctx.numconnections = numconnections;
245
246 userhost = argv[0];
247 hostpath = strchr(userhost, ':');
248 if (hostpath) {
249 *hostpath++ = '\0';
250 pctx.mountpath = hostpath;
251 } else
252 pctx.mountpath = ".";
253
254 add_ssharg(&sshargs, &nargs, argv[0]);
255 add_ssharg(&sshargs, &nargs, "sftp");
256 pctx.sshargs = sshargs;
257
258 pctx.nextino = 2;
259 memset(root, 0, sizeof(struct psshfs_node));
260 pn_root = puffs_pn_new(pu, root);
261 if (pn_root == NULL)
262 return errno;
263 puffs_setroot(pu, pn_root);
264
265 puffs_framev_init(pu, psbuf_read, psbuf_write, psbuf_cmp, NULL, notfn);
266
267 signal(SIGHUP, takehup);
268 puffs_ml_setloopfn(pu, psshfs_loopfn);
269 if (pssh_connect(pu, PSSHFD_META) == -1)
270 err(1, "can't connect meta");
271 if (puffs_framev_addfd(pu, pctx.sshfd,
272 PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1)
273 err(1, "framebuf addfd meta");
274 if (numconnections == 2) {
275 if (pssh_connect(pu, PSSHFD_DATA) == -1)
276 err(1, "can't connect data");
277 if (puffs_framev_addfd(pu, pctx.sshfd_data,
278 PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1)
279 err(1, "framebuf addfd data");
280 } else {
281 pctx.sshfd_data = pctx.sshfd;
282 }
283
284 if (exportfs)
285 puffs_setfhsize(pu, sizeof(struct psshfs_fid),
286 PUFFS_FHFLAG_NFSV2 | PUFFS_FHFLAG_NFSV3);
287
288 rva = &pn_root->pn_va;
289 rva->va_fileid = pctx.nextino++;
290
291 /*
292 * For root link count, just guess something ridiculously high.
293 * Guessing too high has no known adverse effects, but fsu()
294 * doesn't like too low values. This guess will be replaced
295 * with the real value when readdir is first called for
296 * the root directory.
297 */
298 rva->va_nlink = 8811;
299
300 if (detach)
301 if (puffs_daemon(pu, 1, 1) == -1)
302 err(1, "puffs_daemon");
303
304 if (puffs_mount(pu, argv[1], mntflags, puffs_getroot(pu)) == -1)
305 err(1, "puffs_mount");
306 if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1)
307 err(1, "setblockingmode");
308
309 if (puffs_mainloop(pu) == -1)
310 err(1, "mainloop");
311 puffs_exit(pu, 1);
312
313 return 0;
314 }
315
316 #define RETRY_MAX 100
317
318 void
319 psshfs_notify(struct puffs_usermount *pu, int fd, int what)
320 {
321 struct psshfs_ctx *pctx = puffs_getspecific(pu);
322 int nretry, which, newfd, dummy;
323
324 if (fd == pctx->sshfd) {
325 which = PSSHFD_META;
326 } else {
327 assert(fd == pctx->sshfd_data);
328 which = PSSHFD_DATA;
329 }
330
331 if (puffs_getstate(pu) != PUFFS_STATE_RUNNING)
332 return;
333
334 if (what != (PUFFS_FBIO_READ | PUFFS_FBIO_WRITE)) {
335 puffs_framev_removefd(pu, fd, ECONNRESET);
336 return;
337 }
338 close(fd);
339
340 /* deal with zmobies, beware of half-eaten brain */
341 while (waitpid(-1, &dummy, WNOHANG) > 0)
342 continue;
343
344 for (nretry = 0;;nretry++) {
345 if ((newfd = pssh_connect(pu, which)) == -1)
346 goto retry2;
347
348 if (puffs_framev_addfd(pu, newfd,
349 PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1)
350 goto retry1;
351
352 break;
353 retry1:
354 fprintf(stderr, "reconnect failed... ");
355 close(newfd);
356 retry2:
357 if (nretry < RETRY_MAX) {
358 fprintf(stderr, "retry (%d left)\n", RETRY_MAX-nretry);
359 sleep(nretry);
360 } else {
361 fprintf(stderr, "retry count exceeded, going south\n");
362 exit(1); /* XXXXXXX */
363 }
364 }
365 }
366
367 static int
368 pssh_connect(struct puffs_usermount *pu, int which)
369 {
370 struct psshfs_ctx *pctx = puffs_getspecific(pu);
371 char * const *sshargs = pctx->sshargs;
372 int fds[2];
373 pid_t pid;
374 int dnfd, x;
375 int *sshfd;
376 pid_t *sshpid;
377
378 if (which == PSSHFD_META) {
379 sshfd = &pctx->sshfd;
380 sshpid = &pctx->sshpid;
381 } else {
382 assert(which == PSSHFD_DATA);
383 sshfd = &pctx->sshfd_data;
384 sshpid = &pctx->sshpid_data;
385 }
386
387 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == -1)
388 return -1;
389
390 pid = fork();
391 switch (pid) {
392 case -1:
393 return -1;
394 /*NOTREACHED*/
395 case 0: /* child */
396 if (dup2(fds[0], STDIN_FILENO) == -1)
397 err(1, "child dup2");
398 if (dup2(fds[0], STDOUT_FILENO) == -1)
399 err(1, "child dup2");
400 close(fds[0]);
401 close(fds[1]);
402
403 dnfd = open(_PATH_DEVNULL, O_RDWR);
404 if (dnfd != -1)
405 dup2(dnfd, STDERR_FILENO);
406
407 execvp(sshargs[0], sshargs);
408 /*NOTREACHED*/
409 break;
410 default:
411 *sshpid = pid;
412 *sshfd = fds[1];
413 close(fds[0]);
414 break;
415 }
416
417 if (psshfs_handshake(pu, *sshfd) != 0)
418 errx(1, "psshfs_handshake %d", which);
419 x = 1;
420 if (ioctl(*sshfd, FIONBIO, &x) == -1)
421 err(1, "nonblocking descriptor %d", which);
422
423 return *sshfd;
424 }
425
426 static void *
427 invalone(struct puffs_usermount *pu, struct puffs_node *pn, void *arg)
428 {
429 struct psshfs_node *psn = pn->pn_data;
430
431 psn->attrread = 0;
432 psn->dentread = 0;
433 psn->slread = 0;
434
435 return NULL;
436 }
437
438 static void
439 psshfs_loopfn(struct puffs_usermount *pu)
440 {
441
442 if (sighup) {
443 puffs_pn_nodewalk(pu, invalone, NULL);
444 sighup = 0;
445 }
446 }
447