/* * ***** BEGIN LICENSE BLOCK ***** Copyright (C) 2009-2019 Olof Hagsand Copyright (C) 2020-2021 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 ***** */ /* * This program should be run as user www-data * * See draft-ietf-netconf-restconf-13.txt [draft] * sudo apt-get install libfcgi-dev * gcc -o fastcgi fastcgi.c -lfcgi * sudo su -c "/www-data/clixon_restconf -D 1 -f /usr/local/etc/example.xml " -s /bin/sh www-data * This is the interface: * api/data/profile=/metric= PUT data:enable= * api/test */ #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 /* chmod */ /* cligen */ #include /* clicon */ #include #include /* Need to be after clixon_xml.h due to attribute format */ /* restconf */ #include "restconf_lib.h" /* generic shared with plugins */ #include "restconf_handle.h" #include "restconf_api.h" /* generic not shared with plugins */ #include "restconf_err.h" #include "restconf_root.h" /* generic not shared with plugins */ #include "restconf_methods.h" /* fcgi specific */ #include "restconf_methods_get.h" #include "restconf_methods_post.h" #include "restconf_stream.h" /* Command line options to be passed to getopt(3) */ #define RESTCONF_OPTS "hD:f:E:l:p:d:y:a:u:rW:R:o:" /*! Convert FCGI parameters to clixon runtime data * @param[in] h Clixon handle * @param[in] envp Fastcgi request handle parameter array on the format "=" * @see https://nginx.org/en/docs/http/ngx_http_core_module.html#var_https */ static int fcgi_params_set(clicon_handle h, char **envp) { int retval = -1; int i; char *param = NULL; char *val = NULL; clicon_debug(1, "%s", __FUNCTION__); for (i = 0; envp[i] != NULL; i++){ /* on the form = */ if (clixon_strsplit(envp[i], '=', ¶m, &val) < 0) goto done; if (restconf_param_set(h, param, val) < 0) goto done; if (param){ free(param); param = NULL; } if (val){ free(val); val = NULL; } } retval = 0; done: clicon_debug(1, "%s %d", __FUNCTION__, retval); return retval; } /* XXX Need global variable to for SIGCHLD signal handler */ static clicon_handle _CLICON_HANDLE = NULL; /* XXX Need global variable to break FCGI accept loop from signal handler see FCGX_Accept_r(req) */ static int _MYSOCK; /*! Signall terminates process */ static void restconf_sig_term(int arg) { static int i=0; clicon_debug(1, "%s", __FUNCTION__); if (i++ == 0) clicon_log(LOG_NOTICE, "%s: %s: pid: %u Signal %d", __PROGRAM__, __FUNCTION__, getpid(), arg); else{ clicon_debug(1, "%s done", __FUNCTION__); exit(-1); } /* This should ensure no more accepts or incoming packets are processed because next time eventloop * is entered, it will terminate. * However there may be a case of sockets closing rather abruptly for clients */ clixon_exit_set(1); close(_MYSOCK); } /*! Reap stream child * XXX The -1 should be changed to proper pid, see eg clixon_process_waitpid */ static void restconf_sig_child(int arg) { int status; int pid; if ((pid = waitpid(-1, &status, 0)) != -1 && WIFEXITED(status)) stream_child_free(_CLICON_HANDLE, pid); } /*! Usage help routine * @param[in] argv0 command line * @param[in] h Clicon handle */ static void usage(clicon_handle h, char *argv0) { fprintf(stderr, "usage:%s [options]\n" "where options are\n" "\t-h \t\t Help\n" "\t-D \t Debug level\n" "\t-f \t Configuration file (mandatory)\n" "\t-E \t Extra configuration file directory\n" "\t-l > \t Log on (s)yslog, (f)ile (syslog is default)\n" "\t-p \t Yang directory path (see CLICON_YANG_DIR)\n" "\t-y \t Load yang spec file (override yang main module)\n" "\t-a UNIX|IPv4|IPv6 Internal backend socket family\n" "\t-u \t Internal socket domain path or IP addr (see -a)\n" "\t-r \t\t Do not drop privileges if run as root\n" "\t-W \t Run restconf daemon as this user, drop according to CLICON_RESTCONF_PRIVILEGES\n" "\t-R \t Restconf configuration in-line overriding config file\n" "\t-o \"