* Restructure and more generic plugin API (cli,backend,restconf,netconf)

* For preparation for authorization RFC8341
  * Plugins add clixon_plugin_init() and api struct for function pointers, eg:
```
static const struct clixon_plugin_api api = {
    "example",
    clixon_plugin_init,
    ...
}
clixon_plugin_api *clixon_plugin_init(clicon_handle h)
{
    return (void*)&api;
}
```
  * Moved specific plugin functions from apps/ to generic functions in lib/
    * New generic plugin load function: clixon_plugins_load()
  * Removed client-local netconf plugins netconf_plugin_callbacks()
    * This was code used before generic YANG rpc calls
  * Added username to clixon handle:
    * clicon_username_get() / clicon_username_set()
  * Added authentication plugin callback
  * Removed some obscure plugin code that seem not to be used (please report if needed!)
    * CLI parse hook
    * CLICON_FIND_PLUGIN
    * clicon_valcb()
* Removed username to rpc calls (added below)
This commit is contained in:
Olof hagsand 2018-04-02 10:38:53 +02:00
parent b8e35742b9
commit 79e3fbdaa9
41 changed files with 470 additions and 772 deletions

View file

@ -81,7 +81,7 @@ mycallback(clicon_handle h, cvec *cvv, cvec *argv)
/* Show eth0 interfaces config using XPATH */
if (clicon_rpc_get_config(h, "running","/interfaces/interface[name=eth0]",
NULL, &xret) < 0)
&xret) < 0)
goto done;
xml_print(stdout, xret);

View file

@ -46,18 +46,7 @@
#include <clixon/clixon.h>
#include <clixon/clixon_netconf.h>
/*
* Plugin initialization
*/
int
plugin_init(clicon_handle h)
{
return 0;
}
/*
* Plugin start
/*! Plugin start
* Called once everything has been initialized, right before
* the main event loop is entered.
*/
@ -73,3 +62,21 @@ plugin_exit(clicon_handle h)
return 0;
}
clixon_plugin_api * clixon_plugin_init(clicon_handle h);
static const struct clixon_plugin_api api = {
"example",
clixon_plugin_init,
plugin_start,
plugin_exit,
NULL
};
/*! Netconf plugin initialization
*/
clixon_plugin_api *
clixon_plugin_init(clicon_handle h)
{
return (void*)&api;
}

View file

@ -187,15 +187,16 @@ b64_decode(const char *src,
* @param[in] r Fastcgi request handle
* @param[out] username Malloced username, or NULL.
* @retval -1 Fatal error
* @retval 0 OK
* @retval 0 Unauth
* @retval 1 Auth
* For grideye, return "u" entry name if it has a valid "user" entry.
*/
int
plugin_credentials(clicon_handle h,
FCGX_Request *r,
char **username)
void *arg)
{
int retval = -1;
FCGX_Request *r = (FCGX_Request *)arg;
cxobj *xt = NULL;
cxobj *x;
char *xbody;
@ -208,19 +209,18 @@ plugin_credentials(clicon_handle h,
int ret;
clicon_debug(1, "%s", __FUNCTION__);
*username = NULL; /* unauthorized */
/* Check if basic_auth set, if not return OK */
if (clicon_rpc_get_config(h, "running", "/", NULL, &xt) < 0)
if (clicon_rpc_get_config(h, "running", "/", &xt) < 0)
goto done;
if ((x = xpath_first(xt, "basic_auth")) == NULL)
goto none;
goto ok;
if ((xbody = xml_body(x)) == NULL)
goto none;
goto ok;
if (strcmp(xbody, "true"))
goto none;
goto ok;
/* At this point in the code we must use HTTP basic authentication */
if ((auth = FCGX_GetParam("HTTP_AUTHORIZATION", r->envp)) == NULL)
goto done;
goto fail;
if (strlen(auth) < strlen("Basic "))
goto fail;
if (strncmp("Basic ", auth, strlen("Basic ")))
@ -245,17 +245,15 @@ plugin_credentials(clicon_handle h,
cprintf(cb, "auth[user=%s]", user);
if ((x = xpath_first(xt, cbuf_get(cb))) == NULL)
goto fail;
passwd2 = xml_find_body(x, "password");
if (strcmp(passwd, passwd2))
goto fail;
if ((*username = strdup(user)) == NULL){
clicon_err(OE_UNIX, errno, "strdup");
retval = 1;
if (clicon_username_set(h, user) < 0)
goto done;
}
fail:
retval = 0;
done:
ok: /* authenticated */
retval = 1;
done: /* error */
clicon_debug(1, "%s retval:%d", __FUNCTION__, retval);
if (user)
free(user);
@ -264,24 +262,26 @@ plugin_credentials(clicon_handle h,
if (xt)
xml_free(xt);
return retval;
none: /* basic_auth is not enabled, harcode authenticated user "none" */
if ((*username = strdup("none")) == NULL){
clicon_err(OE_XML, errno, "strdup");
goto done;
}
goto fail;
fail: /* unauthenticated */
retval = 0;
goto done;
}
clixon_plugin_api * clixon_plugin_init(clicon_handle h);
static const struct clixon_plugin_api api = {
"example",
clixon_plugin_init,
NULL,
NULL,
plugin_credentials,
};
/*! Restconf plugin initialization
*/
int
plugin_init(clicon_handle h)
clixon_plugin_api *
clixon_plugin_init(clicon_handle h)
{
int retval = -1;
clicon_debug(1, "%s restconf", __FUNCTION__);
retval = 0;
// done:
return retval;
return (void*)&api;
}