cpu.c 14 KB

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