cpu.c 23 KB

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