restconf plugins and figthing with xml->json translation

This commit is contained in:
Olof Hagsand 2016-09-26 21:44:17 +02:00
parent 64f197cb00
commit 3443dbd33f
9 changed files with 117 additions and 51 deletions

View file

@ -835,7 +835,7 @@ cli_notification_cb(int s, void *arg)
if (clicon_xml_parse_string(&eventstr, &xt) < 0) if (clicon_xml_parse_string(&eventstr, &xt) < 0)
goto done; goto done;
if ((xn = xml_child_i(xt, 0)) != NULL){ if ((xn = xml_child_i(xt, 0)) != NULL){
if (xml2json(stdout, xn, 0, 1) < 0) if (xml2json(stdout, xn, 0) < 0)
goto done; goto done;
} }
} }

View file

@ -443,7 +443,7 @@ show_conf_as_json(clicon_handle h,
if (show_conf_as(h, cvv, arg, &xt) < 0) if (show_conf_as(h, cvv, arg, &xt) < 0)
goto done; goto done;
xml2json(stdout, xt, 1, 0); xml2json(stdout, xt, 1);
retval = 0; retval = 0;
done: done:
if (xt) if (xt)

View file

@ -56,7 +56,7 @@ olof@vandal> curl -G http://127.0.0.1/restconf/data/interfaces/interface/name=et
Debugging Debugging
--------- ---------
Start the fastcgi programs with debug flag: Start the restconf programs with debug flag:
sudo su -c "/www-data/clixon_restconf -D" -s /bin/sh www-data sudo su -c "/www-data/clixon_restconf -D" -s /bin/sh www-data
Look at syslog: Look at syslog:

View file

@ -231,8 +231,11 @@ readdata(FCGX_Request *r)
return cb; return cb;
} }
typedef int (credentials_t)(clicon_handle h, FCGX_Request *r);
static int nplugins = 0; static int nplugins = 0;
static plghndl_t *plugins = NULL; static plghndl_t *plugins = NULL;
static credentials_t *p_credentials = NULL; /* Credentials callback */
/*! Load a dynamic plugin object and call it's init-function /*! Load a dynamic plugin object and call it's init-function
* Note 'file' may be destructively modified * Note 'file' may be destructively modified
@ -247,22 +250,29 @@ plugin_load (clicon_handle h,
void *handle = NULL; void *handle = NULL;
plginit_t *initfn; plginit_t *initfn;
clicon_debug(1, "%s", __FUNCTION__);
dlerror(); /* Clear any existing error */ dlerror(); /* Clear any existing error */
if ((handle = dlopen (file, dlflags)) == NULL) { if ((handle = dlopen (file, dlflags)) == NULL) {
error = (char*)dlerror(); error = (char*)dlerror();
clicon_err(OE_PLUGIN, errno, "dlopen: %s\n", error ? error : "Unknown error"); clicon_err(OE_PLUGIN, errno, "dlopen: %s\n", error ? error : "Unknown error");
goto quit; goto done;
} }
/* call plugin_init() if defined */ /* call plugin_init() if defined */
if ((initfn = dlsym(handle, PLUGIN_INIT)) != NULL) { if ((initfn = dlsym(handle, PLUGIN_INIT)) == NULL){
clicon_err(OE_PLUGIN, errno, "Failed to find plugin_init when loading restconf plugin %s", file);
goto err;
}
if (initfn(h) != 0) { if (initfn(h) != 0) {
clicon_err(OE_PLUGIN, errno, "Failed to initiate %s\n", strrchr(file,'/')?strchr(file, '/'):file); clicon_err(OE_PLUGIN, errno, "Failed to initiate %s", strrchr(file,'/')?strchr(file, '/'):file);
goto quit; goto err;
} }
} p_credentials = dlsym(handle, "restconf_credentials");
quit: done:
return handle; return handle;
err:
if (handle)
dlclose(handle);
return NULL;
} }
@ -371,3 +381,25 @@ restconf_plugin_start(clicon_handle h,
return 0; return 0;
} }
int
plugin_credentials(clicon_handle h,
FCGX_Request *r,
int *auth)
{
int retval = -1;
clicon_debug(1, "%s", __FUNCTION__);
/* If no authentication callback then allow anything. Is this OK? */
if (p_credentials == 0){
*auth = 1;
retval = 0;
goto done;
}
if (p_credentials(h, r) < 0)
*auth = 0;
else
*auth = 1;
retval = 0;
done:
return retval;
}

