ssh.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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 protocol */
  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. p = esmprint("%s/ssh/keys", netdir);
  263. keyfd = open(p, ORDWR);
  264. if (keyfd < 0) {
  265. chpid = 0;
  266. sysfatal("failed to open ssh keys in %s: %r", p);
  267. }
  268. kconsfd = open("/dev/cons", ORDWR);
  269. if (kconsfd < 0)
  270. nopw = 1;
  271. buf[0] = 0;
  272. n = read(keyfd, buf, 5); /* reading /net/ssh/keys */
  273. if (n < 0)
  274. sysfatal("%s read: %r", p);
  275. buf[5] = 0;
  276. n = strtol(buf+1, nil, 10);
  277. n = readn(keyfd, buf+5, n);
  278. buf[n < 0? 5: n+5] = 0;
  279. free(p);
  280. switch (*buf) {
  281. case 'f':
  282. if (kconsfd >= 0)
  283. fprint(kconsfd, "%s\n", buf+5);
  284. /* fall through */
  285. case 'o':
  286. close(keyfd);
  287. if (kconsfd >= 0)
  288. close(kconsfd);
  289. break;
  290. default:
  291. if (kconsfd >= 0)
  292. keyprompt(buf, size, n);
  293. else {
  294. fprint(keyfd, "n");
  295. close(keyfd);
  296. }
  297. break;
  298. }
  299. chpid = 0;
  300. exits(nil);
  301. }
  302. /*
  303. * start a subproc to copy from network to stdout
  304. * while we copy from stdin to network.
  305. */
  306. static void
  307. bidircopy(char *buf, int size)
  308. {
  309. int i, n, lstart;
  310. char *path, *p, *q;
  311. rfork(RFNOTEG);
  312. path = esmprint("/proc/%d/notepg", getpid());
  313. notefd = open(path, OWRITE);
  314. switch (rfork(RFPROC|RFMEM|RFNOWAIT)) {
  315. case 0:
  316. while ((n = read(dfd2, buf, size - 1)) > 0) {
  317. if (!stripcr)
  318. p = buf + n;
  319. else
  320. for (i = 0, p = buf, q = buf; i < n; ++i, ++q)
  321. if (*q != '\r')
  322. *p++ = *q;
  323. if (p != buf)
  324. write(1, buf, p-buf);
  325. }
  326. /*
  327. * don't bother; it will be obvious when the user's prompt
  328. * changes.
  329. *
  330. * fprint(2, "%s: Connection closed by server\n", argv0);
  331. */
  332. break;
  333. default:
  334. lstart = 1;
  335. while ((n = read(0, buf, size - 1)) > 0) {
  336. if (!mflag && lstart && buf[0] == 0x1c)
  337. if (cmdmode())
  338. break;
  339. else
  340. continue;
  341. lstart = (buf[n-1] == '\n' || buf[n-1] == '\r');
  342. write(dfd2, buf, n);
  343. }
  344. /*
  345. * don't bother; it will be obvious when the user's prompt
  346. * changes.
  347. *
  348. * fprint(2, "%s: EOF on client side\n", argv0);
  349. */
  350. break;
  351. case -1:
  352. fprint(2, "%s: fork error: %r\n", argv0);
  353. break;
  354. }
  355. bail(nil);
  356. }
  357. static int
  358. connect(char *buf, int size)
  359. {
  360. int nfd, n;
  361. char *dir, *ds, *nf;
  362. dir = esmprint("%s/ssh", netdir);
  363. ds = netmkaddr(remote, dir, "22"); /* tcp port 22 is ssh */
  364. free(dir);
  365. dfd1 = dial(ds, nil, nil, &cfd1);
  366. if (dfd1 < 0) {
  367. fprint(2, "%s: dial conn %s: %r\n", argv0, ds);
  368. if (chpid) {
  369. nf = esmprint("/proc/%d/note", chpid);
  370. nfd = open(nf, OWRITE);
  371. fprint(nfd, "interrupt");
  372. close(nfd);
  373. }
  374. exits("can't dial");
  375. }
  376. seek(cfd1, 0, 0);
  377. n = read(cfd1, buf, size - 1);
  378. buf[n >= 0? n: 0] = 0;
  379. return atoi(buf);
  380. }
  381. static int
  382. chanconnect(int conn, char *buf, int size)
  383. {
  384. int n;
  385. char *path;
  386. path = esmprint("%s/ssh/%d!session", netdir, conn);
  387. dfd2 = dial(path, nil, nil, &cfd2);
  388. if (dfd2 < 0) {
  389. fprint(2, "%s: dial chan %s: %r\n", argv0, path);
  390. bail("dial");
  391. }
  392. free(path);
  393. n = read(cfd2, buf, size - 1);
  394. buf[n >= 0? n: 0] = 0;
  395. return atoi(buf);
  396. }
  397. static void
  398. remotecmd(int argc, char *argv[], int conn, int chan, char *buf, int size)
  399. {
  400. int i;
  401. char *path, *q, *ep;
  402. path = esmprint("%s/ssh/%d/%d/request", netdir, conn, chan);
  403. reqfd = open(path, OWRITE);
  404. if (reqfd < 0)
  405. bail("can't open request chan");
  406. if (argc == 0)
  407. if (readfile("/env/TERM", buf, size) < 0)
  408. fprint(reqfd, "shell");
  409. else
  410. fprint(reqfd, "shell %s", buf);
  411. else {
  412. assert(size >= Bigbufsz);
  413. ep = buf + Bigbufsz;
  414. q = seprint(buf, ep, "exec");
  415. for (i = 0; i < argc; ++i)
  416. q = seprint(q, ep, " %q", argv[i]);
  417. if (q >= ep) {
  418. fprint(2, "%s: command too long\n", argv0);
  419. fprint(reqfd, "close");
  420. bail("cmd too long");
  421. }
  422. write(reqfd, buf, q - buf);
  423. }
  424. }
  425. void
  426. main(int argc, char *argv[])
  427. {
  428. char *whichkey;
  429. int conn, chan, n;
  430. char buf[Copybufsz];
  431. quotefmtinstall();
  432. reqfd = dfd1 = cfd1 = dfd2 = cfd2 = consfd = kconsfd = cctlfd =
  433. notefd = keyfd = -1;
  434. whichkey = nil;
  435. ARGBEGIN {
  436. case 'A': /* auth protos */
  437. case 'c': /* ciphers */
  438. fprint(2, "%s: sorry, -%c is not supported\n", argv0, ARGC());
  439. break;
  440. case 'a': /* compat? */
  441. case 'C': /* cooked mode */
  442. case 'f': /* agent forwarding */
  443. case 'p': /* force pty */
  444. case 'P': /* force no pty */
  445. case 'R': /* force raw mode on pty */
  446. case 'v': /* scp compat */
  447. case 'w': /* send window-size changes */
  448. case 'x': /* unix compat: no x11 forwarding */
  449. break;
  450. case 'd':
  451. debug++;
  452. break;
  453. case 'I': /* non-interactive */
  454. iflag = 0;
  455. break;
  456. case 'i': /* interactive: scp & rx do it */
  457. iflag = 1;
  458. break;
  459. case 'l':
  460. case 'u':
  461. user = EARGF(usage());
  462. break;
  463. case 'k':
  464. nopka = 1;
  465. break;
  466. case 'K':
  467. nopw = 1;
  468. break;
  469. case 'm':
  470. mflag = 1;
  471. break;
  472. case 'n':
  473. netdir = EARGF(usage());
  474. break;
  475. case 'r':
  476. stripcr = 1;
  477. break;
  478. case 'z':
  479. whichkey = EARGF(usage());
  480. break;
  481. default:
  482. usage();
  483. } ARGEND;
  484. if (argc == 0)
  485. usage();
  486. if (iflag == -1)
  487. iflag = isatty(0);
  488. remote = *argv++;
  489. --argc;
  490. parseargs();
  491. if (!user)
  492. user = getuser();
  493. if (user == nil || remote == nil)
  494. sysfatal("out of memory");
  495. starttunnel();
  496. /* fork subproc to handle keys; don't wait for it */
  497. if ((n = rfork(RFPROC|RFMEM|RFFDG|RFNOWAIT)) == 0)
  498. keyproc(buf, sizeof buf);
  499. chpid = n;
  500. atnotify(handler, 1);
  501. /* connect and learn connection number */
  502. conn = connect(buf, sizeof buf);
  503. consfd = open("/dev/cons", ORDWR);
  504. cctlfd = open("/dev/consctl", OWRITE);
  505. fprint(cctlfd, "rawon");
  506. if (doauth(cfd1, whichkey) < 0)
  507. bail("doauth");
  508. /* connect a channel of conn and learn channel number */
  509. chan = chanconnect(conn, buf, sizeof buf);
  510. /* open request channel, request shell or command execution */
  511. remotecmd(argc, argv, conn, chan, buf, sizeof buf);
  512. bidircopy(buf, sizeof buf);
  513. }
  514. int
  515. isatty(int fd)
  516. {
  517. char buf[64];
  518. buf[0] = '\0';
  519. fd2path(fd, buf, sizeof buf);
  520. return strlen(buf) >= 9 && strcmp(buf+strlen(buf)-9, "/dev/cons") == 0;
  521. }
  522. int
  523. doauth(int cfd1, char *whichkey)
  524. {
  525. UserPasswd *up;
  526. int n;
  527. char path[Arbpathlen];
  528. if (!nopka) {
  529. if (whichkey)
  530. n = fprint(cfd1, "ssh-userauth K %q %q", user, whichkey);
  531. else
  532. n = fprint(cfd1, "ssh-userauth K %q", user);
  533. if (n >= 0)
  534. return 0;
  535. }
  536. if (nopw)
  537. return -1;
  538. up = auth_getuserpasswd(iflag? auth_getkey: nil,
  539. "proto=pass service=ssh server=%q user=%q", remote, user);
  540. if (up == nil) {
  541. fprint(2, "%s: didn't get password: %r\n", argv0);
  542. return -1;
  543. }
  544. n = fprint(cfd1, "ssh-userauth k %q %q", user, up->passwd);
  545. if (n >= 0)
  546. return 0;
  547. path[0] = '\0';
  548. fd2path(cfd1, path, sizeof path);
  549. fprint(2, "%s: auth ctl msg `ssh-userauth k %q <password>' for %q: %r\n",
  550. argv0, user, path);
  551. return -1;
  552. }