psshfs.c revision 1.21 1 /* $NetBSD: psshfs.c,v 1.21 2007/05/09 19:54:39 tnn 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.21 2007/05/09 19:54:39 tnn 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 errx(1, "usage: %s "
87 "[-es] [-O sshopt value] [-o opts] user@host:path mountpath",
88 getprogname());
89 }
90
91 int
92 main(int argc, char *argv[])
93 {
94 struct psshfs_ctx pctx;
95 struct puffs_usermount *pu;
96 struct puffs_ops *pops;
97 mntoptparse_t mp;
98 char **sshargs;
99 char *arg;
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 if (optind == argc)
125 usage();
126 if (asprintf(&arg, "-o%s=%s", optarg,
127 argv[optind]) == -1)
128 err(1, "asprintf");
129 add_ssharg(&sshargs, &nargs, arg);
130 optind++;
131 break;
132 case 'o':
133 mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
134 if (mp == NULL)
135 err(1, "getmntopts");
136 freemntopts(mp);
137 break;
138 case 's':
139 detach = 0;
140 break;
141 default:
142 usage();
143 /*NOTREACHED*/
144 }
145 }
146 argc -= optind;
147 argv += optind;
148
149 #if 0
150 /* XXX: noatime is mandatory for now */
151 mntflags |= MNT_NOATIME;
152 #endif
153
154 if (pflags & PUFFS_FLAG_OPDUMP)
155 detach = 0;
156 pflags |= PUFFS_FLAG_BUILDPATH;
157 pflags |= PUFFS_KFLAG_WTCACHE | PUFFS_KFLAG_IAONDEMAND;
158
159 if (argc != 2)
160 usage();
161
162 PUFFSOP_INIT(pops);
163
164 PUFFSOP_SET(pops, psshfs, fs, unmount);
165 PUFFSOP_SETFSNOP(pops, sync); /* XXX */
166 PUFFSOP_SETFSNOP(pops, statvfs);
167 PUFFSOP_SET(pops, psshfs, fs, nodetofh);
168 PUFFSOP_SET(pops, psshfs, fs, fhtonode);
169
170 PUFFSOP_SET(pops, psshfs, node, lookup);
171 PUFFSOP_SET(pops, psshfs, node, create);
172 PUFFSOP_SET(pops, psshfs, node, open);
173 PUFFSOP_SET(pops, psshfs, node, inactive);
174 PUFFSOP_SET(pops, psshfs, node, readdir);
175 PUFFSOP_SET(pops, psshfs, node, getattr);
176 PUFFSOP_SET(pops, psshfs, node, setattr);
177 PUFFSOP_SET(pops, psshfs, node, mkdir);
178 PUFFSOP_SET(pops, psshfs, node, remove);
179 PUFFSOP_SET(pops, psshfs, node, readlink);
180 PUFFSOP_SET(pops, psshfs, node, rmdir);
181 PUFFSOP_SET(pops, psshfs, node, symlink);
182 PUFFSOP_SET(pops, psshfs, node, rename);
183 PUFFSOP_SET(pops, psshfs, node, read);
184 PUFFSOP_SET(pops, psshfs, node, write);
185 PUFFSOP_SET(pops, psshfs, node, reclaim);
186
187 memset(&pctx, 0, sizeof(pctx));
188 pctx.mounttime = time(NULL);
189
190 userhost = argv[0];
191 hostpath = strchr(userhost, ':');
192 if (hostpath) {
193 *hostpath++ = '\0';
194 pctx.mountpath = hostpath;
195 } else
196 pctx.mountpath = ".";
197
198 add_ssharg(&sshargs, &nargs, argv[0]);
199 add_ssharg(&sshargs, &nargs, "sftp");
200
201 pu = puffs_init(pops, "ppshfs", &pctx, pflags);
202 if (pu == NULL)
203 err(1, "puffs_init");
204
205 pssh_connect(&pctx, sshargs);
206
207 if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1)
208 err(1, "setblockingmode");
209 if (exportfs)
210 puffs_setfhsize(pu, sizeof(struct psshfs_fid),
211 PUFFS_FHFLAG_NFSV2 | PUFFS_FHFLAG_NFSV3);
212
213 if (puffs_domount(pu, argv[1], mntflags) == -1)
214 err(1, "puffs_domount");
215
216 if (psshfs_domount(pu) != 0)
217 errx(1, "psshfs_domount");
218
219 x = 1;
220 if (ioctl(pctx.sshfd, FIONBIO, &x) == -1)
221 err(1, "nonblocking descriptor");
222
223 if (detach)
224 daemon(1, 0);
225
226 return puffs_framebuf_eventloop(pu, pctx.sshfd,
227 psbuf_read, psbuf_write, psbuf_cmp, NULL);
228 }
229
230 static void
231 pssh_connect(struct psshfs_ctx *pctx, char **sshargs)
232 {
233 int fds[2];
234 pid_t pid;
235
236 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == -1)
237 err(1, "socketpair");
238
239 pid = fork();
240 switch (pid) {
241 case -1:
242 err(1, "fork");
243 /*NOTREACHED*/
244 case 0: /* child */
245 if (dup2(fds[0], STDIN_FILENO) == -1)
246 err(1, "child dup2");
247 if (dup2(fds[0], STDOUT_FILENO) == -1)
248 err(1, "child dup2");
249 close(fds[0]);
250 close(fds[1]);
251 execvp(sshargs[0], sshargs);
252 break;
253 default:
254 pctx->sshpid = pid;
255 pctx->sshfd = fds[1];
256 close(fds[0]);
257 break;
258 }
259 }
260