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