Debian Codegraph @

tests/client/torture_auth_agent_forwarding.c

raw
1 #include "config.h"
2
3 #if !defined(_WIN32) || (defined(WITH_SERVER) && defined(HAVE_PTHREAD))
4
5 #define LIBSSH_STATIC
6
7 #include "torture.h"
8 #include <stdbool.h>
9 #include <stdlib.h> /* For calloc/free */
10
11 #include "libssh/callbacks.h"
12 #include "libssh/libssh.h"
13 #include "libssh/priv.h"
14
15 #include <errno.h>
16 #include <pwd.h>
17 #include <string.h>
18 #include <sys/types.h>
19 #include <unistd.h> /* usleep */
20
21 #ifndef UNIX_PATH_MAX
22 #define UNIX_PATH_MAX 108
23 #endif
24
25 /* struct to store the state of the test */
26 struct agent_callback_state {
27 int called;
28 ssh_session expected_session;
29 ssh_channel created_channel;
30 };
31
32 /* Agent callback function that will be triggered when a channel open request is
33 * received */
34 static ssh_channel agent_callback(ssh_session session, void *userdata)
35 {
36 struct agent_callback_state *state =
37 (struct agent_callback_state *)userdata;
38 ssh_channel channel = NULL; /* Initialize to NULL */
39
40 /* Increment call counter */
41 state->called++;
42
43 /* Verify session matches what we expect */
44 assert_ptr_equal(session, state->expected_session);
45
46 /* Create a new channel for agent forwarding */
47 channel = ssh_channel_new(session);
48 if (channel == NULL) {
49 return NULL;
50 }
51
52 /* Make the channel non-blocking */
53 ssh_channel_set_blocking(channel, 0);
54
55 /* Store the channel for verification and later cleanup */
56 state->created_channel = channel;
57
58 return channel;
59 }
60
61 static int sshd_setup_agent_forwarding(void **state)
62 {
63 int rc;
64
65 /* Use the standard server setup function */
66 torture_setup_sshd_server(state, false);
67
68 /* Override the default configuration with our own, adding agent forwarding
69 * support */
70 rc = torture_update_sshd_config(state, "AllowAgentForwarding yes\n");
71 assert_int_equal(rc, SSH_OK);
72
73 return 0;
74 }
75
76 /* Only free the session - nothing else */
77 static int session_teardown(void **state)
78 {
79 struct torture_state *s = *state;
80
81 if (s != NULL && s->ssh.ssh.session != NULL) {
82 /* Clean up callback resources first */
83 if (s->ssh.ssh.cb_state != NULL) {
84 struct agent_callback_state *cb_state = s->ssh.ssh.cb_state;
85
86 /* Close and free any open channel from the callback */
87 if (cb_state->created_channel != NULL) {
88 ssh_channel_close(cb_state->created_channel);
89 ssh_channel_free(cb_state->created_channel);
90 }
91
92 free(cb_state);
93 s->ssh.ssh.cb_state = NULL;
94 }
95
96 if (s->ssh.ssh.callbacks != NULL) {
97 free(s->ssh.ssh.callbacks);
98 s->ssh.ssh.callbacks = NULL;
99 }
100
101 /* Disconnect and free the session */
102 ssh_disconnect(s->ssh.ssh.session);
103 ssh_free(s->ssh.ssh.session);
104 s->ssh.ssh.session = NULL;
105 }
106
107 return 0;
108 }
109
110 static int torture_teardown_ssh_agent(void **state)
111 {
112 struct torture_state *s = *state;
113 int rc;
114
115 if (s == NULL) {
116 return 0;
117 }
118
119 /* Kill the SSH agent */
120 rc = torture_cleanup_ssh_agent();
121 assert_return_code(rc, errno);
122
123 /* Use the standard teardown function which will properly clean up */
124 torture_teardown_sshd_server(state);
125
126 return 0;
127 }
128
129 /* Test function to verify if agent forwarding callback works */
130 static void torture_auth_agent_forwarding(void **state)
131 {
132 struct torture_state *s = *state;
133 struct agent_callback_state *cb_state;
134 ssh_session session = NULL;
135 ssh_channel channel = NULL; /* Initialize to NULL */
136 int rc;
137 int port = torture_server_port();
138 char buffer[4096] = {0};
139 int nbytes;
140 int max_read_attempts = 10; /* Limit the number of read attempts */
141 int read_count = 0;
142 bool agent_available = false;
143 bool agent_not_available_found = false;
144 size_t exp_socket_len;
145
146 /* The forwarded agent socket is created under the home directory, which
147 * might easily extend the maximum unix domain socket path length.
148 * If we see this, just skip the test as it will not work */
149 exp_socket_len = strlen(BINARYDIR) +
150 strlen("/home/bob/.ssh/agent.1234567890.sshd.XXXXXXXXXX");
151 if (exp_socket_len > UNIX_PATH_MAX) {
152 SSH_LOG(SSH_LOG_WARNING,
153 "The working directory is too long for agent forwarding to work"
154 ": Skipping the test");
155 skip();
156 }
157
158 assert_non_null(s);
159 session = s->ssh.ssh.session;
160 assert_non_null(session);
161
162 /* Get our callback state */
163 cb_state = (struct agent_callback_state *)s->ssh.ssh.cb_state;
164 assert_non_null(cb_state);
165
166 /* Set username */
167 rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER_BOB);
168 assert_ssh_return_code(session, rc);
169
170 /* Set server address */
171 rc = ssh_options_set(session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER);
172 assert_ssh_return_code(session, rc);
173
174 /* Set port */
175 rc = ssh_options_set(session, SSH_OPTIONS_PORT, &port);
176 assert_ssh_return_code(session, rc);
177
178 /* Connect to server */
179 rc = ssh_connect(session);
180 assert_ssh_return_code(session, rc);
181
182 /* Authenticate */
183 rc = ssh_userauth_password(session, NULL, TORTURE_SSH_USER_BOB_PASSWORD);
184 assert_int_equal(rc, SSH_AUTH_SUCCESS);
185
186 /* Create a single channel that we'll use for all tests */
187 channel = ssh_channel_new(session);
188 assert_non_null(channel);
189
190 rc = ssh_channel_open_session(channel);
191 assert_ssh_return_code(session, rc);
192
193 /* Request agent forwarding */
194 rc = ssh_channel_request_auth_agent(channel);
195 assert_ssh_return_code(session, rc);
196
197 /* Running a command that will try to use the SSH agent */
198 rc = ssh_channel_request_exec(
199 channel,
200 "echo 'Simple command'; "
201 "echo 'ENV SSH_AUTH_SOCK=>['$SSH_AUTH_SOCK']<'; " /* Use boundary
202 markers */
203 "ssh-add -l || echo 'Agent not available'; "
204 "echo 'Done'"); /* Marker for command completion */
205 assert_ssh_return_code(session, rc);
206
207 /* Set to non-blocking mode with manual timeout implementation
208 * This prevents the test from hanging indefinitely if there's an issue with
209 * the channel communication. We implement our own timeout logic using a
210 * counter and sleep, which gives the server time to process our request
211 * while still ensuring the test will eventually terminate even if no EOF is
212 * received.
213 */
214 ssh_channel_set_blocking(channel, 0);
215
216 /* Read with safety counter to prevent infinite loops */
217 while (!ssh_channel_is_eof(channel) && read_count < max_read_attempts) {
218 nbytes = ssh_channel_read_nonblocking(channel,
219 buffer,
220 sizeof(buffer) - 1,
221 0);
222
223 if (nbytes > 0) {
224 buffer[nbytes] = 0;
225 ssh_log_hexdump("Read bytes:", (unsigned char *)buffer, nbytes);
226
227 /* Process the command output to check for three key conditions:
228 * 1. If SSH_AUTH_SOCK is properly set (meaning agent forwarding
229 * works)
230 * 2. If "Agent not available" message appears (indicating failure)
231 * 3. If we've seen the "Done" marker (to know when to stop reading)
232 */
233 /* Check if SSH_AUTH_SOCK has a non-empty value by looking for
234 * boundary markers with content between them */
235 if (strstr(buffer, "ENV SSH_AUTH_SOCK=>[") != NULL &&
236 strstr(buffer, "]<") != NULL &&
237 strstr(buffer, "ENV SSH_AUTH_SOCK=>[]<") == NULL) {
238 agent_available = true;
239 }
240
241 if (strstr(buffer, "Agent not available") != NULL) {
242 agent_not_available_found = true;
243 }
244
245 if (strstr(buffer, "Done") != NULL) {
246 break;
247 }
248 } else if (nbytes == SSH_ERROR) {
249 break;
250 } else if (nbytes == SSH_EOF) {
251 break;
252 }
253
254 /* Short sleep between reads to avoid spinning */
255 usleep(100000); /* 100ms */
256 read_count++;
257 }
258
259 /* Trying to read from stderr as well */
260 ssh_channel_read_nonblocking(channel, buffer, sizeof(buffer) - 1, 1);
261
262 /* Close the channel */
263 ssh_channel_send_eof(channel);
264 ssh_channel_close(channel);
265 ssh_channel_free(channel);
266
267 /* Verify agent forwarding worked correctly */
268
269 /* Verify callback was called exactly once */
270 assert_int_equal(cb_state->called, 1);
271
272 /* Verify "Agent not available" was not found
273 * The agent should be available - we should never see "Agent not available"
274 * output
275 */
276 assert_false(agent_not_available_found);
277
278 /* Verify SSH_AUTH_SOCK is set */
279 assert_true(agent_available);
280
281 /* Any channel created in the callback is freed */
282 if (cb_state->created_channel) {
283 ssh_channel_close(cb_state->created_channel);
284 ssh_channel_free(cb_state->created_channel);
285 cb_state->created_channel = NULL;
286 }
287 }
288
289 /* Session setup function that configures SSH agent */
290 static int session_setup(void **state)
291 {
292 struct torture_state *s = *state;
293 int verbosity = torture_libssh_verbosity();
294 struct agent_callback_state *cb_state = NULL;
295 struct ssh_callbacks_struct *callbacks = NULL;
296 char key_path[1024];
297 struct passwd *pw = NULL;
298 int rc;
299
300 /* Create a new session */
301 s->ssh.ssh.session = ssh_new();
302 assert_non_null(s->ssh.ssh.session);
303
304 rc = ssh_options_set(s->ssh.ssh.session,
305 SSH_OPTIONS_LOG_VERBOSITY,
306 &verbosity);
307 assert_int_equal(rc, SSH_OK);
308
309 /* Create and initialize the callback state */
310 cb_state = calloc(1, sizeof(struct agent_callback_state));
311 assert_non_null(cb_state);
312
313 cb_state->expected_session = s->ssh.ssh.session;
314 cb_state->created_channel = NULL;
315
316 /* Set up the callbacks */
317 callbacks = calloc(1, sizeof(struct ssh_callbacks_struct));
318 assert_non_null(callbacks);
319
320 callbacks->userdata = cb_state;
321 callbacks->channel_open_request_auth_agent_function = agent_callback;
322
323 ssh_callbacks_init(callbacks);
324 rc = ssh_set_callbacks(s->ssh.ssh.session, callbacks);
325 assert_int_equal(rc, SSH_OK);
326
327 /* Store callback state and callbacks */
328 s->ssh.ssh.cb_state = cb_state;
329 s->ssh.ssh.callbacks = callbacks;
330
331 /* Set up SSH agent with Bob's key */
332 pw = getpwnam("bob");
333 assert_non_null(pw);
334 snprintf(key_path, sizeof(key_path), "%s/.ssh/id_rsa", pw->pw_dir);
335 rc = torture_setup_ssh_agent(s, key_path);
336 assert_return_code(rc, errno);
337
338 return 0;
339 }
340
341 /* Main test function */
342 int torture_run_tests(void)
343 {
344 int rc;
345 struct CMUnitTest tests[] = {
346 cmocka_unit_test_setup_teardown(torture_auth_agent_forwarding,
347 session_setup,
348 session_teardown),
349 };
350
351 ssh_init();
352
353 /* Simplify the CMocka test filter handling */
354 #if defined HAVE_CMOCKA_SET_TEST_FILTER
355 cmocka_set_message_output(CM_OUTPUT_STDOUT);
356 #endif
357
358 torture_filter_tests(tests);
359
360 rc = cmocka_run_group_tests(tests,
361 sshd_setup_agent_forwarding,
362 torture_teardown_ssh_agent);
363
364 ssh_finalize();
365
366 return rc;
367 }
368
369 #endif