Debian Codegraph @

tests/pkd/pkd_daemon.c

raw
1 /*
2 * pkd_daemon.c -- a sample public-key testing daemon using libssh
3 *
4 * Uses public key authentication to establish an exec channel and
5 * echo back payloads to the user.
6 *
7 * (c) 2014 Jon Simons
8 */
9
10 #include "config.h"
11
12 #include <errno.h>
13 #include <netinet/in.h>
14 #include <pthread.h>
15 #include <signal.h>
16 #include <stdarg.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <sys/types.h>
20 #include <sys/socket.h>
21
22 #include <libssh/callbacks.h>
23 #include <libssh/libssh.h>
24 #include <libssh/server.h>
25 #include <libssh/kex.h>
26
27 #include "torture.h" // for ssh_fips_mode()
28 #include "pkd_daemon.h"
29
30 #include <setjmp.h> // for cmocka
31 #include <cmocka.h>
32
33 static int pkdout_enabled;
34 static int pkderr_enabled;
35
36 static void pkdout(const char *fmt, ...) PRINTF_ATTRIBUTE(1, 2);
37 static void pkderr(const char *fmt, ...) PRINTF_ATTRIBUTE(1, 2);
38
39 static void pkdout(const char *fmt, ...) {
40 va_list vargs;
41 if (pkdout_enabled) {
42 va_start(vargs, fmt);
43 vfprintf(stdout, fmt, vargs);
44 va_end(vargs);
45 }
46 }
47
48 static void pkderr(const char *fmt, ...) {
49 va_list vargs;
50 if (pkderr_enabled) {
51 va_start(vargs, fmt);
52 vfprintf(stderr, fmt, vargs);
53 va_end(vargs);
54 }
55 }
56
57 /*
58 * pkd state: only one thread can run pkd at a time ---------------------
59 */
60
61 static struct {
62 int rc;
63 pthread_t tid;
64 int keep_going;
65 volatile int pkd_ready;
66 } ctx;
67
68 static struct {
69 int server_fd;
70 int req_exec_received;
71 int close_received;
72 int eof_received;
73 } pkd_state;
74
75 static void pkd_sighandler(int signum) {
76 (void) signum;
77 }
78
79 static int pkd_init_libssh(void)
80 {
81 int rc = ssh_threads_set_callbacks(ssh_threads_get_pthread());
82 return (rc == SSH_OK) ? 0 : 1;
83 }
84
85 static int pkd_init_server_fd(short port) {
86 int rc = 0;
87 int yes = 1;
88 struct sockaddr_in addr;
89
90 int server_fd = socket(PF_INET, SOCK_STREAM, 0);
91 if (server_fd < 0) {
92 rc = -1;
93 goto out;
94 }
95
96 rc = setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
97 if (rc != 0) {
98 goto outclose;
99 }
100
101 memset(&addr, 0x0, sizeof(addr));
102 addr.sin_family = AF_INET;
103 addr.sin_port = htons(port);
104 addr.sin_addr.s_addr = INADDR_ANY;
105 rc = bind(server_fd, (struct sockaddr *)&addr, sizeof(addr));
106 if (rc != 0) {
107 goto outclose;
108 }
109
110 rc = listen(server_fd, 128);
111 if (rc == 0) {
112 goto out;
113 }
114
115 outclose:
116 close(server_fd);
117 server_fd = -1;
118 out:
119 pkd_state.server_fd = server_fd;
120 return rc;
121 }
122
123 static int pkd_accept_fd(void)
124 {
125 int fd = -1;
126 struct sockaddr_in addr;
127 socklen_t len = sizeof(addr);
128
129 do {
130 fd = accept(pkd_state.server_fd, (struct sockaddr *) &addr, &len);
131 } while ((ctx.keep_going != 0) && (fd < 0) && (errno == EINTR));
132
133 return fd;
134 }
135
136 static void pkd_eof(ssh_session session,
137 ssh_channel channel,
138 void *userdata) {
139 (void) session;
140 (void) channel;
141 (void) userdata;
142 pkdout("pkd_eof\n");
143 pkd_state.eof_received = 1;
144 }
145
146 static void pkd_chan_close(ssh_session session,
147 ssh_channel channel,
148 void *userdata) {
149 (void) session;
150 (void) channel;
151 (void) userdata;
152 pkdout("pkd_chan_close\n");
153 pkd_state.close_received = 1;
154 }
155
156 static int pkd_req_exec(ssh_session s,
157 ssh_channel c,
158 const char *cmd,
159 void *userdata) {
160 (void) s;
161 (void) c;
162 (void) cmd;
163 (void) userdata;
164 /* assumes pubkey authentication has already succeeded */
165 pkdout("pkd_req_exec\n");
166 pkd_state.req_exec_received = 1;
167 return 0;
168 }
169
170 /* assumes there is only ever a single channel */
171 static struct ssh_channel_callbacks_struct pkd_channel_cb = {
172 .channel_eof_function = pkd_eof,
173 .channel_close_function = pkd_chan_close,
174 .channel_exec_request_function = pkd_req_exec,
175 };
176
177 static int pkd_auth_pubkey_cb(ssh_session s,
178 const char *user,
179 ssh_key key,
180 char state,
181 void *userdata) {
182 (void) s;
183 (void) user;
184 (void) key;
185 (void) state;
186 (void) userdata;
187 pkdout("pkd_auth_pubkey_cb keytype %s, state: %d\n",
188 ssh_key_type_to_char(ssh_key_type(key)), state);
189 if ((state == SSH_PUBLICKEY_STATE_NONE) ||
190 (state == SSH_PUBLICKEY_STATE_VALID)) {
191 return SSH_AUTH_SUCCESS;
192 }
193 return SSH_AUTH_DENIED;
194 }
195
196 static int pkd_service_request_cb(ssh_session session,
197 const char *service,
198 void *userdata) {
199 (void) session;
200 (void) userdata;
201 pkdout("pkd_service_request_cb: %s\n", service);
202 return (0 == (strcmp(service, "ssh-userauth"))) ? 0 : -1;
203 }
204
205 static ssh_channel pkd_channel_openreq_cb(ssh_session s,
206 void *userdata) {
207 ssh_channel c = NULL;
208 ssh_channel *out = (ssh_channel *) userdata;
209
210 /* assumes pubkey authentication has already succeeded */
211 pkdout("pkd_channel_openreq_cb\n");
212
213 c = ssh_channel_new(s);
214 if (c == NULL) {
215 pkderr("ssh_channel_new: %s\n", ssh_get_error(s));
216 return NULL;
217 }
218
219 ssh_callbacks_init(&pkd_channel_cb);
220 pkd_channel_cb.userdata = userdata;
221 if (ssh_set_channel_callbacks(c, &pkd_channel_cb) != SSH_OK) {
222 pkderr("ssh_set_channel_callbacks: %s\n", ssh_get_error(s));
223 ssh_channel_free(c);
224 c = NULL;
225 }
226
227 *out = c;
228
229 return c;
230 }
231
232 static struct ssh_server_callbacks_struct pkd_server_cb = {
233 .auth_pubkey_function = pkd_auth_pubkey_cb,
234 .service_request_function = pkd_service_request_cb,
235 .channel_open_request_session_function = pkd_channel_openreq_cb,
236 };
237
238 static int pkd_exec_hello(int fd, struct pkd_daemon_args *args)
239 {
240 int rc = -1;
241 ssh_bind b = NULL;
242 ssh_session s = NULL;
243 ssh_event e = NULL;
244 ssh_channel c = NULL;
245 enum ssh_bind_options_e opts = -1;
246
247 int level = args->opts.libssh_log_level;
248 enum pkd_hostkey_type_e type = args->type;
249 const char *hostkeypath = args->hostkeypath;
250 const char *all_kex = NULL;
251 const char *all_ciphers = NULL;
252 const char *all_macs = NULL;
253 const uint64_t rekey_data_limit = args->rekey_data_limit;
254 bool process_config = false;
255
256 pkd_state.eof_received = 0;
257 pkd_state.close_received = 0;
258 pkd_state.req_exec_received = 0;
259
260 b = ssh_bind_new();
261 if (b == NULL) {
262 pkderr("ssh_bind_new\n");
263 goto outclose;
264 }
265
266 if (type == PKD_RSA ||
267 type == PKD_ED25519 ||
268 type == PKD_ECDSA) {
269 opts = SSH_BIND_OPTIONS_HOSTKEY;
270 } else {
271 pkderr("unknown hostkey type: %d\n", type);
272 rc = -1;
273 goto outclose;
274 }
275
276 rc = ssh_bind_options_set(b, opts, hostkeypath);
277 if (rc != 0) {
278 pkderr("ssh_bind_options_set: %s\n", ssh_get_error(b));
279 goto outclose;
280 }
281
282 rc = ssh_bind_options_set(b, SSH_BIND_OPTIONS_LOG_VERBOSITY, &level);
283 if (rc != 0) {
284 pkderr("ssh_bind_options_set log verbosity: %s\n", ssh_get_error(b));
285 goto outclose;
286 }
287
288 rc = ssh_bind_options_set(b, SSH_BIND_OPTIONS_PROCESS_CONFIG,
289 &process_config);
290 if (rc != 0) {
291 pkderr("ssh_bind_options_set process config: %s\n", ssh_get_error(b));
292 goto outclose;
293 }
294
295 if (!ssh_fips_mode()) {
296 const char *all_hostkeys = NULL;
297 /* Add methods not enabled by default */
298
299 /* Enable all supported key exchange methods */
300 all_kex = ssh_get_supported_methods(SSH_KEX);
301 rc = ssh_bind_options_set(b, SSH_BIND_OPTIONS_KEY_EXCHANGE, all_kex);
302 if (rc != 0) {
303 pkderr("ssh_bind_options_set kex methods: %s\n", ssh_get_error(b));
304 goto outclose;
305 }
306
307 /* Enable all supported ciphers */
308 all_ciphers = ssh_get_supported_methods(SSH_CRYPT_C_S);
309 rc = ssh_bind_options_set(b, SSH_BIND_OPTIONS_CIPHERS_C_S, all_ciphers);
310 if (rc != 0) {
311 pkderr("ssh_bind_options_set Ciphers C-S: %s\n", ssh_get_error(b));
312 goto outclose;
313 }
314
315 all_ciphers = ssh_get_supported_methods(SSH_CRYPT_S_C);
316 rc = ssh_bind_options_set(b, SSH_BIND_OPTIONS_CIPHERS_S_C, all_ciphers);
317 if (rc != 0) {
318 pkderr("ssh_bind_options_set Ciphers S-C: %s\n", ssh_get_error(b));
319 goto outclose;
320 }
321
322 /* Enable all hostkey algorithms */
323 all_hostkeys = ssh_get_supported_methods(SSH_HOSTKEYS);
324 rc = ssh_bind_options_set(b, SSH_BIND_OPTIONS_HOSTKEY_ALGORITHMS, all_hostkeys);
325 if (rc != 0) {
326 pkderr("ssh_bind_options_set Hostkeys: %s\n", ssh_get_error(b));
327 goto outclose;
328 }
329
330 /* Enable all message authentication codes */
331 all_macs = ssh_get_supported_methods(SSH_MAC_C_S);
332 rc = ssh_bind_options_set(b, SSH_BIND_OPTIONS_HMAC_C_S, all_macs);
333 if (rc != 0) {
334 pkderr("ssh_bind_options_set MACs C-S: %s\n", ssh_get_error(b));
335 goto outclose;
336 }
337
338 all_macs = ssh_get_supported_methods(SSH_MAC_S_C);
339 rc = ssh_bind_options_set(b, SSH_BIND_OPTIONS_HMAC_S_C, all_macs);
340 if (rc != 0) {
341 pkderr("ssh_bind_options_set MACs S-C: %s\n", ssh_get_error(b));
342 goto outclose;
343 }
344
345 }
346
347 s = ssh_new();
348 if (s == NULL) {
349 pkderr("ssh_new\n");
350 goto outclose;
351 }
352
353 rc = ssh_options_set(s, SSH_OPTIONS_REKEY_DATA, &rekey_data_limit);
354 if (rc != 0) {
355 pkderr("ssh_options_set rekey data: %s\n", ssh_get_error(s));
356 goto outclose;
357 }
358
359 /*
360 * ssh_bind_accept loads host key as side-effect. If this
361 * succeeds, the given 'fd' will be closed upon 'ssh_free(s)'.
362 */
363 rc = ssh_bind_accept_fd(b, s, fd);
364 if (rc != SSH_OK) {
365 pkderr("ssh_bind_accept_fd: %s\n", ssh_get_error(b));
366 goto outclose;
367 }
368
369 /* accept only publickey-based auth */
370 ssh_set_auth_methods(s, SSH_AUTH_METHOD_PUBLICKEY);
371
372 /* initialize callbacks */
373 ssh_callbacks_init(&pkd_server_cb);
374 pkd_server_cb.userdata = &c;
375 rc = ssh_set_server_callbacks(s, &pkd_server_cb);
376 if (rc != SSH_OK) {
377 pkderr("ssh_set_server_callbacks: %s\n", ssh_get_error(s));
378 goto out;
379 }
380
381 /* first do key exchange */
382 rc = ssh_handle_key_exchange(s);
383 if (rc != SSH_OK) {
384 pkderr("ssh_handle_key_exchange: %s\n", ssh_get_error(s));
385 goto out;
386 }
387
388 /* setup and pump event to carry out exec channel */
389 e = ssh_event_new();
390 if (e == NULL) {
391 pkderr("ssh_event_new\n");
392 goto out;
393 }
394
395 rc = ssh_event_add_session(e, s);
396 if (rc != SSH_OK) {
397 pkderr("ssh_event_add_session\n");
398 goto out;
399 }
400
401 /* poll until exec channel established */
402 while ((ctx.keep_going != 0) &&
403 (rc != SSH_ERROR) && (pkd_state.req_exec_received == 0)) {
404 rc = ssh_event_dopoll(e, -1 /* infinite timeout */);
405 }
406
407 if (rc == SSH_ERROR) {
408 pkderr("ssh_event_dopoll\n");
409 goto out;
410 } else if (c == NULL) {
411 pkderr("poll loop exited but exec channel not ready\n");
412 rc = -1;
413 goto out;
414 }
415
416 rc = ssh_channel_write(c, args->payload.buf, args->payload.len);
417 if (rc != (int)args->payload.len) {
418 pkderr("ssh_channel_write partial (%d != %zd)\n", rc, args->payload.len);
419 }
420
421 rc = ssh_channel_request_send_exit_status(c, 0);
422 if (rc != SSH_OK) {
423 pkderr("ssh_channel_request_send_exit_status: %s\n",
424 ssh_get_error(s));
425 goto out;
426 }
427
428 rc = ssh_channel_send_eof(c);
429 if (rc != SSH_OK) {
430 pkderr("ssh_channel_send_eof: %s\n", ssh_get_error(s));
431 goto out;
432 }
433
434 rc = ssh_channel_close(c);
435 if (rc != SSH_OK) {
436 pkderr("ssh_channel_close: %s\n", ssh_get_error(s));
437 goto out;
438 }
439
440 while ((ctx.keep_going != 0) &&
441 (pkd_state.eof_received == 0) &&
442 (pkd_state.close_received == 0)) {
443 rc = ssh_event_dopoll(e, 1000 /* milliseconds */);
444 if (rc == SSH_ERROR) {
445 /* log, but don't consider this fatal */
446 pkdout("ssh_event_dopoll for eof + close: %s\n", ssh_get_error(s));
447 rc = 0;
448 break;
449 } else {
450 rc = 0;
451 }
452 }
453
454 while ((ctx.keep_going != 0) &&
455 (ssh_is_connected(s))) {
456 rc = ssh_event_dopoll(e, 1000 /* milliseconds */);
457 if (rc == SSH_ERROR) {
458 /* log, but don't consider this fatal */
459 pkdout("ssh_event_dopoll for session connection: %s\n", ssh_get_error(s));
460 rc = 0;
461 break;
462 } else {
463 rc = 0;
464 }
465 }
466 goto out;
467
468 outclose:
469 close(fd);
470 out:
471 if (c != NULL) {
472 ssh_channel_free(c);
473 }
474 if (e != NULL) {
475 ssh_event_remove_session(e, s);
476 ssh_event_free(e);
477 }
478 if (s != NULL) {
479 ssh_disconnect(s);
480 ssh_free(s);
481 }
482 if (b != NULL) {
483 ssh_bind_free(b);
484 }
485 return rc;
486 }
487
488 /*
489 * main loop ------------------------------------------------------------
490 */
491
492 static void *pkd_main(void *args) {
493 int rc = -1;
494 struct pkd_daemon_args *a = (struct pkd_daemon_args *) args;
495
496 struct sigaction act = { .sa_handler = pkd_sighandler, };
497
498 pkd_state.server_fd = -1;
499 pkd_state.req_exec_received = 0;
500 pkd_state.close_received = 0;
501 pkd_state.eof_received = 0;
502
503 /* SIGUSR1 is used to interrupt 'pkd_accept_fd'. */
504 rc = sigaction(SIGUSR1, &act, NULL);
505 if (rc != 0) {
506 pkderr("sigaction: %d\n", rc);
507 goto out;
508 }
509
510 /* Ignore SIGPIPE */
511 signal(SIGPIPE, SIG_IGN);
512
513 rc = pkd_init_libssh();
514 if (rc != 0) {
515 pkderr("pkd_init_libssh: %d\n", rc);
516 goto out;
517 }
518
519 rc = pkd_init_server_fd(1234);
520 if (rc != 0) {
521 pkderr("pkd_init_server_fd: %d\n", rc);
522 goto out;
523 }
524
525 ctx.pkd_ready = 1;
526
527 while (ctx.keep_going != 0) {
528 int fd = pkd_accept_fd();
529 if (fd < 0) {
530 if (ctx.keep_going != 0) {
531 pkderr("pkd_accept_fd");
532 rc = -1;
533 } else {
534 rc = 0;
535 }
536 break;
537 }
538
539 rc = pkd_exec_hello(fd, a);
540 if (rc != 0) {
541 pkderr("pkd_exec_hello: %d\n", rc);
542 break;
543 }
544 }
545
546 if (pkd_state.server_fd != -1) {
547 close(pkd_state.server_fd);
548 }
549 pkd_state.server_fd = -1;
550 out:
551 ctx.rc = rc;
552
553 return NULL;
554 }
555
556 /*
557 * pkd start and stop used by setup/teardown test scaffolding -----------
558 */
559
560 int pkd_start(struct pkd_daemon_args *args) {
561 int rc = 0;
562
563 pkdout_enabled = args->opts.log_stdout;
564 pkderr_enabled = args->opts.log_stderr;
565
566 /* Initialize the pkd context. */
567 ctx.rc = -1;
568 ctx.keep_going = 1;
569 ctx.pkd_ready = 0;
570 rc = pthread_create(&ctx.tid, NULL, &pkd_main, args);
571 assert_int_equal(rc, 0);
572
573 /* Busy-spin until pkd thread is ready. */
574 while (ctx.pkd_ready == 0);
575
576 return rc;
577 }
578
579 void pkd_stop(struct pkd_result *out) {
580 int rc = 0;
581
582 ctx.keep_going = 0;
583 close(pkd_state.server_fd);
584
585 rc = pthread_kill(ctx.tid, SIGUSR1);
586 assert_int_not_equal(rc, EINVAL);
587 assert_int_not_equal(rc, ENOTSUP);
588
589 rc = pthread_join(ctx.tid, NULL);
590 assert_int_equal(rc, 0);
591
592 assert_non_null(out);
593 out->ok = (ctx.rc == 0);
594
595 return;
596 }