Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 1 | /** |
| 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 Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 6 | * Copyright (c) 2017 CESNET, z.s.p.o. |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 7 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 8 | * 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 Vasko | ad1cc6a | 2016-02-26 15:06:06 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <pthread.h> |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 16 | #include <unistd.h> |
| 17 | #include <stdlib.h> |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 18 | #include <stdio.h> |
Michal Vasko | a28be9a | 2016-03-01 15:20:47 +0100 | [diff] [blame] | 19 | #include <string.h> |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 20 | #include <sys/types.h> |
| 21 | #include <sys/wait.h> |
| 22 | |
| 23 | #include <libyang/libyang.h> |
| 24 | |
| 25 | #include <session_client.h> |
| 26 | #include <session_server.h> |
| 27 | #include <log.h> |
Jan Kundrát | cf15d6c | 2017-10-26 18:07:56 +0200 | [diff] [blame] | 28 | #include "tests/config.h" |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 29 | |
| 30 | /* millisec */ |
Michal Vasko | 15592cc | 2016-02-09 09:52:43 +0100 | [diff] [blame] | 31 | #define NC_ACCEPT_TIMEOUT 5000 |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 32 | /* millisec */ |
Michal Vasko | 15592cc | 2016-02-09 09:52:43 +0100 | [diff] [blame] | 33 | #define NC_PS_POLL_TIMEOUT 5000 |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 34 | /* sec */ |
Michal Vasko | 15592cc | 2016-02-09 09:52:43 +0100 | [diff] [blame] | 35 | #define CLIENT_SSH_AUTH_TIMEOUT 10 |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 36 | |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 37 | #define nc_assert(cond) if (!(cond)) { fprintf(stderr, "assert failed (%s:%d)\n", __FILE__, __LINE__); exit(1); } |
| 38 | |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 39 | pthread_barrier_t barrier; |
| 40 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 41 | #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS) |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 42 | |
| 43 | static void * |
| 44 | server_thread(void *arg) |
| 45 | { |
| 46 | (void)arg; |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 47 | NC_MSG_TYPE msgtype; |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 48 | int ret; |
| 49 | struct nc_pollsession *ps; |
| 50 | struct nc_session *session; |
| 51 | |
| 52 | ps = nc_ps_new(); |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 53 | nc_assert(ps); |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 54 | |
| 55 | pthread_barrier_wait(&barrier); |
| 56 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 57 | #if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS) |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 58 | msgtype = nc_accept(NC_ACCEPT_TIMEOUT, &session); |
| 59 | nc_assert(msgtype == NC_MSG_HELLO); |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 60 | |
| 61 | nc_ps_add_session(ps, session); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 62 | ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT, NULL); |
| 63 | nc_assert(ret & NC_PSPOLL_RPC); |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 64 | nc_ps_clear(ps, 0, NULL); |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 65 | #endif |
| 66 | |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 67 | msgtype = nc_accept(NC_ACCEPT_TIMEOUT, &session); |
| 68 | nc_assert(msgtype == NC_MSG_HELLO); |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 69 | |
| 70 | nc_ps_add_session(ps, session); |
Michal Vasko | 71090fc | 2016-05-24 16:37:28 +0200 | [diff] [blame] | 71 | ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT, NULL); |
| 72 | nc_assert(ret & NC_PSPOLL_RPC); |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 73 | nc_ps_clear(ps, 0, NULL); |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 74 | |
| 75 | nc_ps_free(ps); |
| 76 | |
| 77 | nc_thread_destroy(); |
| 78 | return NULL; |
| 79 | } |
| 80 | |
Michal Vasko | 338f847 | 2016-10-13 15:01:27 +0200 | [diff] [blame] | 81 | #endif /* NC_ENABLED_SSH || NC_ENABLED_TLS */ |
| 82 | |
| 83 | #ifdef NC_ENABLED_SSH |
| 84 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 85 | static int |
| 86 | clb_hostkeys(const char *name, void *UNUSED(user_data), char **privkey_path, char **UNUSED(privkey_data), |
| 87 | int *UNUSED(privkey_data_rsa)) |
| 88 | { |
| 89 | if (!strcmp(name, "key_rsa")) { |
| 90 | *privkey_path = strdup(TESTS_DIR"/data/key_rsa"); |
| 91 | return 0; |
| 92 | } else if (!strcmp(name, "key_dsa")) { |
| 93 | *privkey_path = strdup(TESTS_DIR"/data/key_dsa"); |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | return 1; |
| 98 | } |
| 99 | |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 100 | static void * |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 101 | add_endpt_thread(void *arg) |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 102 | { |
| 103 | (void)arg; |
| 104 | int ret; |
| 105 | |
| 106 | pthread_barrier_wait(&barrier); |
Michal Vasko | 338f847 | 2016-10-13 15:01:27 +0200 | [diff] [blame] | 107 | ret = nc_server_add_endpt("tertiary", NC_TI_LIBSSH); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 108 | nc_assert(!ret); |
| 109 | |
| 110 | return NULL; |
| 111 | } |
| 112 | |
| 113 | static void * |
| 114 | del_endpt_thread(void *arg) |
| 115 | { |
| 116 | (void)arg; |
| 117 | int ret; |
| 118 | |
| 119 | pthread_barrier_wait(&barrier); |
| 120 | |
Michal Vasko | 5905037 | 2016-11-22 14:33:55 +0100 | [diff] [blame] | 121 | ret = nc_server_del_endpt("secondary", 0); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 122 | nc_assert(!ret); |
| 123 | |
| 124 | return NULL; |
| 125 | } |
| 126 | |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 127 | static void * |
| 128 | ssh_endpt_set_hostkey_thread(void *arg) |
| 129 | { |
| 130 | (void)arg; |
| 131 | int ret; |
| 132 | |
| 133 | pthread_barrier_wait(&barrier); |
| 134 | |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 135 | ret = nc_server_ssh_endpt_add_hostkey("main_ssh", "key_dsa", -1); |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 136 | nc_assert(!ret); |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 137 | |
| 138 | return NULL; |
| 139 | } |
| 140 | |
| 141 | static void * |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 142 | ssh_endpt_set_auth_methods_thread(void *arg) |
| 143 | { |
| 144 | (void)arg; |
| 145 | int ret; |
| 146 | |
| 147 | pthread_barrier_wait(&barrier); |
| 148 | |
Michal Vasko | 338f847 | 2016-10-13 15:01:27 +0200 | [diff] [blame] | 149 | ret = nc_server_ssh_endpt_set_auth_methods("main_ssh", NC_SSH_AUTH_PUBLICKEY | NC_SSH_AUTH_PASSWORD | NC_SSH_AUTH_INTERACTIVE); |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 150 | nc_assert(!ret); |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 151 | |
| 152 | return NULL; |
| 153 | } |
| 154 | |
| 155 | static void * |
| 156 | ssh_endpt_set_auth_attempts_thread(void *arg) |
| 157 | { |
| 158 | (void)arg; |
| 159 | int ret; |
| 160 | |
| 161 | pthread_barrier_wait(&barrier); |
| 162 | |
Michal Vasko | 338f847 | 2016-10-13 15:01:27 +0200 | [diff] [blame] | 163 | ret = nc_server_ssh_endpt_set_auth_attempts("main_ssh", 2); |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 164 | nc_assert(!ret); |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 165 | |
| 166 | return NULL; |
| 167 | } |
| 168 | |
| 169 | static void * |
| 170 | ssh_endpt_set_auth_timeout_thread(void *arg) |
| 171 | { |
| 172 | (void)arg; |
| 173 | int ret; |
| 174 | |
| 175 | pthread_barrier_wait(&barrier); |
| 176 | |
Michal Vasko | 338f847 | 2016-10-13 15:01:27 +0200 | [diff] [blame] | 177 | ret = nc_server_ssh_endpt_set_auth_timeout("main_ssh", 5); |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 178 | nc_assert(!ret); |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 179 | |
| 180 | return NULL; |
| 181 | } |
| 182 | |
| 183 | static void * |
| 184 | ssh_endpt_add_authkey_thread(void *arg) |
| 185 | { |
| 186 | (void)arg; |
| 187 | int ret; |
| 188 | |
| 189 | pthread_barrier_wait(&barrier); |
| 190 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 191 | ret = nc_server_ssh_add_authkey_path(TESTS_DIR"/data/key_rsa.pub", "test3"); |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 192 | nc_assert(!ret); |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 193 | |
| 194 | return NULL; |
| 195 | } |
| 196 | |
| 197 | static void * |
| 198 | ssh_endpt_del_authkey_thread(void *arg) |
| 199 | { |
| 200 | (void)arg; |
| 201 | int ret; |
| 202 | |
| 203 | pthread_barrier_wait(&barrier); |
| 204 | |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 205 | ret = nc_server_ssh_del_authkey(TESTS_DIR"/data/key_ecdsa.pub", NULL, 0, "test2"); |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 206 | nc_assert(!ret); |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 207 | |
| 208 | return NULL; |
| 209 | } |
| 210 | |
Michal Vasko | 693eb11 | 2016-02-18 13:28:49 +0100 | [diff] [blame] | 211 | static int |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 212 | ssh_hostkey_check_clb(const char *hostname, ssh_session session, void *priv) |
Michal Vasko | 693eb11 | 2016-02-18 13:28:49 +0100 | [diff] [blame] | 213 | { |
| 214 | (void)hostname; |
| 215 | (void)session; |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 216 | (void)priv; |
Michal Vasko | 693eb11 | 2016-02-18 13:28:49 +0100 | [diff] [blame] | 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 221 | static void * |
| 222 | ssh_client_thread(void *arg) |
| 223 | { |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 224 | int ret, read_pipe = *(int *)arg; |
| 225 | char buf[9]; |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 226 | struct nc_session *session; |
| 227 | |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 228 | ret = read(read_pipe, buf, 9); |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 229 | nc_assert(ret == 9); |
| 230 | nc_assert(!strncmp(buf, "ssh_ready", 9)); |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 231 | |
Michal Vasko | 693eb11 | 2016-02-18 13:28:49 +0100 | [diff] [blame] | 232 | /* skip the knownhost check */ |
Radek Krejci | 90a84a2 | 2017-05-25 13:53:00 +0200 | [diff] [blame] | 233 | nc_client_ssh_set_auth_hostkey_check_clb(ssh_hostkey_check_clb, NULL); |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 234 | |
Michal Vasko | 693eb11 | 2016-02-18 13:28:49 +0100 | [diff] [blame] | 235 | ret = nc_client_ssh_set_username("test"); |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 236 | nc_assert(!ret); |
Michal Vasko | 693eb11 | 2016-02-18 13:28:49 +0100 | [diff] [blame] | 237 | |
| 238 | ret = nc_client_ssh_add_keypair(TESTS_DIR"/data/key_dsa.pub", TESTS_DIR"/data/key_dsa"); |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 239 | nc_assert(!ret); |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 240 | |
| 241 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PUBLICKEY, 1); |
| 242 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PASSWORD, -1); |
| 243 | nc_client_ssh_set_auth_pref(NC_SSH_AUTH_INTERACTIVE, -1); |
| 244 | |
Michal Vasko | 693eb11 | 2016-02-18 13:28:49 +0100 | [diff] [blame] | 245 | session = nc_connect_ssh("127.0.0.1", 6001, NULL); |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 246 | nc_assert(session); |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 247 | |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 248 | nc_session_free(session, NULL); |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 249 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 250 | fprintf(stdout, "SSH client finished.\n"); |
| 251 | |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 252 | nc_thread_destroy(); |
| 253 | return NULL; |
| 254 | } |
| 255 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 256 | #endif /* NC_ENABLED_SSH */ |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 257 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 258 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 259 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 260 | static int |
| 261 | clb_server_cert(const char *name, void *UNUSED(user_data), char **cert_path, char **cert_data, char **privkey_path, |
| 262 | char **privkey_data, int *privkey_data_rsa) |
| 263 | { |
| 264 | if (!strcmp(name, "server_cert1")) { |
| 265 | *cert_data = strdup("MIIEKjCCAxICCQDqSTPpuoUZkzANBgkqhkiG9w0BAQUFADBYMQswCQYDVQQGEwJB\n" |
| 266 | "VTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0\n" |
| 267 | "cyBQdHkgTHRkMREwDwYDVQQDDAhzZXJ2ZXJjYTAeFw0xNjAyMDgxMTE0MzdaFw0y\n" |
| 268 | "NjAyMDUxMTE0MzdaMFYxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRl\n" |
| 269 | "MSEwHwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQxDzANBgNVBAMMBnNl\n" |
| 270 | "cnZlcjCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAOqI7Y3w5r8kD9WZ\n" |
| 271 | "CMAaa/e3ig7nm76aIJUR0Xb1bk6X/4FNVQKwEJsBodOYupZvE5FZdZ6DJSMSyQ3F\n" |
| 272 | "rJWnlZ+isr7F9B4bELV8Kj6sJGuVAr+mpcH/4rwL3DaXF9Y9Lf7iBgiOHUoip80A\n" |
| 273 |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4="); |
| 288 | *privkey_data = strdup("MIIJKAIBAAKCAgEA6ojtjfDmvyQP1ZkIwBpr97eKDuebvpoglRHRdvVuTpf/gU1V\n" |
| 289 | "ArAQmwGh05i6lm8TkVl1noMlIxLJDcWslaeVn6KyvsX0HhsQtXwqPqwka5UCv6al\n" |
| 290 | "wf/ivAvcNpcX1j0t/uIGCI4dSiKnzQCyf0FTirzQkjrDZUd3meDhNQTruCalGV4g\n" |
| 291 | "fNWIq3e1oGuwAn1tLlu9oTrE4HzMpgbNEU6wNmsSqpwGxUhYLoSaM7b0dLmqP+Zc\n" |
| 292 | "zSS0Uac0PFNkehGQ2CYIT80f580o4XGtoLCUUGkp6YCTL4Z2CeBEaJABWjDIDH+d\n" |
| 293 | "KYIUBqUpz4Th12gXAP+h+3qI6+9eppeHrfrzARDsfLjwUNxQJse1QSArjAytf0FK\n" |
| 294 | "tGHrORc7W0TiCFvR0zaoUNLTKk7enTiRQ9rfWZOAu44fUvPCaXDE6zXXeaVgoKCo\n" |
| 295 | "4VHlho36erUcjlEBM+jk28IykbZGtBb6igKvYa1tPSgeYm/zJoFVjQcnr14uci/f\n" |
| 296 | "t1+Na+hOIEoEEiKxcAPk2b2vBKNlRIW7WLJ3u7ZiuQEJTNm6+3cE4+lfwaBCBqBT\n" |
| 297 | "oE+dpzvoUXoMyFFReUFd1O5axu4fXgt00jMaOQxmE0v9OmR/pL/PWIflVF4Zz5yV\n" |
| 298 | "ONYaDVc7l+veY0oEZruEPJ0hlEgxuCzLrcMhjufl2qE2Q7fQIaav/1NqBVkCAwEA\n" |
| 299 | "AQKCAgAeRZw75Oszoqj0jfMmMILdD3Cfad+dY3FvLESYESeyt0XAX8XoOed6ymQj\n" |
| 300 | "1qPGxQGGkkBvPEgv1b3jrC8Rhfb3Ct39Z7mRpTar5iHhwwBUboBTUmQ0vR173iAH\n" |
| 301 | "X8sw2Oa17mCO/CDlr8Fu4Xcom7r3vlVBepo72VSjpPYMjN0MANjwhEi3NCyWzTXB\n" |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ZaM28zW/ZGk3wE0zy1YT5s30SQkm0NiWN3t/J0l19ccAOxlPWfjhF7v\n" |
| 317 | "IQZr7XMo5HeaK0Ak5+68J6bx6KgcXmlJOup7INaE8DyGXB6vd4K6957IXyqs3/bf\n" |
| 318 | "JAUmz49hnveCfLFdTVVT/Uq4IoPKfQSbSZc0BvPBsnBCF164l4jllGBaWS302dhg\n" |
| 319 | "W4cgxzG0SZGgNwow4AhB+ygiiS8yvOa7UcHfUObVrzWeeq9mYSQ1PkvUTjkWR2/Y\n" |
| 320 | "8xy7WP0TRBdJOVSs90H51lerEDGNQWvQvI97S9ZOsQKCAQB59u9lpuXtqwxAQCFy\n" |
| 321 | "fSFSuQoEHR2nDcOjF4GhbtHum15yCPaw5QVs/33nuPWze4ZLXReKk9p0mTh5V0p+\n" |
| 322 | "N3IvGlXl+uzEVu5d55eI7LIw5sLymHmwjWjxvimiMtrzLbCHSPHGc5JU9NLUH9/b\n" |
| 323 | "BY/JxGpy+NzcsHHOOQTwTdRIjviIOAo7fgQn2RyX0k+zXE8/7zqjqvji9zyemdNu\n" |
| 324 | "8we4uJICSntyvJwkbj/hrufTKEnBrwXpzfVn1EsH+6w32ZPBGLUhT75txJ8r56SR\n" |
| 325 | "q7l1XPU9vxovmT+lSMFF/Y0j1MbHWnds5H1shoFPNtYTvWBL/gfPHjIc+H23zsiu\n" |
| 326 | "3XlZAoIBAC2xB/Pnpoi9vOUMiqFH36AXtYa1DURy+AqCFlYlClMvb7YgvQ1w1eJv\n" |
| 327 | "nwrHSLk7HdKhnwGsLPduuRRH8q0n/osnoOutSQroE0n41UyIv2ZNccRwNmSzQcai\n" |
| 328 | "rBu2dSz02hlsh2otNl5IuGpOqXyPjXBpW4qGD6n2tH7THALnLC0BHtTSQVQsJsRM\n" |
| 329 | "3gX39LoiWvLDp2qJvplm6rTpi8Rgap6rZSqHe1yNKIxxD2vlr/WY9SMgLXYASO4S\n" |
| 330 | "SBz9wfGOmQIPk6KXNJkdV4kC7nNjIi75iwLLCgjHgUiHTrDq5sWekpeNnUoWsinb\n" |
| 331 | "Tsdsjnv3zHG9GyiClyLGxMbs4M5eyYECggEBAKuC8ZMpdIrjk6tERYB6g0LnQ7mW\n" |
| 332 | "8XYbDFAmLYMLs9yfG2jcjVbsW9Kugsr+3poUUv/q+hNO3jfY4HazhZDa0MalgNPo\n" |
| 333 | "Swr/VNRnkck40x2ovFb989J7yl++zTrnIrax9XRH1V0cNu+Kj7OMwZ2RRfbNv5JB\n" |
| 334 | "dOZPvkfqyIKFmbQgYbtD66rHuzNOfJpzqr/WVLO57/zzW8245NKG2B6B0oXkei/K\n" |
| 335 | "qDY0DAbHR3i3EOj1NPtVI1FC/xX8R9BREaid458bqoHJKuInrGcBjaUI9Cvymv8T\n" |
| 336 | "bstUgD6NPbJR4Sm6vrLeUqzjWZP3t1+Z6DjXmnpR2vvhMU/FWb//21p/88o="); |
| 337 | *privkey_data_rsa = 1; |
| 338 | return 0; |
| 339 | } else if (!strcmp(name, "main_cert")) { |
| 340 | *cert_path = strdup(TESTS_DIR"/data/server.crt"); |
| 341 | *privkey_path = strdup(TESTS_DIR"/data/server.key"); |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | return 1; |
| 346 | } |
| 347 | |
| 348 | static int |
| 349 | clb_trusted_cert_lists(const char *name, void *UNUSED(user_data), char ***cert_paths, int *cert_path_count, |
| 350 | char ***cert_data, int *cert_data_count) |
| 351 | { |
| 352 | if (!strcmp(name, "trusted_cert_list1")) { |
Michal Vasko | ad0bcb5 | 2017-03-22 11:55:47 +0100 | [diff] [blame] | 353 | *cert_data = malloc(sizeof **cert_data); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 354 | (*cert_data)[0] = strdup("MIIDgzCCAmugAwIBAgIJAL+y0WMRGax0MA0GCSqGSIb3DQEBBQUAMFgxCzAJBgNV\n" |
| 355 | "BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJ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xCvU\n" |
| 370 | "sgq28vGMSXLSYKBNaP/llXNmqW35oLs6CwVuiCL7Go0IDIOmiXN2bssb87hZSw3B\n" |
| 371 | "6iwU78wYshJUGZjLaK9PuMvFYJLFWSAePA2Yb+aEv80wMbX1oANSryU7Uf5BJk8V\n" |
| 372 | "kO3mlRDh2b1/5Gb5xA2vU2z3ReHdPNy6qSx0Mk4XJvQw9FsVHZ13"); |
| 373 | *cert_data_count = 1; |
| 374 | return 0; |
| 375 | } else if (!strcmp(name, "client_cert_list")) { |
Michal Vasko | 75ff74b | 2017-03-21 15:34:34 +0100 | [diff] [blame] | 376 | *cert_paths = malloc(sizeof **cert_paths); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 377 | (*cert_paths)[0] = strdup(TESTS_DIR"/data/client.crt"); |
| 378 | *cert_path_count = 1; |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | return 1; |
| 383 | } |
| 384 | |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 385 | static void * |
Michal Vasko | 338f847 | 2016-10-13 15:01:27 +0200 | [diff] [blame] | 386 | endpt_set_address_thread(void *arg) |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 387 | { |
| 388 | (void)arg; |
| 389 | int ret; |
| 390 | |
| 391 | pthread_barrier_wait(&barrier); |
| 392 | |
Michal Vasko | 338f847 | 2016-10-13 15:01:27 +0200 | [diff] [blame] | 393 | ret = nc_server_endpt_set_address("quaternary", "0.0.0.0"); |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 394 | nc_assert(!ret); |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 395 | |
| 396 | return NULL; |
| 397 | } |
| 398 | |
| 399 | static void * |
Michal Vasko | 338f847 | 2016-10-13 15:01:27 +0200 | [diff] [blame] | 400 | endpt_set_port_thread(void *arg) |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 401 | { |
| 402 | (void)arg; |
| 403 | int ret; |
| 404 | |
| 405 | pthread_barrier_wait(&barrier); |
| 406 | |
Michal Vasko | 338f847 | 2016-10-13 15:01:27 +0200 | [diff] [blame] | 407 | ret = nc_server_endpt_set_port("quaternary", 6003); |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 408 | nc_assert(!ret); |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 409 | |
| 410 | return NULL; |
| 411 | } |
| 412 | |
| 413 | static void * |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 414 | tls_endpt_set_server_cert_thread(void *arg) |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 415 | { |
| 416 | (void)arg; |
| 417 | int ret; |
| 418 | |
| 419 | pthread_barrier_wait(&barrier); |
| 420 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 421 | ret = nc_server_tls_endpt_set_server_cert("quaternary", "server_cert1"); |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 422 | nc_assert(!ret); |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 423 | |
| 424 | nc_thread_destroy(); |
| 425 | return NULL; |
| 426 | } |
| 427 | |
| 428 | static void * |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 429 | tls_endpt_add_trusted_cert_list_thread(void *arg) |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 430 | { |
| 431 | (void)arg; |
| 432 | int ret; |
| 433 | |
| 434 | pthread_barrier_wait(&barrier); |
| 435 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 436 | ret = nc_server_tls_endpt_add_trusted_cert_list("quaternary", "trusted_cert_list1"); |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 437 | nc_assert(!ret); |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 438 | |
| 439 | nc_thread_destroy(); |
| 440 | return NULL; |
| 441 | } |
| 442 | |
| 443 | static void * |
| 444 | tls_endpt_set_trusted_ca_paths_thread(void *arg) |
| 445 | { |
| 446 | (void)arg; |
| 447 | int ret; |
| 448 | |
| 449 | pthread_barrier_wait(&barrier); |
| 450 | |
Michal Vasko | b477b22 | 2016-02-09 14:30:15 +0100 | [diff] [blame] | 451 | ret = nc_server_tls_endpt_set_trusted_ca_paths("quaternary", TESTS_DIR"/data/serverca.pem", "data"); |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 452 | nc_assert(!ret); |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 453 | |
| 454 | nc_thread_destroy(); |
| 455 | return NULL; |
| 456 | } |
| 457 | |
| 458 | static void * |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 459 | tls_endpt_del_trusted_cert_list_thread(void *arg) |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 460 | { |
| 461 | (void)arg; |
| 462 | |
| 463 | pthread_barrier_wait(&barrier); |
| 464 | |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 465 | nc_server_tls_endpt_del_trusted_cert_list("quaternary", "trusted_cert_list1"); |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 466 | |
| 467 | return NULL; |
| 468 | } |
| 469 | |
| 470 | static void * |
| 471 | tls_endpt_set_crl_paths_thread(void *arg) |
| 472 | { |
| 473 | (void)arg; |
| 474 | int ret; |
| 475 | |
| 476 | pthread_barrier_wait(&barrier); |
| 477 | |
| 478 | ret = nc_server_tls_endpt_set_crl_paths("quaternary", NULL, "data"); |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 479 | nc_assert(!ret); |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 480 | |
| 481 | nc_thread_destroy(); |
| 482 | return NULL; |
| 483 | } |
| 484 | |
| 485 | static void * |
| 486 | tls_endpt_clear_crls_thread(void *arg) |
| 487 | { |
| 488 | (void)arg; |
| 489 | |
| 490 | pthread_barrier_wait(&barrier); |
| 491 | |
| 492 | nc_server_tls_endpt_clear_crls("quaternary"); |
| 493 | |
| 494 | return NULL; |
| 495 | } |
| 496 | |
| 497 | static void * |
| 498 | tls_endpt_add_ctn_thread(void *arg) |
| 499 | { |
| 500 | (void)arg; |
| 501 | int ret; |
| 502 | |
| 503 | pthread_barrier_wait(&barrier); |
| 504 | |
Michal Vasko | 3cf4aaa | 2017-02-01 15:03:36 +0100 | [diff] [blame] | 505 | 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 Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 506 | NC_TLS_CTN_SAN_IP_ADDRESS, NULL); |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 507 | nc_assert(!ret); |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 508 | |
| 509 | return NULL; |
| 510 | } |
| 511 | |
| 512 | static void * |
| 513 | tls_endpt_del_ctn_thread(void *arg) |
| 514 | { |
| 515 | (void)arg; |
| 516 | int ret; |
| 517 | |
| 518 | pthread_barrier_wait(&barrier); |
| 519 | |
Michal Vasko | 338f847 | 2016-10-13 15:01:27 +0200 | [diff] [blame] | 520 | ret = nc_server_tls_endpt_del_ctn("main_tls", -1, NULL, NC_TLS_CTN_SAN_ANY, NULL); |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 521 | nc_assert(!ret); |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 522 | |
| 523 | return NULL; |
| 524 | } |
| 525 | |
| 526 | static void * |
| 527 | tls_client_thread(void *arg) |
| 528 | { |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 529 | int ret, read_pipe = *(int *)arg; |
| 530 | char buf[9]; |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 531 | struct nc_session *session; |
| 532 | |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 533 | ret = read(read_pipe, buf, 9); |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 534 | nc_assert(ret == 9); |
| 535 | nc_assert(!strncmp(buf, "tls_ready", 9)); |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 536 | |
Michal Vasko | b477b22 | 2016-02-09 14:30:15 +0100 | [diff] [blame] | 537 | ret = nc_client_tls_set_cert_key_paths(TESTS_DIR"/data/client.crt", TESTS_DIR"/data/client.key"); |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 538 | nc_assert(!ret); |
Michal Vasko | bc2ae55 | 2016-02-09 14:38:47 +0100 | [diff] [blame] | 539 | ret = nc_client_tls_set_trusted_ca_paths(NULL, TESTS_DIR"/data"); |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 540 | nc_assert(!ret); |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 541 | |
| 542 | session = nc_connect_tls("127.0.0.1", 6501, NULL); |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 543 | nc_assert(session); |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 544 | |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 545 | nc_session_free(session, NULL); |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 546 | |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 547 | fprintf(stdout, "TLS client finished.\n"); |
| 548 | |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 549 | nc_thread_destroy(); |
| 550 | return NULL; |
| 551 | } |
| 552 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 553 | #endif /* NC_ENABLED_TLS */ |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 554 | |
| 555 | static void *(*thread_funcs[])(void *) = { |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 556 | #if defined(NC_ENABLED_SSH) || defined(NC_ENABLED_TLS) |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 557 | server_thread, |
| 558 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 559 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 8727e43 | 2016-10-13 15:44:10 +0200 | [diff] [blame] | 560 | add_endpt_thread, |
| 561 | del_endpt_thread, |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 562 | ssh_endpt_set_hostkey_thread, |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 563 | ssh_endpt_set_auth_methods_thread, |
| 564 | ssh_endpt_set_auth_attempts_thread, |
| 565 | ssh_endpt_set_auth_timeout_thread, |
| 566 | ssh_endpt_add_authkey_thread, |
| 567 | ssh_endpt_del_authkey_thread, |
| 568 | #endif |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 569 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 8727e43 | 2016-10-13 15:44:10 +0200 | [diff] [blame] | 570 | endpt_set_address_thread, |
| 571 | endpt_set_port_thread, |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 572 | tls_endpt_set_server_cert_thread, |
| 573 | tls_endpt_add_trusted_cert_list_thread, |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 574 | tls_endpt_set_trusted_ca_paths_thread, |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 575 | tls_endpt_del_trusted_cert_list_thread, |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 576 | tls_endpt_set_crl_paths_thread, |
| 577 | tls_endpt_clear_crls_thread, |
| 578 | tls_endpt_add_ctn_thread, |
| 579 | tls_endpt_del_ctn_thread, |
| 580 | #endif |
| 581 | }; |
| 582 | |
| 583 | const int thread_count = sizeof thread_funcs / sizeof *thread_funcs; |
| 584 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 585 | #if defined(NC_ENABLED_SSH) && defined(NC_ENABLED_TLS) |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 586 | const int client_count = 2; |
| 587 | pid_t pids[2]; |
| 588 | int pipes[4]; |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 589 | #else |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 590 | const int client_count = 1; |
| 591 | pid_t pids[1]; |
| 592 | int pipes[2]; |
| 593 | #endif |
| 594 | |
| 595 | static void |
| 596 | client_fork(void) |
| 597 | { |
| 598 | int ret, clients = 0; |
| 599 | |
| 600 | #ifdef NC_ENABLED_SSH |
| 601 | pipe(pipes + clients * 2); |
| 602 | |
| 603 | if (!(pids[clients] = fork())) { |
| 604 | nc_client_init(); |
| 605 | |
Michal Vasko | 7f1c0ef | 2016-03-11 11:13:06 +0100 | [diff] [blame] | 606 | ret = nc_client_set_schema_searchpath(TESTS_DIR"/../schemas"); |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 607 | nc_assert(!ret); |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 608 | |
| 609 | /* close write */ |
| 610 | close(pipes[clients * 2 + 1]); |
| 611 | ssh_client_thread(&pipes[clients * 2]); |
| 612 | close(pipes[clients * 2]); |
| 613 | nc_client_destroy(); |
| 614 | exit(0); |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 615 | } |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 616 | /* close read */ |
| 617 | close(pipes[clients * 2]); |
| 618 | |
| 619 | ++clients; |
| 620 | #endif |
| 621 | |
| 622 | #ifdef NC_ENABLED_TLS |
| 623 | pipe(pipes + clients * 2); |
| 624 | |
| 625 | if (!(pids[clients] = fork())) { |
| 626 | nc_client_init(); |
| 627 | |
Michal Vasko | 7f1c0ef | 2016-03-11 11:13:06 +0100 | [diff] [blame] | 628 | ret = nc_client_set_schema_searchpath(TESTS_DIR"/../schemas"); |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 629 | nc_assert(!ret); |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 630 | |
| 631 | /* close write */ |
| 632 | close(pipes[clients * 2 + 1]); |
| 633 | tls_client_thread(&pipes[clients * 2]); |
| 634 | close(pipes[clients * 2]); |
| 635 | nc_client_destroy(); |
| 636 | exit(0); |
| 637 | } |
| 638 | /* close read */ |
| 639 | close(pipes[clients * 2]); |
| 640 | |
| 641 | ++clients; |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 642 | #endif |
| 643 | } |
| 644 | |
| 645 | int |
| 646 | main(void) |
| 647 | { |
| 648 | struct ly_ctx *ctx; |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 649 | int ret, i, clients = 0; |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 650 | pthread_t tids[thread_count]; |
| 651 | |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 652 | nc_verbosity(NC_VERB_VERBOSE); |
| 653 | |
| 654 | client_fork(); |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 655 | |
Radek Krejci | 3222b7d | 2017-09-21 16:04:30 +0200 | [diff] [blame] | 656 | ctx = ly_ctx_new(TESTS_DIR"/../schemas", 0); |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 657 | nc_assert(ctx); |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 658 | ly_ctx_load_module(ctx, "ietf-netconf", NULL); |
| 659 | nc_server_init(ctx); |
| 660 | |
| 661 | pthread_barrier_init(&barrier, NULL, thread_count); |
| 662 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 663 | #ifdef NC_ENABLED_SSH |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 664 | /* set callback */ |
| 665 | nc_server_ssh_set_hostkey_clb(clb_hostkeys, NULL, NULL); |
| 666 | |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 667 | /* do first, so that client can connect on SSH */ |
Michal Vasko | 338f847 | 2016-10-13 15:01:27 +0200 | [diff] [blame] | 668 | ret = nc_server_add_endpt("main_ssh", NC_TI_LIBSSH); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 669 | nc_assert(!ret); |
Michal Vasko | 338f847 | 2016-10-13 15:01:27 +0200 | [diff] [blame] | 670 | ret = nc_server_endpt_set_address("main_ssh", "0.0.0.0"); |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 671 | nc_assert(!ret); |
Michal Vasko | 338f847 | 2016-10-13 15:01:27 +0200 | [diff] [blame] | 672 | ret = nc_server_endpt_set_port("main_ssh", 6001); |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 673 | nc_assert(!ret); |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 674 | ret = nc_server_ssh_add_authkey_path(TESTS_DIR"/data/key_dsa.pub", "test"); |
Michal Vasko | 338f847 | 2016-10-13 15:01:27 +0200 | [diff] [blame] | 675 | nc_assert(!ret); |
Michal Vasko | 7d25588 | 2017-02-09 13:35:08 +0100 | [diff] [blame] | 676 | ret = nc_server_ssh_endpt_add_hostkey("main_ssh", "key_rsa", -1); |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 677 | nc_assert(!ret); |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 678 | |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 679 | /* client ready */ |
| 680 | ret = write(pipes[clients * 2 + 1], "ssh_ready", 9); |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 681 | nc_assert(ret == 9); |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 682 | ++clients; |
| 683 | |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 684 | /* for ssh_endpt_del_authkey */ |
Michal Vasko | 17dfda9 | 2016-12-01 14:06:16 +0100 | [diff] [blame] | 685 | ret = nc_server_ssh_add_authkey_path(TESTS_DIR"/data/key_ecdsa.pub", "test2"); |
Michal Vasko | 338f847 | 2016-10-13 15:01:27 +0200 | [diff] [blame] | 686 | nc_assert(!ret); |
| 687 | |
| 688 | ret = nc_server_add_endpt("secondary", NC_TI_LIBSSH); |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 689 | nc_assert(!ret); |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 690 | #endif |
| 691 | |
Radek Krejci | 53691be | 2016-02-22 13:58:37 +0100 | [diff] [blame] | 692 | #ifdef NC_ENABLED_TLS |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 693 | /* set callbacks */ |
| 694 | nc_server_tls_set_server_cert_clb(clb_server_cert, NULL, NULL); |
| 695 | nc_server_tls_set_trusted_cert_list_clb(clb_trusted_cert_lists, NULL, NULL); |
| 696 | |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 697 | /* do first, so that client can connect on TLS */ |
Michal Vasko | 338f847 | 2016-10-13 15:01:27 +0200 | [diff] [blame] | 698 | ret = nc_server_add_endpt("main_tls", NC_TI_OPENSSL); |
Michal Vasko | e2713da | 2016-08-22 16:06:40 +0200 | [diff] [blame] | 699 | nc_assert(!ret); |
Michal Vasko | 338f847 | 2016-10-13 15:01:27 +0200 | [diff] [blame] | 700 | ret = nc_server_endpt_set_address("main_tls", "0.0.0.0"); |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 701 | nc_assert(!ret); |
Michal Vasko | 338f847 | 2016-10-13 15:01:27 +0200 | [diff] [blame] | 702 | ret = nc_server_endpt_set_port("main_tls", 6501); |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 703 | nc_assert(!ret); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 704 | ret = nc_server_tls_endpt_set_server_cert("main_tls", "main_cert"); |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 705 | nc_assert(!ret); |
Michal Vasko | 4c1fb49 | 2017-01-30 14:31:07 +0100 | [diff] [blame] | 706 | ret = nc_server_tls_endpt_add_trusted_cert_list("main_tls", "client_cert_list"); |
Michal Vasko | 338f847 | 2016-10-13 15:01:27 +0200 | [diff] [blame] | 707 | nc_assert(!ret); |
| 708 | ret = nc_server_tls_endpt_add_ctn("main_tls", 0, "02:D3:03:0E:77:21:E2:14:1F:E5:75:48:98:6B:FD:8A:63:BB:DE:40:34", NC_TLS_CTN_SPECIFIED, "test"); |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 709 | nc_assert(!ret); |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 710 | |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 711 | /* client ready */ |
| 712 | ret = write(pipes[clients * 2 + 1], "tls_ready", 9); |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 713 | nc_assert(ret == 9); |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 714 | ++clients; |
| 715 | |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 716 | /* for tls_endpt_del_ctn */ |
Michal Vasko | 3cf4aaa | 2017-02-01 15:03:36 +0100 | [diff] [blame] | 717 | 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 Vasko | 338f847 | 2016-10-13 15:01:27 +0200 | [diff] [blame] | 718 | nc_assert(!ret); |
| 719 | |
| 720 | ret = nc_server_add_endpt("quaternary", NC_TI_OPENSSL); |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 721 | nc_assert(!ret); |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 722 | #endif |
| 723 | |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 724 | /* threads'n'stuff */ |
| 725 | ret = 0; |
| 726 | for (i = 0; i < thread_count; ++i) { |
| 727 | ret += pthread_create(&tids[i], NULL, thread_funcs[i], NULL); |
| 728 | } |
Michal Vasko | 64b31a2 | 2016-03-03 11:00:00 +0100 | [diff] [blame] | 729 | nc_assert(!ret); |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 730 | |
| 731 | /* cleanup */ |
| 732 | for (i = 0; i < thread_count; ++i) { |
| 733 | pthread_join(tids[i], NULL); |
| 734 | } |
Michal Vasko | a7b8ca5 | 2016-03-01 12:09:29 +0100 | [diff] [blame] | 735 | for (i = 0; i < client_count; ++i) { |
| 736 | waitpid(pids[i], NULL, 0); |
| 737 | close(pipes[i * 2 + 1]); |
| 738 | } |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 739 | |
| 740 | pthread_barrier_destroy(&barrier); |
| 741 | |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 742 | nc_server_destroy(); |
| 743 | ly_ctx_destroy(ctx, NULL); |
| 744 | |
Michal Vasko | 734360b | 2016-02-08 15:38:25 +0100 | [diff] [blame] | 745 | return 0; |
| 746 | } |