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