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