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