]> git.proxmox.com Git - mirror_frr.git/blame - lib/vty.c
Merge pull request #12389 from opensourcerouting/ospf6d-asbr-infinite-loop
[mirror_frr.git] / lib / vty.c
CommitLineData
718e3744 1/*
2 * Virtual terminal [aka TeletYpe] interface routine.
3 * Copyright (C) 1997, 98 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
896014f4
DL
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
718e3744 20 */
21
22#include <zebra.h>
23
fe6b47b9
QY
24#include <lib/version.h>
25#include <sys/types.h>
beee9b4a 26#include <sys/types.h>
061f5d1c
DA
27#ifdef HAVE_LIBPCRE2_POSIX
28#ifndef _FRR_PCRE2_POSIX
29#define _FRR_PCRE2_POSIX
30#include <pcre2posix.h>
31#endif /* _FRR_PCRE2_POSIX */
32#elif defined(HAVE_LIBPCREPOSIX)
beee9b4a
DS
33#include <pcreposix.h>
34#else
fe6b47b9 35#include <regex.h>
061f5d1c 36#endif /* HAVE_LIBPCRE2_POSIX */
6ef6e4f0 37#include <stdio.h>
fe6b47b9 38
718e3744 39#include "linklist.h"
b21b19c5 40#include "thread.h"
718e3744 41#include "buffer.h"
718e3744 42#include "command.h"
43#include "sockunion.h"
718e3744 44#include "memory.h"
718e3744 45#include "log.h"
46#include "prefix.h"
47#include "filter.h"
b21b19c5 48#include "vty.h"
edd7c245 49#include "privs.h"
9fc7ebf1 50#include "network.h"
b85120bc 51#include "libfrr.h"
fe6b47b9 52#include "frrstr.h"
ab99c8e2 53#include "lib_errors.h"
1c2facd1 54#include "northbound_cli.h"
807f5b98 55#include "printfrr.h"
a8dfd147 56#include "json.h"
9fc7ebf1 57
58#include <arpa/telnet.h>
b510a06e 59#include <termios.h>
718e3744 60
2950f5da 61#include "lib/vty_clippy.c"
2950f5da 62
bf8d3d6a 63DEFINE_MTYPE_STATIC(LIB, VTY, "VTY");
26ae7cc2 64DEFINE_MTYPE_STATIC(LIB, VTY_SERV, "VTY server");
bf8d3d6a
DL
65DEFINE_MTYPE_STATIC(LIB, VTY_OUT_BUF, "VTY output buffer");
66DEFINE_MTYPE_STATIC(LIB, VTY_HIST, "VTY history");
4a1ab8e4 67
43dd8caf
DL
68DECLARE_DLIST(vtys, struct vty, itm);
69
718e3744 70/* Vty events */
55a70ffb 71enum vty_event {
d62a17ae 72 VTY_SERV,
73 VTY_READ,
74 VTY_WRITE,
75 VTY_TIMEOUT_RESET,
718e3744 76#ifdef VTYSH
d62a17ae 77 VTYSH_SERV,
78 VTYSH_READ,
79 VTYSH_WRITE
718e3744 80#endif /* VTYSH */
81};
82
26ae7cc2
DL
83PREDECL_DLIST(vtyservs);
84
85struct vty_serv {
86 struct vtyservs_item itm;
87
88 int sock;
89 bool vtysh;
90
91 struct thread *t_accept;
92};
93
94DECLARE_DLIST(vtyservs, struct vty_serv, itm);
95
55a70ffb
DS
96static void vty_event_serv(enum vty_event event, struct vty_serv *);
97static void vty_event(enum vty_event, struct vty *);
718e3744 98
99/* Extern host structure from command.c */
100extern struct host host;
6b0655a2 101
26ae7cc2
DL
102/* active listeners */
103static struct vtyservs_head vty_servs[1] = {INIT_DLIST(vty_servs[0])};
104
43dd8caf
DL
105/* active connections */
106static struct vtys_head vty_sessions[1] = {INIT_DLIST(vty_sessions[0])};
107static struct vtys_head vtysh_sessions[1] = {INIT_DLIST(vtysh_sessions[0])};
763725cd 108
718e3744 109/* Vty timeout value. */
110static unsigned long vty_timeout_val = VTY_TIMEOUT_DEFAULT;
111
112/* Vty access-class command */
113static char *vty_accesslist_name = NULL;
114
115/* Vty access-calss for IPv6. */
116static char *vty_ipv6_accesslist_name = NULL;
117
718e3744 118/* Current directory. */
1b3e9a21 119static char vty_cwd[MAXPATHLEN];
718e3744 120
718e3744 121/* Login password check. */
122static int no_password_check = 0;
123
124/* Integrated configuration file path */
c17faa4b 125static char integrate_default[] = SYSCONFDIR INTEGRATE_DEFAULT_CONFIG;
718e3744 126
2950f5da
DS
127static bool do_log_commands;
128static bool do_log_commands_perm;
6b0655a2 129
2071aa0e
DL
130void vty_frame(struct vty *vty, const char *format, ...)
131{
132 va_list args;
133
134 va_start(args, format);
c7179009
DL
135 vsnprintfrr(vty->frame + vty->frame_pos,
136 sizeof(vty->frame) - vty->frame_pos, format, args);
2071aa0e
DL
137 vty->frame_pos = strlen(vty->frame);
138 va_end(args);
139}
140
141void vty_endframe(struct vty *vty, const char *endtext)
142{
143 if (vty->frame_pos == 0 && endtext)
144 vty_out(vty, "%s", endtext);
145 vty->frame_pos = 0;
146}
147
fe6b47b9
QY
148bool vty_set_include(struct vty *vty, const char *regexp)
149{
150 int errcode;
151 bool ret = true;
152 char errbuf[256];
153
8224c6cf 154 if (!regexp) {
155 if (vty->filter) {
156 regfree(&vty->include);
157 vty->filter = false;
158 }
fe6b47b9
QY
159 return true;
160 }
161
162 errcode = regcomp(&vty->include, regexp,
163 REG_EXTENDED | REG_NEWLINE | REG_NOSUB);
164 if (errcode) {
165 ret = false;
511942ba
IR
166 regerror(errcode, &vty->include, errbuf, sizeof(errbuf));
167 vty_out(vty, "%% Regex compilation error: %s\n", errbuf);
fe6b47b9
QY
168 } else {
169 vty->filter = true;
170 }
171
172 return ret;
173}
174
83eba583 175/* VTY standard output function. */
d62a17ae 176int vty_out(struct vty *vty, const char *format, ...)
177{
178 va_list args;
807f5b98 179 ssize_t len;
d62a17ae 180 char buf[1024];
fa3bf3a2 181 char *p = NULL;
fe6b47b9 182 char *filtered;
2c12c904
DL
183 /* format string may contain %m, keep errno intact for printfrr */
184 int saved_errno = errno;
d62a17ae 185
2071aa0e
DL
186 if (vty->frame_pos) {
187 vty->frame_pos = 0;
188 vty_out(vty, "%s", vty->frame);
189 }
190
2cddf2ff 191 va_start(args, format);
2c12c904 192 errno = saved_errno;
807f5b98 193 p = vasnprintfrr(MTYPE_VTY_OUT_BUF, buf, sizeof(buf), format, args);
2cddf2ff 194 va_end(args);
d62a17ae 195
807f5b98 196 len = strlen(p);
2cddf2ff
QY
197
198 /* filter buffer */
199 if (vty->filter) {
0b42d81a
QY
200 vector lines = frrstr_split_vec(p, "\n");
201
202 /* Place first value in the cache */
203 char *firstline = vector_slot(lines, 0);
204 buffer_put(vty->lbuf, (uint8_t *) firstline, strlen(firstline));
205
206 /* If our split returned more than one entry, time to filter */
207 if (vector_active(lines) > 1) {
208 /*
209 * returned string is MTYPE_TMP so it matches the MTYPE
210 * of everything else in the vector
211 */
212 char *bstr = buffer_getstr(vty->lbuf);
213 buffer_reset(vty->lbuf);
214 XFREE(MTYPE_TMP, lines->index[0]);
215 vector_set_index(lines, 0, bstr);
216 frrstr_filter_vec(lines, &vty->include);
217 vector_compact(lines);
218 /*
219 * Consider the string "foo\n". If the regex is an empty string
220 * and the line ended with a newline, then the vector will look
221 * like:
222 *
223 * [0]: 'foo'
224 * [1]: ''
225 *
226 * If the regex isn't empty, the vector will look like:
227 *
228 * [0]: 'foo'
229 *
230 * In this case we'd like to preserve the newline, so we add
231 * the empty string [1] as in the first example.
232 */
233 if (p[strlen(p) - 1] == '\n' && vector_active(lines) > 0
234 && strlen(vector_slot(lines, vector_active(lines) - 1)))
235 vector_set(lines, XSTRDUP(MTYPE_TMP, ""));
236
237 filtered = frrstr_join_vec(lines, "\n");
238 }
239 else {
240 filtered = NULL;
241 }
5d806ec6 242
2cddf2ff 243 frrstr_strvec_free(lines);
0b42d81a 244
2cddf2ff
QY
245 } else {
246 filtered = p;
247 }
d62a17ae 248
0b42d81a
QY
249 if (!filtered)
250 goto done;
251
2cddf2ff
QY
252 switch (vty->type) {
253 case VTY_TERM:
254 /* print with crlf replacement */
255 buffer_put_crlf(vty->obuf, (uint8_t *)filtered,
256 strlen(filtered));
257 break;
258 case VTY_SHELL:
9934e1c9 259 if (vty->of) {
260 fprintf(vty->of, "%s", filtered);
261 fflush(vty->of);
262 } else if (vty->of_saved) {
263 fprintf(vty->of_saved, "%s", filtered);
264 fflush(vty->of_saved);
265 }
2cddf2ff
QY
266 break;
267 case VTY_SHELL_SERV:
268 case VTY_FILE:
269 default:
270 /* print without crlf replacement */
271 buffer_put(vty->obuf, (uint8_t *)filtered, strlen(filtered));
272 break;
d62a17ae 273 }
274
0b42d81a
QY
275done:
276
277 if (vty->filter && filtered)
2cddf2ff
QY
278 XFREE(MTYPE_TMP, filtered);
279
280 /* If p is not different with buf, it is allocated buffer. */
281 if (p != buf)
282 XFREE(MTYPE_VTY_OUT_BUF, p);
283
d62a17ae 284 return len;
285}
286
a8dfd147
DL
287int vty_json(struct vty *vty, struct json_object *json)
288{
289 const char *text;
290
291 if (!json)
292 return CMD_SUCCESS;
293
294 text = json_object_to_json_string_ext(
295 json, JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_NOSLASHESCAPE);
296 vty_out(vty, "%s\n", text);
297 json_object_free(json);
298
299 return CMD_SUCCESS;
300}
301
718e3744 302/* Output current time to the vty. */
d62a17ae 303void vty_time_print(struct vty *vty, int cr)
718e3744 304{
e36f61b5 305 char buf[FRR_TIMESTAMP_LEN];
d0bfb22c 306
e36f61b5
DS
307 if (frr_timestamp(0, buf, sizeof(buf)) == 0) {
308 zlog_info("frr_timestamp error");
d62a17ae 309 return;
310 }
311 if (cr)
312 vty_out(vty, "%s\n", buf);
313 else
314 vty_out(vty, "%s ", buf);
718e3744 315
d62a17ae 316 return;
718e3744 317}
318
319/* Say hello to vty interface. */
d62a17ae 320void vty_hello(struct vty *vty)
321{
322 if (host.motdfile) {
323 FILE *f;
324 char buf[4096];
325
326 f = fopen(host.motdfile, "r");
327 if (f) {
328 while (fgets(buf, sizeof(buf), f)) {
329 char *s;
330 /* work backwards to ignore trailling isspace()
331 */
332 for (s = buf + strlen(buf);
fefa5e0f
DL
333 (s > buf) && isspace((unsigned char)s[-1]);
334 s--)
d62a17ae 335 ;
336 *s = '\0';
337 vty_out(vty, "%s\n", buf);
338 }
339 fclose(f);
340 } else
341 vty_out(vty, "MOTD file not found\n");
342 } else if (host.motd)
343 vty_out(vty, "%s", host.motd);
718e3744 344}
345
346/* Put out prompt and wait input from user. */
d62a17ae 347static void vty_prompt(struct vty *vty)
718e3744 348{
d62a17ae 349 if (vty->type == VTY_TERM) {
60466a63 350 vty_out(vty, cmd_prompt(vty->node), cmd_hostname_get());
d62a17ae 351 }
718e3744 352}
353
354/* Send WILL TELOPT_ECHO to remote server. */
d62a17ae 355static void vty_will_echo(struct vty *vty)
718e3744 356{
d62a17ae 357 unsigned char cmd[] = {IAC, WILL, TELOPT_ECHO, '\0'};
358 vty_out(vty, "%s", cmd);
718e3744 359}
360
361/* Make suppress Go-Ahead telnet option. */
d62a17ae 362static void vty_will_suppress_go_ahead(struct vty *vty)
718e3744 363{
d62a17ae 364 unsigned char cmd[] = {IAC, WILL, TELOPT_SGA, '\0'};
365 vty_out(vty, "%s", cmd);
718e3744 366}
367
368/* Make don't use linemode over telnet. */
d62a17ae 369static void vty_dont_linemode(struct vty *vty)
718e3744 370{
d62a17ae 371 unsigned char cmd[] = {IAC, DONT, TELOPT_LINEMODE, '\0'};
372 vty_out(vty, "%s", cmd);
718e3744 373}
374
375/* Use window size. */
d62a17ae 376static void vty_do_window_size(struct vty *vty)
718e3744 377{
d62a17ae 378 unsigned char cmd[] = {IAC, DO, TELOPT_NAWS, '\0'};
379 vty_out(vty, "%s", cmd);
718e3744 380}
381
718e3744 382/* Authentication of vty */
d62a17ae 383static void vty_auth(struct vty *vty, char *buf)
384{
385 char *passwd = NULL;
386 enum node_type next_node = 0;
387 int fail;
d62a17ae 388
389 switch (vty->node) {
390 case AUTH_NODE:
391 if (host.encrypt)
392 passwd = host.password_encrypt;
393 else
394 passwd = host.password;
395 if (host.advanced)
396 next_node = host.enable ? VIEW_NODE : ENABLE_NODE;
397 else
398 next_node = VIEW_NODE;
399 break;
400 case AUTH_ENABLE_NODE:
401 if (host.encrypt)
402 passwd = host.enable_encrypt;
403 else
404 passwd = host.enable;
405 next_node = ENABLE_NODE;
406 break;
407 }
408
409 if (passwd) {
410 if (host.encrypt)
411 fail = strcmp(crypt(buf, passwd), passwd);
412 else
413 fail = strcmp(buf, passwd);
414 } else
415 fail = 1;
416
417 if (!fail) {
418 vty->fail = 0;
419 vty->node = next_node; /* Success ! */
420 } else {
421 vty->fail++;
422 if (vty->fail >= 3) {
423 if (vty->node == AUTH_NODE) {
424 vty_out(vty,
425 "%% Bad passwords, too many failures!\n");
426 vty->status = VTY_CLOSE;
427 } else {
428 /* AUTH_ENABLE_NODE */
429 vty->fail = 0;
430 vty_out(vty,
431 "%% Bad enable passwords, too many failures!\n");
432 vty->status = VTY_CLOSE;
433 }
434 }
435 }
718e3744 436}
437
438/* Command execution over the vty interface. */
d62a17ae 439static int vty_command(struct vty *vty, char *buf)
440{
441 int ret;
d62a17ae 442 const char *protocolname;
443 char *cp = NULL;
444
b575a12c
A
445 assert(vty);
446
d62a17ae 447 /*
448 * Log non empty command lines
449 */
a8a0f80b
DS
450 if (do_log_commands &&
451 strncmp(buf, "echo PING", strlen("echo PING")) != 0)
d62a17ae 452 cp = buf;
453 if (cp != NULL) {
454 /* Skip white spaces. */
fefa5e0f 455 while (isspace((unsigned char)*cp) && *cp != '\0')
d62a17ae 456 cp++;
457 }
458 if (cp != NULL && *cp != '\0') {
d62a17ae 459 char vty_str[VTY_BUFSIZ];
460 char prompt_str[VTY_BUFSIZ];
461
462 /* format the base vty info */
43dd8caf
DL
463 snprintf(vty_str, sizeof(vty_str), "vty[%d]@%s", vty->fd,
464 vty->address);
d62a17ae 465
466 /* format the prompt */
467 snprintf(prompt_str, sizeof(prompt_str), cmd_prompt(vty->node),
468 vty_str);
469
470 /* now log the command */
3ec8b5b8 471 zlog_notice("%s%s", prompt_str, buf);
d62a17ae 472 }
718e3744 473
45f01188
DL
474 RUSAGE_T before;
475 RUSAGE_T after;
476 unsigned long walltime, cputime;
477
478 /* cmd_execute() may change cputime_enabled if we're executing the
479 * "service cputime-stats" command, which can result in nonsensical
480 * and very confusing warnings
481 */
482 bool cputime_enabled_here = cputime_enabled;
483
484 GETRUSAGE(&before);
485
486 ret = cmd_execute(vty, buf, NULL, 0);
487
488 GETRUSAGE(&after);
489
490 walltime = thread_consumed_time(&after, &before, &cputime);
491
492 if (cputime_enabled_here && cputime_enabled && cputime_threshold
493 && cputime > cputime_threshold)
494 /* Warn about CPU hog that must be fixed. */
495 flog_warn(EC_LIB_SLOW_THREAD_CPU,
496 "CPU HOG: command took %lums (cpu time %lums): %s",
497 walltime / 1000, cputime / 1000, buf);
498 else if (walltime_threshold && walltime > walltime_threshold)
499 flog_warn(EC_LIB_SLOW_THREAD_WALL,
500 "STARVATION: command took %lums (cpu time %lums): %s",
501 walltime / 1000, cputime / 1000, buf);
502
503 /* Get the name of the protocol if any */
504 protocolname = frr_protoname;
924b9229 505
d62a17ae 506 if (ret != CMD_SUCCESS)
507 switch (ret) {
508 case CMD_WARNING:
509 if (vty->type == VTY_FILE)
510 vty_out(vty, "Warning...\n");
511 break;
512 case CMD_ERR_AMBIGUOUS:
513 vty_out(vty, "%% Ambiguous command.\n");
514 break;
515 case CMD_ERR_NO_MATCH:
516 vty_out(vty, "%% [%s] Unknown command: %s\n",
517 protocolname, buf);
518 break;
519 case CMD_ERR_INCOMPLETE:
520 vty_out(vty, "%% Command incomplete.\n");
521 break;
522 }
d62a17ae 523
524 return ret;
718e3744 525}
6b0655a2 526
9fc7ebf1 527static const char telnet_backward_char = 0x08;
528static const char telnet_space_char = ' ';
718e3744 529
530/* Basic function to write buffer to vty. */
d62a17ae 531static void vty_write(struct vty *vty, const char *buf, size_t nbytes)
718e3744 532{
d62a17ae 533 if ((vty->node == AUTH_NODE) || (vty->node == AUTH_ENABLE_NODE))
534 return;
718e3744 535
d62a17ae 536 /* Should we do buffering here ? And make vty_flush (vty) ? */
537 buffer_put(vty->obuf, buf, nbytes);
718e3744 538}
539
718e3744 540/* Basic function to insert character into vty. */
d62a17ae 541static void vty_self_insert(struct vty *vty, char c)
718e3744 542{
d62a17ae 543 int i;
544 int length;
718e3744 545
d62a17ae 546 if (vty->length + 1 >= VTY_BUFSIZ)
547 return;
2af38873 548
d62a17ae 549 length = vty->length - vty->cp;
550 memmove(&vty->buf[vty->cp + 1], &vty->buf[vty->cp], length);
551 vty->buf[vty->cp] = c;
718e3744 552
d62a17ae 553 vty_write(vty, &vty->buf[vty->cp], length + 1);
554 for (i = 0; i < length; i++)
555 vty_write(vty, &telnet_backward_char, 1);
718e3744 556
d62a17ae 557 vty->cp++;
558 vty->length++;
d1e4a518 559
d62a17ae 560 vty->buf[vty->length] = '\0';
718e3744 561}
562
563/* Self insert character 'c' in overwrite mode. */
d62a17ae 564static void vty_self_insert_overwrite(struct vty *vty, char c)
718e3744 565{
d62a17ae 566 if (vty->cp == vty->length) {
567 vty_self_insert(vty, c);
568 return;
569 }
718e3744 570
d62a17ae 571 vty->buf[vty->cp++] = c;
572 vty_write(vty, &c, 1);
718e3744 573}
574
2af38873
QY
575/**
576 * Insert a string into vty->buf at the current cursor position.
577 *
578 * If the resultant string would be larger than VTY_BUFSIZ it is
579 * truncated to fit.
580 */
d62a17ae 581static void vty_insert_word_overwrite(struct vty *vty, char *str)
718e3744 582{
d62a17ae 583 if (vty->cp == VTY_BUFSIZ)
584 return;
d1e4a518 585
d62a17ae 586 size_t nwrite = MIN((int)strlen(str), VTY_BUFSIZ - vty->cp - 1);
587 memcpy(&vty->buf[vty->cp], str, nwrite);
588 vty->cp += nwrite;
589 vty->length = MAX(vty->cp, vty->length);
590 vty->buf[vty->length] = '\0';
591 vty_write(vty, str, nwrite);
718e3744 592}
593
594/* Forward character. */
d62a17ae 595static void vty_forward_char(struct vty *vty)
718e3744 596{
d62a17ae 597 if (vty->cp < vty->length) {
598 vty_write(vty, &vty->buf[vty->cp], 1);
599 vty->cp++;
600 }
718e3744 601}
602
603/* Backward character. */
d62a17ae 604static void vty_backward_char(struct vty *vty)
718e3744 605{
d62a17ae 606 if (vty->cp > 0) {
607 vty->cp--;
608 vty_write(vty, &telnet_backward_char, 1);
609 }
718e3744 610}
611
612/* Move to the beginning of the line. */
d62a17ae 613static void vty_beginning_of_line(struct vty *vty)
718e3744 614{
d62a17ae 615 while (vty->cp)
616 vty_backward_char(vty);
718e3744 617}
618
619/* Move to the end of the line. */
d62a17ae 620static void vty_end_of_line(struct vty *vty)
718e3744 621{
d62a17ae 622 while (vty->cp < vty->length)
623 vty_forward_char(vty);
718e3744 624}
625
d62a17ae 626static void vty_kill_line_from_beginning(struct vty *);
627static void vty_redraw_line(struct vty *);
718e3744 628
629/* Print command line history. This function is called from
630 vty_next_line and vty_previous_line. */
d62a17ae 631static void vty_history_print(struct vty *vty)
718e3744 632{
d62a17ae 633 int length;
718e3744 634
d62a17ae 635 vty_kill_line_from_beginning(vty);
718e3744 636
d62a17ae 637 /* Get previous line from history buffer */
638 length = strlen(vty->hist[vty->hp]);
639 memcpy(vty->buf, vty->hist[vty->hp], length);
640 vty->cp = vty->length = length;
641 vty->buf[vty->length] = '\0';
718e3744 642
d62a17ae 643 /* Redraw current line */
644 vty_redraw_line(vty);
718e3744 645}
646
647/* Show next command line history. */
d62a17ae 648static void vty_next_line(struct vty *vty)
718e3744 649{
d62a17ae 650 int try_index;
718e3744 651
d62a17ae 652 if (vty->hp == vty->hindex)
653 return;
718e3744 654
d62a17ae 655 /* Try is there history exist or not. */
656 try_index = vty->hp;
657 if (try_index == (VTY_MAXHIST - 1))
658 try_index = 0;
659 else
660 try_index++;
718e3744 661
d62a17ae 662 /* If there is not history return. */
663 if (vty->hist[try_index] == NULL)
664 return;
665 else
666 vty->hp = try_index;
718e3744 667
d62a17ae 668 vty_history_print(vty);
718e3744 669}
670
671/* Show previous command line history. */
d62a17ae 672static void vty_previous_line(struct vty *vty)
718e3744 673{
d62a17ae 674 int try_index;
718e3744 675
d62a17ae 676 try_index = vty->hp;
677 if (try_index == 0)
678 try_index = VTY_MAXHIST - 1;
679 else
680 try_index--;
718e3744 681
d62a17ae 682 if (vty->hist[try_index] == NULL)
683 return;
684 else
685 vty->hp = try_index;
718e3744 686
d62a17ae 687 vty_history_print(vty);
718e3744 688}
689
690/* This function redraw all of the command line character. */
d62a17ae 691static void vty_redraw_line(struct vty *vty)
718e3744 692{
d62a17ae 693 vty_write(vty, vty->buf, vty->length);
694 vty->cp = vty->length;
718e3744 695}
696
697/* Forward word. */
d62a17ae 698static void vty_forward_word(struct vty *vty)
718e3744 699{
d62a17ae 700 while (vty->cp != vty->length && vty->buf[vty->cp] != ' ')
701 vty_forward_char(vty);
d0bfb22c 702
d62a17ae 703 while (vty->cp != vty->length && vty->buf[vty->cp] == ' ')
704 vty_forward_char(vty);
718e3744 705}
706
707/* Backward word without skipping training space. */
d62a17ae 708static void vty_backward_pure_word(struct vty *vty)
718e3744 709{
d62a17ae 710 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
711 vty_backward_char(vty);
718e3744 712}
713
714/* Backward word. */
d62a17ae 715static void vty_backward_word(struct vty *vty)
718e3744 716{
d62a17ae 717 while (vty->cp > 0 && vty->buf[vty->cp - 1] == ' ')
718 vty_backward_char(vty);
718e3744 719
d62a17ae 720 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
721 vty_backward_char(vty);
718e3744 722}
723
724/* When '^D' is typed at the beginning of the line we move to the down
725 level. */
d62a17ae 726static void vty_down_level(struct vty *vty)
718e3744 727{
d62a17ae 728 vty_out(vty, "\n");
729 cmd_exit(vty);
730 vty_prompt(vty);
731 vty->cp = 0;
718e3744 732}
733
734/* When '^Z' is received from vty, move down to the enable mode. */
d62a17ae 735static void vty_end_config(struct vty *vty)
736{
737 vty_out(vty, "\n");
738
cf09d3ca 739 if (vty->config) {
f344c66e 740 vty_config_exit(vty);
d62a17ae 741 vty->node = ENABLE_NODE;
d62a17ae 742 }
743
744 vty_prompt(vty);
745 vty->cp = 0;
718e3744 746}
747
03bad95a 748/* Delete a character at the current point. */
d62a17ae 749static void vty_delete_char(struct vty *vty)
718e3744 750{
d62a17ae 751 int i;
752 int size;
718e3744 753
d62a17ae 754 if (vty->length == 0) {
755 vty_down_level(vty);
756 return;
757 }
718e3744 758
d62a17ae 759 if (vty->cp == vty->length)
760 return; /* completion need here? */
718e3744 761
d62a17ae 762 size = vty->length - vty->cp;
718e3744 763
d62a17ae 764 vty->length--;
765 memmove(&vty->buf[vty->cp], &vty->buf[vty->cp + 1], size - 1);
766 vty->buf[vty->length] = '\0';
d0bfb22c 767
d62a17ae 768 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
769 return;
718e3744 770
d62a17ae 771 vty_write(vty, &vty->buf[vty->cp], size - 1);
772 vty_write(vty, &telnet_space_char, 1);
718e3744 773
d62a17ae 774 for (i = 0; i < size; i++)
775 vty_write(vty, &telnet_backward_char, 1);
718e3744 776}
777
778/* Delete a character before the point. */
d62a17ae 779static void vty_delete_backward_char(struct vty *vty)
718e3744 780{
d62a17ae 781 if (vty->cp == 0)
782 return;
718e3744 783
d62a17ae 784 vty_backward_char(vty);
785 vty_delete_char(vty);
718e3744 786}
787
788/* Kill rest of line from current point. */
d62a17ae 789static void vty_kill_line(struct vty *vty)
718e3744 790{
d62a17ae 791 int i;
792 int size;
718e3744 793
d62a17ae 794 size = vty->length - vty->cp;
d0bfb22c 795
d62a17ae 796 if (size == 0)
797 return;
718e3744 798
d62a17ae 799 for (i = 0; i < size; i++)
800 vty_write(vty, &telnet_space_char, 1);
801 for (i = 0; i < size; i++)
802 vty_write(vty, &telnet_backward_char, 1);
718e3744 803
d62a17ae 804 memset(&vty->buf[vty->cp], 0, size);
805 vty->length = vty->cp;
718e3744 806}
807
808/* Kill line from the beginning. */
d62a17ae 809static void vty_kill_line_from_beginning(struct vty *vty)
718e3744 810{
d62a17ae 811 vty_beginning_of_line(vty);
812 vty_kill_line(vty);
718e3744 813}
814
815/* Delete a word before the point. */
d62a17ae 816static void vty_forward_kill_word(struct vty *vty)
718e3744 817{
d62a17ae 818 while (vty->cp != vty->length && vty->buf[vty->cp] == ' ')
819 vty_delete_char(vty);
820 while (vty->cp != vty->length && vty->buf[vty->cp] != ' ')
821 vty_delete_char(vty);
718e3744 822}
823
824/* Delete a word before the point. */
d62a17ae 825static void vty_backward_kill_word(struct vty *vty)
718e3744 826{
d62a17ae 827 while (vty->cp > 0 && vty->buf[vty->cp - 1] == ' ')
828 vty_delete_backward_char(vty);
829 while (vty->cp > 0 && vty->buf[vty->cp - 1] != ' ')
830 vty_delete_backward_char(vty);
718e3744 831}
832
833/* Transpose chars before or at the point. */
d62a17ae 834static void vty_transpose_chars(struct vty *vty)
718e3744 835{
d62a17ae 836 char c1, c2;
718e3744 837
d62a17ae 838 /* If length is short or point is near by the beginning of line then
839 return. */
840 if (vty->length < 2 || vty->cp < 1)
841 return;
718e3744 842
d62a17ae 843 /* In case of point is located at the end of the line. */
844 if (vty->cp == vty->length) {
845 c1 = vty->buf[vty->cp - 1];
846 c2 = vty->buf[vty->cp - 2];
718e3744 847
d62a17ae 848 vty_backward_char(vty);
849 vty_backward_char(vty);
850 vty_self_insert_overwrite(vty, c1);
851 vty_self_insert_overwrite(vty, c2);
852 } else {
853 c1 = vty->buf[vty->cp];
854 c2 = vty->buf[vty->cp - 1];
718e3744 855
d62a17ae 856 vty_backward_char(vty);
857 vty_self_insert_overwrite(vty, c1);
858 vty_self_insert_overwrite(vty, c2);
859 }
718e3744 860}
861
862/* Do completion at vty interface. */
d62a17ae 863static void vty_complete_command(struct vty *vty)
864{
865 int i;
866 int ret;
867 char **matched = NULL;
868 vector vline;
869
870 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
871 return;
872
873 vline = cmd_make_strvec(vty->buf);
874 if (vline == NULL)
875 return;
876
877 /* In case of 'help \t'. */
fefa5e0f 878 if (isspace((unsigned char)vty->buf[vty->length - 1]))
d62a17ae 879 vector_set(vline, NULL);
880
881 matched = cmd_complete_command(vline, vty, &ret);
882
883 cmd_free_strvec(vline);
884
885 vty_out(vty, "\n");
886 switch (ret) {
887 case CMD_ERR_AMBIGUOUS:
888 vty_out(vty, "%% Ambiguous command.\n");
889 vty_prompt(vty);
890 vty_redraw_line(vty);
891 break;
892 case CMD_ERR_NO_MATCH:
893 /* vty_out (vty, "%% There is no matched command.\n"); */
894 vty_prompt(vty);
895 vty_redraw_line(vty);
896 break;
897 case CMD_COMPLETE_FULL_MATCH:
898 if (!matched[0]) {
899 /* 2016-11-28 equinox -- need to debug, SEGV here */
900 vty_out(vty, "%% CLI BUG: FULL_MATCH with NULL str\n");
901 vty_prompt(vty);
902 vty_redraw_line(vty);
903 break;
904 }
905 vty_prompt(vty);
906 vty_redraw_line(vty);
907 vty_backward_pure_word(vty);
908 vty_insert_word_overwrite(vty, matched[0]);
909 vty_self_insert(vty, ' ');
910 XFREE(MTYPE_COMPLETION, matched[0]);
911 break;
912 case CMD_COMPLETE_MATCH:
913 vty_prompt(vty);
914 vty_redraw_line(vty);
915 vty_backward_pure_word(vty);
916 vty_insert_word_overwrite(vty, matched[0]);
917 XFREE(MTYPE_COMPLETION, matched[0]);
918 break;
919 case CMD_COMPLETE_LIST_MATCH:
920 for (i = 0; matched[i] != NULL; i++) {
921 if (i != 0 && ((i % 6) == 0))
922 vty_out(vty, "\n");
923 vty_out(vty, "%-10s ", matched[i]);
924 XFREE(MTYPE_COMPLETION, matched[i]);
925 }
926 vty_out(vty, "\n");
927
928 vty_prompt(vty);
929 vty_redraw_line(vty);
930 break;
931 case CMD_ERR_NOTHING_TODO:
932 vty_prompt(vty);
933 vty_redraw_line(vty);
934 break;
935 default:
936 break;
937 }
0a22ddfb 938 XFREE(MTYPE_TMP, matched);
d62a17ae 939}
940
941static void vty_describe_fold(struct vty *vty, int cmd_width,
942 unsigned int desc_width, struct cmd_token *token)
943{
944 char *buf;
945 const char *cmd, *p;
946 int pos;
947
948 cmd = token->text;
949
950 if (desc_width <= 0) {
951 vty_out(vty, " %-*s %s\n", cmd_width, cmd, token->desc);
952 return;
953 }
954
955 buf = XCALLOC(MTYPE_TMP, strlen(token->desc) + 1);
956
957 for (p = token->desc; strlen(p) > desc_width; p += pos + 1) {
958 for (pos = desc_width; pos > 0; pos--)
959 if (*(p + pos) == ' ')
960 break;
961
962 if (pos == 0)
963 break;
964
fcb072cd 965 memcpy(buf, p, pos);
d62a17ae 966 buf[pos] = '\0';
967 vty_out(vty, " %-*s %s\n", cmd_width, cmd, buf);
968
969 cmd = "";
970 }
971
972 vty_out(vty, " %-*s %s\n", cmd_width, cmd, p);
973
974 XFREE(MTYPE_TMP, buf);
718e3744 975}
976
977/* Describe matched command function. */
d62a17ae 978static void vty_describe_command(struct vty *vty)
979{
980 int ret;
981 vector vline;
982 vector describe;
983 unsigned int i, width, desc_width;
984 struct cmd_token *token, *token_cr = NULL;
985
986 vline = cmd_make_strvec(vty->buf);
987
988 /* In case of '> ?'. */
989 if (vline == NULL) {
990 vline = vector_init(1);
991 vector_set(vline, NULL);
fefa5e0f 992 } else if (isspace((unsigned char)vty->buf[vty->length - 1]))
d62a17ae 993 vector_set(vline, NULL);
994
995 describe = cmd_describe_command(vline, vty, &ret);
996
997 vty_out(vty, "\n");
998
999 /* Ambiguous error. */
1000 switch (ret) {
1001 case CMD_ERR_AMBIGUOUS:
1002 vty_out(vty, "%% Ambiguous command.\n");
1003 goto out;
1004 break;
1005 case CMD_ERR_NO_MATCH:
1006 vty_out(vty, "%% There is no matched command.\n");
1007 goto out;
1008 break;
1009 }
1010
1011 /* Get width of command string. */
1012 width = 0;
1013 for (i = 0; i < vector_active(describe); i++)
1014 if ((token = vector_slot(describe, i)) != NULL) {
1015 unsigned int len;
1016
1017 if (token->text[0] == '\0')
1018 continue;
1019
1020 len = strlen(token->text);
1021
1022 if (width < len)
1023 width = len;
1024 }
1025
1026 /* Get width of description string. */
1027 desc_width = vty->width - (width + 6);
1028
1029 /* Print out description. */
1030 for (i = 0; i < vector_active(describe); i++)
1031 if ((token = vector_slot(describe, i)) != NULL) {
1032 if (token->text[0] == '\0')
1033 continue;
1034
1035 if (strcmp(token->text, CMD_CR_TEXT) == 0) {
1036 token_cr = token;
1037 continue;
1038 }
1039
1040 if (!token->desc)
1041 vty_out(vty, " %-s\n", token->text);
1042 else if (desc_width >= strlen(token->desc))
1043 vty_out(vty, " %-*s %s\n", width, token->text,
1044 token->desc);
1045 else
1046 vty_describe_fold(vty, width, desc_width,
1047 token);
1048
1049 if (IS_VARYING_TOKEN(token->type)) {
1050 const char *ref = vector_slot(
1051 vline, vector_active(vline) - 1);
1052
1053 vector varcomps = vector_init(VECTOR_MIN_SIZE);
1054 cmd_variable_complete(token, ref, varcomps);
1055
1056 if (vector_active(varcomps) > 0) {
1057 char *ac = cmd_variable_comp2str(
1058 varcomps, vty->width);
1059 vty_out(vty, "%s\n", ac);
1060 XFREE(MTYPE_TMP, ac);
1061 }
1062
1063 vector_free(varcomps);
1064 }
d62a17ae 1065 }
1066
1067 if ((token = token_cr)) {
1068 if (!token->desc)
1069 vty_out(vty, " %-s\n", token->text);
1070 else if (desc_width >= strlen(token->desc))
1071 vty_out(vty, " %-*s %s\n", width, token->text,
1072 token->desc);
1073 else
1074 vty_describe_fold(vty, width, desc_width, token);
1075 }
718e3744 1076
2fe8aba3 1077out:
d62a17ae 1078 cmd_free_strvec(vline);
1079 if (describe)
1080 vector_free(describe);
718e3744 1081
d62a17ae 1082 vty_prompt(vty);
1083 vty_redraw_line(vty);
718e3744 1084}
1085
d62a17ae 1086static void vty_clear_buf(struct vty *vty)
718e3744 1087{
d62a17ae 1088 memset(vty->buf, 0, vty->max);
718e3744 1089}
1090
1091/* ^C stop current input and do not add command line to the history. */
d62a17ae 1092static void vty_stop_input(struct vty *vty)
1093{
1094 vty->cp = vty->length = 0;
1095 vty_clear_buf(vty);
1096 vty_out(vty, "\n");
1097
cf09d3ca 1098 if (vty->config) {
f344c66e 1099 vty_config_exit(vty);
d62a17ae 1100 vty->node = ENABLE_NODE;
d62a17ae 1101 }
cf09d3ca 1102
d62a17ae 1103 vty_prompt(vty);
1104
1105 /* Set history pointer to the latest one. */
1106 vty->hp = vty->hindex;
718e3744 1107}
1108
1109/* Add current command line to the history buffer. */
d62a17ae 1110static void vty_hist_add(struct vty *vty)
718e3744 1111{
d62a17ae 1112 int index;
718e3744 1113
d62a17ae 1114 if (vty->length == 0)
1115 return;
718e3744 1116
d62a17ae 1117 index = vty->hindex ? vty->hindex - 1 : VTY_MAXHIST - 1;
718e3744 1118
d62a17ae 1119 /* Ignore the same string as previous one. */
1120 if (vty->hist[index])
1121 if (strcmp(vty->buf, vty->hist[index]) == 0) {
1122 vty->hp = vty->hindex;
1123 return;
1124 }
718e3744 1125
d62a17ae 1126 /* Insert history entry. */
0a22ddfb 1127 XFREE(MTYPE_VTY_HIST, vty->hist[vty->hindex]);
d62a17ae 1128 vty->hist[vty->hindex] = XSTRDUP(MTYPE_VTY_HIST, vty->buf);
718e3744 1129
d62a17ae 1130 /* History index rotation. */
1131 vty->hindex++;
1132 if (vty->hindex == VTY_MAXHIST)
1133 vty->hindex = 0;
718e3744 1134
d62a17ae 1135 vty->hp = vty->hindex;
718e3744 1136}
1137
1138/* #define TELNET_OPTION_DEBUG */
1139
1140/* Get telnet window size. */
d62a17ae 1141static int vty_telnet_option(struct vty *vty, unsigned char *buf, int nbytes)
718e3744 1142{
1143#ifdef TELNET_OPTION_DEBUG
d62a17ae 1144 int i;
1145
1146 for (i = 0; i < nbytes; i++) {
1147 switch (buf[i]) {
1148 case IAC:
1149 vty_out(vty, "IAC ");
1150 break;
1151 case WILL:
1152 vty_out(vty, "WILL ");
1153 break;
1154 case WONT:
1155 vty_out(vty, "WONT ");
1156 break;
1157 case DO:
1158 vty_out(vty, "DO ");
1159 break;
1160 case DONT:
1161 vty_out(vty, "DONT ");
1162 break;
1163 case SB:
1164 vty_out(vty, "SB ");
1165 break;
1166 case SE:
1167 vty_out(vty, "SE ");
1168 break;
1169 case TELOPT_ECHO:
1170 vty_out(vty, "TELOPT_ECHO \n");
1171 break;
1172 case TELOPT_SGA:
1173 vty_out(vty, "TELOPT_SGA \n");
1174 break;
1175 case TELOPT_NAWS:
1176 vty_out(vty, "TELOPT_NAWS \n");
1177 break;
1178 default:
1179 vty_out(vty, "%x ", buf[i]);
1180 break;
1181 }
1182 }
1183 vty_out(vty, "\n");
718e3744 1184
1185#endif /* TELNET_OPTION_DEBUG */
1186
d62a17ae 1187 switch (buf[0]) {
1188 case SB:
1189 vty->sb_len = 0;
1190 vty->iac_sb_in_progress = 1;
1191 return 0;
d62a17ae 1192 case SE: {
1193 if (!vty->iac_sb_in_progress)
1194 return 0;
1195
1196 if ((vty->sb_len == 0) || (vty->sb_buf[0] == '\0')) {
1197 vty->iac_sb_in_progress = 0;
1198 return 0;
1199 }
1200 switch (vty->sb_buf[0]) {
1201 case TELOPT_NAWS:
1202 if (vty->sb_len != TELNET_NAWS_SB_LEN)
34699016 1203 flog_err(
450971aa 1204 EC_LIB_SYSTEM_CALL,
3efd0893 1205 "RFC 1073 violation detected: telnet NAWS option should send %d characters, but we received %lu",
d62a17ae 1206 TELNET_NAWS_SB_LEN,
d7c0a89a 1207 (unsigned long)vty->sb_len);
d62a17ae 1208 else if (sizeof(vty->sb_buf) < TELNET_NAWS_SB_LEN)
af4c2728 1209 flog_err(
450971aa 1210 EC_LIB_DEVELOPMENT,
472878dc 1211 "Bug detected: sizeof(vty->sb_buf) %lu < %d, too small to handle the telnet NAWS option",
d7c0a89a 1212 (unsigned long)sizeof(vty->sb_buf),
d62a17ae 1213 TELNET_NAWS_SB_LEN);
1214 else {
1215 vty->width = ((vty->sb_buf[1] << 8)
1216 | vty->sb_buf[2]);
1217 vty->height = ((vty->sb_buf[3] << 8)
1218 | vty->sb_buf[4]);
9fc7ebf1 1219#ifdef TELNET_OPTION_DEBUG
d62a17ae 1220 vty_out(vty,
3efd0893 1221 "TELNET NAWS window size negotiation completed: width %d, height %d\n",
d62a17ae 1222 vty->width, vty->height);
9fc7ebf1 1223#endif
d62a17ae 1224 }
1225 break;
1226 }
1227 vty->iac_sb_in_progress = 0;
1228 return 0;
d62a17ae 1229 }
1230 default:
1231 break;
1232 }
1233 return 1;
718e3744 1234}
1235
1236/* Execute current command line. */
d62a17ae 1237static int vty_execute(struct vty *vty)
718e3744 1238{
d62a17ae 1239 int ret;
718e3744 1240
d62a17ae 1241 ret = CMD_SUCCESS;
718e3744 1242
d62a17ae 1243 switch (vty->node) {
1244 case AUTH_NODE:
1245 case AUTH_ENABLE_NODE:
1246 vty_auth(vty, vty->buf);
1247 break;
1248 default:
1249 ret = vty_command(vty, vty->buf);
1250 if (vty->type == VTY_TERM)
1251 vty_hist_add(vty);
1252 break;
1253 }
718e3744 1254
d62a17ae 1255 /* Clear command line buffer. */
1256 vty->cp = vty->length = 0;
1257 vty_clear_buf(vty);
718e3744 1258
d62a17ae 1259 if (vty->status != VTY_CLOSE)
1260 vty_prompt(vty);
718e3744 1261
d62a17ae 1262 return ret;
718e3744 1263}
1264
1265#define CONTROL(X) ((X) - '@')
1266#define VTY_NORMAL 0
1267#define VTY_PRE_ESCAPE 1
1268#define VTY_ESCAPE 2
e0a5979d 1269#define VTY_CR 3
718e3744 1270
1271/* Escape character command map. */
d62a17ae 1272static void vty_escape_map(unsigned char c, struct vty *vty)
1273{
1274 switch (c) {
1275 case ('A'):
1276 vty_previous_line(vty);
1277 break;
1278 case ('B'):
1279 vty_next_line(vty);
1280 break;
1281 case ('C'):
1282 vty_forward_char(vty);
1283 break;
1284 case ('D'):
1285 vty_backward_char(vty);
1286 break;
1287 default:
1288 break;
1289 }
1290
1291 /* Go back to normal mode. */
1292 vty->escape = VTY_NORMAL;
718e3744 1293}
1294
1295/* Quit print out to the buffer. */
d62a17ae 1296static void vty_buffer_reset(struct vty *vty)
718e3744 1297{
d62a17ae 1298 buffer_reset(vty->obuf);
0b42d81a 1299 buffer_reset(vty->lbuf);
d62a17ae 1300 vty_prompt(vty);
1301 vty_redraw_line(vty);
718e3744 1302}
1303
1304/* Read data via vty socket. */
cc9f21da 1305static void vty_read(struct thread *thread)
d62a17ae 1306{
1307 int i;
1308 int nbytes;
1309 unsigned char buf[VTY_READ_BUFSIZ];
1310
d62a17ae 1311 struct vty *vty = THREAD_ARG(thread);
d62a17ae 1312
1313 /* Read raw data from socket */
1314 if ((nbytes = read(vty->fd, buf, VTY_READ_BUFSIZ)) <= 0) {
1315 if (nbytes < 0) {
1316 if (ERRNO_IO_RETRY(errno)) {
ab699721 1317 vty_event(VTY_READ, vty);
cc9f21da 1318 return;
d62a17ae 1319 }
34699016 1320 flog_err(
450971aa 1321 EC_LIB_SOCKET,
d62a17ae 1322 "%s: read error on vty client fd %d, closing: %s",
1323 __func__, vty->fd, safe_strerror(errno));
1324 buffer_reset(vty->obuf);
0b42d81a 1325 buffer_reset(vty->lbuf);
d62a17ae 1326 }
1327 vty->status = VTY_CLOSE;
1328 }
1329
1330 for (i = 0; i < nbytes; i++) {
1331 if (buf[i] == IAC) {
1332 if (!vty->iac) {
1333 vty->iac = 1;
1334 continue;
1335 } else {
1336 vty->iac = 0;
1337 }
1338 }
1339
1340 if (vty->iac_sb_in_progress && !vty->iac) {
1341 if (vty->sb_len < sizeof(vty->sb_buf))
1342 vty->sb_buf[vty->sb_len] = buf[i];
1343 vty->sb_len++;
1344 continue;
1345 }
1346
1347 if (vty->iac) {
1348 /* In case of telnet command */
1349 int ret = 0;
1350 ret = vty_telnet_option(vty, buf + i, nbytes - i);
1351 vty->iac = 0;
1352 i += ret;
1353 continue;
1354 }
1355
1356
1357 if (vty->status == VTY_MORE) {
1358 switch (buf[i]) {
1359 case CONTROL('C'):
1360 case 'q':
1361 case 'Q':
1362 vty_buffer_reset(vty);
1363 break;
d62a17ae 1364 default:
1365 break;
1366 }
1367 continue;
1368 }
1369
1370 /* Escape character. */
1371 if (vty->escape == VTY_ESCAPE) {
1372 vty_escape_map(buf[i], vty);
1373 continue;
1374 }
1375
1376 /* Pre-escape status. */
1377 if (vty->escape == VTY_PRE_ESCAPE) {
1378 switch (buf[i]) {
1379 case '[':
1380 vty->escape = VTY_ESCAPE;
1381 break;
1382 case 'b':
1383 vty_backward_word(vty);
1384 vty->escape = VTY_NORMAL;
1385 break;
1386 case 'f':
1387 vty_forward_word(vty);
1388 vty->escape = VTY_NORMAL;
1389 break;
1390 case 'd':
1391 vty_forward_kill_word(vty);
1392 vty->escape = VTY_NORMAL;
1393 break;
1394 case CONTROL('H'):
1395 case 0x7f:
1396 vty_backward_kill_word(vty);
1397 vty->escape = VTY_NORMAL;
1398 break;
1399 default:
1400 vty->escape = VTY_NORMAL;
1401 break;
1402 }
1403 continue;
1404 }
1405
e0a5979d
DL
1406 if (vty->escape == VTY_CR) {
1407 /* if we get CR+NL, the NL results in an extra empty
1408 * prompt line being printed without this; just drop
1409 * the NL if it immediately follows CR.
1410 */
1411 vty->escape = VTY_NORMAL;
1412
1413 if (buf[i] == '\n')
1414 continue;
1415 }
1416
d62a17ae 1417 switch (buf[i]) {
1418 case CONTROL('A'):
1419 vty_beginning_of_line(vty);
1420 break;
1421 case CONTROL('B'):
1422 vty_backward_char(vty);
1423 break;
1424 case CONTROL('C'):
1425 vty_stop_input(vty);
1426 break;
1427 case CONTROL('D'):
1428 vty_delete_char(vty);
1429 break;
1430 case CONTROL('E'):
1431 vty_end_of_line(vty);
1432 break;
1433 case CONTROL('F'):
1434 vty_forward_char(vty);
1435 break;
1436 case CONTROL('H'):
1437 case 0x7f:
1438 vty_delete_backward_char(vty);
1439 break;
1440 case CONTROL('K'):
1441 vty_kill_line(vty);
1442 break;
1443 case CONTROL('N'):
1444 vty_next_line(vty);
1445 break;
1446 case CONTROL('P'):
1447 vty_previous_line(vty);
1448 break;
1449 case CONTROL('T'):
1450 vty_transpose_chars(vty);
1451 break;
1452 case CONTROL('U'):
1453 vty_kill_line_from_beginning(vty);
1454 break;
1455 case CONTROL('W'):
1456 vty_backward_kill_word(vty);
1457 break;
1458 case CONTROL('Z'):
1459 vty_end_config(vty);
1460 break;
d62a17ae 1461 case '\r':
e0a5979d
DL
1462 vty->escape = VTY_CR;
1463 /* fallthru */
1464 case '\n':
d62a17ae 1465 vty_out(vty, "\n");
ab699721 1466 buffer_flush_available(vty->obuf, vty->wfd);
d62a17ae 1467 vty_execute(vty);
b2dde56b
DL
1468
1469 if (vty->pass_fd != -1) {
1470 close(vty->pass_fd);
1471 vty->pass_fd = -1;
1472 }
d62a17ae 1473 break;
1474 case '\t':
1475 vty_complete_command(vty);
1476 break;
1477 case '?':
1478 if (vty->node == AUTH_NODE
1479 || vty->node == AUTH_ENABLE_NODE)
1480 vty_self_insert(vty, buf[i]);
1481 else
1482 vty_describe_command(vty);
1483 break;
1484 case '\033':
1485 if (i + 1 < nbytes && buf[i + 1] == '[') {
1486 vty->escape = VTY_ESCAPE;
1487 i++;
1488 } else
1489 vty->escape = VTY_PRE_ESCAPE;
1490 break;
1491 default:
1492 if (buf[i] > 31 && buf[i] < 127)
1493 vty_self_insert(vty, buf[i]);
1494 break;
1495 }
1496 }
1497
1498 /* Check status. */
1499 if (vty->status == VTY_CLOSE)
1500 vty_close(vty);
1501 else {
ab699721
DL
1502 vty_event(VTY_WRITE, vty);
1503 vty_event(VTY_READ, vty);
d62a17ae 1504 }
718e3744 1505}
1506
1507/* Flush buffer to the vty. */
cc9f21da 1508static void vty_flush(struct thread *thread)
d62a17ae 1509{
1510 int erase;
1511 buffer_status_t flushrc;
d62a17ae 1512 struct vty *vty = THREAD_ARG(thread);
1513
d62a17ae 1514 /* Tempolary disable read thread. */
43b8ca99
DS
1515 if (vty->lines == 0)
1516 THREAD_OFF(vty->t_read);
d62a17ae 1517
1518 /* Function execution continue. */
1519 erase = ((vty->status == VTY_MORE || vty->status == VTY_MORELINE));
1520
1521 /* N.B. if width is 0, that means we don't know the window size. */
1522 if ((vty->lines == 0) || (vty->width == 0) || (vty->height == 0))
ab699721 1523 flushrc = buffer_flush_available(vty->obuf, vty->wfd);
d62a17ae 1524 else if (vty->status == VTY_MORELINE)
ab699721 1525 flushrc = buffer_flush_window(vty->obuf, vty->wfd, vty->width,
d62a17ae 1526 1, erase, 0);
1527 else
1528 flushrc = buffer_flush_window(
ab699721 1529 vty->obuf, vty->wfd, vty->width,
d62a17ae 1530 vty->lines >= 0 ? vty->lines : vty->height, erase, 0);
1531 switch (flushrc) {
1532 case BUFFER_ERROR:
ab699721
DL
1533 zlog_info("buffer_flush failed on vty client fd %d/%d, closing",
1534 vty->fd, vty->wfd);
0b42d81a 1535 buffer_reset(vty->lbuf);
d62a17ae 1536 buffer_reset(vty->obuf);
1537 vty_close(vty);
cc9f21da 1538 return;
d62a17ae 1539 case BUFFER_EMPTY:
1540 if (vty->status == VTY_CLOSE)
1541 vty_close(vty);
1542 else {
1543 vty->status = VTY_NORMAL;
1544 if (vty->lines == 0)
ab699721 1545 vty_event(VTY_READ, vty);
d62a17ae 1546 }
1547 break;
1548 case BUFFER_PENDING:
1549 /* There is more data waiting to be written. */
1550 vty->status = VTY_MORE;
1551 if (vty->lines == 0)
ab699721 1552 vty_event(VTY_WRITE, vty);
d62a17ae 1553 break;
1554 }
718e3744 1555}
1556
2cddf2ff 1557/* Allocate new vty struct. */
4d762f26 1558struct vty *vty_new(void)
2cddf2ff
QY
1559{
1560 struct vty *new = XCALLOC(MTYPE_VTY, sizeof(struct vty));
1561
1562 new->fd = new->wfd = -1;
6ef6e4f0 1563 new->of = stdout;
0b42d81a 1564 new->lbuf = buffer_new(0);
2cddf2ff
QY
1565 new->obuf = buffer_new(0); /* Use default buffer size. */
1566 new->buf = XCALLOC(MTYPE_VTY, VTY_BUFSIZ);
2cddf2ff 1567 new->max = VTY_BUFSIZ;
b2dde56b 1568 new->pass_fd = -1;
2cddf2ff
QY
1569
1570 return new;
1571}
1572
1573
b7642925 1574/* allocate and initialise vty */
d62a17ae 1575static struct vty *vty_new_init(int vty_sock)
1576{
1577 struct vty *vty;
1578
1579 vty = vty_new();
1580 vty->fd = vty_sock;
1581 vty->wfd = vty_sock;
1582 vty->type = VTY_TERM;
1583 vty->node = AUTH_NODE;
1584 vty->fail = 0;
1585 vty->cp = 0;
1586 vty_clear_buf(vty);
1587 vty->length = 0;
1588 memset(vty->hist, 0, sizeof(vty->hist));
1589 vty->hp = 0;
1590 vty->hindex = 0;
1c2facd1
RW
1591 vty->xpath_index = 0;
1592 memset(vty->xpath, 0, sizeof(vty->xpath));
1593 vty->private_config = false;
1594 vty->candidate_config = vty_shared_candidate_config;
d62a17ae 1595 vty->status = VTY_NORMAL;
1596 vty->lines = -1;
1597 vty->iac = 0;
1598 vty->iac_sb_in_progress = 0;
1599 vty->sb_len = 0;
1600
43dd8caf
DL
1601 vtys_add_tail(vty_sessions, vty);
1602
d62a17ae 1603 return vty;
b7642925
DL
1604}
1605
718e3744 1606/* Create new vty structure. */
d62a17ae 1607static struct vty *vty_create(int vty_sock, union sockunion *su)
1608{
1609 char buf[SU_ADDRSTRLEN];
1610 struct vty *vty;
1611
1612 sockunion2str(su, buf, SU_ADDRSTRLEN);
1613
1614 /* Allocate new vty structure and set up default values. */
1615 vty = vty_new_init(vty_sock);
1616
1617 /* configurable parameters not part of basic init */
1618 vty->v_timeout = vty_timeout_val;
9f73d2c9 1619 strlcpy(vty->address, buf, sizeof(vty->address));
d62a17ae 1620 if (no_password_check) {
1621 if (host.advanced)
1622 vty->node = ENABLE_NODE;
1623 else
1624 vty->node = VIEW_NODE;
1625 }
1626 if (host.lines >= 0)
1627 vty->lines = host.lines;
1628
1629 if (!no_password_check) {
1630 /* Vty is not available if password isn't set. */
1631 if (host.password == NULL && host.password_encrypt == NULL) {
1632 vty_out(vty, "Vty password is not set.\n");
1633 vty->status = VTY_CLOSE;
1634 vty_close(vty);
1635 return NULL;
1636 }
1637 }
1638
1639 /* Say hello to the world. */
1640 vty_hello(vty);
1641 if (!no_password_check)
1642 vty_out(vty, "\nUser Access Verification\n\n");
1643
1644 /* Setting up terminal. */
1645 vty_will_echo(vty);
1646 vty_will_suppress_go_ahead(vty);
1647
1648 vty_dont_linemode(vty);
1649 vty_do_window_size(vty);
1650 /* vty_dont_lflow_ahead (vty); */
1651
1652 vty_prompt(vty);
1653
1654 /* Add read/write thread. */
ab699721
DL
1655 vty_event(VTY_WRITE, vty);
1656 vty_event(VTY_READ, vty);
d62a17ae 1657
1658 return vty;
718e3744 1659}
1660
b7642925 1661/* create vty for stdio */
b510a06e
DL
1662static struct termios stdio_orig_termios;
1663static struct vty *stdio_vty = NULL;
154b9e8f
DL
1664static bool stdio_termios = false;
1665static void (*stdio_vty_atclose)(int isexit);
b510a06e 1666
154b9e8f 1667static void vty_stdio_reset(int isexit)
b510a06e 1668{
d62a17ae 1669 if (stdio_vty) {
154b9e8f
DL
1670 if (stdio_termios)
1671 tcsetattr(0, TCSANOW, &stdio_orig_termios);
1672 stdio_termios = false;
1673
d62a17ae 1674 stdio_vty = NULL;
dbf78092 1675
d62a17ae 1676 if (stdio_vty_atclose)
154b9e8f 1677 stdio_vty_atclose(isexit);
d62a17ae 1678 stdio_vty_atclose = NULL;
1679 }
b510a06e
DL
1680}
1681
154b9e8f
DL
1682static void vty_stdio_atexit(void)
1683{
1684 vty_stdio_reset(1);
1685}
1686
1687void vty_stdio_suspend(void)
1688{
1689 if (!stdio_vty)
1690 return;
1691
43b8ca99
DS
1692 THREAD_OFF(stdio_vty->t_write);
1693 THREAD_OFF(stdio_vty->t_read);
1694 THREAD_OFF(stdio_vty->t_timeout);
154b9e8f
DL
1695
1696 if (stdio_termios)
1697 tcsetattr(0, TCSANOW, &stdio_orig_termios);
1698 stdio_termios = false;
1699}
1700
1701void vty_stdio_resume(void)
1702{
1703 if (!stdio_vty)
1704 return;
1705
1706 if (!tcgetattr(0, &stdio_orig_termios)) {
1707 struct termios termios;
1708
1709 termios = stdio_orig_termios;
1710 termios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR
1711 | IGNCR | ICRNL | IXON);
154b9e8f
DL
1712 termios.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN);
1713 termios.c_cflag &= ~(CSIZE | PARENB);
1714 termios.c_cflag |= CS8;
1715 tcsetattr(0, TCSANOW, &termios);
1716 stdio_termios = true;
1717 }
1718
1719 vty_prompt(stdio_vty);
1720
1721 /* Add read/write thread. */
ab699721
DL
1722 vty_event(VTY_WRITE, stdio_vty);
1723 vty_event(VTY_READ, stdio_vty);
154b9e8f
DL
1724}
1725
1726void vty_stdio_close(void)
1727{
1728 if (!stdio_vty)
1729 return;
1730 vty_close(stdio_vty);
1731}
1732
1733struct vty *vty_stdio(void (*atclose)(int isexit))
b7642925 1734{
d62a17ae 1735 struct vty *vty;
b7642925 1736
d62a17ae 1737 /* refuse creating two vtys on stdio */
1738 if (stdio_vty)
1739 return NULL;
b510a06e 1740
d62a17ae 1741 vty = stdio_vty = vty_new_init(0);
1742 stdio_vty_atclose = atclose;
1743 vty->wfd = 1;
b7642925 1744
d62a17ae 1745 /* always have stdio vty in a known _unchangeable_ state, don't want
1746 * config
1747 * to have any effect here to make sure scripting this works as intended
1748 */
1749 vty->node = ENABLE_NODE;
1750 vty->v_timeout = 0;
9f73d2c9 1751 strlcpy(vty->address, "console", sizeof(vty->address));
b7642925 1752
154b9e8f 1753 vty_stdio_resume();
d62a17ae 1754 return vty;
b7642925
DL
1755}
1756
718e3744 1757/* Accept connection from the network. */
cc9f21da 1758static void vty_accept(struct thread *thread)
d62a17ae 1759{
26ae7cc2 1760 struct vty_serv *vtyserv = THREAD_ARG(thread);
d62a17ae 1761 int vty_sock;
1762 union sockunion su;
1763 int ret;
1764 unsigned int on;
26ae7cc2 1765 int accept_sock = vtyserv->sock;
d62a17ae 1766 struct prefix p;
1767 struct access_list *acl = NULL;
d62a17ae 1768
d62a17ae 1769 /* We continue hearing vty socket. */
26ae7cc2 1770 vty_event_serv(VTY_SERV, vtyserv);
d62a17ae 1771
1772 memset(&su, 0, sizeof(union sockunion));
1773
1774 /* We can handle IPv4 or IPv6 socket. */
1775 vty_sock = sockunion_accept(accept_sock, &su);
1776 if (vty_sock < 0) {
450971aa 1777 flog_err(EC_LIB_SOCKET, "can't accept vty socket : %s",
34699016 1778 safe_strerror(errno));
cc9f21da 1779 return;
d62a17ae 1780 }
1781 set_nonblocking(vty_sock);
1782 set_cloexec(vty_sock);
1783
0154d8ce 1784 if (!sockunion2hostprefix(&su, &p)) {
07d4bb8b 1785 close(vty_sock);
a0ee6f32
DS
1786 zlog_info("Vty unable to convert prefix from sockunion %pSU",
1787 &su);
cc9f21da 1788 return;
0154d8ce 1789 }
d62a17ae 1790
1791 /* VTY's accesslist apply. */
1792 if (p.family == AF_INET && vty_accesslist_name) {
1793 if ((acl = access_list_lookup(AFI_IP, vty_accesslist_name))
1794 && (access_list_apply(acl, &p) == FILTER_DENY)) {
a0ee6f32 1795 zlog_info("Vty connection refused from %pSU", &su);
d62a17ae 1796 close(vty_sock);
cc9f21da 1797 return;
d62a17ae 1798 }
1799 }
1800
1801 /* VTY's ipv6 accesslist apply. */
1802 if (p.family == AF_INET6 && vty_ipv6_accesslist_name) {
1803 if ((acl = access_list_lookup(AFI_IP6,
1804 vty_ipv6_accesslist_name))
1805 && (access_list_apply(acl, &p) == FILTER_DENY)) {
a0ee6f32 1806 zlog_info("Vty connection refused from %pSU", &su);
d62a17ae 1807 close(vty_sock);
cc9f21da 1808 return;
d62a17ae 1809 }
1810 }
1811
1812 on = 1;
1813 ret = setsockopt(vty_sock, IPPROTO_TCP, TCP_NODELAY, (char *)&on,
1814 sizeof(on));
1815 if (ret < 0)
1816 zlog_info("can't set sockopt to vty_sock : %s",
1817 safe_strerror(errno));
1818
a0ee6f32 1819 zlog_info("Vty connection from %pSU", &su);
d62a17ae 1820
1821 vty_create(vty_sock, &su);
d62a17ae 1822}
1823
1824static void vty_serv_sock_addrinfo(const char *hostname, unsigned short port)
1825{
1826 int ret;
1827 struct addrinfo req;
1828 struct addrinfo *ainfo;
1829 struct addrinfo *ainfo_save;
1830 int sock;
1831 char port_str[BUFSIZ];
1832
6006b807 1833 memset(&req, 0, sizeof(req));
d62a17ae 1834 req.ai_flags = AI_PASSIVE;
1835 req.ai_family = AF_UNSPEC;
1836 req.ai_socktype = SOCK_STREAM;
772270f3 1837 snprintf(port_str, sizeof(port_str), "%d", port);
d62a17ae 1838 port_str[sizeof(port_str) - 1] = '\0';
1839
1840 ret = getaddrinfo(hostname, port_str, &req, &ainfo);
1841
1842 if (ret != 0) {
450971aa 1843 flog_err_sys(EC_LIB_SYSTEM_CALL, "getaddrinfo failed: %s",
09c866e3 1844 gai_strerror(ret));
d62a17ae 1845 exit(1);
1846 }
1847
1848 ainfo_save = ainfo;
1849
1850 do {
26ae7cc2
DL
1851 struct vty_serv *vtyserv;
1852
d62a17ae 1853 if (ainfo->ai_family != AF_INET && ainfo->ai_family != AF_INET6)
1854 continue;
1855
1856 sock = socket(ainfo->ai_family, ainfo->ai_socktype,
1857 ainfo->ai_protocol);
1858 if (sock < 0)
1859 continue;
718e3744 1860
d62a17ae 1861 sockopt_v6only(ainfo->ai_family, sock);
1862 sockopt_reuseaddr(sock);
1863 sockopt_reuseport(sock);
1864 set_cloexec(sock);
1865
1866 ret = bind(sock, ainfo->ai_addr, ainfo->ai_addrlen);
1867 if (ret < 0) {
1868 close(sock); /* Avoid sd leak. */
1869 continue;
1870 }
1871
1872 ret = listen(sock, 3);
1873 if (ret < 0) {
1874 close(sock); /* Avoid sd leak. */
1875 continue;
1876 }
1877
26ae7cc2
DL
1878 vtyserv = XCALLOC(MTYPE_VTY_SERV, sizeof(*vtyserv));
1879 vtyserv->sock = sock;
1880 vtyservs_add_tail(vty_servs, vtyserv);
1881
1882 vty_event_serv(VTY_SERV, vtyserv);
d62a17ae 1883 } while ((ainfo = ainfo->ai_next) != NULL);
1884
1885 freeaddrinfo(ainfo_save);
718e3744 1886}
718e3744 1887
1888#ifdef VTYSH
1889/* For sockaddr_un. */
1890#include <sys/un.h>
1891
1892/* VTY shell UNIX domain socket. */
d62a17ae 1893static void vty_serv_un(const char *path)
1894{
26ae7cc2 1895 struct vty_serv *vtyserv;
d62a17ae 1896 int ret;
1897 int sock, len;
1898 struct sockaddr_un serv;
1899 mode_t old_mask;
1900 struct zprivs_ids_t ids;
1901
1902 /* First of all, unlink existing socket */
1903 unlink(path);
1904
1905 /* Set umask */
1906 old_mask = umask(0007);
1907
1908 /* Make UNIX domain socket. */
1909 sock = socket(AF_UNIX, SOCK_STREAM, 0);
1910 if (sock < 0) {
450971aa 1911 flog_err_sys(EC_LIB_SOCKET,
09c866e3
QY
1912 "Cannot create unix stream socket: %s",
1913 safe_strerror(errno));
d62a17ae 1914 return;
1915 }
1916
1917 /* Make server socket. */
6006b807 1918 memset(&serv, 0, sizeof(serv));
d62a17ae 1919 serv.sun_family = AF_UNIX;
1920 strlcpy(serv.sun_path, path, sizeof(serv.sun_path));
6f0e3f6e 1921#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
d62a17ae 1922 len = serv.sun_len = SUN_LEN(&serv);
718e3744 1923#else
d62a17ae 1924 len = sizeof(serv.sun_family) + strlen(serv.sun_path);
6f0e3f6e 1925#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
718e3744 1926
d62a17ae 1927 set_cloexec(sock);
2da59394 1928
d62a17ae 1929 ret = bind(sock, (struct sockaddr *)&serv, len);
1930 if (ret < 0) {
450971aa 1931 flog_err_sys(EC_LIB_SOCKET, "Cannot bind path %s: %s", path,
09c866e3 1932 safe_strerror(errno));
d62a17ae 1933 close(sock); /* Avoid sd leak. */
1934 return;
1935 }
718e3744 1936
d62a17ae 1937 ret = listen(sock, 5);
1938 if (ret < 0) {
450971aa 1939 flog_err_sys(EC_LIB_SOCKET, "listen(fd %d) failed: %s", sock,
09c866e3 1940 safe_strerror(errno));
d62a17ae 1941 close(sock); /* Avoid sd leak. */
1942 return;
1943 }
718e3744 1944
d62a17ae 1945 umask(old_mask);
718e3744 1946
d62a17ae 1947 zprivs_get_ids(&ids);
d0bfb22c 1948
d62a17ae 1949 /* Hack: ids.gid_vty is actually a uint, but we stored -1 in it
1950 earlier for the case when we don't need to chown the file
1951 type casting it here to make a compare */
1952 if ((int)ids.gid_vty > 0) {
1953 /* set group of socket */
1954 if (chown(path, -1, ids.gid_vty)) {
450971aa 1955 flog_err_sys(EC_LIB_SYSTEM_CALL,
09c866e3
QY
1956 "vty_serv_un: could chown socket, %s",
1957 safe_strerror(errno));
d62a17ae 1958 }
1959 }
edd7c245 1960
26ae7cc2
DL
1961 vtyserv = XCALLOC(MTYPE_VTY_SERV, sizeof(*vtyserv));
1962 vtyserv->sock = sock;
1963 vtyserv->vtysh = true;
1964 vtyservs_add_tail(vty_servs, vtyserv);
1965
1966 vty_event_serv(VTYSH_SERV, vtyserv);
718e3744 1967}
1968
1969/* #define VTYSH_DEBUG 1 */
1970
cc9f21da 1971static void vtysh_accept(struct thread *thread)
718e3744 1972{
26ae7cc2
DL
1973 struct vty_serv *vtyserv = THREAD_ARG(thread);
1974 int accept_sock = vtyserv->sock;
d62a17ae 1975 int sock;
1976 int client_len;
1977 struct sockaddr_un client;
1978 struct vty *vty;
d0bfb22c 1979
26ae7cc2 1980 vty_event_serv(VTYSH_SERV, vtyserv);
718e3744 1981
6006b807 1982 memset(&client, 0, sizeof(client));
d62a17ae 1983 client_len = sizeof(struct sockaddr_un);
718e3744 1984
d62a17ae 1985 sock = accept(accept_sock, (struct sockaddr *)&client,
1986 (socklen_t *)&client_len);
718e3744 1987
d62a17ae 1988 if (sock < 0) {
450971aa 1989 flog_err(EC_LIB_SOCKET, "can't accept vty socket : %s",
34699016 1990 safe_strerror(errno));
cc9f21da 1991 return;
d62a17ae 1992 }
718e3744 1993
d62a17ae 1994 if (set_nonblocking(sock) < 0) {
34699016 1995 flog_err(
450971aa 1996 EC_LIB_SOCKET,
34699016 1997 "vtysh_accept: could not set vty socket %d to non-blocking, %s, closing",
d62a17ae 1998 sock, safe_strerror(errno));
1999 close(sock);
cc9f21da 2000 return;
d62a17ae 2001 }
2002 set_cloexec(sock);
2da59394 2003
718e3744 2004#ifdef VTYSH_DEBUG
d62a17ae 2005 printf("VTY shell accept\n");
718e3744 2006#endif /* VTYSH_DEBUG */
2007
d62a17ae 2008 vty = vty_new();
2009 vty->fd = sock;
2010 vty->wfd = sock;
2011 vty->type = VTY_SHELL_SERV;
2012 vty->node = VIEW_NODE;
43dd8caf 2013 vtys_add_tail(vtysh_sessions, vty);
d62a17ae 2014
ab699721 2015 vty_event(VTYSH_READ, vty);
d62a17ae 2016}
2017
b2dde56b
DL
2018static int vtysh_do_pass_fd(struct vty *vty)
2019{
2020 struct iovec iov[1] = {
2021 {
2022 .iov_base = vty->pass_fd_status,
2023 .iov_len = sizeof(vty->pass_fd_status),
2024 },
2025 };
2026 union {
2027 uint8_t buf[CMSG_SPACE(sizeof(int))];
2028 struct cmsghdr align;
2029 } u;
2030 struct msghdr mh = {
2031 .msg_iov = iov,
2032 .msg_iovlen = array_size(iov),
2033 .msg_control = u.buf,
2034 .msg_controllen = sizeof(u.buf),
2035 };
2036 struct cmsghdr *cmh = CMSG_FIRSTHDR(&mh);
2037 ssize_t ret;
2038
b4c94f8c 2039 memset(&u.buf, 0, sizeof(u.buf));
b2dde56b
DL
2040 cmh->cmsg_level = SOL_SOCKET;
2041 cmh->cmsg_type = SCM_RIGHTS;
2042 cmh->cmsg_len = CMSG_LEN(sizeof(int));
2043 memcpy(CMSG_DATA(cmh), &vty->pass_fd, sizeof(int));
2044
2045 ret = sendmsg(vty->wfd, &mh, 0);
2046 if (ret < 0 && ERRNO_IO_RETRY(errno))
2047 return BUFFER_PENDING;
2048
2049 close(vty->pass_fd);
2050 vty->pass_fd = -1;
2051 vty->status = VTY_NORMAL;
2052
2053 if (ret <= 0)
2054 return BUFFER_ERROR;
2055
2056 /* resume accepting commands (suspended in vtysh_read) */
2057 vty_event(VTYSH_READ, vty);
2058
2059 if ((size_t)ret < sizeof(vty->pass_fd_status)) {
2060 size_t remains = sizeof(vty->pass_fd_status) - ret;
2061
2062 buffer_put(vty->obuf, vty->pass_fd_status + ret, remains);
2063 return BUFFER_PENDING;
2064 }
2065 return BUFFER_EMPTY;
2066}
2067
d62a17ae 2068static int vtysh_flush(struct vty *vty)
2069{
b2dde56b
DL
2070 int ret;
2071
2072 ret = buffer_flush_available(vty->obuf, vty->wfd);
2073 if (ret == BUFFER_EMPTY && vty->status == VTY_PASSFD)
2074 ret = vtysh_do_pass_fd(vty);
2075
2076 switch (ret) {
d62a17ae 2077 case BUFFER_PENDING:
ab699721 2078 vty_event(VTYSH_WRITE, vty);
d62a17ae 2079 break;
2080 case BUFFER_ERROR:
450971aa 2081 flog_err(EC_LIB_SOCKET, "%s: write error to fd %d, closing",
34699016 2082 __func__, vty->fd);
0b42d81a 2083 buffer_reset(vty->lbuf);
d62a17ae 2084 buffer_reset(vty->obuf);
2085 vty_close(vty);
2086 return -1;
d62a17ae 2087 case BUFFER_EMPTY:
2088 break;
2089 }
2090 return 0;
2091}
2092
b2dde56b
DL
2093void vty_pass_fd(struct vty *vty, int fd)
2094{
2095 if (vty->pass_fd != -1)
2096 close(vty->pass_fd);
2097
2098 vty->pass_fd = fd;
2099}
2100
cc9f21da 2101static void vtysh_read(struct thread *thread)
d62a17ae 2102{
2103 int ret;
2104 int sock;
2105 int nbytes;
2106 struct vty *vty;
2107 unsigned char buf[VTY_READ_BUFSIZ];
2108 unsigned char *p;
d7c0a89a 2109 uint8_t header[4] = {0, 0, 0, 0};
d62a17ae 2110
2111 sock = THREAD_FD(thread);
2112 vty = THREAD_ARG(thread);
d62a17ae 2113
2114 if ((nbytes = read(sock, buf, VTY_READ_BUFSIZ)) <= 0) {
2115 if (nbytes < 0) {
2116 if (ERRNO_IO_RETRY(errno)) {
ab699721 2117 vty_event(VTYSH_READ, vty);
cc9f21da 2118 return;
d62a17ae 2119 }
34699016 2120 flog_err(
450971aa 2121 EC_LIB_SOCKET,
d62a17ae 2122 "%s: read failed on vtysh client fd %d, closing: %s",
2123 __func__, sock, safe_strerror(errno));
2124 }
0b42d81a 2125 buffer_reset(vty->lbuf);
d62a17ae 2126 buffer_reset(vty->obuf);
2127 vty_close(vty);
718e3744 2128#ifdef VTYSH_DEBUG
d62a17ae 2129 printf("close vtysh\n");
718e3744 2130#endif /* VTYSH_DEBUG */
cc9f21da 2131 return;
d62a17ae 2132 }
718e3744 2133
2134#ifdef VTYSH_DEBUG
d62a17ae 2135 printf("line: %.*s\n", nbytes, buf);
718e3744 2136#endif /* VTYSH_DEBUG */
2137
d62a17ae 2138 if (vty->length + nbytes >= VTY_BUFSIZ) {
2139 /* Clear command line buffer. */
2140 vty->cp = vty->length = 0;
2141 vty_clear_buf(vty);
2142 vty_out(vty, "%% Command is too long.\n");
2143 } else {
2144 for (p = buf; p < buf + nbytes; p++) {
2145 vty->buf[vty->length++] = *p;
2146 if (*p == '\0') {
2147 /* Pass this line to parser. */
2148 ret = vty_execute(vty);
2149/* Note that vty_execute clears the command buffer and resets
2150 vty->length to 0. */
2151
2152/* Return result. */
718e3744 2153#ifdef VTYSH_DEBUG
d62a17ae 2154 printf("result: %d\n", ret);
2155 printf("vtysh node: %d\n", vty->node);
718e3744 2156#endif /* VTYSH_DEBUG */
2157
b2dde56b
DL
2158 if (vty->pass_fd != -1) {
2159 memset(vty->pass_fd_status, 0, 4);
2160 vty->pass_fd_status[3] = ret;
2161 vty->status = VTY_PASSFD;
2162
2163 if (!vty->t_write)
2164 vty_event(VTYSH_WRITE, vty);
2165
2166 /* this introduces a "sequence point"
2167 * command output is written normally,
2168 * read processing is suspended until
2169 * buffer is empty
2170 * then retcode + FD is written
2171 * then normal processing resumes
2172 *
2173 * => skip vty_event(VTYSH_READ, vty)!
2174 */
2175 return;
2176 }
2177
d62a17ae 2178 /* hack for asynchronous "write integrated"
2179 * - other commands in "buf" will be ditched
2180 * - input during pending config-write is
2181 * "unsupported" */
2182 if (ret == CMD_SUSPEND)
2183 break;
95c4aff2 2184
d62a17ae 2185 /* warning: watchfrr hardcodes this result write
2186 */
2187 header[3] = ret;
2188 buffer_put(vty->obuf, header, 4);
9fc7ebf1 2189
d62a17ae 2190 if (!vty->t_write && (vtysh_flush(vty) < 0))
2191 /* Try to flush results; exit if a write
2192 * error occurs. */
cc9f21da 2193 return;
d62a17ae 2194 }
2195 }
2196 }
718e3744 2197
d62a17ae 2198 if (vty->status == VTY_CLOSE)
2199 vty_close(vty);
2200 else
ab699721 2201 vty_event(VTYSH_READ, vty);
718e3744 2202}
49ff6d9d 2203
cc9f21da 2204static void vtysh_write(struct thread *thread)
49ff6d9d 2205{
d62a17ae 2206 struct vty *vty = THREAD_ARG(thread);
49ff6d9d 2207
d62a17ae 2208 vtysh_flush(vty);
49ff6d9d 2209}
2210
718e3744 2211#endif /* VTYSH */
2212
2213/* Determine address family to bind. */
d62a17ae 2214void vty_serv_sock(const char *addr, unsigned short port, const char *path)
718e3744 2215{
d62a17ae 2216 /* If port is set to 0, do not listen on TCP/IP at all! */
2217 if (port)
2218 vty_serv_sock_addrinfo(addr, port);
718e3744 2219
2220#ifdef VTYSH
d62a17ae 2221 vty_serv_un(path);
718e3744 2222#endif /* VTYSH */
2223}
2224
7ab57d19
DS
2225static void vty_error_delete(void *arg)
2226{
2227 struct vty_error *ve = arg;
2228
2229 XFREE(MTYPE_TMP, ve);
2230}
2231
9d0a3260
AS
2232/* Close vty interface. Warning: call this only from functions that
2233 will be careful not to access the vty afterwards (since it has
2234 now been freed). This is safest from top-level functions (called
2235 directly by the thread dispatcher). */
d62a17ae 2236void vty_close(struct vty *vty)
718e3744 2237{
d62a17ae 2238 int i;
2239 bool was_stdio = false;
718e3744 2240
791ded4a
DL
2241 /* Drop out of configure / transaction if needed. */
2242 vty_config_exit(vty);
2243
d62a17ae 2244 /* Cancel threads.*/
43b8ca99
DS
2245 THREAD_OFF(vty->t_read);
2246 THREAD_OFF(vty->t_write);
2247 THREAD_OFF(vty->t_timeout);
718e3744 2248
b2dde56b
DL
2249 if (vty->pass_fd != -1) {
2250 close(vty->pass_fd);
2251 vty->pass_fd = -1;
2252 }
0798d276 2253 zlog_live_close(&vty->live_log);
b2dde56b 2254
d62a17ae 2255 /* Flush buffer. */
2256 buffer_flush_all(vty->obuf, vty->wfd);
718e3744 2257
d62a17ae 2258 /* Free input buffer. */
2259 buffer_free(vty->obuf);
0b42d81a 2260 buffer_free(vty->lbuf);
718e3744 2261
d62a17ae 2262 /* Free command history. */
0a22ddfb
QY
2263 for (i = 0; i < VTY_MAXHIST; i++) {
2264 XFREE(MTYPE_VTY_HIST, vty->hist[i]);
2265 }
718e3744 2266
d62a17ae 2267 /* Unset vector. */
763725cd
IR
2268 if (vty->fd != -1) {
2269 if (vty->type == VTY_SHELL_SERV)
43dd8caf 2270 vtys_del(vtysh_sessions, vty);
763725cd 2271 else
43dd8caf 2272 vtys_del(vty_sessions, vty);
763725cd 2273 }
718e3744 2274
d62a17ae 2275 if (vty->wfd > 0 && vty->type == VTY_FILE)
2276 fsync(vty->wfd);
056cfe49 2277
10b8a9c0
DL
2278 /* Close socket.
2279 * note check is for fd > STDERR_FILENO, not fd != -1.
2280 * We never close stdin/stdout/stderr here, because we may be
2281 * running in foreground mode with logging to stdout. Also,
2282 * additionally, we'd need to replace these fds with /dev/null. */
2283 if (vty->wfd > STDERR_FILENO && vty->wfd != vty->fd)
2284 close(vty->wfd);
572e2644 2285 if (vty->fd > STDERR_FILENO)
d62a17ae 2286 close(vty->fd);
572e2644 2287 if (vty->fd == STDIN_FILENO)
d62a17ae 2288 was_stdio = true;
718e3744 2289
0a22ddfb 2290 XFREE(MTYPE_VTY, vty->buf);
718e3744 2291
7ab57d19
DS
2292 if (vty->error) {
2293 vty->error->del = vty_error_delete;
2294 list_delete(&vty->error);
2295 }
5689fe5f 2296
d62a17ae 2297 /* OK free vty. */
2298 XFREE(MTYPE_VTY, vty);
dd03f8ca 2299
d62a17ae 2300 if (was_stdio)
154b9e8f 2301 vty_stdio_reset(0);
718e3744 2302}
2303
2304/* When time out occur output message then close connection. */
cc9f21da 2305static void vty_timeout(struct thread *thread)
718e3744 2306{
d62a17ae 2307 struct vty *vty;
718e3744 2308
d62a17ae 2309 vty = THREAD_ARG(thread);
d62a17ae 2310 vty->v_timeout = 0;
718e3744 2311
d62a17ae 2312 /* Clear buffer*/
0b42d81a 2313 buffer_reset(vty->lbuf);
d62a17ae 2314 buffer_reset(vty->obuf);
2315 vty_out(vty, "\nVty connection is timed out.\n");
718e3744 2316
d62a17ae 2317 /* Close connection. */
2318 vty->status = VTY_CLOSE;
2319 vty_close(vty);
718e3744 2320}
2321
2322/* Read up configuration file from file_name. */
1c2facd1 2323static void vty_read_file(struct nb_config *config, FILE *confp)
d62a17ae 2324{
2325 int ret;
2326 struct vty *vty;
7ab57d19
DS
2327 struct vty_error *ve;
2328 struct listnode *node;
d62a17ae 2329 unsigned int line_num = 0;
2330
2331 vty = vty_new();
10b8a9c0 2332 /* vty_close won't close stderr; if some config command prints
03bad95a 2333 * something it'll end up there. (not ideal; it'd be better if output
10b8a9c0
DL
2334 * from a file-load went to logging instead. Also note that if this
2335 * function is called after daemonizing, stderr will be /dev/null.)
2336 *
2337 * vty->fd will be -1 from vty_new()
2338 */
2339 vty->wfd = STDERR_FILENO;
d62a17ae 2340 vty->type = VTY_FILE;
2341 vty->node = CONFIG_NODE;
eaf6705d 2342 vty->config = true;
1c2facd1
RW
2343 if (config)
2344 vty->candidate_config = config;
2345 else {
2346 vty->private_config = true;
2347 vty->candidate_config = nb_config_new(NULL);
2348 }
d62a17ae 2349
2350 /* Execute configuration file */
2351 ret = config_from_file(vty, confp, &line_num);
2352
2353 /* Flush any previous errors before printing messages below */
10b8a9c0 2354 buffer_flush_all(vty->obuf, vty->wfd);
d62a17ae 2355
2356 if (!((ret == CMD_SUCCESS) || (ret == CMD_ERR_NOTHING_TODO))) {
2357 const char *message = NULL;
b4fa7c95
DL
2358 char *nl;
2359
d62a17ae 2360 switch (ret) {
2361 case CMD_ERR_AMBIGUOUS:
b4fa7c95 2362 message = "Ambiguous command";
d62a17ae 2363 break;
2364 case CMD_ERR_NO_MATCH:
b4fa7c95 2365 message = "No such command";
d62a17ae 2366 break;
c539c389
DS
2367 case CMD_WARNING:
2368 message = "Command returned Warning";
2369 break;
2370 case CMD_WARNING_CONFIG_FAILED:
2371 message = "Command returned Warning Config Failed";
2372 break;
2373 case CMD_ERR_INCOMPLETE:
2374 message = "Command returned Incomplete";
2375 break;
2376 case CMD_ERR_EXEED_ARGC_MAX:
996c9314
LB
2377 message =
2378 "Command exceeded maximum number of Arguments";
c539c389
DS
2379 break;
2380 default:
2381 message = "Command returned unhandled error message";
2382 break;
d62a17ae 2383 }
b4fa7c95 2384
7ab57d19
DS
2385 for (ALL_LIST_ELEMENTS_RO(vty->error, node, ve)) {
2386 nl = strchr(ve->error_buf, '\n');
2387 if (nl)
2388 *nl = '\0';
ad6f7449 2389 flog_err(EC_LIB_VTY, "%s on config line %u: %s",
7ab57d19
DS
2390 message, ve->line_num, ve->error_buf);
2391 }
d62a17ae 2392 }
2393
1c2facd1
RW
2394 /*
2395 * Automatically commit the candidate configuration after
2396 * reading the configuration file.
2397 */
91f9fd78 2398 if (config == NULL) {
13d6b9c1 2399 struct nb_context context = {};
df5eda3d 2400 char errmsg[BUFSIZ] = {0};
13d6b9c1
RW
2401
2402 context.client = NB_CLIENT_CLI;
2403 context.user = vty;
2404 ret = nb_candidate_commit(&context, vty->candidate_config, true,
df5eda3d
RW
2405 "Read configuration file", NULL,
2406 errmsg, sizeof(errmsg));
1c2facd1 2407 if (ret != NB_OK && ret != NB_ERR_NO_CHANGES)
df5eda3d
RW
2408 zlog_err(
2409 "%s: failed to read configuration file: %s (%s)",
2410 __func__, nb_err_name(ret), errmsg);
1c2facd1
RW
2411 }
2412
d62a17ae 2413 vty_close(vty);
2414}
2415
2416static FILE *vty_use_backup_config(const char *fullpath)
2417{
2418 char *fullpath_sav, *fullpath_tmp;
2419 FILE *ret = NULL;
2420 int tmp, sav;
2421 int c;
2422 char buffer[512];
2423
9f73d2c9
QY
2424 size_t fullpath_sav_sz = strlen(fullpath) + strlen(CONF_BACKUP_EXT) + 1;
2425 fullpath_sav = malloc(fullpath_sav_sz);
2426 strlcpy(fullpath_sav, fullpath, fullpath_sav_sz);
2427 strlcat(fullpath_sav, CONF_BACKUP_EXT, fullpath_sav_sz);
d62a17ae 2428
2429 sav = open(fullpath_sav, O_RDONLY);
2430 if (sav < 0) {
2431 free(fullpath_sav);
2432 return NULL;
2433 }
2434
2435 fullpath_tmp = malloc(strlen(fullpath) + 8);
fc746f1c 2436 snprintf(fullpath_tmp, strlen(fullpath) + 8, "%s.XXXXXX", fullpath);
d62a17ae 2437
2438 /* Open file to configuration write. */
2439 tmp = mkstemp(fullpath_tmp);
2440 if (tmp < 0)
2441 goto out_close_sav;
2442
2443 if (fchmod(tmp, CONFIGFILE_MASK) != 0)
2444 goto out_close;
2445
2446 while ((c = read(sav, buffer, 512)) > 0) {
2447 if (write(tmp, buffer, c) <= 0)
2448 goto out_close;
2449 }
2450 close(sav);
2451 close(tmp);
2452
2453 if (rename(fullpath_tmp, fullpath) == 0)
2454 ret = fopen(fullpath, "r");
2455 else
2456 unlink(fullpath_tmp);
2457
2458 if (0) {
2459 out_close:
2460 close(tmp);
2461 unlink(fullpath_tmp);
2462 out_close_sav:
2463 close(sav);
2464 }
2465
2466 free(fullpath_sav);
2467 free(fullpath_tmp);
2468 return ret;
718e3744 2469}
2470
2471/* Read up configuration file from file_name. */
1c2facd1
RW
2472bool vty_read_config(struct nb_config *config, const char *config_file,
2473 char *config_default_dir)
d62a17ae 2474{
2475 char cwd[MAXPATHLEN];
2476 FILE *confp = NULL;
2477 const char *fullpath;
2478 char *tmp = NULL;
5ede5e4e 2479 bool read_success = false;
d62a17ae 2480
2481 /* If -f flag specified. */
2482 if (config_file != NULL) {
2483 if (!IS_DIRECTORY_SEP(config_file[0])) {
2484 if (getcwd(cwd, MAXPATHLEN) == NULL) {
09c866e3 2485 flog_err_sys(
450971aa 2486 EC_LIB_SYSTEM_CALL,
1c2facd1
RW
2487 "%s: failure to determine Current Working Directory %d!",
2488 __func__, errno);
2489 goto tmp_free_and_out;
d62a17ae 2490 }
7533cad7
QY
2491 size_t tmp_len = strlen(cwd) + strlen(config_file) + 2;
2492 tmp = XMALLOC(MTYPE_TMP, tmp_len);
2493 snprintf(tmp, tmp_len, "%s/%s", cwd, config_file);
d62a17ae 2494 fullpath = tmp;
2495 } else
2496 fullpath = config_file;
2497
2498 confp = fopen(fullpath, "r");
2499
2500 if (confp == NULL) {
34699016 2501 flog_warn(
450971aa 2502 EC_LIB_BACKUP_CONFIG,
34699016
DS
2503 "%s: failed to open configuration file %s: %s, checking backup",
2504 __func__, fullpath, safe_strerror(errno));
d62a17ae 2505
2506 confp = vty_use_backup_config(fullpath);
2507 if (confp)
a0ee6f32
DS
2508 flog_warn(EC_LIB_BACKUP_CONFIG,
2509 "using backup configuration file!");
d62a17ae 2510 else {
1c2facd1
RW
2511 flog_err(
2512 EC_LIB_VTY,
2513 "%s: can't open configuration file [%s]",
2514 __func__, config_file);
2515 goto tmp_free_and_out;
d62a17ae 2516 }
2517 }
2518 } else {
2519
2520 host_config_set(config_default_dir);
a7222276 2521
718e3744 2522#ifdef VTYSH
d62a17ae 2523 int ret;
2524 struct stat conf_stat;
2525
2526 /* !!!!PLEASE LEAVE!!!!
2527 * This is NEEDED for use with vtysh -b, or else you can get
2528 * a real configuration food fight with a lot garbage in the
2529 * merged configuration file it creates coming from the per
2530 * daemon configuration files. This also allows the daemons
2531 * to start if there default configuration file is not
2532 * present or ignore them, as needed when using vtysh -b to
2533 * configure the daemons at boot - MAG
2534 */
2535
2536 /* Stat for vtysh Zebra.conf, if found startup and wait for
2537 * boot configuration
2538 */
2539
2540 if (strstr(config_default_dir, "vtysh") == NULL) {
2541 ret = stat(integrate_default, &conf_stat);
5ede5e4e
DS
2542 if (ret >= 0) {
2543 read_success = true;
d62a17ae 2544 goto tmp_free_and_out;
5ede5e4e 2545 }
d62a17ae 2546 }
a7222276 2547#endif /* VTYSH */
d62a17ae 2548 confp = fopen(config_default_dir, "r");
2549 if (confp == NULL) {
34699016 2550 flog_err(
450971aa 2551 EC_LIB_SYSTEM_CALL,
34699016
DS
2552 "%s: failed to open configuration file %s: %s, checking backup",
2553 __func__, config_default_dir,
2554 safe_strerror(errno));
d62a17ae 2555
2556 confp = vty_use_backup_config(config_default_dir);
2557 if (confp) {
a0ee6f32
DS
2558 flog_warn(EC_LIB_BACKUP_CONFIG,
2559 "using backup configuration file!");
d62a17ae 2560 fullpath = config_default_dir;
2561 } else {
450971aa 2562 flog_err(EC_LIB_VTY,
1c50c1c0
QY
2563 "can't open configuration file [%s]",
2564 config_default_dir);
d62a17ae 2565 goto tmp_free_and_out;
2566 }
2567 } else
2568 fullpath = config_default_dir;
2569 }
2570
1c2facd1 2571 vty_read_file(config, confp);
5ede5e4e 2572 read_success = true;
d62a17ae 2573
2574 fclose(confp);
2575
2576 host_config_set(fullpath);
6eda6425
DS
2577
2578tmp_free_and_out:
0a22ddfb 2579 XFREE(MTYPE_TMP, tmp);
5ede5e4e
DS
2580
2581 return read_success;
718e3744 2582}
2583
763725cd
IR
2584static void update_xpath(struct vty *vty, const char *oldpath,
2585 const char *newpath)
2586{
2587 int i;
2588
2589 for (i = 0; i < vty->xpath_index; i++) {
2590 if (!frrstr_startswith(vty->xpath[i], oldpath))
2591 break;
2592
2593 char *tmp = frrstr_replace(vty->xpath[i], oldpath, newpath);
2594 strlcpy(vty->xpath[i], tmp, sizeof(vty->xpath[0]));
2595 XFREE(MTYPE_TMP, tmp);
2596 }
2597}
2598
2599void vty_update_xpath(const char *oldpath, const char *newpath)
2600{
2601 struct vty *vty;
763725cd 2602
43dd8caf 2603 frr_each (vtys, vtysh_sessions, vty)
763725cd 2604 update_xpath(vty, oldpath, newpath);
43dd8caf 2605 frr_each (vtys, vty_sessions, vty)
763725cd 2606 update_xpath(vty, oldpath, newpath);
763725cd
IR
2607}
2608
f344c66e 2609int vty_config_enter(struct vty *vty, bool private_config, bool exclusive)
718e3744 2610{
364ad673
RW
2611 if (exclusive && nb_running_lock(NB_CLIENT_CLI, vty)) {
2612 vty_out(vty, "%% Configuration is locked by other client\n");
f344c66e 2613 return CMD_WARNING;
d62a17ae 2614 }
f344c66e
RW
2615
2616 vty->node = CONFIG_NODE;
2617 vty->config = true;
2618 vty->private_config = private_config;
41e195d4 2619 vty->xpath_index = 0;
f344c66e 2620
8685be73
RW
2621 if (private_config) {
2622 vty->candidate_config = nb_config_dup(running_config);
2623 vty->candidate_config_base = nb_config_dup(running_config);
2624 vty_out(vty,
2625 "Warning: uncommitted changes will be discarded on exit.\n\n");
2626 } else {
2627 vty->candidate_config = vty_shared_candidate_config;
2628 if (frr_get_cli_mode() == FRR_CLI_TRANSACTIONAL)
f344c66e
RW
2629 vty->candidate_config_base =
2630 nb_config_dup(running_config);
2631 }
2632
2633 return CMD_SUCCESS;
718e3744 2634}
2635
f344c66e 2636void vty_config_exit(struct vty *vty)
718e3744 2637{
24389580
DL
2638 enum node_type node = vty->node;
2639 struct cmd_node *cnode;
2640
2641 /* unlock and jump up to ENABLE_NODE if -and only if- we're
2642 * somewhere below CONFIG_NODE */
2643 while (node && node != CONFIG_NODE) {
2644 cnode = vector_lookup(cmdvec, node);
2645 node = cnode->parent_node;
2646 }
791ded4a
DL
2647 if (node != CONFIG_NODE)
2648 /* called outside config, e.g. vty_close() in ENABLE_NODE */
24389580 2649 return;
24389580
DL
2650
2651 while (vty->node != ENABLE_NODE)
791ded4a 2652 /* will call vty_config_node_exit() below */
24389580 2653 cmd_exit(vty);
791ded4a
DL
2654}
2655
2656int vty_config_node_exit(struct vty *vty)
2657{
2658 vty->xpath_index = 0;
24389580 2659
fd396924
CH
2660 /* Perform any pending commits. */
2661 (void)nb_cli_pending_commit_check(vty);
b855e95f 2662
fbdc1c0a
RW
2663 /* Check if there's a pending confirmed commit. */
2664 if (vty->t_confirmed_commit_timeout) {
2665 vty_out(vty,
a0ee6f32 2666 "exiting with a pending confirmed commit. Rolling back to previous configuration.\n\n");
fbdc1c0a
RW
2667 nb_cli_confirmed_commit_rollback(vty);
2668 nb_cli_confirmed_commit_clean(vty);
2669 }
2670
364ad673 2671 (void)nb_running_unlock(NB_CLIENT_CLI, vty);
1c2facd1
RW
2672
2673 if (vty->candidate_config) {
2674 if (vty->private_config)
2675 nb_config_free(vty->candidate_config);
2676 vty->candidate_config = NULL;
2677 }
2678 if (vty->candidate_config_base) {
2679 nb_config_free(vty->candidate_config_base);
2680 vty->candidate_config_base = NULL;
2681 }
cf09d3ca
RW
2682
2683 vty->config = false;
791ded4a 2684 return 1;
cc933ef9
DL
2685}
2686
718e3744 2687/* Master of the threads. */
79159516 2688static struct thread_master *vty_master;
718e3744 2689
55a70ffb 2690static void vty_event_serv(enum vty_event event, struct vty_serv *vty_serv)
718e3744 2691{
d62a17ae 2692 switch (event) {
2693 case VTY_SERV:
26ae7cc2
DL
2694 thread_add_read(vty_master, vty_accept, vty_serv,
2695 vty_serv->sock, &vty_serv->t_accept);
d62a17ae 2696 break;
718e3744 2697#ifdef VTYSH
d62a17ae 2698 case VTYSH_SERV:
26ae7cc2
DL
2699 thread_add_read(vty_master, vtysh_accept, vty_serv,
2700 vty_serv->sock, &vty_serv->t_accept);
d62a17ae 2701 break;
ab699721
DL
2702#endif /* VTYSH */
2703 default:
2704 assert(!"vty_event_serv() called incorrectly");
2705 }
2706}
2707
55a70ffb 2708static void vty_event(enum vty_event event, struct vty *vty)
ab699721
DL
2709{
2710 switch (event) {
2711#ifdef VTYSH
d62a17ae 2712 case VTYSH_READ:
ab699721 2713 thread_add_read(vty_master, vtysh_read, vty, vty->fd,
d62a17ae 2714 &vty->t_read);
2715 break;
2716 case VTYSH_WRITE:
ab699721 2717 thread_add_write(vty_master, vtysh_write, vty, vty->wfd,
d62a17ae 2718 &vty->t_write);
2719 break;
718e3744 2720#endif /* VTYSH */
d62a17ae 2721 case VTY_READ:
ab699721
DL
2722 thread_add_read(vty_master, vty_read, vty, vty->fd,
2723 &vty->t_read);
d62a17ae 2724
2725 /* Time out treatment. */
2726 if (vty->v_timeout) {
43b8ca99 2727 THREAD_OFF(vty->t_timeout);
d62a17ae 2728 thread_add_timer(vty_master, vty_timeout, vty,
2729 vty->v_timeout, &vty->t_timeout);
2730 }
2731 break;
2732 case VTY_WRITE:
ab699721 2733 thread_add_write(vty_master, vty_flush, vty, vty->wfd,
d62a17ae 2734 &vty->t_write);
2735 break;
2736 case VTY_TIMEOUT_RESET:
43b8ca99
DS
2737 THREAD_OFF(vty->t_timeout);
2738 if (vty->v_timeout)
d62a17ae 2739 thread_add_timer(vty_master, vty_timeout, vty,
2740 vty->v_timeout, &vty->t_timeout);
d62a17ae 2741 break;
ab699721
DL
2742 default:
2743 assert(!"vty_event() called incorrectly");
d62a17ae 2744 }
718e3744 2745}
6b0655a2 2746
505e5056 2747DEFUN_NOSH (config_who,
718e3744 2748 config_who_cmd,
2749 "who",
2750 "Display who is on vty\n")
2751{
d62a17ae 2752 struct vty *v;
718e3744 2753
43dd8caf 2754 frr_each (vtys, vty_sessions, v)
0798d276
DL
2755 vty_out(vty, "%svty[%d] connected from %s%s.\n",
2756 v->config ? "*" : " ", v->fd, v->address,
2757 zlog_live_is_null(&v->live_log) ? "" : ", live log");
d62a17ae 2758 return CMD_SUCCESS;
718e3744 2759}
2760
2761/* Move to vty configuration mode. */
505e5056 2762DEFUN_NOSH (line_vty,
718e3744 2763 line_vty_cmd,
2764 "line vty",
2765 "Configure a terminal line\n"
2766 "Virtual terminal\n")
2767{
d62a17ae 2768 vty->node = VTY_NODE;
2769 return CMD_SUCCESS;
718e3744 2770}
2771
2772/* Set time out value. */
d62a17ae 2773static int exec_timeout(struct vty *vty, const char *min_str,
2774 const char *sec_str)
718e3744 2775{
d62a17ae 2776 unsigned long timeout = 0;
718e3744 2777
d62a17ae 2778 /* min_str and sec_str are already checked by parser. So it must be
2779 all digit string. */
2780 if (min_str) {
2781 timeout = strtol(min_str, NULL, 10);
2782 timeout *= 60;
2783 }
2784 if (sec_str)
2785 timeout += strtol(sec_str, NULL, 10);
718e3744 2786
d62a17ae 2787 vty_timeout_val = timeout;
2788 vty->v_timeout = timeout;
ab699721 2789 vty_event(VTY_TIMEOUT_RESET, vty);
718e3744 2790
2791
d62a17ae 2792 return CMD_SUCCESS;
718e3744 2793}
2794
2795DEFUN (exec_timeout_min,
2796 exec_timeout_min_cmd,
aa1c90a4 2797 "exec-timeout (0-35791)",
718e3744 2798 "Set timeout value\n"
2799 "Timeout value in minutes\n")
2800{
d62a17ae 2801 int idx_number = 1;
2802 return exec_timeout(vty, argv[idx_number]->arg, NULL);
718e3744 2803}
2804
2805DEFUN (exec_timeout_sec,
2806 exec_timeout_sec_cmd,
aa1c90a4 2807 "exec-timeout (0-35791) (0-2147483)",
718e3744 2808 "Set the EXEC timeout\n"
2809 "Timeout in minutes\n"
2810 "Timeout in seconds\n")
2811{
d62a17ae 2812 int idx_number = 1;
2813 int idx_number_2 = 2;
2814 return exec_timeout(vty, argv[idx_number]->arg,
2815 argv[idx_number_2]->arg);
718e3744 2816}
2817
2818DEFUN (no_exec_timeout,
2819 no_exec_timeout_cmd,
2820 "no exec-timeout",
2821 NO_STR
2822 "Set the EXEC timeout\n")
2823{
d62a17ae 2824 return exec_timeout(vty, NULL, NULL);
718e3744 2825}
2826
2827/* Set vty access class. */
2828DEFUN (vty_access_class,
2829 vty_access_class_cmd,
2830 "access-class WORD",
2831 "Filter connections based on an IP access list\n"
2832 "IP access list\n")
2833{
d62a17ae 2834 int idx_word = 1;
2835 if (vty_accesslist_name)
2836 XFREE(MTYPE_VTY, vty_accesslist_name);
718e3744 2837
d62a17ae 2838 vty_accesslist_name = XSTRDUP(MTYPE_VTY, argv[idx_word]->arg);
718e3744 2839
d62a17ae 2840 return CMD_SUCCESS;
718e3744 2841}
2842
2843/* Clear vty access class. */
2844DEFUN (no_vty_access_class,
2845 no_vty_access_class_cmd,
2846 "no access-class [WORD]",
2847 NO_STR
2848 "Filter connections based on an IP access list\n"
2849 "IP access list\n")
2850{
d62a17ae 2851 int idx_word = 2;
2852 const char *accesslist = (argc == 3) ? argv[idx_word]->arg : NULL;
2853 if (!vty_accesslist_name
2854 || (argc == 3 && strcmp(vty_accesslist_name, accesslist))) {
2855 vty_out(vty, "Access-class is not currently applied to vty\n");
2856 return CMD_WARNING_CONFIG_FAILED;
2857 }
718e3744 2858
d62a17ae 2859 XFREE(MTYPE_VTY, vty_accesslist_name);
718e3744 2860
d62a17ae 2861 vty_accesslist_name = NULL;
718e3744 2862
d62a17ae 2863 return CMD_SUCCESS;
718e3744 2864}
2865
718e3744 2866/* Set vty access class. */
2867DEFUN (vty_ipv6_access_class,
2868 vty_ipv6_access_class_cmd,
2869 "ipv6 access-class WORD",
2870 IPV6_STR
2871 "Filter connections based on an IP access list\n"
2872 "IPv6 access list\n")
2873{
d62a17ae 2874 int idx_word = 2;
2875 if (vty_ipv6_accesslist_name)
2876 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
718e3744 2877
d62a17ae 2878 vty_ipv6_accesslist_name = XSTRDUP(MTYPE_VTY, argv[idx_word]->arg);
718e3744 2879
d62a17ae 2880 return CMD_SUCCESS;
718e3744 2881}
2882
2883/* Clear vty access class. */
2884DEFUN (no_vty_ipv6_access_class,
2885 no_vty_ipv6_access_class_cmd,
2886 "no ipv6 access-class [WORD]",
2887 NO_STR
2888 IPV6_STR
2889 "Filter connections based on an IP access list\n"
2890 "IPv6 access list\n")
2891{
d62a17ae 2892 int idx_word = 3;
2893 const char *accesslist = (argc == 4) ? argv[idx_word]->arg : NULL;
aa1c90a4 2894
d62a17ae 2895 if (!vty_ipv6_accesslist_name
2896 || (argc == 4 && strcmp(vty_ipv6_accesslist_name, accesslist))) {
2897 vty_out(vty,
2898 "IPv6 access-class is not currently applied to vty\n");
2899 return CMD_WARNING_CONFIG_FAILED;
2900 }
718e3744 2901
d62a17ae 2902 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
718e3744 2903
d62a17ae 2904 vty_ipv6_accesslist_name = NULL;
718e3744 2905
d62a17ae 2906 return CMD_SUCCESS;
718e3744 2907}
718e3744 2908
2909/* vty login. */
2910DEFUN (vty_login,
2911 vty_login_cmd,
2912 "login",
2913 "Enable password checking\n")
2914{
d62a17ae 2915 no_password_check = 0;
2916 return CMD_SUCCESS;
718e3744 2917}
2918
2919DEFUN (no_vty_login,
2920 no_vty_login_cmd,
2921 "no login",
2922 NO_STR
2923 "Enable password checking\n")
2924{
d62a17ae 2925 no_password_check = 1;
2926 return CMD_SUCCESS;
718e3744 2927}
2928
2929DEFUN (service_advanced_vty,
2930 service_advanced_vty_cmd,
2931 "service advanced-vty",
2932 "Set up miscellaneous service\n"
2933 "Enable advanced mode vty interface\n")
2934{
d62a17ae 2935 host.advanced = 1;
2936 return CMD_SUCCESS;
718e3744 2937}
2938
2939DEFUN (no_service_advanced_vty,
2940 no_service_advanced_vty_cmd,
2941 "no service advanced-vty",
2942 NO_STR
2943 "Set up miscellaneous service\n"
2944 "Enable advanced mode vty interface\n")
2945{
d62a17ae 2946 host.advanced = 0;
2947 return CMD_SUCCESS;
718e3744 2948}
2949
0798d276
DL
2950DEFUN_NOSH(terminal_monitor,
2951 terminal_monitor_cmd,
2952 "terminal monitor [detach]",
2953 "Set terminal line parameters\n"
2954 "Copy debug output to the current terminal line\n"
2955 "Keep logging feed open independent of VTY session\n")
718e3744 2956{
0798d276
DL
2957 int fd_ret = -1;
2958
2959 if (vty->type != VTY_SHELL_SERV) {
2960 vty_out(vty, "%% not supported\n");
2961 return CMD_WARNING;
2962 }
2963
2964 if (argc == 3) {
2965 struct zlog_live_cfg detach_log = {};
2966
2967 zlog_live_open(&detach_log, LOG_DEBUG, &fd_ret);
2968 zlog_live_disown(&detach_log);
2969 } else
2970 zlog_live_open(&vty->live_log, LOG_DEBUG, &fd_ret);
2971
2972 if (fd_ret == -1) {
2973 vty_out(vty, "%% error opening live log: %m\n");
2974 return CMD_WARNING;
2975 }
2976
2977 vty_pass_fd(vty, fd_ret);
d62a17ae 2978 return CMD_SUCCESS;
718e3744 2979}
2980
0798d276
DL
2981DEFUN_NOSH(no_terminal_monitor,
2982 no_terminal_monitor_cmd,
2983 "no terminal monitor",
2984 NO_STR
2985 "Set terminal line parameters\n"
2986 "Copy debug output to the current terminal line\n")
718e3744 2987{
0798d276 2988 zlog_live_close(&vty->live_log);
d62a17ae 2989 return CMD_SUCCESS;
718e3744 2990}
2991
0798d276
DL
2992DEFUN_NOSH(terminal_no_monitor,
2993 terminal_no_monitor_cmd,
2994 "terminal no monitor",
2995 "Set terminal line parameters\n"
2996 NO_STR
2997 "Copy debug output to the current terminal line\n")
f667a580 2998{
0798d276 2999 return no_terminal_monitor(self, vty, argc, argv);
f667a580
QY
3000}
3001
789f78ac 3002
505e5056 3003DEFUN_NOSH (show_history,
718e3744 3004 show_history_cmd,
3005 "show history",
3006 SHOW_STR
3007 "Display the session command history\n")
3008{
d62a17ae 3009 int index;
718e3744 3010
d62a17ae 3011 for (index = vty->hindex + 1; index != vty->hindex;) {
3012 if (index == VTY_MAXHIST) {
3013 index = 0;
3014 continue;
3015 }
718e3744 3016
d62a17ae 3017 if (vty->hist[index] != NULL)
3018 vty_out(vty, " %s\n", vty->hist[index]);
718e3744 3019
d62a17ae 3020 index++;
3021 }
718e3744 3022
d62a17ae 3023 return CMD_SUCCESS;
718e3744 3024}
3025
da688ecd 3026/* vty login. */
2950f5da 3027DEFPY (log_commands,
da688ecd 3028 log_commands_cmd,
2950f5da
DS
3029 "[no] log commands",
3030 NO_STR
da688ecd 3031 "Logging control\n"
2950f5da 3032 "Log all commands\n")
da688ecd 3033{
2950f5da
DS
3034 if (no) {
3035 if (do_log_commands_perm) {
3036 vty_out(vty,
3037 "Daemon started with permanent logging turned on for commands, ignoring\n");
3038 return CMD_WARNING;
3039 }
3040
3041 do_log_commands = false;
3042 } else
3043 do_log_commands = true;
3044
d62a17ae 3045 return CMD_SUCCESS;
da688ecd
LB
3046}
3047
718e3744 3048/* Display current configuration. */
d62a17ae 3049static int vty_config_write(struct vty *vty)
718e3744 3050{
07679ad9 3051 vty_frame(vty, "line vty\n");
718e3744 3052
d62a17ae 3053 if (vty_accesslist_name)
3054 vty_out(vty, " access-class %s\n", vty_accesslist_name);
718e3744 3055
d62a17ae 3056 if (vty_ipv6_accesslist_name)
3057 vty_out(vty, " ipv6 access-class %s\n",
3058 vty_ipv6_accesslist_name);
718e3744 3059
d62a17ae 3060 /* exec-timeout */
3061 if (vty_timeout_val != VTY_TIMEOUT_DEFAULT)
3062 vty_out(vty, " exec-timeout %ld %ld\n", vty_timeout_val / 60,
3063 vty_timeout_val % 60);
718e3744 3064
d62a17ae 3065 /* login */
3066 if (no_password_check)
3067 vty_out(vty, " no login\n");
da688ecd 3068
07679ad9
IR
3069 vty_endframe(vty, "exit\n");
3070
d62a17ae 3071 if (do_log_commands)
3072 vty_out(vty, "log commands\n");
d0bfb22c 3073
d62a17ae 3074 vty_out(vty, "!\n");
718e3744 3075
d62a17ae 3076 return CMD_SUCCESS;
718e3744 3077}
3078
612c2c15 3079static int vty_config_write(struct vty *vty);
d62a17ae 3080struct cmd_node vty_node = {
f4b8291f 3081 .name = "vty",
62b346ee 3082 .node = VTY_NODE,
24389580 3083 .parent_node = CONFIG_NODE,
62b346ee 3084 .prompt = "%s(config-line)# ",
612c2c15 3085 .config_write = vty_config_write,
718e3744 3086};
3087
3088/* Reset all VTY status. */
4d762f26 3089void vty_reset(void)
d62a17ae 3090{
d62a17ae 3091 struct vty *vty;
d62a17ae 3092
43dd8caf
DL
3093 frr_each_safe (vtys, vty_sessions, vty) {
3094 buffer_reset(vty->lbuf);
3095 buffer_reset(vty->obuf);
3096 vty->status = VTY_CLOSE;
3097 vty_close(vty);
3098 }
d62a17ae 3099
d62a17ae 3100 vty_timeout_val = VTY_TIMEOUT_DEFAULT;
3101
e1b36e13
QY
3102 XFREE(MTYPE_VTY, vty_accesslist_name);
3103 XFREE(MTYPE_VTY, vty_ipv6_accesslist_name);
718e3744 3104}
3105
d62a17ae 3106static void vty_save_cwd(void)
718e3744 3107{
d62a17ae 3108 char *c;
79ad2798 3109
daeb97e9 3110 c = getcwd(vty_cwd, sizeof(vty_cwd));
718e3744 3111
d62a17ae 3112 if (!c) {
3113 /*
3114 * At this point if these go wrong, more than likely
3115 * the whole world is coming down around us
3116 * Hence not worrying about it too much.
3117 */
46cba0d4 3118 if (chdir(SYSCONFDIR)) {
450971aa 3119 flog_err_sys(EC_LIB_SYSTEM_CALL,
09c866e3
QY
3120 "Failure to chdir to %s, errno: %d",
3121 SYSCONFDIR, errno);
d62a17ae 3122 exit(-1);
3123 }
daeb97e9 3124 if (getcwd(vty_cwd, sizeof(vty_cwd)) == NULL) {
450971aa 3125 flog_err_sys(EC_LIB_SYSTEM_CALL,
09c866e3 3126 "Failure to getcwd, errno: %d", errno);
d62a17ae 3127 exit(-1);
3128 }
3129 }
718e3744 3130}
3131
4d762f26 3132char *vty_get_cwd(void)
718e3744 3133{
d62a17ae 3134 return vty_cwd;
718e3744 3135}
3136
d62a17ae 3137int vty_shell(struct vty *vty)
718e3744 3138{
d62a17ae 3139 return vty->type == VTY_SHELL ? 1 : 0;
718e3744 3140}
3141
d62a17ae 3142int vty_shell_serv(struct vty *vty)
718e3744 3143{
d62a17ae 3144 return vty->type == VTY_SHELL_SERV ? 1 : 0;
718e3744 3145}
3146
4d762f26 3147void vty_init_vtysh(void)
718e3744 3148{
43dd8caf 3149 /* currently nothing to do, but likely to have future use */
718e3744 3150}
3151
3152/* Install vty's own commands like `who' command. */
2950f5da 3153void vty_init(struct thread_master *master_thread, bool do_command_logging)
d62a17ae 3154{
3155 /* For further configuration read, preserve current directory. */
3156 vty_save_cwd();
3157
d62a17ae 3158 vty_master = master_thread;
3159
154b9e8f 3160 atexit(vty_stdio_atexit);
d62a17ae 3161
d62a17ae 3162 /* Install bgp top node. */
612c2c15 3163 install_node(&vty_node);
d62a17ae 3164
3165 install_element(VIEW_NODE, &config_who_cmd);
3166 install_element(VIEW_NODE, &show_history_cmd);
3167 install_element(CONFIG_NODE, &line_vty_cmd);
3168 install_element(CONFIG_NODE, &service_advanced_vty_cmd);
3169 install_element(CONFIG_NODE, &no_service_advanced_vty_cmd);
3170 install_element(CONFIG_NODE, &show_history_cmd);
3171 install_element(CONFIG_NODE, &log_commands_cmd);
2950f5da
DS
3172
3173 if (do_command_logging) {
3174 do_log_commands = true;
3175 do_log_commands_perm = true;
3176 }
3177
d62a17ae 3178 install_element(ENABLE_NODE, &terminal_monitor_cmd);
3179 install_element(ENABLE_NODE, &terminal_no_monitor_cmd);
3180 install_element(ENABLE_NODE, &no_terminal_monitor_cmd);
3181
3182 install_default(VTY_NODE);
3183 install_element(VTY_NODE, &exec_timeout_min_cmd);
3184 install_element(VTY_NODE, &exec_timeout_sec_cmd);
3185 install_element(VTY_NODE, &no_exec_timeout_cmd);
3186 install_element(VTY_NODE, &vty_access_class_cmd);
3187 install_element(VTY_NODE, &no_vty_access_class_cmd);
3188 install_element(VTY_NODE, &vty_login_cmd);
3189 install_element(VTY_NODE, &no_vty_login_cmd);
3190 install_element(VTY_NODE, &vty_ipv6_access_class_cmd);
3191 install_element(VTY_NODE, &no_vty_ipv6_access_class_cmd);
3192}
3193
3194void vty_terminate(void)
3195{
43dd8caf 3196 struct vty *vty;
26ae7cc2 3197 struct vty_serv *vtyserv;
43dd8caf 3198
daeb97e9 3199 memset(vty_cwd, 0x00, sizeof(vty_cwd));
d62a17ae 3200
43dd8caf
DL
3201 vty_reset();
3202
3203 /* default state of vty_sessions is initialized & empty. */
3204 vtys_fini(vty_sessions);
3205 vtys_init(vty_sessions);
3206
3207 /* vty_reset() doesn't close vtysh sessions */
3208 frr_each_safe (vtys, vtysh_sessions, vty) {
3209 buffer_reset(vty->lbuf);
3210 buffer_reset(vty->obuf);
3211 vty->status = VTY_CLOSE;
3212 vty_close(vty);
3213 }
3214
3215 vtys_fini(vtysh_sessions);
3216 vtys_init(vtysh_sessions);
3217
26ae7cc2
DL
3218 while ((vtyserv = vtyservs_pop(vty_servs))) {
3219 THREAD_OFF(vtyserv->t_accept);
3220 close(vtyserv->sock);
3221 XFREE(MTYPE_VTY_SERV, vtyserv);
d62a17ae 3222 }
26ae7cc2
DL
3223
3224 vtyservs_fini(vty_servs);
3225 vtyservs_init(vty_servs);
228da428 3226}