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