Skip to content

Commit a385e53

Browse files
author
roman
committed
cli UPDATE add mode option to knownhosts command
1 parent 98cbfa3 commit a385e53

File tree

3 files changed

+83
-40
lines changed

3 files changed

+83
-40
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ set(LIBYANG_DEP_SOVERSION 3.0.0)
4949
set(LIBYANG_DEP_SOVERSION_MAJOR 3)
5050

5151
# libnetconf2 required version
52-
set(LIBNETCONF2_DEP_VERSION 3.4.0)
53-
set(LIBNETCONF2_DEP_SOVERSION 4.3.6)
52+
set(LIBNETCONF2_DEP_VERSION 3.5.0)
53+
set(LIBNETCONF2_DEP_SOVERSION 4.4.0)
5454
set(LIBNETCONF2_DEP_SOVERSION_MAJOR 4)
5555

5656
# sysrepo required version

cli/commands.c

Lines changed: 63 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,7 +1275,7 @@ cmd_auth_help(void)
12751275
static void
12761276
cmd_knownhosts_help(void)
12771277
{
1278-
printf("knownhosts [--help] [--del <key_index>]\n");
1278+
printf("knownhosts (--help | --del <key_index> | --mode <accept|accept-new|ask|skip|strict>)\n");
12791279
}
12801280

12811281
static void
@@ -1420,18 +1420,20 @@ cmd_auth(const char *arg, char **UNUSED(tmp_config_file))
14201420
static int
14211421
cmd_knownhosts(const char *arg, char **UNUSED(tmp_config_file))
14221422
{
1423-
char *ptr, *kh_file, *line = NULL, **pkeys = NULL, *text;
1424-
int del_idx = -1, i, j, pkey_len = 0, written, text_len;
1423+
char *ptr, *kh_file = NULL, *line = NULL, **pkeys = NULL, *text = NULL, *mode = NULL;
1424+
int del_idx = -1, i, j, pkey_len = 0, written, text_len, ret = EXIT_SUCCESS;
14251425
size_t line_len;
1426-
FILE *file;
1426+
FILE *file = NULL;
14271427
struct passwd *pwd;
14281428
struct arglist cmd;
14291429
struct option long_options[] = {
14301430
{"help", 0, 0, 'h'},
14311431
{"del", 1, 0, 'd'},
1432+
{"mode", 1, 0, 'm'},
14321433
{0, 0, 0, 0}
14331434
};
14341435
int option_index = 0, c;
1436+
NC_SSH_KNOWNHOSTS_MODE knownhosts_mode;
14351437

14361438
optind = 0;
14371439

@@ -1440,30 +1442,52 @@ cmd_knownhosts(const char *arg, char **UNUSED(tmp_config_file))
14401442
return EXIT_FAILURE;
14411443
}
14421444

1443-
while ((c = getopt_long(cmd.count, cmd.list, "hd:", long_options, &option_index)) != -1) {
1445+
while ((c = getopt_long(cmd.count, cmd.list, "hd:m:", long_options, &option_index)) != -1) {
14441446
switch (c) {
14451447
case 'h':
14461448
cmd_knownhosts_help();
1447-
clear_arglist(&cmd);
1448-
return EXIT_SUCCESS;
1449-
break;
1449+
ret = EXIT_SUCCESS;
1450+
goto cleanup;
14501451
case 'd':
14511452
del_idx = strtol(optarg, &ptr, 10);
14521453
if ((*ptr != '\0') || (del_idx < 0)) {
14531454
ERROR("knownhosts", "Wrong index");
1454-
clear_arglist(&cmd);
1455-
return EXIT_FAILURE;
1455+
ret = EXIT_FAILURE;
1456+
goto cleanup;
14561457
}
14571458
break;
1459+
case 'm':
1460+
mode = optarg;
1461+
break;
14581462
default:
14591463
ERROR("knownhosts", "Unknown option -%c", c);
14601464
cmd_knownhosts_help();
1461-
clear_arglist(&cmd);
1462-
return EXIT_FAILURE;
1465+
ret = EXIT_FAILURE;
1466+
goto cleanup;
14631467
}
14641468
}
14651469

1466-
clear_arglist(&cmd);
1470+
if (mode) {
1471+
if (!strcmp(mode, "accept")) {
1472+
knownhosts_mode = NC_SSH_KNOWNHOSTS_ACCEPT;
1473+
} else if (!strcmp(mode, "accept-new")) {
1474+
knownhosts_mode = NC_SSH_KNOWNHOSTS_ACCEPT_NEW;
1475+
} else if (!strcmp(mode, "ask")) {
1476+
knownhosts_mode = NC_SSH_KNOWNHOSTS_ASK;
1477+
} else if (!strcmp(mode, "skip")) {
1478+
knownhosts_mode = NC_SSH_KNOWNHOSTS_SKIP;
1479+
} else if (!strcmp(mode, "strict")) {
1480+
knownhosts_mode = NC_SSH_KNOWNHOSTS_STRICT;
1481+
} else {
1482+
ERROR("knownhosts", "Unknown mode \"%s\"", mode);
1483+
ret = EXIT_FAILURE;
1484+
goto cleanup;
1485+
}
1486+
1487+
nc_client_ssh_set_knownhosts_mode(knownhosts_mode);
1488+
nc_client_ssh_ch_set_knownhosts_mode(knownhosts_mode);
1489+
goto cleanup;
1490+
}
14671491

14681492
errno = 0;
14691493
pwd = getpwuid(getuid());
@@ -1473,19 +1497,20 @@ cmd_knownhosts(const char *arg, char **UNUSED(tmp_config_file))
14731497
} else {
14741498
ERROR("knownhosts", "Failed to get a pwd entry (%s)", strerror(errno));
14751499
}
1476-
return EXIT_FAILURE;
1500+
ret = EXIT_FAILURE;
1501+
goto cleanup;
14771502
}
14781503

