1 /* Virtual terminal [aka TeletYpe] interface routine
2  * Copyright (C) 1997 Kunihiro Ishiguro
3  *
4  * This file is part of GNU Zebra.
5  *
6  * GNU Zebra is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; either version 2, or (at your option) any
9  * later version.
10  *
11  * GNU Zebra is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; see the file COPYING; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #ifndef _ZEBRA_VTY_H
22 #define _ZEBRA_VTY_H
23 
24 #include <sys/types.h>
25 #ifdef HAVE_LIBPCREPOSIX
26 #include <pcreposix.h>
27 #else
28 #include <regex.h>
29 #endif /* HAVE_LIBPCREPOSIX */
30 
31 #include "thread.h"
32 #include "log.h"
33 #include "sockunion.h"
34 #include "qobj.h"
35 #include "compiler.h"
36 #include "northbound.h"
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 #define VTY_BUFSIZ 4096
43 #define VTY_MAXHIST 20
44 #define VTY_MAXDEPTH 8
45 
46 #define VTY_MAXCFGCHANGES 16
47 
48 struct vty_error {
49 	char error_buf[VTY_BUFSIZ];
50 	uint32_t line_num;
51 };
52 
53 struct vty_cfg_change {
54 	char xpath[XPATH_MAXLEN];
55 	enum nb_operation operation;
56 	const char *value;
57 };
58 
59 /* VTY struct. */
60 struct vty {
61 	/* File descripter of this vty. */
62 	int fd;
63 
64 	/* output FD, to support stdin/stdout combination */
65 	int wfd;
66 
67 	/* File output, used for VTYSH only */
68 	FILE *of;
69 	FILE *of_saved;
70 
71 	/* whether we are using pager or not */
72 	bool is_paged;
73 
74 	/* Is this vty connect to file or not */
75 	enum { VTY_TERM, VTY_FILE, VTY_SHELL, VTY_SHELL_SERV } type;
76 
77 	/* Node status of this vty */
78 	int node;
79 
80 	/* Failure count */
81 	int fail;
82 
83 	/* Output filer regex */
84 	bool filter;
85 	regex_t include;
86 
87 	/* Line buffer */
88 	struct buffer *lbuf;
89 
90 	/* Output buffer. */
91 	struct buffer *obuf;
92 
93 	/* Command input buffer */
94 	char *buf;
95 
96 	/* Command input error buffer */
97 	struct list *error;
98 
99 	/* Command cursor point */
100 	int cp;
101 
102 	/* Command length */
103 	int length;
104 
105 	/* Command max length. */
106 	int max;
107 
108 	/* Histry of command */
109 	char *hist[VTY_MAXHIST];
110 
111 	/* History lookup current point */
112 	int hp;
113 
114 	/* History insert end point */
115 	int hindex;
116 
117 	/* Changes enqueued to be applied in the candidate configuration. */
118 	size_t num_cfg_changes;
119 	struct vty_cfg_change cfg_changes[VTY_MAXCFGCHANGES];
120 
121 	/* XPath of the current node */
122 	int xpath_index;
123 	char xpath[VTY_MAXDEPTH][XPATH_MAXLEN];
124 
125 	/* In configure mode. */
126 	bool config;
127 
128 	/* Private candidate configuration mode. */
129 	bool private_config;
130 
131 	/* Candidate configuration. */
132 	struct nb_config *candidate_config;
133 
134 	/* Base candidate configuration. */
135 	struct nb_config *candidate_config_base;
136 
137 	/* Dynamic transaction information. */
138 	struct timeval backoff_start;
139 	size_t backoff_cmd_count;
140 	struct thread *t_pending_commit;
141 	char *pending_cmds_buf;
142 	size_t pending_cmds_buflen;
143 	size_t pending_cmds_bufpos;
144 
145 	/* Confirmed-commit timeout and rollback configuration. */
146 	struct thread *t_confirmed_commit_timeout;
147 	struct nb_config *confirmed_commit_rollback;
148 
149 	/* qobj object ID (replacement for "index") */
150 	uint64_t qobj_index;
151 
152 	/* qobj second-level object ID (replacement for "index_sub") */
153 	uint64_t qobj_index_sub;
154 
155 	/* For escape character. */
156 	unsigned char escape;
157 
158 	/* Current vty status. */
159 	enum { VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE } status;
160 
161 	/* IAC handling: was the last character received the
162 	   IAC (interpret-as-command) escape character (and therefore the next
163 	   character will be the command code)?  Refer to Telnet RFC 854. */
164 	unsigned char iac;
165 
166 	/* IAC SB (option subnegotiation) handling */
167 	unsigned char iac_sb_in_progress;
168 /* At the moment, we care only about the NAWS (window size) negotiation,
169    and that requires just a 5-character buffer (RFC 1073):
170      <NAWS char> <16-bit width> <16-bit height> */
171 #define TELNET_NAWS_SB_LEN 5
172 	unsigned char sb_buf[TELNET_NAWS_SB_LEN];
173 	/* How many subnegotiation characters have we received?  We just drop
174 	   those that do not fit in the buffer. */
175 	size_t sb_len;
176 
177 	/* Window width/height. */
178 	int width;
179 	int height;
180 
181 	/* Configure lines. */
182 	int lines;
183 
184 	/* Terminal monitor. */
185 	int monitor;
186 
187 	/* Read and write thread. */
188 	struct thread *t_read;
189 	struct thread *t_write;
190 
191 	/* Timeout seconds and thread. */
192 	unsigned long v_timeout;
193 	struct thread *t_timeout;
194 
195 	/* What address is this vty comming from. */
196 	char address[SU_ADDRSTRLEN];
197 
198 	/* "frame" output.  This is buffered and will be printed if some
199 	 * actual output follows, or will be discarded if the frame ends
200 	 * without any output. */
201 	size_t frame_pos;
202 	char frame[1024];
203 };
204 
vty_push_context(struct vty * vty,int node,uint64_t id)205 static inline void vty_push_context(struct vty *vty, int node, uint64_t id)
206 {
207 	vty->node = node;
208 	vty->qobj_index = id;
209 }
210 
211 /* note: VTY_PUSH_CONTEXT(..., NULL) doesn't work, since it will try to
212  * dereference "NULL->qobj_node.nid" */
213 #define VTY_PUSH_CONTEXT(nodeval, ptr)                                         \
214 	vty_push_context(vty, nodeval, QOBJ_ID_0SAFE(ptr))
215 #define VTY_PUSH_CONTEXT_NULL(nodeval) vty_push_context(vty, nodeval, 0ULL)
216 #define VTY_PUSH_CONTEXT_SUB(nodeval, ptr)                                     \
217 	do {                                                                   \
218 		vty->node = nodeval;                                           \
219 		/* qobj_index stays untouched */                               \
220 		vty->qobj_index_sub = QOBJ_ID_0SAFE(ptr);                      \
221 	} while (0)
222 
223 /* can return NULL if context is invalid! */
224 #define VTY_GET_CONTEXT(structname)                                            \
225 	QOBJ_GET_TYPESAFE(vty->qobj_index, structname)
226 #define VTY_GET_CONTEXT_SUB(structname)                                        \
227 	QOBJ_GET_TYPESAFE(vty->qobj_index_sub, structname)
228 
229 /* will return if ptr is NULL. */
230 #define VTY_CHECK_CONTEXT(ptr)                                                 \
231 	if (!ptr) {                                                            \
232 		vty_out(vty,                                                   \
233 			"Current configuration object was deleted "            \
234 			"by another process.\n");                              \
235 		return CMD_WARNING;                                            \
236 	}
237 
238 /* struct structname *ptr = <context>;   ptr will never be NULL. */
239 #define VTY_DECLVAR_CONTEXT(structname, ptr)                                   \
240 	struct structname *ptr = VTY_GET_CONTEXT(structname);                  \
241 	VTY_CHECK_CONTEXT(ptr);
242 #define VTY_DECLVAR_CONTEXT_SUB(structname, ptr)                               \
243 	struct structname *ptr = VTY_GET_CONTEXT_SUB(structname);              \
244 	VTY_CHECK_CONTEXT(ptr);
245 #define VTY_DECLVAR_INSTANCE_CONTEXT(structname, ptr)                          \
246 	if (vty->qobj_index == 0)                                              \
247 		return CMD_NOT_MY_INSTANCE;                                    \
248 	struct structname *ptr = VTY_GET_CONTEXT(structname);                  \
249 	VTY_CHECK_CONTEXT(ptr);
250 
251 /* XPath macros. */
252 #define VTY_PUSH_XPATH(nodeval, value)                                         \
253 	do {                                                                   \
254 		if (vty->xpath_index >= VTY_MAXDEPTH) {                        \
255 			vty_out(vty, "%% Reached maximum CLI depth (%u)\n",    \
256 				VTY_MAXDEPTH);                                 \
257 			return CMD_WARNING;                                    \
258 		}                                                              \
259 		vty->node = nodeval;                                           \
260 		strlcpy(vty->xpath[vty->xpath_index], value,                   \
261 			sizeof(vty->xpath[0]));                                \
262 		vty->xpath_index++;                                            \
263 	} while (0)
264 
265 #define VTY_CURR_XPATH vty->xpath[vty->xpath_index - 1]
266 
267 #define VTY_CHECK_XPATH                                                        \
268 	do {                                                                   \
269 		if (vty->type != VTY_FILE && !vty->private_config              \
270 		    && vty->xpath_index > 0                                    \
271 		    && !yang_dnode_exists(vty->candidate_config->dnode,        \
272 					  VTY_CURR_XPATH)) {                   \
273 			vty_out(vty,                                           \
274 				"Current configuration object was deleted "    \
275 				"by another process.\n\n");                    \
276 			return CMD_WARNING;                                    \
277 		}                                                              \
278 	} while (0)
279 
280 struct vty_arg {
281 	const char *name;
282 	const char *value;
283 	const char **argv;
284 	int argc;
285 };
286 
287 /* Integrated configuration file. */
288 #define INTEGRATE_DEFAULT_CONFIG "frr.conf"
289 
290 /* Default time out value */
291 #define VTY_TIMEOUT_DEFAULT 600
292 
293 /* Vty read buffer size. */
294 #define VTY_READ_BUFSIZ 512
295 
296 /* Directory separator. */
297 #ifndef DIRECTORY_SEP
298 #define DIRECTORY_SEP '/'
299 #endif /* DIRECTORY_SEP */
300 
301 #ifndef IS_DIRECTORY_SEP
302 #define IS_DIRECTORY_SEP(c) ((c) == DIRECTORY_SEP)
303 #endif
304 
305 /* Prototypes. */
306 extern void vty_init(struct thread_master *, bool do_command_logging);
307 extern void vty_init_vtysh(void);
308 extern void vty_terminate(void);
309 extern void vty_reset(void);
310 extern struct vty *vty_new(void);
311 extern struct vty *vty_stdio(void (*atclose)(int isexit));
312 
313 /* - vty_frame() output goes to a buffer (for context-begin markers)
314  * - vty_out() will first print this buffer, and clear it
315  * - vty_endframe() clears the buffer without printing it, and prints an
316  *   extra string if the buffer was empty before (for context-end markers)
317  */
318 extern int vty_out(struct vty *, const char *, ...) PRINTFRR(2, 3);
319 extern void vty_frame(struct vty *, const char *, ...) PRINTFRR(2, 3);
320 extern void vty_endframe(struct vty *, const char *);
321 bool vty_set_include(struct vty *vty, const char *regexp);
322 
323 extern bool vty_read_config(struct nb_config *config, const char *config_file,
324 			    char *config_default_dir);
325 extern void vty_time_print(struct vty *, int);
326 extern void vty_serv_sock(const char *, unsigned short, const char *);
327 extern void vty_close(struct vty *);
328 extern char *vty_get_cwd(void);
329 extern void vty_log(const char *level, const char *proto, const char *msg,
330 		    struct timestamp_control *);
331 extern int vty_config_enter(struct vty *vty, bool private_config,
332 			    bool exclusive);
333 extern void vty_config_exit(struct vty *);
334 extern int vty_config_node_exit(struct vty *);
335 extern int vty_shell(struct vty *);
336 extern int vty_shell_serv(struct vty *);
337 extern void vty_hello(struct vty *);
338 
339 /* ^Z / SIGTSTP handling */
340 extern void vty_stdio_suspend(void);
341 extern void vty_stdio_resume(void);
342 extern void vty_stdio_close(void);
343 
344 /* Send a fixed-size message to all vty terminal monitors; this should be
345    an async-signal-safe function. */
346 extern void vty_log_fixed(char *buf, size_t len);
347 
348 #ifdef __cplusplus
349 }
350 #endif
351 
352 #endif /* _ZEBRA_VTY_H */
353