View file

@ -34,6 +34,6 @@ cbuf *readdata(FCGX_Request *r);
int restconf_plugin_load(clicon_handle h); int restconf_plugin_load(clicon_handle h);
int restconf_plugin_start(clicon_handle h, int argc, char **argv); int restconf_plugin_start(clicon_handle h, int argc, char **argv);
int restconf_plugin_unload(clicon_handle h); int restconf_plugin_unload(clicon_handle h);
int plugin_credentials(clicon_handle h, FCGX_Request *r, int *auth);
#endif /* _RESTCONF_LIB_H_ */ #endif /* _RESTCONF_LIB_H_ */

View file

@ -139,8 +139,8 @@ api_data_get(clicon_handle h,
cvec *cvk = NULL; /* vector of index keys */ cvec *cvk = NULL; /* vector of index keys */
cg_var *cvi; cg_var *cvi;
yspec = clicon_dbspec_yang(h);
clicon_debug(1, "%s", __FUNCTION__); clicon_debug(1, "%s", __FUNCTION__);
yspec = clicon_dbspec_yang(h);
if ((path = cbuf_new()) == NULL) if ((path = cbuf_new()) == NULL)
goto done; goto done;
if ((path1 = cbuf_new()) == NULL) /* without [] qualifiers */ if ((path1 = cbuf_new()) == NULL) /* without [] qualifiers */
@ -179,6 +179,7 @@ api_data_get(clicon_handle h,
if ((ykey = yang_find((yang_node*)y, Y_KEY, NULL)) == NULL){ if ((ykey = yang_find((yang_node*)y, Y_KEY, NULL)) == NULL){
clicon_err(OE_XML, errno, "%s: List statement \"%s\" has no key", clicon_err(OE_XML, errno, "%s: List statement \"%s\" has no key",
__FUNCTION__, y->ys_argument); __FUNCTION__, y->ys_argument);
notfound(r);
goto done; goto done;
} }
clicon_debug(1, "ykey:%s", ykey->ys_argument); clicon_debug(1, "ykey:%s", ykey->ys_argument);
@ -202,21 +203,25 @@ api_data_get(clicon_handle h,
cprintf(path1, "/%s", name); cprintf(path1, "/%s", name);
} }
} }
if (xmldb_get(h, "running", cbuf_get(path), 0, &xt, &vec, &veclen) < 0) clicon_debug(1, "%s path:%s", __FUNCTION__, cbuf_get(path));
if (xmldb_get(h, "running", cbuf_get(path), 1, &xt, &vec, &veclen) < 0)
goto done; goto done;
if ((cbx = cbuf_new()) == NULL)
goto done;
if (veclen==0){
notfound(r);
goto done;
}
FCGX_SetExitStatus(200, r->out); /* OK */ FCGX_SetExitStatus(200, r->out); /* OK */
FCGX_FPrintF(r->out, "Content-Type: application/yang.data+json\r\n"); FCGX_FPrintF(r->out, "Content-Type: application/yang.data+json\r\n");
FCGX_FPrintF(r->out, "\r\n"); FCGX_FPrintF(r->out, "\r\n");
if ((cbx = cbuf_new()) == NULL) if (xml2json_cbuf_vec(cbx, vec, veclen, 0) < 0)
goto done; goto done;
if (xml2json_cbuf(cbx, xt, 1, 0) < 0) FCGX_FPrintF(r->out, "[%s]", cbuf_get(cbx));
goto done; FCGX_FPrintF(r->out, "\r\n\r\n");
FCGX_FPrintF(r->out, "%s", cbuf_get(cbx));
FCGX_FPrintF(r->out, "hej\r\n\r\n");
retval = 0; retval = 0;
done: done:
if (vec)
free(vec);
if (cbx) if (cbx)
cbuf_free(cbx); cbuf_free(cbx);
if (xt) if (xt)
@ -248,7 +253,6 @@ api_data_delete(clicon_handle h,
clicon_debug(1, "%s api_path:%s", __FUNCTION__, api_path); clicon_debug(1, "%s api_path:%s", __FUNCTION__, api_path);
for (i=0; i<pi; i++) for (i=0; i<pi; i++)
api_path = index(api_path+1, '/'); api_path = index(api_path+1, '/');
clicon_debug(1, "%s api_path:%s", __FUNCTION__, api_path);
/* Parse input data as json into xml */ /* Parse input data as json into xml */
if (clicon_rpc_xmlput(h, "candidate", if (clicon_rpc_xmlput(h, "candidate",
@ -256,7 +260,6 @@ api_data_delete(clicon_handle h,
api_path, api_path,
"") < 0) "") < 0)
goto done; goto done;
clicon_debug(1, "%s xmldb_put ok", __FUNCTION__);
if (clicon_rpc_commit(h, "candidate", "running", if (clicon_rpc_commit(h, "candidate", "running",
0, 0) < 0) 0, 0) < 0)
goto done; goto done;
@ -316,26 +319,22 @@ api_data_put(clicon_handle h,
api_path, data); api_path, data);
for (i=0; i<pi; i++) for (i=0; i<pi; i++)
api_path = index(api_path+1, '/'); api_path = index(api_path+1, '/');
clicon_debug(1, "%s api_path:%s", __FUNCTION__, api_path);
/* Parse input data as json into xml */ /* Parse input data as json into xml */
if (json_parse_str(data, &xdata) < 0){ if (json_parse_str(data, &xdata) < 0){
clicon_debug(1, "%s json fail", __FUNCTION__); clicon_debug(1, "%s json fail", __FUNCTION__);
goto done; goto done;
} }
clicon_debug_xml(1, "json xml:", xdata);
if ((cbx = cbuf_new()) == NULL) if ((cbx = cbuf_new()) == NULL)
goto done; goto done;
x = NULL; x = NULL;
while ((x = xml_child_each(xdata, x, -1)) != NULL) while ((x = xml_child_each(xdata, x, -1)) != NULL)
if (clicon_xml2cbuf(cbx, x, 0, 0) < 0) if (clicon_xml2cbuf(cbx, x, 0, 0) < 0)
goto done; goto done;
clicon_debug(1, "xml:%s", cbuf_get(cbx));
if (clicon_rpc_xmlput(h, "candidate", if (clicon_rpc_xmlput(h, "candidate",
OP_MERGE, OP_MERGE,
api_path, api_path,
cbuf_get(cbx)) < 0) cbuf_get(cbx)) < 0)
goto done; goto done;
clicon_debug(1, "%s xmldb_put ok", __FUNCTION__);
if (clicon_rpc_commit(h, "candidate", "running", if (clicon_rpc_commit(h, "candidate", "running",
0, 0) < 0) 0, 0) < 0)
goto done; goto done;
@ -407,6 +406,7 @@ request_process(clicon_handle h,
cvec *pcvec = NULL; /* for rest api */ cvec *pcvec = NULL; /* for rest api */
cbuf *cb = NULL; cbuf *cb = NULL;
char *data; char *data;
int auth = 0;
clicon_debug(1, "%s", __FUNCTION__); clicon_debug(1, "%s", __FUNCTION__);
path = FCGX_GetParam("DOCUMENT_URI", r->envp); path = FCGX_GetParam("DOCUMENT_URI", r->envp);
@ -428,7 +428,14 @@ request_process(clicon_handle h,
method = pvec[2]; method = pvec[2];
retval = 0; retval = 0;
test(r, 1); test(r, 1);
/* XXX Credentials */ /* If present, check credentials */
if (plugin_credentials(h, r, &auth) < 0)
goto done;
clicon_debug(1, "%s credentials ok 1", __FUNCTION__);
if (auth == 0)
goto done;
clicon_debug(1, "%s credentials ok 2", __FUNCTION__);
if (strcmp(method, "data") == 0) /* restconf, skip /api/data */ if (strcmp(method, "data") == 0) /* restconf, skip /api/data */
retval = api_data(h, r, path, pcvec, 2, qvec, data); retval = api_data(h, r, path, pcvec, 2, qvec, data);
else if (strcmp(method, "test") == 0) else if (strcmp(method, "test") == 0)
@ -436,6 +443,7 @@ request_process(clicon_handle h,
else else
retval = notfound(r); retval = notfound(r);
done: done:
clicon_debug(1, "%s retval:%d", __FUNCTION__, retval);
if (dvec) if (dvec)
cvec_free(dvec); cvec_free(dvec);
if (qvec) if (qvec)
@ -445,7 +453,6 @@ request_process(clicon_handle h,
if (cb) if (cb)
cbuf_free(cb); cbuf_free(cb);
unchunk_group(__FUNCTION__); unchunk_group(__FUNCTION__);
clicon_debug(1, "%s end", __FUNCTION__);
return retval; return retval;
} }

View file

@ -27,7 +27,8 @@
* Prototypes * Prototypes
*/ */
int json_parse_str(char *str, cxobj **xt); int json_parse_str(char *str, cxobj **xt);
int xml2json_cbuf(cbuf *cb, cxobj *x, int pretty, int top); int xml2json_cbuf(cbuf *cb, cxobj *x, int pretty);
int xml2json(FILE *f, cxobj *x, int pretty, int top); int xml2json_cbuf_vec(cbuf *cb, cxobj **vec, size_t veclen, int pretty);
int xml2json(FILE *f, cxobj *x, int pretty);
#endif /* _CLIXON_JSON_H */ #endif /* _CLIXON_JSON_H */

View file

@ -244,7 +244,7 @@ xml2json1_cbuf(cbuf *cb,
if (xml2json1_cbuf(cb, xc, level+1, pretty) < 0) if (xml2json1_cbuf(cb, xc, level+1, pretty) < 0)
goto done; goto done;
if (i<xml_child_nr(x)-1){ if (i<xml_child_nr(x)-1){
cprintf(cb, ",", list); cprintf(cb, ",");
cprintf(cb, "%s", pretty?"\n":""); cprintf(cb, "%s", pretty?"\n":"");
} }
} }
@ -312,8 +312,32 @@ xml2json1_cbuf(cbuf *cb,
int int
xml2json_cbuf(cbuf *cb, xml2json_cbuf(cbuf *cb,
cxobj *x, cxobj *x,
int pretty, int pretty)
int top) {
int retval = 1;
int level = 0;
cprintf(cb, "%*s{%s",
pretty?(level*JSON_INDENT):0,"",
pretty?"\n":"");
if (xml2json1_cbuf(cb, x, level+1, pretty) < 0)
goto done;
cprintf(cb, "%*s}%s",
pretty?(level*JSON_INDENT):0,"",
pretty?"\n":"");
retval = 0;
done:
return retval;
}
/*!
* @note can be a problem with vector since xml2json1_cbuf checks parents
*/
int
xml2json_cbuf_vec(cbuf *cb,
cxobj **vec,
size_t veclen,
int pretty)
{ {
int retval = 1; int retval = 1;
int level = 0; int level = 0;
@ -323,15 +347,16 @@ xml2json_cbuf(cbuf *cb,
cprintf(cb, "%*s{%s", cprintf(cb, "%*s{%s",
pretty?(level*JSON_INDENT):0,"", pretty?(level*JSON_INDENT):0,"",
pretty?"\n":""); pretty?"\n":"");
if (top){ for (i=0; i<veclen; i++){
if (xml2json1_cbuf(cb, x, level+1, pretty) < 0) xc = vec[i];
goto done; if (xml2json1_cbuf(cb, xc, level, pretty) < 0)
}
else{
for (i=0; i<xml_child_nr(x); i++){
xc = xml_child_i(x, i);
if (xml2json1_cbuf(cb, xc, level+1, pretty) < 0)
goto done; goto done;
if (i<veclen-1){
if (xml_type(xc)==CX_BODY)
cprintf(cb, "},{");
else
cprintf(cb, ",");
cprintf(cb, "%s", pretty?"\n":"");
} }
} }
cprintf(cb, "%*s}%s", cprintf(cb, "%*s}%s",
@ -358,8 +383,7 @@ xml2json_cbuf(cbuf *cb,
int int
xml2json(FILE *f, xml2json(FILE *f,
cxobj *x, cxobj *x,
int pretty, int pretty)
int top)
{ {
int retval = 1; int retval = 1;
cbuf *cb = NULL; cbuf *cb = NULL;
@ -368,7 +392,7 @@ xml2json(FILE *f,
clicon_err(OE_XML, errno, "cbuf_new"); clicon_err(OE_XML, errno, "cbuf_new");
goto done; goto done;
} }
if (xml2json_cbuf(cb, x, pretty, top) < 0) if (xml2json_cbuf(cb, x, pretty) < 0)
goto done; goto done;
fprintf(f, "%s", cbuf_get(cb)); fprintf(f, "%s", cbuf_get(cb));
retval = 0; retval = 0;

View file

@ -579,6 +579,7 @@ get(char *dbname,
char *arg; char *arg;
cbuf *cb; cbuf *cb;
// clicon_debug(1, "%s xkey:%s val:%s", __FUNCTION__, xk, val);
x = xt; x = xt;
if (xk == NULL || *xk!='/'){ if (xk == NULL || *xk!='/'){
clicon_err(OE_DB, 0, "Invalid key: %s", xk); clicon_err(OE_DB, 0, "Invalid key: %s", xk);
@ -700,7 +701,7 @@ get(char *dbname,
xml_type_set(x, CX_BODY); xml_type_set(x, CX_BODY);
xml_value_set(x, val); xml_value_set(x, val);
} }
if(debug){ if(debug>1){
fprintf(stderr, "%s %s\n", __FUNCTION__, xk); fprintf(stderr, "%s %s\n", __FUNCTION__, xk);
clicon_xml2file(stderr, xt, 0, 1); clicon_xml2file(stderr, xt, 0, 1);
} }
@ -811,10 +812,9 @@ xmldb_get_tree(char *dbname,
goto done; goto done;
if ((xt = xml_new("clicon", NULL)) == NULL) if ((xt = xml_new("clicon", NULL)) == NULL)
goto done; goto done;
if (debug) /* debug */
for (i = 0; i < npairs; i++) for (i = 0; i < npairs; i++)
fprintf(stderr, "%s %s\n", pairs[i].dp_key, pairs[i].dp_val?pairs[i].dp_val:""); clicon_debug(2, "%s %s", pairs[i].dp_key, pairs[i].dp_val?pairs[i].dp_val:"");
// clicon_debug(1, "%s npairs:%d", __FUNCTION__, npairs);
for (i = 0; i < npairs; i++) { for (i = 0; i < npairs; i++) {
if (get(dbname, if (get(dbname,
yspec, yspec,
@ -953,6 +953,7 @@ xmldb_get_local(clicon_handle h,
yang_spec *yspec; yang_spec *yspec;
char *dbname = NULL; char *dbname = NULL;
clicon_debug(1, "%s", __FUNCTION__);
if (db2file(h, db, &dbname) < 0) if (db2file(h, db, &dbname) < 0)
goto done; goto done;
if (dbname==NULL){ if (dbname==NULL){
@ -1019,6 +1020,7 @@ xmldb_get(clicon_handle h,
{ {
int retval = -1; int retval = -1;
clicon_debug(1, "%s", __FUNCTION__);
if (clicon_xmldb_rpc(h)) if (clicon_xmldb_rpc(h))
retval = xmldb_get_rpc(h, db, xpath, vector, xtop, xvec, xlen); retval = xmldb_get_rpc(h, db, xpath, vector, xtop, xvec, xlen);
else else
@ -1422,7 +1424,7 @@ xmldb_put_tree_local(clicon_handle h,
} }
retval = 0; retval = 0;
done: done:
clicon_debug(1, "%s retval:%d", __FUNCTION__, retval); // clicon_debug(1, "%s retval:%d", __FUNCTION__, retval);
if (filename) if (filename)
free(filename); free(filename);
if (ckey) if (ckey)