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