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