psshfs.c revision 1.24 1 /* $NetBSD: psshfs.c,v 1.24 2007/05/11 16:23:01 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.24 2007/05/11 16:23:01 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, ch;
103 int detach, exportfs;
104 int nargs, x;
105
106 setprogname(argv[0]);
107
108 if (argc < 3)
109 usage();
110
111 mntflags = pflags = exportfs = nargs = 0;
112 detach = 1;
113 sshargs = NULL;
114 add_ssharg(&sshargs, &nargs, SSH_PATH);
115 add_ssharg(&sshargs, &nargs, "-axs");
116 add_ssharg(&sshargs, &nargs, "-oClearAllForwardings=yes");
117
118 while ((ch = getopt(argc, argv, "eo:O:s")) != -1) {
119 switch (ch) {
120 case 'e':
121 exportfs = 1;
122 break;
123 case 'O':
124 add_ssharg(&sshargs, &nargs, "-o");
125 add_ssharg(&sshargs, &nargs, optarg);
126 break;
127 case 'o':
128 mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
129 if (mp == NULL)
130 err(1, "getmntopts");
131 freemntopts(mp);
132 break;
133 case 's':
134 detach = 0;
135 break;
136 default:
137 usage();
138 /*NOTREACHED*/
139 }
140 }
141 argc -= optind;
142 argv += optind;
143
144 #if 0
145 /* XXX: noatime is mandatory for now */
146 mntflags |= MNT_NOATIME;
147 #endif
148
149 if (pflags & PUFFS_FLAG_OPDUMP)
150 detach = 0;
151 pflags |= PUFFS_FLAG_BUILDPATH;
152 pflags |= PUFFS_KFLAG_WTCACHE | PUFFS_KFLAG_IAONDEMAND;
153
154 if (argc != 2)
155 usage();
156
157 PUFFSOP_INIT(pops);
158
159 PUFFSOP_SET(pops, psshfs, fs, unmount);
160 PUFFSOP_SETFSNOP(pops, sync); /* XXX */
161 PUFFSOP_SETFSNOP(pops, statvfs);
162 PUFFSOP_SET(pops, psshfs, fs, nodetofh);
163 PUFFSOP_SET(pops, psshfs, fs, fhtonode);
164
165 PUFFSOP_SET(pops, psshfs, node, lookup);
166 PUFFSOP_SET(pops, psshfs, node, create);
167 PUFFSOP_SET(pops, psshfs, node, open);
168 PUFFSOP_SET(pops, psshfs, node, inactive);
169 PUFFSOP_SET(pops, psshfs, node, readdir);
170 PUFFSOP_SET(pops, psshfs, node, getattr);
171 PUFFSOP_SET(pops, psshfs, node, setattr);
172 PUFFSOP_SET(pops, psshfs, node, mkdir);
173 PUFFSOP_SET(pops, psshfs, node, remove);
174 PUFFSOP_SET(pops, psshfs, node, readlink);
175 PUFFSOP_SET(pops, psshfs, node, rmdir);
176 PUFFSOP_SET(pops, psshfs, node, symlink);
177 PUFFSOP_SET(pops, psshfs, node, rename);
178 PUFFSOP_SET(pops, psshfs, node, read);
179 PUFFSOP_SET(pops, psshfs, node, write);
180 PUFFSOP_SET(pops, psshfs, node, reclaim);
181
182 memset(&pctx, 0, sizeof(pctx));
183 pctx.mounttime = time(NULL);
184
185 userhost = argv[0];
186 hostpath = strchr(userhost, ':');
187 if (hostpath) {
188 *hostpath++ = '\0';
189 pctx.mountpath = hostpath;
190 } else
191 pctx.mountpath = ".";
192
193 add_ssharg(&sshargs, &nargs, argv[0]);
194 add_ssharg(&sshargs, &nargs, "sftp");
195
196 pu = puffs_init(pops, "ppshfs", &pctx, pflags);
197 if (pu == NULL)
198 err(1, "puffs_init");
199
200 pssh_connect(&pctx, sshargs);
201
202 if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1)
203 err(1, "setblockingmode");
204 if (exportfs)
205 puffs_setfhsize(pu, sizeof(struct psshfs_fid),
206 PUFFS_FHFLAG_NFSV2 | PUFFS_FHFLAG_NFSV3);
207
208 if (puffs_domount(pu, argv[1], mntflags) == -1)
209 err(1, "puffs_domount");
210
211 if (psshfs_domount(pu) != 0)
212 errx(1, "psshfs_domount");
213
214 x = 1;
215 if (ioctl(pctx.sshfd, FIONBIO, &x) == -1)
216 err(1, "nonblocking descriptor");
217
218 if (detach)
219 daemon(1, 0);
220
221 return puffs_framebuf_eventloop(pu, &pctx.sshfd, 1,
222 psbuf_read, psbuf_write, psbuf_cmp, NULL);
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