2 * cpu.c - Make a connection to a cpu server
4 * Invoked by listen as 'cpu -R | -N service net netdir'
5 * by users as 'cpu [-h system] [-c cmd args ...]'
17 void remoteside(void);
18 void fatal(char*, ...);
19 void lclnoteproc(int);
20 void rmtnoteproc(void);
21 void catcher(void*, char*);
23 void writestr(int, char*, char*, int);
24 int readstr(int, char*, int);
25 char *rexcall(int*, char*, char*);
39 char *srvname = "ncpu";
40 char *exportfs = "/bin/exportfs";
41 char *ealgs = "rc4_256 sha1";
43 /* message size for exportfs; may be larger so we can do big graphics in CPU window */
44 int msgsize = Maxfdata+IOHDRSZ;
46 /* encryption mechanisms */
47 static int clear(int);
49 int (*encryption)(int) = clear;
51 /* authentication mechanisms */
52 static int netkeyauth(int);
53 static int netkeysrvauth(int, char*);
54 static int p9auth(int);
55 static int srvp9auth(int, char*);
56 static int noauth(int);
57 static int srvnoauth(int, char*);
59 typedef struct AuthMethod AuthMethod;
61 char *name; /* name of method */
62 int (*cf)(int); /* client side authentication */
63 int (*sf)(int, char*); /* server side authentication */
65 { "p9", p9auth, srvp9auth,},
66 { "netkey", netkeyauth, netkeysrvauth,},
67 { "none", noauth, srvnoauth,},
70 AuthMethod *am = authmethod; /* default is p9 */
74 char *aan = "/bin/aan";
75 char *anstring = "tcp!*!0";
78 int filter(int fd, char *host);
83 fprint(2, "usage: cpu [-p] [-h system] [-u user] [-a authmethod] "
84 "[-e 'crypt hash'] [-k keypattern] [-P patternfile] "
85 "[-c cmd arg ...]\n");
90 * reading /proc/pid/args yields either "name args" or "name [display args]",
91 * so return only args or display args.
100 snprint(buf, sizeof buf, "#p/%d/args", getpid());
101 if((fd = open(buf, OREAD)) < 0)
104 n = read(fd, buf, sizeof buf-1);
108 if ((lp = strchr(buf, '[')) == nil || (rp = strrchr(buf, ']')) == nil) {
109 lp = strchr(buf, ' ');
120 main(int argc, char **argv)
122 char dat[MaxStr], buf[MaxStr], cmd[MaxStr], *p, *s, *err;
123 int ac, fd, ms, data;
127 origargs = procgetname();
128 /* see if we should use a larger message size */
129 fd = open("/dev/draw", OREAD);
132 if(msgsize < ms+IOHDRSZ)
133 msgsize = ms+IOHDRSZ;
139 fatal("can't read user name: %r");
144 fatal("unknown auth method %s", p);
147 ealgs = EARGF(usage());
148 if(*ealgs == 0 || strcmp(ealgs, "clear") == 0)
155 /* ignored but accepted for compatibility */
158 /* must be specified before -R/-O */
162 anstring = EARGF(usage());
164 case 'R': /* From listen */
168 system = EARGF(usage());
176 s = seprint(s, cmd+sizeof(cmd), " %q", p);
179 keyspec = smprint("%s %s", keyspec, EARGF(usage()));
182 patternfile = EARGF(usage());
185 user = EARGF(usage());
186 keyspec = smprint("%s user=%s", keyspec, user);
206 if(err = rexcall(&data, system, srvname))
207 fatal("%s: %s: %r", err, system);
209 procsetname("%s", origargs);
210 /* Tell the remote side the command to execute and where our working directory is */
212 writestr(data, cmd, "command", 0);
213 if(getwd(dat, sizeof(dat)) == 0)
214 writestr(data, "NO", "dir", 0);
216 writestr(data, dat, "dir", 0);
218 /* start up a process to pass along notes */
222 * Wait for the other end to execute and start our file service
225 if(readstr(data, buf, sizeof(buf)) < 0)
226 fatal("waiting for FS: %r");
227 if(strncmp("FS", buf, 2) != 0) {
228 print("remote cpu: %s", buf);
232 /* Begin serving the gnot namespace */
237 sprint(buf, "%d", msgsize);
244 if(patternfile != nil){
246 av[ac++] = patternfile;
250 fatal("starting exportfs: %r");
254 fatal(char *fmt, ...)
260 vsnprint(buf, sizeof(buf), fmt, arg);
262 fprint(2, "cpu: %s\n", buf);
263 syslog(0, "cpu", "%s", buf);
267 char *negstr = "negotiating authentication method";
269 /* Invoked with stdin and stdout connected to the network connection */
273 char user[MaxStr], buf[MaxStr], xdir[MaxStr], cmd[MaxStr];
274 int i, n, fd, badchdir, gotcmd;
277 putenv("service", "cpu");
280 /* negotiate authentication mechanism */
281 n = readstr(fd, cmd, sizeof(cmd));
283 fatal("authenticating: %r");
285 if(strcmp(cmd, "aan") == 0){
287 writestr(fd, "", nil, 1);
288 n = readstr(fd, cmd, sizeof(cmd));
290 fatal("authenticating: %r");
292 if(setamalg(cmd) < 0 || (nflag == 0 && am->sf == srvnoauth)) {
293 writestr(fd, "unsupported auth method", nil, 0);
294 fatal("bad auth method %s", cmd);
296 writestr(fd, "", "", 1);
298 if((fd = (*am->sf)(fd, user)) < 0)
299 fatal("srvauth: %r");
300 if((fd = filter(fd, nil)) < 0)
302 if((fd = encryption(fd)) < 0)
303 fatal("encrypt: %r");
305 /* Now collect invoking cpu's current directory or possibly a command */
307 if(readstr(fd, xdir, sizeof(xdir)) < 0)
308 fatal("dir/cmd: %r");
310 strcpy(cmd, &xdir[1]);
312 if(readstr(fd, xdir, sizeof(xdir)) < 0)
316 /* Establish the new process at the current working directory of the gnot */
318 if(strcmp(xdir, "NO") != 0)
322 /* Start the gnot serving its namespace */
323 writestr(fd, "FS", "FS", 0);
324 writestr(fd, "/", "exportfs dir", 0);
326 n = read(fd, buf, sizeof(buf));
327 if(n != 2 || buf[0] != 'O' || buf[1] != 'K')
328 exits("remote tree");
330 /* make sure buffers are big by doing fversion explicitly; pick a huge number; other side will trim */
331 strcpy(buf, VERSION9P);
332 if(fversion(fd, 64*1024, buf, sizeof buf) < 0)
333 exits("fversion failed");
334 if(mount(fd, -1, "/mnt/term", MCREATE|MREPL, "") < 0)
335 exits("mount failed");
339 /* the remote noteproc uses the mount so it must follow it */
342 for(i = 0; i < 3; i++)
345 if(open("/mnt/term/dev/cons", OREAD) != 0)
347 if(open("/mnt/term/dev/cons", OWRITE) != 1)
348 exits("open stdout");
352 print("cpu: failed to chdir to '%s'\n", xdir);
355 execl("/bin/rc", "rc", "-lc", cmd, nil);
357 execl("/bin/rc", "rc", "-li", nil);
358 fatal("exec shell: %r");
362 rexcall(int *fd, char *host, char *service)
369 na = netmkaddr(host, 0, service);
370 procsetname("dialing %s", na);
371 if((*fd = dial(na, 0, 0, 0)) < 0)
374 /* negotiate aan filter extension */
376 writestr(*fd, "aan", "negotiating aan", 0);
377 n = readstr(*fd, err, sizeof err);
379 return "negotiating aan";
381 errstr(err, sizeof err);
386 /* negotiate authentication mechanism */
388 snprint(msg, sizeof(msg), "%s %s", am->name, ealgs);
390 snprint(msg, sizeof(msg), "%s", am->name);
391 procsetname("writing %s", msg);
392 writestr(*fd, msg, negstr, 0);
393 procsetname("awaiting auth method");
394 n = readstr(*fd, err, sizeof err);
398 errstr(err, sizeof err);
403 procsetname("%s: auth via %s", origargs, am->name);
404 if((*fd = (*am->cf)(*fd)) < 0)
405 return "can't authenticate";
406 if((*fd = filter(*fd, system)) < 0)
407 return "can't filter";
408 if((*fd = encryption(*fd)) < 0)
409 return "can't encrypt";
414 writestr(int fd, char *str, char *thing, int ignore)
419 n = write(fd, str, l+1);
421 fatal("writing network: %s: %r", thing);
425 readstr(int fd, char *str, int len)
430 n = read(fd, str, 1);
442 readln(char *buf, int n)
447 n--; /* room for \0 */
450 if(read(0, p, 1) != 1)
452 if(*p == '\n' || *p == '\r')
461 * user level challenge/response
469 strecpy(chall, chall+sizeof chall, getuser());
470 print("user[%s]: ", chall);
471 if(readln(resp, sizeof(resp)) < 0)
475 writestr(fd, chall, "challenge/response", 1);
478 if(readstr(fd, chall, sizeof chall) < 0)
482 print("challenge: %s\nresponse: ", chall);
483 if(readln(resp, sizeof(resp)) < 0)
485 writestr(fd, resp, "challenge/response", 1);
491 netkeysrvauth(int fd, char *user)
498 if(readstr(fd, user, MaxStr) < 0)
503 for(tries = 0; tries < 10; tries++){
504 if((ch = auth_challenge("proto=p9cr role=server user=%q", user)) == nil)
506 writestr(fd, ch->chal, "challenge", 1);
507 if(readstr(fd, response, sizeof response) < 0)
510 ch->nresp = strlen(response);
511 if((ai = auth_response(ch)) != nil)
517 writestr(fd, "", "challenge", 1);
518 if(auth_chuid(ai, 0) < 0)
530 static char sslsecret[2][21];
535 return pushssl(fd, ealgs, sslsecret[0], sslsecret[1], nil);
539 mksecret(char *t, uchar *f)
541 sprint(t, "%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux",
542 f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7], f[8], f[9]);
546 sslsetup(int fd, uchar *secret, int nsecret, int isclient)
548 uchar key[16], digest[SHA1dlen];
555 werrstr("secret too small to ssl");
558 memmove(key+4, secret, 8);
560 /* exchange random numbers */
564 for(i = 0; i < 4; i++)
566 if(write(fd, key, 4) != 4)
568 if(readn(fd, key+12, 4) != 4)
571 for(i = 0; i < 4; i++)
573 if(readn(fd, key, 4) != 4)
575 if(write(fd, key+12, 4) != 4)
579 /* scramble into two secrets */
580 sha1(key, sizeof(key), digest, nil);
581 mksecret(sslsecret[isclient == 0], digest);
582 mksecret(sslsecret[isclient != 0], digest+10);
584 encryption = sslencrypt;
590 * plan9 authentication followed by rc4 encryption
597 ai = auth_proxy(fd, auth_getkey, "proto=p9any role=client %s", keyspec);
600 fd = sslsetup(fd, ai->secret, ai->nsecret, 1);
613 srvnoauth(int fd, char *user)
615 strecpy(user, user+MaxStr, getuser());
622 srvp9auth(int fd, char *user)
626 ai = auth_proxy(fd, nil, "proto=p9any role=server %s", keyspec);
629 if(auth_chuid(ai, nil) < 0)
631 snprint(user, MaxStr, "%s", ai->cuid);
632 fd = sslsetup(fd, ai->secret, ai->nsecret, 0);
638 * set authentication mechanism
643 for(am = authmethod; am->name != nil; am++)
644 if(strcmp(am->name, name) == 0)
651 * set authentication mechanism and encryption/hash algs
656 ealgs = strchr(s, ' ');
663 filter(int fd, char *host)
665 char addr[128], buf[256], *s, *file, *argv[16];
666 int p[2], lfd, flags, len, argc;
670 procsetname("filter %s", filterp);
671 flags = RFNOWAIT|RFPROC|RFMEM|RFFDG|RFREND;
674 if(announce(anstring, addr) < 0)
675 fatal("filter: Cannot announce %s: %r", anstring);
676 snprint(buf, sizeof(buf), "%s/local", addr);
677 if((lfd = open(buf, OREAD)) < 0)
678 fatal("filter: Cannot open %s: %r", buf);
679 if((len = read(lfd, buf, sizeof buf - 1)) < 0)
680 fatal("filter: Cannot read %s: %r", buf);
683 if(s = strchr(buf, '\n'))
685 if(write(fd, buf, len) != len)
686 fatal("filter: cannot write port; %r");
690 if((len = read(fd, buf, sizeof buf - 1)) < 0)
691 fatal("filter: cannot read port; %r");
693 if((s = strrchr(buf, '!')) == nil)
694 fatal("filter: malformed remote port: %s", buf);
695 strecpy(addr, addr+sizeof(addr), netmkaddr(host, "tcp", s+1));
696 strecpy(strrchr(addr, '!'), addr+sizeof(addr), s);
699 snprint(buf, sizeof(buf), "%s", filterp);
700 if((argc = tokenize(buf, argv, nelem(argv)-3)) <= 0)
701 fatal("filter: empty command");
707 if(s = strrchr(argv[0], '/'))
711 fatal("filter: pipe; %r");
713 switch(rfork(flags)) {
715 fatal("filter: rfork; %r\n");
717 if (dup(p[0], 1) < 0)
718 fatal("filter: Cannot dup to 1; %r");
719 if (dup(p[0], 0) < 0)
720 fatal("filter: Cannot dup to 0; %r");
724 fatal("filter: exec; %r");
732 char *rmtnotefile = "/mnt/term/dev/cpunote";
735 * loop reading /mnt/term/dev/note looking for notes.
736 * The child returns to start the shell.
741 int n, fd, pid, notepid;
744 /* new proc returns to start shell */
745 pid = rfork(RFPROC|RFFDG|RFNOTEG|RFNAMEG|RFMEM);
748 syslog(0, "cpu", "cpu -R: can't start noteproc: %r");
754 /* new proc reads notes from other side and posts them to shell */
755 switch(notepid = rfork(RFPROC|RFFDG|RFMEM)){
757 syslog(0, "cpu", "cpu -R: can't start wait proc: %r");
760 fd = open(rmtnotefile, OREAD);
765 n = read(fd, buf, sizeof(buf)-1);
767 postnote(PNGROUP, pid, "hangup");
771 postnote(PNGROUP, pid, buf);
775 /* original proc waits for shell proc to die and kills note proc */
778 if(n < 0 || n == pid)
781 postnote(PNPROC, notepid, "kill");
799 [Qdir] { ".", {Qdir, 0, QTDIR}, DMDIR|0555 },
800 [Qcpunote] { "cpunote", {Qcpunote, 0}, 0444 },
803 typedef struct Note Note;
810 typedef struct Request Request;
817 typedef struct Fid Fid;
828 Note *nfirst, *nlast;
829 Request *rfirst, *rlast;
833 fsreply(int fd, Fcall *f)
835 uchar buf[IOHDRSZ+Maxfdata];
839 fprint(2, "notefs: <-%F\n", f);
840 n = convS2M(f, buf, sizeof buf);
842 if(write(fd, buf, n) != n){
850 /* match a note read request with a note, reply to the request */
862 if(rp == nil || np == nil){
866 nfs.rfirst = rp->next;
867 nfs.nfirst = np->next;
871 rp->f.count = strlen(np->msg);
872 rp->f.data = np->msg;
873 rv = fsreply(fd, &rp->f);
888 for(l = &nfs.rfirst; *l != nil; l = &(*l)->next){
890 if(rp->f.tag == tag){
906 for(i = 0; i < Nfid; i++){
907 if(freefid < 0 && fids[i].file < 0)
909 if(fids[i].fid == fid)
913 fids[freefid].fid = fid;
914 return &fids[freefid];
920 fsstat(int fd, Fid *fid, Fcall *f)
925 memset(&d, 0, sizeof(d));
926 d.name = fstab[fid->file].name;
930 d.qid = fstab[fid->file].qid;
931 d.mode = fstab[fid->file].perm;
932 d.atime = d.mtime = time(0);
934 f->nstat = convD2M(&d, statbuf, sizeof statbuf);
935 return fsreply(fd, f);
939 fsread(int fd, Fid *fid, Fcall *f)
949 if(f->offset == 0 && f->count >0){
950 memset(&d, 0, sizeof(d));
951 d.name = fstab[Qcpunote].name;
955 d.qid = fstab[Qcpunote].qid;
956 d.mode = fstab[Qcpunote].perm;
957 d.atime = d.mtime = time(0);
958 f->count = convD2M(&d, buf, sizeof buf);
959 f->data = (char*)buf;
962 return fsreply(fd, f);
964 rp = mallocz(sizeof(*rp), 1);
969 if(nfs.rfirst == nil)
972 nfs.rlast->next = rp;
979 char Eperm[] = "permission denied";
980 char Enofile[] = "out of files";
981 char Enotdir[] = "not a directory";
986 uchar buf[IOHDRSZ+Maxfdata];
994 fmtinstall('F', fcallfmt);
996 for(n = 0; n < Nfid; n++){
1003 n = read9pmsg(fd, buf, sizeof(buf));
1004 if(n <= 0 || convM2S(buf, n, &f) != n)
1007 fprint(2, "notefs: ->%F\n", &f);
1009 fid = getfid(f.fid);
1020 f.ename = "unknown type";
1026 if(f.msize > IOHDRSZ+Maxfdata)
1027 f.msize = IOHDRSZ+Maxfdata;
1031 f.ename = "authentication not required";
1034 f.qid = fstab[Qdir].qid;
1039 if(f.newfid != f.fid){
1040 nfid = getfid(f.newfid);
1043 nfid->file = fid->file;
1046 for(i=0; i<f.nwname && i<MAXWELEM; i++){
1047 if(fid->file != Qdir){
1052 if(strcmp(f.wname[i], "..") == 0){
1053 wqid[i] = fstab[Qdir].qid;
1056 if(strcmp(f.wname[i], "cpunote") != 0){
1059 f.ename = "file does not exist";
1063 fid->file = Qcpunote;
1064 wqid[i] = fstab[Qcpunote].qid;
1066 if(nfid != nil && (f.type == Rerror || i < f.nwname))
1068 if(f.type != Rerror){
1070 for(i=0; i<f.nwqid; i++)
1071 f.wqid[i] = wqid[i];
1075 if(f.mode != OREAD){
1080 fid->omode = f.mode;
1081 if(fid->file == Qcpunote)
1083 f.qid = fstab[fid->file].qid;
1087 if(fsread(fd, fid, &f) < 0)
1092 if(fid->omode != -1 && fid->file == Qcpunote){
1094 if(ncpunote == 0) /* remote side is done */
1101 if(fsstat(fd, fid, &f) < 0)
1114 if(fsreply(fd, &f) < 0)
1119 fprint(2, "notefs exiting: %r\n");
1121 postnote(PNGROUP, exportpid, "kill");
1123 fprint(2, "postnote PNGROUP %d: %r\n", exportpid);
1127 char notebuf[ERRMAX];
1130 catcher(void*, char *text)
1135 if(n >= sizeof(notebuf))
1136 n = sizeof(notebuf)-1;
1137 memmove(notebuf, text, n);
1143 * mount in /dev a note file for the remote side to read.
1146 lclnoteproc(int netfd)
1154 fprint(2, "cpu: can't start note proc: pipe: %r\n");
1158 /* new proc mounts and returns to start exportfs */
1159 switch(pid = rfork(RFPROC|RFNAMEG|RFFDG|RFMEM)){
1164 fprint(2, "cpu: can't start note proc: rfork: %r\n");
1168 if(mount(pfd[1], -1, "/dev", MBEFORE, "") < 0)
1169 fprint(2, "cpu: can't mount note proc: %r\n");
1177 /* new proc listens for note file system rpc's */
1178 switch(rfork(RFPROC|RFNAMEG|RFMEM)){
1180 fprint(2, "cpu: can't start note proc: rfork1: %r\n");
1187 /* original proc waits for notes */
1197 np = mallocz(sizeof(Note), 1);
1199 strcpy(np->msg, notebuf);
1201 if(nfs.nfirst == nil)
1204 nfs.nlast->next = np;
1210 } else if(w->pid == exportpid)
1217 /* exits(w->msg); */