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