/* * ***** BEGIN LICENSE BLOCK ***** Copyright (C) 2009-2019 Olof Hagsand and Benny Holmgren 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 #include #include #include #include /* cligen */ #include /* clicon */ #include #include "clixon_backend_handle.h" #include "backend_socket.h" #include "backend_client.h" #include "backend_plugin.h" #include "backend_commit.h" #include "backend_handle.h" #include "backend_startup.h" /* Command line options to be passed to getopt(3) */ #define BACKEND_OPTS "hD:f:l:d:p:b:Fza:u:P:1s:c:U:g:y:o:" #define BACKEND_LOGFILE "/usr/local/var/clixon_backend.log" /*! Clean and close all state of backend (but dont exit). * Cannot use h after this * @param[in] h Clixon handle */ static int backend_terminate(clicon_handle h) { yang_stmt *yspec; char *pidfile = clicon_backend_pidfile(h); int sockfamily = clicon_sock_family(h); char *sockpath = clicon_sock(h); cxobj *x; struct stat st; int ss; clicon_debug(1, "%s", __FUNCTION__); if ((ss = clicon_socket_get(h)) != -1) close(ss); /* Disconnect datastore */ xmldb_disconnect(h); /* Clear module state caches */ if ((x = clicon_modst_cache_get(h, 0)) != NULL) xml_free(x); if ((x = clicon_modst_cache_get(h, 1)) != NULL) xml_free(x); /* Free changelog */ if ((x = clicon_xml_changelog_get(h)) != NULL) xml_free(x); if ((yspec = clicon_dbspec_yang(h)) != NULL) yspec_free(yspec); if ((yspec = clicon_config_yang(h)) != NULL) yspec_free(yspec); if ((x = clicon_nacm_ext(h)) != NULL) xml_free(x); if ((x = clicon_conf_xml(h)) != NULL) xml_free(x); stream_publish_exit(); clixon_plugin_exit(h); /* Delete all backend plugin RPC callbacks */ rpc_callback_delete_all(h); /* Delete all backend plugin upgrade callbacks */ upgrade_callback_delete_all(h); if (pidfile) unlink(pidfile); if (sockfamily==AF_UNIX && lstat(sockpath, &st) == 0) unlink(sockpath); backend_handle_exit(h); /* Also deletes streams. Cannot use h after this. */ event_exit(); clicon_debug(1, "%s done", __FUNCTION__); clicon_log_exit(); return 0; } /*! Unlink pidfile and quit */ static void backend_sig_term(int arg) { static int i=0; if (i++ == 0) clicon_log(LOG_NOTICE, "%s: %s: pid: %u Signal %d", __PROGRAM__, __FUNCTION__, getpid(), arg); clicon_exit_set(); /* checked in event_loop() */ } /*! Create backend server socket and register callback * @param[in] h Clicon handle * @retval s Server socket file descriptor (see socket(2)) * @retval -1 Error */ static int backend_server_socket(clicon_handle h) { int ss; /* Open control socket */ if ((ss = backend_socket_init(h)) < 0) return -1; /* ss is a server socket that the clients connect to. The callback therefore accepts clients on ss */ if (event_reg_fd(ss, backend_accept_client, h, "server socket") < 0) { close(ss); return -1; } return ss; } /*! Load external NACM file */ static int nacm_load_external(clicon_handle h) { int retval = -1; char *filename; /* NACM config file */ yang_stmt *yspec = NULL; cxobj *xt = NULL; struct stat st; FILE *f = NULL; int fd; filename = clicon_option_str(h, "CLICON_NACM_FILE"); if (filename == NULL || strlen(filename)==0){ clicon_err(OE_UNIX, errno, "CLICON_NACM_FILE not set in NACM external mode"); goto done; } if (stat(filename, &st) < 0){ clicon_err(OE_UNIX, errno, "%s", filename); goto done; } if (!S_ISREG(st.st_mode)){ clicon_err(OE_UNIX, 0, "%s is not a regular file", filename); goto done; } if ((f = fopen(filename, "r")) == NULL) { clicon_err(OE_UNIX, errno, "configure file: %s", filename); return -1; } if ((yspec = yspec_new()) == NULL) goto done; if (yang_spec_parse_module(h, "ietf-netconf-acm", NULL, yspec) < 0) goto done; fd = fileno(f); /* Read configfile */ if (xml_parse_file(fd, "", yspec, &xt) < 0) goto done; if (xt == NULL){ clicon_err(OE_XML, 0, "No xml tree in %s", filename); goto done; } if (clicon_nacm_ext_set(h, xt) < 0) goto done; retval = 0; done: if (yspec) /* The clixon yang-spec is not used after this */ yspec_free(yspec); if (f) fclose(f); return retval; } static int xmldb_drop_priv(clicon_handle h, const char *db, uid_t uid, gid_t gid) { int retval = -1; char *filename = NULL; if (xmldb_db2file(h, db, &filename) < 0) goto done; if (chown(filename, uid, gid) < 0){ clicon_err(OE_UNIX, errno, "chown"); goto done; } retval = 0; done: if (filename) free(filename); return retval; } /*! Drop root privileges uid and gid to Clixon user/group * @param[in] h Clicon handle */ static int drop_priv(clicon_handle h, uid_t uid, gid_t gid) { int retval = -1; if (xmldb_exists(h, "running") != 1) if (xmldb_create(h, "running") < 0) goto done; if (xmldb_drop_priv(h, "running", uid, gid) < 0) goto done; if (xmldb_exists(h, "candidate") != 1) if (xmldb_create(h, "candidate") < 0) goto done; if (xmldb_drop_priv(h, "candidate", uid, gid) < 0) goto done; if (xmldb_exists(h, "startup") != 1) if (xmldb_create(h, "startup") < 0) goto done; if (xmldb_drop_priv(h, "startup", uid, gid) < 0) goto done; if (setgid(gid) == -1) { clicon_err(OE_DEMON, errno, "setgid %d", gid); goto done; } if (setuid(uid) == -1) { clicon_err(OE_DEMON, errno, "setuid %d", uid); goto done; } /* Verify you cannot regain root privileges */ if (setuid(0) != -1){ clicon_err(OE_DEMON, EPERM, "Could regain root privilieges"); goto done; } retval = 0; done: return retval; } /*! Given a retval, transform to status or fatal error * * @param[in] ret Return value from xml validation function * @param[out] status Transform status according to rules below * @retval 0 OK, status set * @retval -1 Fatal error outside scope of startup_status * Transformation rules: * 1) retval -1 assume clicon_errno/suberrno set. Special case from xml parser * is clicon_suberrno = XMLPARSE_ERRNO which assumes an XML (non-fatal) parse * error which translates to -> STARTUP_ERR * All other error cases translates to fatal error * 2) retval 0 is xml validation fails -> STARTUP_INVALID * 3) retval 1 is OK -> STARTUP_OK * 4) any other retval translates to fatal error */ static int ret2status(int ret, enum startup_status *status) { int retval = -1; switch (ret){ case -1: if (clicon_suberrno != XMLPARSE_ERRNO) goto done; clicon_err_reset(); *status = STARTUP_ERR; break; case 0: *status = STARTUP_INVALID; break; case 1: *status = STARTUP_OK; break; default: clicon_err(OE_CFG, EINVAL, "No such retval %d", retval); } /* switch */ retval = 0; done: return retval; } /*! usage */ static void usage(clicon_handle h, char *argv0) { char *plgdir = clicon_backend_dir(h); char *confsock = clicon_sock(h); char *confpid = clicon_backend_pidfile(h); char *group = clicon_sock_group(h); fprintf(stderr, "usage:%s *\n" "where options are\n" "\t-h\t\tHelp\n" "\t-D \tDebug level\n" "\t-f \tCLICON config file\n" "\t-l (s|e|o|f) Log on (s)yslog, std(e)rr or std(o)ut (stderr is default) Only valid if -F, if background syslog is on syslog.\n" "\t-d \tSpecify backend plugin directory (default: %s)\n" "\t-p \tYang directory path (see CLICON_YANG_DIR)\n" "\t-b \tSpecify XMLDB database directory\n" "\t-F\t\tRun in foreground, do not run as daemon\n" "\t-z\t\tKill other config daemon and exit\n" "\t-a UNIX|IPv4|IPv6 Internal backend socket family\n" "\t-u \tInternal socket domain path or IP addr (see -a)(default: %s)\n" "\t-P \tPid filename (default: %s)\n" "\t-1\t\tRun once and then quit (dont wait for events)\n" "\t-s \tSpecify backend startup mode: none|startup|running|init)\n" "\t-c \tLoad extra xml configuration, but don't commit.\n" "\t-U \tRun backend daemon as this user\n" "\t-g \tClient membership required to this group (default: %s)\n" "\t-y \tLoad yang spec file (override yang main module)\n" "\t-o \"