blob: 2af23de7ebb69f9a4995e49c1340ccc389760063 [file] [log] [blame]
Michal Vasko734360b2016-02-08 15:38:25 +01001/**
2 * \file test_server_thread.c
3 * \author Michal Vasko <mvasko@cesnet.cz>
4 * \brief libnetconf2 tests - thread-safety of all server functions
5 *
Michal Vasko4c1fb492017-01-30 14:31:07 +01006 * Copyright (c) 2017 CESNET, z.s.p.o.
Michal Vasko734360b2016-02-08 15:38:25 +01007 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +01008 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
Michal Vaskoad1cc6a2016-02-26 15:06:06 +010011 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * https://opensource.org/licenses/BSD-3-Clause
Michal Vasko734360b2016-02-08 15:38:25 +010013 */
14
Michal Vaskoba9f3582023-02-22 10:26:32 +010015#define _GNU_SOURCE
16
Michal Vasko734360b2016-02-08 15:38:25 +010017#include <pthread.h>
Michal Vasko64b31a22016-03-03 11:00:00 +010018#include <stdio.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020019#include <stdlib.h>
Michal Vaskoa28be9a2016-03-01 15:20:47 +010020#include <string.h>
Michal Vasko734360b2016-02-08 15:38:25 +010021#include <sys/types.h>
22#include <sys/wait.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020023#include <unistd.h>
Michal Vasko734360b2016-02-08 15:38:25 +010024
25#include <libyang/libyang.h>
26
Michal Vaskob83a3fa2021-05-26 09:53:42 +020027#include <log.h>
Michal Vasko734360b2016-02-08 15:38:25 +010028#include <session_client.h>
29#include <session_server.h>
Jan Kundrátcf15d6c2017-10-26 18:07:56 +020030#include "tests/config.h"
Michal Vasko734360b2016-02-08 15:38:25 +010031
32/* millisec */
Michal Vasko15592cc2016-02-09 09:52:43 +010033#define NC_ACCEPT_TIMEOUT 5000
Michal Vasko734360b2016-02-08 15:38:25 +010034/* millisec */
Michal Vasko15592cc2016-02-09 09:52:43 +010035#define NC_PS_POLL_TIMEOUT 5000
Michal Vasko734360b2016-02-08 15:38:25 +010036/* sec */
Michal Vasko15592cc2016-02-09 09:52:43 +010037#define CLIENT_SSH_AUTH_TIMEOUT 10
Michal Vasko734360b2016-02-08 15:38:25 +010038
Michal Vasko64b31a22016-03-03 11:00:00 +010039#define nc_assert(cond) if (!(cond)) { fprintf(stderr, "assert failed (%s:%d)\n", __FILE__, __LINE__); exit(1); }
40
Claus Klein22091912020-01-20 13:45:47 +010041#if _POSIX_BARRIERS >= 200112L
Michal Vasko734360b2016-02-08 15:38:25 +010042pthread_barrier_t barrier;
43
Michal Vaskob83a3fa2021-05-26 09:53:42 +020044#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Michal Vasko734360b2016-02-08 15:38:25 +010045
Michal Vasko93224072021-11-09 12:14:28 +010046struct ly_ctx *ctx;
47
Michal Vasko734360b2016-02-08 15:38:25 +010048static void *
49server_thread(void *arg)
50{
51 (void)arg;
Michal Vasko71090fc2016-05-24 16:37:28 +020052 NC_MSG_TYPE msgtype;
Michal Vasko734360b2016-02-08 15:38:25 +010053 int ret;
54 struct nc_pollsession *ps;
55 struct nc_session *session;
56
57 ps = nc_ps_new();
Michal Vasko64b31a22016-03-03 11:00:00 +010058 nc_assert(ps);
Michal Vasko734360b2016-02-08 15:38:25 +010059
60 pthread_barrier_wait(&barrier);
61
Michal Vaskob83a3fa2021-05-26 09:53:42 +020062#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko93224072021-11-09 12:14:28 +010063 msgtype = nc_accept(NC_ACCEPT_TIMEOUT, ctx, &session);
Michal Vasko71090fc2016-05-24 16:37:28 +020064 nc_assert(msgtype == NC_MSG_HELLO);
Michal Vasko734360b2016-02-08 15:38:25 +010065
66 nc_ps_add_session(ps, session);
Michal Vasko71090fc2016-05-24 16:37:28 +020067 ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT, NULL);
68 nc_assert(ret & NC_PSPOLL_RPC);
Michal Vasko78939072022-12-12 07:43:18 +010069 ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT, NULL);
70 nc_assert(ret & NC_PSPOLL_RPC);
Michal Vasko81f9e052022-12-12 07:53:32 +010071 nc_ps_clear(ps, 1, NULL);
Michal Vasko734360b2016-02-08 15:38:25 +010072#endif
73
Michal Vasko93224072021-11-09 12:14:28 +010074 msgtype = nc_accept(NC_ACCEPT_TIMEOUT, ctx, &session);
Michal Vasko71090fc2016-05-24 16:37:28 +020075 nc_assert(msgtype == NC_MSG_HELLO);
Michal Vasko734360b2016-02-08 15:38:25 +010076
77 nc_ps_add_session(ps, session);
Michal Vasko71090fc2016-05-24 16:37:28 +020078 ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT, NULL);
79 nc_assert(ret & NC_PSPOLL_RPC);
Michal Vasko78939072022-12-12 07:43:18 +010080 ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT, NULL);
81 nc_assert(ret & NC_PSPOLL_RPC);
Michal Vasko81f9e052022-12-12 07:53:32 +010082 nc_ps_clear(ps, 1, NULL);
Michal Vasko734360b2016-02-08 15:38:25 +010083
84 nc_ps_free(ps);
85
86 nc_thread_destroy();
87 return NULL;
88}
89
Michal Vasko338f8472016-10-13 15:01:27 +020090#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
91
92#ifdef NC_ENABLED_SSH
93
Michal Vasko4c1fb492017-01-30 14:31:07 +010094static int
95clb_hostkeys(const char *name, void *UNUSED(user_data), char **privkey_path, char **UNUSED(privkey_data),
Michal Vaskob83a3fa2021-05-26 09:53:42 +020096 NC_SSH_KEY_TYPE *UNUSED(privkey_type))
Michal Vasko4c1fb492017-01-30 14:31:07 +010097{
98 if (!strcmp(name, "key_rsa")) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +020099 *privkey_path = strdup(TESTS_DIR "/data/key_rsa");
Michal Vasko4c1fb492017-01-30 14:31:07 +0100100 return 0;
Michal Vasko7a4213f2022-09-08 11:02:34 +0200101 } else if (!strcmp(name, "key_ecdsa")) {
102 *privkey_path = strdup(TESTS_DIR "/data/key_ecdsa");
Michal Vasko4c1fb492017-01-30 14:31:07 +0100103 return 0;
104 }
105
106 return 1;
107}
108
Michal Vasko734360b2016-02-08 15:38:25 +0100109static void *
Michal Vaskoe2713da2016-08-22 16:06:40 +0200110add_endpt_thread(void *arg)
Michal Vasko734360b2016-02-08 15:38:25 +0100111{
112 (void)arg;
113 int ret;
114
115 pthread_barrier_wait(&barrier);
Michal Vasko338f8472016-10-13 15:01:27 +0200116 ret = nc_server_add_endpt("tertiary", NC_TI_LIBSSH);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200117 nc_assert(!ret);
118
119 return NULL;
120}
121
122static void *
123del_endpt_thread(void *arg)
124{
125 (void)arg;
126 int ret;
127
128 pthread_barrier_wait(&barrier);
129
Michal Vasko59050372016-11-22 14:33:55 +0100130 ret = nc_server_del_endpt("secondary", 0);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200131 nc_assert(!ret);
132
133 return NULL;
134}
135
Michal Vasko734360b2016-02-08 15:38:25 +0100136static void *
137ssh_endpt_set_hostkey_thread(void *arg)
138{
139 (void)arg;
140 int ret;
141
142 pthread_barrier_wait(&barrier);
143
Michal Vasko7a4213f2022-09-08 11:02:34 +0200144 ret = nc_server_ssh_endpt_add_hostkey("main_ssh", "key_ecdsa", -1);
Michal Vasko64b31a22016-03-03 11:00:00 +0100145 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100146
147 return NULL;
148}
149
150static void *
Michal Vasko734360b2016-02-08 15:38:25 +0100151ssh_endpt_set_auth_methods_thread(void *arg)
152{
153 (void)arg;
154 int ret;
155
156 pthread_barrier_wait(&barrier);
157
roman41a11e42022-06-22 09:27:08 +0200158 ret = nc_server_ssh_endpt_set_auth_methods("main_ssh", NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD);
Michal Vasko64b31a22016-03-03 11:00:00 +0100159 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100160
161 return NULL;
162}
163
164static void *
165ssh_endpt_set_auth_attempts_thread(void *arg)
166{
167 (void)arg;
168 int ret;
169
170 pthread_barrier_wait(&barrier);
171
Michal Vasko338f8472016-10-13 15:01:27 +0200172 ret = nc_server_ssh_endpt_set_auth_attempts("main_ssh", 2);
Michal Vasko64b31a22016-03-03 11:00:00 +0100173 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100174
175 return NULL;
176}
177
178static void *
179ssh_endpt_set_auth_timeout_thread(void *arg)
180{
181 (void)arg;
182 int ret;
183
184 pthread_barrier_wait(&barrier);
185
Michal Vasko338f8472016-10-13 15:01:27 +0200186 ret = nc_server_ssh_endpt_set_auth_timeout("main_ssh", 5);
Michal Vasko64b31a22016-03-03 11:00:00 +0100187 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100188
189 return NULL;
190}
191
192static void *
193ssh_endpt_add_authkey_thread(void *arg)
194{
195 (void)arg;
196 int ret;
197
198 pthread_barrier_wait(&barrier);
199
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200200 ret = nc_server_ssh_add_authkey_path(TESTS_DIR "/data/key_rsa.pub", "test3");
Michal Vasko64b31a22016-03-03 11:00:00 +0100201 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100202
203 return NULL;
204}
205
206static void *
207ssh_endpt_del_authkey_thread(void *arg)
208{
209 (void)arg;
210 int ret;
211
212 pthread_barrier_wait(&barrier);
213
Michal Vasko7a4213f2022-09-08 11:02:34 +0200214 ret = nc_server_ssh_del_authkey(TESTS_DIR "/data/key_ecdsa.pub", NULL, 0, "test2");
Michal Vasko64b31a22016-03-03 11:00:00 +0100215 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100216
217 return NULL;
218}
219
Michal Vasko693eb112016-02-18 13:28:49 +0100220static int
Radek Krejci90a84a22017-05-25 13:53:00 +0200221ssh_hostkey_check_clb(const char *hostname, ssh_session session, void *priv)
Michal Vasko693eb112016-02-18 13:28:49 +0100222{
223 (void)hostname;
224 (void)session;
Radek Krejci90a84a22017-05-25 13:53:00 +0200225 (void)priv;
Michal Vasko693eb112016-02-18 13:28:49 +0100226
227 return 0;
228}
229
Michal Vasko734360b2016-02-08 15:38:25 +0100230static void *
231ssh_client_thread(void *arg)
232{
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100233 int ret, read_pipe = *(int *)arg;
234 char buf[9];
Michal Vasko734360b2016-02-08 15:38:25 +0100235 struct nc_session *session;
236
Michal Vasko5a446472020-05-06 12:43:42 +0200237 fprintf(stdout, "SSH client start.\n");
238
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100239 ret = read(read_pipe, buf, 9);
Michal Vasko64b31a22016-03-03 11:00:00 +0100240 nc_assert(ret == 9);
241 nc_assert(!strncmp(buf, "ssh_ready", 9));
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100242
Michal Vasko693eb112016-02-18 13:28:49 +0100243 /* skip the knownhost check */
Radek Krejci90a84a22017-05-25 13:53:00 +0200244 nc_client_ssh_set_auth_hostkey_check_clb(ssh_hostkey_check_clb, NULL);
Michal Vasko734360b2016-02-08 15:38:25 +0100245
Michal Vasko693eb112016-02-18 13:28:49 +0100246 ret = nc_client_ssh_set_username("test");
Michal Vasko64b31a22016-03-03 11:00:00 +0100247 nc_assert(!ret);
Michal Vasko693eb112016-02-18 13:28:49 +0100248
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200249 ret = nc_client_ssh_add_keypair(TESTS_DIR "/data/key_ecdsa.pub", TESTS_DIR "/data/key_ecdsa");
Michal Vasko64b31a22016-03-03 11:00:00 +0100250 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100251
252 nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PUBLICKEY, 1);
253 nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PASSWORD, -1);
254 nc_client_ssh_set_auth_pref(NC_SSH_AUTH_INTERACTIVE, -1);
255
Michal Vasko693eb112016-02-18 13:28:49 +0100256 session = nc_connect_ssh("127.0.0.1", 6001, NULL);
Michal Vasko64b31a22016-03-03 11:00:00 +0100257 nc_assert(session);
Michal Vasko734360b2016-02-08 15:38:25 +0100258
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100259 nc_session_free(session, NULL);
Michal Vasko734360b2016-02-08 15:38:25 +0100260
Michal Vaskoe2713da2016-08-22 16:06:40 +0200261 fprintf(stdout, "SSH client finished.\n");
262
Michal Vasko734360b2016-02-08 15:38:25 +0100263 nc_thread_destroy();
264 return NULL;
265}
266
Radek Krejci53691be2016-02-22 13:58:37 +0100267#endif /* NC_ENABLED_SSH */
Michal Vasko734360b2016-02-08 15:38:25 +0100268
Radek Krejci53691be2016-02-22 13:58:37 +0100269#ifdef NC_ENABLED_TLS
Michal Vasko734360b2016-02-08 15:38:25 +0100270
Michal Vasko4c1fb492017-01-30 14:31:07 +0100271static int
272clb_server_cert(const char *name, void *UNUSED(user_data), char **cert_path, char **cert_data, char **privkey_path,
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200273 char **privkey_data, NC_SSH_KEY_TYPE *privkey_type)
Michal Vasko4c1fb492017-01-30 14:31:07 +0100274{
275 if (!strcmp(name, "server_cert1")) {
Michal Vasko346ac562018-11-05 08:47:18 +0100276 *cert_data = strdup("MIIEQDCCAygCCQCV65JgDvfWkTANBgkqhkiG9w0BAQsFADBjMQswCQYDVQQGEwJD\n"
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200277 "WjETMBEGA1UECAwKU29tZS1TdGF0ZTENMAsGA1UEBwwEQnJubzEPMA0GA1UECgwG\n"
278 "Q0VTTkVUMQwwCgYDVQQLDANUTUMxETAPBgNVBAMMCHNlcnZlcmNhMB4XDTE4MTEw\n"
279 "NTA3MzExMFoXDTI4MTEwMjA3MzExMFowYTELMAkGA1UEBhMCQ1oxEzARBgNVBAgM\n"
280 "ClNvbWUtU3RhdGUxDTALBgNVBAc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jRaraIevjATDPfsbHFx9DTNrS5pXHIFbRcz\n"
296 "y3WniYXTKhpfM6m+1X8ogImE968DG8RqAW5YZZtrZW0VF/dhlQp20jEX/8Rv33Bp\n"
297 "RhNEIhPnYAquKCesMMclUtPW+5n2z8rgj5t/ETv4wc5QegpyPfdHNq09bGKB10Sy\n"
298 "sGvC6hP9GKU3R2Jhxih/t88O3WoisFQ8+Tf9s2LuSxUV0bzp");
Michal Vasko4c1fb492017-01-30 14:31:07 +0100299 *privkey_data = strdup("MIIJKAIBAAKCAgEA6ojtjfDmvyQP1ZkIwBpr97eKDuebvpoglRHRdvVuTpf/gU1V\n"
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200300 "ArAQmwGh05i6lm8TkVl1noMlIxLJDcWslaeVn6KyvsX0HhsQtXwqPqwka5UCv6al\n"
301 "wf/ivAvcNpcX1j0t/uIGCI4dSiKnzQCyf0FTirzQkjrDZUd3meDhNQTruCalGV4g\n"
302 "fNWIq3e1oGuwAn1tLlu9oTrE4HzMpgbNEU6wNmsSqpwGxUhYLoSaM7b0dLmqP+Zc\n"
303 "zSS0Uac0PFNkehGQ2CYIT80f580o4XGtoLCUUGkp6YCTL4Z2CeBEaJABWjDIDH+d\n"
304 "KYIUBqUpz4Th12gXAP+h+3qI6+9eppeHrfrzARDsfLjwUNxQJse1QSArjAytf0FK\n"
305 "tGHrORc7W0TiCFvR0zaoUNLTKk7enTiRQ9rfWZOAu44fUvPCaXDE6zXXeaVgoKCo\n"
306 "4VHlho36erUcjlEBM+jk28IykbZGtBb6igKvYa1tPSgeYm/zJoFVjQcnr14uci/f\n"
307 "t1+Na+hOIEoEEiKxcAPk2b2vBKNlRIW7WLJ3u7ZiuQEJTNm6+3cE4+lfwaBCBqBT\n"
308 "oE+dpzvoUXoMyFFReUFd1O5axu4fXgt00jMaOQxmE0v9OmR/pL/PWIflVF4Zz5yV\n"
309 "ONYaDVc7l+veY0oEZruEPJ0hlEgxuCzLrcMhjufl2qE2Q7fQIaav/1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
340 "3gX39LoiWvLDp2qJvplm6rTpi8Rgap6rZSqHe1yNKIxxD2vlr/WY9SMgLXYASO4S\n"
341 "SBz9wfGOmQIPk6KXNJkdV4kC7nNjIi75iwLLCgjHgUiHTrDq5sWekpeNnUoWsinb\n"
342 "Tsdsjnv3zHG9GyiClyLGxMbs4M5eyYECggEBAKuC8ZMpdIrjk6tERYB6g0LnQ7mW\n"
343 "8XYbDFAmLYMLs9yfG2jcjVbsW9Kugsr+3poUUv/q+hNO3jfY4HazhZDa0MalgNPo\n"
344 "Swr/VNRnkck40x2ovFb989J7yl++zTrnIrax9XRH1V0cNu+Kj7OMwZ2RRfbNv5JB\n"
345 "dOZPvkfqyIKFmbQgYbtD66rHuzNOfJpzqr/WVLO57/zzW8245NKG2B6B0oXkei/K\n"
346 "qDY0DAbHR3i3EOj1NPtVI1FC/xX8R9BREaid458bqoHJKuInrGcBjaUI9Cvymv8T\n"
347 "bstUgD6NPbJR4Sm6vrLeUqzjWZP3t1+Z6DjXmnpR2vvhMU/FWb//21p/88o=");
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200348 *privkey_type = NC_SSH_KEY_RSA;
Michal Vasko4c1fb492017-01-30 14:31:07 +0100349 return 0;
350 } else if (!strcmp(name, "main_cert")) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200351 *cert_path = strdup(TESTS_DIR "/data/server.crt");
352 *privkey_path = strdup(TESTS_DIR "/data/server.key");
Michal Vasko4c1fb492017-01-30 14:31:07 +0100353 return 0;
354 }
355
356 return 1;
357}
358
359static int
360clb_trusted_cert_lists(const char *name, void *UNUSED(user_data), char ***cert_paths, int *cert_path_count,
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200361 char ***cert_data, int *cert_data_count)
Michal Vasko4c1fb492017-01-30 14:31:07 +0100362{
363 if (!strcmp(name, "trusted_cert_list1")) {
Michal Vaskoad0bcb52017-03-22 11:55:47 +0100364 *cert_data = malloc(sizeof **cert_data);
Michal Vasko346ac562018-11-05 08:47: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XiAEbB5dglxVOSa8me5a/70uHK/27JZ6veeIgRqZ4VgPHnBC\n"
380 "a3m6EHr+mnTjjqSUcGIUiKV3g2Dumw8paqZC+Qv+Ib/NKquS1lO2Ry1wHBtXzn5K\n"
381 "KHHyM1bWMDaDszirw2+pp22VdRrPZNA9NWXheEDYOLyQekyL2CfidhxhaXvUZyWg\n"
382 "alLyF2XRZ5/jAT+NjfWw39EmWPUGk13Jm83OaFc1VdrXNCiD0sGCQ+BTCllDinQv\n"
383 "R08yzd4fzA3YXthvX1dBu1SvqQAGOS7gssRCyv9uWI6MXta25X91eY1ZMz1euJ04\n"
384 "mB8EdyYiZc0kzrb9dv5d0g==");
Michal Vasko4c1fb492017-01-30 14:31:07 +0100385 *cert_data_count = 1;
386 return 0;
387 } else if (!strcmp(name, "client_cert_list")) {
Michal Vasko75ff74b2017-03-21 15:34:34 +0100388 *cert_paths = malloc(sizeof **cert_paths);
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200389 (*cert_paths)[0] = strdup(TESTS_DIR "/data/client.crt");
Michal Vasko4c1fb492017-01-30 14:31:07 +0100390 *cert_path_count = 1;
391 return 0;
392 }
393
394 return 1;
395}
396
Michal Vasko734360b2016-02-08 15:38:25 +0100397static void *
Michal Vasko338f8472016-10-13 15:01:27 +0200398endpt_set_address_thread(void *arg)
Michal Vasko734360b2016-02-08 15:38:25 +0100399{
400 (void)arg;
401 int ret;
402
403 pthread_barrier_wait(&barrier);
404
Michal Vasko338f8472016-10-13 15:01:27 +0200405 ret = nc_server_endpt_set_address("quaternary", "0.0.0.0");
Michal Vasko64b31a22016-03-03 11:00:00 +0100406 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100407
408 return NULL;
409}
410
411static void *
Michal Vasko338f8472016-10-13 15:01:27 +0200412endpt_set_port_thread(void *arg)
Michal Vasko734360b2016-02-08 15:38:25 +0100413{
414 (void)arg;
415 int ret;
416
417 pthread_barrier_wait(&barrier);
418
Michal Vasko338f8472016-10-13 15:01:27 +0200419 ret = nc_server_endpt_set_port("quaternary", 6003);
Michal Vasko64b31a22016-03-03 11:00:00 +0100420 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100421
422 return NULL;
423}
424
425static void *
Michal Vasko4c1fb492017-01-30 14:31:07 +0100426tls_endpt_set_server_cert_thread(void *arg)
Michal Vasko734360b2016-02-08 15:38:25 +0100427{
428 (void)arg;
429 int ret;
430
431 pthread_barrier_wait(&barrier);
432
Michal Vasko4c1fb492017-01-30 14:31:07 +0100433 ret = nc_server_tls_endpt_set_server_cert("quaternary", "server_cert1");
Michal Vasko64b31a22016-03-03 11:00:00 +0100434 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100435
436 nc_thread_destroy();
437 return NULL;
438}
439
440static void *
Michal Vasko4c1fb492017-01-30 14:31:07 +0100441tls_endpt_add_trusted_cert_list_thread(void *arg)
Michal Vasko734360b2016-02-08 15:38:25 +0100442{
443 (void)arg;
444 int ret;
445
446 pthread_barrier_wait(&barrier);
447
Michal Vasko4c1fb492017-01-30 14:31:07 +0100448 ret = nc_server_tls_endpt_add_trusted_cert_list("quaternary", "trusted_cert_list1");
Michal Vasko64b31a22016-03-03 11:00:00 +0100449 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100450
451 nc_thread_destroy();
452 return NULL;
453}
454
455static void *
456tls_endpt_set_trusted_ca_paths_thread(void *arg)
457{
458 (void)arg;
459 int ret;
460
461 pthread_barrier_wait(&barrier);
462
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200463 ret = nc_server_tls_endpt_set_trusted_ca_paths("quaternary", TESTS_DIR "/data/serverca.pem", "data");
Michal Vasko64b31a22016-03-03 11:00:00 +0100464 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100465
466 nc_thread_destroy();
467 return NULL;
468}
469
470static void *
Michal Vasko4c1fb492017-01-30 14:31:07 +0100471tls_endpt_del_trusted_cert_list_thread(void *arg)
Michal Vasko734360b2016-02-08 15:38:25 +0100472{
473 (void)arg;
474
475 pthread_barrier_wait(&barrier);
476
Michal Vasko4c1fb492017-01-30 14:31:07 +0100477 nc_server_tls_endpt_del_trusted_cert_list("quaternary", "trusted_cert_list1");
Michal Vasko734360b2016-02-08 15:38:25 +0100478
479 return NULL;
480}
481
482static void *
483tls_endpt_set_crl_paths_thread(void *arg)
484{
485 (void)arg;
486 int ret;
487
488 pthread_barrier_wait(&barrier);
489
490 ret = nc_server_tls_endpt_set_crl_paths("quaternary", NULL, "data");
Michal Vasko64b31a22016-03-03 11:00:00 +0100491 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100492
493 nc_thread_destroy();
494 return NULL;
495}
496
497static void *
498tls_endpt_clear_crls_thread(void *arg)
499{
500 (void)arg;
501
502 pthread_barrier_wait(&barrier);
503
504 nc_server_tls_endpt_clear_crls("quaternary");
505
506 return NULL;
507}
508
509static void *
510tls_endpt_add_ctn_thread(void *arg)
511{
512 (void)arg;
513 int ret;
514
515 pthread_barrier_wait(&barrier);
516
Michal Vasko3cf4aaa2017-02-01 15:03:36 +0100517 ret = nc_server_tls_endpt_add_ctn("main_tls", 2, "02:F0:F1:F2:F3:F4:F5:F6:F7:F8:F9:10:11:12:EE:FF:A0:A1:A2:A3",
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200518 NC_TLS_CTN_SAN_IP_ADDRESS, NULL);
Michal Vasko64b31a22016-03-03 11:00:00 +0100519 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100520
521 return NULL;
522}
523
524static void *
525tls_endpt_del_ctn_thread(void *arg)
526{
527 (void)arg;
528 int ret;
529
530 pthread_barrier_wait(&barrier);
531
Michal Vasko338f8472016-10-13 15:01:27 +0200532 ret = nc_server_tls_endpt_del_ctn("main_tls", -1, NULL, NC_TLS_CTN_SAN_ANY, NULL);
Michal Vasko64b31a22016-03-03 11:00:00 +0100533 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100534
535 return NULL;
536}
537
538static void *
539tls_client_thread(void *arg)
540{
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100541 int ret, read_pipe = *(int *)arg;
542 char buf[9];
Michal Vasko734360b2016-02-08 15:38:25 +0100543 struct nc_session *session;
544
Michal Vasko5a446472020-05-06 12:43:42 +0200545 fprintf(stdout, "TLS client start.\n");
546
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100547 ret = read(read_pipe, buf, 9);
Michal Vasko64b31a22016-03-03 11:00:00 +0100548 nc_assert(ret == 9);
549 nc_assert(!strncmp(buf, "tls_ready", 9));
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100550
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200551 ret = nc_client_tls_set_cert_key_paths(TESTS_DIR "/data/client.crt", TESTS_DIR "/data/client.key");
Michal Vasko64b31a22016-03-03 11:00:00 +0100552 nc_assert(!ret);
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200553 ret = nc_client_tls_set_trusted_ca_paths(NULL, TESTS_DIR "/data");
Michal Vasko64b31a22016-03-03 11:00:00 +0100554 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100555
556 session = nc_connect_tls("127.0.0.1", 6501, NULL);
Michal Vasko64b31a22016-03-03 11:00:00 +0100557 nc_assert(session);
Michal Vasko734360b2016-02-08 15:38:25 +0100558
Frank Rimplercb9274f2019-03-13 16:40:34 +0000559 /* verify some capabilities */
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200560 nc_assert(nc_session_cpblt(session, "urn:jmu:params:xml:ns:yang:module-a?module=module-a&deviations=module-a-dv,module-a-dv2"));
Frank Rimplercb9274f2019-03-13 16:40:34 +0000561
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100562 nc_session_free(session, NULL);
Michal Vasko734360b2016-02-08 15:38:25 +0100563
Michal Vaskoe2713da2016-08-22 16:06:40 +0200564 fprintf(stdout, "TLS client finished.\n");
565
Michal Vasko734360b2016-02-08 15:38:25 +0100566 nc_thread_destroy();
567 return NULL;
568}
569
Radek Krejci53691be2016-02-22 13:58:37 +0100570#endif /* NC_ENABLED_TLS */
Michal Vasko734360b2016-02-08 15:38:25 +0100571
572static void *(*thread_funcs[])(void *) = {
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200573#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Michal Vasko734360b2016-02-08 15:38:25 +0100574 server_thread,
575#endif
Radek Krejci53691be2016-02-22 13:58:37 +0100576#ifdef NC_ENABLED_SSH
Michal Vasko8727e432016-10-13 15:44:10 +0200577 add_endpt_thread,
578 del_endpt_thread,
Michal Vasko734360b2016-02-08 15:38:25 +0100579 ssh_endpt_set_hostkey_thread,
Michal Vasko734360b2016-02-08 15:38:25 +0100580 ssh_endpt_set_auth_methods_thread,
581 ssh_endpt_set_auth_attempts_thread,
582 ssh_endpt_set_auth_timeout_thread,
583 ssh_endpt_add_authkey_thread,
584 ssh_endpt_del_authkey_thread,
585#endif
Radek Krejci53691be2016-02-22 13:58:37 +0100586#ifdef NC_ENABLED_TLS
Michal Vasko8727e432016-10-13 15:44:10 +0200587 endpt_set_address_thread,
588 endpt_set_port_thread,
Michal Vasko4c1fb492017-01-30 14:31:07 +0100589 tls_endpt_set_server_cert_thread,
590 tls_endpt_add_trusted_cert_list_thread,
Michal Vasko734360b2016-02-08 15:38:25 +0100591 tls_endpt_set_trusted_ca_paths_thread,
Michal Vasko4c1fb492017-01-30 14:31:07 +0100592 tls_endpt_del_trusted_cert_list_thread,
Michal Vasko734360b2016-02-08 15:38:25 +0100593 tls_endpt_set_crl_paths_thread,
594 tls_endpt_clear_crls_thread,
595 tls_endpt_add_ctn_thread,
596 tls_endpt_del_ctn_thread,
597#endif
598};
599
600const int thread_count = sizeof thread_funcs / sizeof *thread_funcs;
601
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200602#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100603const int client_count = 2;
604pid_t pids[2];
605int pipes[4];
Michal Vasko734360b2016-02-08 15:38:25 +0100606#else
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100607const int client_count = 1;
608pid_t pids[1];
609int pipes[2];
610#endif
611
612static void
613client_fork(void)
614{
615 int ret, clients = 0;
616
617#ifdef NC_ENABLED_SSH
Jan Kundrát5cec3c22021-10-08 19:54:39 +0200618 nc_assert(pipe(pipes + clients * 2) == 0);
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100619
620 if (!(pids[clients] = fork())) {
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100621
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200622 ret = nc_client_set_schema_searchpath(TESTS_DIR "/data/modules");
Michal Vasko64b31a22016-03-03 11:00:00 +0100623 nc_assert(!ret);
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100624
625 /* close write */
626 close(pipes[clients * 2 + 1]);
627 ssh_client_thread(&pipes[clients * 2]);
628 close(pipes[clients * 2]);
629 nc_client_destroy();
630 exit(0);
Michal Vasko734360b2016-02-08 15:38:25 +0100631 }
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100632 /* close read */
633 close(pipes[clients * 2]);
634
635 ++clients;
636#endif
637
638#ifdef NC_ENABLED_TLS
Jan Kundrát5cec3c22021-10-08 19:54:39 +0200639 nc_assert(pipe(pipes + clients * 2) == 0);
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100640
641 if (!(pids[clients] = fork())) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200642 ret = nc_client_set_schema_searchpath(TESTS_DIR "/data/modules");
Michal Vasko64b31a22016-03-03 11:00:00 +0100643 nc_assert(!ret);
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100644
645 /* close write */
646 close(pipes[clients * 2 + 1]);
647 tls_client_thread(&pipes[clients * 2]);
648 close(pipes[clients * 2]);
649 nc_client_destroy();
650 exit(0);
651 }
652 /* close read */
653 close(pipes[clients * 2]);
654
655 ++clients;
Michal Vasko734360b2016-02-08 15:38:25 +0100656#endif
657}
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200658
Claus Klein22091912020-01-20 13:45:47 +0100659#endif
Michal Vasko734360b2016-02-08 15:38:25 +0100660
661int
662main(void)
663{
Claus Klein22091912020-01-20 13:45:47 +0100664#if _POSIX_BARRIERS >= 200112L
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100665 int ret, i, clients = 0;
Michal Vasko734360b2016-02-08 15:38:25 +0100666 pthread_t tids[thread_count];
667
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100668 nc_verbosity(NC_VERB_VERBOSE);
669
670 client_fork();
Michal Vasko734360b2016-02-08 15:38:25 +0100671
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200672 ly_ctx_new(TESTS_DIR "/data/modules", 0, &ctx);
Michal Vasko64b31a22016-03-03 11:00:00 +0100673 nc_assert(ctx);
Michal Vasko77367452021-02-16 16:32:18 +0100674 ly_ctx_load_module(ctx, "ietf-netconf", NULL, NULL);
Frank Rimplercb9274f2019-03-13 16:40:34 +0000675
676 /* load some application models with deviations */
Michal Vasko77367452021-02-16 16:32:18 +0100677 nc_assert(ly_ctx_load_module(ctx, "module-a", NULL, NULL));
678 nc_assert(ly_ctx_load_module(ctx, "module-a-dv", NULL, NULL));
679 nc_assert(ly_ctx_load_module(ctx, "module-a-dv2", NULL, NULL));
Frank Rimplercb9274f2019-03-13 16:40:34 +0000680
Michal Vasko93224072021-11-09 12:14:28 +0100681 nc_server_init();
Michal Vasko734360b2016-02-08 15:38:25 +0100682
683 pthread_barrier_init(&barrier, NULL, thread_count);
684
Radek Krejci53691be2016-02-22 13:58:37 +0100685#ifdef NC_ENABLED_SSH
Michal Vasko4c1fb492017-01-30 14:31:07 +0100686 /* set callback */
687 nc_server_ssh_set_hostkey_clb(clb_hostkeys, NULL, NULL);
688
Michal Vasko734360b2016-02-08 15:38:25 +0100689 /* do first, so that client can connect on SSH */
Michal Vasko338f8472016-10-13 15:01:27 +0200690 ret = nc_server_add_endpt("main_ssh", NC_TI_LIBSSH);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200691 nc_assert(!ret);
Michal Vasko338f8472016-10-13 15:01:27 +0200692 ret = nc_server_endpt_set_address("main_ssh", "0.0.0.0");
Michal Vasko64b31a22016-03-03 11:00:00 +0100693 nc_assert(!ret);
Michal Vasko338f8472016-10-13 15:01:27 +0200694 ret = nc_server_endpt_set_port("main_ssh", 6001);
Michal Vasko64b31a22016-03-03 11:00:00 +0100695 nc_assert(!ret);
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200696 ret = nc_server_ssh_add_authkey_path(TESTS_DIR "/data/key_ecdsa.pub", "test");
Michal Vasko338f8472016-10-13 15:01:27 +0200697 nc_assert(!ret);
Michal Vasko7d255882017-02-09 13:35:08 +0100698 ret = nc_server_ssh_endpt_add_hostkey("main_ssh", "key_rsa", -1);
Michal Vasko64b31a22016-03-03 11:00:00 +0100699 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100700
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100701 /* client ready */
702 ret = write(pipes[clients * 2 + 1], "ssh_ready", 9);
Michal Vasko64b31a22016-03-03 11:00:00 +0100703 nc_assert(ret == 9);
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100704 ++clients;
705
Michal Vasko734360b2016-02-08 15:38:25 +0100706 /* for ssh_endpt_del_authkey */
Michal Vasko7a4213f2022-09-08 11:02:34 +0200707 ret = nc_server_ssh_add_authkey_path(TESTS_DIR "/data/key_ecdsa.pub", "test2");
Michal Vasko338f8472016-10-13 15:01:27 +0200708 nc_assert(!ret);
709
710 ret = nc_server_add_endpt("secondary", NC_TI_LIBSSH);
Michal Vasko64b31a22016-03-03 11:00:00 +0100711 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100712#endif
713
Radek Krejci53691be2016-02-22 13:58:37 +0100714#ifdef NC_ENABLED_TLS
Michal Vasko4c1fb492017-01-30 14:31:07 +0100715 /* set callbacks */
716 nc_server_tls_set_server_cert_clb(clb_server_cert, NULL, NULL);
717 nc_server_tls_set_trusted_cert_list_clb(clb_trusted_cert_lists, NULL, NULL);
718
Michal Vasko734360b2016-02-08 15:38:25 +0100719 /* do first, so that client can connect on TLS */
Michal Vasko338f8472016-10-13 15:01:27 +0200720 ret = nc_server_add_endpt("main_tls", NC_TI_OPENSSL);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200721 nc_assert(!ret);
Michal Vasko338f8472016-10-13 15:01:27 +0200722 ret = nc_server_endpt_set_address("main_tls", "0.0.0.0");
Michal Vasko64b31a22016-03-03 11:00:00 +0100723 nc_assert(!ret);
Michal Vasko338f8472016-10-13 15:01:27 +0200724 ret = nc_server_endpt_set_port("main_tls", 6501);
Michal Vasko64b31a22016-03-03 11:00:00 +0100725 nc_assert(!ret);
Michal Vasko4c1fb492017-01-30 14:31:07 +0100726 ret = nc_server_tls_endpt_set_server_cert("main_tls", "main_cert");
Michal Vasko64b31a22016-03-03 11:00:00 +0100727 nc_assert(!ret);
Michal Vasko4c1fb492017-01-30 14:31:07 +0100728 ret = nc_server_tls_endpt_add_trusted_cert_list("main_tls", "client_cert_list");
Michal Vasko338f8472016-10-13 15:01:27 +0200729 nc_assert(!ret);
Michal Vasko346ac562018-11-05 08:47:18 +0100730 ret = nc_server_tls_endpt_add_ctn("main_tls", 0, "02:B3:9F:26:65:76:6B:CC:FC:86:8E:D4:1A:81:64:0F:92:EB:18:AE:FF", NC_TLS_CTN_SPECIFIED, "test");
Michal Vasko64b31a22016-03-03 11:00:00 +0100731 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100732
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100733 /* client ready */
734 ret = write(pipes[clients * 2 + 1], "tls_ready", 9);
Michal Vasko64b31a22016-03-03 11:00:00 +0100735 nc_assert(ret == 9);
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100736 ++clients;
737
Michal Vasko734360b2016-02-08 15:38:25 +0100738 /* for tls_endpt_del_ctn */
Michal Vasko3cf4aaa2017-02-01 15:03:36 +0100739 ret = nc_server_tls_endpt_add_ctn("main_tls", 1, "02:00:11:22:33:44:55:66:77:88:99:AA:BB:CC:DD:EE:FF:A0:A1:A2:A3", NC_TLS_CTN_SAN_ANY, NULL);
Michal Vasko338f8472016-10-13 15:01:27 +0200740 nc_assert(!ret);
741
742 ret = nc_server_add_endpt("quaternary", NC_TI_OPENSSL);
Michal Vasko64b31a22016-03-03 11:00:00 +0100743 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100744#endif
745
Michal Vasko734360b2016-02-08 15:38:25 +0100746 /* threads'n'stuff */
747 ret = 0;
748 for (i = 0; i < thread_count; ++i) {
749 ret += pthread_create(&tids[i], NULL, thread_funcs[i], NULL);
750 }
Michal Vasko64b31a22016-03-03 11:00:00 +0100751 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100752
753 /* cleanup */
754 for (i = 0; i < thread_count; ++i) {
755 pthread_join(tids[i], NULL);
756 }
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100757 for (i = 0; i < client_count; ++i) {
758 waitpid(pids[i], NULL, 0);
759 close(pipes[i * 2 + 1]);
760 }
Michal Vasko734360b2016-02-08 15:38:25 +0100761
762 pthread_barrier_destroy(&barrier);
763
Michal Vasko734360b2016-02-08 15:38:25 +0100764 nc_server_destroy();
Michal Vasko1dcb7642021-04-14 15:28:23 +0200765 ly_ctx_destroy(ctx);
Claus Klein22091912020-01-20 13:45:47 +0100766#endif
Michal Vasko734360b2016-02-08 15:38:25 +0100767
Michal Vasko734360b2016-02-08 15:38:25 +0100768 return 0;
769}