/* * ***** BEGIN LICENSE BLOCK ***** Copyright (C) 2009-2016 Olof Hagsand and Benny Holmgren Copyright (C) 2017-2019 Olof Hagsand Copyright (C) 2020 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 #include #include #include /* cligen */ #include /* clicon */ #include #include "clixon_netconf.h" #include "netconf_lib.h" #include "netconf_rpc.h" /* Command line options to be passed to getopt(3) */ #define NETCONF_OPTS "hD:f:E:l:qa:u:d:p:y:U:t:eo:" #define NETCONF_LOGFILE "/tmp/clixon_netconf.log" /*! Ignore errors on packet errors: continue */ static int ignore_packet_errors = 1; static int netconf_hello(clicon_handle h, cxobj *xn) { #ifdef nyi cxobj *x; x = NULL; while ((x = xpath_each(xn, "//capability", x)) != NULL) { } #endif return 0; } /*! Process incoming Netconf RPC netconf message * @param[in] h Clicon handle * @param[in] xreq XML tree containing netconf RPC message * @param[in] yspec YANG spec * @retval 0 OK * @retval -1 Error */ int netconf_rpc_message(clicon_handle h, cxobj *xrpc, yang_stmt *yspec) { int retval = -1; cxobj *xret = NULL; /* Return (out) */ int ret; cbuf *cbret = NULL; cxobj *xc; cxobj *xa; cxobj *xa2; if ((ret = xml_bind_yang_rpc(xrpc, yspec, &xret)) < 0) goto done; if (ret > 0 && (ret = xml_yang_validate_rpc(h, xrpc, &xret)) < 0) goto done; if (ret == 0){ if ((cbret = cbuf_new()) == NULL){ clicon_err(LOG_ERR, errno, "cbuf_new"); goto done; } clicon_xml2cbuf(cbret, xret, 0, 0, -1); netconf_output_encap(1, cbret, "rpc-error"); goto ok; } if (netconf_rpc_dispatch(h, xrpc, &xret) < 0){ goto done; } else{ /* there is a return message in xret */ if (xret == NULL){ if ((cbret = cbuf_new()) == NULL){ clicon_err(LOG_ERR, errno, "cbuf_new"); goto done; } if (netconf_operation_failed(cbret, "rpc", "Internal error: no xml return")< 0) goto done; netconf_output_encap(1, cbret, "rpc-error"); goto done; } if ((xc = xml_child_i(xret, 0))!=NULL){ xa=NULL; /* Copy message-id attribute from incoming to reply. * RFC 6241: * If additional attributes are present in an element, a NETCONF * peer MUST return them unmodified in the element. This * includes any "xmlns" attributes. */ while ((xa = xml_child_each(xrpc, xa, CX_ATTR)) != NULL){ /* If attribute already exists, dont copy it */ if (xml_find_type(xc, NULL, xml_name(xa), CX_ATTR) != NULL) continue; if ((xa2 = xml_dup(xa)) ==NULL) goto done; if (xml_addsub(xc, xa2) < 0) goto done; } if ((cbret = cbuf_new()) == NULL){ clicon_err(LOG_ERR, errno, "cbuf_new"); goto done; } clicon_xml2cbuf(cbret, xml_child_i(xret,0), 0, 0, -1); if (netconf_output_encap(1, cbret, "rpc-reply") < 0){ cbuf_free(cbret); goto done; } } } ok: retval = 0; done: if (cbret) cbuf_free(cbret); if (xret) xml_free(xret); return retval; } /*! Process incoming a single netconf message parsed as XML * Identify what netconf message it is * @param[in] h Clicon handle * @param[in] xreq XML tree containing netconf * @param[in] yspec YANG spec * @retval 0 OK * @retval -1 Error */ static int netconf_input_packet(clicon_handle h, cxobj *xreq, yang_stmt *yspec) { int retval = -1; cbuf *cbret = NULL; char *rpcname; char *rpcprefix; char *namespace = NULL; clicon_debug(1, "%s", __FUNCTION__); rpcname = xml_name(xreq); rpcprefix = xml_prefix(xreq); if (xml2ns(xreq, rpcprefix, &namespace) < 0) goto done; /* Only accept resolved NETCONF base namespace */ if (namespace == NULL || strcmp(namespace, NETCONF_BASE_NAMESPACE) != 0){ if ((cbret = cbuf_new()) == NULL){ clicon_err(LOG_ERR, errno, "cbuf_new"); goto done; } if (netconf_unknown_namespace(cbret, "protocol", rpcprefix, "No appropriate namespace associated with prefix")< 0) goto done; netconf_output_encap(1, cbret, "rpc-error"); } if (strcmp(rpcname, "rpc") == 0){ if (netconf_rpc_message(h, xreq, yspec) < 0) goto done; } else if (strcmp(rpcname, "hello") == 0){ if (netconf_hello(h, xreq) < 0) goto done; } else{ if ((cbret = cbuf_new()) == NULL){ clicon_err(LOG_ERR, errno, "cbuf_new"); goto done; } if (netconf_unknown_element(cbret, "protocol", rpcname, "Unrecognized netconf operation")< 0) goto done; netconf_output_encap(1, cbret, "rpc-error"); } retval = 0; done: if (cbret) cbuf_free(cbret); return retval; } /*! Process incoming frame, ie a char message framed by ]]>]]> * Parse string to xml, check only one netconf message within a frame * @param[in] h Clicon handle * @param[in] cb Packet buffer * @retval 0 OK * @retval -1 Fatal error */ static int netconf_input_frame(clicon_handle h, cbuf *cb) { int retval = -1; char *str = NULL; cxobj *xtop = NULL; /* Request (in) */ cxobj *xreq = NULL; cxobj *xret = NULL; /* Return (out) */ cbuf *cbret = NULL; yang_stmt *yspec; int ret; clicon_debug(1, "%s", __FUNCTION__); clicon_debug(2, "%s: \"%s\"", __FUNCTION__, cbuf_get(cb)); yspec = clicon_dbspec_yang(h); if ((str = strdup(cbuf_get(cb))) == NULL){ clicon_err(OE_UNIX, errno, "strdup"); goto done; } /* Parse incoming XML message */ if ((ret = clixon_xml_parse_string(str, YB_RPC, yspec, &xtop, &xret)) < 0){ if ((cbret = cbuf_new()) == NULL){ clicon_err(OE_UNIX, errno, "cbuf_new"); goto done; } if (netconf_operation_failed(cbret, "rpc", clicon_err_reason)< 0) goto done; netconf_output_encap(1, cbret, "rpc-error"); /* XXX */ goto ok; } if (ret == 0){ if ((cbret = cbuf_new()) == NULL){ clicon_err(LOG_ERR, errno, "cbuf_new"); goto done; } clicon_xml2cbuf(cbret, xret, 0, 0, -1); netconf_output_encap(1, cbret, "rpc-error"); goto ok; } /* Check for empty frame (no mesaages), return empty message, not clear from RFC what to do */ if (xml_child_nr_type(xtop, CX_ELMNT) == 0){ if ((cbret = cbuf_new()) == NULL){ clicon_err(OE_UNIX, errno, "cbuf_new"); goto done; } netconf_output_encap(1, cbret, "rpc-error"); goto ok; } /* Check for multi-messages in frame */ if (xml_child_nr_type(xtop, CX_ELMNT) != 1){ if ((cbret = cbuf_new()) == NULL){ clicon_err(OE_UNIX, errno, "cbuf_new"); goto done; } if (netconf_malformed_message(cbret, "More than one message in netconf rpc frame")< 0) goto done; netconf_output_encap(1, cbret, "rpc-error"); goto ok; } if ((xreq = xml_child_i_type(xtop, 0, CX_ELMNT)) == NULL){ /* Shouldnt happen */ clicon_err(OE_XML, EFAULT, "No xml req (shouldnt happen)"); goto done; } if (netconf_input_packet(h, xreq, yspec) < 0) goto done; ok: retval = 0; done: if (str) free(str); if (xtop) xml_free(xtop); if (xret) xml_free(xret); if (cbret) cbuf_free(cbret); return retval; } /*! Get netconf message: detect end-of-msg * @param[in] s Socket where input arrived. read from this. * @param[in] arg Clicon handle. * This routine continuously reads until no more data on s. There could * be risk of starvation, but the netconf client does little else than * read data so I do not see a danger of true starvation here. */ static int netconf_input_cb(int s, void *arg) { int retval = -1; clicon_handle h = arg; unsigned char buf[BUFSIZ]; int i; int len; cbuf *cb=NULL; int xml_state = 0; int poll; if ((cb = cbuf_new()) == NULL){ clicon_err(OE_XML, errno, "cbuf_new"); return retval; } memset(buf, 0, sizeof(buf)); while (1){ if ((len = read(s, buf, sizeof(buf))) < 0){ if (errno == ECONNRESET) len = 0; /* emulate EOF */ else{ clicon_log(LOG_ERR, "%s: read: %s", __FUNCTION__, strerror(errno)); goto done; } } /* read */ if (len == 0){ /* EOF */ cc_closed++; close(s); retval = 0; goto done; } for (i=0; i]]>", buf[i], &xml_state)) { /* OK, we have an xml string from a client */ /* Remove trailer */ *(((char*)cbuf_get(cb)) + cbuf_len(cb) - strlen("]]>]]>")) = '\0'; if (netconf_input_frame(h, cb) < 0 && !ignore_packet_errors) // default is to ignore errors goto done; if (cc_closed) break; cbuf_reset(cb); } } /* poll==1 if more, poll==0 if none */ if ((poll = clixon_event_poll(s)) < 0) goto done; if (poll == 0) break; /* No data to read */ } /* while */ retval = 0; done: if (cb) cbuf_free(cb); if (cc_closed) retval = -1; return retval; } /*! Send netconf hello message * @param[in] h Clicon handle * @param[in] s File descriptor to write on (eg 1 - stdout) */ static int send_hello(clicon_handle h, int s, uint32_t id) { int retval = -1; cbuf *cb; if ((cb = cbuf_new()) == NULL){ clicon_log(LOG_ERR, "%s: cbuf_new", __FUNCTION__); goto done; } if (netconf_hello_server(h, cb, id) < 0) goto done; if (netconf_output(s, cb, "hello") < 0) goto done; retval = 0; done: if (cb) cbuf_free(cb); return retval; } /*! Clean and close all state of netconf process (but dont exit). * Cannot use h after this * @param[in] h Clixon handle */ static int netconf_terminate(clicon_handle h) { yang_stmt *yspec; cvec *nsctx; cxobj *x; clixon_plugin_exit_all(h); rpc_callback_delete_all(h); clicon_rpc_close_session(h); if ((yspec = clicon_dbspec_yang(h)) != NULL) yspec_free(yspec); if ((yspec = clicon_config_yang(h)) != NULL) yspec_free(yspec); if ((nsctx = clicon_nsctx_global_get(h)) != NULL) cvec_free(nsctx); if ((x = clicon_conf_xml(h)) != NULL) xml_free(x); xpath_optimize_exit(); clixon_event_exit(); clicon_handle_exit(h); clicon_log_exit(); return 0; } static int timeout_fn(int s, void *arg) { clicon_err(OE_EVENTS, ETIMEDOUT, "User request timeout"); return -1; } /*! Usage help routine * @param[in] h Clicon handle * @param[in] argv0 command line */ static void usage(clicon_handle h, char *argv0) { fprintf(stderr, "usage:%s\n" "where options are\n" "\t-h\t\tHelp\n" "\t-D \tDebug level\n" "\t-f \tConfiguration file (mandatory)\n" "\t-E \tExtra configuration file directory\n" "\t-l (e|o|s|f) Log on std(e)rr, std(o)ut, (s)yslog(default), (f)ile\n" "\t-q\t\tQuiet: dont send hello prompt\n" "\t-a UNIX|IPv4|IPv6 Internal backend socket family\n" "\t-u \tInternal socket domain path or IP addr (see -a)\n" "\t-d \tSpecify netconf plugin directory dir (default: %s)\n" "\t-p \tYang directory path (see CLICON_YANG_DIR)\n" "\t-y \tLoad yang spec file (override yang main module)\n" "\t-U \tOver-ride unix user with a pseudo user for NACM.\n" "\t-t \tTimeout in seconds. Quit after this time.\n" "\t-e \t\tDont ignore errors on packet input.\n" "\t-o \"