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