| Commit | Line | Data |
|---|---|---|
| ab5617b3 SW |
1 | /* $NetBSD: psshfs.c,v 1.63 2011/05/19 15:07:16 riastradh 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/types.h> | |
| 43 | #include <sys/wait.h> | |
| 44 | #include <sys/ioctl.h> | |
| 45 | #include <sys/socket.h> | |
| 46 | #include <sys/uio.h> | |
| 47 | ||
| 48 | #include <assert.h> | |
| 49 | #include <err.h> | |
| 50 | #include <errno.h> | |
| 51 | #include <paths.h> | |
| 52 | #include <poll.h> | |
| 53 | #include <puffs.h> | |
| 54 | #include <signal.h> | |
| 55 | #include <stdlib.h> | |
| 56 | #include <stdio.h> | |
| 57 | #include <unistd.h> | |
| 58 | ||
| 59 | #include "psshfs.h" | |
| 60 | #include "util_compat.h" | |
| 61 | ||
| 62 | static int pssh_connect(struct puffs_usermount *, int); | |
| 63 | static void psshfs_loopfn(struct puffs_usermount *); | |
| 64 | static void usage(void); | |
| 65 | static void add_ssharg(char ***, int *, const char *); | |
| 66 | static void psshfs_notify(struct puffs_usermount *, int, int); | |
| 67 | ||
| 68 | #define SSH_PATH "/usr/bin/ssh" | |
| 69 | ||
| 70 | unsigned int max_reads; | |
| 71 | static int sighup; | |
| 72 | ||
| 73 | static void | |
| 74 | add_ssharg(char ***sshargs, int *nargs, const char *arg) | |
| 75 | { | |
| 76 | ||
| 77 | *sshargs = realloc(*sshargs, (*nargs + 2) * sizeof(char*)); | |
| 78 | if (!*sshargs) | |
| 79 | err(1, "realloc"); | |
| 80 | (*sshargs)[(*nargs)++] = estrdup(arg); | |
| 81 | (*sshargs)[*nargs] = NULL; | |
| 82 | } | |
| 83 | ||
| 84 | static void | |
| 89a89091 | 85 | usage(void) |
| ab5617b3 SW |
86 | { |
| 87 | ||
| 88 | fprintf(stderr, "usage: %s " | |
| 89 | "[-ceprst] [-F configfile] [-O sshopt=value] [-o opts] " | |
| 90 | "user@host:path mountpath\n", | |
| 91 | getprogname()); | |
| 92 | exit(1); | |
| 93 | } | |
| 94 | ||
| 95 | static void | |
| 96 | takehup(int sig) | |
| 97 | { | |
| 98 | ||
| 99 | sighup = 1; | |
| 100 | } | |
| 101 | ||
| 102 | int | |
| 103 | main(int argc, char *argv[]) | |
| 104 | { | |
| 105 | struct psshfs_ctx pctx; | |
| 106 | struct puffs_usermount *pu; | |
| 107 | struct puffs_ops *pops; | |
| 108 | struct psshfs_node *root = &pctx.psn_root; | |
| 109 | struct puffs_node *pn_root; | |
| 110 | puffs_framev_fdnotify_fn notfn; | |
| 111 | struct vattr *rva; | |
| 112 | char **sshargs; | |
| 113 | char *userhost; | |
| 114 | char *hostpath; | |
| 115 | int mntflags, pflags, ch; | |
| 116 | int detach; | |
| 117 | int exportfs, refreshival, numconnections; | |
| 118 | int nargs; | |
| 119 | ||
| 120 | setprogname(argv[0]); | |
| 121 | puffs_unmountonsignal(SIGINT, true); | |
| 122 | puffs_unmountonsignal(SIGTERM, true); | |
| 123 | ||
| 124 | if (argc < 3) | |
| 125 | usage(); | |
| 126 | ||
| 127 | memset(&pctx, 0, sizeof(pctx)); | |
| 128 | mntflags = pflags = exportfs = nargs = 0; | |
| 129 | numconnections = 1; | |
| 130 | detach = 1; | |
| 131 | refreshival = DEFAULTREFRESH; | |
| 132 | notfn = puffs_framev_unmountonclose; | |
| 133 | sshargs = NULL; | |
| 134 | add_ssharg(&sshargs, &nargs, SSH_PATH); | |
| 135 | add_ssharg(&sshargs, &nargs, "-axs"); | |
| 136 | add_ssharg(&sshargs, &nargs, "-oClearAllForwardings=yes"); | |
| 137 | ||
| 138 | while ((ch = getopt(argc, argv, "c:eF:g:o:O:pr:st:u:")) != -1) { | |
| 139 | switch (ch) { | |
| 140 | case 'c': | |
| 141 | numconnections = atoi(optarg); | |
| 142 | if (numconnections < 1 || numconnections > 2) { | |
| 143 | fprintf(stderr, "%s: only 1 or 2 connections " | |
| 144 | "permitted currently\n", getprogname()); | |
| 145 | usage(); | |
| 146 | /*NOTREACHED*/ | |
| 147 | } | |
| 148 | break; | |
| 149 | case 'e': | |
| 150 | exportfs = 1; | |
| 151 | break; | |
| 152 | case 'F': | |
| 153 | add_ssharg(&sshargs, &nargs, "-F"); | |
| 154 | add_ssharg(&sshargs, &nargs, optarg); | |
| 155 | break; | |
| 156 | case 'g': | |
| 157 | pctx.domanglegid = 1; | |
| 158 | pctx.manglegid = atoi(optarg); | |
| 159 | if (pctx.manglegid == (gid_t)-1) | |
| 160 | errx(1, "-1 not allowed for -g"); | |
| 161 | pctx.mygid = getegid(); | |
| 162 | break; | |
| 163 | case 'O': | |
| 164 | add_ssharg(&sshargs, &nargs, "-o"); | |
| 165 | add_ssharg(&sshargs, &nargs, optarg); | |
| 166 | break; | |
| 167 | case 'o': | |
| 168 | getmntopts(optarg, puffsmopts, &mntflags, &pflags); | |
| 169 | break; | |
| 170 | case 'p': | |
| 171 | notfn = psshfs_notify; | |
| 172 | break; | |
| 173 | case 'r': | |
| 174 | max_reads = atoi(optarg); | |
| 175 | break; | |
| 176 | case 's': | |
| 177 | detach = 0; | |
| 178 | break; | |
| 179 | case 't': | |
| 180 | refreshival = atoi(optarg); | |
| 181 | if (refreshival < 0 && refreshival != -1) | |
| 182 | errx(1, "invalid timeout %d", refreshival); | |
| 183 | break; | |
| 184 | case 'u': | |
| 185 | pctx.domangleuid = 1; | |
| 186 | pctx.mangleuid = atoi(optarg); | |
| 187 | if (pctx.mangleuid == (uid_t)-1) | |
| 188 | errx(1, "-1 not allowed for -u"); | |
| 189 | pctx.myuid = geteuid(); | |
| 190 | break; | |
| 191 | default: | |
| 192 | usage(); | |
| 193 | /*NOTREACHED*/ | |
| 194 | } | |
| 195 | } | |
| 196 | argc -= optind; | |
| 197 | argv += optind; | |
| 198 | ||
| 199 | if (pflags & PUFFS_FLAG_OPDUMP) | |
| 200 | detach = 0; | |
| 201 | pflags |= PUFFS_FLAG_BUILDPATH; | |
| 202 | pflags |= PUFFS_KFLAG_WTCACHE | PUFFS_KFLAG_IAONDEMAND; | |
| 203 | ||
| 204 | if (argc != 2) | |
| 205 | usage(); | |
| 206 | ||
| 207 | PUFFSOP_INIT(pops); | |
| 208 | ||
| 209 | PUFFSOP_SET(pops, psshfs, fs, unmount); | |
| 210 | PUFFSOP_SETFSNOP(pops, sync); /* XXX */ | |
| 211 | PUFFSOP_SET(pops, psshfs, fs, statvfs); | |
| 212 | PUFFSOP_SET(pops, psshfs, fs, nodetofh); | |
| 213 | PUFFSOP_SET(pops, psshfs, fs, fhtonode); | |
| 214 | ||
| 215 | PUFFSOP_SET(pops, psshfs, node, lookup); | |
| 216 | PUFFSOP_SET(pops, psshfs, node, create); | |
| 217 | PUFFSOP_SET(pops, psshfs, node, open); | |
| 218 | PUFFSOP_SET(pops, psshfs, node, inactive); | |
| 219 | PUFFSOP_SET(pops, psshfs, node, readdir); | |
| 220 | PUFFSOP_SET(pops, psshfs, node, getattr); | |
| 221 | PUFFSOP_SET(pops, psshfs, node, setattr); | |
| 222 | PUFFSOP_SET(pops, psshfs, node, mkdir); | |
| 223 | PUFFSOP_SET(pops, psshfs, node, remove); | |
| 224 | PUFFSOP_SET(pops, psshfs, node, readlink); | |
| 225 | PUFFSOP_SET(pops, psshfs, node, rmdir); | |
| 3f35aa53 | 226 | PUFFSOP_SET(pops, psshfs, node, link); |
| ab5617b3 SW |
227 | PUFFSOP_SET(pops, psshfs, node, symlink); |
| 228 | PUFFSOP_SET(pops, psshfs, node, rename); | |
| 229 | PUFFSOP_SET(pops, psshfs, node, read); | |
| 230 | PUFFSOP_SET(pops, psshfs, node, write); | |
| 231 | PUFFSOP_SET(pops, psshfs, node, reclaim); | |
| 232 | ||
| 233 | pu = puffs_init(pops, argv[0], "psshfs", &pctx, pflags); | |
| 234 | if (pu == NULL) | |
| 235 | err(1, "puffs_init"); | |
| 236 | ||
| 237 | pctx.mounttime = time(NULL); | |
| 238 | pctx.refreshival = refreshival; | |
| 239 | pctx.numconnections = numconnections; | |
| 240 | ||
| 241 | userhost = argv[0]; | |
| 242 | hostpath = strchr(userhost, ':'); | |
| 243 | if (hostpath) { | |
| 244 | *hostpath++ = '\0'; | |
| 245 | pctx.mountpath = hostpath; | |
| 246 | } else | |
| 247 | pctx.mountpath = "."; | |
| 248 | ||
| 249 | add_ssharg(&sshargs, &nargs, argv[0]); | |
| 250 | add_ssharg(&sshargs, &nargs, "sftp"); | |
| 251 | pctx.sshargs = sshargs; | |
| 252 | ||
| 253 | pctx.nextino = 2; | |
| 254 | memset(root, 0, sizeof(struct psshfs_node)); | |
| 255 | TAILQ_INIT(&root->pw); | |
| 256 | pn_root = puffs_pn_new(pu, root); | |
| 257 | if (pn_root == NULL) | |
| 258 | return errno; | |
| 259 | puffs_setroot(pu, pn_root); | |
| 260 | ||
| 261 | puffs_framev_init(pu, psbuf_read, psbuf_write, psbuf_cmp, NULL, notfn); | |
| 262 | ||
| 263 | signal(SIGHUP, takehup); | |
| 264 | puffs_ml_setloopfn(pu, psshfs_loopfn); | |
| 265 | if (pssh_connect(pu, PSSHFD_META) == -1) | |
| 266 | err(1, "can't connect meta"); | |
| 267 | if (puffs_framev_addfd(pu, pctx.sshfd, | |
| 268 | PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1) | |
| 269 | err(1, "framebuf addfd meta"); | |
| 270 | if (numconnections == 2) { | |
| 271 | if (pssh_connect(pu, PSSHFD_DATA) == -1) | |
| 272 | err(1, "can't connect data"); | |
| 273 | if (puffs_framev_addfd(pu, pctx.sshfd_data, | |
| 274 | PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1) | |
| 275 | err(1, "framebuf addfd data"); | |
| 276 | } else { | |
| 277 | pctx.sshfd_data = pctx.sshfd; | |
| 278 | } | |
| 279 | ||
| 280 | if (exportfs) | |
| 281 | puffs_setfhsize(pu, sizeof(struct psshfs_fid), | |
| 282 | PUFFS_FHFLAG_NFSV2 | PUFFS_FHFLAG_NFSV3); | |
| 283 | ||
| 284 | rva = &pn_root->pn_va; | |
| 285 | rva->va_fileid = pctx.nextino++; | |
| 286 | ||
| 287 | /* | |
| 288 | * For root link count, just guess something ridiculously high. | |
| 289 | * Guessing too high has no known adverse effects, but fts(3) | |
| 290 | * doesn't like too low values. This guess will be replaced | |
| 291 | * with the real value when readdir is first called for | |
| 292 | * the root directory. | |
| 293 | */ | |
| 294 | rva->va_nlink = 8811; | |
| 295 | ||
| 296 | if (detach) | |
| 297 | if (puffs_daemon(pu, 1, 1) == -1) | |
| 298 | err(1, "puffs_daemon"); | |
| 299 | ||
| 300 | if (puffs_mount(pu, argv[1], mntflags, puffs_getroot(pu)) == -1) | |
| 301 | err(1, "puffs_mount"); | |
| 302 | if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1) | |
| 303 | err(1, "setblockingmode"); | |
| 304 | ||
| 305 | if (puffs_mainloop(pu) == -1) | |
| 306 | err(1, "mainloop"); | |
| 307 | puffs_exit(pu, 1); | |
| 308 | ||
| 309 | return 0; | |
| 310 | } | |
| 311 | ||
| 312 | #define RETRY_MAX 100 | |
| 313 | ||
| 314 | void | |
| 315 | psshfs_notify(struct puffs_usermount *pu, int fd, int what) | |
| 316 | { | |
| 317 | struct psshfs_ctx *pctx = puffs_getspecific(pu); | |
| 318 | int nretry, which, newfd, dummy; | |
| 319 | ||
| 320 | if (fd == pctx->sshfd) { | |
| 321 | which = PSSHFD_META; | |
| 322 | } else { | |
| 323 | assert(fd == pctx->sshfd_data); | |
| 324 | which = PSSHFD_DATA; | |
| 325 | } | |
| 326 | ||
| 327 | if (puffs_getstate(pu) != PUFFS_STATE_RUNNING) | |
| 328 | return; | |
| 329 | ||
| 330 | if (what != (PUFFS_FBIO_READ | PUFFS_FBIO_WRITE)) { | |
| 331 | puffs_framev_removefd(pu, fd, ECONNRESET); | |
| 332 | return; | |
| 333 | } | |
| 334 | close(fd); | |
| 335 | ||
| 336 | /* deal with zmobies, beware of half-eaten brain */ | |
| 337 | while (waitpid(-1, &dummy, WNOHANG) > 0) | |
| 338 | continue; | |
| 339 | ||
| 340 | for (nretry = 0;;nretry++) { | |
| 341 | if ((newfd = pssh_connect(pu, which)) == -1) | |
| 342 | goto retry2; | |
| 343 | ||
| 344 | if (puffs_framev_addfd(pu, newfd, | |
| 345 | PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1) | |
| 346 | goto retry1; | |
| 347 | ||
| 348 | break; | |
| 349 | retry1: | |
| 350 | fprintf(stderr, "reconnect failed... "); | |
| 351 | close(newfd); | |
| 352 | retry2: | |
| 353 | if (nretry < RETRY_MAX) { | |
| 354 | fprintf(stderr, "retry (%d left)\n", RETRY_MAX-nretry); | |
| 355 | sleep(nretry); | |
| 356 | } else { | |
| 357 | fprintf(stderr, "retry count exceeded, going south\n"); | |
| 358 | exit(1); /* XXXXXXX */ | |
| 359 | } | |
| 360 | } | |
| 361 | } | |
| 362 | ||
| 363 | static int | |
| 364 | pssh_connect(struct puffs_usermount *pu, int which) | |
| 365 | { | |
| 366 | struct psshfs_ctx *pctx = puffs_getspecific(pu); | |
| 367 | char * const *sshargs = pctx->sshargs; | |
| 368 | int fds[2]; | |
| 369 | pid_t pid; | |
| 370 | int dnfd, x; | |
| 371 | int *sshfd; | |
| 372 | pid_t *sshpid; | |
| 373 | ||
| 374 | if (which == PSSHFD_META) { | |
| 375 | sshfd = &pctx->sshfd; | |
| 376 | sshpid = &pctx->sshpid; | |
| 377 | } else { | |
| 378 | assert(which == PSSHFD_DATA); | |
| 379 | sshfd = &pctx->sshfd_data; | |
| 380 | sshpid = &pctx->sshpid_data; | |
| 381 | } | |
| 382 | ||
| 383 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == -1) | |
| 384 | return -1; | |
| 385 | ||
| 386 | pid = fork(); | |
| 387 | switch (pid) { | |
| 388 | case -1: | |
| 389 | return -1; | |
| 390 | /*NOTREACHED*/ | |
| 391 | case 0: /* child */ | |
| 392 | if (dup2(fds[0], STDIN_FILENO) == -1) | |
| 393 | err(1, "child dup2"); | |
| 394 | if (dup2(fds[0], STDOUT_FILENO) == -1) | |
| 395 | err(1, "child dup2"); | |
| 396 | close(fds[0]); | |
| 397 | close(fds[1]); | |
| 398 | ||
| 399 | dnfd = open(_PATH_DEVNULL, O_RDWR); | |
| 400 | if (dnfd != -1) | |
| 401 | dup2(dnfd, STDERR_FILENO); | |
| 402 | ||
| 403 | execvp(sshargs[0], sshargs); | |
| 404 | /*NOTREACHED*/ | |
| 405 | break; | |
| 406 | default: | |
| 407 | *sshpid = pid; | |
| 408 | *sshfd = fds[1]; | |
| 409 | close(fds[0]); | |
| 410 | break; | |
| 411 | } | |
| 412 | ||
| 413 | if (psshfs_handshake(pu, *sshfd) != 0) | |
| 414 | errx(1, "handshake failed, server does not support sftp?"); | |
| 415 | x = 1; | |
| 416 | if (ioctl(*sshfd, FIONBIO, &x) == -1) | |
| 417 | err(1, "nonblocking descriptor %d", which); | |
| 418 | ||
| 419 | return *sshfd; | |
| 420 | } | |
| 421 | ||
| 422 | static void * | |
| 423 | invalone(struct puffs_usermount *pu, struct puffs_node *pn, void *arg) | |
| 424 | { | |
| 425 | struct psshfs_node *psn = pn->pn_data; | |
| 426 | ||
| 427 | psn->attrread = 0; | |
| 428 | psn->dentread = 0; | |
| 429 | psn->slread = 0; | |
| 430 | ||
| 431 | return NULL; | |
| 432 | } | |
| 433 | ||
| 434 | static void | |
| 435 | psshfs_loopfn(struct puffs_usermount *pu) | |
| 436 | { | |
| 437 | ||
| 438 | if (sighup) { | |
| 439 | puffs_pn_nodewalk(pu, invalone, NULL); | |
| 440 | sighup = 0; | |
| 441 | } | |
| 442 | } |