Skip to content

Commit 968f38d

Browse files
author
Petr Hanzlik
committed
add knowhost to config
1 parent 21faa95 commit 968f38d

4 files changed

Lines changed: 120 additions & 51 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: 91 additions & 49 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>
@@ -254,7 +252,7 @@ store_history(void)
254252
free(history_file);
255253
}
256254

257-
void
255+
static void
258256
load_auth_pref(struct lyd_node *match, int auth_pref_type)
259257
{
260258
uint16_t pref_value;
@@ -274,7 +272,9 @@ load_config(void)
274272
struct ly_ctx *ctx = NULL;
275273

276274
#ifdef NC_ENABLED_SSH_TLS
277-
const char *key_pub, *key_priv;
275+
const char *key_pub, *key_priv, *mode;
276+
struct lyd_node *parent = NULL, *key = NULL, *auth_pref = NULL;
277+
NC_SSH_KNOWNHOSTS_MODE knownhosts_mode;
278278
#endif
279279

280280
if ((netconf_dir = get_netconf_dir()) == NULL) {
@@ -314,7 +314,6 @@ load_config(void)
314314

315315
lyd_find_path(config, "/netopeer2-cli:netconf-client", 0, &client);
316316

317-
318317
if (client) {
319318
/* <netconf-client> -> <editor> */
320319
lyd_find_path(client, "editor", 0, &match);
@@ -341,7 +340,7 @@ load_config(void)
341340

342341
/* <netconf-client> -> <output-format> */
343342
lyd_find_path(client, "output-format", 0, &match);
344-
if(match) {
343+
if (match) {
345344
/* <netconf-client> -> <output-format> */
346345
if (!strcmp(lyd_get_value(match), "json")) {
347346
opts.output_format = LYD_JSON;
@@ -350,61 +349,81 @@ load_config(void)
350349
match = NULL;
351350

352351
lyd_find_path( client, "shrink", 0, &match);
353-
if(match) {
352+
if (match) {
354353
/* <netconf-client> -> <shrink> */
355354
if (!strcmp(lyd_get_value(match), "true")) {
356355
opts.output_flag = 1;
357356
} /* else default (formatted XML) */
358357
}
359358
match = NULL;
360359
#ifdef NC_ENABLED_SSH_TLS
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);
360+
lyd_find_path(client, "authentication", 0, &auth_pref);
361+
if (auth_pref) {
362+
/* <netconf-client> -> <authentication> -> <method-preference>*/
363+
lyd_find_path(auth_pref, "method-preference", 0, &parent);
364+
if (parent) {
365+
lyd_find_path(parent, "publickey", 0, &match);
366+
if (match) {
367+
load_auth_pref(match, NC_SSH_AUTH_PUBLICKEY);
394368
match = NULL;
395369
}
396-
397-
lyd_find_path(key, "private", 0, &match);
398-
if(match) {
399-
key_priv = lyd_get_value(match);
370+
lyd_find_path(parent, "interactive", 0, &match);
371+
if (match) {
372+
load_auth_pref(match, NC_SSH_AUTH_INTERACTIVE);
400373
match = NULL;
401374
}
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);
375+
lyd_find_path(parent, "password", 0, &match);
376+
if (match) {
377+
load_auth_pref(match, NC_SSH_AUTH_PASSWORD);
378+
match = NULL;
406379
}
407380
}
381+
/* <netconf-client> -> <authentication> -> <keys>*/
382+
parent = NULL;
383+
lyd_find_path(auth_pref, "keys", 0, &parent);
384+
if (parent) {
385+
LY_LIST_FOR(lyd_child(parent), key) {
386+
key_pub = NULL;
387+
key_priv = NULL;
388+
389+
lyd_find_path(key, "public", 0, &match);
390+
if(match) {
391+
key_pub = lyd_get_value(match);
392+
match = NULL;
393+
}
394+
395+
lyd_find_path(key, "private", 0, &match);
396+
if(match) {
397+
key_priv = lyd_get_value(match);
398+
match = NULL;
399+
}
400+
401+
if (key_pub && key_priv) {
402+
nc_client_ssh_ch_add_keypair(key_pub, key_priv);
403+
nc_client_ssh_add_keypair(key_pub, key_priv);
404+
}
405+
}
406+
}
407+
/* <netconf-client> -> <authentication> -> <knownhost-mode>*/
408+
lyd_find_path(auth_pref, "knownhost-mode", 0, &match);
409+
if (match) {
410+
mode = lyd_get_value(match);
411+
if (!strcmp(mode, "accept")) {
412+
knownhosts_mode = NC_SSH_KNOWNHOSTS_ACCEPT;
413+
} else if (!strcmp(mode, "accept-new")) {
414+
knownhosts_mode = NC_SSH_KNOWNHOSTS_ACCEPT_NEW;
415+
} else if (!strcmp(mode, "ask")) {
416+
knownhosts_mode = NC_SSH_KNOWNHOSTS_ASK;
417+
} else if (!strcmp(mode, "skip")) {
418+
knownhosts_mode = NC_SSH_KNOWNHOSTS_SKIP;
419+
} else if (!strcmp(mode, "strict")) {
420+
knownhosts_mode = NC_SSH_KNOWNHOSTS_STRICT;
421+
}
422+
423+
opts.knownhosts_mode = knownhosts_mode;
424+
nc_client_ssh_set_knownhosts_mode(knownhosts_mode);
425+
nc_client_ssh_ch_set_knownhosts_mode(knownhosts_mode);
426+
}
408427
}
409428

410429
#endif /* NC_ENABLED_SSH_TLS */
@@ -417,7 +436,7 @@ load_config(void)
417436
free(netconf_dir);
418437
}
419438

420-
int
439+
static int
421440
store_auth_pref(int pref_type, struct lyd_node *auth_pref_parent, const char *auth_pref_name)
422441
{
423442
int pref_value;
@@ -539,6 +558,29 @@ store_config(void)
539558
}
540559
}
541560
}
561+
562+
/* knownhost-mode */
563+
if (opts.knownhosts_mode) {
564+
if (opts.knownhosts_mode == NC_SSH_KNOWNHOSTS_ACCEPT) {
565+
str = "accept";
566+
} else if (opts.knownhosts_mode == NC_SSH_KNOWNHOSTS_ACCEPT_NEW) {
567+
str = "accept-new";
568+
} else if (opts.knownhosts_mode == NC_SSH_KNOWNHOSTS_ASK) {
569+
str = "ask";
570+
} else if (opts.knownhosts_mode == NC_SSH_KNOWNHOSTS_SKIP) {
571+
str = "skip";
572+
} else if (opts.knownhosts_mode == NC_SSH_KNOWNHOSTS_STRICT) {
573+
str = "strict";
574+
} else {
575+
ERROR(__func__, "Unknown known host mode.");
576+
goto cleanup;
577+
}
578+
579+
if (lyd_new_term(auth, NULL, "knownhost-mode", str, 0, NULL)) {
580+
goto cleanup;
581+
}
582+
}
583+
542584
#endif /* NC_ENABLED_SSH_TLS */
543585

544586
/* get netconf dir */

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+
NC_SSH_KNOWNHOSTS_MODE knownhosts_mode;
2930
};
3031

3132
extern struct cli_opts opts;

cli/netopeer2-cli.h

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,30 @@ char netopeer2_cli_yang[] = {
310310
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
311311
0x20, 0x7d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
312312
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x0a, 0x20, 0x20, 0x20,
313-
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x0a, 0x20,
313+
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x0a, 0x0a,
314+
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
315+
0x6c, 0x65, 0x61, 0x66, 0x20, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x68, 0x6f,
316+
0x73, 0x74, 0x2d, 0x6d, 0x6f, 0x64, 0x65, 0x20, 0x7b, 0x0a, 0x20, 0x20,
317+
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
318+
0x20, 0x20, 0x74, 0x79, 0x70, 0x65, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e,
319+
0x67, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
320+
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x64, 0x65, 0x73, 0x63, 0x72,
321+
0x69, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20,
322+
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
323+
0x20, 0x20, 0x20, 0x22, 0x53, 0x70, 0x65, 0x63, 0x69, 0x66, 0x69, 0x65,
324+
0x73, 0x20, 0x74, 0x68, 0x65, 0x20, 0x6d, 0x6f, 0x64, 0x65, 0x20, 0x66,
325+
0x6f, 0x72, 0x20, 0x68, 0x61, 0x6e, 0x64, 0x6c, 0x69, 0x6e, 0x67, 0x20,
326+
0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x20, 0x68, 0x6f, 0x73, 0x74, 0x20, 0x6b,
327+
0x65, 0x79, 0x73, 0x2e, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
328+
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
329+
0x20, 0x50, 0x6f, 0x73, 0x73, 0x69, 0x62, 0x6c, 0x65, 0x20, 0x76, 0x61,
330+
0x6c, 0x75, 0x65, 0x73, 0x20, 0x69, 0x6e, 0x63, 0x6c, 0x75, 0x64, 0x65,
331+
0x20, 0x27, 0x61, 0x63, 0x63, 0x65, 0x70, 0x74, 0x27, 0x2c, 0x20, 0x27,
332+
0x61, 0x63, 0x63, 0x65, 0x70, 0x74, 0x2d, 0x6e, 0x65, 0x77, 0x27, 0x2c,
333+
0x20, 0x27, 0x61, 0x73, 0x6b, 0x27, 0x2c, 0x20, 0x27, 0x73, 0x6b, 0x69,
334+
0x70, 0x27, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x27, 0x73, 0x74, 0x72, 0x69,
335+
0x63, 0x74, 0x27, 0x2e, 0x22, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20,
314336
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x0a, 0x20, 0x20, 0x20,
315-
0x20, 0x7d, 0x0a, 0x7d, 0x0a, 0x00
337+
0x20, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x7d,
338+
0x0a, 0x7d, 0x0a, 0x00
316339
};

0 commit comments

Comments
 (0)