@@ -1275,7 +1275,7 @@ cmd_auth_help(void)
1275
1275
static void
1276
1276
cmd_knownhosts_help (void )
1277
1277
{
1278
- printf ("knownhosts [ --help] [ --del <key_index>] \n" );
1278
+ printf ("knownhosts ( --help | --del <key_index> | --mode <accept|accept-new|ask|skip|strict>) \n" );
1279
1279
}
1280
1280
1281
1281
static void
@@ -1420,18 +1420,20 @@ cmd_auth(const char *arg, char **UNUSED(tmp_config_file))
1420
1420
static int
1421
1421
cmd_knownhosts (const char * arg , char * * UNUSED (tmp_config_file ))
1422
1422
{
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 ;
1425
1425
size_t line_len ;
1426
- FILE * file ;
1426
+ FILE * file = NULL ;
1427
1427
struct passwd * pwd ;
1428
1428
struct arglist cmd ;
1429
1429
struct option long_options [] = {
1430
1430
{"help" , 0 , 0 , 'h' },
1431
1431
{"del" , 1 , 0 , 'd' },
1432
+ {"mode" , 1 , 0 , 'm' },
1432
1433
{0 , 0 , 0 , 0 }
1433
1434
};
1434
1435
int option_index = 0 , c ;
1436
+ NC_SSH_KNOWNHOSTS_MODE knownhosts_mode ;
1435
1437
1436
1438
optind = 0 ;
1437
1439
@@ -1440,30 +1442,52 @@ cmd_knownhosts(const char *arg, char **UNUSED(tmp_config_file))
1440
1442
return EXIT_FAILURE ;
1441
1443
}
1442
1444
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 ) {
1444
1446
switch (c ) {
1445
1447
case 'h' :
1446
1448
cmd_knownhosts_help ();
1447
- clear_arglist (& cmd );
1448
- return EXIT_SUCCESS ;
1449
- break ;
1449
+ ret = EXIT_SUCCESS ;
1450
+ goto cleanup ;
1450
1451
case 'd' :
1451
1452
del_idx = strtol (optarg , & ptr , 10 );
1452
1453
if ((* ptr != '\0' ) || (del_idx < 0 )) {
1453
1454
ERROR ("knownhosts" , "Wrong index" );
1454
- clear_arglist ( & cmd ) ;
1455
- return EXIT_FAILURE ;
1455
+ ret = EXIT_FAILURE ;
1456
+ goto cleanup ;
1456
1457
}
1457
1458
break ;
1459
+ case 'm' :
1460
+ mode = optarg ;
1461
+ break ;
1458
1462
default :
1459
1463
ERROR ("knownhosts" , "Unknown option -%c" , c );
1460
1464
cmd_knownhosts_help ();
1461
- clear_arglist ( & cmd ) ;
1462
- return EXIT_FAILURE ;
1465
+ ret = EXIT_FAILURE ;
1466
+ goto cleanup ;
1463
1467
}
1464
1468
}
1465
1469
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
+ }
1467
1491
1468
1492
errno = 0 ;
1469
1493
pwd = getpwuid (getuid ());
@@ -1473,19 +1497,20 @@ cmd_knownhosts(const char *arg, char **UNUSED(tmp_config_file))
1473
1497
} else {
1474
1498
ERROR ("knownhosts" , "Failed to get a pwd entry (%s)" , strerror (errno ));
1475
1499
}
1476
- return EXIT_FAILURE ;
1500
+ ret = EXIT_FAILURE ;
1501
+ goto cleanup ;
1477
1502
}
1478
1503
1479
1504
if (asprintf (& kh_file , "%s/.ssh/known_hosts" , pwd -> pw_dir ) == -1 ) {
1480
- return EXIT_FAILURE ;
1505
+ ret = EXIT_FAILURE ;
1506
+ goto cleanup ;
1481
1507
}
1482
1508
1483
1509
if ((file = fopen (kh_file , "r+" )) == NULL ) {
1484
1510
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 ;
1487
1513
}
1488
- free (kh_file );
1489
1514
1490
1515
/* list */
1491
1516
if (del_idx == -1 ) {
@@ -1558,17 +1583,16 @@ cmd_knownhosts(const char *arg, char **UNUSED(tmp_config_file))
1558
1583
text_len = ftell (file );
1559
1584
if (text_len < 0 ) {
1560
1585
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 ;
1563
1588
}
1564
1589
fseek (file , 0 , SEEK_SET );
1565
1590
1566
1591
text = malloc (text_len + 1 );
1567
1592
if (fread (text , 1 , text_len , file ) < (unsigned )text_len ) {
1568
1593
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 ;
1572
1596
}
1573
1597
text [text_len ] = '\0' ;
1574
1598
fseek (file , 0 , SEEK_SET );
@@ -1577,9 +1601,8 @@ cmd_knownhosts(const char *arg, char **UNUSED(tmp_config_file))
1577
1601
1578
1602
if (!ptr || (strlen (ptr ) < 2 )) {
1579
1603
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 ;
1583
1606
}
1584
1607
1585
1608
if (ptr [0 ] == '\n' ) {
@@ -1590,9 +1613,8 @@ cmd_knownhosts(const char *arg, char **UNUSED(tmp_config_file))
1590
1613
written = fwrite (text , 1 , ptr - text , file );
1591
1614
if (written < ptr - text ) {
1592
1615
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 ;
1596
1618
}
1597
1619
1598
1620
ptr = strchr (ptr , '\n' );
@@ -1602,23 +1624,27 @@ cmd_knownhosts(const char *arg, char **UNUSED(tmp_config_file))
1602
1624
/* write the rest */
1603
1625
if (fwrite (ptr , 1 , strlen (ptr ), file ) < strlen (ptr )) {
1604
1626
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 ;
1608
1629
}
1609
1630
written += strlen (ptr );
1610
1631
}
1611
- free (text );
1612
1632
1613
1633
if (ftruncate (fileno (file ), written ) < 0 ) {
1614
1634
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 ;
1617
1637
}
1618
1638
}
1619
1639
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 ;
1622
1648
}
1623
1649
1624
1650
static int
0 commit comments