92052ae88bfec9fd732bdee9078b74e50e95d513
[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-2010 Ã˜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/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 static int telnet_write(struct connection *connection, const void *data,
48                 int len)
49 {
50         struct telnet_connection *t_con = connection->priv;
51         if (t_con->closed)
52                 return ERROR_SERVER_REMOTE_CLOSED;
53
54         if (write_socket(connection->fd_out, data, len) == len)
55         {
56                 return ERROR_OK;
57         }
58         t_con->closed = 1;
59         return ERROR_SERVER_REMOTE_CLOSED;
60 }
61
62 static int telnet_prompt(struct connection *connection)
63 {
64         struct telnet_connection *t_con = connection->priv;
65
66         return telnet_write(connection, t_con->prompt, strlen(t_con->prompt));
67 }
68
69 static int telnet_outputline(struct connection *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 static int telnet_output(struct command_context *cmd_ctx, const char* line)
98 {
99         struct connection *connection = cmd_ctx->output_handler_priv;
100
101         return telnet_outputline(connection, line);
102 }
103
104 static void telnet_log_callback(void *priv, const char *file, unsigned line,
105                 const char *function, const char *string)
106 {
107         struct connection *connection = priv;
108         struct telnet_connection *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         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);
125
126         /* output the message */
127         telnet_outputline(connection, string);
128
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);
134 }
135
136 static int telnet_new_connection(struct connection *connection)
137 {
138         struct telnet_connection *telnet_connection = malloc(sizeof(struct telnet_connection));
139         struct telnet_service *telnet_service = connection->service->priv;
140         int i;
141
142         connection->priv = telnet_connection;
143
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;
151
152         /* output goes through telnet connection */
153         command_set_output_handler(connection->cmd_ctx, telnet_output, connection);
154
155         /* negotiate telnet options */
156         telnet_write(connection, negotiate, strlen(negotiate));
157
158         /* print connection banner */
159         if (telnet_service->banner)
160         {
161                 telnet_write(connection, telnet_service->banner, strlen(telnet_service->banner));
162                 telnet_write(connection, "\r\n", 2);
163         }
164
165         telnet_write(connection, "\r", 1); /* the prompt is always placed at the line beginning */
166         telnet_prompt(connection);
167
168         /* initialize history */
169         for (i = 0; i < TELNET_LINE_HISTORY_SIZE; i++)
170         {
171                 telnet_connection->history[i] = NULL;
172         }
173         telnet_connection->next_history = 0;
174         telnet_connection->current_history = 0;
175
176         log_add_callback(telnet_log_callback, connection);
177
178         return ERROR_OK;
179 }
180
181 static void telnet_clear_line(struct connection *connection,
182                 struct telnet_connection *t_con)
183 {
184         /* move to end of line */
185         if (t_con->line_cursor < t_con->line_size)
186         {
187                 telnet_write(connection, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor);
188         }
189
190         /* backspace, overwrite with space, backspace */
191         while (t_con->line_size > 0)
192         {
193                 telnet_write(connection, "\b \b", 3);
194                 t_con->line_size--;
195         }
196         t_con->line_cursor = 0;
197 }
198
199 static int telnet_input(struct connection *connection)
200 {
201         int bytes_read;
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;
206
207         bytes_read = read_socket(connection->fd, buffer, TELNET_BUFFER_SIZE);
208
209         if (bytes_read == 0)
210                 return ERROR_SERVER_REMOTE_CLOSED;
211         else if (bytes_read == -1)
212         {
213                 LOG_ERROR("error during read: %s", strerror(errno));
214                 return ERROR_SERVER_REMOTE_CLOSED;
215         }
216
217         buf_p = buffer;
218         while (bytes_read)
219         {
220                 switch (t_con->state)
221                 {
222                         case TELNET_STATE_DATA:
223                                 if (*buf_p == 0xff)
224                                 {
225                                         t_con->state = TELNET_STATE_IAC;
226                                 }
227                                 else
228                                 {
229                                         if (isprint(*buf_p)) /* printable character */
230                                         {
231                                                 /* watch buffer size leaving one spare character for string null termination */
232                                                 if (t_con->line_size == TELNET_LINE_MAX_SIZE-1)
233                                                 {
234                                                         /* output audible bell if buffer is full */
235                                                         telnet_write(connection, "\x07", 1); /* "\a" does not work, at least on windows */
236                                                 }
237                                                 else if (t_con->line_cursor == t_con->line_size)
238                                                 {
239                                                         telnet_write(connection, buf_p, 1);
240                                                         t_con->line[t_con->line_size++] = *buf_p;
241                                                         t_con->line_cursor++;
242                                                 }
243                                                 else
244                                                 {
245                                                         int i;
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;
248                                                         t_con->line_size++;
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++)
252                                                         {
253                                                                 telnet_write(connection, "\b", 1);
254                                                         }
255                                                 }
256                                         }
257                                         else /* non-printable */
258                                         {
259                                                 if (*buf_p == 0x1b) /* escape */
260                                                 {
261                                                         t_con->state = TELNET_STATE_ESCAPE;
262                                                         t_con->last_escape = '\x00';
263                                                 }
264                                                 else if ((*buf_p == 0xd) || (*buf_p == 0xa)) /* CR/LF */
265                                                 {
266                                                         int retval;
267
268                                                         /* skip over combinations with CR/LF and NUL characters */
269                                                         if ((bytes_read > 1) && ((*(buf_p + 1) == 0xa) || (*(buf_p + 1) == 0xd)))
270                                                         {
271                                                                 buf_p++;
272                                                                 bytes_read--;
273                                                         }
274                                                         if ((bytes_read > 1) && (*(buf_p + 1) == 0))
275                                                         {
276                                                                 buf_p++;
277                                                                 bytes_read--;
278                                                         }
279                                                         t_con->line[t_con->line_size] = 0;
280
281                                                         telnet_write(connection, "\r\n\x00", 3);
282
283                                                         if (strcmp(t_con->line, "history") == 0)
284                                                         {
285                                                                 int i;
286                                                                 for (i = 1; i < TELNET_LINE_HISTORY_SIZE; i++)
287                                                                 {
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];
290                                                                         if (history_line)
291                                                                         {
292                                                                                 telnet_write(connection, history_line, strlen(history_line));
293                                                                                 telnet_write(connection, "\r\n\x00", 3);
294                                                                         }
295                                                                 }
296                                                                 t_con->line_size = 0;
297                                                                 t_con->line_cursor = 0;
298                                                                 continue;
299                                                         }
300
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)))
304                                                         {
305                                                                 /* if the history slot is already taken, free it */
306                                                                 if (t_con->history[t_con->next_history])
307                                                                 {
308                                                                         free(t_con->history[t_con->next_history]);
309                                                                 }
310
311                                                                 /* add line to history */
312                                                                 t_con->history[t_con->next_history] = strdup(t_con->line);
313
314                                                                 /* wrap history at TELNET_LINE_HISTORY_SIZE */
315                                                                 t_con->next_history = (t_con->next_history + 1) % TELNET_LINE_HISTORY_SIZE;
316
317                                                                 /* current history line starts at the new entry */
318                                                                 t_con->current_history = t_con->next_history;
319
320                                                                 if (t_con->history[t_con->current_history])
321                                                                 {
322                                                                         free(t_con->history[t_con->current_history]);
323                                                                 }
324                                                                 t_con->history[t_con->current_history] = strdup("");
325                                                         }
326
327                                                         t_con->line_size = 0;
328
329                                                         t_con->line_cursor = -1; /* to supress prompt in log callback during command execution */
330
331                                                         retval = command_run_line(command_context, t_con->line);
332
333                                                         t_con->line_cursor = 0;
334
335                                                         if (retval == ERROR_COMMAND_CLOSE_CONNECTION)
336                                                                 return ERROR_SERVER_REMOTE_CLOSED;
337
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;
342
343                                                 }
344                                                 else if ((*buf_p == 0x7f) || (*buf_p == 0x8)) /* delete character */
345                                                 {
346                                                         if (t_con->line_cursor > 0)
347                                                         {
348                                                                 if (t_con->line_cursor != t_con->line_size)
349                                                                 {
350                                                                         int i;
351                                                                         telnet_write(connection, "\b", 1);
352                                                                         t_con->line_cursor--;
353                                                                         t_con->line_size--;
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);
355
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++)
359                                                                         {
360                                                                                 telnet_write(connection, "\b", 1);
361                                                                         }
362                                                                 }
363                                                                 else
364                                                                 {
365                                                                         t_con->line_size--;
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);
369                                                                 }
370                                                         }
371                                                 }
372                                                 else if (*buf_p == 0x15) /* clear line */
373                                                 {
374                                                         telnet_clear_line(connection, t_con);
375                                                 }
376                                                 else if (*buf_p == CTRL('B')) /* cursor left */
377                                                 {
378                                                         if (t_con->line_cursor > 0)
379                                                         {
380                                                                 telnet_write(connection, "\b", 1);
381                                                                 t_con->line_cursor--;
382                                                         }
383                                                         t_con->state = TELNET_STATE_DATA;
384                                                 }
385                                                 else if (*buf_p == CTRL('F')) /* cursor right */
386                                                 {
387                                                         if (t_con->line_cursor < t_con->line_size)
388                                                         {
389                                                                 telnet_write(connection, t_con->line + t_con->line_cursor++, 1);
390                                                         }
391                                                         t_con->state = TELNET_STATE_DATA;
392                                                 }
393                                                 else
394                                                 {
395                                                         LOG_DEBUG("unhandled nonprintable: %2.2x", *buf_p);
396                                                 }
397                                         }
398                                 }
399                                 break;
400                         case TELNET_STATE_IAC:
401                                 switch (*buf_p)
402                                 {
403                                         case 0xfe:
404                                                 t_con->state = TELNET_STATE_DONT;
405                                                 break;
406                                         case 0xfd:
407                                                 t_con->state = TELNET_STATE_DO;
408                                                 break;
409                                         case 0xfc:
410                                                 t_con->state = TELNET_STATE_WONT;
411                                                 break;
412                                         case 0xfb:
413                                                 t_con->state = TELNET_STATE_WILL;
414                                                 break;
415                                 }
416                                 break;
417                         case TELNET_STATE_SB:
418                                 break;
419                         case TELNET_STATE_SE:
420                                 break;
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;
426                                 break;
427                         case TELNET_STATE_ESCAPE:
428                                 if (t_con->last_escape == '[')
429                                 {
430                                         if (*buf_p == 'D') /* cursor left */
431                                         {
432                                                 if (t_con->line_cursor > 0)
433                                                 {
434                                                         telnet_write(connection, "\b", 1);
435                                                         t_con->line_cursor--;
436                                                 }
437                                                 t_con->state = TELNET_STATE_DATA;
438                                         }
439                                         else if (*buf_p == 'C') /* cursor right */
440                                         {
441                                                 if (t_con->line_cursor < t_con->line_size)
442                                                 {
443                                                         telnet_write(connection, t_con->line + t_con->line_cursor++, 1);
444                                                 }
445                                                 t_con->state = TELNET_STATE_DATA;
446                                         }
447                                         else if (*buf_p == 'A') /* cursor up */
448                                         {
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])
451                                                 {
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;
458                                                 }
459                                                 t_con->state = TELNET_STATE_DATA;
460                                         }
461                                         else if (*buf_p == 'B') /* cursor down */
462                                         {
463                                                 int next_history = (t_con->current_history + 1) % TELNET_LINE_HISTORY_SIZE;
464                                                 if (t_con->history[next_history])
465                                                 {
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;
472                                                 }
473                                                 t_con->state = TELNET_STATE_DATA;
474                                         }
475                                         else if (*buf_p == '3')
476                                         {
477                                                 t_con->last_escape = *buf_p;
478                                         }
479                                         else
480                                         {
481                                                 t_con->state = TELNET_STATE_DATA;
482                                         }
483                                 }
484                                 else if (t_con->last_escape == '3')
485                                 {
486                                         /* Remove character */
487                                         if (*buf_p == '~')
488                                         {
489                                                 if (t_con->line_cursor < t_con->line_size)
490                                                 {
491                                                         int i;
492                                                         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);
495
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);
500
501                                                         /* move back to cursor position*/
502                                                         for (i = t_con->line_cursor; i < t_con->line_size; i++)
503                                                         {
504                                                                 telnet_write(connection, "\b", 1);
505                                                         }
506                                                 }
507
508                                                 t_con->state = TELNET_STATE_DATA;
509                                         }
510                                         else
511                                         {
512                                                 t_con->state = TELNET_STATE_DATA;
513                                         }
514                                 }
515                                 else if (t_con->last_escape == '\x00')
516                                 {
517                                         if (*buf_p == '[')
518                                         {
519                                                 t_con->last_escape = *buf_p;
520                                         }
521                                         else
522                                         {
523                                                 t_con->state = TELNET_STATE_DATA;
524                                         }
525                                 }
526                                 else
527                                 {
528                                         LOG_ERROR("BUG: unexpected value in t_con->last_escape");
529                                         t_con->state = TELNET_STATE_DATA;
530                                 }
531
532                                 break;
533                         default:
534                                 LOG_ERROR("unknown telnet state");
535                                 exit(-1);
536                 }
537
538                 bytes_read--;
539                 buf_p++;
540         }
541
542         return ERROR_OK;
543 }
544
545 static int telnet_connection_closed(struct connection *connection)
546 {
547         struct telnet_connection *t_con = connection->priv;
548         int i;
549
550         log_remove_callback(telnet_log_callback, connection);
551
552         if (t_con->prompt)
553         {
554                 free(t_con->prompt);
555                 t_con->prompt = NULL;
556         }
557
558         for (i = 0; i < TELNET_LINE_HISTORY_SIZE; i++)
559         {
560                 if (t_con->history[i])
561                 {
562                         free(t_con->history[i]);
563                         t_con->history[i] = NULL;
564                 }
565         }
566
567         /* if this connection registered a debug-message receiver delete it */
568         delete_debug_msg_receiver(connection->cmd_ctx, NULL);
569
570         if (connection->priv)
571         {
572                 free(connection->priv);
573                 connection->priv = NULL;
574         }
575         else
576         {
577                 LOG_ERROR("BUG: connection->priv == NULL");
578         }
579
580         return ERROR_OK;
581 }
582
583 int telnet_init(char *banner)
584 {
585         struct telnet_service *telnet_service = malloc(sizeof(struct telnet_service));
586
587         if (telnet_port == 0)
588         {
589                 LOG_INFO("telnet port disabled");
590                 free(telnet_service);
591                 return ERROR_OK;
592         }
593
594         telnet_service->banner = banner;
595
596         add_service("telnet", CONNECTION_TCP, telnet_port, 1, telnet_new_connection, telnet_input, telnet_connection_closed, telnet_service);
597
598         return ERROR_OK;
599 }
600
601 /* daemon configuration command telnet_port */
602 COMMAND_HANDLER(handle_telnet_port_command)
603 {
604         return CALL_COMMAND_HANDLER(server_port_command, &telnet_port);
605 }
606
607 COMMAND_HANDLER(handle_exit_command)
608 {
609         return ERROR_COMMAND_CLOSE_CONNECTION;
610 }
611
612 static const struct command_registration telnet_command_handlers[] = {
613         {
614                 .name = "exit",
615                 .handler = handle_exit_command,
616                 .mode = COMMAND_EXEC,
617                 .help = "exit telnet session",
618         },
619         {
620                 .name = "telnet_port",
621                 .handler = handle_telnet_port_command,
622                 .mode = COMMAND_ANY,
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]",
627         },
628         COMMAND_REGISTRATION_DONE
629 };
630
631 int telnet_register_commands(struct command_context *cmd_ctx)
632 {
633         return register_commands(cmd_ctx, NULL, telnet_command_handlers);
634 }