84959e3a0f37a547ca203ddf77d48df190e90b4b
[fw/openocd] / src / server / telnet_server.c
1 /***************************************************************************
2  *   Copyright (C) 2005 by Dominic Rath                                    *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
5  *   Copyright (C) 2007,2008 Ã˜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, write to the                         *
23  *   Free Software Foundation, Inc.,                                       *
24  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
25  ***************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include "telnet_server.h"
31 #include "target_request.h"
32
33 static unsigned short telnet_port = 4444;
34
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 */
40
41 #define CTRL(c) (c - '@')
42
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
45  * succeed. Shudder!
46  */
47 int telnet_write(connection_t *connection, const void *data, int len)
48 {
49         telnet_connection_t *t_con = connection->priv;
50         if (t_con->closed)
51                 return ERROR_SERVER_REMOTE_CLOSED;
52
53         if (write_socket(connection->fd, data, len) == len)
54         {
55                 return ERROR_OK;
56         }
57         t_con->closed = 1;
58         return ERROR_SERVER_REMOTE_CLOSED;
59 }
60
61 int telnet_prompt(connection_t *connection)
62 {
63         telnet_connection_t *t_con = connection->priv;
64
65         telnet_write(connection, "\r", 1); /* the prompt is always placed at the line beginning */
66         return telnet_write(connection, t_con->prompt, strlen(t_con->prompt));
67 }
68
69 int telnet_outputline(connection_t *connection, const char *line)
70 {
71         int len;
72
73         /* process lines in buffer */
74         while (*line) {
75                 char *line_end = strchr(line, '\n');
76
77                 if (line_end)
78                         len = line_end-line;
79                 else
80                         len = strlen(line);
81
82                 telnet_write(connection, line, len);
83                 if (line_end)
84                 {
85                         telnet_write(connection, "\r\n", 2);
86                         line += len + 1;
87                 }
88                 else
89                 {
90                         line += len;
91                 }
92         }
93
94         return ERROR_OK;
95 }
96
97 int telnet_output(struct command_context_s *cmd_ctx, const char* line)
98 {
99         connection_t *connection = cmd_ctx->output_handler_priv;
100
101         return telnet_outputline(connection, line);
102 }
103
104 void telnet_log_callback(void *priv, const char *file, int line,
105                 const char *function, const char *string)
106 {
107         connection_t *connection = priv;
108         telnet_connection_t *t_con = connection->priv;
109         int i;
110
111         /* if there is no prompt, simply output the message */
112         if (t_con->line_cursor < 0)
113         {
114                 telnet_outputline(connection, string);
115                 return;
116         }
117
118         /* clear the command line */
119         telnet_write(connection, "\r", 1);
120         for (i = strlen(t_con->prompt) + t_con->line_size; i > 0; i -= 16)
121                 telnet_write(connection, "                ", i > 16 ? 16 : i);
122         telnet_write(connection, "\r", 1);
123
124         /* output the message */
125         telnet_outputline(connection, string);
126
127         /* put the command line to its previous state */
128         telnet_prompt(connection);
129         telnet_write(connection, t_con->line, t_con->line_size);
130         for (i = t_con->line_size; i > t_con->line_cursor; i--)
131                 telnet_write(connection, "\b", 1);
132 }
133
134 int telnet_new_connection(connection_t *connection)
135 {
136         telnet_connection_t *telnet_connection = malloc(sizeof(telnet_connection_t));
137         telnet_service_t *telnet_service = connection->service->priv;
138         int i;
139
140         connection->priv = telnet_connection;
141
142         /* initialize telnet connection information */
143         telnet_connection->closed = 0;
144         telnet_connection->line_size = 0;
145         telnet_connection->line_cursor = 0;
146         telnet_connection->option_size = 0;
147         telnet_connection->prompt = strdup("> ");
148         telnet_connection->state = TELNET_STATE_DATA;
149
150         /* output goes through telnet connection */
151         command_set_output_handler(connection->cmd_ctx, telnet_output, connection);
152
153         /* negotiate telnet options */
154         telnet_write(connection, negotiate, strlen(negotiate));
155
156         /* print connection banner */
157         if (telnet_service->banner)
158         {
159                 telnet_write(connection, telnet_service->banner, strlen(telnet_service->banner));
160                 telnet_write(connection, "\r\n", 2);
161         }
162
163         telnet_prompt(connection);
164
165         /* initialize history */
166         for (i = 0; i < TELNET_LINE_HISTORY_SIZE; i++)
167         {
168                 telnet_connection->history[i] = NULL;
169         }
170         telnet_connection->next_history = 0;
171         telnet_connection->current_history = 0;
172
173         log_add_callback(telnet_log_callback, connection);
174
175         return ERROR_OK;
176 }
177
178 void telnet_clear_line(connection_t *connection, telnet_connection_t *t_con)
179 {
180         /* move to end of line */
181         if (t_con->line_cursor < t_con->line_size)
182         {
183                 telnet_write(connection, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);
184         }
185
186         /* backspace, overwrite with space, backspace */
187         while (t_con->line_size > 0)
188         {
189                 telnet_write(connection, "\b \b", 3);
190                 t_con->line_size--;
191         }
192         t_con->line_cursor = 0;
193 }
194
195 int telnet_input(connection_t *connection)
196 {
197         int bytes_read;
198         char buffer[TELNET_BUFFER_SIZE];
199         char *buf_p;
200         telnet_connection_t *t_con = connection->priv;
201         command_context_t *command_context = connection->cmd_ctx;
202
203         bytes_read = read_socket(connection->fd, buffer, TELNET_BUFFER_SIZE);
204
205         if (bytes_read == 0)
206                 return ERROR_SERVER_REMOTE_CLOSED;
207         else if (bytes_read == -1)
208         {
209                 LOG_ERROR("error during read: %s", strerror(errno));
210                 return ERROR_SERVER_REMOTE_CLOSED;
211         }
212
213         buf_p = buffer;
214         while (bytes_read)
215         {
216                 switch (t_con->state)
217                 {
218                         case TELNET_STATE_DATA:
219                                 if (*buf_p == '\xff')
220                                 {
221                                         t_con->state = TELNET_STATE_IAC;
222                                 }
223                                 else
224                                 {
225                                         if (isprint(*buf_p)) /* printable character */
226                                         {
227                                                 /* watch buffer size leaving one spare character for string null termination */
228                                                 if (t_con->line_size == TELNET_LINE_MAX_SIZE-1)
229                                                 {
230                                                         /* output audible bell if buffer is full */
231                                                         telnet_write(connection, "\x07", 1); /* "\a" does not work, at least on windows */
232                                                 }
233                                                 else if (t_con->line_cursor == t_con->line_size)
234                                                 {
235                                                         telnet_write(connection, buf_p, 1);
236                                                         t_con->line[t_con->line_size++] = *buf_p;
237                                                         t_con->line_cursor++;
238                                                 }
239                                                 else
240                                                 {
241                                                         int i;
242                                                         memmove(t_con->line + t_con->line_cursor + 1, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);
243                                                         t_con->line[t_con->line_cursor] = *buf_p;
244                                                         t_con->line_size++;
245                                                         telnet_write(connection, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);
246                                                         t_con->line_cursor++;
247                                                         for (i = t_con->line_cursor; i < t_con->line_size; i++)
248                                                         {
249                                                                 telnet_write(connection, "\b", 1);
250                                                         }
251                                                 }
252                                         }
253                                         else /* non-printable */
254                                         {
255                                                 if (*buf_p == 0x1b) /* escape */
256                                                 {
257                                                         t_con->state = TELNET_STATE_ESCAPE;
258                                                         t_con->last_escape = '\x00';
259                                                 }
260                                                 else if ((*buf_p == 0xd) || (*buf_p == 0xa)) /* CR/LF */
261                                                 {
262                                                         int retval;
263
264                                                         /* skip over combinations with CR/LF and NUL characters */
265                                                         if ((bytes_read > 1) && ((*(buf_p + 1) == 0xa) || (*(buf_p + 1) == 0xd)))
266                                                         {
267                                                                 buf_p++;
268                                                                 bytes_read--;
269                                                         }
270                                                         if ((bytes_read > 1) && (*(buf_p + 1) == 0))
271                                                         {
272                                                                 buf_p++;
273                                                                 bytes_read--;
274                                                         }
275                                                         t_con->line[t_con->line_size] = 0;
276
277                                                         telnet_write(connection, "\r\n\x00", 3);
278
279                                                         if (strcmp(t_con->line, "history") == 0)
280                                                         {
281                                                                 int i;
282                                                                 for (i = 1; i < TELNET_LINE_HISTORY_SIZE; i++)
283                                                                 {
284                                                                         /* the t_con->next_history line contains empty string (unless NULL), thus it is not printed */
285                                                                         char *history_line = t_con->history[(t_con->next_history + i) % TELNET_LINE_HISTORY_SIZE];
286                                                                         if (history_line)
287                                                                         {
288                                                                                 telnet_write(connection, history_line, strlen(history_line));
289                                                                                 telnet_write(connection, "\r\n\x00", 3);
290                                                                         }
291                                                                 }
292                                                                 t_con->line_size = 0;
293                                                                 t_con->line_cursor = 0;
294                                                                 continue;
295                                                         }
296
297                                                         /* save only non-blank not repeating lines in the history */
298                                                         char *prev_line = t_con->history[(t_con->current_history > 0) ? t_con->current_history - 1 : TELNET_LINE_HISTORY_SIZE-1];
299                                                         if (*t_con->line && (prev_line == NULL || strcmp(t_con->line, prev_line)))
300                                                         {
301                                                                 /* if the history slot is already taken, free it */
302                                                                 if (t_con->history[t_con->next_history])
303                                                                 {
304                                                                         free(t_con->history[t_con->next_history]);
305                                                                 }
306
307                                                                 /* add line to history */
308                                                                 t_con->history[t_con->next_history] = strdup(t_con->line);
309
310                                                                 /* wrap history at TELNET_LINE_HISTORY_SIZE */
311                                                                 t_con->next_history = (t_con->next_history + 1) % TELNET_LINE_HISTORY_SIZE;
312
313                                                                 /* current history line starts at the new entry */
314                                                                 t_con->current_history = t_con->next_history;
315
316                                                                 if (t_con->history[t_con->current_history])
317                                                                 {
318                                                                         free(t_con->history[t_con->current_history]);
319                                                                 }
320                                                                 t_con->history[t_con->current_history] = strdup("");
321                                                         }
322
323                                                         t_con->line_size = 0;
324
325                                                         t_con->line_cursor = -1; /* to supress prompt in log callback during command execution */
326
327                                                         retval = command_run_line(command_context, t_con->line);
328
329                                                         t_con->line_cursor = 0;
330
331                                                         if (retval == ERROR_COMMAND_CLOSE_CONNECTION)
332                                                                 return ERROR_SERVER_REMOTE_CLOSED;
333
334                                                         retval = telnet_prompt(connection);
335                                                         if (retval == ERROR_SERVER_REMOTE_CLOSED)
336                                                                 return ERROR_SERVER_REMOTE_CLOSED;
337
338                                                 }
339                                                 else if ((*buf_p == 0x7f) || (*buf_p == 0x8)) /* delete character */
340                                                 {
341                                                         if (t_con->line_cursor > 0)
342                                                         {
343                                                                 if (t_con->line_cursor != t_con->line_size)
344                                                                 {
345                                                                         int i;
346                                                                         telnet_write(connection, "\b", 1);
347                                                                         t_con->line_cursor--;
348                                                                         t_con->line_size--;
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);
350
351                                                                         telnet_write(connection, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);
352                                                                         telnet_write(connection, " \b", 2);
353                                                                         for (i = t_con->line_cursor; i < t_con->line_size; i++)
354                                                                         {
355                                                                                 telnet_write(connection, "\b", 1);
356                                                                         }
357                                                                 }
358                                                                 else
359                                                                 {
360                                                                         t_con->line_size--;
361                                                                         t_con->line_cursor--;
362                                                                         /* back space: move the 'printer' head one char back, overwrite with space, move back again */
363                                                                         telnet_write(connection, "\b \b", 3);
364                                                                 }
365                                                         }
366                                                 }
367                                                 else if (*buf_p == 0x15) /* clear line */
368                                                 {
369                                                         telnet_clear_line(connection, t_con);
370                                                 }
371                                                 else if (*buf_p == CTRL('B')) /* cursor left */
372                                                 {
373                                                         if (t_con->line_cursor > 0)
374                                                         {
375                                                                 telnet_write(connection, "\b", 1);
376                                                                 t_con->line_cursor--;
377                                                         }
378                                                         t_con->state = TELNET_STATE_DATA;
379                                                 }
380                                                 else if (*buf_p == CTRL('F')) /* cursor right */
381                                                 {
382                                                         if (t_con->line_cursor < t_con->line_size)
383                                                         {
384                                                                 telnet_write(connection, t_con->line + t_con->line_cursor++, 1);
385                                                         }
386                                                         t_con->state = TELNET_STATE_DATA;
387                                                 }
388                                                 else
389                                                 {
390                                                         LOG_DEBUG("unhandled nonprintable: %2.2x", *buf_p);
391                                                 }
392                                         }
393                                 }
394                                 break;
395                         case TELNET_STATE_IAC:
396                                 switch (*buf_p)
397                                 {
398                                         case '\xfe':
399                                                 t_con->state = TELNET_STATE_DONT;
400                                                 break;
401                                         case '\xfd':
402                                                 t_con->state = TELNET_STATE_DO;
403                                                 break;
404                                         case '\xfc':
405                                                 t_con->state = TELNET_STATE_WONT;
406                                                 break;
407                                         case '\xfb':
408                                                 t_con->state = TELNET_STATE_WILL;
409                                                 break;
410                                 }
411                                 break;
412                         case TELNET_STATE_SB:
413                                 break;
414                         case TELNET_STATE_SE:
415                                 break;
416                         case TELNET_STATE_WILL:
417                         case TELNET_STATE_WONT:
418                         case TELNET_STATE_DO:
419                         case TELNET_STATE_DONT:
420                                 t_con->state = TELNET_STATE_DATA;
421                                 break;
422                         case TELNET_STATE_ESCAPE:
423                                 if (t_con->last_escape == '[')
424                                 {
425                                         if (*buf_p == 'D') /* cursor left */
426                                         {
427                                                 if (t_con->line_cursor > 0)
428                                                 {
429                                                         telnet_write(connection, "\b", 1);
430                                                         t_con->line_cursor--;
431                                                 }
432                                                 t_con->state = TELNET_STATE_DATA;
433                                         }
434                                         else if (*buf_p == 'C') /* cursor right */
435                                         {
436                                                 if (t_con->line_cursor < t_con->line_size)
437                                                 {
438                                                         telnet_write(connection, t_con->line + t_con->line_cursor++, 1);
439                                                 }
440                                                 t_con->state = TELNET_STATE_DATA;
441                                         }
442                                         else if (*buf_p == 'A') /* cursor up */
443                                         {
444                                                 int last_history = (t_con->current_history > 0) ? t_con->current_history - 1 : TELNET_LINE_HISTORY_SIZE-1;
445                                                 if (t_con->history[last_history])
446                                                 {
447                                                         telnet_clear_line(connection, t_con);
448                                                         t_con->line_size = strlen(t_con->history[last_history]);
449                                                         t_con->line_cursor = t_con->line_size;
450                                                         memcpy(t_con->line, t_con->history[last_history], t_con->line_size);
451                                                         telnet_write(connection, t_con->line, t_con->line_size);
452                                                         t_con->current_history = last_history;
453                                                 }
454                                                 t_con->state = TELNET_STATE_DATA;
455                                         }
456                                         else if (*buf_p == 'B') /* cursor down */
457                                         {
458                                                 int next_history = (t_con->current_history + 1) % TELNET_LINE_HISTORY_SIZE;
459                                                 if (t_con->history[next_history])
460                                                 {
461                                                         telnet_clear_line(connection, t_con);
462                                                         t_con->line_size = strlen(t_con->history[next_history]);
463                                                         t_con->line_cursor = t_con->line_size;
464                                                         memcpy(t_con->line, t_con->history[next_history], t_con->line_size);
465                                                         telnet_write(connection, t_con->line, t_con->line_size);
466                                                         t_con->current_history = next_history;
467                                                 }
468                                                 t_con->state = TELNET_STATE_DATA;
469                                         }
470                                         else if (*buf_p == '3')
471                                         {
472                                                 t_con->last_escape = *buf_p;
473                                         }
474                                         else
475                                         {
476                                                 t_con->state = TELNET_STATE_DATA;
477                                         }
478                                 }
479                                 else if (t_con->last_escape == '3')
480                                 {
481                                         /* Remove character */
482                                         if (*buf_p == '~')
483                                         {
484                                                 if (t_con->line_cursor < t_con->line_size)
485                                                 {
486                                                         int i;
487                                                         t_con->line_size--;
488                                                         /* remove char from line buffer */
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);
490
491                                                         /* print remainder of buffer */
492                                                         telnet_write(connection, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);
493                                                         /* overwrite last char with whitespace */
494                                                         telnet_write(connection, " \b", 2);
495
496                                                         /* move back to cursor position*/
497                                                         for (i = t_con->line_cursor; i < t_con->line_size; i++)
498                                                         {
499                                                                 telnet_write(connection, "\b", 1);
500                                                         }
501                                                 }
502
503                                                 t_con->state = TELNET_STATE_DATA;
504                                         }
505                                         else
506                                         {
507                                                 t_con->state = TELNET_STATE_DATA;
508                                         }
509                                 }
510                                 else if (t_con->last_escape == '\x00')
511                                 {
512                                         if (*buf_p == '[')
513                                         {
514                                                 t_con->last_escape = *buf_p;
515                                         }
516                                         else
517                                         {
518                                                 t_con->state = TELNET_STATE_DATA;
519                                         }
520                                 }
521                                 else
522                                 {
523                                         LOG_ERROR("BUG: unexpected value in t_con->last_escape");
524                                         t_con->state = TELNET_STATE_DATA;
525                                 }
526
527                                 break;
528                         default:
529                                 LOG_ERROR("unknown telnet state");
530                                 exit(-1);
531                 }
532
533                 bytes_read--;
534                 buf_p++;
535         }
536
537         return ERROR_OK;
538 }
539
540 int telnet_connection_closed(connection_t *connection)
541 {
542         telnet_connection_t *t_con = connection->priv;
543         int i;
544
545         log_remove_callback(telnet_log_callback, connection);
546
547         if (t_con->prompt)
548         {
549                 free(t_con->prompt);
550                 t_con->prompt = NULL;
551         }
552
553         for (i = 0; i < TELNET_LINE_HISTORY_SIZE; i++)
554         {
555                 if (t_con->history[i])
556                 {
557                         free(t_con->history[i]);
558                         t_con->history[i] = NULL;
559                 }
560         }
561
562         /* if this connection registered a debug-message receiver delete it */
563         delete_debug_msg_receiver(connection->cmd_ctx, NULL);
564
565         if (connection->priv)
566         {
567                 free(connection->priv);
568                 connection->priv = NULL;
569         }
570         else
571         {
572                 LOG_ERROR("BUG: connection->priv == NULL");
573         }
574
575         return ERROR_OK;
576 }
577
578 int telnet_set_prompt(connection_t *connection, char *prompt)
579 {
580         telnet_connection_t *t_con = connection->priv;
581
582         if (t_con->prompt != NULL)
583                 free(t_con->prompt);
584
585         t_con->prompt = strdup(prompt);
586
587         return ERROR_OK;
588 }
589
590 int telnet_init(char *banner)
591 {
592         telnet_service_t *telnet_service = malloc(sizeof(telnet_service_t));
593
594         if (telnet_port == 0)
595         {
596                 LOG_INFO("telnet port disabled");
597                 free(telnet_service);
598                 return ERROR_OK;
599         }
600
601         telnet_service->banner = banner;
602
603         add_service("telnet", CONNECTION_TCP, telnet_port, 1, telnet_new_connection, telnet_input, telnet_connection_closed, telnet_service);
604
605         return ERROR_OK;
606 }
607
608 /* daemon configuration command telnet_port */
609 static int handle_telnet_port_command(struct command_context_s *cmd_ctx,
610                 char *cmd, char **args, int argc)
611 {
612         return server_port_command(cmd_ctx, cmd, args, argc, &telnet_port);
613 }
614
615 static int handle_exit_command(struct command_context_s *cmd_ctx,
616                 char *cmd, char **args, int argc)
617 {
618         return ERROR_COMMAND_CLOSE_CONNECTION;
619 }
620
621 int telnet_register_commands(command_context_t *command_context)
622 {
623         register_command(command_context, NULL, "exit",
624                         &handle_exit_command, COMMAND_EXEC,
625                         "exit telnet session");
626
627         register_command(command_context, NULL, "telnet_port",
628                         &handle_telnet_port_command, COMMAND_ANY,
629                         "port on which to listen for incoming telnet connections");
630
631         return ERROR_OK;
632 }
633
634