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