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