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

@ -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)