xref: /netbsd-src/usr.sbin/puffs/mount_psshfs/psshfs.c (revision 7fa608457b817eca6e0977b37f758ae064f3c99c)
1 /*	$NetBSD: psshfs.c,v 1.43 2007/11/11 18:06:35 pooka Exp $	*/
2 
3 /*
4  * Copyright (c) 2006  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.43 2007/11/11 18:06:35 pooka Exp $");
45 #endif /* !lint */
46 
47 #include <sys/types.h>
48 
49 #include <assert.h>
50 #include <err.h>
51 #include <errno.h>
52 #include <mntopts.h>
53 #include <paths.h>
54 #include <poll.h>
55 #include <puffs.h>
56 #include <signal.h>
57 #include <stdlib.h>
58 #include <util.h>
59 #include <unistd.h>
60 
61 #include "psshfs.h"
62 
63 static void	pssh_connect(struct psshfs_ctx *, char **);
64 static void	psshfs_loopfn(struct puffs_usermount *);
65 static void	usage(void);
66 static void	add_ssharg(char ***, int *, char *);
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, char *arg)
75 {
76 
77 	*sshargs = realloc(*sshargs, (*nargs + 2) * sizeof(char*));
78 	if (!*sshargs)
79 		err(1, "realloc");
80 	(*sshargs)[(*nargs)++] = arg;
81 	(*sshargs)[*nargs] = NULL;
82 }
83 
84 static void
85 usage()
86 {
87 
88 	fprintf(stderr, "usage: %s "
89 	    "[-es] [-O sshopt=value] [-o opts] user@host:path mountpath\n",
90 	    getprogname());
91 	exit(1);
92 }
93 
94 static void
95 takehup(int sig)
96 {
97 
98 	sighup = 1;
99 }
100 
101 int
102 main(int argc, char *argv[])
103 {
104 	struct psshfs_ctx pctx;
105 	struct puffs_usermount *pu;
106 	struct puffs_ops *pops;
107 	mntoptparse_t mp;
108 	char **sshargs;
109 	char *userhost;
110 	char *hostpath;
111 	int mntflags, pflags, ch;
112 	int detach;
113 	int exportfs, refreshival;
114 	int nargs, x;
115 
116 	setprogname(argv[0]);
117 
118 	if (argc < 3)
119 		usage();
120 
121 	mntflags = pflags = exportfs = nargs = 0;
122 	detach = 1;
123 	refreshival = DEFAULTREFRESH;
124 	sshargs = NULL;
125 	add_ssharg(&sshargs, &nargs, SSH_PATH);
126 	add_ssharg(&sshargs, &nargs, "-axs");
127 	add_ssharg(&sshargs, &nargs, "-oClearAllForwardings=yes");
128 
129 	while ((ch = getopt(argc, argv, "eo:O:r:st:")) != -1) {
130 		switch (ch) {
131 		case 'e':
132 			exportfs = 1;
133 			break;
134 		case 'O':
135 			add_ssharg(&sshargs, &nargs, "-o");
136 			add_ssharg(&sshargs, &nargs, optarg);
137 			break;
138 		case 'o':
139 			mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
140 			if (mp == NULL)
141 				err(1, "getmntopts");
142 			freemntopts(mp);
143 			break;
144 		case 'r':
145 			max_reads = atoi(optarg);
146 			break;
147 		case 's':
148 			detach = 0;
149 			break;
150 		case 't':
151 			refreshival = atoi(optarg);
152 			if (refreshival < 0 && refreshival != -1)
153 				errx(1, "invalid timeout %d", refreshival);
154 			break;
155 		default:
156 			usage();
157 			/*NOTREACHED*/
158 		}
159 	}
160 	argc -= optind;
161 	argv += optind;
162 
163 	if (pflags & PUFFS_FLAG_OPDUMP)
164 		detach = 0;
165 	pflags |= PUFFS_FLAG_BUILDPATH;
166 	pflags |= PUFFS_KFLAG_WTCACHE | PUFFS_KFLAG_IAONDEMAND;
167 
168 	if (argc != 2)
169 		usage();
170 
171 	PUFFSOP_INIT(pops);
172 
173 	PUFFSOP_SET(pops, psshfs, fs, unmount);
174 	PUFFSOP_SETFSNOP(pops, sync); /* XXX */
175 	PUFFSOP_SETFSNOP(pops, statvfs);
176 	PUFFSOP_SET(pops, psshfs, fs, nodetofh);
177 	PUFFSOP_SET(pops, psshfs, fs, fhtonode);
178 
179 	PUFFSOP_SET(pops, psshfs, node, lookup);
180 	PUFFSOP_SET(pops, psshfs, node, create);
181 	PUFFSOP_SET(pops, psshfs, node, open);
182 	PUFFSOP_SET(pops, psshfs, node, close);
183 	PUFFSOP_SET(pops, psshfs, node, mmap);
184 	PUFFSOP_SET(pops, psshfs, node, inactive);
185 	PUFFSOP_SET(pops, psshfs, node, readdir);
186 	PUFFSOP_SET(pops, psshfs, node, getattr);
187 	PUFFSOP_SET(pops, psshfs, node, setattr);
188 	PUFFSOP_SET(pops, psshfs, node, mkdir);
189 	PUFFSOP_SET(pops, psshfs, node, remove);
190 	PUFFSOP_SET(pops, psshfs, node, readlink);
191 	PUFFSOP_SET(pops, psshfs, node, rmdir);
192 	PUFFSOP_SET(pops, psshfs, node, symlink);
193 	PUFFSOP_SET(pops, psshfs, node, rename);
194 	PUFFSOP_SET(pops, psshfs, node, read);
195 	PUFFSOP_SET(pops, psshfs, node, write);
196 	PUFFSOP_SET(pops, psshfs, node, reclaim);
197 
198 	pu = puffs_init(pops, argv[0], "psshfs", &pctx, pflags);
199 	if (pu == NULL)
200 		err(1, "puffs_init");
201 
202 	memset(&pctx, 0, sizeof(pctx));
203 	pctx.mounttime = time(NULL);
204 	pctx.refreshival = refreshival;
205 
206 	userhost = argv[0];
207 	hostpath = strchr(userhost, ':');
208 	if (hostpath) {
209 		*hostpath++ = '\0';
210 		pctx.mountpath = hostpath;
211 	} else
212 		pctx.mountpath = ".";
213 
214 	add_ssharg(&sshargs, &nargs, argv[0]);
215 	add_ssharg(&sshargs, &nargs, "sftp");
216 
217 	signal(SIGHUP, takehup);
218 	puffs_ml_setloopfn(pu, psshfs_loopfn);
219 
220 	pssh_connect(&pctx, sshargs);
221 
222 	if (exportfs)
223 		puffs_setfhsize(pu, sizeof(struct psshfs_fid),
224 		    PUFFS_FHFLAG_NFSV2 | PUFFS_FHFLAG_NFSV3);
225 
226 	if (psshfs_domount(pu) != 0)
227 		errx(1, "psshfs_domount");
228 	x = 1;
229 	if (ioctl(pctx.sshfd, FIONBIO, &x) == -1)
230 		err(1, "nonblocking descriptor");
231 
232 	puffs_framev_init(pu, psbuf_read, psbuf_write, psbuf_cmp, NULL,
233 	    puffs_framev_unmountonclose);
234 	if (puffs_framev_addfd(pu, pctx.sshfd,
235 	    PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1)
236 		err(1, "framebuf addfd");
237 
238 	if (detach)
239 		if (daemon(1, 1) == -1)
240 			err(1, "daemon");
241 
242 	if (puffs_mount(pu, argv[1], mntflags, puffs_getroot(pu)) == -1)
243 		err(1, "puffs_mount");
244 	if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1)
245 		err(1, "setblockingmode");
246 
247 	if (puffs_mainloop(pu) == -1)
248 		err(1, "mainloop");
249 
250 	return 0;
251 }
252 
253 static void
254 pssh_connect(struct psshfs_ctx *pctx, char **sshargs)
255 {
256 	int fds[2];
257 	pid_t pid;
258 	int dnfd;
259 
260 	if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == -1)
261 		err(1, "socketpair");
262 
263 	pid = fork();
264 	switch (pid) {
265 	case -1:
266 		err(1, "fork");
267 		/*NOTREACHED*/
268 	case 0: /* child */
269 		if (dup2(fds[0], STDIN_FILENO) == -1)
270 			err(1, "child dup2");
271 		if (dup2(fds[0], STDOUT_FILENO) == -1)
272 			err(1, "child dup2");
273 		close(fds[0]);
274 		close(fds[1]);
275 
276 		dnfd = open(_PATH_DEVNULL, O_RDWR);
277 		if (dnfd != -1)
278 			dup2(dnfd, STDERR_FILENO);
279 
280 		execvp(sshargs[0], sshargs);
281 		break;
282 	default:
283 		pctx->sshpid = pid;
284 		pctx->sshfd = fds[1];
285 		close(fds[0]);
286 		break;
287 	}
288 }
289 
290 static void *
291 invalone(struct puffs_usermount *pu, struct puffs_node *pn, void *arg)
292 {
293 	struct psshfs_node *psn = pn->pn_data;
294 
295 	psn->attrread = 0;
296 	psn->dentread = 0;
297 	psn->slread = 0;
298 
299 	return NULL;
300 }
301 
302 static void
303 psshfs_loopfn(struct puffs_usermount *pu)
304 {
305 
306 	if (sighup) {
307 		puffs_pn_nodewalk(pu, invalone, NULL);
308 		sighup = 0;
309 	}
310 }
311