1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Virtual terminal [aka TeletYpe] interface routine
3 * Copyright (C) 1997 Kunihiro Ishiguro
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>
19 #endif /* HAVE_LIBPCRE2_POSIX */
23 #include "sockunion.h"
26 #include "northbound.h"
27 #include "zlog_live.h"
29 #include "mgmt_fe_client.h"
37 #define VTY_BUFSIZ 4096
38 #define VTY_MAXHIST 20
39 #define VTY_MAXDEPTH 8
41 #define VTY_MAXCFGCHANGES 16
44 char error_buf
[VTY_BUFSIZ
];
49 struct vty_cfg_change
{
50 char xpath
[XPATH_MAXLEN
];
51 enum nb_operation operation
;
61 /* File descripter of this vty. */
64 /* output FD, to support stdin/stdout combination */
67 /* File output, used for VTYSH only */
71 /* whether we are using pager or not */
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 */
81 /* Node status of this vty */
87 /* Output filer regex */
97 /* Command input buffer */
100 /* Command input error buffer */
103 /* Command cursor point */
109 /* Command max length. */
112 /* Histry of command */
113 char *hist
[VTY_MAXHIST
];
115 /* History lookup current point */
118 /* History insert end point */
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
];
125 /* XPath of the current node */
127 char xpath
[VTY_MAXDEPTH
][XPATH_MAXLEN
];
130 * Keep track of how many SET_CFG requests has been sent so far that
131 * has not been committed yet.
133 size_t mgmt_num_pending_setcfg
;
135 /* In configure mode. */
138 /* Private candidate configuration mode. */
141 /* Candidate configuration. */
142 struct nb_config
*candidate_config
;
144 /* Base candidate configuration. */
145 struct nb_config
*candidate_config_base
;
147 /* Dynamic transaction information. */
148 bool pending_allowed
;
150 bool no_implicit_commit
;
151 char *pending_cmds_buf
;
152 size_t pending_cmds_buflen
;
153 size_t pending_cmds_bufpos
;
155 /* Confirmed-commit timeout and rollback configuration. */
156 struct event
*t_confirmed_commit_timeout
;
157 struct nb_config
*confirmed_commit_rollback
;
159 /* qobj object ID (replacement for "index") */
162 /* qobj second-level object ID (replacement for "index_sub") */
163 uint64_t qobj_index_sub
;
165 /* For escape character. */
166 unsigned char escape
;
168 /* Current vty status. */
177 /* vtysh socket/fd passing (for terminal monitor) */
180 /* CLI command return value (likely CMD_SUCCESS) when pass_fd != -1 */
181 uint8_t pass_fd_status
[4];
183 /* live logging target / terminal monitor */
184 struct zlog_live_cfg live_log
;
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. */
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. */
202 /* Window width/height. */
206 /* Configure lines. */
209 /* Read and write thread. */
210 struct event
*t_read
;
211 struct event
*t_write
;
213 /* Timeout seconds and thread. */
214 unsigned long v_timeout
;
215 struct event
*t_timeout
;
217 /* What address is this vty comming from. */
218 char address
[SU_ADDRSTRLEN
];
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. */
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
232 bool mgmt_req_pending
;
233 bool mgmt_locked_candidate_ds
;
236 static inline void vty_push_context(struct vty
*vty
, int node
, uint64_t id
)
239 vty
->qobj_index
= id
;
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) \
249 vty->node = nodeval; \
250 /* qobj_index stays untouched */ \
251 vty->qobj_index_sub = QOBJ_ID_0SAFE(ptr); \
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)
260 /* will return if ptr is NULL. */
261 #define VTY_CHECK_CONTEXT(ptr) \
264 "Current configuration object was deleted " \
265 "by another process.\n"); \
266 return CMD_WARNING; \
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);
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); \
287 vrfptr = VTY_GET_CONTEXT(vrf); \
288 VTY_CHECK_CONTEXT(vrfptr); \
289 MACRO_REQUIRE_SEMICOLON() /* end */
292 #define VTY_PUSH_XPATH(nodeval, value) \
294 if (vty->xpath_index >= VTY_MAXDEPTH) { \
295 vty_out(vty, "%% Reached maximum CLI depth (%u)\n", \
297 return CMD_WARNING; \
299 vty->node = nodeval; \
300 strlcpy(vty->xpath[vty->xpath_index], value, \
301 sizeof(vty->xpath[0])); \
302 vty->xpath_index++; \
305 #define VTY_CURR_XPATH vty->xpath[vty->xpath_index - 1]
307 #define VTY_CHECK_XPATH \
309 if (vty->type != VTY_FILE && !vty->private_config && \
310 vty->xpath_index > 0 && \
311 !yang_dnode_exists(vty->candidate_config->dnode, \
314 "Current configuration object was deleted " \
315 "by another process.\n\n"); \
316 return CMD_WARNING; \
327 /* Integrated configuration file. */
328 #define INTEGRATE_DEFAULT_CONFIG "frr.conf"
330 /* Default time out value */
331 #define VTY_TIMEOUT_DEFAULT 600
333 /* Vty read buffer size. */
334 #define VTY_READ_BUFSIZ 512
336 /* Directory separator. */
337 #ifndef DIRECTORY_SEP
338 #define DIRECTORY_SEP '/'
339 #endif /* DIRECTORY_SEP */
341 #ifndef IS_DIRECTORY_SEP
342 #define IS_DIRECTORY_SEP(c) ((c) == DIRECTORY_SEP)
345 extern struct nb_config
*vty_mgmt_candidate_config
;
346 extern bool vty_log_commands
;
348 extern char const *const mgmt_daemons
[];
349 extern uint mgmt_daemons_count
;
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
));
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)
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.
371 * _no_pretty means do not add a bunch of newlines and dump the output
372 * as densely as possible.
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
380 extern void vty_pass_fd(struct vty
*vty
, int fd
);
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
,
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
*);
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);
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
);
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
,
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
,
421 extern void vty_mgmt_resume_response(struct vty
*vty
, bool success
);
423 static inline bool vty_needs_implicit_commit(struct vty
*vty
)
425 return (frr_get_cli_mode() == FRR_CLI_CLASSIC
426 ? ((vty
->pending_allowed
|| vty
->no_implicit_commit
)
436 #endif /* _ZEBRA_VTY_H */