blob: 561dfea52ae6fb415290f6cceea4fe98b20939c9 [file] [log] [blame]
roman7d136682023-12-14 10:43:36 +01001/**
2 * @file test_pam.c
3 * @author Roman Janota <xjanot04@fit.vutbr.cz>
4 * @brief libnetconf2 SSH Keyboard Interactive auth using PAM test
5 *
6 * @copyright
7 * Copyright (c) 2023 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 */
15
16#define _GNU_SOURCE
17
18#include <errno.h>
19#include <pthread.h>
20#include <setjmp.h>
21#include <stdarg.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26#include <security/pam_appl.h>
27
28#include <cmocka.h>
29
30#include "tests/config.h"
31
32#define NC_ACCEPT_TIMEOUT 2000
33#define NC_PS_POLL_TIMEOUT 2000
34
35struct ly_ctx *ctx;
36
37struct test_state {
38 pthread_barrier_t barrier;
39};
40
41/* mock pam_start to just call pam_start_confdir instead */
42int __real_pam_start(const char *service_name, const char *user, const struct pam_conv *pam_conversation, pam_handle_t **pamh);
43int
44__wrap_pam_start(const char *service_name, const char *user, const struct pam_conv *pam_conversation, pam_handle_t **pamh)
45{
46 return pam_start_confdir(service_name, user, pam_conversation, BUILD_DIR "/tests", pamh);
47}
48
49static void *
50server_thread(void *arg)
51{
52 int ret;
53 NC_MSG_TYPE msgtype;
54 struct nc_session *session;
55 struct nc_pollsession *ps;
56 struct test_state *state = arg;
57
58 ps = nc_ps_new();
59 assert_non_null(ps);
60
61 /* accept a session and add it to the poll session structure */
62 pthread_barrier_wait(&state->barrier);
63 msgtype = nc_accept(NC_ACCEPT_TIMEOUT, ctx, &session);
64 assert_int_equal(msgtype, NC_MSG_HELLO);
65
66 ret = nc_ps_add_session(ps, session);
67 assert_int_equal(ret, 0);
68
69 do {
70 ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT, NULL);
71 assert_int_equal(ret & NC_PSPOLL_RPC, NC_PSPOLL_RPC);
72 } while (!(ret & NC_PSPOLL_SESSION_TERM));
73
74 nc_ps_clear(ps, 1, NULL);
75 nc_ps_free(ps);
76 return NULL;
77}
78
79static char *
80auth_interactive(const char *UNUSED(auth_name), const char *UNUSED(instruction),
81 const char *prompt, int UNUSED(echo), void *UNUSED(priv))
82{
83 /* send the replies to keyboard-interactive authentication */
84 if (strstr(prompt, "backwards")) {
85 return strdup("tset");
86 } else if (strstr(prompt, "1+1")) {
87 return strdup("2");
88 } else {
89 return NULL;
90 }
91}
92
93static void *
94client_thread(void *arg)
95{
96 int ret;
97 struct nc_session *session = NULL;
98 struct test_state *state = arg;
99
100 /* skip all hostkey and known_hosts checks */
101 nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP);
102
103 ret = nc_client_set_schema_searchpath(MODULES_DIR);
104 assert_int_equal(ret, 0);
105
106 ret = nc_client_ssh_set_username("test");
107 assert_int_equal(ret, 0);
108
109 /* set keyboard-interactive authentication callback */
110 nc_client_ssh_set_auth_interactive_clb(auth_interactive, NULL);
111
112 pthread_barrier_wait(&state->barrier);
Jan Kundrátf8d9e8d2024-04-08 12:13:02 +0200113 session = nc_connect_ssh("127.0.0.1", TEST_PORT, NULL);
roman7d136682023-12-14 10:43:36 +0100114 assert_non_null(session);
115
116 nc_session_free(session, NULL);
117 return NULL;
118}
119
120static void
121test_nc_pam(void **state)
122{
123 int ret, i;
124 pthread_t tids[2];
125
126 assert_non_null(state);
127
128 ret = pthread_create(&tids[0], NULL, client_thread, *state);
129 assert_int_equal(ret, 0);
130 ret = pthread_create(&tids[1], NULL, server_thread, *state);
131 assert_int_equal(ret, 0);
132
133 for (i = 0; i < 2; i++) {
134 pthread_join(tids[i], NULL);
135 }
136}
137
138static int
139setup_f(void **state)
140{
141 int ret;
142 struct lyd_node *tree = NULL;
143 struct test_state *test_state;
144
145 nc_verbosity(NC_VERB_VERBOSE);
146
147 /* init barrier */
148 test_state = malloc(sizeof *test_state);
149 assert_non_null(test_state);
150
151 ret = pthread_barrier_init(&test_state->barrier, NULL, 2);
152 assert_int_equal(ret, 0);
153
154 *state = test_state;
155
156 ret = ly_ctx_new(MODULES_DIR, 0, &ctx);
157 assert_int_equal(ret, 0);
158
159 ret = nc_server_init_ctx(&ctx);
160 assert_int_equal(ret, 0);
161
162 ret = nc_server_config_load_modules(&ctx);
163 assert_int_equal(ret, 0);
164
romand8973d12024-04-25 14:57:18 +0200165 ret = nc_server_config_add_address_port(ctx, "endpt", NC_TI_SSH, "127.0.0.1", TEST_PORT, &tree);
roman7d136682023-12-14 10:43:36 +0100166 assert_int_equal(ret, 0);
167
168 ret = nc_server_config_add_ssh_hostkey(ctx, "endpt", "hostkey", TESTS_DIR "/data/key_ecdsa", NULL, &tree);
169 assert_int_equal(ret, 0);
170
171 ret = nc_server_config_add_ssh_user_interactive(ctx, "endpt", "test", &tree);
172 assert_int_equal(ret, 0);
173
174 ret = nc_server_ssh_set_pam_conf_filename("netconf.conf");
175 assert_int_equal(ret, 0);
176
177 /* configure the server based on the data */
178 ret = nc_server_config_setup_data(tree);
179 assert_int_equal(ret, 0);
180
181 ret = nc_server_init();
182 assert_int_equal(ret, 0);
183
184 /* initialize client */
185 ret = nc_client_init();
186 assert_int_equal(ret, 0);
187
188 lyd_free_all(tree);
189
190 return 0;
191}
192
193static int
194teardown_f(void **state)
195{
196 int ret = 0;
197 struct test_state *test_state;
198
199 assert_non_null(state);
200 test_state = *state;
201
202 ret = pthread_barrier_destroy(&test_state->barrier);
203 assert_int_equal(ret, 0);
204
205 free(*state);
206 nc_client_destroy();
207 nc_server_destroy();
208 ly_ctx_destroy(ctx);
209
210 return 0;
211}
212
213int
214main(void)
215{
216 const struct CMUnitTest tests[] = {
217 cmocka_unit_test_setup_teardown(test_nc_pam, setup_f, teardown_f)
218 };
219
220 setenv("CMOCKA_TEST_ABORT", "1", 1);
221 return cmocka_run_group_tests(tests, NULL, NULL);
222}