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