roman | 60c4ddd | 2023-12-21 11:02:37 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file test_authkeys.c |
| 3 | * @author Roman Janota <xjanot04@fit.vutbr.cz> |
| 4 | * @brief libnetconf2 SSH authentication using mocked system authorized_keys |
| 5 | * |
| 6 | * @copyright |
| 7 | * Copyright (c) 2023 CESNET, z.s.p.o. |
| 8 | * |
| 9 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 10 | * You may not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * https://opensource.org/licenses/BSD-3-Clause |
| 14 | */ |
| 15 | |
| 16 | #define _GNU_SOURCE |
| 17 | |
| 18 | #include <errno.h> |
| 19 | #include <pthread.h> |
| 20 | #include <setjmp.h> |
| 21 | #include <stdarg.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | #include <cmocka.h> |
| 27 | |
roman | abe6990 | 2024-08-13 14:43:49 +0200 | [diff] [blame] | 28 | #include "ln2_test.h" |
roman | 60c4ddd | 2023-12-21 11:02:37 +0100 | [diff] [blame] | 29 | #include "tests/config.h" |
| 30 | |
| 31 | #define NC_ACCEPT_TIMEOUT 2000 |
| 32 | #define NC_PS_POLL_TIMEOUT 2000 |
| 33 | |
| 34 | struct ly_ctx *ctx; |
| 35 | |
| 36 | struct test_state { |
| 37 | pthread_barrier_t barrier; |
| 38 | const char *pubkey_path; |
| 39 | const char *privkey_path; |
| 40 | int expect_ok; |
| 41 | }; |
| 42 | |
roman | abe6990 | 2024-08-13 14:43:49 +0200 | [diff] [blame] | 43 | int TEST_PORT = 10050; |
| 44 | const char *TEST_PORT_STR = "10050"; |
| 45 | |
roman | 60c4ddd | 2023-12-21 11:02:37 +0100 | [diff] [blame] | 46 | static void * |
| 47 | server_thread(void *arg) |
| 48 | { |
| 49 | int ret; |
| 50 | NC_MSG_TYPE msgtype; |
| 51 | struct nc_session *session; |
| 52 | struct nc_pollsession *ps; |
| 53 | struct test_state *state = arg; |
| 54 | |
| 55 | ps = nc_ps_new(); |
| 56 | assert_non_null(ps); |
| 57 | |
| 58 | /* accept a session and add it to the poll session structure */ |
| 59 | pthread_barrier_wait(&state->barrier); |
| 60 | msgtype = nc_accept(NC_ACCEPT_TIMEOUT, ctx, &session); |
| 61 | |
| 62 | /* only continue if we expect to authenticate successfully */ |
| 63 | if (state->expect_ok) { |
| 64 | assert_int_equal(msgtype, NC_MSG_HELLO); |
| 65 | } else { |
| 66 | assert_int_equal(msgtype, NC_MSG_ERROR); |
| 67 | nc_ps_free(ps); |
| 68 | return NULL; |
| 69 | } |
| 70 | |
| 71 | ret = nc_ps_add_session(ps, session); |
| 72 | assert_int_equal(ret, 0); |
| 73 | |
| 74 | do { |
| 75 | ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT, NULL); |
| 76 | assert_int_equal(ret & NC_PSPOLL_RPC, NC_PSPOLL_RPC); |
| 77 | } while (!(ret & NC_PSPOLL_SESSION_TERM)); |
| 78 | |
| 79 | nc_ps_clear(ps, 1, NULL); |
| 80 | nc_ps_free(ps); |
| 81 | return NULL; |
| 82 | } |
| 83 | |
| 84 | static void * |
| 85 | client_thread(void *arg) |
| 86 | { |
| 87 | int ret; |
| 88 | struct nc_session *session = NULL; |
| 89 | struct test_state *state = arg; |
| 90 | |
| 91 | /* skip all hostkey and known_hosts checks */ |
| 92 | nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP); |
| 93 | |
| 94 | /* set directory where to search for modules */ |
| 95 | ret = nc_client_set_schema_searchpath(MODULES_DIR); |
| 96 | assert_int_equal(ret, 0); |
| 97 | |
| 98 | /* add client's key pair */ |
| 99 | ret = nc_client_ssh_add_keypair(state->pubkey_path, state->privkey_path); |
| 100 | assert_int_equal(ret, 0); |
| 101 | |
| 102 | /* set ssh username */ |
| 103 | ret = nc_client_ssh_set_username("test"); |
| 104 | assert_int_equal(ret, 0); |
| 105 | |
| 106 | pthread_barrier_wait(&state->barrier); |
| 107 | /* connect */ |
Jan Kundrát | f8d9e8d | 2024-04-08 12:13:02 +0200 | [diff] [blame] | 108 | session = nc_connect_ssh("127.0.0.1", TEST_PORT, NULL); |
roman | 60c4ddd | 2023-12-21 11:02:37 +0100 | [diff] [blame] | 109 | if (state->expect_ok) { |
| 110 | assert_non_null(session); |
| 111 | } else { |
| 112 | assert_null(session); |
| 113 | } |
| 114 | |
| 115 | nc_session_free(session, NULL); |
| 116 | return NULL; |
| 117 | } |
| 118 | |
| 119 | static void |
| 120 | test_nc_authkey_ok(void **arg) |
| 121 | { |
| 122 | int ret, i; |
| 123 | pthread_t tids[2]; |
| 124 | struct test_state *state; |
| 125 | |
| 126 | assert_non_null(arg); |
| 127 | |
| 128 | state = *(struct test_state **)arg; |
| 129 | |
| 130 | /* set the path to the test's authorized_keys file */ |
| 131 | ret = nc_server_ssh_set_authkey_path_format(TESTS_DIR "/data/authorized_keys"); |
| 132 | assert_int_equal(ret, 0); |
| 133 | |
| 134 | /* set pubkey and privkey path, the pubkey matches the one in authorized keys */ |
| 135 | state->pubkey_path = TESTS_DIR "/data/id_ed25519.pub"; |
| 136 | state->privkey_path = TESTS_DIR "/data/id_ed25519"; |
| 137 | |
| 138 | /* expect ok result */ |
| 139 | state->expect_ok = 1; |
| 140 | |
| 141 | /* client */ |
| 142 | ret = pthread_create(&tids[0], NULL, client_thread, state); |
| 143 | assert_int_equal(ret, 0); |
| 144 | |
| 145 | /* server */ |
| 146 | ret = pthread_create(&tids[1], NULL, server_thread, state); |
| 147 | assert_int_equal(ret, 0); |
| 148 | |
| 149 | for (i = 0; i < 2; i++) { |
| 150 | pthread_join(tids[i], NULL); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | static void |
| 155 | test_nc_authkey_bad_key(void **arg) |
| 156 | { |
| 157 | int ret, i; |
| 158 | pthread_t tids[2]; |
| 159 | struct test_state *state; |
| 160 | |
| 161 | assert_non_null(arg); |
| 162 | |
| 163 | state = *(struct test_state **)arg; |
| 164 | |
| 165 | /* set the path to the test's authorized_keys file */ |
| 166 | ret = nc_server_ssh_set_authkey_path_format(TESTS_DIR "/data/authorized_keys"); |
| 167 | assert_int_equal(ret, 0); |
| 168 | |
| 169 | /* set pubkey and privkey path, the pubkey doesn't match the one in authorized keys */ |
| 170 | state->pubkey_path = TESTS_DIR "/data/id_ecdsa521.pub"; |
| 171 | state->privkey_path = TESTS_DIR "/data/id_ecdsa521"; |
| 172 | |
| 173 | /* expect fail */ |
| 174 | state->expect_ok = 0; |
| 175 | |
| 176 | /* client */ |
| 177 | ret = pthread_create(&tids[0], NULL, client_thread, state); |
| 178 | assert_int_equal(ret, 0); |
| 179 | |
| 180 | /* server */ |
| 181 | ret = pthread_create(&tids[1], NULL, server_thread, state); |
| 182 | assert_int_equal(ret, 0); |
| 183 | |
| 184 | for (i = 0; i < 2; i++) { |
| 185 | pthread_join(tids[i], NULL); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | static void |
| 190 | test_nc_authkey_bad_path(void **arg) |
| 191 | { |
| 192 | int ret, i; |
| 193 | pthread_t tids[2]; |
| 194 | struct test_state *state; |
| 195 | |
| 196 | assert_non_null(arg); |
| 197 | |
| 198 | state = *(struct test_state **)arg; |
| 199 | |
| 200 | /* set the path to the test's authorized_keys file */ |
| 201 | ret = nc_server_ssh_set_authkey_path_format(TESTS_DIR "/some/bad/path"); |
| 202 | assert_int_equal(ret, 0); |
| 203 | |
| 204 | /* set pubkey and privkey path, the pubkey doesn't match the one in authorized keys */ |
| 205 | state->pubkey_path = TESTS_DIR "/data/id_ed25519.pub"; |
| 206 | state->privkey_path = TESTS_DIR "/data/id_ed25519"; |
| 207 | |
| 208 | /* expect fail */ |
| 209 | state->expect_ok = 0; |
| 210 | |
| 211 | /* client */ |
| 212 | ret = pthread_create(&tids[0], NULL, client_thread, state); |
| 213 | assert_int_equal(ret, 0); |
| 214 | |
| 215 | /* server */ |
| 216 | ret = pthread_create(&tids[1], NULL, server_thread, state); |
| 217 | assert_int_equal(ret, 0); |
| 218 | |
| 219 | for (i = 0; i < 2; i++) { |
| 220 | pthread_join(tids[i], NULL); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | static int |
| 225 | setup_f(void **state) |
| 226 | { |
| 227 | int ret; |
| 228 | struct lyd_node *tree = NULL; |
| 229 | struct test_state *test_state; |
| 230 | |
| 231 | nc_verbosity(NC_VERB_VERBOSE); |
| 232 | |
| 233 | /* init barrier */ |
| 234 | test_state = malloc(sizeof *test_state); |
| 235 | assert_non_null(test_state); |
| 236 | |
| 237 | ret = pthread_barrier_init(&test_state->barrier, NULL, 2); |
| 238 | assert_int_equal(ret, 0); |
| 239 | |
| 240 | *state = test_state; |
| 241 | |
| 242 | /* create new context */ |
| 243 | ret = ly_ctx_new(MODULES_DIR, 0, &ctx); |
| 244 | assert_int_equal(ret, 0); |
| 245 | |
| 246 | /* load default modules into context */ |
| 247 | ret = nc_server_init_ctx(&ctx); |
| 248 | assert_int_equal(ret, 0); |
| 249 | |
| 250 | /* load ietf-netconf-server module and it's imports into context */ |
| 251 | ret = nc_server_config_load_modules(&ctx); |
| 252 | assert_int_equal(ret, 0); |
| 253 | |
| 254 | ret = nc_server_config_add_ssh_hostkey(ctx, "endpt", "hostkey", TESTS_DIR "/data/server.key", NULL, &tree); |
| 255 | assert_int_equal(ret, 0); |
| 256 | |
roman | d8973d1 | 2024-04-25 14:57:18 +0200 | [diff] [blame] | 257 | ret = nc_server_config_add_address_port(ctx, "endpt", NC_TI_SSH, "127.0.0.1", TEST_PORT, &tree); |
roman | 60c4ddd | 2023-12-21 11:02:37 +0100 | [diff] [blame] | 258 | assert_int_equal(ret, 0); |
| 259 | |
| 260 | ret = nc_server_config_add_ssh_user_authkey(ctx, "endpt", "test", &tree); |
| 261 | assert_int_equal(ret, 0); |
| 262 | |
| 263 | /* configure the server based on the data */ |
| 264 | ret = nc_server_config_setup_data(tree); |
| 265 | assert_int_equal(ret, 0); |
| 266 | |
| 267 | /* initialize server */ |
| 268 | ret = nc_server_init(); |
| 269 | assert_int_equal(ret, 0); |
| 270 | |
| 271 | /* initialize client */ |
| 272 | ret = nc_client_init(); |
| 273 | assert_int_equal(ret, 0); |
| 274 | |
| 275 | lyd_free_all(tree); |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | static int |
| 281 | teardown_f(void **state) |
| 282 | { |
| 283 | int ret = 0; |
| 284 | struct test_state *test_state; |
| 285 | |
| 286 | assert_non_null(state); |
| 287 | test_state = *state; |
| 288 | |
| 289 | ret = pthread_barrier_destroy(&test_state->barrier); |
| 290 | assert_int_equal(ret, 0); |
| 291 | |
| 292 | free(*state); |
| 293 | nc_client_destroy(); |
| 294 | nc_server_destroy(); |
| 295 | ly_ctx_destroy(ctx); |
| 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | int |
| 301 | main(void) |
| 302 | { |
| 303 | const struct CMUnitTest tests[] = { |
| 304 | cmocka_unit_test_setup_teardown(test_nc_authkey_ok, setup_f, teardown_f), |
| 305 | cmocka_unit_test_setup_teardown(test_nc_authkey_bad_key, setup_f, teardown_f), |
| 306 | cmocka_unit_test_setup_teardown(test_nc_authkey_bad_path, setup_f, teardown_f), |
| 307 | }; |
| 308 | |
roman | abe6990 | 2024-08-13 14:43:49 +0200 | [diff] [blame] | 309 | /* try to get ports from the environment, otherwise use the default */ |
| 310 | if (ln2_glob_test_get_ports(1, &TEST_PORT, &TEST_PORT_STR)) { |
| 311 | return 1; |
| 312 | } |
| 313 | |
roman | 60c4ddd | 2023-12-21 11:02:37 +0100 | [diff] [blame] | 314 | setenv("CMOCKA_TEST_ABORT", "1", 1); |
| 315 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 316 | } |