Restconf: http cookie sent as attribute in rpc restconf_post operations to backend.

This commit is contained in:
Olof Hagsand 2017-10-25 21:08:57 +02:00
parent 178a09cf3b
commit e8a8d6e871
6 changed files with 59 additions and 4 deletions

View file

@ -2,6 +2,7 @@
## 3.3.3 Upcoming
* Restconf: http cookie sent as attribute in rpc restconf_post operations to backend.
* Added option CLICON_CLISPEC_FILE as complement to CLICON_CLISPEC_DIR to
specify single CLI specification file, not only directory containing files.

View file

@ -45,7 +45,7 @@
*/
struct client_entry;
typedef int (*backend_rpc_cb)(
clicon_handle h,
clicon_handle h, /* CLicon handle */
cxobj *xe, /* Request: <rpc><xn></rpc> */
struct client_entry *ce, /* Client session */
cbuf *cbret,/* Reply eg <rpc-reply>... */

View file

@ -330,7 +330,7 @@ restconf_plugin_load(clicon_handle h)
(int)strlen(filename), filename);
if ((handle = plugin_load(h, filename, RTLD_NOW)) == NULL)
goto quit;
p_credentials = dlsym(handle, "restconf_credentials");
p_credentials = dlsym(handle, PLUGIN_CREDENTIALS);
if ((plugins = realloc(plugins, (nplugins+1) * sizeof (*plugins))) == NULL) {
clicon_err(OE_UNIX, errno, "realloc");
goto quit;
@ -406,3 +406,30 @@ plugin_credentials(clicon_handle h,
done:
return retval;
}
/*! Parse a cookie string and return value of cookie attribute
* @param[in] cookiestr cookie string according to rfc6265 (modified)
* @param[in] attribute cookie attribute
* @param[out] val malloced cookie value, free with free()
*/
int
get_user_cookie(char *cookiestr,
char *attribute,
char **val)
{
int retval = -1;
cvec *cvv = NULL;
char *c;
if (str2cvec(cookiestr, ';', '=', &cvv) < 0)
goto done;
if ((c = cvec_find_str(cvv, attribute)) != NULL){
if ((*val = strdup(c)) == NULL)
goto done;
}
retval = 0;
done:
if (cvv)
cvec_free(cvv);
return retval;
}

View file

@ -53,10 +53,11 @@ int clicon_debug_xml(int dbglevel, char *str, cxobj *cx);
int test(FCGX_Request *r, int dbg);
cbuf *readdata(FCGX_Request *r);
int restconf_plugin_load(clicon_handle h);
int restconf_plugin_start(clicon_handle h, int argc, char **argv);
int restconf_plugin_unload(clicon_handle h);
int plugin_credentials(clicon_handle h, FCGX_Request *r, int *auth);
int get_user_cookie(char *cookiestr, char *attribute, char **val);
#endif /* _RESTCONF_LIB_H_ */

View file

@ -706,6 +706,8 @@ api_operation_post(clicon_handle h,
}
for (i=0; i<pi; i++)
oppath = index(oppath+1, '/');
clicon_debug(1, "%s oppath: %s", __FUNCTION__, oppath);
/* Find yang rpc statement, return yang rpc statement if found */
if (yang_abs_schema_nodeid(yspec, oppath, &yrpc) < 0)
goto done;
@ -754,6 +756,25 @@ api_operation_post(clicon_handle h,
}
}
}
/* Non-standard: add cookie as attribute for backend
*/
{
cxobj *xa;
char *cookie;
char *cookieval = NULL;
if ((cookie = FCGX_GetParam("HTTP_COOKIE", r->envp)) != NULL &&
get_user_cookie(cookie, "c-user", &cookieval) ==0){
if ((xa = xml_new("cookie", xtop)) == NULL)
goto done;
xml_type_set(xa, CX_ATTR);
if (xml_value_set(xa, cookieval) < 0)
goto done;
if (cookieval)
free(cookieval);
}
}
/* Send to backend */
if (clicon_rpc_netconf_xml(h, xtop, &xret, NULL) < 0)
goto done;
if ((cbx = cbuf_new()) == NULL)

View file

@ -69,11 +69,16 @@ typedef int (plginit_t)(clicon_handle); /* Plugin Init */
typedef int (plgstart_t)(clicon_handle, int, char **); /* Plugin start */
/* Called just before plugin unloaded.
* @see plgexit_t
*/
#define PLUGIN_EXIT "plugin_exit"
typedef int (plgexit_t)(clicon_handle); /* Plugin exit */
/*! Called by restconf
* Returns 0 if credentials OK, -1 if failed
*/
#define PLUGIN_CREDENTIALS "plugin_credentials"
typedef int (plgcredentials_t)(clicon_handle, void *); /* Plugin credentials */
/* Find a function in global namespace or a plugin. XXX clicon internal */
void *clicon_find_func(clicon_handle h, char *plugin, char *func);