blob: 8c24a6904389499b0dd633538edb0ec8e1764916 [file] [log] [blame]
romanc1d2b092023-02-02 08:58:27 +01001/**
2 * @file test_pam.c
3 * @author Roman Janota <xjanot04@fit.vutbr.cz>
4 * @brief libnetconf2 Linux PAM keyboard-interactive authentication test
5 *
6 * @copyright
7 * Copyright (c) 2022 CESNET, z.s.p.o.
8 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
roman9b1379c2023-03-31 10:11:10 +020015#define _GNU_SOURCE
romanc1d2b092023-02-02 08:58:27 +010016
17#include <errno.h>
18#include <pthread.h>
romanc1d2b092023-02-02 08:58:27 +010019#include <setjmp.h>
roman9b1379c2023-03-31 10:11:10 +020020#include <stdarg.h>
romanc1d2b092023-02-02 08:58:27 +010021#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
roman9b1379c2023-03-31 10:11:10 +020024#include <unistd.h>
romanc1d2b092023-02-02 08:58:27 +010025
26#include <cmocka.h>
27
romanc1d2b092023-02-02 08:58:27 +010028#include "tests/config.h"
29
roman83683fb2023-02-24 09:15:23 +010030#define NC_ACCEPT_TIMEOUT 2000
31#define NC_PS_POLL_TIMEOUT 2000
romanc1d2b092023-02-02 08:58:27 +010032#define BACKOFF_TIMEOUT_USECS 100
33
34struct ly_ctx *ctx;
35int flag = 0;
36
37const char *data =
38 "<netconf-server xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-server\" xmlns:yang=\"urn:ietf:params:xml:ns:yang:1\" yang:operation=\"none\">\n"
39 " <listen yang:operation=\"create\">\n"
40 " <idle-timeout>10</idle-timeout>\n"
41 " <endpoint>\n"
42 " <name>default-ssh</name>\n"
43 " <ssh>\n"
44 " <tcp-server-parameters>\n"
45 " <local-address>127.0.0.1</local-address>\n"
46 " <local-port>10005</local-port>\n"
47 " </tcp-server-parameters>\n"
48 " <ssh-server-parameters>\n"
49 " <server-identity>\n"
50 " <host-key>\n"
51 " <name>key</name>\n"
52 " <public-key>\n"
53 " <local-definition>\n"
54 " <public-key-format xmlns:ct=\"urn:ietf:params:xml:ns:yang:ietf-crypto-types\">ct:ssh-public-key-format</public-key-format>\n"
55 " <public-key>MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA6ojtjfDmvyQP1ZkIwBpr97eKDuebvpoglRHRdvVuTpf/gU1VArAQmwGh05i6lm8TkVl1noMlIxLJDcWslaeVn6KyvsX0HhsQtXwqPqwka5UCv6alwf/ivAvcNpcX1j0t/uIGCI4dSiKnzQCyf0FTirzQkjrDZUd3meDhNQTruCalGV4gfNWIq3e1oGuwAn1tLlu9oTrE4HzMpgbNEU6wNmsSqpwGxUhYLoSaM7b0dLmqP+ZczSS0Uac0PFNkehGQ2CYIT80f580o4XGtoLCUUGkp6YCTL4Z2CeBEaJABWjDIDH+dKYIUBqUpz4Th12gXAP+h+3qI6+9eppeHrfrzARDsfLjwUNxQJse1QSArjAytf0FKtGHrORc7W0TiCFvR0zaoUNLTKk7enTiRQ9rfWZOAu44fUvPCaXDE6zXXeaVgoKCo4VHlho36erUcjlEBM+jk28IykbZGtBb6igKvYa1tPSgeYm/zJoFVjQcnr14uci/ft1+Na+hOIEoEEiKxcAPk2b2vBKNlRIW7WLJ3u7ZiuQEJTNm6+3cE4+lfwaBCBqBToE+dpzvoUXoMyFFReUFd1O5axu4fXgt00jMaOQxmE0v9OmR/pL/PWIflVF4Zz5yVONYaDVc7l+veY0oEZruEPJ0hlEgxuCzLrcMhjufl2qE2Q7fQIaav/1NqBVkCAwEAAQ==</public-key>\n"
56 " <private-key-format xmlns:ct=\"urn:ietf:params:xml:ns:yang:ietf-crypto-types\">ct:rsa-private-key-format</private-key-format>\n"
57 " <cleartext-private-key>MIIJKAIBAAKCAgEA6ojtjfDmvyQP1ZkIwBpr97eKDuebvpoglRHRdvVuTpf/gU1VArAQmwGh05i6lm8TkVl1noMlIxLJDcWslaeVn6Kyvs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QTwTdRIjviIOAo7fgQn2RyX0k+zXE8/7zqjqvji9zyemdNu8we4uJICSntyvJwkbj/hrufTKEnBrwXpzfVn1EsH+6w32ZPBGLUhT75txJ8r56SRq7l1XPU9vxovmT+lSMFF/Y0j1MbHWnds5H1shoFPNtYTvWBL/gfPHjIc+H23zsiu3XlZAoIBAC2xB/Pnpoi9vOUMiqFH36AXtYa1DURy+AqCFlYlClMvb7YgvQ1w1eJvnwrHSLk7HdKhnwGsLPduuRRH8q0n/osnoOutSQroE0n41UyIv2ZNccRwNmSzQcairBu2dSz02hlsh2otNl5IuGpOqXyPjXBpW4qGD6n2tH7THALnLC0BHtTSQVQsJsRM3gX39LoiWvLDp2qJvplm6rTpi8Rgap6rZSqHe1yNKIxxD2vlr/WY9SMgLXYASO4SSBz9wfGOmQIPk6KXNJkdV4kC7nNjIi75iwLLCgjHgUiHTrDq5sWekpeNnUoWsinbTsdsjnv3zHG9GyiClyLGxMbs4M5eyYECggEBAKuC8ZMpdIrjk6tERYB6g0LnQ7mW8XYbDFAmLYMLs9yfG2jcjVbsW9Kugsr+3poUUv/q+hNO3jfY4HazhZDa0MalgNPoSwr/VNRnkck40x2ovFb989J7yl++zTrnIrax9XRH1V0cNu+Kj7OMwZ2RRfbNv5JBdOZPvkfqyIKFmbQgYbtD66rHuzNOfJpzqr/WVLO57/zzW8245NKG2B6B0oXkei/KqDY0DAbHR3i3EOj1NPtVI1FC/xX8R9BREaid458bqoHJKuInrGcBjaUI9Cvymv8TbstUgD6NPbJR4Sm6vrLeUqzjWZP3t1+Z6DjXmnpR2vvhMU/FWb//21p/88o=</cleartext-private-key>\n"
58 " </local-definition>\n"
59 " </public-key>\n"
60 " </host-key>\n"
61 " </server-identity>\n"
62 " <client-authentication>\n"
63 " <users>\n"
64 " <user>\n"
65 " <name>test1</name>\n"
66 " <public-keys>\n"
67 " <local-definition>\n"
68 " <public-key>\n"
69 " <name>client</name>\n"
70 " <public-key-format xmlns:ct=\"urn:ietf:params:xml:ns:yang:ietf-crypto-types\">ct:ssh-public-key-format</public-key-format>\n"
71 " <public-key>AAAAB3NzaC1yc2EAAAADAQABAAABAQDPavVALiM7QwTIUAndO8E9GOkSDQWjuEwkzbJ3kOBPa7kkq71UOZFeecDjFb9eipkljfFys/JYHGQaYVF8/svT0KV5h7HlutRdF6yvqSEbjpbTORb27pdHX3iFEyDCwCIoq9vMeX+wyXnteyn01GpIL0ig0WAnvkqX/SPjuplX5ZItUSr0MhXM7fNSX50BD6G8IO0/djUcdMUcjTjGv73SxB9ZzLvxnhXuUJbzEJJJLj6qajyEIVaJSa73vA33JCD8qzarrsuITojVLPDFmeHwSAoB5dP86yop6e6ypuXzKxxef6yNXcE8oTj8UFYBIXsgIP2nBvWk41EaK0Vk3YFl</public-key>\n"
72 " </public-key>\n"
73 " </local-definition>\n"
74 " </public-keys>\n"
75 " </user>\n"
76 " <user>\n"
77 " <name>test2</name>\n"
78 " <public-keys>\n"
79 " <local-definition>\n"
80 " <public-key>\n"
81 " <name>client</name>\n"
82 " <public-key-format xmlns:ct=\"urn:ietf:params:xml:ns:yang:ietf-crypto-types\">ct:ssh-public-key-format</public-key-format>\n"
83 " <public-key>AAAAB3NzaC1yc2EAAAADAQABAAABAQDPavVALiM7QwTIUAndO8E9GOkSDQWjuEwkzbJ3kOBPa7kkq71UOZFeecDjFb9eipkljfFys/JYHGQaYVF8/svT0KV5h7HlutRdF6yvqSEbjpbTORb27pdHX3iFEyDCwCIoq9vMeX+wyXnteyn01GpIL0ig0WAnvkqX/SPjuplX5ZItUSr0MhXM7fNSX50BD6G8IO0/djUcdMUcjTjGv73SxB9ZzLvxnhXuUJbzEJJJLj6qajyEIVaJSa73vA33JCD8qzarrsuITojVLPDFmeHwSAoB5dP86yop6e6ypuXzKxxef6yNXcE8oTj8UFYBIXsgIP2nBvWk41EaK0Vk3YFl</public-key>\n"
84 " </public-key>\n"
85 " </local-definition>\n"
86 " </public-keys>\n"
87 " </user>\n"
88 " </users>\n"
89 " </client-authentication>\n"
90 " <transport-params>\n"
91 " <host-key>\n"
92 " <host-key-alg xmlns:sshpka=\"urn:ietf:params:xml:ns:yang:iana-ssh-public-key-algs\">sshpka:rsa-sha2-512</host-key-alg>\n"
93 " </host-key>\n"
94 " <key-exchange>\n"
95 " <key-exchange-alg xmlns:sshkea=\"urn:ietf:params:xml:ns:yang:iana-ssh-key-exchange-algs\">sshkea:curve25519-sha256</key-exchange-alg>\n"
96 " </key-exchange>\n"
97 " <encryption>\n"
98 " <encryption-alg xmlns:sshea=\"urn:ietf:params:xml:ns:yang:iana-ssh-encryption-algs\">sshea:aes256-ctr</encryption-alg>\n"
99 " </encryption>\n"
100 " <mac>\n"
101 " <mac-alg xmlns:sshma=\"urn:ietf:params:xml:ns:yang:iana-ssh-mac-algs\">sshma:hmac-sha2-512</mac-alg>\n"
102 " </mac>\n"
103 " </transport-params>\n"
104 " </ssh-server-parameters>\n"
105 " </ssh>\n"
106 " </endpoint>\n"
107 " </listen>\n"
108 "</netconf-server>\n";
109
110static void *
111server_thread(void *arg)
112{
113 int ret, del_session_count = 0, sleep_count = 0;
114 NC_MSG_TYPE msgtype;
115 struct nc_session *session, *new_session;
116 struct nc_pollsession *ps;
117
118 (void) arg;
119
120 ps = nc_ps_new();
121 assert_non_null(ps);
122
123 while (del_session_count < 2) {
124 msgtype = nc_accept(0, ctx, &session);
125
126 if (msgtype == NC_MSG_HELLO) {
127 ret = nc_ps_add_session(ps, session);
128 assert_int_equal(ret, 0);
129 }
130
131 ret = nc_ps_poll(ps, 0, &new_session);
132
133 if (ret & NC_PSPOLL_SESSION_TERM) {
134 nc_ps_del_session(ps, new_session);
135 nc_session_free(new_session, NULL);
136 del_session_count++;
137 } else if (ret & NC_PSPOLL_SSH_CHANNEL) {
138 msgtype = nc_session_accept_ssh_channel(session, &new_session);
139 if (msgtype == NC_MSG_HELLO) {
140 ret = nc_ps_add_session(ps, new_session);
141 assert_int_equal(ret, 0);
142 }
143 } else if (ret & NC_PS_POLL_TIMEOUT) {
144 usleep(BACKOFF_TIMEOUT_USECS);
145 sleep_count++;
146 assert_int_not_equal(sleep_count, 50000);
147 }
148 }
149
150 nc_ps_free(ps);
151 nc_thread_destroy();
152 return NULL;
153}
154
155static int
156ssh_hostkey_check_clb(const char *hostname, ssh_session session, void *priv)
157{
158 (void)hostname;
159 (void)session;
160 (void)priv;
161 /* redundant in this test, nonetheless this callback has to be set */
162
163 return 0;
164}
165
166static void *
167client_thread(void *arg)
168{
169 (void) arg;
170 int ret;
171 struct nc_session *session_cl1, *session_cl2;
172
173 /* initialize client */
174 nc_client_init();
175 ret = nc_client_set_schema_searchpath(MODULES_DIR);
176 assert_int_equal(ret, 0);
177 /* skip the knownhost check */
178 nc_client_ssh_set_auth_hostkey_check_clb(ssh_hostkey_check_clb, NULL);
179
180 nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PUBLICKEY, 1);
181 nc_client_ssh_set_auth_pref(NC_SSH_AUTH_PASSWORD, -1);
182 nc_client_ssh_set_auth_pref(NC_SSH_AUTH_INTERACTIVE, -1);
183
184 ret = nc_client_ssh_add_keypair(TESTS_DIR "/data/key_rsa.pub", TESTS_DIR "/data/key_rsa");
185 assert_int_equal(ret, 0);
186
187 ret = nc_client_ssh_set_username("test1");
188 assert_int_equal(ret, 0);
189
190 session_cl1 = nc_connect_ssh("127.0.0.1", 10005, NULL);
191 assert_non_null(session_cl1);
192
193 ret = nc_client_ssh_set_username("test2");
194 assert_int_equal(ret, 0);
195
196 session_cl2 = nc_connect_ssh_channel(session_cl1, NULL);
197 assert_non_null(session_cl2);
198
199 nc_client_destroy();
200 nc_session_free(session_cl1, NULL);
201 nc_session_free(session_cl2, NULL);
202 nc_thread_destroy();
203 return NULL;
204}
205
206static void
207test_nc_two_channels(void **state)
208{
209 int ret, i;
210 pthread_t tids[2];
211
212 (void) state;
213
214 ret = pthread_create(&tids[0], NULL, client_thread, NULL);
215 assert_int_equal(ret, 0);
216 ret = pthread_create(&tids[1], NULL, server_thread, NULL);
217 assert_int_equal(ret, 0);
218
219 for (i = 0; i < 2; i++) {
220 pthread_join(tids[i], NULL);
221 }
222}
223
224static int
225setup_f(void **state)
226{
227 int ret;
228 struct lyd_node *tree;
229
230 (void) state;
231
232 nc_verbosity(NC_VERB_VERBOSE);
233
234 ret = ly_ctx_new(MODULES_DIR, 0, &ctx);
235 assert_int_equal(ret, 0);
236
237 ret = nc_server_init_ctx(&ctx);
238 assert_int_equal(ret, 0);
239
240 ret = nc_server_config_load_modules(&ctx);
241 assert_int_equal(ret, 0);
242
243 ret = lyd_parse_data_mem(ctx, data, LYD_XML, LYD_PARSE_NO_STATE | LYD_PARSE_STRICT, LYD_VALIDATE_NO_STATE, &tree);
244 assert_int_equal(ret, 0);
245
246 ret = nc_server_config_setup(tree);
247 assert_int_equal(ret, 0);
248
249 ret = nc_server_init();
250 assert_int_equal(ret, 0);
251
252 lyd_free_all(tree);
253
254 return 0;
255}
256
257static int
258teardown_f(void **state)
259{
260 (void) state;
261
262 nc_server_destroy();
263 ly_ctx_destroy(ctx);
264
265 return 0;
266}
267
268int
269main(void)
270{
271 const struct CMUnitTest tests[] = {
272 cmocka_unit_test_setup_teardown(test_nc_two_channels, setup_f, teardown_f),
273 };
274
275 setenv("CMOCKA_TEST_ABORT", "1", 1);
276 return cmocka_run_group_tests(tests, NULL, NULL);
277}