Darren Tucker | 3a7c041 | 2012-10-05 10:51:59 +1000 | [diff] [blame] | 1 | /* $OpenBSD: monitor_wrap.c,v 1.74 2012/10/01 13:59:51 naddy 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 | |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 30 | #include <sys/types.h> |
Darren Tucker | 1a3d6e7 | 2006-08-05 18:46:47 +1000 | [diff] [blame] | 31 | #include <sys/uio.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 32 | |
Darren Tucker | 3997249 | 2006-07-12 22:22:46 +1000 | [diff] [blame] | 33 | #include <errno.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 34 | #include <pwd.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 35 | #include <signal.h> |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 36 | #include <stdarg.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 37 | #include <stdio.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 38 | #include <string.h> |
Darren Tucker | 1a3d6e7 | 2006-08-05 18:46:47 +1000 | [diff] [blame] | 39 | #include <unistd.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 40 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 41 | #include <openssl/bn.h> |
| 42 | #include <openssl/dh.h> |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 43 | #include <openssl/evp.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 44 | |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 45 | #include "openbsd-compat/sys-queue.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 46 | #include "xmalloc.h" |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 47 | #include "ssh.h" |
| 48 | #include "dh.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 49 | #include "buffer.h" |
| 50 | #include "key.h" |
| 51 | #include "cipher.h" |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 52 | #include "kex.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 53 | #include "hostfile.h" |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 54 | #include "auth.h" |
Damien Miller | 06ebedf | 2003-02-24 12:03:38 +1100 | [diff] [blame] | 55 | #include "auth-options.h" |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 56 | #include "packet.h" |
| 57 | #include "mac.h" |
| 58 | #include "log.h" |
Ben Lindstrom | 036768e | 2004-04-08 16:12:30 +0000 | [diff] [blame] | 59 | #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */ |
| 60 | #undef TARGET_OS_MAC |
Ben Lindstrom | 1b9f2a6 | 2004-04-08 05:11:03 +0000 | [diff] [blame] | 61 | #include "zlib.h" |
Ben Lindstrom | 036768e | 2004-04-08 16:12:30 +0000 | [diff] [blame] | 62 | #define TARGET_OS_MAC 1 |
| 63 | #else |
| 64 | #include "zlib.h" |
| 65 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 66 | #include "monitor.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 67 | #ifdef GSSAPI |
| 68 | #include "ssh-gss.h" |
| 69 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 70 | #include "monitor_wrap.h" |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 71 | #include "atomicio.h" |
| 72 | #include "monitor_fdpass.h" |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 73 | #include "misc.h" |
Damien Miller | cee8523 | 2009-03-06 00:58:22 +1100 | [diff] [blame] | 74 | #include "schnorr.h" |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 75 | #include "jpake.h" |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 76 | #include "uuencode.h" |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 77 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 78 | #include "channels.h" |
| 79 | #include "session.h" |
Darren Tucker | 1629c07 | 2007-02-19 22:25:37 +1100 | [diff] [blame] | 80 | #include "servconf.h" |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 81 | #include "roaming.h" |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 82 | |
| 83 | /* Imports */ |
| 84 | extern int compat20; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 85 | extern z_stream incoming_stream; |
| 86 | extern z_stream outgoing_stream; |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 87 | extern struct monitor *pmonitor; |
Darren Tucker | 0999174 | 2004-07-17 17:05:14 +1000 | [diff] [blame] | 88 | extern Buffer loginmsg; |
Damien Miller | 4e448a3 | 2003-05-14 15:11:48 +1000 | [diff] [blame] | 89 | extern ServerOptions options; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 90 | |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 91 | void |
| 92 | mm_log_handler(LogLevel level, const char *msg, void *ctx) |
| 93 | { |
| 94 | Buffer log_msg; |
| 95 | struct monitor *mon = (struct monitor *)ctx; |
| 96 | |
| 97 | if (mon->m_log_sendfd == -1) |
| 98 | fatal("%s: no log channel", __func__); |
| 99 | |
| 100 | buffer_init(&log_msg); |
| 101 | /* |
| 102 | * Placeholder for packet length. Will be filled in with the actual |
| 103 | * packet length once the packet has been constucted. This saves |
| 104 | * fragile math. |
| 105 | */ |
| 106 | buffer_put_int(&log_msg, 0); |
| 107 | |
| 108 | buffer_put_int(&log_msg, level); |
| 109 | buffer_put_cstring(&log_msg, msg); |
| 110 | put_u32(buffer_ptr(&log_msg), buffer_len(&log_msg) - 4); |
| 111 | if (atomicio(vwrite, mon->m_log_sendfd, buffer_ptr(&log_msg), |
| 112 | buffer_len(&log_msg)) != buffer_len(&log_msg)) |
| 113 | fatal("%s: write: %s", __func__, strerror(errno)); |
| 114 | buffer_free(&log_msg); |
| 115 | } |
| 116 | |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 117 | int |
| 118 | mm_is_monitor(void) |
| 119 | { |
| 120 | /* |
| 121 | * m_pid is only set in the privileged part, and |
| 122 | * points to the unprivileged child. |
| 123 | */ |
Darren Tucker | a47c9bc | 2003-11-03 20:03:25 +1100 | [diff] [blame] | 124 | return (pmonitor && pmonitor->m_pid > 0); |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 125 | } |
| 126 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 127 | void |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 128 | mm_request_send(int sock, enum monitor_reqtype type, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 129 | { |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 130 | u_int mlen = buffer_len(m); |
Ben Lindstrom | b1bdc5a | 2002-07-04 00:09:26 +0000 | [diff] [blame] | 131 | u_char buf[5]; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 132 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 133 | debug3("%s entering: type %d", __func__, type); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 134 | |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 135 | put_u32(buf, mlen + 1); |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 136 | buf[4] = (u_char) type; /* 1st byte of payload is mesg-type */ |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 137 | if (atomicio(vwrite, sock, buf, sizeof(buf)) != sizeof(buf)) |
Damien Miller | b253cc4 | 2005-05-26 12:23:44 +1000 | [diff] [blame] | 138 | fatal("%s: write: %s", __func__, strerror(errno)); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 139 | if (atomicio(vwrite, sock, buffer_ptr(m), mlen) != mlen) |
Damien Miller | b253cc4 | 2005-05-26 12:23:44 +1000 | [diff] [blame] | 140 | fatal("%s: write: %s", __func__, strerror(errno)); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 144 | mm_request_receive(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 145 | { |
| 146 | u_char buf[4]; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 147 | u_int msg_len; |
| 148 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 149 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 150 | |
Damien Miller | b253cc4 | 2005-05-26 12:23:44 +1000 | [diff] [blame] | 151 | if (atomicio(read, sock, buf, sizeof(buf)) != sizeof(buf)) { |
| 152 | if (errno == EPIPE) |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 153 | cleanup_exit(255); |
Damien Miller | b253cc4 | 2005-05-26 12:23:44 +1000 | [diff] [blame] | 154 | fatal("%s: read: %s", __func__, strerror(errno)); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 155 | } |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 156 | msg_len = get_u32(buf); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 157 | if (msg_len > 256 * 1024) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 158 | fatal("%s: read: bad msg_len %d", __func__, msg_len); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 159 | buffer_clear(m); |
| 160 | buffer_append_space(m, msg_len); |
Damien Miller | b253cc4 | 2005-05-26 12:23:44 +1000 | [diff] [blame] | 161 | if (atomicio(read, sock, buffer_ptr(m), msg_len) != msg_len) |
| 162 | fatal("%s: read: %s", __func__, strerror(errno)); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | void |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 166 | mm_request_receive_expect(int sock, enum monitor_reqtype type, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 167 | { |
| 168 | u_char rtype; |
| 169 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 170 | debug3("%s entering: type %d", __func__, type); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 171 | |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 172 | mm_request_receive(sock, m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 173 | rtype = buffer_get_char(m); |
| 174 | if (rtype != type) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 175 | fatal("%s: read: rtype %d != type %d", __func__, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 176 | rtype, type); |
| 177 | } |
| 178 | |
| 179 | DH * |
| 180 | mm_choose_dh(int min, int nbits, int max) |
| 181 | { |
| 182 | BIGNUM *p, *g; |
| 183 | int success = 0; |
| 184 | Buffer m; |
| 185 | |
| 186 | buffer_init(&m); |
| 187 | buffer_put_int(&m, min); |
| 188 | buffer_put_int(&m, nbits); |
| 189 | buffer_put_int(&m, max); |
| 190 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 191 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_MODULI, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 192 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 193 | debug3("%s: waiting for MONITOR_ANS_MODULI", __func__); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 194 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_MODULI, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 195 | |
| 196 | success = buffer_get_char(&m); |
| 197 | if (success == 0) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 198 | fatal("%s: MONITOR_ANS_MODULI failed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 199 | |
| 200 | if ((p = BN_new()) == NULL) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 201 | fatal("%s: BN_new failed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 202 | if ((g = BN_new()) == NULL) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 203 | fatal("%s: BN_new failed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 204 | buffer_get_bignum2(&m, p); |
| 205 | buffer_get_bignum2(&m, g); |
| 206 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 207 | debug3("%s: remaining %d", __func__, buffer_len(&m)); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 208 | buffer_free(&m); |
| 209 | |
| 210 | return (dh_new_group(g, p)); |
| 211 | } |
| 212 | |
| 213 | int |
| 214 | mm_key_sign(Key *key, u_char **sigp, u_int *lenp, u_char *data, u_int datalen) |
| 215 | { |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 216 | Kex *kex = *pmonitor->m_pkex; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 217 | Buffer m; |
| 218 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 219 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 220 | |
| 221 | buffer_init(&m); |
| 222 | buffer_put_int(&m, kex->host_key_index(key)); |
| 223 | buffer_put_string(&m, data, datalen); |
| 224 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 225 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SIGN, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 226 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 227 | debug3("%s: waiting for MONITOR_ANS_SIGN", __func__); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 228 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SIGN, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 229 | *sigp = buffer_get_string(&m, lenp); |
| 230 | buffer_free(&m); |
| 231 | |
| 232 | return (0); |
| 233 | } |
| 234 | |
| 235 | struct passwd * |
Darren Tucker | b09b677 | 2004-06-22 15:06:46 +1000 | [diff] [blame] | 236 | mm_getpwnamallow(const char *username) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 237 | { |
| 238 | Buffer m; |
| 239 | struct passwd *pw; |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 240 | u_int len, i; |
Darren Tucker | 1629c07 | 2007-02-19 22:25:37 +1100 | [diff] [blame] | 241 | ServerOptions *newopts; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 242 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 243 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 244 | |
| 245 | buffer_init(&m); |
Darren Tucker | b09b677 | 2004-06-22 15:06:46 +1000 | [diff] [blame] | 246 | buffer_put_cstring(&m, username); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 247 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 248 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PWNAM, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 249 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 250 | debug3("%s: waiting for MONITOR_ANS_PWNAM", __func__); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 251 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PWNAM, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 252 | |
| 253 | if (buffer_get_char(&m) == 0) { |
Darren Tucker | 2f8b3d9 | 2007-12-02 23:02:15 +1100 | [diff] [blame] | 254 | pw = NULL; |
| 255 | goto out; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 256 | } |
Darren Tucker | 1629c07 | 2007-02-19 22:25:37 +1100 | [diff] [blame] | 257 | pw = buffer_get_string(&m, &len); |
| 258 | if (len != sizeof(struct passwd)) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 259 | fatal("%s: struct passwd size mismatch", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 260 | pw->pw_name = buffer_get_string(&m, NULL); |
| 261 | pw->pw_passwd = buffer_get_string(&m, NULL); |
| 262 | pw->pw_gecos = buffer_get_string(&m, NULL); |
Kevin Steves | 7e14760 | 2002-03-22 18:07:17 +0000 | [diff] [blame] | 263 | #ifdef HAVE_PW_CLASS_IN_PASSWD |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 264 | pw->pw_class = buffer_get_string(&m, NULL); |
Kevin Steves | 7e14760 | 2002-03-22 18:07:17 +0000 | [diff] [blame] | 265 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 266 | pw->pw_dir = buffer_get_string(&m, NULL); |
| 267 | pw->pw_shell = buffer_get_string(&m, NULL); |
Darren Tucker | 1629c07 | 2007-02-19 22:25:37 +1100 | [diff] [blame] | 268 | |
Darren Tucker | 2f8b3d9 | 2007-12-02 23:02:15 +1100 | [diff] [blame] | 269 | out: |
Darren Tucker | 1629c07 | 2007-02-19 22:25:37 +1100 | [diff] [blame] | 270 | /* copy options block as a Match directive may have changed some */ |
| 271 | newopts = buffer_get_string(&m, &len); |
| 272 | if (len != sizeof(*newopts)) |
| 273 | fatal("%s: option block size mismatch", __func__); |
Damien Miller | f2e407e | 2011-05-20 19:04:14 +1000 | [diff] [blame] | 274 | |
| 275 | #define M_CP_STROPT(x) do { \ |
| 276 | if (newopts->x != NULL) \ |
| 277 | newopts->x = buffer_get_string(&m, NULL); \ |
| 278 | } while (0) |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 279 | #define M_CP_STRARRAYOPT(x, nx) do { \ |
| 280 | for (i = 0; i < newopts->nx; i++) \ |
| 281 | newopts->x[i] = buffer_get_string(&m, NULL); \ |
| 282 | } while (0) |
Damien Miller | f2e407e | 2011-05-20 19:04:14 +1000 | [diff] [blame] | 283 | /* See comment in servconf.h */ |
| 284 | COPY_MATCH_STRING_OPTS(); |
| 285 | #undef M_CP_STROPT |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 286 | #undef M_CP_STRARRAYOPT |
Damien Miller | f2e407e | 2011-05-20 19:04:14 +1000 | [diff] [blame] | 287 | |
Darren Tucker | 1629c07 | 2007-02-19 22:25:37 +1100 | [diff] [blame] | 288 | copy_set_server_options(&options, newopts, 1); |
| 289 | xfree(newopts); |
| 290 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 291 | buffer_free(&m); |
| 292 | |
| 293 | return (pw); |
| 294 | } |
| 295 | |
Darren Tucker | 7eb3de0 | 2003-10-15 15:56:58 +1000 | [diff] [blame] | 296 | char * |
| 297 | mm_auth2_read_banner(void) |
Damien Miller | 5ad9fd9 | 2002-05-13 11:07:41 +1000 | [diff] [blame] | 298 | { |
| 299 | Buffer m; |
| 300 | char *banner; |
| 301 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 302 | debug3("%s entering", __func__); |
Damien Miller | 5ad9fd9 | 2002-05-13 11:07:41 +1000 | [diff] [blame] | 303 | |
| 304 | buffer_init(&m); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 305 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTH2_READ_BANNER, &m); |
Damien Miller | 5ad9fd9 | 2002-05-13 11:07:41 +1000 | [diff] [blame] | 306 | buffer_clear(&m); |
| 307 | |
Darren Tucker | 7eb3de0 | 2003-10-15 15:56:58 +1000 | [diff] [blame] | 308 | mm_request_receive_expect(pmonitor->m_recvfd, |
| 309 | MONITOR_ANS_AUTH2_READ_BANNER, &m); |
Damien Miller | 5ad9fd9 | 2002-05-13 11:07:41 +1000 | [diff] [blame] | 310 | banner = buffer_get_string(&m, NULL); |
| 311 | buffer_free(&m); |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 312 | |
Darren Tucker | 7eb3de0 | 2003-10-15 15:56:58 +1000 | [diff] [blame] | 313 | /* treat empty banner as missing banner */ |
| 314 | if (strlen(banner) == 0) { |
| 315 | xfree(banner); |
| 316 | banner = NULL; |
| 317 | } |
Damien Miller | 5ad9fd9 | 2002-05-13 11:07:41 +1000 | [diff] [blame] | 318 | return (banner); |
| 319 | } |
| 320 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 321 | /* Inform the privileged process about service and style */ |
| 322 | |
| 323 | void |
| 324 | mm_inform_authserv(char *service, char *style) |
| 325 | { |
| 326 | Buffer m; |
| 327 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 328 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 329 | |
| 330 | buffer_init(&m); |
| 331 | buffer_put_cstring(&m, service); |
| 332 | buffer_put_cstring(&m, style ? style : ""); |
| 333 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 334 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHSERV, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 335 | |
| 336 | buffer_free(&m); |
| 337 | } |
| 338 | |
| 339 | /* Do the password authentication */ |
| 340 | int |
| 341 | mm_auth_password(Authctxt *authctxt, char *password) |
| 342 | { |
| 343 | Buffer m; |
| 344 | int authenticated = 0; |
| 345 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 346 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 347 | |
| 348 | buffer_init(&m); |
| 349 | buffer_put_cstring(&m, password); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 350 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUTHPASSWORD, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 351 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 352 | debug3("%s: waiting for MONITOR_ANS_AUTHPASSWORD", __func__); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 353 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_AUTHPASSWORD, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 354 | |
| 355 | authenticated = buffer_get_int(&m); |
| 356 | |
| 357 | buffer_free(&m); |
| 358 | |
| 359 | debug3("%s: user %sauthenticated", |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 360 | __func__, authenticated ? "" : "not "); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 361 | return (authenticated); |
| 362 | } |
| 363 | |
| 364 | int |
| 365 | mm_user_key_allowed(struct passwd *pw, Key *key) |
| 366 | { |
| 367 | return (mm_key_allowed(MM_USERKEY, NULL, NULL, key)); |
| 368 | } |
| 369 | |
| 370 | int |
| 371 | mm_hostbased_key_allowed(struct passwd *pw, char *user, char *host, |
| 372 | Key *key) |
| 373 | { |
| 374 | return (mm_key_allowed(MM_HOSTKEY, user, host, key)); |
| 375 | } |
| 376 | |
| 377 | int |
| 378 | mm_auth_rhosts_rsa_key_allowed(struct passwd *pw, char *user, |
| 379 | char *host, Key *key) |
| 380 | { |
| 381 | int ret; |
| 382 | |
| 383 | key->type = KEY_RSA; /* XXX hack for key_to_blob */ |
| 384 | ret = mm_key_allowed(MM_RSAHOSTKEY, user, host, key); |
| 385 | key->type = KEY_RSA1; |
| 386 | return (ret); |
| 387 | } |
| 388 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 389 | int |
| 390 | mm_key_allowed(enum mm_keytype type, char *user, char *host, Key *key) |
| 391 | { |
| 392 | Buffer m; |
| 393 | u_char *blob; |
| 394 | u_int len; |
Damien Miller | 06ebedf | 2003-02-24 12:03:38 +1100 | [diff] [blame] | 395 | int allowed = 0, have_forced = 0; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 396 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 397 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 398 | |
| 399 | /* Convert the key to a blob and the pass it over */ |
| 400 | if (!key_to_blob(key, &blob, &len)) |
| 401 | return (0); |
| 402 | |
| 403 | buffer_init(&m); |
| 404 | buffer_put_int(&m, type); |
| 405 | buffer_put_cstring(&m, user ? user : ""); |
| 406 | buffer_put_cstring(&m, host ? host : ""); |
| 407 | buffer_put_string(&m, blob, len); |
| 408 | xfree(blob); |
| 409 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 410 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYALLOWED, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 411 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 412 | debug3("%s: waiting for MONITOR_ANS_KEYALLOWED", __func__); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 413 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYALLOWED, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 414 | |
| 415 | allowed = buffer_get_int(&m); |
| 416 | |
Damien Miller | 06ebedf | 2003-02-24 12:03:38 +1100 | [diff] [blame] | 417 | /* fake forced command */ |
| 418 | auth_clear_options(); |
| 419 | have_forced = buffer_get_int(&m); |
| 420 | forced_command = have_forced ? xstrdup("true") : NULL; |
| 421 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 422 | buffer_free(&m); |
| 423 | |
| 424 | return (allowed); |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | * This key verify needs to send the key type along, because the |
| 429 | * privileged parent makes the decision if the key is allowed |
| 430 | * for authentication. |
| 431 | */ |
| 432 | |
| 433 | int |
| 434 | mm_key_verify(Key *key, u_char *sig, u_int siglen, u_char *data, u_int datalen) |
| 435 | { |
| 436 | Buffer m; |
| 437 | u_char *blob; |
| 438 | u_int len; |
| 439 | int verified = 0; |
| 440 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 441 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 442 | |
| 443 | /* Convert the key to a blob and the pass it over */ |
| 444 | if (!key_to_blob(key, &blob, &len)) |
| 445 | return (0); |
| 446 | |
| 447 | buffer_init(&m); |
| 448 | buffer_put_string(&m, blob, len); |
| 449 | buffer_put_string(&m, sig, siglen); |
| 450 | buffer_put_string(&m, data, datalen); |
| 451 | xfree(blob); |
| 452 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 453 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_KEYVERIFY, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 454 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 455 | debug3("%s: waiting for MONITOR_ANS_KEYVERIFY", __func__); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 456 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_KEYVERIFY, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 457 | |
| 458 | verified = buffer_get_int(&m); |
| 459 | |
| 460 | buffer_free(&m); |
| 461 | |
| 462 | return (verified); |
| 463 | } |
| 464 | |
| 465 | /* Export key state after authentication */ |
| 466 | Newkeys * |
| 467 | mm_newkeys_from_blob(u_char *blob, int blen) |
| 468 | { |
| 469 | Buffer b; |
| 470 | u_int len; |
| 471 | Newkeys *newkey = NULL; |
| 472 | Enc *enc; |
| 473 | Mac *mac; |
| 474 | Comp *comp; |
| 475 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 476 | debug3("%s: %p(%d)", __func__, blob, blen); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 477 | #ifdef DEBUG_PK |
| 478 | dump_base64(stderr, blob, blen); |
| 479 | #endif |
| 480 | buffer_init(&b); |
| 481 | buffer_append(&b, blob, blen); |
| 482 | |
| 483 | newkey = xmalloc(sizeof(*newkey)); |
| 484 | enc = &newkey->enc; |
| 485 | mac = &newkey->mac; |
| 486 | comp = &newkey->comp; |
| 487 | |
| 488 | /* Enc structure */ |
| 489 | enc->name = buffer_get_string(&b, NULL); |
| 490 | buffer_get(&b, &enc->cipher, sizeof(enc->cipher)); |
| 491 | enc->enabled = buffer_get_int(&b); |
| 492 | enc->block_size = buffer_get_int(&b); |
| 493 | enc->key = buffer_get_string(&b, &enc->key_len); |
| 494 | enc->iv = buffer_get_string(&b, &len); |
| 495 | if (len != enc->block_size) |
Ben Lindstrom | 0851249 | 2002-06-27 00:23:02 +0000 | [diff] [blame] | 496 | fatal("%s: bad ivlen: expected %u != %u", __func__, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 497 | enc->block_size, len); |
| 498 | |
| 499 | if (enc->name == NULL || cipher_by_name(enc->name) != enc->cipher) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 500 | fatal("%s: bad cipher name %s or pointer %p", __func__, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 501 | enc->name, enc->cipher); |
| 502 | |
| 503 | /* Mac structure */ |
| 504 | mac->name = buffer_get_string(&b, NULL); |
Darren Tucker | 5f3d5be | 2007-06-05 18:30:18 +1000 | [diff] [blame] | 505 | if (mac->name == NULL || mac_setup(mac, mac->name) == -1) |
Damien Miller | e45796f | 2007-06-11 14:01:42 +1000 | [diff] [blame] | 506 | fatal("%s: can not setup mac %s", __func__, mac->name); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 507 | mac->enabled = buffer_get_int(&b); |
| 508 | mac->key = buffer_get_string(&b, &len); |
| 509 | if (len > mac->key_len) |
Ben Lindstrom | 0851249 | 2002-06-27 00:23:02 +0000 | [diff] [blame] | 510 | fatal("%s: bad mac key length: %u > %d", __func__, len, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 511 | mac->key_len); |
| 512 | mac->key_len = len; |
| 513 | |
| 514 | /* Comp structure */ |
| 515 | comp->type = buffer_get_int(&b); |
| 516 | comp->enabled = buffer_get_int(&b); |
| 517 | comp->name = buffer_get_string(&b, NULL); |
| 518 | |
| 519 | len = buffer_len(&b); |
| 520 | if (len != 0) |
Ben Lindstrom | 0851249 | 2002-06-27 00:23:02 +0000 | [diff] [blame] | 521 | error("newkeys_from_blob: remaining bytes in blob %u", len); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 522 | buffer_free(&b); |
| 523 | return (newkey); |
| 524 | } |
| 525 | |
| 526 | int |
| 527 | mm_newkeys_to_blob(int mode, u_char **blobp, u_int *lenp) |
| 528 | { |
| 529 | Buffer b; |
| 530 | int len; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 531 | Enc *enc; |
| 532 | Mac *mac; |
| 533 | Comp *comp; |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 534 | Newkeys *newkey = (Newkeys *)packet_get_newkeys(mode); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 535 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 536 | debug3("%s: converting %p", __func__, newkey); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 537 | |
| 538 | if (newkey == NULL) { |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 539 | error("%s: newkey == NULL", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 540 | return 0; |
| 541 | } |
| 542 | enc = &newkey->enc; |
| 543 | mac = &newkey->mac; |
| 544 | comp = &newkey->comp; |
| 545 | |
| 546 | buffer_init(&b); |
| 547 | /* Enc structure */ |
| 548 | buffer_put_cstring(&b, enc->name); |
| 549 | /* The cipher struct is constant and shared, you export pointer */ |
| 550 | buffer_append(&b, &enc->cipher, sizeof(enc->cipher)); |
| 551 | buffer_put_int(&b, enc->enabled); |
| 552 | buffer_put_int(&b, enc->block_size); |
| 553 | buffer_put_string(&b, enc->key, enc->key_len); |
| 554 | packet_get_keyiv(mode, enc->iv, enc->block_size); |
| 555 | buffer_put_string(&b, enc->iv, enc->block_size); |
| 556 | |
| 557 | /* Mac structure */ |
| 558 | buffer_put_cstring(&b, mac->name); |
| 559 | buffer_put_int(&b, mac->enabled); |
| 560 | buffer_put_string(&b, mac->key, mac->key_len); |
| 561 | |
| 562 | /* Comp structure */ |
| 563 | buffer_put_int(&b, comp->type); |
| 564 | buffer_put_int(&b, comp->enabled); |
| 565 | buffer_put_cstring(&b, comp->name); |
| 566 | |
| 567 | len = buffer_len(&b); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 568 | if (lenp != NULL) |
| 569 | *lenp = len; |
Ben Lindstrom | 2bf759c | 2002-07-07 22:13:31 +0000 | [diff] [blame] | 570 | if (blobp != NULL) { |
| 571 | *blobp = xmalloc(len); |
| 572 | memcpy(*blobp, buffer_ptr(&b), len); |
| 573 | } |
| 574 | memset(buffer_ptr(&b), 0, len); |
| 575 | buffer_free(&b); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 576 | return len; |
| 577 | } |
| 578 | |
| 579 | static void |
| 580 | mm_send_kex(Buffer *m, Kex *kex) |
| 581 | { |
| 582 | buffer_put_string(m, kex->session_id, kex->session_id_len); |
| 583 | buffer_put_int(m, kex->we_need); |
| 584 | buffer_put_int(m, kex->hostkey_type); |
| 585 | buffer_put_int(m, kex->kex_type); |
| 586 | buffer_put_string(m, buffer_ptr(&kex->my), buffer_len(&kex->my)); |
| 587 | buffer_put_string(m, buffer_ptr(&kex->peer), buffer_len(&kex->peer)); |
| 588 | buffer_put_int(m, kex->flags); |
| 589 | buffer_put_cstring(m, kex->client_version_string); |
| 590 | buffer_put_cstring(m, kex->server_version_string); |
| 591 | } |
| 592 | |
| 593 | void |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 594 | mm_send_keystate(struct monitor *monitor) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 595 | { |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 596 | Buffer m, *input, *output; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 597 | u_char *blob, *p; |
| 598 | u_int bloblen, plen; |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 599 | u_int32_t seqnr, packets; |
Damien Miller | b61f3fc | 2008-07-11 17:36:48 +1000 | [diff] [blame] | 600 | u_int64_t blocks, bytes; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 601 | |
| 602 | buffer_init(&m); |
| 603 | |
| 604 | if (!compat20) { |
| 605 | u_char iv[24]; |
Ben Lindstrom | 402c6cc | 2002-06-21 00:43:42 +0000 | [diff] [blame] | 606 | u_char *key; |
| 607 | u_int ivlen, keylen; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 608 | |
| 609 | buffer_put_int(&m, packet_get_protocol_flags()); |
| 610 | |
| 611 | buffer_put_int(&m, packet_get_ssh1_cipher()); |
| 612 | |
Ben Lindstrom | 402c6cc | 2002-06-21 00:43:42 +0000 | [diff] [blame] | 613 | debug3("%s: Sending ssh1 KEY+IV", __func__); |
| 614 | keylen = packet_get_encryption_key(NULL); |
| 615 | key = xmalloc(keylen+1); /* add 1 if keylen == 0 */ |
| 616 | keylen = packet_get_encryption_key(key); |
| 617 | buffer_put_string(&m, key, keylen); |
| 618 | memset(key, 0, keylen); |
| 619 | xfree(key); |
| 620 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 621 | ivlen = packet_get_keyiv_len(MODE_OUT); |
| 622 | packet_get_keyiv(MODE_OUT, iv, ivlen); |
| 623 | buffer_put_string(&m, iv, ivlen); |
Darren Tucker | 3a7c041 | 2012-10-05 10:51:59 +1000 | [diff] [blame] | 624 | ivlen = packet_get_keyiv_len(MODE_IN); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 625 | packet_get_keyiv(MODE_IN, iv, ivlen); |
| 626 | buffer_put_string(&m, iv, ivlen); |
| 627 | goto skip; |
| 628 | } else { |
| 629 | /* Kex for rekeying */ |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 630 | mm_send_kex(&m, *monitor->m_pkex); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | debug3("%s: Sending new keys: %p %p", |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 634 | __func__, packet_get_newkeys(MODE_OUT), |
| 635 | packet_get_newkeys(MODE_IN)); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 636 | |
| 637 | /* Keys from Kex */ |
| 638 | if (!mm_newkeys_to_blob(MODE_OUT, &blob, &bloblen)) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 639 | fatal("%s: conversion of newkeys failed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 640 | |
| 641 | buffer_put_string(&m, blob, bloblen); |
| 642 | xfree(blob); |
| 643 | |
| 644 | if (!mm_newkeys_to_blob(MODE_IN, &blob, &bloblen)) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 645 | fatal("%s: conversion of newkeys failed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 646 | |
| 647 | buffer_put_string(&m, blob, bloblen); |
| 648 | xfree(blob); |
| 649 | |
Damien Miller | b61f3fc | 2008-07-11 17:36:48 +1000 | [diff] [blame] | 650 | packet_get_state(MODE_OUT, &seqnr, &blocks, &packets, &bytes); |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 651 | buffer_put_int(&m, seqnr); |
| 652 | buffer_put_int64(&m, blocks); |
| 653 | buffer_put_int(&m, packets); |
Damien Miller | b61f3fc | 2008-07-11 17:36:48 +1000 | [diff] [blame] | 654 | buffer_put_int64(&m, bytes); |
| 655 | packet_get_state(MODE_IN, &seqnr, &blocks, &packets, &bytes); |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 656 | buffer_put_int(&m, seqnr); |
| 657 | buffer_put_int64(&m, blocks); |
| 658 | buffer_put_int(&m, packets); |
Damien Miller | b61f3fc | 2008-07-11 17:36:48 +1000 | [diff] [blame] | 659 | buffer_put_int64(&m, bytes); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 660 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 661 | debug3("%s: New keys have been sent", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 662 | skip: |
| 663 | /* More key context */ |
| 664 | plen = packet_get_keycontext(MODE_OUT, NULL); |
| 665 | p = xmalloc(plen+1); |
| 666 | packet_get_keycontext(MODE_OUT, p); |
| 667 | buffer_put_string(&m, p, plen); |
| 668 | xfree(p); |
| 669 | |
| 670 | plen = packet_get_keycontext(MODE_IN, NULL); |
| 671 | p = xmalloc(plen+1); |
| 672 | packet_get_keycontext(MODE_IN, p); |
| 673 | buffer_put_string(&m, p, plen); |
| 674 | xfree(p); |
| 675 | |
| 676 | /* Compression state */ |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 677 | debug3("%s: Sending compression state", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 678 | buffer_put_string(&m, &outgoing_stream, sizeof(outgoing_stream)); |
| 679 | buffer_put_string(&m, &incoming_stream, sizeof(incoming_stream)); |
| 680 | |
| 681 | /* Network I/O buffers */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 682 | input = (Buffer *)packet_get_input(); |
| 683 | output = (Buffer *)packet_get_output(); |
| 684 | buffer_put_string(&m, buffer_ptr(input), buffer_len(input)); |
| 685 | buffer_put_string(&m, buffer_ptr(output), buffer_len(output)); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 686 | |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 687 | /* Roaming */ |
| 688 | if (compat20) { |
| 689 | buffer_put_int64(&m, get_sent_bytes()); |
| 690 | buffer_put_int64(&m, get_recv_bytes()); |
| 691 | } |
| 692 | |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 693 | mm_request_send(monitor->m_recvfd, MONITOR_REQ_KEYEXPORT, &m); |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 694 | debug3("%s: Finished sending state", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 695 | |
| 696 | buffer_free(&m); |
| 697 | } |
| 698 | |
| 699 | int |
Damien Miller | 71a7367 | 2006-03-26 14:04:36 +1100 | [diff] [blame] | 700 | mm_pty_allocate(int *ptyfd, int *ttyfd, char *namebuf, size_t namebuflen) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 701 | { |
| 702 | Buffer m; |
Darren Tucker | 0999174 | 2004-07-17 17:05:14 +1000 | [diff] [blame] | 703 | char *p, *msg; |
Damien Miller | 7207f64 | 2008-05-19 15:34:50 +1000 | [diff] [blame] | 704 | int success = 0, tmp1 = -1, tmp2 = -1; |
| 705 | |
| 706 | /* Kludge: ensure there are fds free to receive the pty/tty */ |
| 707 | if ((tmp1 = dup(pmonitor->m_recvfd)) == -1 || |
| 708 | (tmp2 = dup(pmonitor->m_recvfd)) == -1) { |
| 709 | error("%s: cannot allocate fds for pty", __func__); |
| 710 | if (tmp1 > 0) |
| 711 | close(tmp1); |
| 712 | if (tmp2 > 0) |
| 713 | close(tmp2); |
| 714 | return 0; |
| 715 | } |
| 716 | close(tmp1); |
| 717 | close(tmp2); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 718 | |
| 719 | buffer_init(&m); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 720 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTY, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 721 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 722 | debug3("%s: waiting for MONITOR_ANS_PTY", __func__); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 723 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PTY, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 724 | |
| 725 | success = buffer_get_int(&m); |
| 726 | if (success == 0) { |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 727 | debug3("%s: pty alloc failed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 728 | buffer_free(&m); |
| 729 | return (0); |
| 730 | } |
| 731 | p = buffer_get_string(&m, NULL); |
Darren Tucker | 0999174 | 2004-07-17 17:05:14 +1000 | [diff] [blame] | 732 | msg = buffer_get_string(&m, NULL); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 733 | buffer_free(&m); |
| 734 | |
| 735 | strlcpy(namebuf, p, namebuflen); /* Possible truncation */ |
| 736 | xfree(p); |
| 737 | |
Darren Tucker | 0999174 | 2004-07-17 17:05:14 +1000 | [diff] [blame] | 738 | buffer_append(&loginmsg, msg, strlen(msg)); |
| 739 | xfree(msg); |
| 740 | |
Damien Miller | 54fd7cf | 2007-09-17 12:04:08 +1000 | [diff] [blame] | 741 | if ((*ptyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1 || |
| 742 | (*ttyfd = mm_receive_fd(pmonitor->m_recvfd)) == -1) |
| 743 | fatal("%s: receive fds failed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 744 | |
| 745 | /* Success */ |
| 746 | return (1); |
| 747 | } |
| 748 | |
| 749 | void |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 750 | mm_session_pty_cleanup2(Session *s) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 751 | { |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 752 | Buffer m; |
| 753 | |
| 754 | if (s->ttyfd == -1) |
| 755 | return; |
| 756 | buffer_init(&m); |
| 757 | buffer_put_cstring(&m, s->tty); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 758 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PTYCLEANUP, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 759 | buffer_free(&m); |
| 760 | |
| 761 | /* closed dup'ed master */ |
Damien Miller | 7207f64 | 2008-05-19 15:34:50 +1000 | [diff] [blame] | 762 | if (s->ptymaster != -1 && close(s->ptymaster) < 0) |
| 763 | error("close(s->ptymaster/%d): %s", |
| 764 | s->ptymaster, strerror(errno)); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 765 | |
| 766 | /* unlink pty from session */ |
| 767 | s->ttyfd = -1; |
| 768 | } |
| 769 | |
Damien Miller | 7941855 | 2002-04-23 20:28:48 +1000 | [diff] [blame] | 770 | #ifdef USE_PAM |
| 771 | void |
Darren Tucker | dbf7a74 | 2004-03-08 23:04:06 +1100 | [diff] [blame] | 772 | mm_start_pam(Authctxt *authctxt) |
Damien Miller | 7941855 | 2002-04-23 20:28:48 +1000 | [diff] [blame] | 773 | { |
| 774 | Buffer m; |
| 775 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 776 | debug3("%s entering", __func__); |
Damien Miller | 4e448a3 | 2003-05-14 15:11:48 +1000 | [diff] [blame] | 777 | if (!options.use_pam) |
| 778 | fatal("UsePAM=no, but ended up in %s anyway", __func__); |
Damien Miller | 7941855 | 2002-04-23 20:28:48 +1000 | [diff] [blame] | 779 | |
| 780 | buffer_init(&m); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 781 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_START, &m); |
Damien Miller | 7941855 | 2002-04-23 20:28:48 +1000 | [diff] [blame] | 782 | |
| 783 | buffer_free(&m); |
| 784 | } |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 785 | |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 786 | u_int |
| 787 | mm_do_pam_account(void) |
| 788 | { |
| 789 | Buffer m; |
| 790 | u_int ret; |
Darren Tucker | 77fc29e | 2004-09-11 23:07:03 +1000 | [diff] [blame] | 791 | char *msg; |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 792 | |
| 793 | debug3("%s entering", __func__); |
| 794 | if (!options.use_pam) |
| 795 | fatal("UsePAM=no, but ended up in %s anyway", __func__); |
| 796 | |
| 797 | buffer_init(&m); |
| 798 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_ACCOUNT, &m); |
| 799 | |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 800 | mm_request_receive_expect(pmonitor->m_recvfd, |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 801 | MONITOR_ANS_PAM_ACCOUNT, &m); |
| 802 | ret = buffer_get_int(&m); |
Darren Tucker | 77fc29e | 2004-09-11 23:07:03 +1000 | [diff] [blame] | 803 | msg = buffer_get_string(&m, NULL); |
| 804 | buffer_append(&loginmsg, msg, strlen(msg)); |
| 805 | xfree(msg); |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 806 | |
| 807 | buffer_free(&m); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 808 | |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 809 | debug3("%s returning %d", __func__, ret); |
| 810 | |
| 811 | return (ret); |
| 812 | } |
| 813 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 814 | void * |
| 815 | mm_sshpam_init_ctx(Authctxt *authctxt) |
| 816 | { |
| 817 | Buffer m; |
| 818 | int success; |
| 819 | |
| 820 | debug3("%s", __func__); |
| 821 | buffer_init(&m); |
| 822 | buffer_put_cstring(&m, authctxt->user); |
| 823 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_INIT_CTX, &m); |
| 824 | debug3("%s: waiting for MONITOR_ANS_PAM_INIT_CTX", __func__); |
| 825 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_INIT_CTX, &m); |
| 826 | success = buffer_get_int(&m); |
| 827 | if (success == 0) { |
| 828 | debug3("%s: pam_init_ctx failed", __func__); |
| 829 | buffer_free(&m); |
| 830 | return (NULL); |
| 831 | } |
| 832 | buffer_free(&m); |
| 833 | return (authctxt); |
| 834 | } |
| 835 | |
| 836 | int |
| 837 | mm_sshpam_query(void *ctx, char **name, char **info, |
| 838 | u_int *num, char ***prompts, u_int **echo_on) |
| 839 | { |
| 840 | Buffer m; |
Damien Miller | 04b6533 | 2005-07-17 17:53:31 +1000 | [diff] [blame] | 841 | u_int i; |
| 842 | int ret; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 843 | |
| 844 | debug3("%s", __func__); |
| 845 | buffer_init(&m); |
| 846 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_QUERY, &m); |
| 847 | debug3("%s: waiting for MONITOR_ANS_PAM_QUERY", __func__); |
| 848 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_QUERY, &m); |
| 849 | ret = buffer_get_int(&m); |
| 850 | debug3("%s: pam_query returned %d", __func__, ret); |
| 851 | *name = buffer_get_string(&m, NULL); |
| 852 | *info = buffer_get_string(&m, NULL); |
| 853 | *num = buffer_get_int(&m); |
Darren Tucker | d8093e4 | 2006-05-04 16:24:34 +1000 | [diff] [blame] | 854 | if (*num > PAM_MAX_NUM_MSG) |
| 855 | fatal("%s: recieved %u PAM messages, expected <= %u", |
| 856 | __func__, *num, PAM_MAX_NUM_MSG); |
| 857 | *prompts = xcalloc((*num + 1), sizeof(char *)); |
| 858 | *echo_on = xcalloc((*num + 1), sizeof(u_int)); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 859 | for (i = 0; i < *num; ++i) { |
| 860 | (*prompts)[i] = buffer_get_string(&m, NULL); |
| 861 | (*echo_on)[i] = buffer_get_int(&m); |
| 862 | } |
| 863 | buffer_free(&m); |
| 864 | return (ret); |
| 865 | } |
| 866 | |
| 867 | int |
| 868 | mm_sshpam_respond(void *ctx, u_int num, char **resp) |
| 869 | { |
| 870 | Buffer m; |
Damien Miller | 04b6533 | 2005-07-17 17:53:31 +1000 | [diff] [blame] | 871 | u_int i; |
| 872 | int ret; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 873 | |
| 874 | debug3("%s", __func__); |
| 875 | buffer_init(&m); |
| 876 | buffer_put_int(&m, num); |
| 877 | for (i = 0; i < num; ++i) |
| 878 | buffer_put_cstring(&m, resp[i]); |
| 879 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_RESPOND, &m); |
| 880 | debug3("%s: waiting for MONITOR_ANS_PAM_RESPOND", __func__); |
| 881 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_RESPOND, &m); |
| 882 | ret = buffer_get_int(&m); |
| 883 | debug3("%s: pam_respond returned %d", __func__, ret); |
| 884 | buffer_free(&m); |
| 885 | return (ret); |
| 886 | } |
| 887 | |
| 888 | void |
| 889 | mm_sshpam_free_ctx(void *ctxtp) |
| 890 | { |
| 891 | Buffer m; |
| 892 | |
| 893 | debug3("%s", __func__); |
| 894 | buffer_init(&m); |
| 895 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_PAM_FREE_CTX, &m); |
| 896 | debug3("%s: waiting for MONITOR_ANS_PAM_FREE_CTX", __func__); |
| 897 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_PAM_FREE_CTX, &m); |
| 898 | buffer_free(&m); |
| 899 | } |
Damien Miller | 7941855 | 2002-04-23 20:28:48 +1000 | [diff] [blame] | 900 | #endif /* USE_PAM */ |
| 901 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 902 | /* Request process termination */ |
| 903 | |
| 904 | void |
| 905 | mm_terminate(void) |
| 906 | { |
| 907 | Buffer m; |
| 908 | |
| 909 | buffer_init(&m); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 910 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_TERM, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 911 | buffer_free(&m); |
| 912 | } |
| 913 | |
| 914 | int |
| 915 | mm_ssh1_session_key(BIGNUM *num) |
| 916 | { |
| 917 | int rsafail; |
| 918 | Buffer m; |
| 919 | |
| 920 | buffer_init(&m); |
| 921 | buffer_put_bignum2(&m, num); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 922 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSKEY, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 923 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 924 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SESSKEY, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 925 | |
| 926 | rsafail = buffer_get_int(&m); |
| 927 | buffer_get_bignum2(&m, num); |
| 928 | |
| 929 | buffer_free(&m); |
| 930 | |
| 931 | return (rsafail); |
| 932 | } |
| 933 | |
| 934 | static void |
| 935 | mm_chall_setup(char **name, char **infotxt, u_int *numprompts, |
| 936 | char ***prompts, u_int **echo_on) |
| 937 | { |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 938 | *name = xstrdup(""); |
| 939 | *infotxt = xstrdup(""); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 940 | *numprompts = 1; |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 941 | *prompts = xcalloc(*numprompts, sizeof(char *)); |
| 942 | *echo_on = xcalloc(*numprompts, sizeof(u_int)); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 943 | (*echo_on)[0] = 0; |
| 944 | } |
| 945 | |
| 946 | int |
| 947 | mm_bsdauth_query(void *ctx, char **name, char **infotxt, |
| 948 | u_int *numprompts, char ***prompts, u_int **echo_on) |
| 949 | { |
| 950 | Buffer m; |
Damien Miller | b7df3af | 2003-02-24 11:55:46 +1100 | [diff] [blame] | 951 | u_int success; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 952 | char *challenge; |
| 953 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 954 | debug3("%s: entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 955 | |
| 956 | buffer_init(&m); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 957 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHQUERY, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 958 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 959 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_BSDAUTHQUERY, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 960 | &m); |
Damien Miller | b7df3af | 2003-02-24 11:55:46 +1100 | [diff] [blame] | 961 | success = buffer_get_int(&m); |
| 962 | if (success == 0) { |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 963 | debug3("%s: no challenge", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 964 | buffer_free(&m); |
| 965 | return (-1); |
| 966 | } |
| 967 | |
| 968 | /* Get the challenge, and format the response */ |
| 969 | challenge = buffer_get_string(&m, NULL); |
| 970 | buffer_free(&m); |
| 971 | |
| 972 | mm_chall_setup(name, infotxt, numprompts, prompts, echo_on); |
| 973 | (*prompts)[0] = challenge; |
| 974 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 975 | debug3("%s: received challenge: %s", __func__, challenge); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 976 | |
| 977 | return (0); |
| 978 | } |
| 979 | |
| 980 | int |
| 981 | mm_bsdauth_respond(void *ctx, u_int numresponses, char **responses) |
| 982 | { |
| 983 | Buffer m; |
| 984 | int authok; |
| 985 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 986 | debug3("%s: entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 987 | if (numresponses != 1) |
| 988 | return (-1); |
| 989 | |
| 990 | buffer_init(&m); |
| 991 | buffer_put_cstring(&m, responses[0]); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 992 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_BSDAUTHRESPOND, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 993 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 994 | mm_request_receive_expect(pmonitor->m_recvfd, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 995 | MONITOR_ANS_BSDAUTHRESPOND, &m); |
| 996 | |
| 997 | authok = buffer_get_int(&m); |
| 998 | buffer_free(&m); |
| 999 | |
| 1000 | return ((authok == 0) ? -1 : 0); |
| 1001 | } |
| 1002 | |
Darren Tucker | 042e2e8 | 2004-07-08 23:09:42 +1000 | [diff] [blame] | 1003 | #ifdef SKEY |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1004 | int |
| 1005 | mm_skey_query(void *ctx, char **name, char **infotxt, |
| 1006 | u_int *numprompts, char ***prompts, u_int **echo_on) |
| 1007 | { |
| 1008 | Buffer m; |
Damien Miller | b7df3af | 2003-02-24 11:55:46 +1100 | [diff] [blame] | 1009 | u_int success; |
Darren Tucker | d6a23f2 | 2006-08-05 18:50:35 +1000 | [diff] [blame] | 1010 | char *challenge; |
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); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 1015 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYQUERY, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1016 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 1017 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_SKEYQUERY, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1018 | &m); |
Damien Miller | b7df3af | 2003-02-24 11:55:46 +1100 | [diff] [blame] | 1019 | success = buffer_get_int(&m); |
| 1020 | if (success == 0) { |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1021 | debug3("%s: no challenge", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1022 | buffer_free(&m); |
| 1023 | return (-1); |
| 1024 | } |
| 1025 | |
| 1026 | /* Get the challenge, and format the response */ |
| 1027 | challenge = buffer_get_string(&m, NULL); |
| 1028 | buffer_free(&m); |
| 1029 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1030 | debug3("%s: received challenge: %s", __func__, challenge); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1031 | |
| 1032 | mm_chall_setup(name, infotxt, numprompts, prompts, echo_on); |
| 1033 | |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 1034 | xasprintf(*prompts, "%s%s", challenge, SKEY_PROMPT); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1035 | xfree(challenge); |
| 1036 | |
| 1037 | return (0); |
| 1038 | } |
| 1039 | |
| 1040 | int |
| 1041 | mm_skey_respond(void *ctx, u_int numresponses, char **responses) |
| 1042 | { |
| 1043 | Buffer m; |
| 1044 | int authok; |
| 1045 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1046 | debug3("%s: entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1047 | if (numresponses != 1) |
| 1048 | return (-1); |
| 1049 | |
| 1050 | buffer_init(&m); |
| 1051 | buffer_put_cstring(&m, responses[0]); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 1052 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SKEYRESPOND, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1053 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 1054 | mm_request_receive_expect(pmonitor->m_recvfd, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1055 | MONITOR_ANS_SKEYRESPOND, &m); |
| 1056 | |
| 1057 | authok = buffer_get_int(&m); |
| 1058 | buffer_free(&m); |
| 1059 | |
| 1060 | return ((authok == 0) ? -1 : 0); |
| 1061 | } |
Darren Tucker | 042e2e8 | 2004-07-08 23:09:42 +1000 | [diff] [blame] | 1062 | #endif /* SKEY */ |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1063 | |
| 1064 | void |
| 1065 | mm_ssh1_session_id(u_char session_id[16]) |
| 1066 | { |
| 1067 | Buffer m; |
| 1068 | int i; |
| 1069 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1070 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1071 | |
| 1072 | buffer_init(&m); |
| 1073 | for (i = 0; i < 16; i++) |
| 1074 | buffer_put_char(&m, session_id[i]); |
| 1075 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 1076 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_SESSID, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1077 | buffer_free(&m); |
| 1078 | } |
| 1079 | |
| 1080 | int |
| 1081 | mm_auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey) |
| 1082 | { |
| 1083 | Buffer m; |
| 1084 | Key *key; |
| 1085 | u_char *blob; |
| 1086 | u_int blen; |
Damien Miller | 06ebedf | 2003-02-24 12:03:38 +1100 | [diff] [blame] | 1087 | int allowed = 0, have_forced = 0; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1088 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1089 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1090 | |
| 1091 | buffer_init(&m); |
| 1092 | buffer_put_bignum2(&m, client_n); |
| 1093 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 1094 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSAKEYALLOWED, &m); |
| 1095 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSAKEYALLOWED, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1096 | |
| 1097 | allowed = buffer_get_int(&m); |
| 1098 | |
Damien Miller | 06ebedf | 2003-02-24 12:03:38 +1100 | [diff] [blame] | 1099 | /* fake forced command */ |
| 1100 | auth_clear_options(); |
| 1101 | have_forced = buffer_get_int(&m); |
| 1102 | forced_command = have_forced ? xstrdup("true") : NULL; |
| 1103 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1104 | if (allowed && rkey != NULL) { |
| 1105 | blob = buffer_get_string(&m, &blen); |
| 1106 | if ((key = key_from_blob(blob, blen)) == NULL) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1107 | fatal("%s: key_from_blob failed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1108 | *rkey = key; |
| 1109 | xfree(blob); |
| 1110 | } |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1111 | buffer_free(&m); |
| 1112 | |
| 1113 | return (allowed); |
| 1114 | } |
| 1115 | |
| 1116 | BIGNUM * |
| 1117 | mm_auth_rsa_generate_challenge(Key *key) |
| 1118 | { |
| 1119 | Buffer m; |
| 1120 | BIGNUM *challenge; |
| 1121 | u_char *blob; |
| 1122 | u_int blen; |
| 1123 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1124 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1125 | |
| 1126 | if ((challenge = BN_new()) == NULL) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1127 | fatal("%s: BN_new failed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1128 | |
| 1129 | key->type = KEY_RSA; /* XXX cheat for key_to_blob */ |
| 1130 | if (key_to_blob(key, &blob, &blen) == 0) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1131 | fatal("%s: key_to_blob failed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1132 | key->type = KEY_RSA1; |
| 1133 | |
| 1134 | buffer_init(&m); |
| 1135 | buffer_put_string(&m, blob, blen); |
| 1136 | xfree(blob); |
| 1137 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 1138 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSACHALLENGE, &m); |
| 1139 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSACHALLENGE, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1140 | |
| 1141 | buffer_get_bignum2(&m, challenge); |
| 1142 | buffer_free(&m); |
| 1143 | |
| 1144 | return (challenge); |
| 1145 | } |
| 1146 | |
| 1147 | int |
| 1148 | mm_auth_rsa_verify_response(Key *key, BIGNUM *p, u_char response[16]) |
| 1149 | { |
| 1150 | Buffer m; |
| 1151 | u_char *blob; |
| 1152 | u_int blen; |
| 1153 | int success = 0; |
| 1154 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1155 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1156 | |
| 1157 | key->type = KEY_RSA; /* XXX cheat for key_to_blob */ |
| 1158 | if (key_to_blob(key, &blob, &blen) == 0) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1159 | fatal("%s: key_to_blob failed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1160 | key->type = KEY_RSA1; |
| 1161 | |
| 1162 | buffer_init(&m); |
| 1163 | buffer_put_string(&m, blob, blen); |
| 1164 | buffer_put_string(&m, response, 16); |
| 1165 | xfree(blob); |
| 1166 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 1167 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_RSARESPONSE, &m); |
| 1168 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_RSARESPONSE, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1169 | |
| 1170 | success = buffer_get_int(&m); |
| 1171 | buffer_free(&m); |
| 1172 | |
| 1173 | return (success); |
| 1174 | } |
Damien Miller | 25162f2 | 2002-09-12 09:47:29 +1000 | [diff] [blame] | 1175 | |
Darren Tucker | 2e0cf0d | 2005-02-08 21:52:47 +1100 | [diff] [blame] | 1176 | #ifdef SSH_AUDIT_EVENTS |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 1177 | void |
| 1178 | mm_audit_event(ssh_audit_event_t event) |
| 1179 | { |
| 1180 | Buffer m; |
| 1181 | |
| 1182 | debug3("%s entering", __func__); |
| 1183 | |
| 1184 | buffer_init(&m); |
| 1185 | buffer_put_int(&m, event); |
| 1186 | |
| 1187 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_EVENT, &m); |
| 1188 | buffer_free(&m); |
| 1189 | } |
| 1190 | |
| 1191 | void |
| 1192 | mm_audit_run_command(const char *command) |
| 1193 | { |
| 1194 | Buffer m; |
| 1195 | |
| 1196 | debug3("%s entering command %s", __func__, command); |
| 1197 | |
| 1198 | buffer_init(&m); |
| 1199 | buffer_put_cstring(&m, command); |
| 1200 | |
| 1201 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_AUDIT_COMMAND, &m); |
| 1202 | buffer_free(&m); |
| 1203 | } |
Darren Tucker | 2e0cf0d | 2005-02-08 21:52:47 +1100 | [diff] [blame] | 1204 | #endif /* SSH_AUDIT_EVENTS */ |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 1205 | |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 1206 | #ifdef GSSAPI |
| 1207 | OM_uint32 |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1208 | mm_ssh_gssapi_server_ctx(Gssctxt **ctx, gss_OID goid) |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 1209 | { |
| 1210 | Buffer m; |
| 1211 | OM_uint32 major; |
| 1212 | |
| 1213 | /* Client doesn't get to see the context */ |
| 1214 | *ctx = NULL; |
| 1215 | |
| 1216 | buffer_init(&m); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1217 | buffer_put_string(&m, goid->elements, goid->length); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 1218 | |
| 1219 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSETUP, &m); |
| 1220 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSETUP, &m); |
| 1221 | |
| 1222 | major = buffer_get_int(&m); |
| 1223 | |
| 1224 | buffer_free(&m); |
| 1225 | return (major); |
| 1226 | } |
| 1227 | |
| 1228 | OM_uint32 |
| 1229 | mm_ssh_gssapi_accept_ctx(Gssctxt *ctx, gss_buffer_desc *in, |
| 1230 | gss_buffer_desc *out, OM_uint32 *flags) |
| 1231 | { |
| 1232 | Buffer m; |
| 1233 | OM_uint32 major; |
Darren Tucker | 600ad8d | 2003-08-26 12:10:48 +1000 | [diff] [blame] | 1234 | u_int len; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 1235 | |
| 1236 | buffer_init(&m); |
| 1237 | buffer_put_string(&m, in->value, in->length); |
| 1238 | |
| 1239 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSSTEP, &m); |
| 1240 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSSTEP, &m); |
| 1241 | |
| 1242 | major = buffer_get_int(&m); |
Darren Tucker | 600ad8d | 2003-08-26 12:10:48 +1000 | [diff] [blame] | 1243 | out->value = buffer_get_string(&m, &len); |
| 1244 | out->length = len; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 1245 | if (flags) |
| 1246 | *flags = buffer_get_int(&m); |
| 1247 | |
| 1248 | buffer_free(&m); |
| 1249 | |
| 1250 | return (major); |
| 1251 | } |
| 1252 | |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 1253 | OM_uint32 |
| 1254 | mm_ssh_gssapi_checkmic(Gssctxt *ctx, gss_buffer_t gssbuf, gss_buffer_t gssmic) |
| 1255 | { |
| 1256 | Buffer m; |
| 1257 | OM_uint32 major; |
| 1258 | |
| 1259 | buffer_init(&m); |
| 1260 | buffer_put_string(&m, gssbuf->value, gssbuf->length); |
| 1261 | buffer_put_string(&m, gssmic->value, gssmic->length); |
| 1262 | |
| 1263 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSCHECKMIC, &m); |
| 1264 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSCHECKMIC, |
| 1265 | &m); |
| 1266 | |
| 1267 | major = buffer_get_int(&m); |
| 1268 | buffer_free(&m); |
| 1269 | return(major); |
| 1270 | } |
| 1271 | |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 1272 | int |
| 1273 | mm_ssh_gssapi_userok(char *user) |
| 1274 | { |
| 1275 | Buffer m; |
| 1276 | int authenticated = 0; |
| 1277 | |
| 1278 | buffer_init(&m); |
| 1279 | |
| 1280 | mm_request_send(pmonitor->m_recvfd, MONITOR_REQ_GSSUSEROK, &m); |
| 1281 | mm_request_receive_expect(pmonitor->m_recvfd, MONITOR_ANS_GSSUSEROK, |
| 1282 | &m); |
| 1283 | |
| 1284 | authenticated = buffer_get_int(&m); |
| 1285 | |
| 1286 | buffer_free(&m); |
| 1287 | debug3("%s: user %sauthenticated",__func__, authenticated ? "" : "not "); |
| 1288 | return (authenticated); |
| 1289 | } |
| 1290 | #endif /* GSSAPI */ |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 1291 | |
| 1292 | #ifdef JPAKE |
| 1293 | void |
| 1294 | mm_auth2_jpake_get_pwdata(Authctxt *authctxt, BIGNUM **s, |
| 1295 | char **hash_scheme, char **salt) |
| 1296 | { |
| 1297 | Buffer m; |
| 1298 | |
| 1299 | debug3("%s entering", __func__); |
| 1300 | |
| 1301 | buffer_init(&m); |
| 1302 | mm_request_send(pmonitor->m_recvfd, |
| 1303 | MONITOR_REQ_JPAKE_GET_PWDATA, &m); |
| 1304 | |
| 1305 | debug3("%s: waiting for MONITOR_ANS_JPAKE_GET_PWDATA", __func__); |
| 1306 | mm_request_receive_expect(pmonitor->m_recvfd, |
| 1307 | MONITOR_ANS_JPAKE_GET_PWDATA, &m); |
| 1308 | |
| 1309 | *hash_scheme = buffer_get_string(&m, NULL); |
| 1310 | *salt = buffer_get_string(&m, NULL); |
| 1311 | |
| 1312 | buffer_free(&m); |
| 1313 | } |
| 1314 | |
| 1315 | void |
Damien Miller | cee8523 | 2009-03-06 00:58:22 +1100 | [diff] [blame] | 1316 | mm_jpake_step1(struct modp_group *grp, |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 1317 | u_char **id, u_int *id_len, |
| 1318 | BIGNUM **priv1, BIGNUM **priv2, BIGNUM **g_priv1, BIGNUM **g_priv2, |
| 1319 | u_char **priv1_proof, u_int *priv1_proof_len, |
| 1320 | u_char **priv2_proof, u_int *priv2_proof_len) |
| 1321 | { |
| 1322 | Buffer m; |
| 1323 | |
| 1324 | debug3("%s entering", __func__); |
| 1325 | |
| 1326 | buffer_init(&m); |
| 1327 | mm_request_send(pmonitor->m_recvfd, |
| 1328 | MONITOR_REQ_JPAKE_STEP1, &m); |
| 1329 | |
| 1330 | debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP1", __func__); |
| 1331 | mm_request_receive_expect(pmonitor->m_recvfd, |
| 1332 | MONITOR_ANS_JPAKE_STEP1, &m); |
| 1333 | |
| 1334 | if ((*priv1 = BN_new()) == NULL || |
| 1335 | (*priv2 = BN_new()) == NULL || |
| 1336 | (*g_priv1 = BN_new()) == NULL || |
| 1337 | (*g_priv2 = BN_new()) == NULL) |
| 1338 | fatal("%s: BN_new", __func__); |
| 1339 | |
| 1340 | *id = buffer_get_string(&m, id_len); |
| 1341 | /* priv1 and priv2 are, well, private */ |
| 1342 | buffer_get_bignum2(&m, *g_priv1); |
| 1343 | buffer_get_bignum2(&m, *g_priv2); |
| 1344 | *priv1_proof = buffer_get_string(&m, priv1_proof_len); |
| 1345 | *priv2_proof = buffer_get_string(&m, priv2_proof_len); |
| 1346 | |
| 1347 | buffer_free(&m); |
| 1348 | } |
| 1349 | |
| 1350 | void |
Damien Miller | cee8523 | 2009-03-06 00:58:22 +1100 | [diff] [blame] | 1351 | mm_jpake_step2(struct modp_group *grp, BIGNUM *s, |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 1352 | BIGNUM *mypub1, BIGNUM *theirpub1, BIGNUM *theirpub2, BIGNUM *mypriv2, |
| 1353 | const u_char *theirid, u_int theirid_len, |
| 1354 | const u_char *myid, u_int myid_len, |
| 1355 | const u_char *theirpub1_proof, u_int theirpub1_proof_len, |
| 1356 | const u_char *theirpub2_proof, u_int theirpub2_proof_len, |
| 1357 | BIGNUM **newpub, |
| 1358 | u_char **newpub_exponent_proof, u_int *newpub_exponent_proof_len) |
| 1359 | { |
| 1360 | Buffer m; |
| 1361 | |
| 1362 | debug3("%s entering", __func__); |
| 1363 | |
| 1364 | buffer_init(&m); |
| 1365 | /* monitor already has all bignums except theirpub1, theirpub2 */ |
| 1366 | buffer_put_bignum2(&m, theirpub1); |
| 1367 | buffer_put_bignum2(&m, theirpub2); |
| 1368 | /* monitor already knows our id */ |
| 1369 | buffer_put_string(&m, theirid, theirid_len); |
| 1370 | buffer_put_string(&m, theirpub1_proof, theirpub1_proof_len); |
| 1371 | buffer_put_string(&m, theirpub2_proof, theirpub2_proof_len); |
| 1372 | |
| 1373 | mm_request_send(pmonitor->m_recvfd, |
| 1374 | MONITOR_REQ_JPAKE_STEP2, &m); |
| 1375 | |
| 1376 | debug3("%s: waiting for MONITOR_ANS_JPAKE_STEP2", __func__); |
| 1377 | mm_request_receive_expect(pmonitor->m_recvfd, |
| 1378 | MONITOR_ANS_JPAKE_STEP2, &m); |
| 1379 | |
| 1380 | if ((*newpub = BN_new()) == NULL) |
| 1381 | fatal("%s: BN_new", __func__); |
| 1382 | |
| 1383 | buffer_get_bignum2(&m, *newpub); |
| 1384 | *newpub_exponent_proof = buffer_get_string(&m, |
| 1385 | newpub_exponent_proof_len); |
| 1386 | |
| 1387 | buffer_free(&m); |
| 1388 | } |
| 1389 | |
| 1390 | void |
Damien Miller | cee8523 | 2009-03-06 00:58:22 +1100 | [diff] [blame] | 1391 | mm_jpake_key_confirm(struct modp_group *grp, BIGNUM *s, BIGNUM *step2_val, |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 1392 | BIGNUM *mypriv2, BIGNUM *mypub1, BIGNUM *mypub2, |
| 1393 | BIGNUM *theirpub1, BIGNUM *theirpub2, |
| 1394 | const u_char *my_id, u_int my_id_len, |
| 1395 | const u_char *their_id, u_int their_id_len, |
| 1396 | const u_char *sess_id, u_int sess_id_len, |
| 1397 | const u_char *theirpriv2_s_proof, u_int theirpriv2_s_proof_len, |
| 1398 | BIGNUM **k, |
| 1399 | u_char **confirm_hash, u_int *confirm_hash_len) |
| 1400 | { |
| 1401 | Buffer m; |
| 1402 | |
| 1403 | debug3("%s entering", __func__); |
| 1404 | |
| 1405 | buffer_init(&m); |
| 1406 | /* monitor already has all bignums except step2_val */ |
| 1407 | buffer_put_bignum2(&m, step2_val); |
| 1408 | /* monitor already knows all the ids */ |
| 1409 | buffer_put_string(&m, theirpriv2_s_proof, theirpriv2_s_proof_len); |
| 1410 | |
| 1411 | mm_request_send(pmonitor->m_recvfd, |
| 1412 | MONITOR_REQ_JPAKE_KEY_CONFIRM, &m); |
| 1413 | |
| 1414 | debug3("%s: waiting for MONITOR_ANS_JPAKE_KEY_CONFIRM", __func__); |
| 1415 | mm_request_receive_expect(pmonitor->m_recvfd, |
| 1416 | MONITOR_ANS_JPAKE_KEY_CONFIRM, &m); |
| 1417 | |
| 1418 | /* 'k' is sensitive and stays in the monitor */ |
| 1419 | *confirm_hash = buffer_get_string(&m, confirm_hash_len); |
| 1420 | |
| 1421 | buffer_free(&m); |
| 1422 | } |
| 1423 | |
| 1424 | int |
| 1425 | mm_jpake_check_confirm(const BIGNUM *k, |
| 1426 | const u_char *peer_id, u_int peer_id_len, |
| 1427 | const u_char *sess_id, u_int sess_id_len, |
| 1428 | const u_char *peer_confirm_hash, u_int peer_confirm_hash_len) |
| 1429 | { |
| 1430 | Buffer m; |
| 1431 | int success = 0; |
| 1432 | |
| 1433 | debug3("%s entering", __func__); |
| 1434 | |
| 1435 | buffer_init(&m); |
| 1436 | /* k is dummy in slave, ignored */ |
| 1437 | /* monitor knows all the ids */ |
| 1438 | buffer_put_string(&m, peer_confirm_hash, peer_confirm_hash_len); |
| 1439 | mm_request_send(pmonitor->m_recvfd, |
| 1440 | MONITOR_REQ_JPAKE_CHECK_CONFIRM, &m); |
| 1441 | |
| 1442 | debug3("%s: waiting for MONITOR_ANS_JPAKE_CHECK_CONFIRM", __func__); |
| 1443 | mm_request_receive_expect(pmonitor->m_recvfd, |
| 1444 | MONITOR_ANS_JPAKE_CHECK_CONFIRM, &m); |
| 1445 | |
| 1446 | success = buffer_get_int(&m); |
| 1447 | buffer_free(&m); |
| 1448 | |
| 1449 | debug3("%s: success = %d", __func__, success); |
| 1450 | return success; |
| 1451 | } |
| 1452 | #endif /* JPAKE */ |