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