aboutsummaryrefslogtreecommitdiff
path: root/tests/test_ssh_bind_accept_fd.c
blob: 7611cf4cb61c619d4af43e58a1c8646811c8f238 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* Test the ability to use ssh_bind_accept_fd.
 *
 * Expected behavior: Prints "SUCCESS!"
 *
 * Faulty behavior observed before change: Connection timeout
 */

#include <arpa/inet.h>
#include <err.h>
#include <libssh/libssh.h>
#include <libssh/server.h>
#include <netinet/in.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>

struct options {
  const char *server_keyfile;
} options;

const char HOST[] = "127.0.0.1";
const int PORT = 3333;

int get_connection() {
  int rc, server_socket, client_conn = -1;
  struct sockaddr_in server_socket_addr;
  struct sockaddr_storage client_conn_addr;
  socklen_t client_conn_addr_size = sizeof(client_conn_addr);

  server_socket = socket(PF_INET, SOCK_STREAM, 0);
  if (server_socket < 0) {
    goto out;
  }

  server_socket_addr.sin_family = AF_INET;
  server_socket_addr.sin_port = htons(PORT);
  if (inet_pton(AF_INET, HOST, &server_socket_addr.sin_addr) != 1) {
    goto out;
  }

  rc = bind(server_socket, (struct sockaddr *)&server_socket_addr,
            sizeof(server_socket_addr));
  if (rc < 0) {
    goto out;
  }

  if (listen(server_socket, 0) < 0) {
    goto out;
  }

  client_conn = accept(server_socket,
                       (struct sockaddr *)&client_conn_addr,
                       &client_conn_addr_size);

 out:
  return client_conn;
}

void ssh_server() {
  ssh_bind bind;
  ssh_session session;

  int client_conn = get_connection();
  if (client_conn < 0) {
    err(1, "get_connection");
  }

  bind = ssh_bind_new();
  if (!bind) {
    errx(1, "ssh_bind_new");
  }

  if (ssh_bind_options_set(bind, SSH_BIND_OPTIONS_DSAKEY,
                           options.server_keyfile) != SSH_OK) {
    errx(1, "ssh_bind_options_set(SSH_BIND_OPTIONS_DSAKEY");
  }

  session = ssh_new();
  if (!session) {
    errx(1, "ssh_new");
  }

  if (ssh_bind_accept_fd(bind, session, client_conn) != SSH_OK) {
    errx(1, "ssh_bind_accept: %s", ssh_get_error(bind));
  }

  if (ssh_handle_key_exchange(session) != SSH_OK) {
    errx(1, "ssh_handle_key_exchange: %s", ssh_get_error(session));
  }

  printf("SUCCESS!\n");
}

void ssh_client() {
  ssh_session session;

  session = ssh_new();
  if (!session) {
    errx(1, "ssh_new");
  }

  if (ssh_options_set(session, SSH_OPTIONS_HOST, HOST) < 0) {
    errx(1, "ssh_options_set(SSH_OPTIONS_HOST)");
  }
  if (ssh_options_set(session, SSH_OPTIONS_PORT, &PORT) < 0) {
    errx(1, "ssh_options_set(SSH_OPTIONS_PORT)");
  }

  if (ssh_connect(session) != SSH_OK) {
    errx(1, "ssh_connect: %s", ssh_get_error(session));
  }
}

int main(int argc, const char *argv[]) {
  if (argc != 2) {
    printf("Usage: %s <private key file>\n", argv[0]);
    exit(1);
  }

  options.server_keyfile = argv[1];

  pid_t pid = fork();
  if (pid < 0) {
    errx(1, "fork");
  }
  if (pid == 0) {
    /* Allow the server to get set up */
    sleep(3);

    ssh_client();
  } else {
    ssh_server();
  }

  return 0;
}