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