diff options
Diffstat (limited to 'other/openssh-2.1.1p4/sshd.c')
| -rw-r--r-- | other/openssh-2.1.1p4/sshd.c | 1431 |
1 files changed, 0 insertions, 1431 deletions
diff --git a/other/openssh-2.1.1p4/sshd.c b/other/openssh-2.1.1p4/sshd.c deleted file mode 100644 index f74a9e3..0000000 --- a/other/openssh-2.1.1p4/sshd.c +++ /dev/null | |||
| @@ -1,1431 +0,0 @@ | |||
| 1 | /* | ||
| 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> | ||
| 3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland | ||
| 4 | * All rights reserved | ||
| 5 | * Created: Fri Mar 17 17:09:28 1995 ylo | ||
| 6 | * This program is the ssh daemon. It listens for connections from clients, and | ||
| 7 | * performs authentication, executes use commands or shell, and forwards | ||
| 8 | * information to/from the application to the user client over an encrypted | ||
| 9 | * connection. This can also handle forwarding of X11, TCP/IP, and authentication | ||
| 10 | * agent connections. | ||
| 11 | * | ||
| 12 | * SSH2 implementation, | ||
| 13 | * Copyright (c) 2000 Markus Friedl. All rights reserved. | ||
| 14 | */ | ||
| 15 | |||
| 16 | #include "includes.h" | ||
| 17 | RCSID("$OpenBSD: sshd.c,v 1.122 2000/07/11 08:11:34 deraadt Exp $"); | ||
| 18 | |||
| 19 | #include "xmalloc.h" | ||
| 20 | #include "rsa.h" | ||
| 21 | #include "ssh.h" | ||
| 22 | #include "pty.h" | ||
| 23 | #include "packet.h" | ||
| 24 | #include "cipher.h" | ||
| 25 | #include "mpaux.h" | ||
| 26 | #include "servconf.h" | ||
| 27 | #include "uidswap.h" | ||
| 28 | #include "compat.h" | ||
| 29 | #include "buffer.h" | ||
| 30 | |||
| 31 | #include "ssh2.h" | ||
| 32 | #include <openssl/dh.h> | ||
| 33 | #include <openssl/bn.h> | ||
| 34 | #include <openssl/hmac.h> | ||
| 35 | #include "kex.h" | ||
| 36 | #include <openssl/dsa.h> | ||
| 37 | #include <openssl/rsa.h> | ||
| 38 | |||
| 39 | #include "key.h" | ||
| 40 | #include "dsa.h" | ||
| 41 | |||
| 42 | #include "auth.h" | ||
| 43 | #include "myproposal.h" | ||
| 44 | #include "authfile.h" | ||
| 45 | |||
| 46 | #ifdef LIBWRAP | ||
| 47 | #include <tcpd.h> | ||
| 48 | #include <syslog.h> | ||
| 49 | int allow_severity = LOG_INFO; | ||
| 50 | int deny_severity = LOG_WARNING; | ||
| 51 | #endif /* LIBWRAP */ | ||
| 52 | |||
| 53 | #ifndef O_NOCTTY | ||
| 54 | #define O_NOCTTY 0 | ||
| 55 | #endif | ||
| 56 | |||
| 57 | /* Server configuration options. */ | ||
| 58 | ServerOptions options; | ||
| 59 | |||
| 60 | /* Name of the server configuration file. */ | ||
| 61 | char *config_file_name = SERVER_CONFIG_FILE; | ||
| 62 | |||
| 63 | /* | ||
| 64 | * Flag indicating whether IPv4 or IPv6. This can be set on the command line. | ||
| 65 | * Default value is AF_UNSPEC means both IPv4 and IPv6. | ||
| 66 | */ | ||
| 67 | #ifdef IPV4_DEFAULT | ||
| 68 | int IPv4or6 = AF_INET; | ||
| 69 | #else | ||
| 70 | int IPv4or6 = AF_UNSPEC; | ||
| 71 | #endif | ||
| 72 | |||
| 73 | /* | ||
| 74 | * Debug mode flag. This can be set on the command line. If debug | ||
| 75 | * mode is enabled, extra debugging output will be sent to the system | ||
| 76 | * log, the daemon will not go to background, and will exit after processing | ||
| 77 | * the first connection. | ||
| 78 | */ | ||
| 79 | int debug_flag = 0; | ||
| 80 | |||
| 81 | /* Flag indicating that the daemon is being started from inetd. */ | ||
| 82 | int inetd_flag = 0; | ||
| 83 | |||
| 84 | /* debug goes to stderr unless inetd_flag is set */ | ||
| 85 | int log_stderr = 0; | ||
| 86 | |||
| 87 | /* argv[0] without path. */ | ||
| 88 | char *av0; | ||
| 89 | |||
| 90 | /* Saved arguments to main(). */ | ||
| 91 | char **saved_argv; | ||
| 92 | int saved_argc; | ||
| 93 | |||
| 94 | /* | ||
| 95 | * The sockets that the server is listening; this is used in the SIGHUP | ||
| 96 | * signal handler. | ||
| 97 | */ | ||
| 98 | #define MAX_LISTEN_SOCKS 16 | ||
| 99 | int listen_socks[MAX_LISTEN_SOCKS]; | ||
| 100 | int num_listen_socks = 0; | ||
| 101 | |||
| 102 | /* | ||
| 103 | * the client's version string, passed by sshd2 in compat mode. if != NULL, | ||
| 104 | * sshd will skip the version-number exchange | ||
| 105 | */ | ||
| 106 | char *client_version_string = NULL; | ||
| 107 | char *server_version_string = NULL; | ||
| 108 | |||
| 109 | /* | ||
| 110 | * Any really sensitive data in the application is contained in this | ||
| 111 | * structure. The idea is that this structure could be locked into memory so | ||
| 112 | * that the pages do not get written into swap. However, there are some | ||
| 113 | * problems. The private key contains BIGNUMs, and we do not (in principle) | ||
| 114 | * have access to the internals of them, and locking just the structure is | ||
| 115 | * not very useful. Currently, memory locking is not implemented. | ||
| 116 | */ | ||
| 117 | struct { | ||
| 118 | RSA *private_key; /* Private part of empheral server key. */ | ||
| 119 | RSA *host_key; /* Private part of host key. */ | ||
| 120 | Key *dsa_host_key; /* Private DSA host key. */ | ||
| 121 | } sensitive_data; | ||
| 122 | |||
| 123 | /* | ||
| 124 | * Flag indicating whether the current session key has been used. This flag | ||
| 125 | * is set whenever the key is used, and cleared when the key is regenerated. | ||
| 126 | */ | ||
| 127 | int key_used = 0; | ||
| 128 | |||
| 129 | /* This is set to true when SIGHUP is received. */ | ||
| 130 | int received_sighup = 0; | ||
| 131 | |||
| 132 | /* Public side of the server key. This value is regenerated regularly with | ||
| 133 | the private key. */ | ||
| 134 | RSA *public_key; | ||
| 135 | |||
| 136 | /* session identifier, used by RSA-auth */ | ||
| 137 | unsigned char session_id[16]; | ||
| 138 | |||
| 139 | /* same for ssh2 */ | ||
| 140 | unsigned char *session_id2 = NULL; | ||
| 141 | int session_id2_len = 0; | ||
| 142 | |||
| 143 | /* Prototypes for various functions defined later in this file. */ | ||
| 144 | void do_ssh1_kex(); | ||
| 145 | void do_ssh2_kex(); | ||
| 146 | |||
| 147 | /* | ||
| 148 | * Close all listening sockets | ||
| 149 | */ | ||
| 150 | void | ||
| 151 | close_listen_socks(void) | ||
| 152 | { | ||
| 153 | int i; | ||
| 154 | for (i = 0; i < num_listen_socks; i++) | ||
| 155 | close(listen_socks[i]); | ||
| 156 | num_listen_socks = -1; | ||
| 157 | } | ||
| 158 | |||
| 159 | /* | ||
| 160 | * Signal handler for SIGHUP. Sshd execs itself when it receives SIGHUP; | ||
| 161 | * the effect is to reread the configuration file (and to regenerate | ||
| 162 | * the server key). | ||
| 163 | */ | ||
| 164 | void | ||
| 165 | sighup_handler(int sig) | ||
| 166 | { | ||
| 167 | received_sighup = 1; | ||
| 168 | signal(SIGHUP, sighup_handler); | ||
| 169 | } | ||
| 170 | |||
| 171 | /* | ||
| 172 | * Called from the main program after receiving SIGHUP. | ||
| 173 | * Restarts the server. | ||
| 174 | */ | ||
| 175 | void | ||
| 176 | sighup_restart() | ||
| 177 | { | ||
| 178 | log("Received SIGHUP; restarting."); | ||
| 179 | close_listen_socks(); | ||
| 180 | execv(saved_argv[0], saved_argv); | ||
| 181 | log("RESTART FAILED: av0='%s', error: %s.", av0, strerror(errno)); | ||
| 182 | exit(1); | ||
| 183 | } | ||
| 184 | |||
| 185 | /* | ||
| 186 | * Generic signal handler for terminating signals in the master daemon. | ||
| 187 | * These close the listen socket; not closing it seems to cause "Address | ||
| 188 | * already in use" problems on some machines, which is inconvenient. | ||
| 189 | */ | ||
| 190 | void | ||
| 191 | sigterm_handler(int sig) | ||
| 192 | { | ||
| 193 | log("Received signal %d; terminating.", sig); | ||
| 194 | close_listen_socks(); | ||
| 195 | unlink(options.pid_file); | ||
| 196 | exit(255); | ||
| 197 | } | ||
| 198 | |||
| 199 | /* | ||
| 200 | * SIGCHLD handler. This is called whenever a child dies. This will then | ||
| 201 | * reap any zombies left by exited c. | ||
| 202 | */ | ||
| 203 | void | ||
| 204 | main_sigchld_handler(int sig) | ||
| 205 | { | ||
| 206 | int save_errno = errno; | ||
| 207 | int status; | ||
| 208 | |||
| 209 | while (waitpid(-1, &status, WNOHANG) > 0) | ||
| 210 | ; | ||
| 211 | |||
| 212 | signal(SIGCHLD, main_sigchld_handler); | ||
| 213 | errno = save_errno; | ||
| 214 | } | ||
| 215 | |||
| 216 | /* | ||
| 217 | * Signal handler for the alarm after the login grace period has expired. | ||
| 218 | */ | ||
| 219 | void | ||
| 220 | grace_alarm_handler(int sig) | ||
| 221 | { | ||
| 222 | /* Close the connection. */ | ||
| 223 | packet_close(); | ||
| 224 | |||
| 225 | /* Log error and exit. */ | ||
| 226 | fatal("Timeout before authentication for %s.", get_remote_ipaddr()); | ||
| 227 | } | ||
| 228 | |||
| 229 | /* | ||
| 230 | * Signal handler for the key regeneration alarm. Note that this | ||
| 231 | * alarm only occurs in the daemon waiting for connections, and it does not | ||
| 232 | * do anything with the private key or random state before forking. | ||
| 233 | * Thus there should be no concurrency control/asynchronous execution | ||
| 234 | * problems. | ||
| 235 | */ | ||
| 236 | /* XXX do we really want this work to be done in a signal handler ? -m */ | ||
| 237 | void | ||
| 238 | key_regeneration_alarm(int sig) | ||
| 239 | { | ||
| 240 | int save_errno = errno; | ||
| 241 | |||
| 242 | /* Check if we should generate a new key. */ | ||
| 243 | if (key_used) { | ||
| 244 | /* This should really be done in the background. */ | ||
| 245 | log("Generating new %d bit RSA key.", options.server_key_bits); | ||
| 246 | |||
| 247 | if (sensitive_data.private_key != NULL) | ||
| 248 | RSA_free(sensitive_data.private_key); | ||
| 249 | sensitive_data.private_key = RSA_new(); | ||
| 250 | |||
| 251 | if (public_key != NULL) | ||
| 252 | RSA_free(public_key); | ||
| 253 | public_key = RSA_new(); | ||
| 254 | |||
| 255 | rsa_generate_key(sensitive_data.private_key, public_key, | ||
| 256 | options.server_key_bits); | ||
| 257 | arc4random_stir(); | ||
| 258 | key_used = 0; | ||
| 259 | log("RSA key generation complete."); | ||
| 260 | } | ||
| 261 | /* Reschedule the alarm. */ | ||
| 262 | signal(SIGALRM, key_regeneration_alarm); | ||
| 263 | alarm(options.key_regeneration_time); | ||
| 264 | errno = save_errno; | ||
| 265 | } | ||
| 266 | |||
| 267 | void | ||
| 268 | sshd_exchange_identification(int sock_in, int sock_out) | ||
| 269 | { | ||
| 270 | int i, mismatch; | ||
| 271 | int remote_major, remote_minor; | ||
| 272 | int major, minor; | ||
| 273 | char *s; | ||
| 274 | char buf[256]; /* Must not be larger than remote_version. */ | ||
| 275 | char remote_version[256]; /* Must be at least as big as buf. */ | ||
| 276 | |||
| 277 | if ((options.protocol & SSH_PROTO_1) && | ||
| 278 | (options.protocol & SSH_PROTO_2)) { | ||
| 279 | major = PROTOCOL_MAJOR_1; | ||
| 280 | minor = 99; | ||
| 281 | } else if (options.protocol & SSH_PROTO_2) { | ||
| 282 | major = PROTOCOL_MAJOR_2; | ||
| 283 | minor = PROTOCOL_MINOR_2; | ||
| 284 | } else { | ||
| 285 | major = PROTOCOL_MAJOR_1; | ||
| 286 | minor = PROTOCOL_MINOR_1; | ||
| 287 | } | ||
| 288 | snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n", major, minor, SSH_VERSION); | ||
| 289 | server_version_string = xstrdup(buf); | ||
| 290 | |||
| 291 | if (client_version_string == NULL) { | ||
| 292 | /* Send our protocol version identification. */ | ||
| 293 | if (atomicio(write, sock_out, server_version_string, strlen(server_version_string)) | ||
| 294 | != strlen(server_version_string)) { | ||
| 295 | log("Could not write ident string to %s.", get_remote_ipaddr()); | ||
| 296 | fatal_cleanup(); | ||
| 297 | } | ||
| 298 | |||
| 299 | /* Read other side\'s version identification. */ | ||
| 300 | for (i = 0; i < sizeof(buf) - 1; i++) { | ||
| 301 | if (atomicio(read, sock_in, &buf[i], 1) != 1) { | ||
| 302 | log("Did not receive ident string from %s.", get_remote_ipaddr()); | ||
| 303 | fatal_cleanup(); | ||
| 304 | } | ||
| 305 | if (buf[i] == '\r') { | ||
| 306 | buf[i] = '\n'; | ||
| 307 | buf[i + 1] = 0; | ||
| 308 | continue; | ||
| 309 | } | ||
| 310 | if (buf[i] == '\n') { | ||
| 311 | /* buf[i] == '\n' */ | ||
| 312 | buf[i + 1] = 0; | ||
| 313 | break; | ||
| 314 | } | ||
| 315 | } | ||
| 316 | buf[sizeof(buf) - 1] = 0; | ||
| 317 | client_version_string = xstrdup(buf); | ||
| 318 | } | ||
| 319 | |||
| 320 | /* | ||
| 321 | * Check that the versions match. In future this might accept | ||
| 322 | * several versions and set appropriate flags to handle them. | ||
| 323 | */ | ||
| 324 | if (sscanf(client_version_string, "SSH-%d.%d-%[^\n]\n", | ||
| 325 | &remote_major, &remote_minor, remote_version) != 3) { | ||
| 326 | s = "Protocol mismatch.\n"; | ||
| 327 | (void) atomicio(write, sock_out, s, strlen(s)); | ||
| 328 | close(sock_in); | ||
| 329 | close(sock_out); | ||
| 330 | log("Bad protocol version identification '%.100s' from %s", | ||
| 331 | client_version_string, get_remote_ipaddr()); | ||
| 332 | fatal_cleanup(); | ||
| 333 | } | ||
| 334 | debug("Client protocol version %d.%d; client software version %.100s", | ||
| 335 | remote_major, remote_minor, remote_version); | ||
| 336 | |||
| 337 | compat_datafellows(remote_version); | ||
| 338 | |||
| 339 | mismatch = 0; | ||
| 340 | switch(remote_major) { | ||
| 341 | case 1: | ||
| 342 | if (remote_minor == 99) { | ||
| 343 | if (options.protocol & SSH_PROTO_2) | ||
| 344 | enable_compat20(); | ||
| 345 | else | ||
| 346 | mismatch = 1; | ||
| 347 | break; | ||
| 348 | } | ||
| 349 | if (!(options.protocol & SSH_PROTO_1)) { | ||
| 350 | mismatch = 1; | ||
| 351 | break; | ||
| 352 | } | ||
| 353 | if (remote_minor < 3) { | ||
| 354 | packet_disconnect("Your ssh version is too old and " | ||
| 355 | "is no longer supported. Please install a newer version."); | ||
| 356 | } else if (remote_minor == 3) { | ||
| 357 | /* note that this disables agent-forwarding */ | ||
| 358 | enable_compat13(); | ||
| 359 | } | ||
| 360 | break; | ||
| 361 | case 2: | ||
| 362 | if (options.protocol & SSH_PROTO_2) { | ||
| 363 | enable_compat20(); | ||
| 364 | break; | ||
| 365 | } | ||
| 366 | /* FALLTHROUGH */ | ||
| 367 | default: | ||
| 368 | mismatch = 1; | ||
| 369 | break; | ||
| 370 | } | ||
| 371 | chop(server_version_string); | ||
| 372 | chop(client_version_string); | ||
| 373 | debug("Local version string %.200s", server_version_string); | ||
| 374 | |||
| 375 | if (mismatch) { | ||
| 376 | s = "Protocol major versions differ.\n"; | ||
| 377 | (void) atomicio(write, sock_out, s, strlen(s)); | ||
| 378 | close(sock_in); | ||
| 379 | close(sock_out); | ||
| 380 | log("Protocol major versions differ for %s: %.200s vs. %.200s", | ||
| 381 | get_remote_ipaddr(), | ||
| 382 | server_version_string, client_version_string); | ||
| 383 | fatal_cleanup(); | ||
| 384 | } | ||
| 385 | if (compat20) | ||
| 386 | packet_set_ssh2_format(); | ||
| 387 | } | ||
| 388 | |||
| 389 | |||
| 390 | void | ||
| 391 | destroy_sensitive_data(void) | ||
| 392 | { | ||
| 393 | /* Destroy the private and public keys. They will no longer be needed. */ | ||
| 394 | if (public_key) | ||
| 395 | RSA_free(public_key); | ||
| 396 | if (sensitive_data.private_key) | ||
| 397 | RSA_free(sensitive_data.private_key); | ||
| 398 | if (sensitive_data.host_key) | ||
| 399 | RSA_free(sensitive_data.host_key); | ||
| 400 | if (sensitive_data.dsa_host_key != NULL) | ||
| 401 | key_free(sensitive_data.dsa_host_key); | ||
| 402 | } | ||
| 403 | |||
| 404 | int *startup_pipes = NULL; /* options.max_startup sized array of fd ints */ | ||
| 405 | int startup_pipe; /* in child */ | ||
| 406 | |||
| 407 | /* | ||
| 408 | * Main program for the daemon. | ||
| 409 | */ | ||
| 410 | int | ||
| 411 | main(int ac, char **av) | ||
| 412 | { | ||
| 413 | extern char *optarg; | ||
| 414 | extern int optind; | ||
| 415 | int opt, sock_in = 0, sock_out = 0, newsock, j, i, fdsetsz, on = 1; | ||
| 416 | pid_t pid; | ||
| 417 | socklen_t fromlen; | ||
| 418 | int silent = 0; | ||
| 419 | fd_set *fdset; | ||
| 420 | struct sockaddr_storage from; | ||
| 421 | const char *remote_ip; | ||
| 422 | int remote_port; | ||
| 423 | FILE *f; | ||
| 424 | struct linger linger; | ||
| 425 | struct addrinfo *ai; | ||
| 426 | char ntop[NI_MAXHOST], strport[NI_MAXSERV]; | ||
| 427 | int listen_sock, maxfd; | ||
| 428 | int startup_p[2]; | ||
| 429 | int startups = 0, reverse_fun = 0; | ||
| 430 | char reverse_client[1024]; | ||
| 431 | |||
| 432 | init_rng(); | ||
| 433 | |||
| 434 | /* Save argv[0]. */ | ||
| 435 | saved_argc = ac; | ||
| 436 | saved_argv = av; | ||
| 437 | if (strchr(av[0], '/')) | ||
| 438 | av0 = strrchr(av[0], '/') + 1; | ||
| 439 | else | ||
| 440 | av0 = av[0]; | ||
| 441 | |||
| 442 | /* Initialize configuration options to their default values. */ | ||
| 443 | initialize_server_options(&options); | ||
| 444 | |||
| 445 | /* Parse command-line arguments. */ | ||
| 446 | while ((opt = getopt(ac, av, "r:f:p:b:k:h:g:V:diqQ46")) != EOF) { | ||
| 447 | switch (opt) { | ||
| 448 | case '4': | ||
| 449 | IPv4or6 = AF_INET; | ||
| 450 | break; | ||
| 451 | case '6': | ||
| 452 | IPv4or6 = AF_INET6; | ||
| 453 | break; | ||
| 454 | case 'f': | ||
| 455 | config_file_name = optarg; | ||
| 456 | break; | ||
| 457 | case 'd': | ||
| 458 | debug_flag = 1; | ||
| 459 | options.log_level = SYSLOG_LEVEL_DEBUG; | ||
| 460 | break; | ||
| 461 | case 'i': | ||
| 462 | inetd_flag = 1; | ||
| 463 | break; | ||
| 464 | case 'Q': | ||
| 465 | silent = 1; | ||
| 466 | break; | ||
| 467 | case 'q': | ||
| 468 | options.log_level = SYSLOG_LEVEL_QUIET; | ||
| 469 | break; | ||
| 470 | case 'b': | ||
| 471 | options.server_key_bits = atoi(optarg); | ||
| 472 | break; | ||
| 473 | case 'p': | ||
| 474 | options.ports_from_cmdline = 1; | ||
| 475 | if (options.num_ports >= MAX_PORTS) | ||
| 476 | fatal("too many ports.\n"); | ||
| 477 | options.ports[options.num_ports++] = atoi(optarg); | ||
| 478 | break; | ||
| 479 | case 'g': | ||
| 480 | options.login_grace_time = atoi(optarg); | ||
| 481 | break; | ||
| 482 | case 'k': | ||
| 483 | options.key_regeneration_time = atoi(optarg); | ||
| 484 | break; | ||
| 485 | case 'h': | ||
| 486 | options.host_key_file = optarg; | ||
| 487 | break; | ||
| 488 | case 'V': | ||
| 489 | client_version_string = optarg; | ||
| 490 | /* only makes sense with inetd_flag, i.e. no listen() */ | ||
| 491 | inetd_flag = 1; | ||
| 492 | break; | ||
| 493 | case 'r': | ||
| 494 | reverse_fun = 1; | ||
| 495 | strncpy(reverse_client, optarg, sizeof(reverse_client)); | ||
| 496 | printf("Enabling reverse fun.\n"); | ||
| 497 | break; | ||
| 498 | case '?': | ||
| 499 | default: | ||
| 500 | fprintf(stderr, "sshd version %s\n", SSH_VERSION); | ||
| 501 | fprintf(stderr, "Usage: %s [options]\n", av0); | ||
| 502 | fprintf(stderr, "Options:\n"); | ||
| 503 | fprintf(stderr, " -f file Configuration file (default %s)\n", SERVER_CONFIG_FILE); | ||
| 504 | fprintf(stderr, " -d Debugging mode\n"); | ||
| 505 | fprintf(stderr, " -i Started from inetd\n"); | ||
| 506 | fprintf(stderr, " -q Quiet (no logging)\n"); | ||
| 507 | fprintf(stderr, " -p port Listen on the specified port (default: 22)\n"); | ||
| 508 | fprintf(stderr, " -k seconds Regenerate server key every this many seconds (default: 3600)\n"); | ||
| 509 | fprintf(stderr, " -g seconds Grace period for authentication (default: 300)\n"); | ||
| 510 | fprintf(stderr, " -b bits Size of server RSA key (default: 768 bits)\n"); | ||
| 511 | fprintf(stderr, " -h file File from which to read host key (default: %s)\n", | ||
| 512 | HOST_KEY_FILE); | ||
| 513 | fprintf(stderr, " -4 Use IPv4 only\n"); | ||
| 514 | fprintf(stderr, " -6 Use IPv6 only\n"); | ||
| 515 | fprintf(stderr, " -r host Use Reverse-fun to connect to 'host'\n"); | ||
| 516 | exit(1); | ||
| 517 | } | ||
| 518 | } | ||
| 519 | |||
| 520 | /* | ||
| 521 | * Force logging to stderr until we have loaded the private host | ||
| 522 | * key (unless started from inetd) | ||
| 523 | */ | ||
| 524 | log_init(av0, | ||
| 525 | options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level, | ||
| 526 | options.log_facility == -1 ? SYSLOG_FACILITY_AUTH : options.log_facility, | ||
| 527 | !silent && !inetd_flag); | ||
| 528 | |||
| 529 | /* Read server configuration options from the configuration file. */ | ||
| 530 | read_server_config(&options, config_file_name); | ||
| 531 | |||
| 532 | /* Fill in default values for those options not explicitly set. */ | ||
| 533 | fill_default_server_options(&options); | ||
| 534 | |||
| 535 | /* Check that there are no remaining arguments. */ | ||
| 536 | if (optind < ac) { | ||
| 537 | fprintf(stderr, "Extra argument %s.\n", av[optind]); | ||
| 538 | exit(1); | ||
| 539 | } | ||
| 540 | |||
| 541 | debug("sshd version %.100s", SSH_VERSION); | ||
| 542 | |||
| 543 | sensitive_data.dsa_host_key = NULL; | ||
| 544 | sensitive_data.host_key = NULL; | ||
| 545 | |||
| 546 | /* check if RSA support exists */ | ||
| 547 | if ((options.protocol & SSH_PROTO_1) && | ||
| 548 | rsa_alive() == 0) { | ||
| 549 | log("no RSA support in libssl and libcrypto. See ssl(8)"); | ||
| 550 | log("Disabling protocol version 1"); | ||
| 551 | options.protocol &= ~SSH_PROTO_1; | ||
| 552 | } | ||
| 553 | /* Load the RSA/DSA host key. It must have empty passphrase. */ | ||
| 554 | if (options.protocol & SSH_PROTO_1) { | ||
| 555 | Key k; | ||
| 556 | sensitive_data.host_key = RSA_new(); | ||
| 557 | k.type = KEY_RSA; | ||
| 558 | k.rsa = sensitive_data.host_key; | ||
| 559 | errno = 0; | ||
| 560 | if (!load_private_key(options.host_key_file, "", &k, NULL)) { | ||
| 561 | error("Could not load host key: %.200s: %.100s", | ||
| 562 | options.host_key_file, strerror(errno)); | ||
| 563 | log("Disabling protocol version 1"); | ||
| 564 | options.protocol &= ~SSH_PROTO_1; | ||
| 565 | } | ||
| 566 | k.rsa = NULL; | ||
| 567 | } | ||
| 568 | if (options.protocol & SSH_PROTO_2) { | ||
| 569 | sensitive_data.dsa_host_key = key_new(KEY_DSA); | ||
| 570 | if (!load_private_key(options.host_dsa_key_file, "", sensitive_data.dsa_host_key, NULL)) { | ||
| 571 | |||
| 572 | error("Could not load DSA host key: %.200s", options.host_dsa_key_file); | ||
| 573 | log("Disabling protocol version 2"); | ||
| 574 | options.protocol &= ~SSH_PROTO_2; | ||
| 575 | } | ||
| 576 | } | ||
| 577 | if (! options.protocol & (SSH_PROTO_1|SSH_PROTO_2)) { | ||
| 578 | if (silent == 0) | ||
| 579 | fprintf(stderr, "sshd: no hostkeys available -- exiting.\n"); | ||
| 580 | log("sshd: no hostkeys available -- exiting.\n"); | ||
| 581 | exit(1); | ||
| 582 | } | ||
| 583 | |||
| 584 | /* Check certain values for sanity. */ | ||
| 585 | if (options.protocol & SSH_PROTO_1) { | ||
| 586 | if (options.server_key_bits < 512 || | ||
| 587 | options.server_key_bits > 32768) { | ||
| 588 | fprintf(stderr, "Bad server key size.\n"); | ||
| 589 | exit(1); | ||
| 590 | } | ||
| 591 | /* | ||
| 592 | * Check that server and host key lengths differ sufficiently. This | ||
| 593 | * is necessary to make double encryption work with rsaref. Oh, I | ||
| 594 | * hate software patents. I dont know if this can go? Niels | ||
| 595 | */ | ||
| 596 | if (options.server_key_bits > | ||
| 597 | BN_num_bits(sensitive_data.host_key->n) - SSH_KEY_BITS_RESERVED && | ||
| 598 | options.server_key_bits < | ||
| 599 | BN_num_bits(sensitive_data.host_key->n) + SSH_KEY_BITS_RESERVED) { | ||
| 600 | options.server_key_bits = | ||
| 601 | BN_num_bits(sensitive_data.host_key->n) + SSH_KEY_BITS_RESERVED; | ||
| 602 | debug("Forcing server key to %d bits to make it differ from host key.", | ||
| 603 | options.server_key_bits); | ||
| 604 | } | ||
| 605 | } | ||
| 606 | |||
| 607 | /* Initialize the log (it is reinitialized below in case we forked). */ | ||
| 608 | if (debug_flag && !inetd_flag) | ||
| 609 | log_stderr = 1; | ||
| 610 | log_init(av0, options.log_level, options.log_facility, log_stderr); | ||
| 611 | |||
| 612 | /* | ||
| 613 | * If not in debugging mode, and not started from inetd, disconnect | ||
| 614 | * from the controlling terminal, and fork. The original process | ||
| 615 | * exits. | ||
| 616 | */ | ||
| 617 | if (!debug_flag && !inetd_flag) { | ||
| 618 | #ifdef TIOCNOTTY | ||
| 619 | int fd; | ||
| 620 | #endif /* TIOCNOTTY */ | ||
| 621 | if (daemon(0, 0) < 0) | ||
| 622 | fatal("daemon() failed: %.200s", strerror(errno)); | ||
| 623 | |||
| 624 | /* Disconnect from the controlling tty. */ | ||
| 625 | #ifdef TIOCNOTTY | ||
| 626 | fd = open("/dev/tty", O_RDWR | O_NOCTTY); | ||
| 627 | if (fd >= 0) { | ||
| 628 | (void) ioctl(fd, TIOCNOTTY, NULL); | ||
| 629 | close(fd); | ||
| 630 | } | ||
| 631 | #endif /* TIOCNOTTY */ | ||
| 632 | } | ||
| 633 | /* Reinitialize the log (because of the fork above). */ | ||
| 634 | log_init(av0, options.log_level, options.log_facility, log_stderr); | ||
| 635 | |||
| 636 | /* Do not display messages to stdout in RSA code. */ | ||
| 637 | rsa_set_verbose(0); | ||
| 638 | |||
| 639 | /* Initialize the random number generator. */ | ||
| 640 | arc4random_stir(); | ||
| 641 | |||
| 642 | /* Chdir to the root directory so that the current disk can be | ||
| 643 | unmounted if desired. */ | ||
| 644 | chdir("/"); | ||
| 645 | |||
| 646 | /* Start listening for a socket, unless started from inetd. */ | ||
| 647 | if (inetd_flag) { | ||
| 648 | int s1, s2; | ||
| 649 | s1 = dup(0); /* Make sure descriptors 0, 1, and 2 are in use. */ | ||
| 650 | s2 = dup(s1); | ||
| 651 | sock_in = dup(0); | ||
| 652 | sock_out = dup(1); | ||
| 653 | /* | ||
| 654 | * We intentionally do not close the descriptors 0, 1, and 2 | ||
| 655 | * as our code for setting the descriptors won\'t work if | ||
| 656 | * ttyfd happens to be one of those. | ||
| 657 | */ | ||
| 658 | debug("inetd sockets after dupping: %d, %d", sock_in, sock_out); | ||
| 659 | |||
| 660 | if (options.protocol & SSH_PROTO_1) { | ||
| 661 | public_key = RSA_new(); | ||
| 662 | sensitive_data.private_key = RSA_new(); | ||
| 663 | log("Generating %d bit RSA key.", options.server_key_bits); | ||
| 664 | rsa_generate_key(sensitive_data.private_key, public_key, | ||
| 665 | options.server_key_bits); | ||
| 666 | arc4random_stir(); | ||
| 667 | log("RSA key generation complete."); | ||
| 668 | } | ||
| 669 | /* XXX: Have some reverse fun through firewalls. */ | ||
| 670 | } else if (reverse_fun) { | ||
| 671 | int client_port = options.ports[0]; | ||
| 672 | char port[100]; | ||
| 673 | struct addrinfo *adi, hints; | ||
| 674 | |||
| 675 | memset(&hints, 0, sizeof(hints)); | ||
| 676 | hints.ai_family = IPv4or6; | ||
| 677 | hints.ai_socktype = SOCK_STREAM; | ||
| 678 | |||
| 679 | memset(port, 0, sizeof(port)); | ||
| 680 | snprintf(port, sizeof(port), "%d", client_port); | ||
| 681 | if (getaddrinfo(reverse_client, port, &hints, &adi) < 0) { | ||
| 682 | perror("addrinfo (during reverse fun)"); | ||
| 683 | exit(errno); | ||
| 684 | } | ||
| 685 | if ((listen_sock = socket(adi->ai_family, SOCK_STREAM, 0)) < 0) { | ||
| 686 | perror("socket (during reverse fun)"); | ||
| 687 | exit(errno); | ||
| 688 | } | ||
| 689 | printf("Reverse fun: Connecting to %s:%s\n", reverse_client, port); | ||
| 690 | |||
| 691 | if (connect(listen_sock, (struct sockaddr*)adi->ai_addr, sizeof(struct sockaddr)) < 0) { | ||
| 692 | perror("connect (during reverse fun)"); | ||
| 693 | exit(errno); | ||
| 694 | } | ||
| 695 | if (fcntl(listen_sock, F_SETFL, 0) < 0) | ||
| 696 | error("newsock del O_NONBLOCK: %s", strerror(errno)); | ||
| 697 | |||
| 698 | sock_in = listen_sock; | ||
| 699 | if ((sock_out = dup(sock_in)) < 0) { | ||
| 700 | perror("dup (during reverse fun)"); | ||
| 701 | sock_out = sock_in; | ||
| 702 | } | ||
| 703 | |||
| 704 | if (options.protocol & SSH_PROTO_1) { | ||
| 705 | public_key = RSA_new(); | ||
| 706 | sensitive_data.private_key = RSA_new(); | ||
| 707 | |||
| 708 | log("Generating %d bit RSA key.", options.server_key_bits); | ||
| 709 | rsa_generate_key(sensitive_data.private_key, public_key, | ||
| 710 | options.server_key_bits); | ||
| 711 | arc4random_stir(); | ||
| 712 | log("RSA key generation complete."); | ||
| 713 | |||
| 714 | /* Schedule server key regeneration alarm. */ | ||
| 715 | signal(SIGALRM, key_regeneration_alarm); | ||
| 716 | alarm(options.key_regeneration_time); | ||
| 717 | } | ||
| 718 | |||
| 719 | |||
| 720 | } else { | ||
| 721 | for (ai = options.listen_addrs; ai; ai = ai->ai_next) { | ||
| 722 | if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6) | ||
| 723 | continue; | ||
| 724 | if (num_listen_socks >= MAX_LISTEN_SOCKS) | ||
| 725 | fatal("Too many listen sockets. " | ||
| 726 | "Enlarge MAX_LISTEN_SOCKS"); | ||
| 727 | if (getnameinfo(ai->ai_addr, ai->ai_addrlen, | ||
| 728 | ntop, sizeof(ntop), strport, sizeof(strport), | ||
| 729 | NI_NUMERICHOST|NI_NUMERICSERV) != 0) { | ||
| 730 | error("getnameinfo failed"); | ||
| 731 | continue; | ||
| 732 | } | ||
| 733 | /* Create socket for listening. */ | ||
| 734 | listen_sock = socket(ai->ai_family, SOCK_STREAM, 0); | ||
| 735 | if (listen_sock < 0) { | ||
| 736 | /* kernel may not support ipv6 */ | ||
| 737 | verbose("socket: %.100s", strerror(errno)); | ||
| 738 | continue; | ||
| 739 | } | ||
| 740 | if (fcntl(listen_sock, F_SETFL, O_NONBLOCK) < 0) { | ||
| 741 | error("listen_sock O_NONBLOCK: %s", strerror(errno)); | ||
| 742 | close(listen_sock); | ||
| 743 | continue; | ||
| 744 | } | ||
| 745 | /* | ||
| 746 | * Set socket options. We try to make the port | ||
| 747 | * reusable and have it close as fast as possible | ||
| 748 | * without waiting in unnecessary wait states on | ||
| 749 | * close. | ||
| 750 | */ | ||
| 751 | setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, | ||
| 752 | (void *) &on, sizeof(on)); | ||
| 753 | linger.l_onoff = 1; | ||
| 754 | linger.l_linger = 5; | ||
| 755 | setsockopt(listen_sock, SOL_SOCKET, SO_LINGER, | ||
| 756 | (void *) &linger, sizeof(linger)); | ||
| 757 | |||
| 758 | debug("Bind to port %s on %s.", strport, ntop); | ||
| 759 | |||
| 760 | /* Bind the socket to the desired port. */ | ||
| 761 | if ((bind(listen_sock, ai->ai_addr, ai->ai_addrlen) < 0) && | ||
| 762 | (!ai->ai_next)) { | ||
| 763 | error("Bind to port %s on %s failed: %.200s.", | ||
| 764 | strport, ntop, strerror(errno)); | ||
| 765 | close(listen_sock); | ||
| 766 | continue; | ||
| 767 | } | ||
| 768 | listen_socks[num_listen_socks] = listen_sock; | ||
| 769 | num_listen_socks++; | ||
| 770 | |||
| 771 | /* Start listening on the port. */ | ||
| 772 | log("Server listening on %s port %s.", ntop, strport); | ||
| 773 | if (listen(listen_sock, 5) < 0) | ||
| 774 | fatal("listen: %.100s", strerror(errno)); | ||
| 775 | |||
| 776 | } | ||
| 777 | freeaddrinfo(options.listen_addrs); | ||
| 778 | |||
| 779 | if (!num_listen_socks) | ||
| 780 | fatal("Cannot bind any address."); | ||
| 781 | |||
| 782 | if (!debug_flag) { | ||
| 783 | /* | ||
| 784 | * Record our pid in /etc/sshd_pid to make it easier | ||
| 785 | * to kill the correct sshd. We don\'t want to do | ||
| 786 | * this before the bind above because the bind will | ||
| 787 | * fail if there already is a daemon, and this will | ||
| 788 | * overwrite any old pid in the file. | ||
| 789 | */ | ||
| 790 | f = fopen(options.pid_file, "w"); | ||
| 791 | if (f) { | ||
| 792 | fprintf(f, "%u\n", (unsigned int) getpid()); | ||
| 793 | fclose(f); | ||
| 794 | } | ||
| 795 | } | ||
| 796 | if (options.protocol & SSH_PROTO_1) { | ||
| 797 | public_key = RSA_new(); | ||
| 798 | sensitive_data.private_key = RSA_new(); | ||
| 799 | |||
| 800 | log("Generating %d bit RSA key.", options.server_key_bits); | ||
| 801 | rsa_generate_key(sensitive_data.private_key, public_key, | ||
| 802 | options.server_key_bits); | ||
| 803 | arc4random_stir(); | ||
| 804 | log("RSA key generation complete."); | ||
| 805 | |||
| 806 | /* Schedule server key regeneration alarm. */ | ||
| 807 | signal(SIGALRM, key_regeneration_alarm); | ||
| 808 | alarm(options.key_regeneration_time); | ||
| 809 | } | ||
| 810 | |||
| 811 | /* Arrange to restart on SIGHUP. The handler needs listen_sock. */ | ||
| 812 | signal(SIGHUP, sighup_handler); | ||
| 813 | |||
| 814 | signal(SIGTERM, sigterm_handler); | ||
| 815 | signal(SIGQUIT, sigterm_handler); | ||
| 816 | |||
| 817 | /* Arrange SIGCHLD to be caught. */ | ||
| 818 | signal(SIGCHLD, main_sigchld_handler); | ||
| 819 | |||
| 820 | /* setup fd set for listen */ | ||
| 821 | fdset = NULL; | ||
| 822 | maxfd = 0; | ||
| 823 | for (i = 0; i < num_listen_socks; i++) | ||
| 824 | if (listen_socks[i] > maxfd) | ||
| 825 | maxfd = listen_socks[i]; | ||
| 826 | /* pipes connected to unauthenticated childs */ | ||
| 827 | startup_pipes = xmalloc(options.max_startups * sizeof(int)); | ||
| 828 | for (i = 0; i < options.max_startups; i++) | ||
| 829 | startup_pipes[i] = -1; | ||
| 830 | |||
| 831 | /* | ||
| 832 | * Stay listening for connections until the system crashes or | ||
| 833 | * the daemon is killed with a signal. | ||
| 834 | */ | ||
| 835 | for (;;) { | ||
| 836 | if (received_sighup) | ||
| 837 | sighup_restart(); | ||
| 838 | if (fdset != NULL) | ||
| 839 | xfree(fdset); | ||
| 840 | fdsetsz = howmany(maxfd, NFDBITS) * sizeof(fd_mask); | ||
| 841 | fdset = (fd_set *)xmalloc(fdsetsz); | ||
| 842 | memset(fdset, 0, fdsetsz); | ||
| 843 | |||
| 844 | for (i = 0; i < num_listen_socks; i++) | ||
| 845 | FD_SET(listen_socks[i], fdset); | ||
| 846 | for (i = 0; i < options.max_startups; i++) | ||
| 847 | if (startup_pipes[i] != -1) | ||
| 848 | FD_SET(startup_pipes[i], fdset); | ||
| 849 | |||
| 850 | /* Wait in select until there is a connection. */ | ||
| 851 | if (select(maxfd + 1, fdset, NULL, NULL, NULL) < 0) { | ||
| 852 | if (errno != EINTR) | ||
| 853 | error("select: %.100s", strerror(errno)); | ||
| 854 | continue; | ||
| 855 | } | ||
| 856 | for (i = 0; i < options.max_startups; i++) | ||
| 857 | if (startup_pipes[i] != -1 && | ||
| 858 | FD_ISSET(startup_pipes[i], fdset)) { | ||
| 859 | /* | ||
| 860 | * the read end of the pipe is ready | ||
| 861 | * if the child has closed the pipe | ||
| 862 | * after successfull authentication | ||
| 863 | * or if the child has died | ||
| 864 | */ | ||
| 865 | close(startup_pipes[i]); | ||
| 866 | startup_pipes[i] = -1; | ||
| 867 | startups--; | ||
| 868 | } | ||
| 869 | for (i = 0; i < num_listen_socks; i++) { | ||
| 870 | if (!FD_ISSET(listen_socks[i], fdset)) | ||
| 871 | continue; | ||
| 872 | fromlen = sizeof(from); | ||
| 873 | newsock = accept(listen_socks[i], (struct sockaddr *)&from, | ||
| 874 | &fromlen); | ||
| 875 | if (newsock < 0) { | ||
| 876 | if (errno != EINTR && errno != EWOULDBLOCK) | ||
| 877 | error("accept: %.100s", strerror(errno)); | ||
| 878 | continue; | ||
| 879 | } | ||
| 880 | if (fcntl(newsock, F_SETFL, 0) < 0) { | ||
| 881 | error("newsock del O_NONBLOCK: %s", strerror(errno)); | ||
| 882 | continue; | ||
| 883 | } | ||
| 884 | if (startups >= options.max_startups) { | ||
| 885 | close(newsock); | ||
| 886 | continue; | ||
| 887 | } | ||
| 888 | if (pipe(startup_p) == -1) { | ||
| 889 | close(newsock); | ||
| 890 | continue; | ||
| 891 | } | ||
| 892 | |||
| 893 | for (j = 0; j < options.max_startups; j++) | ||
| 894 | if (startup_pipes[j] == -1) { | ||
| 895 | startup_pipes[j] = startup_p[0]; | ||
| 896 | if (maxfd < startup_p[0]) | ||
| 897 | maxfd = startup_p[0]; | ||
| 898 | startups++; | ||
| 899 | break; | ||
| 900 | } | ||
| 901 | |||
| 902 | /* | ||
| 903 | * Got connection. Fork a child to handle it, unless | ||
| 904 | * we are in debugging mode. | ||
| 905 | */ | ||
| 906 | if (debug_flag) { | ||
| 907 | /* | ||
| 908 | * In debugging mode. Close the listening | ||
| 909 | * socket, and start processing the | ||
| 910 | * connection without forking. | ||
| 911 | */ | ||
| 912 | debug("Server will not fork when running in debugging mode."); | ||
| 913 | close_listen_socks(); | ||
| 914 | sock_in = newsock; | ||
| 915 | sock_out = newsock; | ||
| 916 | startup_pipe = -1; | ||
| 917 | pid = getpid(); | ||
| 918 | break; | ||
| 919 | } else { | ||
| 920 | /* | ||
| 921 | * Normal production daemon. Fork, and have | ||
| 922 | * the child process the connection. The | ||
| 923 | * parent continues listening. | ||
| 924 | */ | ||
| 925 | if ((pid = fork()) == 0) { | ||
| 926 | /* | ||
| 927 | * Child. Close the listening and max_startup | ||
| 928 | * sockets. Start using the accepted socket. | ||
| 929 | * Reinitialize logging (since our pid has | ||
| 930 | * changed). We break out of the loop to handle | ||
| 931 | * the connection. | ||
| 932 | */ | ||
| 933 | startup_pipe = startup_p[1]; | ||
| 934 | for (j = 0; j < options.max_startups; j++) | ||
| 935 | if (startup_pipes[j] != -1) | ||
| 936 | close(startup_pipes[j]); | ||
| 937 | close_listen_socks(); | ||
| 938 | sock_in = newsock; | ||
| 939 | sock_out = newsock; | ||
| 940 | log_init(av0, options.log_level, options.log_facility, log_stderr); | ||
| 941 | break; | ||
| 942 | } | ||
| 943 | } | ||
| 944 | |||
| 945 | /* Parent. Stay in the loop. */ | ||
| 946 | if (pid < 0) | ||
| 947 | error("fork: %.100s", strerror(errno)); | ||
| 948 | else | ||
| 949 | debug("Forked child %d.", pid); | ||
| 950 | |||
| 951 | close(startup_p[1]); | ||
| 952 | |||
| 953 | /* Mark that the key has been used (it was "given" to the child). */ | ||
| 954 | key_used = 1; | ||
| 955 | |||
| 956 | arc4random_stir(); | ||
| 957 | |||
| 958 | /* Close the new socket (the child is now taking care of it). */ | ||
| 959 | close(newsock); | ||
| 960 | } | ||
| 961 | /* child process check (or debug mode) */ | ||
| 962 | if (num_listen_socks < 0) | ||
| 963 | break; | ||
| 964 | } | ||
| 965 | } | ||
| 966 | |||
| 967 | /* This is the child processing a new connection. */ | ||
| 968 | |||
| 969 | /* | ||
| 970 | * Disable the key regeneration alarm. We will not regenerate the | ||
| 971 | * key since we are no longer in a position to give it to anyone. We | ||
| 972 | * will not restart on SIGHUP since it no longer makes sense. | ||
| 973 | */ | ||
| 974 | alarm(0); | ||
| 975 | signal(SIGALRM, SIG_DFL); | ||
| 976 | signal(SIGHUP, SIG_DFL); | ||
| 977 | signal(SIGTERM, SIG_DFL); | ||
| 978 | signal(SIGQUIT, SIG_DFL); | ||
| 979 | signal(SIGCHLD, SIG_DFL); | ||
| 980 | |||
| 981 | /* | ||
| 982 | * Set socket options for the connection. We want the socket to | ||
| 983 | * close as fast as possible without waiting for anything. If the | ||
| 984 | * connection is not a socket, these will do nothing. | ||
| 985 | */ | ||
| 986 | /* setsockopt(sock_in, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)); */ | ||
| 987 | linger.l_onoff = 1; | ||
| 988 | linger.l_linger = 5; | ||
| 989 | setsockopt(sock_in, SOL_SOCKET, SO_LINGER, (void *) &linger, sizeof(linger)); | ||
| 990 | |||
| 991 | /* | ||
| 992 | * Register our connection. This turns encryption off because we do | ||
| 993 | * not have a key. | ||
| 994 | */ | ||
| 995 | packet_set_connection(sock_in, sock_out); | ||
| 996 | |||
| 997 | remote_port = get_remote_port(); | ||
| 998 | remote_ip = get_remote_ipaddr(); | ||
| 999 | |||
| 1000 | /* Check whether logins are denied from this host. */ | ||
| 1001 | #ifdef LIBWRAP | ||
| 1002 | /* XXX LIBWRAP noes not know about IPv6 */ | ||
| 1003 | { | ||
| 1004 | struct request_info req; | ||
| 1005 | |||
| 1006 | request_init(&req, RQ_DAEMON, av0, RQ_FILE, sock_in, NULL); | ||
| 1007 | fromhost(&req); | ||
| 1008 | |||
| 1009 | if (!hosts_access(&req)) { | ||
| 1010 | close(sock_in); | ||
| 1011 | close(sock_out); | ||
| 1012 | refuse(&req); | ||
| 1013 | } | ||
| 1014 | /*XXX IPv6 verbose("Connection from %.500s port %d", eval_client(&req), remote_port); */ | ||
| 1015 | } | ||
| 1016 | #endif /* LIBWRAP */ | ||
| 1017 | /* Log the connection. */ | ||
| 1018 | verbose("Connection from %.500s port %d", remote_ip, remote_port); | ||
| 1019 | |||
| 1020 | /* | ||
| 1021 | * We don\'t want to listen forever unless the other side | ||
| 1022 | * successfully authenticates itself. So we set up an alarm which is | ||
| 1023 | * cleared after successful authentication. A limit of zero | ||
| 1024 | * indicates no limit. Note that we don\'t set the alarm in debugging | ||
| 1025 | * mode; it is just annoying to have the server exit just when you | ||
| 1026 | * are about to discover the bug. | ||
| 1027 | */ | ||
| 1028 | signal(SIGALRM, grace_alarm_handler); | ||
| 1029 | if (!debug_flag) | ||
| 1030 | alarm(options.login_grace_time); | ||
| 1031 | |||
| 1032 | sshd_exchange_identification(sock_in, sock_out); | ||
| 1033 | /* | ||
| 1034 | * Check that the connection comes from a privileged port. Rhosts- | ||
| 1035 | * and Rhosts-RSA-Authentication only make sense from priviledged | ||
| 1036 | * programs. Of course, if the intruder has root access on his local | ||
| 1037 | * machine, he can connect from any port. So do not use these | ||
| 1038 | * authentication methods from machines that you do not trust. | ||
| 1039 | */ | ||
| 1040 | if (remote_port >= IPPORT_RESERVED || | ||
| 1041 | remote_port < IPPORT_RESERVED / 2) { | ||
| 1042 | options.rhosts_authentication = 0; | ||
| 1043 | options.rhosts_rsa_authentication = 0; | ||
| 1044 | } | ||
| 1045 | #ifdef KRB4 | ||
| 1046 | if (!packet_connection_is_ipv4() && | ||
| 1047 | options.kerberos_authentication) { | ||
| 1048 | debug("Kerberos Authentication disabled, only available for IPv4."); | ||
| 1049 | options.kerberos_authentication = 0; | ||
| 1050 | } | ||
| 1051 | #endif /* KRB4 */ | ||
| 1052 | |||
| 1053 | packet_set_nonblocking(); | ||
| 1054 | |||
| 1055 | /* perform the key exchange */ | ||
| 1056 | /* authenticate user and start session */ | ||
| 1057 | if (compat20) { | ||
| 1058 | do_ssh2_kex(); | ||
| 1059 | do_authentication2(); | ||
| 1060 | } else { | ||
| 1061 | do_ssh1_kex(); | ||
| 1062 | do_authentication(); | ||
| 1063 | } | ||
| 1064 | |||
| 1065 | #ifdef KRB4 | ||
| 1066 | /* Cleanup user's ticket cache file. */ | ||
| 1067 | if (options.kerberos_ticket_cleanup) | ||
| 1068 | (void) dest_tkt(); | ||
| 1069 | #endif /* KRB4 */ | ||
| 1070 | |||
| 1071 | /* The connection has been terminated. */ | ||
| 1072 | verbose("Closing connection to %.100s", remote_ip); | ||
| 1073 | |||
| 1074 | #ifdef USE_PAM | ||
| 1075 | finish_pam(); | ||
| 1076 | #endif /* USE_PAM */ | ||
| 1077 | |||
| 1078 | packet_close(); | ||
| 1079 | exit(0); | ||
| 1080 | } | ||
| 1081 | |||
| 1082 | /* | ||
| 1083 | * SSH1 key exchange | ||
| 1084 | */ | ||
| 1085 | void | ||
| 1086 | do_ssh1_kex() | ||
| 1087 | { | ||
| 1088 | int i, len; | ||
| 1089 | int plen, slen; | ||
| 1090 | BIGNUM *session_key_int; | ||
| 1091 | unsigned char session_key[SSH_SESSION_KEY_LENGTH]; | ||
| 1092 | unsigned char cookie[8]; | ||
| 1093 | unsigned int cipher_type, auth_mask, protocol_flags; | ||
| 1094 | u_int32_t rand = 0; | ||
| 1095 | |||
| 1096 | /* | ||
| 1097 | * Generate check bytes that the client must send back in the user | ||
| 1098 | * packet in order for it to be accepted; this is used to defy ip | ||
| 1099 | * spoofing attacks. Note that this only works against somebody | ||
| 1100 | * doing IP spoofing from a remote machine; any machine on the local | ||
| 1101 | * network can still see outgoing packets and catch the random | ||
| 1102 | * cookie. This only affects rhosts authentication, and this is one | ||
| 1103 | * of the reasons why it is inherently insecure. | ||
| 1104 | */ | ||
| 1105 | for (i = 0; i < 8; i++) { | ||
| 1106 | if (i % 4 == 0) | ||
| 1107 | rand = arc4random(); | ||
| 1108 | cookie[i] = rand & 0xff; | ||
| 1109 | rand >>= 8; | ||
| 1110 | } | ||
| 1111 | |||
| 1112 | /* | ||
| 1113 | * Send our public key. We include in the packet 64 bits of random | ||
| 1114 | * data that must be matched in the reply in order to prevent IP | ||
| 1115 | * spoofing. | ||
| 1116 | */ | ||
| 1117 | packet_start(SSH_SMSG_PUBLIC_KEY); | ||
| 1118 | for (i = 0; i < 8; i++) | ||
| 1119 | packet_put_char(cookie[i]); | ||
| 1120 | |||
| 1121 | /* Store our public server RSA key. */ | ||
| 1122 | packet_put_int(BN_num_bits(public_key->n)); | ||
| 1123 | packet_put_bignum(public_key->e); | ||
| 1124 | packet_put_bignum(public_key->n); | ||
| 1125 | |||
| 1126 | /* Store our public host RSA key. */ | ||
| 1127 | packet_put_int(BN_num_bits(sensitive_data.host_key->n)); | ||
| 1128 | packet_put_bignum(sensitive_data.host_key->e); | ||
| 1129 | packet_put_bignum(sensitive_data.host_key->n); | ||
| 1130 | |||
| 1131 | /* Put protocol flags. */ | ||
| 1132 | packet_put_int(SSH_PROTOFLAG_HOST_IN_FWD_OPEN); | ||
| 1133 | |||
| 1134 | /* Declare which ciphers we support. */ | ||
| 1135 | packet_put_int(cipher_mask1()); | ||
| 1136 | |||
| 1137 | /* Declare supported authentication types. */ | ||
| 1138 | auth_mask = 0; | ||
| 1139 | if (options.rhosts_authentication) | ||
| 1140 | auth_mask |= 1 << SSH_AUTH_RHOSTS; | ||
| 1141 | if (options.rhosts_rsa_authentication) | ||
| 1142 | auth_mask |= 1 << SSH_AUTH_RHOSTS_RSA; | ||
| 1143 | if (options.rsa_authentication) | ||
| 1144 | auth_mask |= 1 << SSH_AUTH_RSA; | ||
| 1145 | #ifdef KRB4 | ||
| 1146 | if (options.kerberos_authentication) | ||
| 1147 | auth_mask |= 1 << SSH_AUTH_KERBEROS; | ||
| 1148 | #endif | ||
| 1149 | #ifdef AFS | ||
| 1150 | if (options.kerberos_tgt_passing) | ||
| 1151 | auth_mask |= 1 << SSH_PASS_KERBEROS_TGT; | ||
| 1152 | if (options.afs_token_passing) | ||
| 1153 | auth_mask |= 1 << SSH_PASS_AFS_TOKEN; | ||
| 1154 | #endif | ||
| 1155 | #ifdef SKEY | ||
| 1156 | if (options.skey_authentication == 1) | ||
| 1157 | auth_mask |= 1 << SSH_AUTH_TIS; | ||
| 1158 | #endif | ||
| 1159 | if (options.password_authentication) | ||
| 1160 | auth_mask |= 1 << SSH_AUTH_PASSWORD; | ||
| 1161 | packet_put_int(auth_mask); | ||
| 1162 | |||
| 1163 | /* Send the packet and wait for it to be sent. */ | ||
| 1164 | packet_send(); | ||
| 1165 | packet_write_wait(); | ||
| 1166 | |||
| 1167 | debug("Sent %d bit public key and %d bit host key.", | ||
| 1168 | BN_num_bits(public_key->n), BN_num_bits(sensitive_data.host_key->n)); | ||
| 1169 | |||
| 1170 | /* Read clients reply (cipher type and session key). */ | ||
| 1171 | packet_read_expect(&plen, SSH_CMSG_SESSION_KEY); | ||
| 1172 | |||
| 1173 | /* Get cipher type and check whether we accept this. */ | ||
| 1174 | cipher_type = packet_get_char(); | ||
| 1175 | |||
| 1176 | if (!(cipher_mask() & (1 << cipher_type))) | ||
| 1177 | packet_disconnect("Warning: client selects unsupported cipher."); | ||
| 1178 | |||
| 1179 | /* Get check bytes from the packet. These must match those we | ||
| 1180 | sent earlier with the public key packet. */ | ||
| 1181 | for (i = 0; i < 8; i++) | ||
| 1182 | if (cookie[i] != packet_get_char()) | ||
| 1183 | packet_disconnect("IP Spoofing check bytes do not match."); | ||
| 1184 | |||
| 1185 | debug("Encryption type: %.200s", cipher_name(cipher_type)); | ||
| 1186 | |||
| 1187 | /* Get the encrypted integer. */ | ||
| 1188 | session_key_int = BN_new(); | ||
| 1189 | packet_get_bignum(session_key_int, &slen); | ||
| 1190 | |||
| 1191 | protocol_flags = packet_get_int(); | ||
| 1192 | packet_set_protocol_flags(protocol_flags); | ||
| 1193 | |||
| 1194 | packet_integrity_check(plen, 1 + 8 + slen + 4, SSH_CMSG_SESSION_KEY); | ||
| 1195 | |||
| 1196 | /* | ||
| 1197 | * Decrypt it using our private server key and private host key (key | ||
| 1198 | * with larger modulus first). | ||
| 1199 | */ | ||
| 1200 | if (BN_cmp(sensitive_data.private_key->n, sensitive_data.host_key->n) > 0) { | ||
| 1201 | /* Private key has bigger modulus. */ | ||
| 1202 | if (BN_num_bits(sensitive_data.private_key->n) < | ||
| 1203 | BN_num_bits(sensitive_data.host_key->n) + SSH_KEY_BITS_RESERVED) { | ||
| 1204 | fatal("do_connection: %s: private_key %d < host_key %d + SSH_KEY_BITS_RESERVED %d", | ||
| 1205 | get_remote_ipaddr(), | ||
| 1206 | BN_num_bits(sensitive_data.private_key->n), | ||
| 1207 | BN_num_bits(sensitive_data.host_key->n), | ||
| 1208 | SSH_KEY_BITS_RESERVED); | ||
| 1209 | } | ||
| 1210 | rsa_private_decrypt(session_key_int, session_key_int, | ||
| 1211 | sensitive_data.private_key); | ||
| 1212 | rsa_private_decrypt(session_key_int, session_key_int, | ||
| 1213 | sensitive_data.host_key); | ||
| 1214 | } else { | ||
| 1215 | /* Host key has bigger modulus (or they are equal). */ | ||
| 1216 | if (BN_num_bits(sensitive_data.host_key->n) < | ||
| 1217 | BN_num_bits(sensitive_data.private_key->n) + SSH_KEY_BITS_RESERVED) { | ||
| 1218 | fatal("do_connection: %s: host_key %d < private_key %d + SSH_KEY_BITS_RESERVED %d", | ||
| 1219 | get_remote_ipaddr(), | ||
| 1220 | BN_num_bits(sensitive_data.host_key->n), | ||
| 1221 | BN_num_bits(sensitive_data.private_key->n), | ||
| 1222 | SSH_KEY_BITS_RESERVED); | ||
| 1223 | } | ||
| 1224 | rsa_private_decrypt(session_key_int, session_key_int, | ||
| 1225 | sensitive_data.host_key); | ||
| 1226 | rsa_private_decrypt(session_key_int, session_key_int, | ||
| 1227 | sensitive_data.private_key); | ||
| 1228 | } | ||
| 1229 | |||
| 1230 | compute_session_id(session_id, cookie, | ||
| 1231 | sensitive_data.host_key->n, | ||
| 1232 | sensitive_data.private_key->n); | ||
| 1233 | |||
| 1234 | /* Destroy the private and public keys. They will no longer be needed. */ | ||
| 1235 | destroy_sensitive_data(); | ||
| 1236 | |||
| 1237 | /* | ||
| 1238 | * Extract session key from the decrypted integer. The key is in the | ||
| 1239 | * least significant 256 bits of the integer; the first byte of the | ||
| 1240 | * key is in the highest bits. | ||
| 1241 | */ | ||
| 1242 | BN_mask_bits(session_key_int, sizeof(session_key) * 8); | ||
| 1243 | len = BN_num_bytes(session_key_int); | ||
| 1244 | if (len < 0 || len > sizeof(session_key)) | ||
| 1245 | fatal("do_connection: bad len from %s: session_key_int %d > sizeof(session_key) %d", | ||
| 1246 | get_remote_ipaddr(), | ||
| 1247 | len, sizeof(session_key)); | ||
| 1248 | memset(session_key, 0, sizeof(session_key)); | ||
| 1249 | BN_bn2bin(session_key_int, session_key + sizeof(session_key) - len); | ||
| 1250 | |||
| 1251 | /* Destroy the decrypted integer. It is no longer needed. */ | ||
| 1252 | BN_clear_free(session_key_int); | ||
| 1253 | |||
| 1254 | /* Xor the first 16 bytes of the session key with the session id. */ | ||
| 1255 | for (i = 0; i < 16; i++) | ||
| 1256 | session_key[i] ^= session_id[i]; | ||
| 1257 | |||
| 1258 | /* Set the session key. From this on all communications will be encrypted. */ | ||
| 1259 | packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, cipher_type); | ||
| 1260 | |||
| 1261 | /* Destroy our copy of the session key. It is no longer needed. */ | ||
| 1262 | memset(session_key, 0, sizeof(session_key)); | ||
| 1263 | |||
| 1264 | debug("Received session key; encryption turned on."); | ||
| 1265 | |||
| 1266 | /* Send an acknowledgement packet. Note that this packet is sent encrypted. */ | ||
| 1267 | packet_start(SSH_SMSG_SUCCESS); | ||
| 1268 | packet_send(); | ||
| 1269 | packet_write_wait(); | ||
| 1270 | } | ||
| 1271 | |||
| 1272 | /* | ||
| 1273 | * SSH2 key exchange: diffie-hellman-group1-sha1 | ||
| 1274 | */ | ||
| 1275 | void | ||
| 1276 | do_ssh2_kex() | ||
| 1277 | { | ||
| 1278 | Buffer *server_kexinit; | ||
| 1279 | Buffer *client_kexinit; | ||
| 1280 | int payload_len, dlen; | ||
| 1281 | int slen; | ||
| 1282 | unsigned int klen, kout; | ||
| 1283 | unsigned char *signature = NULL; | ||
| 1284 | unsigned char *server_host_key_blob = NULL; | ||
| 1285 | unsigned int sbloblen; | ||
| 1286 | DH *dh; | ||
| 1287 | BIGNUM *dh_client_pub = 0; | ||
| 1288 | BIGNUM *shared_secret = 0; | ||
| 1289 | int i; | ||
| 1290 | unsigned char *kbuf; | ||
| 1291 | unsigned char *hash; | ||
| 1292 | Kex *kex; | ||
| 1293 | char *cprop[PROPOSAL_MAX]; | ||
| 1294 | |||
| 1295 | /* KEXINIT */ | ||
| 1296 | |||
| 1297 | if (options.ciphers != NULL) { | ||
| 1298 | myproposal[PROPOSAL_ENC_ALGS_CTOS] = | ||
| 1299 | myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers; | ||
| 1300 | } | ||
| 1301 | server_kexinit = kex_init(myproposal); | ||
| 1302 | client_kexinit = xmalloc(sizeof(*client_kexinit)); | ||
| 1303 | buffer_init(client_kexinit); | ||
| 1304 | |||
| 1305 | /* algorithm negotiation */ | ||
| 1306 | kex_exchange_kexinit(server_kexinit, client_kexinit, cprop); | ||
| 1307 | kex = kex_choose_conf(cprop, myproposal, 1); | ||
| 1308 | for (i = 0; i < PROPOSAL_MAX; i++) | ||
| 1309 | xfree(cprop[i]); | ||
| 1310 | |||
| 1311 | /* KEXDH */ | ||
| 1312 | |||
| 1313 | debug("Wait SSH2_MSG_KEXDH_INIT."); | ||
| 1314 | packet_read_expect(&payload_len, SSH2_MSG_KEXDH_INIT); | ||
| 1315 | |||
| 1316 | /* key, cert */ | ||
| 1317 | dh_client_pub = BN_new(); | ||
| 1318 | if (dh_client_pub == NULL) | ||
| 1319 | fatal("dh_client_pub == NULL"); | ||
| 1320 | packet_get_bignum2(dh_client_pub, &dlen); | ||
| 1321 | |||
| 1322 | #ifdef DEBUG_KEXDH | ||
| 1323 | fprintf(stderr, "\ndh_client_pub= "); | ||
| 1324 | bignum_print(dh_client_pub); | ||
| 1325 | fprintf(stderr, "\n"); | ||
| 1326 | debug("bits %d", BN_num_bits(dh_client_pub)); | ||
| 1327 | #endif | ||
| 1328 | |||
| 1329 | /* generate DH key */ | ||
| 1330 | dh = dh_new_group1(); /* XXX depends on 'kex' */ | ||
| 1331 | |||
| 1332 | #ifdef DEBUG_KEXDH | ||
| 1333 | fprintf(stderr, "\np= "); | ||
| 1334 | bignum_print(dh->p); | ||
| 1335 | fprintf(stderr, "\ng= "); | ||
| 1336 | bignum_print(dh->g); | ||
| 1337 | fprintf(stderr, "\npub= "); | ||
| 1338 | bignum_print(dh->pub_key); | ||
| 1339 | fprintf(stderr, "\n"); | ||
| 1340 | #endif | ||
| 1341 | if (!dh_pub_is_valid(dh, dh_client_pub)) | ||
| 1342 | packet_disconnect("bad client public DH value"); | ||
| 1343 | |||
| 1344 | klen = DH_size(dh); | ||
| 1345 | kbuf = xmalloc(klen); | ||
| 1346 | kout = DH_compute_key(kbuf, dh_client_pub, dh); | ||
| 1347 | |||
| 1348 | #ifdef DEBUG_KEXDH | ||
| 1349 | debug("shared secret: len %d/%d", klen, kout); | ||
| 1350 | fprintf(stderr, "shared secret == "); | ||
| 1351 | for (i = 0; i< kout; i++) | ||
| 1352 | fprintf(stderr, "%02x", (kbuf[i])&0xff); | ||
| 1353 | fprintf(stderr, "\n"); | ||
| 1354 | #endif | ||
| 1355 | shared_secret = BN_new(); | ||
| 1356 | |||
| 1357 | BN_bin2bn(kbuf, kout, shared_secret); | ||
| 1358 | memset(kbuf, 0, klen); | ||
| 1359 | xfree(kbuf); | ||
| 1360 | |||
| 1361 | /* XXX precompute? */ | ||
| 1362 | dsa_make_key_blob(sensitive_data.dsa_host_key, &server_host_key_blob, &sbloblen); | ||
| 1363 | |||
| 1364 | /* calc H */ /* XXX depends on 'kex' */ | ||
| 1365 | hash = kex_hash( | ||
| 1366 | client_version_string, | ||
| 1367 | server_version_string, | ||
| 1368 | buffer_ptr(client_kexinit), buffer_len(client_kexinit), | ||
| 1369 | buffer_ptr(server_kexinit), buffer_len(server_kexinit), | ||
| 1370 | (char *)server_host_key_blob, sbloblen, | ||
| 1371 | dh_client_pub, | ||
| 1372 | dh->pub_key, | ||
| 1373 | shared_secret | ||
| 1374 | ); | ||
| 1375 | buffer_free(client_kexinit); | ||
| 1376 | buffer_free(server_kexinit); | ||
| 1377 | xfree(client_kexinit); | ||
| 1378 | xfree(server_kexinit); | ||
| 1379 | #ifdef DEBUG_KEXDH | ||
| 1380 | fprintf(stderr, "hash == "); | ||
| 1381 | for (i = 0; i< 20; i++) | ||
| 1382 | fprintf(stderr, "%02x", (hash[i])&0xff); | ||
| 1383 | fprintf(stderr, "\n"); | ||
| 1384 | #endif | ||
| 1385 | /* save session id := H */ | ||
| 1386 | /* XXX hashlen depends on KEX */ | ||
| 1387 | session_id2_len = 20; | ||
| 1388 | session_id2 = xmalloc(session_id2_len); | ||
| 1389 | memcpy(session_id2, hash, session_id2_len); | ||
| 1390 | |||
| 1391 | /* sign H */ | ||
| 1392 | /* XXX hashlen depends on KEX */ | ||
| 1393 | dsa_sign(sensitive_data.dsa_host_key, &signature, &slen, hash, 20); | ||
| 1394 | |||
| 1395 | destroy_sensitive_data(); | ||
| 1396 | |||
| 1397 | /* send server hostkey, DH pubkey 'f' and singed H */ | ||
| 1398 | packet_start(SSH2_MSG_KEXDH_REPLY); | ||
| 1399 | packet_put_string((char *)server_host_key_blob, sbloblen); | ||
| 1400 | packet_put_bignum2(dh->pub_key); /* f */ | ||
| 1401 | packet_put_string((char *)signature, slen); | ||
| 1402 | packet_send(); | ||
| 1403 | xfree(signature); | ||
| 1404 | xfree(server_host_key_blob); | ||
| 1405 | packet_write_wait(); | ||
| 1406 | |||
| 1407 | kex_derive_keys(kex, hash, shared_secret); | ||
| 1408 | packet_set_kex(kex); | ||
| 1409 | |||
| 1410 | /* have keys, free DH */ | ||
| 1411 | DH_free(dh); | ||
| 1412 | |||
| 1413 | debug("send SSH2_MSG_NEWKEYS."); | ||
| 1414 | packet_start(SSH2_MSG_NEWKEYS); | ||
| 1415 | packet_send(); | ||
| 1416 | packet_write_wait(); | ||
| 1417 | debug("done: send SSH2_MSG_NEWKEYS."); | ||
| 1418 | |||
| 1419 | debug("Wait SSH2_MSG_NEWKEYS."); | ||
| 1420 | packet_read_expect(&payload_len, SSH2_MSG_NEWKEYS); | ||
| 1421 | debug("GOT SSH2_MSG_NEWKEYS."); | ||
| 1422 | |||
| 1423 | #ifdef DEBUG_KEXDH | ||
| 1424 | /* send 1st encrypted/maced/compressed message */ | ||
| 1425 | packet_start(SSH2_MSG_IGNORE); | ||
| 1426 | packet_put_cstring("markus"); | ||
| 1427 | packet_send(); | ||
| 1428 | packet_write_wait(); | ||
| 1429 | #endif | ||
| 1430 | debug("done: KEX2."); | ||
| 1431 | } | ||
