You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

632 lines
18 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * Copyright (C) 2007,2008 Øyvind Harboe *
  6. * oyvind.harboe@zylin.com *
  7. * *
  8. * Copyright (C) 2008 by Spencer Oliver *
  9. * spen@spen-soft.co.uk *
  10. * *
  11. * This program is free software; you can redistribute it and/or modify *
  12. * it under the terms of the GNU General Public License as published by *
  13. * the Free Software Foundation; either version 2 of the License, or *
  14. * (at your option) any later version. *
  15. * *
  16. * This program is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  19. * GNU General Public License for more details. *
  20. * *
  21. * You should have received a copy of the GNU General Public License *
  22. * along with this program; if not, write to the *
  23. * Free Software Foundation, Inc., *
  24. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  25. ***************************************************************************/
  26. #ifdef HAVE_CONFIG_H
  27. #include "config.h"
  28. #endif
  29. #include "telnet_server.h"
  30. #include "target_request.h"
  31. static unsigned short telnet_port = 4444;
  32. int handle_exit_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  33. int handle_telnet_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  34. static char *negotiate =
  35. "\xFF\xFB\x03" /* IAC WILL Suppress Go Ahead */
  36. "\xFF\xFB\x01" /* IAC WILL Echo */
  37. "\xFF\xFD\x03" /* IAC DO Suppress Go Ahead */
  38. "\xFF\xFE\x01"; /* IAC DON'T Echo */
  39. #define CTRL(c) (c - '@')
  40. /* The only way we can detect that the socket is closed is the first time
  41. * we write to it, we will fail. Subsequent write operations will
  42. * succeed. Shudder!
  43. */
  44. int telnet_write(connection_t *connection, const void *data, int len)
  45. {
  46. telnet_connection_t *t_con = connection->priv;
  47. if (t_con->closed)
  48. return ERROR_SERVER_REMOTE_CLOSED;
  49. if (write_socket(connection->fd, data, len) == len)
  50. {
  51. return ERROR_OK;
  52. }
  53. t_con->closed = 1;
  54. return ERROR_SERVER_REMOTE_CLOSED;
  55. }
  56. int telnet_prompt(connection_t *connection)
  57. {
  58. telnet_connection_t *t_con = connection->priv;
  59. telnet_write(connection, "\r", 1); /* the prompt is always placed at the line beginning */
  60. return telnet_write(connection, t_con->prompt, strlen(t_con->prompt));
  61. }
  62. int telnet_outputline(connection_t *connection, const char *line)
  63. {
  64. int len;
  65. /* process lines in buffer */
  66. while (*line) {
  67. char *line_end = strchr(line, '\n');
  68. if (line_end)
  69. len = line_end-line;
  70. else
  71. len = strlen(line);
  72. telnet_write(connection, line, len);
  73. if (line_end)
  74. {
  75. telnet_write(connection, "\r\n", 2);
  76. line += len + 1;
  77. }
  78. else
  79. {
  80. line += len;
  81. }
  82. }
  83. return ERROR_OK;
  84. }
  85. int telnet_output(struct command_context_s *cmd_ctx, const char* line)
  86. {
  87. connection_t *connection = cmd_ctx->output_handler_priv;
  88. return telnet_outputline(connection, line);
  89. }
  90. void telnet_log_callback(void *priv, const char *file, int line,
  91. const char *function, const char *string)
  92. {
  93. connection_t *connection = priv;
  94. telnet_connection_t *t_con = connection->priv;
  95. int i;
  96. /* if there is no prompt, simply output the message */
  97. if (t_con->line_cursor < 0)
  98. {
  99. telnet_outputline(connection, string);
  100. return;
  101. }
  102. /* clear the command line */
  103. telnet_write(connection, "\r", 1);
  104. for (i = strlen(t_con->prompt) + t_con->line_size; i > 0; i -= 16)
  105. telnet_write(connection, " ", i > 16 ? 16 : i);
  106. telnet_write(connection, "\r", 1);
  107. /* output the message */
  108. telnet_outputline(connection, string);
  109. /* put the command line to its previous state */
  110. telnet_prompt(connection);
  111. telnet_write(connection, t_con->line, t_con->line_size);
  112. for (i = t_con->line_size; i > t_con->line_cursor; i--)
  113. telnet_write(connection, "\b", 1);
  114. }
  115. int telnet_new_connection(connection_t *connection)
  116. {
  117. telnet_connection_t *telnet_connection = malloc(sizeof(telnet_connection_t));
  118. telnet_service_t *telnet_service = connection->service->priv;
  119. int i;
  120. connection->priv = telnet_connection;
  121. /* initialize telnet connection information */
  122. telnet_connection->closed = 0;
  123. telnet_connection->line_size = 0;
  124. telnet_connection->line_cursor = 0;
  125. telnet_connection->option_size = 0;
  126. telnet_connection->prompt = strdup("> ");
  127. telnet_connection->state = TELNET_STATE_DATA;
  128. /* output goes through telnet connection */
  129. command_set_output_handler(connection->cmd_ctx, telnet_output, connection);
  130. /* negotiate telnet options */
  131. telnet_write(connection, negotiate, strlen(negotiate));
  132. /* print connection banner */
  133. if (telnet_service->banner)
  134. {
  135. telnet_write(connection, telnet_service->banner, strlen(telnet_service->banner));
  136. telnet_write(connection, "\r\n", 2);
  137. }
  138. telnet_prompt(connection);
  139. /* initialize history */
  140. for (i = 0; i < TELNET_LINE_HISTORY_SIZE; i++)
  141. {
  142. telnet_connection->history[i] = NULL;
  143. }
  144. telnet_connection->next_history = 0;
  145. telnet_connection->current_history = 0;
  146. log_add_callback(telnet_log_callback, connection);
  147. return ERROR_OK;
  148. }
  149. void telnet_clear_line(connection_t *connection, telnet_connection_t *t_con)
  150. {
  151. /* move to end of line */
  152. if (t_con->line_cursor < t_con->line_size)
  153. {
  154. telnet_write(connection, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);
  155. }
  156. /* backspace, overwrite with space, backspace */
  157. while (t_con->line_size > 0)
  158. {
  159. telnet_write(connection, "\b \b", 3);
  160. t_con->line_size--;
  161. }
  162. t_con->line_cursor = 0;
  163. }
  164. int telnet_input(connection_t *connection)
  165. {
  166. int bytes_read;
  167. char buffer[TELNET_BUFFER_SIZE];
  168. char *buf_p;
  169. telnet_connection_t *t_con = connection->priv;
  170. command_context_t *command_context = connection->cmd_ctx;
  171. bytes_read = read_socket(connection->fd, buffer, TELNET_BUFFER_SIZE);
  172. if (bytes_read == 0)
  173. return ERROR_SERVER_REMOTE_CLOSED;
  174. else if (bytes_read == -1)
  175. {
  176. LOG_ERROR("error during read: %s", strerror(errno));
  177. return ERROR_SERVER_REMOTE_CLOSED;
  178. }
  179. buf_p = buffer;
  180. while (bytes_read)
  181. {
  182. switch (t_con->state)
  183. {
  184. case TELNET_STATE_DATA:
  185. if (*buf_p == '\xff')
  186. {
  187. t_con->state = TELNET_STATE_IAC;
  188. }
  189. else
  190. {
  191. if (isprint(*buf_p)) /* printable character */
  192. {
  193. /* watch buffer size leaving one spare character for string null termination */
  194. if (t_con->line_size == TELNET_LINE_MAX_SIZE-1)
  195. {
  196. /* output audible bell if buffer is full */
  197. telnet_write(connection, "\x07", 1); /* "\a" does not work, at least on windows */
  198. }
  199. else if (t_con->line_cursor == t_con->line_size)
  200. {
  201. telnet_write(connection, buf_p, 1);
  202. t_con->line[t_con->line_size++] = *buf_p;
  203. t_con->line_cursor++;
  204. }
  205. else
  206. {
  207. int i;
  208. memmove(t_con->line + t_con->line_cursor + 1, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);
  209. t_con->line[t_con->line_cursor] = *buf_p;
  210. t_con->line_size++;
  211. telnet_write(connection, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);
  212. t_con->line_cursor++;
  213. for (i = t_con->line_cursor; i < t_con->line_size; i++)
  214. {
  215. telnet_write(connection, "\b", 1);
  216. }
  217. }
  218. }
  219. else /* non-printable */
  220. {
  221. if (*buf_p == 0x1b) /* escape */
  222. {
  223. t_con->state = TELNET_STATE_ESCAPE;
  224. t_con->last_escape = '\x00';
  225. }
  226. else if ((*buf_p == 0xd) || (*buf_p == 0xa)) /* CR/LF */
  227. {
  228. int retval;
  229. /* skip over combinations with CR/LF and NUL characters */
  230. if ((bytes_read > 1) && ((*(buf_p + 1) == 0xa) || (*(buf_p + 1) == 0xd)))
  231. {
  232. buf_p++;
  233. bytes_read--;
  234. }
  235. if ((bytes_read > 1) && (*(buf_p + 1) == 0))
  236. {
  237. buf_p++;
  238. bytes_read--;
  239. }
  240. t_con->line[t_con->line_size] = 0;
  241. telnet_write(connection, "\r\n\x00", 3);
  242. if (strcmp(t_con->line, "history") == 0)
  243. {
  244. int i;
  245. for (i = 1; i < TELNET_LINE_HISTORY_SIZE; i++)
  246. {
  247. /* the t_con->next_history line contains empty string (unless NULL), thus it is not printed */
  248. char *history_line = t_con->history[(t_con->next_history + i) % TELNET_LINE_HISTORY_SIZE];
  249. if (history_line)
  250. {
  251. telnet_write(connection, history_line, strlen(history_line));
  252. telnet_write(connection, "\r\n\x00", 3);
  253. }
  254. }
  255. t_con->line_size = 0;
  256. t_con->line_cursor = 0;
  257. continue;
  258. }
  259. /* save only non-blank not repeating lines in the history */
  260. char *prev_line = t_con->history[(t_con->current_history > 0) ? t_con->current_history - 1 : TELNET_LINE_HISTORY_SIZE-1];
  261. if (*t_con->line && (prev_line == NULL || strcmp(t_con->line, prev_line)))
  262. {
  263. /* if the history slot is already taken, free it */
  264. if (t_con->history[t_con->next_history])
  265. {
  266. free(t_con->history[t_con->next_history]);
  267. }
  268. /* add line to history */
  269. t_con->history[t_con->next_history] = strdup(t_con->line);
  270. /* wrap history at TELNET_LINE_HISTORY_SIZE */
  271. t_con->next_history = (t_con->next_history + 1) % TELNET_LINE_HISTORY_SIZE;
  272. /* current history line starts at the new entry */
  273. t_con->current_history = t_con->next_history;
  274. if (t_con->history[t_con->current_history])
  275. {
  276. free(t_con->history[t_con->current_history]);
  277. }
  278. t_con->history[t_con->current_history] = strdup("");
  279. }
  280. t_con->line_size = 0;
  281. t_con->line_cursor = -1; /* to supress prompt in log callback during command execution */
  282. retval = command_run_line(command_context, t_con->line);
  283. t_con->line_cursor = 0;
  284. if (retval == ERROR_COMMAND_CLOSE_CONNECTION)
  285. return ERROR_SERVER_REMOTE_CLOSED;
  286. retval = telnet_prompt(connection);
  287. if (retval == ERROR_SERVER_REMOTE_CLOSED)
  288. return ERROR_SERVER_REMOTE_CLOSED;
  289. }
  290. else if ((*buf_p == 0x7f) || (*buf_p == 0x8)) /* delete character */
  291. {
  292. if (t_con->line_cursor > 0)
  293. {
  294. if (t_con->line_cursor != t_con->line_size)
  295. {
  296. int i;
  297. telnet_write(connection, "\b", 1);
  298. t_con->line_cursor--;
  299. t_con->line_size--;
  300. memmove(t_con->line + t_con->line_cursor, t_con->line + t_con->line_cursor + 1, t_con->line_size - t_con->line_cursor);
  301. telnet_write(connection, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);
  302. telnet_write(connection, " \b", 2);
  303. for (i = t_con->line_cursor; i < t_con->line_size; i++)
  304. {
  305. telnet_write(connection, "\b", 1);
  306. }
  307. }
  308. else
  309. {
  310. t_con->line_size--;
  311. t_con->line_cursor--;
  312. /* back space: move the 'printer' head one char back, overwrite with space, move back again */
  313. telnet_write(connection, "\b \b", 3);
  314. }
  315. }
  316. }
  317. else if (*buf_p == 0x15) /* clear line */
  318. {
  319. telnet_clear_line(connection, t_con);
  320. }
  321. else if (*buf_p == CTRL('B')) /* cursor left */
  322. {
  323. if (t_con->line_cursor > 0)
  324. {
  325. telnet_write(connection, "\b", 1);
  326. t_con->line_cursor--;
  327. }
  328. t_con->state = TELNET_STATE_DATA;
  329. }
  330. else if (*buf_p == CTRL('F')) /* cursor right */
  331. {
  332. if (t_con->line_cursor < t_con->line_size)
  333. {
  334. telnet_write(connection, t_con->line + t_con->line_cursor++, 1);
  335. }
  336. t_con->state = TELNET_STATE_DATA;
  337. }
  338. else
  339. {
  340. LOG_DEBUG("unhandled nonprintable: %2.2x", *buf_p);
  341. }
  342. }
  343. }
  344. break;
  345. case TELNET_STATE_IAC:
  346. switch (*buf_p)
  347. {
  348. case '\xfe':
  349. t_con->state = TELNET_STATE_DONT;
  350. break;
  351. case '\xfd':
  352. t_con->state = TELNET_STATE_DO;
  353. break;
  354. case '\xfc':
  355. t_con->state = TELNET_STATE_WONT;
  356. break;
  357. case '\xfb':
  358. t_con->state = TELNET_STATE_WILL;
  359. break;
  360. }
  361. break;
  362. case TELNET_STATE_SB:
  363. break;
  364. case TELNET_STATE_SE:
  365. break;
  366. case TELNET_STATE_WILL:
  367. case TELNET_STATE_WONT:
  368. case TELNET_STATE_DO:
  369. case TELNET_STATE_DONT:
  370. t_con->state = TELNET_STATE_DATA;
  371. break;
  372. case TELNET_STATE_ESCAPE:
  373. if (t_con->last_escape == '[')
  374. {
  375. if (*buf_p == 'D') /* cursor left */
  376. {
  377. if (t_con->line_cursor > 0)
  378. {
  379. telnet_write(connection, "\b", 1);
  380. t_con->line_cursor--;
  381. }
  382. t_con->state = TELNET_STATE_DATA;
  383. }
  384. else if (*buf_p == 'C') /* cursor right */
  385. {
  386. if (t_con->line_cursor < t_con->line_size)
  387. {
  388. telnet_write(connection, t_con->line + t_con->line_cursor++, 1);
  389. }
  390. t_con->state = TELNET_STATE_DATA;
  391. }
  392. else if (*buf_p == 'A') /* cursor up */
  393. {
  394. int last_history = (t_con->current_history > 0) ? t_con->current_history - 1 : TELNET_LINE_HISTORY_SIZE-1;
  395. if (t_con->history[last_history])
  396. {
  397. telnet_clear_line(connection, t_con);
  398. t_con->line_size = strlen(t_con->history[last_history]);
  399. t_con->line_cursor = t_con->line_size;
  400. memcpy(t_con->line, t_con->history[last_history], t_con->line_size);
  401. telnet_write(connection, t_con->line, t_con->line_size);
  402. t_con->current_history = last_history;
  403. }
  404. t_con->state = TELNET_STATE_DATA;
  405. }
  406. else if (*buf_p == 'B') /* cursor down */
  407. {
  408. int next_history = (t_con->current_history + 1) % TELNET_LINE_HISTORY_SIZE;
  409. if (t_con->history[next_history])
  410. {
  411. telnet_clear_line(connection, t_con);
  412. t_con->line_size = strlen(t_con->history[next_history]);
  413. t_con->line_cursor = t_con->line_size;
  414. memcpy(t_con->line, t_con->history[next_history], t_con->line_size);
  415. telnet_write(connection, t_con->line, t_con->line_size);
  416. t_con->current_history = next_history;
  417. }
  418. t_con->state = TELNET_STATE_DATA;
  419. }
  420. else if (*buf_p == '3')
  421. {
  422. t_con->last_escape = *buf_p;
  423. }
  424. else
  425. {
  426. t_con->state = TELNET_STATE_DATA;
  427. }
  428. }
  429. else if (t_con->last_escape == '3')
  430. {
  431. /* Remove character */
  432. if (*buf_p == '~')
  433. {
  434. if (t_con->line_cursor < t_con->line_size)
  435. {
  436. int i;
  437. t_con->line_size--;
  438. /* remove char from line buffer */
  439. memmove(t_con->line + t_con->line_cursor, t_con->line + t_con->line_cursor + 1, t_con->line_size - t_con->line_cursor);
  440. /* print remainder of buffer */
  441. telnet_write(connection, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);
  442. /* overwrite last char with whitespace */
  443. telnet_write(connection, " \b", 2);
  444. /* move back to cursor position*/
  445. for (i = t_con->line_cursor; i < t_con->line_size; i++)
  446. {
  447. telnet_write(connection, "\b", 1);
  448. }
  449. }
  450. t_con->state = TELNET_STATE_DATA;
  451. }
  452. else
  453. {
  454. t_con->state = TELNET_STATE_DATA;
  455. }
  456. }
  457. else if (t_con->last_escape == '\x00')
  458. {
  459. if (*buf_p == '[')
  460. {
  461. t_con->last_escape = *buf_p;
  462. }
  463. else
  464. {
  465. t_con->state = TELNET_STATE_DATA;
  466. }
  467. }
  468. else
  469. {
  470. LOG_ERROR("BUG: unexpected value in t_con->last_escape");
  471. t_con->state = TELNET_STATE_DATA;
  472. }
  473. break;
  474. default:
  475. LOG_ERROR("unknown telnet state");
  476. exit(-1);
  477. }
  478. bytes_read--;
  479. buf_p++;
  480. }
  481. return ERROR_OK;
  482. }
  483. int telnet_connection_closed(connection_t *connection)
  484. {
  485. telnet_connection_t *t_con = connection->priv;
  486. int i;
  487. log_remove_callback(telnet_log_callback, connection);
  488. if (t_con->prompt)
  489. {
  490. free(t_con->prompt);
  491. t_con->prompt = NULL;
  492. }
  493. for (i = 0; i < TELNET_LINE_HISTORY_SIZE; i++)
  494. {
  495. if (t_con->history[i])
  496. {
  497. free(t_con->history[i]);
  498. t_con->history[i] = NULL;
  499. }
  500. }
  501. /* if this connection registered a debug-message receiver delete it */
  502. delete_debug_msg_receiver(connection->cmd_ctx, NULL);
  503. if (connection->priv)
  504. {
  505. free(connection->priv);
  506. connection->priv = NULL;
  507. }
  508. else
  509. {
  510. LOG_ERROR("BUG: connection->priv == NULL");
  511. }
  512. return ERROR_OK;
  513. }
  514. int telnet_set_prompt(connection_t *connection, char *prompt)
  515. {
  516. telnet_connection_t *t_con = connection->priv;
  517. if (t_con->prompt != NULL)
  518. free(t_con->prompt);
  519. t_con->prompt = strdup(prompt);
  520. return ERROR_OK;
  521. }
  522. int telnet_init(char *banner)
  523. {
  524. telnet_service_t *telnet_service = malloc(sizeof(telnet_service_t));
  525. if (telnet_port == 0)
  526. {
  527. LOG_INFO("telnet port disabled");
  528. free(telnet_service);
  529. return ERROR_OK;
  530. }
  531. telnet_service->banner = banner;
  532. add_service("telnet", CONNECTION_TCP, telnet_port, 1, telnet_new_connection, telnet_input, telnet_connection_closed, telnet_service);
  533. return ERROR_OK;
  534. }
  535. int telnet_register_commands(command_context_t *command_context)
  536. {
  537. register_command(command_context, NULL, "exit", handle_exit_command,
  538. COMMAND_EXEC, "exit telnet session");
  539. register_command(command_context, NULL, "telnet_port", handle_telnet_port_command,
  540. COMMAND_ANY, "port on which to listen for incoming telnet connections");
  541. return ERROR_OK;
  542. }
  543. /* daemon configuration command telnet_port */
  544. int handle_telnet_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  545. {
  546. return server_port_command(cmd_ctx, cmd, args, argc, &telnet_port);
  547. }
  548. int handle_exit_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  549. {
  550. return ERROR_COMMAND_CLOSE_CONNECTION;
  551. }