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