psshfs.c revision 1.26 1 /* $NetBSD: psshfs.c,v 1.26 2007/05/15 13:46:48 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 * 3. The name of the company nor the name of the author may be used to
15 * endorse or promote products derived from this software without specific
16 * prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
19 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 /*
32 * psshfs: puffs sshfs
33 *
34 * psshfs implements sshfs functionality on top of puffs making it
35 * possible to mount a filesystme through the sftp service.
36 *
37 * psshfs can execute multiple operations in "parallel" by using the
38 * puffs_cc framework for continuations.
39 *
40 * Concurrency control is handled currently by vnode locking (this
41 * will change in the future). Context switch locations are easy to
42 * find by grepping for puffs_framebuf_enqueue_cc().
43 */
44
45 #include <sys/cdefs.h>
46 #ifndef lint
47 __RCSID("$NetBSD: psshfs.c,v 1.26 2007/05/15 13:46:48 pooka Exp $");
48 #endif /* !lint */
49
50 #include <sys/types.h>
51
52 #include <assert.h>
53 #include <err.h>
54 #include <errno.h>
55 #include <mntopts.h>
56 #include <poll.h>
57 #include <puffs.h>
58 #include <signal.h>
59 #include <stdlib.h>
60 #include <util.h>
61 #include <unistd.h>
62
63 #include "psshfs.h"
64
65 static void pssh_connect(struct psshfs_ctx *, char **);
66 static void usage(void);
67 static void add_ssharg(char ***, int *, char *);
68
69 #define SSH_PATH "/usr/bin/ssh"
70
71 static void
72 add_ssharg(char ***sshargs, int *nargs, char *arg)
73 {
74
75 *sshargs = realloc(*sshargs, (*nargs + 2) * sizeof(char*));
76 if (!*sshargs)
77 err(1, "realloc");
78 (*sshargs)[(*nargs)++] = arg;
79 (*sshargs)[*nargs] = NULL;
80 }
81
82 static void
83 usage()
84 {
85
86 fprintf(stderr, "usage: %s "
87 "[-es] [-O sshopt=value] [-o opts] user@host:path mountpath\n",
88 getprogname());
89 exit(1);
90 }
91
92 int
93 main(int argc, char *argv[])
94 {
95 struct psshfs_ctx pctx;
96 struct puffs_usermount *pu;
97 struct puffs_ops *pops;
98 mntoptparse_t mp;
99 char **sshargs;
100 char *userhost;
101 char *hostpath;
102 int mntflags, pflags, lflags, ch;
103 int exportfs;
104 int nargs, x;
105
106 setprogname(argv[0]);
107
108 if (argc < 3)
109 usage();
110
111 mntflags = pflags = lflags = exportfs = nargs = 0;
112 sshargs = NULL;
113 add_ssharg(&sshargs, &nargs, SSH_PATH);
114 add_ssharg(&sshargs, &nargs, "-axs");
115 add_ssharg(&sshargs, &nargs, "-oClearAllForwardings=yes");
116
117 while ((ch = getopt(argc, argv, "eo:O:s")) != -1) {
118 switch (ch) {
119 case 'e':
120 exportfs = 1;
121 break;
122 case 'O':
123 add_ssharg(&sshargs, &nargs, "-o");
124 add_ssharg(&sshargs, &nargs, optarg);
125 break;
126 case 'o':
127 mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
128 if (mp == NULL)
129 err(1, "getmntopts");
130 freemntopts(mp);
131 break;
132 case 's':
133 lflags |= PUFFSLOOP_NODAEMON;
134 break;
135 default:
136 usage();
137 /*NOTREACHED*/
138 }
139 }
140 argc -= optind;
141 argv += optind;
142
143 #if 0
144 /* XXX: noatime is mandatory for now */
145 mntflags |= MNT_NOATIME;
146 #endif
147
148 if (pflags & PUFFS_FLAG_OPDUMP)
149 lflags |= PUFFSLOOP_NODAEMON;
150 pflags |= PUFFS_FLAG_BUILDPATH;
151 pflags |= PUFFS_KFLAG_WTCACHE | PUFFS_KFLAG_IAONDEMAND;
152
153 if (argc != 2)
154 usage();
155
156 PUFFSOP_INIT(pops);
157
158 PUFFSOP_SET(pops, psshfs, fs, unmount);
159 PUFFSOP_SETFSNOP(pops, sync); /* XXX */
160 PUFFSOP_SETFSNOP(pops, statvfs);
161 PUFFSOP_SET(pops, psshfs, fs, nodetofh);
162 PUFFSOP_SET(pops, psshfs, fs, fhtonode);
163
164 PUFFSOP_SET(pops, psshfs, node, lookup);
165 PUFFSOP_SET(pops, psshfs, node, create);
166 PUFFSOP_SET(pops, psshfs, node, open);
167 PUFFSOP_SET(pops, psshfs, node, inactive);
168 PUFFSOP_SET(pops, psshfs, node, readdir);
169 PUFFSOP_SET(pops, psshfs, node, getattr);
170 PUFFSOP_SET(pops, psshfs, node, setattr);
171 PUFFSOP_SET(pops, psshfs, node, mkdir);
172 PUFFSOP_SET(pops, psshfs, node, remove);
173 PUFFSOP_SET(pops, psshfs, node, readlink);
174 PUFFSOP_SET(pops, psshfs, node, rmdir);
175 PUFFSOP_SET(pops, psshfs, node, symlink);
176 PUFFSOP_SET(pops, psshfs, node, rename);
177 PUFFSOP_SET(pops, psshfs, node, read);
178 PUFFSOP_SET(pops, psshfs, node, write);
179 PUFFSOP_SET(pops, psshfs, node, reclaim);
180
181 memset(&pctx, 0, sizeof(pctx));
182 pctx.mounttime = time(NULL);
183
184 userhost = argv[0];
185 hostpath = strchr(userhost, ':');
186 if (hostpath) {
187 *hostpath++ = '\0';
188 pctx.mountpath = hostpath;
189 } else
190 pctx.mountpath = ".";
191
192 add_ssharg(&sshargs, &nargs, argv[0]);
193 add_ssharg(&sshargs, &nargs, "sftp");
194
195 pu = puffs_init(pops, "ppshfs", &pctx, pflags);
196 if (pu == NULL)
197 err(1, "puffs_init");
198
199 pssh_connect(&pctx, sshargs);
200
201 if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1)
202 err(1, "setblockingmode");
203 if (exportfs)
204 puffs_setfhsize(pu, sizeof(struct psshfs_fid),
205 PUFFS_FHFLAG_NFSV2 | PUFFS_FHFLAG_NFSV3);
206
207 if (puffs_domount(pu, argv[1], mntflags) == -1)
208 err(1, "puffs_domount");
209
210 if (psshfs_domount(pu) != 0)
211 errx(1, "psshfs_domount");
212
213 x = 1;
214 if (ioctl(pctx.sshfd, FIONBIO, &x) == -1)
215 err(1, "nonblocking descriptor");
216
217 puffs_framev_init(pu, psbuf_read, psbuf_write, psbuf_cmp,
218 puffs_framev_unmountonclose);
219 if (puffs_framev_addfd(pu, pctx.sshfd) == -1)
220 err(1, "framebuf addfd");
221
222 return puffs_mainloop(pu, lflags);
223 }
224
225 static void
226 pssh_connect(struct psshfs_ctx *pctx, char **sshargs)
227 {
228 int fds[2];
229 pid_t pid;
230
231 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == -1)
232 err(1, "socketpair");
233
234 pid = fork();
235 switch (pid) {
236 case -1:
237 err(1, "fork");
238 /*NOTREACHED*/
239 case 0: /* child */
240 if (dup2(fds[0], STDIN_FILENO) == -1)
241 err(1, "child dup2");
242 if (dup2(fds[0], STDOUT_FILENO) == -1)
243 err(1, "child dup2");
244 close(fds[0]);
245 close(fds[1]);
246 execvp(sshargs[0], sshargs);
247 break;
248 default:
249 pctx->sshpid = pid;
250 pctx->sshfd = fds[1];
251 close(fds[0]);
252 break;
253 }
254 }
255