14791504
if (asprintf(&kh_file, "%s/.ssh/known_hosts", pwd->pw_dir) == -1) {
1480-
return EXIT_FAILURE;
1505+
ret = EXIT_FAILURE;
1506+
goto cleanup;
14811507
}
14821508

14831509
if ((file = fopen(kh_file, "r+")) == NULL) {
14841510
ERROR("knownhosts", "Cannot open \"%s\" (%s)", kh_file, strerror(errno));
1485-
free(kh_file);
1486-
return EXIT_FAILURE;
1511+
ret = EXIT_FAILURE;
1512+
goto cleanup;
14871513
}
1488-
free(kh_file);
14891514

14901515
/* list */
14911516
if (del_idx == -1) {
@@ -1558,17 +1583,16 @@ cmd_knownhosts(const char *arg, char **UNUSED(tmp_config_file))
15581583
text_len = ftell(file);
15591584
if (text_len < 0) {
15601585
ERROR("knownhosts", "ftell on the known hosts file failed (%s)", strerror(errno));
1561-
fclose(file);
1562-
return EXIT_FAILURE;
1586+
ret = EXIT_FAILURE;
1587+
goto cleanup;
15631588
}
15641589
fseek(file, 0, SEEK_SET);
15651590

15661591
text = malloc(text_len + 1);
15671592
if (fread(text, 1, text_len, file) < (unsigned)text_len) {
15681593
ERROR("knownhosts", "Cannot read known hosts file (%s)", strerror(ferror(file)));
1569-
free(text);
1570-
fclose(file);
1571-
return EXIT_FAILURE;
1594+
ret = EXIT_FAILURE;
1595+
goto cleanup;
15721596
}
15731597
text[text_len] = '\0';
15741598
fseek(file, 0, SEEK_SET);
@@ -1577,9 +1601,8 @@ cmd_knownhosts(const char *arg, char **UNUSED(tmp_config_file))
15771601

15781602
if (!ptr || (strlen(ptr) < 2)) {
15791603
ERROR("knownhosts", "Key index %d does not exist", del_idx);
1580-
free(text);
1581-
fclose(file);
1582-
return EXIT_FAILURE;
1604+
ret = EXIT_FAILURE;
1605+
goto cleanup;
15831606
}
15841607

15851608
if (ptr[0] == '\n') {
@@ -1590,9 +1613,8 @@ cmd_knownhosts(const char *arg, char **UNUSED(tmp_config_file))
15901613
written = fwrite(text, 1, ptr - text, file);
15911614
if (written < ptr - text) {
15921615
ERROR("knownhosts", "Failed to write to known hosts file (%s)", strerror(ferror(file)));
1593-
free(text);
1594-
fclose(file);
1595-
return EXIT_FAILURE;
1616+
ret = EXIT_FAILURE;
1617+
goto cleanup;
15961618
}
15971619

15981620
ptr = strchr(ptr, '\n');
@@ -1602,23 +1624,27 @@ cmd_knownhosts(const char *arg, char **UNUSED(tmp_config_file))
16021624
/* write the rest */
16031625
if (fwrite(ptr, 1, strlen(ptr), file) < strlen(ptr)) {
16041626
ERROR("knownhosts", "Failed to write to known hosts file (%s)", strerror(ferror(file)));
1605-
free(text);
1606-
fclose(file);
1607-
return EXIT_FAILURE;
1627+
ret = EXIT_FAILURE;
1628+
goto cleanup;
16081629
}
16091630
written += strlen(ptr);
16101631
}
1611-
free(text);
16121632

16131633
if (ftruncate(fileno(file), written) < 0) {
16141634
ERROR("knownhosts", "ftruncate() on known hosts file failed (%s)", strerror(ferror(file)));
1615-
fclose(file);
1616-
return EXIT_FAILURE;
1635+
ret = EXIT_FAILURE;
1636+
goto cleanup;
16171637
}
16181638
}
16191639

1620-
fclose(file);
1621-
return EXIT_SUCCESS;
1640+
cleanup:
1641+
clear_arglist(&cmd);
1642+
free(kh_file);
1643+
free(text);
1644+
if (file) {
1645+
fclose(file);
1646+
}
1647+
return ret;
16221648
}
16231649

16241650
static int

cli/doc/netopeer2-cli.1

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ Manage the user knownhosts file where all the known SSH server host keys are sto
935935
.PP
936936

937937
.B knownhosts
938-
[\-\-help] [\-\-del <key_index>]
938+
(\-\-help | \-\-del <key_index> | \-\-mode <accept|accept-new|ask|skip|strict>)
939939
.PP
940940
.RS 4
941941

@@ -948,6 +948,23 @@ a modified host key.
948948
.RE
949949
.PP
950950

951+
.B \-\-(m)ode
952+
\fIaccept|accept-new|ask|skip|strict\fR
953+
.RS 4
954+
Set the host key checking mode used when connecting over SSH.
955+
.IP accept
956+
Add the host key to the knownhosts file without prompting and allow connections to servers that changed their host key.
957+
.IP accept-new
958+
Add the host key to the knownhosts file without prompting, but only if it is not already there.
959+
.IP \fIask\fR
960+
Prompt the user to accept the host key. This is the default mode.
961+
.IP skip
962+
Skip the host key and do not add it to the knownhosts file.
963+
.IP strict
964+
Do not add the host key to the knownhosts file and refuse to connect to hosts whose host key is not known or has changed.
965+
.RE
966+
.PP
967+
951968

952969
.SS listen
953970
Listen for a NETCONF Call Home connection.

0 commit comments

Comments
 (0)