Skip to content

Commit 66c4894

Browse files
author
Petr Hanzlik
committed
repair all bugs
1 parent f26d4da commit 66c4894

1 file changed

Lines changed: 46 additions & 44 deletions

File tree

cli/configuration.c

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414
* https://opensource.org/licenses/BSD-3-Clause
1515
*/
1616

17+
1718
#define _GNU_SOURCE
1819
#include <assert.h>
1920
#include <dirent.h>
2021
#include <errno.h>
2122
#include <fcntl.h>
2223
#include <pwd.h>
2324
#include <stdio.h>
25+
#include <stdint.h>
2426
#include <stdlib.h>
2527
#include <string.h>
2628
#include <sys/stat.h>
@@ -314,27 +316,32 @@ load_config(void)
314316
/* <netconf-client> -> <output-format> */
315317
if (!strcmp(lyd_get_value(child), "json")) {
316318
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;
324324
} /* else default (formatted XML) */
325325
}
326326
#ifdef NC_ENABLED_SSH_TLS
327327
else if (!strcmp(LYD_NAME(child), "authentication")) {
328328
/* <netconf-client> -> <authentication> */
329329
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")) {
331331
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+
332339
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);
334341
} 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);
336343
} 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);
338345
}
339346
}
340347
} else if (!strcmp(LYD_NAME(auth_child), "keys")) {
@@ -368,19 +375,38 @@ load_config(void)
368375
free(netconf_dir);
369376
}
370377

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+
371399
void
372400
store_config(void)
373401
{
374402
#ifdef NC_ENABLED_SSH_TLS
375-
char buf[23];
376403
struct lyd_node *auth, *pref, *keys, *pair;
377404
#endif /* NC_ENABLED_SSH_TLS */
378405
char *netconf_dir = NULL, *history_file = NULL, *config_file = NULL;
379406
struct ly_ctx *ctx = NULL;
380407
struct lyd_node *root = NULL;
381408
struct lys_module *cli = NULL;
382409
const char *str;
383-
int pref_value;
384410

385411
if (ly_ctx_new(NULL, 0, &ctx)) {
386412
ERROR(__func__, "Failed to create context.");
@@ -439,41 +465,17 @@ store_config(void)
439465
if (lyd_new_inner(auth, NULL, "method-preference", 0,&pref)) {
440466
goto cleanup;
441467
}
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;
453471
}
454472

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;
465475
}
466476

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;
477479
}
478480

479481
/* keys */

0 commit comments

Comments
 (0)