summaryrefslogtreecommitdiff
path: root/sshd.c
diff options
context:
space:
mode:
Diffstat (limited to 'sshd.c')
-rw-r--r--sshd.c181
1 files changed, 67 insertions, 114 deletions
diff --git a/sshd.c b/sshd.c
index 0f2b2a3ce..47df9caf1 100644
--- a/sshd.c
+++ b/sshd.c
@@ -42,7 +42,7 @@
42 */ 42 */
43 43
44#include "includes.h" 44#include "includes.h"
45RCSID("$OpenBSD: sshd.c,v 1.263 2003/02/16 17:09:57 markus Exp $"); 45RCSID("$OpenBSD: sshd.c,v 1.276 2003/08/28 12:54:34 markus Exp $");
46 46
47#include <openssl/dh.h> 47#include <openssl/dh.h>
48#include <openssl/bn.h> 48#include <openssl/bn.h>
@@ -112,11 +112,7 @@ char *config_file_name = _PATH_SERVER_CONFIG_FILE;
112 * Flag indicating whether IPv4 or IPv6. This can be set on the command line. 112 * Flag indicating whether IPv4 or IPv6. This can be set on the command line.
113 * Default value is AF_UNSPEC means both IPv4 and IPv6. 113 * Default value is AF_UNSPEC means both IPv4 and IPv6.
114 */ 114 */
115#ifdef IPV4_DEFAULT
116int IPv4or6 = AF_INET;
117#else
118int IPv4or6 = AF_UNSPEC; 115int IPv4or6 = AF_UNSPEC;
119#endif
120 116
121/* 117/*
122 * Debug mode flag. This can be set on the command line. If debug 118 * Debug mode flag. This can be set on the command line. If debug
@@ -192,7 +188,7 @@ u_char session_id[16];
192 188
193/* same for ssh2 */ 189/* same for ssh2 */
194u_char *session_id2 = NULL; 190u_char *session_id2 = NULL;
195int session_id2_len = 0; 191u_int session_id2_len = 0;
196 192
197/* record remote hostname or ip */ 193/* record remote hostname or ip */
198u_int utmp_len = MAXHOSTNAMELEN; 194u_int utmp_len = MAXHOSTNAMELEN;
@@ -205,6 +201,9 @@ int startup_pipe; /* in child */
205int use_privsep; 201int use_privsep;
206struct monitor *pmonitor; 202struct monitor *pmonitor;
207 203
204/* message to be displayed after login */
205Buffer loginmsg;
206
208/* Prototypes for various functions defined later in this file. */ 207/* Prototypes for various functions defined later in this file. */
209void destroy_sensitive_data(void); 208void destroy_sensitive_data(void);
210void demote_sensitive_data(void); 209void demote_sensitive_data(void);
@@ -258,11 +257,11 @@ sighup_handler(int sig)
258static void 257static void
259sighup_restart(void) 258sighup_restart(void)
260{ 259{
261 log("Received SIGHUP; restarting."); 260 logit("Received SIGHUP; restarting.");
262 close_listen_socks(); 261 close_listen_socks();
263 close_startup_pipes(); 262 close_startup_pipes();
264 execv(saved_argv[0], saved_argv); 263 execv(saved_argv[0], saved_argv);
265 log("RESTART FAILED: av[0]='%.100s', error: %.100s.", saved_argv[0], 264 logit("RESTART FAILED: av[0]='%.100s', error: %.100s.", saved_argv[0],
266 strerror(errno)); 265 strerror(errno));
267 exit(1); 266 exit(1);
268} 267}
@@ -371,39 +370,37 @@ sshd_exchange_identification(int sock_in, int sock_out)
371 snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n", major, minor, SSH_VERSION); 370 snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n", major, minor, SSH_VERSION);
372 server_version_string = xstrdup(buf); 371 server_version_string = xstrdup(buf);
373 372
374 if (client_version_string == NULL) { 373 /* Send our protocol version identification. */
375 /* Send our protocol version identification. */ 374 if (atomicio(vwrite, sock_out, server_version_string,
376 if (atomicio(write, sock_out, server_version_string, 375 strlen(server_version_string))
377 strlen(server_version_string)) 376 != strlen(server_version_string)) {
378 != strlen(server_version_string)) { 377 logit("Could not write ident string to %s", get_remote_ipaddr());
379 log("Could not write ident string to %s", get_remote_ipaddr()); 378 fatal_cleanup();
379 }
380
381 /* Read other sides version identification. */
382 memset(buf, 0, sizeof(buf));
383 for (i = 0; i < sizeof(buf) - 1; i++) {
384 if (atomicio(read, sock_in, &buf[i], 1) != 1) {
385 logit("Did not receive identification string from %s",
386 get_remote_ipaddr());
380 fatal_cleanup(); 387 fatal_cleanup();
381 } 388 }
382 389 if (buf[i] == '\r') {
383 /* Read other sides version identification. */ 390 buf[i] = 0;
384 memset(buf, 0, sizeof(buf)); 391 /* Kludge for F-Secure Macintosh < 1.0.2 */
385 for (i = 0; i < sizeof(buf) - 1; i++) { 392 if (i == 12 &&
386 if (atomicio(read, sock_in, &buf[i], 1) != 1) { 393 strncmp(buf, "SSH-1.5-W1.0", 12) == 0)
387 log("Did not receive identification string from %s",
388 get_remote_ipaddr());
389 fatal_cleanup();
390 }
391 if (buf[i] == '\r') {
392 buf[i] = 0;
393 /* Kludge for F-Secure Macintosh < 1.0.2 */
394 if (i == 12 &&
395 strncmp(buf, "SSH-1.5-W1.0", 12) == 0)
396 break;
397 continue;
398 }
399 if (buf[i] == '\n') {
400 buf[i] = 0;
401 break; 394 break;
402 } 395 continue;
396 }
397 if (buf[i] == '\n') {
398 buf[i] = 0;
399 break;
403 } 400 }
404 buf[sizeof(buf) - 1] = 0;
405 client_version_string = xstrdup(buf);
406 } 401 }
402 buf[sizeof(buf) - 1] = 0;
403 client_version_string = xstrdup(buf);
407 404
408 /* 405 /*
409 * Check that the versions match. In future this might accept 406 * Check that the versions match. In future this might accept
@@ -412,10 +409,10 @@ sshd_exchange_identification(int sock_in, int sock_out)
412 if (sscanf(client_version_string, "SSH-%d.%d-%[^\n]\n", 409 if (sscanf(client_version_string, "SSH-%d.%d-%[^\n]\n",
413 &remote_major, &remote_minor, remote_version) != 3) { 410 &remote_major, &remote_minor, remote_version) != 3) {
414 s = "Protocol mismatch.\n"; 411 s = "Protocol mismatch.\n";
415 (void) atomicio(write, sock_out, s, strlen(s)); 412 (void) atomicio(vwrite, sock_out, s, strlen(s));
416 close(sock_in); 413 close(sock_in);
417 close(sock_out); 414 close(sock_out);
418 log("Bad protocol version identification '%.100s' from %s", 415 logit("Bad protocol version identification '%.100s' from %s",
419 client_version_string, get_remote_ipaddr()); 416 client_version_string, get_remote_ipaddr());
420 fatal_cleanup(); 417 fatal_cleanup();
421 } 418 }
@@ -425,13 +422,13 @@ sshd_exchange_identification(int sock_in, int sock_out)
425 compat_datafellows(remote_version); 422 compat_datafellows(remote_version);
426 423
427 if (datafellows & SSH_BUG_PROBE) { 424 if (datafellows & SSH_BUG_PROBE) {
428 log("probed from %s with %s. Don't panic.", 425 logit("probed from %s with %s. Don't panic.",
429 get_remote_ipaddr(), client_version_string); 426 get_remote_ipaddr(), client_version_string);
430 fatal_cleanup(); 427 fatal_cleanup();
431 } 428 }
432 429
433 if (datafellows & SSH_BUG_SCANNER) { 430 if (datafellows & SSH_BUG_SCANNER) {
434 log("scanned from %s with %s. Don't panic.", 431 logit("scanned from %s with %s. Don't panic.",
435 get_remote_ipaddr(), client_version_string); 432 get_remote_ipaddr(), client_version_string);
436 fatal_cleanup(); 433 fatal_cleanup();
437 } 434 }
@@ -473,10 +470,10 @@ sshd_exchange_identification(int sock_in, int sock_out)
473 470
474 if (mismatch) { 471 if (mismatch) {
475 s = "Protocol major versions differ.\n"; 472 s = "Protocol major versions differ.\n";
476 (void) atomicio(write, sock_out, s, strlen(s)); 473 (void) atomicio(vwrite, sock_out, s, strlen(s));
477 close(sock_in); 474 close(sock_in);
478 close(sock_out); 475 close(sock_out);
479 log("Protocol major versions differ for %s: %.200s vs. %.200s", 476 logit("Protocol major versions differ for %s: %.200s vs. %.200s",
480 get_remote_ipaddr(), 477 get_remote_ipaddr(),
481 server_version_string, client_version_string); 478 server_version_string, client_version_string);
482 fatal_cleanup(); 479 fatal_cleanup();
@@ -568,8 +565,6 @@ privsep_preauth_child(void)
568 do_setusercontext(pw); 565 do_setusercontext(pw);
569#else 566#else
570 gidset[0] = pw->pw_gid; 567 gidset[0] = pw->pw_gid;
571 if (setgid(pw->pw_gid) < 0)
572 fatal("setgid failed for %u", pw->pw_gid );
573 if (setgroups(1, gidset) < 0) 568 if (setgroups(1, gidset) < 0)
574 fatal("setgroups: %.100s", strerror(errno)); 569 fatal("setgroups: %.100s", strerror(errno));
575 permanently_set_uid(pw); 570 permanently_set_uid(pw);
@@ -824,26 +819,27 @@ main(int ac, char **av)
824#ifdef HAVE_SECUREWARE 819#ifdef HAVE_SECUREWARE
825 (void)set_auth_parameters(ac, av); 820 (void)set_auth_parameters(ac, av);
826#endif 821#endif
827 __progname = get_progname(av[0]); 822 __progname = ssh_get_progname(av[0]);
828 init_rng(); 823 init_rng();
829 824
830 /* Save argv. Duplicate so setproctitle emulation doesn't clobber it */ 825 /* Save argv. Duplicate so setproctitle emulation doesn't clobber it */
831 saved_argc = ac; 826 saved_argc = ac;
832 saved_argv = av; 827 saved_argv = xmalloc(sizeof(*saved_argv) * (ac + 1));
833 saved_argv = xmalloc(sizeof(*saved_argv) * ac);
834 for (i = 0; i < ac; i++) 828 for (i = 0; i < ac; i++)
835 saved_argv[i] = xstrdup(av[i]); 829 saved_argv[i] = xstrdup(av[i]);
830 saved_argv[i] = NULL;
836 831
837#ifndef HAVE_SETPROCTITLE 832#ifndef HAVE_SETPROCTITLE
838 /* Prepare for later setproctitle emulation */ 833 /* Prepare for later setproctitle emulation */
839 compat_init_setproctitle(ac, av); 834 compat_init_setproctitle(ac, av);
835 av = saved_argv;
840#endif 836#endif
841 837
842 /* Initialize configuration options to their default values. */ 838 /* Initialize configuration options to their default values. */
843 initialize_server_options(&options); 839 initialize_server_options(&options);
844 840
845 /* Parse command-line arguments. */ 841 /* Parse command-line arguments. */
846 while ((opt = getopt(ac, av, "f:p:b:k:h:g:V:u:o:dDeiqtQ46")) != -1) { 842 while ((opt = getopt(ac, av, "f:p:b:k:h:g:u:o:dDeiqtQ46")) != -1) {
847 switch (opt) { 843 switch (opt) {
848 case '4': 844 case '4':
849 IPv4or6 = AF_INET; 845 IPv4or6 = AF_INET;
@@ -855,15 +851,11 @@ main(int ac, char **av)
855 config_file_name = optarg; 851 config_file_name = optarg;
856 break; 852 break;
857 case 'd': 853 case 'd':
858 if (0 == debug_flag) { 854 if (debug_flag == 0) {
859 debug_flag = 1; 855 debug_flag = 1;
860 options.log_level = SYSLOG_LEVEL_DEBUG1; 856 options.log_level = SYSLOG_LEVEL_DEBUG1;
861 } else if (options.log_level < SYSLOG_LEVEL_DEBUG3) { 857 } else if (options.log_level < SYSLOG_LEVEL_DEBUG3)
862 options.log_level++; 858 options.log_level++;
863 } else {
864 fprintf(stderr, "Too high debugging level.\n");
865 exit(1);
866 }
867 break; 859 break;
868 case 'D': 860 case 'D':
869 no_daemon_flag = 1; 861 no_daemon_flag = 1;
@@ -914,11 +906,6 @@ main(int ac, char **av)
914 } 906 }
915 options.host_key_files[options.num_host_key_files++] = optarg; 907 options.host_key_files[options.num_host_key_files++] = optarg;
916 break; 908 break;
917 case 'V':
918 client_version_string = optarg;
919 /* only makes sense with inetd_flag, i.e. no listen() */
920 inetd_flag = 1;
921 break;
922 case 't': 909 case 't':
923 test_flag = 1; 910 test_flag = 1;
924 break; 911 break;
@@ -1010,15 +997,15 @@ main(int ac, char **av)
1010 key_type(key)); 997 key_type(key));
1011 } 998 }
1012 if ((options.protocol & SSH_PROTO_1) && !sensitive_data.have_ssh1_key) { 999 if ((options.protocol & SSH_PROTO_1) && !sensitive_data.have_ssh1_key) {
1013 log("Disabling protocol version 1. Could not load host key"); 1000 logit("Disabling protocol version 1. Could not load host key");
1014 options.protocol &= ~SSH_PROTO_1; 1001 options.protocol &= ~SSH_PROTO_1;
1015 } 1002 }
1016 if ((options.protocol & SSH_PROTO_2) && !sensitive_data.have_ssh2_key) { 1003 if ((options.protocol & SSH_PROTO_2) && !sensitive_data.have_ssh2_key) {
1017 log("Disabling protocol version 2. Could not load host key"); 1004 logit("Disabling protocol version 2. Could not load host key");
1018 options.protocol &= ~SSH_PROTO_2; 1005 options.protocol &= ~SSH_PROTO_2;
1019 } 1006 }
1020 if (!(options.protocol & (SSH_PROTO_1|SSH_PROTO_2))) { 1007 if (!(options.protocol & (SSH_PROTO_1|SSH_PROTO_2))) {
1021 log("sshd: no hostkeys available -- exiting."); 1008 logit("sshd: no hostkeys available -- exiting.");
1022 exit(1); 1009 exit(1);
1023 } 1010 }
1024 1011
@@ -1153,7 +1140,8 @@ main(int ac, char **av)
1153 continue; 1140 continue;
1154 } 1141 }
1155 /* Create socket for listening. */ 1142 /* Create socket for listening. */
1156 listen_sock = socket(ai->ai_family, SOCK_STREAM, 0); 1143 listen_sock = socket(ai->ai_family, ai->ai_socktype,
1144 ai->ai_protocol);
1157 if (listen_sock < 0) { 1145 if (listen_sock < 0) {
1158 /* kernel may not support ipv6 */ 1146 /* kernel may not support ipv6 */
1159 verbose("socket: %.100s", strerror(errno)); 1147 verbose("socket: %.100s", strerror(errno));
@@ -1186,7 +1174,7 @@ main(int ac, char **av)
1186 num_listen_socks++; 1174 num_listen_socks++;
1187 1175
1188 /* Start listening on the port. */ 1176 /* Start listening on the port. */
1189 log("Server listening on %s port %s.", ntop, strport); 1177 logit("Server listening on %s port %s.", ntop, strport);
1190 if (listen(listen_sock, 5) < 0) 1178 if (listen(listen_sock, 5) < 0)
1191 fatal("listen: %.100s", strerror(errno)); 1179 fatal("listen: %.100s", strerror(errno));
1192 1180
@@ -1221,7 +1209,10 @@ main(int ac, char **av)
1221 * overwrite any old pid in the file. 1209 * overwrite any old pid in the file.
1222 */ 1210 */
1223 f = fopen(options.pid_file, "wb"); 1211 f = fopen(options.pid_file, "wb");
1224 if (f) { 1212 if (f == NULL) {
1213 error("Couldn't create pid file \"%s\": %s",
1214 options.pid_file, strerror(errno));
1215 } else {
1225 fprintf(f, "%ld\n", (long) getpid()); 1216 fprintf(f, "%ld\n", (long) getpid());
1226 fclose(f); 1217 fclose(f);
1227 } 1218 }
@@ -1262,7 +1253,7 @@ main(int ac, char **av)
1262 if (ret < 0 && errno != EINTR) 1253 if (ret < 0 && errno != EINTR)
1263 error("select: %.100s", strerror(errno)); 1254 error("select: %.100s", strerror(errno));
1264 if (received_sigterm) { 1255 if (received_sigterm) {
1265 log("Received signal %d; terminating.", 1256 logit("Received signal %d; terminating.",
1266 (int) received_sigterm); 1257 (int) received_sigterm);
1267 close_listen_socks(); 1258 close_listen_socks();
1268 unlink(options.pid_file); 1259 unlink(options.pid_file);
@@ -1400,11 +1391,11 @@ main(int ac, char **av)
1400 * setlogin() affects the entire process group. We don't 1391 * setlogin() affects the entire process group. We don't
1401 * want the child to be able to affect the parent. 1392 * want the child to be able to affect the parent.
1402 */ 1393 */
1403#if !defined(STREAMS_PUSH_ACQUIRES_CTTY) 1394#if !defined(SSHD_ACQUIRES_CTTY)
1404 /* 1395 /*
1405 * If setsid is called on Solaris, sshd will acquire the controlling 1396 * If setsid is called, on some platforms sshd will later acquire a
1406 * terminal while pushing STREAMS modules. This will prevent the 1397 * controlling terminal which will result in "could not set
1407 * shell from acquiring it later. 1398 * controlling tty" errors.
1408 */ 1399 */
1409 if (!debug_flag && !inetd_flag && setsid() < 0) 1400 if (!debug_flag && !inetd_flag && setsid() < 0)
1410 error("setsid: %.100s", strerror(errno)); 1401 error("setsid: %.100s", strerror(errno));
@@ -1471,37 +1462,12 @@ main(int ac, char **av)
1471 alarm(options.login_grace_time); 1462 alarm(options.login_grace_time);
1472 1463
1473 sshd_exchange_identification(sock_in, sock_out); 1464 sshd_exchange_identification(sock_in, sock_out);
1474 /*
1475 * Check that the connection comes from a privileged port.
1476 * Rhosts-Authentication only makes sense from privileged
1477 * programs. Of course, if the intruder has root access on his local
1478 * machine, he can connect from any port. So do not use these
1479 * authentication methods from machines that you do not trust.
1480 */
1481 if (options.rhosts_authentication &&
1482 (remote_port >= IPPORT_RESERVED ||
1483 remote_port < IPPORT_RESERVED / 2)) {
1484 debug("Rhosts Authentication disabled, "
1485 "originating port %d not trusted.", remote_port);
1486 options.rhosts_authentication = 0;
1487 }
1488#if defined(KRB4) && !defined(KRB5)
1489 if (!packet_connection_is_ipv4() &&
1490 options.kerberos_authentication) {
1491 debug("Kerberos Authentication disabled, only available for IPv4.");
1492 options.kerberos_authentication = 0;
1493 }
1494#endif /* KRB4 && !KRB5 */
1495#ifdef AFS
1496 /* If machine has AFS, set process authentication group. */
1497 if (k_hasafs()) {
1498 k_setpag();
1499 k_unlog();
1500 }
1501#endif /* AFS */
1502 1465
1503 packet_set_nonblocking(); 1466 packet_set_nonblocking();
1504 1467
1468 /* prepare buffers to collect authentication messages */
1469 buffer_init(&loginmsg);
1470
1505 if (use_privsep) 1471 if (use_privsep)
1506 if ((authctxt = privsep_preauth()) != NULL) 1472 if ((authctxt = privsep_preauth()) != NULL)
1507 goto authenticated; 1473 goto authenticated;
@@ -1543,7 +1509,8 @@ main(int ac, char **av)
1543 verbose("Closing connection to %.100s", remote_ip); 1509 verbose("Closing connection to %.100s", remote_ip);
1544 1510
1545#ifdef USE_PAM 1511#ifdef USE_PAM
1546 finish_pam(); 1512 if (options.use_pam)
1513 finish_pam();
1547#endif /* USE_PAM */ 1514#endif /* USE_PAM */
1548 1515
1549 packet_close(); 1516 packet_close();
@@ -1655,24 +1622,10 @@ do_ssh1_kex(void)
1655 1622
1656 /* Declare supported authentication types. */ 1623 /* Declare supported authentication types. */
1657 auth_mask = 0; 1624 auth_mask = 0;
1658 if (options.rhosts_authentication)
1659 auth_mask |= 1 << SSH_AUTH_RHOSTS;
1660 if (options.rhosts_rsa_authentication) 1625 if (options.rhosts_rsa_authentication)
1661 auth_mask |= 1 << SSH_AUTH_RHOSTS_RSA; 1626 auth_mask |= 1 << SSH_AUTH_RHOSTS_RSA;
1662 if (options.rsa_authentication) 1627 if (options.rsa_authentication)
1663 auth_mask |= 1 << SSH_AUTH_RSA; 1628 auth_mask |= 1 << SSH_AUTH_RSA;
1664#if defined(KRB4) || defined(KRB5)
1665 if (options.kerberos_authentication)
1666 auth_mask |= 1 << SSH_AUTH_KERBEROS;
1667#endif
1668#if defined(AFS) || defined(KRB5)
1669 if (options.kerberos_tgt_passing)
1670 auth_mask |= 1 << SSH_PASS_KERBEROS_TGT;
1671#endif
1672#ifdef AFS
1673 if (options.afs_token_passing)
1674 auth_mask |= 1 << SSH_PASS_AFS_TOKEN;
1675#endif
1676 if (options.challenge_response_authentication == 1) 1629 if (options.challenge_response_authentication == 1)
1677 auth_mask |= 1 << SSH_AUTH_TIS; 1630 auth_mask |= 1 << SSH_AUTH_TIS;
1678 if (options.password_authentication) 1631 if (options.password_authentication)
@@ -1750,7 +1703,7 @@ do_ssh1_kex(void)
1750 u_char *buf = xmalloc(bytes); 1703 u_char *buf = xmalloc(bytes);
1751 MD5_CTX md; 1704 MD5_CTX md;
1752 1705
1753 log("do_connection: generating a fake encryption key"); 1706 logit("do_connection: generating a fake encryption key");
1754 BN_bn2bin(session_key_int, buf); 1707 BN_bn2bin(session_key_int, buf);
1755 MD5_Init(&md); 1708 MD5_Init(&md);
1756 MD5_Update(&md, buf, bytes); 1709 MD5_Update(&md, buf, bytes);