Annotation of src/usr.bin/ssh/sshd.c, Revision 1.253
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.253 ! deraadt 45: RCSID("$OpenBSD: sshd.c,v 1.252 2002/06/28 10:08:25 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.253 ! deraadt 520: gid_t gidset[1];
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);
1.252 deraadt 885: if (utmp_len < 0 || utmp_len > MAXHOSTNAMELEN)
886: usage();
1.125 markus 887: break;
1.215 markus 888: case 'o':
1.217 deraadt 889: if (process_server_config_line(&options, optarg,
1.215 markus 890: "command-line", 0) != 0)
1.217 deraadt 891: exit(1);
1.215 markus 892: break;
1.64 markus 893: case '?':
894: default:
1.215 markus 895: usage();
896: break;
1.64 markus 897: }
898: }
1.180 markus 899: SSLeay_add_all_algorithms();
1.205 markus 900: channel_set_af(IPv4or6);
1.64 markus 901:
1.75 markus 902: /*
903: * Force logging to stderr until we have loaded the private host
904: * key (unless started from inetd)
905: */
1.138 markus 906: log_init(__progname,
1.224 markus 907: options.log_level == SYSLOG_LEVEL_NOT_SET ?
908: SYSLOG_LEVEL_INFO : options.log_level,
909: options.log_facility == SYSLOG_FACILITY_NOT_SET ?
910: SYSLOG_FACILITY_AUTH : options.log_facility,
1.158 markus 911: !inetd_flag);
1.75 markus 912:
1.64 markus 913: /* Read server configuration options from the configuration file. */
914: read_server_config(&options, config_file_name);
915:
916: /* Fill in default values for those options not explicitly set. */
917: fill_default_server_options(&options);
918:
919: /* Check that there are no remaining arguments. */
920: if (optind < ac) {
921: fprintf(stderr, "Extra argument %s.\n", av[optind]);
922: exit(1);
923: }
924:
925: debug("sshd version %.100s", SSH_VERSION);
926:
1.134 markus 927: /* load private host keys */
928: sensitive_data.host_keys = xmalloc(options.num_host_key_files*sizeof(Key*));
1.217 deraadt 929: for (i = 0; i < options.num_host_key_files; i++)
1.141 markus 930: sensitive_data.host_keys[i] = NULL;
1.134 markus 931: sensitive_data.server_key = NULL;
932: sensitive_data.ssh1_host_key = NULL;
933: sensitive_data.have_ssh1_key = 0;
934: sensitive_data.have_ssh2_key = 0;
935:
1.217 deraadt 936: for (i = 0; i < options.num_host_key_files; i++) {
1.179 markus 937: key = key_load_private(options.host_key_files[i], "", NULL);
938: sensitive_data.host_keys[i] = key;
1.134 markus 939: if (key == NULL) {
1.195 markus 940: error("Could not load host key: %s",
941: options.host_key_files[i]);
1.179 markus 942: sensitive_data.host_keys[i] = NULL;
1.134 markus 943: continue;
944: }
1.214 deraadt 945: switch (key->type) {
1.134 markus 946: case KEY_RSA1:
947: sensitive_data.ssh1_host_key = key;
948: sensitive_data.have_ssh1_key = 1;
949: break;
950: case KEY_RSA:
951: case KEY_DSA:
952: sensitive_data.have_ssh2_key = 1;
953: break;
954: }
1.179 markus 955: debug("private host key: #%d type %d %s", i, key->type,
956: key_type(key));
1.134 markus 957: }
958: if ((options.protocol & SSH_PROTO_1) && !sensitive_data.have_ssh1_key) {
959: log("Disabling protocol version 1. Could not load host key");
1.108 markus 960: options.protocol &= ~SSH_PROTO_1;
961: }
1.134 markus 962: if ((options.protocol & SSH_PROTO_2) && !sensitive_data.have_ssh2_key) {
963: log("Disabling protocol version 2. Could not load host key");
964: options.protocol &= ~SSH_PROTO_2;
1.108 markus 965: }
1.162 stevesk 966: if (!(options.protocol & (SSH_PROTO_1|SSH_PROTO_2))) {
1.172 millert 967: log("sshd: no hostkeys available -- exiting.");
1.64 markus 968: exit(1);
969: }
970:
1.108 markus 971: /* Check certain values for sanity. */
972: if (options.protocol & SSH_PROTO_1) {
973: if (options.server_key_bits < 512 ||
974: options.server_key_bits > 32768) {
975: fprintf(stderr, "Bad server key size.\n");
976: exit(1);
977: }
978: /*
979: * Check that server and host key lengths differ sufficiently. This
980: * is necessary to make double encryption work with rsaref. Oh, I
981: * hate software patents. I dont know if this can go? Niels
982: */
983: if (options.server_key_bits >
1.250 deraadt 984: BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) -
985: SSH_KEY_BITS_RESERVED && options.server_key_bits <
986: BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) +
987: SSH_KEY_BITS_RESERVED) {
1.108 markus 988: options.server_key_bits =
1.250 deraadt 989: BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) +
990: SSH_KEY_BITS_RESERVED;
1.108 markus 991: debug("Forcing server key to %d bits to make it differ from host key.",
992: options.server_key_bits);
993: }
1.244 markus 994: }
995:
996: if (use_privsep) {
997: struct passwd *pw;
998: struct stat st;
999:
1000: if ((pw = getpwnam(SSH_PRIVSEP_USER)) == NULL)
1001: fatal("Privilege separation user %s does not exist",
1002: SSH_PRIVSEP_USER);
1003: if ((stat(_PATH_PRIVSEP_CHROOT_DIR, &st) == -1) ||
1004: (S_ISDIR(st.st_mode) == 0))
1005: fatal("Missing privilege separation directory: %s",
1.247 stevesk 1006: _PATH_PRIVSEP_CHROOT_DIR);
1007: if (st.st_uid != 0 || (st.st_mode & (S_IWGRP|S_IWOTH)) != 0)
1008: fatal("Bad owner or mode for %s",
1.244 markus 1009: _PATH_PRIVSEP_CHROOT_DIR);
1.108 markus 1010: }
1.203 stevesk 1011:
1012: /* Configuration looks good, so exit if in test mode. */
1013: if (test_flag)
1014: exit(0);
1.108 markus 1015:
1016: /* Initialize the log (it is reinitialized below in case we forked). */
1.64 markus 1017: if (debug_flag && !inetd_flag)
1018: log_stderr = 1;
1.138 markus 1019: log_init(__progname, options.log_level, options.log_facility, log_stderr);
1.64 markus 1020:
1.108 markus 1021: /*
1022: * If not in debugging mode, and not started from inetd, disconnect
1023: * from the controlling terminal, and fork. The original process
1024: * exits.
1025: */
1.135 markus 1026: if (!(debug_flag || inetd_flag || no_daemon_flag)) {
1.1 deraadt 1027: #ifdef TIOCNOTTY
1.64 markus 1028: int fd;
1.1 deraadt 1029: #endif /* TIOCNOTTY */
1.64 markus 1030: if (daemon(0, 0) < 0)
1031: fatal("daemon() failed: %.200s", strerror(errno));
1032:
1033: /* Disconnect from the controlling tty. */
1.1 deraadt 1034: #ifdef TIOCNOTTY
1.165 itojun 1035: fd = open(_PATH_TTY, O_RDWR | O_NOCTTY);
1.64 markus 1036: if (fd >= 0) {
1037: (void) ioctl(fd, TIOCNOTTY, NULL);
1038: close(fd);
1039: }
1040: #endif /* TIOCNOTTY */
1041: }
1042: /* Reinitialize the log (because of the fork above). */
1.138 markus 1043: log_init(__progname, options.log_level, options.log_facility, log_stderr);
1.64 markus 1044:
1045: /* Initialize the random number generator. */
1046: arc4random_stir();
1047:
1048: /* Chdir to the root directory so that the current disk can be
1049: unmounted if desired. */
1050: chdir("/");
1.217 deraadt 1051:
1.178 markus 1052: /* ignore SIGPIPE */
1053: signal(SIGPIPE, SIG_IGN);
1.64 markus 1054:
1055: /* Start listening for a socket, unless started from inetd. */
1056: if (inetd_flag) {
1.194 markus 1057: int s1;
1.64 markus 1058: s1 = dup(0); /* Make sure descriptors 0, 1, and 2 are in use. */
1.194 markus 1059: dup(s1);
1.64 markus 1060: sock_in = dup(0);
1061: sock_out = dup(1);
1.123 djm 1062: startup_pipe = -1;
1.108 markus 1063: /*
1064: * We intentionally do not close the descriptors 0, 1, and 2
1065: * as our code for setting the descriptors won\'t work if
1066: * ttyfd happens to be one of those.
1067: */
1.64 markus 1068: debug("inetd sockets after dupping: %d, %d", sock_in, sock_out);
1.134 markus 1069: if (options.protocol & SSH_PROTO_1)
1.174 deraadt 1070: generate_ephemeral_server_key();
1.64 markus 1071: } else {
1.75 markus 1072: for (ai = options.listen_addrs; ai; ai = ai->ai_next) {
1073: if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1074: continue;
1075: if (num_listen_socks >= MAX_LISTEN_SOCKS)
1076: fatal("Too many listen sockets. "
1077: "Enlarge MAX_LISTEN_SOCKS");
1078: if (getnameinfo(ai->ai_addr, ai->ai_addrlen,
1079: ntop, sizeof(ntop), strport, sizeof(strport),
1080: NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
1081: error("getnameinfo failed");
1082: continue;
1083: }
1084: /* Create socket for listening. */
1085: listen_sock = socket(ai->ai_family, SOCK_STREAM, 0);
1086: if (listen_sock < 0) {
1087: /* kernel may not support ipv6 */
1088: verbose("socket: %.100s", strerror(errno));
1089: continue;
1090: }
1091: if (fcntl(listen_sock, F_SETFL, O_NONBLOCK) < 0) {
1092: error("listen_sock O_NONBLOCK: %s", strerror(errno));
1093: close(listen_sock);
1094: continue;
1095: }
1096: /*
1097: * Set socket options. We try to make the port
1098: * reusable and have it close as fast as possible
1099: * without waiting in unnecessary wait states on
1100: * close.
1101: */
1102: setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR,
1.228 stevesk 1103: &on, sizeof(on));
1.75 markus 1104: linger.l_onoff = 1;
1105: linger.l_linger = 5;
1106: setsockopt(listen_sock, SOL_SOCKET, SO_LINGER,
1.228 stevesk 1107: &linger, sizeof(linger));
1.75 markus 1108:
1109: debug("Bind to port %s on %s.", strport, ntop);
1110:
1111: /* Bind the socket to the desired port. */
1112: if (bind(listen_sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1113: error("Bind to port %s on %s failed: %.200s.",
1114: strport, ntop, strerror(errno));
1115: close(listen_sock);
1116: continue;
1117: }
1118: listen_socks[num_listen_socks] = listen_sock;
1119: num_listen_socks++;
1120:
1121: /* Start listening on the port. */
1122: log("Server listening on %s port %s.", ntop, strport);
1123: if (listen(listen_sock, 5) < 0)
1124: fatal("listen: %.100s", strerror(errno));
1125:
1.64 markus 1126: }
1.75 markus 1127: freeaddrinfo(options.listen_addrs);
1128:
1129: if (!num_listen_socks)
1130: fatal("Cannot bind any address.");
1131:
1.201 markus 1132: if (options.protocol & SSH_PROTO_1)
1133: generate_ephemeral_server_key();
1134:
1135: /*
1136: * Arrange to restart on SIGHUP. The handler needs
1137: * listen_sock.
1138: */
1139: signal(SIGHUP, sighup_handler);
1140:
1141: signal(SIGTERM, sigterm_handler);
1142: signal(SIGQUIT, sigterm_handler);
1143:
1144: /* Arrange SIGCHLD to be caught. */
1145: signal(SIGCHLD, main_sigchld_handler);
1146:
1147: /* Write out the pid file after the sigterm handler is setup */
1.64 markus 1148: if (!debug_flag) {
1.66 markus 1149: /*
1.136 todd 1150: * Record our pid in /var/run/sshd.pid to make it
1151: * easier to kill the correct sshd. We don't want to
1152: * do this before the bind above because the bind will
1.66 markus 1153: * fail if there already is a daemon, and this will
1154: * overwrite any old pid in the file.
1155: */
1.112 markus 1156: f = fopen(options.pid_file, "w");
1.64 markus 1157: if (f) {
1.245 mpech 1158: fprintf(f, "%ld\n", (long) getpid());
1.64 markus 1159: fclose(f);
1160: }
1161: }
1162:
1.75 markus 1163: /* setup fd set for listen */
1.120 markus 1164: fdset = NULL;
1.75 markus 1165: maxfd = 0;
1166: for (i = 0; i < num_listen_socks; i++)
1167: if (listen_socks[i] > maxfd)
1168: maxfd = listen_socks[i];
1.120 markus 1169: /* pipes connected to unauthenticated childs */
1170: startup_pipes = xmalloc(options.max_startups * sizeof(int));
1171: for (i = 0; i < options.max_startups; i++)
1172: startup_pipes[i] = -1;
1.75 markus 1173:
1.66 markus 1174: /*
1175: * Stay listening for connections until the system crashes or
1176: * the daemon is killed with a signal.
1177: */
1.64 markus 1178: for (;;) {
1179: if (received_sighup)
1180: sighup_restart();
1.120 markus 1181: if (fdset != NULL)
1182: xfree(fdset);
1.148 markus 1183: fdsetsz = howmany(maxfd+1, NFDBITS) * sizeof(fd_mask);
1.120 markus 1184: fdset = (fd_set *)xmalloc(fdsetsz);
1.75 markus 1185: memset(fdset, 0, fdsetsz);
1.120 markus 1186:
1.75 markus 1187: for (i = 0; i < num_listen_socks; i++)
1188: FD_SET(listen_socks[i], fdset);
1.120 markus 1189: for (i = 0; i < options.max_startups; i++)
1190: if (startup_pipes[i] != -1)
1191: FD_SET(startup_pipes[i], fdset);
1192:
1193: /* Wait in select until there is a connection. */
1.151 markus 1194: ret = select(maxfd+1, fdset, NULL, NULL, NULL);
1195: if (ret < 0 && errno != EINTR)
1196: error("select: %.100s", strerror(errno));
1.199 markus 1197: if (received_sigterm) {
1198: log("Received signal %d; terminating.",
1.213 itojun 1199: (int) received_sigterm);
1.199 markus 1200: close_listen_socks();
1201: unlink(options.pid_file);
1202: exit(255);
1203: }
1.151 markus 1204: if (key_used && key_do_regen) {
1.174 deraadt 1205: generate_ephemeral_server_key();
1.151 markus 1206: key_used = 0;
1207: key_do_regen = 0;
1208: }
1209: if (ret < 0)
1.75 markus 1210: continue;
1.151 markus 1211:
1.120 markus 1212: for (i = 0; i < options.max_startups; i++)
1213: if (startup_pipes[i] != -1 &&
1214: FD_ISSET(startup_pipes[i], fdset)) {
1215: /*
1216: * the read end of the pipe is ready
1217: * if the child has closed the pipe
1.143 markus 1218: * after successful authentication
1.120 markus 1219: * or if the child has died
1220: */
1221: close(startup_pipes[i]);
1222: startup_pipes[i] = -1;
1223: startups--;
1224: }
1.75 markus 1225: for (i = 0; i < num_listen_socks; i++) {
1226: if (!FD_ISSET(listen_socks[i], fdset))
1.70 provos 1227: continue;
1.120 markus 1228: fromlen = sizeof(from);
1229: newsock = accept(listen_socks[i], (struct sockaddr *)&from,
1230: &fromlen);
1231: if (newsock < 0) {
1232: if (errno != EINTR && errno != EWOULDBLOCK)
1233: error("accept: %.100s", strerror(errno));
1234: continue;
1235: }
1236: if (fcntl(newsock, F_SETFL, 0) < 0) {
1237: error("newsock del O_NONBLOCK: %s", strerror(errno));
1.216 stevesk 1238: close(newsock);
1.120 markus 1239: continue;
1240: }
1.124 markus 1241: if (drop_connection(startups) == 1) {
1242: debug("drop connection #%d", startups);
1.120 markus 1243: close(newsock);
1244: continue;
1245: }
1246: if (pipe(startup_p) == -1) {
1247: close(newsock);
1248: continue;
1249: }
1250:
1251: for (j = 0; j < options.max_startups; j++)
1252: if (startup_pipes[j] == -1) {
1253: startup_pipes[j] = startup_p[0];
1254: if (maxfd < startup_p[0])
1255: maxfd = startup_p[0];
1256: startups++;
1257: break;
1258: }
1.161 stevesk 1259:
1.66 markus 1260: /*
1.120 markus 1261: * Got connection. Fork a child to handle it, unless
1262: * we are in debugging mode.
1.66 markus 1263: */
1.120 markus 1264: if (debug_flag) {
1.66 markus 1265: /*
1.120 markus 1266: * In debugging mode. Close the listening
1267: * socket, and start processing the
1268: * connection without forking.
1.66 markus 1269: */
1.120 markus 1270: debug("Server will not fork when running in debugging mode.");
1.75 markus 1271: close_listen_socks();
1.64 markus 1272: sock_in = newsock;
1273: sock_out = newsock;
1.122 deraadt 1274: startup_pipe = -1;
1.120 markus 1275: pid = getpid();
1.64 markus 1276: break;
1.120 markus 1277: } else {
1278: /*
1279: * Normal production daemon. Fork, and have
1280: * the child process the connection. The
1281: * parent continues listening.
1282: */
1283: if ((pid = fork()) == 0) {
1284: /*
1285: * Child. Close the listening and max_startup
1286: * sockets. Start using the accepted socket.
1287: * Reinitialize logging (since our pid has
1288: * changed). We break out of the loop to handle
1289: * the connection.
1290: */
1291: startup_pipe = startup_p[1];
1.211 markus 1292: close_startup_pipes();
1.120 markus 1293: close_listen_socks();
1294: sock_in = newsock;
1295: sock_out = newsock;
1.138 markus 1296: log_init(__progname, options.log_level, options.log_facility, log_stderr);
1.120 markus 1297: break;
1298: }
1.64 markus 1299: }
1300:
1.120 markus 1301: /* Parent. Stay in the loop. */
1302: if (pid < 0)
1303: error("fork: %.100s", strerror(errno));
1304: else
1.245 mpech 1305: debug("Forked child %ld.", (long)pid);
1.120 markus 1306:
1307: close(startup_p[1]);
1.1 deraadt 1308:
1.120 markus 1309: /* Mark that the key has been used (it was "given" to the child). */
1.151 markus 1310: if ((options.protocol & SSH_PROTO_1) &&
1311: key_used == 0) {
1312: /* Schedule server key regeneration alarm. */
1313: signal(SIGALRM, key_regeneration_alarm);
1314: alarm(options.key_regeneration_time);
1315: key_used = 1;
1316: }
1.1 deraadt 1317:
1.120 markus 1318: arc4random_stir();
1.1 deraadt 1319:
1.120 markus 1320: /* Close the new socket (the child is now taking care of it). */
1321: close(newsock);
1322: }
1.75 markus 1323: /* child process check (or debug mode) */
1324: if (num_listen_socks < 0)
1325: break;
1.64 markus 1326: }
1.1 deraadt 1327: }
1328:
1.64 markus 1329: /* This is the child processing a new connection. */
1.241 millert 1330:
1331: /*
1332: * Create a new session and process group since the 4.4BSD
1333: * setlogin() affects the entire process group. We don't
1334: * want the child to be able to affect the parent.
1335: */
1.248 stevesk 1336: if (!debug_flag && !inetd_flag && setsid() < 0)
1.241 millert 1337: error("setsid: %.100s", strerror(errno));
1.64 markus 1338:
1.66 markus 1339: /*
1340: * Disable the key regeneration alarm. We will not regenerate the
1341: * key since we are no longer in a position to give it to anyone. We
1342: * will not restart on SIGHUP since it no longer makes sense.
1343: */
1.64 markus 1344: alarm(0);
1345: signal(SIGALRM, SIG_DFL);
1346: signal(SIGHUP, SIG_DFL);
1347: signal(SIGTERM, SIG_DFL);
1348: signal(SIGQUIT, SIG_DFL);
1349: signal(SIGCHLD, SIG_DFL);
1350:
1.66 markus 1351: /*
1352: * Set socket options for the connection. We want the socket to
1353: * close as fast as possible without waiting for anything. If the
1354: * connection is not a socket, these will do nothing.
1355: */
1356: /* setsockopt(sock_in, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)); */
1.64 markus 1357: linger.l_onoff = 1;
1358: linger.l_linger = 5;
1.228 stevesk 1359: setsockopt(sock_in, SOL_SOCKET, SO_LINGER, &linger, sizeof(linger));
1.150 markus 1360:
1361: /* Set keepalives if requested. */
1362: if (options.keepalives &&
1.228 stevesk 1363: setsockopt(sock_in, SOL_SOCKET, SO_KEEPALIVE, &on,
1.150 markus 1364: sizeof(on)) < 0)
1365: error("setsockopt SO_KEEPALIVE: %.100s", strerror(errno));
1.64 markus 1366:
1.66 markus 1367: /*
1368: * Register our connection. This turns encryption off because we do
1369: * not have a key.
1370: */
1.64 markus 1371: packet_set_connection(sock_in, sock_out);
1.1 deraadt 1372:
1.64 markus 1373: remote_port = get_remote_port();
1374: remote_ip = get_remote_ipaddr();
1.52 markus 1375:
1.209 markus 1376: #ifdef LIBWRAP
1.64 markus 1377: /* Check whether logins are denied from this host. */
1378: {
1379: struct request_info req;
1.37 dugsong 1380:
1.204 camield 1381: request_init(&req, RQ_DAEMON, __progname, RQ_FILE, sock_in, 0);
1.64 markus 1382: fromhost(&req);
1.37 dugsong 1383:
1.64 markus 1384: if (!hosts_access(&req)) {
1.209 markus 1385: debug("Connection refused by tcp wrapper");
1.182 markus 1386: refuse(&req);
1.209 markus 1387: /* NOTREACHED */
1388: fatal("libwrap refuse returns");
1.64 markus 1389: }
1390: }
1.75 markus 1391: #endif /* LIBWRAP */
1.209 markus 1392:
1.64 markus 1393: /* Log the connection. */
1394: verbose("Connection from %.500s port %d", remote_ip, remote_port);
1.1 deraadt 1395:
1.66 markus 1396: /*
1397: * We don\'t want to listen forever unless the other side
1398: * successfully authenticates itself. So we set up an alarm which is
1399: * cleared after successful authentication. A limit of zero
1400: * indicates no limit. Note that we don\'t set the alarm in debugging
1401: * mode; it is just annoying to have the server exit just when you
1402: * are about to discover the bug.
1403: */
1.64 markus 1404: signal(SIGALRM, grace_alarm_handler);
1405: if (!debug_flag)
1406: alarm(options.login_grace_time);
1407:
1.96 markus 1408: sshd_exchange_identification(sock_in, sock_out);
1.66 markus 1409: /*
1.137 markus 1410: * Check that the connection comes from a privileged port.
1.243 deraadt 1411: * Rhosts-Authentication only makes sense from privileged
1.66 markus 1412: * programs. Of course, if the intruder has root access on his local
1413: * machine, he can connect from any port. So do not use these
1414: * authentication methods from machines that you do not trust.
1415: */
1.225 markus 1416: if (options.rhosts_authentication &&
1417: (remote_port >= IPPORT_RESERVED ||
1418: remote_port < IPPORT_RESERVED / 2)) {
1.137 markus 1419: debug("Rhosts Authentication disabled, "
1.207 markus 1420: "originating port %d not trusted.", remote_port);
1.64 markus 1421: options.rhosts_authentication = 0;
1422: }
1.202 dugsong 1423: #if defined(KRB4) && !defined(KRB5)
1.76 markus 1424: if (!packet_connection_is_ipv4() &&
1425: options.kerberos_authentication) {
1426: debug("Kerberos Authentication disabled, only available for IPv4.");
1427: options.kerberos_authentication = 0;
1428: }
1.202 dugsong 1429: #endif /* KRB4 && !KRB5 */
1.164 markus 1430: #ifdef AFS
1431: /* If machine has AFS, set process authentication group. */
1432: if (k_hasafs()) {
1433: k_setpag();
1434: k_unlog();
1435: }
1436: #endif /* AFS */
1.76 markus 1437:
1.64 markus 1438: packet_set_nonblocking();
1.1 deraadt 1439:
1.237 markus 1440: if (use_privsep)
1441: if ((authctxt = privsep_preauth()) != NULL)
1442: goto authenticated;
1.231 provos 1443:
1.77 markus 1444: /* perform the key exchange */
1445: /* authenticate user and start session */
1.98 markus 1446: if (compat20) {
1447: do_ssh2_kex();
1.230 provos 1448: authctxt = do_authentication2();
1.98 markus 1449: } else {
1450: do_ssh1_kex();
1.230 provos 1451: authctxt = do_authentication();
1.98 markus 1452: }
1.237 markus 1453: /*
1454: * If we use privilege separation, the unprivileged child transfers
1455: * the current keystate and exits
1456: */
1457: if (use_privsep) {
1.242 mouring 1458: mm_send_keystate(pmonitor);
1.231 provos 1459: exit(0);
1.237 markus 1460: }
1.231 provos 1461:
1462: authenticated:
1.234 markus 1463: /*
1.231 provos 1464: * In privilege separation, we fork another child and prepare
1465: * file descriptor passing.
1466: */
1467: if (use_privsep) {
1.237 markus 1468: privsep_postauth(authctxt);
1469: /* the monitor process [priv] will not return */
1.231 provos 1470: if (!compat20)
1471: destroy_sensitive_data();
1472: }
1.230 provos 1473:
1474: /* Perform session preparation. */
1475: do_authenticated(authctxt);
1476:
1.64 markus 1477: /* The connection has been terminated. */
1478: verbose("Closing connection to %.100s", remote_ip);
1479: packet_close();
1.231 provos 1480:
1481: if (use_privsep)
1482: mm_terminate();
1483:
1.64 markus 1484: exit(0);
1.1 deraadt 1485: }
1486:
1.65 deraadt 1487: /*
1.229 markus 1488: * Decrypt session_key_int using our private server key and private host key
1489: * (key with larger modulus first).
1490: */
1.231 provos 1491: int
1.229 markus 1492: ssh1_session_key(BIGNUM *session_key_int)
1493: {
1494: int rsafail = 0;
1495:
1496: if (BN_cmp(sensitive_data.server_key->rsa->n, sensitive_data.ssh1_host_key->rsa->n) > 0) {
1497: /* Server key has bigger modulus. */
1498: if (BN_num_bits(sensitive_data.server_key->rsa->n) <
1499: BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
1500: fatal("do_connection: %s: server_key %d < host_key %d + SSH_KEY_BITS_RESERVED %d",
1501: get_remote_ipaddr(),
1502: BN_num_bits(sensitive_data.server_key->rsa->n),
1503: BN_num_bits(sensitive_data.ssh1_host_key->rsa->n),
1504: SSH_KEY_BITS_RESERVED);
1505: }
1506: if (rsa_private_decrypt(session_key_int, session_key_int,
1507: sensitive_data.server_key->rsa) <= 0)
1508: rsafail++;
1509: if (rsa_private_decrypt(session_key_int, session_key_int,
1510: sensitive_data.ssh1_host_key->rsa) <= 0)
1511: rsafail++;
1512: } else {
1513: /* Host key has bigger modulus (or they are equal). */
1514: if (BN_num_bits(sensitive_data.ssh1_host_key->rsa->n) <
1515: BN_num_bits(sensitive_data.server_key->rsa->n) + SSH_KEY_BITS_RESERVED) {
1516: fatal("do_connection: %s: host_key %d < server_key %d + SSH_KEY_BITS_RESERVED %d",
1517: get_remote_ipaddr(),
1518: BN_num_bits(sensitive_data.ssh1_host_key->rsa->n),
1519: BN_num_bits(sensitive_data.server_key->rsa->n),
1520: SSH_KEY_BITS_RESERVED);
1521: }
1522: if (rsa_private_decrypt(session_key_int, session_key_int,
1523: sensitive_data.ssh1_host_key->rsa) < 0)
1524: rsafail++;
1525: if (rsa_private_decrypt(session_key_int, session_key_int,
1526: sensitive_data.server_key->rsa) < 0)
1527: rsafail++;
1528: }
1529: return (rsafail);
1530: }
1531: /*
1.77 markus 1532: * SSH1 key exchange
1.65 deraadt 1533: */
1.200 itojun 1534: static void
1.142 markus 1535: do_ssh1_kex(void)
1.1 deraadt 1536: {
1.64 markus 1537: int i, len;
1.159 markus 1538: int rsafail = 0;
1.64 markus 1539: BIGNUM *session_key_int;
1.140 markus 1540: u_char session_key[SSH_SESSION_KEY_LENGTH];
1541: u_char cookie[8];
1542: u_int cipher_type, auth_mask, protocol_flags;
1.64 markus 1543: u_int32_t rand = 0;
1544:
1.66 markus 1545: /*
1546: * Generate check bytes that the client must send back in the user
1547: * packet in order for it to be accepted; this is used to defy ip
1548: * spoofing attacks. Note that this only works against somebody
1549: * doing IP spoofing from a remote machine; any machine on the local
1550: * network can still see outgoing packets and catch the random
1551: * cookie. This only affects rhosts authentication, and this is one
1552: * of the reasons why it is inherently insecure.
1553: */
1.64 markus 1554: for (i = 0; i < 8; i++) {
1555: if (i % 4 == 0)
1556: rand = arc4random();
1.77 markus 1557: cookie[i] = rand & 0xff;
1.64 markus 1558: rand >>= 8;
1559: }
1560:
1.66 markus 1561: /*
1562: * Send our public key. We include in the packet 64 bits of random
1563: * data that must be matched in the reply in order to prevent IP
1564: * spoofing.
1565: */
1.64 markus 1566: packet_start(SSH_SMSG_PUBLIC_KEY);
1567: for (i = 0; i < 8; i++)
1.77 markus 1568: packet_put_char(cookie[i]);
1.64 markus 1569:
1570: /* Store our public server RSA key. */
1.134 markus 1571: packet_put_int(BN_num_bits(sensitive_data.server_key->rsa->n));
1572: packet_put_bignum(sensitive_data.server_key->rsa->e);
1573: packet_put_bignum(sensitive_data.server_key->rsa->n);
1.64 markus 1574:
1575: /* Store our public host RSA key. */
1.134 markus 1576: packet_put_int(BN_num_bits(sensitive_data.ssh1_host_key->rsa->n));
1577: packet_put_bignum(sensitive_data.ssh1_host_key->rsa->e);
1578: packet_put_bignum(sensitive_data.ssh1_host_key->rsa->n);
1.64 markus 1579:
1580: /* Put protocol flags. */
1581: packet_put_int(SSH_PROTOFLAG_HOST_IN_FWD_OPEN);
1582:
1583: /* Declare which ciphers we support. */
1.131 markus 1584: packet_put_int(cipher_mask_ssh1(0));
1.64 markus 1585:
1586: /* Declare supported authentication types. */
1587: auth_mask = 0;
1588: if (options.rhosts_authentication)
1589: auth_mask |= 1 << SSH_AUTH_RHOSTS;
1590: if (options.rhosts_rsa_authentication)
1591: auth_mask |= 1 << SSH_AUTH_RHOSTS_RSA;
1592: if (options.rsa_authentication)
1593: auth_mask |= 1 << SSH_AUTH_RSA;
1.202 dugsong 1594: #if defined(KRB4) || defined(KRB5)
1.64 markus 1595: if (options.kerberos_authentication)
1596: auth_mask |= 1 << SSH_AUTH_KERBEROS;
1.1 deraadt 1597: #endif
1.202 dugsong 1598: #if defined(AFS) || defined(KRB5)
1.64 markus 1599: if (options.kerberos_tgt_passing)
1600: auth_mask |= 1 << SSH_PASS_KERBEROS_TGT;
1.202 dugsong 1601: #endif
1602: #ifdef AFS
1.64 markus 1603: if (options.afs_token_passing)
1604: auth_mask |= 1 << SSH_PASS_AFS_TOKEN;
1.1 deraadt 1605: #endif
1.196 markus 1606: if (options.challenge_response_authentication == 1)
1.64 markus 1607: auth_mask |= 1 << SSH_AUTH_TIS;
1608: if (options.password_authentication)
1609: auth_mask |= 1 << SSH_AUTH_PASSWORD;
1610: packet_put_int(auth_mask);
1611:
1612: /* Send the packet and wait for it to be sent. */
1613: packet_send();
1614: packet_write_wait();
1615:
1.134 markus 1616: debug("Sent %d bit server key and %d bit host key.",
1617: BN_num_bits(sensitive_data.server_key->rsa->n),
1618: BN_num_bits(sensitive_data.ssh1_host_key->rsa->n));
1.64 markus 1619:
1620: /* Read clients reply (cipher type and session key). */
1.222 markus 1621: packet_read_expect(SSH_CMSG_SESSION_KEY);
1.64 markus 1622:
1.69 markus 1623: /* Get cipher type and check whether we accept this. */
1.64 markus 1624: cipher_type = packet_get_char();
1.69 markus 1625:
1.131 markus 1626: if (!(cipher_mask_ssh1(0) & (1 << cipher_type)))
1.69 markus 1627: packet_disconnect("Warning: client selects unsupported cipher.");
1.64 markus 1628:
1629: /* Get check bytes from the packet. These must match those we
1630: sent earlier with the public key packet. */
1631: for (i = 0; i < 8; i++)
1.77 markus 1632: if (cookie[i] != packet_get_char())
1.64 markus 1633: packet_disconnect("IP Spoofing check bytes do not match.");
1634:
1635: debug("Encryption type: %.200s", cipher_name(cipher_type));
1636:
1637: /* Get the encrypted integer. */
1.218 markus 1638: if ((session_key_int = BN_new()) == NULL)
1639: fatal("do_ssh1_kex: BN_new failed");
1.221 markus 1640: packet_get_bignum(session_key_int);
1.64 markus 1641:
1642: protocol_flags = packet_get_int();
1643: packet_set_protocol_flags(protocol_flags);
1.220 markus 1644: packet_check_eom();
1.64 markus 1645:
1.229 markus 1646: /* Decrypt session_key_int using host/server keys */
1.231 provos 1647: rsafail = PRIVSEP(ssh1_session_key(session_key_int));
1648:
1.66 markus 1649: /*
1650: * Extract session key from the decrypted integer. The key is in the
1651: * least significant 256 bits of the integer; the first byte of the
1652: * key is in the highest bits.
1653: */
1.159 markus 1654: if (!rsafail) {
1655: BN_mask_bits(session_key_int, sizeof(session_key) * 8);
1656: len = BN_num_bytes(session_key_int);
1657: if (len < 0 || len > sizeof(session_key)) {
1658: error("do_connection: bad session key len from %s: "
1.165 itojun 1659: "session_key_int %d > sizeof(session_key) %lu",
1660: get_remote_ipaddr(), len, (u_long)sizeof(session_key));
1.159 markus 1661: rsafail++;
1662: } else {
1663: memset(session_key, 0, sizeof(session_key));
1664: BN_bn2bin(session_key_int,
1665: session_key + sizeof(session_key) - len);
1.169 markus 1666:
1667: compute_session_id(session_id, cookie,
1668: sensitive_data.ssh1_host_key->rsa->n,
1669: sensitive_data.server_key->rsa->n);
1670: /*
1671: * Xor the first 16 bytes of the session key with the
1672: * session id.
1673: */
1674: for (i = 0; i < 16; i++)
1675: session_key[i] ^= session_id[i];
1.159 markus 1676: }
1677: }
1678: if (rsafail) {
1.169 markus 1679: int bytes = BN_num_bytes(session_key_int);
1.227 stevesk 1680: u_char *buf = xmalloc(bytes);
1.169 markus 1681: MD5_CTX md;
1682:
1.159 markus 1683: log("do_connection: generating a fake encryption key");
1.169 markus 1684: BN_bn2bin(session_key_int, buf);
1685: MD5_Init(&md);
1686: MD5_Update(&md, buf, bytes);
1687: MD5_Update(&md, sensitive_data.ssh1_cookie, SSH_SESSION_KEY_LENGTH);
1688: MD5_Final(session_key, &md);
1689: MD5_Init(&md);
1690: MD5_Update(&md, session_key, 16);
1691: MD5_Update(&md, buf, bytes);
1692: MD5_Update(&md, sensitive_data.ssh1_cookie, SSH_SESSION_KEY_LENGTH);
1693: MD5_Final(session_key + 16, &md);
1694: memset(buf, 0, bytes);
1695: xfree(buf);
1.170 markus 1696: for (i = 0; i < 16; i++)
1697: session_id[i] = session_key[i] ^ session_key[i + 16];
1.159 markus 1698: }
1.231 provos 1699: /* Destroy the private and public keys. No longer. */
1.169 markus 1700: destroy_sensitive_data();
1701:
1.231 provos 1702: if (use_privsep)
1703: mm_ssh1_session_id(session_id);
1704:
1.77 markus 1705: /* Destroy the decrypted integer. It is no longer needed. */
1706: BN_clear_free(session_key_int);
1.64 markus 1707:
1708: /* Set the session key. From this on all communications will be encrypted. */
1709: packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, cipher_type);
1710:
1711: /* Destroy our copy of the session key. It is no longer needed. */
1712: memset(session_key, 0, sizeof(session_key));
1713:
1714: debug("Received session key; encryption turned on.");
1715:
1.243 deraadt 1716: /* Send an acknowledgment packet. Note that this packet is sent encrypted. */
1.64 markus 1717: packet_start(SSH_SMSG_SUCCESS);
1718: packet_send();
1719: packet_write_wait();
1.98 markus 1720: }
1721:
1722: /*
1723: * SSH2 key exchange: diffie-hellman-group1-sha1
1724: */
1.200 itojun 1725: static void
1.142 markus 1726: do_ssh2_kex(void)
1.98 markus 1727: {
1728: Kex *kex;
1.102 markus 1729:
1730: if (options.ciphers != NULL) {
1.105 markus 1731: myproposal[PROPOSAL_ENC_ALGS_CTOS] =
1.102 markus 1732: myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
1.166 markus 1733: }
1.184 stevesk 1734: myproposal[PROPOSAL_ENC_ALGS_CTOS] =
1735: compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
1736: myproposal[PROPOSAL_ENC_ALGS_STOC] =
1737: compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
1738:
1.166 markus 1739: if (options.macs != NULL) {
1740: myproposal[PROPOSAL_MAC_ALGS_CTOS] =
1741: myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
1.246 markus 1742: }
1743: if (!options.compression) {
1744: myproposal[PROPOSAL_COMP_ALGS_CTOS] =
1745: myproposal[PROPOSAL_COMP_ALGS_STOC] = "none";
1.102 markus 1746: }
1.134 markus 1747: myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = list_hostkey_types();
1748:
1.189 markus 1749: /* start key exchange */
1.188 markus 1750: kex = kex_setup(myproposal);
1.186 markus 1751: kex->server = 1;
1752: kex->client_version_string=client_version_string;
1753: kex->server_version_string=server_version_string;
1754: kex->load_host_key=&get_hostkey_by_type;
1.231 provos 1755: kex->host_key_index=&get_hostkey_index;
1.129 provos 1756:
1.189 markus 1757: xxx_kex = kex;
1758:
1.190 markus 1759: dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
1.187 markus 1760:
1761: session_id2 = kex->session_id;
1762: session_id2_len = kex->session_id_len;
1.129 provos 1763:
1764: #ifdef DEBUG_KEXDH
1765: /* send 1st encrypted/maced/compressed message */
1766: packet_start(SSH2_MSG_IGNORE);
1767: packet_put_cstring("markus");
1768: packet_send();
1769: packet_write_wait();
1770: #endif
1.186 markus 1771: debug("KEX done");
1.1 deraadt 1772: }