| 1 |
/* |
| 2 |
* This file is part of the SSH Library |
| 3 |
* |
| 4 |
* Copyright (c) 2019 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 "config.h" |
| 25 |
|
| 26 |
#define LIBSSH_STATIC |
| 27 |
|
| 28 |
#include <sys/types.h> |
| 29 |
#include <sys/stat.h> |
| 30 |
#include <errno.h> |
| 31 |
#include <pwd.h> |
| 32 |
|
| 33 |
#include "torture.h" |
| 34 |
#include "torture_key.h" |
| 35 |
#include "libssh/libssh.h" |
| 36 |
#include "libssh/priv.h" |
| 37 |
#include "libssh/session.h" |
| 38 |
|
| 39 |
#include <signal.h> |
| 40 |
#include <sys/wait.h> |
| 41 |
#include <sys/ioctl.h> |
| 42 |
|
| 43 |
#include "test_server.h" |
| 44 |
#include "default_cb.h" |
| 45 |
|
| 46 |
#define TORTURE_KNOWN_HOSTS_FILE "libssh_torture_knownhosts" |
| 47 |
|
| 48 |
enum { |
| 49 |
SUCCESS, |
| 50 |
MORE, |
| 51 |
FAILED |
| 52 |
}; |
| 53 |
|
| 54 |
struct test_server_st { |
| 55 |
struct torture_state *state; |
| 56 |
struct server_state_st *ss; |
| 57 |
}; |
| 58 |
|
| 59 |
#ifdef WITH_PCAP |
| 60 |
static void set_pcap(struct session_data_st *sdata, |
| 61 |
ssh_session session, |
| 62 |
char *pcap_file) |
| 63 |
{ |
| 64 |
int rc = 0; |
| 65 |
|
| 66 |
if (sdata == NULL) { |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
if (pcap_file == NULL) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
sdata->pcap = ssh_pcap_file_new(); |
| 75 |
if (sdata->pcap == NULL) { |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
rc = ssh_pcap_file_open(sdata->pcap, pcap_file); |
| 80 |
if (rc == SSH_ERROR) { |
| 81 |
fprintf(stderr, "Error opening pcap file\n"); |
| 82 |
ssh_pcap_file_free(sdata->pcap); |
| 83 |
sdata->pcap = NULL; |
| 84 |
return; |
| 85 |
} |
| 86 |
ssh_set_pcap_file(session, sdata->pcap); |
| 87 |
} |
| 88 |
|
| 89 |
static void cleanup_pcap(struct session_data_st *sdata) |
| 90 |
{ |
| 91 |
if (sdata == NULL) { |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
if (sdata->pcap == NULL) { |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
ssh_pcap_file_free(sdata->pcap); |
| 100 |
sdata->pcap = NULL; |
| 101 |
} |
| 102 |
#endif |
| 103 |
|
| 104 |
static int process_stdout(socket_t fd, int revents, void *userdata) |
| 105 |
{ |
| 106 |
char buf[BUF_SIZE]; |
| 107 |
int n = -1; |
| 108 |
ssh_channel channel = (ssh_channel) userdata; |
| 109 |
|
| 110 |
if (channel != NULL && (revents & POLLIN) != 0) { |
| 111 |
n = read(fd, buf, BUF_SIZE); |
| 112 |
if (n > 0) { |
| 113 |
ssh_channel_write(channel, buf, n); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
return n; |
| 118 |
} |
| 119 |
|
| 120 |
static int process_stderr(socket_t fd, int revents, void *userdata) |
| 121 |
{ |
| 122 |
char buf[BUF_SIZE]; |
| 123 |
int n = -1; |
| 124 |
ssh_channel channel = (ssh_channel) userdata; |
| 125 |
|
| 126 |
if (channel != NULL && (revents & POLLIN) != 0) { |
| 127 |
n = read(fd, buf, BUF_SIZE); |
| 128 |
if (n > 0) { |
| 129 |
ssh_channel_write_stderr(channel, buf, n); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
return n; |
| 134 |
} |
| 135 |
|
| 136 |
static int authenticate_kbdint(ssh_session session, |
| 137 |
ssh_message message, |
| 138 |
void *userdata) |
| 139 |
{ |
| 140 |
int rc = 0; |
| 141 |
int count; |
| 142 |
int *step = NULL; |
| 143 |
size_t expected_len; |
| 144 |
|
| 145 |
const char instruction[] = "Type the requested data"; |
| 146 |
const char name[] = "Keyboard-Interactive Authentication\n"; |
| 147 |
char initial_echo[] = {1, 0}; |
| 148 |
char retype_echo[] = {0}; |
| 149 |
const char *initial_prompt[2]; |
| 150 |
const char *retype_prompt[1]; |
| 151 |
int cmp; |
| 152 |
|
| 153 |
const char *answer; |
| 154 |
|
| 155 |
struct session_data_st *sdata = (struct session_data_st *)userdata; |
| 156 |
|
| 157 |
initial_prompt[0] = "username: "; |
| 158 |
initial_prompt[1] = "password: "; |
| 159 |
|
| 160 |
/* Prompt for additional prompts */ |
| 161 |
retype_prompt[0] = "retype password: "; |
| 162 |
|
| 163 |
if ((session == NULL) || (message == NULL) || (sdata == NULL)) { |
| 164 |
fprintf(stderr, "Null argument provided\n"); |
| 165 |
goto failed; |
| 166 |
} |
| 167 |
|
| 168 |
if (sdata->extra_data == NULL) { |
| 169 |
goto failed; |
| 170 |
} |
| 171 |
|
| 172 |
step = (int *)sdata->extra_data; |
| 173 |
|
| 174 |
switch (*step) { |
| 175 |
case 0: |
| 176 |
ssh_message_auth_interactive_request(message, name, instruction, 2, |
| 177 |
initial_prompt, initial_echo); |
| 178 |
rc = MORE; |
| 179 |
goto end; |
| 180 |
case 1: |
| 181 |
count = ssh_userauth_kbdint_getnanswers(session); |
| 182 |
if (count != 2) { |
| 183 |
goto failed; |
| 184 |
} |
| 185 |
|
| 186 |
if ((sdata->username == NULL) || (sdata->password == NULL)) { |
| 187 |
goto failed; |
| 188 |
} |
| 189 |
|
| 190 |
/* Get and compare username */ |
| 191 |
expected_len = strlen(sdata->username); |
| 192 |
if (expected_len <= 0) { |
| 193 |
goto failed; |
| 194 |
} |
| 195 |
|
| 196 |
answer = ssh_userauth_kbdint_getanswer(session, 0); |
| 197 |
if (answer == NULL) { |
| 198 |
goto failed; |
| 199 |
} |
| 200 |
|
| 201 |
cmp = strncmp(answer, sdata->username, expected_len); |
| 202 |
if (cmp != 0) { |
| 203 |
goto failed; |
| 204 |
} |
| 205 |
|
| 206 |
/* Get and compare password */ |
| 207 |
expected_len = strlen(sdata->password); |
| 208 |
if (expected_len <= 0) { |
| 209 |
goto failed; |
| 210 |
} |
| 211 |
|
| 212 |
answer = ssh_userauth_kbdint_getanswer(session, 1); |
| 213 |
if (answer == NULL) { |
| 214 |
goto failed; |
| 215 |
} |
| 216 |
|
| 217 |
cmp = strncmp(answer, sdata->password, expected_len); |
| 218 |
if (cmp != 0) { |
| 219 |
goto failed; |
| 220 |
} |
| 221 |
|
| 222 |
/* Username and password matched. Ask for a retype. */ |
| 223 |
ssh_message_auth_interactive_request(message, |
| 224 |
name, |
| 225 |
instruction, |
| 226 |
1, |
| 227 |
retype_prompt, |
| 228 |
retype_echo); |
| 229 |
|
| 230 |
rc = MORE; |
| 231 |
goto end; |
| 232 |
case 2: |
| 233 |
/* Get and compare password */ |
| 234 |
expected_len = strlen(sdata->password); |
| 235 |
if (expected_len <= 0) { |
| 236 |
goto failed; |
| 237 |
} |
| 238 |
|
| 239 |
answer = ssh_userauth_kbdint_getanswer(session, 0); |
| 240 |
if (answer == NULL) { |
| 241 |
goto failed; |
| 242 |
} |
| 243 |
|
| 244 |
cmp = strncmp(answer, sdata->password, expected_len); |
| 245 |
if (cmp != 0) { |
| 246 |
goto failed; |
| 247 |
} |
| 248 |
|
| 249 |
/* Password was correct, authenticated */ |
| 250 |
rc = SUCCESS; |
| 251 |
goto end; |
| 252 |
default: |
| 253 |
goto failed; |
| 254 |
} |
| 255 |
|
| 256 |
failed: |
| 257 |
if (step != NULL) { |
| 258 |
*step = 0; |
| 259 |
} |
| 260 |
return FAILED; |
| 261 |
|
| 262 |
end: |
| 263 |
if (step != NULL) { |
| 264 |
(*step)++; |
| 265 |
} |
| 266 |
return rc; |
| 267 |
} |
| 268 |
|
| 269 |
static int authenticate_callback(ssh_session session, |
| 270 |
ssh_message message, |
| 271 |
void *userdata) |
| 272 |
{ |
| 273 |
struct session_data_st *sdata = (struct session_data_st *)userdata; |
| 274 |
int rc; |
| 275 |
|
| 276 |
if (sdata == NULL) { |
| 277 |
fprintf(stderr, "Null userdata\n"); |
| 278 |
goto denied; |
| 279 |
} |
| 280 |
|
| 281 |
if (sdata->extra_data == NULL) { |
| 282 |
sdata->extra_data = (void *)calloc(1, sizeof(int)); |
| 283 |
} |
| 284 |
|
| 285 |
switch (ssh_message_type(message)) { |
| 286 |
case SSH_REQUEST_AUTH: |
| 287 |
switch (ssh_message_subtype(message)) { |
| 288 |
case SSH_AUTH_METHOD_INTERACTIVE: |
| 289 |
rc = authenticate_kbdint(session, message, (void *)sdata); |
| 290 |
if (rc == SUCCESS) { |
| 291 |
goto accept; |
| 292 |
} |
| 293 |
else if (rc == MORE) { |
| 294 |
goto more; |
| 295 |
} |
| 296 |
ssh_message_auth_set_methods(message, SSH_AUTH_METHOD_INTERACTIVE); |
| 297 |
goto denied; |
| 298 |
default: |
| 299 |
ssh_message_auth_set_methods(message, SSH_AUTH_METHOD_INTERACTIVE); |
| 300 |
goto denied; |
| 301 |
} |
| 302 |
default: |
| 303 |
ssh_message_auth_set_methods(message, SSH_AUTH_METHOD_INTERACTIVE); |
| 304 |
goto denied; |
| 305 |
} |
| 306 |
|
| 307 |
ssh_message_free(message); |
| 308 |
|
| 309 |
accept: |
| 310 |
if (sdata) { |
| 311 |
if (sdata->extra_data) { |
| 312 |
free(sdata->extra_data); |
| 313 |
sdata->extra_data = NULL; |
| 314 |
} |
| 315 |
} |
| 316 |
ssh_message_auth_reply_success (message, 0); |
| 317 |
more: |
| 318 |
return 0; |
| 319 |
denied: |
| 320 |
if (sdata) { |
| 321 |
if (sdata->extra_data) { |
| 322 |
free(sdata->extra_data); |
| 323 |
sdata->extra_data = NULL; |
| 324 |
} |
| 325 |
} |
| 326 |
return 1; |
| 327 |
} |
| 328 |
|
| 329 |
static void handle_kbdint_session_cb(ssh_event event, |
| 330 |
ssh_session session, |
| 331 |
struct server_state_st *state) |
| 332 |
{ |
| 333 |
int n; |
| 334 |
int rc = 0; |
| 335 |
|
| 336 |
/* Structure for storing the pty size. */ |
| 337 |
struct winsize wsize = { |
| 338 |
.ws_row = 0, |
| 339 |
.ws_col = 0, |
| 340 |
.ws_xpixel = 0, |
| 341 |
.ws_ypixel = 0 |
| 342 |
}; |
| 343 |
|
| 344 |
/* Our struct holding information about the channel. */ |
| 345 |
struct channel_data_st cdata = { |
| 346 |
.pid = 0, |
| 347 |
.pty_master = -1, |
| 348 |
.pty_slave = -1, |
| 349 |
.child_stdin = -1, |
| 350 |
.child_stdout = -1, |
| 351 |
.child_stderr = -1, |
| 352 |
.event = NULL, |
| 353 |
.winsize = &wsize |
| 354 |
}; |
| 355 |
|
| 356 |
/* Our struct holding information about the session. */ |
| 357 |
struct session_data_st sdata = { |
| 358 |
.channel = NULL, |
| 359 |
.auth_attempts = 0, |
| 360 |
.authenticated = 0, |
| 361 |
.username = TORTURE_SSH_USER_BOB, |
| 362 |
.password = TORTURE_SSH_USER_BOB_PASSWORD |
| 363 |
}; |
| 364 |
|
| 365 |
struct ssh_channel_callbacks_struct *channel_cb = NULL; |
| 366 |
struct ssh_server_callbacks_struct *server_cb = NULL; |
| 367 |
|
| 368 |
if (state == NULL) { |
| 369 |
fprintf(stderr, "NULL server state provided\n"); |
| 370 |
goto end; |
| 371 |
} |
| 372 |
|
| 373 |
server_cb = get_default_server_cb(); |
| 374 |
if (server_cb == NULL) { |
| 375 |
goto end; |
| 376 |
} |
| 377 |
|
| 378 |
/* |
| 379 |
* This test was written prior to adding the kbdint callback |
| 380 |
* for the server. Hence, here the server uses the |
| 381 |
* ssh_message_callback for kbdint authentication, |
| 382 |
* instead of the kbdint callback. |
| 383 |
* |
| 384 |
* Setting the kbdint callback as NULL ensures that the |
| 385 |
* default kbdint callback for test_server doesn't get used |
| 386 |
* for kbdint authentication. |
| 387 |
* |
| 388 |
* The test for kbdint callback based authentication has |
| 389 |
* been added in torture_server.c, libssh keeps this test to |
| 390 |
* test the old way of doing kbdint authentication using |
| 391 |
* ssh_message_callback. |
| 392 |
*/ |
| 393 |
server_cb->auth_kbdint_function = NULL; |
| 394 |
server_cb->userdata = &sdata; |
| 395 |
|
| 396 |
/* This is a macro, it does not return a value */ |
| 397 |
ssh_callbacks_init(server_cb); |
| 398 |
|
| 399 |
rc = ssh_set_server_callbacks(session, server_cb); |
| 400 |
if (rc) { |
| 401 |
goto end; |
| 402 |
} |
| 403 |
|
| 404 |
#ifdef WITH_PCAP |
| 405 |
set_pcap(&sdata, session, state->pcap_file); |
| 406 |
#endif |
| 407 |
|
| 408 |
rc = ssh_handle_key_exchange(session); |
| 409 |
if (rc != SSH_OK) { |
| 410 |
fprintf(stderr, "%s\n", ssh_get_error(session)); |
| 411 |
goto end; |
| 412 |
} |
| 413 |
|
| 414 |
/* Set the supported authentication methods */ |
| 415 |
ssh_set_auth_methods(session, SSH_AUTH_METHOD_INTERACTIVE); |
| 416 |
|
| 417 |
ssh_set_message_callback(session, authenticate_callback, &sdata); |
| 418 |
|
| 419 |
rc = ssh_event_add_session(event, session); |
| 420 |
if (rc != 0) { |
| 421 |
fprintf(stderr, "Error adding session to event\n"); |
| 422 |
goto end; |
| 423 |
} |
| 424 |
|
| 425 |
n = 0; |
| 426 |
while (sdata.authenticated == 0 || sdata.channel == NULL) { |
| 427 |
/* If the user has used up all attempts, or if he hasn't been able to |
| 428 |
* authenticate in 10 seconds (n * 100ms), disconnect. */ |
| 429 |
if (sdata.auth_attempts >= state->max_tries || n >= 100) { |
| 430 |
goto end; |
| 431 |
} |
| 432 |
|
| 433 |
if (ssh_event_dopoll(event, 100) == SSH_ERROR) { |
| 434 |
fprintf(stderr, "do_poll error: %s\n", ssh_get_error(session)); |
| 435 |
goto end; |
| 436 |
} |
| 437 |
n++; |
| 438 |
} |
| 439 |
|
| 440 |
channel_cb = get_default_channel_cb(); |
| 441 |
if (channel_cb == NULL) { |
| 442 |
goto end; |
| 443 |
} |
| 444 |
|
| 445 |
channel_cb->userdata = &cdata; |
| 446 |
|
| 447 |
ssh_callbacks_init(channel_cb); |
| 448 |
rc = ssh_set_channel_callbacks(sdata.channel, channel_cb); |
| 449 |
if (rc != 0) { |
| 450 |
goto end; |
| 451 |
} |
| 452 |
|
| 453 |
do { |
| 454 |
/* Poll the main event which takes care of the session, the channel and |
| 455 |
* even our child process's stdout/stderr (once it's started). */ |
| 456 |
rc = ssh_event_dopoll(event, -1); |
| 457 |
if (rc == SSH_ERROR) { |
| 458 |
ssh_channel_close(sdata.channel); |
| 459 |
} |
| 460 |
|
| 461 |
/* If child process's stdout/stderr has been registered with the event, |
| 462 |
* or the child process hasn't started yet, continue. */ |
| 463 |
if (cdata.event != NULL || cdata.pid == 0) { |
| 464 |
continue; |
| 465 |
} |
| 466 |
/* Executed only once, once the child process starts. */ |
| 467 |
cdata.event = event; |
| 468 |
/* If stdout valid, add stdout to be monitored by the poll event. */ |
| 469 |
if (cdata.child_stdout != -1) { |
| 470 |
if (ssh_event_add_fd(event, cdata.child_stdout, POLLIN, process_stdout, |
| 471 |
sdata.channel) != SSH_OK) { |
| 472 |
fprintf(stderr, "Failed to register stdout to poll context\n"); |
| 473 |
ssh_channel_close(sdata.channel); |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
/* If stderr valid, add stderr to be monitored by the poll event. */ |
| 478 |
if (cdata.child_stderr != -1){ |
| 479 |
if (ssh_event_add_fd(event, cdata.child_stderr, POLLIN, process_stderr, |
| 480 |
sdata.channel) != SSH_OK) { |
| 481 |
fprintf(stderr, "Failed to register stderr to poll context\n"); |
| 482 |
ssh_channel_close(sdata.channel); |
| 483 |
} |
| 484 |
} |
| 485 |
} while(ssh_channel_is_open(sdata.channel) && |
| 486 |
(cdata.pid == 0 || waitpid(cdata.pid, &rc, WNOHANG) == 0)); |
| 487 |
|
| 488 |
close(cdata.pty_master); |
| 489 |
close(cdata.child_stdin); |
| 490 |
close(cdata.child_stdout); |
| 491 |
close(cdata.child_stderr); |
| 492 |
|
| 493 |
/* Remove the descriptors from the polling context, since they are now |
| 494 |
* closed, they will always trigger during the poll calls. */ |
| 495 |
ssh_event_remove_fd(event, cdata.child_stdout); |
| 496 |
ssh_event_remove_fd(event, cdata.child_stderr); |
| 497 |
|
| 498 |
/* If the child process exited. */ |
| 499 |
if (kill(cdata.pid, 0) < 0 && WIFEXITED(rc)) { |
| 500 |
rc = WEXITSTATUS(rc); |
| 501 |
ssh_channel_request_send_exit_status(sdata.channel, rc); |
| 502 |
/* If client terminated the channel or the process did not exit nicely, |
| 503 |
* but only if something has been forked. */ |
| 504 |
} else if (cdata.pid > 0) { |
| 505 |
kill(cdata.pid, SIGKILL); |
| 506 |
} |
| 507 |
|
| 508 |
ssh_channel_send_eof(sdata.channel); |
| 509 |
ssh_channel_close(sdata.channel); |
| 510 |
|
| 511 |
/* Wait up to 5 seconds for the client to terminate the session. */ |
| 512 |
for (n = 0; n < 50 && (ssh_get_status(session) & SESSION_END) == 0; n++) { |
| 513 |
ssh_event_dopoll(event, 100); |
| 514 |
} |
| 515 |
|
| 516 |
end: |
| 517 |
#ifdef WITH_PCAP |
| 518 |
cleanup_pcap(&sdata); |
| 519 |
#endif |
| 520 |
if (channel_cb != NULL) { |
| 521 |
free(channel_cb); |
| 522 |
} |
| 523 |
if (server_cb != NULL) { |
| 524 |
free(server_cb); |
| 525 |
} |
| 526 |
return; |
| 527 |
} |
| 528 |
|
| 529 |
static void free_test_server_state(void **state) |
| 530 |
{ |
| 531 |
struct test_server_st *tss = *state; |
| 532 |
|
| 533 |
torture_free_state(tss->state); |
| 534 |
SAFE_FREE(tss); |
| 535 |
} |
| 536 |
|
| 537 |
static int setup_kbdint_server(void **state) |
| 538 |
{ |
| 539 |
struct torture_state *s; |
| 540 |
struct server_state_st *ss; |
| 541 |
struct test_server_st *tss; |
| 542 |
|
| 543 |
char rsa_hostkey[1024] = {0}; |
| 544 |
|
| 545 |
char sshd_path[1024]; |
| 546 |
char log_file[1024]; |
| 547 |
|
| 548 |
int rc; |
| 549 |
|
| 550 |
char pid_str[1024]; |
| 551 |
|
| 552 |
pid_t pid; |
| 553 |
|
| 554 |
assert_non_null(state); |
| 555 |
|
| 556 |
tss = (struct test_server_st*)calloc(1, sizeof(struct test_server_st)); |
| 557 |
assert_non_null(tss); |
| 558 |
|
| 559 |
torture_setup_socket_dir((void **)&s); |
| 560 |
assert_non_null(s->socket_dir); |
| 561 |
|
| 562 |
/* Set the default interface for the server */ |
| 563 |
setenv("SOCKET_WRAPPER_DEFAULT_IFACE", "10", 1); |
| 564 |
setenv("PAM_WRAPPER", "1", 1); |
| 565 |
|
| 566 |
snprintf(sshd_path, |
| 567 |
sizeof(sshd_path), |
| 568 |
"%s/sshd", |
| 569 |
s->socket_dir); |
| 570 |
|
| 571 |
rc = mkdir(sshd_path, 0755); |
| 572 |
assert_return_code(rc, errno); |
| 573 |
|
| 574 |
snprintf(log_file, |
| 575 |
sizeof(log_file), |
| 576 |
"%s/sshd/log", |
| 577 |
s->socket_dir); |
| 578 |
|
| 579 |
snprintf(rsa_hostkey, |
| 580 |
sizeof(rsa_hostkey), |
| 581 |
"%s/sshd/ssh_host_rsa_key", |
| 582 |
s->socket_dir); |
| 583 |
torture_write_file(rsa_hostkey, |
| 584 |
torture_get_openssh_testkey(SSH_KEYTYPE_RSA, 0)); |
| 585 |
|
| 586 |
/* Create the server state */ |
| 587 |
ss = (struct server_state_st *)calloc(1, sizeof(struct server_state_st)); |
| 588 |
assert_non_null(ss); |
| 589 |
|
| 590 |
ss->address = strdup("127.0.0.10"); |
| 591 |
assert_non_null(ss->address); |
| 592 |
|
| 593 |
ss->port = 22; |
| 594 |
|
| 595 |
ss->host_key = strdup(rsa_hostkey); |
| 596 |
assert_non_null(rsa_hostkey); |
| 597 |
|
| 598 |
/* not to mix up the client and server messages */ |
| 599 |
ss->verbosity = torture_libssh_verbosity(); |
| 600 |
ss->log_file = strdup(log_file); |
| 601 |
|
| 602 |
#ifdef WITH_PCAP |
| 603 |
ss->with_pcap = 1; |
| 604 |
ss->pcap_file = strdup(s->pcap_file); |
| 605 |
assert_non_null(ss->pcap_file); |
| 606 |
#endif |
| 607 |
|
| 608 |
ss->max_tries = 3; |
| 609 |
ss->error = 0; |
| 610 |
|
| 611 |
tss->state = s; |
| 612 |
tss->ss = ss; |
| 613 |
|
| 614 |
/* Set the session handling function */ |
| 615 |
ss->handle_session = handle_kbdint_session_cb; |
| 616 |
assert_non_null(ss->handle_session); |
| 617 |
|
| 618 |
/* Start the server */ |
| 619 |
pid = fork_run_server(ss, free_test_server_state, &tss); |
| 620 |
if (pid < 0) { |
| 621 |
fail(); |
| 622 |
} |
| 623 |
|
| 624 |
snprintf(pid_str, sizeof(pid_str), "%d", pid); |
| 625 |
|
| 626 |
torture_write_file(s->srv_pidfile, (const char *)pid_str); |
| 627 |
|
| 628 |
setenv("SOCKET_WRAPPER_DEFAULT_IFACE", "21", 1); |
| 629 |
unsetenv("PAM_WRAPPER"); |
| 630 |
|
| 631 |
rc = torture_wait_for_daemon(15); |
| 632 |
assert_int_equal(rc, 0); |
| 633 |
|
| 634 |
*state = tss; |
| 635 |
|
| 636 |
return 0; |
| 637 |
} |
| 638 |
|
| 639 |
static int teardown_kbdint_server(void **state) |
| 640 |
{ |
| 641 |
struct torture_state *s; |
| 642 |
struct server_state_st *ss; |
| 643 |
struct test_server_st *tss; |
| 644 |
|
| 645 |
tss = *state; |
| 646 |
assert_non_null(tss); |
| 647 |
|
| 648 |
s = tss->state; |
| 649 |
assert_non_null(s); |
| 650 |
|
| 651 |
ss = tss->ss; |
| 652 |
assert_non_null(ss); |
| 653 |
|
| 654 |
/* This function can be reused */ |
| 655 |
torture_teardown_sshd_server((void **)&s); |
| 656 |
|
| 657 |
free_server_state(tss->ss); |
| 658 |
SAFE_FREE(tss->ss); |
| 659 |
SAFE_FREE(tss); |
| 660 |
|
| 661 |
return 0; |
| 662 |
} |
| 663 |
|
| 664 |
static int session_setup(void **state) |
| 665 |
{ |
| 666 |
struct test_server_st *tss = *state; |
| 667 |
struct torture_state *s; |
| 668 |
int verbosity = torture_libssh_verbosity(); |
| 669 |
struct passwd *pwd; |
| 670 |
bool b = false; |
| 671 |
int rc; |
| 672 |
|
| 673 |
assert_non_null(tss); |
| 674 |
|
| 675 |
s = tss->state; |
| 676 |
assert_non_null(s); |
| 677 |
|
| 678 |
pwd = getpwnam("bob"); |
| 679 |
assert_non_null(pwd); |
| 680 |
|
| 681 |
rc = setuid(pwd->pw_uid); |
| 682 |
assert_return_code(rc, errno); |
| 683 |
|
| 684 |
s->ssh.session = ssh_new(); |
| 685 |
assert_non_null(s->ssh.session); |
| 686 |
|
| 687 |
rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity); |
| 688 |
assert_ssh_return_code(s->ssh.session, rc); |
| 689 |
rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_HOST, TORTURE_SSH_SERVER); |
| 690 |
assert_ssh_return_code(s->ssh.session, rc); |
| 691 |
/* Make sure no other configuration options from system will get used */ |
| 692 |
rc = ssh_options_set(s->ssh.session, SSH_OPTIONS_PROCESS_CONFIG, &b); |
| 693 |
assert_ssh_return_code(s->ssh.session, rc); |
| 694 |
|
| 695 |
return 0; |
| 696 |
} |
| 697 |
|
| 698 |
static int session_teardown(void **state) |
| 699 |
{ |
| 700 |
struct test_server_st *tss = *state; |
| 701 |
struct torture_state *s; |
| 702 |
|
| 703 |
assert_non_null(tss); |
| 704 |
|
| 705 |
s = tss->state; |
| 706 |
assert_non_null(s); |
| 707 |
|
| 708 |
ssh_disconnect(s->ssh.session); |
| 709 |
ssh_free(s->ssh.session); |
| 710 |
|
| 711 |
return 0; |
| 712 |
} |
| 713 |
|
| 714 |
static void torture_server_auth_kbdint(void **state) |
| 715 |
{ |
| 716 |
struct test_server_st *tss = *state; |
| 717 |
struct torture_state *s; |
| 718 |
ssh_session session; |
| 719 |
int rc; |
| 720 |
|
| 721 |
assert_non_null(tss); |
| 722 |
|
| 723 |
s = tss->state; |
| 724 |
assert_non_null(s); |
| 725 |
|
| 726 |
session = s->ssh.session; |
| 727 |
assert_non_null(session); |
| 728 |
|
| 729 |
rc = ssh_options_set(session, SSH_OPTIONS_USER, TORTURE_SSH_USER_BOB); |
| 730 |
assert_ssh_return_code(session, rc); |
| 731 |
|
| 732 |
rc = ssh_connect(session); |
| 733 |
assert_ssh_return_code(session, rc); |
| 734 |
|
| 735 |
rc = ssh_userauth_none(session,NULL); |
| 736 |
/* This request should return a SSH_REQUEST_DENIED error */ |
| 737 |
if (rc == SSH_ERROR) { |
| 738 |
assert_int_equal(ssh_get_error_code(session), SSH_REQUEST_DENIED); |
| 739 |
} |
| 740 |
rc = ssh_userauth_list(session, NULL); |
| 741 |
assert_true(rc & SSH_AUTH_METHOD_INTERACTIVE); |
| 742 |
|
| 743 |
rc = ssh_userauth_kbdint(session, NULL, NULL); |
| 744 |
assert_int_equal(rc, SSH_AUTH_INFO); |
| 745 |
assert_int_equal(ssh_userauth_kbdint_getnprompts(session), 2); |
| 746 |
|
| 747 |
/* Reply the first 2 prompts using the username and password */ |
| 748 |
rc = ssh_userauth_kbdint_setanswer(session, 0, |
| 749 |
TORTURE_SSH_USER_BOB); |
| 750 |
assert_false(rc < 0); |
| 751 |
|
| 752 |
rc = ssh_userauth_kbdint_setanswer(session, 1, |
| 753 |
TORTURE_SSH_USER_BOB_PASSWORD); |
| 754 |
assert_false(rc < 0); |
| 755 |
|
| 756 |
/* Resend the password */ |
| 757 |
rc = ssh_userauth_kbdint(session, NULL, NULL); |
| 758 |
assert_int_equal(rc, SSH_AUTH_INFO); |
| 759 |
assert_int_equal(ssh_userauth_kbdint_getnprompts(session), 1); |
| 760 |
|
| 761 |
rc = ssh_userauth_kbdint_setanswer(session, 0, |
| 762 |
TORTURE_SSH_USER_BOB_PASSWORD); |
| 763 |
assert_false(rc < 0); |
| 764 |
|
| 765 |
rc = ssh_userauth_kbdint(session, NULL, NULL); |
| 766 |
|
| 767 |
/* Sometimes, SSH server send an empty query at the end of exchange */ |
| 768 |
if(rc == SSH_AUTH_INFO) { |
| 769 |
assert_int_equal(ssh_userauth_kbdint_getnprompts(session), 0); |
| 770 |
rc = ssh_userauth_kbdint(session, NULL, NULL); |
| 771 |
} |
| 772 |
|
| 773 |
assert_int_equal(rc, SSH_AUTH_SUCCESS); |
| 774 |
} |
| 775 |
|
| 776 |
int torture_run_tests(void) |
| 777 |
{ |
| 778 |
int rc; |
| 779 |
struct CMUnitTest tests[] = { |
| 780 |
cmocka_unit_test_setup_teardown(torture_server_auth_kbdint, |
| 781 |
session_setup, |
| 782 |
session_teardown), |
| 783 |
}; |
| 784 |
|
| 785 |
ssh_init(); |
| 786 |
|
| 787 |
torture_filter_tests(tests); |
| 788 |
rc = cmocka_run_group_tests(tests, |
| 789 |
setup_kbdint_server, |
| 790 |
teardown_kbdint_server); |
| 791 |
|
| 792 |
ssh_finalize(); |
| 793 |
|
| 794 |
return rc; |
| 795 |
} |