psshfs.c revision 1.34 1 /* $NetBSD: psshfs.c,v 1.34 2007/07/27 09:46:27 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.34 2007/07/27 09:46:27 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 usage(void);
65 static void add_ssharg(char ***, int *, char *);
66
67 #define SSH_PATH "/usr/bin/ssh"
68
69 static void
70 add_ssharg(char ***sshargs, int *nargs, char *arg)
71 {
72
73 *sshargs = realloc(*sshargs, (*nargs + 2) * sizeof(char*));
74 if (!*sshargs)
75 err(1, "realloc");
76 (*sshargs)[(*nargs)++] = arg;
77 (*sshargs)[*nargs] = NULL;
78 }
79
80 static void
81 usage()
82 {
83
84 fprintf(stderr, "usage: %s "
85 "[-es] [-O sshopt=value] [-o opts] user@host:path mountpath\n",
86 getprogname());
87 exit(1);
88 }
89
90 int
91 main(int argc, char *argv[])
92 {
93 struct psshfs_ctx pctx;
94 struct puffs_usermount *pu;
95 struct puffs_ops *pops;
96 mntoptparse_t mp;
97 char **sshargs;
98 char *userhost;
99 char *hostpath;
100 int mntflags, pflags, lflags, ch;
101 int exportfs;
102 int nargs, x;
103
104 setprogname(argv[0]);
105
106 if (argc < 3)
107 usage();
108
109 mntflags = pflags = lflags = exportfs = nargs = 0;
110 sshargs = NULL;
111 add_ssharg(&sshargs, &nargs, SSH_PATH);
112 add_ssharg(&sshargs, &nargs, "-axs");
113 add_ssharg(&sshargs, &nargs, "-oClearAllForwardings=yes");
114
115 while ((ch = getopt(argc, argv, "eo:O:s")) != -1) {
116 switch (ch) {
117 case 'e':
118 exportfs = 1;
119 break;
120 case 'O':
121 add_ssharg(&sshargs, &nargs, "-o");
122 add_ssharg(&sshargs, &nargs, optarg);
123 break;
124 case 'o':
125 mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
126 if (mp == NULL)
127 err(1, "getmntopts");
128 freemntopts(mp);
129 break;
130 case 's':
131 lflags |= PUFFSLOOP_NODAEMON;
132 break;
133 default:
134 usage();
135 /*NOTREACHED*/
136 }
137 }
138 argc -= optind;
139 argv += optind;
140
141 if (pflags & PUFFS_FLAG_OPDUMP)
142 lflags |= PUFFSLOOP_NODAEMON;
143 pflags |= PUFFS_FLAG_BUILDPATH;
144 pflags |= PUFFS_KFLAG_WTCACHE | PUFFS_KFLAG_IAONDEMAND;
145
146 if (argc != 2)
147 usage();
148
149 PUFFSOP_INIT(pops);
150
151 PUFFSOP_SET(pops, psshfs, fs, unmount);
152 PUFFSOP_SETFSNOP(pops, sync); /* XXX */
153 PUFFSOP_SETFSNOP(pops, statvfs);
154 PUFFSOP_SET(pops, psshfs, fs, nodetofh);
155 PUFFSOP_SET(pops, psshfs, fs, fhtonode);
156
157 PUFFSOP_SET(pops, psshfs, node, lookup);
158 PUFFSOP_SET(pops, psshfs, node, create);
159 PUFFSOP_SET(pops, psshfs, node, open);
160 PUFFSOP_SET(pops, psshfs, node, close);
161 PUFFSOP_SET(pops, psshfs, node, mmap);
162 PUFFSOP_SET(pops, psshfs, node, inactive);
163 PUFFSOP_SET(pops, psshfs, node, readdir);
164 PUFFSOP_SET(pops, psshfs, node, getattr);
165 PUFFSOP_SET(pops, psshfs, node, setattr);
166 PUFFSOP_SET(pops, psshfs, node, mkdir);
167 PUFFSOP_SET(pops, psshfs, node, remove);
168 PUFFSOP_SET(pops, psshfs, node, readlink);
169 PUFFSOP_SET(pops, psshfs, node, rmdir);
170 PUFFSOP_SET(pops, psshfs, node, symlink);
171 PUFFSOP_SET(pops, psshfs, node, rename);
172 PUFFSOP_SET(pops, psshfs, node, read);
173 PUFFSOP_SET(pops, psshfs, node, write);
174 PUFFSOP_SET(pops, psshfs, node, reclaim);
175
176 pu = puffs_init(pops, argv[0], "psshfs", &pctx, pflags);
177 if (pu == NULL)
178 err(1, "puffs_init");
179
180 memset(&pctx, 0, sizeof(pctx));
181 pctx.mounttime = time(NULL);
182
183 userhost = argv[0];
184 hostpath = strchr(userhost, ':');
185 if (hostpath) {
186 *hostpath++ = '\0';
187 pctx.mountpath = hostpath;
188 } else
189 pctx.mountpath = ".";
190
191 add_ssharg(&sshargs, &nargs, argv[0]);
192 add_ssharg(&sshargs, &nargs, "sftp");
193
194 pssh_connect(&pctx, sshargs);
195
196 if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1)
197 err(1, "setblockingmode");
198 if (exportfs)
199 puffs_setfhsize(pu, sizeof(struct psshfs_fid),
200 PUFFS_FHFLAG_NFSV2 | PUFFS_FHFLAG_NFSV3);
201
202 if (psshfs_domount(pu) != 0)
203 errx(1, "psshfs_domount");
204 x = 1;
205 if (ioctl(pctx.sshfd, FIONBIO, &x) == -1)
206 err(1, "nonblocking descriptor");
207
208 puffs_framev_init(pu, psbuf_read, psbuf_write, psbuf_cmp,
209 puffs_framev_unmountonclose);
210 if (puffs_framev_addfd(pu, pctx.sshfd,
211 PUFFS_FBIO_READ | PUFFS_FBIO_WRITE) == -1)
212 err(1, "framebuf addfd");
213
214 if (puffs_mount(pu, argv[1], mntflags, puffs_getroot(pu)) == -1)
215 err(1, "puffs_mount");
216
217 return puffs_mainloop(pu, lflags);
218 }
219
220 static void
221 pssh_connect(struct psshfs_ctx *pctx, char **sshargs)
222 {
223 int fds[2];
224 pid_t pid;
225 int dnfd;
226
227 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == -1)
228 err(1, "socketpair");
229
230 pid = fork();
231 switch (pid) {
232 case -1:
233 err(1, "fork");
234 /*NOTREACHED*/
235 case 0: /* child */
236 if (dup2(fds[0], STDIN_FILENO) == -1)
237 err(1, "child dup2");
238 if (dup2(fds[0], STDOUT_FILENO) == -1)
239 err(1, "child dup2");
240 close(fds[0]);
241 close(fds[1]);
242
243 dnfd = open(_PATH_DEVNULL, O_RDWR);
244 if (dnfd != -1)
245 dup2(dnfd, STDERR_FILENO);
246
247 execvp(sshargs[0], sshargs);
248 break;
249 default:
250 pctx->sshpid = pid;
251 pctx->sshfd = fds[1];
252 close(fds[0]);
253 break;
254 }
255 }
256