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.
 
 
 
 
 
 

948 lines
27 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * Copyright (C) 2007-2010 Ø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, see <http://www.gnu.org/licenses/>. *
  23. ***************************************************************************/
  24. #ifdef HAVE_CONFIG_H
  25. #include "config.h"
  26. #endif
  27. #include "telnet_server.h"
  28. #include <target/target_request.h>
  29. #include <helper/configuration.h>
  30. #include <helper/list.h>
  31. static char *telnet_port;
  32. static char *negotiate =
  33. "\xFF\xFB\x03" /* IAC WILL Suppress Go Ahead */
  34. "\xFF\xFB\x01" /* IAC WILL Echo */
  35. "\xFF\xFD\x03" /* IAC DO Suppress Go Ahead */
  36. "\xFF\xFE\x01"; /* IAC DON'T Echo */
  37. #define CTRL(c) (c - '@')
  38. #define TELNET_HISTORY ".openocd_history"
  39. /* The only way we can detect that the socket is closed is the first time
  40. * we write to it, we will fail. Subsequent write operations will
  41. * succeed. Shudder!
  42. */
  43. static int telnet_write(struct connection *connection, const void *data,
  44. int len)
  45. {
  46. struct telnet_connection *t_con = connection->priv;
  47. if (t_con->closed)
  48. return ERROR_SERVER_REMOTE_CLOSED;
  49. if (connection_write(connection, data, len) == len)
  50. return ERROR_OK;
  51. t_con->closed = true;
  52. return ERROR_SERVER_REMOTE_CLOSED;
  53. }
  54. /* output an audible bell */
  55. static int telnet_bell(struct connection *connection)
  56. {
  57. /* ("\a" does not work, at least on windows) */
  58. return telnet_write(connection, "\x07", 1);
  59. }
  60. static int telnet_prompt(struct connection *connection)
  61. {
  62. struct telnet_connection *t_con = connection->priv;
  63. return telnet_write(connection, t_con->prompt, strlen(t_con->prompt));
  64. }
  65. static int telnet_outputline(struct connection *connection, const char *line)
  66. {
  67. int len;
  68. /* process lines in buffer */
  69. while (*line) {
  70. char *line_end = strchr(line, '\n');
  71. if (line_end)
  72. len = line_end-line;
  73. else
  74. len = strlen(line);
  75. telnet_write(connection, line, len);
  76. if (line_end) {
  77. telnet_write(connection, "\r\n", 2);
  78. line += len + 1;
  79. } else
  80. line += len;
  81. }
  82. return ERROR_OK;
  83. }
  84. static int telnet_output(struct command_context *cmd_ctx, const char *line)
  85. {
  86. struct connection *connection = cmd_ctx->output_handler_priv;
  87. return telnet_outputline(connection, line);
  88. }
  89. static void telnet_log_callback(void *priv, const char *file, unsigned line,
  90. const char *function, const char *string)
  91. {
  92. struct connection *connection = priv;
  93. struct telnet_connection *t_con = connection->priv;
  94. size_t i;
  95. size_t tmp;
  96. /* If the prompt is not visible, simply output the message. */
  97. if (!t_con->prompt_visible) {
  98. telnet_outputline(connection, string);
  99. return;
  100. }
  101. /* Clear the command line. */
  102. tmp = strlen(t_con->prompt) + t_con->line_size;
  103. for (i = 0; i < tmp; i += 16)
  104. telnet_write(connection, "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b",
  105. MIN(tmp - i, 16));
  106. for (i = 0; i < tmp; i += 16)
  107. telnet_write(connection, " ", MIN(tmp - i, 16));
  108. for (i = 0; i < tmp; i += 16)
  109. telnet_write(connection, "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b",
  110. MIN(tmp - i, 16));
  111. telnet_outputline(connection, string);
  112. /* Put the command line to its previous state. */
  113. telnet_prompt(connection);
  114. telnet_write(connection, t_con->line, t_con->line_size);
  115. for (i = t_con->line_cursor; i < t_con->line_size; i++)
  116. telnet_write(connection, "\b", 1);
  117. }
  118. static void telnet_load_history(struct telnet_connection *t_con)
  119. {
  120. FILE *histfp;
  121. char buffer[TELNET_BUFFER_SIZE];
  122. int i = 0;
  123. char *history = get_home_dir(TELNET_HISTORY);
  124. if (!history) {
  125. LOG_INFO("unable to get user home directory, telnet history will be disabled");
  126. return;
  127. }
  128. histfp = fopen(history, "rb");
  129. if (histfp) {
  130. while (fgets(buffer, sizeof(buffer), histfp) != NULL) {
  131. char *p = strchr(buffer, '\n');
  132. if (p)
  133. *p = '\0';
  134. if (buffer[0] && i < TELNET_LINE_HISTORY_SIZE)
  135. t_con->history[i++] = strdup(buffer);
  136. }
  137. t_con->next_history = i;
  138. t_con->next_history %= TELNET_LINE_HISTORY_SIZE;
  139. /* try to set to last entry - 1, that way we skip over any exit/shutdown cmds */
  140. t_con->current_history = t_con->next_history > 0 ? i - 1 : 0;
  141. fclose(histfp);
  142. }
  143. free(history);
  144. }
  145. static void telnet_save_history(struct telnet_connection *t_con)
  146. {
  147. FILE *histfp;
  148. int i;
  149. int num;
  150. char *history = get_home_dir(TELNET_HISTORY);
  151. if (!history) {
  152. LOG_INFO("unable to get user home directory, telnet history will be disabled");
  153. return;
  154. }
  155. histfp = fopen(history, "wb");
  156. if (histfp) {
  157. num = TELNET_LINE_HISTORY_SIZE;
  158. i = t_con->current_history + 1;
  159. i %= TELNET_LINE_HISTORY_SIZE;
  160. while (!t_con->history[i] && num > 0) {
  161. i++;
  162. i %= TELNET_LINE_HISTORY_SIZE;
  163. num--;
  164. }
  165. if (num > 0) {
  166. for (; num > 0; num--) {
  167. fprintf(histfp, "%s\n", t_con->history[i]);
  168. i++;
  169. i %= TELNET_LINE_HISTORY_SIZE;
  170. }
  171. }
  172. fclose(histfp);
  173. }
  174. free(history);
  175. }
  176. static int telnet_new_connection(struct connection *connection)
  177. {
  178. struct telnet_connection *telnet_connection;
  179. struct telnet_service *telnet_service = connection->service->priv;
  180. int i;
  181. telnet_connection = malloc(sizeof(struct telnet_connection));
  182. if (!telnet_connection) {
  183. LOG_ERROR("Failed to allocate telnet connection.");
  184. return ERROR_FAIL;
  185. }
  186. connection->priv = telnet_connection;
  187. /* initialize telnet connection information */
  188. telnet_connection->closed = false;
  189. telnet_connection->line_size = 0;
  190. telnet_connection->line_cursor = 0;
  191. telnet_connection->prompt = strdup("> ");
  192. telnet_connection->prompt_visible = true;
  193. telnet_connection->state = TELNET_STATE_DATA;
  194. /* output goes through telnet connection */
  195. command_set_output_handler(connection->cmd_ctx, telnet_output, connection);
  196. /* negotiate telnet options */
  197. telnet_write(connection, negotiate, strlen(negotiate));
  198. /* print connection banner */
  199. if (telnet_service->banner) {
  200. telnet_write(connection, telnet_service->banner, strlen(telnet_service->banner));
  201. telnet_write(connection, "\r\n", 2);
  202. }
  203. /* the prompt is always placed at the line beginning */
  204. telnet_write(connection, "\r", 1);
  205. telnet_prompt(connection);
  206. /* initialize history */
  207. for (i = 0; i < TELNET_LINE_HISTORY_SIZE; i++)
  208. telnet_connection->history[i] = NULL;
  209. telnet_connection->next_history = 0;
  210. telnet_connection->current_history = 0;
  211. telnet_load_history(telnet_connection);
  212. log_add_callback(telnet_log_callback, connection);
  213. return ERROR_OK;
  214. }
  215. static void telnet_clear_line(struct connection *connection,
  216. struct telnet_connection *t_con)
  217. {
  218. /* move to end of line */
  219. if (t_con->line_cursor < t_con->line_size)
  220. telnet_write(connection,
  221. t_con->line + t_con->line_cursor,
  222. t_con->line_size - t_con->line_cursor);
  223. /* backspace, overwrite with space, backspace */
  224. while (t_con->line_size > 0) {
  225. telnet_write(connection, "\b \b", 3);
  226. t_con->line_size--;
  227. }
  228. t_con->line_cursor = 0;
  229. }
  230. static void telnet_history_go(struct connection *connection, int idx)
  231. {
  232. struct telnet_connection *t_con = connection->priv;
  233. if (t_con->history[idx]) {
  234. telnet_clear_line(connection, t_con);
  235. t_con->line_size = strlen(t_con->history[idx]);
  236. t_con->line_cursor = t_con->line_size;
  237. memcpy(t_con->line, t_con->history[idx], t_con->line_size);
  238. telnet_write(connection, t_con->line, t_con->line_size);
  239. t_con->current_history = idx;
  240. }
  241. t_con->state = TELNET_STATE_DATA;
  242. }
  243. static void telnet_history_up(struct connection *connection)
  244. {
  245. struct telnet_connection *t_con = connection->priv;
  246. size_t last_history = (t_con->current_history > 0) ?
  247. t_con->current_history - 1 :
  248. TELNET_LINE_HISTORY_SIZE-1;
  249. telnet_history_go(connection, last_history);
  250. }
  251. static void telnet_history_down(struct connection *connection)
  252. {
  253. struct telnet_connection *t_con = connection->priv;
  254. size_t next_history;
  255. next_history = (t_con->current_history + 1) % TELNET_LINE_HISTORY_SIZE;
  256. telnet_history_go(connection, next_history);
  257. }
  258. static int telnet_history_print(struct connection *connection)
  259. {
  260. struct telnet_connection *tc;
  261. tc = connection->priv;
  262. for (size_t i = 1; i < TELNET_LINE_HISTORY_SIZE; i++) {
  263. char *line;
  264. /*
  265. * The tc->next_history line contains empty string (unless NULL), thus
  266. * it is not printed.
  267. */
  268. line = tc->history[(tc->next_history + i) % TELNET_LINE_HISTORY_SIZE];
  269. if (line) {
  270. telnet_write(connection, line, strlen(line));
  271. telnet_write(connection, "\r\n\x00", 3);
  272. }
  273. }
  274. tc->line_size = 0;
  275. tc->line_cursor = 0;
  276. /* The prompt is always placed at the line beginning. */
  277. telnet_write(connection, "\r", 1);
  278. return telnet_prompt(connection);
  279. }
  280. static void telnet_move_cursor(struct connection *connection, size_t pos)
  281. {
  282. struct telnet_connection *tc;
  283. size_t tmp;
  284. tc = connection->priv;
  285. if (pos < tc->line_cursor) {
  286. tmp = tc->line_cursor - pos;
  287. for (size_t i = 0; i < tmp; i += 16)
  288. telnet_write(connection, "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b",
  289. MIN(tmp - i, 16));
  290. } else {
  291. tmp = pos - tc->line_cursor;
  292. for (size_t i = 0; i < tmp; i += 16)
  293. telnet_write(connection, tc->line + tc->line_cursor + i,
  294. MIN(tmp - i, 16));
  295. }
  296. tc->line_cursor = pos;
  297. }
  298. /* check buffer size leaving one spare character for string null termination */
  299. static inline bool telnet_can_insert(struct connection *connection, size_t len)
  300. {
  301. struct telnet_connection *t_con = connection->priv;
  302. return t_con->line_size + len < TELNET_LINE_MAX_SIZE;
  303. }
  304. /* write to telnet console, and update the telnet_connection members
  305. * this function is capable of inserting in the middle of a line
  306. * please ensure that data does not contain special characters (\n, \r, \t, \b ...)
  307. *
  308. * returns false when it fails to insert the requested data
  309. */
  310. static bool telnet_insert(struct connection *connection, const void *data, size_t len)
  311. {
  312. struct telnet_connection *t_con = connection->priv;
  313. if (!telnet_can_insert(connection, len)) {
  314. telnet_bell(connection);
  315. return false;
  316. }
  317. if (t_con->line_cursor < t_con->line_size) {
  318. /* we have some content after the cursor */
  319. memmove(t_con->line + t_con->line_cursor + len,
  320. t_con->line + t_con->line_cursor,
  321. t_con->line_size - t_con->line_cursor);
  322. }
  323. strncpy(t_con->line + t_con->line_cursor, data, len);
  324. telnet_write(connection,
  325. t_con->line + t_con->line_cursor,
  326. t_con->line_size + len - t_con->line_cursor);
  327. t_con->line_size += len;
  328. t_con->line_cursor += len;
  329. for (size_t i = t_con->line_cursor; i < t_con->line_size; i++)
  330. telnet_write(connection, "\b", 1);
  331. return true;
  332. }
  333. static void telnet_auto_complete(struct connection *connection)
  334. {
  335. struct telnet_connection *t_con = connection->priv;
  336. struct command_context *command_context = connection->cmd_ctx;
  337. struct cmd_match {
  338. char *cmd;
  339. struct list_head lh;
  340. };
  341. LIST_HEAD(matches);
  342. /* user command sequence, either at line beginning
  343. * or we start over after these characters ';', '[', '{' */
  344. size_t seq_start = (t_con->line_cursor == 0) ? 0 : (t_con->line_cursor - 1);
  345. while (seq_start > 0) {
  346. char c = t_con->line[seq_start];
  347. if (c == ';' || c == '[' || c == '{') {
  348. seq_start++;
  349. break;
  350. }
  351. seq_start--;
  352. }
  353. /* user command position in the line, ignore leading spaces */
  354. size_t usr_cmd_pos = seq_start;
  355. while ((usr_cmd_pos < t_con->line_cursor) && isspace(t_con->line[usr_cmd_pos]))
  356. usr_cmd_pos++;
  357. /* user command length */
  358. size_t usr_cmd_len = t_con->line_cursor - usr_cmd_pos;
  359. /* optimize multiple spaces in the user command,
  360. * because info commands does not tolerate multiple spaces */
  361. size_t optimized_spaces = 0;
  362. char query[usr_cmd_len + 1];
  363. for (size_t i = 0; i < usr_cmd_len; i++) {
  364. if ((i < usr_cmd_len - 1) && isspace(t_con->line[usr_cmd_pos + i])
  365. && isspace(t_con->line[usr_cmd_pos + i + 1])) {
  366. optimized_spaces++;
  367. continue;
  368. }
  369. query[i - optimized_spaces] = t_con->line[usr_cmd_pos + i];
  370. }
  371. usr_cmd_len -= optimized_spaces;
  372. query[usr_cmd_len] = '\0';
  373. /* filter commands */
  374. char *query_cmd = alloc_printf("_telnet_autocomplete_helper {%s*}", query);
  375. if (!query_cmd) {
  376. LOG_ERROR("Out of memory");
  377. return;
  378. }
  379. int retval = Jim_EvalSource(command_context->interp, __FILE__, __LINE__, query_cmd);
  380. free(query_cmd);
  381. if (retval != JIM_OK)
  382. return;
  383. Jim_Obj *list = Jim_GetResult(command_context->interp);
  384. Jim_IncrRefCount(list);
  385. /* common prefix length of the matched commands */
  386. size_t common_len = 0;
  387. char *first_match = NULL; /* used to compute the common prefix length */
  388. int len = Jim_ListLength(command_context->interp, list);
  389. for (int i = 0; i < len; i++) {
  390. Jim_Obj *elem = Jim_ListGetIndex(command_context->interp, list, i);
  391. Jim_IncrRefCount(elem);
  392. char *name = (char *)Jim_GetString(elem, NULL);
  393. /* validate the command */
  394. bool ignore_cmd = false;
  395. Jim_Cmd *jim_cmd = Jim_GetCommand(command_context->interp, elem, JIM_NONE);
  396. if (!jim_cmd) {
  397. /* Why we are here? Let's ignore it! */
  398. ignore_cmd = true;
  399. } else if (jimcmd_is_oocd_command(jim_cmd)) {
  400. struct command *cmd = jimcmd_privdata(jim_cmd);
  401. if (cmd && !cmd->handler && !cmd->jim_handler) {
  402. /* Initial part of a multi-word command. Ignore it! */
  403. ignore_cmd = true;
  404. } else if (cmd && cmd->mode == COMMAND_CONFIG) {
  405. /* Not executable after config phase. Ignore it! */
  406. ignore_cmd = true;
  407. }
  408. }
  409. /* save the command in the prediction list */
  410. if (!ignore_cmd) {
  411. struct cmd_match *match = calloc(1, sizeof(struct cmd_match));
  412. if (!match) {
  413. LOG_ERROR("Out of memory");
  414. Jim_DecrRefCount(command_context->interp, elem);
  415. break; /* break the for loop */
  416. }
  417. if (list_empty(&matches)) {
  418. common_len = strlen(name);
  419. first_match = name;
  420. } else {
  421. size_t new_common_len = usr_cmd_len; /* save some loops */
  422. while (new_common_len < common_len && first_match[new_common_len] == name[new_common_len])
  423. new_common_len++;
  424. common_len = new_common_len;
  425. }
  426. match->cmd = name;
  427. list_add_tail(&match->lh, &matches);
  428. }
  429. Jim_DecrRefCount(command_context->interp, elem);
  430. }
  431. /* end of command filtering */
  432. /* proceed with auto-completion */
  433. if (list_empty(&matches))
  434. telnet_bell(connection);
  435. else if (common_len == usr_cmd_len && list_is_singular(&matches) && t_con->line_cursor == t_con->line_size)
  436. telnet_insert(connection, " ", 1);
  437. else if (common_len > usr_cmd_len) {
  438. int completion_size = common_len - usr_cmd_len;
  439. if (telnet_insert(connection, first_match + usr_cmd_len, completion_size)) {
  440. /* in bash this extra space is only added when the cursor in at the end of line */
  441. if (list_is_singular(&matches) && t_con->line_cursor == t_con->line_size)
  442. telnet_insert(connection, " ", 1);
  443. }
  444. } else if (!list_is_singular(&matches)) {
  445. telnet_write(connection, "\n\r", 2);
  446. struct cmd_match *match;
  447. list_for_each_entry(match, &matches, lh) {
  448. telnet_write(connection, match->cmd, strlen(match->cmd));
  449. telnet_write(connection, "\n\r", 2);
  450. }
  451. telnet_prompt(connection);
  452. telnet_write(connection, t_con->line, t_con->line_size);
  453. /* restore the terminal visible cursor location */
  454. for (size_t i = t_con->line_cursor; i < t_con->line_size; i++)
  455. telnet_write(connection, "\b", 1);
  456. }
  457. /* destroy the command_list */
  458. struct cmd_match *tmp, *match;
  459. list_for_each_entry_safe(match, tmp, &matches, lh)
  460. free(match);
  461. Jim_DecrRefCount(command_context->interp, list);
  462. }
  463. static int telnet_input(struct connection *connection)
  464. {
  465. int bytes_read;
  466. unsigned char buffer[TELNET_BUFFER_SIZE];
  467. unsigned char *buf_p;
  468. struct telnet_connection *t_con = connection->priv;
  469. struct command_context *command_context = connection->cmd_ctx;
  470. bytes_read = connection_read(connection, buffer, TELNET_BUFFER_SIZE);
  471. if (bytes_read == 0)
  472. return ERROR_SERVER_REMOTE_CLOSED;
  473. else if (bytes_read == -1) {
  474. LOG_ERROR("error during read: %s", strerror(errno));
  475. return ERROR_SERVER_REMOTE_CLOSED;
  476. }
  477. buf_p = buffer;
  478. while (bytes_read) {
  479. switch (t_con->state) {
  480. case TELNET_STATE_DATA:
  481. if (*buf_p == 0xff)
  482. t_con->state = TELNET_STATE_IAC;
  483. else {
  484. if (isprint(*buf_p)) { /* printable character */
  485. telnet_insert(connection, buf_p, 1);
  486. } else { /* non-printable */
  487. if (*buf_p == 0x1b) { /* escape */
  488. t_con->state = TELNET_STATE_ESCAPE;
  489. t_con->last_escape = '\x00';
  490. } else if ((*buf_p == 0xd) || (*buf_p == 0xa)) { /* CR/LF */
  491. int retval;
  492. /* skip over combinations with CR/LF and NUL characters */
  493. if ((bytes_read > 1) && ((*(buf_p + 1) == 0xa) ||
  494. (*(buf_p + 1) == 0xd))) {
  495. buf_p++;
  496. bytes_read--;
  497. }
  498. if ((bytes_read > 1) && (*(buf_p + 1) == 0)) {
  499. buf_p++;
  500. bytes_read--;
  501. }
  502. t_con->line[t_con->line_size] = 0;
  503. telnet_write(connection, "\r\n\x00", 3);
  504. if (strcmp(t_con->line, "history") == 0) {
  505. retval = telnet_history_print(connection);
  506. if (retval != ERROR_OK)
  507. return retval;
  508. continue;
  509. }
  510. /* save only non-blank not repeating lines in the history */
  511. char *prev_line = t_con->history[(t_con->current_history > 0) ?
  512. t_con->current_history - 1 : TELNET_LINE_HISTORY_SIZE-1];
  513. if (*t_con->line && (!prev_line ||
  514. strcmp(t_con->line, prev_line))) {
  515. /* if the history slot is already taken, free it */
  516. free(t_con->history[t_con->next_history]);
  517. /* add line to history */
  518. t_con->history[t_con->next_history] = strdup(t_con->line);
  519. /* wrap history at TELNET_LINE_HISTORY_SIZE */
  520. t_con->next_history = (t_con->next_history + 1) %
  521. TELNET_LINE_HISTORY_SIZE;
  522. /* current history line starts at the new entry */
  523. t_con->current_history =
  524. t_con->next_history;
  525. free(t_con->history[t_con->current_history]);
  526. t_con->history[t_con->current_history] = strdup("");
  527. }
  528. t_con->line_size = 0;
  529. /* to suppress prompt in log callback during command execution */
  530. t_con->prompt_visible = false;
  531. if (strcmp(t_con->line, "shutdown") == 0)
  532. telnet_save_history(t_con);
  533. retval = command_run_line(command_context, t_con->line);
  534. t_con->line_cursor = 0;
  535. t_con->prompt_visible = true;
  536. if (retval == ERROR_COMMAND_CLOSE_CONNECTION)
  537. return ERROR_SERVER_REMOTE_CLOSED;
  538. /* the prompt is always * placed at the line beginning */
  539. telnet_write(connection, "\r", 1);
  540. retval = telnet_prompt(connection);
  541. if (retval == ERROR_SERVER_REMOTE_CLOSED)
  542. return ERROR_SERVER_REMOTE_CLOSED;
  543. } else if ((*buf_p == 0x7f) || (*buf_p == 0x8)) { /* delete character */
  544. if (t_con->line_cursor > 0) {
  545. if (t_con->line_cursor != t_con->line_size) {
  546. size_t i;
  547. telnet_write(connection, "\b", 1);
  548. t_con->line_cursor--;
  549. t_con->line_size--;
  550. memmove(t_con->line + t_con->line_cursor,
  551. t_con->line + t_con->line_cursor + 1,
  552. t_con->line_size -
  553. t_con->line_cursor);
  554. telnet_write(connection,
  555. t_con->line + t_con->line_cursor,
  556. t_con->line_size -
  557. t_con->line_cursor);
  558. telnet_write(connection, " \b", 2);
  559. for (i = t_con->line_cursor; i < t_con->line_size; i++)
  560. telnet_write(connection, "\b", 1);
  561. } else {
  562. t_con->line_size--;
  563. t_con->line_cursor--;
  564. /* back space: move the 'printer' head one char
  565. * back, overwrite with space, move back again */
  566. telnet_write(connection, "\b \b", 3);
  567. }
  568. }
  569. } else if (*buf_p == 0x15) /* clear line */
  570. telnet_clear_line(connection, t_con);
  571. else if (*buf_p == CTRL('B')) { /* cursor left */
  572. if (t_con->line_cursor > 0) {
  573. telnet_write(connection, "\b", 1);
  574. t_con->line_cursor--;
  575. }
  576. t_con->state = TELNET_STATE_DATA;
  577. } else if (*buf_p == CTRL('F')) { /* cursor right */
  578. if (t_con->line_cursor < t_con->line_size)
  579. telnet_write(connection, t_con->line + t_con->line_cursor++, 1);
  580. t_con->state = TELNET_STATE_DATA;
  581. } else if (*buf_p == CTRL('P')) /* cursor up */
  582. telnet_history_up(connection);
  583. else if (*buf_p == CTRL('N')) /* cursor down */
  584. telnet_history_down(connection);
  585. else if (*buf_p == CTRL('A'))
  586. telnet_move_cursor(connection, 0);
  587. else if (*buf_p == CTRL('E'))
  588. telnet_move_cursor(connection, t_con->line_size);
  589. else if (*buf_p == CTRL('K')) { /* kill line to end */
  590. if (t_con->line_cursor < t_con->line_size) {
  591. /* overwrite with space, until end of line, move back */
  592. for (size_t i = t_con->line_cursor; i < t_con->line_size; i++)
  593. telnet_write(connection, " ", 1);
  594. for (size_t i = t_con->line_cursor; i < t_con->line_size; i++)
  595. telnet_write(connection, "\b", 1);
  596. t_con->line[t_con->line_cursor] = '\0';
  597. t_con->line_size = t_con->line_cursor;
  598. }
  599. } else if (*buf_p == '\t')
  600. telnet_auto_complete(connection);
  601. else
  602. LOG_DEBUG("unhandled nonprintable: %2.2x", *buf_p);
  603. }
  604. }
  605. break;
  606. case TELNET_STATE_IAC:
  607. switch (*buf_p) {
  608. case 0xfe:
  609. t_con->state = TELNET_STATE_DONT;
  610. break;
  611. case 0xfd:
  612. t_con->state = TELNET_STATE_DO;
  613. break;
  614. case 0xfc:
  615. t_con->state = TELNET_STATE_WONT;
  616. break;
  617. case 0xfb:
  618. t_con->state = TELNET_STATE_WILL;
  619. break;
  620. }
  621. break;
  622. case TELNET_STATE_SB:
  623. break;
  624. case TELNET_STATE_SE:
  625. break;
  626. case TELNET_STATE_WILL:
  627. case TELNET_STATE_WONT:
  628. case TELNET_STATE_DO:
  629. case TELNET_STATE_DONT:
  630. t_con->state = TELNET_STATE_DATA;
  631. break;
  632. case TELNET_STATE_ESCAPE:
  633. if (t_con->last_escape == '[') {
  634. if (*buf_p == 'D') { /* cursor left */
  635. if (t_con->line_cursor > 0) {
  636. telnet_write(connection, "\b", 1);
  637. t_con->line_cursor--;
  638. }
  639. t_con->state = TELNET_STATE_DATA;
  640. } else if (*buf_p == 'C') { /* cursor right */
  641. if (t_con->line_cursor < t_con->line_size)
  642. telnet_write(connection,
  643. t_con->line + t_con->line_cursor++, 1);
  644. t_con->state = TELNET_STATE_DATA;
  645. } else if (*buf_p == 'A') { /* cursor up */
  646. telnet_history_up(connection);
  647. } else if (*buf_p == 'B') { /* cursor down */
  648. telnet_history_down(connection);
  649. } else if (*buf_p == 'F') { /* end key */
  650. telnet_move_cursor(connection, t_con->line_size);
  651. t_con->state = TELNET_STATE_DATA;
  652. } else if (*buf_p == 'H') { /* home key */
  653. telnet_move_cursor(connection, 0);
  654. t_con->state = TELNET_STATE_DATA;
  655. } else if (*buf_p == '3')
  656. t_con->last_escape = *buf_p;
  657. else
  658. t_con->state = TELNET_STATE_DATA;
  659. } else if (t_con->last_escape == '3') {
  660. /* Remove character */
  661. if (*buf_p == '~') {
  662. if (t_con->line_cursor < t_con->line_size) {
  663. size_t i;
  664. t_con->line_size--;
  665. /* remove char from line buffer */
  666. memmove(t_con->line + t_con->line_cursor,
  667. t_con->line + t_con->line_cursor + 1,
  668. t_con->line_size - t_con->line_cursor);
  669. /* print remainder of buffer */
  670. telnet_write(connection, t_con->line + t_con->line_cursor,
  671. t_con->line_size - t_con->line_cursor);
  672. /* overwrite last char with whitespace */
  673. telnet_write(connection, " \b", 2);
  674. /* move back to cursor position*/
  675. for (i = t_con->line_cursor; i < t_con->line_size; i++)
  676. telnet_write(connection, "\b", 1);
  677. }
  678. t_con->state = TELNET_STATE_DATA;
  679. } else
  680. t_con->state = TELNET_STATE_DATA;
  681. } else if (t_con->last_escape == '\x00') {
  682. if (*buf_p == '[')
  683. t_con->last_escape = *buf_p;
  684. else
  685. t_con->state = TELNET_STATE_DATA;
  686. } else {
  687. LOG_ERROR("BUG: unexpected value in t_con->last_escape");
  688. t_con->state = TELNET_STATE_DATA;
  689. }
  690. break;
  691. default:
  692. LOG_ERROR("unknown telnet state");
  693. return ERROR_FAIL;
  694. }
  695. bytes_read--;
  696. buf_p++;
  697. }
  698. return ERROR_OK;
  699. }
  700. static int telnet_connection_closed(struct connection *connection)
  701. {
  702. struct telnet_connection *t_con = connection->priv;
  703. int i;
  704. log_remove_callback(telnet_log_callback, connection);
  705. free(t_con->prompt);
  706. t_con->prompt = NULL;
  707. /* save telnet history */
  708. telnet_save_history(t_con);
  709. for (i = 0; i < TELNET_LINE_HISTORY_SIZE; i++) {
  710. free(t_con->history[i]);
  711. t_con->history[i] = NULL;
  712. }
  713. /* if this connection registered a debug-message receiver delete it */
  714. delete_debug_msg_receiver(connection->cmd_ctx, NULL);
  715. free(connection->priv);
  716. connection->priv = NULL;
  717. return ERROR_OK;
  718. }
  719. int telnet_init(char *banner)
  720. {
  721. if (strcmp(telnet_port, "disabled") == 0) {
  722. LOG_INFO("telnet server disabled");
  723. return ERROR_OK;
  724. }
  725. struct telnet_service *telnet_service =
  726. malloc(sizeof(struct telnet_service));
  727. if (!telnet_service) {
  728. LOG_ERROR("Failed to allocate telnet service.");
  729. return ERROR_FAIL;
  730. }
  731. telnet_service->banner = banner;
  732. int ret = add_service("telnet", telnet_port, CONNECTION_LIMIT_UNLIMITED,
  733. telnet_new_connection, telnet_input, telnet_connection_closed,
  734. telnet_service);
  735. if (ret != ERROR_OK) {
  736. free(telnet_service);
  737. return ret;
  738. }
  739. return ERROR_OK;
  740. }
  741. /* daemon configuration command telnet_port */
  742. COMMAND_HANDLER(handle_telnet_port_command)
  743. {
  744. return CALL_COMMAND_HANDLER(server_pipe_command, &telnet_port);
  745. }
  746. COMMAND_HANDLER(handle_exit_command)
  747. {
  748. return ERROR_COMMAND_CLOSE_CONNECTION;
  749. }
  750. static const struct command_registration telnet_command_handlers[] = {
  751. {
  752. .name = "exit",
  753. .handler = handle_exit_command,
  754. .mode = COMMAND_EXEC,
  755. .usage = "",
  756. .help = "exit telnet session",
  757. },
  758. {
  759. .name = "telnet_port",
  760. .handler = handle_telnet_port_command,
  761. .mode = COMMAND_CONFIG,
  762. .help = "Specify port on which to listen "
  763. "for incoming telnet connections. "
  764. "Read help on 'gdb_port'.",
  765. .usage = "[port_num]",
  766. },
  767. COMMAND_REGISTRATION_DONE
  768. };
  769. int telnet_register_commands(struct command_context *cmd_ctx)
  770. {
  771. telnet_port = strdup("4444");
  772. return register_commands(cmd_ctx, NULL, telnet_command_handlers);
  773. }
  774. void telnet_service_free(void)
  775. {
  776. free(telnet_port);
  777. }