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