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 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
27 #include "replacements.h"
29 #include "telnet_server.h"
35 #include "target_request.h"
43 static unsigned short telnet_port = 0;
45 int handle_exit_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
46 int handle_telnet_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
48 static char *negotiate =
49 "\xFF\xFB\x03" /* IAC WILL Suppress Go Ahead */
50 "\xFF\xFB\x01" /* IAC WILL Echo */
51 "\xFF\xFD\x03" /* IAC DO Suppress Go Ahead */
52 "\xFF\xFE\x01"; /* IAC DON'T Echo */
54 #define CTRL(c) (c - '@')
56 /* The only way we can detect that the socket is closed is the first time
57 * we write to it, we will fail. Subsequent write operations will
60 int telnet_write(connection_t *connection, const void *data, int len)
62 telnet_connection_t *t_con = connection->priv;
64 return ERROR_SERVER_REMOTE_CLOSED;
66 if (write_socket(connection->fd, data, len) == len)
71 return ERROR_SERVER_REMOTE_CLOSED;
74 int telnet_prompt(connection_t *connection)
76 telnet_connection_t *t_con = connection->priv;
78 telnet_write(connection, "\r", 1); /* the prompt is always placed at the line beginning */
79 return telnet_write(connection, t_con->prompt, strlen(t_con->prompt));
82 int telnet_outputline(connection_t *connection, const char *line)
86 /* process lines in buffer */
88 char *line_end = strchr(line, '\n');
95 telnet_write(connection, line, len);
98 telnet_write(connection, "\r\n", 2);
110 int telnet_output(struct command_context_s *cmd_ctx, const char* line)
112 connection_t *connection = cmd_ctx->output_handler_priv;
114 return telnet_outputline(connection, line);
117 void telnet_log_callback(void *priv, const char *file, int line,
118 const char *function, const char *string)
120 connection_t *connection = priv;
121 telnet_connection_t *t_con = connection->priv;
124 /* if there is no prompt, simply output the message */
125 if (t_con->line_cursor < 0)
127 telnet_outputline(connection, string);
131 /* clear the command line */
132 telnet_write(connection, "\r", 1);
133 for (i = strlen(t_con->prompt) + t_con->line_size; i>0; i-=16)
134 telnet_write(connection, " ", i>16 ? 16 : i);
135 telnet_write(connection, "\r", 1);
137 /* output the message */
138 telnet_outputline(connection, string);
140 /* put the command line to its previous state */
141 telnet_prompt(connection);
142 telnet_write(connection, t_con->line, t_con->line_size);
143 for (i=t_con->line_size; i>t_con->line_cursor; i--)
144 telnet_write(connection, "\b", 1);
147 int telnet_new_connection(connection_t *connection)
149 telnet_connection_t *telnet_connection = malloc(sizeof(telnet_connection_t));
150 telnet_service_t *telnet_service = connection->service->priv;
153 connection->priv = telnet_connection;
155 /* initialize telnet connection information */
156 telnet_connection->closed = 0;
157 telnet_connection->line_size = 0;
158 telnet_connection->line_cursor = 0;
159 telnet_connection->option_size = 0;
160 telnet_connection->prompt = strdup("> ");
161 telnet_connection->state = TELNET_STATE_DATA;
163 /* output goes through telnet connection */
164 command_set_output_handler(connection->cmd_ctx, telnet_output, connection);
166 /* negotiate telnet options */
167 telnet_write(connection, negotiate, strlen(negotiate));
169 /* print connection banner */
170 if (telnet_service->banner)
172 telnet_write(connection, telnet_service->banner, strlen(telnet_service->banner));
173 telnet_write(connection, "\r\n", 2);
176 telnet_prompt(connection);
178 /* initialize history */
179 for (i = 0; i < TELNET_LINE_HISTORY_SIZE; i++)
181 telnet_connection->history[i] = NULL;
183 telnet_connection->next_history = 0;
184 telnet_connection->current_history = 0;
186 log_add_callback(telnet_log_callback, connection);
193 void telnet_clear_line(connection_t *connection, telnet_connection_t *t_con)
195 /* move to end of line */
196 if (t_con->line_cursor < t_con->line_size)
198 telnet_write(connection, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);
201 /* backspace, overwrite with space, backspace */
202 while (t_con->line_size > 0)
204 telnet_write(connection, "\b \b", 3);
207 t_con->line_cursor = 0;
210 int telnet_input(connection_t *connection)
213 char buffer[TELNET_BUFFER_SIZE];
215 telnet_connection_t *t_con = connection->priv;
216 command_context_t *command_context = connection->cmd_ctx;
218 bytes_read = read_socket(connection->fd, buffer, TELNET_BUFFER_SIZE);
221 return ERROR_SERVER_REMOTE_CLOSED;
222 else if (bytes_read == -1)
224 LOG_ERROR("error during read: %s", strerror(errno));
225 return ERROR_SERVER_REMOTE_CLOSED;
231 switch (t_con->state)
233 case TELNET_STATE_DATA:
234 if (*buf_p == '\xff')
236 t_con->state = TELNET_STATE_IAC;
240 if (isprint(*buf_p)) /* printable character */
242 /* watch buffer size leaving one spare character for string null termination */
243 if (t_con->line_size == TELNET_LINE_MAX_SIZE-1)
245 /* output audible bell if buffer is full */
246 telnet_write(connection, "\x07", 1); /* "\a" does not work, at least on windows */
248 else if (t_con->line_cursor == t_con->line_size)
250 telnet_write(connection, buf_p, 1);
251 t_con->line[t_con->line_size++] = *buf_p;
252 t_con->line_cursor++;
257 memmove(t_con->line + t_con->line_cursor + 1, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);
258 t_con->line[t_con->line_cursor] = *buf_p;
260 telnet_write(connection, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);
261 t_con->line_cursor++;
262 for (i = t_con->line_cursor; i < t_con->line_size; i++)
264 telnet_write(connection, "\b", 1);
268 else /* non-printable */
270 if (*buf_p == 0x1b) /* escape */
272 t_con->state = TELNET_STATE_ESCAPE;
273 t_con->last_escape = '\x00';
275 else if ((*buf_p == 0xd) || (*buf_p == 0xa)) /* CR/LF */
279 /* skip over combinations with CR/LF and NUL characters */
280 if ((bytes_read > 1) && ((*(buf_p + 1) == 0xa) || (*(buf_p + 1) == 0xd)))
285 if ((bytes_read > 1) && (*(buf_p + 1) == 0))
290 t_con->line[t_con->line_size] = 0;
292 telnet_write(connection, "\r\n\x00", 3);
294 if (strcmp(t_con->line, "history") == 0)
297 for (i = 1; i < TELNET_LINE_HISTORY_SIZE; i++)
299 /* the t_con->next_history line contains empty string (unless NULL), thus it is not printed */
300 char *history_line = t_con->history[(t_con->next_history + i) % TELNET_LINE_HISTORY_SIZE];
303 telnet_write(connection, history_line, strlen(history_line));
304 telnet_write(connection, "\r\n\x00", 3);
307 t_con->line_size = 0;
308 t_con->line_cursor = 0;
312 /* save only non-blank not repeating lines in the history */
313 char *prev_line = t_con->history[(t_con->current_history > 0) ? t_con->current_history - 1 : TELNET_LINE_HISTORY_SIZE-1];
314 if (*t_con->line && (prev_line == NULL || strcmp(t_con->line, prev_line)))
316 /* if the history slot is already taken, free it */
317 if (t_con->history[t_con->next_history])
319 free(t_con->history[t_con->next_history]);
322 /* add line to history */
323 t_con->history[t_con->next_history] = strdup(t_con->line);
325 /* wrap history at TELNET_LINE_HISTORY_SIZE */
326 t_con->next_history = (t_con->next_history + 1) % TELNET_LINE_HISTORY_SIZE;
328 /* current history line starts at the new entry */
329 t_con->current_history = t_con->next_history;
331 if (t_con->history[t_con->current_history])
333 free(t_con->history[t_con->current_history]);
335 t_con->history[t_con->current_history] = strdup("");
338 t_con->line_size = 0;
340 t_con->line_cursor = -1; /* to supress prompt in log callback during command execution */
341 retval = command_run_line(command_context, t_con->line);
342 t_con->line_cursor = 0;
344 if (retval == ERROR_COMMAND_CLOSE_CONNECTION)
345 return ERROR_SERVER_REMOTE_CLOSED;
347 retval = telnet_prompt(connection);
348 if (retval == ERROR_SERVER_REMOTE_CLOSED)
349 return ERROR_SERVER_REMOTE_CLOSED;
352 else if ((*buf_p == 0x7f) || (*buf_p == 0x8)) /* delete character */
354 if (t_con->line_cursor > 0)
356 if (t_con->line_cursor != t_con->line_size)
359 telnet_write(connection, "\b", 1);
360 t_con->line_cursor--;
362 memmove(t_con->line + t_con->line_cursor, t_con->line + t_con->line_cursor + 1, t_con->line_size - t_con->line_cursor);
364 telnet_write(connection, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);
365 telnet_write(connection, " \b", 2);
366 for (i = t_con->line_cursor; i < t_con->line_size; i++)
368 telnet_write(connection, "\b", 1);
374 t_con->line_cursor--;
375 /* back space: move the 'printer' head one char back, overwrite with space, move back again */
376 telnet_write(connection, "\b \b", 3);
380 else if (*buf_p == 0x15) /* clear line */
382 telnet_clear_line(connection, t_con);
384 else if (*buf_p == CTRL('B')) /* cursor left */
386 if (t_con->line_cursor > 0)
388 telnet_write(connection, "\b", 1);
389 t_con->line_cursor--;
391 t_con->state = TELNET_STATE_DATA;
393 else if (*buf_p == CTRL('F')) /* cursor right */
395 if (t_con->line_cursor < t_con->line_size)
397 telnet_write(connection, t_con->line + t_con->line_cursor++, 1);
399 t_con->state = TELNET_STATE_DATA;
403 LOG_DEBUG("unhandled nonprintable: %2.2x", *buf_p);
408 case TELNET_STATE_IAC:
412 t_con->state = TELNET_STATE_DONT;
415 t_con->state = TELNET_STATE_DO;
418 t_con->state = TELNET_STATE_WONT;
421 t_con->state = TELNET_STATE_WILL;
425 case TELNET_STATE_SB:
427 case TELNET_STATE_SE:
429 case TELNET_STATE_WILL:
430 case TELNET_STATE_WONT:
431 case TELNET_STATE_DO:
432 case TELNET_STATE_DONT:
433 t_con->state = TELNET_STATE_DATA;
435 case TELNET_STATE_ESCAPE:
436 if (t_con->last_escape == '[')
438 if (*buf_p == 'D') /* cursor left */
440 if (t_con->line_cursor > 0)
442 telnet_write(connection, "\b", 1);
443 t_con->line_cursor--;
445 t_con->state = TELNET_STATE_DATA;
447 else if (*buf_p == 'C') /* cursor right */
449 if (t_con->line_cursor < t_con->line_size)
451 telnet_write(connection, t_con->line + t_con->line_cursor++, 1);
453 t_con->state = TELNET_STATE_DATA;
455 else if (*buf_p == 'A') /* cursor up */
457 int last_history = (t_con->current_history > 0) ? t_con->current_history - 1 : TELNET_LINE_HISTORY_SIZE-1;
458 if (t_con->history[last_history])
460 telnet_clear_line(connection, t_con);
461 t_con->line_size = strlen(t_con->history[last_history]);
462 t_con->line_cursor = t_con->line_size;
463 memcpy(t_con->line, t_con->history[last_history], t_con->line_size);
464 telnet_write(connection, t_con->line, t_con->line_size);
465 t_con->current_history = last_history;
467 t_con->state = TELNET_STATE_DATA;
469 else if (*buf_p == 'B') /* cursor down */
471 int next_history = (t_con->current_history + 1) % TELNET_LINE_HISTORY_SIZE;
472 if (t_con->history[next_history])
474 telnet_clear_line(connection, t_con);
475 t_con->line_size = strlen(t_con->history[next_history]);
476 t_con->line_cursor = t_con->line_size;
477 memcpy(t_con->line, t_con->history[next_history], t_con->line_size);
478 telnet_write(connection, t_con->line, t_con->line_size);
479 t_con->current_history = next_history;
481 t_con->state = TELNET_STATE_DATA;
483 else if (*buf_p == '3')
485 t_con->last_escape = *buf_p;
489 t_con->state = TELNET_STATE_DATA;
492 else if (t_con->last_escape == '3')
494 /* Remove character */
497 if (t_con->line_cursor < t_con->line_size)
501 /* remove char from line buffer */
502 memmove(t_con->line + t_con->line_cursor, t_con->line + t_con->line_cursor + 1, t_con->line_size - t_con->line_cursor);
504 /* print remainder of buffer */
505 telnet_write(connection, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);
506 /* overwrite last char with whitespace */
507 telnet_write(connection, " \b", 2);
509 /* move back to cursor position*/
510 for (i = t_con->line_cursor; i < t_con->line_size; i++)
512 telnet_write(connection, "\b", 1);
516 t_con->state = TELNET_STATE_DATA;
520 t_con->state = TELNET_STATE_DATA;
523 else if (t_con->last_escape == '\x00')
527 t_con->last_escape = *buf_p;
531 t_con->state = TELNET_STATE_DATA;
536 LOG_ERROR("BUG: unexpected value in t_con->last_escape");
537 t_con->state = TELNET_STATE_DATA;
542 LOG_ERROR("unknown telnet state");
553 int telnet_connection_closed(connection_t *connection)
555 telnet_connection_t *t_con = connection->priv;
558 log_remove_callback(telnet_log_callback, connection);
563 t_con->prompt = NULL;
566 for (i = 0; i < TELNET_LINE_HISTORY_SIZE; i++)
568 if (t_con->history[i])
570 free(t_con->history[i]);
571 t_con->history[i] = NULL;
575 /* if this connection registered a debug-message receiver delete it */
576 delete_debug_msg_receiver(connection->cmd_ctx, NULL);
578 if (connection->priv)
580 free(connection->priv);
581 connection->priv = NULL;
585 LOG_ERROR("BUG: connection->priv == NULL");
591 int telnet_set_prompt(connection_t *connection, char *prompt)
593 telnet_connection_t *t_con = connection->priv;
595 if (t_con->prompt != NULL)
598 t_con->prompt = strdup(prompt);
603 int telnet_init(char *banner)
605 telnet_service_t *telnet_service = malloc(sizeof(telnet_service_t));
607 if (telnet_port == 0)
609 LOG_WARNING("no telnet port specified, using default port 4444");
613 telnet_service->banner = banner;
615 add_service("telnet", CONNECTION_TELNET, telnet_port, 1, telnet_new_connection, telnet_input, telnet_connection_closed, telnet_service);
620 int telnet_register_commands(command_context_t *command_context)
622 register_command(command_context, NULL, "exit", handle_exit_command,
623 COMMAND_EXEC, "exit telnet session");
625 register_command(command_context, NULL, "telnet_port", handle_telnet_port_command,
631 /* daemon configuration command telnet_port */
632 int handle_telnet_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
637 telnet_port = strtoul(args[0], NULL, 0);
642 int handle_exit_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
644 return ERROR_COMMAND_CLOSE_CONNECTION;