]> andersk Git - openssh.git/blobdiff - monitor.c
- markus@cvs.openbsd.org 2002/06/04 19:53:40
[openssh.git] / monitor.c
index 03f8dc74af7bf8ba7ec4b32ce77c746a9f114f22..a96ca04d5812b1f338376473ee2cc2a64e033256 100644 (file)
--- a/monitor.c
+++ b/monitor.c
@@ -25,7 +25,7 @@
  */
 
 #include "includes.h"
-RCSID("$OpenBSD: monitor.c,v 1.9 2002/03/30 18:51:15 markus Exp $");
+RCSID("$OpenBSD: monitor.c,v 1.13 2002/06/04 19:53:40 markus Exp $");
 
 #include <openssl/dh.h>
 
@@ -96,6 +96,7 @@ struct {
 int mm_answer_moduli(int, Buffer *);
 int mm_answer_sign(int, Buffer *);
 int mm_answer_pwnamallow(int, Buffer *);
+int mm_answer_auth2_read_banner(int, Buffer *);
 int mm_answer_authserv(int, Buffer *);
 int mm_answer_authpassword(int, Buffer *);
 int mm_answer_bsdauthquery(int, Buffer *);
@@ -113,6 +114,10 @@ int mm_answer_rsa_response(int, Buffer *);
 int mm_answer_sesskey(int, Buffer *);
 int mm_answer_sessid(int, Buffer *);
 
+#ifdef USE_PAM
+int mm_answer_pam_start(int, Buffer *);
+#endif
+
 static Authctxt *authctxt;
 static BIGNUM *ssh1_challenge = NULL;  /* used for ssh1 rsa auth */
 
@@ -123,6 +128,8 @@ static int key_blobtype = MM_NOKEY;
 static u_char *hostbased_cuser = NULL;
 static u_char *hostbased_chost = NULL;
 static char *auth_method = "unknown";
+static int session_id2_len = 0;
+static u_char *session_id2 = NULL;
 
 struct mon_table {
        enum monitor_reqtype type;
@@ -143,8 +150,10 @@ struct mon_table mon_dispatch_proto20[] = {
     {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
     {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
     {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
-#if !defined(USE_PAM)
+    {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
     {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
+#ifdef USE_PAM
+    {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
 #endif
 #ifdef BSD_AUTH
     {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
@@ -172,13 +181,14 @@ struct mon_table mon_dispatch_proto15[] = {
     {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
     {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey},
     {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid},
-#if !defined(USE_PAM)
     {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
-#endif
     {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH, mm_answer_rsa_keyallowed},
     {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
     {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge},
     {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response},
+#ifdef USE_PAM
+    {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
+#endif
 #ifdef BSD_AUTH
     {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
     {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH,mm_answer_bsdauthrespond},
@@ -186,6 +196,9 @@ struct mon_table mon_dispatch_proto15[] = {
 #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},
 #endif
     {0, 0, NULL}
 };
@@ -229,7 +242,7 @@ monitor_permit_authentications(int permit)
 }
 
 Authctxt *
-monitor_child_preauth(struct monitor *monitor)
+monitor_child_preauth(struct monitor *pmonitor)
 {
        struct mon_table *ent;
        int authenticated = 0;
@@ -252,7 +265,7 @@ monitor_child_preauth(struct monitor *monitor)
 
        /* The first few requests do not require asynchronous access */
        while (!authenticated) {
-               authenticated = monitor_read(monitor, mon_dispatch, &ent);
+               authenticated = monitor_read(pmonitor, mon_dispatch, &ent);
                if (authenticated) {
                        if (!(ent->flags & MON_AUTHDECIDE))
                                fatal("%s: unexpected authentication from %d",
@@ -260,6 +273,10 @@ monitor_child_preauth(struct monitor *monitor)
                        if (authctxt->pw->pw_uid == 0 &&
                            !auth_root_allowed(auth_method))
                                authenticated = 0;
+#ifdef USE_PAM
+                       if (!do_pam_account(authctxt->pw->pw_name, NULL))
+                               authenticated = 0;
+#endif
                }
 
                if (ent->flags & MON_AUTHDECIDE) {
@@ -276,13 +293,13 @@ monitor_child_preauth(struct monitor *monitor)
        debug("%s: %s has been authenticated by privileged process",
            __FUNCTION__, authctxt->user);
 
-       mm_get_keystate(monitor);
+       mm_get_keystate(pmonitor);
 
        return (authctxt);
 }
 
 void
-monitor_child_postauth(struct monitor *monitor)
+monitor_child_postauth(struct monitor *pmonitor)
 {
        if (compat20) {
                mon_dispatch = mon_dispatch_postauth20;
@@ -302,18 +319,18 @@ monitor_child_postauth(struct monitor *monitor)
        }
 
        for (;;)
-               monitor_read(monitor, mon_dispatch, NULL);
+               monitor_read(pmonitor, mon_dispatch, NULL);
 }
 
 void
-monitor_sync(struct monitor *monitor)
+monitor_sync(struct monitor *pmonitor)
 {
        /* The member allocation is not visible, so sync it */
-       mm_share_sync(&monitor->m_zlib, &monitor->m_zback);
+       mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback);
 }
 
 int
-monitor_read(struct monitor *monitor, struct mon_table *ent,
+monitor_read(struct monitor *pmonitor, struct mon_table *ent,
     struct mon_table **pent)
 {
        Buffer m;
@@ -322,7 +339,7 @@ monitor_read(struct monitor *monitor, struct mon_table *ent,
 
        buffer_init(&m);
 
-       mm_request_receive(monitor->m_sendfd, &m);
+       mm_request_receive(pmonitor->m_sendfd, &m);
        type = buffer_get_char(&m);
 
        debug3("%s: checking request %d", __FUNCTION__, type);
@@ -337,7 +354,7 @@ monitor_read(struct monitor *monitor, struct mon_table *ent,
                if (!(ent->flags & MON_PERMIT))
                        fatal("%s: unpermitted request %d", __FUNCTION__,
                            type);
-               ret = (*ent->f)(monitor->m_sendfd, &m);
+               ret = (*ent->f)(pmonitor->m_sendfd, &m);
                buffer_free(&m);
 
                /* The child may use this request only once, disable it */
@@ -439,6 +456,13 @@ mm_answer_sign(int socket, Buffer *m)
        if (datlen != 20)
                fatal("%s: data length incorrect: %d", __FUNCTION__, datlen);
 
+       /* save session id, it will be passed on the first call */
+       if (session_id2_len == 0) {
+               session_id2_len = datlen;
+               session_id2 = xmalloc(session_id2_len);
+               memcpy(session_id2, p, session_id2_len);
+       }
+
        if ((key = get_hostkey_by_index(keyid)) == NULL)
                fatal("%s: no hostkey from index %d", __FUNCTION__, keyid);
        if (key_sign(key, &signature, &siglen, p, datlen) < 0)
@@ -511,10 +535,30 @@ mm_answer_pwnamallow(int socket, Buffer *m)
        /* For SSHv1 allow authentication now */
        if (!compat20)
                monitor_permit_authentications(1);
-       else
+       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);
+       }
 
+#ifdef USE_PAM
+       monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
+#endif
+
+       return (0);
+}
+
+int mm_answer_auth2_read_banner(int socket, Buffer *m)
+{
+       char *banner;
+
+       buffer_clear(m);
+       banner = auth2_read_banner();
+       buffer_put_cstring(m, banner != NULL ? banner : "");
+       mm_request_send(socket, MONITOR_ANS_AUTH2_READ_BANNER, m);
+
+       if (banner != NULL)
+               free(banner);
 
        return (0);
 }
@@ -537,7 +581,6 @@ mm_answer_authserv(int socket, Buffer *m)
        return (0);
 }
 
-#if !defined(USE_PAM)
 int
 mm_answer_authpassword(int socket, Buffer *m)
 {
@@ -547,7 +590,8 @@ mm_answer_authpassword(int socket, Buffer *m)
 
        passwd = buffer_get_string(m, &plen);
        /* Only authenticate if the context is valid */
-       authenticated = authctxt->valid && auth_password(authctxt, passwd);
+       authenticated = options.password_authentication &&
+           authctxt->valid && auth_password(authctxt, passwd);
        memset(passwd, 0, strlen(passwd));
        xfree(passwd);
 
@@ -566,7 +610,6 @@ mm_answer_authpassword(int socket, Buffer *m)
        /* Causes monitor loop to terminate if authenticated */
        return (authenticated);
 }
-#endif
 
 #ifdef BSD_AUTH
 int
@@ -609,7 +652,8 @@ mm_answer_bsdauthrespond(int socket, Buffer *m)
                fatal("%s: no bsd auth session", __FUNCTION__);
 
        response = buffer_get_string(m, NULL);
-       authok = auth_userresponse(authctxt->as, response, 0);
+       authok = options.challenge_response_authentication &&
+           auth_userresponse(authctxt->as, response, 0);
        authctxt->as = NULL;
        debug3("%s: <%s> = <%d>", __FUNCTION__, response, authok);
        xfree(response);
@@ -655,7 +699,8 @@ mm_answer_skeyrespond(int socket, Buffer *m)
 
        response = buffer_get_string(m, NULL);
 
-       authok = (authctxt->valid &&
+       authok = (options.challenge_response_authentication &&
+           authctxt->valid &&
            skey_haskey(authctxt->pw->pw_name) == 0 &&
            skey_passcheck(authctxt->pw->pw_name, response) != -1);
 
@@ -673,6 +718,22 @@ mm_answer_skeyrespond(int socket, Buffer *m)
 }
 #endif
 
+#ifdef USE_PAM
+int
+mm_answer_pam_start(int socket, Buffer *m)
+{
+       char *user;
+       
+       user = buffer_get_string(m, NULL);
+
+       start_pam(user);
+
+       xfree(user);
+
+       return (0);
+}
+#endif
+
 static void
 mm_append_debug(Buffer *m)
 {
@@ -711,15 +772,18 @@ mm_answer_keyallowed(int socket, Buffer *m)
        if (key != NULL && authctxt->pw != NULL) {
                switch(type) {
                case MM_USERKEY:
-                       allowed = user_key_allowed(authctxt->pw, key);
+                       allowed = options.pubkey_authentication &&
+                           user_key_allowed(authctxt->pw, key);
                        break;
                case MM_HOSTKEY:
-                       allowed = hostbased_key_allowed(authctxt->pw,
+                       allowed = options.hostbased_authentication &&
+                           hostbased_key_allowed(authctxt->pw,
                            cuser, chost, key);
                        break;
                case MM_RSAHOSTKEY:
                        key->type = KEY_RSA1; /* XXX */
-                       allowed = auth_rhosts_rsa_key_allowed(authctxt->pw,
+                       allowed = options.rhosts_rsa_authentication &&
+                           auth_rhosts_rsa_key_allowed(authctxt->pw,
                            cuser, chost, key);
                        break;
                default:
@@ -764,17 +828,25 @@ monitor_valid_userblob(u_char *data, u_int datalen)
        u_char *p;
        u_int len;
        int fail = 0;
-       int session_id2_len = 20 /*XXX should get from [net] */;
 
        buffer_init(&b);
        buffer_append(&b, data, datalen);
 
        if (datafellows & SSH_OLD_SESSIONID) {
+               p = buffer_ptr(&b);
+               len = buffer_len(&b);
+               if ((session_id2 == NULL) ||
+                   (len < session_id2_len) ||
+                   (memcmp(p, session_id2, session_id2_len) != 0))
+                       fail++;
                buffer_consume(&b, session_id2_len);
        } else {
-               xfree(buffer_get_string(&b, &len));
-               if (len != session_id2_len)
+               p = buffer_get_string(&b, &len);
+               if ((session_id2 == NULL) ||
+                   (len != session_id2_len) ||
+                   (memcmp(p, session_id2, session_id2_len) != 0))
                        fail++;
+               xfree(p);
        }
        if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
                fail++;
@@ -813,14 +885,17 @@ monitor_valid_hostbasedblob(u_char *data, u_int datalen, u_char *cuser,
        u_char *p;
        u_int len;
        int fail = 0;
-       int session_id2_len = 20 /*XXX should get from [net] */;
 
        buffer_init(&b);
        buffer_append(&b, data, datalen);
 
-       xfree(buffer_get_string(&b, &len));
-       if (len != session_id2_len)
+       p = buffer_get_string(&b, &len);
+       if ((session_id2 == NULL) ||
+           (len != session_id2_len) ||
+           (memcmp(p, session_id2, session_id2_len) != 0))
                fail++;
+       xfree(p);
+
        if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
                fail++;
        p = buffer_get_string(&b, NULL);
@@ -909,7 +984,7 @@ mm_answer_keyverify(int socket, Buffer *m)
        buffer_put_int(m, verified);
        mm_request_send(socket, MONITOR_ANS_KEYVERIFY, m);
 
-       auth_method = "publickey";
+       auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased";
 
        return (verified);
 }
@@ -954,7 +1029,7 @@ mm_session_close(Session *s)
 int
 mm_answer_pty(int socket, Buffer *m)
 {
-       extern struct monitor *monitor;
+       extern struct monitor *pmonitor;
        Session *s;
        int res, fd0;
 
@@ -966,7 +1041,7 @@ mm_answer_pty(int socket, Buffer *m)
                goto error;
        s->authctxt = authctxt;
        s->pw = authctxt->pw;
-       s->pid = monitor->m_pid;
+       s->pid = pmonitor->m_pid;
        res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
        if (res == 0)
                goto error;
@@ -1088,7 +1163,7 @@ mm_answer_rsa_keyallowed(int socket, Buffer *m)
 
        debug3("%s entering", __FUNCTION__);
 
-       if (authctxt->valid) {
+       if (options.rsa_authentication && authctxt->valid) {
                if ((client_n = BN_new()) == NULL)
                        fatal("%s: BN_new", __FUNCTION__);
                buffer_get_bignum2(m, client_n);
@@ -1203,7 +1278,7 @@ mm_answer_rsa_response(int socket, Buffer *m)
 int
 mm_answer_term(int socket, Buffer *req)
 {
-       extern struct monitor *monitor;
+       extern struct monitor *pmonitor;
        int res, status;
 
        debug3("%s: tearing down sessions", __FUNCTION__);
@@ -1211,7 +1286,7 @@ mm_answer_term(int socket, Buffer *req)
        /* The child is terminating */
        session_destroy_all(&mm_session_close);
 
-       while (waitpid(monitor->m_pid, &status, 0) == -1)
+       while (waitpid(pmonitor->m_pid, &status, 0) == -1)
                if (errno != EINTR)
                        exit(1);
 
@@ -1222,7 +1297,7 @@ mm_answer_term(int socket, Buffer *req)
 }
 
 void
-monitor_apply_keystate(struct monitor *monitor)
+monitor_apply_keystate(struct monitor *pmonitor)
 {
        if (compat20) {
                set_newkeys(MODE_IN);
@@ -1254,7 +1329,7 @@ monitor_apply_keystate(struct monitor *monitor)
            sizeof(outgoing_stream));
 
        /* Update with new address */
-       mm_init_compression(monitor->m_zlib);
+       mm_init_compression(pmonitor->m_zlib);
 
        /* Network I/O buffers */
        /* XXX inefficient for large buffers, need: buffer_init_from_string */
@@ -1279,6 +1354,10 @@ mm_get_kex(Buffer *m)
        kex = xmalloc(sizeof(*kex));
        memset(kex, 0, sizeof(*kex));
        kex->session_id = buffer_get_string(m, &kex->session_id_len);
+       if ((session_id2 == NULL) ||
+           (kex->session_id_len != session_id2_len) ||
+           (memcmp(kex->session_id, session_id2, session_id2_len) != 0))
+               fatal("mm_get_get: internal error: bad session id");
        kex->we_need = buffer_get_int(m);
        kex->server = 1;
        kex->hostkey_type = buffer_get_int(m);
@@ -1304,7 +1383,7 @@ mm_get_kex(Buffer *m)
 /* This function requries careful sanity checking */
 
 void
-mm_get_keystate(struct monitor *monitor)
+mm_get_keystate(struct monitor *pmonitor)
 {
        Buffer m;
        u_char *blob, *p;
@@ -1313,7 +1392,7 @@ mm_get_keystate(struct monitor *monitor)
        debug3("%s: Waiting for new keys", __FUNCTION__);
 
        buffer_init(&m);
-       mm_request_receive_expect(monitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
+       mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m);
        if (!compat20) {
                child_state.ssh1protoflags = buffer_get_int(&m);
                child_state.ssh1cipher = buffer_get_int(&m);
@@ -1323,7 +1402,7 @@ mm_get_keystate(struct monitor *monitor)
                goto skip;
        } else {
                /* Get the Kex for rekeying */
-               *monitor->m_pkex = mm_get_kex(&m);
+               *pmonitor->m_pkex = mm_get_kex(&m);
        }
 
        blob = buffer_get_string(&m, &bloblen);
@@ -1406,8 +1485,13 @@ mm_init_compression(struct mm_master *mm)
 static void
 monitor_socketpair(int *pair)
 {
+#ifdef HAVE_SOCKETPAIR
        if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
                fatal("%s: socketpair", __FUNCTION__);
+#else
+       fatal("%s: UsePrivilegeSeparation=yes not supported",
+           __FUNCTION__);
+#endif
        FD_CLOSEONEXEC(pair[0]);
        FD_CLOSEONEXEC(pair[1]);
 }
This page took 0.085105 seconds and 4 git commands to generate.