Annotation of src/usr.bin/ssh/sshd.c, Revision 1.251
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.251 ! markus 45: RCSID("$OpenBSD: sshd.c,v 1.250 2002/06/23 10:29:52 deraadt 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.242 mouring 192: extern struct monitor *pmonitor;
1.231 provos 193: extern int use_privsep;
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: /* Close the connection. */
294: packet_close();
295:
296: /* Log error and exit. */
297: fatal("Timeout before authentication for %s.", get_remote_ipaddr());
1.62 markus 298: }
299:
1.65 deraadt 300: /*
301: * Signal handler for the key regeneration alarm. Note that this
302: * alarm only occurs in the daemon waiting for connections, and it does not
303: * do anything with the private key or random state before forking.
304: * Thus there should be no concurrency control/asynchronous execution
305: * problems.
306: */
1.200 itojun 307: static void
1.174 deraadt 308: generate_ephemeral_server_key(void)
1.134 markus 309: {
1.169 markus 310: u_int32_t rand = 0;
311: int i;
312:
1.191 markus 313: verbose("Generating %s%d bit RSA key.",
1.185 djm 314: sensitive_data.server_key ? "new " : "", options.server_key_bits);
1.134 markus 315: if (sensitive_data.server_key != NULL)
316: key_free(sensitive_data.server_key);
1.191 markus 317: sensitive_data.server_key = key_generate(KEY_RSA1,
1.185 djm 318: options.server_key_bits);
319: verbose("RSA key generation complete.");
1.169 markus 320:
321: for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) {
322: if (i % 4 == 0)
323: rand = arc4random();
324: sensitive_data.ssh1_cookie[i] = rand & 0xff;
325: rand >>= 8;
326: }
1.134 markus 327: arc4random_stir();
328: }
1.147 deraadt 329:
1.200 itojun 330: static void
1.64 markus 331: key_regeneration_alarm(int sig)
1.1 deraadt 332: {
1.64 markus 333: int save_errno = errno;
1.250 deraadt 334:
1.151 markus 335: signal(SIGALRM, SIG_DFL);
1.64 markus 336: errno = save_errno;
1.151 markus 337: key_do_regen = 1;
1.98 markus 338: }
339:
1.200 itojun 340: static void
1.96 markus 341: sshd_exchange_identification(int sock_in, int sock_out)
342: {
1.102 markus 343: int i, mismatch;
1.96 markus 344: int remote_major, remote_minor;
1.102 markus 345: int major, minor;
1.96 markus 346: char *s;
347: char buf[256]; /* Must not be larger than remote_version. */
348: char remote_version[256]; /* Must be at least as big as buf. */
349:
1.103 markus 350: if ((options.protocol & SSH_PROTO_1) &&
351: (options.protocol & SSH_PROTO_2)) {
1.102 markus 352: major = PROTOCOL_MAJOR_1;
353: minor = 99;
354: } else if (options.protocol & SSH_PROTO_2) {
355: major = PROTOCOL_MAJOR_2;
356: minor = PROTOCOL_MINOR_2;
357: } else {
358: major = PROTOCOL_MAJOR_1;
359: minor = PROTOCOL_MINOR_1;
360: }
361: snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n", major, minor, SSH_VERSION);
1.96 markus 362: server_version_string = xstrdup(buf);
363:
364: if (client_version_string == NULL) {
365: /* Send our protocol version identification. */
1.249 deraadt 366: if (atomicio(write, sock_out, server_version_string,
367: strlen(server_version_string))
1.96 markus 368: != strlen(server_version_string)) {
1.208 markus 369: log("Could not write ident string to %s", get_remote_ipaddr());
1.96 markus 370: fatal_cleanup();
371: }
372:
1.243 deraadt 373: /* Read other sides version identification. */
1.191 markus 374: memset(buf, 0, sizeof(buf));
1.96 markus 375: for (i = 0; i < sizeof(buf) - 1; i++) {
1.119 markus 376: if (atomicio(read, sock_in, &buf[i], 1) != 1) {
1.208 markus 377: log("Did not receive identification string from %s",
1.168 deraadt 378: get_remote_ipaddr());
1.96 markus 379: fatal_cleanup();
380: }
381: if (buf[i] == '\r') {
1.176 deraadt 382: buf[i] = 0;
1.132 markus 383: /* Kludge for F-Secure Macintosh < 1.0.2 */
384: if (i == 12 &&
385: strncmp(buf, "SSH-1.5-W1.0", 12) == 0)
386: break;
1.96 markus 387: continue;
388: }
389: if (buf[i] == '\n') {
1.176 deraadt 390: buf[i] = 0;
1.96 markus 391: break;
392: }
393: }
394: buf[sizeof(buf) - 1] = 0;
395: client_version_string = xstrdup(buf);
396: }
397:
398: /*
399: * Check that the versions match. In future this might accept
400: * several versions and set appropriate flags to handle them.
401: */
402: if (sscanf(client_version_string, "SSH-%d.%d-%[^\n]\n",
403: &remote_major, &remote_minor, remote_version) != 3) {
1.105 markus 404: s = "Protocol mismatch.\n";
1.96 markus 405: (void) atomicio(write, sock_out, s, strlen(s));
406: close(sock_in);
407: close(sock_out);
408: log("Bad protocol version identification '%.100s' from %s",
409: client_version_string, get_remote_ipaddr());
410: fatal_cleanup();
411: }
412: debug("Client protocol version %d.%d; client software version %.100s",
1.217 deraadt 413: remote_major, remote_minor, remote_version);
1.96 markus 414:
1.98 markus 415: compat_datafellows(remote_version);
1.175 deraadt 416:
417: if (datafellows & SSH_BUG_SCANNER) {
418: log("scanned from %s with %s. Don't panic.",
419: get_remote_ipaddr(), client_version_string);
420: fatal_cleanup();
421: }
1.98 markus 422:
1.102 markus 423: mismatch = 0;
1.214 deraadt 424: switch (remote_major) {
1.96 markus 425: case 1:
1.108 markus 426: if (remote_minor == 99) {
427: if (options.protocol & SSH_PROTO_2)
428: enable_compat20();
429: else
430: mismatch = 1;
431: break;
432: }
1.102 markus 433: if (!(options.protocol & SSH_PROTO_1)) {
434: mismatch = 1;
435: break;
436: }
1.96 markus 437: if (remote_minor < 3) {
1.121 provos 438: packet_disconnect("Your ssh version is too old and "
1.96 markus 439: "is no longer supported. Please install a newer version.");
440: } else if (remote_minor == 3) {
441: /* note that this disables agent-forwarding */
442: enable_compat13();
443: }
1.102 markus 444: break;
1.98 markus 445: case 2:
1.102 markus 446: if (options.protocol & SSH_PROTO_2) {
1.98 markus 447: enable_compat20();
448: break;
449: }
1.99 markus 450: /* FALLTHROUGH */
1.105 markus 451: default:
1.102 markus 452: mismatch = 1;
453: break;
454: }
455: chop(server_version_string);
456: debug("Local version string %.200s", server_version_string);
457:
458: if (mismatch) {
1.96 markus 459: s = "Protocol major versions differ.\n";
460: (void) atomicio(write, sock_out, s, strlen(s));
461: close(sock_in);
462: close(sock_out);
1.102 markus 463: log("Protocol major versions differ for %s: %.200s vs. %.200s",
464: get_remote_ipaddr(),
465: server_version_string, client_version_string);
1.96 markus 466: fatal_cleanup();
467: }
1.108 markus 468: }
469:
1.134 markus 470: /* Destroy the host and server keys. They will no longer be needed. */
1.108 markus 471: void
472: destroy_sensitive_data(void)
473: {
1.134 markus 474: int i;
475:
476: if (sensitive_data.server_key) {
477: key_free(sensitive_data.server_key);
478: sensitive_data.server_key = NULL;
479: }
1.217 deraadt 480: for (i = 0; i < options.num_host_key_files; i++) {
1.134 markus 481: if (sensitive_data.host_keys[i]) {
482: key_free(sensitive_data.host_keys[i]);
483: sensitive_data.host_keys[i] = NULL;
484: }
485: }
486: sensitive_data.ssh1_host_key = NULL;
1.169 markus 487: memset(sensitive_data.ssh1_cookie, 0, SSH_SESSION_KEY_LENGTH);
1.134 markus 488: }
489:
1.231 provos 490: /* Demote private to public keys for network child */
491: void
492: demote_sensitive_data(void)
493: {
494: Key *tmp;
495: int i;
496:
497: if (sensitive_data.server_key) {
498: tmp = key_demote(sensitive_data.server_key);
499: key_free(sensitive_data.server_key);
500: sensitive_data.server_key = tmp;
501: }
502:
503: for (i = 0; i < options.num_host_key_files; i++) {
504: if (sensitive_data.host_keys[i]) {
505: tmp = key_demote(sensitive_data.host_keys[i]);
506: key_free(sensitive_data.host_keys[i]);
507: sensitive_data.host_keys[i] = tmp;
508: if (tmp->type == KEY_RSA1)
509: sensitive_data.ssh1_host_key = tmp;
510: }
511: }
512:
513: /* We do not clear ssh1_host key and cookie. XXX - Okay Niels? */
514: }
515:
1.233 markus 516: static void
1.231 provos 517: privsep_preauth_child(void)
518: {
519: u_int32_t rand[256];
1.251 ! markus 520: gid_t gidset[2];
1.250 deraadt 521: struct passwd *pw;
1.231 provos 522: int i;
523:
524: /* Enable challenge-response authentication for privilege separation */
525: privsep_challenge_enable();
526:
527: for (i = 0; i < 256; i++)
528: rand[i] = arc4random();
529: RAND_seed(rand, sizeof(rand));
530:
531: /* Demote the private keys to public keys. */
532: demote_sensitive_data();
533:
1.235 stevesk 534: if ((pw = getpwnam(SSH_PRIVSEP_USER)) == NULL)
1.240 djm 535: fatal("Privilege separation user %s does not exist",
536: SSH_PRIVSEP_USER);
1.235 stevesk 537: memset(pw->pw_passwd, 0, strlen(pw->pw_passwd));
538: endpwent();
539:
1.231 provos 540: /* Change our root directory*/
1.232 stevesk 541: if (chroot(_PATH_PRIVSEP_CHROOT_DIR) == -1)
542: fatal("chroot(\"%s\"): %s", _PATH_PRIVSEP_CHROOT_DIR,
543: strerror(errno));
1.231 provos 544: if (chdir("/") == -1)
1.236 stevesk 545: fatal("chdir(\"/\"): %s", strerror(errno));
1.234 markus 546:
1.231 provos 547: /* Drop our privileges */
1.235 stevesk 548: debug3("privsep user:group %u:%u", (u_int)pw->pw_uid,
549: (u_int)pw->pw_gid);
1.251 ! markus 550: #if 0
! 551: /* XXX not ready, to heavy after chroot */
1.235 stevesk 552: do_setusercontext(pw);
1.251 ! markus 553: #else
! 554: gidset[0] = pw->pw_gid;
! 555: if (setgid(pw->pw_gid) < 0)
! 556: fatal("setgid failed for %u", pw->pw_gid );
! 557: if (setgroups(1, gidset) < 0)
! 558: fatal("setgroups: %.100s", strerror(errno));
! 559: permanently_set_uid(pw);
! 560: #endif
1.231 provos 561: }
562:
1.237 markus 563: static Authctxt*
564: privsep_preauth(void)
565: {
566: Authctxt *authctxt = NULL;
567: int status;
568: pid_t pid;
569:
570: /* Set up unprivileged child process to deal with network data */
1.242 mouring 571: pmonitor = monitor_init();
1.237 markus 572: /* Store a pointer to the kex for later rekeying */
1.242 mouring 573: pmonitor->m_pkex = &xxx_kex;
1.237 markus 574:
575: pid = fork();
576: if (pid == -1) {
577: fatal("fork of unprivileged child failed");
578: } else if (pid != 0) {
1.245 mpech 579: debug2("Network child is on pid %ld", (long)pid);
1.237 markus 580:
1.242 mouring 581: close(pmonitor->m_recvfd);
582: authctxt = monitor_child_preauth(pmonitor);
583: close(pmonitor->m_sendfd);
1.237 markus 584:
585: /* Sync memory */
1.242 mouring 586: monitor_sync(pmonitor);
1.237 markus 587:
588: /* Wait for the child's exit status */
1.239 markus 589: while (waitpid(pid, &status, 0) < 0)
590: if (errno != EINTR)
591: break;
1.237 markus 592: return (authctxt);
593: } else {
594: /* child */
595:
1.242 mouring 596: close(pmonitor->m_sendfd);
1.237 markus 597:
598: /* Demote the child */
599: if (getuid() == 0 || geteuid() == 0)
600: privsep_preauth_child();
1.238 stevesk 601: setproctitle("%s", "[net]");
1.237 markus 602: }
603: return (NULL);
604: }
605:
1.233 markus 606: static void
1.237 markus 607: privsep_postauth(Authctxt *authctxt)
1.231 provos 608: {
609: extern Authctxt *x_authctxt;
610:
611: /* XXX - Remote port forwarding */
612: x_authctxt = authctxt;
613:
614: if (authctxt->pw->pw_uid == 0 || options.use_login) {
615: /* File descriptor passing is broken or root login */
1.242 mouring 616: monitor_apply_keystate(pmonitor);
1.231 provos 617: use_privsep = 0;
618: return;
619: }
1.234 markus 620:
1.231 provos 621: /* Authentication complete */
622: alarm(0);
623: if (startup_pipe != -1) {
624: close(startup_pipe);
625: startup_pipe = -1;
626: }
627:
628: /* New socket pair */
1.242 mouring 629: monitor_reinit(pmonitor);
1.231 provos 630:
1.242 mouring 631: pmonitor->m_pid = fork();
632: if (pmonitor->m_pid == -1)
1.231 provos 633: fatal("fork of unprivileged child failed");
1.242 mouring 634: else if (pmonitor->m_pid != 0) {
1.245 mpech 635: debug2("User child is on pid %ld", (long)pmonitor->m_pid);
1.242 mouring 636: close(pmonitor->m_recvfd);
637: monitor_child_postauth(pmonitor);
1.231 provos 638:
639: /* NEVERREACHED */
640: exit(0);
641: }
642:
1.242 mouring 643: close(pmonitor->m_sendfd);
1.231 provos 644:
645: /* Demote the private keys to public keys. */
646: demote_sensitive_data();
647:
648: /* Drop privileges */
649: do_setusercontext(authctxt->pw);
650:
651: /* It is safe now to apply the key state */
1.242 mouring 652: monitor_apply_keystate(pmonitor);
1.231 provos 653: }
654:
1.200 itojun 655: static char *
1.134 markus 656: list_hostkey_types(void)
657: {
1.223 markus 658: Buffer b;
659: char *p;
1.134 markus 660: int i;
1.223 markus 661:
662: buffer_init(&b);
1.217 deraadt 663: for (i = 0; i < options.num_host_key_files; i++) {
1.134 markus 664: Key *key = sensitive_data.host_keys[i];
665: if (key == NULL)
666: continue;
1.214 deraadt 667: switch (key->type) {
1.134 markus 668: case KEY_RSA:
669: case KEY_DSA:
1.223 markus 670: if (buffer_len(&b) > 0)
671: buffer_append(&b, ",", 1);
672: p = key_ssh_name(key);
673: buffer_append(&b, p, strlen(p));
1.134 markus 674: break;
675: }
676: }
1.223 markus 677: buffer_append(&b, "\0", 1);
678: p = xstrdup(buffer_ptr(&b));
679: buffer_free(&b);
680: debug("list_hostkey_types: %s", p);
681: return p;
1.134 markus 682: }
683:
1.231 provos 684: Key *
1.134 markus 685: get_hostkey_by_type(int type)
686: {
687: int i;
1.250 deraadt 688:
1.217 deraadt 689: for (i = 0; i < options.num_host_key_files; i++) {
1.134 markus 690: Key *key = sensitive_data.host_keys[i];
691: if (key != NULL && key->type == type)
692: return key;
693: }
694: return NULL;
1.96 markus 695: }
696:
1.231 provos 697: Key *
698: get_hostkey_by_index(int ind)
699: {
700: if (ind < 0 || ind >= options.num_host_key_files)
701: return (NULL);
702: return (sensitive_data.host_keys[ind]);
703: }
704:
705: int
706: get_hostkey_index(Key *key)
707: {
708: int i;
1.250 deraadt 709:
1.231 provos 710: for (i = 0; i < options.num_host_key_files; i++) {
711: if (key == sensitive_data.host_keys[i])
712: return (i);
713: }
714: return (-1);
715: }
716:
1.124 markus 717: /*
718: * returns 1 if connection should be dropped, 0 otherwise.
719: * dropping starts at connection #max_startups_begin with a probability
720: * of (max_startups_rate/100). the probability increases linearly until
721: * all connections are dropped for startups > max_startups
722: */
1.200 itojun 723: static int
1.124 markus 724: drop_connection(int startups)
725: {
726: double p, r;
727:
728: if (startups < options.max_startups_begin)
729: return 0;
730: if (startups >= options.max_startups)
731: return 1;
732: if (options.max_startups_rate == 100)
733: return 1;
734:
735: p = 100 - options.max_startups_rate;
736: p *= startups - options.max_startups_begin;
737: p /= (double) (options.max_startups - options.max_startups_begin);
738: p += options.max_startups_rate;
739: p /= 100.0;
740: r = arc4random() / (double) UINT_MAX;
741:
742: debug("drop_connection: p %g, r %g", p, r);
743: return (r < p) ? 1 : 0;
744: }
745:
1.215 markus 746: static void
747: usage(void)
748: {
749: fprintf(stderr, "sshd version %s\n", SSH_VERSION);
750: fprintf(stderr, "Usage: %s [options]\n", __progname);
751: fprintf(stderr, "Options:\n");
752: fprintf(stderr, " -f file Configuration file (default %s)\n", _PATH_SERVER_CONFIG_FILE);
753: fprintf(stderr, " -d Debugging mode (multiple -d means more debugging)\n");
754: fprintf(stderr, " -i Started from inetd\n");
755: fprintf(stderr, " -D Do not fork into daemon mode\n");
756: fprintf(stderr, " -t Only test configuration file and keys\n");
757: fprintf(stderr, " -q Quiet (no logging)\n");
758: fprintf(stderr, " -p port Listen on the specified port (default: 22)\n");
759: fprintf(stderr, " -k seconds Regenerate server key every this many seconds (default: 3600)\n");
760: fprintf(stderr, " -g seconds Grace period for authentication (default: 600)\n");
761: fprintf(stderr, " -b bits Size of server RSA key (default: 768 bits)\n");
762: fprintf(stderr, " -h file File from which to read host key (default: %s)\n",
763: _PATH_HOST_KEY_FILE);
764: fprintf(stderr, " -u len Maximum hostname length for utmp recording\n");
765: fprintf(stderr, " -4 Use IPv4 only\n");
766: fprintf(stderr, " -6 Use IPv6 only\n");
767: fprintf(stderr, " -o option Process the option as if it was read from a configuration file.\n");
768: exit(1);
769: }
770:
1.65 deraadt 771: /*
772: * Main program for the daemon.
773: */
1.2 provos 774: int
775: main(int ac, char **av)
1.1 deraadt 776: {
1.64 markus 777: extern char *optarg;
778: extern int optind;
1.120 markus 779: int opt, sock_in = 0, sock_out = 0, newsock, j, i, fdsetsz, on = 1;
1.107 deraadt 780: pid_t pid;
1.75 markus 781: socklen_t fromlen;
782: fd_set *fdset;
783: struct sockaddr_storage from;
1.64 markus 784: const char *remote_ip;
785: int remote_port;
786: FILE *f;
787: struct linger linger;
1.75 markus 788: struct addrinfo *ai;
789: char ntop[NI_MAXHOST], strport[NI_MAXSERV];
790: int listen_sock, maxfd;
1.120 markus 791: int startup_p[2];
792: int startups = 0;
1.230 provos 793: Authctxt *authctxt;
1.179 markus 794: Key *key;
1.151 markus 795: int ret, key_used = 0;
1.64 markus 796:
1.138 markus 797: /* Save argv. */
1.64 markus 798: saved_argv = av;
799:
800: /* Initialize configuration options to their default values. */
801: initialize_server_options(&options);
802:
803: /* Parse command-line arguments. */
1.215 markus 804: while ((opt = getopt(ac, av, "f:p:b:k:h:g:V:u:o:dDeiqtQ46")) != -1) {
1.64 markus 805: switch (opt) {
1.75 markus 806: case '4':
807: IPv4or6 = AF_INET;
808: break;
809: case '6':
810: IPv4or6 = AF_INET6;
811: break;
1.64 markus 812: case 'f':
813: config_file_name = optarg;
814: break;
815: case 'd':
1.127 markus 816: if (0 == debug_flag) {
817: debug_flag = 1;
818: options.log_level = SYSLOG_LEVEL_DEBUG1;
819: } else if (options.log_level < SYSLOG_LEVEL_DEBUG3) {
820: options.log_level++;
821: } else {
822: fprintf(stderr, "Too high debugging level.\n");
823: exit(1);
824: }
1.64 markus 825: break;
1.135 markus 826: case 'D':
827: no_daemon_flag = 1;
1.192 lebel 828: break;
829: case 'e':
830: log_stderr = 1;
1.135 markus 831: break;
1.64 markus 832: case 'i':
833: inetd_flag = 1;
834: break;
835: case 'Q':
1.158 markus 836: /* ignored */
1.64 markus 837: break;
838: case 'q':
839: options.log_level = SYSLOG_LEVEL_QUIET;
840: break;
841: case 'b':
842: options.server_key_bits = atoi(optarg);
843: break;
844: case 'p':
1.75 markus 845: options.ports_from_cmdline = 1;
1.127 markus 846: if (options.num_ports >= MAX_PORTS) {
847: fprintf(stderr, "too many ports.\n");
848: exit(1);
849: }
1.193 stevesk 850: options.ports[options.num_ports++] = a2port(optarg);
851: if (options.ports[options.num_ports-1] == 0) {
852: fprintf(stderr, "Bad port number.\n");
853: exit(1);
854: }
1.64 markus 855: break;
856: case 'g':
1.197 stevesk 857: if ((options.login_grace_time = convtime(optarg)) == -1) {
858: fprintf(stderr, "Invalid login grace time.\n");
859: exit(1);
860: }
1.64 markus 861: break;
862: case 'k':
1.197 stevesk 863: if ((options.key_regeneration_time = convtime(optarg)) == -1) {
864: fprintf(stderr, "Invalid key regeneration interval.\n");
865: exit(1);
866: }
1.64 markus 867: break;
868: case 'h':
1.134 markus 869: if (options.num_host_key_files >= MAX_HOSTKEYS) {
870: fprintf(stderr, "too many host keys.\n");
871: exit(1);
872: }
873: options.host_key_files[options.num_host_key_files++] = optarg;
1.64 markus 874: break;
875: case 'V':
876: client_version_string = optarg;
877: /* only makes sense with inetd_flag, i.e. no listen() */
878: inetd_flag = 1;
879: break;
1.203 stevesk 880: case 't':
881: test_flag = 1;
882: break;
1.125 markus 883: case 'u':
884: utmp_len = atoi(optarg);
885: break;
1.215 markus 886: case 'o':
1.217 deraadt 887: if (process_server_config_line(&options, optarg,
1.215 markus 888: "command-line", 0) != 0)
1.217 deraadt 889: exit(1);
1.215 markus 890: break;
1.64 markus 891: case '?':
892: default:
1.215 markus 893: usage();
894: break;
1.64 markus 895: }
896: }
1.180 markus 897: SSLeay_add_all_algorithms();
1.205 markus 898: channel_set_af(IPv4or6);
1.64 markus 899:
1.75 markus 900: /*
901: * Force logging to stderr until we have loaded the private host
902: * key (unless started from inetd)
903: */
1.138 markus 904: log_init(__progname,
1.224 markus 905: options.log_level == SYSLOG_LEVEL_NOT_SET ?
906: SYSLOG_LEVEL_INFO : options.log_level,
907: options.log_facility == SYSLOG_FACILITY_NOT_SET ?
908: SYSLOG_FACILITY_AUTH : options.log_facility,
1.158 markus 909: !inetd_flag);
1.75 markus 910:
1.64 markus 911: /* Read server configuration options from the configuration file. */
912: read_server_config(&options, config_file_name);
913:
914: /* Fill in default values for those options not explicitly set. */
915: fill_default_server_options(&options);
916:
917: /* Check that there are no remaining arguments. */
918: if (optind < ac) {
919: fprintf(stderr, "Extra argument %s.\n", av[optind]);
920: exit(1);
921: }
922:
923: debug("sshd version %.100s", SSH_VERSION);
924:
1.134 markus 925: /* load private host keys */
926: sensitive_data.host_keys = xmalloc(options.num_host_key_files*sizeof(Key*));
1.217 deraadt 927: for (i = 0; i < options.num_host_key_files; i++)
1.141 markus 928: sensitive_data.host_keys[i] = NULL;
1.134 markus 929: sensitive_data.server_key = NULL;
930: sensitive_data.ssh1_host_key = NULL;
931: sensitive_data.have_ssh1_key = 0;
932: sensitive_data.have_ssh2_key = 0;
933:
1.217 deraadt 934: for (i = 0; i < options.num_host_key_files; i++) {
1.179 markus 935: key = key_load_private(options.host_key_files[i], "", NULL);
936: sensitive_data.host_keys[i] = key;
1.134 markus 937: if (key == NULL) {
1.195 markus 938: error("Could not load host key: %s",
939: options.host_key_files[i]);
1.179 markus 940: sensitive_data.host_keys[i] = NULL;
1.134 markus 941: continue;
942: }
1.214 deraadt 943: switch (key->type) {
1.134 markus 944: case KEY_RSA1:
945: sensitive_data.ssh1_host_key = key;
946: sensitive_data.have_ssh1_key = 1;
947: break;
948: case KEY_RSA:
949: case KEY_DSA:
950: sensitive_data.have_ssh2_key = 1;
951: break;
952: }
1.179 markus 953: debug("private host key: #%d type %d %s", i, key->type,
954: key_type(key));
1.134 markus 955: }
956: if ((options.protocol & SSH_PROTO_1) && !sensitive_data.have_ssh1_key) {
957: log("Disabling protocol version 1. Could not load host key");
1.108 markus 958: options.protocol &= ~SSH_PROTO_1;
959: }
1.134 markus 960: if ((options.protocol & SSH_PROTO_2) && !sensitive_data.have_ssh2_key) {
961: log("Disabling protocol version 2. Could not load host key");
962: options.protocol &= ~SSH_PROTO_2;
1.108 markus 963: }
1.162 stevesk 964: if (!(options.protocol & (SSH_PROTO_1|SSH_PROTO_2))) {
1.172 millert 965: log("sshd: no hostkeys available -- exiting.");
1.64 markus 966: exit(1);
967: }
968:
1.108 markus 969: /* Check certain values for sanity. */
970: if (options.protocol & SSH_PROTO_1) {
971: if (options.server_key_bits < 512 ||
972: options.server_key_bits > 32768) {
973: fprintf(stderr, "Bad server key size.\n");
974: exit(1);
975: }
976: /*
977: * Check that server and host key lengths differ sufficiently. This
978: * is necessary to make double encryption work with rsaref. Oh, I
979: * hate software patents. I dont know if this can go? Niels
980: */
981: if (options.server_key_bits >
1.250 deraadt 982: BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) -
983: SSH_KEY_BITS_RESERVED && options.server_key_bits <
984: BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) +
985: SSH_KEY_BITS_RESERVED) {
1.108 markus 986: options.server_key_bits =
1.250 deraadt 987: BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) +
988: SSH_KEY_BITS_RESERVED;
1.108 markus 989: debug("Forcing server key to %d bits to make it differ from host key.",
990: options.server_key_bits);
991: }
1.244 markus 992: }
993:
994: if (use_privsep) {
995: struct passwd *pw;
996: struct stat st;
997:
998: if ((pw = getpwnam(SSH_PRIVSEP_USER)) == NULL)
999: fatal("Privilege separation user %s does not exist",
1000: SSH_PRIVSEP_USER);
1001: if ((stat(_PATH_PRIVSEP_CHROOT_DIR, &st) == -1) ||
1002: (S_ISDIR(st.st_mode) == 0))
1003: fatal("Missing privilege separation directory: %s",
1.247 stevesk 1004: _PATH_PRIVSEP_CHROOT_DIR);
1005: if (st.st_uid != 0 || (st.st_mode & (S_IWGRP|S_IWOTH)) != 0)
1006: fatal("Bad owner or mode for %s",
1.244 markus 1007: _PATH_PRIVSEP_CHROOT_DIR);
1.108 markus 1008: }
1.203 stevesk 1009:
1010: /* Configuration looks good, so exit if in test mode. */
1011: if (test_flag)
1012: exit(0);
1.108 markus 1013:
1014: /* Initialize the log (it is reinitialized below in case we forked). */
1.64 markus 1015: if (debug_flag && !inetd_flag)
1016: log_stderr = 1;
1.138 markus 1017: log_init(__progname, options.log_level, options.log_facility, log_stderr);
1.64 markus 1018:
1.108 markus 1019: /*
1020: * If not in debugging mode, and not started from inetd, disconnect
1021: * from the controlling terminal, and fork. The original process
1022: * exits.
1023: */
1.135 markus 1024: if (!(debug_flag || inetd_flag || no_daemon_flag)) {
1.1 deraadt 1025: #ifdef TIOCNOTTY
1.64 markus 1026: int fd;
1.1 deraadt 1027: #endif /* TIOCNOTTY */
1.64 markus 1028: if (daemon(0, 0) < 0)
1029: fatal("daemon() failed: %.200s", strerror(errno));
1030:
1031: /* Disconnect from the controlling tty. */
1.1 deraadt 1032: #ifdef TIOCNOTTY
1.165 itojun 1033: fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
1.64 markus 1034: if (fd >= 0) {
1035: (void) ioctl(fd, TIOCNOTTY, NULL);
1036: close(fd);
1037: }
1038: #endif /* TIOCNOTTY */
1039: }
1040: /* Reinitialize the log (because of the fork above). */
1.138 markus 1041: log_init(__progname, options.log_level, options.log_facility, log_stderr);
1.64 markus 1042:
1043: /* Initialize the random number generator. */
1044: arc4random_stir();
1045:
1046: /* Chdir to the root directory so that the current disk can be
1047: unmounted if desired. */
1048: chdir("/");
1.217 deraadt 1049:
1.178 markus 1050: /* ignore SIGPIPE */
1051: signal(SIGPIPE, SIG_IGN);
1.64 markus 1052:
1053: /* Start listening for a socket, unless started from inetd. */
1054: if (inetd_flag) {
1.194 markus 1055: int s1;
1.64 markus 1056: s1 = dup(0); /* Make sure descriptors 0, 1, and 2 are in use. */
1.194 markus 1057: dup(s1);
1.64 markus 1058: sock_in = dup(0);
1059: sock_out = dup(1);
1.123 djm 1060: startup_pipe = -1;
1.108 markus 1061: /*
1062: * We intentionally do not close the descriptors 0, 1, and 2
1063: * as our code for setting the descriptors won\'t work if
1064: * ttyfd happens to be one of those.
1065: */
1.64 markus 1066: debug("inetd sockets after dupping: %d, %d", sock_in, sock_out);
1.134 markus 1067: if (options.protocol & SSH_PROTO_1)
1.174 deraadt 1068: generate_ephemeral_server_key();
1.64 markus 1069: } else {
1.75 markus 1070: for (ai = options.listen_addrs; ai; ai = ai->ai_next) {
1071: if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1072: continue;
1073: if (num_listen_socks >= MAX_LISTEN_SOCKS)
1074: fatal("Too many listen sockets. "
1075: "Enlarge MAX_LISTEN_SOCKS");
1076: if (getnameinfo(ai->ai_addr, ai->ai_addrlen,
1077: ntop, sizeof(ntop), strport, sizeof(strport),
1078: NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
1079: error("getnameinfo failed");
1080: continue;
1081: }
1082: /* Create socket for listening. */
1083: listen_sock = socket(ai->ai_family, SOCK_STREAM, 0);
1084: if (listen_sock < 0) {
1085: /* kernel may not support ipv6 */
1086: verbose("socket: %.100s", strerror(errno));
1087: continue;
1088: }
1089: if (fcntl(listen_sock, F_SETFL, O_NONBLOCK) < 0) {
1090: error("listen_sock O_NONBLOCK: %s", strerror(errno));
1091: close(listen_sock);
1092: continue;
1093: }
1094: /*
1095: * Set socket options. We try to make the port
1096: * reusable and have it close as fast as possible
1097: * without waiting in unnecessary wait states on
1098: * close.
1099: */
1100: setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR,
1.228 stevesk 1101: &on, sizeof(on));
1.75 markus 1102: linger.l_onoff = 1;
1103: linger.l_linger = 5;
1104: setsockopt(listen_sock, SOL_SOCKET, SO_LINGER,
1.228 stevesk 1105: &linger, sizeof(linger));
1.75 markus 1106:
1107: debug("Bind to port %s on %s.", strport, ntop);
1108:
1109: /* Bind the socket to the desired port. */
1110: if (bind(listen_sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1111: error("Bind to port %s on %s failed: %.200s.",
1112: strport, ntop, strerror(errno));
1113: close(listen_sock);
1114: continue;
1115: }
1116: listen_socks[num_listen_socks] = listen_sock;
1117: num_listen_socks++;
1118:
1119: /* Start listening on the port. */
1120: log("Server listening on %s port %s.", ntop, strport);
1121: if (listen(listen_sock, 5) < 0)
1122: fatal("listen: %.100s", strerror(errno));
1123:
1.64 markus 1124: }
1.75 markus 1125: freeaddrinfo(options.listen_addrs);
1126:
1127: if (!num_listen_socks)
1128: fatal("Cannot bind any address.");
1129:
1.201 markus 1130: if (options.protocol & SSH_PROTO_1)
1131: generate_ephemeral_server_key();
1132:
1133: /*
1134: * Arrange to restart on SIGHUP. The handler needs
1135: * listen_sock.
1136: */
1137: signal(SIGHUP, sighup_handler);
1138:
1139: signal(SIGTERM, sigterm_handler);
1140: signal(SIGQUIT, sigterm_handler);
1141:
1142: /* Arrange SIGCHLD to be caught. */
1143: signal(SIGCHLD, main_sigchld_handler);
1144:
1145: /* Write out the pid file after the sigterm handler is setup */
1.64 markus 1146: if (!debug_flag) {
1.66 markus 1147: /*
1.136 todd 1148: * Record our pid in /var/run/sshd.pid to make it
1149: * easier to kill the correct sshd. We don't want to
1150: * do this before the bind above because the bind will
1.66 markus 1151: * fail if there already is a daemon, and this will
1152: * overwrite any old pid in the file.
1153: */
1.112 markus 1154: f = fopen(options.pid_file, "w");
1.64 markus 1155: if (f) {
1.245 mpech 1156: fprintf(f, "%ld\n", (long) getpid());
1.64 markus 1157: fclose(f);
1158: }
1159: }
1160:
1.75 markus 1161: /* setup fd set for listen */
1.120 markus 1162: fdset = NULL;
1.75 markus 1163: maxfd = 0;
1164: for (i = 0; i < num_listen_socks; i++)
1165: if (listen_socks[i] > maxfd)
1166: maxfd = listen_socks[i];
1.120 markus 1167: /* pipes connected to unauthenticated childs */
1168: startup_pipes = xmalloc(options.max_startups * sizeof(int));
1169: for (i = 0; i < options.max_startups; i++)
1170: startup_pipes[i] = -1;
1.75 markus 1171:
1.66 markus 1172: /*
1173: * Stay listening for connections until the system crashes or
1174: * the daemon is killed with a signal.
1175: */
1.64 markus 1176: for (;;) {
1177: if (received_sighup)
1178: sighup_restart();
1.120 markus 1179: if (fdset != NULL)
1180: xfree(fdset);
1.148 markus 1181: fdsetsz = howmany(maxfd+1, NFDBITS) * sizeof(fd_mask);
1.120 markus 1182: fdset = (fd_set *)xmalloc(fdsetsz);
1.75 markus 1183: memset(fdset, 0, fdsetsz);
1.120 markus 1184:
1.75 markus 1185: for (i = 0; i < num_listen_socks; i++)
1186: FD_SET(listen_socks[i], fdset);
1.120 markus 1187: for (i = 0; i < options.max_startups; i++)
1188: if (startup_pipes[i] != -1)
1189: FD_SET(startup_pipes[i], fdset);
1190:
1191: /* Wait in select until there is a connection. */
1.151 markus 1192: ret = select(maxfd+1, fdset, NULL, NULL, NULL);
1193: if (ret < 0 && errno != EINTR)
1194: error("select: %.100s", strerror(errno));
1.199 markus 1195: if (received_sigterm) {
1196: log("Received signal %d; terminating.",
1.213 itojun 1197: (int) received_sigterm);
1.199 markus 1198: close_listen_socks();
1199: unlink(options.pid_file);
1200: exit(255);
1201: }
1.151 markus 1202: if (key_used && key_do_regen) {
1.174 deraadt 1203: generate_ephemeral_server_key();
1.151 markus 1204: key_used = 0;
1205: key_do_regen = 0;
1206: }
1207: if (ret < 0)
1.75 markus 1208: continue;
1.151 markus 1209:
1.120 markus 1210: for (i = 0; i < options.max_startups; i++)
1211: if (startup_pipes[i] != -1 &&
1212: FD_ISSET(startup_pipes[i], fdset)) {
1213: /*
1214: * the read end of the pipe is ready
1215: * if the child has closed the pipe
1.143 markus 1216: * after successful authentication
1.120 markus 1217: * or if the child has died
1218: */
1219: close(startup_pipes[i]);
1220: startup_pipes[i] = -1;
1221: startups--;
1222: }
1.75 markus 1223: for (i = 0; i < num_listen_socks; i++) {
1224: if (!FD_ISSET(listen_socks[i], fdset))
1.70 provos 1225: continue;
1.120 markus 1226: fromlen = sizeof(from);
1227: newsock = accept(listen_socks[i], (struct sockaddr *)&from,
1228: &fromlen);
1229: if (newsock < 0) {
1230: if (errno != EINTR && errno != EWOULDBLOCK)
1231: error("accept: %.100s", strerror(errno));
1232: continue;
1233: }
1234: if (fcntl(newsock, F_SETFL, 0) < 0) {
1235: error("newsock del O_NONBLOCK: %s", strerror(errno));
1.216 stevesk 1236: close(newsock);
1.120 markus 1237: continue;
1238: }
1.124 markus 1239: if (drop_connection(startups) == 1) {
1240: debug("drop connection #%d", startups);
1.120 markus 1241: close(newsock);
1242: continue;
1243: }
1244: if (pipe(startup_p) == -1) {
1245: close(newsock);
1246: continue;
1247: }
1248:
1249: for (j = 0; j < options.max_startups; j++)
1250: if (startup_pipes[j] == -1) {
1251: startup_pipes[j] = startup_p[0];
1252: if (maxfd < startup_p[0])
1253: maxfd = startup_p[0];
1254: startups++;
1255: break;
1256: }
1.161 stevesk 1257:
1.66 markus 1258: /*
1.120 markus 1259: * Got connection. Fork a child to handle it, unless
1260: * we are in debugging mode.
1.66 markus 1261: */
1.120 markus 1262: if (debug_flag) {
1.66 markus 1263: /*
1.120 markus 1264: * In debugging mode. Close the listening
1265: * socket, and start processing the
1266: * connection without forking.
1.66 markus 1267: */
1.120 markus 1268: debug("Server will not fork when running in debugging mode.");
1.75 markus 1269: close_listen_socks();
1.64 markus 1270: sock_in = newsock;
1271: sock_out = newsock;
1.122 deraadt 1272: startup_pipe = -1;
1.120 markus 1273: pid = getpid();
1.64 markus 1274: break;
1.120 markus 1275: } else {
1276: /*
1277: * Normal production daemon. Fork, and have
1278: * the child process the connection. The
1279: * parent continues listening.
1280: */
1281: if ((pid = fork()) == 0) {
1282: /*
1283: * Child. Close the listening and max_startup
1284: * sockets. Start using the accepted socket.
1285: * Reinitialize logging (since our pid has
1286: * changed). We break out of the loop to handle
1287: * the connection.
1288: */
1289: startup_pipe = startup_p[1];
1.211 markus 1290: close_startup_pipes();
1.120 markus 1291: close_listen_socks();
1292: sock_in = newsock;
1293: sock_out = newsock;
1.138 markus 1294: log_init(__progname, options.log_level, options.log_facility, log_stderr);
1.120 markus 1295: break;
1296: }
1.64 markus 1297: }
1298:
1.120 markus 1299: /* Parent. Stay in the loop. */
1300: if (pid < 0)
1301: error("fork: %.100s", strerror(errno));
1302: else
1.245 mpech 1303: debug("Forked child %ld.", (long)pid);
1.120 markus 1304:
1305: close(startup_p[1]);
1.1 deraadt 1306:
1.120 markus 1307: /* Mark that the key has been used (it was "given" to the child). */
1.151 markus 1308: if ((options.protocol & SSH_PROTO_1) &&
1309: key_used == 0) {
1310: /* Schedule server key regeneration alarm. */
1311: signal(SIGALRM, key_regeneration_alarm);
1312: alarm(options.key_regeneration_time);
1313: key_used = 1;
1314: }
1.1 deraadt 1315:
1.120 markus 1316: arc4random_stir();
1.1 deraadt 1317:
1.120 markus 1318: /* Close the new socket (the child is now taking care of it). */
1319: close(newsock);
1320: }
1.75 markus 1321: /* child process check (or debug mode) */
1322: if (num_listen_socks < 0)
1323: break;
1.64 markus 1324: }
1.1 deraadt 1325: }
1326:
1.64 markus 1327: /* This is the child processing a new connection. */
1.241 millert 1328:
1329: /*
1330: * Create a new session and process group since the 4.4BSD
1331: * setlogin() affects the entire process group. We don't
1332: * want the child to be able to affect the parent.
1333: */
1.248 stevesk 1334: if (!debug_flag && !inetd_flag && setsid() < 0)
1.241 millert 1335: error("setsid: %.100s", strerror(errno));
1.64 markus 1336:
1.66 markus 1337: /*
1338: * Disable the key regeneration alarm. We will not regenerate the
1339: * key since we are no longer in a position to give it to anyone. We
1340: * will not restart on SIGHUP since it no longer makes sense.
1341: */
1.64 markus 1342: alarm(0);
1343: signal(SIGALRM, SIG_DFL);
1344: signal(SIGHUP, SIG_DFL);
1345: signal(SIGTERM, SIG_DFL);
1346: signal(SIGQUIT, SIG_DFL);
1347: signal(SIGCHLD, SIG_DFL);
1348:
1.66 markus 1349: /*
1350: * Set socket options for the connection. We want the socket to
1351: * close as fast as possible without waiting for anything. If the
1352: * connection is not a socket, these will do nothing.
1353: */
1354: /* setsockopt(sock_in, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)); */
1.64 markus 1355: linger.l_onoff = 1;
1356: linger.l_linger = 5;
1.228 stevesk 1357: setsockopt(sock_in, SOL_SOCKET, SO_LINGER, &linger, sizeof(linger));
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.64 markus 1541: u_int32_t rand = 0;
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)
1554: rand = arc4random();
1.77 markus 1555: cookie[i] = rand & 0xff;
1.64 markus 1556: rand >>= 8;
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.186 markus 1749: kex->server = 1;
1750: kex->client_version_string=client_version_string;
1751: kex->server_version_string=server_version_string;
1752: kex->load_host_key=&get_hostkey_by_type;
1.231 provos 1753: kex->host_key_index=&get_hostkey_index;
1.129 provos 1754:
1.189 markus 1755: xxx_kex = kex;
1756:
1.190 markus 1757: dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
1.187 markus 1758:
1759: session_id2 = kex->session_id;
1760: session_id2_len = kex->session_id_len;
1.129 provos 1761:
1762: #ifdef DEBUG_KEXDH
1763: /* send 1st encrypted/maced/compressed message */
1764: packet_start(SSH2_MSG_IGNORE);
1765: packet_put_cstring("markus");
1766: packet_send();
1767: packet_write_wait();
1768: #endif
1.186 markus 1769: debug("KEX done");
1.1 deraadt 1770: }