/* * ***** BEGIN LICENSE BLOCK ***** Copyright (C) 2009-2016 Olof Hagsand and Benny Holmgren Copyright (C) 2017-2019 Olof Hagsand Copyright (C) 2020-2022 Olof Hagsand and Rubicon Communications, LLC(Netgate) This file is part of CLIXON. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Alternatively, the contents of this file may be used under the terms of the GNU General Public License Version 3 or later (the "GPL"), in which case the provisions of the GPL are applicable instead of those above. If you wish to allow use of your version of this file only under the terms of the GPL, and not to allow others to use your version of this file under the terms of Apache License version 2, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL. If you do not delete the provisions above, a recipient may use your version of this file under the terms of any one of the Apache License version 2 or the GPL. ***** END LICENSE BLOCK ***** * */ #ifdef HAVE_CONFIG_H #include "clixon_config.h" /* generated by config & autoconf */ #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* cligen */ #include /* clicon */ #include #include "clixon_cli_api.h" #include "cli_plugin.h" #include "cli_autocli.h" #include "cli_generate.h" #include "cli_common.h" #include "cli_handle.h" /* Command line options to be passed to getopt(3) */ #define CLI_OPTS "+hD:f:E:l:C:F:1a:u:d:m:qp:GLy:c:U:o:" /*! Check if there is a CLI history file and if so dump the CLI histiry to it * Just log if file does not exist or is not readable * @param[in] h CLICON handle */ static int cli_history_load(clicon_handle h) { int retval = -1; int lines; char *filename; FILE *f = NULL; wordexp_t result = {0,}; /* for tilde expansion */ /* Get history size from clixon option, if not use cligen default. */ if (clicon_option_exists(h, "CLICON_CLI_HIST_SIZE")) lines = clicon_option_int(h,"CLICON_CLI_HIST_SIZE"); else lines = CLIGEN_HISTSIZE_DEFAULT; /* Re-init history with clixon lines (1st time was w cligen defaults) */ if (cligen_hist_init(cli_cligen(h), lines) < 0) goto done; if ((filename = clicon_option_str(h,"CLICON_CLI_HIST_FILE")) == NULL) goto ok; /* ignore */ if (wordexp(filename, &result, 0) < 0){ clicon_err(OE_UNIX, errno, "wordexp"); goto done; } if ((f = fopen(result.we_wordv[0], "r")) == NULL){ clicon_log(LOG_DEBUG, "Warning: Could not open CLI history file for reading: %s: %s", result.we_wordv[0], strerror(errno)); goto ok; } if (cligen_hist_file_load(cli_cligen(h), f) < 0){ clicon_err(OE_UNIX, errno, "cligen_hist_file_load"); goto done; } ok: retval = 0; done: wordfree(&result); if (f) fclose(f); return retval; } /*! Start CLI history and load from file * Just log if file does not exist or is not readable * @param[in] h CLICON handle */ static int cli_history_save(clicon_handle h) { int retval = -1; char *filename; FILE *f = NULL; wordexp_t result = {0,}; /* for tilde expansion */ if ((filename = clicon_option_str(h, "CLICON_CLI_HIST_FILE")) == NULL) goto ok; /* ignore */ if (wordexp(filename, &result, 0) < 0){ clicon_err(OE_UNIX, errno, "wordexp"); goto done; } if ((f = fopen(result.we_wordv[0], "w+")) == NULL){ clicon_log(LOG_DEBUG, "Warning: Could not open CLI history file for writing: %s: %s", result.we_wordv[0], strerror(errno)); goto ok; } if (cligen_hist_file_save(cli_cligen(h), f) < 0){ clicon_err(OE_UNIX, errno, "cligen_hist_file_save"); goto done; } ok: retval = 0; done: wordfree(&result); if (f) fclose(f); return retval; } /*! Clean and close all state of cli process (but dont exit). * Cannot use h after this * @param[in] h Clixon handle */ static int cli_terminate(clicon_handle h) { yang_stmt *yspec; cvec *nsctx; cxobj *x; if (clixon_exit_get() == 0) clixon_exit_set(1); if (clicon_data_get(h, "session-transport", NULL) == 0) clicon_rpc_close_session(h); if ((yspec = clicon_dbspec_yang(h)) != NULL) ys_free(yspec); if ((yspec = clicon_config_yang(h)) != NULL) ys_free(yspec); if ((nsctx = clicon_nsctx_global_get(h)) != NULL) cvec_free(nsctx); if ((x = clicon_conf_xml(h)) != NULL) xml_free(x); clicon_data_cvec_del(h, "cli-edit-cvv");; clicon_data_cvec_del(h, "cli-edit-filter");; xpath_optimize_exit(); /* Delete all plugins, and RPC callbacks */ clixon_plugin_module_exit(h); /* Delete CLI syntax et al */ cli_plugin_finish(h); cli_history_save(h); cli_handle_exit(h); clixon_err_exit(); clicon_log_exit(); return 0; } /*! Unlink pidfile and quit */ static void cli_sig_term(int arg) { clicon_log(LOG_NOTICE, "%s: %u Terminated (killed by sig %d)", __PROGRAM__, getpid(), arg); exit(1); } /*! Setup signal handlers */ static int cli_signal_init (clicon_handle h) { int retval = -1; cli_signal_block(h); if (set_signal(SIGTERM, cli_sig_term, NULL) < 0){ clicon_err(OE_UNIX, errno, "Setting SIGTERM signal"); goto done; } if (set_signal(SIGPIPE, SIG_IGN, NULL) < 0){ clicon_err(OE_UNIX, errno, "Setting DIGPIPE signal"); goto done; } retval = 0; done: return retval; } /*! Interactive CLI command loop * @param[in] h CLICON handle * @retval 0 * @retval -1 * @see cligen_loop */ static int cli_interactive(clicon_handle h) { int retval = -1; char *cmd; char *new_mode; cligen_result result; int ret; pt_head *ph; /* Loop through all commands */ while(!cligen_exiting(cli_cligen(h))) { if ((ph = cligen_pt_head_active_get(cli_cligen(h))) == NULL){ clicon_err(OE_UNIX, 0, "active tree not found"); goto done; } new_mode = cligen_ph_name_get(ph); cmd = NULL; /* Read a CLI string, including expand handling */ if ((ret = clicon_cliread(h, ph, &cmd)) < 0) goto done; if (ret == 0) continue; if (cmd == NULL) { /* EOF */ cligen_exiting_set(cli_cligen(h), 1); continue; } /* Here errors are handled */ if (clicon_parse(h, cmd, &new_mode, &result, NULL) < 0) goto done; /* Why not check result? */ } retval = 0; done: return retval; } /*! Create pre-5.5 tree-refs for backward compatibility * should probably be moved to clispec default */ static int autocli_trees_default(clicon_handle h) { int retval = -1; cbuf *cb = NULL; int mode = 0; parse_tree *pt = NULL; pt_head *ph; /* Create backward compatible tree: @datamodel */ if ((ph = cligen_ph_add(cli_cligen(h), "datamodel")) == NULL) goto done; if ((pt = pt_new()) == NULL){ clicon_err(OE_UNIX, errno, "pt_new"); goto done; } if (clispec_parse_str(cli_cligen(h), "@basemodel, @remove:act-prekey, @remove:act-list, @remove:act-leafconst, @remove:ac-state;", "datamodel", NULL, pt, NULL) < 0) goto done; if (cligen_ph_parsetree_set(ph, pt) < 0) goto done; /* Create backward compatible tree: @datamodelshow */ if ((ph = cligen_ph_add(cli_cligen(h), "datamodelshow")) == NULL) goto done; if ((pt = pt_new()) == NULL){ clicon_err(OE_UNIX, errno, "pt_new"); goto done; } if (clispec_parse_str(cli_cligen(h), "@basemodel, @remove:act-leafvar, @remove:ac-state;", "datamodelshow", NULL, pt, NULL) < 0) goto done; if (cligen_ph_parsetree_set(ph, pt) < 0) goto done; /* Create backward compatible tree: @datamodelstate */ if ((ph = cligen_ph_add(cli_cligen(h), "datamodelstate")) == NULL) goto done; if ((pt = pt_new()) == NULL){ clicon_err(OE_UNIX, errno, "pt_new"); goto done; } if (clispec_parse_str(cli_cligen(h), "@basemodel, @remove:act-leafvar;", "datamodelstate", NULL, pt, NULL) < 0) goto done; if (cligen_ph_parsetree_set(ph, pt) < 0) goto done; /* Create new tree: @datamodelmode */ if ((ph = cligen_ph_add(cli_cligen(h), "datamodelmode")) == NULL) goto done; if ((pt = pt_new()) == NULL){ clicon_err(OE_UNIX, errno, "pt_new"); goto done; } if ((cb = cbuf_new()) == NULL){ clicon_err(OE_UNIX, errno, "cbuf_new"); goto done; } cprintf(cb, "@basemodel, @remove:act-prekey, @remove:act-leafconst, @remove:ac-state"); /* Check if container and list are allowed edit modes */ mode = 0; if (autocli_edit_mode(h, "container", &mode) < 0) goto done; if (mode == 0) cprintf(cb, ", @remove:act-container"); mode = 0; if (autocli_edit_mode(h, "listall", &mode) < 0) goto done; if (mode == 0) cprintf(cb, ", @remove:act-list"); mode = 0; if (autocli_edit_mode(h, "list", &mode) < 0) goto done; if (mode == 0) cprintf(cb, ", @remove:act-lastkey"); mode = 0; if (autocli_edit_mode(h, "leaf", &mode) < 0) goto done; if (mode == 0) cprintf(cb, ", @remove:ac-leaf"); cprintf(cb, ";"); if (clispec_parse_str(cli_cligen(h), cbuf_get(cb), "datamodelmode", NULL, pt, NULL) < 0) goto done; if (cligen_ph_parsetree_set(ph, pt) < 0) goto done; retval = 0; done: if (cb) cbuf_free(cb); return retval; } /*! Generate autocli, ie if enabled, generate clispec from YANG and add to cligen parse-trees * * Generate clispec (basemodel) from YANG dataspec and add to the set of cligen trees * This tree is referenced from the main CLI spec (CLICON_CLISPEC_DIR) using the * "tree reference" syntax. * * @param[in] h Clixon handle * @retval 0 OK * @retval -1 Error */ static int autocli_start(clicon_handle h) { int retval = -1; yang_stmt *yspec; int enable = 0; clicon_debug(1, "%s", __FUNCTION__); /* There is no single "enable-autocli" flag, * but set * false * with no rules: * enable * is disable */ if (autocli_module(h, NULL, &enable) < 0) goto done; if (!enable){ clicon_debug(1, "%s Autocli not enabled (clixon-autocli)", __FUNCTION__); goto ok; } /* Init yang2cli */ if (yang2cli_init(h) < 0) goto done; yspec = clicon_dbspec_yang(h); /* The actual generating call from yang to clispec for the complete yang spec, @basemodel */ if (yang2cli_yspec(h, yspec, AUTOCLI_TREENAME) < 0) goto done; /* XXX Create pre-5.5 tree-refs for backward compatibility */ if (autocli_trees_default(h) < 0) goto done; ok: retval = 0; done: return retval; } /*! Split remaining argv/argc into [commands] and [-- ] * * CLI command-line is: [options] [commands] [-- extra-options]" * The special argument "--" forces an end of option-scanning regardless of the scanning mode. * For example: clixon_cli -f /etc/clixon.xml show config -- -m clixon-mount * @þaram[in] h Clixon handle * @þaram[in] argc Input commands after * @þaram[in] argv Input commands after * @þaram[in] argv0 First command * @þaram[out] restcmd Commands * @retval 0 OK * @retval -1 Error */ static int options_split(clicon_handle h, char *argv0, int argc, char **argv, char **restcmd) { int retval = -1; int i; int j; /* Join rest of argv to a single command (but only up to -- ) */ for (i = 0; i < argc; i++){ if (strcmp(argv[i], "--") == 0) break; } if (i < argc){ /* '--' delimiter found */ *restcmd = clicon_strjoin(i, argv, " "); for (j=0; j \tDebug level\n" "\t-f \tConfig-file (mandatory)\n" "\t-E \tExtra configuration file directory\n" "\t-l > \tLog on (s)yslog, std(e)rr, std(o)ut, (n)one or (f)ile (stderr is default)\n" "\t-C \tDump configuration options on stdout after loading. Format is xml|json|text\n" "\t-F \tRead commands from file (default stdin)\n" "\t-1\t\tDo not enter interactive mode\n" "\t-a UNIX|IPv4|IPv6\tInternal backend socket family\n" "\t-u \tInternal socket domain path or IP addr (see -a)\n" "\t-d \tSpecify plugin directory (default: %s)\n" "\t-m \tSpecify plugin syntax mode\n" "\t-q \t\tQuiet mode, dont print greetings or prompt, terminate on ctrl-C\n" "\t-p \tYang directory path (see CLICON_YANG_DIR)\n" "\t-G \t\tPrint auto-cli CLI syntax generated from YANG\n" "\t-L \t\tDebug print dynamic CLI syntax including completions and expansions\n" "\t-y \tOverride yang spec file (dont include .yang suffix)\n" "\t-c \tSpecify cli spec file.\n" "\t-U \tOver-ride unix user with a pseudo user for NACM.\n" "\t-o \"