Skip to content

Commit acd4ba8

Browse files
committed
session client UPDATE store absolute SSH key paths
1 parent 5bc2feb commit acd4ba8

2 files changed

Lines changed: 22 additions & 10 deletions

File tree

src/session_client_ssh.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <assert.h>
2222
#include <errno.h>
2323
#include <fcntl.h>
24+
#include <limits.h>
2425
#include <pthread.h>
2526
#include <pwd.h>
2627
#include <stddef.h>
@@ -876,14 +877,25 @@ _nc_client_ssh_add_keypair(const char *pub_key, const char *priv_key, struct nc_
876877
}
877878

878879
/* add the keys */
879-
++opts->key_count;
880-
opts->keys = nc_realloc(opts->keys, opts->key_count * sizeof *opts->keys);
880+
opts->keys = nc_realloc(opts->keys, (opts->key_count + 1) * sizeof *opts->keys);
881881
NC_CHECK_ERRMEM_RET(!opts->keys, -1);
882-
opts->keys[opts->key_count - 1].pubkey_path = strdup(pub_key);
883-
opts->keys[opts->key_count - 1].privkey_path = strdup(priv_key);
884-
opts->keys[opts->key_count - 1].privkey_crypt = 0;
885882

886-
NC_CHECK_ERRMEM_RET(!opts->keys[opts->key_count - 1].pubkey_path || !opts->keys[opts->key_count - 1].privkey_path, -1);
883+
opts->keys[opts->key_count].pubkey_path = realpath(pub_key, NULL);
884+
if (!opts->keys[opts->key_count].pubkey_path) {
885+
ERR(NULL, "Invalid public key path \"%s\" (%s).", pub_key, strerror(errno));
886+
return -1;
887+
}
888+
opts->keys[opts->key_count].privkey_path = realpath(priv_key, NULL);
889+
if (!opts->keys[opts->key_count].privkey_path) {
890+
ERR(NULL, "Invalid private key path \"%s\" (%s).", priv_key, strerror(errno));
891+
free(opts->keys[opts->key_count].pubkey_path);
892+
return -1;
893+
}
894+
opts->keys[opts->key_count].privkey_crypt = 0;
895+
++opts->key_count;
896+
897+
/* use normalized path */
898+
priv_key = opts->keys[opts->key_count - 1].privkey_path;
887899

888900
/* check encryption */
889901
if ((key = fopen(priv_key, "r"))) {

tests/test_client_ssh.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,14 +383,14 @@ test_nc_client_ssh_adding_keypair(void **state)
383383
assert_int_equal(ret, 1);
384384

385385
/* add second keypair */
386-
ret = nc_client_ssh_add_keypair("key_pub", "key_priv");
386+
ret = nc_client_ssh_add_keypair(TESTS_DIR "/data/key_rsa.pub", TESTS_DIR "/data/key_rsa");
387387
assert_int_equal(ret, 0);
388388
ret = nc_client_ssh_get_keypair_count();
389389
assert_int_equal(ret, 2);
390390
ret = nc_client_ssh_get_keypair(1, &pubkey1, &pubkey2);
391391
assert_int_equal(ret, 0);
392-
assert_string_equal(pubkey1, "key_pub");
393-
assert_string_equal(pubkey2, "key_priv");
392+
assert_string_equal(pubkey1, TESTS_DIR "/data/key_rsa.pub");
393+
assert_string_equal(pubkey2, TESTS_DIR "/data/key_rsa");
394394

395395
/* delete first keypair */
396396
ret = nc_client_ssh_del_keypair(0);
@@ -402,7 +402,7 @@ test_nc_client_ssh_adding_keypair(void **state)
402402
assert_int_equal(ret, -1);
403403

404404
/* try to add keypair that is already set */
405-
ret = nc_client_ssh_add_keypair("key_pub", "key_priv");
405+
ret = nc_client_ssh_add_keypair(TESTS_DIR "/data/key_rsa.pub", TESTS_DIR "/data/key_rsa");
406406
assert_int_equal(ret, -1);
407407
ret = nc_client_ssh_get_keypair_count();
408408
assert_int_equal(ret, 1);

0 commit comments

Comments
 (0)