Skip to content

Commit 8c60c38

Browse files
author
Petr Hanzlik
committed
cli UPDATE add known_hosts mode to config
1 parent ea3943d commit 8c60c38

4 files changed

Lines changed: 72 additions & 334 deletions

File tree

cli/commands.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,8 +1479,11 @@ cmd_knownhosts(const char *arg, char **UNUSED(tmp_config_file))
14791479
goto cleanup;
14801480
}
14811481

1482+
opts.knownhosts_mode = knownhosts_mode;
14821483
nc_client_ssh_set_knownhosts_mode(knownhosts_mode);
14831484
nc_client_ssh_ch_set_knownhosts_mode(knownhosts_mode);
1485+
1486+
store_config();
14841487
goto cleanup;
14851488
}
14861489

cli/configuration.c

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

17-
1817
#define _GNU_SOURCE
1918
#include <assert.h>
2019
#include <dirent.h>
2120
#include <errno.h>
2221
#include <fcntl.h>
2322
#include <pwd.h>
2423
#include <stdio.h>
25-
#include <stdint.h>
2624
#include <stdlib.h>
2725
#include <string.h>
2826
#include <sys/stat.h>
@@ -40,9 +38,9 @@
4038
#include "compat.h"
4139
#include "configuration.h"
4240
#include "linenoise/linenoise.h"
43-
#include "netopeer2-cli.h"
41+
#include "netopeer2_cli_yang.h"
4442

45-
struct cli_opts opts = {.output_format = LYD_XML};
43+
struct cli_opts opts = {.output_format = LYD_XML, .knownhosts_mode = -1};
4644

4745
/* NetConf Client home (appended to ~/) */
4846
#define NCC_DIR ".netopeer2-cli"
@@ -254,10 +252,11 @@ store_history(void)
254252
free(history_file);
255253
}
256254

