Damien Miller | 1c89ce0 | 2006-08-19 00:22:40 +1000 | [diff] [blame] | 1 | /* $OpenBSD: monitor_wrap.c,v 1.54 2006/08/12 20:46:46 miod Exp $ */ |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2002 Niels Provos <provos@citi.umich.edu> |
| 4 | * Copyright 2002 Markus Friedl <markus@openbsd.org> |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "includes.h" |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 29 | |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 30 | #include <sys/types.h> |
Darren Tucker | 1a3d6e7 | 2006-08-05 18:46:47 +1000 | [diff] [blame] | 31 | #include <sys/uio.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 32 | |
Darren Tucker | 3997249 | 2006-07-12 22:22:46 +1000 | [diff] [blame] | 33 | #include <errno.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 34 | #include <pwd.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 35 | #include <signal.h> |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 36 | #include <stdarg.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 37 | #include <stdio.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 38 | #include <string.h> |
Darren Tucker | 1a3d6e7 | 2006-08-05 18:46:47 +1000 | [diff] [blame] | 39 | #include <unistd.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 40 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 41 | #include <openssl/bn.h> |
| 42 | #include <openssl/dh.h> |
| 43 | |
| 44 | #include "xmalloc.h" |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 45 | #include "ssh.h" |
| 46 | #include "dh.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 47 | #include "buffer.h" |
| 48 | #include "key.h" |
| 49 | #include "cipher.h" |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 50 | #include "kex.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 51 | #include "hostfile.h" |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 52 | #include "auth.h" |
Damien Miller | 06ebedf | 2003-02-24 12:03:38 +1100 | [diff] [blame] | 53 | #include "auth-options.h" |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 54 | #include "packet.h" |
| 55 | #include "mac.h" |
| 56 | #include "log.h" |
Ben Lindstrom | 036768e | 2004-04-08 16:12:30 +0000 | [diff] [blame] | 57 | #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */ |
| 58 | #undef TARGET_OS_MAC |
Ben Lindstrom | 1b9f2a6 | 2004-04-08 05:11:03 +0000 | [diff] [blame] | 59 | #include "zlib.h" |
Ben Lindstrom | 036768e | 2004-04-08 16:12:30 +0000 | [diff] [blame] | 60 | #define TARGET_OS_MAC 1 |
| 61 | #else |
| 62 | #include "zlib.h" |
| 63 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 64 | #include "monitor.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 65 | #ifdef GSSAPI |
| 66 | #include "ssh-gss.h" |
| 67 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 68 | #include "monitor_wrap.h" |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 69 | #include "atomicio.h" |
| 70 | #include "monitor_fdpass.h" |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 71 | #include "misc.h" |
Damien Miller | 4e448a3 | 2003-05-14 15:11:48 +1000 | [diff] [blame] | 72 | #include "servconf.h" |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 73 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 74 | #include "channels.h" |
| 75 | #include "session.h" |
| 76 | |
| 77 | /* Imports */ |
| 78 | extern int compat20; |
| 79 | extern Newkeys *newkeys[]; |
| 80 | extern z_stream incoming_stream; |
| 81 | extern z_stream outgoing_stream; |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 82 | extern struct monitor *pmonitor; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 83 | extern Buffer input, output; |
Darren Tucker | 0999174 | 2004-07-17 17:05:14 +1000 | [diff] [blame] | 84 | extern Buffer loginmsg; |
Damien Miller | 4e448a3 | 2003-05-14 15:11:48 +1000 | [diff] [blame] | 85 | extern ServerOptions options; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 86 | |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 87 | int |
| 88 | mm_is_monitor(void) |
| 89 | { |
| 90 | /* |
| 91 | * m_pid is only set in the privileged part, and |
| 92 | * points to the unprivileged child. |
| 93 | */ |
Darren Tucker | a47c9bc | 2003-11-03 20:03:25 +1100 | [diff] [blame] | 94 | return (pmonitor && pmonitor->m_pid > 0); |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 95 | } |
| 96 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 97 | void |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 98 | mm_request_send(int sock, enum monitor_reqtype type, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 99 | { |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 100 | u_int mlen = buffer_len(m); |
Ben Lindstrom | b1bdc5a | 2002-07-04 00:09:26 +0000 | [diff] [blame] | 101 | u_char buf[5]; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 102 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 103 | debug3("%s entering: type %d", __func__, type); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 104 | |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 105 | put_u32(buf, mlen + 1); |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 106 | buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */ |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 107 | if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf)) |
Damien Miller | b253cc4 | 2005-05-26 12:23:44 +1000 | [diff] [blame] | 108 | fatal("%s: write: %s", __func__, strerror(errno)); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 109 | if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen) |
Damien Miller | b253cc4 | 2005-05-26 12:23:44 +1000 | [diff] [blame] | 110 | fatal("%s: write: %s", __func__, strerror(errno)); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 114 | mm_request_receive(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 115 | { |
| 116 | u_char buf[4]; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 117 | u_int msg_len; |
| 118 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 119 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 120 | |
Damien Miller | b253cc4 | 2005-05-26 12:23:44 +1000 | [diff] [blame] | 121 | if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) { |
| 122 | if (errno == EPIPE) |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 123 | cleanup_exit(255); |
Damien Miller | b253cc4 | 2005-05-26 12:23:44 +1000 | [diff] [blame] | 124 | fatal("%s: read: %s", __func__, strerror(errno)); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 125 | } |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 126 | msg_len = get_u32(buf); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 127 | if (msg_len > 256 * 1024) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 128 | fatal("%s: read: bad msg_len %d", __func__, msg_len); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 129 | buffer_clear(m); |
| 130 | buffer_append_space(m, msg_len); |
Damien Miller | b253cc4 | 2005-05-26 12:23:44 +1000 | [diff] [blame] | 131 | if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len) |
| 132 | fatal("%s: read: %s", __func__, strerror(errno)); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 136 | mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 137 | { |
| 138 | u_char rtype; |
| 139 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 140 | debug3("%s entering: type %d", __func__, type); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 141 | |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 142 | mm_request_receive(sock, m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 143 | rtype = buffer_get_char(m); |
| 144 | if (rtype != type) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 145 | fatal("%s: read: rtype %d != type %d", __func__, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 146 | rtype, type); |
| 147 | } |
| 148 | |
| 149 | DH * |
| 150 | mm_choose_dh(int min, int nbits, int max) |
| 151 | { |
| 152 | BIGNUM *p, *g; |
| 153 | int success = 0; |
| 154 | Buffer m; |
| 155 | |
| 156 | buffer_init(&m); |
| 157 | buffer_put_int(&m, min); |
| 158 | buffer_put_int(&m, nbits); |
| 159 | buffer_put_int(&m, max); |
| 160 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 161 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 162 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 163 | debug3("%s: waiting for MONITOR_ANS_MODULI", __func__); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 164 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 165 | |
| 166 | success = buffer_get_char(&m); |
| 167 | if (success == 0) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 168 | fatal("%s: MONITOR_ANS_MODULI failed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 169 | |
| 170 | if ((p = BN_new()) == NULL) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 171 | fatal("%s: BN_new failed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 172 | if ((g = BN_new()) == NULL) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 173 | fatal("%s: BN_new failed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 174 | buffer_get_bignum2(&m, p); |
| 175 | buffer_get_bignum2(&m, g); |
| 176 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 177 | debug3("%s: remaining %d", __func__, buffer_len(&m)); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 178 | buffer_free(&m); |
| 179 | |
| 180 | return (dh_new_group(g, p)); |
| 181 | } |
| 182 | |
| 183 | int |
| 184 | mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen) |
| 185 | { |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 186 | Kex *kex = *pmonitor->m_pkex; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 187 | Buffer m; |
| 188 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 189 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 190 | |
| 191 | buffer_init(&m); |
| 192 | buffer_put_int(&m, kex->host_key_index(key)); |
| 193 | buffer_put_string(&m, data, datalen); |
| 194 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 195 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 196 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 197 | debug3("%s: waiting for MONITOR_ANS_SIGN", __func__); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 198 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 199 | *sigp = buffer_get_string(&m, lenp); |
| 200 | buffer_free(&m); |
| 201 | |
| 202 | return (0); |
| 203 | } |
| 204 | |
| 205 | struct passwd * |
Darren Tucker | b09b677 | 2004-06-22 15:06:46 +1000 | [diff] [blame] | 206 | mm_getpwnamallow(const char *username) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 207 | { |
| 208 | Buffer m; |
| 209 | struct passwd *pw; |
| 210 | u_int pwlen; |
| 211 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 212 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 213 | |
| 214 | buffer_init(&m); |
Darren Tucker | b09b677 | 2004-06-22 15:06:46 +1000 | [diff] [blame] | 215 | buffer_put_cstring(&m, username); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 216 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 217 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 218 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 219 | debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 220 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 221 | |
| 222 | if (buffer_get_char(&m) == 0) { |
| 223 | buffer_free(&m); |
| 224 | return (NULL); |
| 225 | } |
| 226 | pw = buffer_get_string(&m, &pwlen); |
| 227 | if (pwlen != sizeof(struct passwd)) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 228 | fatal("%s: struct passwd size mismatch", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 229 | pw->pw_name = buffer_get_string(&m, NULL); |
| 230 | pw->pw_passwd = buffer_get_string(&m, NULL); |
| 231 | pw->pw_gecos = buffer_get_string(&m, NULL); |
Kevin Steves | 7e14760 | 2002-03-22 18:07:17 +0000 | [diff] [blame] | 232 | #ifdef HAVE_PW_CLASS_IN_PASSWD |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 233 | pw->pw_class = buffer_get_string(&m, NULL); |
Kevin Steves | 7e14760 | 2002-03-22 18:07:17 +0000 | [diff] [blame] | 234 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 235 | pw->pw_dir = buffer_get_string(&m, NULL); |
| 236 | pw->pw_shell = buffer_get_string(&m, NULL); |
| 237 | buffer_free(&m); |
| 238 | |
| 239 | return (pw); |
| 240 | } |
| 241 | |
Darren Tucker | 7eb3de0 | 2003-10-15 15:56:58 +1000 | [diff] [blame] | 242 | char * |
| 243 | mm_auth2_read_banner(void) |
Damien Miller | 5ad9fd9 | 2002-05-13 11:07:41 +1000 | [diff] [blame] | 244 | { |
| 245 | Buffer m; |
| 246 | char *banner; |
| 247 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 248 | debug3("%s entering", __func__); |
Damien Miller | 5ad9fd9 | 2002-05-13 11:07:41 +1000 | [diff] [blame] | 249 | |
| 250 | buffer_init(&m); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 251 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m); |
Damien Miller | 5ad9fd9 | 2002-05-13 11:07:41 +1000 | [diff] [blame] | 252 | buffer_clear(&m); |
| 253 | |
Darren Tucker | 7eb3de0 | 2003-10-15 15:56:58 +1000 | [diff] [blame] | 254 | mm_request_receive_expect(pmonitor->m_recvfd, |
| 255 | MONITOR_ANS_AUTH2_READ_BANNER, &m); |
Damien Miller | 5ad9fd9 | 2002-05-13 11:07:41 +1000 | [diff] [blame] | 256 | banner = buffer_get_string(&m, NULL); |
| 257 | buffer_free(&m); |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 258 | |
Darren Tucker | 7eb3de0 | 2003-10-15 15:56:58 +1000 | [diff] [blame] | 259 | /* treat empty banner as missing banner */ |
| 260 | if (strlen(banner) == 0) { |
| 261 | xfree(banner); |
| 262 | banner = NULL; |
| 263 | } |
Damien Miller | 5ad9fd9 | 2002-05-13 11:07:41 +1000 | [diff] [blame] | 264 | return (banner); |
| 265 | } |
| 266 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 267 | /* Inform the privileged process about service and style */ |
| 268 | |
| 269 | void |
| 270 | mm_inform_authserv(char *service, char *style) |
| 271 | { |
| 272 | Buffer m; |
| 273 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 274 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 275 | |
| 276 | buffer_init(&m); |
| 277 | buffer_put_cstring(&m, service); |
| 278 | buffer_put_cstring(&m, style ? style : ""); |
| 279 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 280 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 281 | |
| 282 | buffer_free(&m); |
| 283 | } |
| 284 | |
| 285 | /* Do the password authentication */ |
| 286 | int |
| 287 | mm_auth_password(Authctxt *authctxt, char *password) |
| 288 | { |
| 289 | Buffer m; |
| 290 | int authenticated = 0; |
| 291 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 292 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 293 | |
| 294 | buffer_init(&m); |
| 295 | buffer_put_cstring(&m, password); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 296 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 297 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 298 | debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 299 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 300 | |
| 301 | authenticated = buffer_get_int(&m); |
| 302 | |
| 303 | buffer_free(&m); |
| 304 | |
| 305 | debug3("%s: user %sauthenticated", |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 306 | __func__, authenticated ? "" : "not "); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 307 | return (authenticated); |
| 308 | } |
| 309 | |
| 310 | int |
| 311 | mm_user_key_allowed(struct passwd *pw, Key *key) |
| 312 | { |
| 313 | return (mm_key_allowed(MM_USERKEY, NULL, NULL, key)); |
| 314 | } |
| 315 | |
| 316 | int |
| 317 | mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host, |
| 318 | Key *key) |
| 319 | { |
| 320 | return (mm_key_allowed(MM_HOSTKEY, user, host, key)); |
| 321 | } |
| 322 | |
| 323 | int |
| 324 | mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user, |
| 325 | char *host, Key *key) |
| 326 | { |
| 327 | int ret; |
| 328 | |
| 329 | key->type = KEY_RSA; /* XXX hack for key_to_blob */ |
| 330 | ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key); |
| 331 | key->type = KEY_RSA1; |
| 332 | return (ret); |
| 333 | } |
| 334 | |
| 335 | static void |
| 336 | mm_send_debug(Buffer *m) |
| 337 | { |
| 338 | char *msg; |
| 339 | |
| 340 | while (buffer_len(m)) { |
| 341 | msg = buffer_get_string(m, NULL); |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 342 | debug3("%s: Sending debug: %s", __func__, msg); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 343 | packet_send_debug("%s", msg); |
| 344 | xfree(msg); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | int |
| 349 | mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key) |
| 350 | { |
| 351 | Buffer m; |
| 352 | u_char *blob; |
| 353 | u_int len; |
Damien Miller | 06ebedf | 2003-02-24 12:03:38 +1100 | [diff] [blame] | 354 | int allowed = 0, have_forced = 0; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 355 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 356 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 357 | |
| 358 | /* Convert the key to a blob and the pass it over */ |
| 359 | if (!key_to_blob(key, &blob, &len)) |
| 360 | return (0); |
| 361 | |
| 362 | buffer_init(&m); |
| 363 | buffer_put_int(&m, type); |
| 364 | buffer_put_cstring(&m, user ? user : ""); |
| 365 | buffer_put_cstring(&m, host ? host : ""); |
| 366 | buffer_put_string(&m, blob, len); |
| 367 | xfree(blob); |
| 368 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 369 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 370 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 371 | debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 372 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 373 | |
| 374 | allowed = buffer_get_int(&m); |
| 375 | |
Damien Miller | 06ebedf | 2003-02-24 12:03:38 +1100 | [diff] [blame] | 376 | /* fake forced command */ |
| 377 | auth_clear_options(); |
| 378 | have_forced = buffer_get_int(&m); |
| 379 | forced_command = have_forced ? xstrdup("true") : NULL; |
| 380 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 381 | /* Send potential debug messages */ |
| 382 | mm_send_debug(&m); |
| 383 | |
| 384 | buffer_free(&m); |
| 385 | |
| 386 | return (allowed); |
| 387 | } |
| 388 | |
| 389 | /* |
| 390 | * This key verify needs to send the key type along, because the |
| 391 | * privileged parent makes the decision if the key is allowed |
| 392 | * for authentication. |
| 393 | */ |
| 394 | |
| 395 | int |
| 396 | mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen) |
| 397 | { |
| 398 | Buffer m; |
| 399 | u_char *blob; |
| 400 | u_int len; |
| 401 | int verified = 0; |
| 402 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 403 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 404 | |
| 405 | /* Convert the key to a blob and the pass it over */ |
| 406 | if (!key_to_blob(key, &blob, &len)) |
| 407 | return (0); |
| 408 | |
| 409 | buffer_init(&m); |
| 410 | buffer_put_string(&m, blob, len); |
| 411 | buffer_put_string(&m, sig, siglen); |
| 412 | buffer_put_string(&m, data, datalen); |
| 413 | xfree(blob); |
| 414 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 415 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 416 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 417 | debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 418 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 419 | |
| 420 | verified = buffer_get_int(&m); |
| 421 | |
| 422 | buffer_free(&m); |
| 423 | |
| 424 | return (verified); |
| 425 | } |
| 426 | |
| 427 | /* Export key state after authentication */ |
| 428 | Newkeys * |
| 429 | mm_newkeys_from_blob(u_char *blob, int blen) |
| 430 | { |
| 431 | Buffer b; |
| 432 | u_int len; |
| 433 | Newkeys *newkey = NULL; |
| 434 | Enc *enc; |
| 435 | Mac *mac; |
| 436 | Comp *comp; |
| 437 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 438 | debug3("%s: %p(%d)", __func__, blob, blen); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 439 | #ifdef DEBUG_PK |
| 440 | dump_base64(stderr, blob, blen); |
| 441 | #endif |
| 442 | buffer_init(&b); |
| 443 | buffer_append(&b, blob, blen); |
| 444 | |
| 445 | newkey = xmalloc(sizeof(*newkey)); |
| 446 | enc = &newkey->enc; |
| 447 | mac = &newkey->mac; |
| 448 | comp = &newkey->comp; |
| 449 | |
| 450 | /* Enc structure */ |
| 451 | enc->name = buffer_get_string(&b, NULL); |
| 452 | buffer_get(&b, &enc->cipher, sizeof(enc->cipher)); |
| 453 | enc->enabled = buffer_get_int(&b); |
| 454 | enc->block_size = buffer_get_int(&b); |
| 455 | enc->key = buffer_get_string(&b, &enc->key_len); |
| 456 | enc->iv = buffer_get_string(&b, &len); |
| 457 | if (len != enc->block_size) |
Ben Lindstrom | 0851249 | 2002-06-27 00:23:02 +0000 | [diff] [blame] | 458 | fatal("%s: bad ivlen: expected %u != %u", __func__, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 459 | enc->block_size, len); |
| 460 | |
| 461 | if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 462 | fatal("%s: bad cipher name %s or pointer %p", __func__, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 463 | enc->name, enc->cipher); |
| 464 | |
| 465 | /* Mac structure */ |
| 466 | mac->name = buffer_get_string(&b, NULL); |
| 467 | if (mac->name == NULL || mac_init(mac, mac->name) == -1) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 468 | fatal("%s: can not init mac %s", __func__, mac->name); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 469 | mac->enabled = buffer_get_int(&b); |
| 470 | mac->key = buffer_get_string(&b, &len); |
| 471 | if (len > mac->key_len) |
Ben Lindstrom | 0851249 | 2002-06-27 00:23:02 +0000 | [diff] [blame] | 472 | fatal("%s: bad mac key length: %u > %d", __func__, len, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 473 | mac->key_len); |
| 474 | mac->key_len = len; |
| 475 | |
| 476 | /* Comp structure */ |
| 477 | comp->type = buffer_get_int(&b); |
| 478 | comp->enabled = buffer_get_int(&b); |
| 479 | comp->name = buffer_get_string(&b, NULL); |
| 480 | |
| 481 | len = buffer_len(&b); |
| 482 | if (len != 0) |
Ben Lindstrom | 0851249 | 2002-06-27 00:23:02 +0000 | [diff] [blame] | 483 | error("newkeys_from_blob: remaining bytes in blob %u", len); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 484 | buffer_free(&b); |
| 485 | return (newkey); |
| 486 | } |
| 487 | |
| 488 | int |
| 489 | mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp) |
| 490 | { |
| 491 | Buffer b; |
| 492 | int len; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 493 | Enc *enc; |
| 494 | Mac *mac; |
| 495 | Comp *comp; |
| 496 | Newkeys *newkey = newkeys[mode]; |
| 497 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 498 | debug3("%s: converting %p", __func__, newkey); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 499 | |
| 500 | if (newkey == NULL) { |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 501 | error("%s: newkey == NULL", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 502 | return 0; |
| 503 | } |
| 504 | enc = &newkey->enc; |
| 505 | mac = &newkey->mac; |
| 506 | comp = &newkey->comp; |
| 507 | |
| 508 | buffer_init(&b); |
| 509 | /* Enc structure */ |
| 510 | buffer_put_cstring(&b, enc->name); |
| 511 | /* The cipher struct is constant and shared, you export pointer */ |
| 512 | buffer_append(&b, &enc->cipher, sizeof(enc->cipher)); |
| 513 | buffer_put_int(&b, enc->enabled); |
| 514 | buffer_put_int(&b, enc->block_size); |
| 515 | buffer_put_string(&b, enc->key, enc->key_len); |
| 516 | packet_get_keyiv(mode, enc->iv, enc->block_size); |
| 517 | buffer_put_string(&b, enc->iv, enc->block_size); |
| 518 | |
| 519 | /* Mac structure */ |
| 520 | buffer_put_cstring(&b, mac->name); |
| 521 | buffer_put_int(&b, mac->enabled); |
| 522 | buffer_put_string(&b, mac->key, mac->key_len); |
| 523 | |
| 524 | /* Comp structure */ |
| 525 | buffer_put_int(&b, comp->type); |
| 526 | buffer_put_int(&b, comp->enabled); |
| 527 | buffer_put_cstring(&b, comp->name); |
| 528 | |
| 529 | len = buffer_len(&b); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 530 | if (lenp != NULL) |
| 531 | *lenp = len; |
Ben Lindstrom | 2bf759c | 2002-07-07 22:13:31 +0000 | [diff] [blame] | 532 | if (blobp != NULL) { |
| 533 | *blobp = xmalloc(len); |
| 534 | memcpy(*blobp, buffer_ptr(&b), len); |
| 535 | } |
| 536 | memset(buffer_ptr(&b), 0, len); |
| 537 | buffer_free(&b); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 538 | return len; |
| 539 | } |
| 540 | |
| 541 | static void |
| 542 | mm_send_kex(Buffer *m, Kex *kex) |
| 543 | { |
| 544 | buffer_put_string(m, kex->session_id, kex->session_id_len); |
| 545 | buffer_put_int(m, kex->we_need); |
| 546 | buffer_put_int(m, kex->hostkey_type); |
| 547 | buffer_put_int(m, kex->kex_type); |
| 548 | buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my)); |
| 549 | buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer)); |
| 550 | buffer_put_int(m, kex->flags); |
| 551 | buffer_put_cstring(m, kex->client_version_string); |
| 552 | buffer_put_cstring(m, kex->server_version_string); |
| 553 | } |
| 554 | |
| 555 | void |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 556 | mm_send_keystate(struct monitor *monitor) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 557 | { |
| 558 | Buffer m; |
| 559 | u_char *blob, *p; |
| 560 | u_int bloblen, plen; |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 561 | u_int32_t seqnr, packets; |
| 562 | u_int64_t blocks; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 563 | |
| 564 | buffer_init(&m); |
| 565 | |
| 566 | if (!compat20) { |
| 567 | u_char iv[24]; |
Ben Lindstrom | 402c6cc | 2002-06-21 00:43:42 +0000 | [diff] [blame] | 568 | u_char *key; |
| 569 | u_int ivlen, keylen; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 570 | |
| 571 | buffer_put_int(&m, packet_get_protocol_flags()); |
| 572 | |
| 573 | buffer_put_int(&m, packet_get_ssh1_cipher()); |
| 574 | |
Ben Lindstrom | 402c6cc | 2002-06-21 00:43:42 +0000 | [diff] [blame] | 575 | debug3("%s: Sending ssh1 KEY+IV", __func__); |
| 576 | keylen = packet_get_encryption_key(NULL); |
| 577 | key = xmalloc(keylen+1); /* add 1 if keylen == 0 */ |
| 578 | keylen = packet_get_encryption_key(key); |
| 579 | buffer_put_string(&m, key, keylen); |
| 580 | memset(key, 0, keylen); |
| 581 | xfree(key); |
| 582 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 583 | ivlen = packet_get_keyiv_len(MODE_OUT); |
| 584 | packet_get_keyiv(MODE_OUT, iv, ivlen); |
| 585 | buffer_put_string(&m, iv, ivlen); |
| 586 | ivlen = packet_get_keyiv_len(MODE_OUT); |
| 587 | packet_get_keyiv(MODE_IN, iv, ivlen); |
| 588 | buffer_put_string(&m, iv, ivlen); |
| 589 | goto skip; |
| 590 | } else { |
| 591 | /* Kex for rekeying */ |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 592 | mm_send_kex(&m, *monitor->m_pkex); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | debug3("%s: Sending new keys: %p %p", |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 596 | __func__, newkeys[MODE_OUT], newkeys[MODE_IN]); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 597 | |
| 598 | /* Keys from Kex */ |
| 599 | if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen)) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 600 | fatal("%s: conversion of newkeys failed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 601 | |
| 602 | buffer_put_string(&m, blob, bloblen); |
| 603 | xfree(blob); |
| 604 | |
| 605 | if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen)) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 606 | fatal("%s: conversion of newkeys failed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 607 | |
| 608 | buffer_put_string(&m, blob, bloblen); |
| 609 | xfree(blob); |
| 610 | |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 611 | packet_get_state(MODE_OUT, &seqnr, &blocks, &packets); |
| 612 | buffer_put_int(&m, seqnr); |
| 613 | buffer_put_int64(&m, blocks); |
| 614 | buffer_put_int(&m, packets); |
Damien Miller | b1ecd9c | 2003-04-09 20:51:24 +1000 | [diff] [blame] | 615 | packet_get_state(MODE_IN, &seqnr, &blocks, &packets); |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 616 | buffer_put_int(&m, seqnr); |
| 617 | buffer_put_int64(&m, blocks); |
| 618 | buffer_put_int(&m, packets); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 619 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 620 | debug3("%s: New keys have been sent", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 621 | skip: |
| 622 | /* More key context */ |
| 623 | plen = packet_get_keycontext(MODE_OUT, NULL); |
| 624 | p = xmalloc(plen+1); |
| 625 | packet_get_keycontext(MODE_OUT, p); |
| 626 | buffer_put_string(&m, p, plen); |
| 627 | xfree(p); |
| 628 | |
| 629 | plen = packet_get_keycontext(MODE_IN, NULL); |
| 630 | p = xmalloc(plen+1); |
| 631 | packet_get_keycontext(MODE_IN, p); |
| 632 | buffer_put_string(&m, p, plen); |
| 633 | xfree(p); |
| 634 | |
| 635 | /* Compression state */ |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 636 | debug3("%s: Sending compression state", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 637 | buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream)); |
| 638 | buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream)); |
| 639 | |
| 640 | /* Network I/O buffers */ |
| 641 | buffer_put_string(&m, buffer_ptr(&input), buffer_len(&input)); |
| 642 | buffer_put_string(&m, buffer_ptr(&output), buffer_len(&output)); |
| 643 | |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 644 | mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m); |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 645 | debug3("%s: Finished sending state", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 646 | |
| 647 | buffer_free(&m); |
| 648 | } |
| 649 | |
| 650 | int |
Damien Miller | 71a7367 | 2006-03-26 14:04:36 +1100 | [diff] [blame] | 651 | mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 652 | { |
| 653 | Buffer m; |
Darren Tucker | 0999174 | 2004-07-17 17:05:14 +1000 | [diff] [blame] | 654 | char *p, *msg; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 655 | int success = 0; |
| 656 | |
| 657 | buffer_init(&m); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 658 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 659 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 660 | debug3("%s: waiting for MONITOR_ANS_PTY", __func__); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 661 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 662 | |
| 663 | success = buffer_get_int(&m); |
| 664 | if (success == 0) { |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 665 | debug3("%s: pty alloc failed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 666 | buffer_free(&m); |
| 667 | return (0); |
| 668 | } |
| 669 | p = buffer_get_string(&m, NULL); |
Darren Tucker | 0999174 | 2004-07-17 17:05:14 +1000 | [diff] [blame] | 670 | msg = buffer_get_string(&m, NULL); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 671 | buffer_free(&m); |
| 672 | |
| 673 | strlcpy(namebuf, p, namebuflen); /* Possible truncation */ |
| 674 | xfree(p); |
| 675 | |
Darren Tucker | 0999174 | 2004-07-17 17:05:14 +1000 | [diff] [blame] | 676 | buffer_append(&loginmsg, msg, strlen(msg)); |
| 677 | xfree(msg); |
| 678 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 679 | *ptyfd = mm_receive_fd(pmonitor->m_recvfd); |
| 680 | *ttyfd = mm_receive_fd(pmonitor->m_recvfd); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 681 | |
| 682 | /* Success */ |
| 683 | return (1); |
| 684 | } |
| 685 | |
| 686 | void |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 687 | mm_session_pty_cleanup2(Session *s) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 688 | { |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 689 | Buffer m; |
| 690 | |
| 691 | if (s->ttyfd == -1) |
| 692 | return; |
| 693 | buffer_init(&m); |
| 694 | buffer_put_cstring(&m, s->tty); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 695 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 696 | buffer_free(&m); |
| 697 | |
| 698 | /* closed dup'ed master */ |
| 699 | if (close(s->ptymaster) < 0) |
| 700 | error("close(s->ptymaster): %s", strerror(errno)); |
| 701 | |
| 702 | /* unlink pty from session */ |
| 703 | s->ttyfd = -1; |
| 704 | } |
| 705 | |
Damien Miller | 7941855 | 2002-04-23 20:28:48 +1000 | [diff] [blame] | 706 | #ifdef USE_PAM |
| 707 | void |
Darren Tucker | dbf7a74 | 2004-03-08 23:04:06 +1100 | [diff] [blame] | 708 | mm_start_pam(Authctxt *authctxt) |
Damien Miller | 7941855 | 2002-04-23 20:28:48 +1000 | [diff] [blame] | 709 | { |
| 710 | Buffer m; |
| 711 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 712 | debug3("%s entering", __func__); |
Damien Miller | 4e448a3 | 2003-05-14 15:11:48 +1000 | [diff] [blame] | 713 | if (!options.use_pam) |
| 714 | fatal("UsePAM=no, but ended up in %s anyway", __func__); |
Damien Miller | 7941855 | 2002-04-23 20:28:48 +1000 | [diff] [blame] | 715 | |
| 716 | buffer_init(&m); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 717 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m); |
Damien Miller | 7941855 | 2002-04-23 20:28:48 +1000 | [diff] [blame] | 718 | |
| 719 | buffer_free(&m); |
| 720 | } |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 721 | |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 722 | u_int |
| 723 | mm_do_pam_account(void) |
| 724 | { |
| 725 | Buffer m; |
| 726 | u_int ret; |
Darren Tucker | 77fc29e | 2004-09-11 23:07:03 +1000 | [diff] [blame] | 727 | char *msg; |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 728 | |
| 729 | debug3("%s entering", __func__); |
| 730 | if (!options.use_pam) |
| 731 | fatal("UsePAM=no, but ended up in %s anyway", __func__); |
| 732 | |
| 733 | buffer_init(&m); |
| 734 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m); |
| 735 | |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 736 | mm_request_receive_expect(pmonitor->m_recvfd, |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 737 | MONITOR_ANS_PAM_ACCOUNT, &m); |
| 738 | ret = buffer_get_int(&m); |
Darren Tucker | 77fc29e | 2004-09-11 23:07:03 +1000 | [diff] [blame] | 739 | msg = buffer_get_string(&m, NULL); |
| 740 | buffer_append(&loginmsg, msg, strlen(msg)); |
| 741 | xfree(msg); |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 742 | |
| 743 | buffer_free(&m); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 744 | |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 745 | debug3("%s returning %d", __func__, ret); |
| 746 | |
| 747 | return (ret); |
| 748 | } |
| 749 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 750 | void * |
| 751 | mm_sshpam_init_ctx(Authctxt *authctxt) |
| 752 | { |
| 753 | Buffer m; |
| 754 | int success; |
| 755 | |
| 756 | debug3("%s", __func__); |
| 757 | buffer_init(&m); |
| 758 | buffer_put_cstring(&m, authctxt->user); |
| 759 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m); |
| 760 | debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__); |
| 761 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m); |
| 762 | success = buffer_get_int(&m); |
| 763 | if (success == 0) { |
| 764 | debug3("%s: pam_init_ctx failed", __func__); |
| 765 | buffer_free(&m); |
| 766 | return (NULL); |
| 767 | } |
| 768 | buffer_free(&m); |
| 769 | return (authctxt); |
| 770 | } |
| 771 | |
| 772 | int |
| 773 | mm_sshpam_query(void *ctx, char **name, char **info, |
| 774 | u_int *num, char ***prompts, u_int **echo_on) |
| 775 | { |
| 776 | Buffer m; |
Damien Miller | 04b6533 | 2005-07-17 17:53:31 +1000 | [diff] [blame] | 777 | u_int i; |
| 778 | int ret; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 779 | |
| 780 | debug3("%s", __func__); |
| 781 | buffer_init(&m); |
| 782 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m); |
| 783 | debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__); |
| 784 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m); |
| 785 | ret = buffer_get_int(&m); |
| 786 | debug3("%s: pam_query returned %d", __func__, ret); |
| 787 | *name = buffer_get_string(&m, NULL); |
| 788 | *info = buffer_get_string(&m, NULL); |
| 789 | *num = buffer_get_int(&m); |
Darren Tucker | d8093e4 | 2006-05-04 16:24:34 +1000 | [diff] [blame] | 790 | if (*num > PAM_MAX_NUM_MSG) |
| 791 | fatal("%s: recieved %u PAM messages, expected <= %u", |
| 792 | __func__, *num, PAM_MAX_NUM_MSG); |
| 793 | *prompts = xcalloc((*num + 1), sizeof(char *)); |
| 794 | *echo_on = xcalloc((*num + 1), sizeof(u_int)); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 795 | for (i = 0; i < *num; ++i) { |
| 796 | (*prompts)[i] = buffer_get_string(&m, NULL); |
| 797 | (*echo_on)[i] = buffer_get_int(&m); |
| 798 | } |
| 799 | buffer_free(&m); |
| 800 | return (ret); |
| 801 | } |
| 802 | |
| 803 | int |
| 804 | mm_sshpam_respond(void *ctx, u_int num, char **resp) |
| 805 | { |
| 806 | Buffer m; |
Damien Miller | 04b6533 | 2005-07-17 17:53:31 +1000 | [diff] [blame] | 807 | u_int i; |
| 808 | int ret; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 809 | |
| 810 | debug3("%s", __func__); |
| 811 | buffer_init(&m); |
| 812 | buffer_put_int(&m, num); |
| 813 | for (i = 0; i < num; ++i) |
| 814 | buffer_put_cstring(&m, resp[i]); |
| 815 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m); |
| 816 | debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__); |
| 817 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m); |
| 818 | ret = buffer_get_int(&m); |
| 819 | debug3("%s: pam_respond returned %d", __func__, ret); |
| 820 | buffer_free(&m); |
| 821 | return (ret); |
| 822 | } |
| 823 | |
| 824 | void |
| 825 | mm_sshpam_free_ctx(void *ctxtp) |
| 826 | { |
| 827 | Buffer m; |
| 828 | |
| 829 | debug3("%s", __func__); |
| 830 | buffer_init(&m); |
| 831 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m); |
| 832 | debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__); |
| 833 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m); |
| 834 | buffer_free(&m); |
| 835 | } |
Damien Miller | 7941855 | 2002-04-23 20:28:48 +1000 | [diff] [blame] | 836 | #endif /* USE_PAM */ |
| 837 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 838 | /* Request process termination */ |
| 839 | |
| 840 | void |
| 841 | mm_terminate(void) |
| 842 | { |
| 843 | Buffer m; |
| 844 | |
| 845 | buffer_init(&m); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 846 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 847 | buffer_free(&m); |
| 848 | } |
| 849 | |
| 850 | int |
| 851 | mm_ssh1_session_key(BIGNUM *num) |
| 852 | { |
| 853 | int rsafail; |
| 854 | Buffer m; |
| 855 | |
| 856 | buffer_init(&m); |
| 857 | buffer_put_bignum2(&m, num); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 858 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 859 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 860 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 861 | |
| 862 | rsafail = buffer_get_int(&m); |
| 863 | buffer_get_bignum2(&m, num); |
| 864 | |
| 865 | buffer_free(&m); |
| 866 | |
| 867 | return (rsafail); |
| 868 | } |
| 869 | |
| 870 | static void |
| 871 | mm_chall_setup(char **name, char **infotxt, u_int *numprompts, |
| 872 | char ***prompts, u_int **echo_on) |
| 873 | { |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 874 | *name = xstrdup(""); |
| 875 | *infotxt = xstrdup(""); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 876 | *numprompts = 1; |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 877 | *prompts = xcalloc(*numprompts, sizeof(char *)); |
| 878 | *echo_on = xcalloc(*numprompts, sizeof(u_int)); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 879 | (*echo_on)[0] = 0; |
| 880 | } |
| 881 | |
| 882 | int |
| 883 | mm_bsdauth_query(void *ctx, char **name, char **infotxt, |
| 884 | u_int *numprompts, char ***prompts, u_int **echo_on) |
| 885 | { |
| 886 | Buffer m; |
Damien Miller | b7df3af | 2003-02-24 11:55:46 +1100 | [diff] [blame] | 887 | u_int success; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 888 | char *challenge; |
| 889 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 890 | debug3("%s: entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 891 | |
| 892 | buffer_init(&m); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 893 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 894 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 895 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 896 | &m); |
Damien Miller | b7df3af | 2003-02-24 11:55:46 +1100 | [diff] [blame] | 897 | success = buffer_get_int(&m); |
| 898 | if (success == 0) { |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 899 | debug3("%s: no challenge", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 900 | buffer_free(&m); |
| 901 | return (-1); |
| 902 | } |
| 903 | |
| 904 | /* Get the challenge, and format the response */ |
| 905 | challenge = buffer_get_string(&m, NULL); |
| 906 | buffer_free(&m); |
| 907 | |
| 908 | mm_chall_setup(name, infotxt, numprompts, prompts, echo_on); |
| 909 | (*prompts)[0] = challenge; |
| 910 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 911 | debug3("%s: received challenge: %s", __func__, challenge); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 912 | |
| 913 | return (0); |
| 914 | } |
| 915 | |
| 916 | int |
| 917 | mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses) |
| 918 | { |
| 919 | Buffer m; |
| 920 | int authok; |
| 921 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 922 | debug3("%s: entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 923 | if (numresponses != 1) |
| 924 | return (-1); |
| 925 | |
| 926 | buffer_init(&m); |
| 927 | buffer_put_cstring(&m, responses[0]); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 928 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 929 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 930 | mm_request_receive_expect(pmonitor->m_recvfd, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 931 | MONITOR_ANS_BSDAUTHRESPOND, &m); |
| 932 | |
| 933 | authok = buffer_get_int(&m); |
| 934 | buffer_free(&m); |
| 935 | |
| 936 | return ((authok == 0) ? -1 : 0); |
| 937 | } |
| 938 | |
Darren Tucker | 042e2e8 | 2004-07-08 23:09:42 +1000 | [diff] [blame] | 939 | #ifdef SKEY |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 940 | int |
| 941 | mm_skey_query(void *ctx, char **name, char **infotxt, |
| 942 | u_int *numprompts, char ***prompts, u_int **echo_on) |
| 943 | { |
| 944 | Buffer m; |
Damien Miller | b7df3af | 2003-02-24 11:55:46 +1100 | [diff] [blame] | 945 | u_int success; |
Darren Tucker | d6a23f2 | 2006-08-05 18:50:35 +1000 | [diff] [blame] | 946 | char *challenge; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 947 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 948 | debug3("%s: entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 949 | |
| 950 | buffer_init(&m); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 951 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 952 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 953 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 954 | &m); |
Damien Miller | b7df3af | 2003-02-24 11:55:46 +1100 | [diff] [blame] | 955 | success = buffer_get_int(&m); |
| 956 | if (success == 0) { |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 957 | debug3("%s: no challenge", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 958 | buffer_free(&m); |
| 959 | return (-1); |
| 960 | } |
| 961 | |
| 962 | /* Get the challenge, and format the response */ |
| 963 | challenge = buffer_get_string(&m, NULL); |
| 964 | buffer_free(&m); |
| 965 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 966 | debug3("%s: received challenge: %s", __func__, challenge); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 967 | |
| 968 | mm_chall_setup(name, infotxt, numprompts, prompts, echo_on); |
| 969 | |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 970 | xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 971 | xfree(challenge); |
| 972 | |
| 973 | return (0); |
| 974 | } |
| 975 | |
| 976 | int |
| 977 | mm_skey_respond(void *ctx, u_int numresponses, char **responses) |
| 978 | { |
| 979 | Buffer m; |
| 980 | int authok; |
| 981 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 982 | debug3("%s: entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 983 | if (numresponses != 1) |
| 984 | return (-1); |
| 985 | |
| 986 | buffer_init(&m); |
| 987 | buffer_put_cstring(&m, responses[0]); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 988 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 989 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 990 | mm_request_receive_expect(pmonitor->m_recvfd, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 991 | MONITOR_ANS_SKEYRESPOND, &m); |
| 992 | |
| 993 | authok = buffer_get_int(&m); |
| 994 | buffer_free(&m); |
| 995 | |
| 996 | return ((authok == 0) ? -1 : 0); |
| 997 | } |
Darren Tucker | 042e2e8 | 2004-07-08 23:09:42 +1000 | [diff] [blame] | 998 | #endif /* SKEY */ |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 999 | |
| 1000 | void |
| 1001 | mm_ssh1_session_id(u_char session_id[16]) |
| 1002 | { |
| 1003 | Buffer m; |
| 1004 | int i; |
| 1005 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1006 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1007 | |
| 1008 | buffer_init(&m); |
| 1009 | for (i = 0; i < 16; i++) |
| 1010 | buffer_put_char(&m, session_id[i]); |
| 1011 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 1012 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1013 | buffer_free(&m); |
| 1014 | } |
| 1015 | |
| 1016 | int |
| 1017 | mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey) |
| 1018 | { |
| 1019 | Buffer m; |
| 1020 | Key *key; |
| 1021 | u_char *blob; |
| 1022 | u_int blen; |
Damien Miller | 06ebedf | 2003-02-24 12:03:38 +1100 | [diff] [blame] | 1023 | int allowed = 0, have_forced = 0; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1024 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1025 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1026 | |
| 1027 | buffer_init(&m); |
| 1028 | buffer_put_bignum2(&m, client_n); |
| 1029 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 1030 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m); |
| 1031 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1032 | |
| 1033 | allowed = buffer_get_int(&m); |
| 1034 | |
Damien Miller | 06ebedf | 2003-02-24 12:03:38 +1100 | [diff] [blame] | 1035 | /* fake forced command */ |
| 1036 | auth_clear_options(); |
| 1037 | have_forced = buffer_get_int(&m); |
| 1038 | forced_command = have_forced ? xstrdup("true") : NULL; |
| 1039 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1040 | if (allowed && rkey != NULL) { |
| 1041 | blob = buffer_get_string(&m, &blen); |
| 1042 | if ((key = key_from_blob(blob, blen)) == NULL) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1043 | fatal("%s: key_from_blob failed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1044 | *rkey = key; |
| 1045 | xfree(blob); |
| 1046 | } |
| 1047 | mm_send_debug(&m); |
| 1048 | buffer_free(&m); |
| 1049 | |
| 1050 | return (allowed); |
| 1051 | } |
| 1052 | |
| 1053 | BIGNUM * |
| 1054 | mm_auth_rsa_generate_challenge(Key *key) |
| 1055 | { |
| 1056 | Buffer m; |
| 1057 | BIGNUM *challenge; |
| 1058 | u_char *blob; |
| 1059 | u_int blen; |
| 1060 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1061 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1062 | |
| 1063 | if ((challenge = BN_new()) == NULL) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1064 | fatal("%s: BN_new failed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1065 | |
| 1066 | key->type = KEY_RSA; /* XXX cheat for key_to_blob */ |
| 1067 | if (key_to_blob(key, &blob, &blen) == 0) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1068 | fatal("%s: key_to_blob failed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1069 | key->type = KEY_RSA1; |
| 1070 | |
| 1071 | buffer_init(&m); |
| 1072 | buffer_put_string(&m, blob, blen); |
| 1073 | xfree(blob); |
| 1074 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 1075 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m); |
| 1076 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1077 | |
| 1078 | buffer_get_bignum2(&m, challenge); |
| 1079 | buffer_free(&m); |
| 1080 | |
| 1081 | return (challenge); |
| 1082 | } |
| 1083 | |
| 1084 | int |
| 1085 | mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16]) |
| 1086 | { |
| 1087 | Buffer m; |
| 1088 | u_char *blob; |
| 1089 | u_int blen; |
| 1090 | int success = 0; |
| 1091 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1092 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1093 | |
| 1094 | key->type = KEY_RSA; /* XXX cheat for key_to_blob */ |
| 1095 | if (key_to_blob(key, &blob, &blen) == 0) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1096 | fatal("%s: key_to_blob failed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1097 | key->type = KEY_RSA1; |
| 1098 | |
| 1099 | buffer_init(&m); |
| 1100 | buffer_put_string(&m, blob, blen); |
| 1101 | buffer_put_string(&m, response, 16); |
| 1102 | xfree(blob); |
| 1103 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 1104 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m); |
| 1105 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1106 | |
| 1107 | success = buffer_get_int(&m); |
| 1108 | buffer_free(&m); |
| 1109 | |
| 1110 | return (success); |
| 1111 | } |
Damien Miller | 25162f2 | 2002-09-12 09:47:29 +1000 | [diff] [blame] | 1112 | |
Darren Tucker | 2e0cf0d | 2005-02-08 21:52:47 +1100 | [diff] [blame] | 1113 | #ifdef SSH_AUDIT_EVENTS |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 1114 | void |
| 1115 | mm_audit_event(ssh_audit_event_t event) |
| 1116 | { |
| 1117 | Buffer m; |
| 1118 | |
| 1119 | debug3("%s entering", __func__); |
| 1120 | |
| 1121 | buffer_init(&m); |
| 1122 | buffer_put_int(&m, event); |
| 1123 | |
| 1124 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m); |
| 1125 | buffer_free(&m); |
| 1126 | } |
| 1127 | |
| 1128 | void |
| 1129 | mm_audit_run_command(const char *command) |
| 1130 | { |
| 1131 | Buffer m; |
| 1132 | |
| 1133 | debug3("%s entering command %s", __func__, command); |
| 1134 | |
| 1135 | buffer_init(&m); |
| 1136 | buffer_put_cstring(&m, command); |
| 1137 | |
| 1138 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m); |
| 1139 | buffer_free(&m); |
| 1140 | } |
Darren Tucker | 2e0cf0d | 2005-02-08 21:52:47 +1100 | [diff] [blame] | 1141 | #endif /* SSH_AUDIT_EVENTS */ |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 1142 | |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 1143 | #ifdef GSSAPI |
| 1144 | OM_uint32 |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1145 | mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid) |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 1146 | { |
| 1147 | Buffer m; |
| 1148 | OM_uint32 major; |
| 1149 | |
| 1150 | /* Client doesn't get to see the context */ |
| 1151 | *ctx = NULL; |
| 1152 | |
| 1153 | buffer_init(&m); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1154 | buffer_put_string(&m, goid->elements, goid->length); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 1155 | |
| 1156 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m); |
| 1157 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m); |
| 1158 | |
| 1159 | major = buffer_get_int(&m); |
| 1160 | |
| 1161 | buffer_free(&m); |
| 1162 | return (major); |
| 1163 | } |
| 1164 | |
| 1165 | OM_uint32 |
| 1166 | mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in, |
| 1167 | gss_buffer_desc *out, OM_uint32 *flags) |
| 1168 | { |
| 1169 | Buffer m; |
| 1170 | OM_uint32 major; |
Darren Tucker | 600ad8d | 2003-08-26 12:10:48 +1000 | [diff] [blame] | 1171 | u_int len; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 1172 | |
| 1173 | buffer_init(&m); |
| 1174 | buffer_put_string(&m, in->value, in->length); |
| 1175 | |
| 1176 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m); |
| 1177 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m); |
| 1178 | |
| 1179 | major = buffer_get_int(&m); |
Darren Tucker | 600ad8d | 2003-08-26 12:10:48 +1000 | [diff] [blame] | 1180 | out->value = buffer_get_string(&m, &len); |
| 1181 | out->length = len; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 1182 | if (flags) |
| 1183 | *flags = buffer_get_int(&m); |
| 1184 | |
| 1185 | buffer_free(&m); |
| 1186 | |
| 1187 | return (major); |
| 1188 | } |
| 1189 | |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 1190 | OM_uint32 |
| 1191 | mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic) |
| 1192 | { |
| 1193 | Buffer m; |
| 1194 | OM_uint32 major; |
| 1195 | |
| 1196 | buffer_init(&m); |
| 1197 | buffer_put_string(&m, gssbuf->value, gssbuf->length); |
| 1198 | buffer_put_string(&m, gssmic->value, gssmic->length); |
| 1199 | |
| 1200 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m); |
| 1201 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC, |
| 1202 | &m); |
| 1203 | |
| 1204 | major = buffer_get_int(&m); |
| 1205 | buffer_free(&m); |
| 1206 | return(major); |
| 1207 | } |
| 1208 | |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 1209 | int |
| 1210 | mm_ssh_gssapi_userok(char *user) |
| 1211 | { |
| 1212 | Buffer m; |
| 1213 | int authenticated = 0; |
| 1214 | |
| 1215 | buffer_init(&m); |
| 1216 | |
| 1217 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m); |
| 1218 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK, |
| 1219 | &m); |
| 1220 | |
| 1221 | authenticated = buffer_get_int(&m); |
| 1222 | |
| 1223 | buffer_free(&m); |
| 1224 | debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not "); |
| 1225 | return (authenticated); |
| 1226 | } |
| 1227 | #endif /* GSSAPI */ |