OSDN Git Service

release-request-6db00e04-e62d-4e43-807a-a9cc1185dcbb-for-git_oc-mr1-release-4136929...
[android-x86/external-openssh.git] / monitor.c
index 8a53dd7..c36c890 100644 (file)
--- a/monitor.c
+++ b/monitor.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: monitor.c,v 1.150 2015/06/22 23:42:16 djm Exp $ */
+/* $OpenBSD: monitor.c,v 1.167 2017/02/03 23:05:57 djm Exp $ */
 /*
  * Copyright 2002 Niels Provos <provos@citi.umich.edu>
  * Copyright 2002 Markus Friedl <markus@openbsd.org>
@@ -34,6 +34,7 @@
 
 #include <errno.h>
 #include <fcntl.h>
+#include <limits.h>
 #ifdef HAVE_PATHS_H
 #include <paths.h>
 #endif
@@ -74,6 +75,7 @@
 #include "cipher.h"
 #include "kex.h"
 #include "dh.h"
+#include "auth-pam.h"
 #ifdef TARGET_OS_MAC   /* XXX Broken krb5 headers on Mac */
 #undef TARGET_OS_MAC
 #include "zlib.h"
@@ -92,7 +94,6 @@
 #include "misc.h"
 #include "servconf.h"
 #include "monitor.h"
-#include "monitor_mm.h"
 #ifdef GSSAPI
 #include "ssh-gss.h"
 #endif
 #include "monitor_fdpass.h"
 #include "compat.h"
 #include "ssh2.h"
-#include "roaming.h"
 #include "authfd.h"
 #include "match.h"
 #include "ssherr.h"
@@ -168,10 +168,6 @@ static int monitor_read_log(struct monitor *);
 
 static Authctxt *authctxt;
 
-#ifdef WITH_SSH1
-static BIGNUM *ssh1_challenge = NULL;  /* used for ssh1 rsa auth */
-#endif
-
 /* local state for key verify */
 static u_char *key_blob = NULL;
 static u_int key_bloblen = 0;
