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