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/cdefs.h> 43 #ifndef lint 44 __RCSID("$NetBSD: psshfs.c,v 1.63 2011/05/19 15:07:16 riastradh 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 TAILQ_INIT(&root->pw); 261 pn_root = puffs_pn_new(pu, root); 262 if (pn_root == NULL) 263 return errno; 264 puffs_setroot(pu, pn_root); 265 266 puffs_framev_init(pu, psbuf_read, psbuf_write, psbuf_cmp, NULL, notfn); 267 268 signal(SIGHUP, takehup); 269 puffs_ml_setloopfn(pu, psshfs_loopfn); 270 if (pssh_connect(pu, PSSHFD_META) == -1) 271 err(1, "can't connect meta"); 272 if (puffs_framev_addfd(pu, pctx.sshfd, 273 PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1) 274 err(1, "framebuf addfd meta"); 275 if (numconnections == 2) { 276 if (pssh_connect(pu, PSSHFD_DATA) == -1) 277 err(1, "can't connect data"); 278 if (puffs_framev_addfd(pu, pctx.sshfd_data, 279 PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1) 280 err(1, "framebuf addfd data"); 281 } else { 282 pctx.sshfd_data = pctx.sshfd; 283 } 284 285 if (exportfs) 286 puffs_setfhsize(pu, sizeof(struct psshfs_fid), 287 PUFFS_FHFLAG_NFSV2 | PUFFS_FHFLAG_NFSV3); 288 289 rva = &pn_root->pn_va; 290 rva->va_fileid = pctx.nextino++; 291 292 /* 293 * For root link count, just guess something ridiculously high. 294 * Guessing too high has no known adverse effects, but fts(3) 295 * doesn't like too low values. This guess will be replaced 296 * with the real value when readdir is first called for 297 * the root directory. 298 */ 299 rva->va_nlink = 8811; 300 301 if (detach) 302 if (puffs_daemon(pu, 1, 1) == -1) 303 err(1, "puffs_daemon"); 304 305 if (puffs_mount(pu, argv[1], mntflags, puffs_getroot(pu)) == -1) 306 err(1, "puffs_mount"); 307 if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1) 308 err(1, "setblockingmode"); 309 310 if (puffs_mainloop(pu) == -1) 311 err(1, "mainloop"); 312 puffs_exit(pu, 1); 313 314 return 0; 315 } 316 317 #define RETRY_MAX 100 318 319 void 320 psshfs_notify(struct puffs_usermount *pu, int fd, int what) 321 { 322 struct psshfs_ctx *pctx = puffs_getspecific(pu); 323 int nretry, which, newfd, dummy; 324 325 if (fd == pctx->sshfd) { 326 which = PSSHFD_META; 327 } else { 328 assert(fd == pctx->sshfd_data); 329 which = PSSHFD_DATA; 330 } 331 332 if (puffs_getstate(pu) != PUFFS_STATE_RUNNING) 333 return; 334 335 if (what != (PUFFS_FBIO_READ | PUFFS_FBIO_WRITE)) { 336 puffs_framev_removefd(pu, fd, ECONNRESET); 337 return; 338 } 339 close(fd); 340 341 /* deal with zmobies, beware of half-eaten brain */ 342 while (waitpid(-1, &dummy, WNOHANG) > 0) 343 continue; 344 345 for (nretry = 0;;nretry++) { 346 if ((newfd = pssh_connect(pu, which)) == -1) 347 goto retry2; 348 349 if (puffs_framev_addfd(pu, newfd, 350 PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1) 351 goto retry1; 352 353 break; 354 retry1: 355 fprintf(stderr, "reconnect failed... "); 356 close(newfd); 357 retry2: 358 if (nretry < RETRY_MAX) { 359 fprintf(stderr, "retry (%d left)\n", RETRY_MAX-nretry); 360 sleep(nretry); 361 } else { 362 fprintf(stderr, "retry count exceeded, going south\n"); 363 exit(1); /* XXXXXXX */ 364 } 365 } 366 } 367 368 static int 369 pssh_connect(struct puffs_usermount *pu, int which) 370 { 371 struct psshfs_ctx *pctx = puffs_getspecific(pu); 372 char * const *sshargs = pctx->sshargs; 373 int fds[2]; 374 pid_t pid; 375 int dnfd, x; 376 int *sshfd; 377 pid_t *sshpid; 378 379 if (which == PSSHFD_META) { 380 sshfd = &pctx->sshfd; 381 sshpid = &pctx->sshpid; 382 } else { 383 assert(which == PSSHFD_DATA); 384 sshfd = &pctx->sshfd_data; 385 sshpid = &pctx->sshpid_data; 386 } 387 388 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == -1) 389 return -1; 390 391 pid = fork(); 392 switch (pid) { 393 case -1: 394 return -1; 395 /*NOTREACHED*/ 396 case 0: /* child */ 397 if (dup2(fds[0], STDIN_FILENO) == -1) 398 err(1, "child dup2"); 399 if (dup2(fds[0], STDOUT_FILENO) == -1) 400 err(1, "child dup2"); 401 close(fds[0]); 402 close(fds[1]); 403 404 dnfd = open(_PATH_DEVNULL, O_RDWR); 405 if (dnfd != -1) 406 dup2(dnfd, STDERR_FILENO); 407 408 execvp(sshargs[0], sshargs); 409 /*NOTREACHED*/ 410 break; 411 default: 412 *sshpid = pid; 413 *sshfd = fds[1]; 414 close(fds[0]); 415 break; 416 } 417 418 if (psshfs_handshake(pu, *sshfd) != 0) 419 errx(1, "handshake failed, server does not support sftp?"); 420 x = 1; 421 if (ioctl(*sshfd, FIONBIO, &x) == -1) 422 err(1, "nonblocking descriptor %d", which); 423 424 return *sshfd; 425 } 426 427 static void * 428 invalone(struct puffs_usermount *pu, struct puffs_node *pn, void *arg) 429 { 430 struct psshfs_node *psn = pn->pn_data; 431 432 psn->attrread = 0; 433 psn->dentread = 0; 434 psn->slread = 0; 435 436 return NULL; 437 } 438 439 static void 440 psshfs_loopfn(struct puffs_usermount *pu) 441 { 442 443 if (sighup) { 444 puffs_pn_nodewalk(pu, invalone, NULL); 445 sighup = 0; 446 } 447 } 448