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