ssh2.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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. * ssh - remote login via SSH v2
  11. * /net/ssh does most of the work; we copy bytes back and forth
  12. */
  13. #include <u.h>
  14. #include <libc.h>
  15. #include <auth.h>
  16. #include "ssh2.h"
  17. int doauth(int, char *);
  18. int isatty(int);
  19. char *user, *remote;
  20. char *netdir = "/net";
  21. int debug = 0;
  22. static int stripcr = 0;
  23. static int mflag = 0;
  24. static int iflag = -1;
  25. static int nopw = 0, nopka = 0;
  26. static int chpid;
  27. static int reqfd, dfd1, cfd1, dfd2, cfd2, consfd, kconsfd, cctlfd, notefd, keyfd;
  28. void
  29. usage(void)
  30. {
  31. fprint(2, "usage: %s [-dkKmr] [-l user] [-n dir] [-z attr=val] addr "
  32. "[cmd [args]]\n", argv0);
  33. exits("usage");
  34. }
  35. /*
  36. * this is probably overkill except writing "kill" to notefd;
  37. * file descriptors are closed by the kernel upon exit.
  38. */
  39. static void
  40. shutdown(void)
  41. {
  42. if (cctlfd > 0) {
  43. fprint(cctlfd, "rawoff");
  44. close(cctlfd);
  45. }
  46. if (consfd > 0)
  47. close(consfd);
  48. if (reqfd > 0) {
  49. fprint(reqfd, "close");
  50. close(reqfd);
  51. }
  52. close(dfd2);
  53. close(dfd1);
  54. close(cfd2);
  55. close(cfd1);
  56. fprint(notefd, "kill");
  57. close(notefd);
  58. }
  59. static void
  60. bail(char *sts)
  61. {
  62. shutdown();
  63. exits(sts);
  64. }
  65. int
  66. handler(void *, char *s)
  67. {
  68. char *nf;
  69. int fd;
  70. if (strstr(s, "alarm") != nil)
  71. return 0;
  72. if (chpid) {
  73. nf = esmprint("/proc/%d/note", chpid);
  74. fd = open(nf, OWRITE);
  75. fprint(fd, "interrupt");
  76. close(fd);
  77. free(nf);
  78. }
  79. shutdown();
  80. return 1;
  81. }
  82. static void
  83. parseargs(void)
  84. {
  85. int n;
  86. char *p, *q;
  87. q = strchr(remote, '@');
  88. if (q != nil) {
  89. user = remote;
  90. *q++ = 0;
  91. remote = q;
  92. }
  93. q = strchr(remote, '!');
  94. if (q) {
  95. n = q - remote;
  96. netdir = malloc(n+1);
  97. if (netdir == nil)
  98. sysfatal("out of memory");
  99. strncpy(netdir, remote, n+1);
  100. netdir[n] = '\0';
  101. p = strrchr(netdir, '/');
  102. if (p == nil) {
  103. free(netdir);
  104. netdir = "/net";
  105. } else if (strcmp(p+1, "ssh") == 0)
  106. *p = '\0';
  107. else
  108. remote = esmprint("%s/ssh", netdir);
  109. }
  110. }
  111. static int
  112. catcher(void *, char *s)
  113. {
  114. return strstr(s, "alarm") != nil;
  115. }
  116. static int
  117. timedmount(int fd, int afd, char *mntpt, int flag, char *aname)
  118. {
  119. int oalarm, ret;
  120. atnotify(catcher, 1);
  121. oalarm = alarm(5*1000); /* don't get stuck here */
  122. ret = mount(fd, afd, mntpt, flag, aname, 'M');
  123. alarm(oalarm);
  124. atnotify(catcher, 0);
  125. return ret;
  126. }
  127. static void
  128. mounttunnel(char *srv)
  129. {
  130. int fd;
  131. if (debug)
  132. fprint(2, "%s: mounting %s on /net\n", argv0, srv);
  133. fd = open(srv, OREAD);
  134. if (fd < 0) {
  135. if (debug)
  136. fprint(2, "%s: can't open %s: %r\n", argv0, srv);
  137. } else if (timedmount(fd, -1, netdir, MBEFORE, "") < 0) {
  138. fprint(2, "can't mount %s on %s: %r\n", srv, netdir);
  139. close(fd);
  140. }
  141. }
  142. static void
  143. newtunnel(char *myname)
  144. {
  145. int kid, pid;
  146. if(debug)
  147. fprint(2, "%s: starting new netssh for key access\n", argv0);
  148. kid = rfork(RFPROC|RFNOTEG|RFENVG /* |RFFDG */);
  149. if (kid == 0) {
  150. // for (fd = 3; fd < 40; fd++)
  151. // close(fd);
  152. execl("/bin/netssh", "netssh", "-m", netdir, "-s", myname, nil);
  153. sysfatal("no /bin/netssh: %r");
  154. } else if (kid < 0)
  155. sysfatal("fork failed: %r");
  156. while ((pid = waitpid()) != kid && pid >= 0)
  157. ;
  158. }
  159. static void
  160. starttunnel(void)
  161. {
  162. char *keys, *mysrv, *myname;
  163. keys = esmprint("%s/ssh/keys", netdir);
  164. myname = esmprint("ssh.%s", getuser());
  165. mysrv = esmprint("/srv/%s", myname);
  166. if (access(keys, ORDWR) < 0)
  167. mounttunnel("/srv/netssh"); /* old name */
  168. if (access(keys, ORDWR) < 0)
  169. mounttunnel("/srv/ssh");
  170. if (access(keys, ORDWR) < 0)
  171. mounttunnel(mysrv);
  172. if (access(keys, ORDWR) < 0)
  173. newtunnel(myname);
  174. if (access(keys, ORDWR) < 0)
  175. mounttunnel(mysrv);
  176. /* if we *still* can't see our own tunnel, throw a tantrum. */
  177. if (access(keys, ORDWR) < 0)
  178. sysfatal("%s inaccessible: %r", keys); /* WTF? */
  179. free(myname);
  180. free(mysrv);
  181. free(keys);
  182. }
  183. int
  184. cmdmode(void)
  185. {
  186. int n, m;
  187. char buf[Arbbufsz];
  188. for(;;) {
  189. reprompt:
  190. print("\n>>> ");
  191. n = 0;
  192. do {
  193. m = read(0, buf + n, sizeof buf - n - 1);
  194. if (m <= 0)
  195. return 1;
  196. write(1, buf + n, m);
  197. n += m;
  198. buf[n] = '\0';
  199. if (buf[n-1] == ('u' & 037))
  200. goto reprompt;
  201. } while (buf[n-1] != '\n' && buf[n-1] != '\r');
  202. switch (buf[0]) {
  203. case '\n':
  204. case '\r':
  205. break;
  206. case 'q':
  207. return 1;
  208. case 'c':
  209. return 0;
  210. case 'r':
  211. stripcr = !stripcr;
  212. return 0;
  213. case 'h':
  214. print("c - continue\n");
  215. print("h - help\n");
  216. print("q - quit\n");
  217. print("r - toggle carriage return stripping\n");
  218. break;
  219. default:
  220. print("unknown command\n");
  221. break;
  222. }
  223. }
  224. }
  225. static void
  226. keyprompt(char *buf, int size, int n)
  227. {
  228. if (*buf == 'c') {
  229. fprint(kconsfd, "The following key has been offered by the server:\n");
  230. write(kconsfd, buf+5, n);
  231. fprint(kconsfd, "\n\n");
  232. fprint(kconsfd, "Add this key? (yes, no, session) ");
  233. } else {
  234. fprint(kconsfd, "The following key does NOT match the known "
  235. "key(s) for the server:\n");
  236. write(kconsfd, buf+5, n);
  237. fprint(kconsfd, "\n\n");
  238. fprint(kconsfd, "Add this key? (yes, no, session, replace) ");
  239. }
  240. n = read(kconsfd, buf, size - 1);
  241. if (n <= 0)
  242. return;
  243. write(keyfd, buf, n); /* user's response -> /net/ssh/keys */
  244. seek(keyfd, 0, 2);
  245. if (readn(keyfd, buf, 5) <= 0)
  246. return;
  247. buf[5] = 0;
  248. n = strtol(buf+1, nil, 10);
  249. n = readn(keyfd, buf+5, n);
  250. if (n <= 0)
  251. return;
  252. buf[n+5] = 0;
  253. switch (*buf) {
  254. case 'b':
  255. case 'f':
  256. fprint(kconsfd, "%s\n", buf+5);
  257. case 'o':
  258. close(keyfd);
  259. close(kconsfd);
  260. }
  261. }
  262. /* talk the undocumented /net/ssh/keys protocol */
  263. static void
  264. keyproc(char *buf, int size)
  265. {
  266. int n;
  267. char *p;
  268. if (size < 6)
  269. exits("keyproc buffer too small");
  270. p = esmprint("%s/ssh/keys", netdir);
  271. keyfd = open(p, ORDWR);
  272. if (keyfd < 0) {
  273. chpid = 0;
  274. sysfatal("failed to open ssh keys in %s: %r", p);
  275. }
  276. kconsfd = open("/dev/cons", ORDWR);
  277. if (kconsfd < 0)
  278. nopw = 1;
  279. buf[0] = 0;
  280. n = read(keyfd, buf, 5); /* reading /net/ssh/keys */
  281. if (n < 0)
  282. sysfatal("%s read: %r", p);
  283. buf[5] = 0;
  284. n = strtol(buf+1, nil, 10);
  285. n = readn(keyfd, buf+5, n);
  286. buf[n < 0? 5: n+5] = 0;
  287. free(p);
  288. switch (*buf) {
  289. case 'f':
  290. if (kconsfd >= 0)
  291. fprint(kconsfd, "%s\n", buf+5);
  292. /* fall through */
  293. case 'o':
  294. close(keyfd);
  295. if (kconsfd >= 0)
  296. close(kconsfd);
  297. break;
  298. default:
  299. if (kconsfd >= 0)
  300. keyprompt(buf, size, n);
  301. else {
  302. fprint(keyfd, "n");
  303. close(keyfd);
  304. }
  305. break;
  306. }
  307. chpid = 0;
  308. exits(nil);
  309. }
  310. /*
  311. * start a subproc to copy from network to stdout
  312. * while we copy from stdin to network.
  313. */
  314. static void
  315. bidircopy(char *buf, int size)
  316. {
  317. int i, n, lstart;
  318. char *path, *p, *q;
  319. rfork(RFNOTEG);
  320. path = esmprint("/proc/%d/notepg", getpid());
  321. notefd = open(path, OWRITE);
  322. switch (rfork(RFPROC|RFMEM|RFNOWAIT)) {
  323. case 0:
  324. while ((n = read(dfd2, buf, size - 1)) > 0) {
  325. if (!stripcr)
  326. p = buf + n;
  327. else
  328. for (i = 0, p = buf, q = buf; i < n; ++i, ++q)
  329. if (*q != '\r')
  330. *p++ = *q;
  331. if (p != buf)
  332. write(1, buf, p-buf);
  333. }
  334. /*
  335. * don't bother; it will be obvious when the user's prompt
  336. * changes.
  337. *
  338. * fprint(2, "%s: Connection closed by server\n", argv0);
  339. */
  340. break;
  341. default:
  342. lstart = 1;
  343. while ((n = read(0, buf, size - 1)) > 0) {
  344. if (!mflag && lstart && buf[0] == 0x1c)
  345. if (cmdmode())
  346. break;
  347. else
  348. continue;
  349. lstart = (buf[n-1] == '\n' || buf[n-1] == '\r');
  350. write(dfd2, buf, n);
  351. }
  352. /*
  353. * don't bother; it will be obvious when the user's prompt
  354. * changes.
  355. *
  356. * fprint(2, "%s: EOF on client side\n", argv0);
  357. */
  358. break;
  359. case -1:
  360. fprint(2, "%s: fork error: %r\n", argv0);
  361. break;
  362. }
  363. bail(nil);
  364. }
  365. static int
  366. connect(char *buf, int size)
  367. {
  368. int nfd, n;
  369. char *dir, *ds, *nf;
  370. dir = esmprint("%s/ssh", netdir);
  371. ds = netmkaddr(remote, dir, "22"); /* tcp port 22 is ssh */
  372. free(dir);
  373. dfd1 = dial(ds, nil, nil, &cfd1);
  374. if (dfd1 < 0) {
  375. fprint(2, "%s: dial conn %s: %r\n", argv0, ds);
  376. if (chpid) {
  377. nf = esmprint("/proc/%d/note", chpid);
  378. nfd = open(nf, OWRITE);
  379. fprint(nfd, "interrupt");
  380. close(nfd);
  381. }
  382. exits("can't dial");
  383. }
  384. seek(cfd1, 0, 0);
  385. n = read(cfd1, buf, size - 1);
  386. buf[n >= 0? n: 0] = 0;
  387. return atoi(buf);
  388. }
  389. static int
  390. chanconnect(int conn, char *buf, int size)
  391. {
  392. int n;
  393. char *path;
  394. path = esmprint("%s/ssh/%d!session", netdir, conn);
  395. dfd2 = dial(path, nil, nil, &cfd2);
  396. if (dfd2 < 0) {
  397. fprint(2, "%s: dial chan %s: %r\n", argv0, path);
  398. bail("dial");
  399. }
  400. free(path);
  401. n = read(cfd2, buf, size - 1);
  402. buf[n >= 0? n: 0] = 0;
  403. return atoi(buf);
  404. }
  405. static void
  406. remotecmd(int argc, char *argv[], int conn, int chan, char *buf,
  407. int size)
  408. {
  409. int i;
  410. char *path, *q, *ep;
  411. path = esmprint("%s/ssh/%d/%d/request", netdir, conn, chan);
  412. reqfd = open(path, OWRITE);
  413. if (reqfd < 0)
  414. bail("can't open request chan");
  415. if (argc == 0)
  416. if (readfile("/env/TERM", buf, size) < 0)
  417. fprint(reqfd, "shell");
  418. else
  419. fprint(reqfd, "shell %s", buf);
  420. else {
  421. assert(size >= Bigbufsz);
  422. ep = buf + Bigbufsz;
  423. q = seprint(buf, ep, "exec");
  424. for (i = 0; i < argc; ++i)
  425. q = seprint(q, ep, " %q", argv[i]);
  426. if (q >= ep) {
  427. fprint(2, "%s: command too long\n", argv0);
  428. fprint(reqfd, "close");
  429. bail("cmd too long");
  430. }
  431. write(reqfd, buf, q - buf);
  432. }
  433. }
  434. void
  435. main(int argc, char *argv[])
  436. {
  437. char *whichkey;
  438. int conn, chan, n;
  439. char buf[Copybufsz];
  440. quotefmtinstall();
  441. reqfd = dfd1 = cfd1 = dfd2 = cfd2 = consfd = kconsfd = cctlfd =
  442. notefd = keyfd = -1;
  443. whichkey = nil;
  444. ARGBEGIN {
  445. case 'A': /* auth protos */
  446. case 'c': /* ciphers */
  447. fprint(2, "%s: sorry, -%c is not supported\n", argv0, ARGC());
  448. break;
  449. case 'a': /* compat? */
  450. case 'C': /* cooked mode */
  451. case 'f': /* agent forwarding */
  452. case 'p': /* force pty */
  453. case 'P': /* force no pty */
  454. case 'R': /* force raw mode on pty */
  455. case 'v': /* scp compat */
  456. case 'w': /* send window-size changes */
  457. case 'x': /* unix compat: no x11 forwarding */
  458. break;
  459. case 'd':
  460. debug++;
  461. break;
  462. case 'I': /* non-interactive */
  463. iflag = 0;
  464. break;
  465. case 'i': /* interactive: scp & rx do it */
  466. iflag = 1;
  467. break;
  468. case 'l':
  469. case 'u':
  470. user = EARGF(usage());
  471. break;
  472. case 'k':
  473. nopka = 1;
  474. break;
  475. case 'K':
  476. nopw = 1;
  477. break;
  478. case 'm':
  479. mflag = 1;
  480. break;
  481. case 'n':
  482. netdir = EARGF(usage());
  483. break;
  484. case 'r':
  485. stripcr = 1;
  486. break;
  487. case 'z':
  488. whichkey = EARGF(usage());
  489. break;
  490. default:
  491. usage();
  492. } ARGEND;
  493. if (argc == 0)
  494. usage();
  495. if (iflag == -1)
  496. iflag = isatty(0);
  497. remote = *argv++;
  498. --argc;
  499. parseargs();
  500. if (!user)
  501. user = getuser();
  502. if (user == nil || remote == nil)
  503. sysfatal("out of memory");
  504. starttunnel();
  505. /* fork subproc to handle keys; don't wait for it */
  506. if ((n = rfork(RFPROC|RFMEM|RFFDG|RFNOWAIT)) == 0)
  507. keyproc(buf, sizeof buf);
  508. chpid = n;
  509. atnotify(handler, 1);
  510. /* connect and learn connection number */
  511. conn = connect(buf, sizeof buf);
  512. consfd = open("/dev/cons", ORDWR);
  513. cctlfd = open("/dev/consctl", OWRITE);
  514. fprint(cctlfd, "rawon");
  515. if (doauth(cfd1, whichkey) < 0)
  516. bail("doauth");
  517. /* connect a channel of conn and learn channel number */
  518. chan = chanconnect(conn, buf, sizeof buf);
  519. /* open request channel, request shell or command execution */
  520. remotecmd(argc, argv, conn, chan, buf, sizeof buf);
  521. bidircopy(buf, sizeof buf);
  522. }
  523. int
  524. isatty(int fd)
  525. {
  526. char buf[64];
  527. buf[0] = '\0';
  528. fd2path(fd, buf, sizeof buf);
  529. return strlen(buf) >= 9 && strcmp(buf+strlen(buf)-9, "/dev/cons") == 0;
  530. }
  531. int
  532. doauth(int cfd1, char *whichkey)
  533. {
  534. UserPasswd *up;
  535. int n;
  536. char path[Arbpathlen];
  537. if (!nopka) {
  538. if (whichkey)
  539. n = fprint(cfd1, "ssh-userauth K %q %q", user, whichkey);
  540. else
  541. n = fprint(cfd1, "ssh-userauth K %q", user);
  542. if (n >= 0)
  543. return 0;
  544. }
  545. if (nopw)
  546. return -1;
  547. up = auth_getuserpasswd(iflag? auth_getkey: nil,
  548. "proto=pass service=ssh server=%q user=%q", remote, user);
  549. if (up == nil) {
  550. fprint(2, "%s: didn't get password: %r\n", argv0);
  551. return -1;
  552. }
  553. n = fprint(cfd1, "ssh-userauth k %q %q", user, up->passwd);
  554. if (n >= 0)
  555. return 0;
  556. path[0] = '\0';
  557. fd2path(cfd1, path, sizeof path);
  558. fprint(2, "%s: auth ctl msg `ssh-userauth k %q <password>' for %q: %r\n",
  559. argv0, user, path);
  560. return -1;
  561. }