1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
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. *
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. *
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 ***************************************************************************/
30 #include "replacements.h"
32 #include "telnet_server.h"
38 #include "target_request.h"
46 static unsigned short telnet_port = 0;
48 int handle_exit_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
49 int handle_telnet_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
51 static int telnet_async()
53 return jim_global_long("telnet_async_state");
56 static char *negotiate =
57 "\xFF\xFB\x03" /* IAC WILL Suppress Go Ahead */
58 "\xFF\xFB\x01" /* IAC WILL Echo */
59 "\xFF\xFD\x03" /* IAC DO Suppress Go Ahead */
60 "\xFF\xFE\x01"; /* IAC DON'T Echo */
62 #define CTRL(c) (c - '@')
64 /* The only way we can detect that the socket is closed is the first time
65 * we write to it, we will fail. Subsequent write operations will
68 int telnet_write(connection_t *connection, const void *data, int len)
70 telnet_connection_t *t_con = connection->priv;
72 return ERROR_SERVER_REMOTE_CLOSED;
74 if (write_socket(connection->fd, data, len) == len)
79 return ERROR_SERVER_REMOTE_CLOSED;
82 int telnet_prompt(connection_t *connection)
84 telnet_connection_t *t_con = connection->priv;
86 telnet_write(connection, "\r", 1); /* the prompt is always placed at the line beginning */
87 return telnet_write(connection, t_con->prompt, strlen(t_con->prompt));
90 int telnet_outputline(connection_t *connection, const char *line)
94 /* process lines in buffer */
96 char *line_end = strchr(line, '\n');
103 telnet_write(connection, line, len);
106 telnet_write(connection, "\r\n", 2);
118 int telnet_output(struct command_context_s *cmd_ctx, const char* line)
120 connection_t *connection = cmd_ctx->output_handler_priv;
122 return telnet_outputline(connection, line);
125 void telnet_log_callback(void *priv, const char *file, int line,
126 const char *function, const char *string)
128 connection_t *connection = priv;
129 telnet_connection_t *t_con = connection->priv;
132 /* if there is no prompt, simply output the message */
133 if (t_con->line_cursor < 0)
135 telnet_outputline(connection, string);
139 /* clear the command line */
140 telnet_write(connection, "\r", 1);
141 for (i = strlen(t_con->prompt) + t_con->line_size; i>0; i-=16)
142 telnet_write(connection, " ", i>16 ? 16 : i);
143 telnet_write(connection, "\r", 1);
145 /* output the message */
146 telnet_outputline(connection, string);
148 /* put the command line to its previous state */
149 telnet_prompt(connection);
150 telnet_write(connection, t_con->line, t_con->line_size);
151 for (i=t_con->line_size; i>t_con->line_cursor; i--)
152 telnet_write(connection, "\b", 1);
155 int telnet_new_connection(connection_t *connection)
157 telnet_connection_t *telnet_connection = malloc(sizeof(telnet_connection_t));
158 telnet_service_t *telnet_service = connection->service->priv;
161 connection->priv = telnet_connection;
163 /* initialize telnet connection information */
164 telnet_connection->closed = 0;
165 telnet_connection->line_size = 0;
166 telnet_connection->line_cursor = 0;
167 telnet_connection->option_size = 0;
168 telnet_connection->prompt = strdup("> ");
169 telnet_connection->state = TELNET_STATE_DATA;
171 /* output goes through telnet connection */
172 command_set_output_handler(connection->cmd_ctx, telnet_output, connection);
174 /* negotiate telnet options */
175 telnet_write(connection, negotiate, strlen(negotiate));
177 /* print connection banner */
178 if (telnet_service->banner)
180 telnet_write(connection, telnet_service->banner, strlen(telnet_service->banner));
181 telnet_write(connection, "\r\n", 2);
184 telnet_prompt(connection);
186 /* initialize history */
187 for (i = 0; i < TELNET_LINE_HISTORY_SIZE; i++)
189 telnet_connection->history[i] = NULL;
191 telnet_connection->next_history = 0;
192 telnet_connection->current_history = 0;
195 log_add_callback(telnet_log_callback, connection);
202 void telnet_clear_line(connection_t *connection, telnet_connection_t *t_con)
204 /* move to end of line */
205 if (t_con->line_cursor < t_con->line_size)
207 telnet_write(connection, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);
210 /* backspace, overwrite with space, backspace */
211 while (t_con->line_size > 0)
213 telnet_write(connection, "\b \b", 3);
216 t_con->line_cursor = 0;
219 int telnet_input(connection_t *connection)
222 char buffer[TELNET_BUFFER_SIZE];
224 telnet_connection_t *t_con = connection->priv;
225 command_context_t *command_context = connection->cmd_ctx;
227 bytes_read = read_socket(connection->fd, buffer, TELNET_BUFFER_SIZE);
230 return ERROR_SERVER_REMOTE_CLOSED;
231 else if (bytes_read == -1)
233 LOG_ERROR("error during read: %s", strerror(errno));
234 return ERROR_SERVER_REMOTE_CLOSED;
240 switch (t_con->state)
242 case TELNET_STATE_DATA:
243 if (*buf_p == '\xff')
245 t_con->state = TELNET_STATE_IAC;
249 if (isprint(*buf_p)) /* printable character */
251 /* watch buffer size leaving one spare character for string null termination */
252 if (t_con->line_size == TELNET_LINE_MAX_SIZE-1)
254 /* output audible bell if buffer is full */
255 telnet_write(connection, "\x07", 1); /* "\a" does not work, at least on windows */
257 else if (t_con->line_cursor == t_con->line_size)
259 telnet_write(connection, buf_p, 1);
260 t_con->line[t_con->line_size++] = *buf_p;
261 t_con->line_cursor++;
266 memmove(t_con->line + t_con->line_cursor + 1, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);
267 t_con->line[t_con->line_cursor] = *buf_p;
269 telnet_write(connection, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);
270 t_con->line_cursor++;
271 for (i = t_con->line_cursor; i < t_con->line_size; i++)
273 telnet_write(connection, "\b", 1);
277 else /* non-printable */
279 if (*buf_p == 0x1b) /* escape */
281 t_con->state = TELNET_STATE_ESCAPE;
282 t_con->last_escape = '\x00';
284 else if ((*buf_p == 0xd) || (*buf_p == 0xa)) /* CR/LF */
288 /* skip over combinations with CR/LF and NUL characters */
289 if ((bytes_read > 1) && ((*(buf_p + 1) == 0xa) || (*(buf_p + 1) == 0xd)))
294 if ((bytes_read > 1) && (*(buf_p + 1) == 0))
299 t_con->line[t_con->line_size] = 0;
301 telnet_write(connection, "\r\n\x00", 3);
303 if (strcmp(t_con->line, "history") == 0)
306 for (i = 1; i < TELNET_LINE_HISTORY_SIZE; i++)
308 /* the t_con->next_history line contains empty string (unless NULL), thus it is not printed */
309 char *history_line = t_con->history[(t_con->next_history + i) % TELNET_LINE_HISTORY_SIZE];
312 telnet_write(connection, history_line, strlen(history_line));
313 telnet_write(connection, "\r\n\x00", 3);
316 t_con->line_size = 0;
317 t_con->line_cursor = 0;
321 /* save only non-blank not repeating lines in the history */
322 char *prev_line = t_con->history[(t_con->current_history > 0) ? t_con->current_history - 1 : TELNET_LINE_HISTORY_SIZE-1];
323 if (*t_con->line && (prev_line == NULL || strcmp(t_con->line, prev_line)))
325 /* if the history slot is already taken, free it */
326 if (t_con->history[t_con->next_history])
328 free(t_con->history[t_con->next_history]);
331 /* add line to history */
332 t_con->history[t_con->next_history] = strdup(t_con->line);
334 /* wrap history at TELNET_LINE_HISTORY_SIZE */
335 t_con->next_history = (t_con->next_history + 1) % TELNET_LINE_HISTORY_SIZE;
337 /* current history line starts at the new entry */
338 t_con->current_history = t_con->next_history;
340 if (t_con->history[t_con->current_history])
342 free(t_con->history[t_con->current_history]);
344 t_con->history[t_con->current_history] = strdup("");
347 t_con->line_size = 0;
349 t_con->line_cursor = -1; /* to supress prompt in log callback during command execution */
352 log_add_callback(telnet_log_callback, connection);
354 retval = command_run_line(command_context, t_con->line);
357 log_remove_callback(telnet_log_callback, connection);
359 t_con->line_cursor = 0;
361 if (retval == ERROR_COMMAND_CLOSE_CONNECTION)
362 return ERROR_SERVER_REMOTE_CLOSED;
364 retval = telnet_prompt(connection);
365 if (retval == ERROR_SERVER_REMOTE_CLOSED)
366 return ERROR_SERVER_REMOTE_CLOSED;
369 else if ((*buf_p == 0x7f) || (*buf_p == 0x8)) /* delete character */
371 if (t_con->line_cursor > 0)
373 if (t_con->line_cursor != t_con->line_size)
376 telnet_write(connection, "\b", 1);
377 t_con->line_cursor--;
379 memmove(t_con->line + t_con->line_cursor, t_con->line + t_con->line_cursor + 1, t_con->line_size - t_con->line_cursor);
381 telnet_write(connection, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);
382 telnet_write(connection, " \b", 2);
383 for (i = t_con->line_cursor; i < t_con->line_size; i++)
385 telnet_write(connection, "\b", 1);
391 t_con->line_cursor--;
392 /* back space: move the 'printer' head one char back, overwrite with space, move back again */
393 telnet_write(connection, "\b \b", 3);
397 else if (*buf_p == 0x15) /* clear line */
399 telnet_clear_line(connection, t_con);
401 else if (*buf_p == CTRL('B')) /* cursor left */
403 if (t_con->line_cursor > 0)
405 telnet_write(connection, "\b", 1);
406 t_con->line_cursor--;
408 t_con->state = TELNET_STATE_DATA;
410 else if (*buf_p == CTRL('F')) /* cursor right */
412 if (t_con->line_cursor < t_con->line_size)
414 telnet_write(connection, t_con->line + t_con->line_cursor++, 1);
416 t_con->state = TELNET_STATE_DATA;
420 LOG_DEBUG("unhandled nonprintable: %2.2x", *buf_p);
425 case TELNET_STATE_IAC:
429 t_con->state = TELNET_STATE_DONT;
432 t_con->state = TELNET_STATE_DO;
435 t_con->state = TELNET_STATE_WONT;
438 t_con->state = TELNET_STATE_WILL;
442 case TELNET_STATE_SB:
444 case TELNET_STATE_SE:
446 case TELNET_STATE_WILL:
447 case TELNET_STATE_WONT:
448 case TELNET_STATE_DO:
449 case TELNET_STATE_DONT:
450 t_con->state = TELNET_STATE_DATA;
452 case TELNET_STATE_ESCAPE:
453 if (t_con->last_escape == '[')
455 if (*buf_p == 'D') /* cursor left */
457 if (t_con->line_cursor > 0)
459 telnet_write(connection, "\b", 1);
460 t_con->line_cursor--;
462 t_con->state = TELNET_STATE_DATA;
464 else if (*buf_p == 'C') /* cursor right */
466 if (t_con->line_cursor < t_con->line_size)
468 telnet_write(connection, t_con->line + t_con->line_cursor++, 1);
470 t_con->state = TELNET_STATE_DATA;
472 else if (*buf_p == 'A') /* cursor up */
474 int last_history = (t_con->current_history > 0) ? t_con->current_history - 1 : TELNET_LINE_HISTORY_SIZE-1;
475 if (t_con->history[last_history])
477 telnet_clear_line(connection, t_con);
478 t_con->line_size = strlen(t_con->history[last_history]);
479 t_con->line_cursor = t_con->line_size;
480 memcpy(t_con->line, t_con->history[last_history], t_con->line_size);
481 telnet_write(connection, t_con->line, t_con->line_size);
482 t_con->current_history = last_history;
484 t_con->state = TELNET_STATE_DATA;
486 else if (*buf_p == 'B') /* cursor down */
488 int next_history = (t_con->current_history + 1) % TELNET_LINE_HISTORY_SIZE;
489 if (t_con->history[next_history])
491 telnet_clear_line(connection, t_con);
492 t_con->line_size = strlen(t_con->history[next_history]);
493 t_con->line_cursor = t_con->line_size;
494 memcpy(t_con->line, t_con->history[next_history], t_con->line_size);
495 telnet_write(connection, t_con->line, t_con->line_size);
496 t_con->current_history = next_history;
498 t_con->state = TELNET_STATE_DATA;
500 else if (*buf_p == '3')
502 t_con->last_escape = *buf_p;
506 t_con->state = TELNET_STATE_DATA;
509 else if (t_con->last_escape == '3')
511 /* Remove character */
514 if (t_con->line_cursor < t_con->line_size)
518 /* remove char from line buffer */
519 memmove(t_con->line + t_con->line_cursor, t_con->line + t_con->line_cursor + 1, t_con->line_size - t_con->line_cursor);
521 /* print remainder of buffer */
522 telnet_write(connection, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);
523 /* overwrite last char with whitespace */
524 telnet_write(connection, " \b", 2);
526 /* move back to cursor position*/
527 for (i = t_con->line_cursor; i < t_con->line_size; i++)
529 telnet_write(connection, "\b", 1);
533 t_con->state = TELNET_STATE_DATA;
537 t_con->state = TELNET_STATE_DATA;
540 else if (t_con->last_escape == '\x00')
544 t_con->last_escape = *buf_p;
548 t_con->state = TELNET_STATE_DATA;
553 LOG_ERROR("BUG: unexpected value in t_con->last_escape");
554 t_con->state = TELNET_STATE_DATA;
559 LOG_ERROR("unknown telnet state");
570 int telnet_connection_closed(connection_t *connection)
572 telnet_connection_t *t_con = connection->priv;
575 log_remove_callback(telnet_log_callback, connection);
580 t_con->prompt = NULL;
583 for (i = 0; i < TELNET_LINE_HISTORY_SIZE; i++)
585 if (t_con->history[i])
587 free(t_con->history[i]);
588 t_con->history[i] = NULL;
592 /* if this connection registered a debug-message receiver delete it */
593 delete_debug_msg_receiver(connection->cmd_ctx, NULL);
595 if (connection->priv)
597 free(connection->priv);
598 connection->priv = NULL;
602 LOG_ERROR("BUG: connection->priv == NULL");
608 int telnet_set_prompt(connection_t *connection, char *prompt)
610 telnet_connection_t *t_con = connection->priv;
612 if (t_con->prompt != NULL)
615 t_con->prompt = strdup(prompt);
620 int telnet_init(char *banner)
622 telnet_service_t *telnet_service = malloc(sizeof(telnet_service_t));
624 if (telnet_port == 0)
626 LOG_WARNING("no telnet port specified, using default port 4444");
630 telnet_service->banner = banner;
632 add_service("telnet", CONNECTION_TELNET, telnet_port, 1, telnet_new_connection, telnet_input, telnet_connection_closed, telnet_service);
637 int telnet_register_commands(command_context_t *command_context)
639 register_command(command_context, NULL, "exit", handle_exit_command,
640 COMMAND_EXEC, "exit telnet session");
642 register_command(command_context, NULL, "telnet_port", handle_telnet_port_command,
643 COMMAND_CONFIG, "port on which to listen for incoming telnet connections");
648 /* daemon configuration command telnet_port */
649 int handle_telnet_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
654 telnet_port = strtoul(args[0], NULL, 0);
659 int handle_exit_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
661 return ERROR_COMMAND_CLOSE_CONNECTION;