roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 1 | /** |
| 2 | * @file test_auth.c |
| 3 | * @author Roman Janota <xjanot04@fit.vutbr.cz> |
roman | 808f3f6 | 2023-11-23 16:01:04 +0100 | [diff] [blame] | 4 | * @brief libnetconf2 SSH authentication methods test |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 5 | * |
| 6 | * @copyright |
roman | 808f3f6 | 2023-11-23 16:01:04 +0100 | [diff] [blame] | 7 | * Copyright (c) 2023 CESNET, z.s.p.o. |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 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 | |
roman | 9b1379c | 2023-03-31 10:11:10 +0200 | [diff] [blame] | 16 | #define _GNU_SOURCE |
| 17 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 18 | #include <errno.h> |
| 19 | #include <pthread.h> |
| 20 | #include <setjmp.h> |
roman | 9b1379c | 2023-03-31 10:11:10 +0200 | [diff] [blame] | 21 | #include <stdarg.h> |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | #include <cmocka.h> |
| 27 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 28 | #include "tests/config.h" |
| 29 | |
roman | 83683fb | 2023-02-24 09:15:23 +0100 | [diff] [blame] | 30 | #define NC_ACCEPT_TIMEOUT 2000 |
| 31 | #define NC_PS_POLL_TIMEOUT 2000 |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 32 | |
| 33 | struct ly_ctx *ctx; |
| 34 | |
| 35 | struct test_state { |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 36 | pthread_barrier_t barrier; |
| 37 | }; |
| 38 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 39 | static void * |
| 40 | server_thread(void *arg) |
| 41 | { |
| 42 | int ret; |
| 43 | NC_MSG_TYPE msgtype; |
| 44 | struct nc_session *session; |
| 45 | struct nc_pollsession *ps; |
| 46 | struct test_state *state = arg; |
| 47 | |
| 48 | (void) arg; |
| 49 | |
| 50 | ps = nc_ps_new(); |
| 51 | assert_non_null(ps); |
| 52 | |
| 53 | /* accept a session and add it to the poll session structure */ |
| 54 | pthread_barrier_wait(&state->barrier); |
| 55 | msgtype = nc_accept(NC_ACCEPT_TIMEOUT, ctx, &session); |
| 56 | assert_int_equal(msgtype, NC_MSG_HELLO); |
| 57 | |
| 58 | ret = nc_ps_add_session(ps, session); |
| 59 | assert_int_equal(ret, 0); |
| 60 | |
| 61 | do { |
| 62 | ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT, NULL); |
| 63 | assert_int_equal(ret & NC_PSPOLL_RPC, NC_PSPOLL_RPC); |
| 64 | } while (!(ret & NC_PSPOLL_SESSION_TERM)); |
| 65 | |
| 66 | nc_ps_clear(ps, 1, NULL); |
| 67 | nc_ps_free(ps); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 68 | return NULL; |
| 69 | } |
| 70 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 71 | static char * |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 72 | auth_password(const char *username, const char *hostname, void *priv) |
| 73 | { |
| 74 | (void) hostname; |
| 75 | (void) priv; |
| 76 | |
roman | 9b1379c | 2023-03-31 10:11:10 +0200 | [diff] [blame] | 77 | /* set the reply to password authentication */ |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 78 | if (!strcmp(username, "test_pw")) { |
| 79 | return strdup("testpw"); |
| 80 | } else { |
| 81 | return NULL; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | static void * |
| 86 | client_thread_password(void *arg) |
| 87 | { |
| 88 | int ret; |
| 89 | struct nc_session *session = NULL; |
| 90 | struct test_state *state = arg; |
| 91 | |
roman | 472420c | 2023-04-24 16:28:09 +0200 | [diff] [blame] | 92 | /* skip all hostkey and known_hosts checks */ |
| 93 | nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP); |
| 94 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 95 | ret = nc_client_set_schema_searchpath(MODULES_DIR); |
| 96 | assert_int_equal(ret, 0); |
| 97 | |
| 98 | ret = nc_client_ssh_set_username("test_pw"); |
| 99 | assert_int_equal(ret, 0); |
| 100 | |
| 101 | nc_client_ssh_set_auth_password_clb(auth_password, NULL); |
| 102 | |
| 103 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PUBLICKEY, -1); |
| 104 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PASSWORD, 1); |
| 105 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_INTERACTIVE, -1); |
| 106 | |
| 107 | pthread_barrier_wait(&state->barrier); |
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 | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 109 | assert_non_null(session); |
| 110 | |
| 111 | nc_session_free(session, NULL); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 112 | return NULL; |
| 113 | } |
| 114 | |
| 115 | static void |
| 116 | test_nc_auth_password(void **state) |
| 117 | { |
| 118 | int ret, i; |
| 119 | pthread_t tids[2]; |
| 120 | |
| 121 | assert_non_null(state); |
| 122 | |
| 123 | ret = pthread_create(&tids[0], NULL, client_thread_password, *state); |
| 124 | assert_int_equal(ret, 0); |
| 125 | ret = pthread_create(&tids[1], NULL, server_thread, *state); |
| 126 | assert_int_equal(ret, 0); |
| 127 | |
| 128 | for (i = 0; i < 2; i++) { |
| 129 | pthread_join(tids[i], NULL); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | static void * |
| 134 | client_thread_pubkey(void *arg) |
| 135 | { |
| 136 | int ret; |
| 137 | struct nc_session *session = NULL; |
| 138 | struct test_state *state = arg; |
| 139 | |
roman | 472420c | 2023-04-24 16:28:09 +0200 | [diff] [blame] | 140 | /* skip all hostkey and known_hosts checks */ |
| 141 | nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP); |
| 142 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 143 | ret = nc_client_set_schema_searchpath(MODULES_DIR); |
| 144 | assert_int_equal(ret, 0); |
| 145 | |
| 146 | ret = nc_client_ssh_set_username("test_pk"); |
| 147 | assert_int_equal(ret, 0); |
| 148 | |
| 149 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PUBLICKEY, 1); |
| 150 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PASSWORD, -1); |
| 151 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_INTERACTIVE, -1); |
| 152 | |
| 153 | ret = nc_client_ssh_add_keypair(TESTS_DIR "/data/key_rsa.pub", TESTS_DIR "/data/key_rsa"); |
| 154 | assert_int_equal(ret, 0); |
| 155 | |
| 156 | pthread_barrier_wait(&state->barrier); |
Jan Kundrát | f8d9e8d | 2024-04-08 12:13:02 +0200 | [diff] [blame] | 157 | session = nc_connect_ssh("127.0.0.1", TEST_PORT, NULL); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 158 | assert_non_null(session); |
| 159 | |
| 160 | nc_session_free(session, NULL); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 161 | return NULL; |
| 162 | } |
| 163 | |
| 164 | static void |
| 165 | test_nc_auth_pubkey(void **state) |
| 166 | { |
| 167 | int ret, i; |
| 168 | pthread_t tids[2]; |
| 169 | |
| 170 | assert_non_null(state); |
| 171 | |
| 172 | ret = pthread_create(&tids[0], NULL, client_thread_pubkey, *state); |
| 173 | assert_int_equal(ret, 0); |
| 174 | ret = pthread_create(&tids[1], NULL, server_thread, *state); |
| 175 | assert_int_equal(ret, 0); |
| 176 | |
| 177 | for (i = 0; i < 2; i++) { |
| 178 | pthread_join(tids[i], NULL); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | static void * |
| 183 | client_thread_none(void *arg) |
| 184 | { |
| 185 | int ret; |
| 186 | struct nc_session *session = NULL; |
| 187 | struct test_state *state = arg; |
| 188 | |
roman | 472420c | 2023-04-24 16:28:09 +0200 | [diff] [blame] | 189 | /* skip all hostkey and known_hosts checks */ |
| 190 | nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP); |
| 191 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 192 | ret = nc_client_set_schema_searchpath(MODULES_DIR); |
| 193 | assert_int_equal(ret, 0); |
| 194 | |
| 195 | ret = nc_client_ssh_set_username("test_none"); |
| 196 | assert_int_equal(ret, 0); |
| 197 | |
| 198 | pthread_barrier_wait(&state->barrier); |
Jan Kundrát | f8d9e8d | 2024-04-08 12:13:02 +0200 | [diff] [blame] | 199 | session = nc_connect_ssh("127.0.0.1", TEST_PORT, NULL); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 200 | assert_non_null(session); |
| 201 | |
| 202 | nc_session_free(session, NULL); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 203 | return NULL; |
| 204 | } |
| 205 | |
| 206 | static void |
| 207 | test_nc_auth_none(void **state) |
| 208 | { |
| 209 | int ret, i; |
| 210 | pthread_t tids[2]; |
| 211 | |
| 212 | assert_non_null(state); |
| 213 | |
| 214 | ret = pthread_create(&tids[0], NULL, client_thread_none, *state); |
| 215 | assert_int_equal(ret, 0); |
| 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; |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 228 | struct lyd_node *tree = NULL; |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 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 | ret = ly_ctx_new(MODULES_DIR, 0, &ctx); |
| 243 | assert_int_equal(ret, 0); |
| 244 | |
| 245 | ret = nc_server_init_ctx(&ctx); |
| 246 | assert_int_equal(ret, 0); |
| 247 | |
| 248 | ret = nc_server_config_load_modules(&ctx); |
| 249 | assert_int_equal(ret, 0); |
| 250 | |
roman | d8973d1 | 2024-04-25 14:57:18 +0200 | [diff] [blame] | 251 | ret = nc_server_config_add_address_port(ctx, "endpt", NC_TI_SSH, "127.0.0.1", TEST_PORT, &tree); |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 252 | assert_int_equal(ret, 0); |
| 253 | |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 254 | ret = nc_server_config_add_ssh_hostkey(ctx, "endpt", "hostkey", TESTS_DIR "/data/key_ecdsa", NULL, &tree); |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 255 | assert_int_equal(ret, 0); |
| 256 | |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 257 | ret = nc_server_config_add_ssh_user_pubkey(ctx, "endpt", "test_pk", "pubkey", TESTS_DIR "/data/key_rsa.pub", &tree); |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 258 | assert_int_equal(ret, 0); |
| 259 | |
Roytak | b279485 | 2023-10-18 14:30:22 +0200 | [diff] [blame] | 260 | ret = nc_server_config_add_ssh_user_password(ctx, "endpt", "test_pw", "testpw", &tree); |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 261 | assert_int_equal(ret, 0); |
| 262 | |
Michal Vasko | cf89817 | 2024-01-15 15:04:28 +0100 | [diff] [blame] | 263 | ret = lyd_new_path(tree, ctx, "/ietf-netconf-server:netconf-server/listen/endpoints/endpoint[name='endpt']/ssh/" |
| 264 | "ssh-server-parameters/client-authentication/users/user[name='test_none']/none", NULL, 0, NULL); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 265 | assert_int_equal(ret, 0); |
| 266 | |
| 267 | /* configure the server based on the data */ |
roman | 142718b | 2023-06-29 09:15:29 +0200 | [diff] [blame] | 268 | ret = nc_server_config_setup_data(tree); |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 269 | assert_int_equal(ret, 0); |
| 270 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 271 | ret = nc_server_init(); |
| 272 | assert_int_equal(ret, 0); |
| 273 | |
roman | eadc478 | 2023-09-14 10:10:08 +0200 | [diff] [blame] | 274 | /* initialize client */ |
| 275 | ret = nc_client_init(); |
| 276 | assert_int_equal(ret, 0); |
| 277 | |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 278 | lyd_free_all(tree); |
| 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | static int |
| 284 | teardown_f(void **state) |
| 285 | { |
| 286 | int ret = 0; |
| 287 | struct test_state *test_state; |
| 288 | |
| 289 | assert_non_null(state); |
| 290 | test_state = *state; |
| 291 | |
| 292 | ret = pthread_barrier_destroy(&test_state->barrier); |
| 293 | assert_int_equal(ret, 0); |
| 294 | |
| 295 | free(*state); |
| 296 | nc_client_destroy(); |
| 297 | nc_server_destroy(); |
| 298 | ly_ctx_destroy(ctx); |
| 299 | |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | int |
| 304 | main(void) |
| 305 | { |
| 306 | const struct CMUnitTest tests[] = { |
roman | c1d2b09 | 2023-02-02 08:58:27 +0100 | [diff] [blame] | 307 | cmocka_unit_test_setup_teardown(test_nc_auth_pubkey, setup_f, teardown_f), |
| 308 | cmocka_unit_test_setup_teardown(test_nc_auth_password, setup_f, teardown_f), |
| 309 | cmocka_unit_test_setup_teardown(test_nc_auth_none, setup_f, teardown_f) |
| 310 | }; |
| 311 | |
| 312 | setenv("CMOCKA_TEST_ABORT", "1", 1); |
| 313 | return cmocka_run_group_tests(tests, NULL, NULL); |
| 314 | } |