cli-chansession.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. * Dropbear SSH
  3. *
  4. * Copyright (c) 2002,2003 Matt Johnston
  5. * Copyright (c) 2004 by Mihnea Stoenescu
  6. * All rights reserved.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE. */
  25. #include "includes.h"
  26. #include "packet.h"
  27. #include "buffer.h"
  28. #include "session.h"
  29. #include "dbutil.h"
  30. #include "channel.h"
  31. #include "ssh.h"
  32. #include "runopts.h"
  33. #include "termcodes.h"
  34. #include "chansession.h"
  35. #include "agentfwd.h"
  36. static void cli_closechansess(struct Channel *channel);
  37. static int cli_initchansess(struct Channel *channel);
  38. static void cli_chansessreq(struct Channel *channel);
  39. static void send_chansess_pty_req(struct Channel *channel);
  40. static void send_chansess_shell_req(struct Channel *channel);
  41. static void cli_escape_handler(struct Channel *channel, unsigned char* buf, int *len);
  42. static int cli_init_netcat(struct Channel *channel);
  43. static void cli_tty_setup(void);
  44. const struct ChanType clichansess = {
  45. 0, /* sepfds */
  46. "session", /* name */
  47. cli_initchansess, /* inithandler */
  48. NULL, /* checkclosehandler */
  49. cli_chansessreq, /* reqhandler */
  50. cli_closechansess, /* closehandler */
  51. };
  52. static void cli_chansessreq(struct Channel *channel) {
  53. char* type = NULL;
  54. int wantreply;
  55. TRACE(("enter cli_chansessreq"))
  56. type = buf_getstring(ses.payload, NULL);
  57. wantreply = buf_getbool(ses.payload);
  58. if (strcmp(type, "exit-status") == 0) {
  59. cli_ses.retval = buf_getint(ses.payload);
  60. TRACE(("got exit-status of '%d'", cli_ses.retval))
  61. } else if (strcmp(type, "exit-signal") == 0) {
  62. TRACE(("got exit-signal, ignoring it"))
  63. } else {
  64. TRACE(("unknown request '%s'", type))
  65. if (wantreply) {
  66. send_msg_channel_failure(channel);
  67. }
  68. goto out;
  69. }
  70. out:
  71. m_free(type);
  72. }
  73. /* If the main session goes, we close it up */
  74. static void cli_closechansess(struct Channel *UNUSED(channel)) {
  75. cli_tty_cleanup(); /* Restore tty modes etc */
  76. /* This channel hasn't gone yet, so we have > 1 */
  77. if (ses.chancount > 1) {
  78. dropbear_log(LOG_INFO, "Waiting for other channels to close...");
  79. }
  80. }
  81. /* Taken from OpenSSH's sshtty.c:
  82. * RCSID("OpenBSD: sshtty.c,v 1.5 2003/09/19 17:43:35 markus Exp "); */
  83. static void cli_tty_setup() {
  84. struct termios tio;
  85. TRACE(("enter cli_pty_setup"))
  86. if (cli_ses.tty_raw_mode == 1) {
  87. TRACE(("leave cli_tty_setup: already in raw mode!"))
  88. return;
  89. }
  90. if (tcgetattr(STDIN_FILENO, &tio) == -1) {
  91. dropbear_exit("Failed to set raw TTY mode");
  92. }
  93. /* make a copy */
  94. cli_ses.saved_tio = tio;
  95. tio.c_iflag |= IGNPAR;
  96. tio.c_iflag &= ~(ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXANY | IXOFF);
  97. #ifdef IUCLC
  98. tio.c_iflag &= ~IUCLC;
  99. #endif
  100. tio.c_lflag &= ~(ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL);
  101. #ifdef IEXTEN
  102. tio.c_lflag &= ~IEXTEN;
  103. #endif
  104. tio.c_oflag &= ~OPOST;
  105. tio.c_cc[VMIN] = 1;
  106. tio.c_cc[VTIME] = 0;
  107. if (tcsetattr(STDIN_FILENO, TCSADRAIN, &tio) == -1) {
  108. dropbear_exit("Failed to set raw TTY mode");
  109. }
  110. cli_ses.tty_raw_mode = 1;
  111. TRACE(("leave cli_tty_setup"))
  112. }
  113. void cli_tty_cleanup() {
  114. TRACE(("enter cli_tty_cleanup"))
  115. if (cli_ses.tty_raw_mode == 0) {
  116. TRACE(("leave cli_tty_cleanup: not in raw mode"))
  117. return;
  118. }
  119. if (tcsetattr(STDIN_FILENO, TCSADRAIN, &cli_ses.saved_tio) == -1) {
  120. dropbear_log(LOG_WARNING, "Failed restoring TTY");
  121. } else {
  122. cli_ses.tty_raw_mode = 0;
  123. }
  124. TRACE(("leave cli_tty_cleanup"))
  125. }
  126. static void put_termcodes() {
  127. struct termios tio;
  128. unsigned int sshcode;
  129. const struct TermCode *termcode;
  130. unsigned int value;
  131. unsigned int mapcode;
  132. unsigned int bufpos1, bufpos2;
  133. TRACE(("enter put_termcodes"))
  134. if (tcgetattr(STDIN_FILENO, &tio) == -1) {
  135. dropbear_log(LOG_WARNING, "Failed reading termmodes");
  136. buf_putint(ses.writepayload, 1); /* Just the terminator */
  137. buf_putbyte(ses.writepayload, 0); /* TTY_OP_END */
  138. return;
  139. }
  140. bufpos1 = ses.writepayload->pos;
  141. buf_putint(ses.writepayload, 0); /* A placeholder for the final length */
  142. /* As with Dropbear server, we ignore baud rates for now */
  143. for (sshcode = 1; sshcode < MAX_TERMCODE; sshcode++) {
  144. termcode = &termcodes[sshcode];
  145. mapcode = termcode->mapcode;
  146. switch (termcode->type) {
  147. case TERMCODE_NONE:
  148. continue;
  149. case TERMCODE_CONTROLCHAR:
  150. value = tio.c_cc[mapcode];
  151. break;
  152. case TERMCODE_INPUT:
  153. value = tio.c_iflag & mapcode;
  154. break;
  155. case TERMCODE_OUTPUT:
  156. value = tio.c_oflag & mapcode;
  157. break;
  158. case TERMCODE_LOCAL:
  159. value = tio.c_lflag & mapcode;
  160. break;
  161. case TERMCODE_CONTROL:
  162. value = tio.c_cflag & mapcode;
  163. break;
  164. default:
  165. continue;
  166. }
  167. /* If we reach here, we have something to say */
  168. buf_putbyte(ses.writepayload, sshcode);
  169. buf_putint(ses.writepayload, value);
  170. }
  171. buf_putbyte(ses.writepayload, 0); /* THE END, aka TTY_OP_END */
  172. /* Put the string length at the start of the buffer */
  173. bufpos2 = ses.writepayload->pos;
  174. buf_setpos(ses.writepayload, bufpos1); /* Jump back */
  175. buf_putint(ses.writepayload, bufpos2 - bufpos1 - 4); /* len(termcodes) */
  176. buf_setpos(ses.writepayload, bufpos2); /* Back where we were */
  177. TRACE(("leave put_termcodes"))
  178. }
  179. static void put_winsize() {
  180. struct winsize ws;
  181. if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) < 0) {
  182. /* Some sane defaults */
  183. ws.ws_row = 25;
  184. ws.ws_col = 80;
  185. ws.ws_xpixel = 0;
  186. ws.ws_ypixel = 0;
  187. }
  188. buf_putint(ses.writepayload, ws.ws_col); /* Cols */
  189. buf_putint(ses.writepayload, ws.ws_row); /* Rows */
  190. buf_putint(ses.writepayload, ws.ws_xpixel); /* Width */
  191. buf_putint(ses.writepayload, ws.ws_ypixel); /* Height */
  192. }
  193. static void sigwinch_handler(int UNUSED(unused)) {
  194. cli_ses.winchange = 1;
  195. }
  196. void cli_chansess_winchange() {
  197. unsigned int i;
  198. struct Channel *channel = NULL;
  199. for (i = 0; i < ses.chansize; i++) {
  200. channel = ses.channels[i];
  201. if (channel != NULL && channel->type == &clichansess) {
  202. CHECKCLEARTOWRITE();
  203. buf_putbyte(ses.writepayload, SSH_MSG_CHANNEL_REQUEST);
  204. buf_putint(ses.writepayload, channel->remotechan);
  205. buf_putstring(ses.writepayload, "window-change", 13);
  206. buf_putbyte(ses.writepayload, 0); /* FALSE says the spec */
  207. put_winsize();
  208. encrypt_packet();
  209. }
  210. }
  211. cli_ses.winchange = 0;
  212. }
  213. static void send_chansess_pty_req(struct Channel *channel) {
  214. char* term = NULL;
  215. TRACE(("enter send_chansess_pty_req"))
  216. start_send_channel_request(channel, "pty-req");
  217. /* Don't want replies */
  218. buf_putbyte(ses.writepayload, 0);
  219. /* Get the terminal */
  220. term = getenv("TERM");
  221. if (term == NULL) {
  222. term = "vt100"; /* Seems a safe default */
  223. }
  224. buf_putstring(ses.writepayload, term, strlen(term));
  225. /* Window size */
  226. put_winsize();
  227. /* Terminal mode encoding */
  228. put_termcodes();
  229. encrypt_packet();
  230. /* Set up a window-change handler */
  231. if (signal(SIGWINCH, sigwinch_handler) == SIG_ERR) {
  232. dropbear_exit("Signal error");
  233. }
  234. TRACE(("leave send_chansess_pty_req"))
  235. }
  236. static void send_chansess_shell_req(struct Channel *channel) {
  237. char* reqtype = NULL;
  238. TRACE(("enter send_chansess_shell_req"))
  239. if (cli_opts.cmd) {
  240. if (cli_opts.is_subsystem) {
  241. reqtype = "subsystem";
  242. } else {
  243. reqtype = "exec";
  244. }
  245. } else {
  246. reqtype = "shell";
  247. }
  248. start_send_channel_request(channel, reqtype);
  249. /* XXX TODO */
  250. buf_putbyte(ses.writepayload, 0); /* Don't want replies */
  251. if (cli_opts.cmd) {
  252. buf_putstring(ses.writepayload, cli_opts.cmd, strlen(cli_opts.cmd));
  253. }
  254. encrypt_packet();
  255. TRACE(("leave send_chansess_shell_req"))
  256. }
  257. /* Shared for normal client channel and netcat-alike */
  258. static int cli_init_stdpipe_sess(struct Channel *channel) {
  259. channel->writefd = STDOUT_FILENO;
  260. setnonblocking(STDOUT_FILENO);
  261. channel->readfd = STDIN_FILENO;
  262. setnonblocking(STDIN_FILENO);
  263. channel->errfd = STDERR_FILENO;
  264. setnonblocking(STDERR_FILENO);
  265. channel->extrabuf = cbuf_new(opts.recv_window);
  266. return 0;
  267. }
  268. static int cli_init_netcat(struct Channel *channel) {
  269. channel->prio = DROPBEAR_CHANNEL_PRIO_UNKNOWABLE;
  270. return cli_init_stdpipe_sess(channel);
  271. }
  272. static int cli_initchansess(struct Channel *channel) {
  273. cli_init_stdpipe_sess(channel);
  274. #ifdef ENABLE_CLI_AGENTFWD
  275. if (cli_opts.agent_fwd) {
  276. cli_setup_agent(channel);
  277. }
  278. #endif
  279. if (cli_opts.wantpty) {
  280. send_chansess_pty_req(channel);
  281. channel->prio = DROPBEAR_CHANNEL_PRIO_INTERACTIVE;
  282. } else {
  283. channel->prio = DROPBEAR_CHANNEL_PRIO_BULK;
  284. }
  285. send_chansess_shell_req(channel);
  286. if (cli_opts.wantpty) {
  287. cli_tty_setup();
  288. channel->read_mangler = cli_escape_handler;
  289. cli_ses.last_char = '\r';
  290. }
  291. return 0; /* Success */
  292. }
  293. #ifdef ENABLE_CLI_NETCAT
  294. static const struct ChanType cli_chan_netcat = {
  295. 0, /* sepfds */
  296. "direct-tcpip",
  297. cli_init_netcat, /* inithandler */
  298. NULL,
  299. NULL,
  300. cli_closechansess
  301. };
  302. void cli_send_netcat_request() {
  303. const char* source_host = "127.0.0.1";
  304. const int source_port = 22;
  305. TRACE(("enter cli_send_netcat_request"))
  306. cli_opts.wantpty = 0;
  307. if (send_msg_channel_open_init(STDIN_FILENO, &cli_chan_netcat)
  308. == DROPBEAR_FAILURE) {
  309. dropbear_exit("Couldn't open initial channel");
  310. }
  311. buf_putstring(ses.writepayload, cli_opts.netcat_host,
  312. strlen(cli_opts.netcat_host));
  313. buf_putint(ses.writepayload, cli_opts.netcat_port);
  314. /* originator ip - localhost is accurate enough */
  315. buf_putstring(ses.writepayload, source_host, strlen(source_host));
  316. buf_putint(ses.writepayload, source_port);
  317. encrypt_packet();
  318. TRACE(("leave cli_send_netcat_request"))
  319. }
  320. #endif
  321. void cli_send_chansess_request() {
  322. TRACE(("enter cli_send_chansess_request"))
  323. if (send_msg_channel_open_init(STDIN_FILENO, &clichansess)
  324. == DROPBEAR_FAILURE) {
  325. dropbear_exit("Couldn't open initial channel");
  326. }
  327. /* No special channel request data */
  328. encrypt_packet();
  329. TRACE(("leave cli_send_chansess_request"))
  330. }
  331. /* returns 1 if the character should be consumed, 0 to pass through */
  332. static int
  333. do_escape(unsigned char c) {
  334. switch (c) {
  335. case '.':
  336. dropbear_exit("Terminated");
  337. return 1;
  338. case 0x1a:
  339. /* ctrl-z */
  340. cli_tty_cleanup();
  341. kill(getpid(), SIGTSTP);
  342. /* after continuation */
  343. cli_tty_setup();
  344. cli_ses.winchange = 1;
  345. return 1;
  346. default:
  347. return 0;
  348. }
  349. }
  350. static
  351. void cli_escape_handler(struct Channel* UNUSED(channel), unsigned char* buf, int *len) {
  352. char c;
  353. int skip_char = 0;
  354. /* only handle escape characters if they are read one at a time. simplifies
  355. the code and avoids nasty people putting ~. at the start of a line to paste */
  356. if (*len != 1) {
  357. cli_ses.last_char = 0x0;
  358. return;
  359. }
  360. c = buf[0];
  361. if (cli_ses.last_char == DROPBEAR_ESCAPE_CHAR) {
  362. skip_char = do_escape(c);
  363. cli_ses.last_char = 0x0;
  364. } else {
  365. if (c == DROPBEAR_ESCAPE_CHAR) {
  366. if (cli_ses.last_char == '\r') {
  367. cli_ses.last_char = DROPBEAR_ESCAPE_CHAR;
  368. skip_char = 1;
  369. } else {
  370. cli_ses.last_char = 0x0;
  371. }
  372. } else {
  373. cli_ses.last_char = c;
  374. }
  375. }
  376. if (skip_char) {
  377. *len = 0;
  378. }
  379. }