12 static char *encprotos[] = {
21 char *ealgs = "rc4_256 sha1";
22 int encproto = Encnone;
23 char *aan = "/bin/aan";
24 char *anstring = "tcp!*!0";
31 int connect(char*, char*);
33 void catcher(void*, char*);
34 void sysfatal(char*, ...);
36 int filter(int, char *, char *);
38 static void mksecret(char *, uchar *);
41 * based on libthread's threadsetname, but drags in less library code.
42 * actually just sets the arguments displayed.
45 procsetname(char *fmt, ...)
53 cmdname = vsmprint(fmt, arg);
57 snprint(buf, sizeof buf, "#p/%d/args", getpid());
58 if((fd = open(buf, OWRITE)) >= 0){
59 write(fd, cmdname, strlen(cmdname)+1);
66 post(char *name, char *envname, int srvfd)
71 fd = create(name, OWRITE, 0600);
74 snprint(buf, sizeof(buf), "%d", srvfd);
75 if(write(fd, buf, strlen(buf)) != strlen(buf))
76 sysfatal("srv write: %r");
78 putenv(envname, name);
82 lookup(char *s, char *l[])
86 for (i = 0; l[i] != 0; i++)
87 if (strcmp(l[i], s) == 0)
93 main(int argc, char **argv)
95 char *mntpt, *srvpost, srvfile[64];
96 int backwards = 0, fd, mntflags;
121 /* ignored but allowed for compatibility */
124 if ((encproto = lookup(EARGF(usage()), encprotos)) < 0)
128 ealgs = EARGF(usage());
129 if(*ealgs == 0 || strcmp(ealgs, "clear") == 0)
133 keyspec = EARGF(usage());
139 anstring = EARGF(usage());
142 srvpost = EARGF(usage());
154 mntpt = 0; /* to shut up compiler */
176 if (encproto == Enctls)
177 sysfatal("%s: tls has not yet been implemented", argv[0]);
185 fd = connect(argv[0], argv[1]);
187 fprint(fd, "impo %s %s\n", filterp? "aan": "nofilter", encprotos[encproto]);
189 if (encproto != Encnone && ealgs && ai) {
190 uchar key[16], digest[SHA1dlen];
191 char fromclientsecret[21];
192 char fromserversecret[21];
195 assert(ai->nsecret <= sizeof(key)-4);
196 memmove(key+4, ai->secret, ai->nsecret);
198 /* exchange random numbers */
200 for(i = 0; i < 4; i++)
202 if(write(fd, key, 4) != 4)
203 sysfatal("can't write key part: %r");
204 if(readn(fd, key+12, 4) != 4)
205 sysfatal("can't read key part: %r");
207 /* scramble into two secrets */
208 sha1(key, sizeof(key), digest, nil);
209 mksecret(fromclientsecret, digest);
210 mksecret(fromserversecret, digest+10);
213 fd = filter(fd, filterp, backwards ? nil : argv[0]);
215 /* set up encryption */
216 procsetname("pushssl");
217 fd = pushssl(fd, ealgs, fromclientsecret, fromserversecret, nil);
219 sysfatal("can't establish ssl connection: %r");
222 fd = filter(fd, filterp, backwards ? nil : argv[0]);
228 snprint(srvfile, sizeof(srvfile), "/srv/%s", srvpost);
230 post(srvfile, srvpost, fd);
232 procsetname("mount on %s", mntpt);
233 if(mount(fd, -1, mntpt, mntflags, "") < 0)
234 sysfatal("can't mount %s: %r", argv[1]);
237 if(backwards && argc > 1){
238 exec(argv[1], &argv[1]);
239 sysfatal("exec: %r");
245 catcher(void*, char *msg)
248 if(strcmp(msg, "alarm") == 0)
254 connect(char *system, char *tree)
256 char buf[ERRMAX], dir[128], *na;
259 na = netmkaddr(system, 0, "exportfs");
260 procsetname("dial %s", na);
261 if((fd = dial(na, 0, dir, 0)) < 0)
262 sysfatal("can't dial %s: %r", system);
265 procsetname("auth_proxy auth_getkey proto=p9any role=client %s", keyspec);
266 ai = auth_proxy(fd, auth_getkey, "proto=p9any role=client %s", keyspec);
268 sysfatal("%r: %s", system);
272 procsetname("writing tree name %s", tree);
273 n = write(fd, tree, strlen(tree));
275 sysfatal("can't write tree: %r");
277 strcpy(buf, "can't read tree");
279 procsetname("awaiting OK for %s", tree);
280 n = read(fd, buf, sizeof buf - 1);
281 if(n!=2 || buf[0]!='O' || buf[1]!='K'){
283 sysfatal("timed out connecting to %s", na);
284 buf[sizeof buf - 1] = '\0';
285 sysfatal("bad remote tree: %s", buf);
297 * Ignore doauth==0 on purpose. Is it useful here?
300 procsetname("auth_proxy auth_getkey proto=p9any role=server");
301 ai = auth_proxy(0, auth_getkey, "proto=p9any role=server");
303 sysfatal("auth_proxy: %r");
304 if(auth_chuid(ai, nil) < 0)
305 sysfatal("auth_chuid: %r");
306 putenv("service", "import");
310 open("/dev/null", ORDWR);
312 open("/dev/null", ORDWR);
320 fprint(2, "usage: import [-abcC] [-A] [-E clear|ssl|tls] "
321 "[-e 'crypt auth'|clear] [-k keypattern] [-p] [-n address ] [-z] host remotefs [mountpoint]\n");
326 filter(int fd, char *cmd, char *host)
328 char addr[128], buf[256], *s, *file, *argv[16];
329 int lfd, p[2], len, argc;
332 /* Get a free port and post it to the client. */
333 if (announce(anstring, addr) < 0)
334 sysfatal("filter: Cannot announce %s: %r", anstring);
336 snprint(buf, sizeof(buf), "%s/local", addr);
337 if ((lfd = open(buf, OREAD)) < 0)
338 sysfatal("filter: Cannot open %s: %r", buf);
339 if ((len = read(lfd, buf, sizeof buf - 1)) < 0)
340 sysfatal("filter: Cannot read %s: %r", buf);
343 if ((s = strchr(buf, '\n')) != nil)
345 if (write(fd, buf, len) != len)
346 sysfatal("filter: cannot write port; %r");
348 /* Read address string from connection */
349 if ((len = read(fd, buf, sizeof buf - 1)) < 0)
350 sysfatal("filter: cannot write port; %r");
353 if ((s = strrchr(buf, '!')) == nil)
354 sysfatal("filter: illegally formatted port %s", buf);
355 strecpy(addr, addr+sizeof(addr), netmkaddr(host, "tcp", s+1));
356 strecpy(strrchr(addr, '!'), addr+sizeof(addr), s);
360 fprint(2, "filter: %s\n", addr);
362 snprint(buf, sizeof(buf), "%s", cmd);
363 argc = tokenize(buf, argv, nelem(argv)-3);
365 sysfatal("filter: empty command");
373 if((s = strrchr(argv[0], '/')) != nil)
377 sysfatal("pipe: %r");
379 switch(rfork(RFNOWAIT|RFPROC|RFMEM|RFFDG|RFREND)) {
381 sysfatal("filter: rfork; %r\n");
384 if (dup(p[0], 1) < 0)
385 sysfatal("filter: Cannot dup to 1; %r");
386 if (dup(p[0], 0) < 0)
387 sysfatal("filter: Cannot dup to 0; %r");
391 sysfatal("filter: exec; %r");
401 mksecret(char *t, uchar *f)
403 sprint(t, "%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux",
404 f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7], f[8], f[9]);