Debian Codegraph @

tests/client/torture_client_config.c

raw
1 #include "config.h"
2
3 #define LIBSSH_STATIC
4
5 #include <pwd.h>
6 #include <errno.h>
7 #include "torture.h"
8 #include "libssh/session.h"
9 #include "libssh/options.h"
10 #include "libssh/misc.h"
11
12 #define LIBSSH_SSH_CONFIG "libssh_config"
13
14 #define TORTURE_CONFIG_USER "test-user"
15
16 #define CIPHERS "[email protected],[email protected]"
17 #define CIPHERS2 "aes256-cbc,aes128-ctr"
18
19 static int sshd_setup(void **state)
20 {
21 torture_setup_sshd_server(state, false);
22
23 return 0;
24 }
25
26 static int sshd_teardown(void **state) {
27 torture_teardown_sshd_server(state);
28
29 return 0;
30 }
31
32 static int setup_config_files(void **state)
33 {
34 struct torture_state *s = *state;
35 int verbosity;
36 struct passwd *pwd;
37 char *filename = NULL;
38 int rc;
39
40 /* Work under the bob's UID to be able to load his configuration file */
41 pwd = getpwnam("bob");
42 assert_non_null(pwd);
43
44 rc = setuid(pwd->pw_uid);
45 assert_return_code(rc, errno);
46
47 s->ssh.session = ssh_new();
48 assert_non_null(s->ssh.session);
49
50 filename = ssh_path_expand_tilde("~/.ssh/config");
51 torture_write_file(filename, "Ciphers "CIPHERS"\nTestBogus1\nUser "TORTURE_CONFIG_USER);
52 free(filename);
53
54 torture_write_file(LIBSSH_SSH_CONFIG, "Ciphers "CIPHERS2"\nTestBogus2\n");
55
56 verbosity = torture_libssh_verbosity();
57 ssh_options_set(s->ssh.session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
58 ssh_options_set(s->ssh.session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER);
59
60 return 0;
61 }
62
63 static int setup_session(void **state)
64 {
65 struct torture_state *s = *state;
66 int verbosity;
67
68 s->ssh.session = ssh_new();
69 assert_non_null(s->ssh.session);
70
71 verbosity = torture_libssh_verbosity();
72 ssh_options_set(s->ssh.session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
73 ssh_options_set(s->ssh.session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER);
74
75 setenv("NSS_WRAPPER_HOSTNAME", "client.libssh.site", 1);
76
77 return 0;
78 }
79
80 static int teardown(void **state)
81 {
82 struct torture_state *s = *state;
83 char *filename;
84
85 filename = ssh_path_expand_tilde("~/.ssh/config");
86 if (filename != NULL) {
87 if (strlen(filename) > 0) {
88 unlink(filename);
89 }
90 SAFE_FREE(filename);
91 }
92
93 unlink(LIBSSH_SSH_CONFIG);
94
95 ssh_disconnect(s->ssh.session);
96 ssh_free(s->ssh.session);
97
98 return 0;
99 }
100
101 static int teardown_session(void **state)
102 {
103 struct torture_state *s = *state;
104
105 ssh_disconnect(s->ssh.session);
106 ssh_free(s->ssh.session);
107
108 return 0;
109 }
110
111 /* This tests makes sure that parsing both system-wide and per-user
112 * configuration files retains OpenSSH semantics (the per-user overrides
113 * the system-wide values).
114 * This function ssh_options_parse_config() has hardcoded path to the
115 * system-wide configuration file so this might not test anything at all
116 * if this system-wide file does not overwrite this option.
117 */
118 static void torture_client_config_system(void **state)
119 {
120 struct torture_state *s = *state;
121 int ret = 0;
122
123 char *fips_ciphers = NULL;
124
125 if (ssh_fips_mode()) {
126 fips_ciphers = ssh_keep_fips_algos(SSH_CRYPT_C_S, CIPHERS);
127 assert_non_null(fips_ciphers);
128 }
129
130 /* The first tests assumes there is system-wide configuration file
131 * setting Ciphers to some non-default value. We do not have any control
132 * of that in this test case.
133 */
134 ret = ssh_options_parse_config(s->ssh.session, NULL);
135 assert_ssh_return_code(s->ssh.session, ret);
136
137 assert_non_null(s->ssh.session->opts.wanted_methods[SSH_CRYPT_C_S]);
138 assert_non_null(s->ssh.session->opts.wanted_methods[SSH_CRYPT_S_C]);
139 if (ssh_fips_mode()) {
140 assert_string_equal(s->ssh.session->opts.wanted_methods[SSH_CRYPT_C_S],
141 fips_ciphers);
142 assert_string_equal(s->ssh.session->opts.wanted_methods[SSH_CRYPT_S_C],
143 fips_ciphers);
144 } else {
145 assert_string_equal(s->ssh.session->opts.wanted_methods[SSH_CRYPT_C_S],
146 CIPHERS);
147 assert_string_equal(s->ssh.session->opts.wanted_methods[SSH_CRYPT_S_C],
148 CIPHERS);
149 }
150
151 /* Make sure the configuration was processed and user modified */
152 assert_string_equal(s->ssh.session->opts.username, TORTURE_CONFIG_USER);
153
154 SAFE_FREE(fips_ciphers);
155 }
156
157 /* This tests makes sure that parsing both system-wide and per-user
158 * configuration files retains OpenSSH semantics (the per-user overrides
159 * the system-wide values).
160 * The function ssh_options_parse_config() has hardcoded path to the
161 * system-wide configuration file so we try to emulate the behavior by parsing
162 * the files separately in the same order.
163 */
164 static void torture_client_config_emulate(void **state)
165 {
166 struct torture_state *s = *state;
167 char *filename = NULL;
168 int ret = 0;
169
170 char *fips_ciphers = NULL;
171
172 if (ssh_fips_mode()) {
173 fips_ciphers = ssh_keep_fips_algos(SSH_CRYPT_C_S, CIPHERS);
174 assert_non_null(fips_ciphers);
175 }
176
177 /* The first tests assumes there is system-wide configuration file
178 * setting Ciphers to some non-default value. We do not have any control
179 * of that in this test case
180 */
181 filename = ssh_path_expand_tilde("~/.ssh/config");
182 ret = ssh_options_parse_config(s->ssh.session, filename);
183 free(filename);
184 assert_ssh_return_code(s->ssh.session, ret);
185
186 ret = ssh_options_parse_config(s->ssh.session, LIBSSH_SSH_CONFIG);
187 assert_ssh_return_code(s->ssh.session, ret);
188
189 assert_non_null(s->ssh.session->opts.wanted_methods[SSH_CRYPT_C_S]);
190 assert_non_null(s->ssh.session->opts.wanted_methods[SSH_CRYPT_S_C]);
191 if (ssh_fips_mode()) {
192 assert_string_equal(s->ssh.session->opts.wanted_methods[SSH_CRYPT_C_S],
193 fips_ciphers);
194 assert_string_equal(s->ssh.session->opts.wanted_methods[SSH_CRYPT_S_C],
195 fips_ciphers);
196 } else {
197 assert_string_equal(s->ssh.session->opts.wanted_methods[SSH_CRYPT_C_S],
198 CIPHERS);
199 assert_string_equal(s->ssh.session->opts.wanted_methods[SSH_CRYPT_S_C],
200 CIPHERS);
201 }
202 /* Make sure the configuration was processed and user modified */
203 assert_string_equal(s->ssh.session->opts.username, TORTURE_CONFIG_USER);
204
205 SAFE_FREE(fips_ciphers);
206 }
207
208 /* This verifies that configuration files are parsed by default.
209 */
210 static void torture_client_config_autoparse(void **state)
211 {
212 struct torture_state *s = *state;
213 int ret = 0;
214
215 ret = ssh_connect(s->ssh.session);
216 assert_ssh_return_code(s->ssh.session, ret);
217
218 /* Make sure the configuration was processed and user modified */
219 assert_string_equal(s->ssh.session->opts.username, TORTURE_CONFIG_USER);
220 }
221
222 /* This verifies that we are able to suppress parsing of the configuration files
223 * on connect using an option.
224 */
225 static void torture_client_config_suppress(void **state)
226 {
227 struct torture_state *s = *state;
228 bool b = false;
229 int ret = 0;
230
231 ret = ssh_options_set(s->ssh.session, SSH_OPTIONS_PROCESS_CONFIG, &b);
232 assert_ssh_return_code(s->ssh.session, ret);
233
234 ret = ssh_connect(s->ssh.session);
235 assert_ssh_return_code(s->ssh.session, ret);
236
237 /* Make sure the configuration was not processed and user modified */
238 assert_string_equal(s->ssh.session->opts.username, "bob");
239 }
240
241 static void torture_client_config_expand_bad(void **state)
242 {
243 ssh_session session = ssh_new();
244 int ret = 0;
245
246 (void)state;
247
248 assert_non_null(session);
249
250 /* The hash without host fails, but does not crash */
251 ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, "%C");
252
253 ret = ssh_options_apply(session);
254 assert_ssh_return_code_equal(session, ret, SSH_ERROR);
255
256 /* The hash without host fails, but does not crash */
257 ssh_options_set(session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER);
258 ssh_options_set(session, SSH_OPTIONS_KNOWNHOSTS, "%C");
259
260 ret = ssh_options_apply(session);
261 assert_ssh_return_code_equal(session, ret, SSH_OK);
262
263 ssh_free(session);
264 }
265
266 static void torture_client_config_expand(void **state)
267 {
268 struct torture_state *s = *state;
269 int ret = 0;
270
271 /* TEST: user home directory */
272 ssh_options_set(s->ssh.session, SSH_OPTIONS_KNOWNHOSTS, "%d");
273
274 ret = ssh_options_apply(s->ssh.session);
275 assert_ssh_return_code(s->ssh.session, ret);
276
277 assert_string_equal(s->ssh.session->opts.knownhosts,
278 BINARYDIR "/tests/home");
279
280 /* Reset the flag so we can repeat the test */
281 s->ssh.session->opts.exp_flags &= ~SSH_OPT_EXP_FLAG_KNOWNHOSTS;
282
283
284 /* TEST: target host name */
285 ssh_options_set(s->ssh.session, SSH_OPTIONS_KNOWNHOSTS, "%h");
286
287 ret = ssh_options_apply(s->ssh.session);
288 assert_ssh_return_code(s->ssh.session, ret);
289
290 assert_string_equal(s->ssh.session->opts.knownhosts, TORTURE_SSH_SERVER);
291
292 /* Reset the flag so we can repeat the test */
293 s->ssh.session->opts.exp_flags &= ~SSH_OPT_EXP_FLAG_KNOWNHOSTS;
294
295
296 /* TEST: local username */
297 ssh_options_set(s->ssh.session, SSH_OPTIONS_KNOWNHOSTS, "%u");
298
299 ret = ssh_options_apply(s->ssh.session);
300 assert_ssh_return_code(s->ssh.session, ret);
301
302 assert_string_equal(s->ssh.session->opts.knownhosts, "root");
303
304 /* Reset the flag so we can repeat the test */
305 s->ssh.session->opts.exp_flags &= ~SSH_OPT_EXP_FLAG_KNOWNHOSTS;
306
307
308 /* TEST: local hostname */
309 ssh_options_set(s->ssh.session, SSH_OPTIONS_KNOWNHOSTS, "%l");
310
311 ret = ssh_options_apply(s->ssh.session);
312 assert_ssh_return_code(s->ssh.session, ret);
313
314 assert_string_equal(s->ssh.session->opts.knownhosts, "client.libssh.site");
315
316 /* Reset the flag so we can repeat the test */
317 s->ssh.session->opts.exp_flags &= ~SSH_OPT_EXP_FLAG_KNOWNHOSTS;
318
319
320 /* TEST: remote username */
321 ssh_options_set(s->ssh.session, SSH_OPTIONS_USER, "alice");
322 ssh_options_set(s->ssh.session, SSH_OPTIONS_KNOWNHOSTS, "%r");
323
324 ret = ssh_options_apply(s->ssh.session);
325 assert_ssh_return_code(s->ssh.session, ret);
326
327 assert_string_equal(s->ssh.session->opts.knownhosts, "alice");
328
329 /* Reset the flag so we can repeat the test */
330 s->ssh.session->opts.exp_flags &= ~SSH_OPT_EXP_FLAG_KNOWNHOSTS;
331
332
333 /* TEST: remote port */
334 ssh_options_set(s->ssh.session, SSH_OPTIONS_PORT_STR, "2222");
335 ssh_options_set(s->ssh.session, SSH_OPTIONS_KNOWNHOSTS, "%p");
336
337 ret = ssh_options_apply(s->ssh.session);
338 assert_ssh_return_code(s->ssh.session, ret);
339
340 assert_string_equal(s->ssh.session->opts.knownhosts, "2222");
341
342 /* Reset the flag so we can repeat the test */
343 s->ssh.session->opts.exp_flags &= ~SSH_OPT_EXP_FLAG_KNOWNHOSTS;
344
345
346 /* TEST: empty proxyjump */
347 ssh_options_set(s->ssh.session, SSH_OPTIONS_KNOWNHOSTS, "%j");
348
349 ret = ssh_options_apply(s->ssh.session);
350 assert_ssh_return_code(s->ssh.session, ret);
351
352 /* No proxyjump string should not explode */
353 assert_string_equal(s->ssh.session->opts.knownhosts, "");
354
355 /* Reset the flag so we can repeat the test */
356 s->ssh.session->opts.exp_flags &= ~SSH_OPT_EXP_FLAG_KNOWNHOSTS;
357
358
359 /* TEST: proxyjump string present */
360 ssh_options_set(s->ssh.session, SSH_OPTIONS_KNOWNHOSTS, "%j");
361 ssh_options_set(s->ssh.session,
362 SSH_OPTIONS_PROXYJUMP,
363 "user@" TORTURE_SSH_SERVER ":22");
364
365 ret = ssh_options_apply(s->ssh.session);
366 assert_ssh_return_code(s->ssh.session, ret);
367
368 assert_string_equal(s->ssh.session->opts.knownhosts,
369 "user@" TORTURE_SSH_SERVER ":22");
370
371 /* Reset the flag so we can repeat the test */
372 s->ssh.session->opts.exp_flags &= ~SSH_OPT_EXP_FLAG_KNOWNHOSTS;
373
374
375 /* TEST: separate list %l-%h-%p-%r-%j with empty ProxyJump */
376 ssh_options_set(s->ssh.session, SSH_OPTIONS_KNOWNHOSTS, "%l-%h-%p-%r-%j");
377 ssh_options_set(s->ssh.session, SSH_OPTIONS_PROXYJUMP, "none");
378 ssh_options_set(s->ssh.session, SSH_OPTIONS_PORT_STR, "22");
379
380 ret = ssh_options_apply(s->ssh.session);
381 assert_ssh_return_code(s->ssh.session, ret);
382
383 // Tested by
384 // ret = system(SSH_EXECUTABLE
385 // " -p 22 -o UserKnownHostsFile=/dev/null"
386 // " -o KnownHostsCommand='/bin/touch \"/tmp/%l-%h-%p-%r-%j\"'"
387 // " alice@" TORTURE_SSH_SERVER);
388 // assert_return_code(ret, errno);
389 assert_string_equal(s->ssh.session->opts.knownhosts,
390 "client.libssh.site-127.0.0.10-22-alice-");
391
392
393 /* TEST: hash of %l%h%p%r%j with empty ProxyJump */
394 ssh_options_set(s->ssh.session, SSH_OPTIONS_KNOWNHOSTS, "%C");
395
396 ret = ssh_options_apply(s->ssh.session);
397 assert_ssh_return_code(s->ssh.session, ret);
398
399 // Tested by
400 // ret = system(SSH_EXECUTABLE
401 // " -p 22 -o UserKnownHostsFile=/dev/null"
402 // " -o KnownHostsCommand='/bin/touch \"/tmp/%C\"'"
403 // " alice@" TORTURE_SSH_SERVER);
404 // assert_return_code(ret, errno);
405 assert_string_equal(s->ssh.session->opts.knownhosts,
406 "133e3957ff9d01fdcf1f6c7f83325a8ce49bf850");
407
408 /* Reset the flag so we can repeat the test */
409 s->ssh.session->opts.exp_flags &= ~SSH_OPT_EXP_FLAG_KNOWNHOSTS;
410
411
412 /* TEST: separate list %l-%h-%p-%r-%j */
413 ssh_options_set(s->ssh.session, SSH_OPTIONS_KNOWNHOSTS, "%l-%h-%p-%r-%j");
414 ssh_options_set(s->ssh.session,
415 SSH_OPTIONS_PROXYJUMP,
416 "user@" TORTURE_SSH_SERVER ":22");
417
418 ret = ssh_options_apply(s->ssh.session);
419 assert_ssh_return_code(s->ssh.session, ret);
420
421 // Tested by
422 // ret = system(SSH_EXECUTABLE
423 // " -p 22 -oProxyJump=user@" TORTURE_SSH_SERVER ":22"
424 // " -o UserKnownHostsFile=/dev/null"
425 // " -o KnownHostsCommand='/bin/touch \"/tmp/%l-%h-%p-%r-%j\"'"
426 // " alice@" TORTURE_SSH_SERVER);
427 // assert_return_code(ret, errno);
428 assert_string_equal(s->ssh.session->opts.knownhosts,
429 "client.libssh.site-127.0.0.10-22-alice-user@"
430 TORTURE_SSH_SERVER ":22");
431
432
433 /* Reset the flag so we can repeat the test */
434 s->ssh.session->opts.exp_flags &= ~SSH_OPT_EXP_FLAG_KNOWNHOSTS;
435
436
437 /* TEST: hash of %l%h%p%r%j */
438 ssh_options_set(s->ssh.session, SSH_OPTIONS_KNOWNHOSTS, "%C");
439
440 ret = ssh_options_apply(s->ssh.session);
441 assert_ssh_return_code(s->ssh.session, ret);
442
443 // Tested by
444 // ret = system(SSH_EXECUTABLE
445 // " -p 22 -oProxyJump=user@" TORTURE_SSH_SERVER ":22"
446 // " -o UserKnownHostsFile=/dev/null"
447 // " -o KnownHostsCommand='/bin/touch \"/tmp/%C\"'"
448 // " alice@" TORTURE_SSH_SERVER);
449 // assert_return_code(ret, errno);
450 assert_string_equal(s->ssh.session->opts.knownhosts,
451 "adf0b7c4e71a0fee85fd97506507ba8591f3663b");
452
453 /* Reset the flag so we can repeat the test */
454 s->ssh.session->opts.exp_flags &= ~SSH_OPT_EXP_FLAG_KNOWNHOSTS;
455
456 }
457
458
459 int torture_run_tests(void) {
460 int rc;
461 struct CMUnitTest tests[] = {
462 /* Keep these first -- following setup is changing user to bob, which we
463 * do not want */
464 cmocka_unit_test(torture_client_config_expand_bad),
465 cmocka_unit_test_setup_teardown(torture_client_config_expand,
466 setup_session,
467 teardown_session),
468 cmocka_unit_test_setup_teardown(torture_client_config_system,
469 setup_config_files,
470 teardown),
471 cmocka_unit_test_setup_teardown(torture_client_config_emulate,
472 setup_config_files,
473 teardown),
474 cmocka_unit_test_setup_teardown(torture_client_config_autoparse,
475 setup_config_files,
476 teardown),
477 cmocka_unit_test_setup_teardown(torture_client_config_suppress,
478 setup_config_files,
479 teardown),
480 };
481
482 ssh_init();
483 torture_filter_tests(tests);
484 rc = cmocka_run_group_tests(tests, sshd_setup, sshd_teardown);
485 ssh_finalize();
486 return rc;
487 }