blob: ae23959be35d5f02f833aa5d20dfc61d77802353 [file] [log] [blame]
romanc1d2b092023-02-02 08:58:27 +01001/**
2 * @file test_auth.c
3 * @author Roman Janota <xjanot04@fit.vutbr.cz>
roman808f3f62023-11-23 16:01:04 +01004 * @brief libnetconf2 SSH authentication methods test
romanc1d2b092023-02-02 08:58:27 +01005 *
6 * @copyright
roman808f3f62023-11-23 16:01:04 +01007 * Copyright (c) 2023 CESNET, z.s.p.o.
romanc1d2b092023-02-02 08:58:27 +01008 *
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
roman9b1379c2023-03-31 10:11:10 +020016#define _GNU_SOURCE
17
romanc1d2b092023-02-02 08:58:27 +010018#include <errno.h>
19#include <pthread.h>
20#include <setjmp.h>
roman9b1379c2023-03-31 10:11:10 +020021#include <stdarg.h>
romanc1d2b092023-02-02 08:58:27 +010022#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26#include <cmocka.h>
27
romanc1d2b092023-02-02 08:58:27 +010028#include "tests/config.h"
29
roman83683fb2023-02-24 09:15:23 +010030#define NC_ACCEPT_TIMEOUT 2000
31#define NC_PS_POLL_TIMEOUT 2000
romanc1d2b092023-02-02 08:58:27 +010032
33struct ly_ctx *ctx;
34
35struct test_state {
romanc1d2b092023-02-02 08:58:27 +010036 pthread_barrier_t barrier;
37};
38
romanc1d2b092023-02-02 08:58:27 +010039static void *
40server_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);
romanc1d2b092023-02-02 08:58:27 +010068 return NULL;
69}
70
romanc1d2b092023-02-02 08:58:27 +010071static char *
romanc1d2b092023-02-02 08:58:27 +010072auth_password(const char *username, const char *hostname, void *priv)
73{
74 (void) hostname;
75 (void) priv;
76
roman9b1379c2023-03-31 10:11:10 +020077 /* set the reply to password authentication */
romanc1d2b092023-02-02 08:58:27 +010078 if (!strcmp(username, "test_pw")) {
79 return strdup("testpw");
80 } else {
81 return NULL;
82 }
83}
84
85static void *
86client_thread_password(void *arg)
87{
88 int ret;
89 struct nc_session *session = NULL;
90 struct test_state *state = arg;
91
roman472420c2023-04-24 16:28:09 +020092 /* skip all hostkey and known_hosts checks */
93 nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP);
94
romanc1d2b092023-02-02 08:58:27 +010095 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átf8d9e8d2024-04-08 12:13:02 +0200108 session = nc_connect_ssh("127.0.0.1", TEST_PORT, NULL);
romanc1d2b092023-02-02 08:58:27 +0100109 assert_non_null(session);
110
111 nc_session_free(session, NULL);
romanc1d2b092023-02-02 08:58:27 +0100112 return NULL;
113}
114
115static void
116test_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
133static void *
134client_thread_pubkey(void *arg)
135{
136 int ret;
137 struct nc_session *session = NULL;
138 struct test_state *state = arg;
139
roman472420c2023-04-24 16:28:09 +0200140 /* skip all hostkey and known_hosts checks */
141 nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP);
142
romanc1d2b092023-02-02 08:58:27 +0100143 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átf8d9e8d2024-04-08 12:13:02 +0200157 session = nc_connect_ssh("127.0.0.1", TEST_PORT, NULL);
romanc1d2b092023-02-02 08:58:27 +0100158 assert_non_null(session);
159
160 nc_session_free(session, NULL);
romanc1d2b092023-02-02 08:58:27 +0100161 return NULL;
162}
163
164static void
165test_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
182static void *
183client_thread_none(void *arg)
184{
185 int ret;
186 struct nc_session *session = NULL;
187 struct test_state *state = arg;
188
roman472420c2023-04-24 16:28:09 +0200189 /* skip all hostkey and known_hosts checks */
190 nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP);
191
romanc1d2b092023-02-02 08:58:27 +0100192 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átf8d9e8d2024-04-08 12:13:02 +0200199 session = nc_connect_ssh("127.0.0.1", TEST_PORT, NULL);
romanc1d2b092023-02-02 08:58:27 +0100200 assert_non_null(session);
201
202 nc_session_free(session, NULL);
romanc1d2b092023-02-02 08:58:27 +0100203 return NULL;
204}
205
206static void
207test_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
224static int
225setup_f(void **state)
226{
227 int ret;
roman142718b2023-06-29 09:15:29 +0200228 struct lyd_node *tree = NULL;
romanc1d2b092023-02-02 08:58:27 +0100229 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
romand8973d12024-04-25 14:57:18 +0200251 ret = nc_server_config_add_address_port(ctx, "endpt", NC_TI_SSH, "127.0.0.1", TEST_PORT, &tree);
roman142718b2023-06-29 09:15:29 +0200252 assert_int_equal(ret, 0);
253
Roytakb2794852023-10-18 14:30:22 +0200254 ret = nc_server_config_add_ssh_hostkey(ctx, "endpt", "hostkey", TESTS_DIR "/data/key_ecdsa", NULL, &tree);
roman142718b2023-06-29 09:15:29 +0200255 assert_int_equal(ret, 0);
256
Roytakb2794852023-10-18 14:30:22 +0200257 ret = nc_server_config_add_ssh_user_pubkey(ctx, "endpt", "test_pk", "pubkey", TESTS_DIR "/data/key_rsa.pub", &tree);
roman142718b2023-06-29 09:15:29 +0200258 assert_int_equal(ret, 0);
259
Roytakb2794852023-10-18 14:30:22 +0200260 ret = nc_server_config_add_ssh_user_password(ctx, "endpt", "test_pw", "testpw", &tree);
roman142718b2023-06-29 09:15:29 +0200261 assert_int_equal(ret, 0);
262
Michal Vaskocf898172024-01-15 15:04:28 +0100263 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);
romanc1d2b092023-02-02 08:58:27 +0100265 assert_int_equal(ret, 0);
266
267 /* configure the server based on the data */
roman142718b2023-06-29 09:15:29 +0200268 ret = nc_server_config_setup_data(tree);
romanc1d2b092023-02-02 08:58:27 +0100269 assert_int_equal(ret, 0);
270
romanc1d2b092023-02-02 08:58:27 +0100271 ret = nc_server_init();
272 assert_int_equal(ret, 0);
273
romaneadc4782023-09-14 10:10:08 +0200274 /* initialize client */
275 ret = nc_client_init();
276 assert_int_equal(ret, 0);
277
romanc1d2b092023-02-02 08:58:27 +0100278 lyd_free_all(tree);
279
280 return 0;
281}
282
283static int
284teardown_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
303int
304main(void)
305{
306 const struct CMUnitTest tests[] = {
romanc1d2b092023-02-02 08:58:27 +0100307 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}