]> andersk Git - gssapi-openssh.git/blobdiff - openssh/monitor_wrap.c
merged OpenSSH 5.1p1 to trunk
[gssapi-openssh.git] / openssh / monitor_wrap.c
index dfbfa77d8988b657e9fb04accf13df5897b9f3b4..54000e05985fd899571a7df67fa2ef49e7b4275e 100644 (file)
@@ -1,3 +1,4 @@
+/* $OpenBSD: monitor_wrap.c,v 1.63 2008/07/10 18:08:11 markus Exp $ */
 /*
  * Copyright 2002 Niels Provos <provos@citi.umich.edu>
  * Copyright 2002 Markus Friedl <markus@openbsd.org>
  */
 
 #include "includes.h"
-RCSID("$OpenBSD: monitor_wrap.c,v 1.39 2004/07/17 05:31:41 dtucker Exp $");
+
+#include <sys/types.h>
+#include <sys/uio.h>
+
+#include <errno.h>
+#include <pwd.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
 
 #include <openssl/bn.h>
 #include <openssl/dh.h>
 
+#include "openbsd-compat/sys-queue.h"
+#include "xmalloc.h"
 #include "ssh.h"
 #include "dh.h"
+#include "buffer.h"
+#include "key.h"
+#include "cipher.h"
 #include "kex.h"
+#include "hostfile.h"
 #include "auth.h"
 #include "auth-options.h"
-#include "buffer.h"
-#include "bufaux.h"
 #include "packet.h"
 #include "mac.h"
 #include "log.h"
@@ -48,20 +63,18 @@ RCSID("$OpenBSD: monitor_wrap.c,v 1.39 2004/07/17 05:31:41 dtucker Exp $");
 #include "zlib.h"
 #endif
 #include "monitor.h"
+#ifdef GSSAPI
+#include "ssh-gss.h"
+#endif
 #include "monitor_wrap.h"
-#include "xmalloc.h"
 #include "atomicio.h"
 #include "monitor_fdpass.h"
-#include "getput.h"
+#include "misc.h"
 #include "servconf.h"
 
-#include "auth.h"
 #include "channels.h"
 #include "session.h"
-
-#ifdef GSSAPI
-#include "ssh-gss.h"
-#endif
+#include "servconf.h"
 
 /* Imports */
 extern int compat20;
@@ -91,12 +104,12 @@ mm_request_send(int sock, enum monitor_reqtype type, Buffer *m)
 
        debug3("%s entering: type %d", __func__, type);
 
-       PUT_32BIT(buf, mlen + 1);
+       put_u32(buf, mlen + 1);
        buf[4] = (u_char) type;         /* 1st byte of payload is mesg-type */
        if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf))
-               fatal("%s: write", __func__);
+               fatal("%s: write: %s", __func__, strerror(errno));
        if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen)
-               fatal("%s: write", __func__);
+               fatal("%s: write: %s", __func__, strerror(errno));
 }
 
 void
@@ -104,24 +117,21 @@ mm_request_receive(int sock, Buffer *m)
 {
        u_char buf[4];
        u_int msg_len;
-       ssize_t res;
 
        debug3("%s entering", __func__);
 
-       res = atomicio(read, sock, buf, sizeof(buf));
-       if (res != sizeof(buf)) {
-               if (res == 0)
+       if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) {
+               if (errno == EPIPE)
                        cleanup_exit(255);
-               fatal("%s: read: %ld", __func__, (long)res);
+               fatal("%s: read: %s", __func__, strerror(errno));
        }
-       msg_len = GET_32BIT(buf);
+       msg_len = get_u32(buf);
        if (msg_len > 256 * 1024)
                fatal("%s: read: bad msg_len %d", __func__, msg_len);
        buffer_clear(m);
        buffer_append_space(m, msg_len);
-       res = atomicio(read, sock, buffer_ptr(m), msg_len);
-       if (res != msg_len)
-               fatal("%s: read: %ld != msg_len", __func__, (long)res);
+       if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len)
+               fatal("%s: read: %s", __func__, strerror(errno));
 }
 
 void
@@ -199,7 +209,8 @@ mm_getpwnamallow(const char *username)
 {
        Buffer m;
        struct passwd *pw;
-       u_int pwlen;
+       u_int len;
+       ServerOptions *newopts;
 
        debug3("%s entering", __func__);
 
@@ -212,11 +223,11 @@ mm_getpwnamallow(const char *username)
        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m);
 
        if (buffer_get_char(&m) == 0) {
-               buffer_free(&m);
-               return (NULL);
+               pw = NULL;
+               goto out;
        }
-       pw = buffer_get_string(&m, &pwlen);
-       if (pwlen != sizeof(struct passwd))
+       pw = buffer_get_string(&m, &len);
+       if (len != sizeof(struct passwd))
                fatal("%s: struct passwd size mismatch", __func__);
        pw->pw_name = buffer_get_string(&m, NULL);
        pw->pw_passwd = buffer_get_string(&m, NULL);
