|
14 | 14 | * https://opensource.org/licenses/BSD-3-Clause |
15 | 15 | */ |
16 | 16 |
|
| 17 | + |
17 | 18 | #define _GNU_SOURCE |
18 | 19 | #include <assert.h> |
19 | 20 | #include <dirent.h> |
20 | 21 | #include <errno.h> |
21 | 22 | #include <fcntl.h> |
22 | 23 | #include <pwd.h> |
23 | 24 | #include <stdio.h> |
| 25 | +#include <stdint.h> |
24 | 26 | #include <stdlib.h> |
25 | 27 | #include <string.h> |
26 | 28 | #include <sys/stat.h> |
@@ -314,27 +316,32 @@ load_config(void) |
314 | 316 | /* <netconf-client> -> <output-format> */ |
315 | 317 | if (!strcmp(lyd_get_value(child), "json")) { |
316 | 318 | opts.output_format = LYD_JSON; |
317 | | - opts.output_flag = 0; |
318 | | - } else if (!strcmp(lyd_get_value(child), "json_noformat")) { |
319 | | - opts.output_format = LYD_JSON; |
320 | | - opts.output_flag = LYD_PRINT_SHRINK; |
321 | | - } else if (!strcmp(lyd_get_value(child), "xml_noformat")) { |
322 | | - opts.output_format = LYD_XML; |
323 | | - opts.output_flag = LYD_PRINT_SHRINK; |
| 319 | + } /* else default (formatted XML) */ |
| 320 | + } else if (!strcmp(LYD_NAME(child), "shrink")) { |
| 321 | + /* <netconf-client> -> <shrink> */ |
| 322 | + if (!strcmp(lyd_get_value(child), "true")) { |
| 323 | + opts.output_flag = 1; |
324 | 324 | } /* else default (formatted XML) */ |
325 | 325 | } |
326 | 326 | #ifdef NC_ENABLED_SSH_TLS |
327 | 327 | else if (!strcmp(LYD_NAME(child), "authentication")) { |
328 | 328 | /* <netconf-client> -> <authentication> */ |
329 | 329 | LY_LIST_FOR(lyd_child(child), auth_child) { |
330 | | - if (!strcmp(LYD_NAME(auth_child), "method-preference ")) { |
| 330 | + if (!strcmp(LYD_NAME(auth_child), "method-preference")) { |
331 | 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 | + |
332 | 339 | if (!strcmp(LYD_NAME(pref_child), "publickey")) { |
333 | | - nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PUBLICKEY, atoi(lyd_get_value(pref_child))); |
| 340 | + nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PUBLICKEY, pref_value); |
334 | 341 | } else if (!strcmp(LYD_NAME(pref_child), "interactive")) { |
335 | | - nc_client_ssh_set_auth_pref(NC_SSH_AUTH_INTERACTIVE, atoi(lyd_get_value(pref_child))); |
| 342 | + nc_client_ssh_set_auth_pref(NC_SSH_AUTH_INTERACTIVE, pref_value); |
336 | 343 | } else if (!strcmp(LYD_NAME(pref_child), "password")) { |
337 | | - nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PASSWORD, atoi(lyd_get_value(pref_child))); |
| 344 | + nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PASSWORD, pref_value); |
338 | 345 | } |
339 | 346 | } |
340 | 347 | } else if (!strcmp(LYD_NAME(auth_child), "keys")) { |
@@ -368,19 +375,38 @@ load_config(void) |
368 | 375 | free(netconf_dir); |
369 | 376 | } |
370 | 377 |
|
| 378 | +int |
| 379 | +store_pref(int pref_type, struct lyd_node *pref_parent, const char *pref_name) |
| 380 | +{ |
| 381 | + int pref_value; |
| 382 | + char buf[23]; |
| 383 | + |
| 384 | + pref_value = nc_client_ssh_get_auth_pref(pref_type); |
| 385 | + if (pref_value < 0) { |
| 386 | + if (lyd_new_term(pref_parent, NULL, pref_name, "disabled", 0, NULL)) { |
| 387 | + return 1; |
| 388 | + } |
| 389 | + } else { |
| 390 | + sprintf(buf, "%d", pref_value); |
| 391 | + if (lyd_new_term(pref_parent, NULL, pref_name, buf, 0, NULL)) { |
| 392 | + return 1; |
| 393 | + } |
| 394 | + } |
| 395 | + |
| 396 | + return 0; |
| 397 | +} |
| 398 | + |
371 | 399 | void |
372 | 400 | store_config(void) |
373 | 401 | { |
374 | 402 | #ifdef NC_ENABLED_SSH_TLS |
375 | | - char buf[23]; |
376 | 403 | struct lyd_node *auth, *pref, *keys, *pair; |
377 | 404 | #endif /* NC_ENABLED_SSH_TLS */ |
378 | 405 | char *netconf_dir = NULL, *history_file = NULL, *config_file = NULL; |
379 | 406 | struct ly_ctx *ctx = NULL; |
380 | 407 | struct lyd_node *root = NULL; |
381 | 408 | struct lys_module *cli = NULL; |
382 | 409 | const char *str; |
383 | | - int pref_value; |
384 | 410 |
|
385 | 411 | if (ly_ctx_new(NULL, 0, &ctx)) { |
386 | 412 | ERROR(__func__, "Failed to create context."); |
@@ -439,41 +465,17 @@ store_config(void) |
439 | 465 | if (lyd_new_inner(auth, NULL, "method-preference", 0,&pref)) { |
440 | 466 | goto cleanup; |
441 | 467 | } |
442 | | - |
443 | | - pref_value = nc_client_ssh_get_auth_pref(NC_SSH_AUTH_PUBLICKEY); |
444 | | - if (pref_value < 0) { |
445 | | - if (lyd_new_term(pref, NULL, "publickey", "disabled", 0, NULL)) { |
446 | | - goto cleanup; |
447 | | - } |
448 | | - } else { |
449 | | - sprintf(buf, "%d", pref_value); |
450 | | - if (lyd_new_term(pref, NULL, "publickey", buf, 0, NULL)) { |
451 | | - goto cleanup; |
452 | | - } |
| 468 | + |
| 469 | + if (store_pref(NC_SSH_AUTH_PUBLICKEY, pref, "publickey")) { |
| 470 | + goto cleanup; |
453 | 471 | } |
454 | 472 |
|
455 | | - pref_value = nc_client_ssh_get_auth_pref(NC_SSH_AUTH_PASSWORD); |
456 | | - if (pref_value < 0) { |
457 | | - if (lyd_new_term(pref, NULL, "password", "disabled", 0, NULL)) { |
458 | | - goto cleanup; |
459 | | - } |
460 | | - } else { |
461 | | - sprintf(buf, "%d", pref_value); |
462 | | - if (lyd_new_term(pref, NULL, "password", buf, 0, NULL)) { |
463 | | - goto cleanup; |
464 | | - } |
| 473 | + if (store_pref(NC_SSH_AUTH_PASSWORD, pref, "password")) { |
| 474 | + goto cleanup; |
465 | 475 | } |
466 | 476 |
|
467 | | - pref_value = nc_client_ssh_get_auth_pref(NC_SSH_AUTH_INTERACTIVE); |
468 | | - if (pref_value < 0) { |
469 | | - if (lyd_new_term(pref, NULL, "interactive", "disabled", 0, NULL)) { |
470 | | - goto cleanup; |
471 | | - } |
472 | | - } else { |
473 | | - sprintf(buf, "%d", nc_client_ssh_get_auth_pref(NC_SSH_AUTH_INTERACTIVE)); |
474 | | - if (lyd_new_term(pref, NULL, "interactive", buf, 0, NULL)) { |
475 | | - goto cleanup; |
476 | | - } |
| 477 | + if (store_pref(NC_SSH_AUTH_INTERACTIVE, pref, "interactive")) { |
| 478 | + goto cleanup; |
477 | 479 | } |
478 | 480 |
|
479 | 481 | /* keys */ |
|
0 commit comments