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