]> git.proxmox.com Git - mirror_frr.git/blob - lib/vty.h
Merge pull request #13649 from donaldsharp/unlock_the_node_or_else
[mirror_frr.git] / lib / vty.h
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Virtual terminal [aka TeletYpe] interface routine
3 * Copyright (C) 1997 Kunihiro Ishiguro
4 */
5
6 #ifndef _ZEBRA_VTY_H
7 #define _ZEBRA_VTY_H
8
9 #include <sys/types.h>
10 #ifdef HAVE_LIBPCRE2_POSIX
11 #ifndef _FRR_PCRE2_POSIX
12 #define _FRR_PCRE2_POSIX
13 #include <pcre2posix.h>
14 #endif /* _FRR_PCRE2_POSIX */
15 #elif defined(HAVE_LIBPCREPOSIX)
16 #include <pcreposix.h>
17 #else
18 #include <regex.h>
19 #endif /* HAVE_LIBPCRE2_POSIX */
20
21 #include "frrevent.h"
22 #include "log.h"
23 #include "sockunion.h"
24 #include "qobj.h"
25 #include "compiler.h"
26 #include "northbound.h"
27 #include "zlog_live.h"
28 #include "libfrr.h"
29 #include "mgmt_fe_client.h"
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 struct json_object;
36
37 #define VTY_BUFSIZ 4096
38 #define VTY_MAXHIST 20
39 #define VTY_MAXDEPTH 8
40
41 #define VTY_MAXCFGCHANGES 16
42
43 struct vty_error {
44 char error_buf[VTY_BUFSIZ];
45 uint32_t line_num;
46 int cmd_ret;
47 };
48
49 struct vty_cfg_change {
50 char xpath[XPATH_MAXLEN];
51 enum nb_operation operation;
52 const char *value;
53 };
54
55 PREDECL_DLIST(vtys);
56
57 /* VTY struct. */
58 struct vty {
59 struct vtys_item itm;
60
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, /* telnet conn or stdin/stdout UI */
76 VTY_FILE, /* reading and writing config files */
77 VTY_SHELL, /* vtysh client side UI */
78 VTY_SHELL_SERV, /* server-side vtysh connection */
79 } type;
80
81 /* Node status of this vty */
82 int node;
83
84 /* Failure count */
85 int fail;
86
87 /* Output filer regex */
88 bool filter;
89 regex_t include;
90
91 /* Line buffer */
92 struct buffer *lbuf;
93
94 /* Output buffer. */
95 struct buffer *obuf;
96
97 /* Command input buffer */
98 char *buf;
99
100 /* Command input error buffer */
101 struct list *error;
102
103 /* Command cursor point */
104 int cp;
105
106 /* Command length */
107 int length;
108
109 /* Command max length. */
110 int max;
111
112 /* Histry of command */
113 char *hist[VTY_MAXHIST];
114
115 /* History lookup current point */
116 int hp;
117
118 /* History insert end point */
119 int hindex;
120
121 /* Changes enqueued to be applied in the candidate configuration. */
122 size_t num_cfg_changes;
123 struct nb_cfg_change cfg_changes[VTY_MAXCFGCHANGES];
124
125 /* XPath of the current node */
126 int xpath_index;
127 char xpath[VTY_MAXDEPTH][XPATH_MAXLEN];
128
129 /*
130 * Keep track of how many SET_CFG requests has been sent so far that
131 * has not been committed yet.
132 */
133 size_t mgmt_num_pending_setcfg;
134
135 /* In configure mode. */
136 bool config;
137
138 /* Private candidate configuration mode. */
139 bool private_config;
140
141 /* Candidate configuration. */
142 struct nb_config *candidate_config;
143
144 /* Base candidate configuration. */
145 struct nb_config *candidate_config_base;
146
147 /* Dynamic transaction information. */
148 bool pending_allowed;
149 bool pending_commit;
150 bool no_implicit_commit;
151 char *pending_cmds_buf;
152 size_t pending_cmds_buflen;
153 size_t pending_cmds_bufpos;
154
155 /* Confirmed-commit timeout and rollback configuration. */
156 struct event *t_confirmed_commit_timeout;
157 struct nb_config *confirmed_commit_rollback;
158
159 /* qobj object ID (replacement for "index") */
160 uint64_t qobj_index;
161
162 /* qobj second-level object ID (replacement for "index_sub") */
163 uint64_t qobj_index_sub;
164
165 /* For escape character. */
166 unsigned char escape;
167
168 /* Current vty status. */
169 enum {
170 VTY_NORMAL,
171 VTY_CLOSE,
172 VTY_MORE,
173 VTY_MORELINE,
174 VTY_PASSFD,
175 } status;
176
177 /* vtysh socket/fd passing (for terminal monitor) */
178 int pass_fd;
179
180 /* CLI command return value (likely CMD_SUCCESS) when pass_fd != -1 */
181 uint8_t pass_fd_status[4];
182
183 /* live logging target / terminal monitor */
184 struct zlog_live_cfg live_log;
185
186 /* IAC handling: was the last character received the
187 IAC (interpret-as-command) escape character (and therefore the next
188 character will be the command code)? Refer to Telnet RFC 854. */
189 unsigned char iac;
190
191 /* IAC SB (option subnegotiation) handling */
192 unsigned char iac_sb_in_progress;
193 /* At the moment, we care only about the NAWS (window size) negotiation,
194 and that requires just a 5-character buffer (RFC 1073):
195 <NAWS char> <16-bit width> <16-bit height> */
196 #define TELNET_NAWS_SB_LEN 5
197 unsigned char sb_buf[TELNET_NAWS_SB_LEN];
198 /* How many subnegotiation characters have we received? We just drop
199 those that do not fit in the buffer. */
200 size_t sb_len;
201
202 /* Window width/height. */
203 int width;
204 int height;
205
206 /* Configure lines. */
207 int lines;
208
209 /* Read and write thread. */
210 struct event *t_read;
211 struct event *t_write;
212
213 /* Timeout seconds and thread. */
214 unsigned long v_timeout;
215 struct event *t_timeout;
216
217 /* What address is this vty comming from. */
218 char address[SU_ADDRSTRLEN];
219
220 /* "frame" output. This is buffered and will be printed if some
221 * actual output follows, or will be discarded if the frame ends
222 * without any output. */
223 size_t frame_pos;
224 char frame[1024];
225
226 uint64_t mgmt_session_id; /* FE adapter identifies session w/ this */
227 uint64_t mgmt_client_id; /* FE vty client identifies w/ this ID */
228 uint64_t mgmt_req_id;
229 /* set when we have sent mgmtd a *REQ command in response to some vty
230 * CLI command and we are waiting on the reply so we can respond to the
231 * vty user. */
232 bool mgmt_req_pending;
233 bool mgmt_locked_candidate_ds;
234 };
235
236 static inline void vty_push_context(struct vty *vty, int node, uint64_t id)
237 {
238 vty->node = node;
239 vty->qobj_index = id;
240 }
241
242 /* note: VTY_PUSH_CONTEXT(..., NULL) doesn't work, since it will try to
243 * dereference "NULL->qobj_node.nid" */
244 #define VTY_PUSH_CONTEXT(nodeval, ptr) \
245 vty_push_context(vty, nodeval, QOBJ_ID_0SAFE(ptr))
246 #define VTY_PUSH_CONTEXT_NULL(nodeval) vty_push_context(vty, nodeval, 0ULL)
247 #define VTY_PUSH_CONTEXT_SUB(nodeval, ptr) \
248 do { \
249 vty->node = nodeval; \
250 /* qobj_index stays untouched */ \
251 vty->qobj_index_sub = QOBJ_ID_0SAFE(ptr); \
252 } while (0)
253
254 /* can return NULL if context is invalid! */
255 #define VTY_GET_CONTEXT(structname) \
256 QOBJ_GET_TYPESAFE(vty->qobj_index, structname)
257 #define VTY_GET_CONTEXT_SUB(structname) \
258 QOBJ_GET_TYPESAFE(vty->qobj_index_sub, structname)
259
260 /* will return if ptr is NULL. */
261 #define VTY_CHECK_CONTEXT(ptr) \
262 if (!ptr) { \
263 vty_out(vty, \
264 "Current configuration object was deleted " \
265 "by another process.\n"); \
266 return CMD_WARNING; \
267 }
268
269 /* struct structname *ptr = <context>; ptr will never be NULL. */
270 #define VTY_DECLVAR_CONTEXT(structname, ptr) \
271 struct structname *ptr = VTY_GET_CONTEXT(structname); \
272 VTY_CHECK_CONTEXT(ptr);
273 #define VTY_DECLVAR_CONTEXT_SUB(structname, ptr) \
274 struct structname *ptr = VTY_GET_CONTEXT_SUB(structname); \
275 VTY_CHECK_CONTEXT(ptr);
276 #define VTY_DECLVAR_INSTANCE_CONTEXT(structname, ptr) \
277 if (vty->qobj_index == 0) \
278 return CMD_NOT_MY_INSTANCE; \
279 struct structname *ptr = VTY_GET_CONTEXT(structname); \
280 VTY_CHECK_CONTEXT(ptr);
281
282 #define VTY_DECLVAR_CONTEXT_VRF(vrfptr) \
283 struct vrf *vrfptr; \
284 if (vty->node == CONFIG_NODE) \
285 vrfptr = vrf_lookup_by_id(VRF_DEFAULT); \
286 else \
287 vrfptr = VTY_GET_CONTEXT(vrf); \
288 VTY_CHECK_CONTEXT(vrfptr); \
289 MACRO_REQUIRE_SEMICOLON() /* end */
290
291 /* XPath macros. */
292 #define VTY_PUSH_XPATH(nodeval, value) \
293 do { \
294 if (vty->xpath_index >= VTY_MAXDEPTH) { \
295 vty_out(vty, "%% Reached maximum CLI depth (%u)\n", \
296 VTY_MAXDEPTH); \
297 return CMD_WARNING; \
298 } \
299 vty->node = nodeval; \
300 strlcpy(vty->xpath[vty->xpath_index], value, \
301 sizeof(vty->xpath[0])); \
302 vty->xpath_index++; \
303 } while (0)
304
305 #define VTY_CURR_XPATH vty->xpath[vty->xpath_index - 1]
306
307 #define VTY_CHECK_XPATH \
308 do { \
309 if (vty->type != VTY_FILE && !vty->private_config && \
310 vty->xpath_index > 0 && \
311 !yang_dnode_exists(vty->candidate_config->dnode, \
312 VTY_CURR_XPATH)) { \
313 vty_out(vty, \
314 "Current configuration object was deleted " \
315 "by another process.\n\n"); \
316 return CMD_WARNING; \
317 } \
318 } while (0)
319
320 struct vty_arg {
321 const char *name;
322 const char *value;
323 const char **argv;
324 int argc;
325 };
326
327 /* Integrated configuration file. */
328 #define INTEGRATE_DEFAULT_CONFIG "frr.conf"
329
330 /* Default time out value */
331 #define VTY_TIMEOUT_DEFAULT 600
332
333 /* Vty read buffer size. */
334 #define VTY_READ_BUFSIZ 512
335
336 /* Directory separator. */
337 #ifndef DIRECTORY_SEP
338 #define DIRECTORY_SEP '/'
339 #endif /* DIRECTORY_SEP */
340
341 #ifndef IS_DIRECTORY_SEP
342 #define IS_DIRECTORY_SEP(c) ((c) == DIRECTORY_SEP)
343 #endif
344
345 extern struct nb_config *vty_mgmt_candidate_config;
346 extern bool vty_log_commands;
347
348 extern char const *const mgmt_daemons[];
349 extern uint mgmt_daemons_count;
350
351 /* Prototypes. */
352 extern void vty_init(struct event_loop *m, bool do_command_logging);
353 extern void vty_init_vtysh(void);
354 extern void vty_terminate(void);
355 extern void vty_reset(void);
356 extern struct vty *vty_new(void);
357 extern struct vty *vty_stdio(void (*atclose)(int isexit));
358
359 /* - vty_frame() output goes to a buffer (for context-begin markers)
360 * - vty_out() will first print this buffer, and clear it
361 * - vty_endframe() clears the buffer without printing it, and prints an
362 * extra string if the buffer was empty before (for context-end markers)
363 */
364 extern int vty_out(struct vty *, const char *, ...) PRINTFRR(2, 3);
365 extern void vty_frame(struct vty *, const char *, ...) PRINTFRR(2, 3);
366 extern void vty_endframe(struct vty *, const char *);
367 extern bool vty_set_include(struct vty *vty, const char *regexp);
368 /* returns CMD_SUCCESS so you can do a one-line "return vty_json(...)"
369 * NULL check and json_object_free() is included.
370 *
371 * _no_pretty means do not add a bunch of newlines and dump the output
372 * as densely as possible.
373 */
374 extern int vty_json(struct vty *vty, struct json_object *json);
375 extern int vty_json_no_pretty(struct vty *vty, struct json_object *json);
376 extern void vty_json_empty(struct vty *vty);
377 /* post fd to be passed to the vtysh client
378 * fd is owned by the VTY code after this and will be closed when done
379 */
380 extern void vty_pass_fd(struct vty *vty, int fd);
381
382 extern FILE *vty_open_config(const char *config_file, char *config_default_dir);
383 extern bool vty_read_config(struct nb_config *config, const char *config_file,
384 char *config_default_dir);
385 extern void vty_read_file(struct nb_config *config, FILE *confp);
386 extern void vty_read_file_finish(struct vty *vty, struct nb_config *config);
387 extern void vty_time_print(struct vty *, int);
388 extern void vty_serv_start(const char *, unsigned short, const char *);
389 extern void vty_serv_stop(void);
390 extern void vty_close(struct vty *);
391 extern char *vty_get_cwd(void);
392 extern void vty_update_xpath(const char *oldpath, const char *newpath);
393 extern int vty_config_enter(struct vty *vty, bool private_config,
394 bool exclusive);
395 extern void vty_config_exit(struct vty *);
396 extern int vty_config_node_exit(struct vty *);
397 extern int vty_shell(struct vty *);
398 extern int vty_shell_serv(struct vty *);
399 extern void vty_hello(struct vty *);
400
401 /* ^Z / SIGTSTP handling */
402 extern void vty_stdio_suspend(void);
403 extern void vty_stdio_resume(void);
404 extern void vty_stdio_close(void);
405
406 extern void vty_init_mgmt_fe(void);
407 extern bool vty_mgmt_fe_enabled(void);
408 extern bool vty_mgmt_should_process_cli_apply_changes(struct vty *vty);
409
410 extern bool mgmt_vty_read_configs(void);
411 extern int vty_mgmt_send_config_data(struct vty *vty);
412 extern int vty_mgmt_send_commit_config(struct vty *vty, bool validate_only,
413 bool abort);
414 extern int vty_mgmt_send_get_config(struct vty *vty,
415 Mgmtd__DatastoreId datastore,
416 const char **xpath_list, int num_req);
417 extern int vty_mgmt_send_get_data(struct vty *vty, Mgmtd__DatastoreId datastore,
418 const char **xpath_list, int num_req);
419 extern int vty_mgmt_send_lockds_req(struct vty *vty, Mgmtd__DatastoreId ds_id,
420 bool lock);
421 extern void vty_mgmt_resume_response(struct vty *vty, bool success);
422
423 static inline bool vty_needs_implicit_commit(struct vty *vty)
424 {
425 return (frr_get_cli_mode() == FRR_CLI_CLASSIC
426 ? ((vty->pending_allowed || vty->no_implicit_commit)
427 ? false
428 : true)
429 : false);
430 }
431
432 #ifdef __cplusplus
433 }
434 #endif
435
436 #endif /* _ZEBRA_VTY_H */