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