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