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