cpu.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226
  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 *c, 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 aint32_t 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, "", 'M') < 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. /*
  557. * these two functions may lead to a security hole and should only be enabled
  558. * for new ports.
  559. */
  560. #if 0
  561. static int
  562. noauth(int fd)
  563. {
  564. ealgs = nil;
  565. return fd;
  566. }
  567. static int
  568. srvnoauth(int fd, char *user)
  569. {
  570. strecpy(user, user+MaxStr, getuser());
  571. ealgs = nil;
  572. newns(user, nil);
  573. return fd;
  574. }
  575. #endif
  576. void
  577. loghex(uint8_t *p, int n)
  578. {
  579. char buf[100];
  580. int i;
  581. for(i = 0; i < n; i++)
  582. sprint(buf+2*i, "%2.2ux", p[i]);
  583. syslog(0, "cpu", buf);
  584. }
  585. static int
  586. srvp9auth(int fd, char *user)
  587. {
  588. uint8_t key[16];
  589. uint8_t digest[SHA1dlen];
  590. char fromclientsecret[21];
  591. char fromserversecret[21];
  592. int i;
  593. AuthInfo *ai;
  594. ai = auth_proxy(0, nil, "proto=%q role=server %s", p9authproto, keyspec);
  595. if(ai == nil)
  596. return -1;
  597. if(auth_chuid(ai, nil) < 0)
  598. return -1;
  599. strecpy(user, user+MaxStr, ai->cuid);
  600. memmove(key+4, ai->secret, ai->nsecret);
  601. if(ealgs == nil)
  602. return fd;
  603. /* exchange random numbers */
  604. srand(truerand());
  605. for(i = 0; i < 4; i++)
  606. key[i+12] = rand();
  607. if(readn(fd, key, 4) != 4)
  608. return -1;
  609. if(write(fd, key+12, 4) != 4)
  610. return -1;
  611. /* scramble into two secrets */
  612. sha1(key, sizeof(key), digest, nil);
  613. mksecret(fromclientsecret, digest);
  614. mksecret(fromserversecret, digest+10);
  615. /* set up encryption */
  616. i = pushssl(fd, ealgs, fromserversecret, fromclientsecret, nil);
  617. if(i < 0)
  618. werrstr("can't establish ssl connection: %r");
  619. return i;
  620. }
  621. /*
  622. * set authentication mechanism
  623. */
  624. int
  625. setam(char *name)
  626. {
  627. for(am = authmethod; am->name != nil; am++)
  628. if(strcmp(am->name, name) == 0)
  629. return 0;
  630. am = authmethod;
  631. return -1;
  632. }
  633. /*
  634. * set authentication mechanism and encryption/hash algs
  635. */
  636. int
  637. setamalg(char *s)
  638. {
  639. ealgs = strchr(s, ' ');
  640. if(ealgs != nil)
  641. *ealgs++ = 0;
  642. return setam(s);
  643. }
  644. char *rmtnotefile = "/mnt/term/dev/cpunote";
  645. /*
  646. * loop reading /mnt/term/dev/note looking for notes.
  647. * The child returns to start the shell.
  648. */
  649. void
  650. rmtnoteproc(void)
  651. {
  652. int n, fd, pid, notepid;
  653. char buf[256];
  654. /* new proc returns to start shell */
  655. pid = rfork(RFPROC|RFFDG|RFNOTEG|RFNAMEG|RFMEM);
  656. switch(pid){
  657. case -1:
  658. syslog(0, "cpu", "cpu -R: can't start noteproc: %r");
  659. return;
  660. case 0:
  661. return;
  662. }
  663. /* new proc reads notes from other side and posts them to shell */
  664. switch(notepid = rfork(RFPROC|RFFDG|RFMEM)){
  665. case -1:
  666. syslog(0, "cpu", "cpu -R: can't start wait proc: %r");
  667. _exits(0);
  668. case 0:
  669. fd = open(rmtnotefile, OREAD);
  670. if(fd < 0){
  671. syslog(0, "cpu", "cpu -R: can't open %s", rmtnotefile);
  672. _exits(0);
  673. }
  674. for(;;){
  675. n = read(fd, buf, sizeof(buf)-1);
  676. if(n <= 0){
  677. postnote(PNGROUP, pid, "hangup");
  678. _exits(0);
  679. }
  680. buf[n] = 0;
  681. postnote(PNGROUP, pid, buf);
  682. }
  683. }
  684. /* original proc waits for shell proc to die and kills note proc */
  685. for(;;){
  686. n = waitpid();
  687. if(n < 0 || n == pid)
  688. break;
  689. }
  690. postnote(PNPROC, notepid, "kill");
  691. _exits(0);
  692. }
  693. enum
  694. {
  695. Qdir,
  696. Qcpunote,
  697. Nfid = 32,
  698. };
  699. struct {
  700. char *name;
  701. Qid qid;
  702. uint32_t perm;
  703. } fstab[] =
  704. {
  705. [Qdir] { ".", {Qdir, 0, QTDIR}, DMDIR|0555 },
  706. [Qcpunote] { "cpunote", {Qcpunote, 0}, 0444 },
  707. };
  708. typedef struct Note Note;
  709. struct Note
  710. {
  711. Note *next;
  712. char msg[ERRMAX];
  713. };
  714. typedef struct Request Request;
  715. struct Request
  716. {
  717. Request *next;
  718. Fcall f;
  719. };
  720. typedef struct Fid Fid;
  721. struct Fid
  722. {
  723. int fid;
  724. int file;
  725. int omode;
  726. };
  727. Fid fids[Nfid];
  728. struct {
  729. Lock;
  730. Note *nfirst, *nlast;
  731. Request *rfirst, *rlast;
  732. } nfs;
  733. int
  734. fsreply(int fd, Fcall *f)
  735. {
  736. uint8_t buf[IOHDRSZ+Maxfdata];
  737. int n;
  738. if(dbg)
  739. fprint(2, "notefs: <-%F\n", f);
  740. n = convS2M(f, buf, sizeof buf);
  741. if(n > 0){
  742. if(write(fd, buf, n) != n){
  743. close(fd);
  744. return -1;
  745. }
  746. }
  747. return 0;
  748. }
  749. /* match a note read request with a note, reply to the request */
  750. int
  751. kick(int fd)
  752. {
  753. Request *rp;
  754. Note *np;
  755. int rv;
  756. for(;;){
  757. lock(&nfs);
  758. rp = nfs.rfirst;
  759. np = nfs.nfirst;
  760. if(rp == nil || np == nil){
  761. unlock(&nfs);
  762. break;
  763. }
  764. nfs.rfirst = rp->next;
  765. nfs.nfirst = np->next;
  766. unlock(&nfs);
  767. rp->f.type = Rread;
  768. rp->f.count = strlen(np->msg);
  769. rp->f.data = np->msg;
  770. rv = fsreply(fd, &rp->f);
  771. free(rp);
  772. free(np);
  773. if(rv < 0)
  774. return -1;
  775. }
  776. return 0;
  777. }
  778. void
  779. flushreq(int tag)
  780. {
  781. Request **l, *rp;
  782. lock(&nfs);
  783. for(l = &nfs.rfirst; *l != nil; l = &(*l)->next){
  784. rp = *l;
  785. if(rp->f.tag == tag){
  786. *l = rp->next;
  787. unlock(&nfs);
  788. free(rp);
  789. return;
  790. }
  791. }
  792. unlock(&nfs);
  793. }
  794. Fid*
  795. getfid(int fid)
  796. {
  797. int i, freefid;
  798. freefid = -1;
  799. for(i = 0; i < Nfid; i++){
  800. if(freefid < 0 && fids[i].file < 0)
  801. freefid = i;
  802. if(fids[i].fid == fid)
  803. return &fids[i];
  804. }
  805. if(freefid >= 0){
  806. fids[freefid].fid = fid;
  807. return &fids[freefid];
  808. }
  809. return nil;
  810. }
  811. int
  812. fsstat(int fd, Fid *fid, Fcall *f)
  813. {
  814. Dir d;
  815. uint8_t statbuf[256];
  816. memset(&d, 0, sizeof(d));
  817. d.name = fstab[fid->file].name;
  818. d.uid = user;
  819. d.gid = user;
  820. d.muid = user;
  821. d.qid = fstab[fid->file].qid;
  822. d.mode = fstab[fid->file].perm;
  823. d.atime = d.mtime = time(0);
  824. f->stat = statbuf;
  825. f->nstat = convD2M(&d, statbuf, sizeof statbuf);
  826. return fsreply(fd, f);
  827. }
  828. int
  829. fsread(int fd, Fid *fid, Fcall *f)
  830. {
  831. Dir d;
  832. uint8_t buf[256];
  833. Request *rp;
  834. switch(fid->file){
  835. default:
  836. return -1;
  837. case Qdir:
  838. if(f->offset == 0 && f->count >0){
  839. memset(&d, 0, sizeof(d));
  840. d.name = fstab[Qcpunote].name;
  841. d.uid = user;
  842. d.gid = user;
  843. d.muid = user;
  844. d.qid = fstab[Qcpunote].qid;
  845. d.mode = fstab[Qcpunote].perm;
  846. d.atime = d.mtime = time(0);
  847. f->count = convD2M(&d, buf, sizeof buf);
  848. f->data = (char*)buf;
  849. } else
  850. f->count = 0;
  851. return fsreply(fd, f);
  852. case Qcpunote:
  853. rp = mallocz(sizeof(*rp), 1);
  854. if(rp == nil)
  855. return -1;
  856. rp->f = *f;
  857. lock(&nfs);
  858. if(nfs.rfirst == nil)
  859. nfs.rfirst = rp;
  860. else
  861. nfs.rlast->next = rp;
  862. nfs.rlast = rp;
  863. unlock(&nfs);
  864. return kick(fd);;
  865. }
  866. }
  867. char Eperm[] = "permission denied";
  868. char Enofile[] = "out of files";
  869. char Enotdir[] = "not a directory";
  870. void
  871. notefs(int fd)
  872. {
  873. uint8_t buf[IOHDRSZ+Maxfdata];
  874. int i, n, ncpunote;
  875. Fcall f;
  876. Qid wqid[MAXWELEM];
  877. Fid *fid, *nfid;
  878. int doreply;
  879. rfork(RFNOTEG);
  880. fmtinstall('F', fcallfmt);
  881. for(n = 0; n < Nfid; n++){
  882. fids[n].file = -1;
  883. fids[n].omode = -1;
  884. }
  885. ncpunote = 0;
  886. for(;;){
  887. n = read9pmsg(fd, buf, sizeof(buf));
  888. if(n <= 0){
  889. if(dbg)
  890. fprint(2, "read9pmsg(%d) returns %d: %r\n", fd, n);
  891. break;
  892. }
  893. if(convM2S(buf, n, &f) <= BIT16SZ)
  894. break;
  895. if(dbg)
  896. fprint(2, "notefs: ->%F\n", &f);
  897. doreply = 1;
  898. fid = getfid(f.fid);
  899. if(fid == nil){
  900. nofids:
  901. f.type = Rerror;
  902. f.ename = Enofile;
  903. fsreply(fd, &f);
  904. continue;
  905. }
  906. switch(f.type++){
  907. default:
  908. f.type = Rerror;
  909. f.ename = "unknown type";
  910. break;
  911. case Tflush:
  912. flushreq(f.oldtag);
  913. break;
  914. case Tversion:
  915. if(f.msize > IOHDRSZ+Maxfdata)
  916. f.msize = IOHDRSZ+Maxfdata;
  917. break;
  918. case Tauth:
  919. f.type = Rerror;
  920. f.ename = "authentication not required";
  921. break;
  922. case Tattach:
  923. f.qid = fstab[Qdir].qid;
  924. fid->file = Qdir;
  925. break;
  926. case Twalk:
  927. nfid = nil;
  928. if(f.newfid != f.fid){
  929. nfid = getfid(f.newfid);
  930. if(nfid == nil)
  931. goto nofids;
  932. nfid->file = fid->file;
  933. fid = nfid;
  934. }
  935. for(i=0; i<f.nwname && i<MAXWELEM; i++){
  936. if(fid->file != Qdir){
  937. f.type = Rerror;
  938. f.ename = Enotdir;
  939. break;
  940. }
  941. if(strcmp(f.wname[i], "..") == 0){
  942. wqid[i] = fstab[Qdir].qid;
  943. continue;
  944. }
  945. if(strcmp(f.wname[i], "cpunote") != 0){
  946. if(i == 0){
  947. f.type = Rerror;
  948. f.ename = "file does not exist";
  949. }
  950. break;
  951. }
  952. fid->file = Qcpunote;
  953. wqid[i] = fstab[Qcpunote].qid;
  954. }
  955. if(nfid != nil && (f.type == Rerror || i < f.nwname))
  956. nfid ->file = -1;
  957. if(f.type != Rerror){
  958. f.nwqid = i;
  959. for(i=0; i<f.nwqid; i++)
  960. f.wqid[i] = wqid[i];
  961. }
  962. break;
  963. case Topen:
  964. if(f.mode != OREAD){
  965. f.type = Rerror;
  966. f.ename = Eperm;
  967. break;
  968. }
  969. fid->omode = f.mode;
  970. if(fid->file == Qcpunote)
  971. ncpunote++;
  972. f.qid = fstab[fid->file].qid;
  973. f.iounit = 0;
  974. break;
  975. case Tread:
  976. if(fsread(fd, fid, &f) < 0)
  977. goto err;
  978. doreply = 0;
  979. break;
  980. case Tclunk:
  981. if(fid->omode != -1 && fid->file == Qcpunote){
  982. ncpunote--;
  983. if(ncpunote == 0) /* remote side is done */
  984. goto err;
  985. }
  986. fid->file = -1;
  987. fid->omode = -1;
  988. break;
  989. case Tstat:
  990. if(fsstat(fd, fid, &f) < 0)
  991. goto err;
  992. doreply = 0;
  993. break;
  994. case Tcreate:
  995. case Twrite:
  996. case Tremove:
  997. case Twstat:
  998. f.type = Rerror;
  999. f.ename = Eperm;
  1000. break;
  1001. }
  1002. if(doreply)
  1003. if(fsreply(fd, &f) < 0)
  1004. break;
  1005. }
  1006. err:
  1007. if(dbg)
  1008. fprint(2, "notefs exiting: %r\n");
  1009. werrstr("success");
  1010. postnote(PNGROUP, exportpid, "kill");
  1011. if(dbg)
  1012. fprint(2, "postnote PNGROUP %d: %r\n", exportpid);
  1013. close(fd);
  1014. }
  1015. char notebuf[ERRMAX];
  1016. void
  1017. catcher(void *v, char *text)
  1018. {
  1019. int n;
  1020. n = strlen(text);
  1021. if(n >= sizeof(notebuf))
  1022. n = sizeof(notebuf)-1;
  1023. memmove(notebuf, text, n);
  1024. notebuf[n] = '\0';
  1025. noted(NCONT);
  1026. }
  1027. /*
  1028. * mount in /dev a note file for the remote side to read.
  1029. */
  1030. void
  1031. lclnoteproc(int netfd)
  1032. {
  1033. Waitmsg *w;
  1034. Note *np;
  1035. int pfd[2];
  1036. int pid;
  1037. if(pipe(pfd) < 0){
  1038. fprint(2, "cpu: can't start note proc: pipe: %r\n");
  1039. return;
  1040. }
  1041. /* new proc mounts and returns to start exportfs */
  1042. switch(pid = rfork(RFPROC|RFNAMEG|RFFDG|RFMEM)){
  1043. default:
  1044. exportpid = pid;
  1045. break;
  1046. case -1:
  1047. fprint(2, "cpu: can't start note proc: rfork: %r\n");
  1048. return;
  1049. case 0:
  1050. close(pfd[0]);
  1051. if(mount(pfd[1], -1, "/dev", MBEFORE, "", 'M') < 0)
  1052. fprint(2, "cpu: can't mount note proc: %r\n");
  1053. close(pfd[1]);
  1054. return;
  1055. }
  1056. close(netfd);
  1057. close(pfd[1]);
  1058. /* new proc listens for note file system rpc's */
  1059. switch(rfork(RFPROC|RFNAMEG|RFMEM)){
  1060. case -1:
  1061. fprint(2, "cpu: can't start note proc: rfork1: %r\n");
  1062. _exits(0);
  1063. case 0:
  1064. notefs(pfd[0]);
  1065. _exits(0);
  1066. }
  1067. /* original proc waits for notes */
  1068. notify(catcher);
  1069. w = nil;
  1070. for(;;) {
  1071. *notebuf = 0;
  1072. free(w);
  1073. w = wait();
  1074. if(w == nil) {
  1075. if(*notebuf == 0)
  1076. break;
  1077. np = mallocz(sizeof(Note), 1);
  1078. if(np != nil){
  1079. strcpy(np->msg, notebuf);
  1080. lock(&nfs);
  1081. if(nfs.nfirst == nil)
  1082. nfs.nfirst = np;
  1083. else
  1084. nfs.nlast->next = np;
  1085. nfs.nlast = np;
  1086. unlock(&nfs);
  1087. kick(pfd[0]);
  1088. }
  1089. unlock(&nfs);
  1090. } else if(w->pid == exportpid)
  1091. break;
  1092. }
  1093. if(w == nil)
  1094. exits(nil);
  1095. exits(0);
  1096. /* exits(w->msg); */
  1097. }