Debian Codegraph @

tests/server/test_server/test_server.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 #include "test_server.h"
25 #include "testserver_common.h"
26
27 #include <libssh/priv.h>
28 #include <libssh/libssh.h>
29 #include <libssh/server.h>
30 #include <libssh/callbacks.h>
31
32 #include <signal.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <stdbool.h>
37 #include <errno.h>
38
39 #include <sys/wait.h>
40
41 void free_server_state(struct server_state_st *state)
42 {
43 if (state == NULL) {
44 return;
45 }
46
47 SAFE_FREE(state->address);
48
49 SAFE_FREE(state->ecdsa_key);
50 SAFE_FREE(state->ed25519_key);
51 SAFE_FREE(state->rsa_key);
52 SAFE_FREE(state->host_key);
53
54 SAFE_FREE(state->pcap_file);
55
56 SAFE_FREE(state->expected_username);
57 SAFE_FREE(state->expected_password);
58 SAFE_FREE(state->config_file);
59 SAFE_FREE(state->log_file);
60 SAFE_FREE(state->server_cb);
61 SAFE_FREE(state->channel_cb);
62 }
63
64 /* SIGCHLD handler for cleaning up dead children. */
65 static void sigchld_handler(int signo) {
66 (void) signo;
67 while (waitpid(-1, NULL, WNOHANG) > 0);
68 }
69
70 bool done = false;
71
72 static void sigterm_handler(int signo)
73 {
74 (void) signo;
75 fprintf(stderr, "Received SIGTERM. Gracefully exiting ...\n");
76 done = true;
77 }
78
79 int run_server(struct server_state_st *state)
80 {
81 ssh_session session = NULL;
82 ssh_bind sshbind = NULL;
83 ssh_event event = NULL;
84
85 struct sigaction sa = {
86 .sa_flags = 0
87 };
88
89 int rc = SSH_ERROR;
90
91 /* Check provided state */
92 if (state == NULL) {
93 fprintf(stderr, "Invalid state\n");
94 goto out;
95 }
96
97 /* Set up SIGCHLD handler. */
98 sa.sa_handler = sigchld_handler;
99 sigemptyset(&sa.sa_mask);
100 sa.sa_flags = SA_RESTART | SA_NOCLDSTOP;
101
102 if (sigaction(SIGCHLD, &sa, NULL) != 0) {
103 fprintf(stderr, "Failed to register SIGCHLD handler\n");
104 goto out;
105 }
106
107 /* Set up SIGTERM handler. */
108 sa.sa_handler = sigterm_handler;
109 sa.sa_flags = 0;
110
111 if (sigaction(SIGTERM, &sa, NULL) != 0) {
112 fprintf(stderr, "Failed to register SIGTERM handler\n");
113 goto out;
114 }
115
116 /* Redirect all the output and errors to the file to avoid mixing up with
117 * the output from the client */
118 if (state->log_file != NULL) {
119 int fd;
120 FILE *f = fopen(state->log_file, "a");
121 if (f == NULL) {
122 fprintf(stderr, "Failed to open the log file: %s\n", strerror(errno));
123 goto out;
124 }
125 fd = dup2(fileno(f), STDERR_FILENO);
126 if (fd == -1) {
127 fprintf(stderr, "dup2 of log file to stderr failed: %s\n",
128 strerror(errno));
129 fclose(f);
130 goto out;
131 }
132 fd = dup2(fileno(f), STDOUT_FILENO);
133 if (fd == -1) {
134 fprintf(stderr, "dup2 of log file to stdout failed: %s\n",
135 strerror(errno));
136 fclose(f);
137 goto out;
138 }
139 fclose(f);
140 }
141
142 if (state->address == NULL) {
143 fprintf(stderr, "Missing bind address\n");
144 goto out;
145 }
146
147 if (state->host_key == NULL && state->rsa_key == NULL &&
148 state->ecdsa_key == NULL && state->ed25519_key == NULL) {
149 fprintf(stderr, "Missing host key\n");
150 }
151
152 sshbind = ssh_bind_new();
153 if (sshbind == NULL) {
154 fprintf(stderr, "Out of memory\n");
155 goto out;
156 }
157
158 if (state->verbosity) {
159 rc = ssh_bind_options_set(sshbind,
160 SSH_BIND_OPTIONS_LOG_VERBOSITY,
161 &state->verbosity);
162 if (rc != 0) {
163 fprintf(stderr,
164 "Error setting verbosity level: %s\n",
165 ssh_get_error(sshbind));
166 goto out;
167 }
168 }
169
170 if (!state->parse_global_config) {
171 rc = ssh_bind_options_set(sshbind,
172 SSH_BIND_OPTIONS_PROCESS_CONFIG,
173 &(state->parse_global_config));
174 if (rc != 0) {
175 goto out;
176 }
177 }
178
179 if (state->config_file) {
180 rc = ssh_bind_options_parse_config(sshbind, state->config_file);
181 if (rc != 0) {
182 goto out;
183 }
184 }
185
186 rc = ssh_bind_options_set(sshbind,
187 SSH_BIND_OPTIONS_BINDADDR,
188 state->address);
189 if (rc != 0) {
190 fprintf(stderr,
191 "Error setting bind address: %s\n",
192 ssh_get_error(sshbind));
193 goto out;
194 }
195
196 #ifdef WITH_GSSAPI
197 rc = ssh_bind_options_set(sshbind,
198 SSH_BIND_OPTIONS_GSSAPI_KEY_EXCHANGE,
199 &(state->gssapi_key_exchange));
200 if (rc != 0) {
201 fprintf(stderr,
202 "Error setting GSSAPI key exchange: %s\n",
203 ssh_get_error(sshbind));
204 goto out;
205 }
206
207 if (state->gssapi_key_exchange_algs != NULL) {
208 rc = ssh_bind_options_set(sshbind,
209 SSH_BIND_OPTIONS_GSSAPI_KEY_EXCHANGE_ALGS,
210 state->gssapi_key_exchange_algs);
211 if (rc != 0) {
212 fprintf(stderr,
213 "Error setting GSSAPI key exchange algorithms: %s\n",
214 ssh_get_error(sshbind));
215 goto out;
216 }
217 }
218 #endif /* WITH_GSSAPI */
219
220 rc = ssh_bind_options_set(sshbind,
221 SSH_BIND_OPTIONS_BINDPORT,
222 &(state->port));
223 if (rc != 0) {
224 fprintf(stderr,
225 "Error setting bind port: %s\n",
226 ssh_get_error(sshbind));
227 goto out;
228 }
229
230 if (state->rsa_key != NULL) {
231 rc = ssh_bind_options_set(sshbind,
232 SSH_BIND_OPTIONS_HOSTKEY,
233 state->rsa_key);
234 if (rc != 0) {
235 fprintf(stderr,
236 "Error setting RSA key: %s\n",
237 ssh_get_error(sshbind));
238 goto out;
239 }
240 }
241
242 if (state->ecdsa_key != NULL) {
243 rc = ssh_bind_options_set(sshbind,
244 SSH_BIND_OPTIONS_HOSTKEY,
245 state->ecdsa_key);
246 if (rc != 0) {
247 fprintf(stderr,
248 "Error setting ECDSA key: %s\n",
249 ssh_get_error(sshbind));
250 goto out;
251 }
252 }
253
254 if (state->host_key) {
255 rc = ssh_bind_options_set(sshbind,
256 SSH_BIND_OPTIONS_HOSTKEY,
257 state->host_key);
258 if (rc) {
259 fprintf(stderr,
260 "Error setting hostkey: %s\n",
261 ssh_get_error(sshbind));
262 goto out;
263 }
264 }
265
266 rc = ssh_bind_listen(sshbind);
267 if (rc != 0) {
268 fprintf(stderr,
269 "Error listening to socket: %s\n",
270 ssh_get_error(sshbind));
271 goto out;
272 }
273
274 printf("%d: Started libssh test server on port %d\n", getpid(), state->port);
275
276 while (done == false) {
277 session = ssh_new();
278 if (session == NULL) {
279 fprintf(stderr, "Out of memory\n");
280 rc = SSH_ERROR;
281 goto out;
282 }
283
284 /* Blocks until there is a new incoming connection. */
285 rc = ssh_bind_accept(sshbind, session);
286 if (rc != SSH_ERROR) {
287 pid_t pid = fork();
288
289 switch(pid) {
290 case 0:
291 /* Remove the SIGCHLD handler inherited from parent. */
292 sa.sa_handler = SIG_DFL;
293 sigaction(SIGCHLD, &sa, NULL);
294 /* Remove the SIGTERM handler inherited from parent. */
295 sa.sa_handler = SIG_DFL;
296 sigaction(SIGTERM, &sa, NULL);
297 /* Remove socket binding, which allows us to restart the
298 * parent process, without terminating existing sessions. */
299 ssh_bind_free(sshbind);
300
301 event = ssh_event_new();
302 if (event != NULL) {
303 /* Blocks until the SSH session ends by either
304 * child process exiting, or client disconnecting. */
305 state->handle_session(event, session, state);
306 ssh_event_free(event);
307 } else {
308 fprintf(stderr, "Could not create polling context\n");
309 }
310 ssh_disconnect(session);
311 ssh_free(session);
312
313 free_server_state(state);
314 SAFE_FREE(state);
315 finalize_openssl();
316 exit(0);
317 case -1:
318 fprintf(stderr, "Failed to fork\n");
319 }
320 fprintf(stderr, "Forked process PID %d\n", pid);
321 } else {
322 fprintf(stderr,
323 "Error accepting a connection: %s\n",
324 ssh_get_error(sshbind));
325 }
326
327 /* Since the session has been passed to a child fork, do some cleaning
328 * up at the parent process. */
329 ssh_disconnect(session);
330 ssh_free(session);
331 }
332
333 rc = 0;
334
335 out:
336 free_server_state(state);
337 SAFE_FREE(state);
338 ssh_bind_free(sshbind);
339 return rc;
340 }
341
342 pid_t
343 fork_run_server(struct server_state_st *state,
344 void (*free_test_state) (void **userdata),
345 void *userdata)
346 {
347 pid_t pid;
348 int rc;
349
350 char err_str[1024] = {0};
351
352 struct sigaction sa;
353
354 /* Check provided state */
355 if (state == NULL) {
356 fprintf(stderr, "Invalid state\n");
357 return -1;
358 }
359
360 /* Set up SIGCHLD handler. */
361 sa.sa_handler = sigchld_handler;
362 sigemptyset(&sa.sa_mask);
363 sa.sa_flags = SA_RESTART | SA_NOCLDSTOP;
364
365 if (sigaction(SIGCHLD, &sa, NULL) != 0) {
366 strerror_r(errno, err_str, 1024);
367 fprintf(stderr, "Failed to register SIGCHLD handler: %s\n",
368 err_str);
369 return -1;
370 }
371
372 pid = fork();
373 switch(pid) {
374 case 0:
375 /* no longer needed */
376 free_test_state(userdata);
377 /* Remove the SIGCHLD handler inherited from parent. */
378 sa.sa_handler = SIG_DFL;
379 sigaction(SIGCHLD, &sa, NULL);
380
381 /* The child process starts a server which will listen for connections */
382 rc = run_server(state);
383 finalize_openssl();
384 exit(rc);
385 case -1:
386 strerror_r(errno, err_str, 1024);
387 fprintf(stderr, "Failed to fork: %s\n",
388 err_str);
389 return -1;
390 default:
391 /* Return the child pid */
392 fprintf(stderr, "Forked process PID %d\n", pid);
393 return pid;
394 }
395 }