@@ -211,9 +207,9 @@ struct mon_table mon_dispatch_proto20[] = {
 #ifdef USE_PAM
     {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
     {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
-    {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
-    {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
-    {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
+    {MONITOR_REQ_PAM_INIT_CTX, MON_ONCE, mm_answer_pam_init_ctx},
+    {MONITOR_REQ_PAM_QUERY, 0, mm_answer_pam_query},
+    {MONITOR_REQ_PAM_RESPOND, MON_ONCE, mm_answer_pam_respond},
     {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
 #endif
 #ifdef SSH_AUDIT_EVENTS
@@ -231,9 +227,9 @@ struct mon_table mon_dispatch_proto20[] = {
     {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
 #ifdef GSSAPI
     {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
-    {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
-    {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
-    {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic},
+    {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
+    {MONITOR_REQ_GSSUSEROK, MON_ONCE|MON_AUTHDECIDE, mm_answer_gss_userok},
+    {MONITOR_REQ_GSSCHECKMIC, MON_ONCE, mm_answer_gss_checkmic},
 #endif
     {0, 0, NULL}
 };
@@ -253,52 +249,6 @@ struct mon_table mon_dispatch_postauth20[] = {
     {0, 0, NULL}
 };
 
-struct mon_table mon_dispatch_proto15[] = {
-#ifdef WITH_SSH1
-    {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
-    {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
-    {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
-    {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
-    {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed},
-    {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed},
-    {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
-    {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
-#ifdef BSD_AUTH
-    {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
-    {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
-#endif
-#ifdef SKEY
-    {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery},
-    {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond},
-#endif
-#ifdef USE_PAM
-    {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
-    {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
-    {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
-    {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
-    {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
-    {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
-#endif
-#ifdef SSH_AUDIT_EVENTS
-    {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
-#endif
-#endif /* WITH_SSH1 */
-    {0, 0, NULL}
-};
-
-struct mon_table mon_dispatch_postauth15[] = {
-#ifdef WITH_SSH1
-    {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty},
-    {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup},
-    {MONITOR_REQ_TERM, 0, mm_answer_term},
-#ifdef SSH_AUDIT_EVENTS
-    {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
-    {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command},
-#endif
-#endif /* WITH_SSH1 */
-    {0, 0, NULL}
-};
-
 struct mon_table *mon_dispatch;
 
 /* Specifies if a certain message is allowed at the moment */
@@ -333,6 +283,7 @@ monitor_permit_authentications(int permit)
 void
 monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
 {
+       struct ssh *ssh = active_state; /* XXX */
        struct mon_table *ent;
        int authenticated = 0, partial = 0;
 
@@ -347,17 +298,10 @@ monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
 
        authctxt->loginmsg = &loginmsg;
 
-       if (compat20) {
-               mon_dispatch = mon_dispatch_proto20;
-
-               /* Permit requests for moduli and signatures */
-               monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
-               monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
-       } else {
-               mon_dispatch = mon_dispatch_proto15;
-
-               monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1);
-       }
+       mon_dispatch = mon_dispatch_proto20;
+       /* Permit requests for moduli and signatures */
+       monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
+       monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
 
        /* The first few requests do not require asynchronous access */
        while (!authenticated) {
@@ -368,9 +312,6 @@ monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
 
                /* Special handling for multiple required authentications */
                if (options.num_auth_methods != 0) {
-                       if (!compat20)
-                               fatal("AuthenticationMethods is not supported"
-                                   "with SSH protocol 1");
                        if (authenticated &&
                            !auth2_update_methods_lists(authctxt,
                            auth_method, auth_submethod)) {
@@ -416,6 +357,7 @@ monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor)
 
        debug("%s: %s has been authenticated by privileged process",
            __func__, authctxt->user);
+       ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user);
 
        mm_get_keystate(pmonitor);
 
@@ -454,17 +396,13 @@ monitor_child_postauth(struct monitor *pmonitor)
        signal(SIGXFSZ, SIG_IGN);
 #endif
 
-       if (compat20) {
-               mon_dispatch = mon_dispatch_postauth20;
+       mon_dispatch = mon_dispatch_postauth20;
+
+       /* Permit requests for moduli and signatures */
+       monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
+       monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
+       monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
 
-               /* Permit requests for moduli and signatures */
-               monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
-               monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
-               monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
-       } else {
-               mon_dispatch = mon_dispatch_postauth15;
-               monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
-       }
        if (!no_pty_flag) {
                monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
                monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
@@ -474,36 +412,6 @@ monitor_child_postauth(struct monitor *pmonitor)
                monitor_read(pmonitor, mon_dispatch, NULL);
 }
 
-void
-monitor_sync(struct monitor *pmonitor)
-{
-       if (options.compression) {
-               /* The member allocation is not visible, so sync it */
-               mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
-       }
-}
-
-/* Allocation functions for zlib */
-static void *
-mm_zalloc(struct mm_master *mm, u_int ncount, u_int size)
-{
-       size_t len = (size_t) size * ncount;
-       void *address;
-
-       if (len == 0 || ncount > SIZE_MAX / size)
-               fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size);
-
-       address = mm_malloc(mm, len);
-
-       return (address);
-}
-
-static void
-mm_zfree(struct mm_master *mm, void *address)
-{
-       mm_free(mm, address);
-}
-
 static int
 monitor_read_log(struct monitor *pmonitor)
 {
@@ -690,18 +598,22 @@ mm_answer_sign(int sock, Buffer *m)
        struct ssh *ssh = active_state;         /* XXX */
        extern int auth_sock;                   /* XXX move to state struct? */
        struct sshkey *key;
-       struct sshbuf *sigbuf;
-       u_char *p;
-       u_char *signature;
-       size_t datlen, siglen;
-       int r, keyid, is_proof = 0;
+       struct sshbuf *sigbuf = NULL;
+       u_char *p = NULL, *signature = NULL;
+       char *alg = NULL;
+       size_t datlen, siglen, alglen;
+       int r, is_proof = 0;
+       u_int keyid;
        const char proof_req[] = "hostkeys-prove-00@openssh.com";
 
        debug3("%s", __func__);
 
        if ((r = sshbuf_get_u32(m, &keyid)) != 0 ||
-           (r = sshbuf_get_string(m, &p, &datlen)) != 0)
+           (r = sshbuf_get_string(m, &p, &datlen)) != 0 ||
+           (r = sshbuf_get_cstring(m, &alg, &alglen)) != 0)
                fatal("%s: buffer error: %s", __func__, ssh_err(r));
+       if (keyid > INT_MAX)
+               fatal("%s: invalid key ID", __func__);
 
        /*
         * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
@@ -727,7 +639,7 @@ mm_answer_sign(int sock, Buffer *m)
                        fatal("%s: sshbuf_new", __func__);
                if ((r = sshbuf_put_cstring(sigbuf, proof_req)) != 0 ||
                    (r = sshbuf_put_string(sigbuf, session_id2,
-                   session_id2_len) != 0) ||
+                   session_id2_len)) != 0 ||
                    (r = sshkey_puts(key, sigbuf)) != 0)
                        fatal("%s: couldn't prepare private key "
                            "proof buffer: %s", __func__, ssh_err(r));
@@ -747,14 +659,14 @@ mm_answer_sign(int sock, Buffer *m)
        }
 
        if ((key = get_hostkey_by_index(keyid)) != NULL) {
-               if ((r = sshkey_sign(key, &signature, &siglen, p, datlen,
+               if ((r = sshkey_sign(key, &signature, &siglen, p, datlen, alg,
                    datafellows)) != 0)
                        fatal("%s: sshkey_sign failed: %s",
                            __func__, ssh_err(r));
        } else if ((key = get_hostkey_public_by_index(keyid, ssh)) != NULL &&
            auth_sock > 0) {
                if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen,
-                   p, datlen, datafellows)) != 0) {
+                   p, datlen, alg, datafellows)) != 0) {
                        fatal("%s: ssh_agent_sign failed: %s",
                            __func__, ssh_err(r));
                }
@@ -768,6 +680,7 @@ mm_answer_sign(int sock, Buffer *m)
        if ((r = sshbuf_put_string(m, signature, siglen)) != 0)
                fatal("%s: buffer error: %s", __func__, ssh_err(r));
 
+       free(alg);
        free(p);
        free(signature);
 
@@ -784,6 +697,7 @@ mm_answer_sign(int sock, Buffer *m)
 int
 mm_answer_pwnamallow(int sock, Buffer *m)
 {
+       struct ssh *ssh = active_state; /* XXX */
        char *username;
        struct passwd *pwent;
        int allowed = 0;
@@ -828,6 +742,8 @@ mm_answer_pwnamallow(int sock, Buffer *m)
        buffer_put_cstring(m, pwent->pw_shell);
 
  out:
+       ssh_packet_set_log_preamble(ssh, "%suser %s",
+           authctxt->valid ? "authenticating" : "invalid ", authctxt->user);
        buffer_put_string(m, &options, sizeof(options));
 
 #define M_CP_STROPT(x) do { \
@@ -844,7 +760,7 @@ mm_answer_pwnamallow(int sock, Buffer *m)
 #undef M_CP_STRARRAYOPT
 
        /* Create valid auth method lists */
-       if (compat20 && auth2_setup_methods_lists(authctxt) != 0) {
+       if (auth2_setup_methods_lists(authctxt) != 0) {
                /*
                 * The monitor will continue long enough to let the child
                 * run to it's packet_disconnect(), but it must not allow any
@@ -856,14 +772,10 @@ mm_answer_pwnamallow(int sock, Buffer *m)
        debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
        mm_request_send(sock, MONITOR_ANS_PWNAM, m);
 
-       /* For SSHv1 allow authentication now */
-       if (!compat20)
-               monitor_permit_authentications(1);
-       else {
-               /* Allow service/style information on the auth context */
-               monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
-               monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
-       }
+       /* Allow service/style information on the auth context */
+       monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
+       monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
+
 #ifdef USE_PAM
        if (options.use_pam)
                monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
@@ -911,6 +823,8 @@ mm_answer_authpassword(int sock, Buffer *m)
        int authenticated;
        u_int plen;
 
+       if (!options.password_authentication)
+               fatal("%s: password authentication not enabled", __func__);
        passwd = buffer_get_string(m, &plen);
 #if !defined(ANDROID)
        /* Only authenticate if the context is valid */
@@ -925,6 +839,9 @@ mm_answer_authpassword(int sock, Buffer *m)
 
        buffer_clear(m);
        buffer_put_int(m, authenticated);
+#ifdef USE_PAM
+       buffer_put_int(m, sshpam_get_maxtries_reached());
+#endif
 
        debug3("%s: sending result %d", __func__, authenticated);
        mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
@@ -949,6 +866,8 @@ mm_answer_bsdauthquery(int sock, Buffer *m)
        char **prompts;
        u_int success;
 
+       if (!options.kbd_interactive_authentication)
+               fatal("%s: kbd-int authentication not enabled", __func__);
        success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
            &prompts, &echo_on) < 0 ? 0 : 1;
 
@@ -976,7 +895,9 @@ mm_answer_bsdauthrespond(int sock, Buffer *m)
        char *response;
        int authok;
 
-       if (authctxt->as == 0)
+       if (!options.kbd_interactive_authentication)
+               fatal("%s: kbd-int authentication not enabled", __func__);
+       if (authctxt->as == NULL)
                fatal("%s: no bsd auth session", __func__);
 
        response = buffer_get_string(m, NULL);
@@ -992,11 +913,8 @@ mm_answer_bsdauthrespond(int sock, Buffer *m)
        debug3("%s: sending authenticated: %d", __func__, authok);
        mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
 
-       if (compat20) {
-               auth_method = "keyboard-interactive";
-               auth_submethod = "bsdauth";
-       } else
-               auth_method = "bsdauth";
+       auth_method = "keyboard-interactive";
+       auth_submethod = "bsdauth";
 
        return (authok != 0);
 }
@@ -1045,7 +963,8 @@ mm_answer_skeyrespond(int sock, Buffer *m)
        debug3("%s: sending authenticated: %d", __func__, authok);
        mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m);
 
-       auth_method = "skey";
+       auth_method = "keyboard-interactive";
+       auth_submethod = "skey";
 
        return (authok != 0);
 }
@@ -1061,6 +980,8 @@ mm_answer_pam_start(int sock, Buffer *m)
        start_pam(authctxt);
 
        monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
+       if (options.kbd_interactive_authentication)
+               monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
 
        return (0);
 }
@@ -1071,7 +992,7 @@ mm_answer_pam_account(int sock, Buffer *m)
        u_int ret;
 
        if (!options.use_pam)
-               fatal("UsePAM not set, but ended up in %s anyway", __func__);
+               fatal("%s: PAM not enabled", __func__);
 
        ret = do_pam_account();
 
@@ -1090,11 +1011,16 @@ int
 mm_answer_pam_init_ctx(int sock, Buffer *m)
 {
        debug3("%s", __func__);
+       if (!options.kbd_interactive_authentication)
+               fatal("%s: kbd-int authentication not enabled", __func__);
+       if (sshpam_ctxt != NULL)
+               fatal("%s: already called", __func__);
        sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
        sshpam_authok = NULL;
        buffer_clear(m);
        if (sshpam_ctxt != NULL) {
                monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
+               monitor_permit(mon_dispatch, MONITOR_REQ_PAM_QUERY, 1);
                buffer_put_int(m, 1);
        } else {
                buffer_put_int(m, 0);
@@ -1112,17 +1038,22 @@ mm_answer_pam_query(int sock, Buffer *m)
 
        debug3("%s", __func__);
        sshpam_authok = NULL;
-       ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
+       if (sshpam_ctxt == NULL)
+               fatal("%s: no context", __func__);
+       ret = (sshpam_device.query)(sshpam_ctxt, &name, &info,
+           &num, &prompts, &echo_on);
        if (ret == 0 && num == 0)
                sshpam_authok = sshpam_ctxt;
        if (num > 1 || name == NULL || info == NULL)
-               ret = -1;
+               fatal("sshpam_device.query failed");
+       monitor_permit(mon_dispatch, MONITOR_REQ_PAM_RESPOND, 1);
        buffer_clear(m);
        buffer_put_int(m, ret);
        buffer_put_cstring(m, name);
        free(name);
        buffer_put_cstring(m, info);
        free(info);
+       buffer_put_int(m, sshpam_get_maxtries_reached());
        buffer_put_int(m, num);
        for (i = 0; i < num; ++i) {
                buffer_put_cstring(m, prompts[i]);
@@ -1145,6 +1076,8 @@ mm_answer_pam_respond(int sock, Buffer *m)
        int ret;
 
        debug3("%s", __func__);
+       if (sshpam_ctxt == NULL)
+               fatal("%s: no context", __func__);
        sshpam_authok = NULL;
        num = buffer_get_int(m);
        if (num > 0) {
@@ -1174,10 +1107,14 @@ mm_answer_pam_free_ctx(int sock, Buffer *m)
        int r = sshpam_authok != NULL && sshpam_authok == sshpam_ctxt;
 
        debug3("%s", __func__);
+       if (sshpam_ctxt == NULL)
+               fatal("%s: no context", __func__);
        (sshpam_device.free_ctx)(sshpam_ctxt);
        sshpam_ctxt = sshpam_authok = NULL;
        buffer_clear(m);
        mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
+       /* Allow another attempt */
+       monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
        auth_method = "keyboard-interactive";
        auth_submethod = "pam";
        return r;
@@ -1204,10 +1141,6 @@ mm_answer_keyallowed(int sock, Buffer *m)
 
        key = key_from_blob(blob, bloblen);
 
-       if ((compat20 && type == MM_RSAHOSTKEY) ||
-           (!compat20 && type != MM_RSAHOSTKEY))
-               fatal("%s: key type and protocol mismatch", __func__);
-
        debug3("%s: key_from_blob: %p", __func__, key);
 
        if (key != NULL && authctxt->valid) {
@@ -1241,22 +1174,15 @@ mm_answer_keyallowed(int sock, Buffer *m)
                            cuser, chost);
                        auth_method = "hostbased";
                        break;
-#ifdef WITH_SSH1
-               case MM_RSAHOSTKEY:
-                       key->type = KEY_RSA1; /* XXX */
-                       allowed = options.rhosts_rsa_authentication &&
-                           auth_rhosts_rsa_key_allowed(authctxt->pw,
-                           cuser, chost, key);
-                       if (options.rhosts_rsa_authentication && allowed != 1)
-                               auth_clear_options();
-                       auth_method = "rsa";
-                       break;
-#endif
                default:
                        fatal("%s: unknown key type %d", __func__, type);
                        break;
                }
        }
+
+       debug3("%s: key %p is %s",
+           __func__, key, allowed ? "allowed" : "not allowed");
+
        if (key != NULL)
                key_free(key);
 
@@ -1278,18 +1204,12 @@ mm_answer_keyallowed(int sock, Buffer *m)
                free(chost);
        }
 
-       debug3("%s: key %p is %s",
-           __func__, key, allowed ? "allowed" : "not allowed");
-
        buffer_clear(m);
        buffer_put_int(m, allowed);
        buffer_put_int(m, forced_command != NULL);
 
        mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
 
-       if (type == MM_RSAHOSTKEY)
-               monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
-
        return (0);
 }
 
@@ -1297,7 +1217,8 @@ static int
 monitor_valid_userblob(u_char *data, u_int datalen)
 {
        Buffer b;
-       char *p, *userstyle;
+       u_char *p;
+       char *userstyle, *cp;
        u_int len;
        int fail = 0;
 
@@ -1322,26 +1243,26 @@ monitor_valid_userblob(u_char *data, u_int datalen)
        }
        if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
                fail++;
-       p = buffer_get_cstring(&b, NULL);
+       cp = buffer_get_cstring(&b, NULL);
        xasprintf(&userstyle, "%s%s%s", authctxt->user,
            authctxt->style ? ":" : "",
            authctxt->style ? authctxt->style : "");
-       if (strcmp(userstyle, p) != 0) {
-               logit("wrong user name passed to monitor: expected %s != %.100s",
-                   userstyle, p);
+       if (strcmp(userstyle, cp) != 0) {
+               logit("wrong user name passed to monitor: "
+                   "expected %s != %.100s", userstyle, cp);
                fail++;
        }
        free(userstyle);
-       free(p);
+       free(cp);
        buffer_skip_string(&b);
        if (datafellows & SSH_BUG_PKAUTH) {
                if (!buffer_get_char(&b))
                        fail++;
        } else {
-               p = buffer_get_cstring(&b, NULL);
-               if (strcmp("publickey", p) != 0)
+               cp = buffer_get_cstring(&b, NULL);
+               if (strcmp("publickey", cp) != 0)
                        fail++;
-               free(p);
+               free(cp);
                if (!buffer_get_char(&b))
                        fail++;
                buffer_skip_string(&b);
@@ -1454,7 +1375,7 @@ mm_answer_keyverify(int sock, Buffer *m)
            __func__, key, (verified == 1) ? "verified" : "unverified");
 
        /* If auth was successful then record key to ensure it isn't reused */
-       if (verified == 1)
+       if (verified == 1 && key_blobtype == MM_USERKEY)
                auth2_record_userkey(authctxt, key);
        else
                key_free(key);
@@ -1477,12 +1398,10 @@ mm_answer_keyverify(int sock, Buffer *m)
 static void
 mm_record_login(Session *s, struct passwd *pw)
 {
+       struct ssh *ssh = active_state; /* XXX */
        socklen_t fromlen;
        struct sockaddr_storage from;
 
-       if (options.use_login)
-               return;
-
        /*
         * Get IP address of client. If the connection is not a socket, let
         * the address be 0.0.0.0.
@@ -1498,7 +1417,7 @@ mm_record_login(Session *s, struct passwd *pw)
        }
        /* Record that there was a login on that tty from the remote host. */
        record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
-           get_remote_name_or_ip(utmp_len, options.use_dns),
+           session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
            (struct sockaddr *)&from, fromlen);
 }
 
@@ -1596,186 +1515,6 @@ mm_answer_pty_cleanup(int sock, Buffer *m)
        return (0);
 }
 
-#ifdef WITH_SSH1
-int
-mm_answer_sesskey(int sock, Buffer *m)
-{
-       BIGNUM *p;
-       int rsafail;
-
-       /* Turn off permissions */
-       monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0);
-
-       if ((p = BN_new()) == NULL)
-               fatal("%s: BN_new", __func__);
-
-       buffer_get_bignum2(m, p);
-
-       rsafail = ssh1_session_key(p);
-
-       buffer_clear(m);
-       buffer_put_int(m, rsafail);
-       buffer_put_bignum2(m, p);
-
-       BN_clear_free(p);
-
-       mm_request_send(sock, MONITOR_ANS_SESSKEY, m);
-
-       /* Turn on permissions for sessid passing */
-       monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1);
-
-       return (0);
-}
-
-int
-mm_answer_sessid(int sock, Buffer *m)
-{
-       int i;
-
-       debug3("%s entering", __func__);
-
-       if (buffer_len(m) != 16)
-               fatal("%s: bad ssh1 session id", __func__);
-       for (i = 0; i < 16; i++)
-               session_id[i] = buffer_get_char(m);
-
-       /* Turn on permissions for getpwnam */
-       monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
-
-       return (0);
-}
-
-int
-mm_answer_rsa_keyallowed(int sock, Buffer *m)
-{
-       BIGNUM *client_n;
-       Key *key = NULL;
-       u_char *blob = NULL;
-       u_int blen = 0;
-       int allowed = 0;
-
-       debug3("%s entering", __func__);
-
-       auth_method = "rsa";
-       if (options.rsa_authentication && authctxt->valid) {
-               if ((client_n = BN_new()) == NULL)
-                       fatal("%s: BN_new", __func__);
-               buffer_get_bignum2(m, client_n);
-               allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key);
-               BN_clear_free(client_n);
-       }
-       buffer_clear(m);
-       buffer_put_int(m, allowed);
-       buffer_put_int(m, forced_command != NULL);
-
-       /* clear temporarily storage (used by generate challenge) */
-       monitor_reset_key_state();
-
-       if (allowed && key != NULL) {
-               key->type = KEY_RSA;    /* cheat for key_to_blob */
-               if (key_to_blob(key, &blob, &blen) == 0)
-                       fatal("%s: key_to_blob failed", __func__);
-               buffer_put_string(m, blob, blen);
-
-               /* Save temporarily for comparison in verify */
-               key_blob = blob;
-               key_bloblen = blen;
-               key_blobtype = MM_RSAUSERKEY;
-       }
-       if (key != NULL)
-               key_free(key);
-
-       mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m);
-
-       monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed);
-       monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0);
-       return (0);
-}
-
-int
-mm_answer_rsa_challenge(int sock, Buffer *m)
-{
-       Key *key = NULL;
-       u_char *blob;
-       u_int blen;
-
-       debug3("%s entering", __func__);
-
-       if (!authctxt->valid)
-               fatal("%s: authctxt not valid", __func__);
-       blob = buffer_get_string(m, &blen);
-       if (!monitor_allowed_key(blob, blen))
-               fatal("%s: bad key, not previously allowed", __func__);
-       if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
-               fatal("%s: key type mismatch", __func__);
-       if ((key = key_from_blob(blob, blen)) == NULL)
-               fatal("%s: received bad key", __func__);
-       if (key->type != KEY_RSA)
-               fatal("%s: received bad key type %d", __func__, key->type);
-       key->type = KEY_RSA1;
-       if (ssh1_challenge)
-               BN_clear_free(ssh1_challenge);
-       ssh1_challenge = auth_rsa_generate_challenge(key);
-
-       buffer_clear(m);
-       buffer_put_bignum2(m, ssh1_challenge);
-
-       debug3("%s sending reply", __func__);
-       mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m);
-
-       monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1);
-
-       free(blob);
-       key_free(key);
-       return (0);
-}
-
-int
-mm_answer_rsa_response(int sock, Buffer *m)
-{
-       Key *key = NULL;
-       u_char *blob, *response;
-       u_int blen, len;
-       int success;
-
-       debug3("%s entering", __func__);
-
-       if (!authctxt->valid)
-               fatal("%s: authctxt not valid", __func__);
-       if (ssh1_challenge == NULL)
-               fatal("%s: no ssh1_challenge", __func__);
-
-       blob = buffer_get_string(m, &blen);
-       if (!monitor_allowed_key(blob, blen))
-               fatal("%s: bad key, not previously allowed", __func__);
-       if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY)
-               fatal("%s: key type mismatch: %d", __func__, key_blobtype);
-       if ((key = key_from_blob(blob, blen)) == NULL)
-               fatal("%s: received bad key", __func__);
-       response = buffer_get_string(m, &len);
-       if (len != 16)
-               fatal("%s: received bad response to challenge", __func__);
-       success = auth_rsa_verify_response(key, ssh1_challenge, response);
-
-       free(blob);
-       key_free(key);
-       free(response);
-
-       auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa";
-
-       /* reset state */
-       BN_clear_free(ssh1_challenge);
-       ssh1_challenge = NULL;
-       monitor_reset_key_state();
-
-       buffer_clear(m);
-       buffer_put_int(m, success);
-       mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m);
-
-       return (success);
-}
-#endif
-
 int
 mm_answer_term(int sock, Buffer *req)
 {
@@ -1857,11 +1596,14 @@ monitor_apply_keystate(struct monitor *pmonitor)
        sshbuf_free(child_state);
        child_state = NULL;
 
-       if ((kex = ssh->kex) != 0) {
+       if ((kex = ssh->kex) != NULL) {
                /* XXX set callbacks */
 #ifdef WITH_OPENSSL
                kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server;
                kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server;
+               kex->kex[KEX_DH_GRP14_SHA256] = kexdh_server;
+               kex->kex[KEX_DH_GRP16_SHA512] = kexdh_server;
+               kex->kex[KEX_DH_GRP18_SHA512] = kexdh_server;
                kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
                kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
 # ifdef OPENSSL_HAS_ECC
@@ -1874,13 +1616,6 @@ monitor_apply_keystate(struct monitor *pmonitor)
                kex->host_key_index=&get_hostkey_index;
                kex->sign = sshd_hostkey_sign;
        }
-
-       /* Update with new address */
-       if (options.compression) {
-               ssh_packet_set_compress_hooks(ssh, pmonitor->m_zlib,
-                   (ssh_packet_comp_alloc_func *)mm_zalloc,
-                   (ssh_packet_comp_free_func *)mm_zfree);
-       }
 }
 
 /* This function requries careful sanity checking */
@@ -1933,24 +1668,11 @@ monitor_openfds(struct monitor *mon, int do_logfds)
 struct monitor *
 monitor_init(void)
 {
-       struct ssh *ssh = active_state;                 /* XXX */
        struct monitor *mon;
 
        mon = xcalloc(1, sizeof(*mon));
-
        monitor_openfds(mon, 1);
 
-       /* Used to share zlib space across processes */
-       if (options.compression) {
-               mon->m_zback = mm_create(NULL, MM_MEMSIZE);
-               mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE);
-
-               /* Compression needs to share state across borders */
-               ssh_packet_set_compress_hooks(ssh, mon->m_zlib,
-                   (ssh_packet_comp_alloc_func *)mm_zalloc,
-                   (ssh_packet_comp_free_func *)mm_zfree);
-       }
-
        return mon;
 }
 
@@ -1968,6 +1690,9 @@ mm_answer_gss_setup_ctx(int sock, Buffer *m)
        OM_uint32 major;
        u_int len;
 
+       if (!options.gss_authentication)
+               fatal("%s: GSSAPI authentication not enabled", __func__);
+
        goid.elements = buffer_get_string(m, &len);
        goid.length = len;
 
@@ -1995,6 +1720,9 @@ mm_answer_gss_accept_ctx(int sock, Buffer *m)
        OM_uint32 flags = 0; /* GSI needs this */
        u_int len;
 
+       if (!options.gss_authentication)
+               fatal("%s: GSSAPI authentication not enabled", __func__);
+
        in.value = buffer_get_string(m, &len);
        in.length = len;
        major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
@@ -2023,6 +1751,9 @@ mm_answer_gss_checkmic(int sock, Buffer *m)
        OM_uint32 ret;
        u_int len;
 
+       if (!options.gss_authentication)
+               fatal("%s: GSSAPI authentication not enabled", __func__);
+
        gssbuf.value = buffer_get_string(m, &len);
        gssbuf.length = len;
        mic.value = buffer_get_string(m, &len);
@@ -2049,6 +1780,9 @@ mm_answer_gss_userok(int sock, Buffer *m)
 {
        int authenticated;
 
+       if (!options.gss_authentication)
+               fatal("%s: GSSAPI authentication not enabled", __func__);
+
        authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
 
        buffer_clear(m);
@@ -2063,4 +1797,3 @@ mm_answer_gss_userok(int sock, Buffer *m)
        return (authenticated);
 }
 #endif /* GSSAPI */
-