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

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)