blob: 15e5d4cfb3e8ac88a659bacdc0d9a8c962dfd28e [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
15#include <pthread.h>
Michal Vasko64b31a22016-03-03 11:00:00 +010016#include <stdio.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020017#include <stdlib.h>
Michal Vaskoa28be9a2016-03-01 15:20:47 +010018#include <string.h>
Michal Vasko734360b2016-02-08 15:38:25 +010019#include <sys/types.h>
20#include <sys/wait.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020021#include <unistd.h>
Michal Vasko734360b2016-02-08 15:38:25 +010022
23#include <libyang/libyang.h>
24
Michal Vaskob83a3fa2021-05-26 09:53:42 +020025#include <log.h>
Michal Vasko734360b2016-02-08 15:38:25 +010026#include <session_client.h>
27#include <session_server.h>
Jan Kundrátcf15d6c2017-10-26 18:07:56 +020028#include "tests/config.h"
Michal Vasko734360b2016-02-08 15:38:25 +010029
30/* millisec */
Michal Vasko15592cc2016-02-09 09:52:43 +010031#define NC_ACCEPT_TIMEOUT 5000
Michal Vasko734360b2016-02-08 15:38:25 +010032/* millisec */
Michal Vasko15592cc2016-02-09 09:52:43 +010033#define NC_PS_POLL_TIMEOUT 5000
Michal Vasko734360b2016-02-08 15:38:25 +010034/* sec */
Michal Vasko15592cc2016-02-09 09:52:43 +010035#define CLIENT_SSH_AUTH_TIMEOUT 10
Michal Vasko734360b2016-02-08 15:38:25 +010036
Michal Vasko64b31a22016-03-03 11:00:00 +010037#define nc_assert(cond) if (!(cond)) { fprintf(stderr, "assert failed (%s:%d)\n", __FILE__, __LINE__); exit(1); }
38
Claus Klein22091912020-01-20 13:45:47 +010039#if _POSIX_BARRIERS >= 200112L
Michal Vasko734360b2016-02-08 15:38:25 +010040pthread_barrier_t barrier;
41
Michal Vaskob83a3fa2021-05-26 09:53:42 +020042#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Michal Vasko734360b2016-02-08 15:38:25 +010043
44static void *
45server_thread(void *arg)
46{
47 (void)arg;
Michal Vasko71090fc2016-05-24 16:37:28 +020048 NC_MSG_TYPE msgtype;
Michal Vasko734360b2016-02-08 15:38:25 +010049 int ret;
50 struct nc_pollsession *ps;
51 struct nc_session *session;
52
53 ps = nc_ps_new();
Michal Vasko64b31a22016-03-03 11:00:00 +010054 nc_assert(ps);
Michal Vasko734360b2016-02-08 15:38:25 +010055
56 pthread_barrier_wait(&barrier);
57
Michal Vaskob83a3fa2021-05-26 09:53:42 +020058#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko71090fc2016-05-24 16:37:28 +020059 msgtype = nc_accept(NC_ACCEPT_TIMEOUT, &session);
60 nc_assert(msgtype == NC_MSG_HELLO);
Michal Vasko734360b2016-02-08 15:38:25 +010061
62 nc_ps_add_session(ps, session);
Michal Vasko71090fc2016-05-24 16:37:28 +020063 ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT, NULL);
64 nc_assert(ret & NC_PSPOLL_RPC);
Michal Vaskoe1a64ec2016-03-01 12:21:58 +010065 nc_ps_clear(ps, 0, NULL);
Michal Vasko734360b2016-02-08 15:38:25 +010066#endif
67
Michal Vasko71090fc2016-05-24 16:37:28 +020068 msgtype = nc_accept(NC_ACCEPT_TIMEOUT, &session);
69 nc_assert(msgtype == NC_MSG_HELLO);
Michal Vasko734360b2016-02-08 15:38:25 +010070
71 nc_ps_add_session(ps, session);
Michal Vasko71090fc2016-05-24 16:37:28 +020072 ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT, NULL);
73 nc_assert(ret & NC_PSPOLL_RPC);
Michal Vaskoe1a64ec2016-03-01 12:21:58 +010074 nc_ps_clear(ps, 0, NULL);
Michal Vasko734360b2016-02-08 15:38:25 +010075
76 nc_ps_free(ps);
77
78 nc_thread_destroy();
79 return NULL;
80}
81
Michal Vasko338f8472016-10-13 15:01:27 +020082#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
83
84#ifdef NC_ENABLED_SSH
85
Michal Vasko4c1fb492017-01-30 14:31:07 +010086static int
87clb_hostkeys(const char *name, void *UNUSED(user_data), char **privkey_path, char **UNUSED(privkey_data),
Michal Vaskob83a3fa2021-05-26 09:53:42 +020088 NC_SSH_KEY_TYPE *UNUSED(privkey_type))
Michal Vasko4c1fb492017-01-30 14:31:07 +010089{
90 if (!strcmp(name, "key_rsa")) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +020091 *privkey_path = strdup(TESTS_DIR "/data/key_rsa");
Michal Vasko4c1fb492017-01-30 14:31:07 +010092 return 0;
93 } else if (!strcmp(name, "key_dsa")) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +020094 *privkey_path = strdup(TESTS_DIR "/data/key_dsa");
Michal Vasko4c1fb492017-01-30 14:31:07 +010095 return 0;
96 }
97
98 return 1;
99}
100
Michal Vasko734360b2016-02-08 15:38:25 +0100101static void *
Michal Vaskoe2713da2016-08-22 16:06:40 +0200102add_endpt_thread(void *arg)
Michal Vasko734360b2016-02-08 15:38:25 +0100103{
104 (void)arg;
105 int ret;
106
107 pthread_barrier_wait(&barrier);
Michal Vasko338f8472016-10-13 15:01:27 +0200108 ret = nc_server_add_endpt("tertiary", NC_TI_LIBSSH);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200109 nc_assert(!ret);
110
111 return NULL;
112}
113
114static void *
115del_endpt_thread(void *arg)
116{
117 (void)arg;
118 int ret;
119
120 pthread_barrier_wait(&barrier);
121
Michal Vasko59050372016-11-22 14:33:55 +0100122 ret = nc_server_del_endpt("secondary", 0);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200123 nc_assert(!ret);
124
125 return NULL;
126}
127
Michal Vasko734360b2016-02-08 15:38:25 +0100128static void *
129ssh_endpt_set_hostkey_thread(void *arg)
130{
131 (void)arg;
132 int ret;
133
134 pthread_barrier_wait(&barrier);
135
Michal Vasko7d255882017-02-09 13:35:08 +0100136 ret = nc_server_ssh_endpt_add_hostkey("main_ssh", "key_dsa", -1);
Michal Vasko64b31a22016-03-03 11:00:00 +0100137 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100138
139 return NULL;
140}
141
142static void *
Michal Vasko734360b2016-02-08 15:38:25 +0100143ssh_endpt_set_auth_methods_thread(void *arg)
144{
145 (void)arg;
146 int ret;
147
148 pthread_barrier_wait(&barrier);
149
Michal Vasko338f8472016-10-13 15:01:27 +0200150 ret = nc_server_ssh_endpt_set_auth_methods("main_ssh", NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD | NC_SSH_AUTH_INTERACTIVE);
Michal Vasko64b31a22016-03-03 11:00:00 +0100151 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100152
153 return NULL;
154}
155
156static void *
157ssh_endpt_set_auth_attempts_thread(void *arg)
158{
159 (void)arg;
160 int ret;
161
162 pthread_barrier_wait(&barrier);
163
Michal Vasko338f8472016-10-13 15:01:27 +0200164 ret = nc_server_ssh_endpt_set_auth_attempts("main_ssh", 2);
Michal Vasko64b31a22016-03-03 11:00:00 +0100165 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100166
167 return NULL;
168}
169
170static void *
171ssh_endpt_set_auth_timeout_thread(void *arg)
172{
173 (void)arg;
174 int ret;
175
176 pthread_barrier_wait(&barrier);
177
Michal Vasko338f8472016-10-13 15:01:27 +0200178 ret = nc_server_ssh_endpt_set_auth_timeout("main_ssh", 5);
Michal Vasko64b31a22016-03-03 11:00:00 +0100179 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100180
181 return NULL;
182}
183
184static void *
185ssh_endpt_add_authkey_thread(void *arg)
186{
187 (void)arg;
188 int ret;
189
190 pthread_barrier_wait(&barrier);
191
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200192 ret = nc_server_ssh_add_authkey_path(TESTS_DIR "/data/key_rsa.pub", "test3");
Michal Vasko64b31a22016-03-03 11:00:00 +0100193 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100194
195 return NULL;
196}
197
198static void *
199ssh_endpt_del_authkey_thread(void *arg)
200{
201 (void)arg;
202 int ret;
203
204 pthread_barrier_wait(&barrier);
205
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200206 ret = nc_server_ssh_del_authkey(TESTS_DIR "/data/key_dsa.pub", NULL, 0, "test2");
Michal Vasko64b31a22016-03-03 11:00:00 +0100207 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100208
209 return NULL;
210}
211
Michal Vasko693eb112016-02-18 13:28:49 +0100212static int
Radek Krejci90a84a22017-05-25 13:53:00 +0200213ssh_hostkey_check_clb(const char *hostname, ssh_session session, void *priv)
Michal Vasko693eb112016-02-18 13:28:49 +0100214{
215 (void)hostname;
216 (void)session;
Radek Krejci90a84a22017-05-25 13:53:00 +0200217 (void)priv;
Michal Vasko693eb112016-02-18 13:28:49 +0100218
219 return 0;
220}
221
Michal Vasko734360b2016-02-08 15:38:25 +0100222static void *
223ssh_client_thread(void *arg)
224{
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100225 int ret, read_pipe = *(int *)arg;
226 char buf[9];
Michal Vasko734360b2016-02-08 15:38:25 +0100227 struct nc_session *session;
228
Michal Vasko5a446472020-05-06 12:43:42 +0200229 fprintf(stdout, "SSH client start.\n");
230
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100231 ret = read(read_pipe, buf, 9);
Michal Vasko64b31a22016-03-03 11:00:00 +0100232 nc_assert(ret == 9);
233 nc_assert(!strncmp(buf, "ssh_ready", 9));
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100234
Michal Vasko693eb112016-02-18 13:28:49 +0100235 /* skip the knownhost check */
Radek Krejci90a84a22017-05-25 13:53:00 +0200236 nc_client_ssh_set_auth_hostkey_check_clb(ssh_hostkey_check_clb, NULL);
Michal Vasko734360b2016-02-08 15:38:25 +0100237
Michal Vasko693eb112016-02-18 13:28:49 +0100238 ret = nc_client_ssh_set_username("test");
Michal Vasko64b31a22016-03-03 11:00:00 +0100239 nc_assert(!ret);
Michal Vasko693eb112016-02-18 13:28:49 +0100240
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200241 ret = nc_client_ssh_add_keypair(TESTS_DIR "/data/key_ecdsa.pub", TESTS_DIR "/data/key_ecdsa");
Michal Vasko64b31a22016-03-03 11:00:00 +0100242 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100243
244 nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PUBLICKEY, 1);
245 nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PASSWORD, -1);
246 nc_client_ssh_set_auth_pref(NC_SSH_AUTH_INTERACTIVE, -1);
247
Michal Vasko693eb112016-02-18 13:28:49 +0100248 session = nc_connect_ssh("127.0.0.1", 6001, NULL);
Michal Vasko64b31a22016-03-03 11:00:00 +0100249 nc_assert(session);
Michal Vasko734360b2016-02-08 15:38:25 +0100250
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100251 nc_session_free(session, NULL);
Michal Vasko734360b2016-02-08 15:38:25 +0100252
Michal Vaskoe2713da2016-08-22 16:06:40 +0200253 fprintf(stdout, "SSH client finished.\n");
254
Michal Vasko734360b2016-02-08 15:38:25 +0100255 nc_thread_destroy();
256 return NULL;
257}
258
Radek Krejci53691be2016-02-22 13:58:37 +0100259#endif /* NC_ENABLED_SSH */
Michal Vasko734360b2016-02-08 15:38:25 +0100260
Radek Krejci53691be2016-02-22 13:58:37 +0100261#ifdef NC_ENABLED_TLS
Michal Vasko734360b2016-02-08 15:38:25 +0100262
Michal Vasko4c1fb492017-01-30 14:31:07 +0100263static int
264clb_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 +0200265 char **privkey_data, NC_SSH_KEY_TYPE *privkey_type)
Michal Vasko4c1fb492017-01-30 14:31:07 +0100266{
267 if (!strcmp(name, "server_cert1")) {
Michal Vasko346ac562018-11-05 08:47:18 +0100268 *cert_data = strdup("MIIEQDCCAygCCQCV65JgDvfWkTANBgkqhkiG9w0BAQsFADBjMQswCQYDVQQGEwJD\n"
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200269 "WjETMBEGA1UECAwKU29tZS1TdGF0ZTENMAsGA1UEBwwEQnJubzEPMA0GA1UECgwG\n"
270 "Q0VTTkVUMQwwCgYDVQQLDANUTUMxETAPBgNVBAMMCHNlcnZlcmNhMB4XDTE4MTEw\n"
271 "NTA3MzExMFoXDTI4MTEwMjA3MzExMFowYTELMAkGA1UEBhMCQ1oxEzARBgNVBAgM\n"
272 "ClNvbWUtU3RhdGUxDTALBgNVBAcMBEJybm8xDzANBgNVBAoMBkNFU05FVDEMMAoG\n"
273 "A1UECwwDVE1DMQ8wDQYDVQQDDAZzZXJ2ZXIwggIiMA0GCSqGSIb3DQEBAQUAA4IC\n"
274 "DwAwggIKAoICAQDqiO2N8Oa/JA/VmQjAGmv3t4oO55u+miCVEdF29W5Ol/+BTVUC\n"
275 "sBCbAaHTmLqWbxORWXWegyUjEskNxayVp5WforK+xfQeGxC1fCo+rCRrlQK/pqXB\n"
276 "/+K8C9w2lxfWPS3+4gYIjh1KIqfNALJ/QVOKvNCSOsNlR3eZ4OE1BOu4JqUZXiB8\n"
277 "1Yird7Wga7ACfW0uW72hOsTgfMymBs0RTrA2axKqnAbFSFguhJoztvR0uao/5lzN\n"
278 "JLRRpzQ8U2R6EZDYJghPzR/nzSjhca2gsJRQaSnpgJMvhnYJ4ERokAFaMMgMf50p\n"
279 "ghQGpSnPhOHXaBcA/6H7eojr716ml4et+vMBEOx8uPBQ3FAmx7VBICuMDK1/QUq0\n"
280 "Yes5FztbROIIW9HTNqhQ0tMqTt6dOJFD2t9Zk4C7jh9S88JpcMTrNdd5pWCgoKjh\n"
281 "UeWGjfp6tRyOUQEz6OTbwjKRtka0FvqKAq9hrW09KB5ib/MmgVWNByevXi5yL9+3\n"
282 "X41r6E4gSgQSIrFwA+TZva8Eo2VEhbtYsne7tmK5AQlM2br7dwTj6V/BoEIGoFOg\n"
283 "T52nO+hRegzIUVF5QV3U7lrG7h9eC3TSMxo5DGYTS/06ZH+kv89Yh+VUXhnPnJU4\n"
284 "1hoNVzuX695jSgRmu4Q8nSGUSDG4LMutwyGO5+XaoTZDt9Ahpq//U2oFWQIDAQAB\n"
285 "MA0GCSqGSIb3DQEBCwUAA4IBAQAXWHf/MG8RPCyA0rC3RwxmM70ndyKPIJoL4ggU\n"
286 "VgkN66BdpsE4UlWdlp0XL3aauMPxzLn9rq1yRtoHWT4/ucL9iEa6B295JBNjkgW+\n"
287 "ct9/y8060P9BUhY1DTv5DLzitsA4bjRaraIevjATDPfsbHFx9DTNrS5pXHIFbRcz\n"
288 "y3WniYXTKhpfM6m+1X8ogImE968DG8RqAW5YZZtrZW0VF/dhlQp20jEX/8Rv33Bp\n"
289 "RhNEIhPnYAquKCesMMclUtPW+5n2z8rgj5t/ETv4wc5QegpyPfdHNq09bGKB10Sy\n"
290 "sGvC6hP9GKU3R2Jhxih/t88O3WoisFQ8+Tf9s2LuSxUV0bzp");
Michal Vasko4c1fb492017-01-30 14:31:07 +0100291 *privkey_data = strdup("MIIJKAIBAAKCAgEA6ojtjfDmvyQP1ZkIwBpr97eKDuebvpoglRHRdvVuTpf/gU1V\n"
Michal Vaskob83a3fa2021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"dOZPvkfqyIKFmbQgYbtD66rHuzNOfJpzqr/WVLO57/zzW8245NKG2B6B0oXkei/K\n"
338 "qDY0DAbHR3i3EOj1NPtVI1FC/xX8R9BREaid458bqoHJKuInrGcBjaUI9Cvymv8T\n"
339 "bstUgD6NPbJR4Sm6vrLeUqzjWZP3t1+Z6DjXmnpR2vvhMU/FWb//21p/88o=");
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200340 *privkey_type = NC_SSH_KEY_RSA;
Michal Vasko4c1fb492017-01-30 14:31:07 +0100341 return 0;
342 } else if (!strcmp(name, "main_cert")) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200343 *cert_path = strdup(TESTS_DIR "/data/server.crt");
344 *privkey_path = strdup(TESTS_DIR "/data/server.key");
Michal Vasko4c1fb492017-01-30 14:31:07 +0100345 return 0;
346 }
347
348 return 1;
349}
350
351static int
352clb_trusted_cert_lists(const char *name, void *UNUSED(user_data), char ***cert_paths, int *cert_path_count,
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200353 char ***cert_data, int *cert_data_count)
Michal Vasko4c1fb492017-01-30 14:31:07 +0100354{
355 if (!strcmp(name, "trusted_cert_list1")) {
Michal Vaskoad0bcb52017-03-22 11:55:47 +0100356 *cert_data = malloc(sizeof **cert_data);
Michal Vasko346ac562018-11-05 08:47:18 +0100357 (*cert_data)[0] = strdup("MIIDnDCCAoSgAwIBAgIJAIjf7UNx4uabMA0GCSqGSIb3DQEBCwUAMGMxCzAJBgNV\n"
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200358 "BAYTAkNaMRMwEQYDVQQIDApTb21lLVN0YXRlMQ0wCwYDVQQHDARCcm5vMQ8wDQYD\n"
359 "VQQKDAZDRVNORVQxDDAKBgNVBAsMA1RNQzERMA8GA1UEAwwIc2VydmVyY2EwHhcN\n"
360 "MTgxMTA1MDcyNjM5WhcNMjgxMTAyMDcyNjM5WjBjMQswCQYDVQQGEwJDWjETMBEG\n"
361 "A1UECAwKU29tZS1TdG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==");
Michal Vasko4c1fb492017-01-30 14:31:07 +0100377 *cert_data_count = 1;
378 return 0;
379 } else if (!strcmp(name, "client_cert_list")) {
Michal Vasko75ff74b2017-03-21 15:34:34 +0100380 *cert_paths = malloc(sizeof **cert_paths);
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200381 (*cert_paths)[0] = strdup(TESTS_DIR "/data/client.crt");
Michal Vasko4c1fb492017-01-30 14:31:07 +0100382 *cert_path_count = 1;
383 return 0;
384 }
385
386 return 1;
387}
388
Michal Vasko734360b2016-02-08 15:38:25 +0100389static void *
Michal Vasko338f8472016-10-13 15:01:27 +0200390endpt_set_address_thread(void *arg)
Michal Vasko734360b2016-02-08 15:38:25 +0100391{
392 (void)arg;
393 int ret;
394
395 pthread_barrier_wait(&barrier);
396
Michal Vasko338f8472016-10-13 15:01:27 +0200397 ret = nc_server_endpt_set_address("quaternary", "0.0.0.0");
Michal Vasko64b31a22016-03-03 11:00:00 +0100398 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100399
400 return NULL;
401}
402
403static void *
Michal Vasko338f8472016-10-13 15:01:27 +0200404endpt_set_port_thread(void *arg)
Michal Vasko734360b2016-02-08 15:38:25 +0100405{
406 (void)arg;
407 int ret;
408
409 pthread_barrier_wait(&barrier);
410
Michal Vasko338f8472016-10-13 15:01:27 +0200411 ret = nc_server_endpt_set_port("quaternary", 6003);
Michal Vasko64b31a22016-03-03 11:00:00 +0100412 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100413
414 return NULL;
415}
416
417static void *
Michal Vasko4c1fb492017-01-30 14:31:07 +0100418tls_endpt_set_server_cert_thread(void *arg)
Michal Vasko734360b2016-02-08 15:38:25 +0100419{
420 (void)arg;
421 int ret;
422
423 pthread_barrier_wait(&barrier);
424
Michal Vasko4c1fb492017-01-30 14:31:07 +0100425 ret = nc_server_tls_endpt_set_server_cert("quaternary", "server_cert1");
Michal Vasko64b31a22016-03-03 11:00:00 +0100426 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100427
428 nc_thread_destroy();
429 return NULL;
430}
431
432static void *
Michal Vasko4c1fb492017-01-30 14:31:07 +0100433tls_endpt_add_trusted_cert_list_thread(void *arg)
Michal Vasko734360b2016-02-08 15:38:25 +0100434{
435 (void)arg;
436 int ret;
437
438 pthread_barrier_wait(&barrier);
439
Michal Vasko4c1fb492017-01-30 14:31:07 +0100440 ret = nc_server_tls_endpt_add_trusted_cert_list("quaternary", "trusted_cert_list1");
Michal Vasko64b31a22016-03-03 11:00:00 +0100441 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100442
443 nc_thread_destroy();
444 return NULL;
445}
446
447static void *
448tls_endpt_set_trusted_ca_paths_thread(void *arg)
449{
450 (void)arg;
451 int ret;
452
453 pthread_barrier_wait(&barrier);
454
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200455 ret = nc_server_tls_endpt_set_trusted_ca_paths("quaternary", TESTS_DIR "/data/serverca.pem", "data");
Michal Vasko64b31a22016-03-03 11:00:00 +0100456 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100457
458 nc_thread_destroy();
459 return NULL;
460}
461
462static void *
Michal Vasko4c1fb492017-01-30 14:31:07 +0100463tls_endpt_del_trusted_cert_list_thread(void *arg)
Michal Vasko734360b2016-02-08 15:38:25 +0100464{
465 (void)arg;
466
467 pthread_barrier_wait(&barrier);
468
Michal Vasko4c1fb492017-01-30 14:31:07 +0100469 nc_server_tls_endpt_del_trusted_cert_list("quaternary", "trusted_cert_list1");
Michal Vasko734360b2016-02-08 15:38:25 +0100470
471 return NULL;
472}
473
474static void *
475tls_endpt_set_crl_paths_thread(void *arg)
476{
477 (void)arg;
478 int ret;
479
480 pthread_barrier_wait(&barrier);
481
482 ret = nc_server_tls_endpt_set_crl_paths("quaternary", NULL, "data");
Michal Vasko64b31a22016-03-03 11:00:00 +0100483 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100484
485 nc_thread_destroy();
486 return NULL;
487}
488
489static void *
490tls_endpt_clear_crls_thread(void *arg)
491{
492 (void)arg;
493
494 pthread_barrier_wait(&barrier);
495
496 nc_server_tls_endpt_clear_crls("quaternary");
497
498 return NULL;
499}
500
501static void *
502tls_endpt_add_ctn_thread(void *arg)
503{
504 (void)arg;
505 int ret;
506
507 pthread_barrier_wait(&barrier);
508
Michal Vasko3cf4aaa2017-02-01 15:03:36 +0100509 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 +0200510 NC_TLS_CTN_SAN_IP_ADDRESS, NULL);
Michal Vasko64b31a22016-03-03 11:00:00 +0100511 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100512
513 return NULL;
514}
515
516static void *
517tls_endpt_del_ctn_thread(void *arg)
518{
519 (void)arg;
520 int ret;
521
522 pthread_barrier_wait(&barrier);
523
Michal Vasko338f8472016-10-13 15:01:27 +0200524 ret = nc_server_tls_endpt_del_ctn("main_tls", -1, NULL, NC_TLS_CTN_SAN_ANY, NULL);
Michal Vasko64b31a22016-03-03 11:00:00 +0100525 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100526
527 return NULL;
528}
529
530static void *
531tls_client_thread(void *arg)
532{
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100533 int ret, read_pipe = *(int *)arg;
534 char buf[9];
Michal Vasko734360b2016-02-08 15:38:25 +0100535 struct nc_session *session;
536
Michal Vasko5a446472020-05-06 12:43:42 +0200537 fprintf(stdout, "TLS client start.\n");
538
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100539 ret = read(read_pipe, buf, 9);
Michal Vasko64b31a22016-03-03 11:00:00 +0100540 nc_assert(ret == 9);
541 nc_assert(!strncmp(buf, "tls_ready", 9));
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100542
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200543 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 +0100544 nc_assert(!ret);
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200545 ret = nc_client_tls_set_trusted_ca_paths(NULL, TESTS_DIR "/data");
Michal Vasko64b31a22016-03-03 11:00:00 +0100546 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100547
548 session = nc_connect_tls("127.0.0.1", 6501, NULL);
Michal Vasko64b31a22016-03-03 11:00:00 +0100549 nc_assert(session);
Michal Vasko734360b2016-02-08 15:38:25 +0100550
Frank Rimplercb9274f2019-03-13 16:40:34 +0000551 /* verify some capabilities */
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200552 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 +0000553
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100554 nc_session_free(session, NULL);
Michal Vasko734360b2016-02-08 15:38:25 +0100555
Michal Vaskoe2713da2016-08-22 16:06:40 +0200556 fprintf(stdout, "TLS client finished.\n");
557
Michal Vasko734360b2016-02-08 15:38:25 +0100558 nc_thread_destroy();
559 return NULL;
560}
561
Radek Krejci53691be2016-02-22 13:58:37 +0100562#endif /* NC_ENABLED_TLS */
Michal Vasko734360b2016-02-08 15:38:25 +0100563
564static void *(*thread_funcs[])(void *) = {
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200565#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Michal Vasko734360b2016-02-08 15:38:25 +0100566 server_thread,
567#endif
Radek Krejci53691be2016-02-22 13:58:37 +0100568#ifdef NC_ENABLED_SSH
Michal Vasko8727e432016-10-13 15:44:10 +0200569 add_endpt_thread,
570 del_endpt_thread,
Michal Vasko734360b2016-02-08 15:38:25 +0100571 ssh_endpt_set_hostkey_thread,
Michal Vasko734360b2016-02-08 15:38:25 +0100572 ssh_endpt_set_auth_methods_thread,
573 ssh_endpt_set_auth_attempts_thread,
574 ssh_endpt_set_auth_timeout_thread,
575 ssh_endpt_add_authkey_thread,
576 ssh_endpt_del_authkey_thread,
577#endif
Radek Krejci53691be2016-02-22 13:58:37 +0100578#ifdef NC_ENABLED_TLS
Michal Vasko8727e432016-10-13 15:44:10 +0200579 endpt_set_address_thread,
580 endpt_set_port_thread,
Michal Vasko4c1fb492017-01-30 14:31:07 +0100581 tls_endpt_set_server_cert_thread,
582 tls_endpt_add_trusted_cert_list_thread,
Michal Vasko734360b2016-02-08 15:38:25 +0100583 tls_endpt_set_trusted_ca_paths_thread,
Michal Vasko4c1fb492017-01-30 14:31:07 +0100584 tls_endpt_del_trusted_cert_list_thread,
Michal Vasko734360b2016-02-08 15:38:25 +0100585 tls_endpt_set_crl_paths_thread,
586 tls_endpt_clear_crls_thread,
587 tls_endpt_add_ctn_thread,
588 tls_endpt_del_ctn_thread,
589#endif
590};
591
592const int thread_count = sizeof thread_funcs / sizeof *thread_funcs;
593
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200594#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100595const int client_count = 2;
596pid_t pids[2];
597int pipes[4];
Michal Vasko734360b2016-02-08 15:38:25 +0100598#else
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100599const int client_count = 1;
600pid_t pids[1];
601int pipes[2];
602#endif
603
604static void
605client_fork(void)
606{
607 int ret, clients = 0;
608
609#ifdef NC_ENABLED_SSH
610 pipe(pipes + clients * 2);
611
612 if (!(pids[clients] = fork())) {
613 nc_client_init();
614
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200615 ret = nc_client_set_schema_searchpath(TESTS_DIR "/data/modules");
Michal Vasko64b31a22016-03-03 11:00:00 +0100616 nc_assert(!ret);
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100617
618 /* close write */
619 close(pipes[clients * 2 + 1]);
620 ssh_client_thread(&pipes[clients * 2]);
621 close(pipes[clients * 2]);
622 nc_client_destroy();
623 exit(0);
Michal Vasko734360b2016-02-08 15:38:25 +0100624 }
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100625 /* close read */
626 close(pipes[clients * 2]);
627
628 ++clients;
629#endif
630
631#ifdef NC_ENABLED_TLS
632 pipe(pipes + clients * 2);
633
634 if (!(pids[clients] = fork())) {
635 nc_client_init();
636
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200637 ret = nc_client_set_schema_searchpath(TESTS_DIR "/data/modules");
Michal Vasko64b31a22016-03-03 11:00:00 +0100638 nc_assert(!ret);
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100639
640 /* close write */
641 close(pipes[clients * 2 + 1]);
642 tls_client_thread(&pipes[clients * 2]);
643 close(pipes[clients * 2]);
644 nc_client_destroy();
645 exit(0);
646 }
647 /* close read */
648 close(pipes[clients * 2]);
649
650 ++clients;
Michal Vasko734360b2016-02-08 15:38:25 +0100651#endif
652}
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200653
Claus Klein22091912020-01-20 13:45:47 +0100654#endif
Michal Vasko734360b2016-02-08 15:38:25 +0100655
656int
657main(void)
658{
Claus Klein22091912020-01-20 13:45:47 +0100659#if _POSIX_BARRIERS >= 200112L
Michal Vasko734360b2016-02-08 15:38:25 +0100660 struct ly_ctx *ctx;
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100661 int ret, i, clients = 0;
Michal Vasko734360b2016-02-08 15:38:25 +0100662 pthread_t tids[thread_count];
663
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100664 nc_verbosity(NC_VERB_VERBOSE);
665
666 client_fork();
Michal Vasko734360b2016-02-08 15:38:25 +0100667
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200668 ly_ctx_new(TESTS_DIR "/data/modules", 0, &ctx);
Michal Vasko64b31a22016-03-03 11:00:00 +0100669 nc_assert(ctx);
Michal Vasko77367452021-02-16 16:32:18 +0100670 ly_ctx_load_module(ctx, "ietf-netconf", NULL, NULL);
Frank Rimplercb9274f2019-03-13 16:40:34 +0000671
672 /* load some application models with deviations */
Michal Vasko77367452021-02-16 16:32:18 +0100673 nc_assert(ly_ctx_load_module(ctx, "module-a", NULL, NULL));
674 nc_assert(ly_ctx_load_module(ctx, "module-a-dv", NULL, NULL));
675 nc_assert(ly_ctx_load_module(ctx, "module-a-dv2", NULL, NULL));
Frank Rimplercb9274f2019-03-13 16:40:34 +0000676
Michal Vasko734360b2016-02-08 15:38:25 +0100677 nc_server_init(ctx);
678
679 pthread_barrier_init(&barrier, NULL, thread_count);
680
Radek Krejci53691be2016-02-22 13:58:37 +0100681#ifdef NC_ENABLED_SSH
Michal Vasko4c1fb492017-01-30 14:31:07 +0100682 /* set callback */
683 nc_server_ssh_set_hostkey_clb(clb_hostkeys, NULL, NULL);
684
Michal Vasko734360b2016-02-08 15:38:25 +0100685 /* do first, so that client can connect on SSH */
Michal Vasko338f8472016-10-13 15:01:27 +0200686 ret = nc_server_add_endpt("main_ssh", NC_TI_LIBSSH);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200687 nc_assert(!ret);
Michal Vasko338f8472016-10-13 15:01:27 +0200688 ret = nc_server_endpt_set_address("main_ssh", "0.0.0.0");
Michal Vasko64b31a22016-03-03 11:00:00 +0100689 nc_assert(!ret);
Michal Vasko338f8472016-10-13 15:01:27 +0200690 ret = nc_server_endpt_set_port("main_ssh", 6001);
Michal Vasko64b31a22016-03-03 11:00:00 +0100691 nc_assert(!ret);
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200692 ret = nc_server_ssh_add_authkey_path(TESTS_DIR "/data/key_ecdsa.pub", "test");
Michal Vasko338f8472016-10-13 15:01:27 +0200693 nc_assert(!ret);
Michal Vasko7d255882017-02-09 13:35:08 +0100694 ret = nc_server_ssh_endpt_add_hostkey("main_ssh", "key_rsa", -1);
Michal Vasko64b31a22016-03-03 11:00:00 +0100695 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100696
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100697 /* client ready */
698 ret = write(pipes[clients * 2 + 1], "ssh_ready", 9);
Michal Vasko64b31a22016-03-03 11:00:00 +0100699 nc_assert(ret == 9);
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100700 ++clients;
701
Michal Vasko734360b2016-02-08 15:38:25 +0100702 /* for ssh_endpt_del_authkey */
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200703 ret = nc_server_ssh_add_authkey_path(TESTS_DIR "/data/key_dsa.pub", "test2");
Michal Vasko338f8472016-10-13 15:01:27 +0200704 nc_assert(!ret);
705
706 ret = nc_server_add_endpt("secondary", NC_TI_LIBSSH);
Michal Vasko64b31a22016-03-03 11:00:00 +0100707 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100708#endif
709
Radek Krejci53691be2016-02-22 13:58:37 +0100710#ifdef NC_ENABLED_TLS
Michal Vasko4c1fb492017-01-30 14:31:07 +0100711 /* set callbacks */
712 nc_server_tls_set_server_cert_clb(clb_server_cert, NULL, NULL);
713 nc_server_tls_set_trusted_cert_list_clb(clb_trusted_cert_lists, NULL, NULL);
714
Michal Vasko734360b2016-02-08 15:38:25 +0100715 /* do first, so that client can connect on TLS */
Michal Vasko338f8472016-10-13 15:01:27 +0200716 ret = nc_server_add_endpt("main_tls", NC_TI_OPENSSL);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200717 nc_assert(!ret);
Michal Vasko338f8472016-10-13 15:01:27 +0200718 ret = nc_server_endpt_set_address("main_tls", "0.0.0.0");
Michal Vasko64b31a22016-03-03 11:00:00 +0100719 nc_assert(!ret);
Michal Vasko338f8472016-10-13 15:01:27 +0200720 ret = nc_server_endpt_set_port("main_tls", 6501);
Michal Vasko64b31a22016-03-03 11:00:00 +0100721 nc_assert(!ret);
Michal Vasko4c1fb492017-01-30 14:31:07 +0100722 ret = nc_server_tls_endpt_set_server_cert("main_tls", "main_cert");
Michal Vasko64b31a22016-03-03 11:00:00 +0100723 nc_assert(!ret);
Michal Vasko4c1fb492017-01-30 14:31:07 +0100724 ret = nc_server_tls_endpt_add_trusted_cert_list("main_tls", "client_cert_list");
Michal Vasko338f8472016-10-13 15:01:27 +0200725 nc_assert(!ret);
Michal Vasko346ac562018-11-05 08:47:18 +0100726 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 +0100727 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100728
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100729 /* client ready */
730 ret = write(pipes[clients * 2 + 1], "tls_ready", 9);
Michal Vasko64b31a22016-03-03 11:00:00 +0100731 nc_assert(ret == 9);
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100732 ++clients;
733
Michal Vasko734360b2016-02-08 15:38:25 +0100734 /* for tls_endpt_del_ctn */
Michal Vasko3cf4aaa2017-02-01 15:03:36 +0100735 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 +0200736 nc_assert(!ret);
737
738 ret = nc_server_add_endpt("quaternary", NC_TI_OPENSSL);
Michal Vasko64b31a22016-03-03 11:00:00 +0100739 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100740#endif
741
Michal Vasko734360b2016-02-08 15:38:25 +0100742 /* threads'n'stuff */
743 ret = 0;
744 for (i = 0; i < thread_count; ++i) {
745 ret += pthread_create(&tids[i], NULL, thread_funcs[i], NULL);
746 }
Michal Vasko64b31a22016-03-03 11:00:00 +0100747 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100748
749 /* cleanup */
750 for (i = 0; i < thread_count; ++i) {
751 pthread_join(tids[i], NULL);
752 }
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100753 for (i = 0; i < client_count; ++i) {
754 waitpid(pids[i], NULL, 0);
755 close(pipes[i * 2 + 1]);
756 }
Michal Vasko734360b2016-02-08 15:38:25 +0100757
758 pthread_barrier_destroy(&barrier);
759
Michal Vasko734360b2016-02-08 15:38:25 +0100760 nc_server_destroy();
Michal Vasko1dcb7642021-04-14 15:28:23 +0200761 ly_ctx_destroy(ctx);
Claus Klein22091912020-01-20 13:45:47 +0100762#endif
Michal Vasko734360b2016-02-08 15:38:25 +0100763
Michal Vasko734360b2016-02-08 15:38:25 +0100764 return 0;
765}