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