cpu.c 22 KB

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