Sylvain Munaut | 12ba778 | 2014-06-16 10:13:40 +0200 | [diff] [blame] | 1 | #pragma once |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <stdarg.h> |
Neels Hofmeyr | 6305300 | 2019-04-10 02:41:53 +0200 | [diff] [blame] | 5 | #include <stdbool.h> |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 6 | |
Neels Hofmeyr | 4a31ffa | 2017-09-07 03:08:06 +0200 | [diff] [blame] | 7 | #include <osmocom/core/linuxlist.h> |
Neels Hofmeyr | 70ce871 | 2019-11-24 19:52:44 +0100 | [diff] [blame] | 8 | #include <osmocom/core/defs.h> |
Neels Hofmeyr | 4a31ffa | 2017-09-07 03:08:06 +0200 | [diff] [blame] | 9 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 10 | /*! \defgroup vty VTY (Virtual TTY) interface |
| 11 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 12 | * \file vty.h */ |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 13 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 14 | /* GCC have printf type attribute check. */ |
| 15 | #ifdef __GNUC__ |
| 16 | #define VTY_PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b))) |
| 17 | #else |
| 18 | #define VTY_PRINTF_ATTRIBUTE(a,b) |
| 19 | #endif /* __GNUC__ */ |
| 20 | |
| 21 | /* Does the I/O error indicate that the operation should be retried later? */ |
| 22 | #define ERRNO_IO_RETRY(EN) \ |
| 23 | (((EN) == EAGAIN) || ((EN) == EWOULDBLOCK) || ((EN) == EINTR)) |
| 24 | |
| 25 | /* Vty read buffer size. */ |
| 26 | #define VTY_READ_BUFSIZ 512 |
| 27 | |
| 28 | #define VTY_BUFSIZ 512 |
| 29 | #define VTY_MAXHIST 20 |
| 30 | |
Vadim Yanitskiy | e566bdd | 2020-10-06 17:41:22 +0700 | [diff] [blame] | 31 | /* Number of application / library specific VTY attributes */ |
| 32 | #define VTY_CMD_USR_ATTR_NUM 32 |
Vadim Yanitskiy | ef4c597 | 2020-10-07 13:44:31 +0700 | [diff] [blame] | 33 | /* Flag characters reserved for global VTY attributes */ |
| 34 | #define VTY_CMD_ATTR_FLAGS_RESERVED \ |
Vadim Yanitskiy | 72b9088 | 2020-10-21 05:07:34 +0700 | [diff] [blame] | 35 | { '.', '!', '@', '^' } |
Vadim Yanitskiy | e566bdd | 2020-10-06 17:41:22 +0700 | [diff] [blame] | 36 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 37 | /*! VTY events */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 38 | enum event { |
| 39 | VTY_SERV, |
| 40 | VTY_READ, |
| 41 | VTY_WRITE, |
| 42 | VTY_CLOSED, |
| 43 | VTY_TIMEOUT_RESET, |
| 44 | #ifdef VTYSH |
| 45 | VTYSH_SERV, |
| 46 | VTYSH_READ, |
| 47 | VTYSH_WRITE |
| 48 | #endif /* VTYSH */ |
| 49 | }; |
| 50 | |
Andreas Eversberg | 3c6a2ce | 2012-07-12 09:22:56 +0200 | [diff] [blame] | 51 | enum vty_type { |
| 52 | VTY_TERM, |
| 53 | VTY_FILE, |
| 54 | VTY_SHELL, |
| 55 | VTY_SHELL_SERV |
| 56 | }; |
| 57 | |
Neels Hofmeyr | 4a31ffa | 2017-09-07 03:08:06 +0200 | [diff] [blame] | 58 | struct vty_parent_node { |
| 59 | struct llist_head entry; |
| 60 | |
| 61 | /*! private data, specified by creator */ |
| 62 | void *priv; |
| 63 | |
| 64 | /*! Node status of this vty */ |
| 65 | int node; |
| 66 | |
| 67 | /*! When reading from a config file, these are the indenting characters expected for children of |
| 68 | * this VTY node. */ |
| 69 | char *indent; |
| 70 | }; |
| 71 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 72 | /*! Internal representation of a single VTY */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 73 | struct vty { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 74 | /*! underlying file (if any) */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 75 | FILE *file; |
| 76 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 77 | /*! private data, specified by creator */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 78 | void *priv; |
| 79 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 80 | /*! File descripter of this vty. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 81 | int fd; |
| 82 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 83 | /*! Is this vty connect to file or not */ |
Andreas Eversberg | 3c6a2ce | 2012-07-12 09:22:56 +0200 | [diff] [blame] | 84 | enum vty_type type; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 85 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 86 | /*! Node status of this vty */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 87 | int node; |
| 88 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 89 | /*! Failure count */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 90 | int fail; |
| 91 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 92 | /*! Output buffer. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 93 | struct buffer *obuf; |
| 94 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 95 | /*! Command input buffer */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 96 | char *buf; |
| 97 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 98 | /*! Command cursor point */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 99 | int cp; |
| 100 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 101 | /*! Command length */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 102 | int length; |
| 103 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 104 | /*! Command max length. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 105 | int max; |
| 106 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 107 | /*! Histry of command */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 108 | char *hist[VTY_MAXHIST]; |
| 109 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 110 | /*! History lookup current point */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 111 | int hp; |
| 112 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 113 | /*! History insert end point */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 114 | int hindex; |
| 115 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 116 | /*! For current referencing point of interface, route-map, |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 117 | access-list etc... */ |
| 118 | void *index; |
| 119 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 120 | /*! For multiple level index treatment such as key chain and key. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 121 | void *index_sub; |
| 122 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 123 | /*! For escape character. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 124 | unsigned char escape; |
| 125 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 126 | /*! Current vty status. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 127 | enum { VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE } status; |
| 128 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 129 | /*! IAC handling |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 130 | * |
| 131 | * IAC handling: was the last character received the IAC |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 132 | * (interpret-as-command) escape character (and therefore the next |
| 133 | * character will be the command code)? Refer to Telnet RFC 854. */ |
| 134 | unsigned char iac; |
| 135 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 136 | /*! IAC SB (option subnegotiation) handling */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 137 | unsigned char iac_sb_in_progress; |
| 138 | /* At the moment, we care only about the NAWS (window size) negotiation, |
| 139 | * and that requires just a 5-character buffer (RFC 1073): |
| 140 | * <NAWS char> <16-bit width> <16-bit height> */ |
| 141 | #define TELNET_NAWS_SB_LEN 5 |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 142 | /*! sub-negotiation buffer */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 143 | unsigned char sb_buf[TELNET_NAWS_SB_LEN]; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 144 | /*! How many subnegotiation characters have we received? |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 145 | * |
| 146 | * We just drop those that do not fit in the buffer. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 147 | size_t sb_len; |
| 148 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 149 | /*! Window width */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 150 | int width; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 151 | /*! Widnow height */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 152 | int height; |
| 153 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 154 | /*! Configure lines. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 155 | int lines; |
| 156 | |
| 157 | int monitor; |
| 158 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 159 | /*! In configure mode. */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 160 | int config; |
Neels Hofmeyr | 4a31ffa | 2017-09-07 03:08:06 +0200 | [diff] [blame] | 161 | |
| 162 | /*! List of parent nodes, last item is the outermost parent. */ |
| 163 | struct llist_head parent_nodes; |
| 164 | |
| 165 | /*! When reading from a config file, these are the indenting characters expected for children of |
| 166 | * the current VTY node. */ |
| 167 | char *indent; |
Vadim Yanitskiy | 0a2d9bd | 2020-10-25 16:34:57 +0700 | [diff] [blame] | 168 | |
| 169 | /*! Whether the expert mode is enabled. */ |
| 170 | bool expert_mode; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 171 | }; |
| 172 | |
| 173 | /* Small macro to determine newline is newline only or linefeed needed. */ |
| 174 | #define VTY_NEWLINE ((vty->type == VTY_TERM) ? "\r\n" : "\n") |
| 175 | |
Andreas Eversberg | 3c6a2ce | 2012-07-12 09:22:56 +0200 | [diff] [blame] | 176 | static inline const char *vty_newline(struct vty *vty) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 177 | { |
| 178 | return VTY_NEWLINE; |
| 179 | } |
| 180 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 181 | /*! Information an application registers with the VTY */ |
Harald Welte | 237f624 | 2010-05-25 23:00:45 +0200 | [diff] [blame] | 182 | struct vty_app_info { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 183 | /*! name of the application */ |
Holger Hans Peter Freyther | dd19527 | 2010-06-08 16:12:58 +0800 | [diff] [blame] | 184 | const char *name; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 185 | /*! version string of the application */ |
Holger Hans Peter Freyther | dd19527 | 2010-06-08 16:12:58 +0800 | [diff] [blame] | 186 | const char *version; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 187 | /*! copyright string of the application */ |
Holger Hans Peter Freyther | dd19527 | 2010-06-08 16:12:58 +0800 | [diff] [blame] | 188 | const char *copyright; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 189 | /*! \ref talloc context */ |
Harald Welte | 237f624 | 2010-05-25 23:00:45 +0200 | [diff] [blame] | 190 | void *tall_ctx; |
Neels Hofmeyr | d31de23 | 2019-10-31 16:09:23 +0100 | [diff] [blame] | 191 | /*! Call-back for taking actions upon exiting a node. |
| 192 | * The return value is ignored, and changes to vty->node and vty->index made in this callback are ignored. |
| 193 | * Implicit parent node tracking always sets the correct parent node and vty->index after this callback exits, |
| 194 | * so this callback can handle only those nodes that should take specific actions upon node exit, or can be left |
| 195 | * NULL entirely. */ |
Holger Hans Peter Freyther | a9e5252 | 2015-08-02 02:14:07 +0000 | [diff] [blame] | 196 | int (*go_parent_cb)(struct vty *vty); |
Neels Hofmeyr | d31de23 | 2019-10-31 16:09:23 +0100 | [diff] [blame] | 197 | /*! OBSOLETED: Implicit parent node tracking has replaced the use of this callback. This callback is no longer |
| 198 | * called, ever, and can be left NULL. */ |
Neels Hofmeyr | 70ce871 | 2019-11-24 19:52:44 +0100 | [diff] [blame] | 199 | int (*is_config_node)(struct vty *vty, int node) |
| 200 | OSMO_DEPRECATED("Implicit parent node tracking has replaced the use of this callback. This callback is" |
| 201 | " no longer called, ever, and can be left NULL."); |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 202 | /*! Check if the config is consistent before write */ |
Holger Hans Peter Freyther | 9f0f978 | 2014-11-21 10:40:07 +0100 | [diff] [blame] | 203 | int (*config_is_consistent)(struct vty *vty); |
Vadim Yanitskiy | 7f1ecd9 | 2020-08-15 22:16:30 +0700 | [diff] [blame] | 204 | /*! Description of the application specific VTY attributes (optional). */ |
Vadim Yanitskiy | e566bdd | 2020-10-06 17:41:22 +0700 | [diff] [blame] | 205 | const char * usr_attr_desc[VTY_CMD_USR_ATTR_NUM]; |
Vadim Yanitskiy | 7f1ecd9 | 2020-08-15 22:16:30 +0700 | [diff] [blame] | 206 | /*! Flag letters of the application specific VTY attributes (optional). */ |
Vadim Yanitskiy | e566bdd | 2020-10-06 17:41:22 +0700 | [diff] [blame] | 207 | char usr_attr_letters[VTY_CMD_USR_ATTR_NUM]; |
Harald Welte | 237f624 | 2010-05-25 23:00:45 +0200 | [diff] [blame] | 208 | }; |
| 209 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 210 | /* Prototypes. */ |
Harald Welte | 237f624 | 2010-05-25 23:00:45 +0200 | [diff] [blame] | 211 | void vty_init(struct vty_app_info *app_info); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 212 | int vty_read_config_file(const char *file_name, void *priv); |
| 213 | void vty_init_vtysh (void); |
| 214 | void vty_reset (void); |
| 215 | struct vty *vty_new (void); |
| 216 | struct vty *vty_create (int vty_sock, void *priv); |
Neels Hofmeyr | 6305300 | 2019-04-10 02:41:53 +0200 | [diff] [blame] | 217 | bool vty_is_active(struct vty *vty); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 218 | int vty_out (struct vty *, const char *, ...) VTY_PRINTF_ATTRIBUTE(2, 3); |
Neels Hofmeyr | c1aa178 | 2019-02-01 05:38:44 +0100 | [diff] [blame] | 219 | int vty_out_va(struct vty *vty, const char *format, va_list ap); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 220 | int vty_out_newline(struct vty *); |
| 221 | int vty_read(struct vty *vty); |
| 222 | //void vty_time_print (struct vty *, int); |
| 223 | void vty_close (struct vty *); |
| 224 | char *vty_get_cwd (void); |
| 225 | void vty_log (const char *level, const char *proto, const char *fmt, va_list); |
| 226 | int vty_config_lock (struct vty *); |
| 227 | int vty_config_unlock (struct vty *); |
| 228 | int vty_shell (struct vty *); |
| 229 | int vty_shell_serv (struct vty *); |
| 230 | void vty_hello (struct vty *); |
Holger Hans Peter Freyther | 08aaded | 2010-09-14 02:24:03 +0800 | [diff] [blame] | 231 | void *vty_current_index(struct vty *); |
| 232 | int vty_current_node(struct vty *vty); |
Holger Hans Peter Freyther | a9e5252 | 2015-08-02 02:14:07 +0000 | [diff] [blame] | 233 | int vty_go_parent(struct vty *vty); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 234 | |
Neels Hofmeyr | 96172f0 | 2016-02-23 14:01:41 +0100 | [diff] [blame] | 235 | /* Return IP address passed to the 'line vty'/'bind' command, or "127.0.0.1" */ |
| 236 | const char *vty_get_bind_addr(void); |
Holger Hans Peter Freyther | 99ae401 | 2018-12-15 17:36:41 +0000 | [diff] [blame] | 237 | /** Returns configured port passed to the 'line vty'/'bind' command or default_port. */ |
| 238 | int vty_get_bind_port(int default_port); |
Neels Hofmeyr | 96172f0 | 2016-02-23 14:01:41 +0100 | [diff] [blame] | 239 | |
Holger Hans Peter Freyther | 4aaccd7 | 2010-08-31 17:09:44 +0800 | [diff] [blame] | 240 | extern void *tall_vty_ctx; |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 241 | |
Harald Welte | d61d517 | 2011-09-04 22:56:10 +0200 | [diff] [blame] | 242 | extern struct cmd_element cfg_description_cmd; |
| 243 | extern struct cmd_element cfg_no_description_cmd; |
| 244 | |
Holger Hans Peter Freyther | 2c9168c | 2013-10-10 20:21:33 +0200 | [diff] [blame] | 245 | |
| 246 | /** |
| 247 | * signal handling |
| 248 | */ |
| 249 | enum signal_vty { |
| 250 | S_VTY_EVENT, |
| 251 | }; |
| 252 | |
| 253 | struct vty_signal_data { |
| 254 | enum event event; |
| 255 | int sock; |
| 256 | struct vty *vty; |
| 257 | }; |
| 258 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 259 | /*! @} */ |