]> andersk Git - openssh.git/blobdiff - sshd.c
- markus@cvs.openbsd.org 2003/09/26 08:19:29
[openssh.git] / sshd.c
diff --git a/sshd.c b/sshd.c
index e73135c7b0863c7497ac803996c3829bce7a69d5..36f34ff5809080a5fee092f01de7851f7b34f57c 100644 (file)
--- a/sshd.c
+++ b/sshd.c
@@ -42,7 +42,7 @@
  */
 
 #include "includes.h"
-RCSID("$OpenBSD: sshd.c,v 1.267 2003/05/29 16:58:45 deraadt Exp $");
+RCSID("$OpenBSD: sshd.c,v 1.279 2003/09/26 08:19:29 markus Exp $");
 
 #include <openssl/dh.h>
 #include <openssl/bn.h>
@@ -188,7 +188,7 @@ u_char session_id[16];
 
 /* same for ssh2 */
 u_char *session_id2 = NULL;
-int session_id2_len = 0;
+u_int session_id2_len = 0;
 
 /* record remote hostname or ip */
 u_int utmp_len = MAXHOSTNAMELEN;
@@ -201,6 +201,12 @@ int startup_pipe;          /* in child */
 int use_privsep;
 struct monitor *pmonitor;
 
+/* message to be displayed after login */
+Buffer loginmsg;
+
+/* global authentication context */
+Authctxt *the_authctxt = NULL;
+
 /* Prototypes for various functions defined later in this file. */
 void destroy_sensitive_data(void);
 void demote_sensitive_data(void);
@@ -367,39 +373,37 @@ sshd_exchange_identification(int sock_in, int sock_out)
        snprintf(buf, sizeof buf, "SSH-%d.%d-%.100s\n", major, minor, SSH_VERSION);
        server_version_string = xstrdup(buf);
 
