xref: /netbsd/usr.sbin/puffs/mount_psshfs/psshfs.c (revision ef38ca99)
1 /*	$NetBSD: psshfs.c,v 1.59 2010/01/12 18:43:37 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.59 2010/01/12 18:43:37 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 	rva->va_nlink = 101; /* XXX */
291 
292 	if (detach)
293 		if (puffs_daemon(pu, 1, 1) == -1)
294 			err(1, "puffs_daemon");
295 
296 	if (puffs_mount(pu, argv[1], mntflags, puffs_getroot(pu)) == -1)
297 		err(1, "puffs_mount");
298 	if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1)
299 		err(1, "setblockingmode");
300 
301 	if (puffs_mainloop(pu) == -1)
302 		err(1, "mainloop");
303 	puffs_exit(pu, 1);
304 
305 	return 0;
306 }
307 
308 #define RETRY_MAX 100
309 
310 void
311 psshfs_notify(struct puffs_usermount *pu, int fd, int what)
312 {
313 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
314 	int nretry, which, newfd, dummy;
315 
316 	if (fd == pctx->sshfd) {
317 		which = PSSHFD_META;
318 	} else {
319 		assert(fd == pctx->sshfd_data);
320 		which = PSSHFD_DATA;
321 	}
322 
323 	if (puffs_getstate(pu) != PUFFS_STATE_RUNNING)
324 		return;
325 
326 	if (what != (PUFFS_FBIO_READ | PUFFS_FBIO_WRITE)) {
327 		puffs_framev_removefd(pu, fd, ECONNRESET);
328 		return;
329 	}
330 	close(fd);
331 
332 	/* deal with zmobies, beware of half-eaten brain */
333 	while (waitpid(-1, &dummy, WNOHANG) > 0)
334 		continue;
335 
336 	for (nretry = 0;;nretry++) {
337 		if ((newfd = pssh_connect(pu, which)) == -1)
338 			goto retry2;
339 
340 		if (puffs_framev_addfd(pu, newfd,
341 		    PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1)
342 			goto retry1;
343 
344 		break;
345  retry1:
346 		fprintf(stderr, "reconnect failed... ");
347 		close(newfd);
348  retry2:
349 		if (nretry < RETRY_MAX) {
350 			fprintf(stderr, "retry (%d left)\n", RETRY_MAX-nretry);
351 			sleep(nretry);
352 		} else {
353 			fprintf(stderr, "retry count exceeded, going south\n");
354 			exit(1); /* XXXXXXX */
355 		}
356 	}
357 }
358 
359 static int
360 pssh_connect(struct puffs_usermount *pu, int which)
361 {
362 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
363 	char * const *sshargs = pctx->sshargs;
364 	int fds[2];
365 	pid_t pid;
366 	int dnfd, x;
367 	int *sshfd;
368 	pid_t *sshpid;
369 
370 	if (which == PSSHFD_META) {
371 		sshfd = &pctx->sshfd;
372 		sshpid = &pctx->sshpid;
373 	} else {
374 		assert(which == PSSHFD_DATA);
375 		sshfd = &pctx->sshfd_data;
376 		sshpid = &pctx->sshpid_data;
377 	}
378 
379 	if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == -1)
380 		return -1;
381 
382 	pid = fork();
383 	switch (pid) {
384 	case -1:
385 		return -1;
386 		/*NOTREACHED*/
387 	case 0: /* child */
388 		if (dup2(fds[0], STDIN_FILENO) == -1)
389 			err(1, "child dup2");
390 		if (dup2(fds[0], STDOUT_FILENO) == -1)
391 			err(1, "child dup2");
392 		close(fds[0]);
393 		close(fds[1]);
394 
395 		dnfd = open(_PATH_DEVNULL, O_RDWR);
396 		if (dnfd != -1)
397 			dup2(dnfd, STDERR_FILENO);
398 
399 		execvp(sshargs[0], sshargs);
400 		/*NOTREACHED*/
401 		break;
402 	default:
403 		*sshpid = pid;
404 		*sshfd = fds[1];
405 		close(fds[0]);
406 		break;
407 	}
408 
409 	if (psshfs_handshake(pu, *sshfd) != 0)
410 		errx(1, "psshfs_handshake %d", which);
411 	x = 1;
412 	if (ioctl(*sshfd, FIONBIO, &x) == -1)
413 		err(1, "nonblocking descriptor %d", which);
414 
415 	return *sshfd;
416 }
417 
418 static void *
419 invalone(struct puffs_usermount *pu, struct puffs_node *pn, void *arg)
420 {
421 	struct psshfs_node *psn = pn->pn_data;
422 
423 	psn->attrread = 0;
424 	psn->dentread = 0;
425 	psn->slread = 0;
426 
427 	return NULL;
428 }
429 
430 static void
431 psshfs_loopfn(struct puffs_usermount *pu)
432 {
433 
434 	if (sighup) {
435 		puffs_pn_nodewalk(pu, invalone, NULL);
436 		sighup = 0;
437 	}
438 }
439