cpu.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * cpu.c - Make a connection to a cpu server
  11. *
  12. * Invoked by listen as 'cpu -R | -N service net netdir'
  13. * by users as 'cpu [-h system] [-c cmd args ...]'
  14. */
  15. #include <u.h>
  16. #include <libc.h>
  17. #include <bio.h>
  18. #include <auth.h>
  19. #include <fcall.h>
  20. #include <libsec.h>
  21. #define Maxfdata 8192
  22. #define MaxStr 128
  23. void remoteside(int);
  24. void fatal(int, char*, ...);
  25. void lclnoteproc(int);
  26. void rmtnoteproc(void);
  27. void catcher(void*, char*);
  28. void usage(void);
  29. void writestr(int, char*, char*, int);
  30. int readstr(int, char*, int);
  31. char *rexcall(int*, char*, char*);
  32. int setamalg(char*);
  33. char *keyspec = "";
  34. int notechan;
  35. int exportpid;
  36. char *system;
  37. int cflag;
  38. int dbg;
  39. char *user;
  40. char *patternfile;
  41. char *origargs;
  42. char *srvname = "ncpu";
  43. char *exportfs = "/bin/exportfs";
  44. char *ealgs = "rc4_256 sha1";
  45. /* message size for exportfs; may be larger so we can do big graphics in CPU window */
  46. int msgsize = Maxfdata+IOHDRSZ;
  47. /* authentication mechanisms */
  48. static int netkeyauth(int);
  49. static int netkeysrvauth(int, char*);
  50. static int p9auth(int);
  51. static int srvp9auth(int, char*);
  52. static int noauth(int);
  53. static int srvnoauth(int, char*);
  54. typedef struct AuthMethod AuthMethod;
  55. struct AuthMethod {
  56. char *name; /* name of method */
  57. int (*cf)(int); /* client side authentication */
  58. int (*sf)(int, char*); /* server side authentication */
  59. } authmethod[] =
  60. {
  61. { "p9", p9auth, srvp9auth,},
  62. { "netkey", netkeyauth, netkeysrvauth,},
  63. // { "none", noauth, srvnoauth,},
  64. { nil, nil}
  65. };
  66. AuthMethod *am = authmethod; /* default is p9 */
  67. char *p9authproto = "p9any";
  68. int setam(char*);
  69. void
  70. usage(void)
  71. {
  72. fprint(2, "usage: cpu [-h system] [-u user] [-a authmethod] "
  73. "[-e 'crypt hash'] [-k keypattern] [-P patternfile] "
  74. "[-c cmd arg ...]\n");
  75. exits("usage");
  76. }
  77. /*
  78. * reading /proc/pid/args yields either "name args" or "name [display args]",
  79. * so return only args or display args.
  80. */
  81. static char *
  82. procgetname(void)
  83. {
  84. int fd, n;
  85. char *lp, *rp;
  86. char buf[256];
  87. snprint(buf, sizeof buf, "#p/%d/args", getpid());
  88. if((fd = open(buf, OREAD)) < 0)
  89. return strdup("");
  90. *buf = '\0';
  91. n = read(fd, buf, sizeof buf-1);
  92. close(fd);
  93. if (n >= 0)
  94. buf[n] = '\0';
  95. if ((lp = strchr(buf, '[')) == nil || (rp = strrchr(buf, ']')) == nil) {
  96. lp = strchr(buf, ' ');
  97. if (lp == nil)
  98. return strdup("");
  99. else
  100. return strdup(lp+1);
  101. }
  102. *rp = '\0';
  103. return strdup(lp+1);
  104. }
  105. /*
  106. * based on libthread's threadsetname, but drags in less library code.
  107. * actually just sets the arguments displayed.
  108. */
  109. void
  110. procsetname(char *fmt, ...)
  111. {
  112. int fd;
  113. char *cmdname;
  114. char buf[128];
  115. va_list arg;
  116. va_start(arg, fmt);
  117. cmdname = vsmprint(fmt, arg);
  118. va_end(arg);
  119. if (cmdname == nil)
  120. return;
  121. snprint(buf, sizeof buf, "#p/%d/args", getpid());
  122. if((fd = open(buf, OWRITE)) >= 0){
  123. write(fd, cmdname, strlen(cmdname)+1);
  124. close(fd);
  125. }
  126. free(cmdname);
  127. }
  128. void
  129. main(int argc, char **argv)
  130. {
  131. char dat[MaxStr], buf[MaxStr], cmd[MaxStr], *p, *err;
  132. int ac, fd, ms, data;
  133. char *av[10];
  134. quotefmtinstall();
  135. origargs = procgetname();
  136. /* see if we should use a larger message size */
  137. fd = open("/dev/draw", OREAD);
  138. if(fd > 0){
  139. ms = iounit(fd);
  140. if(msgsize < ms+IOHDRSZ)
  141. msgsize = ms+IOHDRSZ;
  142. close(fd);
  143. }
  144. user = getuser();
  145. if(user == nil)
  146. fatal(1, "can't read user name");
  147. ARGBEGIN{
  148. case 'a':
  149. p = EARGF(usage());
  150. if(setam(p) < 0)
  151. fatal(0, "unknown auth method %s", p);
  152. break;
  153. case 'e':
  154. ealgs = EARGF(usage());
  155. if(*ealgs == 0 || strcmp(ealgs, "clear") == 0)
  156. ealgs = nil;
  157. break;
  158. case 'd':
  159. dbg++;
  160. break;
  161. case 'f':
  162. /* ignored but accepted for compatibility */
  163. break;
  164. case 'O':
  165. p9authproto = "p9sk2";
  166. remoteside(1); /* From listen */
  167. break;
  168. case 'R': /* From listen */
  169. remoteside(0);
  170. break;
  171. case 'h':
  172. system = EARGF(usage());
  173. break;
  174. case 'c':
  175. cflag++;
  176. cmd[0] = '!';
  177. cmd[1] = '\0';
  178. while(p = ARGF()) {
  179. strcat(cmd, " ");
  180. strcat(cmd, p);
  181. }
  182. break;
  183. case 'k':
  184. keyspec = smprint("%s %s", keyspec, EARGF(usage()));
  185. break;
  186. case 'P':
  187. patternfile = EARGF(usage());
  188. break;
  189. case 'u':
  190. user = EARGF(usage());
  191. keyspec = smprint("%s user=%s", keyspec, user);
  192. break;
  193. default:
  194. usage();
  195. }ARGEND;
  196. if(argc != 0)
  197. usage();
  198. if(system == nil) {
  199. p = getenv("cpu");
  200. if(p == 0)
  201. fatal(0, "set $cpu");
  202. system = p;
  203. }
  204. if(err = rexcall(&data, system, srvname))
  205. fatal(1, "%s: %s", err, system);
  206. procsetname("%s", origargs);
  207. /* Tell the remote side the command to execute and where our working directory is */
  208. if(cflag)
  209. writestr(data, cmd, "command", 0);
  210. if(getwd(dat, sizeof(dat)) == 0)
  211. writestr(data, "NO", "dir", 0);
  212. else
  213. writestr(data, dat, "dir", 0);
  214. /* start up a process to pass along notes */
  215. lclnoteproc(data);
  216. /*
  217. * Wait for the other end to execute and start our file service
  218. * of /mnt/term
  219. */
  220. if(readstr(data, buf, sizeof(buf)) < 0)
  221. fatal(1, "waiting for FS: %r");
  222. if(strncmp("FS", buf, 2) != 0) {
  223. print("remote cpu: %s", buf);
  224. exits(buf);
  225. }
  226. /* Begin serving the gnot namespace */
  227. close(0);
  228. dup(data, 0);
  229. close(data);
  230. sprint(buf, "%d", msgsize);
  231. ac = 0;
  232. av[ac++] = exportfs;
  233. av[ac++] = "-m";
  234. av[ac++] = buf;
  235. if(dbg)
  236. av[ac++] = "-d";
  237. if(patternfile != nil){
  238. av[ac++] = "-P";
  239. av[ac++] = patternfile;
  240. }
  241. av[ac] = nil;
  242. exec(exportfs, av);
  243. fatal(1, "starting exportfs");
  244. }
  245. void
  246. fatal(int syserr, char *fmt, ...)
  247. {
  248. Fmt f;
  249. char *str;
  250. va_list arg;
  251. fmtstrinit(&f);
  252. fmtprint(&f, "cpu: ");
  253. va_start(arg, fmt);
  254. fmtvprint(&f, fmt, arg);
  255. va_end(arg);
  256. if(syserr)
  257. fmtprint(&f, ": %r");
  258. str = fmtstrflush(&f);
  259. fprint(2, "%s\n", str);
  260. syslog(0, "cpu", str);
  261. exits(str);
  262. }
  263. char *negstr = "negotiating authentication method";
  264. char bug[256];
  265. int
  266. old9p(int fd)
  267. {
  268. int p[2];
  269. if(pipe(p) < 0)
  270. fatal(1, "pipe");
  271. switch(rfork(RFPROC|RFFDG|RFNAMEG)) {
  272. case -1:
  273. fatal(1, "rfork srvold9p");
  274. case 0:
  275. if(fd != 1){
  276. dup(fd, 1);
  277. close(fd);
  278. }
  279. if(p[0] != 0){
  280. dup(p[0], 0);
  281. close(p[0]);
  282. }
  283. close(p[1]);
  284. if(0){
  285. fd = open("/sys/log/cpu", OWRITE);
  286. if(fd != 2){
  287. dup(fd, 2);
  288. close(fd);
  289. }
  290. execl("/bin/srvold9p", "srvold9p", "-ds", nil);
  291. } else
  292. execl("/bin/srvold9p", "srvold9p", "-s", nil);
  293. fatal(1, "exec srvold9p");
  294. default:
  295. close(fd);
  296. close(p[0]);
  297. }
  298. return p[1];
  299. }
  300. /* Invoked with stdin, stdout and stderr connected to the network connection */
  301. void
  302. remoteside(int old)
  303. {
  304. char user[MaxStr], home[MaxStr], buf[MaxStr], xdir[MaxStr], cmd[MaxStr];
  305. int i, n, fd, badchdir, gotcmd;
  306. rfork(RFENVG);
  307. putenv("service", "cpu");
  308. fd = 0;
  309. /* negotiate authentication mechanism */
  310. n = readstr(fd, cmd, sizeof(cmd));
  311. if(n < 0)
  312. fatal(1, "authenticating");
  313. if(setamalg(cmd) < 0){
  314. writestr(fd, "unsupported auth method", nil, 0);
  315. fatal(1, "bad auth method %s", cmd);
  316. } else
  317. writestr(fd, "", "", 1);
  318. fd = (*am->sf)(fd, user);
  319. if(fd < 0)
  320. fatal(1, "srvauth");
  321. /* Set environment values for the user */
  322. putenv("user", user);
  323. sprint(home, "/usr/%s", user);
  324. putenv("home", home);
  325. /* Now collect invoking cpu's current directory or possibly a command */
  326. gotcmd = 0;
  327. if(readstr(fd, xdir, sizeof(xdir)) < 0)
  328. fatal(1, "dir/cmd");
  329. if(xdir[0] == '!') {
  330. strcpy(cmd, &xdir[1]);
  331. gotcmd = 1;
  332. if(readstr(fd, xdir, sizeof(xdir)) < 0)
  333. fatal(1, "dir");
  334. }
  335. /* Establish the new process at the current working directory of the
  336. * gnot */
  337. badchdir = 0;
  338. if(strcmp(xdir, "NO") == 0)
  339. chdir(home);
  340. else if(chdir(xdir) < 0) {
  341. badchdir = 1;
  342. chdir(home);
  343. }
  344. /* Start the gnot serving its namespace */
  345. writestr(fd, "FS", "FS", 0);
  346. writestr(fd, "/", "exportfs dir", 0);
  347. n = read(fd, buf, sizeof(buf));
  348. if(n != 2 || buf[0] != 'O' || buf[1] != 'K')
  349. exits("remote tree");
  350. if(old)
  351. fd = old9p(fd);
  352. /* make sure buffers are big by doing fversion explicitly; pick a huge number; other side will trim */
  353. strcpy(buf, VERSION9P);
  354. if(fversion(fd, 64*1024, buf, sizeof buf) < 0)
  355. exits("fversion failed");
  356. if(mount(fd, -1, "/mnt/term", MCREATE|MREPL, "") < 0)
  357. exits("mount failed");
  358. close(fd);
  359. /* the remote noteproc uses the mount so it must follow it */
  360. rmtnoteproc();
  361. for(i = 0; i < 3; i++)
  362. close(i);
  363. if(open("/mnt/term/dev/cons", OREAD) != 0)
  364. exits("open stdin");
  365. if(open("/mnt/term/dev/cons", OWRITE) != 1)
  366. exits("open stdout");
  367. dup(1, 2);
  368. if(badchdir)
  369. print("cpu: failed to chdir to '%s'\n", xdir);
  370. if(gotcmd)
  371. execl("/bin/rc", "rc", "-lc", cmd, nil);
  372. else
  373. execl("/bin/rc", "rc", "-li", nil);
  374. fatal(1, "exec shell");
  375. }
  376. char*
  377. rexcall(int *fd, char *host, char *service)
  378. {
  379. char *na;
  380. char dir[MaxStr];
  381. char err[ERRMAX];
  382. char msg[MaxStr];
  383. int n;
  384. na = netmkaddr(host, 0, service);
  385. procsetname("dialing %s", na);
  386. if((*fd = dial(na, 0, dir, 0)) < 0)
  387. return "can't dial";
  388. /* negotiate authentication mechanism */
  389. if(ealgs != nil)
  390. snprint(msg, sizeof(msg), "%s %s", am->name, ealgs);
  391. else
  392. snprint(msg, sizeof(msg), "%s", am->name);
  393. procsetname("writing %s", msg);
  394. writestr(*fd, msg, negstr, 0);
  395. procsetname("awaiting auth method");
  396. n = readstr(*fd, err, sizeof err);
  397. if(n < 0)
  398. return negstr;
  399. if(*err){
  400. werrstr(err);
  401. return negstr;
  402. }
  403. /* authenticate */
  404. procsetname("%s: auth via %s", origargs, am->name);
  405. *fd = (*am->cf)(*fd);
  406. if(*fd < 0)
  407. return "can't authenticate";
  408. return 0;
  409. }
  410. void
  411. writestr(int fd, char *str, char *thing, int ignore)
  412. {
  413. int l, n;
  414. l = strlen(str);
  415. n = write(fd, str, l+1);
  416. if(!ignore && n < 0)
  417. fatal(1, "writing network: %s", thing);
  418. }
  419. int
  420. readstr(int fd, char *str, int len)
  421. {
  422. int n;
  423. while(len) {
  424. n = read(fd, str, 1);
  425. if(n < 0)
  426. return -1;
  427. if(*str == '\0')
  428. return 0;
  429. str++;
  430. len--;
  431. }
  432. return -1;
  433. }
  434. static int
  435. readln(char *buf, int n)
  436. {
  437. int i;
  438. char *p;
  439. n--; /* room for \0 */
  440. p = buf;
  441. for(i=0; i<n; i++){
  442. if(read(0, p, 1) != 1)
  443. break;
  444. if(*p == '\n' || *p == '\r')
  445. break;
  446. p++;
  447. }
  448. *p = '\0';
  449. return p-buf;
  450. }
  451. /*
  452. * user level challenge/response
  453. */
  454. static int
  455. netkeyauth(int fd)
  456. {
  457. char chall[32];
  458. char resp[32];
  459. strecpy(chall, chall+sizeof chall, getuser());
  460. print("user[%s]: ", chall);
  461. if(readln(resp, sizeof(resp)) < 0)
  462. return -1;
  463. if(*resp != 0)
  464. strcpy(chall, resp);
  465. writestr(fd, chall, "challenge/response", 1);
  466. for(;;){
  467. if(readstr(fd, chall, sizeof chall) < 0)
  468. break;
  469. if(*chall == 0)
  470. return fd;
  471. print("challenge: %s\nresponse: ", chall);
  472. if(readln(resp, sizeof(resp)) < 0)
  473. break;
  474. writestr(fd, resp, "challenge/response", 1);
  475. }
  476. return -1;
  477. }
  478. static int
  479. netkeysrvauth(int fd, char *user)
  480. {
  481. char response[32];
  482. Chalstate *ch;
  483. int tries;
  484. AuthInfo *ai;
  485. if(readstr(fd, user, 32) < 0)
  486. return -1;
  487. ai = nil;
  488. ch = nil;
  489. for(tries = 0; tries < 10; tries++){
  490. if((ch = auth_challenge("proto=p9cr role=server user=%q", user)) == nil)
  491. return -1;
  492. writestr(fd, ch->chal, "challenge", 1);
  493. if(readstr(fd, response, sizeof response) < 0)
  494. return -1;
  495. ch->resp = response;
  496. ch->nresp = strlen(response);
  497. if((ai = auth_response(ch)) != nil)
  498. break;
  499. }
  500. auth_freechal(ch);
  501. if(ai == nil)
  502. return -1;
  503. writestr(fd, "", "challenge", 1);
  504. if(auth_chuid(ai, 0) < 0)
  505. fatal(1, "newns");
  506. auth_freeAI(ai);
  507. return fd;
  508. }
  509. static void
  510. mksecret(char *t, uint8_t *f)
  511. {
  512. sprint(t, "%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux",
  513. f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7], f[8], f[9]);
  514. }
  515. /*
  516. * plan9 authentication followed by rc4 encryption
  517. */
  518. static int
  519. p9auth(int fd)
  520. {
  521. uint8_t key[16];
  522. uint8_t digest[SHA1dlen];
  523. char fromclientsecret[21];
  524. char fromserversecret[21];
  525. int i;
  526. AuthInfo *ai;
  527. procsetname("%s: auth_proxy proto=%q role=client %s",
  528. origargs, p9authproto, keyspec);
  529. ai = auth_proxy(fd, auth_getkey, "proto=%q role=client %s", p9authproto, keyspec);
  530. if(ai == nil)
  531. return -1;
  532. memmove(key+4, ai->secret, ai->nsecret);
  533. if(ealgs == nil)
  534. return fd;
  535. /* exchange random numbers */
  536. srand(truerand());
  537. for(i = 0; i < 4; i++)
  538. key[i] = rand();
  539. procsetname("writing p9 key");
  540. if(write(fd, key, 4) != 4)
  541. return -1;
  542. procsetname("reading p9 key");
  543. if(readn(fd, key+12, 4) != 4)
  544. return -1;
  545. /* scramble into two secrets */
  546. sha1(key, sizeof(key), digest, nil);
  547. mksecret(fromclientsecret, digest);
  548. mksecret(fromserversecret, digest+10);
  549. /* set up encryption */
  550. procsetname("pushssl");
  551. i = pushssl(fd, ealgs, fromclientsecret, fromserversecret, nil);
  552. if(i < 0)
  553. werrstr("can't establish ssl connection: %r");
  554. return i;
  555. }
  556. static int
  557. noauth(int fd)
  558. {
  559. ealgs = nil;
  560. return fd;
  561. }
  562. static int
  563. srvnoauth(int fd, char *user)
  564. {
  565. strecpy(user, user+MaxStr, getuser());
  566. ealgs = nil;
  567. newns(user, nil);
  568. return fd;
  569. }
  570. void
  571. loghex(uint8_t *p, int n)
  572. {
  573. char buf[100];
  574. int i;
  575. for(i = 0; i < n; i++)
  576. sprint(buf+2*i, "%2.2ux", p[i]);
  577. syslog(0, "cpu", buf);
  578. }
  579. static int
  580. srvp9auth(int fd, char *user)
  581. {
  582. uint8_t key[16];
  583. uint8_t digest[SHA1dlen];
  584. char fromclientsecret[21];
  585. char fromserversecret[21];
  586. int i;
  587. AuthInfo *ai;
  588. ai = auth_proxy(0, nil, "proto=%q role=server %s", p9authproto, keyspec);
  589. if(ai == nil)
  590. return -1;
  591. if(auth_chuid(ai, nil) < 0)
  592. return -1;
  593. strecpy(user, user+MaxStr, ai->cuid);
  594. memmove(key+4, ai->secret, ai->nsecret);
  595. if(ealgs == nil)
  596. return fd;
  597. /* exchange random numbers */
  598. srand(truerand());
  599. for(i = 0; i < 4; i++)
  600. key[i+12] = rand();
  601. if(readn(fd, key, 4) != 4)
  602. return -1;
  603. if(write(fd, key+12, 4) != 4)
  604. return -1;
  605. /* scramble into two secrets */
  606. sha1(key, sizeof(key), digest, nil);
  607. mksecret(fromclientsecret, digest);
  608. mksecret(fromserversecret, digest+10);
  609. /* set up encryption */
  610. i = pushssl(fd, ealgs, fromserversecret, fromclientsecret, nil);
  611. if(i < 0)
  612. werrstr("can't establish ssl connection: %r");
  613. return i;
  614. }
  615. /*
  616. * set authentication mechanism
  617. */
  618. int
  619. setam(char *name)
  620. {
  621. for(am = authmethod; am->name != nil; am++)
  622. if(strcmp(am->name, name) == 0)
  623. return 0;
  624. am = authmethod;
  625. return -1;
  626. }
  627. /*
  628. * set authentication mechanism and encryption/hash algs
  629. */
  630. int
  631. setamalg(char *s)
  632. {
  633. ealgs = strchr(s, ' ');
  634. if(ealgs != nil)
  635. *ealgs++ = 0;
  636. return setam(s);
  637. }
  638. char *rmtnotefile = "/mnt/term/dev/cpunote";
  639. /*
  640. * loop reading /mnt/term/dev/note looking for notes.
  641. * The child returns to start the shell.
  642. */
  643. void
  644. rmtnoteproc(void)
  645. {
  646. int n, fd, pid, notepid;
  647. char buf[256];
  648. /* new proc returns to start shell */
  649. pid = rfork(RFPROC|RFFDG|RFNOTEG|RFNAMEG|RFMEM);
  650. switch(pid){
  651. case -1:
  652. syslog(0, "cpu", "cpu -R: can't start noteproc: %r");
  653. return;
  654. case 0:
  655. return;
  656. }
  657. /* new proc reads notes from other side and posts them to shell */
  658. switch(notepid = rfork(RFPROC|RFFDG|RFMEM)){
  659. case -1:
  660. syslog(0, "cpu", "cpu -R: can't start wait proc: %r");
  661. _exits(0);
  662. case 0:
  663. fd = open(rmtnotefile, OREAD);
  664. if(fd < 0){
  665. syslog(0, "cpu", "cpu -R: can't open %s", rmtnotefile);
  666. _exits(0);
  667. }
  668. for(;;){
  669. n = read(fd, buf, sizeof(buf)-1);
  670. if(n <= 0){
  671. postnote(PNGROUP, pid, "hangup");
  672. _exits(0);
  673. }
  674. buf[n] = 0;
  675. postnote(PNGROUP, pid, buf);
  676. }
  677. }
  678. /* original proc waits for shell proc to die and kills note proc */
  679. for(;;){
  680. n = waitpid();
  681. if(n < 0 || n == pid)
  682. break;
  683. }
  684. postnote(PNPROC, notepid, "kill");
  685. _exits(0);
  686. }
  687. enum
  688. {
  689. Qdir,
  690. Qcpunote,
  691. Nfid = 32,
  692. };
  693. struct {
  694. char *name;
  695. Qid qid;
  696. ulong perm;
  697. } fstab[] =
  698. {
  699. [Qdir] { ".", {Qdir, 0, QTDIR}, DMDIR|0555 },
  700. [Qcpunote] { "cpunote", {Qcpunote, 0}, 0444 },
  701. };
  702. typedef struct Note Note;
  703. struct Note
  704. {
  705. Note *next;
  706. char msg[ERRMAX];
  707. };
  708. typedef struct Request Request;
  709. struct Request
  710. {
  711. Request *next;
  712. Fcall f;
  713. };
  714. typedef struct Fid Fid;
  715. struct Fid
  716. {
  717. int fid;
  718. int file;
  719. int omode;
  720. };
  721. Fid fids[Nfid];
  722. struct {
  723. Lock;
  724. Note *nfirst, *nlast;
  725. Request *rfirst, *rlast;
  726. } nfs;
  727. int
  728. fsreply(int fd, Fcall *f)
  729. {
  730. uint8_t buf[IOHDRSZ+Maxfdata];
  731. int n;
  732. if(dbg)
  733. fprint(2, "notefs: <-%F\n", f);
  734. n = convS2M(f, buf, sizeof buf);
  735. if(n > 0){
  736. if(write(fd, buf, n) != n){
  737. close(fd);
  738. return -1;
  739. }
  740. }
  741. return 0;
  742. }
  743. /* match a note read request with a note, reply to the request */
  744. int
  745. kick(int fd)
  746. {
  747. Request *rp;
  748. Note *np;
  749. int rv;
  750. for(;;){
  751. lock(&nfs);
  752. rp = nfs.rfirst;
  753. np = nfs.nfirst;
  754. if(rp == nil || np == nil){
  755. unlock(&nfs);
  756. break;
  757. }
  758. nfs.rfirst = rp->next;
  759. nfs.nfirst = np->next;
  760. unlock(&nfs);
  761. rp->f.type = Rread;
  762. rp->f.count = strlen(np->msg);
  763. rp->f.data = np->msg;
  764. rv = fsreply(fd, &rp->f);
  765. free(rp);
  766. free(np);
  767. if(rv < 0)
  768. return -1;
  769. }
  770. return 0;
  771. }
  772. void
  773. flushreq(int tag)
  774. {
  775. Request **l, *rp;
  776. lock(&nfs);
  777. for(l = &nfs.rfirst; *l != nil; l = &(*l)->next){
  778. rp = *l;
  779. if(rp->f.tag == tag){
  780. *l = rp->next;
  781. unlock(&nfs);
  782. free(rp);
  783. return;
  784. }
  785. }
  786. unlock(&nfs);
  787. }
  788. Fid*
  789. getfid(int fid)
  790. {
  791. int i, freefid;
  792. freefid = -1;
  793. for(i = 0; i < Nfid; i++){
  794. if(freefid < 0 && fids[i].file < 0)
  795. freefid = i;
  796. if(fids[i].fid == fid)
  797. return &fids[i];
  798. }
  799. if(freefid >= 0){
  800. fids[freefid].fid = fid;
  801. return &fids[freefid];
  802. }
  803. return nil;
  804. }
  805. int
  806. fsstat(int fd, Fid *fid, Fcall *f)
  807. {
  808. Dir d;
  809. uint8_t statbuf[256];
  810. memset(&d, 0, sizeof(d));
  811. d.name = fstab[fid->file].name;
  812. d.uid = user;
  813. d.gid = user;
  814. d.muid = user;
  815. d.qid = fstab[fid->file].qid;
  816. d.mode = fstab[fid->file].perm;
  817. d.atime = d.mtime = time(0);
  818. f->stat = statbuf;
  819. f->nstat = convD2M(&d, statbuf, sizeof statbuf);
  820. return fsreply(fd, f);
  821. }
  822. int
  823. fsread(int fd, Fid *fid, Fcall *f)
  824. {
  825. Dir d;
  826. uint8_t buf[256];
  827. Request *rp;
  828. switch(fid->file){
  829. default:
  830. return -1;
  831. case Qdir:
  832. if(f->offset == 0 && f->count >0){
  833. memset(&d, 0, sizeof(d));
  834. d.name = fstab[Qcpunote].name;
  835. d.uid = user;
  836. d.gid = user;
  837. d.muid = user;
  838. d.qid = fstab[Qcpunote].qid;
  839. d.mode = fstab[Qcpunote].perm;
  840. d.atime = d.mtime = time(0);
  841. f->count = convD2M(&d, buf, sizeof buf);
  842. f->data = (char*)buf;
  843. } else
  844. f->count = 0;
  845. return fsreply(fd, f);
  846. case Qcpunote:
  847. rp = mallocz(sizeof(*rp), 1);
  848. if(rp == nil)
  849. return -1;
  850. rp->f = *f;
  851. lock(&nfs);
  852. if(nfs.rfirst == nil)
  853. nfs.rfirst = rp;
  854. else
  855. nfs.rlast->next = rp;
  856. nfs.rlast = rp;
  857. unlock(&nfs);
  858. return kick(fd);;
  859. }
  860. }
  861. char Eperm[] = "permission denied";
  862. char Enofile[] = "out of files";
  863. char Enotdir[] = "not a directory";
  864. void
  865. notefs(int fd)
  866. {
  867. uint8_t buf[IOHDRSZ+Maxfdata];
  868. int i, n, ncpunote;
  869. Fcall f;
  870. Qid wqid[MAXWELEM];
  871. Fid *fid, *nfid;
  872. int doreply;
  873. rfork(RFNOTEG);
  874. fmtinstall('F', fcallfmt);
  875. for(n = 0; n < Nfid; n++){
  876. fids[n].file = -1;
  877. fids[n].omode = -1;
  878. }
  879. ncpunote = 0;
  880. for(;;){
  881. n = read9pmsg(fd, buf, sizeof(buf));
  882. if(n <= 0){
  883. if(dbg)
  884. fprint(2, "read9pmsg(%d) returns %d: %r\n", fd, n);
  885. break;
  886. }
  887. if(convM2S(buf, n, &f) <= BIT16SZ)
  888. break;
  889. if(dbg)
  890. fprint(2, "notefs: ->%F\n", &f);
  891. doreply = 1;
  892. fid = getfid(f.fid);
  893. if(fid == nil){
  894. nofids:
  895. f.type = Rerror;
  896. f.ename = Enofile;
  897. fsreply(fd, &f);
  898. continue;
  899. }
  900. switch(f.type++){
  901. default:
  902. f.type = Rerror;
  903. f.ename = "unknown type";
  904. break;
  905. case Tflush:
  906. flushreq(f.oldtag);
  907. break;
  908. case Tversion:
  909. if(f.msize > IOHDRSZ+Maxfdata)
  910. f.msize = IOHDRSZ+Maxfdata;
  911. break;
  912. case Tauth:
  913. f.type = Rerror;
  914. f.ename = "authentication not required";
  915. break;
  916. case Tattach:
  917. f.qid = fstab[Qdir].qid;
  918. fid->file = Qdir;
  919. break;
  920. case Twalk:
  921. nfid = nil;
  922. if(f.newfid != f.fid){
  923. nfid = getfid(f.newfid);
  924. if(nfid == nil)
  925. goto nofids;
  926. nfid->file = fid->file;
  927. fid = nfid;
  928. }
  929. for(i=0; i<f.nwname && i<MAXWELEM; i++){
  930. if(fid->file != Qdir){
  931. f.type = Rerror;
  932. f.ename = Enotdir;
  933. break;
  934. }
  935. if(strcmp(f.wname[i], "..") == 0){
  936. wqid[i] = fstab[Qdir].qid;
  937. continue;
  938. }
  939. if(strcmp(f.wname[i], "cpunote") != 0){
  940. if(i == 0){
  941. f.type = Rerror;
  942. f.ename = "file does not exist";
  943. }
  944. break;
  945. }
  946. fid->file = Qcpunote;
  947. wqid[i] = fstab[Qcpunote].qid;
  948. }
  949. if(nfid != nil && (f.type == Rerror || i < f.nwname))
  950. nfid ->file = -1;
  951. if(f.type != Rerror){
  952. f.nwqid = i;
  953. for(i=0; i<f.nwqid; i++)
  954. f.wqid[i] = wqid[i];
  955. }
  956. break;
  957. case Topen:
  958. if(f.mode != OREAD){
  959. f.type = Rerror;
  960. f.ename = Eperm;
  961. break;
  962. }
  963. fid->omode = f.mode;
  964. if(fid->file == Qcpunote)
  965. ncpunote++;
  966. f.qid = fstab[fid->file].qid;
  967. f.iounit = 0;
  968. break;
  969. case Tread:
  970. if(fsread(fd, fid, &f) < 0)
  971. goto err;
  972. doreply = 0;
  973. break;
  974. case Tclunk:
  975. if(fid->omode != -1 && fid->file == Qcpunote){
  976. ncpunote--;
  977. if(ncpunote == 0) /* remote side is done */
  978. goto err;
  979. }
  980. fid->file = -1;
  981. fid->omode = -1;
  982. break;
  983. case Tstat:
  984. if(fsstat(fd, fid, &f) < 0)
  985. goto err;
  986. doreply = 0;
  987. break;
  988. case Tcreate:
  989. case Twrite:
  990. case Tremove:
  991. case Twstat:
  992. f.type = Rerror;
  993. f.ename = Eperm;
  994. break;
  995. }
  996. if(doreply)
  997. if(fsreply(fd, &f) < 0)
  998. break;
  999. }
  1000. err:
  1001. if(dbg)
  1002. fprint(2, "notefs exiting: %r\n");
  1003. werrstr("success");
  1004. postnote(PNGROUP, exportpid, "kill");
  1005. if(dbg)
  1006. fprint(2, "postnote PNGROUP %d: %r\n", exportpid);
  1007. close(fd);
  1008. }
  1009. char notebuf[ERRMAX];
  1010. void
  1011. catcher(void*, char *text)
  1012. {
  1013. int n;
  1014. n = strlen(text);
  1015. if(n >= sizeof(notebuf))
  1016. n = sizeof(notebuf)-1;
  1017. memmove(notebuf, text, n);
  1018. notebuf[n] = '\0';
  1019. noted(NCONT);
  1020. }
  1021. /*
  1022. * mount in /dev a note file for the remote side to read.
  1023. */
  1024. void
  1025. lclnoteproc(int netfd)
  1026. {
  1027. Waitmsg *w;
  1028. Note *np;
  1029. int pfd[2];
  1030. int pid;
  1031. if(pipe(pfd) < 0){
  1032. fprint(2, "cpu: can't start note proc: pipe: %r\n");
  1033. return;
  1034. }
  1035. /* new proc mounts and returns to start exportfs */
  1036. switch(pid = rfork(RFPROC|RFNAMEG|RFFDG|RFMEM)){
  1037. default:
  1038. exportpid = pid;
  1039. break;
  1040. case -1:
  1041. fprint(2, "cpu: can't start note proc: rfork: %r\n");
  1042. return;
  1043. case 0:
  1044. close(pfd[0]);
  1045. if(mount(pfd[1], -1, "/dev", MBEFORE, "") < 0)
  1046. fprint(2, "cpu: can't mount note proc: %r\n");
  1047. close(pfd[1]);
  1048. return;
  1049. }
  1050. close(netfd);
  1051. close(pfd[1]);
  1052. /* new proc listens for note file system rpc's */
  1053. switch(rfork(RFPROC|RFNAMEG|RFMEM)){
  1054. case -1:
  1055. fprint(2, "cpu: can't start note proc: rfork1: %r\n");
  1056. _exits(0);
  1057. case 0:
  1058. notefs(pfd[0]);
  1059. _exits(0);
  1060. }
  1061. /* original proc waits for notes */
  1062. notify(catcher);
  1063. w = nil;
  1064. for(;;) {
  1065. *notebuf = 0;
  1066. free(w);
  1067. w = wait();
  1068. if(w == nil) {
  1069. if(*notebuf == 0)
  1070. break;
  1071. np = mallocz(sizeof(Note), 1);
  1072. if(np != nil){
  1073. strcpy(np->msg, notebuf);
  1074. lock(&nfs);
  1075. if(nfs.nfirst == nil)
  1076. nfs.nfirst = np;
  1077. else
  1078. nfs.nlast->next = np;
  1079. nfs.nlast = np;
  1080. unlock(&nfs);
  1081. kick(pfd[0]);
  1082. }
  1083. unlock(&nfs);
  1084. } else if(w->pid == exportpid)
  1085. break;
  1086. }
  1087. if(w == nil)
  1088. exits(nil);
  1089. exits(0);
  1090. /* exits(w->msg); */
  1091. }