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