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