@@ -226,6 +237,17 @@ mm_getpwnamallow(const char *username)
 #endif
        pw->pw_dir = buffer_get_string(&m, NULL);
        pw->pw_shell = buffer_get_string(&m, NULL);
+
+out:
+       /* copy options block as a Match directive may have changed some */
+       newopts = buffer_get_string(&m, &len);
+       if (len != sizeof(*newopts))
+               fatal("%s: option block size mismatch", __func__);
+       if (newopts->banner != NULL)
+               newopts->banner = buffer_get_string(&m, NULL);
+       copy_set_server_options(&options, newopts, 1);
+       xfree(newopts);
+
        buffer_free(&m);
 
        return (pw);
@@ -456,8 +478,8 @@ mm_newkeys_from_blob(u_char *blob, int blen)
 
        /* Mac structure */
        mac->name = buffer_get_string(&b, NULL);
-       if (mac->name == NULL || mac_init(mac, mac->name) == -1)
-               fatal("%s: can not init mac %s", __func__, mac->name);
+       if (mac->name == NULL || mac_setup(mac, mac->name) == -1)
+               fatal("%s: can not setup mac %s", __func__, mac->name);
        mac->enabled = buffer_get_int(&b);
        mac->key = buffer_get_string(&b, &len);
        if (len > mac->key_len)
@@ -551,7 +573,7 @@ mm_send_keystate(struct monitor *monitor)
        u_char *blob, *p;
        u_int bloblen, plen;
        u_int32_t seqnr, packets;
-       u_int64_t blocks;
+       u_int64_t blocks, bytes;
 
        buffer_init(&m);
 
@@ -600,14 +622,16 @@ mm_send_keystate(struct monitor *monitor)
        buffer_put_string(&m, blob, bloblen);
        xfree(blob);
 
-       packet_get_state(MODE_OUT, &seqnr, &blocks, &packets);
+       packet_get_state(MODE_OUT, &seqnr, &blocks, &packets, &bytes);
        buffer_put_int(&m, seqnr);
        buffer_put_int64(&m, blocks);
        buffer_put_int(&m, packets);
-       packet_get_state(MODE_IN, &seqnr, &blocks, &packets);
+       buffer_put_int64(&m, bytes);
+       packet_get_state(MODE_IN, &seqnr, &blocks, &packets, &bytes);
        buffer_put_int(&m, seqnr);
        buffer_put_int64(&m, blocks);
        buffer_put_int(&m, packets);
+       buffer_put_int64(&m, bytes);
 
        debug3("%s: New keys have been sent", __func__);
  skip:
@@ -640,11 +664,24 @@ mm_send_keystate(struct monitor *monitor)
 }
 
 int
-mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
+mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen)
 {
        Buffer m;
        char *p, *msg;
-       int success = 0;
+       int success = 0, tmp1 = -1, tmp2 = -1;
+
+       /* Kludge: ensure there are fds free to receive the pty/tty */
+       if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 ||
+           (tmp2 = dup(pmonitor->m_recvfd)) == -1) {
+               error("%s: cannot allocate fds for pty", __func__);
+               if (tmp1 > 0)
+                       close(tmp1);
+               if (tmp2 > 0)
+                       close(tmp2);
+               return 0;
+       }
+       close(tmp1);
+       close(tmp2);
 
        buffer_init(&m);
        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m);
@@ -668,8 +705,9 @@ mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen)
        buffer_append(&loginmsg, msg, strlen(msg));
        xfree(msg);
 
-       *ptyfd = mm_receive_fd(pmonitor->m_recvfd);
-       *ttyfd = mm_receive_fd(pmonitor->m_recvfd);
+       if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 ||
+           (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1)
+               fatal("%s: receive fds failed", __func__);
 
        /* Success */
        return (1);
@@ -688,8 +726,9 @@ mm_session_pty_cleanup2(Session *s)
        buffer_free(&m);
 
        /* closed dup'ed master */
-       if (close(s->ptymaster) < 0)
-               error("close(s->ptymaster): %s", strerror(errno));
+       if (s->ptymaster != -1 && close(s->ptymaster) < 0)
+               error("close(s->ptymaster/%d): %s",
+                   s->ptymaster, strerror(errno));
 
        /* unlink pty from session */
        s->ttyfd = -1;
