Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2002 Niels Provos <provos@citi.umich.edu> |
| 3 | * Copyright 2002 Markus Friedl <markus@openbsd.org> |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 16 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 17 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 18 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 19 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 20 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 21 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 22 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 24 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | |
| 27 | #include "includes.h" |
Ben Lindstrom | 402c6cc | 2002-06-21 00:43:42 +0000 | [diff] [blame] | 28 | RCSID("$OpenBSD: monitor_wrap.c,v 1.11 2002/06/19 18:01:00 markus Exp $"); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 29 | |
| 30 | #include <openssl/bn.h> |
| 31 | #include <openssl/dh.h> |
| 32 | |
| 33 | #include "ssh.h" |
| 34 | #include "dh.h" |
| 35 | #include "kex.h" |
| 36 | #include "auth.h" |
| 37 | #include "buffer.h" |
| 38 | #include "bufaux.h" |
| 39 | #include "packet.h" |
| 40 | #include "mac.h" |
| 41 | #include "log.h" |
| 42 | #include "zlib.h" |
| 43 | #include "monitor.h" |
| 44 | #include "monitor_wrap.h" |
| 45 | #include "xmalloc.h" |
| 46 | #include "atomicio.h" |
| 47 | #include "monitor_fdpass.h" |
| 48 | #include "getput.h" |
| 49 | |
| 50 | #include "auth.h" |
| 51 | #include "channels.h" |
| 52 | #include "session.h" |
| 53 | |
| 54 | /* Imports */ |
| 55 | extern int compat20; |
| 56 | extern Newkeys *newkeys[]; |
| 57 | extern z_stream incoming_stream; |
| 58 | extern z_stream outgoing_stream; |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 59 | extern struct monitor *pmonitor; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 60 | extern Buffer input, output; |
| 61 | |
| 62 | void |
| 63 | mm_request_send(int socket, enum monitor_reqtype type, Buffer *m) |
| 64 | { |
| 65 | u_char buf[5]; |
| 66 | u_int mlen = buffer_len(m); |
| 67 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 68 | debug3("%s entering: type %d", __func__, type); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 69 | |
| 70 | PUT_32BIT(buf, mlen + 1); |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 71 | buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */ |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 72 | if (atomicio(write, socket, buf, sizeof(buf)) != sizeof(buf)) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 73 | fatal("%s: write", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 74 | if (atomicio(write, socket, buffer_ptr(m), mlen) != mlen) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 75 | fatal("%s: write", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void |
| 79 | mm_request_receive(int socket, Buffer *m) |
| 80 | { |
| 81 | u_char buf[4]; |
| 82 | ssize_t res; |
| 83 | u_int msg_len; |
| 84 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 85 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 86 | |
| 87 | res = atomicio(read, socket, buf, sizeof(buf)); |
| 88 | if (res != sizeof(buf)) { |
| 89 | if (res == 0) |
| 90 | fatal_cleanup(); |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 91 | fatal("%s: read: %ld", __func__, (long)res); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 92 | } |
| 93 | msg_len = GET_32BIT(buf); |
| 94 | if (msg_len > 256 * 1024) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 95 | fatal("%s: read: bad msg_len %d", __func__, msg_len); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 96 | buffer_clear(m); |
| 97 | buffer_append_space(m, msg_len); |
| 98 | res = atomicio(read, socket, buffer_ptr(m), msg_len); |
| 99 | if (res != msg_len) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 100 | fatal("%s: read: %ld != msg_len", __func__, (long)res); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void |
| 104 | mm_request_receive_expect(int socket, enum monitor_reqtype type, Buffer *m) |
| 105 | { |
| 106 | u_char rtype; |
| 107 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 108 | debug3("%s entering: type %d", __func__, type); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 109 | |
| 110 | mm_request_receive(socket, m); |
| 111 | rtype = buffer_get_char(m); |
| 112 | if (rtype != type) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 113 | fatal("%s: read: rtype %d != type %d", __func__, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 114 | rtype, type); |
| 115 | } |
| 116 | |
| 117 | DH * |
| 118 | mm_choose_dh(int min, int nbits, int max) |
| 119 | { |
| 120 | BIGNUM *p, *g; |
| 121 | int success = 0; |
| 122 | Buffer m; |
| 123 | |
| 124 | buffer_init(&m); |
| 125 | buffer_put_int(&m, min); |
| 126 | buffer_put_int(&m, nbits); |
| 127 | buffer_put_int(&m, max); |
| 128 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 129 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 130 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 131 | debug3("%s: waiting for MONITOR_ANS_MODULI", __func__); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 132 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 133 | |
| 134 | success = buffer_get_char(&m); |
| 135 | if (success == 0) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 136 | fatal("%s: MONITOR_ANS_MODULI failed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 137 | |
| 138 | if ((p = BN_new()) == NULL) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 139 | fatal("%s: BN_new failed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 140 | if ((g = BN_new()) == NULL) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 141 | fatal("%s: BN_new failed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 142 | buffer_get_bignum2(&m, p); |
| 143 | buffer_get_bignum2(&m, g); |
| 144 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 145 | debug3("%s: remaining %d", __func__, buffer_len(&m)); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 146 | buffer_free(&m); |
| 147 | |
| 148 | return (dh_new_group(g, p)); |
| 149 | } |
| 150 | |
| 151 | int |
| 152 | mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen) |
| 153 | { |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 154 | Kex *kex = *pmonitor->m_pkex; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 155 | Buffer m; |
| 156 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 157 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 158 | |
| 159 | buffer_init(&m); |
| 160 | buffer_put_int(&m, kex->host_key_index(key)); |
| 161 | buffer_put_string(&m, data, datalen); |
| 162 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 163 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 164 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 165 | debug3("%s: waiting for MONITOR_ANS_SIGN", __func__); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 166 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 167 | *sigp = buffer_get_string(&m, lenp); |
| 168 | buffer_free(&m); |
| 169 | |
| 170 | return (0); |
| 171 | } |
| 172 | |
| 173 | struct passwd * |
| 174 | mm_getpwnamallow(const char *login) |
| 175 | { |
| 176 | Buffer m; |
| 177 | struct passwd *pw; |
| 178 | u_int pwlen; |
| 179 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 180 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 181 | |
| 182 | buffer_init(&m); |
| 183 | buffer_put_cstring(&m, login); |
| 184 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 185 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 186 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 187 | debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 188 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 189 | |
| 190 | if (buffer_get_char(&m) == 0) { |
| 191 | buffer_free(&m); |
| 192 | return (NULL); |
| 193 | } |
| 194 | pw = buffer_get_string(&m, &pwlen); |
| 195 | if (pwlen != sizeof(struct passwd)) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 196 | fatal("%s: struct passwd size mismatch", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 197 | pw->pw_name = buffer_get_string(&m, NULL); |
| 198 | pw->pw_passwd = buffer_get_string(&m, NULL); |
| 199 | pw->pw_gecos = buffer_get_string(&m, NULL); |
Kevin Steves | 7e14760 | 2002-03-22 18:07:17 +0000 | [diff] [blame] | 200 | #ifdef HAVE_PW_CLASS_IN_PASSWD |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 201 | pw->pw_class = buffer_get_string(&m, NULL); |
Kevin Steves | 7e14760 | 2002-03-22 18:07:17 +0000 | [diff] [blame] | 202 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 203 | pw->pw_dir = buffer_get_string(&m, NULL); |
| 204 | pw->pw_shell = buffer_get_string(&m, NULL); |
| 205 | buffer_free(&m); |
| 206 | |
| 207 | return (pw); |
| 208 | } |
| 209 | |
Damien Miller | 5ad9fd9 | 2002-05-13 11:07:41 +1000 | [diff] [blame] | 210 | char* mm_auth2_read_banner(void) |
| 211 | { |
| 212 | Buffer m; |
| 213 | char *banner; |
| 214 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 215 | debug3("%s entering", __func__); |
Damien Miller | 5ad9fd9 | 2002-05-13 11:07:41 +1000 | [diff] [blame] | 216 | |
| 217 | buffer_init(&m); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 218 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m); |
Damien Miller | 5ad9fd9 | 2002-05-13 11:07:41 +1000 | [diff] [blame] | 219 | buffer_clear(&m); |
| 220 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 221 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTH2_READ_BANNER, &m); |
Damien Miller | 5ad9fd9 | 2002-05-13 11:07:41 +1000 | [diff] [blame] | 222 | banner = buffer_get_string(&m, NULL); |
| 223 | buffer_free(&m); |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 224 | |
Damien Miller | 5ad9fd9 | 2002-05-13 11:07:41 +1000 | [diff] [blame] | 225 | return (banner); |
| 226 | } |
| 227 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 228 | /* Inform the privileged process about service and style */ |
| 229 | |
| 230 | void |
| 231 | mm_inform_authserv(char *service, char *style) |
| 232 | { |
| 233 | Buffer m; |
| 234 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 235 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 236 | |
| 237 | buffer_init(&m); |
| 238 | buffer_put_cstring(&m, service); |
| 239 | buffer_put_cstring(&m, style ? style : ""); |
| 240 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 241 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 242 | |
| 243 | buffer_free(&m); |
| 244 | } |
| 245 | |
| 246 | /* Do the password authentication */ |
| 247 | int |
| 248 | mm_auth_password(Authctxt *authctxt, char *password) |
| 249 | { |
| 250 | Buffer m; |
| 251 | int authenticated = 0; |
| 252 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 253 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 254 | |
| 255 | buffer_init(&m); |
| 256 | buffer_put_cstring(&m, password); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 257 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 258 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 259 | debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 260 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 261 | |
| 262 | authenticated = buffer_get_int(&m); |
| 263 | |
| 264 | buffer_free(&m); |
| 265 | |
| 266 | debug3("%s: user %sauthenticated", |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 267 | __func__, authenticated ? "" : "not "); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 268 | return (authenticated); |
| 269 | } |
| 270 | |
| 271 | int |
| 272 | mm_user_key_allowed(struct passwd *pw, Key *key) |
| 273 | { |
| 274 | return (mm_key_allowed(MM_USERKEY, NULL, NULL, key)); |
| 275 | } |
| 276 | |
| 277 | int |
| 278 | mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host, |
| 279 | Key *key) |
| 280 | { |
| 281 | return (mm_key_allowed(MM_HOSTKEY, user, host, key)); |
| 282 | } |
| 283 | |
| 284 | int |
| 285 | mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user, |
| 286 | char *host, Key *key) |
| 287 | { |
| 288 | int ret; |
| 289 | |
| 290 | key->type = KEY_RSA; /* XXX hack for key_to_blob */ |
| 291 | ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key); |
| 292 | key->type = KEY_RSA1; |
| 293 | return (ret); |
| 294 | } |
| 295 | |
| 296 | static void |
| 297 | mm_send_debug(Buffer *m) |
| 298 | { |
| 299 | char *msg; |
| 300 | |
| 301 | while (buffer_len(m)) { |
| 302 | msg = buffer_get_string(m, NULL); |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 303 | debug3("%s: Sending debug: %s", __func__, msg); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 304 | packet_send_debug("%s", msg); |
| 305 | xfree(msg); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | int |
| 310 | mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key) |
| 311 | { |
| 312 | Buffer m; |
| 313 | u_char *blob; |
| 314 | u_int len; |
| 315 | int allowed = 0; |
| 316 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 317 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 318 | |
| 319 | /* Convert the key to a blob and the pass it over */ |
| 320 | if (!key_to_blob(key, &blob, &len)) |
| 321 | return (0); |
| 322 | |
| 323 | buffer_init(&m); |
| 324 | buffer_put_int(&m, type); |
| 325 | buffer_put_cstring(&m, user ? user : ""); |
| 326 | buffer_put_cstring(&m, host ? host : ""); |
| 327 | buffer_put_string(&m, blob, len); |
| 328 | xfree(blob); |
| 329 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 330 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 331 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 332 | debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 333 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 334 | |
| 335 | allowed = buffer_get_int(&m); |
| 336 | |
| 337 | /* Send potential debug messages */ |
| 338 | mm_send_debug(&m); |
| 339 | |
| 340 | buffer_free(&m); |
| 341 | |
| 342 | return (allowed); |
| 343 | } |
| 344 | |
| 345 | /* |
| 346 | * This key verify needs to send the key type along, because the |
| 347 | * privileged parent makes the decision if the key is allowed |
| 348 | * for authentication. |
| 349 | */ |
| 350 | |
| 351 | int |
| 352 | mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen) |
| 353 | { |
| 354 | Buffer m; |
| 355 | u_char *blob; |
| 356 | u_int len; |
| 357 | int verified = 0; |
| 358 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 359 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 360 | |
| 361 | /* Convert the key to a blob and the pass it over */ |
| 362 | if (!key_to_blob(key, &blob, &len)) |
| 363 | return (0); |
| 364 | |
| 365 | buffer_init(&m); |
| 366 | buffer_put_string(&m, blob, len); |
| 367 | buffer_put_string(&m, sig, siglen); |
| 368 | buffer_put_string(&m, data, datalen); |
| 369 | xfree(blob); |
| 370 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 371 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 372 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 373 | debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 374 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 375 | |
| 376 | verified = buffer_get_int(&m); |
| 377 | |
| 378 | buffer_free(&m); |
| 379 | |
| 380 | return (verified); |
| 381 | } |
| 382 | |
| 383 | /* Export key state after authentication */ |
| 384 | Newkeys * |
| 385 | mm_newkeys_from_blob(u_char *blob, int blen) |
| 386 | { |
| 387 | Buffer b; |
| 388 | u_int len; |
| 389 | Newkeys *newkey = NULL; |
| 390 | Enc *enc; |
| 391 | Mac *mac; |
| 392 | Comp *comp; |
| 393 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 394 | debug3("%s: %p(%d)", __func__, blob, blen); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 395 | #ifdef DEBUG_PK |
| 396 | dump_base64(stderr, blob, blen); |
| 397 | #endif |
| 398 | buffer_init(&b); |
| 399 | buffer_append(&b, blob, blen); |
| 400 | |
| 401 | newkey = xmalloc(sizeof(*newkey)); |
| 402 | enc = &newkey->enc; |
| 403 | mac = &newkey->mac; |
| 404 | comp = &newkey->comp; |
| 405 | |
| 406 | /* Enc structure */ |
| 407 | enc->name = buffer_get_string(&b, NULL); |
| 408 | buffer_get(&b, &enc->cipher, sizeof(enc->cipher)); |
| 409 | enc->enabled = buffer_get_int(&b); |
| 410 | enc->block_size = buffer_get_int(&b); |
| 411 | enc->key = buffer_get_string(&b, &enc->key_len); |
| 412 | enc->iv = buffer_get_string(&b, &len); |
| 413 | if (len != enc->block_size) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 414 | fatal("%s: bad ivlen: expected %d != %d", __func__, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 415 | enc->block_size, len); |
| 416 | |
| 417 | if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 418 | fatal("%s: bad cipher name %s or pointer %p", __func__, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 419 | enc->name, enc->cipher); |
| 420 | |
| 421 | /* Mac structure */ |
| 422 | mac->name = buffer_get_string(&b, NULL); |
| 423 | if (mac->name == NULL || mac_init(mac, mac->name) == -1) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 424 | fatal("%s: can not init mac %s", __func__, mac->name); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 425 | mac->enabled = buffer_get_int(&b); |
| 426 | mac->key = buffer_get_string(&b, &len); |
| 427 | if (len > mac->key_len) |
Ben Lindstrom | 3c73dfe | 2002-06-20 23:53:53 +0000 | [diff] [blame] | 428 | fatal("%s: bad mac key length: %d > %d", __func__, len, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 429 | mac->key_len); |
| 430 | mac->key_len = len; |
| 431 | |
| 432 | /* Comp structure */ |
| 433 | comp->type = buffer_get_int(&b); |
| 434 | comp->enabled = buffer_get_int(&b); |
| 435 | comp->name = buffer_get_string(&b, NULL); |
| 436 | |
| 437 | len = buffer_len(&b); |
| 438 | if (len != 0) |
| 439 | error("newkeys_from_blob: remaining bytes in blob %d", len); |
| 440 | buffer_free(&b); |
| 441 | return (newkey); |
| 442 | } |
| 443 | |
| 444 | int |
| 445 | mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp) |
| 446 | { |
| 447 | Buffer b; |
| 448 | int len; |
| 449 | u_char *buf; |
| 450 | Enc *enc; |
| 451 | Mac *mac; |
| 452 | Comp *comp; |
| 453 | Newkeys *newkey = newkeys[mode]; |
| 454 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 455 | debug3("%s: converting %p", __func__, newkey); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 456 | |
| 457 | if (newkey == NULL) { |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 458 | error("%s: newkey == NULL", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 459 | return 0; |
| 460 | } |
| 461 | enc = &newkey->enc; |
| 462 | mac = &newkey->mac; |
| 463 | comp = &newkey->comp; |
| 464 | |
| 465 | buffer_init(&b); |
| 466 | /* Enc structure */ |
| 467 | buffer_put_cstring(&b, enc->name); |
| 468 | /* The cipher struct is constant and shared, you export pointer */ |
| 469 | buffer_append(&b, &enc->cipher, sizeof(enc->cipher)); |
| 470 | buffer_put_int(&b, enc->enabled); |
| 471 | buffer_put_int(&b, enc->block_size); |
| 472 | buffer_put_string(&b, enc->key, enc->key_len); |
| 473 | packet_get_keyiv(mode, enc->iv, enc->block_size); |
| 474 | buffer_put_string(&b, enc->iv, enc->block_size); |
| 475 | |
| 476 | /* Mac structure */ |
| 477 | buffer_put_cstring(&b, mac->name); |
| 478 | buffer_put_int(&b, mac->enabled); |
| 479 | buffer_put_string(&b, mac->key, mac->key_len); |
| 480 | |
| 481 | /* Comp structure */ |
| 482 | buffer_put_int(&b, comp->type); |
| 483 | buffer_put_int(&b, comp->enabled); |
| 484 | buffer_put_cstring(&b, comp->name); |
| 485 | |
| 486 | len = buffer_len(&b); |
| 487 | buf = xmalloc(len); |
| 488 | memcpy(buf, buffer_ptr(&b), len); |
| 489 | memset(buffer_ptr(&b), 0, len); |
| 490 | buffer_free(&b); |
| 491 | if (lenp != NULL) |
| 492 | *lenp = len; |
| 493 | if (blobp != NULL) |
| 494 | *blobp = buf; |
| 495 | return len; |
| 496 | } |
| 497 | |
| 498 | static void |
| 499 | mm_send_kex(Buffer *m, Kex *kex) |
| 500 | { |
| 501 | buffer_put_string(m, kex->session_id, kex->session_id_len); |
| 502 | buffer_put_int(m, kex->we_need); |
| 503 | buffer_put_int(m, kex->hostkey_type); |
| 504 | buffer_put_int(m, kex->kex_type); |
| 505 | buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my)); |
| 506 | buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer)); |
| 507 | buffer_put_int(m, kex->flags); |
| 508 | buffer_put_cstring(m, kex->client_version_string); |
| 509 | buffer_put_cstring(m, kex->server_version_string); |
| 510 | } |
| 511 | |
| 512 | void |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 513 | mm_send_keystate(struct monitor *pmonitor) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 514 | { |
| 515 | Buffer m; |
| 516 | u_char *blob, *p; |
| 517 | u_int bloblen, plen; |
| 518 | |
| 519 | buffer_init(&m); |
| 520 | |
| 521 | if (!compat20) { |
| 522 | u_char iv[24]; |
Ben Lindstrom | 402c6cc | 2002-06-21 00:43:42 +0000 | [diff] [blame] | 523 | u_char *key; |
| 524 | u_int ivlen, keylen; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 525 | |
| 526 | buffer_put_int(&m, packet_get_protocol_flags()); |
| 527 | |
| 528 | buffer_put_int(&m, packet_get_ssh1_cipher()); |
| 529 | |
Ben Lindstrom | 402c6cc | 2002-06-21 00:43:42 +0000 | [diff] [blame] | 530 | debug3("%s: Sending ssh1 KEY+IV", __func__); |
| 531 | keylen = packet_get_encryption_key(NULL); |
| 532 | key = xmalloc(keylen+1); /* add 1 if keylen == 0 */ |
| 533 | keylen = packet_get_encryption_key(key); |
| 534 | buffer_put_string(&m, key, keylen); |
| 535 | memset(key, 0, keylen); |
| 536 | xfree(key); |
| 537 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 538 | ivlen = packet_get_keyiv_len(MODE_OUT); |
| 539 | packet_get_keyiv(MODE_OUT, iv, ivlen); |
| 540 | buffer_put_string(&m, iv, ivlen); |
| 541 | ivlen = packet_get_keyiv_len(MODE_OUT); |
| 542 | packet_get_keyiv(MODE_IN, iv, ivlen); |
| 543 | buffer_put_string(&m, iv, ivlen); |
| 544 | goto skip; |
| 545 | } else { |
| 546 | /* Kex for rekeying */ |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 547 | mm_send_kex(&m, *pmonitor->m_pkex); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | debug3("%s: Sending new keys: %p %p", |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 551 | __func__, newkeys[MODE_OUT], newkeys[MODE_IN]); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 552 | |
| 553 | /* Keys from Kex */ |
| 554 | if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen)) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 555 | fatal("%s: conversion of newkeys failed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 556 | |
| 557 | buffer_put_string(&m, blob, bloblen); |
| 558 | xfree(blob); |
| 559 | |
| 560 | if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen)) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 561 | fatal("%s: conversion of newkeys failed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 562 | |
| 563 | buffer_put_string(&m, blob, bloblen); |
| 564 | xfree(blob); |
| 565 | |
| 566 | buffer_put_int(&m, packet_get_seqnr(MODE_OUT)); |
| 567 | buffer_put_int(&m, packet_get_seqnr(MODE_IN)); |
| 568 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 569 | debug3("%s: New keys have been sent", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 570 | skip: |
| 571 | /* More key context */ |
| 572 | plen = packet_get_keycontext(MODE_OUT, NULL); |
| 573 | p = xmalloc(plen+1); |
| 574 | packet_get_keycontext(MODE_OUT, p); |
| 575 | buffer_put_string(&m, p, plen); |
| 576 | xfree(p); |
| 577 | |
| 578 | plen = packet_get_keycontext(MODE_IN, NULL); |
| 579 | p = xmalloc(plen+1); |
| 580 | packet_get_keycontext(MODE_IN, p); |
| 581 | buffer_put_string(&m, p, plen); |
| 582 | xfree(p); |
| 583 | |
| 584 | /* Compression state */ |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 585 | debug3("%s: Sending compression state", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 586 | buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream)); |
| 587 | buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream)); |
| 588 | |
| 589 | /* Network I/O buffers */ |
| 590 | buffer_put_string(&m, buffer_ptr(&input), buffer_len(&input)); |
| 591 | buffer_put_string(&m, buffer_ptr(&output), buffer_len(&output)); |
| 592 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 593 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m); |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 594 | debug3("%s: Finished sending state", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 595 | |
| 596 | buffer_free(&m); |
| 597 | } |
| 598 | |
| 599 | int |
| 600 | mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, int namebuflen) |
| 601 | { |
| 602 | Buffer m; |
| 603 | u_char *p; |
| 604 | int success = 0; |
| 605 | |
| 606 | buffer_init(&m); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 607 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 608 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 609 | debug3("%s: waiting for MONITOR_ANS_PTY", __func__); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 610 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 611 | |
| 612 | success = buffer_get_int(&m); |
| 613 | if (success == 0) { |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 614 | debug3("%s: pty alloc failed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 615 | buffer_free(&m); |
| 616 | return (0); |
| 617 | } |
| 618 | p = buffer_get_string(&m, NULL); |
| 619 | buffer_free(&m); |
| 620 | |
| 621 | strlcpy(namebuf, p, namebuflen); /* Possible truncation */ |
| 622 | xfree(p); |
| 623 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 624 | *ptyfd = mm_receive_fd(pmonitor->m_recvfd); |
| 625 | *ttyfd = mm_receive_fd(pmonitor->m_recvfd); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 626 | |
| 627 | /* Success */ |
| 628 | return (1); |
| 629 | } |
| 630 | |
| 631 | void |
| 632 | mm_session_pty_cleanup2(void *session) |
| 633 | { |
| 634 | Session *s = session; |
| 635 | Buffer m; |
| 636 | |
| 637 | if (s->ttyfd == -1) |
| 638 | return; |
| 639 | buffer_init(&m); |
| 640 | buffer_put_cstring(&m, s->tty); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 641 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 642 | buffer_free(&m); |
| 643 | |
| 644 | /* closed dup'ed master */ |
| 645 | if (close(s->ptymaster) < 0) |
| 646 | error("close(s->ptymaster): %s", strerror(errno)); |
| 647 | |
| 648 | /* unlink pty from session */ |
| 649 | s->ttyfd = -1; |
| 650 | } |
| 651 | |
Damien Miller | 7941855 | 2002-04-23 20:28:48 +1000 | [diff] [blame] | 652 | #ifdef USE_PAM |
| 653 | void |
| 654 | mm_start_pam(char *user) |
| 655 | { |
| 656 | Buffer m; |
| 657 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 658 | debug3("%s entering", __func__); |
Damien Miller | 7941855 | 2002-04-23 20:28:48 +1000 | [diff] [blame] | 659 | |
| 660 | buffer_init(&m); |
| 661 | buffer_put_cstring(&m, user); |
| 662 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 663 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m); |
Damien Miller | 7941855 | 2002-04-23 20:28:48 +1000 | [diff] [blame] | 664 | |
| 665 | buffer_free(&m); |
| 666 | } |
| 667 | #endif /* USE_PAM */ |
| 668 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 669 | /* Request process termination */ |
| 670 | |
| 671 | void |
| 672 | mm_terminate(void) |
| 673 | { |
| 674 | Buffer m; |
| 675 | |
| 676 | buffer_init(&m); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 677 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 678 | buffer_free(&m); |
| 679 | } |
| 680 | |
| 681 | int |
| 682 | mm_ssh1_session_key(BIGNUM *num) |
| 683 | { |
| 684 | int rsafail; |
| 685 | Buffer m; |
| 686 | |
| 687 | buffer_init(&m); |
| 688 | buffer_put_bignum2(&m, num); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 689 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 690 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 691 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 692 | |
| 693 | rsafail = buffer_get_int(&m); |
| 694 | buffer_get_bignum2(&m, num); |
| 695 | |
| 696 | buffer_free(&m); |
| 697 | |
| 698 | return (rsafail); |
| 699 | } |
| 700 | |
| 701 | static void |
| 702 | mm_chall_setup(char **name, char **infotxt, u_int *numprompts, |
| 703 | char ***prompts, u_int **echo_on) |
| 704 | { |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 705 | *name = xstrdup(""); |
| 706 | *infotxt = xstrdup(""); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 707 | *numprompts = 1; |
| 708 | *prompts = xmalloc(*numprompts * sizeof(char*)); |
| 709 | *echo_on = xmalloc(*numprompts * sizeof(u_int)); |
| 710 | (*echo_on)[0] = 0; |
| 711 | } |
| 712 | |
| 713 | int |
| 714 | mm_bsdauth_query(void *ctx, char **name, char **infotxt, |
| 715 | u_int *numprompts, char ***prompts, u_int **echo_on) |
| 716 | { |
| 717 | Buffer m; |
| 718 | int res; |
| 719 | char *challenge; |
| 720 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 721 | debug3("%s: entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 722 | |
| 723 | buffer_init(&m); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 724 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 725 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 726 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 727 | &m); |
| 728 | res = buffer_get_int(&m); |
| 729 | if (res == -1) { |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 730 | debug3("%s: no challenge", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 731 | buffer_free(&m); |
| 732 | return (-1); |
| 733 | } |
| 734 | |
| 735 | /* Get the challenge, and format the response */ |
| 736 | challenge = buffer_get_string(&m, NULL); |
| 737 | buffer_free(&m); |
| 738 | |
| 739 | mm_chall_setup(name, infotxt, numprompts, prompts, echo_on); |
| 740 | (*prompts)[0] = challenge; |
| 741 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 742 | debug3("%s: received challenge: %s", __func__, challenge); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 743 | |
| 744 | return (0); |
| 745 | } |
| 746 | |
| 747 | int |
| 748 | mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses) |
| 749 | { |
| 750 | Buffer m; |
| 751 | int authok; |
| 752 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 753 | debug3("%s: entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 754 | if (numresponses != 1) |
| 755 | return (-1); |
| 756 | |
| 757 | buffer_init(&m); |
| 758 | buffer_put_cstring(&m, responses[0]); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 759 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 760 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 761 | mm_request_receive_expect(pmonitor->m_recvfd, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 762 | MONITOR_ANS_BSDAUTHRESPOND, &m); |
| 763 | |
| 764 | authok = buffer_get_int(&m); |
| 765 | buffer_free(&m); |
| 766 | |
| 767 | return ((authok == 0) ? -1 : 0); |
| 768 | } |
| 769 | |
| 770 | int |
| 771 | mm_skey_query(void *ctx, char **name, char **infotxt, |
| 772 | u_int *numprompts, char ***prompts, u_int **echo_on) |
| 773 | { |
| 774 | Buffer m; |
| 775 | int len, res; |
| 776 | char *p, *challenge; |
| 777 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 778 | debug3("%s: entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 779 | |
| 780 | buffer_init(&m); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 781 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 782 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 783 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 784 | &m); |
| 785 | res = buffer_get_int(&m); |
| 786 | if (res == -1) { |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 787 | debug3("%s: no challenge", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 788 | buffer_free(&m); |
| 789 | return (-1); |
| 790 | } |
| 791 | |
| 792 | /* Get the challenge, and format the response */ |
| 793 | challenge = buffer_get_string(&m, NULL); |
| 794 | buffer_free(&m); |
| 795 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 796 | debug3("%s: received challenge: %s", __func__, challenge); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 797 | |
| 798 | mm_chall_setup(name, infotxt, numprompts, prompts, echo_on); |
| 799 | |
| 800 | len = strlen(challenge) + strlen(SKEY_PROMPT) + 1; |
| 801 | p = xmalloc(len); |
| 802 | strlcpy(p, challenge, len); |
| 803 | strlcat(p, SKEY_PROMPT, len); |
| 804 | (*prompts)[0] = p; |
| 805 | xfree(challenge); |
| 806 | |
| 807 | return (0); |
| 808 | } |
| 809 | |
| 810 | int |
| 811 | mm_skey_respond(void *ctx, u_int numresponses, char **responses) |
| 812 | { |
| 813 | Buffer m; |
| 814 | int authok; |
| 815 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 816 | debug3("%s: entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 817 | if (numresponses != 1) |
| 818 | return (-1); |
| 819 | |
| 820 | buffer_init(&m); |
| 821 | buffer_put_cstring(&m, responses[0]); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 822 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 823 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 824 | mm_request_receive_expect(pmonitor->m_recvfd, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 825 | MONITOR_ANS_SKEYRESPOND, &m); |
| 826 | |
| 827 | authok = buffer_get_int(&m); |
| 828 | buffer_free(&m); |
| 829 | |
| 830 | return ((authok == 0) ? -1 : 0); |
| 831 | } |
| 832 | |
| 833 | void |
| 834 | mm_ssh1_session_id(u_char session_id[16]) |
| 835 | { |
| 836 | Buffer m; |
| 837 | int i; |
| 838 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 839 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 840 | |
| 841 | buffer_init(&m); |
| 842 | for (i = 0; i < 16; i++) |
| 843 | buffer_put_char(&m, session_id[i]); |
| 844 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 845 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 846 | buffer_free(&m); |
| 847 | } |
| 848 | |
| 849 | int |
| 850 | mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey) |
| 851 | { |
| 852 | Buffer m; |
| 853 | Key *key; |
| 854 | u_char *blob; |
| 855 | u_int blen; |
| 856 | int allowed = 0; |
| 857 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 858 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 859 | |
| 860 | buffer_init(&m); |
| 861 | buffer_put_bignum2(&m, client_n); |
| 862 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 863 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m); |
| 864 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 865 | |
| 866 | allowed = buffer_get_int(&m); |
| 867 | |
| 868 | if (allowed && rkey != NULL) { |
| 869 | blob = buffer_get_string(&m, &blen); |
| 870 | if ((key = key_from_blob(blob, blen)) == NULL) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 871 | fatal("%s: key_from_blob failed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 872 | *rkey = key; |
| 873 | xfree(blob); |
| 874 | } |
| 875 | mm_send_debug(&m); |
| 876 | buffer_free(&m); |
| 877 | |
| 878 | return (allowed); |
| 879 | } |
| 880 | |
| 881 | BIGNUM * |
| 882 | mm_auth_rsa_generate_challenge(Key *key) |
| 883 | { |
| 884 | Buffer m; |
| 885 | BIGNUM *challenge; |
| 886 | u_char *blob; |
| 887 | u_int blen; |
| 888 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 889 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 890 | |
| 891 | if ((challenge = BN_new()) == NULL) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 892 | fatal("%s: BN_new failed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 893 | |
| 894 | key->type = KEY_RSA; /* XXX cheat for key_to_blob */ |
| 895 | if (key_to_blob(key, &blob, &blen) == 0) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 896 | fatal("%s: key_to_blob failed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 897 | key->type = KEY_RSA1; |
| 898 | |
| 899 | buffer_init(&m); |
| 900 | buffer_put_string(&m, blob, blen); |
| 901 | xfree(blob); |
| 902 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 903 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m); |
| 904 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 905 | |
| 906 | buffer_get_bignum2(&m, challenge); |
| 907 | buffer_free(&m); |
| 908 | |
| 909 | return (challenge); |
| 910 | } |
| 911 | |
| 912 | int |
| 913 | mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16]) |
| 914 | { |
| 915 | Buffer m; |
| 916 | u_char *blob; |
| 917 | u_int blen; |
| 918 | int success = 0; |
| 919 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 920 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 921 | |
| 922 | key->type = KEY_RSA; /* XXX cheat for key_to_blob */ |
| 923 | if (key_to_blob(key, &blob, &blen) == 0) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 924 | fatal("%s: key_to_blob failed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 925 | key->type = KEY_RSA1; |
| 926 | |
| 927 | buffer_init(&m); |
| 928 | buffer_put_string(&m, blob, blen); |
| 929 | buffer_put_string(&m, response, 16); |
| 930 | xfree(blob); |
| 931 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 932 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m); |
| 933 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 934 | |
| 935 | success = buffer_get_int(&m); |
| 936 | buffer_free(&m); |
| 937 | |
| 938 | return (success); |
| 939 | } |