diff --git a/apps/restconf/restconf_methods_post.c b/apps/restconf/restconf_methods_post.c index 735dd6e7..0b06e295 100644 --- a/apps/restconf/restconf_methods_post.c +++ b/apps/restconf/restconf_methods_post.c @@ -324,7 +324,10 @@ api_data_post(clicon_handle h, goto done; /* ybot is parent of spec(parent(data))) */ if (ymoddata && (ydata = xml_spec(xdata)) != NULL){ - if (ys_real_module(ydata) != ymoddata){ + yang_stmt *ymod = NULL; + if (ys_real_module(ydata, &ymod) < 0) + goto done; + if (ymod != ymoddata){ if (netconf_malformed_message_xml(&xerr, "Data is not prefixed with matching namespace") < 0) goto done; if ((xe = xpath_first(xerr, NULL, "rpc-error")) == NULL){ diff --git a/lib/clixon/clixon_yang.h b/lib/clixon/clixon_yang.h index bc7fc3e2..48ef85b7 100644 --- a/lib/clixon/clixon_yang.h +++ b/lib/clixon/clixon_yang.h @@ -215,7 +215,7 @@ yang_stmt *yn_each(yang_stmt *yn, yang_stmt *ys); char *yang_key2str(int keyword); int ys_module_by_xml(yang_stmt *ysp, struct xml *xt, yang_stmt **ymodp); yang_stmt *ys_module(yang_stmt *ys); -yang_stmt *ys_real_module(yang_stmt *ys); +int ys_real_module(yang_stmt *ys, yang_stmt **ymod); yang_stmt *ys_spec(yang_stmt *ys); yang_stmt *yang_find(yang_stmt *yn, int keyword, const char *argument); int yang_match(yang_stmt *yn, int keyword, char *argument); diff --git a/lib/src/clixon_json.c b/lib/src/clixon_json.c index ab1abba5..2b40ca69 100644 --- a/lib/src/clixon_json.c +++ b/lib/src/clixon_json.c @@ -716,12 +716,13 @@ xml2json1_cbuf(cbuf *cb, enum childtype childt; enum array_element_type xc_arraytype; yang_stmt *ys; - yang_stmt *ymod; /* yang module */ + yang_stmt *ymod = NULL; /* yang module */ int commas; char *modname = NULL; if ((ys = xml_spec(x)) != NULL){ - ymod = ys_real_module(ys); + if (ys_real_module(ys, &ymod) < 0) + goto done; modname = yang_argument_get(ymod); if (modname0 && strcmp(modname, modname0) == 0) modname=NULL; diff --git a/lib/src/clixon_path.c b/lib/src/clixon_path.c index 672230f1..04d9d3e2 100644 --- a/lib/src/clixon_path.c +++ b/lib/src/clixon_path.c @@ -298,11 +298,12 @@ yang2api_path_fmt_1(yang_stmt *ys, cbuf *cb) { yang_stmt *yp; /* parent */ - yang_stmt *ymod; + yang_stmt *ymod = NULL; + yang_stmt *ypmod = NULL; int i; cvec *cvk = NULL; /* vector of index keys */ int retval = -1; - + if ((yp = yang_parent_get(ys)) == NULL){ clicon_err(OE_YANG, EINVAL, "yang expected parent %s", yang_argument_get(ys)); goto done; @@ -329,7 +330,11 @@ yang2api_path_fmt_1(yang_stmt *ys, cprintf(cb, "/"); } /* If parent namespace/module is different from child -> add child prefix */ - if (ys_real_module(yp) != (ymod = ys_real_module(ys))) + if (ys_real_module(yp, &ypmod) < 0) + goto done; + if (ys_real_module(ys, &ymod) < 0) + goto done; + if (ypmod != ymod) cprintf(cb, "%s:", yang_argument_get(ymod)); } else /* top symbol - mark with name prefix */ diff --git a/lib/src/clixon_yang.c b/lib/src/clixon_yang.c index 15ab9767..be5585d3 100644 --- a/lib/src/clixon_yang.c +++ b/lib/src/clixon_yang.c @@ -882,9 +882,10 @@ yang_find_schemanode(yang_stmt *yn, } /*! Given a yang statement, find the prefix associated to this module + * * @param[in] ys Yang statement in module tree (or module itself) - * @retval NULL Not found - * @retval prefix Prefix as char* pointer into yang tree + * @retval NULL No prefix found. This is an error + * @retval prefix OK: Prefix as char* pointer into yang tree * @code * char *myprefix; * myprefix = yang_find_myprefix(ys); @@ -893,25 +894,26 @@ yang_find_schemanode(yang_stmt *yn, char * yang_find_myprefix(yang_stmt *ys) { - yang_stmt *ymod; /* My module */ + yang_stmt *ymod = NULL; /* My module */ yang_stmt *yprefix; char *prefix = NULL; /* Not good enough with submodule, must be actual module */ - if ((ymod = ys_real_module(ys)) == NULL){ - clicon_err(OE_YANG, 0, "My yang module not found"); + if (ys_real_module(ys, &ymod) < 0) + goto done; + if ((yprefix = yang_find(ymod, Y_PREFIX, NULL)) == NULL){ + clicon_err(OE_YANG, ENOENT, "No prefix found for module %s", yang_argument_get(ymod)); goto done; } - if ((yprefix = yang_find(ymod, Y_PREFIX, NULL)) == NULL) - goto done; prefix = yang_argument_get(yprefix); done: return prefix; } /*! Given a yang statement, find the namespace URI associated to this module + * * @param[in] ys Yang statement in module tree (or module itself) - * @retval NULL Not found + * @retval NULL Error: No namespace found. This is an error * @retval ns Namspace URI as char* pointer into yang tree * @code * char *myns = yang_find_mynamespace(ys); @@ -921,16 +923,16 @@ yang_find_myprefix(yang_stmt *ys) char * yang_find_mynamespace(yang_stmt *ys) { - yang_stmt *ymod; /* My module */ + yang_stmt *ymod = NULL; /* My module */ yang_stmt *ynamespace; char *ns = NULL; - if ((ymod = ys_real_module(ys)) == NULL){ - clicon_err(OE_YANG, ENOENT, "My yang module not found"); + if (ys_real_module(ys, &ymod) < 0) + goto done; + if ((ynamespace = yang_find(ymod, Y_NAMESPACE, NULL)) == NULL){ + clicon_err(OE_YANG, ENOENT, "No namespace found for module %s", yang_argument_get(ymod)); goto done; } - if ((ynamespace = yang_find(ymod, Y_NAMESPACE, NULL)) == NULL) - goto done; ns = yang_argument_get(ynamespace); done: return ns; @@ -1254,22 +1256,30 @@ ys_module(yang_stmt *ys) /*! Find real top module given a statement in a yang tree * With "real" top module means that if sub-module is the top-node, * the module that the sub-module belongs-to is found recursively - * @param[in] ys Any yang statement in a yang tree - * @retval ymod The top module or sub-module + * @param[in] ys Any yang statement in a yang tree + * @param[out] ymod The top module or sub-module + * @retval 0 OK, returned module in ymod + * @retval -1 YANG validation error: all yang statements should have a "real" module * @see ys_module * @note For an augmented node, the original module is returned */ -yang_stmt * -ys_real_module(yang_stmt *ys) +int +ys_real_module(yang_stmt *ys, + yang_stmt **ymod) { + int retval = -1; yang_stmt *ym = NULL; yang_stmt *yb; char *name; yang_stmt *yspec; + if (ymod == NULL){ + clicon_err(OE_YANG, EINVAL, "ymod is NULL"); + goto done; + } if ((ym = ys_module(ys)) != NULL){ yspec = ys_spec(ym); - while (yang_keyword_get(ym) == Y_SUBMODULE){ + while (ym && yang_keyword_get(ym) == Y_SUBMODULE){ if ((yb = yang_find(ym, Y_BELONGS_TO, NULL)) == NULL){ clicon_err(OE_YANG, ENOENT, "No belongs-to statement of submodule %s", yang_argument_get(ym)); /* shouldnt happen */ goto done; @@ -1278,12 +1288,14 @@ ys_real_module(yang_stmt *ys) clicon_err(OE_YANG, ENOENT, "Belongs-to statement of submodule %s has no argument", yang_argument_get(ym)); /* shouldnt happen */ goto done; } - ym = yang_find_module_by_name(yspec, name); + if ((ym = yang_find_module_by_name(yspec, name)) == NULL) + goto done; } } - return ym; + *ymod = ym; + retval = 0; done: - return NULL; + return retval; } /*! Find top of tree, the yang specification from within the tree diff --git a/lib/src/clixon_yang_module.c b/lib/src/clixon_yang_module.c index 70770bcd..7f7f588b 100644 --- a/lib/src/clixon_yang_module.c +++ b/lib/src/clixon_yang_module.c @@ -504,6 +504,7 @@ yang_find_module_by_prefix(yang_stmt *ys, clicon_err(OE_YANG, 0, "My yang spec not found"); goto done; } + /* First try own module */ if ((my_ymod = ys_module(ys)) == NULL){ clicon_err(OE_YANG, 0, "My yang module not found"); goto done; @@ -513,6 +514,7 @@ yang_find_module_by_prefix(yang_stmt *ys, ymod = my_ymod; goto done; } + /* If no match, try imported modules */ yimport = NULL; while ((yimport = yn_each(my_ymod, yimport)) != NULL) { if (yang_keyword_get(yimport) != Y_IMPORT) diff --git a/lib/src/clixon_yang_parse_lib.c b/lib/src/clixon_yang_parse_lib.c index 50b98c49..f0131516 100644 --- a/lib/src/clixon_yang_parse_lib.c +++ b/lib/src/clixon_yang_parse_lib.c @@ -353,7 +353,7 @@ yang_expand_grouping(yang_stmt *yn) do { if (yp == ygrouping){ - clicon_err(OE_YANG, EFAULT, "Yang use of grouping %s in module %s is defined inside the grouping (recursion)", + clicon_err(OE_YANG, EFAULT, "Yang use of grouping %s in module %s is defined inside the grouping (recursion), see RFC 7950 Sec 7.12: A grouping MUST NOT reference itself", yang_argument_get(ys), yang_argument_get(ys_module(yn)) );