257-
void
258-
load_auth_pref(struct lyd_node *match, int auth_pref_type)
255+
static void
256+
load_auth_pref(struct lyd_node *match, int auth_pref_type)
259257
{
260258
uint16_t pref_value;
259+
261260
if (!strcmp(lyd_get_value(match), "disabled")) {
262261
pref_value = -1;
263262
} else {
@@ -266,6 +265,24 @@ load_auth_pref(struct lyd_node *match, int auth_pref_type)
266265
nc_client_ssh_set_auth_pref(auth_pref_type, pref_value);
267266
}
268267

268+
static int
269+
str2knownhosts_mode(const char *mode_str)
270+
{
271+
if (!strcmp(mode_str, "accept")) {
272+
return NC_SSH_KNOWNHOSTS_ACCEPT;
273+
} else if (!strcmp(mode_str, "accept-new")) {
274+
return NC_SSH_KNOWNHOSTS_ACCEPT_NEW;
275+
} else if (!strcmp(mode_str, "skip")) {
276+
return NC_SSH_KNOWNHOSTS_SKIP;
277+
} else if (!strcmp(mode_str, "strict")) {
278+
return NC_SSH_KNOWNHOSTS_STRICT;
279+
} else if (!strcmp(mode_str, "ask")) {
280+
return NC_SSH_KNOWNHOSTS_ASK;
281+
} else {
282+
return -1;
283+
}
284+
}
285+
269286
void
270287
load_config(void)
271288
{
@@ -275,7 +292,7 @@ load_config(void)
275292

276293
#ifdef NC_ENABLED_SSH_TLS
277294
const char *key_pub, *key_priv;
278-
struct lyd_node *auth_pref, *parent, *key;
295+
struct lyd_node *parent = NULL, *key = NULL, *auth_pref = NULL;
279296
#endif
280297

281298
if ((netconf_dir = get_netconf_dir()) == NULL) {
@@ -383,10 +400,12 @@ load_config(void)
383400

384401
/* <netconf-client> -> <authentication> -> <knownhost-mode>*/
385402
lyd_find_path(auth_pref, "knownhost-mode", 0, &match);
386-
str2knownhosts_mode(lyd_get_value(match), &opts.knownhosts_mode);
387403

388-
nc_client_ssh_set_knownhosts_mode(opts.knownhosts_mode);
389-
nc_client_ssh_ch_set_knownhosts_mode(opts.knownhosts_mode);
404+
if (match) {
405+
opts.knownhosts_mode = str2knownhosts_mode(lyd_get_value(match));
406+
nc_client_ssh_set_knownhosts_mode(opts.knownhosts_mode);
407+
nc_client_ssh_ch_set_knownhosts_mode(opts.knownhosts_mode);
408+
}
390409
#endif /* NC_ENABLED_SSH_TLS */
391410

392411
cleanup:
@@ -396,8 +415,8 @@ load_config(void)
396415
free(netconf_dir);
397416
}
398417

399-
int
400-
store_auth_pref(int pref_type, struct lyd_node *auth_pref_parent, const char *auth_pref_name)
418+
static int
419+
store_auth_pref(int pref_type, struct lyd_node *auth_pref_parent, const char *auth_pref_name)
401420
{
402421
int pref_value;
403422
char buf[23];
@@ -417,6 +436,24 @@ store_auth_pref(int pref_type, struct lyd_node *auth_pref_parent, const char *au
417436
return 0;
418437
}
419438

439+
static const char *
440+
knownhosts_mode2str(NC_SSH_KNOWNHOSTS_MODE mode)
441+
{
442+
if (mode == NC_SSH_KNOWNHOSTS_ACCEPT) {
443+
return "accept";
444+
} else if (mode == NC_SSH_KNOWNHOSTS_ACCEPT_NEW) {
445+
return "accept-new";
446+
} else if (mode == NC_SSH_KNOWNHOSTS_ASK) {
447+
return "ask";
448+
} else if (mode == NC_SSH_KNOWNHOSTS_SKIP) {
449+
return "skip";
450+
} else if (mode == NC_SSH_KNOWNHOSTS_STRICT) {
451+
return "strict";
452+
} else {
453+
return NULL;
454+
}
455+
}
456+
420457
void
421458
store_config(void)
422459
{
@@ -458,7 +495,7 @@ store_config(void)
458495
} else {
459496
ERROR(__func__, "Unknown format.");
460497
goto cleanup;
461-
}
498+
}
462499

463500
if (lyd_new_term(root, NULL, "output-format", str, 0, NULL)) {
464501
goto cleanup;
@@ -483,18 +520,18 @@ store_config(void)
483520
}
484521

485522
/* pref */
486-
if (lyd_new_inner(auth, NULL, "method-preference", 0,&pref)) {
523+
if (lyd_new_inner(auth, NULL, "method-preference", 0, &pref)) {
487524
goto cleanup;
488525
}
489-
526+
490527
if (store_auth_pref(NC_SSH_AUTH_PUBLICKEY, pref, "publickey")) {
491528
goto cleanup;
492529
}
493-
530+
494531
if (store_auth_pref(NC_SSH_AUTH_PASSWORD, pref, "password")) {
495532
goto cleanup;
496533
}
497-
534+
498535
if (store_auth_pref(NC_SSH_AUTH_INTERACTIVE, pref, "interactive")) {
499536
goto cleanup;
500537
}
@@ -518,6 +555,19 @@ store_config(void)
518555
}
519556
}
520557
}
558+
559+
/* knownhost-mode */
560+
if (opts.knownhosts_mode != -1) {
561+
str = knownhosts_mode2str(opts.knownhosts_mode);
562+
if (!str) {
563+
ERROR(__func__, "Unknown known host mode.");
564+
goto cleanup;
565+
}
566+
567+
if (lyd_new_term(auth, NULL, "knownhost-mode", str, 0, NULL)) {
568+
goto cleanup;
569+
}
570+
}
521571
#endif /* NC_ENABLED_SSH_TLS */
522572

523573
/* get netconf dir */
@@ -542,4 +592,4 @@ store_config(void)
542592
free(history_file);
543593
free(netconf_dir);
544594
free(config_file);
545-
}
595+
}

cli/configuration.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ struct cli_opts {
2626
LYD_FORMAT output_format;
2727
uint32_t output_flag;
2828
char *config_editor;
29+
int knownhosts_mode;
2930
};
3031

3132
extern struct cli_opts opts;

0 commit comments

Comments
 (0)