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