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