| 1 |
#include "config.h" |
| 2 |
|
| 3 |
#define LIBSSH_STATIC |
| 4 |
|
| 5 |
#include "sftp.c" |
| 6 |
#include "torture.h" |
| 7 |
|
| 8 |
#include <errno.h> |
| 9 |
#include <pwd.h> |
| 10 |
#include <stdlib.h> |
| 11 |
#include <sys/types.h> |
| 12 |
|
| 13 |
static int |
| 14 |
sshd_setup(void **state) |
| 15 |
{ |
| 16 |
/* |
| 17 |
The SFTP server used for testing is executed as a separate binary, which |
| 18 |
is making the uid_wrapper lose information about what user is used, and |
| 19 |
therefore, pwd is initialized to some bad value. |
| 20 |
If the embedded version using internal-sftp is used in sshd, it works ok. |
| 21 |
*/ |
| 22 |
setenv("TORTURE_SFTP_SERVER", "internal-sftp", 1); |
| 23 |
torture_setup_sshd_server(state, false); |
| 24 |
return 0; |
| 25 |
} |
| 26 |
|
| 27 |
static int |
| 28 |
sshd_teardown(void **state) |
| 29 |
{ |
| 30 |
unsetenv("TORTURE_SFTP_SERVER"); |
| 31 |
torture_teardown_sshd_server(state); |
| 32 |
return 0; |
| 33 |
} |
| 34 |
|
| 35 |
static int |
| 36 |
session_setup(void **state) |
| 37 |
{ |
| 38 |
struct torture_state *s = *state; |
| 39 |
struct passwd *pwd = NULL; |
| 40 |
int rc; |
| 41 |
|
| 42 |
pwd = getpwnam("bob"); |
| 43 |
assert_non_null(pwd); |
| 44 |
|
| 45 |
rc = setuid(pwd->pw_uid); |
| 46 |
assert_return_code(rc, errno); |
| 47 |
|
| 48 |
s->ssh.session = torture_ssh_session(s, |
| 49 |
TORTURE_SSH_SERVER, |
| 50 |
NULL, |
| 51 |
TORTURE_SSH_USER_ALICE, |
| 52 |
NULL); |
| 53 |
assert_non_null(s->ssh.session); |
| 54 |
|
| 55 |
s->ssh.tsftp = torture_sftp_session(s->ssh.session); |
| 56 |
assert_non_null(s->ssh.tsftp); |
| 57 |
|
| 58 |
return 0; |
| 59 |
} |
| 60 |
|
| 61 |
static int |
| 62 |
session_teardown(void **state) |
| 63 |
{ |
| 64 |
struct torture_state *s = *state; |
| 65 |
|
| 66 |
torture_rmdirs(s->ssh.tsftp->testdir); |
| 67 |
torture_sftp_close(s->ssh.tsftp); |
| 68 |
ssh_disconnect(s->ssh.session); |
| 69 |
ssh_free(s->ssh.session); |
| 70 |
|
| 71 |
return 0; |
| 72 |
} |
| 73 |
|
| 74 |
static void |
| 75 |
torture_sftp_home_directory(void **state) |
| 76 |
{ |
| 77 |
struct torture_state *s = *state; |
| 78 |
struct torture_sftp *t = s->ssh.tsftp; |
| 79 |
struct passwd *pwd = NULL; |
| 80 |
char *home_path = NULL; |
| 81 |
int rc; |
| 82 |
|
| 83 |
rc = sftp_extension_supported(t->sftp, "home-directory", "1"); |
| 84 |
if (!rc) { |
| 85 |
skip(); |
| 86 |
} |
| 87 |
|
| 88 |
pwd = getpwnam(TORTURE_SSH_USER_ALICE); |
| 89 |
assert_non_null(pwd); |
| 90 |
|
| 91 |
/* testing for NULL sftp session */ |
| 92 |
home_path = sftp_home_directory(NULL, NULL); |
| 93 |
assert_null(home_path); |
| 94 |
|
| 95 |
/* testing for ~ */ |
| 96 |
/* |
| 97 |
home_path = sftp_home_directory(t->sftp, NULL); |
| 98 |
assert_non_null(home_path); |
| 99 |
assert_string_equal(home_path, pwd->pw_dir); |
| 100 |
SSH_STRING_FREE_CHAR(home_path); |
| 101 |
|
| 102 |
home_path = sftp_home_directory(t->sftp, ""); |
| 103 |
assert_non_null(home_path); |
| 104 |
assert_string_equal(home_path, pwd->pw_dir); |
| 105 |
SSH_STRING_FREE_CHAR(home_path); |
| 106 |
*/ |
| 107 |
|
| 108 |
/* |
| 109 |
OpenSSH code handling this extension does not handle empty string for |
| 110 |
username. getpwnam() also does not handle empty string. |
| 111 |
PR in OpenSSH for fix: |
| 112 |
https://github.com/openssh/openssh-portable/pull/477/ |
| 113 |
*/ |
| 114 |
|
| 115 |
/* testing for ~user */ |
| 116 |
home_path = sftp_home_directory(t->sftp, pwd->pw_name); |
| 117 |
fprintf(stderr, |
| 118 |
"sftp error: %d, ssh error: %s\n", |
| 119 |
sftp_get_error(t->sftp), |
| 120 |
ssh_get_error(t->sftp->session)); |
| 121 |
assert_non_null(home_path); |
| 122 |
assert_string_equal(home_path, pwd->pw_dir); |
| 123 |
SSH_STRING_FREE_CHAR(home_path); |
| 124 |
} |
| 125 |
|
| 126 |
int |
| 127 |
torture_run_tests(void) |
| 128 |
{ |
| 129 |
int rc; |
| 130 |
struct CMUnitTest tests[] = { |
| 131 |
cmocka_unit_test_setup_teardown(torture_sftp_home_directory, |
| 132 |
session_setup, |
| 133 |
session_teardown)}; |
| 134 |
|
| 135 |
ssh_init(); |
| 136 |
|
| 137 |
torture_filter_tests(tests); |
| 138 |
rc = cmocka_run_group_tests(tests, sshd_setup, sshd_teardown); |
| 139 |
ssh_finalize(); |
| 140 |
|
| 141 |
return rc; |
| 142 |
} |