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