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