blob: a867c10adc21bb64a30e3cdf128532f256e6e0cd [file] [log] [blame]
roman7d136682023-12-14 10:43:36 +01001/**
2 * @file test_pam.c
roman2ca058f2024-08-14 10:35:36 +02003 * @author Roman Janota <janota@cesnet.cz>
roman7d136682023-12-14 10:43:36 +01004 * @brief libnetconf2 SSH Keyboard Interactive auth using PAM test
5 *
6 * @copyright
roman2ca058f2024-08-14 10:35:36 +02007 * Copyright (c) 2023 - 2024 CESNET, z.s.p.o.
roman7d136682023-12-14 10:43:36 +01008 *
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
romanabe69902024-08-13 14:43:49 +020030#include "ln2_test.h"
roman7d136682023-12-14 10:43:36 +010031
romanabe69902024-08-13 14:43:49 +020032int TEST_PORT = 10050;
33const char *TEST_PORT_STR = "10050";
34
roman7d136682023-12-14 10:43:36 +010035/* mock pam_start to just call pam_start_confdir instead */
36int __real_pam_start(const char *service_name, const char *user, const struct pam_conv *pam_conversation, pam_handle_t **pamh);
37int
38__wrap_pam_start(const char *service_name, const char *user, const struct pam_conv *pam_conversation, pam_handle_t **pamh)
39{
40 return pam_start_confdir(service_name, user, pam_conversation, BUILD_DIR "/tests", pamh);
41}
42
roman7d136682023-12-14 10:43:36 +010043static char *
44auth_interactive(const char *UNUSED(auth_name), const char *UNUSED(instruction),
45 const char *prompt, int UNUSED(echo), void *UNUSED(priv))
46{
47 /* send the replies to keyboard-interactive authentication */
48 if (strstr(prompt, "backwards")) {
49 return strdup("tset");
50 } else if (strstr(prompt, "1+1")) {
51 return strdup("2");
52 } else {
53 return NULL;
54 }
55}
56
57static void *
58client_thread(void *arg)
59{
60 int ret;
61 struct nc_session *session = NULL;
roman2ca058f2024-08-14 10:35:36 +020062 struct ln2_test_ctx *test_ctx = arg;
roman7d136682023-12-14 10:43:36 +010063
64 /* skip all hostkey and known_hosts checks */
65 nc_client_ssh_set_knownhosts_mode(NC_SSH_KNOWNHOSTS_SKIP);
66
67 ret = nc_client_set_schema_searchpath(MODULES_DIR);
68 assert_int_equal(ret, 0);
69
70 ret = nc_client_ssh_set_username("test");
71 assert_int_equal(ret, 0);
72
73 /* set keyboard-interactive authentication callback */
74 nc_client_ssh_set_auth_interactive_clb(auth_interactive, NULL);
75
roman2ca058f2024-08-14 10:35:36 +020076 pthread_barrier_wait(&test_ctx->barrier);
Jan Kundrátf8d9e8d2024-04-08 12:13:02 +020077 session = nc_connect_ssh("127.0.0.1", TEST_PORT, NULL);
roman7d136682023-12-14 10:43:36 +010078 assert_non_null(session);
79
80 nc_session_free(session, NULL);
81 return NULL;
82}
83
84static void
85test_nc_pam(void **state)
86{
87 int ret, i;
88 pthread_t tids[2];
89
90 assert_non_null(state);
91
92 ret = pthread_create(&tids[0], NULL, client_thread, *state);
93 assert_int_equal(ret, 0);
roman2ca058f2024-08-14 10:35:36 +020094 ret = pthread_create(&tids[1], NULL, ln2_glob_test_server_thread, *state);
roman7d136682023-12-14 10:43:36 +010095 assert_int_equal(ret, 0);
96
97 for (i = 0; i < 2; i++) {
98 pthread_join(tids[i], NULL);
99 }
100}
101
102static int
103setup_f(void **state)
104{
105 int ret;
106 struct lyd_node *tree = NULL;
roman2ca058f2024-08-14 10:35:36 +0200107 struct ln2_test_ctx *test_ctx;
roman7d136682023-12-14 10:43:36 +0100108
roman2ca058f2024-08-14 10:35:36 +0200109 ret = ln2_glob_test_setup(&test_ctx);
roman7d136682023-12-14 10:43:36 +0100110 assert_int_equal(ret, 0);
111
roman2ca058f2024-08-14 10:35:36 +0200112 *state = test_ctx;
roman7d136682023-12-14 10:43:36 +0100113
roman2ca058f2024-08-14 10:35:36 +0200114 ret = nc_server_config_add_address_port(test_ctx->ctx, "endpt", NC_TI_SSH, "127.0.0.1", TEST_PORT, &tree);
roman7d136682023-12-14 10:43:36 +0100115 assert_int_equal(ret, 0);
116
roman2ca058f2024-08-14 10:35:36 +0200117 ret = nc_server_config_add_ssh_hostkey(test_ctx->ctx, "endpt", "hostkey", TESTS_DIR "/data/key_ecdsa", NULL, &tree);
roman7d136682023-12-14 10:43:36 +0100118 assert_int_equal(ret, 0);
119
roman2ca058f2024-08-14 10:35:36 +0200120 ret = nc_server_config_add_ssh_user_interactive(test_ctx->ctx, "endpt", "test", &tree);
roman7d136682023-12-14 10:43:36 +0100121 assert_int_equal(ret, 0);
122
123 ret = nc_server_ssh_set_pam_conf_filename("netconf.conf");
124 assert_int_equal(ret, 0);
125
126 /* configure the server based on the data */
127 ret = nc_server_config_setup_data(tree);
128 assert_int_equal(ret, 0);
129
roman7d136682023-12-14 10:43:36 +0100130 lyd_free_all(tree);
131
132 return 0;
133}
134
roman7d136682023-12-14 10:43:36 +0100135int
136main(void)
137{
138 const struct CMUnitTest tests[] = {
roman2ca058f2024-08-14 10:35:36 +0200139 cmocka_unit_test_setup_teardown(test_nc_pam, setup_f, ln2_glob_test_teardown)
roman7d136682023-12-14 10:43:36 +0100140 };
141
romanabe69902024-08-13 14:43:49 +0200142 /* try to get ports from the environment, otherwise use the default */
143 if (ln2_glob_test_get_ports(1, &TEST_PORT, &TEST_PORT_STR)) {
144 return 1;
145 }
146
roman7d136682023-12-14 10:43:36 +0100147 setenv("CMOCKA_TEST_ABORT", "1", 1);
148 return cmocka_run_group_tests(tests, NULL, NULL);
149}