-       if (client_version_string == NULL) {
-               /* Send our protocol version identification. */
-               if (atomicio(write, sock_out, server_version_string,
-                   strlen(server_version_string))
-                   != strlen(server_version_string)) {
-                       logit("Could not write ident string to %s", get_remote_ipaddr());
-                       fatal_cleanup();
-               }
+       /* Send our protocol version identification. */
+       if (atomicio(vwrite, sock_out, server_version_string,
+           strlen(server_version_string))
+           != strlen(server_version_string)) {
+               logit("Could not write ident string to %s", get_remote_ipaddr());
+               cleanup_exit(255);
+       }
 
-               /* Read other sides version identification. */
-               memset(buf, 0, sizeof(buf));
-               for (i = 0; i < sizeof(buf) - 1; i++) {
-                       if (atomicio(read, sock_in, &buf[i], 1) != 1) {
-                               logit("Did not receive identification string from %s",
-                                   get_remote_ipaddr());
-                               fatal_cleanup();
-                       }
-                       if (buf[i] == '\r') {
-                               buf[i] = 0;
-                               /* Kludge for F-Secure Macintosh < 1.0.2 */
-                               if (i == 12 &&
-                                   strncmp(buf, "SSH-1.5-W1.0", 12) == 0)
-                                       break;
-                               continue;
-                       }
-                       if (buf[i] == '\n') {
-                               buf[i] = 0;
+       /* Read other sides version identification. */
+       memset(buf, 0, sizeof(buf));
+       for (i = 0; i < sizeof(buf) - 1; i++) {
+               if (atomicio(read, sock_in, &buf[i], 1) != 1) {
+                       logit("Did not receive identification string from %s",
+                           get_remote_ipaddr());
+                       cleanup_exit(255);
+               }
+               if (buf[i] == '\r') {
+                       buf[i] = 0;
+                       /* Kludge for F-Secure Macintosh < 1.0.2 */
+                       if (i == 12 &&
+                           strncmp(buf, "SSH-1.5-W1.0", 12) == 0)
                                break;
-                       }
+                       continue;
+               }
+               if (buf[i] == '\n') {
+                       buf[i] = 0;
+                       break;
                }
-               buf[sizeof(buf) - 1] = 0;
-               client_version_string = xstrdup(buf);
        }
+       buf[sizeof(buf) - 1] = 0;
+       client_version_string = xstrdup(buf);
 
        /*
         * Check that the versions match.  In future this might accept
@@ -408,12 +412,12 @@ sshd_exchange_identification(int sock_in, int sock_out)
        if (sscanf(client_version_string, "SSH-%d.%d-%[^\n]\n",
            &remote_major, &remote_minor, remote_version) != 3) {
                s = "Protocol mismatch.\n";
-               (void) atomicio(write, sock_out, s, strlen(s));
+               (void) atomicio(vwrite, sock_out, s, strlen(s));
                close(sock_in);
                close(sock_out);
                logit("Bad protocol version identification '%.100s' from %s",
                    client_version_string, get_remote_ipaddr());
-               fatal_cleanup();
+               cleanup_exit(255);
        }
        debug("Client protocol version %d.%d; client software version %.100s",
            remote_major, remote_minor, remote_version);
@@ -423,13 +427,13 @@ sshd_exchange_identification(int sock_in, int sock_out)
        if (datafellows & SSH_BUG_PROBE) {
                logit("probed from %s with %s.  Don't panic.",
                    get_remote_ipaddr(), client_version_string);
-               fatal_cleanup();
+               cleanup_exit(255);
        }
 
        if (datafellows & SSH_BUG_SCANNER) {
                logit("scanned from %s with %s.  Don't panic.",
                    get_remote_ipaddr(), client_version_string);
-               fatal_cleanup();
+               cleanup_exit(255);
        }
 
        mismatch = 0;
@@ -469,13 +473,13 @@ sshd_exchange_identification(int sock_in, int sock_out)
 
        if (mismatch) {
                s = "Protocol major versions differ.\n";
-               (void) atomicio(write, sock_out, s, strlen(s));
+               (void) atomicio(vwrite, sock_out, s, strlen(s));
                close(sock_in);
                close(sock_out);
                logit("Protocol major versions differ for %s: %.200s vs. %.200s",
                    get_remote_ipaddr(),
                    server_version_string, client_version_string);
-               fatal_cleanup();
+               cleanup_exit(255);
        }
 }
 
@@ -564,20 +568,15 @@ privsep_preauth_child(void)
        do_setusercontext(pw);
 #else
        gidset[0] = pw->pw_gid;
-       if (setegid(pw->pw_gid) < 0)
-               fatal("setegid failed for %u", (u_int)pw->pw_gid);
-       if (setgid(pw->pw_gid) < 0)
-               fatal("setgid failed for %u", (u_int)pw->pw_gid);
        if (setgroups(1, gidset) < 0)
                fatal("setgroups: %.100s", strerror(errno));
        permanently_set_uid(pw);
 #endif
 }
 
-static Authctxt *
-privsep_preauth(void)
+static int
+privsep_preauth(Authctxt *authctxt)
 {
-       Authctxt *authctxt = NULL;
        int status;
        pid_t pid;
 
@@ -590,12 +589,10 @@ privsep_preauth(void)
        if (pid == -1) {
                fatal("fork of unprivileged child failed");
        } else if (pid != 0) {
-               fatal_remove_cleanup((void (*) (void *)) packet_close, NULL);
-
                debug2("Network child is on pid %ld", (long)pid);
 
                close(pmonitor->m_recvfd);
-               authctxt = monitor_child_preauth(pmonitor);
+               monitor_child_preauth(authctxt, pmonitor);
                close(pmonitor->m_sendfd);
 
                /* Sync memory */
@@ -605,11 +602,7 @@ privsep_preauth(void)
                while (waitpid(pid, &status, 0) < 0)
                        if (errno != EINTR)
                                break;
-
-               /* Reinstall, since the child has finished */
-               fatal_add_cleanup((void (*) (void *)) packet_close, NULL);
-
-               return (authctxt);
+               return (1);
        } else {
                /* child */
 
@@ -620,17 +613,12 @@ privsep_preauth(void)
                        privsep_preauth_child();
                setproctitle("%s", "[net]");
        }
-       return (NULL);
+       return (0);
 }
 
 static void
 privsep_postauth(Authctxt *authctxt)
 {
-       extern Authctxt *x_authctxt;
-
-       /* XXX - Remote port forwarding */
-       x_authctxt = authctxt;
-
 #ifdef DISABLE_FD_PASSING
        if (1) {
 #else
@@ -656,8 +644,6 @@ privsep_postauth(Authctxt *authctxt)
        if (pmonitor->m_pid == -1)
                fatal("fork of unprivileged child failed");
        else if (pmonitor->m_pid != 0) {
-               fatal_remove_cleanup((void (*) (void *)) packet_close, NULL);
-
                debug2("User child is on pid %ld", (long)pmonitor->m_pid);
                close(pmonitor->m_recvfd);
                monitor_child_postauth(pmonitor);
@@ -815,14 +801,14 @@ main(int ac, char **av)
        int listen_sock, maxfd;
        int startup_p[2];
        int startups = 0;
-       Authctxt *authctxt;
        Key *key;
+       Authctxt *authctxt;
        int ret, key_used = 0;
 
 #ifdef HAVE_SECUREWARE
        (void)set_auth_parameters(ac, av);
 #endif
-       __progname = get_progname(av[0]);
+       __progname = ssh_get_progname(av[0]);
        init_rng();
 
        /* Save argv. Duplicate so setproctitle emulation doesn't clobber it */
@@ -842,7 +828,7 @@ main(int ac, char **av)
        initialize_server_options(&options);
 
        /* Parse command-line arguments. */
-       while ((opt = getopt(ac, av, "f:p:b:k:h:g:V:u:o:dDeiqtQ46")) != -1) {
+       while ((opt = getopt(ac, av, "f:p:b:k:h:g:u:o:dDeiqtQ46")) != -1) {
                switch (opt) {
                case '4':
                        IPv4or6 = AF_INET;
@@ -854,15 +840,11 @@ main(int ac, char **av)
                        config_file_name = optarg;
                        break;
                case 'd':
-                       if (0 == debug_flag) {
+                       if (debug_flag == 0) {
                                debug_flag = 1;
                                options.log_level = SYSLOG_LEVEL_DEBUG1;
-                       } else if (options.log_level < SYSLOG_LEVEL_DEBUG3) {
+                       } else if (options.log_level < SYSLOG_LEVEL_DEBUG3)
                                options.log_level++;
-                       } else {
-                               fprintf(stderr, "Too high debugging level.\n");
-                               exit(1);
-                       }
                        break;
                case 'D':
                        no_daemon_flag = 1;
@@ -913,11 +895,6 @@ main(int ac, char **av)
                        }
                        options.host_key_files[options.num_host_key_files++] = optarg;
                        break;
-               case 'V':
-                       client_version_string = optarg;
-                       /* only makes sense with inetd_flag, i.e. no listen() */
-                       inetd_flag = 1;
-                       break;
                case 't':
                        test_flag = 1;
                        break;
@@ -1159,11 +1136,6 @@ main(int ac, char **av)
                                verbose("socket: %.100s", strerror(errno));
                                continue;
                        }
-                       if (fcntl(listen_sock, F_SETFL, O_NONBLOCK) < 0) {
-                               error("listen_sock O_NONBLOCK: %s", strerror(errno));
-                               close(listen_sock);
-                               continue;
-                       }
                        /*
                         * Set socket options.
                         * Allow local port reuse in TIME_WAIT.
@@ -1221,7 +1193,10 @@ main(int ac, char **av)
                         * overwrite any old pid in the file.
                         */
                        f = fopen(options.pid_file, "wb");
-                       if (f) {
+                       if (f == NULL) {
+                               error("Couldn't create pid file \"%s\": %s",
+                                   options.pid_file, strerror(errno));
+                       } else {
                                fprintf(f, "%ld\n", (long) getpid());
                                fclose(f);
                        }
@@ -1300,11 +1275,6 @@ main(int ac, char **av)
                                                error("accept: %.100s", strerror(errno));
                                        continue;
                                }
-                               if (fcntl(newsock, F_SETFL, 0) < 0) {
-                                       error("newsock del O_NONBLOCK: %s", strerror(errno));
-                                       close(newsock);
-                                       continue;
-                               }
                                if (drop_connection(startups) == 1) {
                                        debug("drop connection #%d", startups);
                                        close(newsock);
@@ -1471,49 +1441,31 @@ main(int ac, char **av)
                alarm(options.login_grace_time);
 
        sshd_exchange_identification(sock_in, sock_out);
-       /*
-        * Check that the connection comes from a privileged port.
-        * Rhosts-Authentication only makes sense from privileged
-        * programs.  Of course, if the intruder has root access on his local
-        * machine, he can connect from any port.  So do not use these
-        * authentication methods from machines that you do not trust.
-        */
-       if (options.rhosts_authentication &&
-           (remote_port >= IPPORT_RESERVED ||
-           remote_port < IPPORT_RESERVED / 2)) {
-               debug("Rhosts Authentication disabled, "
-                   "originating port %d not trusted.", remote_port);
-               options.rhosts_authentication = 0;
-       }
-#if defined(KRB4) && !defined(KRB5)
-       if (!packet_connection_is_ipv4() &&
-           options.kerberos_authentication) {
-               debug("Kerberos Authentication disabled, only available for IPv4.");
-               options.kerberos_authentication = 0;
-       }
-#endif /* KRB4 && !KRB5 */
-#ifdef AFS
-       /* If machine has AFS, set process authentication group. */
-       if (k_hasafs()) {
-               k_setpag();
-               k_unlog();
-       }
-#endif /* AFS */
 
        packet_set_nonblocking();
 
+        /* prepare buffers to collect authentication messages */
+       buffer_init(&loginmsg);
+
+       /* allocate authentication context */
+       authctxt = xmalloc(sizeof(*authctxt));
+       memset(authctxt, 0, sizeof(*authctxt));
+
+       /* XXX global for cleanup, access from other modules */
+       the_authctxt = authctxt;
+
        if (use_privsep)
-               if ((authctxt = privsep_preauth()) != NULL)
+               if (privsep_preauth(authctxt) == 1)
                        goto authenticated;
 
        /* perform the key exchange */
        /* authenticate user and start session */
        if (compat20) {
                do_ssh2_kex();
-               authctxt = do_authentication2();
+               do_authentication2(authctxt);
        } else {
                do_ssh1_kex();
-               authctxt = do_authentication();
+               do_authentication(authctxt);
        }
        /*
         * If we use privilege separation, the unprivileged child transfers
@@ -1536,7 +1488,7 @@ main(int ac, char **av)
                        destroy_sensitive_data();
        }
 
-       /* Perform session preparation. */
+       /* Start session. */
        do_authenticated(authctxt);
 
        /* The connection has been terminated. */
@@ -1656,24 +1608,10 @@ do_ssh1_kex(void)
 
        /* Declare supported authentication types. */
        auth_mask = 0;
-       if (options.rhosts_authentication)
-               auth_mask |= 1 << SSH_AUTH_RHOSTS;
        if (options.rhosts_rsa_authentication)
                auth_mask |= 1 << SSH_AUTH_RHOSTS_RSA;
        if (options.rsa_authentication)
                auth_mask |= 1 << SSH_AUTH_RSA;
-#if defined(KRB4) || defined(KRB5)
-       if (options.kerberos_authentication)
-               auth_mask |= 1 << SSH_AUTH_KERBEROS;
-#endif
-#if defined(AFS) || defined(KRB5)
-       if (options.kerberos_tgt_passing)
-               auth_mask |= 1 << SSH_PASS_KERBEROS_TGT;
-#endif
-#ifdef AFS
-       if (options.afs_token_passing)
-               auth_mask |= 1 << SSH_PASS_AFS_TOKEN;
-#endif
        if (options.challenge_response_authentication == 1)
                auth_mask |= 1 << SSH_AUTH_TIS;
        if (options.password_authentication)
@@ -1843,3 +1781,12 @@ do_ssh2_kex(void)
 #endif
        debug("KEX done");
 }
+
+/* server specific fatal cleanup */
+void
+cleanup_exit(int i)
+{
+       if (the_authctxt)
+               do_cleanup(the_authctxt);
+       _exit(i);
+}
This page took 0.110239 seconds and 4 git commands to generate.