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