psshfs.c revision 1.19 1 /* $NetBSD: psshfs.c,v 1.19 2007/05/06 19:48: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.19 2007/05/06 19:48: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, open);
148 PUFFSOP_SET(pops, psshfs, node, inactive);
149 PUFFSOP_SET(pops, psshfs, node, readdir);
150 PUFFSOP_SET(pops, psshfs, node, getattr);
151 PUFFSOP_SET(pops, psshfs, node, setattr);
152 PUFFSOP_SET(pops, psshfs, node, mkdir);
153 PUFFSOP_SET(pops, psshfs, node, remove);
154 PUFFSOP_SET(pops, psshfs, node, readlink);
155 PUFFSOP_SET(pops, psshfs, node, rmdir);
156 PUFFSOP_SET(pops, psshfs, node, symlink);
157 PUFFSOP_SET(pops, psshfs, node, rename);
158 PUFFSOP_SET(pops, psshfs, node, read);
159 PUFFSOP_SET(pops, psshfs, node, write);
160 PUFFSOP_SET(pops, psshfs, node, reclaim);
161
162 memset(&pctx, 0, sizeof(pctx));
163 pctx.mounttime = time(NULL);
164
165 userhost = argv[0];
166 hostpath = strchr(userhost, ':');
167 if (hostpath) {
168 *hostpath++ = '\0';
169 pctx.mountpath = hostpath;
170 } else
171 pctx.mountpath = ".";
172
173 /* xblah */
174 argi = 0;
175 sshargs[argi++] = SSH_PATH;
176 sshargs[argi++] = argv[0];
177 sshargs[argi++] = "-oClearAllForwardings=yes";
178 sshargs[argi++] = "-a";
179 sshargs[argi++] = "-x";
180 sshargs[argi++] = "-s";
181 if (sshport) {
182 sshargs[argi++] = "-p";
183 sshargs[argi++] = sshport;
184 }
185 sshargs[argi++] = "sftp";
186 sshargs[argi] = 0;
187
188 pu = puffs_init(pops, "ppshfs", &pctx, pflags);
189 if (pu == NULL)
190 err(1, "puffs_init");
191
192 pssh_connect(&pctx, sshargs);
193
194 if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1)
195 err(1, "setblockingmode");
196 if (exportfs)
197 puffs_setfhsize(pu, sizeof(struct psshfs_fid),
198 PUFFS_FHFLAG_NFSV2 | PUFFS_FHFLAG_NFSV3);
199
200 if (puffs_domount(pu, argv[1], mntflags) == -1)
201 err(1, "puffs_domount");
202
203 if (psshfs_domount(pu) != 0)
204 errx(1, "psshfs_domount");
205
206 x = 1;
207 if (ioctl(pctx.sshfd, FIONBIO, &x) == -1)
208 err(1, "nonblocking descriptor");
209
210 if (detach)
211 daemon(1, 0);
212
213 return puffs_framebuf_eventloop(pu, pctx.sshfd,
214 psbuf_read, psbuf_write, psbuf_cmp, NULL);
215 }
216
217 static void
218 pssh_connect(struct psshfs_ctx *pctx, char **sshargs)
219 {
220 int fds[2];
221 pid_t pid;
222
223 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == -1)
224 err(1, "socketpair");
225
226 pid = fork();
227 switch (pid) {
228 case -1:
229 err(1, "fork");
230 /*NOTREACHED*/
231 case 0: /* child */
232 if (dup2(fds[0], STDIN_FILENO) == -1)
233 err(1, "child dup2");
234 if (dup2(fds[0], STDOUT_FILENO) == -1)
235 err(1, "child dup2");
236 close(fds[0]);
237 close(fds[1]);
238 execvp(sshargs[0], sshargs);
239 break;
240 default:
241 pctx->sshpid = pid;
242 pctx->sshfd = fds[1];
243 close(fds[0]);
244 break;
245 }
246 }
247