xref: /netbsd-src/usr.sbin/puffs/mount_psshfs/psshfs.c (revision 10ad5ffa714ce1a679dcc9dd8159648df2d67b5a)
1 /*	$NetBSD: psshfs.c,v 1.54 2009/05/20 15:04:36 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.54 2009/05/20 15:04:36 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 *, 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, char *arg)
77 {
78 
79 	*sshargs = realloc(*sshargs, (*nargs + 2) * sizeof(char*));
80 	if (!*sshargs)
81 		err(1, "realloc");
82 	(*sshargs)[(*nargs)++] = 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 
125 	if (argc < 3)
126 		usage();
127 
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:o:O:pr:st:")) != -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 'O':
157 			add_ssharg(&sshargs, &nargs, "-o");
158 			add_ssharg(&sshargs, &nargs, optarg);
159 			break;
160 		case 'o':
161 			mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
162 			if (mp == NULL)
163 				err(1, "getmntopts");
164 			freemntopts(mp);
165 			break;
166 		case 'p':
167 			notfn = psshfs_notify;
168 			break;
169 		case 'r':
170 			max_reads = atoi(optarg);
171 			break;
172 		case 's':
173 			detach = 0;
174 			break;
175 		case 't':
176 			refreshival = atoi(optarg);
177 			if (refreshival < 0 && refreshival != -1)
178 				errx(1, "invalid timeout %d", refreshival);
179 			break;
180 		default:
181 			usage();
182 			/*NOTREACHED*/
183 		}
184 	}
185 	argc -= optind;
186 	argv += optind;
187 
188 	if (pflags & PUFFS_FLAG_OPDUMP)
189 		detach = 0;
190 	pflags |= PUFFS_FLAG_BUILDPATH;
191 	pflags |= PUFFS_KFLAG_WTCACHE | PUFFS_KFLAG_IAONDEMAND;
192 
193 	if (argc != 2)
194 		usage();
195 
196 	PUFFSOP_INIT(pops);
197 
198 	PUFFSOP_SET(pops, psshfs, fs, unmount);
199 	PUFFSOP_SETFSNOP(pops, sync); /* XXX */
200 	PUFFSOP_SET(pops, psshfs, fs, statvfs);
201 	PUFFSOP_SET(pops, psshfs, fs, nodetofh);
202 	PUFFSOP_SET(pops, psshfs, fs, fhtonode);
203 
204 	PUFFSOP_SET(pops, psshfs, node, lookup);
205 	PUFFSOP_SET(pops, psshfs, node, create);
206 	PUFFSOP_SET(pops, psshfs, node, open);
207 	PUFFSOP_SET(pops, psshfs, node, inactive);
208 	PUFFSOP_SET(pops, psshfs, node, readdir);
209 	PUFFSOP_SET(pops, psshfs, node, getattr);
210 	PUFFSOP_SET(pops, psshfs, node, setattr);
211 	PUFFSOP_SET(pops, psshfs, node, mkdir);
212 	PUFFSOP_SET(pops, psshfs, node, remove);
213 	PUFFSOP_SET(pops, psshfs, node, readlink);
214 	PUFFSOP_SET(pops, psshfs, node, rmdir);
215 	PUFFSOP_SET(pops, psshfs, node, symlink);
216 	PUFFSOP_SET(pops, psshfs, node, rename);
217 	PUFFSOP_SET(pops, psshfs, node, read);
218 	PUFFSOP_SET(pops, psshfs, node, write);
219 	PUFFSOP_SET(pops, psshfs, node, reclaim);
220 
221 	pu = puffs_init(pops, argv[0], "psshfs", &pctx, pflags);
222 	if (pu == NULL)
223 		err(1, "puffs_init");
224 
225 	memset(&pctx, 0, sizeof(pctx));
226 	pctx.mounttime = time(NULL);
227 	pctx.refreshival = refreshival;
228 	pctx.numconnections = numconnections;
229 
230 	userhost = argv[0];
231 	hostpath = strchr(userhost, ':');
232 	if (hostpath) {
233 		*hostpath++ = '\0';
234 		pctx.mountpath = hostpath;
235 	} else
236 		pctx.mountpath = ".";
237 
238 	add_ssharg(&sshargs, &nargs, argv[0]);
239 	add_ssharg(&sshargs, &nargs, "sftp");
240 	pctx.sshargs = sshargs;
241 
242 	pctx.nextino = 2;
243 	memset(root, 0, sizeof(struct psshfs_node));
244 	pn_root = puffs_pn_new(pu, root);
245 	if (pn_root == NULL)
246 		return errno;
247 	puffs_setroot(pu, pn_root);
248 
249 	puffs_framev_init(pu, psbuf_read, psbuf_write, psbuf_cmp, NULL, notfn);
250 
251 	signal(SIGHUP, takehup);
252 	puffs_ml_setloopfn(pu, psshfs_loopfn);
253 	if (pssh_connect(pu, PSSHFD_META) == -1)
254 		err(1, "can't connect meta");
255 	if (puffs_framev_addfd(pu, pctx.sshfd,
256 	    PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1)
257 		err(1, "framebuf addfd meta");
258 	if (numconnections == 2) {
259 		if (pssh_connect(pu, PSSHFD_DATA) == -1)
260 			err(1, "can't connect data");
261 		if (puffs_framev_addfd(pu, pctx.sshfd_data,
262 		    PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1)
263 			err(1, "framebuf addfd data");
264 	} else {
265 		pctx.sshfd_data = pctx.sshfd;
266 	}
267 
268 	if (exportfs)
269 		puffs_setfhsize(pu, sizeof(struct psshfs_fid),
270 		    PUFFS_FHFLAG_NFSV2 | PUFFS_FHFLAG_NFSV3);
271 
272 	rva = &pn_root->pn_va;
273 	rva->va_fileid = pctx.nextino++;
274 	rva->va_nlink = 101; /* XXX */
275 
276 	if (detach)
277 		if (puffs_daemon(pu, 1, 1) == -1)
278 			err(1, "puffs_daemon");
279 
280 	if (puffs_mount(pu, argv[1], mntflags, puffs_getroot(pu)) == -1)
281 		err(1, "puffs_mount");
282 	if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1)
283 		err(1, "setblockingmode");
284 
285 	if (puffs_mainloop(pu) == -1)
286 		err(1, "mainloop");
287 	puffs_exit(pu, 1);
288 
289 	return 0;
290 }
291 
292 #define RETRY_MAX 100
293 
294 void
295 psshfs_notify(struct puffs_usermount *pu, int fd, int what)
296 {
297 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
298 	int nretry, which, newfd, dummy;
299 
300 	if (fd == pctx->sshfd) {
301 		which = PSSHFD_META;
302 	} else {
303 		assert(fd == pctx->sshfd_data);
304 		which = PSSHFD_DATA;
305 	}
306 
307 	if (puffs_getstate(pu) != PUFFS_STATE_RUNNING)
308 		return;
309 
310 	if (what != (PUFFS_FBIO_READ | PUFFS_FBIO_WRITE)) {
311 		puffs_framev_removefd(pu, fd, ECONNRESET);
312 		return;
313 	}
314 	close(fd);
315 
316 	/* deal with zmobies, beware of half-eaten brain */
317 	while (waitpid(-1, &dummy, WNOHANG) > 0)
318 		continue;
319 
320 	for (nretry = 0;;nretry++) {
321 		if ((newfd = pssh_connect(pu, which)) == -1)
322 			goto retry2;
323 
324 		if (puffs_framev_addfd(pu, newfd,
325 		    PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1)
326 			goto retry1;
327 
328 		break;
329  retry1:
330 		fprintf(stderr, "reconnect failed... ");
331 		close(newfd);
332  retry2:
333 		if (nretry < RETRY_MAX) {
334 			fprintf(stderr, "retry (%d left)\n", RETRY_MAX-nretry);
335 			sleep(nretry);
336 		} else {
337 			fprintf(stderr, "retry count exceeded, going south\n");
338 			exit(1); /* XXXXXXX */
339 		}
340 	}
341 }
342 
343 static int
344 pssh_connect(struct puffs_usermount *pu, int which)
345 {
346 	struct psshfs_ctx *pctx = puffs_getspecific(pu);
347 	char **sshargs = pctx->sshargs;
348 	int fds[2];
349 	pid_t pid;
350 	int dnfd, x;
351 	int *sshfd;
352 	pid_t *sshpid;
353 
354 	if (which == PSSHFD_META) {
355 		sshfd = &pctx->sshfd;
356 		sshpid = &pctx->sshpid;
357 	} else {
358 		assert(which == PSSHFD_DATA);
359 		sshfd = &pctx->sshfd_data;
360 		sshpid = &pctx->sshpid_data;
361 	}
362 
363 	if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == -1)
364 		return -1;
365 
366 	pid = fork();
367 	switch (pid) {
368 	case -1:
369 		return -1;
370 		/*NOTREACHED*/
371 	case 0: /* child */
372 		if (dup2(fds[0], STDIN_FILENO) == -1)
373 			err(1, "child dup2");
374 		if (dup2(fds[0], STDOUT_FILENO) == -1)
375 			err(1, "child dup2");
376 		close(fds[0]);
377 		close(fds[1]);
378 
379 		dnfd = open(_PATH_DEVNULL, O_RDWR);
380 		if (dnfd != -1)
381 			dup2(dnfd, STDERR_FILENO);
382 
383 		execvp(sshargs[0], sshargs);
384 		/*NOTREACHED*/
385 		break;
386 	default:
387 		*sshpid = pid;
388 		*sshfd = fds[1];
389 		close(fds[0]);
390 		break;
391 	}
392 
393 	if (psshfs_handshake(pu, *sshfd) != 0)
394 		errx(1, "psshfs_handshake %d", which);
395 	x = 1;
396 	if (ioctl(*sshfd, FIONBIO, &x) == -1)
397 		err(1, "nonblocking descriptor %d", which);
398 
399 	return *sshfd;
400 }
401 
402 static void *
403 invalone(struct puffs_usermount *pu, struct puffs_node *pn, void *arg)
404 {
405 	struct psshfs_node *psn = pn->pn_data;
406 
407 	psn->attrread = 0;
408 	psn->dentread = 0;
409 	psn->slread = 0;
410 
411 	return NULL;
412 }
413 
414 static void
415 psshfs_loopfn(struct puffs_usermount *pu)
416 {
417 
418 	if (sighup) {
419 		puffs_pn_nodewalk(pu, invalone, NULL);
420 		sighup = 0;
421 	}
422 }
423