1 /***************************************************************************
\r
2 * Copyright (C) 2005 by Dominic Rath *
\r
3 * Dominic.Rath@gmx.de *
\r
5 * This program is free software; you can redistribute it and/or modify *
\r
6 * it under the terms of the GNU General Public License as published by *
\r
7 * the Free Software Foundation; either version 2 of the License, or *
\r
8 * (at your option) any later version. *
\r
10 * This program is distributed in the hope that it will be useful, *
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
\r
13 * GNU General Public License for more details. *
\r
15 * You should have received a copy of the GNU General Public License *
\r
16 * along with this program; if not, write to the *
\r
17 * Free Software Foundation, Inc., *
\r
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
\r
19 ***************************************************************************/
\r
20 #ifdef HAVE_CONFIG_H
\r
24 #include "replacements.h"
\r
26 #include "telnet_server.h"
\r
30 #include "command.h"
\r
32 #include "target_request.h"
\r
40 static unsigned short telnet_port = 0;
\r
42 int handle_exit_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
\r
43 int handle_telnet_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
\r
45 static char *negotiate =
\r
46 "\xFF\xFB\x03" /* IAC WILL Suppress Go Ahead */
\r
47 "\xFF\xFB\x01" /* IAC WILL Echo */
\r
48 "\xFF\xFD\x03" /* IAC DO Suppress Go Ahead */
\r
49 "\xFF\xFE\x01"; /* IAC DON'T Echo */
\r
51 #define CTRL(c) (c - '@')
\r
53 /* The only way we can detect that the socket is closed is the first time
\r
54 * we write to it, we will fail. Subsequent write operations will
\r
57 int telnet_write(connection_t *connection, void *data, int len)
\r
59 telnet_connection_t *t_con = connection->priv;
\r
61 return ERROR_SERVER_REMOTE_CLOSED;
\r
63 if (write_socket(connection->fd, data, len) == len)
\r
68 return ERROR_SERVER_REMOTE_CLOSED;
\r
71 int telnet_prompt(connection_t *connection)
\r
73 telnet_connection_t *t_con = connection->priv;
\r
75 return telnet_write(connection, t_con->prompt, strlen(t_con->prompt));
\r
78 int telnet_outputline(connection_t *connection, char* line)
\r
80 telnet_write(connection, line, strlen(line));
\r
81 return telnet_write(connection, "\r\n\0", 3);
\r
84 int telnet_output(struct command_context_s *cmd_ctx, char* line)
\r
86 connection_t *connection = cmd_ctx->output_handler_priv;
\r
88 return telnet_outputline(connection, line);
\r
91 void telnet_log_callback(void *priv, const char *file, int line,
\r
92 const char *function, const char *format, va_list args)
\r
94 connection_t *connection = priv;
\r
95 char *t = alloc_printf(format, args);
\r
103 if ((endline=strchr(t2, '\n'))!=NULL)
\r
107 telnet_outputline(connection, t2);
\r
114 int telnet_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv)
\r
116 struct command_context_s *cmd_ctx = priv;
\r
117 connection_t *connection = cmd_ctx->output_handler_priv;
\r
118 telnet_connection_t *t_con = connection->priv;
\r
122 case TARGET_EVENT_HALTED:
\r
123 target_arch_state(target);
\r
124 if (!t_con->suppress_prompt)
\r
125 telnet_prompt(connection);
\r
127 case TARGET_EVENT_RESUMED:
\r
128 if (!t_con->suppress_prompt)
\r
129 telnet_prompt(connection);
\r
138 int telnet_new_connection(connection_t *connection)
\r
140 telnet_connection_t *telnet_connection = malloc(sizeof(telnet_connection_t));
\r
141 telnet_service_t *telnet_service = connection->service->priv;
\r
144 connection->priv = telnet_connection;
\r
146 /* initialize telnet connection information */
\r
147 telnet_connection->closed = 0;
\r
148 telnet_connection->line_size = 0;
\r
149 telnet_connection->line_cursor = 0;
\r
150 telnet_connection->option_size = 0;
\r
151 telnet_connection->prompt = strdup("> ");
\r
152 telnet_connection->suppress_prompt = 0;
\r
153 telnet_connection->state = TELNET_STATE_DATA;
\r
155 /* output goes through telnet connection */
\r
156 command_set_output_handler(connection->cmd_ctx, telnet_output, connection);
\r
158 /* negotiate telnet options */
\r
159 telnet_write(connection, negotiate, strlen(negotiate));
\r
161 /* print connection banner */
\r
162 if (telnet_service->banner)
\r
164 telnet_write(connection, telnet_service->banner, strlen(telnet_service->banner));
\r
165 telnet_write(connection, "\r\n\0", 3);
\r
168 telnet_prompt(connection);
\r
170 /* initialize history */
\r
171 for (i = 0; i < TELNET_LINE_HISTORY_SIZE; i++)
\r
173 telnet_connection->history[i] = NULL;
\r
175 telnet_connection->next_history = 0;
\r
176 telnet_connection->current_history = 0;
\r
178 target_register_event_callback(telnet_target_callback_event_handler, connection->cmd_ctx);
\r
183 void telnet_clear_line(connection_t *connection, telnet_connection_t *t_con)
\r
185 /* move to end of line */
\r
186 if (t_con->line_cursor < t_con->line_size)
\r
188 telnet_write(connection, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);
\r
191 /* backspace, overwrite with space, backspace */
\r
192 while (t_con->line_size > 0)
\r
194 telnet_write(connection, "\b \b", 3);
\r
195 t_con->line_size--;
\r
197 t_con->line_cursor = 0;
\r
200 int telnet_input(connection_t *connection)
\r
203 char buffer[TELNET_BUFFER_SIZE];
\r
205 telnet_connection_t *t_con = connection->priv;
\r
206 command_context_t *command_context = connection->cmd_ctx;
\r
208 bytes_read = read_socket(connection->fd, buffer, TELNET_BUFFER_SIZE);
\r
210 if (bytes_read == 0)
\r
211 return ERROR_SERVER_REMOTE_CLOSED;
\r
212 else if (bytes_read == -1)
\r
214 ERROR("error during read: %s", strerror(errno));
\r
215 return ERROR_SERVER_REMOTE_CLOSED;
\r
221 switch (t_con->state)
\r
223 case TELNET_STATE_DATA:
\r
224 if (*buf_p == '\xff')
\r
226 t_con->state = TELNET_STATE_IAC;
\r
230 if (isprint(*buf_p)) /* printable character */
\r
232 telnet_write(connection, buf_p, 1);
\r
233 if (t_con->line_cursor == t_con->line_size)
\r
235 t_con->line[t_con->line_size++] = *buf_p;
\r
236 t_con->line_cursor++;
\r
241 memmove(t_con->line + t_con->line_cursor + 1, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);
\r
242 t_con->line[t_con->line_cursor++] = *buf_p;
\r
243 t_con->line_size++;
\r
244 telnet_write(connection, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);
\r
245 for (i = t_con->line_cursor; i < t_con->line_size; i++)
\r
247 telnet_write(connection, "\b", 1);
\r
251 else /* non-printable */
\r
253 if (*buf_p == 0x1b) /* escape */
\r
255 t_con->state = TELNET_STATE_ESCAPE;
\r
256 t_con->last_escape = '\x00';
\r
258 else if ((*buf_p == 0xd) || (*buf_p == 0xa)) /* CR/LF */
\r
262 /* skip over combinations with CR/LF + NUL */
\r
263 if (((*(buf_p + 1) == 0xa) || (*(buf_p + 1) == 0xd)) && (bytes_read > 1))
\r
268 if ((*(buf_p + 1) == 0) && (bytes_read > 1))
\r
273 t_con->line[t_con->line_size] = 0;
\r
275 telnet_write(connection, "\r\n\x00", 3);
\r
277 if (strcmp(t_con->line, "history") == 0)
\r
280 for (i = 0; i < TELNET_LINE_HISTORY_SIZE; i++)
\r
282 if (t_con->history[i])
\r
284 telnet_write(connection, t_con->history[i], strlen(t_con->history[i]));
\r
285 telnet_write(connection, "\r\n\x00", 3);
\r
288 telnet_prompt(connection);
\r
289 t_con->line_size = 0;
\r
290 t_con->line_cursor = 0;
\r
294 log_add_callback(telnet_log_callback, connection);
\r
295 t_con->suppress_prompt = 1;
\r
297 retval = command_run_line(command_context, t_con->line);
\r
299 log_remove_callback(telnet_log_callback, connection);
\r
300 t_con->suppress_prompt = 0;
\r
302 if (retval == ERROR_COMMAND_CLOSE_CONNECTION)
\r
304 return ERROR_SERVER_REMOTE_CLOSED;
\r
307 /* Save only non-blank lines in the history */
\r
308 if (t_con->line_size > 0)
\r
310 /* if the history slot is already taken, free it */
\r
311 if (t_con->history[t_con->next_history])
\r
313 free(t_con->history[t_con->next_history]);
\r
316 /* add line to history */
\r
317 t_con->history[t_con->next_history] = strdup(t_con->line);
\r
319 /* wrap history at TELNET_LINE_HISTORY_SIZE */
\r
320 t_con->next_history = (t_con->next_history + 1) % TELNET_LINE_HISTORY_SIZE;
\r
322 /* current history line starts at the new entry */
\r
323 t_con->current_history = t_con->next_history;
\r
325 if (t_con->history[t_con->current_history])
\r
327 free(t_con->history[t_con->current_history]);
\r
329 t_con->history[t_con->current_history] = strdup("");
\r
332 int t = telnet_prompt(connection);
\r
333 if (t == ERROR_SERVER_REMOTE_CLOSED)
\r
336 t_con->line_size = 0;
\r
337 t_con->line_cursor = 0;
\r
339 else if ((*buf_p == 0x7f) || (*buf_p == 0x8)) /* delete character */
\r
341 if (t_con->line_cursor > 0)
\r
343 if (t_con->line_cursor != t_con->line_size)
\r
346 telnet_write(connection, "\b", 1);
\r
347 t_con->line_cursor--;
\r
348 t_con->line_size--;
\r
349 memmove(t_con->line + t_con->line_cursor, t_con->line + t_con->line_cursor + 1, t_con->line_size - t_con->line_cursor);
\r
351 telnet_write(connection, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);
\r
352 telnet_write(connection, " \b", 2);
\r
353 for (i = t_con->line_cursor; i < t_con->line_size; i++)
\r
355 telnet_write(connection, "\b", 1);
\r
360 t_con->line_size--;
\r
361 t_con->line_cursor--;
\r
362 /* back space: move the 'printer' head one char back, overwrite with space, move back again */
\r
363 telnet_write(connection, "\b \b", 3);
\r
367 else if (*buf_p == 0x15) /* clear line */
\r
369 telnet_clear_line(connection, t_con);
\r
371 else if (*buf_p == CTRL('B')) /* cursor left */
\r
373 if (t_con->line_cursor > 0)
\r
375 telnet_write(connection, "\b", 1);
\r
376 t_con->line_cursor--;
\r
378 t_con->state = TELNET_STATE_DATA;
\r
380 else if (*buf_p == CTRL('F')) /* cursor right */
\r
382 if (t_con->line_cursor < t_con->line_size)
\r
384 telnet_write(connection, t_con->line + t_con->line_cursor++, 1);
\r
386 t_con->state = TELNET_STATE_DATA;
\r
390 DEBUG("unhandled nonprintable: %2.2x", *buf_p);
\r
395 case TELNET_STATE_IAC:
\r
399 t_con->state = TELNET_STATE_DONT;
\r
402 t_con->state = TELNET_STATE_DO;
\r
405 t_con->state = TELNET_STATE_WONT;
\r
408 t_con->state = TELNET_STATE_WILL;
\r
412 case TELNET_STATE_SB:
\r
414 case TELNET_STATE_SE:
\r
416 case TELNET_STATE_WILL:
\r
417 case TELNET_STATE_WONT:
\r
418 case TELNET_STATE_DO:
\r
419 case TELNET_STATE_DONT:
\r
420 t_con->state = TELNET_STATE_DATA;
\r
422 case TELNET_STATE_ESCAPE:
\r
423 if (t_con->last_escape == '[')
\r
425 if (*buf_p == 'D') /* cursor left */
\r
427 if (t_con->line_cursor > 0)
\r
429 telnet_write(connection, "\b", 1);
\r
430 t_con->line_cursor--;
\r
432 t_con->state = TELNET_STATE_DATA;
\r
434 else if (*buf_p == 'C') /* cursor right */
\r
436 if (t_con->line_cursor < t_con->line_size)
\r
438 telnet_write(connection, t_con->line + t_con->line_cursor++, 1);
\r
440 t_con->state = TELNET_STATE_DATA;
\r
442 else if (*buf_p == 'A') /* cursor up */
\r
444 int last_history = (t_con->current_history > 0) ? t_con->current_history - 1 : TELNET_LINE_HISTORY_SIZE-1;
\r
445 if (t_con->history[last_history])
\r
447 telnet_clear_line(connection, t_con);
\r
448 t_con->line_size = strlen(t_con->history[last_history]);
\r
449 t_con->line_cursor = t_con->line_size;
\r
450 memcpy(t_con->line, t_con->history[last_history], t_con->line_size + 1);
\r
451 telnet_write(connection, t_con->line, t_con->line_size);
\r
452 t_con->current_history = last_history;
\r
454 t_con->state = TELNET_STATE_DATA;
\r
456 else if (*buf_p == 'B') /* cursor down */
\r
458 int next_history = (t_con->current_history + 1) % TELNET_LINE_HISTORY_SIZE;
\r
459 if (t_con->history[next_history])
\r
461 telnet_clear_line(connection, t_con);
\r
462 t_con->line_size = strlen(t_con->history[next_history]);
\r
463 t_con->line_cursor = t_con->line_size;
\r
464 memcpy(t_con->line, t_con->history[next_history], t_con->line_size + 1);
\r
465 telnet_write(connection, t_con->line, t_con->line_size);
\r
466 t_con->current_history = next_history;
\r
468 t_con->state = TELNET_STATE_DATA;
\r
470 else if (*buf_p == '3')
\r
472 t_con->last_escape = *buf_p;
\r
476 t_con->state = TELNET_STATE_DATA;
\r
479 else if (t_con->last_escape == '3')
\r
481 /* Remove character */
\r
484 if (t_con->line_cursor < t_con->line_size)
\r
487 t_con->line_size--;
\r
488 /* remove char from line buffer */
\r
489 memmove(t_con->line + t_con->line_cursor, t_con->line + t_con->line_cursor + 1, t_con->line_size - t_con->line_cursor);
\r
491 /* print remainder of buffer */
\r
492 telnet_write(connection, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);
\r
493 /* overwrite last char with whitespace */
\r
494 telnet_write(connection, " \b", 2);
\r
496 /* move back to cursor position*/
\r
497 for (i = t_con->line_cursor; i < t_con->line_size; i++)
\r
499 telnet_write(connection, "\b", 1);
\r
503 t_con->state = TELNET_STATE_DATA;
\r
507 t_con->state = TELNET_STATE_DATA;
\r
510 else if (t_con->last_escape == '\x00')
\r
514 t_con->last_escape = *buf_p;
\r
518 t_con->state = TELNET_STATE_DATA;
\r
523 ERROR("BUG: unexpected value in t_con->last_escape");
\r
524 t_con->state = TELNET_STATE_DATA;
\r
529 ERROR("unknown telnet state");
\r
540 int telnet_connection_closed(connection_t *connection)
\r
542 telnet_connection_t *t_con = connection->priv;
\r
547 free(t_con->prompt);
\r
548 t_con->prompt = NULL;
\r
551 for (i = 0; i < TELNET_LINE_HISTORY_SIZE; i++)
\r
553 if (t_con->history[i])
\r
555 free(t_con->history[i]);
\r
556 t_con->history[i] = NULL;
\r
560 /* if this connection registered a debug-message receiver delete it */
\r
561 delete_debug_msg_receiver(connection->cmd_ctx, NULL);
\r
563 if (connection->priv)
\r
565 free(connection->priv);
\r
566 connection->priv = NULL;
\r
570 ERROR("BUG: connection->priv == NULL");
\r
573 target_unregister_event_callback(telnet_target_callback_event_handler, connection->cmd_ctx);
\r
578 int telnet_set_prompt(connection_t *connection, char *prompt)
\r
580 telnet_connection_t *t_con = connection->priv;
\r
582 t_con->prompt = strdup(prompt);
\r
587 int telnet_init(char *banner)
\r
589 telnet_service_t *telnet_service = malloc(sizeof(telnet_service_t));
\r
591 if (telnet_port == 0)
\r
593 WARNING("no telnet port specified, using default port 4444");
\r
594 telnet_port = 4444;
\r
597 telnet_service->banner = banner;
\r
599 add_service("telnet", CONNECTION_TELNET, telnet_port, 1, telnet_new_connection, telnet_input, telnet_connection_closed, telnet_service);
\r
604 int telnet_register_commands(command_context_t *command_context)
\r
606 register_command(command_context, NULL, "exit", handle_exit_command,
\r
607 COMMAND_EXEC, "exit telnet session");
\r
609 register_command(command_context, NULL, "telnet_port", handle_telnet_port_command,
\r
610 COMMAND_CONFIG, "");
\r
615 /* daemon configuration command telnet_port */
\r
616 int handle_telnet_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
\r
621 /* only if the port wasn't overwritten by cmdline */
\r
622 if (telnet_port == 0)
\r
623 telnet_port = strtoul(args[0], NULL, 0);
\r
628 int handle_exit_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
\r
630 return ERROR_COMMAND_CLOSE_CONNECTION;
\r