@@ -716,6 +755,7 @@ mm_do_pam_account(void)
 {
        Buffer m;
        u_int ret;
+       char *msg;
 
        debug3("%s entering", __func__);
        if (!options.use_pam)
@@ -727,6 +767,9 @@ mm_do_pam_account(void)
        mm_request_receive_expect(pmonitor->m_recvfd,
            MONITOR_ANS_PAM_ACCOUNT, &m);
        ret = buffer_get_int(&m);
+       msg = buffer_get_string(&m, NULL);
+       buffer_append(&loginmsg, msg, strlen(msg));
+       xfree(msg);
 
        buffer_free(&m);
 
@@ -762,7 +805,8 @@ mm_sshpam_query(void *ctx, char **name, char **info,
     u_int *num, char ***prompts, u_int **echo_on)
 {
        Buffer m;
-       int i, ret;
+       u_int i;
+       int ret;
 
        debug3("%s", __func__);
        buffer_init(&m);
@@ -774,8 +818,11 @@ mm_sshpam_query(void *ctx, char **name, char **info,
        *name = buffer_get_string(&m, NULL);
        *info = buffer_get_string(&m, NULL);
        *num = buffer_get_int(&m);
-       *prompts = xmalloc((*num + 1) * sizeof(char *));
-       *echo_on = xmalloc((*num + 1) * sizeof(u_int));
+       if (*num > PAM_MAX_NUM_MSG)
+               fatal("%s: recieved %u PAM messages, expected <= %u",
+                   __func__, *num, PAM_MAX_NUM_MSG);
+       *prompts = xcalloc((*num + 1), sizeof(char *));
+       *echo_on = xcalloc((*num + 1), sizeof(u_int));
        for (i = 0; i < *num; ++i) {
                (*prompts)[i] = buffer_get_string(&m, NULL);
                (*echo_on)[i] = buffer_get_int(&m);
@@ -788,7 +835,8 @@ int
 mm_sshpam_respond(void *ctx, u_int num, char **resp)
 {
        Buffer m;
-       int i, ret;
+       u_int i;
+       int ret;
 
        debug3("%s", __func__);
        buffer_init(&m);
@@ -857,8 +905,8 @@ mm_chall_setup(char **name, char **infotxt, u_int *numprompts,
        *name = xstrdup("");
        *infotxt = xstrdup("");
        *numprompts = 1;
-       *prompts = xmalloc(*numprompts * sizeof(char *));
-       *echo_on = xmalloc(*numprompts * sizeof(u_int));
+       *prompts = xcalloc(*numprompts, sizeof(char *));
+       *echo_on = xcalloc(*numprompts, sizeof(u_int));
        (*echo_on)[0] = 0;
 }
 
@@ -925,9 +973,8 @@ mm_skey_query(void *ctx, char **name, char **infotxt,
    u_int *numprompts, char ***prompts, u_int **echo_on)
 {
        Buffer m;
-       int len;
        u_int success;
-       char *p, *challenge;
+       char *challenge;
 
        debug3("%s: entering", __func__);
 
@@ -951,11 +998,7 @@ mm_skey_query(void *ctx, char **name, char **infotxt,
 
        mm_chall_setup(name, infotxt, numprompts, prompts, echo_on);
 
-       len = strlen(challenge) + strlen(SKEY_PROMPT) + 1;
-       p = xmalloc(len);
-       strlcpy(p, challenge, len);
-       strlcat(p, SKEY_PROMPT, len);
-       (*prompts)[0] = p;
+       xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT);
        xfree(challenge);
 
        return (0);
@@ -1098,6 +1141,36 @@ mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16])
        return (success);
 }
 
+#ifdef SSH_AUDIT_EVENTS
+void
+mm_audit_event(ssh_audit_event_t event)
+{
+       Buffer m;
+
+       debug3("%s entering", __func__);
+
+       buffer_init(&m);
+       buffer_put_int(&m, event);
+
+       mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m);
+       buffer_free(&m);
+}
+
+void
+mm_audit_run_command(const char *command)
+{
+       Buffer m;
+
+       debug3("%s entering command %s", __func__, command);
+
+       buffer_init(&m);
+       buffer_put_cstring(&m, command);
+
+       mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m);
+       buffer_free(&m);
+}
+#endif /* SSH_AUDIT_EVENTS */
+
 #ifdef GSSAPI
 OM_uint32
 mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid)
@@ -1184,24 +1257,25 @@ mm_ssh_gssapi_userok(char *user)
 }
 
 OM_uint32
-mm_ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_desc *data, gss_buffer_desc *hash) {
-        Buffer m;
-        OM_uint32 major;
+mm_ssh_gssapi_sign(Gssctxt *ctx, gss_buffer_desc *data, gss_buffer_desc *hash)
+{
+       Buffer m;
+       OM_uint32 major;
        u_int len;
 
-        buffer_init(&m);
-        buffer_put_string(&m, data->value, data->length);
+       buffer_init(&m);
+       buffer_put_string(&m, data->value, data->length);
 
-        mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSIGN, &m);
-        mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSIGN, &m);
+       mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSIGN, &m);
+       mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSIGN, &m);
 
-        major=buffer_get_int(&m);
-        hash->value = buffer_get_string(&m, &len);
+       major = buffer_get_int(&m);
+       hash->value = buffer_get_string(&m, &len);
        hash->length = len;
 
        buffer_free(&m);
-       
-        return(major);
+
+       return(major);
 }
 
 char *
@@ -1280,4 +1354,5 @@ mm_ssh_gssapi_localname(char **lname)
        
         return(0);
 }      
+
 #endif /* GSSAPI */
This page took 1.547586 seconds and 4 git commands to generate.