Skip to content

Commit 21faa95

Browse files
author
Petr Hanzlik
committed
change load function with ly_find_path
1 parent 66c4894 commit 21faa95

1 file changed

Lines changed: 103 additions & 61 deletions

File tree

cli/configuration.c

Lines changed: 103 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -254,16 +254,27 @@ store_history(void)
254254
free(history_file);
255255
}
256256

257+
void
258+
load_auth_pref(struct lyd_node *match, int auth_pref_type)
259+
{
260+
uint16_t pref_value;
261+
if (!strcmp(lyd_get_value(match), "disabled")) {
262+
pref_value = -1;
263+
} else {
264+
pref_value = strtoul(lyd_get_value(match), NULL, 10);
265+
}
266+
nc_client_ssh_set_auth_pref(auth_pref_type, pref_value);
267+
}
268+
257269
void
258270
load_config(void)
259271
{
260272
char *netconf_dir = NULL, *config_file = NULL;
261-
struct lyd_node *config = NULL, *child;
273+
struct lyd_node *config = NULL, *match = NULL, *client;
262274
struct ly_ctx *ctx = NULL;
263275

264276
#ifdef NC_ENABLED_SSH_TLS
265277
const char *key_pub, *key_priv;
266-
struct lyd_node *auth_child, *pref_child, *key_child, *pair_child;
267278
#endif
268279

269280
if ((netconf_dir = get_netconf_dir()) == NULL) {
@@ -276,6 +287,11 @@ load_config(void)
276287
goto cleanup;
277288
}
278289

290+
if (lys_parse_mem(ctx, netopeer2_cli_yang, LYS_IN_YANG, NULL)) {
291+
ERROR(__func__, "Failed to load netopeer2-cli YANG module from memory.");
292+
goto cleanup;
293+
}
294+
279295
if (asprintf(&config_file, "%s/config.xml", netconf_dir) == -1) {
280296
ERROR(__func__, "asprintf() failed (%s:%d).", __FILE__, __LINE__);
281297
ERROR(__func__, "Unable to load configuration due to the previous error.");
@@ -286,7 +302,7 @@ load_config(void)
286302
goto cleanup;
287303
}
288304

289-
if (lyd_parse_data_path(ctx, config_file, LYD_XML, LYD_PARSE_ONLY | LYD_PARSE_OPAQ, 0, &config)) {
305+
if (lyd_parse_data_path(ctx, config_file, LYD_XML, LYD_PARSE_ONLY , 0, &config)) {
290306
ERROR(__func__, "Failed to load configuration of NETCONF client (lyxml_read_path failed).");
291307
goto cleanup;
292308
}
@@ -296,75 +312,101 @@ load_config(void)
296312
goto cleanup;
297313
}
298314

299-
LY_LIST_FOR(lyd_child(config), child) {
300-
if (!strcmp(LYD_NAME(child), "editor")) {
301-
/* <netconf-client> -> <editor> */
315+
lyd_find_path(config, "/netopeer2-cli:netconf-client", 0, &client);
316+
317+
318+
if (client) {
319+
/* <netconf-client> -> <editor> */
320+
lyd_find_path(client, "editor", 0, &match);
321+
if(match) {
302322
free(opts.config_editor);
303-
opts.config_editor = strdup(lyd_get_value(child));
304-
} else if (!strcmp(LYD_NAME(child), "searchpath")) {
305-
/* <netconf-client> -> <searchpath> */
323+
opts.config_editor = strdup(lyd_get_value(match));
324+
}
325+
match = NULL;
326+
327+
/* <netconf-client> -> <search-path> */
328+
lyd_find_path(client, "search-path", 0, &match);
329+
if(match) {
306330
errno = 0;
307-
if (!mkdir(lyd_get_value(child), 00700) || (errno == EEXIST)) {
331+
if (!mkdir(lyd_get_value(match), 00700) || (errno == EEXIST)) {
308332
if (errno == 0) {
309-
ERROR(__func__, "Search path \"%s\" did not exist, created.", lyd_get_value(child));
333+
ERROR(__func__, "Search path \"%s\" did not exist, created.", lyd_get_value(match));
310334
}
311-
nc_client_set_schema_searchpath(lyd_get_value(child));
335+
nc_client_set_schema_searchpath(lyd_get_value(match));
312336
} else {
313-
ERROR(__func__, "Search path \"%s\" cannot be created (%s).", lyd_get_value(child), strerror(errno));
337+
ERROR(__func__, "Search path \"%s\" cannot be created (%s).", lyd_get_value(match), strerror(errno));
314338
}
315-
} else if (!strcmp(LYD_NAME(child), "output-format")) {
339+
}
340+
match = NULL;
341+
342+
/* <netconf-client> -> <output-format> */
343+
lyd_find_path(client, "output-format", 0, &match);
344+
if(match) {
316345
/* <netconf-client> -> <output-format> */
317-
if (!strcmp(lyd_get_value(child), "json")) {
346+
if (!strcmp(lyd_get_value(match), "json")) {
318347
opts.output_format = LYD_JSON;
319348
} /* else default (formatted XML) */
320-
} else if (!strcmp(LYD_NAME(child), "shrink")) {
349+
}
350+
match = NULL;
351+
352+
lyd_find_path( client, "shrink", 0, &match);
353+
if(match) {
321354
/* <netconf-client> -> <shrink> */
322-
if (!strcmp(lyd_get_value(child), "true")) {
355+
if (!strcmp(lyd_get_value(match), "true")) {
323356
opts.output_flag = 1;
324357
} /* else default (formatted XML) */
325358
}
359+
match = NULL;
326360
#ifdef NC_ENABLED_SSH_TLS
327-
else if (!strcmp(LYD_NAME(child), "authentication")) {
328-
/* <netconf-client> -> <authentication> */
329-
LY_LIST_FOR(lyd_child(child), auth_child) {
330-
if (!strcmp(LYD_NAME(auth_child), "method-preference")) {
331-
LY_LIST_FOR(lyd_child(auth_child), pref_child) {
332-
uint16_t pref_value;
333-
if (!strcmp(lyd_get_value(pref_child), "disabled")) {
334-
pref_value = -1;
335-
} else {
336-
pref_value = strtoul(lyd_get_value(pref_child), NULL, 10);
337-
}
338-
339-
if (!strcmp(LYD_NAME(pref_child), "publickey")) {
340-
nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PUBLICKEY, pref_value);
341-
} else if (!strcmp(LYD_NAME(pref_child), "interactive")) {
342-
nc_client_ssh_set_auth_pref(NC_SSH_AUTH_INTERACTIVE, pref_value);
343-
} else if (!strcmp(LYD_NAME(pref_child), "password")) {
344-
nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PASSWORD, pref_value);
345-
}
346-
}
347-
} else if (!strcmp(LYD_NAME(auth_child), "keys")) {
348-
LY_LIST_FOR(lyd_child(auth_child), key_child) {
349-
if (!strcmp(LYD_NAME(key_child), "pair")) {
350-
key_pub = NULL;
351-
key_priv = NULL;
352-
LY_LIST_FOR(lyd_child(key_child), pair_child) {
353-
if (!strcmp(LYD_NAME(pair_child), "public")) {
354-
key_pub = lyd_get_value(pair_child);
355-
} else if (!strcmp(LYD_NAME(pair_child), "private")) {
356-
key_priv = lyd_get_value(pair_child);
357-
}
358-
}
359-
if (key_pub && key_priv) {
360-
nc_client_ssh_ch_add_keypair(key_pub, key_priv);
361-
nc_client_ssh_add_keypair(key_pub, key_priv);
362-
}
363-
}
364-
}
361+
struct lyd_node *parent = NULL, *key = NULL;
362+
LY_ERR err;
363+
364+
/* <netconf-client> -> <authentication> -> <method-preference>*/
365+
lyd_find_path(client, "authentication/method-preference", 0, &parent);
366+
if (parent) {
367+
lyd_find_path(parent, "publickey", 0, &match);
368+
if(match) {
369+
load_auth_pref(match, NC_SSH_AUTH_PUBLICKEY);
370+
match = NULL;
371+
}
372+
lyd_find_path(parent, "interactive", 0, &match);
373+
if(match) {
374+
load_auth_pref(match, NC_SSH_AUTH_INTERACTIVE);
375+
match = NULL;
376+
}
377+
lyd_find_path(parent, "password", 0, &match);
378+
if(match) {
379+
load_auth_pref(match, NC_SSH_AUTH_PASSWORD);
380+
match = NULL;
381+
}
382+
}
383+
/* <netconf-client> -> <authentication> -> <keys>*/
384+
parent = NULL;
385+
err = lyd_find_path(client, "authentication/keys", 0, &parent);
386+
if (err == LY_SUCCESS && parent) {
387+
LY_LIST_FOR(lyd_child(parent), key) {
388+
key_pub = NULL;
389+
key_priv = NULL;
390+
391+
lyd_find_path(key, "public", 0, &match);
392+
if(match) {
393+
key_pub = lyd_get_value(match);
394+
match = NULL;
395+
}
396+
397+
lyd_find_path(key, "private", 0, &match);
398+
if(match) {
399+
key_priv = lyd_get_value(match);
400+
match = NULL;
401+
}
402+
403+
if (key_pub && key_priv) {
404+
nc_client_ssh_ch_add_keypair(key_pub, key_priv);
405+
nc_client_ssh_add_keypair(key_pub, key_priv);
365406
}
366407
}
367408
}
409+
368410
#endif /* NC_ENABLED_SSH_TLS */
369411
}
370412

@@ -376,19 +418,19 @@ load_config(void)
376418
}
377419

378420
int
379-
store_pref(int pref_type, struct lyd_node *pref_parent, const char *pref_name)
421+
store_auth_pref(int pref_type, struct lyd_node *auth_pref_parent, const char *auth_pref_name)
380422
{
381423
int pref_value;
382424
char buf[23];
383425

384426
pref_value = nc_client_ssh_get_auth_pref(pref_type);
385427
if (pref_value < 0) {
386-
if (lyd_new_term(pref_parent, NULL, pref_name, "disabled", 0, NULL)) {
428+
if (lyd_new_term(auth_pref_parent, NULL, auth_pref_name, "disabled", 0, NULL)) {
387429
return 1;
388430
}
389431
} else {
390432
sprintf(buf, "%d", pref_value);
391-
if (lyd_new_term(pref_parent, NULL, pref_name, buf, 0, NULL)) {
433+
if (lyd_new_term(auth_pref_parent, NULL, auth_pref_name, buf, 0, NULL)) {
392434
return 1;
393435
}
394436
}
@@ -466,15 +508,15 @@ store_config(void)
466508
goto cleanup;
467509
}
468510

469-
if (store_pref(NC_SSH_AUTH_PUBLICKEY, pref, "publickey")) {
511+
if (store_auth_pref(NC_SSH_AUTH_PUBLICKEY, pref, "publickey")) {
470512
goto cleanup;
471513
}
472514

473-
if (store_pref(NC_SSH_AUTH_PASSWORD, pref, "password")) {
515+
if (store_auth_pref(NC_SSH_AUTH_PASSWORD, pref, "password")) {
474516
goto cleanup;
475517
}
476518

477-
if (store_pref(NC_SSH_AUTH_INTERACTIVE, pref, "interactive")) {
519+
if (store_auth_pref(NC_SSH_AUTH_INTERACTIVE, pref, "interactive")) {
478520
goto cleanup;
479521
}
480522

0 commit comments

Comments
 (0)