/* * This file is part of the UCB release of Plan 9. It is subject to the license * terms in the LICENSE file found in the top-level directory of this * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No * part of the UCB release of Plan 9, including this file, may be copied, * modified, propagated, or distributed except according to the terms contained * in the LICENSE file. */ /* * cpu.c - Make a connection to a cpu server * * Invoked by listen as 'cpu -R | -N service net netdir' * by users as 'cpu [-h system] [-c cmd args ...]' */ #include <u.h>#include <libc.h>#include <bio.h>#include <auth.h>#include <fcall.h>#include <libsec.h> #define Maxfdata 8192#define MaxStr 128 void remoteside(int);void fatal(int, char*, ...);void lclnoteproc(int);void rmtnoteproc(void);void catcher(void*, char*);void usage(void);void writestr(int, char*, char*, int);int readstr(int, char*, int);char *rexcall(int*, char*, char*);int setamalg(char*);char *keyspec = ""; int notechan;int exportpid;char *system;int cflag;int dbg;char *user;char *patternfile;char *origargs; char *srvname = "ncpu";char *exportfs = "/bin/exportfs";char *ealgs = "rc4_256 sha1"; /* message size for exportfs; may be larger so we can do big graphics in CPU window */int msgsize = Maxfdata+IOHDRSZ; /* authentication mechanisms */static int netkeyauth(int);static int netkeysrvauth(int, char*);static int p9auth(int);static int srvp9auth(int, char*);static int noauth(int);static int srvnoauth(int, char*); typedef struct AuthMethod AuthMethod;struct AuthMethod { char *name; /* name of method */ int (*cf)(int); /* client side authentication */ int (*sf)(int, char*); /* server side authentication */} authmethod[] ={ { "p9", p9auth, srvp9auth,}, { "netkey", netkeyauth, netkeysrvauth,},// { "none", noauth, srvnoauth,}, { nil, nil}};AuthMethod *am = authmethod; /* default is p9 */ char *p9authproto = "p9any"; int setam(char*); voidusage(void){ fprint(2, "usage: cpu [-h system] [-u user] [-a authmethod] " "[-e 'crypt hash'] [-k keypattern] [-P patternfile] " "[-c cmd arg ...]\n"); exits("usage");} /* * reading /proc/pid/args yields either "name args" or "name [display args]", * so return only args or display args. */static char *procgetname(void){ int fd, n; char *lp, *rp; char buf[256]; snprint(buf, sizeof buf, "#p/%d/args", getpid()); if((fd = open(buf, OREAD)) < 0) return strdup(""); *buf = '\0'; n = read(fd, buf, sizeof buf-1); close(fd); if (n >= 0) buf[n] = '\0'; if ((lp = strchr(buf, '[')) == nil || (rp = strrchr(buf, ']')) == nil) { lp = strchr(buf, ' '); if (lp == nil) return strdup(""); else return strdup(lp+1); } *rp = '\0'; return strdup(lp+1);} /* * based on libthread's threadsetname, but drags in less library code. * actually just sets the arguments displayed. */voidprocsetname(char *fmt, ...){ int fd; char *cmdname; char buf[128]; va_list arg; va_start(arg, fmt); cmdname = vsmprint(fmt, arg); va_end(arg); if (cmdname == nil) return; snprint(buf, sizeof buf, "#p/%d/args", getpid()); if((fd = open(buf, OWRITE)) >= 0){ write(fd, cmdname, strlen(cmdname)+1); close(fd); } free(cmdname);} voidmain(int argc, char **argv){ char dat[MaxStr], buf[MaxStr], cmd[MaxStr], *p, *err; int ac, fd, ms, data; char *av[10]; quotefmtinstall(); origargs = procgetname(); /* see if we should use a larger message size */ fd = open("/dev/draw", OREAD); if(fd > 0){ ms = iounit(fd); if(msgsize < ms+IOHDRSZ) msgsize = ms+IOHDRSZ; close(fd); } user = getuser(); if(user == nil) fatal(1, "can't read user name"); ARGBEGIN{ case 'a': p = EARGF(usage()); if(setam(p) < 0) fatal(0, "unknown auth method %s", p); break; case 'e': ealgs = EARGF(usage()); if(*ealgs == 0 || strcmp(ealgs, "clear") == 0) ealgs = nil; break; case 'd': dbg++; break; case 'f': /* ignored but accepted for compatibility */ break; case 'O': p9authproto = "p9sk2"; remoteside(1); /* From listen */ break; case 'R': /* From listen */ remoteside(0); break; case 'h': system = EARGF(usage()); break; case 'c': cflag++; cmd[0] = '!'; cmd[1] = '\0'; while(p = ARGF()) { strcat(cmd, " "); strcat(cmd, p); } break; case 'k': keyspec = smprint("%s %s", keyspec, EARGF(usage())); break; case 'P': patternfile = EARGF(usage()); break; case 'u': user = EARGF(usage()); keyspec = smprint("%s user=%s", keyspec, user); break; default: usage(); }ARGEND; if(argc != 0) usage(); if(system == nil) { p = getenv("cpu"); if(p == 0) fatal(0, "set $cpu"); system = p; } if(err = rexcall(&data, system, srvname)) fatal(1, "%s: %s", err, system); procsetname("%s", origargs); /* Tell the remote side the command to execute and where our working directory is */ if(cflag) writestr(data, cmd, "command", 0); if(getwd(dat, sizeof(dat)) == 0) writestr(data, "NO", "dir", 0); else writestr(data, dat, "dir", 0); /* start up a process to pass along notes */ lclnoteproc(data); /* * Wait for the other end to execute and start our file service * of /mnt/term */ if(readstr(data, buf, sizeof(buf)) < 0) fatal(1, "waiting for FS: %r"); if(strncmp("FS", buf, 2) != 0) { print("remote cpu: %s", buf); exits(buf); } /* Begin serving the gnot namespace */ close(0); dup(data, 0); close(data); sprint(buf, "%d", msgsize); ac = 0; av[ac++] = exportfs; av[ac++] = "-m"; av[ac++] = buf; if(dbg) av[ac++] = "-d"; if(patternfile != nil){ av[ac++] = "-P"; av[ac++] = patternfile; } av[ac] = nil; exec(exportfs, av); fatal(1, "starting exportfs");} voidfatal(int syserr, char *fmt, ...){ Fmt f; char *str; va_list arg; fmtstrinit(&f); fmtprint(&f, "cpu: "); va_start(arg, fmt); fmtvprint(&f, fmt, arg); va_end(arg); if(syserr) fmtprint(&f, ": %r"); str = fmtstrflush(&f); fprint(2, "%s\n", str); syslog(0, "cpu", str); exits(str);} char *negstr = "negotiating authentication method"; char bug[256]; intold9p(int fd){ int p[2]; if(pipe(p) < 0) fatal(1, "pipe"); switch(rfork(RFPROC|RFFDG|RFNAMEG)) { case -1: fatal(1, "rfork srvold9p"); case 0: if(fd != 1){ dup(fd, 1); close(fd); } if(p[0] != 0){ dup(p[0], 0); close(p[0]); } close(p[1]); if(0){ fd = open("/sys/log/cpu", OWRITE); if(fd != 2){ dup(fd, 2); close(fd); } execl("/bin/srvold9p", "srvold9p", "-ds", nil); } else execl("/bin/srvold9p", "srvold9p", "-s", nil); fatal(1, "exec srvold9p"); default: close(fd); close(p[0]); } return p[1]; } /* Invoked with stdin, stdout and stderr connected to the network connection */voidremoteside(int old){ char user[MaxStr], home[MaxStr], buf[MaxStr], xdir[MaxStr], cmd[MaxStr]; int i, n, fd, badchdir, gotcmd; rfork(RFENVG); putenv("service", "cpu"); fd = 0; /* negotiate authentication mechanism */ n = readstr(fd, cmd, sizeof(cmd)); if(n < 0) fatal(1, "authenticating"); if(setamalg(cmd) < 0){ writestr(fd, "unsupported auth method", nil, 0); fatal(1, "bad auth method %s", cmd); } else writestr(fd, "", "", 1); fd = (*am->sf)(fd, user); if(fd < 0) fatal(1, "srvauth"); /* Set environment values for the user */ putenv("user", user); sprint(home, "/usr/%s", user); putenv("home", home); /* Now collect invoking cpu's current directory or possibly a command */ gotcmd = 0; if(readstr(fd, xdir, sizeof(xdir)) < 0) fatal(1, "dir/cmd"); if(xdir[0] == '!') { strcpy(cmd, &xdir[1]); gotcmd = 1; if(readstr(fd, xdir, sizeof(xdir)) < 0) fatal(1, "dir"); } /* Establish the new process at the current working directory of the * gnot */ badchdir = 0; if(strcmp(xdir, "NO") == 0) chdir(home); else if(chdir(xdir) < 0) { badchdir = 1; chdir(home); } /* Start the gnot serving its namespace */ writestr(fd, "FS", "FS", 0); writestr(fd, "/", "exportfs dir", 0); n = read(fd, buf, sizeof(buf)); if(n != 2 || buf[0] != 'O' || buf[1] != 'K') exits("remote tree"); if(old) fd = old9p(fd); /* make sure buffers are big by doing fversion explicitly; pick a huge number; other side will trim */ strcpy(buf, VERSION9P); if(fversion(fd, 64*1024, buf, sizeof buf) < 0) exits("fversion failed"); if(mount(fd, -1, "/mnt/term", MCREATE|MREPL, "") < 0) exits("mount failed"); close(fd); /* the remote noteproc uses the mount so it must follow it */ rmtnoteproc(); for(i = 0; i < 3; i++) close(i); if(open("/mnt/term/dev/cons", OREAD) != 0) exits("open stdin"); if(open("/mnt/term/dev/cons", OWRITE) != 1) exits("open stdout"); dup(1, 2); if(badchdir) print("cpu: failed to chdir to '%s'\n", xdir); if(gotcmd) execl("/bin/rc", "rc", "-lc", cmd, nil); else execl("/bin/rc", "rc", "-li", nil); fatal(1, "exec shell");} char*rexcall(int *fd, char *host, char *service){ char *na; char dir[MaxStr]; char err[ERRMAX]; char msg[MaxStr]; int n; na = netmkaddr(host, 0, service); procsetname("dialing %s", na); if((*fd = dial(na, 0, dir, 0)) < 0) return "can't dial"; /* negotiate authentication mechanism */ if(ealgs != nil) snprint(msg, sizeof(msg), "%s %s", am->name, ealgs); else snprint(msg, sizeof(msg), "%s", am->name); procsetname("writing %s", msg); writestr(*fd, msg, negstr, 0); procsetname("awaiting auth method"); n = readstr(*fd, err, sizeof err); if(n < 0) return negstr; if(*err){ werrstr(err); return negstr; } /* authenticate */ procsetname("%s: auth via %s", origargs, am->name); *fd = (*am->cf)(*fd); if(*fd < 0) return "can't authenticate"; return 0;} voidwritestr(int fd, char *str, char *thing, int ignore){ int l, n; l = strlen(str); n = write(fd, str, l+1); if(!ignore && n < 0) fatal(1, "writing network: %s", thing);} intreadstr(int fd, char *str, int len){ int n; while(len) { n = read(fd, str, 1); if(n < 0) return -1; if(*str == '\0') return 0; str++; len--; } return -1;} static intreadln(char *buf, int n){ int i; char *p;Lines 1–500 of 1219. Load more
/* * This file is part of the UCB release of Plan 9. It is subject to the license * terms in the LICENSE file found in the top-level directory of this * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No * part of the UCB release of Plan 9, including this file, may be copied, * modified, propagated, or distributed except according to the terms contained * in the LICENSE file. */ /* * cpu.c - Make a connection to a cpu server * * Invoked by listen as 'cpu -R | -N service net netdir' * by users as 'cpu [-h system] [-c cmd args ...]' */ #include <u.h> #include <libc.h> #include <bio.h> #include <auth.h> #include <fcall.h> #include <libsec.h> #define Maxfdata 8192 #define MaxStr 128 void remoteside(int); void fatal(int, char*, ...); void lclnoteproc(int); void rmtnoteproc(void); void catcher(void*, char*); void usage(void); void writestr(int, char*, char*, int); int readstr(int, char*, int); char *rexcall(int*, char*, char*); int setamalg(char*); char *keyspec = ""; int notechan; int exportpid; char *system; int cflag; int dbg; char *user; char *patternfile; char *origargs; char *srvname = "ncpu"; char *exportfs = "/bin/exportfs"; char *ealgs = "rc4_256 sha1"; /* message size for exportfs; may be larger so we can do big graphics in CPU window */ int msgsize = Maxfdata+IOHDRSZ; /* authentication mechanisms */ static int netkeyauth(int); static int netkeysrvauth(int, char*); static int p9auth(int); static int srvp9auth(int, char*); static int noauth(int); static int srvnoauth(int, char*); typedef struct AuthMethod AuthMethod; struct AuthMethod { char *name; /* name of method */ int (*cf)(int); /* client side authentication */ int (*sf)(int, char*); /* server side authentication */ } authmethod[] = { { "p9", p9auth, srvp9auth,}, { "netkey", netkeyauth, netkeysrvauth,}, // { "none", noauth, srvnoauth,}, { nil, nil} }; AuthMethod *am = authmethod; /* default is p9 */ char *p9authproto = "p9any"; int setam(char*); void usage(void) { fprint(2, "usage: cpu [-h system] [-u user] [-a authmethod] " "[-e 'crypt hash'] [-k keypattern] [-P patternfile] " "[-c cmd arg ...]\n"); exits("usage"); } /* * reading /proc/pid/args yields either "name args" or "name [display args]", * so return only args or display args. */ static char * procgetname(void) { int fd, n; char *lp, *rp; char buf[256]; snprint(buf, sizeof buf, "#p/%d/args", getpid()); if((fd = open(buf, OREAD)) < 0) return strdup(""); *buf = '\0'; n = read(fd, buf, sizeof buf-1); close(fd); if (n >= 0) buf[n] = '\0'; if ((lp = strchr(buf, '[')) == nil || (rp = strrchr(buf, ']')) == nil) { lp = strchr(buf, ' '); if (lp == nil) return strdup(""); else return strdup(lp+1); } *rp = '\0'; return strdup(lp+1); } /* * based on libthread's threadsetname, but drags in less library code. * actually just sets the arguments displayed. */ void procsetname(char *fmt, ...) { int fd; char *cmdname; char buf[128]; va_list arg; va_start(arg, fmt); cmdname = vsmprint(fmt, arg); va_end(arg); if (cmdname == nil) return; snprint(buf, sizeof buf, "#p/%d/args", getpid()); if((fd = open(buf, OWRITE)) >= 0){ write(fd, cmdname, strlen(cmdname)+1); close(fd); } free(cmdname); } void main(int argc, char **argv) { char dat[MaxStr], buf[MaxStr], cmd[MaxStr], *p, *err; int ac, fd, ms, data; char *av[10]; quotefmtinstall(); origargs = procgetname(); /* see if we should use a larger message size */ fd = open("/dev/draw", OREAD); if(fd > 0){ ms = iounit(fd); if(msgsize < ms+IOHDRSZ) msgsize = ms+IOHDRSZ; close(fd); } user = getuser(); if(user == nil) fatal(1, "can't read user name"); ARGBEGIN{ case 'a': p = EARGF(usage()); if(setam(p) < 0) fatal(0, "unknown auth method %s", p); break; case 'e': ealgs = EARGF(usage()); if(*ealgs == 0 || strcmp(ealgs, "clear") == 0) ealgs = nil; break; case 'd': dbg++; break; case 'f': /* ignored but accepted for compatibility */ break; case 'O': p9authproto = "p9sk2"; remoteside(1); /* From listen */ break; case 'R': /* From listen */ remoteside(0); break; case 'h': system = EARGF(usage()); break; case 'c': cflag++; cmd[0] = '!'; cmd[1] = '\0'; while(p = ARGF()) { strcat(cmd, " "); strcat(cmd, p); } break; case 'k': keyspec = smprint("%s %s", keyspec, EARGF(usage())); break; case 'P': patternfile = EARGF(usage()); break; case 'u': user = EARGF(usage()); keyspec = smprint("%s user=%s", keyspec, user); break; default: usage(); }ARGEND; if(argc != 0) usage(); if(system == nil) { p = getenv("cpu"); if(p == 0) fatal(0, "set $cpu"); system = p; } if(err = rexcall(&data, system, srvname)) fatal(1, "%s: %s", err, system); procsetname("%s", origargs); /* Tell the remote side the command to execute and where our working directory is */ if(cflag) writestr(data, cmd, "command", 0); if(getwd(dat, sizeof(dat)) == 0) writestr(data, "NO", "dir", 0); else writestr(data, dat, "dir", 0); /* start up a process to pass along notes */ lclnoteproc(data); /* * Wait for the other end to execute and start our file service * of /mnt/term */ if(readstr(data, buf, sizeof(buf)) < 0) fatal(1, "waiting for FS: %r"); if(strncmp("FS", buf, 2) != 0) { print("remote cpu: %s", buf); exits(buf); } /* Begin serving the gnot namespace */ close(0); dup(data, 0); close(data); sprint(buf, "%d", msgsize); ac = 0; av[ac++] = exportfs; av[ac++] = "-m"; av[ac++] = buf; if(dbg) av[ac++] = "-d"; if(patternfile != nil){ av[ac++] = "-P"; av[ac++] = patternfile; } av[ac] = nil; exec(exportfs, av); fatal(1, "starting exportfs"); } void fatal(int syserr, char *fmt, ...) { Fmt f; char *str; va_list arg; fmtstrinit(&f); fmtprint(&f, "cpu: "); va_start(arg, fmt); fmtvprint(&f, fmt, arg); va_end(arg); if(syserr) fmtprint(&f, ": %r"); str = fmtstrflush(&f); fprint(2, "%s\n", str); syslog(0, "cpu", str); exits(str); } char *negstr = "negotiating authentication method"; char bug[256]; int old9p(int fd) { int p[2]; if(pipe(p) < 0) fatal(1, "pipe"); switch(rfork(RFPROC|RFFDG|RFNAMEG)) { case -1: fatal(1, "rfork srvold9p"); case 0: if(fd != 1){ dup(fd, 1); close(fd); } if(p[0] != 0){ dup(p[0], 0); close(p[0]); } close(p[1]); if(0){ fd = open("/sys/log/cpu", OWRITE); if(fd != 2){ dup(fd, 2); close(fd); } execl("/bin/srvold9p", "srvold9p", "-ds", nil); } else execl("/bin/srvold9p", "srvold9p", "-s", nil); fatal(1, "exec srvold9p"); default: close(fd); close(p[0]); } return p[1]; } /* Invoked with stdin, stdout and stderr connected to the network connection */ void remoteside(int old) { char user[MaxStr], home[MaxStr], buf[MaxStr], xdir[MaxStr], cmd[MaxStr]; int i, n, fd, badchdir, gotcmd; rfork(RFENVG); putenv("service", "cpu"); fd = 0; /* negotiate authentication mechanism */ n = readstr(fd, cmd, sizeof(cmd)); if(n < 0) fatal(1, "authenticating"); if(setamalg(cmd) < 0){ writestr(fd, "unsupported auth method", nil, 0); fatal(1, "bad auth method %s", cmd); } else writestr(fd, "", "", 1); fd = (*am->sf)(fd, user); if(fd < 0) fatal(1, "srvauth"); /* Set environment values for the user */ putenv("user", user); sprint(home, "/usr/%s", user); putenv("home", home); /* Now collect invoking cpu's current directory or possibly a command */ gotcmd = 0; if(readstr(fd, xdir, sizeof(xdir)) < 0) fatal(1, "dir/cmd"); if(xdir[0] == '!') { strcpy(cmd, &xdir[1]); gotcmd = 1; if(readstr(fd, xdir, sizeof(xdir)) < 0) fatal(1, "dir"); } /* Establish the new process at the current working directory of the * gnot */ badchdir = 0; if(strcmp(xdir, "NO") == 0) chdir(home); else if(chdir(xdir) < 0) { badchdir = 1; chdir(home); } /* Start the gnot serving its namespace */ writestr(fd, "FS", "FS", 0); writestr(fd, "/", "exportfs dir", 0); n = read(fd, buf, sizeof(buf)); if(n != 2 || buf[0] != 'O' || buf[1] != 'K') exits("remote tree"); if(old) fd = old9p(fd); /* make sure buffers are big by doing fversion explicitly; pick a huge number; other side will trim */ strcpy(buf, VERSION9P); if(fversion(fd, 64*1024, buf, sizeof buf) < 0) exits("fversion failed"); if(mount(fd, -1, "/mnt/term", MCREATE|MREPL, "") < 0) exits("mount failed"); close(fd); /* the remote noteproc uses the mount so it must follow it */ rmtnoteproc(); for(i = 0; i < 3; i++) close(i); if(open("/mnt/term/dev/cons", OREAD) != 0) exits("open stdin"); if(open("/mnt/term/dev/cons", OWRITE) != 1) exits("open stdout"); dup(1, 2); if(badchdir) print("cpu: failed to chdir to '%s'\n", xdir); if(gotcmd) execl("/bin/rc", "rc", "-lc", cmd, nil); else execl("/bin/rc", "rc", "-li", nil); fatal(1, "exec shell"); } char* rexcall(int *fd, char *host, char *service) { char *na; char dir[MaxStr]; char err[ERRMAX]; char msg[MaxStr]; int n; na = netmkaddr(host, 0, service); procsetname("dialing %s", na); if((*fd = dial(na, 0, dir, 0)) < 0) return "can't dial"; /* negotiate authentication mechanism */ if(ealgs != nil) snprint(msg, sizeof(msg), "%s %s", am->name, ealgs); else snprint(msg, sizeof(msg), "%s", am->name); procsetname("writing %s", msg); writestr(*fd, msg, negstr, 0); procsetname("awaiting auth method"); n = readstr(*fd, err, sizeof err); if(n < 0) return negstr; if(*err){ werrstr(err); return negstr; } /* authenticate */ procsetname("%s: auth via %s", origargs, am->name); *fd = (*am->cf)(*fd); if(*fd < 0) return "can't authenticate"; return 0; } void writestr(int fd, char *str, char *thing, int ignore) { int l, n; l = strlen(str); n = write(fd, str, l+1); if(!ignore && n < 0) fatal(1, "writing network: %s", thing); } int readstr(int fd, char *str, int len) { int n; while(len) { n = read(fd, str, 1); if(n < 0) return -1; if(*str == '\0') return 0; str++; len--; } return -1; } static int readln(char *buf, int n) { int i; char *p;