Debian Codegraph @

tests/server/test_server/default_cb.c

raw
1 /*
2 * This file is part of the SSH Library
3 *
4 * Copyright (c) 2018 by Red Hat, Inc.
5 *
6 * Author: Anderson Toshiyuki Sasaki <[email protected]>
7 *
8 * The SSH Library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or (at your
11 * option) any later version.
12 *
13 * The SSH Library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 * License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with the SSH Library; see the file COPYING. If not, write to
20 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21 * MA 02111-1307, USA.
22 */
23
24
25 #include "config.h"
26 #include "test_server.h"
27 #include "default_cb.h"
28 #include "testserver_common.h"
29
30 #include <libssh/callbacks.h>
31 #include <libssh/server.h>
32 #include <libssh/priv.h>
33
34 #include <stdlib.h>
35 #include <string.h>
36 #include <stdio.h>
37 #include <stdbool.h>
38 #include <poll.h>
39 #include <signal.h>
40 #include <sys/wait.h>
41 #include <sys/ioctl.h>
42
43 #ifdef HAVE_LIBUTIL_H
44 #include <libutil.h>
45 #endif
46 #ifdef HAVE_PTY_H
47 #include <pty.h>
48 #endif
49 #ifdef HAVE_UTMP_H
50 #include <utmp.h>
51 #endif
52 #ifdef HAVE_UTIL_H
53 #include <util.h>
54 #endif
55
56 int auth_none_cb(UNUSED_PARAM(ssh_session session),
57 const char *user,
58 void *userdata)
59 {
60 struct session_data_st *sdata = NULL;
61 ssh_string banner = NULL;
62
63 sdata = (struct session_data_st *)userdata;
64 if (sdata == NULL) {
65 fprintf(stderr, "Error: NULL userdata\n");
66 goto denied;
67 }
68
69 if (sdata->username == NULL) {
70 fprintf(stderr, "Error: expected username not set\n");
71 goto denied;
72 }
73
74 printf("None authentication of user %s\n", user);
75
76 /* Send the banner */
77 banner = ssh_string_from_char(SSHD_BANNER_MESSAGE);
78 if (banner == NULL) {
79 goto denied;
80 }
81 if (ssh_send_issue_banner(session, banner) == SSH_ERROR) {
82 fprintf(stderr, "Error: Failed to send the banner.\n");
83 goto denied;
84 }
85 denied:
86 ssh_string_free(banner);
87 return SSH_AUTH_DENIED;
88 }
89
90 int auth_pubkey_cb(UNUSED_PARAM(ssh_session session),
91 const char *user,
92 UNUSED_PARAM(struct ssh_key_struct *pubkey),
93 char signature_state,
94 void *userdata)
95 {
96 struct session_data_st *sdata;
97
98 sdata = (struct session_data_st *)userdata;
99 if (sdata == NULL) {
100 fprintf(stderr, "Error: NULL userdata\n");
101 goto null_userdata;
102 }
103
104 printf("Public key authentication of user %s\n", user);
105
106 switch(signature_state) {
107 case SSH_PUBLICKEY_STATE_NONE:
108 case SSH_PUBLICKEY_STATE_VALID:
109 break;
110 default:
111 goto denied;
112 }
113
114 /* TODO */
115 /* Check whether the user and public key are in authorized keys list */
116
117 /* Authenticated */
118 printf("Authenticated\n");
119 sdata->authenticated = 1;
120 sdata->auth_attempts = 0;
121 return SSH_AUTH_SUCCESS;
122
123 denied:
124 sdata->auth_attempts++;
125 null_userdata:
126 return SSH_AUTH_DENIED;
127 }
128
129 /* TODO implement proper pam authentication cb */
130 int auth_password_cb(UNUSED_PARAM(ssh_session session),
131 const char *user,
132 const char *password,
133 void *userdata)
134 {
135 bool known_user = false;
136 bool valid_password = false;
137
138 struct session_data_st *sdata;
139
140 sdata = (struct session_data_st *)userdata;
141
142 if (sdata == NULL) {
143 fprintf(stderr, "Error: NULL userdata\n");
144 goto null_userdata;
145 }
146
147 if (sdata->username == NULL) {
148 fprintf(stderr, "Error: expected username not set\n");
149 goto denied;
150 }
151
152 if (sdata->password == NULL) {
153 fprintf(stderr, "Error: expected password not set\n");
154 goto denied;
155 }
156
157 printf("Password authentication of user %s\n", user);
158
159 known_user = !(strcmp(user, sdata->username));
160 valid_password = !(strcmp(password, sdata->password));
161
162 if (known_user && valid_password) {
163 sdata->authenticated = 1;
164 sdata->auth_attempts = 0;
165 printf("Authenticated\n");
166 return SSH_AUTH_SUCCESS;
167 }
168
169 denied:
170 sdata->auth_attempts++;
171 null_userdata:
172 return SSH_AUTH_DENIED;
173 }
174
175 static int kbdint_check_response(ssh_session session, struct session_data_st *sdata)
176 {
177 int count, cmp;
178 const char *answer = NULL;
179
180 count = ssh_userauth_kbdint_getnanswers(session);
181 if (count != 2) {
182 return 0;
183 }
184
185 answer = ssh_userauth_kbdint_getanswer(session, 0);
186 cmp = strcasecmp(sdata->username, answer);
187 if (cmp != 0) {
188 return 0;
189 }
190 answer = ssh_userauth_kbdint_getanswer(session, 1);
191 cmp = strcmp(sdata->password, answer);
192 if (cmp != 0) {
193 return 0;
194 }
195
196 return 1;
197 }
198
199 static int
200 auth_kbdint_cb(ssh_message message, ssh_session session, void *userdata)
201 {
202 struct session_data_st *sdata = (struct session_data_st *)userdata;
203
204 const char *name = "\n\nKeyboard-Interactive Fancy Authentication\n";
205 const char *instruction = "Get yourself authenticated";
206 const char *prompts[2] = {"Username: ", "Password: "};
207 char echo[] = {1, 0};
208
209 if (sdata == NULL) {
210 fprintf(stderr, "Error: NULL userdata\n");
211 return SSH_AUTH_DENIED;
212 }
213
214 if (!ssh_message_auth_kbdint_is_response(message)) {
215 printf("User %s wants to auth with kbdint\n",
216 ssh_message_auth_user(message));
217 ssh_message_auth_interactive_request(message,
218 name,
219 instruction,
220 2,
221 prompts,
222 echo);
223 return SSH_AUTH_INFO;
224 } else {
225 if (kbdint_check_response(session, sdata)) {
226 sdata->authenticated = 1;
227 return SSH_AUTH_SUCCESS;
228 }
229 }
230
231 return SSH_AUTH_DENIED;
232 }
233
234 #if WITH_GSSAPI
235 int auth_gssapi_mic_cb(ssh_session session,
236 UNUSED_PARAM(const char *user),
237 UNUSED_PARAM(const char *principal),
238 void *userdata)
239 {
240 ssh_gssapi_creds creds;
241 struct session_data_st *sdata;
242
243 sdata = (struct session_data_st *)userdata;
244
245 if (sdata == NULL) {
246 fprintf(stderr, "Error: NULL userdata\n");
247 goto null_userdata;
248 }
249
250 printf("GSSAPI authentication\n");
251
252 creds = ssh_gssapi_get_creds(session);
253 if (creds != NULL) {
254 printf("Received some gssapi credentials\n");
255 } else {
256 printf("Not received any forwardable creds\n");
257 }
258
259 printf("Authenticated\n");
260
261 sdata->authenticated = 1;
262 sdata->auth_attempts = 0;
263
264 return SSH_AUTH_SUCCESS;
265
266 null_userdata:
267 return SSH_AUTH_DENIED;
268 }
269 #endif
270
271 int channel_data_cb(UNUSED_PARAM(ssh_session session),
272 UNUSED_PARAM(ssh_channel channel),
273 void *data,
274 uint32_t len,
275 UNUSED_PARAM(int is_stderr),
276 void *userdata)
277 {
278 struct channel_data_st *cdata;
279 int rc;
280
281 cdata = (struct channel_data_st *)userdata;
282
283 if (cdata == NULL) {
284 fprintf(stderr, "NULL userdata\n");
285 rc = SSH_ERROR;
286 goto end;
287 }
288
289 if (len == 0 || cdata->pid < 1 || kill(cdata->pid, 0) < 0) {
290 rc = SSH_OK;
291 goto end;
292 }
293
294 rc = write(cdata->child_stdin, (char *) data, len);
295
296 end:
297 return rc;
298 }
299
300 void channel_eof_cb(UNUSED_PARAM(ssh_session session),
301 UNUSED_PARAM(ssh_channel channel),
302 void *userdata)
303 {
304 struct channel_data_st *cdata;
305
306 cdata = (struct channel_data_st *)userdata;
307
308 if (cdata == NULL) {
309 fprintf(stderr, "NULL userdata\n");
310 goto end;
311 }
312
313 end:
314 return;
315 }
316
317 void channel_close_cb(UNUSED_PARAM(ssh_session session),
318 UNUSED_PARAM(ssh_channel channel),
319 void *userdata)
320 {
321 struct channel_data_st *cdata;
322
323 cdata = (struct channel_data_st *)userdata;
324
325 if (cdata == NULL) {
326 fprintf(stderr, "NULL userdata\n");
327 goto end;
328 }
329
330 end:
331 return;
332 }
333
334 void channel_signal_cb(UNUSED_PARAM(ssh_session session),
335 UNUSED_PARAM(ssh_channel channel),
336 UNUSED_PARAM(const char *signal),
337 void *userdata)
338 {
339 struct channel_data_st *cdata;
340
341 cdata = (struct channel_data_st *)userdata;
342
343 if (cdata == NULL) {
344 fprintf(stderr, "NULL userdata\n");
345 goto end;
346 }
347
348 end:
349 return;
350 }
351
352 void channel_exit_status_cb(UNUSED_PARAM(ssh_session session),
353 UNUSED_PARAM(ssh_channel channel),
354 UNUSED_PARAM(int exit_status),
355 void *userdata)
356 {
357 struct channel_data_st *cdata;
358
359 cdata = (struct channel_data_st *)userdata;
360
361 if (cdata == NULL) {
362 fprintf(stderr, "NULL userdata\n");
363 goto end;
364 }
365
366 end:
367 return;
368 }
369
370 void channel_exit_signal_cb(UNUSED_PARAM(ssh_session session),
371 UNUSED_PARAM(ssh_channel channel),
372 UNUSED_PARAM(const char *signal),
373 UNUSED_PARAM(int core),
374 UNUSED_PARAM(const char *errmsg),
375 UNUSED_PARAM(const char *lang),
376 void *userdata)
377 {
378 struct channel_data_st *cdata;
379
380 cdata = (struct channel_data_st *)userdata;
381
382 if (cdata == NULL) {
383 fprintf(stderr, "NULL userdata\n");
384 goto end;
385 }
386
387 end:
388 return;
389 }
390
391 int channel_pty_request_cb(UNUSED_PARAM(ssh_session session),
392 UNUSED_PARAM(ssh_channel channel),
393 UNUSED_PARAM(const char *term),
394 int cols,
395 int rows,
396 int py,
397 int px,
398 void *userdata)
399 {
400 struct channel_data_st *cdata;
401 int rc;
402
403 cdata = (struct channel_data_st *)userdata;
404
405 if (cdata == NULL) {
406 fprintf(stderr, "NULL userdata\n");
407 rc = SSH_ERROR;
408 goto end;
409 }
410
411 cdata->winsize->ws_row = rows;
412 cdata->winsize->ws_col = cols;
413 cdata->winsize->ws_xpixel = px;
414 cdata->winsize->ws_ypixel = py;
415
416 rc = openpty(&cdata->pty_master,
417 &cdata->pty_slave,
418 NULL,
419 NULL,
420 cdata->winsize);
421 if (rc != 0) {
422 fprintf(stderr, "Failed to open pty\n");
423 rc = SSH_ERROR;
424 goto end;
425 }
426
427 rc = SSH_OK;
428
429 end:
430 return rc;
431 }
432
433 int channel_pty_resize_cb(ssh_session session,
434 ssh_channel channel,
435 int cols,
436 int rows,
437 int py,
438 int px,
439 void *userdata)
440 {
441 struct channel_data_st *cdata;
442 int rc;
443
444 (void) session;
445 (void) channel;
446
447 cdata = (struct channel_data_st *)userdata;
448
449 if (cdata == NULL) {
450 fprintf(stderr, "NULL userdata\n");
451 rc = SSH_ERROR;
452 goto end;
453 }
454
455 cdata->winsize->ws_row = rows;
456 cdata->winsize->ws_col = cols;
457 cdata->winsize->ws_xpixel = px;
458 cdata->winsize->ws_ypixel = py;
459
460 if (cdata->pty_master != -1) {
461 rc = ioctl(cdata->pty_master, TIOCSWINSZ, cdata->winsize);
462 goto end;
463 }
464
465 rc = SSH_ERROR;
466
467 end:
468 return rc;
469 }
470
471 void channel_auth_agent_req_callback(UNUSED_PARAM(ssh_session session),
472 UNUSED_PARAM(ssh_channel channel),
473 UNUSED_PARAM(void *userdata))
474 {
475 /* TODO */
476 }
477
478 void channel_x11_req_callback(UNUSED_PARAM(ssh_session session),
479 UNUSED_PARAM(ssh_channel channel),
480 UNUSED_PARAM(int single_connection),
481 UNUSED_PARAM(const char *auth_protocol),
482 UNUSED_PARAM(const char *auth_cookie),
483 UNUSED_PARAM(uint32_t screen_number),
484 UNUSED_PARAM(void *userdata))
485 {
486 /* TODO */
487 }
488
489 static int exec_pty(const char *mode,
490 const char *command,
491 struct channel_data_st *cdata)
492 {
493 int rc;
494
495 if (cdata == NULL) {
496 fprintf(stderr, "NULL userdata\n");
497 rc = SSH_ERROR;
498 goto end;
499 }
500
501 cdata->pid = fork();
502 switch(cdata->pid) {
503 case -1:
504 close(cdata->pty_master);
505 close(cdata->pty_slave);
506 fprintf(stderr, "Failed to fork\n");
507 rc = SSH_ERROR;
508 goto end;
509 case 0:
510 close(cdata->pty_master);
511 if (login_tty(cdata->pty_slave) != 0) {
512 finalize_openssl();
513 exit(1);
514 }
515 execl("/bin/sh", "sh", mode, command, NULL);
516 finalize_openssl();
517 exit(0);
518 default:
519 close(cdata->pty_slave);
520 /* pty fd is bi-directional */
521 cdata->child_stdout = cdata->child_stdin = cdata->pty_master;
522 }
523
524 rc = SSH_OK;
525
526 end:
527 return rc;
528 }
529
530 static int exec_nopty(const char *command, struct channel_data_st *cdata)
531 {
532 int in[2], out[2], err[2];
533
534 if (cdata == NULL) {
535 fprintf(stderr, "NULL userdata\n");
536 goto stdin_failed;
537 }
538
539 /* Do the plumbing to be able to talk with the child process. */
540 if (pipe(in) != 0) {
541 goto stdin_failed;
542 }
543 if (pipe(out) != 0) {
544 goto stdout_failed;
545 }
546 if (pipe(err) != 0) {
547 goto stderr_failed;
548 }
549
550 switch(cdata->pid = fork()) {
551 case -1:
552 goto fork_failed;
553 case 0:
554 /* Finish the plumbing in the child process. */
555 close(in[1]);
556 close(out[0]);
557 close(err[0]);
558 dup2(in[0], STDIN_FILENO);
559 dup2(out[1], STDOUT_FILENO);
560 dup2(err[1], STDERR_FILENO);
561 close(in[0]);
562 close(out[1]);
563 close(err[1]);
564 /* exec the requested command. */
565 execl("/bin/sh", "sh", "-c", command, NULL);
566 finalize_openssl();
567 exit(0);
568 }
569
570 close(in[0]);
571 close(out[1]);
572 close(err[1]);
573
574 cdata->child_stdin = in[1];
575 cdata->child_stdout = out[0];
576 cdata->child_stderr = err[0];
577
578 return SSH_OK;
579
580 fork_failed:
581 close(err[0]);
582 close(err[1]);
583 stderr_failed:
584 close(out[0]);
585 close(out[1]);
586 stdout_failed:
587 close(in[0]);
588 close(in[1]);
589 stdin_failed:
590 return SSH_ERROR;
591 }
592
593 int channel_shell_request_cb(UNUSED_PARAM(ssh_session session),
594 UNUSED_PARAM(ssh_channel channel),
595 void *userdata)
596 {
597 struct channel_data_st *cdata;
598 int rc;
599
600 cdata = (struct channel_data_st *)userdata;
601
602 if (cdata == NULL) {
603 fprintf(stderr, "NULL userdata\n");
604 rc = SSH_ERROR;
605 goto end;
606 }
607
608 if(cdata->pid > 0) {
609 rc = SSH_ERROR;
610 goto end;
611 }
612
613 if (cdata->pty_master != -1 && cdata->pty_slave != -1) {
614 rc = exec_pty("-l", NULL, cdata);
615 goto end;
616 }
617
618 /* Client requested a shell without a pty, let's pretend we allow that */
619 rc = SSH_OK;
620
621 end:
622 return rc;
623 }
624
625 int channel_exec_request_cb(UNUSED_PARAM(ssh_session session),
626 UNUSED_PARAM(ssh_channel channel),
627 const char *command,
628 void *userdata)
629 {
630 struct channel_data_st *cdata;
631 int rc;
632
633 cdata = (struct channel_data_st *)userdata;
634
635 if (cdata == NULL) {
636 fprintf(stderr, "NULL userdata\n");
637 rc = SSH_ERROR;
638 goto end;
639 }
640
641 if(cdata->pid > 0) {
642 rc = SSH_ERROR;
643 goto end;
644 }
645
646 if (cdata->pty_master != -1 && cdata->pty_slave != -1) {
647 rc = exec_pty("-c", command, cdata);
648 goto end;
649 }
650
651 rc = exec_nopty(command, cdata);
652
653 end:
654 return rc;
655 }
656
657 int channel_env_request_cb(UNUSED_PARAM(ssh_session session),
658 UNUSED_PARAM(ssh_channel channel),
659 UNUSED_PARAM(const char *env_name),
660 UNUSED_PARAM(const char *env_value),
661 void *userdata)
662 {
663 struct channel_data_st *cdata;
664 int rc;
665
666 cdata = (struct channel_data_st *)userdata;
667
668 if (cdata == NULL) {
669 fprintf(stderr, "NULL userdata\n");
670 rc = SSH_ERROR;
671 goto end;
672 }
673
674 rc = SSH_OK;
675
676 end:
677 return rc;
678 }
679
680 int channel_subsystem_request_cb(ssh_session session,
681 ssh_channel channel,
682 const char *subsystem,
683 void *userdata)
684 {
685 struct channel_data_st *cdata;
686 int rc;
687
688 cdata = (struct channel_data_st *)userdata;
689
690 if (cdata == NULL) {
691 fprintf(stderr, "NULL userdata\n");
692 rc = SSH_ERROR;
693 goto end;
694 }
695
696 rc = strcmp(subsystem, "sftp");
697 if (rc == 0) {
698 rc = channel_exec_request_cb(session,
699 channel,
700 SFTP_SERVER_PATH,
701 userdata);
702 goto end;
703 }
704
705 /* TODO add other subsystems */
706
707 rc = SSH_ERROR;
708
709 end:
710 return rc;
711 }
712
713 int channel_write_wontblock_cb(UNUSED_PARAM(ssh_session session),
714 UNUSED_PARAM(ssh_channel channel),
715 UNUSED_PARAM(size_t bytes),
716 UNUSED_PARAM(void *userdata))
717 {
718 /* TODO */
719
720 return 0;
721 }
722
723 ssh_channel channel_new_session_cb(ssh_session session, void *userdata)
724 {
725 struct session_data_st *sdata = NULL;
726 ssh_channel chan = NULL;
727
728 sdata = (struct session_data_st *)userdata;
729
730 if (sdata == NULL) {
731 fprintf(stderr, "NULL userdata");
732 goto end;
733 }
734
735 chan = ssh_channel_new(session);
736 if (chan == NULL) {
737 fprintf(stderr, "Error creating channel: %s\n",
738 ssh_get_error(session));
739 goto end;
740 }
741
742 sdata->channel = chan;
743
744 end:
745 return chan;
746 }
747
748 #ifdef WITH_PCAP
749 static void set_pcap(struct session_data_st *sdata,
750 ssh_session session,
751 char *pcap_file)
752 {
753 int rc = 0;
754
755 if (sdata == NULL) {
756 return;
757 }
758
759 if (pcap_file == NULL) {
760 return;
761 }
762
763 sdata->pcap = ssh_pcap_file_new();
764 if (sdata->pcap == NULL) {
765 return;
766 }
767
768 rc = ssh_pcap_file_open(sdata->pcap, pcap_file);
769 if (rc == SSH_ERROR) {
770 fprintf(stderr, "Error opening pcap file\n");
771 ssh_pcap_file_free(sdata->pcap);
772 sdata->pcap = NULL;
773 return;
774 }
775 ssh_set_pcap_file(session, sdata->pcap);
776 }
777
778 static void cleanup_pcap(struct session_data_st *sdata)
779 {
780 if (sdata == NULL) {
781 return;
782 }
783
784 if (sdata->pcap == NULL) {
785 return;
786 }
787
788 ssh_pcap_file_free(sdata->pcap);
789 sdata->pcap = NULL;
790 }
791 #endif
792
793 static int process_stdout(socket_t fd, int revents, void *userdata)
794 {
795 char buf[BUF_SIZE];
796 int n = -1;
797 ssh_channel channel = (ssh_channel) userdata;
798
799 if (channel != NULL && (revents & POLLIN) != 0) {
800 n = read(fd, buf, BUF_SIZE);
801 if (n > 0) {
802 ssh_channel_write(channel, buf, n);
803 }
804 }
805
806 return n;
807 }
808
809 static int process_stderr(socket_t fd, int revents, void *userdata)
810 {
811 char buf[BUF_SIZE];
812 int n = -1;
813 ssh_channel channel = (ssh_channel) userdata;
814
815 if (channel != NULL && (revents & POLLIN) != 0) {
816 n = read(fd, buf, BUF_SIZE);
817 if (n > 0) {
818 ssh_channel_write_stderr(channel, buf, n);
819 }
820 }
821
822 return n;
823 }
824
825 /* The caller is responsible to set the userdata to be provided to the callback
826 * The caller is responsible to free the allocated structure
827 * */
828 struct ssh_server_callbacks_struct *get_default_server_cb(void)
829 {
830
831 struct ssh_server_callbacks_struct *cb;
832
833 cb = (struct ssh_server_callbacks_struct *)calloc(1,
834 sizeof(struct ssh_server_callbacks_struct));
835
836 if (cb == NULL) {
837 fprintf(stderr, "Out of memory\n");
838 goto end;
839 }
840
841 cb->auth_none_function = auth_none_cb;
842 cb->auth_password_function = auth_password_cb;
843 cb->auth_pubkey_function = auth_pubkey_cb;
844 cb->channel_open_request_session_function = channel_new_session_cb;
845 cb->auth_kbdint_function = auth_kbdint_cb;
846 #if WITH_GSSAPI
847 cb->auth_gssapi_mic_function = auth_gssapi_mic_cb;
848 #endif
849
850 end:
851 return cb;
852 }
853
854 /* The caller is responsible to set the userdata to be provided to the callback
855 * The caller is responsible to free the allocated structure
856 * */
857 struct ssh_channel_callbacks_struct *get_default_channel_cb(void)
858 {
859 struct ssh_channel_callbacks_struct *cb;
860
861 cb = (struct ssh_channel_callbacks_struct *)calloc(1,
862 sizeof(struct ssh_channel_callbacks_struct));
863 if (cb == NULL) {
864 fprintf(stderr, "Out of memory\n");
865 goto end;
866 }
867
868 cb->channel_pty_request_function = channel_pty_request_cb;
869 cb->channel_pty_window_change_function = channel_pty_resize_cb;
870 cb->channel_shell_request_function = channel_shell_request_cb;
871 cb->channel_env_request_function = channel_env_request_cb;
872 cb->channel_subsystem_request_function = channel_subsystem_request_cb;
873 cb->channel_exec_request_function = channel_exec_request_cb;
874 cb->channel_data_function = channel_data_cb;
875
876 end:
877 return cb;
878 };
879
880 void default_handle_session_cb(ssh_event event,
881 ssh_session session,
882 struct server_state_st *state)
883 {
884 int n;
885 int rc = 0;
886
887 /* Structure for storing the pty size. */
888 struct winsize wsize = {
889 .ws_row = 0,
890 .ws_col = 0,
891 .ws_xpixel = 0,
892 .ws_ypixel = 0
893 };
894
895 /* Our struct holding information about the channel. */
896 struct channel_data_st cdata = {
897 .pid = 0,
898 .pty_master = -1,
899 .pty_slave = -1,
900 .child_stdin = -1,
901 .child_stdout = -1,
902 .child_stderr = -1,
903 .event = NULL,
904 .winsize = &wsize
905 };
906
907 /* Our struct holding information about the session. */
908 struct session_data_st sdata = {
909 .channel = NULL,
910 .auth_attempts = 0,
911 .authenticated = 0,
912 .username = SSHD_DEFAULT_USER,
913 .password = SSHD_DEFAULT_PASSWORD
914 };
915
916 struct ssh_channel_callbacks_struct *channel_cb = NULL;
917 struct ssh_server_callbacks_struct *server_cb = NULL;
918
919 if (state == NULL) {
920 fprintf(stderr, "NULL server state provided\n");
921 goto end;
922 }
923
924 /* If callbacks were provided use them. Otherwise, use default callbacks */
925 if (state->server_cb != NULL) {
926 /* This is a macro, it does not return a value */
927 ssh_callbacks_init(state->server_cb);
928
929 rc = ssh_set_server_callbacks(session, state->server_cb);
930 if (rc) {
931 goto end;
932 }
933 } else {
934 server_cb = get_default_server_cb();
935 if (server_cb == NULL) {
936 goto end;
937 }
938
939 server_cb->userdata = &sdata;
940
941 /* This is a macro, it does not return a value */
942 ssh_callbacks_init(server_cb);
943
944 rc = ssh_set_server_callbacks(session, server_cb);
945 if (rc) {
946 goto end;
947 }
948 }
949
950 sdata.server_state = (void *)state;
951
952 #ifdef WITH_PCAP
953 set_pcap(&sdata, session, state->pcap_file);
954 #endif
955
956 if (state->expected_username != NULL) {
957 sdata.username = state->expected_username;
958 }
959
960 if (state->expected_password != NULL) {
961 sdata.password = state->expected_password;
962 }
963
964 if (ssh_handle_key_exchange(session) != SSH_OK) {
965 fprintf(stderr, "%s\n", ssh_get_error(session));
966 goto end;
967 }
968
969 /* Set the supported authentication methods */
970 if (state->auth_methods) {
971 ssh_set_auth_methods(session, state->auth_methods);
972 } else {
973 ssh_set_auth_methods(session,
974 SSH_AUTH_METHOD_PASSWORD |
975 SSH_AUTH_METHOD_PUBLICKEY |
976 SSH_AUTH_METHOD_INTERACTIVE |
977 SSH_AUTH_METHOD_GSSAPI_MIC);
978 }
979
980 ssh_event_add_session(event, session);
981
982 n = 0;
983 while (sdata.authenticated == 0 || sdata.channel == NULL) {
984 /* If the user has used up all attempts, or if he hasn't been able to
985 * authenticate in 10 seconds (n * 100ms), disconnect. */
986 if (sdata.auth_attempts >= state->max_tries || n >= 100) {
987 goto end;
988 }
989
990 if (ssh_event_dopoll(event, 100) == SSH_ERROR) {
991 fprintf(stderr, "do_poll error: %s\n", ssh_get_error(session));
992 goto end;
993 }
994 n++;
995 }
996
997 /* TODO check return values */
998 if (state->channel_cb != NULL) {
999 ssh_callbacks_init(state->channel_cb);
1000
1001 rc = ssh_set_channel_callbacks(sdata.channel, state->channel_cb);
1002 if (rc) {
1003 goto end;
1004 }
1005 } else {
1006 channel_cb = get_default_channel_cb();
1007 if (channel_cb == NULL) {
1008 goto end;
1009 }
1010
1011 channel_cb->userdata = &cdata;
1012
1013 ssh_callbacks_init(channel_cb);
1014 rc = ssh_set_channel_callbacks(sdata.channel, channel_cb);
1015 if (rc) {
1016 goto end;
1017 }
1018 }
1019
1020 do {
1021 /* Poll the main event which takes care of the session, the channel and
1022 * even our child process's stdout/stderr (once it's started). */
1023 if (ssh_event_dopoll(event, -1) == SSH_ERROR) {
1024 ssh_channel_close(sdata.channel);
1025 }
1026
1027 /* If child process's stdout/stderr has been registered with the event,
1028 * or the child process hasn't started yet, continue. */
1029 if (cdata.event != NULL || cdata.pid == 0) {
1030 continue;
1031 }
1032 /* Executed only once, once the child process starts. */
1033 cdata.event = event;
1034 /* If stdout valid, add stdout to be monitored by the poll event. */
1035 if (cdata.child_stdout != -1) {
1036 if (ssh_event_add_fd(event, cdata.child_stdout, POLLIN, process_stdout,
1037 sdata.channel) != SSH_OK) {
1038 fprintf(stderr, "Failed to register stdout to poll context\n");
1039 ssh_channel_close(sdata.channel);
1040 }
1041 }
1042
1043 /* If stderr valid, add stderr to be monitored by the poll event. */
1044 if (cdata.child_stderr != -1){
1045 if (ssh_event_add_fd(event, cdata.child_stderr, POLLIN, process_stderr,
1046 sdata.channel) != SSH_OK) {
1047 fprintf(stderr, "Failed to register stderr to poll context\n");
1048 ssh_channel_close(sdata.channel);
1049 }
1050 }
1051 } while(ssh_channel_is_open(sdata.channel) &&
1052 (cdata.pid == 0 || waitpid(cdata.pid, &rc, WNOHANG) == 0));
1053
1054 close(cdata.pty_master);
1055 close(cdata.child_stdin);
1056 close(cdata.child_stdout);
1057 close(cdata.child_stderr);
1058
1059 /* Remove the descriptors from the polling context, since they are now
1060 * closed, they will always trigger during the poll calls. */
1061 ssh_event_remove_fd(event, cdata.child_stdout);
1062 ssh_event_remove_fd(event, cdata.child_stderr);
1063
1064 /* If the child process exited. */
1065 if (kill(cdata.pid, 0) < 0 && WIFEXITED(rc)) {
1066 rc = WEXITSTATUS(rc);
1067 ssh_channel_request_send_exit_status(sdata.channel, rc);
1068 /* If client terminated the channel or the process did not exit nicely,
1069 * but only if something has been forked. */
1070 } else if (cdata.pid > 0) {
1071 kill(cdata.pid, SIGKILL);
1072 }
1073
1074 ssh_channel_send_eof(sdata.channel);
1075 ssh_channel_close(sdata.channel);
1076
1077 /* Wait up to 5 seconds for the client to terminate the session. */
1078 for (n = 0; n < 50 && (ssh_get_status(session) & SESSION_END) == 0; n++) {
1079 ssh_event_dopoll(event, 100);
1080 }
1081
1082 end:
1083 #ifdef WITH_PCAP
1084 cleanup_pcap(&sdata);
1085 #endif
1086 if (channel_cb != NULL) {
1087 free(channel_cb);
1088 }
1089 if (server_cb != NULL) {
1090 free(server_cb);
1091 }
1092 return;
1093 }