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 "telnet_server.h"
31 #include <target/target_request.h>
33 static unsigned short telnet_port
= 4444;
35 static char *negotiate
=
36 "\xFF\xFB\x03" /* IAC WILL Suppress Go Ahead */
37 "\xFF\xFB\x01" /* IAC WILL Echo */
38 "\xFF\xFD\x03" /* IAC DO Suppress Go Ahead */
39 "\xFF\xFE\x01"; /* IAC DON'T Echo */
41 #define CTRL(c) (c - '@')
43 /* The only way we can detect that the socket is closed is the first time
44 * we write to it, we will fail. Subsequent write operations will
47 static int telnet_write(struct connection
*connection
, const void *data
,
50 struct telnet_connection
*t_con
= connection
->priv
;
52 return ERROR_SERVER_REMOTE_CLOSED
;
54 if (write_socket(connection
->fd
, data
, len
) == len
)
59 return ERROR_SERVER_REMOTE_CLOSED
;
62 static int telnet_prompt(struct connection
*connection
)
64 struct telnet_connection
*t_con
= connection
->priv
;
66 return telnet_write(connection
, t_con
->prompt
, strlen(t_con
->prompt
));
69 static int telnet_outputline(struct connection
*connection
, const char *line
)
73 /* process lines in buffer */
75 char *line_end
= strchr(line
, '\n');
82 telnet_write(connection
, line
, len
);
85 telnet_write(connection
, "\r\n", 2);
97 static int telnet_output(struct command_context
*cmd_ctx
, const char* line
)
99 struct connection
*connection
= cmd_ctx
->output_handler_priv
;
101 return telnet_outputline(connection
, line
);
104 static void telnet_log_callback(void *priv
, const char *file
, unsigned line
,
105 const char *function
, const char *string
)
107 struct connection
*connection
= priv
;
108 struct telnet_connection
*t_con
= connection
->priv
;
111 /* if there is no prompt, simply output the message */
112 if (t_con
->line_cursor
< 0)
114 telnet_outputline(connection
, string
);
118 /* clear the command line */
119 for (i
= strlen(t_con
->prompt
) + t_con
->line_size
; i
> 0; i
-= 16)
120 telnet_write(connection
, "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b", i
> 16 ? 16 : i
);
121 for (i
= strlen(t_con
->prompt
) + t_con
->line_size
; i
> 0; i
-= 16)
122 telnet_write(connection
, " ", i
> 16 ? 16 : i
);
123 for (i
= strlen(t_con
->prompt
) + t_con
->line_size
; i
> 0; i
-= 16)
124 telnet_write(connection
, "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b", i
> 16 ? 16 : i
);
126 /* output the message */
127 telnet_outputline(connection
, string
);
129 /* put the command line to its previous state */
130 telnet_prompt(connection
);
131 telnet_write(connection
, t_con
->line
, t_con
->line_size
);
132 for (i
= t_con
->line_size
; i
> t_con
->line_cursor
; i
--)
133 telnet_write(connection
, "\b", 1);
136 static int telnet_new_connection(struct connection
*connection
)
138 struct telnet_connection
*telnet_connection
= malloc(sizeof(struct telnet_connection
));
139 struct telnet_service
*telnet_service
= connection
->service
->priv
;
142 connection
->priv
= telnet_connection
;
144 /* initialize telnet connection information */
145 telnet_connection
->closed
= 0;
146 telnet_connection
->line_size
= 0;
147 telnet_connection
->line_cursor
= 0;
148 telnet_connection
->option_size
= 0;
149 telnet_connection
->prompt
= strdup("> ");
150 telnet_connection
->state
= TELNET_STATE_DATA
;
152 /* output goes through telnet connection */
153 command_set_output_handler(connection
->cmd_ctx
, telnet_output
, connection
);
155 /* negotiate telnet options */
156 telnet_write(connection
, negotiate
, strlen(negotiate
));
158 /* print connection banner */
159 if (telnet_service
->banner
)
161 telnet_write(connection
, telnet_service
->banner
, strlen(telnet_service
->banner
));
162 telnet_write(connection
, "\r\n", 2);
165 telnet_write(connection
, "\r", 1); /* the prompt is always placed at the line beginning */
166 telnet_prompt(connection
);
168 /* initialize history */
169 for (i
= 0; i
< TELNET_LINE_HISTORY_SIZE
; i
++)
171 telnet_connection
->history
[i
] = NULL
;
173 telnet_connection
->next_history
= 0;
174 telnet_connection
->current_history
= 0;
176 log_add_callback(telnet_log_callback
, connection
);
181 static void telnet_clear_line(struct connection
*connection
,
182 struct telnet_connection
*t_con
)
184 /* move to end of line */
185 if (t_con
->line_cursor
< t_con
->line_size
)
187 telnet_write(connection
, t_con
->line
+ t_con
->line_cursor
, t_con
->line_size
- t_con
->line_cursor
);
190 /* backspace, overwrite with space, backspace */
191 while (t_con
->line_size
> 0)
193 telnet_write(connection
, "\b \b", 3);
196 t_con
->line_cursor
= 0;
199 static int telnet_input(struct connection
*connection
)
202 unsigned char buffer
[TELNET_BUFFER_SIZE
];
203 unsigned char *buf_p
;
204 struct telnet_connection
*t_con
= connection
->priv
;
205 struct command_context
*command_context
= connection
->cmd_ctx
;
207 bytes_read
= read_socket(connection
->fd
, buffer
, TELNET_BUFFER_SIZE
);
210 return ERROR_SERVER_REMOTE_CLOSED
;
211 else if (bytes_read
== -1)
213 LOG_ERROR("error during read: %s", strerror(errno
));
214 return ERROR_SERVER_REMOTE_CLOSED
;
220 switch (t_con
->state
)
222 case TELNET_STATE_DATA
:
225 t_con
->state
= TELNET_STATE_IAC
;
229 if (isprint(*buf_p
)) /* printable character */
231 /* watch buffer size leaving one spare character for string null termination */
232 if (t_con
->line_size
== TELNET_LINE_MAX_SIZE
-1)
234 /* output audible bell if buffer is full */
235 telnet_write(connection
, "\x07", 1); /* "\a" does not work, at least on windows */
237 else if (t_con
->line_cursor
== t_con
->line_size
)
239 telnet_write(connection
, buf_p
, 1);
240 t_con
->line
[t_con
->line_size
++] = *buf_p
;
241 t_con
->line_cursor
++;
246 memmove(t_con
->line
+ t_con
->line_cursor
+ 1, t_con
->line
+ t_con
->line_cursor
, t_con
->line_size
- t_con
->line_cursor
);
247 t_con
->line
[t_con
->line_cursor
] = *buf_p
;
249 telnet_write(connection
, t_con
->line
+ t_con
->line_cursor
, t_con
->line_size
- t_con
->line_cursor
);
250 t_con
->line_cursor
++;
251 for (i
= t_con
->line_cursor
; i
< t_con
->line_size
; i
++)
253 telnet_write(connection
, "\b", 1);
257 else /* non-printable */
259 if (*buf_p
== 0x1b) /* escape */
261 t_con
->state
= TELNET_STATE_ESCAPE
;
262 t_con
->last_escape
= '\x00';
264 else if ((*buf_p
== 0xd) || (*buf_p
== 0xa)) /* CR/LF */
268 /* skip over combinations with CR/LF and NUL characters */
269 if ((bytes_read
> 1) && ((*(buf_p
+ 1) == 0xa) || (*(buf_p
+ 1) == 0xd)))
274 if ((bytes_read
> 1) && (*(buf_p
+ 1) == 0))
279 t_con
->line
[t_con
->line_size
] = 0;
281 telnet_write(connection
, "\r\n\x00", 3);
283 if (strcmp(t_con
->line
, "history") == 0)
286 for (i
= 1; i
< TELNET_LINE_HISTORY_SIZE
; i
++)
288 /* the t_con->next_history line contains empty string (unless NULL), thus it is not printed */
289 char *history_line
= t_con
->history
[(t_con
->next_history
+ i
) % TELNET_LINE_HISTORY_SIZE
];
292 telnet_write(connection
, history_line
, strlen(history_line
));
293 telnet_write(connection
, "\r\n\x00", 3);
296 t_con
->line_size
= 0;
297 t_con
->line_cursor
= 0;
301 /* save only non-blank not repeating lines in the history */
302 char *prev_line
= t_con
->history
[(t_con
->current_history
> 0) ? t_con
->current_history
- 1 : TELNET_LINE_HISTORY_SIZE
-1];
303 if (*t_con
->line
&& (prev_line
== NULL
|| strcmp(t_con
->line
, prev_line
)))
305 /* if the history slot is already taken, free it */
306 if (t_con
->history
[t_con
->next_history
])
308 free(t_con
->history
[t_con
->next_history
]);
311 /* add line to history */
312 t_con
->history
[t_con
->next_history
] = strdup(t_con
->line
);
314 /* wrap history at TELNET_LINE_HISTORY_SIZE */
315 t_con
->next_history
= (t_con
->next_history
+ 1) % TELNET_LINE_HISTORY_SIZE
;
317 /* current history line starts at the new entry */
318 t_con
->current_history
= t_con
->next_history
;
320 if (t_con
->history
[t_con
->current_history
])
322 free(t_con
->history
[t_con
->current_history
]);
324 t_con
->history
[t_con
->current_history
] = strdup("");
327 t_con
->line_size
= 0;
329 t_con
->line_cursor
= -1; /* to supress prompt in log callback during command execution */
331 retval
= command_run_line(command_context
, t_con
->line
);
333 t_con
->line_cursor
= 0;
335 if (retval
== ERROR_COMMAND_CLOSE_CONNECTION
)
336 return ERROR_SERVER_REMOTE_CLOSED
;
338 telnet_write(connection
, "\r", 1); /* the prompt is always placed at the line beginning */
339 retval
= telnet_prompt(connection
);
340 if (retval
== ERROR_SERVER_REMOTE_CLOSED
)
341 return ERROR_SERVER_REMOTE_CLOSED
;
344 else if ((*buf_p
== 0x7f) || (*buf_p
== 0x8)) /* delete character */
346 if (t_con
->line_cursor
> 0)
348 if (t_con
->line_cursor
!= t_con
->line_size
)
351 telnet_write(connection
, "\b", 1);
352 t_con
->line_cursor
--;
354 memmove(t_con
->line
+ t_con
->line_cursor
, t_con
->line
+ t_con
->line_cursor
+ 1, t_con
->line_size
- t_con
->line_cursor
);
356 telnet_write(connection
, t_con
->line
+ t_con
->line_cursor
, t_con
->line_size
- t_con
->line_cursor
);
357 telnet_write(connection
, " \b", 2);
358 for (i
= t_con
->line_cursor
; i
< t_con
->line_size
; i
++)
360 telnet_write(connection
, "\b", 1);
366 t_con
->line_cursor
--;
367 /* back space: move the 'printer' head one char back, overwrite with space, move back again */
368 telnet_write(connection
, "\b \b", 3);
372 else if (*buf_p
== 0x15) /* clear line */
374 telnet_clear_line(connection
, t_con
);
376 else if (*buf_p
== CTRL('B')) /* cursor left */
378 if (t_con
->line_cursor
> 0)
380 telnet_write(connection
, "\b", 1);
381 t_con
->line_cursor
--;
383 t_con
->state
= TELNET_STATE_DATA
;
385 else if (*buf_p
== CTRL('F')) /* cursor right */
387 if (t_con
->line_cursor
< t_con
->line_size
)
389 telnet_write(connection
, t_con
->line
+ t_con
->line_cursor
++, 1);
391 t_con
->state
= TELNET_STATE_DATA
;
395 LOG_DEBUG("unhandled nonprintable: %2.2x", *buf_p
);
400 case TELNET_STATE_IAC
:
404 t_con
->state
= TELNET_STATE_DONT
;
407 t_con
->state
= TELNET_STATE_DO
;
410 t_con
->state
= TELNET_STATE_WONT
;
413 t_con
->state
= TELNET_STATE_WILL
;
417 case TELNET_STATE_SB
:
419 case TELNET_STATE_SE
:
421 case TELNET_STATE_WILL
:
422 case TELNET_STATE_WONT
:
423 case TELNET_STATE_DO
:
424 case TELNET_STATE_DONT
:
425 t_con
->state
= TELNET_STATE_DATA
;
427 case TELNET_STATE_ESCAPE
:
428 if (t_con
->last_escape
== '[')
430 if (*buf_p
== 'D') /* cursor left */
432 if (t_con
->line_cursor
> 0)
434 telnet_write(connection
, "\b", 1);
435 t_con
->line_cursor
--;
437 t_con
->state
= TELNET_STATE_DATA
;
439 else if (*buf_p
== 'C') /* cursor right */
441 if (t_con
->line_cursor
< t_con
->line_size
)
443 telnet_write(connection
, t_con
->line
+ t_con
->line_cursor
++, 1);
445 t_con
->state
= TELNET_STATE_DATA
;
447 else if (*buf_p
== 'A') /* cursor up */
449 int last_history
= (t_con
->current_history
> 0) ? t_con
->current_history
- 1 : TELNET_LINE_HISTORY_SIZE
-1;
450 if (t_con
->history
[last_history
])
452 telnet_clear_line(connection
, t_con
);
453 t_con
->line_size
= strlen(t_con
->history
[last_history
]);
454 t_con
->line_cursor
= t_con
->line_size
;
455 memcpy(t_con
->line
, t_con
->history
[last_history
], t_con
->line_size
);
456 telnet_write(connection
, t_con
->line
, t_con
->line_size
);
457 t_con
->current_history
= last_history
;
459 t_con
->state
= TELNET_STATE_DATA
;
461 else if (*buf_p
== 'B') /* cursor down */
463 int next_history
= (t_con
->current_history
+ 1) % TELNET_LINE_HISTORY_SIZE
;
464 if (t_con
->history
[next_history
])
466 telnet_clear_line(connection
, t_con
);
467 t_con
->line_size
= strlen(t_con
->history
[next_history
]);
468 t_con
->line_cursor
= t_con
->line_size
;
469 memcpy(t_con
->line
, t_con
->history
[next_history
], t_con
->line_size
);
470 telnet_write(connection
, t_con
->line
, t_con
->line_size
);
471 t_con
->current_history
= next_history
;
473 t_con
->state
= TELNET_STATE_DATA
;
475 else if (*buf_p
== '3')
477 t_con
->last_escape
= *buf_p
;
481 t_con
->state
= TELNET_STATE_DATA
;
484 else if (t_con
->last_escape
== '3')
486 /* Remove character */
489 if (t_con
->line_cursor
< t_con
->line_size
)
493 /* remove char from line buffer */
494 memmove(t_con
->line
+ t_con
->line_cursor
, t_con
->line
+ t_con
->line_cursor
+ 1, t_con
->line_size
- t_con
->line_cursor
);
496 /* print remainder of buffer */
497 telnet_write(connection
, t_con
->line
+ t_con
->line_cursor
, t_con
->line_size
- t_con
->line_cursor
);
498 /* overwrite last char with whitespace */
499 telnet_write(connection
, " \b", 2);
501 /* move back to cursor position*/
502 for (i
= t_con
->line_cursor
; i
< t_con
->line_size
; i
++)
504 telnet_write(connection
, "\b", 1);
508 t_con
->state
= TELNET_STATE_DATA
;
512 t_con
->state
= TELNET_STATE_DATA
;
515 else if (t_con
->last_escape
== '\x00')
519 t_con
->last_escape
= *buf_p
;
523 t_con
->state
= TELNET_STATE_DATA
;
528 LOG_ERROR("BUG: unexpected value in t_con->last_escape");
529 t_con
->state
= TELNET_STATE_DATA
;
534 LOG_ERROR("unknown telnet state");
545 static int telnet_connection_closed(struct connection
*connection
)
547 struct telnet_connection
*t_con
= connection
->priv
;
550 log_remove_callback(telnet_log_callback
, connection
);
555 t_con
->prompt
= NULL
;
558 for (i
= 0; i
< TELNET_LINE_HISTORY_SIZE
; i
++)
560 if (t_con
->history
[i
])
562 free(t_con
->history
[i
]);
563 t_con
->history
[i
] = NULL
;
567 /* if this connection registered a debug-message receiver delete it */
568 delete_debug_msg_receiver(connection
->cmd_ctx
, NULL
);
570 if (connection
->priv
)
572 free(connection
->priv
);
573 connection
->priv
= NULL
;
577 LOG_ERROR("BUG: connection->priv == NULL");
583 int telnet_init(char *banner
)
585 struct telnet_service
*telnet_service
= malloc(sizeof(struct telnet_service
));
587 if (telnet_port
== 0)
589 LOG_INFO("telnet port disabled");
590 free(telnet_service
);
594 telnet_service
->banner
= banner
;
596 add_service("telnet", CONNECTION_TCP
, telnet_port
, 1, telnet_new_connection
, telnet_input
, telnet_connection_closed
, telnet_service
);
601 /* daemon configuration command telnet_port */
602 COMMAND_HANDLER(handle_telnet_port_command
)
604 return CALL_COMMAND_HANDLER(server_port_command
, &telnet_port
);
607 COMMAND_HANDLER(handle_exit_command
)
609 return ERROR_COMMAND_CLOSE_CONNECTION
;
612 static const struct command_registration telnet_command_handlers
[] = {
615 .handler
= handle_exit_command
,
616 .mode
= COMMAND_EXEC
,
617 .help
= "exit telnet session",
620 .name
= "telnet_port",
621 .handler
= handle_telnet_port_command
,
623 .help
= "Specify port on which to listen "
624 "for incoming telnet connections. "
625 "No arguments reports telnet port; zero disables.",
626 .usage
= "[port_num]",
628 COMMAND_REGISTRATION_DONE
631 int telnet_register_commands(struct command_context
*cmd_ctx
)
633 return register_commands(cmd_ctx
, NULL
, telnet_command_handlers
);