cpu-bl.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. /*
  2. * cpu.c - Make a connection to a cpu server
  3. *
  4. * Invoked by listen as 'cpu -R | -N service net netdir'
  5. * by users as 'cpu [-h system] [-c cmd args ...]'
  6. */
  7. #include <u.h>
  8. #include <libc.h>
  9. #include <auth.h>
  10. #include <fcall.h>
  11. #include <authsrv.h>
  12. #include <libsec.h>
  13. #include "args.h"
  14. #include "drawterm.h"
  15. #define Maxfdata 8192
  16. #define MaxStr 128
  17. static void fatal(int, char*, ...);
  18. static void usage(void);
  19. static void writestr(int, char*, char*, int);
  20. static int readstr(int, char*, int);
  21. static char *rexcall(int*, char*, char*);
  22. static char *keyspec = "";
  23. static AuthInfo *p9any(int);
  24. #define system csystem
  25. static char *system;
  26. static int cflag;
  27. extern int dbg;
  28. static char *srvname = "ncpu";
  29. static char *ealgs = "rc4_256 sha1";
  30. /* message size for exportfs; may be larger so we can do big graphics in CPU window */
  31. static int msgsize = Maxfdata+IOHDRSZ;
  32. /* authentication mechanisms */
  33. static int netkeyauth(int);
  34. static int netkeysrvauth(int, char*);
  35. static int p9auth(int);
  36. static int srvp9auth(int, char*);
  37. char *authserver;
  38. typedef struct AuthMethod AuthMethod;
  39. struct AuthMethod {
  40. char *name; /* name of method */
  41. int (*cf)(int); /* client side authentication */
  42. int (*sf)(int, char*); /* server side authentication */
  43. } authmethod[] =
  44. {
  45. { "p9", p9auth, srvp9auth,},
  46. { "netkey", netkeyauth, netkeysrvauth,},
  47. // { "none", noauth, srvnoauth,},
  48. { nil, nil}
  49. };
  50. AuthMethod *am = authmethod; /* default is p9 */
  51. char *p9authproto = "p9any";
  52. int setam(char*);
  53. void
  54. exits(char *s)
  55. {
  56. print("\ngoodbye\n");
  57. for(;;) osyield();
  58. }
  59. void
  60. usage(void)
  61. {
  62. fprint(2, "usage: drawterm [-a authserver] [-c cpuserver] [-s secstore] [-u user]\n");
  63. exits("usage");
  64. }
  65. int fdd;
  66. int
  67. mountfactotum(void)
  68. {
  69. int fd;
  70. if((fd = dialfactotum()) < 0)
  71. return -1;
  72. if(sysmount(fd, -1, "/mnt/factotum", MREPL, "") < 0){
  73. fprint(2, "mount factotum: %r\n");
  74. return -1;
  75. }
  76. if((fd = open("/mnt/factotum/ctl", OREAD)) < 0){
  77. fprint(2, "open /mnt/factotum/ctl: %r\n");
  78. return -1;
  79. }
  80. close(fd);
  81. return 0;
  82. }
  83. void
  84. cpumain(int argc, char **argv)
  85. {
  86. char dat[MaxStr], buf[MaxStr], cmd[MaxStr], *err, *secstoreserver, *p, *s;
  87. int fd, ms, data;
  88. /* see if we should use a larger message size */
  89. fd = open("/dev/draw", OREAD);
  90. if(fd > 0){
  91. ms = iounit(fd);
  92. if(msgsize < ms+IOHDRSZ)
  93. msgsize = ms+IOHDRSZ;
  94. close(fd);
  95. }
  96. user = getenv("USER");
  97. if(user == nil)
  98. user = readcons("user", nil, 0);
  99. secstoreserver = nil;
  100. authserver = getenv("auth");
  101. if(authserver == nil)
  102. authserver = "lookout.cs.bell-labs.com";
  103. system = getenv("cpu");
  104. if(system == nil)
  105. system = "anna.cs.bell-labs.com";
  106. ARGBEGIN{
  107. case 'a':
  108. authserver = EARGF(usage());
  109. break;
  110. case 'c':
  111. system = EARGF(usage());
  112. break;
  113. case 'C':
  114. cflag++;
  115. cmd[0] = '!';
  116. cmd[1] = '\0';
  117. while((p = ARGF()) != nil) {
  118. strcat(cmd, " ");
  119. strcat(cmd, p);
  120. }
  121. break;
  122. case 'd':
  123. dbg++;
  124. break;
  125. case 'e':
  126. ealgs = EARGF(usage());
  127. if(*ealgs == 0 || strcmp(ealgs, "clear") == 0)
  128. ealgs = nil;
  129. break;
  130. case 'k':
  131. keyspec = EARGF(usage());
  132. break;
  133. case 'o':
  134. authserver = "plan9.bell-labs.com";
  135. system = "plan9.bell-labs.com";
  136. break;
  137. case 's':
  138. secstoreserver = EARGF(usage());
  139. break;
  140. case 'u':
  141. user = EARGF(usage());
  142. break;
  143. default:
  144. usage();
  145. }ARGEND;
  146. if(argc != 0)
  147. usage();
  148. if(mountfactotum() < 0){
  149. if(secstoreserver == nil)
  150. secstoreserver = authserver;
  151. if(havesecstore(secstoreserver, user)){
  152. s = secstorefetch(secstoreserver, user, nil);
  153. if(s){
  154. if(strlen(s) >= sizeof secstorebuf)
  155. sysfatal("secstore data too big");
  156. strcpy(secstorebuf, s);
  157. }
  158. }
  159. }
  160. if((err = rexcall(&data, system, srvname)))
  161. fatal(1, "%s: %s", err, system);
  162. /* Tell the remote side the command to execute and where our working directory is */
  163. if(cflag)
  164. writestr(data, cmd, "command", 0);
  165. if(getcwd(dat, sizeof(dat)) == 0)
  166. writestr(data, "NO", "dir", 0);
  167. else
  168. writestr(data, dat, "dir", 0);
  169. /*
  170. * Wait for the other end to execute and start our file service
  171. * of /mnt/term
  172. */
  173. if(readstr(data, buf, sizeof(buf)) < 0)
  174. fatal(1, "waiting for FS: %r");
  175. if(strncmp("FS", buf, 2) != 0) {
  176. print("remote cpu: %s", buf);
  177. exits(buf);
  178. }
  179. if(readstr(data, buf, sizeof buf) < 0)
  180. fatal(1, "waiting for remote export: %r");
  181. if(strcmp(buf, "/") != 0){
  182. print("remote cpu: %s" , buf);
  183. exits(buf);
  184. }
  185. write(data, "OK", 2);
  186. /* Begin serving the gnot namespace */
  187. exportfs(data, msgsize);
  188. fatal(1, "starting exportfs");
  189. }
  190. void
  191. fatal(int syserr, char *fmt, ...)
  192. {
  193. Fmt f;
  194. char *str;
  195. va_list arg;
  196. fmtstrinit(&f);
  197. fmtprint(&f, "cpu: ");
  198. va_start(arg, fmt);
  199. fmtvprint(&f, fmt, arg);
  200. va_end(arg);
  201. if(syserr)
  202. fmtprint(&f, ": %r");
  203. fmtprint(&f, "\n");
  204. str = fmtstrflush(&f);
  205. write(2, str, strlen(str));
  206. exits(str);
  207. }
  208. char *negstr = "negotiating authentication method";
  209. char bug[256];
  210. char*
  211. rexcall(int *fd, char *host, char *service)
  212. {
  213. char *na;
  214. char dir[MaxStr];
  215. char err[ERRMAX];
  216. char msg[MaxStr];
  217. int n;
  218. na = netmkaddr(host, "tcp", "17010");
  219. if((*fd = dial(na, 0, dir, 0)) < 0)
  220. return "can't dial";
  221. /* negotiate authentication mechanism */
  222. if(ealgs != nil)
  223. snprint(msg, sizeof(msg), "%s %s", am->name, ealgs);
  224. else
  225. snprint(msg, sizeof(msg), "%s", am->name);
  226. writestr(*fd, msg, negstr, 0);
  227. n = readstr(*fd, err, sizeof err);
  228. if(n < 0)
  229. return negstr;
  230. if(*err){
  231. werrstr(err);
  232. return negstr;
  233. }
  234. /* authenticate */
  235. *fd = (*am->cf)(*fd);
  236. if(*fd < 0)
  237. return "can't authenticate";
  238. return 0;
  239. }
  240. void
  241. writestr(int fd, char *str, char *thing, int ignore)
  242. {
  243. int l, n;
  244. l = strlen(str);
  245. n = write(fd, str, l+1);
  246. if(!ignore && n < 0)
  247. fatal(1, "writing network: %s", thing);
  248. }
  249. int
  250. readstr(int fd, char *str, int len)
  251. {
  252. int n;
  253. while(len) {
  254. n = read(fd, str, 1);
  255. if(n < 0)
  256. return -1;
  257. if(*str == '\0')
  258. return 0;
  259. str++;
  260. len--;
  261. }
  262. return -1;
  263. }
  264. static int
  265. readln(char *buf, int n)
  266. {
  267. int i;
  268. char *p;
  269. n--; /* room for \0 */
  270. p = buf;
  271. for(i=0; i<n; i++){
  272. if(read(0, p, 1) != 1)
  273. break;
  274. if(*p == '\n' || *p == '\r')
  275. break;
  276. p++;
  277. }
  278. *p = '\0';
  279. return p-buf;
  280. }
  281. /*
  282. * user level challenge/response
  283. */
  284. static int
  285. netkeyauth(int fd)
  286. {
  287. char chall[32];
  288. char resp[32];
  289. strecpy(chall, chall+sizeof chall, getuser());
  290. print("user[%s]: ", chall);
  291. if(readln(resp, sizeof(resp)) < 0)
  292. return -1;
  293. if(*resp != 0)
  294. strcpy(chall, resp);
  295. writestr(fd, chall, "challenge/response", 1);
  296. for(;;){
  297. if(readstr(fd, chall, sizeof chall) < 0)
  298. break;
  299. if(*chall == 0)
  300. return fd;
  301. print("challenge: %s\nresponse: ", chall);
  302. if(readln(resp, sizeof(resp)) < 0)
  303. break;
  304. writestr(fd, resp, "challenge/response", 1);
  305. }
  306. return -1;
  307. }
  308. static int
  309. netkeysrvauth(int fd, char *user)
  310. {
  311. return -1;
  312. }
  313. static void
  314. mksecret(char *t, uchar *f)
  315. {
  316. sprint(t, "%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux",
  317. f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7], f[8], f[9]);
  318. }
  319. /*
  320. * plan9 authentication followed by rc4 encryption
  321. */
  322. static int
  323. p9auth(int fd)
  324. {
  325. uchar key[16];
  326. uchar digest[SHA1dlen];
  327. char fromclientsecret[21];
  328. char fromserversecret[21];
  329. int i;
  330. AuthInfo *ai;
  331. ai = p9any(fd);
  332. if(ai == nil)
  333. return -1;
  334. memmove(key+4, ai->secret, ai->nsecret);
  335. if(ealgs == nil)
  336. return fd;
  337. /* exchange random numbers */
  338. for(i = 0; i < 4; i++)
  339. key[i] = fastrand();
  340. if(write(fd, key, 4) != 4)
  341. return -1;
  342. if(readn(fd, key+12, 4) != 4)
  343. return -1;
  344. /* scramble into two secrets */
  345. sha1(key, sizeof(key), digest, nil);
  346. mksecret(fromclientsecret, digest);
  347. mksecret(fromserversecret, digest+10);
  348. /* set up encryption */
  349. i = pushssl(fd, ealgs, fromclientsecret, fromserversecret, nil);
  350. if(i < 0)
  351. werrstr("can't establish ssl connection: %r");
  352. return i;
  353. }
  354. int
  355. authdial(char *net, char *dom)
  356. {
  357. int fd;
  358. fd = dial(netmkaddr(authserver, "tcp", "567"), 0, 0, 0);
  359. //print("authdial %d\n", fd);
  360. return fd;
  361. }
  362. static int
  363. getastickets(Ticketreq *tr, char *trbuf, char *tbuf)
  364. {
  365. int asfd, rv;
  366. char *dom;
  367. dom = tr->authdom;
  368. asfd = authdial(nil, dom);
  369. if(asfd < 0)
  370. return -1;
  371. rv = _asgetticket(asfd, trbuf, tbuf);
  372. close(asfd);
  373. return rv;
  374. }
  375. static int
  376. mkserverticket(Ticketreq *tr, char *authkey, char *tbuf)
  377. {
  378. int i;
  379. Ticket t;
  380. if(strcmp(tr->authid, tr->hostid) != 0)
  381. return -1;
  382. memset(&t, 0, sizeof(t));
  383. memmove(t.chal, tr->chal, CHALLEN);
  384. strcpy(t.cuid, tr->uid);
  385. strcpy(t.suid, tr->uid);
  386. for(i=0; i<DESKEYLEN; i++)
  387. t.key[i] = fastrand();
  388. t.num = AuthTc;
  389. convT2M(&t, tbuf, authkey);
  390. t.num = AuthTs;
  391. convT2M(&t, tbuf+TICKETLEN, authkey);
  392. return 0;
  393. }
  394. static int
  395. gettickets(Ticketreq *tr, char *key, char *trbuf, char *tbuf)
  396. {
  397. if(getastickets(tr, trbuf, tbuf) >= 0)
  398. return 0;
  399. return mkserverticket(tr, key, tbuf);
  400. }
  401. /*
  402. * prompt user for a key. don't care about memory leaks, runs standalone
  403. */
  404. static Attr*
  405. promptforkey(char *params)
  406. {
  407. char *v;
  408. int fd;
  409. Attr *a, *attr;
  410. char *def;
  411. fd = open("/dev/cons", ORDWR);
  412. if(fd < 0)
  413. sysfatal("opening /dev/cons: %r");
  414. attr = _parseattr(params);
  415. fprint(fd, "\n!Adding key:");
  416. for(a=attr; a; a=a->next)
  417. if(a->type != AttrQuery && a->name[0] != '!')
  418. fprint(fd, " %q=%q", a->name, a->val);
  419. fprint(fd, "\n");
  420. for(a=attr; a; a=a->next){
  421. v = a->name;
  422. if(a->type != AttrQuery || v[0]=='!')
  423. continue;
  424. def = nil;
  425. if(strcmp(v, "user") == 0)
  426. def = getuser();
  427. a->val = readcons(v, def, 0);
  428. if(a->val == nil)
  429. sysfatal("user terminated key input");
  430. a->type = AttrNameval;
  431. }
  432. for(a=attr; a; a=a->next){
  433. v = a->name;
  434. if(a->type != AttrQuery || v[0]!='!')
  435. continue;
  436. def = nil;
  437. if(strcmp(v+1, "user") == 0)
  438. def = getuser();
  439. a->val = readcons(v+1, def, 1);
  440. if(a->val == nil)
  441. sysfatal("user terminated key input");
  442. a->type = AttrNameval;
  443. }
  444. fprint(fd, "!\n");
  445. close(fd);
  446. return attr;
  447. }
  448. /*
  449. * send a key to the mounted factotum
  450. */
  451. static int
  452. sendkey(Attr *attr)
  453. {
  454. int fd, rv;
  455. char buf[1024];
  456. fd = open("/mnt/factotum/ctl", ORDWR);
  457. if(fd < 0)
  458. sysfatal("opening /mnt/factotum/ctl: %r");
  459. rv = fprint(fd, "key %A\n", attr);
  460. read(fd, buf, sizeof buf);
  461. close(fd);
  462. return rv;
  463. }
  464. int
  465. askuser(char *params)
  466. {
  467. Attr *attr;
  468. fmtinstall('A', _attrfmt);
  469. attr = promptforkey(params);
  470. if(attr == nil)
  471. sysfatal("no key supplied");
  472. if(sendkey(attr) < 0)
  473. sysfatal("sending key to factotum: %r");
  474. return 0;
  475. }
  476. AuthInfo*
  477. p9anyfactotum(int fd, int afd)
  478. {
  479. return auth_proxy(fd, askuser, "proto=p9any role=client %s", keyspec);
  480. }
  481. AuthInfo*
  482. p9any(int fd)
  483. {
  484. char buf[1024], buf2[1024], cchal[CHALLEN], *bbuf, *p, *dom, *u;
  485. char *pass;
  486. char tbuf[TICKETLEN+TICKETLEN+AUTHENTLEN], trbuf[TICKREQLEN];
  487. char authkey[DESKEYLEN];
  488. Authenticator auth;
  489. int afd, i, v2;
  490. Ticketreq tr;
  491. Ticket t;
  492. AuthInfo *ai;
  493. if((afd = open("/mnt/factotum/ctl", ORDWR)) >= 0)
  494. return p9anyfactotum(fd, afd);
  495. if(readstr(fd, buf, sizeof buf) < 0)
  496. fatal(1, "cannot read p9any negotiation");
  497. bbuf = buf;
  498. v2 = 0;
  499. if(strncmp(buf, "v.2 ", 4) == 0){
  500. v2 = 1;
  501. bbuf += 4;
  502. }
  503. if((p = strchr(bbuf, ' ')))
  504. *p = 0;
  505. p = bbuf;
  506. if((dom = strchr(p, '@')) == nil)
  507. fatal(1, "bad p9any domain");
  508. *dom++ = 0;
  509. if(strcmp(p, "p9sk1") != 0)
  510. fatal(1, "server did not offer p9sk1");
  511. sprint(buf2, "%s %s", p, dom);
  512. if(write(fd, buf2, strlen(buf2)+1) != strlen(buf2)+1)
  513. fatal(1, "cannot write user/domain choice in p9any");
  514. if(v2){
  515. if(readstr(fd, buf, sizeof buf) != 3)
  516. fatal(1, "cannot read OK in p9any");
  517. if(memcmp(buf, "OK\0", 3) != 0)
  518. fatal(1, "did not get OK in p9any");
  519. }
  520. for(i=0; i<CHALLEN; i++)
  521. cchal[i] = fastrand();
  522. if(write(fd, cchal, 8) != 8)
  523. fatal(1, "cannot write p9sk1 challenge");
  524. if(readn(fd, trbuf, TICKREQLEN) != TICKREQLEN)
  525. fatal(1, "cannot read ticket request in p9sk1");
  526. convM2TR(trbuf, &tr);
  527. u = user;
  528. pass = findkey(&u, tr.authdom);
  529. if(pass == nil)
  530. again:
  531. pass = getkey(u, tr.authdom);
  532. if(pass == nil)
  533. fatal(1, "no password");
  534. passtokey(authkey, pass);
  535. memset(pass, 0, strlen(pass));
  536. tr.type = AuthTreq;
  537. strecpy(tr.hostid, tr.hostid+sizeof tr.hostid, u);
  538. strecpy(tr.uid, tr.uid+sizeof tr.uid, u);
  539. convTR2M(&tr, trbuf);
  540. if(gettickets(&tr, authkey, trbuf, tbuf) < 0)
  541. fatal(1, "cannot get auth tickets in p9sk1");
  542. convM2T(tbuf, &t, authkey);
  543. if(t.num != AuthTc){
  544. print("?password mismatch with auth server\n");
  545. goto again;
  546. }
  547. memmove(tbuf, tbuf+TICKETLEN, TICKETLEN);
  548. auth.num = AuthAc;
  549. memmove(auth.chal, tr.chal, CHALLEN);
  550. auth.id = 0;
  551. convA2M(&auth, tbuf+TICKETLEN, t.key);
  552. if(write(fd, tbuf, TICKETLEN+AUTHENTLEN) != TICKETLEN+AUTHENTLEN)
  553. fatal(1, "cannot send ticket and authenticator back in p9sk1");
  554. if(readn(fd, tbuf, AUTHENTLEN) != AUTHENTLEN)
  555. fatal(1, "cannot read authenticator in p9sk1");
  556. convM2A(tbuf, &auth, t.key);
  557. if(auth.num != AuthAs
  558. || memcmp(auth.chal, cchal, CHALLEN) != 0
  559. || auth.id != 0){
  560. print("?you and auth server agree about password.\n");
  561. print("?server is confused.\n");
  562. fatal(1, "server lies got %llux.%d want %llux.%d", *(vlong*)auth.chal, auth.id, *(vlong*)cchal, 0);
  563. }
  564. //print("i am %s there.\n", t.suid);
  565. ai = mallocz(sizeof(AuthInfo), 1);
  566. ai->secret = mallocz(8, 1);
  567. des56to64((uchar*)t.key, ai->secret);
  568. ai->nsecret = 8;
  569. ai->suid = strdup(t.suid);
  570. ai->cuid = strdup(t.cuid);
  571. memset(authkey, 0, sizeof authkey);
  572. return ai;
  573. }
  574. /*
  575. static int
  576. noauth(int fd)
  577. {
  578. ealgs = nil;
  579. return fd;
  580. }
  581. static int
  582. srvnoauth(int fd, char *user)
  583. {
  584. strecpy(user, user+MaxStr, getuser());
  585. ealgs = nil;
  586. return fd;
  587. }
  588. */
  589. void
  590. loghex(uchar *p, int n)
  591. {
  592. char buf[100];
  593. int i;
  594. for(i = 0; i < n; i++)
  595. sprint(buf+2*i, "%2.2ux", p[i]);
  596. // syslog(0, "cpu", buf);
  597. }
  598. static int
  599. srvp9auth(int fd, char *user)
  600. {
  601. return -1;
  602. }
  603. /*
  604. * set authentication mechanism
  605. */
  606. int
  607. setam(char *name)
  608. {
  609. for(am = authmethod; am->name != nil; am++)
  610. if(strcmp(am->name, name) == 0)
  611. return 0;
  612. am = authmethod;
  613. return -1;
  614. }
  615. /*
  616. * set authentication mechanism and encryption/hash algs
  617. *
  618. int
  619. setamalg(char *s)
  620. {
  621. ealgs = strchr(s, ' ');
  622. if(ealgs != nil)
  623. *ealgs++ = 0;
  624. return setam(s);
  625. }
  626. */