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