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