ssh.c 12 KB

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