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