xref: /netbsd-src/usr.sbin/puffs/mount_psshfs/psshfs.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: psshfs.c,v 1.47 2007/12/14 10:56:22 jmmv 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.47 2007/12/14 10:56:22 jmmv 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] [-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 	mntoptparse_t mp;
109 	char **sshargs;
110 	char *userhost;
111 	char *hostpath;
112 	int mntflags, pflags, ch;
113 	int detach;
114 	int exportfs, refreshival;
115 	int nargs, x;
116 
117 	setprogname(argv[0]);
118 
119 	if (argc < 3)
120 		usage();
121 
122 	mntflags = pflags = exportfs = nargs = 0;
123 	detach = 1;
124 	refreshival = DEFAULTREFRESH;
125 	sshargs = NULL;
126 	add_ssharg(&sshargs, &nargs, SSH_PATH);
127 	add_ssharg(&sshargs, &nargs, "-axs");
128 	add_ssharg(&sshargs, &nargs, "-oClearAllForwardings=yes");
129 
130 	while ((ch = getopt(argc, argv, "eF:o:O:r:st:")) != -1) {
131 		switch (ch) {
132 		case 'e':
133 			exportfs = 1;
134 			break;
135 		case 'F':
136 			add_ssharg(&sshargs, &nargs, "-F");
137 			add_ssharg(&sshargs, &nargs, optarg);
138 			break;
139 		case 'O':
140 			add_ssharg(&sshargs, &nargs, "-o");
141 			add_ssharg(&sshargs, &nargs, optarg);
142 			break;
143 		case 'o':
144 			mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
145 			if (mp == NULL)
146 				err(1, "getmntopts");
147 			freemntopts(mp);
148 			break;
149 		case 'r':
150 			max_reads = atoi(optarg);
151 			break;
152 		case 's':
153 			detach = 0;
154 			break;
155 		case 't':
156 			refreshival = atoi(optarg);
157 			if (refreshival < 0 && refreshival != -1)
158 				errx(1, "invalid timeout %d", refreshival);
159 			break;
160 		default:
161 			usage();
162 			/*NOTREACHED*/
163 		}
164 	}
165 	argc -= optind;
166 	argv += optind;
167 
168 	if (pflags & PUFFS_FLAG_OPDUMP)
169 		detach = 0;
170 	pflags |= PUFFS_FLAG_BUILDPATH;
171 	pflags |= PUFFS_KFLAG_WTCACHE | PUFFS_KFLAG_IAONDEMAND;
172 
173 	if (argc != 2)
174 		usage();
175 
176 	PUFFSOP_INIT(pops);
177 
178 	PUFFSOP_SET(pops, psshfs, fs, unmount);
179 	PUFFSOP_SETFSNOP(pops, sync); /* XXX */
180 	PUFFSOP_SETFSNOP(pops, statvfs);
181 	PUFFSOP_SET(pops, psshfs, fs, nodetofh);
182 	PUFFSOP_SET(pops, psshfs, fs, fhtonode);
183 
184 	PUFFSOP_SET(pops, psshfs, node, lookup);
185 	PUFFSOP_SET(pops, psshfs, node, create);
186 	PUFFSOP_SET(pops, psshfs, node, open);
187 	PUFFSOP_SET(pops, psshfs, node, inactive);
188 	PUFFSOP_SET(pops, psshfs, node, readdir);
189 	PUFFSOP_SET(pops, psshfs, node, getattr);
190 	PUFFSOP_SET(pops, psshfs, node, setattr);
191 	PUFFSOP_SET(pops, psshfs, node, mkdir);
192 	PUFFSOP_SET(pops, psshfs, node, remove);
193 	PUFFSOP_SET(pops, psshfs, node, readlink);
194 	PUFFSOP_SET(pops, psshfs, node, rmdir);
195 	PUFFSOP_SET(pops, psshfs, node, symlink);
196 	PUFFSOP_SET(pops, psshfs, node, rename);
197 	PUFFSOP_SET(pops, psshfs, node, read);
198 	PUFFSOP_SET(pops, psshfs, node, write);
199 	PUFFSOP_SET(pops, psshfs, node, reclaim);
200 
201 	pu = puffs_init(pops, argv[0], "psshfs", &pctx, pflags);
202 	if (pu == NULL)
203 		err(1, "puffs_init");
204 
205 	memset(&pctx, 0, sizeof(pctx));
206 	pctx.mounttime = time(NULL);
207 	pctx.refreshival = refreshival;
208 
209 	userhost = argv[0];
210 	hostpath = strchr(userhost, ':');
211 	if (hostpath) {
212 		*hostpath++ = '\0';
213 		pctx.mountpath = hostpath;
214 	} else
215 		pctx.mountpath = ".";
216 
217 	add_ssharg(&sshargs, &nargs, argv[0]);
218 	add_ssharg(&sshargs, &nargs, "sftp");
219 
220 	signal(SIGHUP, takehup);
221 	puffs_ml_setloopfn(pu, psshfs_loopfn);
222 
223 	pssh_connect(&pctx, sshargs);
224 
225 	if (exportfs)
226 		puffs_setfhsize(pu, sizeof(struct psshfs_fid),
227 		    PUFFS_FHFLAG_NFSV2 | PUFFS_FHFLAG_NFSV3);
228 
229 	if (psshfs_domount(pu) != 0)
230 		errx(1, "psshfs_domount");
231 	x = 1;
232 	if (ioctl(pctx.sshfd, FIONBIO, &x) == -1)
233 		err(1, "nonblocking descriptor");
234 
235 	puffs_framev_init(pu, psbuf_read, psbuf_write, psbuf_cmp, NULL,
236 	    puffs_framev_unmountonclose);
237 	if (puffs_framev_addfd(pu, pctx.sshfd,
238 	    PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1)
239 		err(1, "framebuf addfd");
240 
241 	if (detach)
242 		if (puffs_daemon(pu, 1, 1) == -1)
243 			err(1, "puffs_daemon");
244 
245 	if (puffs_mount(pu, argv[1], mntflags, puffs_getroot(pu)) == -1)
246 		err(1, "puffs_mount");
247 	if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1)
248 		err(1, "setblockingmode");
249 
250 	if (puffs_mainloop(pu) == -1)
251 		err(1, "mainloop");
252 
253 	return 0;
254 }
255 
256 static void
257 pssh_connect(struct psshfs_ctx *pctx, char **sshargs)
258 {
259 	int fds[2];
260 	pid_t pid;
261 	int dnfd;
262 
263 	if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == -1)
264 		err(1, "socketpair");
265 
266 	pid = fork();
267 	switch (pid) {
268 	case -1:
269 		err(1, "fork");
270 		/*NOTREACHED*/
271 	case 0: /* child */
272 		if (dup2(fds[0], STDIN_FILENO) == -1)
273 			err(1, "child dup2");
274 		if (dup2(fds[0], STDOUT_FILENO) == -1)
275 			err(1, "child dup2");
276 		close(fds[0]);
277 		close(fds[1]);
278 
279 		dnfd = open(_PATH_DEVNULL, O_RDWR);
280 		if (dnfd != -1)
281 			dup2(dnfd, STDERR_FILENO);
282 
283 		execvp(sshargs[0], sshargs);
284 		break;
285 	default:
286 		pctx->sshpid = pid;
287 		pctx->sshfd = fds[1];
288 		close(fds[0]);
289 		break;
290 	}
291 }
292 
293 static void *
294 invalone(struct puffs_usermount *pu, struct puffs_node *pn, void *arg)
295 {
296 	struct psshfs_node *psn = pn->pn_data;
297 
298 	psn->attrread = 0;
299 	psn->dentread = 0;
300 	psn->slread = 0;
301 
302 	return NULL;
303 }
304 
305 static void
306 psshfs_loopfn(struct puffs_usermount *pu)
307 {
308 
309 	if (sighup) {
310 		puffs_pn_nodewalk(pu, invalone, NULL);
311 		sighup = 0;
312 	}
313 }
314