| 1 |
#include "config.h" |
| 2 |
|
| 3 |
#define LIBSSH_STATIC |
| 4 |
|
| 5 |
#ifndef _WIN32 |
| 6 |
#define _POSIX_PTHREAD_SEMANTICS |
| 7 |
#include <pwd.h> |
| 8 |
#endif |
| 9 |
|
| 10 |
#include "torture.h" |
| 11 |
#include "libssh/options.h" |
| 12 |
#include "libssh/session.h" |
| 13 |
#include "libssh/config_parser.h" |
| 14 |
#include "match.c" |
| 15 |
#include "config.c" |
| 16 |
#include "libssh/socket.h" |
| 17 |
#include "libssh/misc.h" |
| 18 |
|
| 19 |
extern LIBSSH_THREAD int ssh_log_level; |
| 20 |
|
| 21 |
#define USERNAME "testuser" |
| 22 |
#define PROXYCMD "ssh -q -W %h:%p gateway.example.com" |
| 23 |
#define ID_FILE "/etc/xxx" |
| 24 |
#define KEXALGORITHMS "ecdh-sha2-nistp521,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group14-sha1" |
| 25 |
#define HOSTKEYALGORITHMS "ssh-ed25519,ecdsa-sha2-nistp521,ssh-rsa" |
| 26 |
#define PUBKEYACCEPTEDTYPES "rsa-sha2-512,ssh-rsa,ecdsa-sha2-nistp521" |
| 27 |
#define MACS "hmac-sha1,hmac-sha2-256,hmac-sha2-512,[email protected],[email protected],[email protected]" |
| 28 |
#define USER_KNOWN_HOSTS "%d/.ssh/my_known_hosts" |
| 29 |
#define GLOBAL_KNOWN_HOSTS "/etc/ssh/my_ssh_known_hosts" |
| 30 |
#define BIND_ADDRESS "::1" |
| 31 |
|
| 32 |
|
| 33 |
#define LIBSSH_TESTCONFIG1 "libssh_testconfig1.tmp" |
| 34 |
#define LIBSSH_TESTCONFIG2 "libssh_testconfig2.tmp" |
| 35 |
#define LIBSSH_TESTCONFIG3 "libssh_testconfig3.tmp" |
| 36 |
#define LIBSSH_TESTCONFIG4 "libssh_testconfig4.tmp" |
| 37 |
#define LIBSSH_TESTCONFIG5 "libssh_testconfig5.tmp" |
| 38 |
#define LIBSSH_TESTCONFIG6 "libssh_testconfig6.tmp" |
| 39 |
#define LIBSSH_TESTCONFIG7 "libssh_testconfig7.tmp" |
| 40 |
#define LIBSSH_TESTCONFIG8 "libssh_testconfig8.tmp" |
| 41 |
#define LIBSSH_TESTCONFIG9 "libssh_testconfig9.tmp" |
| 42 |
#define LIBSSH_TESTCONFIG10 "libssh_testconfig10.tmp" |
| 43 |
#define LIBSSH_TESTCONFIG11 "libssh_testconfig11.tmp" |
| 44 |
#define LIBSSH_TESTCONFIG12 "libssh_testconfig12.tmp" |
| 45 |
#define LIBSSH_TESTCONFIG14 "libssh_testconfig14.tmp" |
| 46 |
#define LIBSSH_TESTCONFIG15 "libssh_testconfig15.tmp" |
| 47 |
#define LIBSSH_TESTCONFIG16 "libssh_testconfig16.tmp" |
| 48 |
#define LIBSSH_TESTCONFIG17 "libssh_testconfig17.tmp" |
| 49 |
#define LIBSSH_TESTCONFIG18 "libssh_testconfig18.tmp" |
| 50 |
#define LIBSSH_TESTCONFIGGLOB "libssh_testc*[36].tmp" |
| 51 |
#define LIBSSH_TEST_PUBKEYTYPES "libssh_test_PubkeyAcceptedKeyTypes.tmp" |
| 52 |
#define LIBSSH_TEST_PUBKEYALGORITHMS "libssh_test_PubkeyAcceptedAlgorithms.tmp" |
| 53 |
#define LIBSSH_TEST_NONEWLINEEND "libssh_test_NoNewLineEnd.tmp" |
| 54 |
#define LIBSSH_TEST_NONEWLINEONELINE "libssh_test_NoNewLineOneline.tmp" |
| 55 |
#define LIBSSH_TEST_RECURSIVE_INCLUDE "libssh_test_recursive_include.tmp" |
| 56 |
#define LIBSSH_TESTCONFIG_MATCH_COMPLEX "libssh_test_match_complex.tmp" |
| 57 |
#define LIBSSH_TESTCONFIG_LOGLEVEL_MISSING "libssh_test_loglevel_missing.tmp" |
| 58 |
#define LIBSSH_TESTCONFIG_JUMP "libssh_test_jump.tmp" |
| 59 |
|
| 60 |
#define LIBSSH_TESTCONFIG_STRING1 \ |
| 61 |
"User "USERNAME"\nInclude "LIBSSH_TESTCONFIG2"\n\n" |
| 62 |
|
| 63 |
#define LIBSSH_TESTCONFIG_STRING2 \ |
| 64 |
"Include "LIBSSH_TESTCONFIG3"\n" \ |
| 65 |
"ProxyCommand "PROXYCMD"\n\n" |
| 66 |
|
| 67 |
#define LIBSSH_TESTCONFIG_STRING3 \ |
| 68 |
"\n\nIdentityFile "ID_FILE"\n" \ |
| 69 |
"\n\nKexAlgorithms "KEXALGORITHMS"\n" \ |
| 70 |
"\n\nHostKeyAlgorithms "HOSTKEYALGORITHMS"\n" \ |
| 71 |
"\n\nPubkeyAcceptedAlgorithms "PUBKEYACCEPTEDTYPES"\n" \ |
| 72 |
"\n\nMACs "MACS"\n" |
| 73 |
|
| 74 |
/* Multiple Port settings -> parsing returns early. */ |
| 75 |
#define LIBSSH_TESTCONFIG_STRING4 \ |
| 76 |
"Port 123\nPort 456\n" |
| 77 |
|
| 78 |
/* Testing glob include */ |
| 79 |
#define LIBSSH_TESTCONFIG_STRING5 \ |
| 80 |
"User "USERNAME"\nInclude "LIBSSH_TESTCONFIGGLOB"\n\n" \ |
| 81 |
|
| 82 |
#define LIBSSH_TESTCONFIG_STRING6 \ |
| 83 |
"ProxyCommand "PROXYCMD"\n\n" |
| 84 |
|
| 85 |
/* new options */ |
| 86 |
#define LIBSSH_TESTCONFIG_STRING7 \ |
| 87 |
"\tBindAddress "BIND_ADDRESS"\n" \ |
| 88 |
"\tConnectTimeout 30\n" \ |
| 89 |
"\tLogLevel DEBUG3\n" \ |
| 90 |
"\tGlobalKnownHostsFile "GLOBAL_KNOWN_HOSTS"\n" \ |
| 91 |
"\tCompression yes\n" \ |
| 92 |
"\tStrictHostkeyChecking no\n" \ |
| 93 |
"\tGSSAPIDelegateCredentials yes\n" \ |
| 94 |
"\tGSSAPIServerIdentity example.com\n" \ |
| 95 |
"\tGSSAPIClientIdentity home.sweet\n" \ |
| 96 |
"\tUserKnownHostsFile "USER_KNOWN_HOSTS"\n" \ |
| 97 |
"\tRequiredRSASize 2233\n" \ |
| 98 |
"\tGSSAPIKeyExchange yes\n" \ |
| 99 |
"\tGSSAPIKexAlgorithms gss-group14-sha256-\n" |
| 100 |
|
| 101 |
/* authentication methods */ |
| 102 |
#define LIBSSH_TESTCONFIG_STRING8 \ |
| 103 |
"Host gss\n" \ |
| 104 |
"\tGSSAPIAuthentication yes\n" \ |
| 105 |
"Host kbd\n" \ |
| 106 |
"\tKbdInteractiveAuthentication yes\n" \ |
| 107 |
"Host pass\n" \ |
| 108 |
"\tPasswordAuthentication yes\n" \ |
| 109 |
"Host pubkey\n" \ |
| 110 |
"\tPubkeyAuthentication yes\n" \ |
| 111 |
"Host nogss\n" \ |
| 112 |
"\tGSSAPIAuthentication no\n" \ |
| 113 |
"Host nokbd\n" \ |
| 114 |
"\tKbdInteractiveAuthentication no\n" \ |
| 115 |
"Host nopass\n" \ |
| 116 |
"\tPasswordAuthentication no\n" \ |
| 117 |
"Host nopubkey\n" \ |
| 118 |
"\tPubkeyAuthentication no\n" |
| 119 |
|
| 120 |
/* unsupported options and corner cases */ |
| 121 |
#define LIBSSH_TESTCONFIG_STRING9 \ |
| 122 |
"\n" /* empty line */ \ |
| 123 |
"# comment line\n" \ |
| 124 |
" # comment line not starting with hash\n" \ |
| 125 |
"UnknownConfigurationOption yes\n" \ |
| 126 |
"GSSAPIKexAlgorithms yes\n" \ |
| 127 |
"ControlMaster auto\n" /* SOC_NA */ \ |
| 128 |
"VisualHostkey yes\n" /* SOC_UNSUPPORTED */ \ |
| 129 |
"HostName =equal.sign\n" /* valid */ \ |
| 130 |
"ProxyJump = many-spaces.com\n" /* valid */ |
| 131 |
|
| 132 |
/* Match keyword */ |
| 133 |
#define LIBSSH_TESTCONFIG_STRING10 \ |
| 134 |
"Match host example\n" \ |
| 135 |
"\tHostName example.com\n" \ |
| 136 |
"Match host example1,example2\n" \ |
| 137 |
"\tHostName exampleN\n" \ |
| 138 |
"Match user guest\n" \ |
| 139 |
"\tHostName guest.com\n" \ |
| 140 |
"Match user tester host testhost\n" \ |
| 141 |
"\tHostName testhost.com\n" \ |
| 142 |
"Match !user tester host testhost\n" \ |
| 143 |
"\tHostName nonuser-testhost.com\n" \ |
| 144 |
"Match all\n" \ |
| 145 |
"\tHostName all-matched.com\n" \ |
| 146 |
/* Unsupported options */ \ |
| 147 |
"Match originalhost example\n" \ |
| 148 |
"\tHostName original-example.com\n" \ |
| 149 |
"Match localuser guest\n" \ |
| 150 |
"\tHostName local-guest.com\n" |
| 151 |
|
| 152 |
/* ProxyJump */ |
| 153 |
#define LIBSSH_TESTCONFIG_STRING11 \ |
| 154 |
"Host simple\n" \ |
| 155 |
"\tProxyJump jumpbox\n" \ |
| 156 |
"Host user\n" \ |
| 157 |
"\tProxyJump user@jumpbox\n" \ |
| 158 |
"Host port\n" \ |
| 159 |
"\tProxyJump jumpbox:2222\n" \ |
| 160 |
"Host two-step\n" \ |
| 161 |
"\tProxyJump u1@first:222,u2@second:33\n" \ |
| 162 |
"Host three-step\n" \ |
| 163 |
"\tProxyJump u1@first:222,u2@second:33,u3@third:444\n" \ |
| 164 |
"Host none\n" \ |
| 165 |
"\tProxyJump none\n" \ |
| 166 |
"Host only-command\n" \ |
| 167 |
"\tProxyCommand "PROXYCMD"\n" \ |
| 168 |
"\tProxyJump jumpbox\n" \ |
| 169 |
"Host only-jump\n" \ |
| 170 |
"\tProxyJump jumpbox\n" \ |
| 171 |
"\tProxyCommand "PROXYCMD"\n" \ |
| 172 |
"Host ipv6\n" \ |
| 173 |
"\tProxyJump [2620:52:0::fed]\n" |
| 174 |
|
| 175 |
/* RekeyLimit combinations */ |
| 176 |
#define LIBSSH_TESTCONFIG_STRING12 \ |
| 177 |
"Host default\n" \ |
| 178 |
"\tRekeyLimit default none\n" \ |
| 179 |
"Host data1\n" \ |
| 180 |
"\tRekeyLimit 42G\n" \ |
| 181 |
"Host data2\n" \ |
| 182 |
"\tRekeyLimit 31M\n" \ |
| 183 |
"Host data3\n" \ |
| 184 |
"\tRekeyLimit 521K\n" \ |
| 185 |
"Host time1\n" \ |
| 186 |
"\tRekeyLimit default 3D\n" \ |
| 187 |
"Host time2\n" \ |
| 188 |
"\tRekeyLimit default 2h\n" \ |
| 189 |
"Host time3\n" \ |
| 190 |
"\tRekeyLimit default 160m\n" \ |
| 191 |
"Host time4\n" \ |
| 192 |
"\tRekeyLimit default 9600\n" |
| 193 |
|
| 194 |
/* Multiple IdentityFile settings all are applied */ |
| 195 |
#define LIBSSH_TESTCONFIG_STRING13 \ |
| 196 |
"IdentityFile id_rsa_one\n" \ |
| 197 |
"CertificateFile id_rsa_one-cert.pub\n" \ |
| 198 |
"IdentityFile id_ecdsa_two\n" \ |
| 199 |
"CertificateFile id_ecdsa_two-cert.pub\n" \ |
| 200 |
|
| 201 |
/* +,-,^ features for all supported list */ |
| 202 |
/* kex won't work in fips */ |
| 203 |
#define LIBSSH_TESTCONFIG_STRING14 \ |
| 204 |
"HostKeyAlgorithms +ssh-rsa\n" \ |
| 205 |
"Ciphers +aes128-cbc,aes256-cbc\n" \ |
| 206 |
"KexAlgorithms +diffie-hellman-group14-sha1,diffie-hellman-group1-sha1\n" \ |
| 207 |
"MACs +hmac-sha1,[email protected]\n" |
| 208 |
|
| 209 |
/* have to be algorithms which are in the default list */ |
| 210 |
#define LIBSSH_TESTCONFIG_STRING15 \ |
| 211 |
"HostKeyAlgorithms -rsa-sha2-512,rsa-sha2-256\n" \ |
| 212 |
"Ciphers -aes256-ctr\n" \ |
| 213 |
"KexAlgorithms -diffie-hellman-group18-sha512,diffie-hellman-group16-sha512\n" \ |
| 214 |
"MACs [email protected]\n" |
| 215 |
|
| 216 |
#define LIBSSH_TESTCONFIG_STRING16 \ |
| 217 |
"HostKeyAlgorithms ^rsa-sha2-512,rsa-sha2-256\n" \ |
| 218 |
"Ciphers ^aes256-cbc\n" \ |
| 219 |
"KexAlgorithms ^diffie-hellman-group18-sha512,diffie-hellman-group16-sha512\n" \ |
| 220 |
"MACs ^hmac-sha1\n" |
| 221 |
|
| 222 |
/* Connection Multiplexing */ |
| 223 |
#define LIBSSH_TESTCONFIG_STRING17 \ |
| 224 |
"Host simple\n" \ |
| 225 |
"\tControlMaster auto\n" \ |
| 226 |
"\tControlPath /tmp/ssh-%r@%h:%p\n" \ |
| 227 |
"Host none\n" \ |
| 228 |
"\tControlMaster yes\n" \ |
| 229 |
"\tControlPath none\n" |
| 230 |
|
| 231 |
#define LIBSSH_TESTCONFIG_STRING18 \ |
| 232 |
"Host simple\n" \ |
| 233 |
"Host af\n" \ |
| 234 |
"\tAddressFamily any\n" \ |
| 235 |
"Host af4\n" \ |
| 236 |
"\tAddressFamily inet\n" \ |
| 237 |
"Host af6\n" \ |
| 238 |
"\tAddressFamily inet6\n" |
| 239 |
|
| 240 |
#define LIBSSH_TEST_PUBKEYTYPES_STRING \ |
| 241 |
"PubkeyAcceptedKeyTypes "PUBKEYACCEPTEDTYPES"\n" |
| 242 |
|
| 243 |
#define LIBSSH_TEST_PUBKEYALGORITHMS_STRING \ |
| 244 |
"PubkeyAcceptedAlgorithms "PUBKEYACCEPTEDTYPES"\n" |
| 245 |
|
| 246 |
#define LIBSSH_TEST_NONEWLINEEND_STRING \ |
| 247 |
"ConnectTimeout 30\n" \ |
| 248 |
"LogLevel DEBUG3" |
| 249 |
|
| 250 |
#define LIBSSH_TEST_NONEWLINEONELINE_STRING \ |
| 251 |
"ConnectTimeout 30" |
| 252 |
|
| 253 |
#define LIBSSH_TEST_RECURSIVE_INCLUDE_STRING \ |
| 254 |
"Include " LIBSSH_TEST_RECURSIVE_INCLUDE |
| 255 |
|
| 256 |
/* Complex match cases */ |
| 257 |
#define LIBSSH_TESTCONFIG_MATCH_COMPLEX_STRING \ |
| 258 |
"Match originalhost \"Foo,Bar\" exec \"[ \\\"$(ps h o comm p $(ps h o ppid p $PPID))\\\" != \\\"rsync\\\" ]\"\n" \ |
| 259 |
"Match exec \"[ \\\"$(ps h o comm p $(ps h o ppid p $PPID))\\\" != \\\"rsync\\\" ]\"\n" \ |
| 260 |
"\tForwardAgent yes\n" \ |
| 261 |
"\tHostName complex-match\n" |
| 262 |
|
| 263 |
#define LIBSSH_TESTCONFIG_LOGLEVEL_MISSING_STRING "LogLevel\n" |
| 264 |
#define LIBSSH_TESTCONFIG_JUMP_STRING \ |
| 265 |
"# The jump host\n" \ |
| 266 |
"Host ub-jumphost\n" \ |
| 267 |
" HostName 1xxxxxx\n" \ |
| 268 |
" User ubuntu\n" \ |
| 269 |
" IdentityFile ~/of/temp-libssh.pem\n" \ |
| 270 |
" Port 23\n" \ |
| 271 |
" LogLevel DEBUG3\n" \ |
| 272 |
"\n" \ |
| 273 |
"# Cisco Router through Jump Host\n" \ |
| 274 |
"Host cisco-router\n" \ |
| 275 |
" HostName xx.xxxxxxxxx\n" \ |
| 276 |
" User username\n" \ |
| 277 |
" ProxyJump ub-jumphost\n" \ |
| 278 |
" Port 5555\n" \ |
| 279 |
" #RequiredRSASize 512\n" \ |
| 280 |
" PasswordAuthentication yes\n" \ |
| 281 |
" LogLevel DEBUG3\n" |
| 282 |
|
| 283 |
/** |
| 284 |
* @brief helper function loading configuration from either file or string |
| 285 |
*/ |
| 286 |
static void _parse_config(ssh_session session, |
| 287 |
const char *file, const char *string, int expected) |
| 288 |
{ |
| 289 |
int ret = -1; |
| 290 |
|
| 291 |
/* make sure either config file or config string is given, |
| 292 |
* not both */ |
| 293 |
assert_int_not_equal(file == NULL, string == NULL); |
| 294 |
|
| 295 |
if (file != NULL) { |
| 296 |
ret = ssh_config_parse_file(session, file); |
| 297 |
} else if (string != NULL) { |
| 298 |
ret = ssh_config_parse_string(session, string); |
| 299 |
} else { |
| 300 |
/* should not happen */ |
| 301 |
fail(); |
| 302 |
} |
| 303 |
|
| 304 |
/* make sure parsing went as expected */ |
| 305 |
assert_ssh_return_code_equal(session, ret, expected); |
| 306 |
} |
| 307 |
|
| 308 |
static int setup_config_files(void **state) |
| 309 |
{ |
| 310 |
(void) state; /* unused */ |
| 311 |
|
| 312 |
unlink(LIBSSH_TESTCONFIG1); |
| 313 |
unlink(LIBSSH_TESTCONFIG2); |
| 314 |
unlink(LIBSSH_TESTCONFIG3); |
| 315 |
unlink(LIBSSH_TESTCONFIG4); |
| 316 |
unlink(LIBSSH_TESTCONFIG5); |
| 317 |
unlink(LIBSSH_TESTCONFIG6); |
| 318 |
unlink(LIBSSH_TESTCONFIG7); |
| 319 |
unlink(LIBSSH_TESTCONFIG8); |
| 320 |
unlink(LIBSSH_TESTCONFIG9); |
| 321 |
unlink(LIBSSH_TESTCONFIG10); |
| 322 |
unlink(LIBSSH_TESTCONFIG11); |
| 323 |
unlink(LIBSSH_TESTCONFIG12); |
| 324 |
unlink(LIBSSH_TESTCONFIG14); |
| 325 |
unlink(LIBSSH_TESTCONFIG15); |
| 326 |
unlink(LIBSSH_TESTCONFIG16); |
| 327 |
unlink(LIBSSH_TESTCONFIG17); |
| 328 |
unlink(LIBSSH_TESTCONFIG18); |
| 329 |
unlink(LIBSSH_TEST_PUBKEYTYPES); |
| 330 |
unlink(LIBSSH_TEST_PUBKEYALGORITHMS); |
| 331 |
unlink(LIBSSH_TEST_NONEWLINEEND); |
| 332 |
unlink(LIBSSH_TEST_NONEWLINEONELINE); |
| 333 |
unlink(LIBSSH_TESTCONFIG_MATCH_COMPLEX); |
| 334 |
unlink(LIBSSH_TESTCONFIG_LOGLEVEL_MISSING); |
| 335 |
unlink(LIBSSH_TESTCONFIG_JUMP); |
| 336 |
|
| 337 |
torture_write_file(LIBSSH_TESTCONFIG1, |
| 338 |
LIBSSH_TESTCONFIG_STRING1); |
| 339 |
torture_write_file(LIBSSH_TESTCONFIG2, |
| 340 |
LIBSSH_TESTCONFIG_STRING2); |
| 341 |
torture_write_file(LIBSSH_TESTCONFIG3, |
| 342 |
LIBSSH_TESTCONFIG_STRING3); |
| 343 |
|
| 344 |
/* Multiple Port settings -> parsing returns early. */ |
| 345 |
torture_write_file(LIBSSH_TESTCONFIG4, |
| 346 |
LIBSSH_TESTCONFIG_STRING4); |
| 347 |
|
| 348 |
/* Testing glob include */ |
| 349 |
torture_write_file(LIBSSH_TESTCONFIG5, |
| 350 |
LIBSSH_TESTCONFIG_STRING5); |
| 351 |
|
| 352 |
torture_write_file(LIBSSH_TESTCONFIG6, |
| 353 |
LIBSSH_TESTCONFIG_STRING6); |
| 354 |
|
| 355 |
/* new options */ |
| 356 |
torture_write_file(LIBSSH_TESTCONFIG7, |
| 357 |
LIBSSH_TESTCONFIG_STRING7); |
| 358 |
|
| 359 |
/* authentication methods */ |
| 360 |
torture_write_file(LIBSSH_TESTCONFIG8, |
| 361 |
LIBSSH_TESTCONFIG_STRING8); |
| 362 |
|
| 363 |
/* unsupported options and corner cases */ |
| 364 |
torture_write_file(LIBSSH_TESTCONFIG9, |
| 365 |
LIBSSH_TESTCONFIG_STRING9); |
| 366 |
|
| 367 |
/* Match keyword */ |
| 368 |
torture_write_file(LIBSSH_TESTCONFIG10, |
| 369 |
LIBSSH_TESTCONFIG_STRING10); |
| 370 |
|
| 371 |
/* ProxyJump */ |
| 372 |
torture_write_file(LIBSSH_TESTCONFIG11, |
| 373 |
LIBSSH_TESTCONFIG_STRING11); |
| 374 |
|
| 375 |
/* RekeyLimit combinations */ |
| 376 |
torture_write_file(LIBSSH_TESTCONFIG12, |
| 377 |
LIBSSH_TESTCONFIG_STRING12); |
| 378 |
|
| 379 |
/* +,-,^ feature */ |
| 380 |
torture_write_file(LIBSSH_TESTCONFIG14, |
| 381 |
LIBSSH_TESTCONFIG_STRING14); |
| 382 |
torture_write_file(LIBSSH_TESTCONFIG15, |
| 383 |
LIBSSH_TESTCONFIG_STRING15); |
| 384 |
torture_write_file(LIBSSH_TESTCONFIG16, |
| 385 |
LIBSSH_TESTCONFIG_STRING16); |
| 386 |
torture_write_file(LIBSSH_TESTCONFIG17, |
| 387 |
LIBSSH_TESTCONFIG_STRING17); |
| 388 |
torture_write_file(LIBSSH_TESTCONFIG18, |
| 389 |
LIBSSH_TESTCONFIG_STRING18); |
| 390 |
|
| 391 |
torture_write_file(LIBSSH_TEST_PUBKEYTYPES, |
| 392 |
LIBSSH_TEST_PUBKEYTYPES_STRING); |
| 393 |
|
| 394 |
torture_write_file(LIBSSH_TEST_PUBKEYALGORITHMS, |
| 395 |
LIBSSH_TEST_PUBKEYALGORITHMS_STRING); |
| 396 |
|
| 397 |
torture_write_file(LIBSSH_TEST_NONEWLINEEND, |
| 398 |
LIBSSH_TEST_NONEWLINEEND_STRING); |
| 399 |
|
| 400 |
torture_write_file(LIBSSH_TEST_NONEWLINEONELINE, |
| 401 |
LIBSSH_TEST_NONEWLINEONELINE_STRING); |
| 402 |
|
| 403 |
/* Match complex combinations */ |
| 404 |
torture_write_file(LIBSSH_TESTCONFIG_MATCH_COMPLEX, |
| 405 |
LIBSSH_TESTCONFIG_MATCH_COMPLEX_STRING); |
| 406 |
torture_write_file(LIBSSH_TESTCONFIG_LOGLEVEL_MISSING, |
| 407 |
LIBSSH_TESTCONFIG_LOGLEVEL_MISSING_STRING); |
| 408 |
torture_write_file(LIBSSH_TESTCONFIG_JUMP, |
| 409 |
LIBSSH_TESTCONFIG_JUMP_STRING); |
| 410 |
|
| 411 |
return 0; |
| 412 |
} |
| 413 |
|
| 414 |
static int teardown_config_files(void **state) |
| 415 |
{ |
| 416 |
(void) state; /* unused */ |
| 417 |
|
| 418 |
unlink(LIBSSH_TESTCONFIG1); |
| 419 |
unlink(LIBSSH_TESTCONFIG2); |
| 420 |
unlink(LIBSSH_TESTCONFIG3); |
| 421 |
unlink(LIBSSH_TESTCONFIG4); |
| 422 |
unlink(LIBSSH_TESTCONFIG5); |
| 423 |
unlink(LIBSSH_TESTCONFIG6); |
| 424 |
unlink(LIBSSH_TESTCONFIG7); |
| 425 |
unlink(LIBSSH_TESTCONFIG8); |
| 426 |
unlink(LIBSSH_TESTCONFIG9); |
| 427 |
unlink(LIBSSH_TESTCONFIG10); |
| 428 |
unlink(LIBSSH_TESTCONFIG11); |
| 429 |
unlink(LIBSSH_TESTCONFIG12); |
| 430 |
unlink(LIBSSH_TESTCONFIG14); |
| 431 |
unlink(LIBSSH_TESTCONFIG15); |
| 432 |
unlink(LIBSSH_TESTCONFIG16); |
| 433 |
unlink(LIBSSH_TESTCONFIG17); |
| 434 |
unlink(LIBSSH_TESTCONFIG18); |
| 435 |
unlink(LIBSSH_TEST_PUBKEYTYPES); |
| 436 |
unlink(LIBSSH_TEST_PUBKEYALGORITHMS); |
| 437 |
unlink(LIBSSH_TEST_NONEWLINEEND); |
| 438 |
unlink(LIBSSH_TEST_NONEWLINEONELINE); |
| 439 |
unlink(LIBSSH_TESTCONFIG_MATCH_COMPLEX); |
| 440 |
unlink(LIBSSH_TESTCONFIG_LOGLEVEL_MISSING); |
| 441 |
unlink(LIBSSH_TESTCONFIG_JUMP); |
| 442 |
|
| 443 |
return 0; |
| 444 |
} |
| 445 |
|
| 446 |
static int setup(void **state) |
| 447 |
{ |
| 448 |
ssh_session session = NULL; |
| 449 |
char *wd = NULL; |
| 450 |
int verbosity; |
| 451 |
|
| 452 |
session = ssh_new(); |
| 453 |
|
| 454 |
verbosity = torture_libssh_verbosity(); |
| 455 |
ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity); |
| 456 |
wd = torture_get_current_working_dir(); |
| 457 |
ssh_options_set(session, SSH_OPTIONS_SSH_DIR, wd); |
| 458 |
free(wd); |
| 459 |
|
| 460 |
*state = session; |
| 461 |
|
| 462 |
return 0; |
| 463 |
} |
| 464 |
|
| 465 |
static int setup_no_sshdir(void **state) |
| 466 |
{ |
| 467 |
ssh_session session = NULL; |
| 468 |
int verbosity; |
| 469 |
|
| 470 |
session = ssh_new(); |
| 471 |
|
| 472 |
verbosity = torture_libssh_verbosity(); |
| 473 |
ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity); |
| 474 |
|
| 475 |
*state = session; |
| 476 |
|
| 477 |
return 0; |
| 478 |
} |
| 479 |
|
| 480 |
static int teardown(void **state) |
| 481 |
{ |
| 482 |
ssh_free(*state); |
| 483 |
|
| 484 |
return 0; |
| 485 |
} |
| 486 |
|
| 487 |
/** |
| 488 |
* @brief tests ssh config parsing with Include directives |
| 489 |
*/ |
| 490 |
static void torture_config_include(void **state, |
| 491 |
const char *file, const char *string) |
| 492 |
{ |
| 493 |
int ret; |
| 494 |
char *v = NULL; |
| 495 |
char *fips_algos = NULL; |
| 496 |
ssh_session session = *state; |
| 497 |
|
| 498 |
_parse_config(session, file, string, SSH_OK); |
| 499 |
|
| 500 |
/* Test the variable presence */ |
| 501 |
ret = ssh_options_get(session, SSH_OPTIONS_PROXYCOMMAND, &v); |
| 502 |
assert_true(ret == 0); |
| 503 |
assert_non_null(v); |
| 504 |
|
| 505 |
assert_string_equal(v, PROXYCMD); |
| 506 |
SSH_STRING_FREE_CHAR(v); |
| 507 |
|
| 508 |
ret = ssh_options_get(session, SSH_OPTIONS_IDENTITY, &v); |
| 509 |
assert_true(ret == 0); |
| 510 |
assert_non_null(v); |
| 511 |
|
| 512 |
assert_string_equal(v, ID_FILE); |
| 513 |
SSH_STRING_FREE_CHAR(v); |
| 514 |
|
| 515 |
ret = ssh_options_get(session, SSH_OPTIONS_USER, &v); |
| 516 |
assert_true(ret == 0); |
| 517 |
assert_non_null(v); |
| 518 |
|
| 519 |
assert_string_equal(v, USERNAME); |
| 520 |
SSH_STRING_FREE_CHAR(v); |
| 521 |
|
| 522 |
if (ssh_fips_mode()) { |
| 523 |
fips_algos = ssh_keep_fips_algos(SSH_KEX, KEXALGORITHMS); |
| 524 |
assert_non_null(fips_algos); |
| 525 |
assert_string_equal(session->opts.wanted_methods[SSH_KEX], fips_algos); |
| 526 |
SAFE_FREE(fips_algos); |
| 527 |
fips_algos = ssh_keep_fips_algos(SSH_HOSTKEYS, HOSTKEYALGORITHMS); |
| 528 |
assert_non_null(fips_algos); |
| 529 |
assert_string_equal(session->opts.wanted_methods[SSH_HOSTKEYS], |
| 530 |
fips_algos); |
| 531 |
SAFE_FREE(fips_algos); |
| 532 |
fips_algos = ssh_keep_fips_algos(SSH_HOSTKEYS, PUBKEYACCEPTEDTYPES); |
| 533 |
assert_non_null(fips_algos); |
| 534 |
assert_string_equal(session->opts.pubkey_accepted_types, fips_algos); |
| 535 |
SAFE_FREE(fips_algos); |
| 536 |
fips_algos = ssh_keep_fips_algos(SSH_MAC_C_S, MACS); |
| 537 |
assert_non_null(fips_algos); |
| 538 |
assert_string_equal(session->opts.wanted_methods[SSH_MAC_C_S], |
| 539 |
fips_algos); |
| 540 |
SAFE_FREE(fips_algos); |
| 541 |
fips_algos = ssh_keep_fips_algos(SSH_MAC_S_C, MACS); |
| 542 |
assert_non_null(fips_algos); |
| 543 |
assert_string_equal(session->opts.wanted_methods[SSH_MAC_S_C], |
| 544 |
fips_algos); |
| 545 |
SAFE_FREE(fips_algos); |
| 546 |
} else { |
| 547 |
assert_non_null(session->opts.wanted_methods[SSH_KEX]); |
| 548 |
assert_string_equal(session->opts.wanted_methods[SSH_KEX], |
| 549 |
KEXALGORITHMS); |
| 550 |
assert_non_null(session->opts.wanted_methods[SSH_HOSTKEYS]); |
| 551 |
assert_string_equal(session->opts.wanted_methods[SSH_HOSTKEYS], |
| 552 |
HOSTKEYALGORITHMS); |
| 553 |
assert_non_null(session->opts.pubkey_accepted_types); |
| 554 |
assert_string_equal(session->opts.pubkey_accepted_types, |
| 555 |
PUBKEYACCEPTEDTYPES); |
| 556 |
assert_non_null(session->opts.wanted_methods[SSH_MAC_S_C]); |
| 557 |
assert_string_equal(session->opts.wanted_methods[SSH_MAC_C_S], MACS); |
| 558 |
assert_non_null(session->opts.wanted_methods[SSH_MAC_S_C]); |
| 559 |
assert_string_equal(session->opts.wanted_methods[SSH_MAC_S_C], MACS); |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
/** |
| 564 |
* @brief tests ssh_config_parse_file with Include directives from file |
| 565 |
*/ |
| 566 |
static void torture_config_include_file(void **state) |
| 567 |
{ |
| 568 |
torture_config_include(state, LIBSSH_TESTCONFIG1, NULL); |
| 569 |
} |
| 570 |
|
| 571 |
/** |
| 572 |
* @brief tests ssh_config_parse_string with Include directives from string |
| 573 |
*/ |
| 574 |
static void torture_config_include_string(void **state) |
| 575 |
{ |
| 576 |
torture_config_include(state, NULL, LIBSSH_TESTCONFIG_STRING1); |
| 577 |
} |
| 578 |
|
| 579 |
/** |
| 580 |
* @brief tests ssh_config_parse_file with recursive Include directives from file |
| 581 |
*/ |
| 582 |
static void torture_config_include_recursive_file(void **state) |
| 583 |
{ |
| 584 |
_parse_config(*state, LIBSSH_TEST_RECURSIVE_INCLUDE, NULL, SSH_OK); |
| 585 |
} |
| 586 |
|
| 587 |
/** |
| 588 |
* @brief tests ssh_config_parse_string with Include directives from string |
| 589 |
*/ |
| 590 |
static void torture_config_include_recursive_string(void **state) |
| 591 |
{ |
| 592 |
_parse_config(*state, NULL, LIBSSH_TEST_RECURSIVE_INCLUDE_STRING, SSH_OK); |
| 593 |
} |
| 594 |
|
| 595 |
/** |
| 596 |
* @brief tests ssh_config_parse_file with multiple Port settings. |
| 597 |
*/ |
| 598 |
static void torture_config_double_ports_file(void **state) |
| 599 |
{ |
| 600 |
_parse_config(*state, LIBSSH_TESTCONFIG4, NULL, SSH_OK); |
| 601 |
} |
| 602 |
|
| 603 |
/** |
| 604 |
* @brief tests ssh_config_parse_string with multiple Port settings. |
| 605 |
*/ |
| 606 |
static void torture_config_double_ports_string(void **state) |
| 607 |
{ |
| 608 |
_parse_config(*state, NULL, LIBSSH_TESTCONFIG_STRING4, SSH_OK); |
| 609 |
} |
| 610 |
|
| 611 |
static void torture_config_glob(void **state, |
| 612 |
const char *file, const char *string) |
| 613 |
{ |
| 614 |
#if defined(HAVE_GLOB) && defined(HAVE_GLOB_GL_FLAGS_MEMBER) |
| 615 |
int ret; |
| 616 |
char *v; |
| 617 |
ssh_session session = *state; |
| 618 |
|
| 619 |
_parse_config(session, file, string, SSH_OK); |
| 620 |
|
| 621 |
/* Test the variable presence */ |
| 622 |
|
| 623 |
ret = ssh_options_get(session, SSH_OPTIONS_PROXYCOMMAND, &v); |
| 624 |
assert_true(ret == 0); |
| 625 |
assert_non_null(v); |
| 626 |
|
| 627 |
assert_string_equal(v, PROXYCMD); |
| 628 |
SSH_STRING_FREE_CHAR(v); |
| 629 |
|
| 630 |
ret = ssh_options_get(session, SSH_OPTIONS_IDENTITY, &v); |
| 631 |
assert_true(ret == 0); |
| 632 |
assert_non_null(v); |
| 633 |
|
| 634 |
assert_string_equal(v, ID_FILE); |
| 635 |
SSH_STRING_FREE_CHAR(v); |
| 636 |
#endif /* HAVE_GLOB && HAVE_GLOB_GL_FLAGS_MEMBER */ |
| 637 |
} |
| 638 |
|
| 639 |
static void torture_config_glob_file(void **state) |
| 640 |
{ |
| 641 |
torture_config_glob(state, LIBSSH_TESTCONFIG5, NULL); |
| 642 |
} |
| 643 |
|
| 644 |
static void torture_config_glob_string(void **state) |
| 645 |
{ |
| 646 |
torture_config_glob(state, NULL, LIBSSH_TESTCONFIG_STRING5); |
| 647 |
} |
| 648 |
|
| 649 |
/** |
| 650 |
* @brief Verify the new options are passed from configuration |
| 651 |
*/ |
| 652 |
static void torture_config_new(void ** state, |
| 653 |
const char *file, const char *string) |
| 654 |
{ |
| 655 |
ssh_session session = *state; |
| 656 |
|
| 657 |
_parse_config(session, file, string, SSH_OK); |
| 658 |
|
| 659 |
assert_string_equal(session->opts.knownhosts, USER_KNOWN_HOSTS); |
| 660 |
assert_string_equal(session->opts.global_knownhosts, GLOBAL_KNOWN_HOSTS); |
| 661 |
assert_int_equal(session->opts.timeout, 30); |
| 662 |
assert_string_equal(session->opts.bindaddr, BIND_ADDRESS); |
| 663 |
#ifdef WITH_ZLIB |
| 664 |
assert_string_equal(session->opts.wanted_methods[SSH_COMP_C_S], |
| 665 |
"[email protected],none"); |
| 666 |
assert_string_equal(session->opts.wanted_methods[SSH_COMP_S_C], |
| 667 |
"[email protected],none"); |
| 668 |
#else |
| 669 |
assert_string_equal(session->opts.wanted_methods[SSH_COMP_C_S], |
| 670 |
"none"); |
| 671 |
assert_string_equal(session->opts.wanted_methods[SSH_COMP_S_C], |
| 672 |
"none"); |
| 673 |
#endif /* WITH_ZLIB */ |
| 674 |
assert_int_equal(session->opts.StrictHostKeyChecking, 0); |
| 675 |
assert_int_equal(session->opts.gss_delegate_creds, 1); |
| 676 |
assert_string_equal(session->opts.gss_server_identity, "example.com"); |
| 677 |
assert_string_equal(session->opts.gss_client_identity, "home.sweet"); |
| 678 |
#ifdef WITH_GSSAPI |
| 679 |
assert_true(session->opts.gssapi_key_exchange); |
| 680 |
assert_string_equal(session->opts.gssapi_key_exchange_algs, |
| 681 |
"gss-group14-sha256-"); |
| 682 |
#endif /* WITH_GSSAPI */ |
| 683 |
|
| 684 |
assert_int_equal(ssh_get_log_level(), SSH_LOG_TRACE); |
| 685 |
assert_int_equal(session->common.log_verbosity, SSH_LOG_TRACE); |
| 686 |
assert_int_equal(session->opts.rsa_min_size, 2233); |
| 687 |
} |
| 688 |
|
| 689 |
static void torture_config_new_file(void **state) |
| 690 |
{ |
| 691 |
torture_config_new(state, LIBSSH_TESTCONFIG7, NULL); |
| 692 |
} |
| 693 |
|
| 694 |
static void torture_config_new_string(void **state) |
| 695 |
{ |
| 696 |
torture_config_new(state, NULL, LIBSSH_TESTCONFIG_STRING7); |
| 697 |
} |
| 698 |
|
| 699 |
/** |
| 700 |
* @brief Verify the authentication methods from configuration are effective |
| 701 |
*/ |
| 702 |
static void torture_config_auth_methods(void **state, |
| 703 |
const char *file, const char *string) |
| 704 |
{ |
| 705 |
ssh_session session = *state; |
| 706 |
|
| 707 |
/* gradually disable all the methods based on different hosts */ |
| 708 |
ssh_options_set(session, SSH_OPTIONS_HOST, "nogss"); |
| 709 |
_parse_config(session, file, string, SSH_OK); |
| 710 |
assert_false(session->opts.flags & SSH_OPT_FLAG_GSSAPI_AUTH); |
| 711 |
assert_true(session->opts.flags & SSH_OPT_FLAG_KBDINT_AUTH); |
| 712 |
|
| 713 |
ssh_options_set(session, SSH_OPTIONS_HOST, "nokbd"); |
| 714 |
_parse_config(session, file, string, SSH_OK); |
| 715 |
assert_false(session->opts.flags & SSH_OPT_FLAG_KBDINT_AUTH); |
| 716 |
|
| 717 |
ssh_options_set(session, SSH_OPTIONS_HOST, "nopass"); |
| 718 |
_parse_config(session, file, string, SSH_OK); |
| 719 |
assert_false(session->opts.flags & SSH_OPT_FLAG_PASSWORD_AUTH); |
| 720 |
|
| 721 |
ssh_options_set(session, SSH_OPTIONS_HOST, "nopubkey"); |
| 722 |
_parse_config(session, file, string, SSH_OK); |
| 723 |
assert_false(session->opts.flags & SSH_OPT_FLAG_PUBKEY_AUTH); |
| 724 |
|
| 725 |
/* no method should be left enabled */ |
| 726 |
assert_int_equal(session->opts.flags, 0); |
| 727 |
|
| 728 |
/* gradually enable them again */ |
| 729 |
torture_reset_config(session); |
| 730 |
ssh_options_set(session, SSH_OPTIONS_HOST, "gss"); |
| 731 |
_parse_config(session, file, string, SSH_OK); |
| 732 |
assert_true(session->opts.flags & SSH_OPT_FLAG_GSSAPI_AUTH); |
| 733 |
assert_false(session->opts.flags & SSH_OPT_FLAG_KBDINT_AUTH); |
| 734 |
|
| 735 |
ssh_options_set(session, SSH_OPTIONS_HOST, "kbd"); |
| 736 |
_parse_config(session, file, string, SSH_OK); |
| 737 |
assert_true(session->opts.flags & SSH_OPT_FLAG_KBDINT_AUTH); |
| 738 |
|
| 739 |
ssh_options_set(session, SSH_OPTIONS_HOST, "pass"); |
| 740 |
_parse_config(session, file, string, SSH_OK); |
| 741 |
assert_true(session->opts.flags & SSH_OPT_FLAG_PASSWORD_AUTH); |
| 742 |
|
| 743 |
ssh_options_set(session, SSH_OPTIONS_HOST, "pubkey"); |
| 744 |
_parse_config(session, file, string, SSH_OK); |
| 745 |
assert_true(session->opts.flags & SSH_OPT_FLAG_PUBKEY_AUTH); |
| 746 |
} |
| 747 |
|
| 748 |
/** |
| 749 |
* @brief Verify the authentication methods from configuration file |
| 750 |
* are effective |
| 751 |
*/ |
| 752 |
static void torture_config_auth_methods_file(void **state) |
| 753 |
{ |
| 754 |
torture_config_auth_methods(state, LIBSSH_TESTCONFIG8, NULL); |
| 755 |
} |
| 756 |
|
| 757 |
/** |
| 758 |
* @brief Verify the authentication methods from configuration string |
| 759 |
* are effective |
| 760 |
*/ |
| 761 |
static void torture_config_auth_methods_string(void **state) |
| 762 |
{ |
| 763 |
torture_config_auth_methods(state, NULL, LIBSSH_TESTCONFIG_STRING8); |
| 764 |
} |
| 765 |
|
| 766 |
/** |
| 767 |
* @brief Helper for checking hostname, username and port of ssh_jump_info_struct |
| 768 |
*/ |
| 769 |
static void |
| 770 |
helper_proxy_jump_check(struct ssh_iterator *jump, |
| 771 |
const char *hostname, |
| 772 |
const char *username, |
| 773 |
const char *port) |
| 774 |
{ |
| 775 |
struct ssh_jump_info_struct *jis = |
| 776 |
ssh_iterator_value(struct ssh_jump_info_struct *, jump); |
| 777 |
|
| 778 |
assert_string_equal(jis->hostname, hostname); |
| 779 |
|
| 780 |
if (username != NULL) { |
| 781 |
assert_string_equal(jis->username, username); |
| 782 |
} else { |
| 783 |
assert_null(jis->username); |
| 784 |
} |
| 785 |
|
| 786 |
if (port != NULL) { |
| 787 |
int iport = strtol(port, NULL, 10); |
| 788 |
assert_int_equal(jis->port, iport); |
| 789 |
} else { |
| 790 |
assert_int_equal(jis->port, 22); |
| 791 |
} |
| 792 |
} |
| 793 |
|
| 794 |
/** |
| 795 |
* @brief Verify the configuration parser does not choke on unknown |
| 796 |
* or unsupported configuration options |
| 797 |
*/ |
| 798 |
static void torture_config_unknown(void **state, |
| 799 |
const char *file, const char *string) |
| 800 |
{ |
| 801 |
ssh_session session = *state; |
| 802 |
int ret = 0; |
| 803 |
|
| 804 |
/* test corner cases */ |
| 805 |
/* Without libssh proxy jump */ |
| 806 |
torture_setenv("OPENSSH_PROXYJUMP", "1"); |
| 807 |
_parse_config(session, file, string, SSH_OK); |
| 808 |
assert_string_equal(session->opts.ProxyCommand, |
| 809 |
"ssh -W '[%h]:%p' many-spaces.com"); |
| 810 |
assert_string_equal(session->opts.host, "equal.sign"); |
| 811 |
|
| 812 |
ret = ssh_config_parse_file(session, "/etc/ssh/ssh_config"); |
| 813 |
assert_true(ret == 0); |
| 814 |
ret = ssh_config_parse_file(session, GLOBAL_CLIENT_CONFIG); |
| 815 |
assert_true(ret == 0); |
| 816 |
torture_unsetenv("OPENSSH_PROXYJUMP"); |
| 817 |
} |
| 818 |
|
| 819 |
/** |
| 820 |
* @brief Verify the configuration parser does not choke on unknown |
| 821 |
* or unsupported configuration options in configuration file |
| 822 |
*/ |
| 823 |
static void torture_config_unknown_file(void **state) |
| 824 |
{ |
| 825 |
torture_config_unknown(state, LIBSSH_TESTCONFIG9, NULL); |
| 826 |
} |
| 827 |
|
| 828 |
/** |
| 829 |
* @brief Verify the configuration parser does not choke on unknown |
| 830 |
* or unsupported configuration options in configuration string |
| 831 |
*/ |
| 832 |
static void torture_config_unknown_string(void **state) |
| 833 |
{ |
| 834 |
torture_config_unknown(state, NULL, LIBSSH_TESTCONFIG_STRING9); |
| 835 |
} |
| 836 |
|
| 837 |
/** |
| 838 |
* @brief Verify the configuration parser accepts Match keyword with |
| 839 |
* full OpenSSH syntax. |
| 840 |
*/ |
| 841 |
static void torture_config_match(void **state, |
| 842 |
const char *file, const char *string) |
| 843 |
{ |
| 844 |
ssh_session session = *state; |
| 845 |
char *localuser = NULL; |
| 846 |
const char *config = NULL; |
| 847 |
char config_string[1024]; |
| 848 |
|
| 849 |
/* Without any settings we should get all-matched.com hostname */ |
| 850 |
torture_reset_config(session); |
| 851 |
ssh_options_set(session, SSH_OPTIONS_HOST, "unmatched"); |
| 852 |
_parse_config(session, file, string, SSH_OK); |
| 853 |
assert_string_equal(session->opts.host, "all-matched.com"); |
| 854 |
|
| 855 |
/* Hostname example does simple hostname matching */ |
| 856 |
torture_reset_config(session); |
| 857 |
ssh_options_set(session, SSH_OPTIONS_HOST, "example"); |
| 858 |
_parse_config(session, file, string, SSH_OK); |
| 859 |
assert_string_equal(session->opts.host, "example.com"); |
| 860 |
|
| 861 |
/* We can match also both hosts from a comma separated list */ |
| 862 |
torture_reset_config(session); |
| 863 |
ssh_options_set(session, SSH_OPTIONS_HOST, "example1"); |
| 864 |
_parse_config(session, file, string, SSH_OK); |
| 865 |
assert_string_equal(session->opts.host, "exampleN"); |
| 866 |
|
| 867 |
torture_reset_config(session); |
| 868 |
ssh_options_set(session, SSH_OPTIONS_HOST, "example2"); |
| 869 |
_parse_config(session, file, string, SSH_OK); |
| 870 |
assert_string_equal(session->opts.host, "exampleN"); |
| 871 |
|
| 872 |
/* We can match by user */ |
| 873 |
torture_reset_config(session); |
| 874 |
ssh_options_set(session, SSH_OPTIONS_USER, "guest"); |
| 875 |
_parse_config(session, file, string, SSH_OK); |
| 876 |
assert_string_equal(session->opts.host, "guest.com"); |
| 877 |
|
| 878 |
/* We can combine two options on a single line to match both of them */ |
| 879 |
torture_reset_config(session); |
| 880 |
ssh_options_set(session, SSH_OPTIONS_USER, "tester"); |
| 881 |
ssh_options_set(session, SSH_OPTIONS_HOST, "testhost"); |
| 882 |
_parse_config(session, file, string, SSH_OK); |
| 883 |
assert_string_equal(session->opts.host, "testhost.com"); |
| 884 |
|
| 885 |
/* We can also negate conditions */ |
| 886 |
torture_reset_config(session); |
| 887 |
ssh_options_set(session, SSH_OPTIONS_USER, "not-tester"); |
| 888 |
ssh_options_set(session, SSH_OPTIONS_HOST, "testhost"); |
| 889 |
_parse_config(session, file, string, SSH_OK); |
| 890 |
assert_string_equal(session->opts.host, "nonuser-testhost.com"); |
| 891 |
|
| 892 |
/* In this part, we try various other config files and strings. */ |
| 893 |
|
| 894 |
/* Match final is not completely supported, but should do quite much the |
| 895 |
* same as "match all". The trailing "all" is not mandatory. */ |
| 896 |
config = "Match final all\n" |
| 897 |
"\tHostName final-all.com\n"; |
| 898 |
if (file != NULL) { |
| 899 |
torture_write_file(file, config); |
| 900 |
} else { |
| 901 |
string = config; |
| 902 |
} |
| 903 |
torture_reset_config(session); |
| 904 |
_parse_config(session, file, string, SSH_OK); |
| 905 |
assert_string_equal(session->opts.host, "final-all.com"); |
| 906 |
|
| 907 |
config = "Match final\n" |
| 908 |
"\tHostName final.com\n"; |
| 909 |
if (file != NULL) { |
| 910 |
torture_write_file(file, config); |
| 911 |
} else { |
| 912 |
string = config; |
| 913 |
} |
| 914 |
torture_reset_config(session); |
| 915 |
_parse_config(session, file, string, SSH_OK); |
| 916 |
assert_string_equal(session->opts.host, "final.com"); |
| 917 |
|
| 918 |
/* Match canonical is not completely supported, but should do quite |
| 919 |
* much the same as "match all". The trailing "all" is not mandatory. */ |
| 920 |
config = "Match canonical all\n" |
| 921 |
"\tHostName canonical-all.com\n"; |
| 922 |
if (file != NULL) { |
| 923 |
torture_write_file(file, config); |
| 924 |
} else { |
| 925 |
string = config; |
| 926 |
} |
| 927 |
torture_reset_config(session); |
| 928 |
_parse_config(session, file, string, SSH_OK); |
| 929 |
assert_string_equal(session->opts.host, "canonical-all.com"); |
| 930 |
|
| 931 |
config = "Match canonical all\n" |
| 932 |
"\tHostName canonical.com\n"; |
| 933 |
if (file != NULL) { |
| 934 |
torture_write_file(file, config); |
| 935 |
} else { |
| 936 |
string = config; |
| 937 |
} |
| 938 |
torture_reset_config(session); |
| 939 |
_parse_config(session, file, string, SSH_OK); |
| 940 |
assert_string_equal(session->opts.host, "canonical.com"); |
| 941 |
|
| 942 |
localuser = ssh_get_local_username(); |
| 943 |
assert_non_null(localuser); |
| 944 |
snprintf(config_string, sizeof(config_string), |
| 945 |
"Match localuser %s\n" |
| 946 |
"\tHostName otherhost\n", |
| 947 |
localuser); |
| 948 |
config = config_string; |
| 949 |
free(localuser); |
| 950 |
if (file != NULL) { |
| 951 |
torture_write_file(file, config); |
| 952 |
} else { |
| 953 |
string = config; |
| 954 |
} |
| 955 |
torture_reset_config(session); |
| 956 |
_parse_config(session, file, string, SSH_OK); |
| 957 |
assert_string_equal(session->opts.host, "otherhost"); |
| 958 |
|
| 959 |
config = "Match exec true\n" |
| 960 |
"\tHostName execed-true.com\n"; |
| 961 |
if (file != NULL) { |
| 962 |
torture_write_file(file, config); |
| 963 |
} else { |
| 964 |
string = config; |
| 965 |
} |
| 966 |
torture_reset_config(session); |
| 967 |
_parse_config(session, file, string, SSH_OK); |
| 968 |
#ifndef WITH_EXEC |
| 969 |
/* The match exec is not supported on windows at this moment */ |
| 970 |
assert_string_equal(session->opts.host, "otherhost"); |
| 971 |
#else |
| 972 |
assert_string_equal(session->opts.host, "execed-true.com"); |
| 973 |
#endif |
| 974 |
|
| 975 |
config = "Match !exec false\n" |
| 976 |
"\tHostName execed-false.com\n"; |
| 977 |
if (file != NULL) { |
| 978 |
torture_write_file(file, config); |
| 979 |
} else { |
| 980 |
string = config; |
| 981 |
} |
| 982 |
torture_reset_config(session); |
| 983 |
_parse_config(session, file, string, SSH_OK); |
| 984 |
#ifndef WITH_EXEC |
| 985 |
/* The match exec is not supported on windows at this moment */ |
| 986 |
assert_string_equal(session->opts.host, "otherhost"); |
| 987 |
#else |
| 988 |
assert_string_equal(session->opts.host, "execed-false.com"); |
| 989 |
#endif |
| 990 |
|
| 991 |
config = "Match exec \"test 1 -eq 1\"\n" |
| 992 |
"\tHostName execed-arguments.com\n"; |
| 993 |
if (file != NULL) { |
| 994 |
torture_write_file(file, config); |
| 995 |
} else { |
| 996 |
string = config; |
| 997 |
} |
| 998 |
torture_reset_config(session); |
| 999 |
_parse_config(session, file, string, SSH_OK); |
| 1000 |
#ifndef WITH_EXEC |
| 1001 |
/* The match exec is not supported on windows at this moment */ |
| 1002 |
assert_string_equal(session->opts.host, "otherhost"); |
| 1003 |
#else |
| 1004 |
assert_string_equal(session->opts.host, "execed-arguments.com"); |
| 1005 |
#endif |
| 1006 |
|
| 1007 |
/* Try to create some invalid configurations */ |
| 1008 |
/* Missing argument to Match*/ |
| 1009 |
config = "Match\n" |
| 1010 |
"\tHost missing.com\n"; |
| 1011 |
if (file != NULL) { |
| 1012 |
torture_write_file(file, config); |
| 1013 |
} else { |
| 1014 |
string = config; |
| 1015 |
} |
| 1016 |
torture_reset_config(session); |
| 1017 |
ssh_options_set(session, SSH_OPTIONS_HOST, "unmatched"); |
| 1018 |
_parse_config(session, file, string, SSH_OK); |
| 1019 |
assert_string_equal(session->opts.host, "unmatched"); |
| 1020 |
|
| 1021 |
/* Missing argument to unsupported option originalhost */ |
| 1022 |
config = "Match originalhost\n" |
| 1023 |
"\tHost originalhost.com\n"; |
| 1024 |
if (file != NULL) { |
| 1025 |
torture_write_file(file, config); |
| 1026 |
} else { |
| 1027 |
string = config; |
| 1028 |
} |
| 1029 |
torture_reset_config(session); |
| 1030 |
_parse_config(session, file, string, SSH_ERROR); |
| 1031 |
|
| 1032 |
/* Missing argument to option localuser */ |
| 1033 |
config = "Match localuser\n" |
| 1034 |
"\tUser localuser2\n"; |
| 1035 |
if (file != NULL) { |
| 1036 |
torture_write_file(file, config); |
| 1037 |
} else { |
| 1038 |
string = config; |
| 1039 |
} |
| 1040 |
torture_reset_config(session); |
| 1041 |
_parse_config(session, file, string, SSH_ERROR); |
| 1042 |
|
| 1043 |
/* Missing argument to option user */ |
| 1044 |
config = "Match user\n" |
| 1045 |
"\tUser user2\n"; |
| 1046 |
if (file != NULL) { |
| 1047 |
torture_write_file(file, config); |
| 1048 |
} else { |
| 1049 |
string = config; |
| 1050 |
} |
| 1051 |
torture_reset_config(session); |
| 1052 |
_parse_config(session, file, string, SSH_ERROR); |
| 1053 |
|
| 1054 |
/* Missing argument to option host */ |
| 1055 |
config = "Match host\n" |
| 1056 |
"\tUser host2\n"; |
| 1057 |
if (file != NULL) { |
| 1058 |
torture_write_file(file, config); |
| 1059 |
} else { |
| 1060 |
string = config; |
| 1061 |
} |
| 1062 |
torture_reset_config(session); |
| 1063 |
_parse_config(session, file, string, SSH_ERROR); |
| 1064 |
|
| 1065 |
/* Missing argument to option exec */ |
| 1066 |
config = "Match exec\n" |
| 1067 |
"\tUser exec\n"; |
| 1068 |
if (file != NULL) { |
| 1069 |
torture_write_file(file, config); |
| 1070 |
} else { |
| 1071 |
string = config; |
| 1072 |
} |
| 1073 |
torture_reset_config(session); |
| 1074 |
_parse_config(session, file, string, SSH_ERROR); |
| 1075 |
|
| 1076 |
/* Unknown argument to Match keyword */ |
| 1077 |
config = "Match tagged tag_name\n" |
| 1078 |
"\tHostName never-matched.com\n" |
| 1079 |
"Match all\n" |
| 1080 |
"\tHostName config-host.com\n"; |
| 1081 |
if (file != NULL) { |
| 1082 |
torture_write_file(file, config); |
| 1083 |
} else { |
| 1084 |
string = config; |
| 1085 |
} |
| 1086 |
torture_reset_config(session); |
| 1087 |
ssh_options_set(session, SSH_OPTIONS_HOST, "example.com"); |
| 1088 |
_parse_config(session, file, string, SSH_OK); |
| 1089 |
assert_string_equal(session->opts.host, "config-host.com"); |
| 1090 |
|
| 1091 |
/* Missing argument to Match keyword */ |
| 1092 |
config = "Match\n" |
| 1093 |
"\tHostName never-matched.com\n" |
| 1094 |
"Match all\n" |
| 1095 |
"\tHostName config-host.com\n"; |
| 1096 |
if (file != NULL) { |
| 1097 |
torture_write_file(file, config); |
| 1098 |
} else { |
| 1099 |
string = config; |
| 1100 |
} |
| 1101 |
torture_reset_config(session); |
| 1102 |
ssh_options_set(session, SSH_OPTIONS_HOST, "example.com"); |
| 1103 |
_parse_config(session, file, string, SSH_OK); |
| 1104 |
assert_string_equal(session->opts.host, "config-host.com"); |
| 1105 |
} |
| 1106 |
|
| 1107 |
/** |
| 1108 |
* @brief Verify the configuration parser accepts Match keyword with |
| 1109 |
* full OpenSSH syntax through configuration file. |
| 1110 |
*/ |
| 1111 |
static void torture_config_match_file(void **state) |
| 1112 |
{ |
| 1113 |
torture_config_match(state, LIBSSH_TESTCONFIG10, NULL); |
| 1114 |
} |
| 1115 |
|
| 1116 |
/** |
| 1117 |
* @brief Verify the configuration parser accepts Match keyword with |
| 1118 |
* full OpenSSH syntax through configuration string. |
| 1119 |
*/ |
| 1120 |
static void torture_config_match_string(void **state) |
| 1121 |
{ |
| 1122 |
torture_config_match(state, NULL, LIBSSH_TESTCONFIG_STRING10); |
| 1123 |
} |
| 1124 |
|
| 1125 |
/** |
| 1126 |
* @brief Verify we can parse ProxyJump configuration option |
| 1127 |
*/ |
| 1128 |
static void torture_config_proxyjump(void **state, |
| 1129 |
const char *file, const char *string) |
| 1130 |
{ |
| 1131 |
ssh_session session = *state; |
| 1132 |
|
| 1133 |
const char *config = NULL; |
| 1134 |
|
| 1135 |
|
| 1136 |
/* Tests for libssh based proxyjump */ |
| 1137 |
/* Simplest version with just a hostname */ |
| 1138 |
torture_reset_config(session); |
| 1139 |
ssh_options_set(session, SSH_OPTIONS_HOST, "simple"); |
| 1140 |
_parse_config(session, file, string, SSH_OK); |
| 1141 |
helper_proxy_jump_check(session->opts.proxy_jumps->root, |
| 1142 |
"jumpbox", |
| 1143 |
NULL, |
| 1144 |
NULL); |
| 1145 |
|
| 1146 |
/* With username */ |
| 1147 |
torture_reset_config(session); |
| 1148 |
ssh_options_set(session, SSH_OPTIONS_HOST, "user"); |
| 1149 |
_parse_config(session, file, string, SSH_OK); |
| 1150 |
helper_proxy_jump_check(session->opts.proxy_jumps->root, |
| 1151 |
"jumpbox", |
| 1152 |
"user", |
| 1153 |
NULL); |
| 1154 |
|
| 1155 |
/* With port */ |
| 1156 |
torture_reset_config(session); |
| 1157 |
ssh_options_set(session, SSH_OPTIONS_HOST, "port"); |
| 1158 |
_parse_config(session, file, string, SSH_OK); |
| 1159 |
helper_proxy_jump_check(session->opts.proxy_jumps->root, |
| 1160 |
"jumpbox", |
| 1161 |
NULL, |
| 1162 |
"2222"); |
| 1163 |
|
| 1164 |
/* Two step jump */ |
| 1165 |
torture_reset_config(session); |
| 1166 |
ssh_options_set(session, SSH_OPTIONS_HOST, "two-step"); |
| 1167 |
_parse_config(session, file, string, SSH_OK); |
| 1168 |
helper_proxy_jump_check(session->opts.proxy_jumps->root, |
| 1169 |
"second", |
| 1170 |
"u2", |
| 1171 |
"33"); |
| 1172 |
helper_proxy_jump_check(session->opts.proxy_jumps->root->next, |
| 1173 |
"first", |
| 1174 |
"u1", |
| 1175 |
"222"); |
| 1176 |
|
| 1177 |
/* Three step jump */ |
| 1178 |
torture_reset_config(session); |
| 1179 |
ssh_options_set(session, SSH_OPTIONS_HOST, "three-step"); |
| 1180 |
_parse_config(session, file, string, SSH_OK); |
| 1181 |
helper_proxy_jump_check(session->opts.proxy_jumps->root, |
| 1182 |
"third", |
| 1183 |
"u3", |
| 1184 |
"444"); |
| 1185 |
helper_proxy_jump_check(session->opts.proxy_jumps->root->next, |
| 1186 |
"second", |
| 1187 |
"u2", |
| 1188 |
"33"); |
| 1189 |
helper_proxy_jump_check(session->opts.proxy_jumps->root->next->next, |
| 1190 |
"first", |
| 1191 |
"u1", |
| 1192 |
"222"); |
| 1193 |
|
| 1194 |
/* none */ |
| 1195 |
torture_reset_config(session); |
| 1196 |
ssh_options_set(session, SSH_OPTIONS_HOST, "none"); |
| 1197 |
_parse_config(session, file, string, SSH_OK); |
| 1198 |
assert_int_equal(ssh_list_count(session->opts.proxy_jumps), 0); |
| 1199 |
|
| 1200 |
/* If also ProxyCommand is specified, the first is applied */ |
| 1201 |
torture_reset_config(session); |
| 1202 |
ssh_options_set(session, SSH_OPTIONS_HOST, "only-command"); |
| 1203 |
_parse_config(session, file, string, SSH_OK); |
| 1204 |
assert_string_equal(session->opts.ProxyCommand, PROXYCMD); |
| 1205 |
assert_int_equal(ssh_list_count(session->opts.proxy_jumps), 0); |
| 1206 |
|
| 1207 |
/* If also ProxyCommand is specified, the first is applied */ |
| 1208 |
torture_reset_config(session); |
| 1209 |
SAFE_FREE(session->opts.ProxyCommand); |
| 1210 |
ssh_options_set(session, SSH_OPTIONS_HOST, "only-jump"); |
| 1211 |
_parse_config(session, file, string, SSH_OK); |
| 1212 |
assert_null(session->opts.ProxyCommand); |
| 1213 |
helper_proxy_jump_check(session->opts.proxy_jumps->root, |
| 1214 |
"jumpbox", |
| 1215 |
NULL, |
| 1216 |
NULL); |
| 1217 |
|
| 1218 |
/* IPv6 address */ |
| 1219 |
torture_reset_config(session); |
| 1220 |
ssh_options_set(session, SSH_OPTIONS_HOST, "ipv6"); |
| 1221 |
_parse_config(session, file, string, SSH_OK); |
| 1222 |
helper_proxy_jump_check(session->opts.proxy_jumps->root, |
| 1223 |
"2620:52:0::fed", |
| 1224 |
NULL, |
| 1225 |
NULL); |
| 1226 |
|
| 1227 |
torture_reset_config(session); |
| 1228 |
|
| 1229 |
/* Tests for proxycommand based proxyjump */ |
| 1230 |
torture_setenv("OPENSSH_PROXYJUMP", "1"); |
| 1231 |
|
| 1232 |
/* Simplest version with just a hostname */ |
| 1233 |
torture_reset_config(session); |
| 1234 |
ssh_options_set(session, SSH_OPTIONS_HOST, "simple"); |
| 1235 |
_parse_config(session, file, string, SSH_OK); |
| 1236 |
assert_string_equal(session->opts.ProxyCommand, "ssh -W '[%h]:%p' jumpbox"); |
| 1237 |
|
| 1238 |
/* With username */ |
| 1239 |
torture_reset_config(session); |
| 1240 |
ssh_options_set(session, SSH_OPTIONS_HOST, "user"); |
| 1241 |
_parse_config(session, file, string, SSH_OK); |
| 1242 |
assert_string_equal(session->opts.ProxyCommand, |
| 1243 |
"ssh -l user -W '[%h]:%p' jumpbox"); |
| 1244 |
|
| 1245 |
/* With port */ |
| 1246 |
torture_reset_config(session); |
| 1247 |
ssh_options_set(session, SSH_OPTIONS_HOST, "port"); |
| 1248 |
_parse_config(session, file, string, SSH_OK); |
| 1249 |
assert_string_equal(session->opts.ProxyCommand, |
| 1250 |
"ssh -p 2222 -W '[%h]:%p' jumpbox"); |
| 1251 |
|
| 1252 |
/* Two step jump */ |
| 1253 |
torture_reset_config(session); |
| 1254 |
ssh_options_set(session, SSH_OPTIONS_HOST, "two-step"); |
| 1255 |
_parse_config(session, file, string, SSH_OK); |
| 1256 |
assert_string_equal(session->opts.ProxyCommand, |
| 1257 |
"ssh -l u1 -p 222 -J u2@second:33 -W '[%h]:%p' first"); |
| 1258 |
|
| 1259 |
/* Three step jump */ |
| 1260 |
torture_reset_config(session); |
| 1261 |
ssh_options_set(session, SSH_OPTIONS_HOST, "three-step"); |
| 1262 |
_parse_config(session, file, string, SSH_OK); |
| 1263 |
assert_string_equal(session->opts.ProxyCommand, |
| 1264 |
"ssh -l u1 -p 222 -J u2@second:33,u3@third:444 -W '[%h]:%p' first"); |
| 1265 |
|
| 1266 |
/* none */ |
| 1267 |
torture_reset_config(session); |
| 1268 |
ssh_options_set(session, SSH_OPTIONS_HOST, "none"); |
| 1269 |
_parse_config(session, file, string, SSH_OK); |
| 1270 |
assert_true(session->opts.ProxyCommand == NULL); |
| 1271 |
|
| 1272 |
/* If also ProxyCommand is specified, the first is applied */ |
| 1273 |
torture_reset_config(session); |
| 1274 |
ssh_options_set(session, SSH_OPTIONS_HOST, "only-command"); |
| 1275 |
_parse_config(session, file, string, SSH_OK); |
| 1276 |
assert_string_equal(session->opts.ProxyCommand, PROXYCMD); |
| 1277 |
|
| 1278 |
/* If also ProxyCommand is specified, the first is applied */ |
| 1279 |
torture_reset_config(session); |
| 1280 |
ssh_options_set(session, SSH_OPTIONS_HOST, "only-jump"); |
| 1281 |
_parse_config(session, file, string, SSH_OK); |
| 1282 |
assert_string_equal(session->opts.ProxyCommand, |
| 1283 |
"ssh -W '[%h]:%p' jumpbox"); |
| 1284 |
|
| 1285 |
/* IPv6 address */ |
| 1286 |
torture_reset_config(session); |
| 1287 |
ssh_options_set(session, SSH_OPTIONS_HOST, "ipv6"); |
| 1288 |
_parse_config(session, file, string, SSH_OK); |
| 1289 |
assert_string_equal(session->opts.ProxyCommand, |
| 1290 |
"ssh -W '[%h]:%p' 2620:52:0::fed"); |
| 1291 |
|
| 1292 |
|
| 1293 |
/* Multiple @ is allowed in second jump */ |
| 1294 |
config = "Host allowed-hostname\n" |
| 1295 |
"\tProxyJump localhost,[email protected]@jumpbox:22\n"; |
| 1296 |
if (file != NULL) { |
| 1297 |
torture_write_file(file, config); |
| 1298 |
} else { |
| 1299 |
string = config; |
| 1300 |
} |
| 1301 |
torture_reset_config(session); |
| 1302 |
ssh_options_set(session, SSH_OPTIONS_HOST, "allowed-hostname"); |
| 1303 |
_parse_config(session, file, string, SSH_OK); |
| 1304 |
assert_string_equal(session->opts.ProxyCommand, |
| 1305 |
"ssh -J [email protected]@jumpbox:22 -W '[%h]:%p' localhost"); |
| 1306 |
|
| 1307 |
/* Multiple @ is allowed */ |
| 1308 |
config = "Host allowed-hostname\n" |
| 1309 |
"\tProxyJump [email protected]@jumpbox:22\n"; |
| 1310 |
if (file != NULL) { |
| 1311 |
torture_write_file(file, config); |
| 1312 |
} else { |
| 1313 |
string = config; |
| 1314 |
} |
| 1315 |
torture_reset_config(session); |
| 1316 |
ssh_options_set(session, SSH_OPTIONS_HOST, "allowed-hostname"); |
| 1317 |
_parse_config(session, file, string, SSH_OK); |
| 1318 |
assert_string_equal(session->opts.ProxyCommand, |
| 1319 |
"ssh -l [email protected] -p 22 -W '[%h]:%p' jumpbox"); |
| 1320 |
torture_unsetenv("OPENSSH_PROXYJUMP"); |
| 1321 |
|
| 1322 |
/* Tests for libssh based proxyjump */ |
| 1323 |
/* Multiple @ is allowed in second jump */ |
| 1324 |
config = "Host allowed-hostname\n" |
| 1325 |
"\tProxyJump localhost,[email protected]@jumpbox:22\n"; |
| 1326 |
if (file != NULL) { |
| 1327 |
torture_write_file(file, config); |
| 1328 |
} else { |
| 1329 |
string = config; |
| 1330 |
} |
| 1331 |
torture_reset_config(session); |
| 1332 |
ssh_options_set(session, SSH_OPTIONS_HOST, "allowed-hostname"); |
| 1333 |
_parse_config(session, file, string, SSH_OK); |
| 1334 |
helper_proxy_jump_check(session->opts.proxy_jumps->root, |
| 1335 |
"jumpbox", |
| 1336 |
"[email protected]", |
| 1337 |
"22"); |
| 1338 |
|
| 1339 |
/* Multiple @ is allowed */ |
| 1340 |
config = "Host allowed-hostname\n" |
| 1341 |
"\tProxyJump [email protected]@jumpbox:22\n"; |
| 1342 |
if (file != NULL) { |
| 1343 |
torture_write_file(file, config); |
| 1344 |
} else { |
| 1345 |
string = config; |
| 1346 |
} |
| 1347 |
torture_reset_config(session); |
| 1348 |
ssh_options_set(session, SSH_OPTIONS_HOST, "allowed-hostname"); |
| 1349 |
_parse_config(session, file, string, SSH_OK); |
| 1350 |
helper_proxy_jump_check(session->opts.proxy_jumps->root, |
| 1351 |
"jumpbox", |
| 1352 |
"[email protected]", |
| 1353 |
"22"); |
| 1354 |
torture_reset_config(session); |
| 1355 |
|
| 1356 |
/* In this part, we try various other config files and strings. */ |
| 1357 |
torture_setenv("OPENSSH_PROXYJUMP", "1"); |
| 1358 |
|
| 1359 |
/* Try to create some invalid configurations */ |
| 1360 |
/* Non-numeric port */ |
| 1361 |
config = "Host bad-port\n" |
| 1362 |
"\tProxyJump jumpbox:22bad22\n"; |
| 1363 |
if (file != NULL) { |
| 1364 |
torture_write_file(file, config); |
| 1365 |
} else { |
| 1366 |
string = config; |
| 1367 |
} |
| 1368 |
torture_reset_config(session); |
| 1369 |
ssh_options_set(session, SSH_OPTIONS_HOST, "bad-port"); |
| 1370 |
_parse_config(session, file, string, SSH_ERROR); |
| 1371 |
|
| 1372 |
/* Braces mismatch in hostname */ |
| 1373 |
config = "Host mismatch\n" |
| 1374 |
"\tProxyJump [::1\n"; |
| 1375 |
if (file != NULL) { |
| 1376 |
torture_write_file(file, config); |
| 1377 |
} else { |
| 1378 |
string = config; |
| 1379 |
} |
| 1380 |
torture_reset_config(session); |
| 1381 |
ssh_options_set(session, SSH_OPTIONS_HOST, "mismatch"); |
| 1382 |
_parse_config(session, file, string, SSH_ERROR); |
| 1383 |
|
| 1384 |
/* Bad host-port separator */ |
| 1385 |
config = "Host beef\n" |
| 1386 |
"\tProxyJump [dead::beef]::22\n"; |
| 1387 |
if (file != NULL) { |
| 1388 |
torture_write_file(file, config); |
| 1389 |
} else { |
| 1390 |
string = config; |
| 1391 |
} |
| 1392 |
torture_reset_config(session); |
| 1393 |
ssh_options_set(session, SSH_OPTIONS_HOST, "beef"); |
| 1394 |
_parse_config(session, file, string, SSH_ERROR); |
| 1395 |
|
| 1396 |
/* Missing hostname */ |
| 1397 |
config = "Host no-host\n" |
| 1398 |
"\tProxyJump user@:22\n"; |
| 1399 |
if (file != NULL) { |
| 1400 |
torture_write_file(file, config); |
| 1401 |
} else { |
| 1402 |
string = config; |
| 1403 |
} |
| 1404 |
torture_reset_config(session); |
| 1405 |
ssh_options_set(session, SSH_OPTIONS_HOST, "no-host"); |
| 1406 |
_parse_config(session, file, string, SSH_ERROR); |
| 1407 |
|
| 1408 |
/* Missing user */ |
| 1409 |
config = "Host no-user\n" |
| 1410 |
"\tProxyJump @host:22\n"; |
| 1411 |
if (file != NULL) { |
| 1412 |
torture_write_file(file, config); |
| 1413 |
} else { |
| 1414 |
string = config; |
| 1415 |
} |
| 1416 |
torture_reset_config(session); |
| 1417 |
ssh_options_set(session, SSH_OPTIONS_HOST, "no-user"); |
| 1418 |
_parse_config(session, file, string, SSH_ERROR); |
| 1419 |
|
| 1420 |
/* Missing port */ |
| 1421 |
config = "Host no-port\n" |
| 1422 |
"\tProxyJump host:\n"; |
| 1423 |
if (file != NULL) { |
| 1424 |
torture_write_file(file, config); |
| 1425 |
} else { |
| 1426 |
string = config; |
| 1427 |
} |
| 1428 |
torture_reset_config(session); |
| 1429 |
ssh_options_set(session, SSH_OPTIONS_HOST, "no-port"); |
| 1430 |
_parse_config(session, file, string, SSH_ERROR); |
| 1431 |
|
| 1432 |
/* Non-numeric port in second jump */ |
| 1433 |
config = "Host bad-port-2\n" |
| 1434 |
"\tProxyJump localhost,jumpbox:22bad22\n"; |
| 1435 |
if (file != NULL) { |
| 1436 |
torture_write_file(file, config); |
| 1437 |
} else { |
| 1438 |
string = config; |
| 1439 |
} |
| 1440 |
torture_reset_config(session); |
| 1441 |
ssh_options_set(session, SSH_OPTIONS_HOST, "bad-port-2"); |
| 1442 |
_parse_config(session, file, string, SSH_ERROR); |
| 1443 |
|
| 1444 |
/* Braces mismatch in second jump */ |
| 1445 |
config = "Host mismatch\n" |
| 1446 |
"\tProxyJump localhost,[::1:20\n"; |
| 1447 |
if (file != NULL) { |
| 1448 |
torture_write_file(file, config); |
| 1449 |
} else { |
| 1450 |
string = config; |
| 1451 |
} |
| 1452 |
torture_reset_config(session); |
| 1453 |
ssh_options_set(session, SSH_OPTIONS_HOST, "mismatch"); |
| 1454 |
_parse_config(session, file, string, SSH_ERROR); |
| 1455 |
|
| 1456 |
/* Bad host-port separator in second jump */ |
| 1457 |
config = "Host beef\n" |
| 1458 |
"\tProxyJump localhost,[dead::beef]::22\n"; |
| 1459 |
if (file != NULL) { |
| 1460 |
torture_write_file(file, config); |
| 1461 |
} else { |
| 1462 |
string = config; |
| 1463 |
} |
| 1464 |
torture_reset_config(session); |
| 1465 |
ssh_options_set(session, SSH_OPTIONS_HOST, "beef"); |
| 1466 |
_parse_config(session, file, string, SSH_ERROR); |
| 1467 |
|
| 1468 |
/* Missing hostname in second jump */ |
| 1469 |
config = "Host no-host\n" |
| 1470 |
"\tProxyJump localhost,user@:22\n"; |
| 1471 |
if (file != NULL) { |
| 1472 |
torture_write_file(file, config); |
| 1473 |
} else { |
| 1474 |
string = config; |
| 1475 |
} |
| 1476 |
torture_reset_config(session); |
| 1477 |
ssh_options_set(session, SSH_OPTIONS_HOST, "no-host"); |
| 1478 |
_parse_config(session, file, string, SSH_ERROR); |
| 1479 |
|
| 1480 |
/* Missing user in second jump */ |
| 1481 |
config = "Host no-user\n" |
| 1482 |
"\tProxyJump localhost,@host:22\n"; |
| 1483 |
if (file != NULL) { |
| 1484 |
torture_write_file(file, config); |
| 1485 |
} else { |
| 1486 |
string = config; |
| 1487 |
} |
| 1488 |
torture_reset_config(session); |
| 1489 |
ssh_options_set(session, SSH_OPTIONS_HOST, "no-user"); |
| 1490 |
_parse_config(session, file, string, SSH_ERROR); |
| 1491 |
|
| 1492 |
/* Missing port in second jump */ |
| 1493 |
config = "Host no-port\n" |
| 1494 |
"\tProxyJump localhost,host:\n"; |
| 1495 |
if (file != NULL) { |
| 1496 |
torture_write_file(file, config); |
| 1497 |
} else { |
| 1498 |
string = config; |
| 1499 |
} |
| 1500 |
torture_reset_config(session); |
| 1501 |
ssh_options_set(session, SSH_OPTIONS_HOST, "no-port"); |
| 1502 |
_parse_config(session, file, string, SSH_ERROR); |
| 1503 |
|
| 1504 |
torture_unsetenv("OPENSSH_PROXYJUMP"); |
| 1505 |
} |
| 1506 |
|
| 1507 |
/** |
| 1508 |
* @brief Verify we can parse ProxyJump configuration option from file |
| 1509 |
*/ |
| 1510 |
static void torture_config_proxyjump_file(void **state) |
| 1511 |
{ |
| 1512 |
torture_config_proxyjump(state, LIBSSH_TESTCONFIG11, NULL); |
| 1513 |
} |
| 1514 |
|
| 1515 |
/** |
| 1516 |
* @brief Verify we can parse ProxyJump configuration option from string |
| 1517 |
*/ |
| 1518 |
static void torture_config_proxyjump_string(void **state) |
| 1519 |
{ |
| 1520 |
torture_config_proxyjump(state, NULL, LIBSSH_TESTCONFIG_STRING11); |
| 1521 |
} |
| 1522 |
|
| 1523 |
/** |
| 1524 |
* @brief Verify we can parse ControlPath configuration option |
| 1525 |
*/ |
| 1526 |
static void torture_config_control_path(void **state, |
| 1527 |
const char *file, const char *string) |
| 1528 |
{ |
| 1529 |
ssh_session session = *state; |
| 1530 |
|
| 1531 |
torture_reset_config(session); |
| 1532 |
ssh_options_set(session, SSH_OPTIONS_HOST, "simple"); |
| 1533 |
_parse_config(session, file, string, SSH_OK); |
| 1534 |
assert_null(session->opts.control_path); |
| 1535 |
|
| 1536 |
torture_reset_config(session); |
| 1537 |
ssh_options_set(session, SSH_OPTIONS_HOST, "none"); |
| 1538 |
_parse_config(session, file, string, SSH_OK); |
| 1539 |
assert_null(session->opts.control_path); |
| 1540 |
} |
| 1541 |
|
| 1542 |
/** |
| 1543 |
* @brief Verify we can parse ControlPath configuration option from string |
| 1544 |
*/ |
| 1545 |
static void torture_config_control_path_string(void **state) |
| 1546 |
{ |
| 1547 |
torture_config_control_path(state, NULL, LIBSSH_TESTCONFIG_STRING17); |
| 1548 |
} |
| 1549 |
|
| 1550 |
/** |
| 1551 |
* @brief Verify we can parse ControlPath configuration option from file |
| 1552 |
*/ |
| 1553 |
static void torture_config_control_path_file(void **state) |
| 1554 |
{ |
| 1555 |
torture_config_control_path(state, LIBSSH_TESTCONFIG17, NULL); |
| 1556 |
} |
| 1557 |
|
| 1558 |
/** |
| 1559 |
* @brief Verify we can parse ControlMaster configuration option |
| 1560 |
*/ |
| 1561 |
static void torture_config_control_master(void **state, |
| 1562 |
const char *file, const char *string) |
| 1563 |
{ |
| 1564 |
ssh_session session = *state; |
| 1565 |
|
| 1566 |
torture_reset_config(session); |
| 1567 |
ssh_options_set(session, SSH_OPTIONS_HOST, "simple"); |
| 1568 |
_parse_config(session, file, string, SSH_OK); |
| 1569 |
assert_int_equal(session->opts.control_master, SSH_CONTROL_MASTER_NO); |
| 1570 |
|
| 1571 |
torture_reset_config(session); |
| 1572 |
ssh_options_set(session, SSH_OPTIONS_HOST, "none"); |
| 1573 |
_parse_config(session, file, string, SSH_OK); |
| 1574 |
assert_int_equal(session->opts.control_master, SSH_CONTROL_MASTER_NO); |
| 1575 |
} |
| 1576 |
|
| 1577 |
/** |
| 1578 |
* @brief Verify we can parse ControlMaster configuration option from string |
| 1579 |
*/ |
| 1580 |
static void torture_config_control_master_string(void **state) |
| 1581 |
{ |
| 1582 |
torture_config_control_master(state, NULL, LIBSSH_TESTCONFIG_STRING17); |
| 1583 |
} |
| 1584 |
|
| 1585 |
/** |
| 1586 |
* @brief Verify we can parse ControlMaster configuration option from file |
| 1587 |
*/ |
| 1588 |
static void torture_config_control_master_file(void **state) |
| 1589 |
{ |
| 1590 |
torture_config_control_master(state, LIBSSH_TESTCONFIG17, NULL); |
| 1591 |
} |
| 1592 |
|
| 1593 |
/** |
| 1594 |
* @brief Verify we can parse AdressFamily configuration option |
| 1595 |
*/ |
| 1596 |
static void torture_config_address_family(void **state, |
| 1597 |
const char *file, |
| 1598 |
const char *string) |
| 1599 |
{ |
| 1600 |
ssh_session session = *state; |
| 1601 |
|
| 1602 |
const char *config = NULL; |
| 1603 |
|
| 1604 |
torture_reset_config(session); |
| 1605 |
ssh_options_set(session, SSH_OPTIONS_HOST, "simple"); |
| 1606 |
_parse_config(session, file, string, SSH_OK); |
| 1607 |
assert_int_equal(session->opts.address_family, SSH_ADDRESS_FAMILY_ANY); |
| 1608 |
|
| 1609 |
torture_reset_config(session); |
| 1610 |
ssh_options_set(session, SSH_OPTIONS_HOST, "af"); |
| 1611 |
_parse_config(session, file, string, SSH_OK); |
| 1612 |
assert_int_equal(session->opts.address_family, SSH_ADDRESS_FAMILY_ANY); |
| 1613 |
|
| 1614 |
torture_reset_config(session); |
| 1615 |
ssh_options_set(session, SSH_OPTIONS_HOST, "af4"); |
| 1616 |
_parse_config(session, file, string, SSH_OK); |
| 1617 |
assert_int_equal(session->opts.address_family, SSH_ADDRESS_FAMILY_INET); |
| 1618 |
|
| 1619 |
torture_reset_config(session); |
| 1620 |
ssh_options_set(session, SSH_OPTIONS_HOST, "af6"); |
| 1621 |
_parse_config(session, file, string, SSH_OK); |
| 1622 |
assert_int_equal(session->opts.address_family, SSH_ADDRESS_FAMILY_INET6); |
| 1623 |
|
| 1624 |
/* test for parsing failures */ |
| 1625 |
config = "Host afmissing\n" |
| 1626 |
"\tAddressFamily\n"; |
| 1627 |
if (file != NULL) { |
| 1628 |
torture_write_file(file, config); |
| 1629 |
} else { |
| 1630 |
string = config; |
| 1631 |
} |
| 1632 |
|
| 1633 |
torture_reset_config(session); |
| 1634 |
ssh_options_set(session, SSH_OPTIONS_HOST, "afmissing"); |
| 1635 |
_parse_config(session, file, string, SSH_ERROR); |
| 1636 |
|
| 1637 |
config = "Host afinvalid\n" |
| 1638 |
"\tAddressFamily wurstkäse\n"; |
| 1639 |
if (file != NULL) { |
| 1640 |
torture_write_file(file, config); |
| 1641 |
} else { |
| 1642 |
string = config; |
| 1643 |
} |
| 1644 |
|
| 1645 |
torture_reset_config(session); |
| 1646 |
ssh_options_set(session, SSH_OPTIONS_HOST, "afinvalid"); |
| 1647 |
_parse_config(session, file, string, SSH_ERROR); |
| 1648 |
} |
| 1649 |
|
| 1650 |
/** |
| 1651 |
* @brief Verify we can parse AdressFamily configuration option from string |
| 1652 |
*/ |
| 1653 |
static void torture_config_address_family_string(void **state) |
| 1654 |
{ |
| 1655 |
torture_config_address_family(state, NULL, LIBSSH_TESTCONFIG_STRING18); |
| 1656 |
} |
| 1657 |
|
| 1658 |
/** |
| 1659 |
* @brief Verify we can parse AdressFamily configuration option from file |
| 1660 |
*/ |
| 1661 |
static void torture_config_address_family_file(void **state) |
| 1662 |
{ |
| 1663 |
torture_config_address_family(state, LIBSSH_TESTCONFIG18, NULL); |
| 1664 |
} |
| 1665 |
|
| 1666 |
/** |
| 1667 |
* @brief Verify the configuration parser handles all the possible |
| 1668 |
* versions of RekeyLimit configuration option. |
| 1669 |
*/ |
| 1670 |
static void torture_config_rekey(void **state, |
| 1671 |
const char *file, const char *string) |
| 1672 |
{ |
| 1673 |
ssh_session session = *state; |
| 1674 |
|
| 1675 |
/* Default values */ |
| 1676 |
ssh_options_set(session, SSH_OPTIONS_HOST, "default"); |
| 1677 |
_parse_config(session, file, string, SSH_OK); |
| 1678 |
assert_int_equal(session->opts.rekey_data, 0); |
| 1679 |
assert_int_equal(session->opts.rekey_time, 0); |
| 1680 |
|
| 1681 |
/* 42 GB */ |
| 1682 |
torture_reset_config(session); |
| 1683 |
ssh_options_set(session, SSH_OPTIONS_HOST, "data1"); |
| 1684 |
_parse_config(session, file, string, SSH_OK); |
| 1685 |
assert_int_equal(session->opts.rekey_data, |
| 1686 |
(uint64_t) 42 * 1024 * 1024 * 1024); |
| 1687 |
assert_int_equal(session->opts.rekey_time, 0); |
| 1688 |
|
| 1689 |
/* 41 MB */ |
| 1690 |
torture_reset_config(session); |
| 1691 |
ssh_options_set(session, SSH_OPTIONS_HOST, "data2"); |
| 1692 |
_parse_config(session, file, string, SSH_OK); |
| 1693 |
assert_int_equal(session->opts.rekey_data, 31 * 1024 * 1024); |
| 1694 |
assert_int_equal(session->opts.rekey_time, 0); |
| 1695 |
|
| 1696 |
/* 521 KB */ |
| 1697 |
torture_reset_config(session); |
| 1698 |
ssh_options_set(session, SSH_OPTIONS_HOST, "data3"); |
| 1699 |
_parse_config(session, file, string, SSH_OK); |
| 1700 |
assert_int_equal(session->opts.rekey_data, 521 * 1024); |
| 1701 |
assert_int_equal(session->opts.rekey_time, 0); |
| 1702 |
|
| 1703 |
/* default 3D */ |
| 1704 |
torture_reset_config(session); |
| 1705 |
ssh_options_set(session, SSH_OPTIONS_HOST, "time1"); |
| 1706 |
_parse_config(session, file, string, SSH_OK); |
| 1707 |
assert_int_equal(session->opts.rekey_data, 0); |
| 1708 |
assert_int_equal(session->opts.rekey_time, 3 * 24 * 60 * 60 * 1000); |
| 1709 |
|
| 1710 |
/* default 2h */ |
| 1711 |
torture_reset_config(session); |
| 1712 |
ssh_options_set(session, SSH_OPTIONS_HOST, "time2"); |
| 1713 |
_parse_config(session, file, string, SSH_OK); |
| 1714 |
assert_int_equal(session->opts.rekey_data, 0); |
| 1715 |
assert_int_equal(session->opts.rekey_time, 2 * 60 * 60 * 1000); |
| 1716 |
|
| 1717 |
/* default 160m */ |
| 1718 |
torture_reset_config(session); |
| 1719 |
ssh_options_set(session, SSH_OPTIONS_HOST, "time3"); |
| 1720 |
_parse_config(session, file, string, SSH_OK); |
| 1721 |
assert_int_equal(session->opts.rekey_data, 0); |
| 1722 |
assert_int_equal(session->opts.rekey_time, 160 * 60 * 1000); |
| 1723 |
|
| 1724 |
/* default 9600 [s] */ |
| 1725 |
torture_reset_config(session); |
| 1726 |
ssh_options_set(session, SSH_OPTIONS_HOST, "time4"); |
| 1727 |
_parse_config(session, file, string, SSH_OK); |
| 1728 |
assert_int_equal(session->opts.rekey_data, 0); |
| 1729 |
assert_int_equal(session->opts.rekey_time, 9600 * 1000); |
| 1730 |
|
| 1731 |
} |
| 1732 |
|
| 1733 |
/** |
| 1734 |
* @brief Verify the configuration parser handles all the possible |
| 1735 |
* versions of RekeyLimit configuration option in file |
| 1736 |
*/ |
| 1737 |
static void torture_config_rekey_file(void **state) |
| 1738 |
{ |
| 1739 |
torture_config_rekey(state, LIBSSH_TESTCONFIG12, NULL); |
| 1740 |
} |
| 1741 |
|
| 1742 |
/** |
| 1743 |
* @brief Verify the configuration parser handles all the possible |
| 1744 |
* versions of RekeyLimit configuration option in string |
| 1745 |
*/ |
| 1746 |
static void torture_config_rekey_string(void **state) |
| 1747 |
{ |
| 1748 |
torture_config_rekey(state, NULL, LIBSSH_TESTCONFIG_STRING12); |
| 1749 |
} |
| 1750 |
|
| 1751 |
/** |
| 1752 |
* @brief Remove substring from a string |
| 1753 |
* |
| 1754 |
* @param occurrence 0 means "remove the first occurrence" |
| 1755 |
* 1 means "remove the second occurrence" and so on |
| 1756 |
*/ |
| 1757 |
static void helper_remove_substring(char *s, const char *subs, int occurrence) { |
| 1758 |
char *p; |
| 1759 |
/* remove the substring from the defaults */ |
| 1760 |
p = strstr(s, subs); |
| 1761 |
assert_non_null(p); |
| 1762 |
/* look for second occurrence */ |
| 1763 |
for (int i = 0; i < occurrence; i++) { |
| 1764 |
p = strstr(p + 1, subs); |
| 1765 |
assert_non_null(p); |
| 1766 |
} |
| 1767 |
memmove(p, p + strlen(subs), strlen(p + strlen(subs)) + 1); |
| 1768 |
} |
| 1769 |
|
| 1770 |
/** |
| 1771 |
* @brief test that openssh style '+' feature works |
| 1772 |
*/ |
| 1773 |
static void torture_config_plus(void **state, |
| 1774 |
const char *file, const char *string) |
| 1775 |
{ |
| 1776 |
ssh_session session = *state; |
| 1777 |
const char *def_hostkeys = ssh_kex_get_default_methods(SSH_HOSTKEYS); |
| 1778 |
const char *fips_hostkeys = ssh_kex_get_fips_methods(SSH_HOSTKEYS); |
| 1779 |
const char *def_ciphers = ssh_kex_get_default_methods(SSH_CRYPT_C_S); |
| 1780 |
const char *fips_ciphers = ssh_kex_get_fips_methods(SSH_CRYPT_C_S); |
| 1781 |
const char *def_kex = ssh_kex_get_default_methods(SSH_KEX); |
| 1782 |
const char *fips_kex = ssh_kex_get_fips_methods(SSH_KEX); |
| 1783 |
const char *def_mac = ssh_kex_get_default_methods(SSH_MAC_C_S); |
| 1784 |
const char *fips_mac = ssh_kex_get_fips_methods(SSH_MAC_C_S); |
| 1785 |
const char *hostkeys_added = ",ssh-rsa"; |
| 1786 |
const char *ciphers_added = ",aes128-cbc,aes256-cbc"; |
| 1787 |
const char *kex_added = ",diffie-hellman-group14-sha1,diffie-hellman-group1-sha1"; |
| 1788 |
const char *mac_added = ",hmac-sha1,[email protected]"; |
| 1789 |
char *awaited = NULL; |
| 1790 |
int rc; |
| 1791 |
|
| 1792 |
_parse_config(session, file, string, SSH_OK); |
| 1793 |
|
| 1794 |
/* check hostkeys */ |
| 1795 |
if (ssh_fips_mode()) { |
| 1796 |
/* ssh-rsa is disabled in fips */ |
| 1797 |
assert_string_equal(session->opts.wanted_methods[SSH_HOSTKEYS], fips_hostkeys); |
| 1798 |
} else { |
| 1799 |
awaited = calloc(strlen(def_hostkeys) + strlen(hostkeys_added) + 1, 1); |
| 1800 |
rc = snprintf(awaited, strlen(def_hostkeys) + strlen(hostkeys_added) + 1, |
| 1801 |
"%s%s", def_hostkeys, hostkeys_added); |
| 1802 |
assert_int_equal(rc, strlen(def_hostkeys) + strlen(hostkeys_added)); |
| 1803 |
|
| 1804 |
assert_string_equal(session->opts.wanted_methods[SSH_HOSTKEYS], awaited); |
| 1805 |
free(awaited); |
| 1806 |
} |
| 1807 |
|
| 1808 |
/* check ciphers */ |
| 1809 |
if (ssh_fips_mode()) { |
| 1810 |
/* already all supported is in the list */ |
| 1811 |
assert_string_equal(session->opts.wanted_methods[SSH_CRYPT_C_S], fips_ciphers); |
| 1812 |
} else { |
| 1813 |
awaited = calloc(strlen(def_ciphers) + strlen(ciphers_added) + 1, 1); |
| 1814 |
rc = snprintf(awaited, strlen(def_ciphers) + strlen(ciphers_added) + 1, |
| 1815 |
"%s%s", def_ciphers, ciphers_added); |
| 1816 |
assert_int_equal(rc, strlen(def_ciphers) + strlen(ciphers_added)); |
| 1817 |
assert_string_equal(session->opts.wanted_methods[SSH_CRYPT_C_S], awaited); |
| 1818 |
free(awaited); |
| 1819 |
} |
| 1820 |
|
| 1821 |
/* check kex */ |
| 1822 |
if (ssh_fips_mode()) { |
| 1823 |
/* sha1 is disabled in fips */ |
| 1824 |
assert_string_equal(session->opts.wanted_methods[SSH_KEX], fips_kex); |
| 1825 |
} else { |
| 1826 |
awaited = calloc(strlen(def_kex) + strlen(kex_added) + 1, 1); |
| 1827 |
rc = snprintf(awaited, strlen(def_kex) + strlen(kex_added) + 1, |
| 1828 |
"%s%s", def_kex, kex_added); |
| 1829 |
assert_int_equal(rc, strlen(def_kex) + strlen(kex_added)); |
| 1830 |
assert_string_equal(session->opts.wanted_methods[SSH_KEX], awaited); |
| 1831 |
free(awaited); |
| 1832 |
} |
| 1833 |
|
| 1834 |
/* check mac */ |
| 1835 |
if (ssh_fips_mode()) { |
| 1836 |
/* the added algos are already in the fips_methods */ |
| 1837 |
assert_string_equal(session->opts.wanted_methods[SSH_MAC_C_S], fips_mac); |
| 1838 |
} else { |
| 1839 |
awaited = calloc(strlen(def_mac) + strlen(mac_added) + 1, 1); |
| 1840 |
rc = snprintf(awaited, strlen(def_mac) + strlen(mac_added) + 1, |
| 1841 |
"%s%s", def_mac, mac_added); |
| 1842 |
assert_int_equal(rc, strlen(def_mac) + strlen(mac_added)); |
| 1843 |
assert_string_equal(session->opts.wanted_methods[SSH_MAC_C_S], awaited); |
| 1844 |
free(awaited); |
| 1845 |
} |
| 1846 |
} |
| 1847 |
|
| 1848 |
/** |
| 1849 |
* @brief test that openssh style '+' feature works from file |
| 1850 |
*/ |
| 1851 |
static void torture_config_plus_file(void **state) |
| 1852 |
{ |
| 1853 |
torture_config_plus(state, LIBSSH_TESTCONFIG14, NULL); |
| 1854 |
} |
| 1855 |
|
| 1856 |
/** |
| 1857 |
* @brief test that openssh style '+' feature works from string |
| 1858 |
*/ |
| 1859 |
static void torture_config_plus_string(void **state) |
| 1860 |
{ |
| 1861 |
torture_config_plus(state, NULL, LIBSSH_TESTCONFIG_STRING14); |
| 1862 |
} |
| 1863 |
|
| 1864 |
/** |
| 1865 |
* @brief test that openssh style '-' feature works from string |
| 1866 |
*/ |
| 1867 |
static void torture_config_minus(void **state, |
| 1868 |
const char *file, const char *string) |
| 1869 |
{ |
| 1870 |
ssh_session session = *state; |
| 1871 |
const char *def_hostkeys = ssh_kex_get_default_methods(SSH_HOSTKEYS); |
| 1872 |
const char *fips_hostkeys = ssh_kex_get_fips_methods(SSH_HOSTKEYS); |
| 1873 |
const char *def_ciphers = ssh_kex_get_default_methods(SSH_CRYPT_C_S); |
| 1874 |
const char *fips_ciphers = ssh_kex_get_fips_methods(SSH_CRYPT_C_S); |
| 1875 |
const char *def_kex = ssh_kex_get_default_methods(SSH_KEX); |
| 1876 |
const char *fips_kex = ssh_kex_get_fips_methods(SSH_KEX); |
| 1877 |
const char *def_mac = ssh_kex_get_default_methods(SSH_MAC_C_S); |
| 1878 |
const char *fips_mac = ssh_kex_get_fips_methods(SSH_MAC_C_S); |
| 1879 |
const char *hostkeys_removed = ",rsa-sha2-512,rsa-sha2-256"; |
| 1880 |
const char *ciphers_removed = ",aes256-ctr"; |
| 1881 |
const char *kex_removed = ",diffie-hellman-group18-sha512,diffie-hellman-group16-sha512"; |
| 1882 |
const char *fips_kex_removed = ",diffie-hellman-group16-sha512,diffie-hellman-group18-sha512"; |
| 1883 |
const char *mac_removed = "[email protected],"; |
| 1884 |
char *awaited = NULL; |
| 1885 |
int rc; |
| 1886 |
|
| 1887 |
_parse_config(session, file, string, SSH_OK); |
| 1888 |
|
| 1889 |
/* check hostkeys */ |
| 1890 |
if (ssh_fips_mode()) { |
| 1891 |
awaited = calloc(strlen(fips_hostkeys) + 1, 1); |
| 1892 |
rc = snprintf(awaited, strlen(fips_hostkeys) + 1, "%s", fips_hostkeys); |
| 1893 |
assert_int_equal(rc, strlen(fips_hostkeys)); |
| 1894 |
} else { |
| 1895 |
awaited = calloc(strlen(def_hostkeys) + 1, 1); |
| 1896 |
rc = snprintf(awaited, strlen(def_hostkeys) + 1, "%s", def_hostkeys); |
| 1897 |
assert_int_equal(rc, strlen(def_hostkeys)); |
| 1898 |
} |
| 1899 |
/* remove the substring from the defaults */ |
| 1900 |
helper_remove_substring(awaited, hostkeys_removed, 0); |
| 1901 |
assert_string_equal(session->opts.wanted_methods[SSH_HOSTKEYS], awaited); |
| 1902 |
free(awaited); |
| 1903 |
|
| 1904 |
/* check ciphers */ |
| 1905 |
if (ssh_fips_mode()) { |
| 1906 |
awaited = calloc(strlen(fips_ciphers) + 1, 1); |
| 1907 |
rc = snprintf(awaited, strlen(fips_ciphers) + 1, "%s", fips_ciphers); |
| 1908 |
assert_int_equal(rc, strlen(fips_ciphers)); |
| 1909 |
} else { |
| 1910 |
awaited = calloc(strlen(def_ciphers) + 1, 1); |
| 1911 |
rc = snprintf(awaited, strlen(def_ciphers) + 1, "%s", def_ciphers); |
| 1912 |
assert_int_equal(rc, strlen(def_ciphers)); |
| 1913 |
} |
| 1914 |
/* remove the substring from the defaults */ |
| 1915 |
helper_remove_substring(awaited, ciphers_removed, 0); |
| 1916 |
assert_string_equal(session->opts.wanted_methods[SSH_CRYPT_C_S], awaited); |
| 1917 |
free(awaited); |
| 1918 |
|
| 1919 |
/* check kex */ |
| 1920 |
if (ssh_fips_mode()) { |
| 1921 |
awaited = calloc(strlen(fips_kex) + 1, 1); |
| 1922 |
rc = snprintf(awaited, strlen(fips_kex) + 1, "%s", fips_kex); |
| 1923 |
assert_int_equal(rc, strlen(fips_kex)); |
| 1924 |
/* remove the substring from the defaults */ |
| 1925 |
helper_remove_substring(awaited, fips_kex_removed, 0); |
| 1926 |
} else { |
| 1927 |
awaited = calloc(strlen(def_kex) + 1, 1); |
| 1928 |
rc = snprintf(awaited, strlen(def_kex) + 1, "%s", def_kex); |
| 1929 |
assert_int_equal(rc, strlen(def_kex)); |
| 1930 |
/* remove the substring from the defaults */ |
| 1931 |
helper_remove_substring(awaited, kex_removed, 0); |
| 1932 |
} |
| 1933 |
assert_string_equal(session->opts.wanted_methods[SSH_KEX], awaited); |
| 1934 |
free(awaited); |
| 1935 |
|
| 1936 |
/* check mac */ |
| 1937 |
if (ssh_fips_mode()) { |
| 1938 |
awaited = calloc(strlen(fips_mac) + 1, 1); |
| 1939 |
rc = snprintf(awaited, strlen(fips_mac) + 1, "%s", fips_mac); |
| 1940 |
assert_int_equal(rc, strlen(fips_mac)); |
| 1941 |
} else { |
| 1942 |
awaited = calloc(strlen(def_mac) + 1, 1); |
| 1943 |
rc = snprintf(awaited, strlen(def_mac) + 1, "%s", def_mac); |
| 1944 |
assert_int_equal(rc, strlen(def_mac)); |
| 1945 |
} |
| 1946 |
/* remove the substring from the defaults */ |
| 1947 |
helper_remove_substring(awaited, mac_removed, 0); |
| 1948 |
assert_string_equal(session->opts.wanted_methods[SSH_MAC_C_S], awaited); |
| 1949 |
free(awaited); |
| 1950 |
} |
| 1951 |
|
| 1952 |
/** |
| 1953 |
* @brief test that openssh style '-' feature works from file |
| 1954 |
*/ |
| 1955 |
static void torture_config_minus_file(void **state) |
| 1956 |
{ |
| 1957 |
torture_config_minus(state, LIBSSH_TESTCONFIG15, NULL); |
| 1958 |
} |
| 1959 |
|
| 1960 |
/** |
| 1961 |
* @brief test that openssh style '-' feature works from string |
| 1962 |
*/ |
| 1963 |
static void torture_config_minus_string(void **state) |
| 1964 |
{ |
| 1965 |
torture_config_minus(state, NULL, LIBSSH_TESTCONFIG_STRING15); |
| 1966 |
} |
| 1967 |
|
| 1968 |
/** |
| 1969 |
* @brief test that openssh style '^' feature works from string |
| 1970 |
*/ |
| 1971 |
static void torture_config_caret(void **state, |
| 1972 |
const char *file, const char *string) |
| 1973 |
{ |
| 1974 |
ssh_session session = *state; |
| 1975 |
const char *def_hostkeys = ssh_kex_get_default_methods(SSH_HOSTKEYS); |
| 1976 |
const char *fips_hostkeys = ssh_kex_get_fips_methods(SSH_HOSTKEYS); |
| 1977 |
const char *def_ciphers = ssh_kex_get_default_methods(SSH_CRYPT_C_S); |
| 1978 |
const char *fips_ciphers = ssh_kex_get_fips_methods(SSH_CRYPT_C_S); |
| 1979 |
const char *def_kex = ssh_kex_get_default_methods(SSH_KEX); |
| 1980 |
const char *fips_kex = ssh_kex_get_fips_methods(SSH_KEX); |
| 1981 |
const char *def_mac = ssh_kex_get_default_methods(SSH_MAC_C_S); |
| 1982 |
const char *fips_mac = ssh_kex_get_fips_methods(SSH_MAC_C_S); |
| 1983 |
const char *hostkeys_prio = "rsa-sha2-512,rsa-sha2-256"; |
| 1984 |
const char *ciphers_prio = "aes256-cbc,"; |
| 1985 |
const char *kex_prio = "diffie-hellman-group18-sha512,diffie-hellman-group16-sha512,"; |
| 1986 |
const char *fips_kex_prio = ",diffie-hellman-group16-sha512,diffie-hellman-group18-sha512"; |
| 1987 |
const char *mac_prio = "hmac-sha1,"; |
| 1988 |
char *awaited = NULL; |
| 1989 |
int rc; |
| 1990 |
|
| 1991 |
_parse_config(session, file, string, SSH_OK); |
| 1992 |
|
| 1993 |
/* check hostkeys */ |
| 1994 |
/* +2 for the added comma and the \0 */ |
| 1995 |
if (ssh_fips_mode()) { |
| 1996 |
awaited = calloc(strlen(hostkeys_prio) + strlen(fips_hostkeys) + 2, 1); |
| 1997 |
rc = snprintf(awaited, strlen(hostkeys_prio) + strlen(fips_hostkeys) + 2, |
| 1998 |
"%s,%s", hostkeys_prio, fips_hostkeys); |
| 1999 |
assert_int_equal(rc, strlen(hostkeys_prio) + strlen(fips_hostkeys) + 1); |
| 2000 |
} else { |
| 2001 |
awaited = calloc(strlen(def_hostkeys) + strlen(hostkeys_prio) + 2, 1); |
| 2002 |
rc = snprintf(awaited, strlen(hostkeys_prio) + strlen(def_hostkeys) + 2, |
| 2003 |
"%s,%s", hostkeys_prio, def_hostkeys); |
| 2004 |
assert_int_equal(rc, strlen(hostkeys_prio) + strlen(def_hostkeys) + 1); |
| 2005 |
} |
| 2006 |
|
| 2007 |
/* remove the substring from the defaults */ |
| 2008 |
helper_remove_substring(awaited, hostkeys_prio, 1); |
| 2009 |
/* remove the comma at the end of the list */ |
| 2010 |
awaited[strlen(awaited) - 1] = '\0'; |
| 2011 |
|
| 2012 |
assert_string_equal(session->opts.wanted_methods[SSH_HOSTKEYS], awaited); |
| 2013 |
free(awaited); |
| 2014 |
|
| 2015 |
/* check ciphers */ |
| 2016 |
if (ssh_fips_mode()) { |
| 2017 |
awaited = calloc(strlen(ciphers_prio) + strlen(fips_ciphers) + 1, 1); |
| 2018 |
rc = snprintf(awaited, strlen(ciphers_prio) + strlen(fips_ciphers) + 1, |
| 2019 |
"%s%s", ciphers_prio, fips_ciphers); |
| 2020 |
assert_int_equal(rc, strlen(ciphers_prio) + strlen(fips_ciphers)); |
| 2021 |
/* remove the substring from the defaults */ |
| 2022 |
helper_remove_substring(awaited, ciphers_prio, 1); |
| 2023 |
} else { |
| 2024 |
/* + 2 because the '\0' and the comma */ |
| 2025 |
awaited = calloc(strlen(ciphers_prio) + strlen(def_ciphers) + 1, 1); |
| 2026 |
rc = snprintf(awaited, strlen(ciphers_prio) + strlen(def_ciphers) + 1, |
| 2027 |
"%s%s", ciphers_prio, def_ciphers); |
| 2028 |
assert_int_equal(rc, strlen(ciphers_prio) + strlen(def_ciphers)); |
| 2029 |
} |
| 2030 |
|
| 2031 |
assert_string_equal(session->opts.wanted_methods[SSH_CRYPT_C_S], awaited); |
| 2032 |
free(awaited); |
| 2033 |
|
| 2034 |
/* check kex */ |
| 2035 |
if (ssh_fips_mode()) { |
| 2036 |
awaited = calloc(strlen(kex_prio) + strlen(fips_kex) + 1, 1); |
| 2037 |
rc = snprintf(awaited, strlen(kex_prio) + strlen(fips_kex) + 1, |
| 2038 |
"%s%s", kex_prio, fips_kex); |
| 2039 |
assert_int_equal(rc, strlen(kex_prio) + strlen(fips_kex)); |
| 2040 |
/* remove the substring from the defaults */ |
| 2041 |
/* the default list has different order of these two algos than the fips |
| 2042 |
* and because here is a braindead string substitution being done, |
| 2043 |
* change the order and remove the first occurrence of it */ |
| 2044 |
helper_remove_substring(awaited, fips_kex_prio, 0); |
| 2045 |
} else { |
| 2046 |
awaited = calloc(strlen(kex_prio) + strlen(def_kex) + 1, 1); |
| 2047 |
rc = snprintf(awaited, strlen(kex_prio) + strlen(def_kex) + 1, |
| 2048 |
"%s%s", kex_prio, def_kex); |
| 2049 |
assert_int_equal(rc, strlen(def_kex) + strlen(kex_prio)); |
| 2050 |
/* remove the substring from the defaults */ |
| 2051 |
helper_remove_substring(awaited, kex_prio, 1); |
| 2052 |
} |
| 2053 |
|
| 2054 |
assert_string_equal(session->opts.wanted_methods[SSH_KEX], awaited); |
| 2055 |
free(awaited); |
| 2056 |
|
| 2057 |
/* check mac */ |
| 2058 |
if (ssh_fips_mode()) { |
| 2059 |
awaited = calloc(strlen(mac_prio) + strlen(fips_mac) + 1, 1); |
| 2060 |
rc = snprintf(awaited, strlen(mac_prio) + strlen(fips_mac) + 1, "%s%s", mac_prio, fips_mac); |
| 2061 |
assert_int_equal(rc, strlen(mac_prio) + strlen(fips_mac)); |
| 2062 |
/* the fips list contains hmac-sha1 algo */ |
| 2063 |
helper_remove_substring(awaited, mac_prio, 1); |
| 2064 |
} else { |
| 2065 |
awaited = calloc(strlen(mac_prio) + strlen(def_mac) + 1, 1); |
| 2066 |
/* the mac is not in default; it is added to the list */ |
| 2067 |
rc = snprintf(awaited, strlen(mac_prio) + strlen(def_mac) + 1, "%s%s", mac_prio, def_mac); |
| 2068 |
assert_int_equal(rc, strlen(mac_prio) + strlen(def_mac)); |
| 2069 |
} |
| 2070 |
assert_string_equal(session->opts.wanted_methods[SSH_MAC_C_S], awaited); |
| 2071 |
free(awaited); |
| 2072 |
} |
| 2073 |
|
| 2074 |
/** |
| 2075 |
* @brief test that openssh style '^' feature works from file |
| 2076 |
*/ |
| 2077 |
static void torture_config_caret_file(void **state) |
| 2078 |
{ |
| 2079 |
torture_config_caret(state, LIBSSH_TESTCONFIG16, NULL); |
| 2080 |
} |
| 2081 |
|
| 2082 |
/** |
| 2083 |
* @brief test that openssh style '^' feature works from string |
| 2084 |
*/ |
| 2085 |
static void torture_config_caret_string(void **state) |
| 2086 |
{ |
| 2087 |
torture_config_caret(state, NULL, LIBSSH_TESTCONFIG_STRING16); |
| 2088 |
} |
| 2089 |
|
| 2090 |
/** |
| 2091 |
* @brief test PubkeyAcceptedKeyTypes helper function |
| 2092 |
*/ |
| 2093 |
static void torture_config_pubkeytypes(void **state, |
| 2094 |
const char *file, const char *string) |
| 2095 |
{ |
| 2096 |
ssh_session session = *state; |
| 2097 |
char *fips_algos; |
| 2098 |
|
| 2099 |
_parse_config(session, file, string, SSH_OK); |
| 2100 |
|
| 2101 |
if (ssh_fips_mode()) { |
| 2102 |
fips_algos = ssh_keep_fips_algos(SSH_HOSTKEYS, PUBKEYACCEPTEDTYPES); |
| 2103 |
assert_non_null(fips_algos); |
| 2104 |
assert_string_equal(session->opts.pubkey_accepted_types, fips_algos); |
| 2105 |
SAFE_FREE(fips_algos); |
| 2106 |
} else { |
| 2107 |
assert_string_equal(session->opts.pubkey_accepted_types, |
| 2108 |
PUBKEYACCEPTEDTYPES); |
| 2109 |
} |
| 2110 |
} |
| 2111 |
|
| 2112 |
/** |
| 2113 |
* @brief test parsing PubkeyAcceptedKeyTypes from file |
| 2114 |
*/ |
| 2115 |
static void torture_config_pubkeytypes_file(void **state) |
| 2116 |
{ |
| 2117 |
torture_config_pubkeytypes(state, LIBSSH_TEST_PUBKEYTYPES, NULL); |
| 2118 |
} |
| 2119 |
|
| 2120 |
/** |
| 2121 |
* @brief test parsing PubkeyAcceptedKeyTypes from string |
| 2122 |
*/ |
| 2123 |
static void torture_config_pubkeytypes_string(void **state) |
| 2124 |
{ |
| 2125 |
torture_config_pubkeytypes(state, NULL, LIBSSH_TEST_PUBKEYTYPES_STRING); |
| 2126 |
} |
| 2127 |
|
| 2128 |
/** |
| 2129 |
* @brief test parsing PubkeyAcceptedKAlgorithms from file |
| 2130 |
*/ |
| 2131 |
static void torture_config_pubkeyalgorithms_file(void **state) |
| 2132 |
{ |
| 2133 |
torture_config_pubkeytypes(state, LIBSSH_TEST_PUBKEYALGORITHMS, NULL); |
| 2134 |
} |
| 2135 |
|
| 2136 |
/** |
| 2137 |
* @brief test parsing PubkeyAcceptedAlgorithms from string |
| 2138 |
*/ |
| 2139 |
static void torture_config_pubkeyalgorithms_string(void **state) |
| 2140 |
{ |
| 2141 |
torture_config_pubkeytypes(state, NULL, LIBSSH_TEST_PUBKEYALGORITHMS_STRING); |
| 2142 |
} |
| 2143 |
|
| 2144 |
/** |
| 2145 |
* @brief Verify the configuration parser handles |
| 2146 |
* missing newline in the end |
| 2147 |
*/ |
| 2148 |
static void torture_config_nonewlineend(void **state, |
| 2149 |
const char *file, const char *string) |
| 2150 |
{ |
| 2151 |
_parse_config(*state, file, string, SSH_OK); |
| 2152 |
} |
| 2153 |
|
| 2154 |
/** |
| 2155 |
* @brief Verify the configuration parser handles |
| 2156 |
* missing newline in the end of file |
| 2157 |
*/ |
| 2158 |
static void torture_config_nonewlineend_file(void **state) |
| 2159 |
{ |
| 2160 |
torture_config_nonewlineend(state, LIBSSH_TEST_NONEWLINEEND, NULL); |
| 2161 |
} |
| 2162 |
|
| 2163 |
/** |
| 2164 |
* @brief Verify the configuration parser handles |
| 2165 |
* missing newline in the end of string |
| 2166 |
*/ |
| 2167 |
static void torture_config_nonewlineend_string(void **state) |
| 2168 |
{ |
| 2169 |
torture_config_nonewlineend(state, NULL, LIBSSH_TEST_NONEWLINEEND_STRING); |
| 2170 |
} |
| 2171 |
|
| 2172 |
/** |
| 2173 |
* @brief Verify the configuration parser handles |
| 2174 |
* missing newline in the end |
| 2175 |
*/ |
| 2176 |
static void torture_config_nonewlineoneline(void **state, |
| 2177 |
const char *file, |
| 2178 |
const char *string) |
| 2179 |
{ |
| 2180 |
_parse_config(*state, file, string, SSH_OK); |
| 2181 |
} |
| 2182 |
|
| 2183 |
/** |
| 2184 |
* @brief Verify the configuration parser handles |
| 2185 |
* missing newline in the end of file |
| 2186 |
*/ |
| 2187 |
static void torture_config_nonewlineoneline_file(void **state) |
| 2188 |
{ |
| 2189 |
torture_config_nonewlineend(state, LIBSSH_TEST_NONEWLINEONELINE, NULL); |
| 2190 |
} |
| 2191 |
|
| 2192 |
/** |
| 2193 |
* @brief Verify the configuration parser handles |
| 2194 |
* missing newline in the end of string |
| 2195 |
*/ |
| 2196 |
static void torture_config_nonewlineoneline_string(void **state) |
| 2197 |
{ |
| 2198 |
torture_config_nonewlineoneline(state, |
| 2199 |
NULL, LIBSSH_TEST_NONEWLINEONELINE_STRING); |
| 2200 |
} |
| 2201 |
|
| 2202 |
/* ssh_config_get_cmd() does these two things: |
| 2203 |
* * Strips leading whitespace |
| 2204 |
* * Terminate on the end of line |
| 2205 |
*/ |
| 2206 |
static void torture_config_parser_get_cmd(void **state) |
| 2207 |
{ |
| 2208 |
char *p = NULL, *tok = NULL; |
| 2209 |
char data[256]; |
| 2210 |
#ifdef WITH_EXEC |
| 2211 |
FILE *outfile = NULL, *infile = NULL; |
| 2212 |
int pid; |
| 2213 |
char buffer[256] = {0}; |
| 2214 |
#endif |
| 2215 |
(void)state; |
| 2216 |
|
| 2217 |
/* Ignore leading whitespace */ |
| 2218 |
strncpy(data, " \t\t string\n", sizeof(data)); |
| 2219 |
p = data; |
| 2220 |
tok = ssh_config_get_cmd(&p); |
| 2221 |
assert_string_equal(tok, "string"); |
| 2222 |
assert_int_equal(*p, '\0'); |
| 2223 |
|
| 2224 |
/* but keeps the trailing whitespace */ |
| 2225 |
strncpy(data, "string \t\t \n", sizeof(data)); |
| 2226 |
p = data; |
| 2227 |
tok = ssh_config_get_cmd(&p); |
| 2228 |
assert_string_equal(tok, "string \t\t "); |
| 2229 |
assert_int_equal(*p, '\0'); |
| 2230 |
|
| 2231 |
/* should not drop the quotes and not split them into separate arguments */ |
| 2232 |
strncpy(data, "\"multi string\" something\n", sizeof(data)); |
| 2233 |
p = data; |
| 2234 |
tok = ssh_config_get_cmd(&p); |
| 2235 |
assert_string_equal(tok, "\"multi string\" something"); |
| 2236 |
assert_int_equal(*p, '\0'); |
| 2237 |
|
| 2238 |
/* But it does not split tokens by whitespace |
| 2239 |
* if they are not quoted, which is weird */ |
| 2240 |
strncpy(data, "multi string something\n", sizeof(data)); |
| 2241 |
p = data; |
| 2242 |
tok = ssh_config_get_cmd(&p); |
| 2243 |
assert_string_equal(tok, "multi string something"); |
| 2244 |
assert_int_equal(*p, '\0'); |
| 2245 |
|
| 2246 |
/* Commands in quotes are not treated special */ |
| 2247 |
sprintf(data, "%s%s%s%s", "\"", SOURCEDIR "/tests/unittests/hello world.sh", "\" ", "\"hello libssh\"\n"); |
| 2248 |
printf("%s\n", data); |
| 2249 |
p = data; |
| 2250 |
tok = ssh_config_get_cmd(&p); |
| 2251 |
assert_string_equal(tok, data); |
| 2252 |
assert_int_equal(*p, '\0'); |
| 2253 |
|
| 2254 |
#ifdef WITH_EXEC |
| 2255 |
/* Check if the command would get correctly executed |
| 2256 |
* Use the script file "hello world.sh" to echo the first argument |
| 2257 |
* Run as <= "/workdir/hello world.sh" "hello libssh" => */ |
| 2258 |
|
| 2259 |
/* output to file and check wrong */ |
| 2260 |
outfile = fopen("output.log", "a+"); |
| 2261 |
assert_non_null(outfile); |
| 2262 |
printf("the tok is %s\n", tok); |
| 2263 |
|
| 2264 |
pid = fork(); |
| 2265 |
if (pid == -1) { |
| 2266 |
perror("fork"); |
| 2267 |
} else if (pid == 0) { |
| 2268 |
ssh_execute_command(tok, fileno(outfile), fileno(outfile)); |
| 2269 |
/* Does not return */ |
| 2270 |
} else { |
| 2271 |
/* parent |
| 2272 |
* wait child process */ |
| 2273 |
wait(NULL); |
| 2274 |
infile = fopen("output.log", "r"); |
| 2275 |
assert_non_null(infile); |
| 2276 |
p = fgets(buffer, sizeof(buffer), infile); |
| 2277 |
fclose(infile); |
| 2278 |
remove("output.log"); |
| 2279 |
assert_non_null(p); |
| 2280 |
} |
| 2281 |
|
| 2282 |
fclose(outfile); |
| 2283 |
assert_string_equal(buffer, "hello libssh"); |
| 2284 |
#endif /* WITH_EXEC */ |
| 2285 |
} |
| 2286 |
|
| 2287 |
/* ssh_config_get_token() should behave as expected |
| 2288 |
* * Strip leading whitespace |
| 2289 |
* * Return first token separated by whitespace or equal sign, |
| 2290 |
* respecting quotes! |
| 2291 |
* * Correctly treat escaped quotes inside of quotes. |
| 2292 |
*/ |
| 2293 |
static void torture_config_parser_get_token(void **state) |
| 2294 |
{ |
| 2295 |
char *p = NULL, *tok = NULL; |
| 2296 |
char data[256]; |
| 2297 |
|
| 2298 |
(void) state; |
| 2299 |
|
| 2300 |
/* Ignore leading whitespace (from get_cmd() already */ |
| 2301 |
strncpy(data, " \t\t string\n", sizeof(data)); |
| 2302 |
p = data; |
| 2303 |
tok = ssh_config_get_token(&p); |
| 2304 |
assert_string_equal(tok, "string"); |
| 2305 |
assert_int_equal(*p, '\0'); |
| 2306 |
|
| 2307 |
strncpy(data, " \t\t string", sizeof(data)); |
| 2308 |
p = data; |
| 2309 |
tok = ssh_config_get_token(&p); |
| 2310 |
assert_string_equal(tok, "string"); |
| 2311 |
assert_int_equal(*p, '\0'); |
| 2312 |
|
| 2313 |
/* drops trailing whitespace */ |
| 2314 |
strncpy(data, "string \t\t \n", sizeof(data)); |
| 2315 |
p = data; |
| 2316 |
tok = ssh_config_get_token(&p); |
| 2317 |
assert_string_equal(tok, "string"); |
| 2318 |
assert_int_equal(*p, '\0'); |
| 2319 |
|
| 2320 |
strncpy(data, "string \t\t ", sizeof(data)); |
| 2321 |
p = data; |
| 2322 |
tok = ssh_config_get_token(&p); |
| 2323 |
assert_string_equal(tok, "string"); |
| 2324 |
assert_int_equal(*p, '\0'); |
| 2325 |
|
| 2326 |
/* Correctly handles tokens in quotes */ |
| 2327 |
strncpy(data, "\"multi string\" something\n", sizeof(data)); |
| 2328 |
p = data; |
| 2329 |
tok = ssh_config_get_token(&p); |
| 2330 |
assert_string_equal(tok, "multi string"); |
| 2331 |
assert_int_equal(*p, 's'); |
| 2332 |
tok = ssh_config_get_token(&p); |
| 2333 |
assert_string_equal(tok, "something"); |
| 2334 |
assert_int_equal(*p, '\0'); |
| 2335 |
|
| 2336 |
strncpy(data, "\"multi string\" something", sizeof(data)); |
| 2337 |
p = data; |
| 2338 |
tok = ssh_config_get_token(&p); |
| 2339 |
assert_string_equal(tok, "multi string"); |
| 2340 |
assert_int_equal(*p, 's'); |
| 2341 |
tok = ssh_config_get_token(&p); |
| 2342 |
assert_string_equal(tok, "something"); |
| 2343 |
assert_int_equal(*p, '\0'); |
| 2344 |
|
| 2345 |
/* Consistently splits unquoted strings */ |
| 2346 |
strncpy(data, "multi string something\n", sizeof(data)); |
| 2347 |
p = data; |
| 2348 |
tok = ssh_config_get_token(&p); |
| 2349 |
assert_string_equal(tok, "multi"); |
| 2350 |
assert_int_equal(*p, 's'); |
| 2351 |
tok = ssh_config_get_token(&p); |
| 2352 |
assert_string_equal(tok, "string"); |
| 2353 |
assert_int_equal(*p, 's'); |
| 2354 |
tok = ssh_config_get_token(&p); |
| 2355 |
assert_string_equal(tok, "something"); |
| 2356 |
assert_int_equal(*p, '\0'); |
| 2357 |
|
| 2358 |
strncpy(data, "multi string something", sizeof(data)); |
| 2359 |
p = data; |
| 2360 |
tok = ssh_config_get_token(&p); |
| 2361 |
assert_string_equal(tok, "multi"); |
| 2362 |
assert_int_equal(*p, 's'); |
| 2363 |
tok = ssh_config_get_token(&p); |
| 2364 |
assert_string_equal(tok, "string"); |
| 2365 |
assert_int_equal(*p, 's'); |
| 2366 |
tok = ssh_config_get_token(&p); |
| 2367 |
assert_string_equal(tok, "something"); |
| 2368 |
assert_int_equal(*p, '\0'); |
| 2369 |
|
| 2370 |
/* It is made to parse also option=value pairs as well */ |
| 2371 |
strncpy(data, " key=value \n", sizeof(data)); |
| 2372 |
p = data; |
| 2373 |
tok = ssh_config_get_token(&p); |
| 2374 |
assert_string_equal(tok, "key"); |
| 2375 |
assert_int_equal(*p, 'v'); |
| 2376 |
tok = ssh_config_get_token(&p); |
| 2377 |
assert_string_equal(tok, "value"); |
| 2378 |
assert_int_equal(*p, '\0'); |
| 2379 |
|
| 2380 |
strncpy(data, " key=value ", sizeof(data)); |
| 2381 |
p = data; |
| 2382 |
tok = ssh_config_get_token(&p); |
| 2383 |
assert_string_equal(tok, "key"); |
| 2384 |
assert_int_equal(*p, 'v'); |
| 2385 |
tok = ssh_config_get_token(&p); |
| 2386 |
assert_string_equal(tok, "value"); |
| 2387 |
assert_int_equal(*p, '\0'); |
| 2388 |
|
| 2389 |
/* spaces are allowed also around the equal sign */ |
| 2390 |
strncpy(data, " key = value \n", sizeof(data)); |
| 2391 |
p = data; |
| 2392 |
tok = ssh_config_get_token(&p); |
| 2393 |
assert_string_equal(tok, "key"); |
| 2394 |
assert_int_equal(*p, 'v'); |
| 2395 |
tok = ssh_config_get_token(&p); |
| 2396 |
assert_string_equal(tok, "value"); |
| 2397 |
assert_int_equal(*p, '\0'); |
| 2398 |
|
| 2399 |
strncpy(data, " key = value ", sizeof(data)); |
| 2400 |
p = data; |
| 2401 |
tok = ssh_config_get_token(&p); |
| 2402 |
assert_string_equal(tok, "key"); |
| 2403 |
assert_int_equal(*p, 'v'); |
| 2404 |
tok = ssh_config_get_token(&p); |
| 2405 |
assert_string_equal(tok, "value"); |
| 2406 |
assert_int_equal(*p, '\0'); |
| 2407 |
|
| 2408 |
/* correctly parses even key=value pairs with either one in quotes */ |
| 2409 |
strncpy(data, " key=\"value with spaces\" \n", sizeof(data)); |
| 2410 |
p = data; |
| 2411 |
tok = ssh_config_get_token(&p); |
| 2412 |
assert_string_equal(tok, "key"); |
| 2413 |
assert_int_equal(*p, '\"'); |
| 2414 |
tok = ssh_config_get_token(&p); |
| 2415 |
assert_string_equal(tok, "value with spaces"); |
| 2416 |
assert_int_equal(*p, '\0'); |
| 2417 |
|
| 2418 |
strncpy(data, " key=\"value with spaces\" ", sizeof(data)); |
| 2419 |
p = data; |
| 2420 |
tok = ssh_config_get_token(&p); |
| 2421 |
assert_string_equal(tok, "key"); |
| 2422 |
assert_int_equal(*p, '\"'); |
| 2423 |
tok = ssh_config_get_token(&p); |
| 2424 |
assert_string_equal(tok, "value with spaces"); |
| 2425 |
assert_int_equal(*p, '\0'); |
| 2426 |
|
| 2427 |
/* Only one equal sign is allowed */ |
| 2428 |
strncpy(data, "key==value\n", sizeof(data)); |
| 2429 |
p = data; |
| 2430 |
tok = ssh_config_get_token(&p); |
| 2431 |
assert_string_equal(tok, "key"); |
| 2432 |
assert_int_equal(*p, '='); |
| 2433 |
tok = ssh_config_get_token(&p); |
| 2434 |
assert_string_equal(tok, ""); |
| 2435 |
assert_int_equal(*p, 'v'); |
| 2436 |
tok = ssh_config_get_token(&p); |
| 2437 |
assert_string_equal(tok, "value"); |
| 2438 |
assert_int_equal(*p, '\0'); |
| 2439 |
|
| 2440 |
strncpy(data, "key==value", sizeof(data)); |
| 2441 |
p = data; |
| 2442 |
tok = ssh_config_get_token(&p); |
| 2443 |
assert_string_equal(tok, "key"); |
| 2444 |
assert_int_equal(*p, '='); |
| 2445 |
tok = ssh_config_get_token(&p); |
| 2446 |
assert_string_equal(tok, ""); |
| 2447 |
assert_int_equal(*p, 'v'); |
| 2448 |
tok = ssh_config_get_token(&p); |
| 2449 |
assert_string_equal(tok, "value"); |
| 2450 |
assert_int_equal(*p, '\0'); |
| 2451 |
|
| 2452 |
/* Unmatched quotes */ |
| 2453 |
strncpy(data, " \"value\n", sizeof(data)); |
| 2454 |
p = data; |
| 2455 |
tok = ssh_config_get_token(&p); |
| 2456 |
assert_string_equal(tok, "value"); |
| 2457 |
assert_int_equal(*p, '\0'); |
| 2458 |
|
| 2459 |
strncpy(data, " \"value", sizeof(data)); |
| 2460 |
p = data; |
| 2461 |
tok = ssh_config_get_token(&p); |
| 2462 |
assert_string_equal(tok, "value"); |
| 2463 |
assert_int_equal(*p, '\0'); |
| 2464 |
|
| 2465 |
/* Escaped quotes */ |
| 2466 |
strncpy(data, " \"value with \\\"escaped\\\" quotes\" \n", sizeof(data)); |
| 2467 |
p = data; |
| 2468 |
tok = ssh_config_get_token(&p); |
| 2469 |
assert_string_equal(tok, "value with \"escaped\" quotes"); |
| 2470 |
assert_int_equal(*p, '\0'); |
| 2471 |
|
| 2472 |
strncpy(data, "\\\"value with \\\"escaped\\\" quotes\\\"\n", sizeof(data)); |
| 2473 |
p = data; |
| 2474 |
tok = ssh_config_get_token(&p); |
| 2475 |
assert_string_equal(tok, "\\\"value"); |
| 2476 |
assert_int_equal(*p, 'w'); |
| 2477 |
tok = ssh_config_get_token(&p); |
| 2478 |
assert_string_equal(tok, "with"); |
| 2479 |
assert_int_equal(*p, '\\'); |
| 2480 |
tok = ssh_config_get_token(&p); |
| 2481 |
assert_string_equal(tok, "\\\"escaped\\\""); |
| 2482 |
assert_int_equal(*p, 'q'); |
| 2483 |
tok = ssh_config_get_token(&p); |
| 2484 |
assert_string_equal(tok, "quotes\\\""); |
| 2485 |
assert_int_equal(*p, '\0'); |
| 2486 |
} |
| 2487 |
|
| 2488 |
/* match_pattern() sanity tests |
| 2489 |
*/ |
| 2490 |
static void torture_config_match_pattern(void **state) |
| 2491 |
{ |
| 2492 |
int rv = 0; |
| 2493 |
|
| 2494 |
(void) state; |
| 2495 |
|
| 2496 |
/* Simple test "a" matches "a" */ |
| 2497 |
rv = match_pattern("a", "a"); |
| 2498 |
assert_int_equal(rv, 1); |
| 2499 |
|
| 2500 |
/* Simple test "a" does not match "b" */ |
| 2501 |
rv = match_pattern("a", "b"); |
| 2502 |
assert_int_equal(rv, 0); |
| 2503 |
|
| 2504 |
/* NULL arguments are correctly handled */ |
| 2505 |
rv = match_pattern("a", NULL); |
| 2506 |
assert_int_equal(rv, 0); |
| 2507 |
rv = match_pattern(NULL, "a"); |
| 2508 |
assert_int_equal(rv, 0); |
| 2509 |
|
| 2510 |
/* Simple wildcard ? is handled in pattern */ |
| 2511 |
rv = match_pattern("a", "?"); |
| 2512 |
assert_int_equal(rv, 1); |
| 2513 |
rv = match_pattern("aa", "?"); |
| 2514 |
assert_int_equal(rv, 0); |
| 2515 |
/* Wildcard in search string */ |
| 2516 |
rv = match_pattern("?", "a"); |
| 2517 |
assert_int_equal(rv, 0); |
| 2518 |
rv = match_pattern("?", "?"); |
| 2519 |
assert_int_equal(rv, 1); |
| 2520 |
|
| 2521 |
/* Simple wildcard * is handled in pattern */ |
| 2522 |
rv = match_pattern("a", "*"); |
| 2523 |
assert_int_equal(rv, 1); |
| 2524 |
rv = match_pattern("aa", "*"); |
| 2525 |
assert_int_equal(rv, 1); |
| 2526 |
/* Wildcard in search string */ |
| 2527 |
rv = match_pattern("*", "a"); |
| 2528 |
assert_int_equal(rv, 0); |
| 2529 |
rv = match_pattern("*", "*"); |
| 2530 |
assert_int_equal(rv, 1); |
| 2531 |
|
| 2532 |
/* More complicated patterns */ |
| 2533 |
rv = match_pattern("a", "*a"); |
| 2534 |
assert_int_equal(rv, 1); |
| 2535 |
rv = match_pattern("a", "a*"); |
| 2536 |
assert_int_equal(rv, 1); |
| 2537 |
rv = match_pattern("abababc", "*abc"); |
| 2538 |
assert_int_equal(rv, 1); |
| 2539 |
rv = match_pattern("ababababca", "*abc"); |
| 2540 |
assert_int_equal(rv, 0); |
| 2541 |
rv = match_pattern("ababababca", "*abc*"); |
| 2542 |
assert_int_equal(rv, 1); |
| 2543 |
|
| 2544 |
/* Multiple wildcards in row */ |
| 2545 |
rv = match_pattern("aa", "??"); |
| 2546 |
assert_int_equal(rv, 1); |
| 2547 |
rv = match_pattern("bba", "??a"); |
| 2548 |
assert_int_equal(rv, 1); |
| 2549 |
rv = match_pattern("aaa", "**a"); |
| 2550 |
assert_int_equal(rv, 1); |
| 2551 |
rv = match_pattern("bbb", "**a"); |
| 2552 |
assert_int_equal(rv, 0); |
| 2553 |
|
| 2554 |
/* Consecutive asterisks do not make sense and do not need to recurse */ |
| 2555 |
rv = match_pattern("hostname", "**********pattern"); |
| 2556 |
assert_int_equal(rv, 0); |
| 2557 |
rv = match_pattern("hostname", "pattern**********"); |
| 2558 |
assert_int_equal(rv, 0); |
| 2559 |
rv = match_pattern("pattern", "***********pattern"); |
| 2560 |
assert_int_equal(rv, 1); |
| 2561 |
rv = match_pattern("pattern", "pattern***********"); |
| 2562 |
assert_int_equal(rv, 1); |
| 2563 |
|
| 2564 |
rv = match_pattern("hostname", "*p*a*t*t*e*r*n*"); |
| 2565 |
assert_int_equal(rv, 0); |
| 2566 |
rv = match_pattern("pattern", "*p*a*t*t*e*r*n*"); |
| 2567 |
assert_int_equal(rv, 1); |
| 2568 |
|
| 2569 |
/* Regular Expression Denial of Service */ |
| 2570 |
rv = match_pattern("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", |
| 2571 |
"*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a"); |
| 2572 |
assert_int_equal(rv, 1); |
| 2573 |
rv = match_pattern("ababababababababababababababababababababab", |
| 2574 |
"*a*b*a*b*a*b*a*b*a*b*a*b*a*b*a*b"); |
| 2575 |
assert_int_equal(rv, 1); |
| 2576 |
|
| 2577 |
/* A lot of backtracking */ |
| 2578 |
rv = match_pattern("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaax", |
| 2579 |
"a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*ax"); |
| 2580 |
assert_int_equal(rv, 1); |
| 2581 |
|
| 2582 |
/* Test backtracking: *a matches first 'a', fails on 'b', must backtrack */ |
| 2583 |
rv = match_pattern("axaxaxb", "*a*b"); |
| 2584 |
assert_int_equal(rv, 1); |
| 2585 |
|
| 2586 |
/* Test greedy consumption with suffix */ |
| 2587 |
rv = match_pattern("foo_bar_baz_bar", "*bar"); |
| 2588 |
assert_int_equal(rv, 1); |
| 2589 |
|
| 2590 |
/* Test exact suffix requirement (ensure no partial match acceptance) */ |
| 2591 |
rv = match_pattern("foobar_extra", "*bar"); |
| 2592 |
assert_int_equal(rv, 0); |
| 2593 |
|
| 2594 |
/* Test multiple distinct wildcards */ |
| 2595 |
rv = match_pattern("a_very_long_string_with_a_pattern", "*long*pattern"); |
| 2596 |
assert_int_equal(rv, 1); |
| 2597 |
|
| 2598 |
/* ? inside a * sequence */ |
| 2599 |
rv = match_pattern("abcdefg", "a*c?e*g"); |
| 2600 |
assert_int_equal(rv, 1); |
| 2601 |
|
| 2602 |
/* Consecutive mixed wildcards */ |
| 2603 |
rv = match_pattern("abc", "*?c"); |
| 2604 |
assert_int_equal(rv, 1); |
| 2605 |
|
| 2606 |
/* ? at the very end after * */ |
| 2607 |
rv = match_pattern("abc", "ab?"); |
| 2608 |
assert_int_equal(rv, 1); |
| 2609 |
rv = match_pattern("abc", "ab*?"); |
| 2610 |
assert_int_equal(rv, 1); |
| 2611 |
|
| 2612 |
/* Consecutive stars should be collapsed or handled gracefully */ |
| 2613 |
rv = match_pattern("abc", "a**c"); |
| 2614 |
assert_int_equal(rv, 1); |
| 2615 |
rv = match_pattern("abc", "***"); |
| 2616 |
assert_int_equal(rv, 1); |
| 2617 |
|
| 2618 |
/* Empty string handling */ |
| 2619 |
rv = match_pattern("", "*"); |
| 2620 |
assert_int_equal(rv, 1); |
| 2621 |
rv = match_pattern("", "?"); |
| 2622 |
assert_int_equal(rv, 0); |
| 2623 |
rv = match_pattern("", ""); |
| 2624 |
assert_int_equal(rv, 1); |
| 2625 |
|
| 2626 |
/* Pattern longer than string */ |
| 2627 |
rv = match_pattern("short", "short_but_longer"); |
| 2628 |
assert_int_equal(rv, 0); |
| 2629 |
} |
| 2630 |
|
| 2631 |
/* Identity file can be specified multiple times in the configuration |
| 2632 |
*/ |
| 2633 |
static void torture_config_identity(void **state) |
| 2634 |
{ |
| 2635 |
const char *id = NULL; |
| 2636 |
const char *cert = NULL; |
| 2637 |
struct ssh_iterator *it = NULL; |
| 2638 |
ssh_session session = *state; |
| 2639 |
|
| 2640 |
_parse_config(session, NULL, LIBSSH_TESTCONFIG_STRING13, SSH_OK); |
| 2641 |
|
| 2642 |
/* The identities are first added to this temporary list before expanding */ |
| 2643 |
it = ssh_list_get_iterator(session->opts.identity_non_exp); |
| 2644 |
assert_non_null(it); |
| 2645 |
id = it->data; |
| 2646 |
/* The identities are prepended to the list so we start with second one */ |
| 2647 |
assert_string_equal(id, "id_ecdsa_two"); |
| 2648 |
|
| 2649 |
it = it->next; |
| 2650 |
assert_non_null(it); |
| 2651 |
id = it->data; |
| 2652 |
assert_string_equal(id, "id_rsa_one"); |
| 2653 |
|
| 2654 |
/* The certs are first added to this temporary list before expanding */ |
| 2655 |
it = ssh_list_get_iterator(session->opts.certificate_non_exp); |
| 2656 |
assert_non_null(it); |
| 2657 |
cert = it->data; |
| 2658 |
/* The certs are coming as listed in the configuration file */ |
| 2659 |
assert_string_equal(cert, "id_rsa_one-cert.pub"); |
| 2660 |
|
| 2661 |
it = it->next; |
| 2662 |
assert_non_null(it); |
| 2663 |
cert = it->data; |
| 2664 |
assert_string_equal(cert, "id_ecdsa_two-cert.pub"); |
| 2665 |
/* and that is all */ |
| 2666 |
assert_null(it->next); |
| 2667 |
} |
| 2668 |
|
| 2669 |
/* Make absolute path for config include |
| 2670 |
*/ |
| 2671 |
static void torture_config_make_absolute_int(void **state, bool no_sshdir_fails) |
| 2672 |
{ |
| 2673 |
ssh_session session = *state; |
| 2674 |
char *result = NULL; |
| 2675 |
#ifndef _WIN32 |
| 2676 |
char h[256] = {0}; |
| 2677 |
char *user = NULL; |
| 2678 |
char *home = NULL; |
| 2679 |
struct passwd *pw = getpwuid(getuid()); |
| 2680 |
assert_non_null(pw); |
| 2681 |
user = strdup(pw->pw_name); |
| 2682 |
assert_non_null(user); |
| 2683 |
home = strdup(pw->pw_dir); |
| 2684 |
assert_non_null(home); |
| 2685 |
#endif |
| 2686 |
|
| 2687 |
/* Absolute path already -- should not change in any case */ |
| 2688 |
result = ssh_config_make_absolute(session, "/etc/ssh/ssh_config.d/*.conf", 1); |
| 2689 |
assert_string_equal(result, "/etc/ssh/ssh_config.d/*.conf"); |
| 2690 |
free(result); |
| 2691 |
result = ssh_config_make_absolute(session, "/etc/ssh/ssh_config.d/*.conf", 0); |
| 2692 |
assert_string_equal(result, "/etc/ssh/ssh_config.d/*.conf"); |
| 2693 |
free(result); |
| 2694 |
|
| 2695 |
/* Global is relative to /etc/ssh/ */ |
| 2696 |
result = ssh_config_make_absolute(session, "ssh_config.d/test.conf", 1); |
| 2697 |
assert_string_equal(result, "/etc/ssh/ssh_config.d/test.conf"); |
| 2698 |
free(result); |
| 2699 |
result = ssh_config_make_absolute(session, "./ssh_config.d/test.conf", 1); |
| 2700 |
assert_string_equal(result, "/etc/ssh/./ssh_config.d/test.conf"); |
| 2701 |
free(result); |
| 2702 |
|
| 2703 |
/* User config is relative to sshdir -- here faked to /tmp/ssh/ */ |
| 2704 |
result = ssh_config_make_absolute(session, "my_config", 0); |
| 2705 |
if (no_sshdir_fails) { |
| 2706 |
assert_null(result); |
| 2707 |
} else { |
| 2708 |
/* The path depends on the PWD so lets skip checking the actual path here */ |
| 2709 |
assert_non_null(result); |
| 2710 |
} |
| 2711 |
free(result); |
| 2712 |
|
| 2713 |
/* User config is relative to sshdir -- here faked to /tmp/ssh/ */ |
| 2714 |
ssh_options_set(session, SSH_OPTIONS_SSH_DIR, "/tmp/ssh"); |
| 2715 |
result = ssh_config_make_absolute(session, "my_config", 0); |
| 2716 |
assert_string_equal(result, "/tmp/ssh/my_config"); |
| 2717 |
free(result); |
| 2718 |
|
| 2719 |
#ifndef _WIN32 |
| 2720 |
/* Tilde expansion works only in user config */ |
| 2721 |
result = ssh_config_make_absolute(session, "~/.ssh/config.d/*.conf", 0); |
| 2722 |
snprintf(h, 256 - 1, "%s/.ssh/config.d/*.conf", home); |
| 2723 |
assert_string_equal(result, h); |
| 2724 |
free(result); |
| 2725 |
|
| 2726 |
snprintf(h, 256 - 1, "~%s/.ssh/config.d/*.conf", user); |
| 2727 |
result = ssh_config_make_absolute(session, h, 0); |
| 2728 |
snprintf(h, 256 - 1, "%s/.ssh/config.d/*.conf", home); |
| 2729 |
assert_string_equal(result, h); |
| 2730 |
free(result); |
| 2731 |
|
| 2732 |
/* in global config its just prefixed without expansion */ |
| 2733 |
result = ssh_config_make_absolute(session, "~/.ssh/config.d/*.conf", 1); |
| 2734 |
assert_string_equal(result, "/etc/ssh/~/.ssh/config.d/*.conf"); |
| 2735 |
free(result); |
| 2736 |
snprintf(h, 256 - 1, "~%s/.ssh/config.d/*.conf", user); |
| 2737 |
result = ssh_config_make_absolute(session, h, 1); |
| 2738 |
snprintf(h, 256 - 1, "/etc/ssh/~%s/.ssh/config.d/*.conf", user); |
| 2739 |
assert_string_equal(result, h); |
| 2740 |
free(result); |
| 2741 |
free(home); |
| 2742 |
free(user); |
| 2743 |
#endif |
| 2744 |
} |
| 2745 |
|
| 2746 |
static void torture_config_make_absolute(void **state) |
| 2747 |
{ |
| 2748 |
torture_config_make_absolute_int(state, 0); |
| 2749 |
} |
| 2750 |
|
| 2751 |
static void torture_config_make_absolute_no_sshdir(void **state) |
| 2752 |
{ |
| 2753 |
torture_config_make_absolute_int(state, 1); |
| 2754 |
} |
| 2755 |
|
| 2756 |
static void torture_config_parse_uri(void **state) |
| 2757 |
{ |
| 2758 |
char *username = NULL; |
| 2759 |
char *hostname = NULL; |
| 2760 |
char *port = NULL; |
| 2761 |
int rc; |
| 2762 |
|
| 2763 |
(void)state; /* unused */ |
| 2764 |
|
| 2765 |
rc = ssh_config_parse_uri("localhost", &username, &hostname, &port, false); |
| 2766 |
assert_return_code(rc, errno); |
| 2767 |
assert_null(username); |
| 2768 |
assert_string_equal(hostname, "localhost"); |
| 2769 |
SAFE_FREE(hostname); |
| 2770 |
assert_null(port); |
| 2771 |
|
| 2772 |
rc = ssh_config_parse_uri("1.2.3.4", &username, &hostname, &port, false); |
| 2773 |
assert_return_code(rc, errno); |
| 2774 |
assert_null(username); |
| 2775 |
assert_string_equal(hostname, "1.2.3.4"); |
| 2776 |
SAFE_FREE(hostname); |
| 2777 |
assert_null(port); |
| 2778 |
|
| 2779 |
rc = ssh_config_parse_uri("1.2.3.4:2222", &username, &hostname, &port, false); |
| 2780 |
assert_return_code(rc, errno); |
| 2781 |
assert_null(username); |
| 2782 |
assert_string_equal(hostname, "1.2.3.4"); |
| 2783 |
SAFE_FREE(hostname); |
| 2784 |
assert_string_equal(port, "2222"); |
| 2785 |
SAFE_FREE(port); |
| 2786 |
|
| 2787 |
rc = ssh_config_parse_uri("[1:2:3::4]:2222", &username, &hostname, &port, false); |
| 2788 |
assert_return_code(rc, errno); |
| 2789 |
assert_null(username); |
| 2790 |
assert_string_equal(hostname, "1:2:3::4"); |
| 2791 |
SAFE_FREE(hostname); |
| 2792 |
assert_string_equal(port, "2222"); |
| 2793 |
SAFE_FREE(port); |
| 2794 |
|
| 2795 |
/* do not want port */ |
| 2796 |
rc = ssh_config_parse_uri("1:2:3::4", &username, &hostname, NULL, true); |
| 2797 |
assert_return_code(rc, errno); |
| 2798 |
assert_null(username); |
| 2799 |
assert_string_equal(hostname, "1:2:3::4"); |
| 2800 |
SAFE_FREE(hostname); |
| 2801 |
|
| 2802 |
rc = ssh_config_parse_uri("user -name@", &username, NULL, NULL, true); |
| 2803 |
assert_int_equal(rc, SSH_ERROR); |
| 2804 |
} |
| 2805 |
|
| 2806 |
/* Complex ssh match configurations |
| 2807 |
*/ |
| 2808 |
static void torture_config_match_complex(void **state) |
| 2809 |
{ |
| 2810 |
ssh_session session = *state; |
| 2811 |
char *v = NULL; |
| 2812 |
int ret; |
| 2813 |
|
| 2814 |
ssh_options_set(session, SSH_OPTIONS_HOST, "Bar"); |
| 2815 |
|
| 2816 |
_parse_config(session, LIBSSH_TESTCONFIG_MATCH_COMPLEX, NULL, SSH_OK); |
| 2817 |
|
| 2818 |
/* Test the variable presence */ |
| 2819 |
ret = ssh_options_get(session, SSH_OPTIONS_HOST, &v); |
| 2820 |
assert_return_code(ret, errno); |
| 2821 |
assert_non_null(v); |
| 2822 |
#ifndef WITH_EXEC |
| 2823 |
assert_string_equal(session->opts.host, "Bar"); |
| 2824 |
#else |
| 2825 |
assert_string_equal(v, "complex-match"); |
| 2826 |
#endif |
| 2827 |
ssh_string_free_char(v); |
| 2828 |
} |
| 2829 |
|
| 2830 |
/* Missing value to LogLevel configuration option |
| 2831 |
*/ |
| 2832 |
static void torture_config_loglevel_missing_value(void **state) |
| 2833 |
{ |
| 2834 |
ssh_session session = *state; |
| 2835 |
|
| 2836 |
ssh_options_set(session, SSH_OPTIONS_HOST, "Bar"); |
| 2837 |
|
| 2838 |
_parse_config(session, LIBSSH_TESTCONFIG_LOGLEVEL_MISSING, NULL, SSH_OK); |
| 2839 |
} |
| 2840 |
|
| 2841 |
static int before_connection(ssh_session jump_session, void *user) |
| 2842 |
{ |
| 2843 |
char *v = NULL; |
| 2844 |
int ret; |
| 2845 |
|
| 2846 |
(void)user; |
| 2847 |
|
| 2848 |
/* During the connection, we force parsing the same configuration file |
| 2849 |
* (would be normally parsed automatically during the connection itself) |
| 2850 |
*/ |
| 2851 |
ret = ssh_config_parse_file(jump_session, LIBSSH_TESTCONFIG_JUMP); |
| 2852 |
assert_return_code(ret, errno); |
| 2853 |
|
| 2854 |
/* Test the variable presence */ |
| 2855 |
ret = ssh_options_get(jump_session, SSH_OPTIONS_HOST, &v); |
| 2856 |
assert_return_code(ret, errno); |
| 2857 |
assert_string_equal(v, "1xxxxxx"); |
| 2858 |
ssh_string_free_char(v); |
| 2859 |
|
| 2860 |
ret = ssh_options_get(jump_session, SSH_OPTIONS_USER, &v); |
| 2861 |
assert_return_code(ret, errno); |
| 2862 |
assert_string_equal(v, "ubuntu"); |
| 2863 |
ssh_string_free_char(v); |
| 2864 |
|
| 2865 |
assert_int_equal(jump_session->opts.port, 23); |
| 2866 |
|
| 2867 |
/* Fail the connection -- we are in unit tests so it would fail anyway */ |
| 2868 |
return 1; |
| 2869 |
} |
| 2870 |
|
| 2871 |
static int verify_knownhost(ssh_session jump_session, void *user) |
| 2872 |
{ |
| 2873 |
(void)jump_session; |
| 2874 |
(void)user; |
| 2875 |
|
| 2876 |
return 0; |
| 2877 |
} |
| 2878 |
|
| 2879 |
static int authenticate(ssh_session jump_session, void *user) |
| 2880 |
{ |
| 2881 |
(void)jump_session; |
| 2882 |
(void)user; |
| 2883 |
|
| 2884 |
return 0; |
| 2885 |
} |
| 2886 |
/* Reproducer for complex proxy jump |
| 2887 |
*/ |
| 2888 |
static void torture_config_jump(void **state) |
| 2889 |
{ |
| 2890 |
ssh_session session = *state; |
| 2891 |
struct ssh_jump_callbacks_struct c = { |
| 2892 |
.before_connection = before_connection, |
| 2893 |
.verify_knownhost = verify_knownhost, |
| 2894 |
.authenticate = authenticate, |
| 2895 |
}; |
| 2896 |
char *v = NULL; |
| 2897 |
int ret; |
| 2898 |
|
| 2899 |
ssh_options_set(session, SSH_OPTIONS_HOST, "cisco-router"); |
| 2900 |
|
| 2901 |
_parse_config(session, LIBSSH_TESTCONFIG_JUMP, NULL, SSH_OK); |
| 2902 |
|
| 2903 |
/* Test the variable presence */ |
| 2904 |
ret = ssh_options_get(session, SSH_OPTIONS_HOST, &v); |
| 2905 |
assert_return_code(ret, errno); |
| 2906 |
assert_string_equal(v, "xx.xxxxxxxxx"); |
| 2907 |
ssh_string_free_char(v); |
| 2908 |
|
| 2909 |
ret = ssh_options_get(session, SSH_OPTIONS_USER, &v); |
| 2910 |
assert_return_code(ret, errno); |
| 2911 |
assert_string_equal(v, "username"); |
| 2912 |
ssh_string_free_char(v); |
| 2913 |
|
| 2914 |
assert_int_equal(session->opts.port, 5555); |
| 2915 |
|
| 2916 |
/* At this point, the configuration file is not parsed for the jump host so |
| 2917 |
* we are getting just the the hostname -- the port and username will get |
| 2918 |
* pulled during the session connecting to this host */ |
| 2919 |
assert_int_equal(ssh_list_count(session->opts.proxy_jumps), 1); |
| 2920 |
helper_proxy_jump_check(session->opts.proxy_jumps->root, |
| 2921 |
"ub-jumphost", |
| 2922 |
NULL, |
| 2923 |
NULL); |
| 2924 |
|
| 2925 |
/* Set up the callbacks -- they should verify we are going to connect to the |
| 2926 |
* right host */ |
| 2927 |
ret = ssh_options_set(session, SSH_OPTIONS_PROXYJUMP_CB_LIST_APPEND, &c); |
| 2928 |
assert_ssh_return_code(session, ret); |
| 2929 |
|
| 2930 |
ret = ssh_connect(session); |
| 2931 |
assert_ssh_return_code_equal(session, ret, SSH_ERROR); |
| 2932 |
|
| 2933 |
printf("%s: EOF\n", __func__); |
| 2934 |
} |
| 2935 |
|
| 2936 |
/* Invalid configuration files |
| 2937 |
*/ |
| 2938 |
static void torture_config_invalid(void **state) |
| 2939 |
{ |
| 2940 |
ssh_session session = *state; |
| 2941 |
|
| 2942 |
ssh_options_set(session, SSH_OPTIONS_HOST, "Bar"); |
| 2943 |
|
| 2944 |
/* non-regular file -- ignored (or missing on non-unix) so OK */ |
| 2945 |
_parse_config(session, "/dev/random", NULL, SSH_OK); |
| 2946 |
|
| 2947 |
#ifndef _WIN32 |
| 2948 |
/* huge file -- ignored (or missing on non-unix) so OK */ |
| 2949 |
_parse_config(session, "/proc/kcore", NULL, SSH_OK); |
| 2950 |
#endif |
| 2951 |
} |
| 2952 |
|
| 2953 |
int torture_run_tests(void) |
| 2954 |
{ |
| 2955 |
int rc; |
| 2956 |
struct CMUnitTest tests[] = { |
| 2957 |
cmocka_unit_test_setup_teardown(torture_config_include_file, |
| 2958 |
setup, |
| 2959 |
teardown), |
| 2960 |
cmocka_unit_test_setup_teardown(torture_config_include_string, |
| 2961 |
setup, |
| 2962 |
teardown), |
| 2963 |
cmocka_unit_test_setup_teardown(torture_config_include_recursive_file, |
| 2964 |
setup, |
| 2965 |
teardown), |
| 2966 |
cmocka_unit_test_setup_teardown(torture_config_include_recursive_string, |
| 2967 |
setup, |
| 2968 |
teardown), |
| 2969 |
cmocka_unit_test_setup_teardown(torture_config_double_ports_file, |
| 2970 |
setup, |
| 2971 |
teardown), |
| 2972 |
cmocka_unit_test_setup_teardown(torture_config_double_ports_string, |
| 2973 |
setup, |
| 2974 |
teardown), |
| 2975 |
cmocka_unit_test_setup_teardown(torture_config_glob_file, |
| 2976 |
setup, |
| 2977 |
teardown), |
| 2978 |
cmocka_unit_test_setup_teardown(torture_config_glob_string, |
| 2979 |
setup, |
| 2980 |
teardown), |
| 2981 |
cmocka_unit_test_setup_teardown(torture_config_new_file, |
| 2982 |
setup, |
| 2983 |
teardown), |
| 2984 |
cmocka_unit_test_setup_teardown(torture_config_new_string, |
| 2985 |
setup, |
| 2986 |
teardown), |
| 2987 |
cmocka_unit_test_setup_teardown(torture_config_auth_methods_file, |
| 2988 |
setup, |
| 2989 |
teardown), |
| 2990 |
cmocka_unit_test_setup_teardown(torture_config_auth_methods_string, |
| 2991 |
setup, |
| 2992 |
teardown), |
| 2993 |
cmocka_unit_test_setup_teardown(torture_config_unknown_file, |
| 2994 |
setup, |
| 2995 |
teardown), |
| 2996 |
cmocka_unit_test_setup_teardown(torture_config_unknown_string, |
| 2997 |
setup, |
| 2998 |
teardown), |
| 2999 |
cmocka_unit_test_setup_teardown(torture_config_match_file, |
| 3000 |
setup, |
| 3001 |
teardown), |
| 3002 |
cmocka_unit_test_setup_teardown(torture_config_match_string, |
| 3003 |
setup, |
| 3004 |
teardown), |
| 3005 |
cmocka_unit_test_setup_teardown(torture_config_proxyjump_file, |
| 3006 |
setup, |
| 3007 |
teardown), |
| 3008 |
cmocka_unit_test_setup_teardown(torture_config_proxyjump_string, |
| 3009 |
setup, |
| 3010 |
teardown), |
| 3011 |
cmocka_unit_test_setup_teardown(torture_config_control_path_file, |
| 3012 |
setup, |
| 3013 |
teardown), |
| 3014 |
cmocka_unit_test_setup_teardown(torture_config_control_path_string, |
| 3015 |
setup, |
| 3016 |
teardown), |
| 3017 |
cmocka_unit_test_setup_teardown(torture_config_control_master_file, |
| 3018 |
setup, |
| 3019 |
teardown), |
| 3020 |
cmocka_unit_test_setup_teardown(torture_config_control_master_string, |
| 3021 |
setup, |
| 3022 |
teardown), |
| 3023 |
cmocka_unit_test_setup_teardown(torture_config_address_family_file, |
| 3024 |
setup, |
| 3025 |
teardown), |
| 3026 |
cmocka_unit_test_setup_teardown(torture_config_address_family_string, |
| 3027 |
setup, |
| 3028 |
teardown), |
| 3029 |
cmocka_unit_test_setup_teardown(torture_config_rekey_file, |
| 3030 |
setup, |
| 3031 |
teardown), |
| 3032 |
cmocka_unit_test_setup_teardown(torture_config_rekey_string, |
| 3033 |
setup, |
| 3034 |
teardown), |
| 3035 |
cmocka_unit_test_setup_teardown(torture_config_plus_file, |
| 3036 |
setup, |
| 3037 |
teardown), |
| 3038 |
cmocka_unit_test_setup_teardown(torture_config_plus_string, |
| 3039 |
setup, |
| 3040 |
teardown), |
| 3041 |
cmocka_unit_test_setup_teardown(torture_config_minus_file, |
| 3042 |
setup, |
| 3043 |
teardown), |
| 3044 |
cmocka_unit_test_setup_teardown(torture_config_minus_string, |
| 3045 |
setup, |
| 3046 |
teardown), |
| 3047 |
cmocka_unit_test_setup_teardown(torture_config_caret_file, |
| 3048 |
setup, |
| 3049 |
teardown), |
| 3050 |
cmocka_unit_test_setup_teardown(torture_config_caret_string, |
| 3051 |
setup, |
| 3052 |
teardown), |
| 3053 |
cmocka_unit_test_setup_teardown(torture_config_pubkeytypes_file, |
| 3054 |
setup, |
| 3055 |
teardown), |
| 3056 |
cmocka_unit_test_setup_teardown(torture_config_pubkeytypes_string, |
| 3057 |
setup, |
| 3058 |
teardown), |
| 3059 |
cmocka_unit_test_setup_teardown(torture_config_pubkeyalgorithms_file, |
| 3060 |
setup, |
| 3061 |
teardown), |
| 3062 |
cmocka_unit_test_setup_teardown(torture_config_pubkeyalgorithms_string, |
| 3063 |
setup, |
| 3064 |
teardown), |
| 3065 |
cmocka_unit_test_setup_teardown(torture_config_nonewlineend_file, |
| 3066 |
setup, |
| 3067 |
teardown), |
| 3068 |
cmocka_unit_test_setup_teardown(torture_config_nonewlineend_string, |
| 3069 |
setup, |
| 3070 |
teardown), |
| 3071 |
cmocka_unit_test_setup_teardown(torture_config_nonewlineoneline_file, |
| 3072 |
setup, |
| 3073 |
teardown), |
| 3074 |
cmocka_unit_test_setup_teardown(torture_config_nonewlineoneline_string, |
| 3075 |
setup, |
| 3076 |
teardown), |
| 3077 |
cmocka_unit_test_setup_teardown(torture_config_parser_get_cmd, |
| 3078 |
setup, |
| 3079 |
teardown), |
| 3080 |
cmocka_unit_test_setup_teardown(torture_config_parser_get_token, |
| 3081 |
setup, |
| 3082 |
teardown), |
| 3083 |
cmocka_unit_test_setup_teardown(torture_config_match_pattern, |
| 3084 |
setup, |
| 3085 |
teardown), |
| 3086 |
cmocka_unit_test_setup_teardown(torture_config_identity, |
| 3087 |
setup, |
| 3088 |
teardown), |
| 3089 |
cmocka_unit_test_setup_teardown(torture_config_make_absolute, |
| 3090 |
setup, |
| 3091 |
teardown), |
| 3092 |
cmocka_unit_test_setup_teardown(torture_config_make_absolute_no_sshdir, |
| 3093 |
setup_no_sshdir, |
| 3094 |
teardown), |
| 3095 |
cmocka_unit_test_setup_teardown(torture_config_parse_uri, |
| 3096 |
setup, |
| 3097 |
teardown), |
| 3098 |
cmocka_unit_test_setup_teardown(torture_config_match_complex, |
| 3099 |
setup, |
| 3100 |
teardown), |
| 3101 |
cmocka_unit_test_setup_teardown(torture_config_loglevel_missing_value, |
| 3102 |
setup, |
| 3103 |
teardown), |
| 3104 |
cmocka_unit_test_setup_teardown(torture_config_jump, |
| 3105 |
setup, |
| 3106 |
teardown), |
| 3107 |
cmocka_unit_test_setup_teardown(torture_config_invalid, |
| 3108 |
setup, |
| 3109 |
teardown), |
| 3110 |
}; |
| 3111 |
|
| 3112 |
ssh_init(); |
| 3113 |
torture_filter_tests(tests); |
| 3114 |
rc = cmocka_run_group_tests(tests, |
| 3115 |
setup_config_files, teardown_config_files); |
| 3116 |
ssh_finalize(); |
| 3117 |
return rc; |
| 3118 |
} |