blob: 0bc3b9571774cdad0d24bbc2ebc070ef1c9a1b49 [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
Michal Vasko93224072021-11-09 12:14:28 +010044struct ly_ctx *ctx;
45
Michal Vasko734360b2016-02-08 15:38:25 +010046static void *
47server_thread(void *arg)
48{
49 (void)arg;
Michal Vasko71090fc2016-05-24 16:37:28 +020050 NC_MSG_TYPE msgtype;
Michal Vasko734360b2016-02-08 15:38:25 +010051 int ret;
52 struct nc_pollsession *ps;
53 struct nc_session *session;
54
55 ps = nc_ps_new();
Michal Vasko64b31a22016-03-03 11:00:00 +010056 nc_assert(ps);
Michal Vasko734360b2016-02-08 15:38:25 +010057
58 pthread_barrier_wait(&barrier);
59
Michal Vaskob83a3fa2021-05-26 09:53:42 +020060#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vasko93224072021-11-09 12:14:28 +010061 msgtype = nc_accept(NC_ACCEPT_TIMEOUT, ctx, &session);
Michal Vasko71090fc2016-05-24 16:37:28 +020062 nc_assert(msgtype == NC_MSG_HELLO);
Michal Vasko734360b2016-02-08 15:38:25 +010063
64 nc_ps_add_session(ps, session);
Michal Vasko71090fc2016-05-24 16:37:28 +020065 ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT, NULL);
66 nc_assert(ret & NC_PSPOLL_RPC);
Michal Vaskoe1a64ec2016-03-01 12:21:58 +010067 nc_ps_clear(ps, 0, NULL);
Michal Vasko734360b2016-02-08 15:38:25 +010068#endif
69
Michal Vasko93224072021-11-09 12:14:28 +010070 msgtype = nc_accept(NC_ACCEPT_TIMEOUT, ctx, &session);
Michal Vasko71090fc2016-05-24 16:37:28 +020071 nc_assert(msgtype == NC_MSG_HELLO);
Michal Vasko734360b2016-02-08 15:38:25 +010072
73 nc_ps_add_session(ps, session);
Michal Vasko71090fc2016-05-24 16:37:28 +020074 ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT, NULL);
75 nc_assert(ret & NC_PSPOLL_RPC);
Michal Vaskoe1a64ec2016-03-01 12:21:58 +010076 nc_ps_clear(ps, 0, NULL);
Michal Vasko734360b2016-02-08 15:38:25 +010077
78 nc_ps_free(ps);
79
80 nc_thread_destroy();
81 return NULL;
82}
83
Michal Vasko338f8472016-10-13 15:01:27 +020084#endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */
85
86#ifdef NC_ENABLED_SSH
87
Michal Vasko4c1fb492017-01-30 14:31:07 +010088static int
89clb_hostkeys(const char *name, void *UNUSED(user_data), char **privkey_path, char **UNUSED(privkey_data),
Michal Vaskob83a3fa2021-05-26 09:53:42 +020090 NC_SSH_KEY_TYPE *UNUSED(privkey_type))
Michal Vasko4c1fb492017-01-30 14:31:07 +010091{
92 if (!strcmp(name, "key_rsa")) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +020093 *privkey_path = strdup(TESTS_DIR "/data/key_rsa");
Michal Vasko4c1fb492017-01-30 14:31:07 +010094 return 0;
Michal Vasko7a4213f2022-09-08 11:02:34 +020095 } else if (!strcmp(name, "key_ecdsa")) {
96 *privkey_path = strdup(TESTS_DIR "/data/key_ecdsa");
Michal Vasko4c1fb492017-01-30 14:31:07 +010097 return 0;
98 }
99
100 return 1;
101}
102
Michal Vasko734360b2016-02-08 15:38:25 +0100103static void *
Michal Vaskoe2713da2016-08-22 16:06:40 +0200104add_endpt_thread(void *arg)
Michal Vasko734360b2016-02-08 15:38:25 +0100105{
106 (void)arg;
107 int ret;
108
109 pthread_barrier_wait(&barrier);
Michal Vasko338f8472016-10-13 15:01:27 +0200110 ret = nc_server_add_endpt("tertiary", NC_TI_LIBSSH);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200111 nc_assert(!ret);
112
113 return NULL;
114}
115
116static void *
117del_endpt_thread(void *arg)
118{
119 (void)arg;
120 int ret;
121
122 pthread_barrier_wait(&barrier);
123
Michal Vasko59050372016-11-22 14:33:55 +0100124 ret = nc_server_del_endpt("secondary", 0);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200125 nc_assert(!ret);
126
127 return NULL;
128}
129
Michal Vasko734360b2016-02-08 15:38:25 +0100130static void *
131ssh_endpt_set_hostkey_thread(void *arg)
132{
133 (void)arg;
134 int ret;
135
136 pthread_barrier_wait(&barrier);
137
Michal Vasko7a4213f2022-09-08 11:02:34 +0200138 ret = nc_server_ssh_endpt_add_hostkey("main_ssh", "key_ecdsa", -1);
Michal Vasko64b31a22016-03-03 11:00:00 +0100139 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100140
141 return NULL;
142}
143
144static void *
Michal Vasko734360b2016-02-08 15:38:25 +0100145ssh_endpt_set_auth_methods_thread(void *arg)
146{
147 (void)arg;
148 int ret;
149
150 pthread_barrier_wait(&barrier);
151
roman41a11e42022-06-22 09:27:08 +0200152 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 +0100153 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100154
155 return NULL;
156}
157
158static void *
159ssh_endpt_set_auth_attempts_thread(void *arg)
160{
161 (void)arg;
162 int ret;
163
164 pthread_barrier_wait(&barrier);
165
Michal Vasko338f8472016-10-13 15:01:27 +0200166 ret = nc_server_ssh_endpt_set_auth_attempts("main_ssh", 2);
Michal Vasko64b31a22016-03-03 11:00:00 +0100167 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100168
169 return NULL;
170}
171
172static void *
173ssh_endpt_set_auth_timeout_thread(void *arg)
174{
175 (void)arg;
176 int ret;
177
178 pthread_barrier_wait(&barrier);
179
Michal Vasko338f8472016-10-13 15:01:27 +0200180 ret = nc_server_ssh_endpt_set_auth_timeout("main_ssh", 5);
Michal Vasko64b31a22016-03-03 11:00:00 +0100181 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100182
183 return NULL;
184}
185
186static void *
187ssh_endpt_add_authkey_thread(void *arg)
188{
189 (void)arg;
190 int ret;
191
192 pthread_barrier_wait(&barrier);
193
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200194 ret = nc_server_ssh_add_authkey_path(TESTS_DIR "/data/key_rsa.pub", "test3");
Michal Vasko64b31a22016-03-03 11:00:00 +0100195 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100196
197 return NULL;
198}
199
200static void *
201ssh_endpt_del_authkey_thread(void *arg)
202{
203 (void)arg;
204 int ret;
205
206 pthread_barrier_wait(&barrier);
207
Michal Vasko7a4213f2022-09-08 11:02:34 +0200208 ret = nc_server_ssh_del_authkey(TESTS_DIR "/data/key_ecdsa.pub", NULL, 0, "test2");
Michal Vasko64b31a22016-03-03 11:00:00 +0100209 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100210
211 return NULL;
212}
213
Michal Vasko693eb112016-02-18 13:28:49 +0100214static int
Radek Krejci90a84a22017-05-25 13:53:00 +0200215ssh_hostkey_check_clb(const char *hostname, ssh_session session, void *priv)
Michal Vasko693eb112016-02-18 13:28:49 +0100216{
217 (void)hostname;
218 (void)session;
Radek Krejci90a84a22017-05-25 13:53:00 +0200219 (void)priv;
Michal Vasko693eb112016-02-18 13:28:49 +0100220
221 return 0;
222}
223
Michal Vasko734360b2016-02-08 15:38:25 +0100224static void *
225ssh_client_thread(void *arg)
226{
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100227 int ret, read_pipe = *(int *)arg;
228 char buf[9];
Michal Vasko734360b2016-02-08 15:38:25 +0100229 struct nc_session *session;
230
Michal Vasko5a446472020-05-06 12:43:42 +0200231 fprintf(stdout, "SSH client start.\n");
232
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100233 ret = read(read_pipe, buf, 9);
Michal Vasko64b31a22016-03-03 11:00:00 +0100234 nc_assert(ret == 9);
235 nc_assert(!strncmp(buf, "ssh_ready", 9));
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100236
Michal Vasko693eb112016-02-18 13:28:49 +0100237 /* skip the knownhost check */
Radek Krejci90a84a22017-05-25 13:53:00 +0200238 nc_client_ssh_set_auth_hostkey_check_clb(ssh_hostkey_check_clb, NULL);
Michal Vasko734360b2016-02-08 15:38:25 +0100239
Michal Vasko693eb112016-02-18 13:28:49 +0100240 ret = nc_client_ssh_set_username("test");
Michal Vasko64b31a22016-03-03 11:00:00 +0100241 nc_assert(!ret);
Michal Vasko693eb112016-02-18 13:28:49 +0100242
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200243 ret = nc_client_ssh_add_keypair(TESTS_DIR "/data/key_ecdsa.pub", TESTS_DIR "/data/key_ecdsa");
Michal Vasko64b31a22016-03-03 11:00:00 +0100244 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100245
246 nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PUBLICKEY, 1);
247 nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PASSWORD, -1);
248 nc_client_ssh_set_auth_pref(NC_SSH_AUTH_INTERACTIVE, -1);
249
Michal Vasko693eb112016-02-18 13:28:49 +0100250 session = nc_connect_ssh("127.0.0.1", 6001, NULL);
Michal Vasko64b31a22016-03-03 11:00:00 +0100251 nc_assert(session);
Michal Vasko734360b2016-02-08 15:38:25 +0100252
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100253 nc_session_free(session, NULL);
Michal Vasko734360b2016-02-08 15:38:25 +0100254
Michal Vaskoe2713da2016-08-22 16:06:40 +0200255 fprintf(stdout, "SSH client finished.\n");
256
Michal Vasko734360b2016-02-08 15:38:25 +0100257 nc_thread_destroy();
258 return NULL;
259}
260
Radek Krejci53691be2016-02-22 13:58:37 +0100261#endif /* NC_ENABLED_SSH */
Michal Vasko734360b2016-02-08 15:38:25 +0100262
Radek Krejci53691be2016-02-22 13:58:37 +0100263#ifdef NC_ENABLED_TLS
Michal Vasko734360b2016-02-08 15:38:25 +0100264
Michal Vasko4c1fb492017-01-30 14:31:07 +0100265static int
266clb_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 +0200267 char **privkey_data, NC_SSH_KEY_TYPE *privkey_type)
Michal Vasko4c1fb492017-01-30 14:31:07 +0100268{
269 if (!strcmp(name, "server_cert1")) {
Michal Vasko346ac562018-11-05 08:47:18 +0100270 *cert_data = strdup("MIIEQDCCAygCCQCV65JgDvfWkTANBgkqhkiG9w0BAQsFADBjMQswCQYDVQQGEwJD\n"
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200271 "WjETMBEGA1UECAwKU29tZS1TdGF0ZTENMAsGA1UEBwwEQnJubzEPMA0GA1UECgwG\n"
272 "Q0VTTkVUMQwwCgYDVQQLDANUTUMxETAPBgNVBAMMCHNlcnZlcmNhMB4XDTE4MTEw\n"
273 "NTA3MzExMFoXDTI4MTEwMjA3MzExMFowYTELMAkGA1UEBhMCQ1oxEzARBgNVBAgM\n"
274 "ClNvbWUtU3RhdGUxDTALBgNVBAcMBEJybm8xDzANBgNVBAoMBkNFU05FVDEMMAoG\n"
275 "A1UECwwDVE1DMQ8wDQYDVQQDDAZzZXJ2ZXIwggIiMA0GCSqGSIb3DQEBAQUAA4IC\n"
276 "DwAwggIKAoICAQDqiO2N8Oa/JA/VmQjAGmv3t4oO55u+miCVEdF29W5Ol/+BTVUC\n"
277 "sBCbAaHTmLqWbxORWXWegyUjEskNxayVp5WforK+xfQeGxC1fCo+rCRrlQK/pqXB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30 14:31:07 +0100293 *privkey_data = strdup("MIIJKAIBAAKCAgEA6ojtjfDmvyQP1ZkIwBpr97eKDuebvpoglRHRdvVuTpf/gU1V\n"
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200294 "ArAQmwGh05i6lm8TkVl1noMlIxLJDcWslaeVn6KyvsX0HhsQtXwqPqwka5UCv6al\n"
295 "wf/ivAvcNpcX1j0t/uIGCI4dSiKnzQCyf0FTirzQkjrDZUd3meDhNQTruCalGV4g\n"
296 "fNWIq3e1oGuwAn1tLlu9oTrE4HzMpgbNEU6wNmsSqpwGxUhYLoSaM7b0dLmqP+Zc\n"
297 "zSS0Uac0PFNkehGQ2CYIT80f580o4XGtoLCUUGkp6YCTL4Z2CeBEaJABWjDIDH+d\n"
298 "KYIUBqUpz4Th12gXAP+h+3qI6+9eppeHrfrzARDsfLjwUNxQJse1QSArjAytf0FK\n"
299 "tGHrORc7W0TiCFvR0zaoUNLTKk7enTiRQ9rfWZOAu44fUvPCaXDE6zXXeaVgoKCo\n"
300 "4VHlho36erUcjlEBM+jk28IykbZGtBb6igKvYa1tPSgeYm/zJoFVjQcnr14uci/f\n"
301 "t1+Na+hOIEoEEiKxcAPk2b2vBKNlRIW7WLJ3u7ZiuQEJTNm6+3cE4+lfwaBCBqBT\n"
302 "oE+dpzvoUXoMyFFReUFd1O5axu4fXgt00jMaOQxmE0v9OmR/pL/PWIflVF4Zz5yV\n"
303 "ONYaDVc7l+veY0oEZruEPJ0hlEgxuCzLrcMhjufl2qE2Q7fQIaav/1NqBVkCAwEA\n"
304 "AQKCAgAeRZw75Oszoqj0jfMmMILdD3Cfad+dY3FvLESYESeyt0XAX8XoOed6ymQj\n"
305 "1qPGxQGGkkBvPEgv1b3jrC8Rhfb3Ct39Z7mRpTar5iHhwwBUboBTUmQ0vR173iAH\n"
306 "X8sw2Oa17mCO/CDlr8Fu4Xcom7r3vlVBepo72VSjpPYMjN0MANjwhEi3NCyWzTXB\n"
307 "RgUK3TuZbzfzto0w2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sr+3poUUv/q+hNO3jfY4HazhZDa0MalgNPo\n"
338 "Swr/VNRnkck40x2ovFb989J7yl++zTrnIrax9XRH1V0cNu+Kj7OMwZ2RRfbNv5JB\n"
339 "dOZPvkfqyIKFmbQgYbtD66rHuzNOfJpzqr/WVLO57/zzW8245NKG2B6B0oXkei/K\n"
340 "qDY0DAbHR3i3EOj1NPtVI1FC/xX8R9BREaid458bqoHJKuInrGcBjaUI9Cvymv8T\n"
341 "bstUgD6NPbJR4Sm6vrLeUqzjWZP3t1+Z6DjXmnpR2vvhMU/FWb//21p/88o=");
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200342 *privkey_type = NC_SSH_KEY_RSA;
Michal Vasko4c1fb492017-01-30 14:31:07 +0100343 return 0;
344 } else if (!strcmp(name, "main_cert")) {
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200345 *cert_path = strdup(TESTS_DIR "/data/server.crt");
346 *privkey_path = strdup(TESTS_DIR "/data/server.key");
Michal Vasko4c1fb492017-01-30 14:31:07 +0100347 return 0;
348 }
349
350 return 1;
351}
352
353static int
354clb_trusted_cert_lists(const char *name, void *UNUSED(user_data), char ***cert_paths, int *cert_path_count,
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200355 char ***cert_data, int *cert_data_count)
Michal Vasko4c1fb492017-01-30 14:31:07 +0100356{
357 if (!strcmp(name, "trusted_cert_list1")) {
Michal Vaskoad0bcb52017-03-22 11:55:47 +0100358 *cert_data = malloc(sizeof **cert_data);
Michal Vasko346ac562018-11-05 08:47:18 +0100359 (*cert_data)[0] = strdup("MIIDnDCCAoSgAwIBAgIJAIjf7UNx4uabMA0GCSqGSIb3DQEBCwUAMGMxCzAJBgNV\n"
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200360 "BAYTAkNaMRMwEQYDVQQIDApTb21lLVN0YXRlMQ0wCwYDVQQHDARCcm5vMQ8wDQYD\n"
361 "VQQKDAZDRVNORVQxDDAKBgNVBAsMA1RNQzERMA8GA1UEAwwIc2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Q+BTCllDinQv\n"
377 "R08yzd4fzA3YXthvX1dBu1SvqQAGOS7gssRCyv9uWI6MXta25X91eY1ZMz1euJ04\n"
378 "mB8EdyYiZc0kzrb9dv5d0g==");
Michal Vasko4c1fb492017-01-30 14:31:07 +0100379 *cert_data_count = 1;
380 return 0;
381 } else if (!strcmp(name, "client_cert_list")) {
Michal Vasko75ff74b2017-03-21 15:34:34 +0100382 *cert_paths = malloc(sizeof **cert_paths);
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200383 (*cert_paths)[0] = strdup(TESTS_DIR "/data/client.crt");
Michal Vasko4c1fb492017-01-30 14:31:07 +0100384 *cert_path_count = 1;
385 return 0;
386 }
387
388 return 1;
389}
390
Michal Vasko734360b2016-02-08 15:38:25 +0100391static void *
Michal Vasko338f8472016-10-13 15:01:27 +0200392endpt_set_address_thread(void *arg)
Michal Vasko734360b2016-02-08 15:38:25 +0100393{
394 (void)arg;
395 int ret;
396
397 pthread_barrier_wait(&barrier);
398
Michal Vasko338f8472016-10-13 15:01:27 +0200399 ret = nc_server_endpt_set_address("quaternary", "0.0.0.0");
Michal Vasko64b31a22016-03-03 11:00:00 +0100400 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100401
402 return NULL;
403}
404
405static void *
Michal Vasko338f8472016-10-13 15:01:27 +0200406endpt_set_port_thread(void *arg)
Michal Vasko734360b2016-02-08 15:38:25 +0100407{
408 (void)arg;
409 int ret;
410
411 pthread_barrier_wait(&barrier);
412
Michal Vasko338f8472016-10-13 15:01:27 +0200413 ret = nc_server_endpt_set_port("quaternary", 6003);
Michal Vasko64b31a22016-03-03 11:00:00 +0100414 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100415
416 return NULL;
417}
418
419static void *
Michal Vasko4c1fb492017-01-30 14:31:07 +0100420tls_endpt_set_server_cert_thread(void *arg)
Michal Vasko734360b2016-02-08 15:38:25 +0100421{
422 (void)arg;
423 int ret;
424
425 pthread_barrier_wait(&barrier);
426
Michal Vasko4c1fb492017-01-30 14:31:07 +0100427 ret = nc_server_tls_endpt_set_server_cert("quaternary", "server_cert1");
Michal Vasko64b31a22016-03-03 11:00:00 +0100428 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100429
430 nc_thread_destroy();
431 return NULL;
432}
433
434static void *
Michal Vasko4c1fb492017-01-30 14:31:07 +0100435tls_endpt_add_trusted_cert_list_thread(void *arg)
Michal Vasko734360b2016-02-08 15:38:25 +0100436{
437 (void)arg;
438 int ret;
439
440 pthread_barrier_wait(&barrier);
441
Michal Vasko4c1fb492017-01-30 14:31:07 +0100442 ret = nc_server_tls_endpt_add_trusted_cert_list("quaternary", "trusted_cert_list1");
Michal Vasko64b31a22016-03-03 11:00:00 +0100443 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100444
445 nc_thread_destroy();
446 return NULL;
447}
448
449static void *
450tls_endpt_set_trusted_ca_paths_thread(void *arg)
451{
452 (void)arg;
453 int ret;
454
455 pthread_barrier_wait(&barrier);
456
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200457 ret = nc_server_tls_endpt_set_trusted_ca_paths("quaternary", TESTS_DIR "/data/serverca.pem", "data");
Michal Vasko64b31a22016-03-03 11:00:00 +0100458 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100459
460 nc_thread_destroy();
461 return NULL;
462}
463
464static void *
Michal Vasko4c1fb492017-01-30 14:31:07 +0100465tls_endpt_del_trusted_cert_list_thread(void *arg)
Michal Vasko734360b2016-02-08 15:38:25 +0100466{
467 (void)arg;
468
469 pthread_barrier_wait(&barrier);
470
Michal Vasko4c1fb492017-01-30 14:31:07 +0100471 nc_server_tls_endpt_del_trusted_cert_list("quaternary", "trusted_cert_list1");
Michal Vasko734360b2016-02-08 15:38:25 +0100472
473 return NULL;
474}
475
476static void *
477tls_endpt_set_crl_paths_thread(void *arg)
478{
479 (void)arg;
480 int ret;
481
482 pthread_barrier_wait(&barrier);
483
484 ret = nc_server_tls_endpt_set_crl_paths("quaternary", NULL, "data");
Michal Vasko64b31a22016-03-03 11:00:00 +0100485 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100486
487 nc_thread_destroy();
488 return NULL;
489}
490
491static void *
492tls_endpt_clear_crls_thread(void *arg)
493{
494 (void)arg;
495
496 pthread_barrier_wait(&barrier);
497
498 nc_server_tls_endpt_clear_crls("quaternary");
499
500 return NULL;
501}
502
503static void *
504tls_endpt_add_ctn_thread(void *arg)
505{
506 (void)arg;
507 int ret;
508
509 pthread_barrier_wait(&barrier);
510
Michal Vasko3cf4aaa2017-02-01 15:03:36 +0100511 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 +0200512 NC_TLS_CTN_SAN_IP_ADDRESS, NULL);
Michal Vasko64b31a22016-03-03 11:00:00 +0100513 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100514
515 return NULL;
516}
517
518static void *
519tls_endpt_del_ctn_thread(void *arg)
520{
521 (void)arg;
522 int ret;
523
524 pthread_barrier_wait(&barrier);
525
Michal Vasko338f8472016-10-13 15:01:27 +0200526 ret = nc_server_tls_endpt_del_ctn("main_tls", -1, NULL, NC_TLS_CTN_SAN_ANY, NULL);
Michal Vasko64b31a22016-03-03 11:00:00 +0100527 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100528
529 return NULL;
530}
531
532static void *
533tls_client_thread(void *arg)
534{
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100535 int ret, read_pipe = *(int *)arg;
536 char buf[9];
Michal Vasko734360b2016-02-08 15:38:25 +0100537 struct nc_session *session;
538
Michal Vasko5a446472020-05-06 12:43:42 +0200539 fprintf(stdout, "TLS client start.\n");
540
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100541 ret = read(read_pipe, buf, 9);
Michal Vasko64b31a22016-03-03 11:00:00 +0100542 nc_assert(ret == 9);
543 nc_assert(!strncmp(buf, "tls_ready", 9));
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100544
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200545 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 +0100546 nc_assert(!ret);
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200547 ret = nc_client_tls_set_trusted_ca_paths(NULL, TESTS_DIR "/data");
Michal Vasko64b31a22016-03-03 11:00:00 +0100548 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100549
550 session = nc_connect_tls("127.0.0.1", 6501, NULL);
Michal Vasko64b31a22016-03-03 11:00:00 +0100551 nc_assert(session);
Michal Vasko734360b2016-02-08 15:38:25 +0100552
Frank Rimplercb9274f2019-03-13 16:40:34 +0000553 /* verify some capabilities */
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200554 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 +0000555
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100556 nc_session_free(session, NULL);
Michal Vasko734360b2016-02-08 15:38:25 +0100557
Michal Vaskoe2713da2016-08-22 16:06:40 +0200558 fprintf(stdout, "TLS client finished.\n");
559
Michal Vasko734360b2016-02-08 15:38:25 +0100560 nc_thread_destroy();
561 return NULL;
562}
563
Radek Krejci53691be2016-02-22 13:58:37 +0100564#endif /* NC_ENABLED_TLS */
Michal Vasko734360b2016-02-08 15:38:25 +0100565
566static void *(*thread_funcs[])(void *) = {
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200567#if defined (NC_ENABLED_SSH) || defined (NC_ENABLED_TLS)
Michal Vasko734360b2016-02-08 15:38:25 +0100568 server_thread,
569#endif
Radek Krejci53691be2016-02-22 13:58:37 +0100570#ifdef NC_ENABLED_SSH
Michal Vasko8727e432016-10-13 15:44:10 +0200571 add_endpt_thread,
572 del_endpt_thread,
Michal Vasko734360b2016-02-08 15:38:25 +0100573 ssh_endpt_set_hostkey_thread,
Michal Vasko734360b2016-02-08 15:38:25 +0100574 ssh_endpt_set_auth_methods_thread,
575 ssh_endpt_set_auth_attempts_thread,
576 ssh_endpt_set_auth_timeout_thread,
577 ssh_endpt_add_authkey_thread,
578 ssh_endpt_del_authkey_thread,
579#endif
Radek Krejci53691be2016-02-22 13:58:37 +0100580#ifdef NC_ENABLED_TLS
Michal Vasko8727e432016-10-13 15:44:10 +0200581 endpt_set_address_thread,
582 endpt_set_port_thread,
Michal Vasko4c1fb492017-01-30 14:31:07 +0100583 tls_endpt_set_server_cert_thread,
584 tls_endpt_add_trusted_cert_list_thread,
Michal Vasko734360b2016-02-08 15:38:25 +0100585 tls_endpt_set_trusted_ca_paths_thread,
Michal Vasko4c1fb492017-01-30 14:31:07 +0100586 tls_endpt_del_trusted_cert_list_thread,
Michal Vasko734360b2016-02-08 15:38:25 +0100587 tls_endpt_set_crl_paths_thread,
588 tls_endpt_clear_crls_thread,
589 tls_endpt_add_ctn_thread,
590 tls_endpt_del_ctn_thread,
591#endif
592};
593
594const int thread_count = sizeof thread_funcs / sizeof *thread_funcs;
595
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200596#if defined (NC_ENABLED_SSH) && defined (NC_ENABLED_TLS)
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100597const int client_count = 2;
598pid_t pids[2];
599int pipes[4];
Michal Vasko734360b2016-02-08 15:38:25 +0100600#else
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100601const int client_count = 1;
602pid_t pids[1];
603int pipes[2];
604#endif
605
606static void
607client_fork(void)
608{
609 int ret, clients = 0;
610
611#ifdef NC_ENABLED_SSH
Jan Kundrát5cec3c22021-10-08 19:54:39 +0200612 nc_assert(pipe(pipes + clients * 2) == 0);
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100613
614 if (!(pids[clients] = fork())) {
615 nc_client_init();
616
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200617 ret = nc_client_set_schema_searchpath(TESTS_DIR "/data/modules");
Michal Vasko64b31a22016-03-03 11:00:00 +0100618 nc_assert(!ret);
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100619
620 /* close write */
621 close(pipes[clients * 2 + 1]);
622 ssh_client_thread(&pipes[clients * 2]);
623 close(pipes[clients * 2]);
624 nc_client_destroy();
625 exit(0);
Michal Vasko734360b2016-02-08 15:38:25 +0100626 }
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100627 /* close read */
628 close(pipes[clients * 2]);
629
630 ++clients;
631#endif
632
633#ifdef NC_ENABLED_TLS
Jan Kundrát5cec3c22021-10-08 19:54:39 +0200634 nc_assert(pipe(pipes + clients * 2) == 0);
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100635
636 if (!(pids[clients] = fork())) {
637 nc_client_init();
638
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200639 ret = nc_client_set_schema_searchpath(TESTS_DIR "/data/modules");
Michal Vasko64b31a22016-03-03 11:00:00 +0100640 nc_assert(!ret);
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100641
642 /* close write */
643 close(pipes[clients * 2 + 1]);
644 tls_client_thread(&pipes[clients * 2]);
645 close(pipes[clients * 2]);
646 nc_client_destroy();
647 exit(0);
648 }
649 /* close read */
650 close(pipes[clients * 2]);
651
652 ++clients;
Michal Vasko734360b2016-02-08 15:38:25 +0100653#endif
654}
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200655
Claus Klein22091912020-01-20 13:45:47 +0100656#endif
Michal Vasko734360b2016-02-08 15:38:25 +0100657
658int
659main(void)
660{
Claus Klein22091912020-01-20 13:45:47 +0100661#if _POSIX_BARRIERS >= 200112L
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100662 int ret, i, clients = 0;
Michal Vasko734360b2016-02-08 15:38:25 +0100663 pthread_t tids[thread_count];
664
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100665 nc_verbosity(NC_VERB_VERBOSE);
666
667 client_fork();
Michal Vasko734360b2016-02-08 15:38:25 +0100668
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200669 ly_ctx_new(TESTS_DIR "/data/modules", 0, &ctx);
Michal Vasko64b31a22016-03-03 11:00:00 +0100670 nc_assert(ctx);
Michal Vasko77367452021-02-16 16:32:18 +0100671 ly_ctx_load_module(ctx, "ietf-netconf", NULL, NULL);
Frank Rimplercb9274f2019-03-13 16:40:34 +0000672
673 /* load some application models with deviations */
Michal Vasko77367452021-02-16 16:32:18 +0100674 nc_assert(ly_ctx_load_module(ctx, "module-a", NULL, NULL));
675 nc_assert(ly_ctx_load_module(ctx, "module-a-dv", NULL, NULL));
676 nc_assert(ly_ctx_load_module(ctx, "module-a-dv2", NULL, NULL));
Frank Rimplercb9274f2019-03-13 16:40:34 +0000677
Michal Vasko93224072021-11-09 12:14:28 +0100678 nc_server_init();
Michal Vasko734360b2016-02-08 15:38:25 +0100679
680 pthread_barrier_init(&barrier, NULL, thread_count);
681
Radek Krejci53691be2016-02-22 13:58:37 +0100682#ifdef NC_ENABLED_SSH
Michal Vasko4c1fb492017-01-30 14:31:07 +0100683 /* set callback */
684 nc_server_ssh_set_hostkey_clb(clb_hostkeys, NULL, NULL);
685
Michal Vasko734360b2016-02-08 15:38:25 +0100686 /* do first, so that client can connect on SSH */
Michal Vasko338f8472016-10-13 15:01:27 +0200687 ret = nc_server_add_endpt("main_ssh", NC_TI_LIBSSH);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200688 nc_assert(!ret);
Michal Vasko338f8472016-10-13 15:01:27 +0200689 ret = nc_server_endpt_set_address("main_ssh", "0.0.0.0");
Michal Vasko64b31a22016-03-03 11:00:00 +0100690 nc_assert(!ret);
Michal Vasko338f8472016-10-13 15:01:27 +0200691 ret = nc_server_endpt_set_port("main_ssh", 6001);
Michal Vasko64b31a22016-03-03 11:00:00 +0100692 nc_assert(!ret);
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200693 ret = nc_server_ssh_add_authkey_path(TESTS_DIR "/data/key_ecdsa.pub", "test");
Michal Vasko338f8472016-10-13 15:01:27 +0200694 nc_assert(!ret);
Michal Vasko7d255882017-02-09 13:35:08 +0100695 ret = nc_server_ssh_endpt_add_hostkey("main_ssh", "key_rsa", -1);
Michal Vasko64b31a22016-03-03 11:00:00 +0100696 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100697
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100698 /* client ready */
699 ret = write(pipes[clients * 2 + 1], "ssh_ready", 9);
Michal Vasko64b31a22016-03-03 11:00:00 +0100700 nc_assert(ret == 9);
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100701 ++clients;
702
Michal Vasko734360b2016-02-08 15:38:25 +0100703 /* for ssh_endpt_del_authkey */
Michal Vasko7a4213f2022-09-08 11:02:34 +0200704 ret = nc_server_ssh_add_authkey_path(TESTS_DIR "/data/key_ecdsa.pub", "test2");
Michal Vasko338f8472016-10-13 15:01:27 +0200705 nc_assert(!ret);
706
707 ret = nc_server_add_endpt("secondary", NC_TI_LIBSSH);
Michal Vasko64b31a22016-03-03 11:00:00 +0100708 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100709#endif
710
Radek Krejci53691be2016-02-22 13:58:37 +0100711#ifdef NC_ENABLED_TLS
Michal Vasko4c1fb492017-01-30 14:31:07 +0100712 /* set callbacks */
713 nc_server_tls_set_server_cert_clb(clb_server_cert, NULL, NULL);
714 nc_server_tls_set_trusted_cert_list_clb(clb_trusted_cert_lists, NULL, NULL);
715
Michal Vasko734360b2016-02-08 15:38:25 +0100716 /* do first, so that client can connect on TLS */
Michal Vasko338f8472016-10-13 15:01:27 +0200717 ret = nc_server_add_endpt("main_tls", NC_TI_OPENSSL);
Michal Vaskoe2713da2016-08-22 16:06:40 +0200718 nc_assert(!ret);
Michal Vasko338f8472016-10-13 15:01:27 +0200719 ret = nc_server_endpt_set_address("main_tls", "0.0.0.0");
Michal Vasko64b31a22016-03-03 11:00:00 +0100720 nc_assert(!ret);
Michal Vasko338f8472016-10-13 15:01:27 +0200721 ret = nc_server_endpt_set_port("main_tls", 6501);
Michal Vasko64b31a22016-03-03 11:00:00 +0100722 nc_assert(!ret);
Michal Vasko4c1fb492017-01-30 14:31:07 +0100723 ret = nc_server_tls_endpt_set_server_cert("main_tls", "main_cert");
Michal Vasko64b31a22016-03-03 11:00:00 +0100724 nc_assert(!ret);
Michal Vasko4c1fb492017-01-30 14:31:07 +0100725 ret = nc_server_tls_endpt_add_trusted_cert_list("main_tls", "client_cert_list");
Michal Vasko338f8472016-10-13 15:01:27 +0200726 nc_assert(!ret);
Michal Vasko346ac562018-11-05 08:47:18 +0100727 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 +0100728 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100729
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100730 /* client ready */
731 ret = write(pipes[clients * 2 + 1], "tls_ready", 9);
Michal Vasko64b31a22016-03-03 11:00:00 +0100732 nc_assert(ret == 9);
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100733 ++clients;
734
Michal Vasko734360b2016-02-08 15:38:25 +0100735 /* for tls_endpt_del_ctn */
Michal Vasko3cf4aaa2017-02-01 15:03:36 +0100736 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 +0200737 nc_assert(!ret);
738
739 ret = nc_server_add_endpt("quaternary", NC_TI_OPENSSL);
Michal Vasko64b31a22016-03-03 11:00:00 +0100740 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100741#endif
742
Michal Vasko734360b2016-02-08 15:38:25 +0100743 /* threads'n'stuff */
744 ret = 0;
745 for (i = 0; i < thread_count; ++i) {
746 ret += pthread_create(&tids[i], NULL, thread_funcs[i], NULL);
747 }
Michal Vasko64b31a22016-03-03 11:00:00 +0100748 nc_assert(!ret);
Michal Vasko734360b2016-02-08 15:38:25 +0100749
750 /* cleanup */
751 for (i = 0; i < thread_count; ++i) {
752 pthread_join(tids[i], NULL);
753 }
Michal Vaskoa7b8ca52016-03-01 12:09:29 +0100754 for (i = 0; i < client_count; ++i) {
755 waitpid(pids[i], NULL, 0);
756 close(pipes[i * 2 + 1]);
757 }
Michal Vasko734360b2016-02-08 15:38:25 +0100758
759 pthread_barrier_destroy(&barrier);
760
Michal Vasko734360b2016-02-08 15:38:25 +0100761 nc_server_destroy();
Michal Vasko1dcb7642021-04-14 15:28:23 +0200762 ly_ctx_destroy(ctx);
Claus Klein22091912020-01-20 13:45:47 +0100763#endif
Michal Vasko734360b2016-02-08 15:38:25 +0100764
Michal Vasko734360b2016-02-08 15:38:25 +0100765 return 0;
766}