djm@openbsd.org | 1f729f0 | 2015-01-13 07:39:19 +0000 | [diff] [blame] | 1 | /* $OpenBSD: monitor.c,v 1.137 2015/01/13 07:39:19 djm 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" |
Damien Miller | 9cf6d07 | 2006-03-15 11:29:24 +1100 | [diff] [blame] | 29 | |
| 30 | #include <sys/types.h> |
Damien Miller | 8dbffe7 | 2006-08-05 11:02:17 +1000 | [diff] [blame] | 31 | #include <sys/param.h> |
Damien Miller | e3b60b5 | 2006-07-10 21:08:03 +1000 | [diff] [blame] | 32 | #include <sys/socket.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 33 | #include "openbsd-compat/sys-tree.h" |
Damien Miller | 9cf6d07 | 2006-03-15 11:29:24 +1100 | [diff] [blame] | 34 | #include <sys/wait.h> |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 35 | |
Darren Tucker | 3997249 | 2006-07-12 22:22:46 +1000 | [diff] [blame] | 36 | #include <errno.h> |
Damien Miller | 57cf638 | 2006-07-10 21:13:46 +1000 | [diff] [blame] | 37 | #include <fcntl.h> |
Damien Miller | 6645e7a | 2006-03-15 14:42:54 +1100 | [diff] [blame] | 38 | #ifdef HAVE_PATHS_H |
Damien Miller | 03e2003 | 2006-03-15 11:16:59 +1100 | [diff] [blame] | 39 | #include <paths.h> |
Damien Miller | 6645e7a | 2006-03-15 14:42:54 +1100 | [diff] [blame] | 40 | #endif |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 41 | #include <pwd.h> |
Damien Miller | 6ff3cad | 2006-03-15 11:52:09 +1100 | [diff] [blame] | 42 | #include <signal.h> |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 43 | #include <stdlib.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 44 | #include <string.h> |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 45 | #include <stdarg.h> |
| 46 | #include <stdio.h> |
Damien Miller | 607aede | 2006-09-01 15:48:19 +1000 | [diff] [blame] | 47 | #include <unistd.h> |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 48 | #ifdef HAVE_POLL_H |
| 49 | #include <poll.h> |
| 50 | #else |
| 51 | # ifdef HAVE_SYS_POLL_H |
| 52 | # include <sys/poll.h> |
| 53 | # endif |
| 54 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 55 | |
| 56 | #ifdef SKEY |
| 57 | #include <skey.h> |
| 58 | #endif |
| 59 | |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 60 | #ifdef WITH_OPENSSL |
Damien Miller | 03e2003 | 2006-03-15 11:16:59 +1100 | [diff] [blame] | 61 | #include <openssl/dh.h> |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 62 | #endif |
Damien Miller | 03e2003 | 2006-03-15 11:16:59 +1100 | [diff] [blame] | 63 | |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 64 | #include "openbsd-compat/sys-queue.h" |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 65 | #include "atomicio.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 66 | #include "xmalloc.h" |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 67 | #include "ssh.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 68 | #include "key.h" |
| 69 | #include "buffer.h" |
| 70 | #include "hostfile.h" |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 71 | #include "auth.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 72 | #include "cipher.h" |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 73 | #include "kex.h" |
| 74 | #include "dh.h" |
Ben Lindstrom | 036768e | 2004-04-08 16:12:30 +0000 | [diff] [blame] | 75 | #ifdef TARGET_OS_MAC /* XXX Broken krb5 headers on Mac */ |
| 76 | #undef TARGET_OS_MAC |
Ben Lindstrom | 1b9f2a6 | 2004-04-08 05:11:03 +0000 | [diff] [blame] | 77 | #include "zlib.h" |
Ben Lindstrom | 036768e | 2004-04-08 16:12:30 +0000 | [diff] [blame] | 78 | #define TARGET_OS_MAC 1 |
| 79 | #else |
| 80 | #include "zlib.h" |
| 81 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 82 | #include "packet.h" |
| 83 | #include "auth-options.h" |
| 84 | #include "sshpty.h" |
| 85 | #include "channels.h" |
| 86 | #include "session.h" |
| 87 | #include "sshlogin.h" |
| 88 | #include "canohost.h" |
| 89 | #include "log.h" |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 90 | #include "misc.h" |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 91 | #include "servconf.h" |
| 92 | #include "monitor.h" |
| 93 | #include "monitor_mm.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 94 | #ifdef GSSAPI |
| 95 | #include "ssh-gss.h" |
| 96 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 97 | #include "monitor_wrap.h" |
| 98 | #include "monitor_fdpass.h" |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 99 | #include "compat.h" |
| 100 | #include "ssh2.h" |
Darren Tucker | b422afa | 2009-06-21 18:58:46 +1000 | [diff] [blame] | 101 | #include "roaming.h" |
Damien Miller | 85b45e0 | 2013-07-20 13:21:52 +1000 | [diff] [blame] | 102 | #include "authfd.h" |
djm@openbsd.org | 1f729f0 | 2015-01-13 07:39:19 +0000 | [diff] [blame] | 103 | #include "match.h" |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 104 | |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 105 | #ifdef GSSAPI |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 106 | static Gssctxt *gsscontext = NULL; |
| 107 | #endif |
| 108 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 109 | /* Imports */ |
| 110 | extern ServerOptions options; |
| 111 | extern u_int utmp_len; |
| 112 | extern Newkeys *current_keys[]; |
| 113 | extern z_stream incoming_stream; |
| 114 | extern z_stream outgoing_stream; |
| 115 | extern u_char session_id[]; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 116 | extern Buffer auth_debug; |
| 117 | extern int auth_debug_init; |
Darren Tucker | 0999174 | 2004-07-17 17:05:14 +1000 | [diff] [blame] | 118 | extern Buffer loginmsg; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 119 | |
| 120 | /* State exported from the child */ |
| 121 | |
| 122 | struct { |
| 123 | z_stream incoming; |
| 124 | z_stream outgoing; |
| 125 | u_char *keyin; |
| 126 | u_int keyinlen; |
| 127 | u_char *keyout; |
| 128 | u_int keyoutlen; |
| 129 | u_char *ivin; |
| 130 | u_int ivinlen; |
| 131 | u_char *ivout; |
| 132 | u_int ivoutlen; |
Ben Lindstrom | 402c6cc | 2002-06-21 00:43:42 +0000 | [diff] [blame] | 133 | u_char *ssh1key; |
| 134 | u_int ssh1keylen; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 135 | int ssh1cipher; |
| 136 | int ssh1protoflags; |
| 137 | u_char *input; |
| 138 | u_int ilen; |
| 139 | u_char *output; |
| 140 | u_int olen; |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 141 | u_int64_t sent_bytes; |
| 142 | u_int64_t recv_bytes; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 143 | } child_state; |
| 144 | |
Damien Miller | 469954d | 2003-06-18 20:25:33 +1000 | [diff] [blame] | 145 | /* Functions on the monitor that answer unprivileged requests */ |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 146 | |
| 147 | int mm_answer_moduli(int, Buffer *); |
| 148 | int mm_answer_sign(int, Buffer *); |
| 149 | int mm_answer_pwnamallow(int, Buffer *); |
Damien Miller | 5ad9fd9 | 2002-05-13 11:07:41 +1000 | [diff] [blame] | 150 | int mm_answer_auth2_read_banner(int, Buffer *); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 151 | int mm_answer_authserv(int, Buffer *); |
| 152 | int mm_answer_authpassword(int, Buffer *); |
| 153 | int mm_answer_bsdauthquery(int, Buffer *); |
| 154 | int mm_answer_bsdauthrespond(int, Buffer *); |
| 155 | int mm_answer_skeyquery(int, Buffer *); |
| 156 | int mm_answer_skeyrespond(int, Buffer *); |
| 157 | int mm_answer_keyallowed(int, Buffer *); |
| 158 | int mm_answer_keyverify(int, Buffer *); |
| 159 | int mm_answer_pty(int, Buffer *); |
| 160 | int mm_answer_pty_cleanup(int, Buffer *); |
| 161 | int mm_answer_term(int, Buffer *); |
| 162 | int mm_answer_rsa_keyallowed(int, Buffer *); |
| 163 | int mm_answer_rsa_challenge(int, Buffer *); |
| 164 | int mm_answer_rsa_response(int, Buffer *); |
| 165 | int mm_answer_sesskey(int, Buffer *); |
| 166 | int mm_answer_sessid(int, Buffer *); |
| 167 | |
Damien Miller | 7941855 | 2002-04-23 20:28:48 +1000 | [diff] [blame] | 168 | #ifdef USE_PAM |
| 169 | int mm_answer_pam_start(int, Buffer *); |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 170 | int mm_answer_pam_account(int, Buffer *); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 171 | int mm_answer_pam_init_ctx(int, Buffer *); |
| 172 | int mm_answer_pam_query(int, Buffer *); |
| 173 | int mm_answer_pam_respond(int, Buffer *); |
| 174 | int mm_answer_pam_free_ctx(int, Buffer *); |
Damien Miller | 7941855 | 2002-04-23 20:28:48 +1000 | [diff] [blame] | 175 | #endif |
| 176 | |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 177 | #ifdef GSSAPI |
| 178 | int mm_answer_gss_setup_ctx(int, Buffer *); |
| 179 | int mm_answer_gss_accept_ctx(int, Buffer *); |
| 180 | int mm_answer_gss_userok(int, Buffer *); |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 181 | int mm_answer_gss_checkmic(int, Buffer *); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 182 | #endif |
Damien Miller | 25162f2 | 2002-09-12 09:47:29 +1000 | [diff] [blame] | 183 | |
Darren Tucker | 2e0cf0d | 2005-02-08 21:52:47 +1100 | [diff] [blame] | 184 | #ifdef SSH_AUDIT_EVENTS |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 185 | int mm_answer_audit_event(int, Buffer *); |
| 186 | int mm_answer_audit_command(int, Buffer *); |
| 187 | #endif |
| 188 | |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 189 | static int monitor_read_log(struct monitor *); |
| 190 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 191 | static Authctxt *authctxt; |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 192 | |
| 193 | #ifdef WITH_SSH1 |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 194 | static BIGNUM *ssh1_challenge = NULL; /* used for ssh1 rsa auth */ |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 195 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 196 | |
| 197 | /* local state for key verify */ |
| 198 | static u_char *key_blob = NULL; |
| 199 | static u_int key_bloblen = 0; |
| 200 | static int key_blobtype = MM_NOKEY; |
Damien Miller | a10f561 | 2002-09-12 09:49:15 +1000 | [diff] [blame] | 201 | static char *hostbased_cuser = NULL; |
| 202 | static char *hostbased_chost = NULL; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 203 | static char *auth_method = "unknown"; |
Damien Miller | 15b05cf | 2012-12-03 09:53:20 +1100 | [diff] [blame] | 204 | static char *auth_submethod = NULL; |
Darren Tucker | 502d384 | 2003-06-28 12:38:01 +1000 | [diff] [blame] | 205 | static u_int session_id2_len = 0; |
Ben Lindstrom | f67e077 | 2002-06-06 20:58:19 +0000 | [diff] [blame] | 206 | static u_char *session_id2 = NULL; |
Damien Miller | be64d43 | 2003-05-14 19:31:12 +1000 | [diff] [blame] | 207 | static pid_t monitor_child_pid; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 208 | |
| 209 | struct mon_table { |
| 210 | enum monitor_reqtype type; |
| 211 | int flags; |
| 212 | int (*f)(int, Buffer *); |
| 213 | }; |
| 214 | |
| 215 | #define MON_ISAUTH 0x0004 /* Required for Authentication */ |
| 216 | #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */ |
| 217 | #define MON_ONCE 0x0010 /* Disable after calling */ |
Damien Miller | 7a8f5b3 | 2006-03-31 23:14:23 +1100 | [diff] [blame] | 218 | #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */ |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 219 | |
| 220 | #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE) |
| 221 | |
| 222 | #define MON_PERMIT 0x1000 /* Request is permitted */ |
| 223 | |
| 224 | struct mon_table mon_dispatch_proto20[] = { |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 225 | #ifdef WITH_OPENSSL |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 226 | {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli}, |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 227 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 228 | {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign}, |
| 229 | {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow}, |
| 230 | {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv}, |
Damien Miller | 5ad9fd9 | 2002-05-13 11:07:41 +1000 | [diff] [blame] | 231 | {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner}, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 232 | {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword}, |
Damien Miller | 7941855 | 2002-04-23 20:28:48 +1000 | [diff] [blame] | 233 | #ifdef USE_PAM |
| 234 | {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start}, |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 235 | {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account}, |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 236 | {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx}, |
| 237 | {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query}, |
| 238 | {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond}, |
| 239 | {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx}, |
Kevin Steves | bd1901b | 2002-04-01 18:04:35 +0000 | [diff] [blame] | 240 | #endif |
Darren Tucker | 2e0cf0d | 2005-02-08 21:52:47 +1100 | [diff] [blame] | 241 | #ifdef SSH_AUDIT_EVENTS |
Darren Tucker | 3745e2b | 2005-03-06 22:31:35 +1100 | [diff] [blame] | 242 | {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event}, |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 243 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 244 | #ifdef BSD_AUTH |
| 245 | {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery}, |
Damien Miller | 0b70b54 | 2006-03-15 11:20:03 +1100 | [diff] [blame] | 246 | {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond}, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 247 | #endif |
| 248 | #ifdef SKEY |
| 249 | {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery}, |
| 250 | {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond}, |
| 251 | #endif |
| 252 | {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed}, |
| 253 | {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify}, |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 254 | #ifdef GSSAPI |
| 255 | {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx}, |
| 256 | {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx}, |
| 257 | {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok}, |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 258 | {MONITOR_REQ_GSSCHECKMIC, MON_ISAUTH, mm_answer_gss_checkmic}, |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 259 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 260 | {0, 0, NULL} |
| 261 | }; |
| 262 | |
| 263 | struct mon_table mon_dispatch_postauth20[] = { |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 264 | #ifdef WITH_OPENSSL |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 265 | {MONITOR_REQ_MODULI, 0, mm_answer_moduli}, |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 266 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 267 | {MONITOR_REQ_SIGN, 0, mm_answer_sign}, |
| 268 | {MONITOR_REQ_PTY, 0, mm_answer_pty}, |
| 269 | {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup}, |
| 270 | {MONITOR_REQ_TERM, 0, mm_answer_term}, |
Darren Tucker | 2e0cf0d | 2005-02-08 21:52:47 +1100 | [diff] [blame] | 271 | #ifdef SSH_AUDIT_EVENTS |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 272 | {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event}, |
| 273 | {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command}, |
| 274 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 275 | {0, 0, NULL} |
| 276 | }; |
| 277 | |
| 278 | struct mon_table mon_dispatch_proto15[] = { |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 279 | #ifdef WITH_SSH1 |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 280 | {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow}, |
| 281 | {MONITOR_REQ_SESSKEY, MON_ONCE, mm_answer_sesskey}, |
| 282 | {MONITOR_REQ_SESSID, MON_ONCE, mm_answer_sessid}, |
| 283 | {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword}, |
Damien Miller | 7a8f5b3 | 2006-03-31 23:14:23 +1100 | [diff] [blame] | 284 | {MONITOR_REQ_RSAKEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_rsa_keyallowed}, |
| 285 | {MONITOR_REQ_KEYALLOWED, MON_ISAUTH|MON_ALOG, mm_answer_keyallowed}, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 286 | {MONITOR_REQ_RSACHALLENGE, MON_ONCE, mm_answer_rsa_challenge}, |
| 287 | {MONITOR_REQ_RSARESPONSE, MON_ONCE|MON_AUTHDECIDE, mm_answer_rsa_response}, |
| 288 | #ifdef BSD_AUTH |
| 289 | {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery}, |
Damien Miller | 0b70b54 | 2006-03-15 11:20:03 +1100 | [diff] [blame] | 290 | {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond}, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 291 | #endif |
| 292 | #ifdef SKEY |
| 293 | {MONITOR_REQ_SKEYQUERY, MON_ISAUTH, mm_answer_skeyquery}, |
| 294 | {MONITOR_REQ_SKEYRESPOND, MON_AUTH, mm_answer_skeyrespond}, |
| 295 | #endif |
Damien Miller | a33501b | 2002-05-08 12:24:42 +1000 | [diff] [blame] | 296 | #ifdef USE_PAM |
| 297 | {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start}, |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 298 | {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account}, |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 299 | {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx}, |
| 300 | {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query}, |
| 301 | {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond}, |
| 302 | {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx}, |
Damien Miller | a33501b | 2002-05-08 12:24:42 +1000 | [diff] [blame] | 303 | #endif |
Darren Tucker | 2e0cf0d | 2005-02-08 21:52:47 +1100 | [diff] [blame] | 304 | #ifdef SSH_AUDIT_EVENTS |
Darren Tucker | 3745e2b | 2005-03-06 22:31:35 +1100 | [diff] [blame] | 305 | {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event}, |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 306 | #endif |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 307 | #endif /* WITH_SSH1 */ |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 308 | {0, 0, NULL} |
| 309 | }; |
| 310 | |
| 311 | struct mon_table mon_dispatch_postauth15[] = { |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 312 | #ifdef WITH_SSH1 |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 313 | {MONITOR_REQ_PTY, MON_ONCE, mm_answer_pty}, |
| 314 | {MONITOR_REQ_PTYCLEANUP, MON_ONCE, mm_answer_pty_cleanup}, |
| 315 | {MONITOR_REQ_TERM, 0, mm_answer_term}, |
Darren Tucker | 2e0cf0d | 2005-02-08 21:52:47 +1100 | [diff] [blame] | 316 | #ifdef SSH_AUDIT_EVENTS |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 317 | {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event}, |
Darren Tucker | 5965ae1 | 2006-09-17 12:00:13 +1000 | [diff] [blame] | 318 | {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT|MON_ONCE, mm_answer_audit_command}, |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 319 | #endif |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 320 | #endif /* WITH_SSH1 */ |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 321 | {0, 0, NULL} |
| 322 | }; |
| 323 | |
| 324 | struct mon_table *mon_dispatch; |
| 325 | |
| 326 | /* Specifies if a certain message is allowed at the moment */ |
| 327 | |
| 328 | static void |
| 329 | monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit) |
| 330 | { |
| 331 | while (ent->f != NULL) { |
| 332 | if (ent->type == type) { |
| 333 | ent->flags &= ~MON_PERMIT; |
| 334 | ent->flags |= permit ? MON_PERMIT : 0; |
| 335 | return; |
| 336 | } |
| 337 | ent++; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | static void |
| 342 | monitor_permit_authentications(int permit) |
| 343 | { |
| 344 | struct mon_table *ent = mon_dispatch; |
| 345 | |
| 346 | while (ent->f != NULL) { |
| 347 | if (ent->flags & MON_AUTH) { |
| 348 | ent->flags &= ~MON_PERMIT; |
| 349 | ent->flags |= permit ? MON_PERMIT : 0; |
| 350 | } |
| 351 | ent++; |
| 352 | } |
| 353 | } |
| 354 | |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 355 | void |
| 356 | monitor_child_preauth(Authctxt *_authctxt, struct monitor *pmonitor) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 357 | { |
| 358 | struct mon_table *ent; |
Damien Miller | 15b05cf | 2012-12-03 09:53:20 +1100 | [diff] [blame] | 359 | int authenticated = 0, partial = 0; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 360 | |
| 361 | debug3("preauth child monitor started"); |
| 362 | |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 363 | close(pmonitor->m_recvfd); |
| 364 | close(pmonitor->m_log_sendfd); |
| 365 | pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1; |
| 366 | |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 367 | authctxt = _authctxt; |
| 368 | memset(authctxt, 0, sizeof(*authctxt)); |
| 369 | |
Darren Tucker | de0de39 | 2005-03-31 23:52:04 +1000 | [diff] [blame] | 370 | authctxt->loginmsg = &loginmsg; |
| 371 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 372 | if (compat20) { |
| 373 | mon_dispatch = mon_dispatch_proto20; |
| 374 | |
| 375 | /* Permit requests for moduli and signatures */ |
| 376 | monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1); |
| 377 | monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1); |
| 378 | } else { |
| 379 | mon_dispatch = mon_dispatch_proto15; |
| 380 | |
| 381 | monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 1); |
| 382 | } |
| 383 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 384 | /* The first few requests do not require asynchronous access */ |
| 385 | while (!authenticated) { |
Damien Miller | 15b05cf | 2012-12-03 09:53:20 +1100 | [diff] [blame] | 386 | partial = 0; |
Damien Miller | 7a8f5b3 | 2006-03-31 23:14:23 +1100 | [diff] [blame] | 387 | auth_method = "unknown"; |
Damien Miller | 15b05cf | 2012-12-03 09:53:20 +1100 | [diff] [blame] | 388 | auth_submethod = NULL; |
Darren Tucker | fbba735 | 2006-11-07 23:16:08 +1100 | [diff] [blame] | 389 | authenticated = (monitor_read(pmonitor, mon_dispatch, &ent) == 1); |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 390 | |
| 391 | /* Special handling for multiple required authentications */ |
| 392 | if (options.num_auth_methods != 0) { |
| 393 | if (!compat20) |
| 394 | fatal("AuthenticationMethods is not supported" |
| 395 | "with SSH protocol 1"); |
| 396 | if (authenticated && |
| 397 | !auth2_update_methods_lists(authctxt, |
Damien Miller | 91a55f2 | 2013-04-23 15:18:10 +1000 | [diff] [blame] | 398 | auth_method, auth_submethod)) { |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 399 | debug3("%s: method %s: partial", __func__, |
| 400 | auth_method); |
| 401 | authenticated = 0; |
Damien Miller | 15b05cf | 2012-12-03 09:53:20 +1100 | [diff] [blame] | 402 | partial = 1; |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 406 | if (authenticated) { |
| 407 | if (!(ent->flags & MON_AUTHDECIDE)) |
| 408 | fatal("%s: unexpected authentication from %d", |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 409 | __func__, ent->type); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 410 | if (authctxt->pw->pw_uid == 0 && |
| 411 | !auth_root_allowed(auth_method)) |
| 412 | authenticated = 0; |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 413 | #ifdef USE_PAM |
| 414 | /* PAM needs to perform account checks after auth */ |
Damien Miller | 6aef38f | 2003-11-18 10:45:20 +1100 | [diff] [blame] | 415 | if (options.use_pam && authenticated) { |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 416 | Buffer m; |
| 417 | |
| 418 | buffer_init(&m); |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 419 | mm_request_receive_expect(pmonitor->m_sendfd, |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 420 | MONITOR_REQ_PAM_ACCOUNT, &m); |
| 421 | authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m); |
| 422 | buffer_free(&m); |
| 423 | } |
| 424 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 425 | } |
Damien Miller | 7a8f5b3 | 2006-03-31 23:14:23 +1100 | [diff] [blame] | 426 | if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) { |
Damien Miller | 15b05cf | 2012-12-03 09:53:20 +1100 | [diff] [blame] | 427 | auth_log(authctxt, authenticated, partial, |
Darren Tucker | 0acca37 | 2013-06-02 07:41:51 +1000 | [diff] [blame] | 428 | auth_method, auth_submethod); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 429 | if (!authenticated) |
| 430 | authctxt->failures++; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | if (!authctxt->valid) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 435 | fatal("%s: authenticated invalid user", __func__); |
Damien Miller | 7a8f5b3 | 2006-03-31 23:14:23 +1100 | [diff] [blame] | 436 | if (strcmp(auth_method, "unknown") == 0) |
| 437 | fatal("%s: authentication method name unknown", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 438 | |
| 439 | debug("%s: %s has been authenticated by privileged process", |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 440 | __func__, authctxt->user); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 441 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 442 | mm_get_keystate(pmonitor); |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 443 | |
Damien Miller | 6a1937e | 2012-12-12 10:44:38 +1100 | [diff] [blame] | 444 | /* Drain any buffered messages from the child */ |
| 445 | while (pmonitor->m_log_recvfd != -1 && monitor_read_log(pmonitor) == 0) |
| 446 | ; |
| 447 | |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 448 | close(pmonitor->m_sendfd); |
| 449 | close(pmonitor->m_log_recvfd); |
| 450 | pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Damien Miller | be64d43 | 2003-05-14 19:31:12 +1000 | [diff] [blame] | 453 | static void |
| 454 | monitor_set_child_handler(pid_t pid) |
| 455 | { |
| 456 | monitor_child_pid = pid; |
| 457 | } |
| 458 | |
| 459 | static void |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 460 | monitor_child_handler(int sig) |
Damien Miller | be64d43 | 2003-05-14 19:31:12 +1000 | [diff] [blame] | 461 | { |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 462 | kill(monitor_child_pid, sig); |
Damien Miller | be64d43 | 2003-05-14 19:31:12 +1000 | [diff] [blame] | 463 | } |
| 464 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 465 | void |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 466 | monitor_child_postauth(struct monitor *pmonitor) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 467 | { |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 468 | close(pmonitor->m_recvfd); |
| 469 | pmonitor->m_recvfd = -1; |
| 470 | |
Damien Miller | be64d43 | 2003-05-14 19:31:12 +1000 | [diff] [blame] | 471 | monitor_set_child_handler(pmonitor->m_pid); |
| 472 | signal(SIGHUP, &monitor_child_handler); |
| 473 | signal(SIGTERM, &monitor_child_handler); |
Darren Tucker | 7fa339b | 2007-05-20 15:10:16 +1000 | [diff] [blame] | 474 | signal(SIGINT, &monitor_child_handler); |
Damien Miller | 146218a | 2014-08-27 04:11:55 +1000 | [diff] [blame] | 475 | #ifdef SIGXFSZ |
| 476 | signal(SIGXFSZ, SIG_IGN); |
| 477 | #endif |
Damien Miller | be64d43 | 2003-05-14 19:31:12 +1000 | [diff] [blame] | 478 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 479 | if (compat20) { |
| 480 | mon_dispatch = mon_dispatch_postauth20; |
| 481 | |
| 482 | /* Permit requests for moduli and signatures */ |
| 483 | monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1); |
| 484 | monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1); |
| 485 | monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 486 | } else { |
| 487 | mon_dispatch = mon_dispatch_postauth15; |
| 488 | monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1); |
| 489 | } |
| 490 | if (!no_pty_flag) { |
| 491 | monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1); |
| 492 | monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1); |
| 493 | } |
| 494 | |
| 495 | for (;;) |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 496 | monitor_read(pmonitor, mon_dispatch, NULL); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | void |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 500 | monitor_sync(struct monitor *pmonitor) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 501 | { |
Damien Miller | 2d6b833 | 2002-06-21 15:59:49 +1000 | [diff] [blame] | 502 | if (options.compression) { |
| 503 | /* The member allocation is not visible, so sync it */ |
| 504 | mm_share_sync(&pmonitor->m_zlib, &pmonitor->m_zback); |
| 505 | } |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 508 | static int |
| 509 | monitor_read_log(struct monitor *pmonitor) |
| 510 | { |
| 511 | Buffer logmsg; |
| 512 | u_int len, level; |
| 513 | char *msg; |
| 514 | |
| 515 | buffer_init(&logmsg); |
| 516 | |
| 517 | /* Read length */ |
| 518 | buffer_append_space(&logmsg, 4); |
| 519 | if (atomicio(read, pmonitor->m_log_recvfd, |
| 520 | buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg)) { |
| 521 | if (errno == EPIPE) { |
Damien Miller | a2876db | 2012-02-11 08:16:06 +1100 | [diff] [blame] | 522 | buffer_free(&logmsg); |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 523 | debug("%s: child log fd closed", __func__); |
| 524 | close(pmonitor->m_log_recvfd); |
| 525 | pmonitor->m_log_recvfd = -1; |
| 526 | return -1; |
| 527 | } |
| 528 | fatal("%s: log fd read: %s", __func__, strerror(errno)); |
| 529 | } |
| 530 | len = buffer_get_int(&logmsg); |
| 531 | if (len <= 4 || len > 8192) |
| 532 | fatal("%s: invalid log message length %u", __func__, len); |
| 533 | |
| 534 | /* Read severity, message */ |
| 535 | buffer_clear(&logmsg); |
| 536 | buffer_append_space(&logmsg, len); |
| 537 | if (atomicio(read, pmonitor->m_log_recvfd, |
| 538 | buffer_ptr(&logmsg), buffer_len(&logmsg)) != buffer_len(&logmsg)) |
| 539 | fatal("%s: log fd read: %s", __func__, strerror(errno)); |
| 540 | |
| 541 | /* Log it */ |
| 542 | level = buffer_get_int(&logmsg); |
| 543 | msg = buffer_get_string(&logmsg, NULL); |
| 544 | if (log_level_name(level) == NULL) |
| 545 | fatal("%s: invalid log level %u (corrupted message?)", |
| 546 | __func__, level); |
| 547 | do_log2(level, "%s [preauth]", msg); |
| 548 | |
| 549 | buffer_free(&logmsg); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 550 | free(msg); |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 551 | |
| 552 | return 0; |
| 553 | } |
| 554 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 555 | int |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 556 | monitor_read(struct monitor *pmonitor, struct mon_table *ent, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 557 | struct mon_table **pent) |
| 558 | { |
| 559 | Buffer m; |
| 560 | int ret; |
| 561 | u_char type; |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 562 | struct pollfd pfd[2]; |
| 563 | |
| 564 | for (;;) { |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 565 | memset(&pfd, 0, sizeof(pfd)); |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 566 | pfd[0].fd = pmonitor->m_sendfd; |
| 567 | pfd[0].events = POLLIN; |
| 568 | pfd[1].fd = pmonitor->m_log_recvfd; |
| 569 | pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN; |
Damien Miller | 7741ce8 | 2011-08-06 06:15:15 +1000 | [diff] [blame] | 570 | if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) { |
| 571 | if (errno == EINTR || errno == EAGAIN) |
| 572 | continue; |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 573 | fatal("%s: poll: %s", __func__, strerror(errno)); |
Damien Miller | 7741ce8 | 2011-08-06 06:15:15 +1000 | [diff] [blame] | 574 | } |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 575 | if (pfd[1].revents) { |
| 576 | /* |
| 577 | * Drain all log messages before processing next |
| 578 | * monitor request. |
| 579 | */ |
| 580 | monitor_read_log(pmonitor); |
| 581 | continue; |
| 582 | } |
| 583 | if (pfd[0].revents) |
| 584 | break; /* Continues below */ |
| 585 | } |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 586 | |
| 587 | buffer_init(&m); |
| 588 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 589 | mm_request_receive(pmonitor->m_sendfd, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 590 | type = buffer_get_char(&m); |
| 591 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 592 | debug3("%s: checking request %d", __func__, type); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 593 | |
| 594 | while (ent->f != NULL) { |
| 595 | if (ent->type == type) |
| 596 | break; |
| 597 | ent++; |
| 598 | } |
| 599 | |
| 600 | if (ent->f != NULL) { |
| 601 | if (!(ent->flags & MON_PERMIT)) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 602 | fatal("%s: unpermitted request %d", __func__, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 603 | type); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 604 | ret = (*ent->f)(pmonitor->m_sendfd, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 605 | buffer_free(&m); |
| 606 | |
| 607 | /* The child may use this request only once, disable it */ |
| 608 | if (ent->flags & MON_ONCE) { |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 609 | debug2("%s: %d used once, disabling now", __func__, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 610 | type); |
| 611 | ent->flags &= ~MON_PERMIT; |
| 612 | } |
| 613 | |
| 614 | if (pent != NULL) |
| 615 | *pent = ent; |
| 616 | |
| 617 | return ret; |
| 618 | } |
| 619 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 620 | fatal("%s: unsupported request: %d", __func__, type); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 621 | |
| 622 | /* NOTREACHED */ |
| 623 | return (-1); |
| 624 | } |
| 625 | |
| 626 | /* allowed key state */ |
| 627 | static int |
| 628 | monitor_allowed_key(u_char *blob, u_int bloblen) |
| 629 | { |
| 630 | /* make sure key is allowed */ |
| 631 | if (key_blob == NULL || key_bloblen != bloblen || |
Damien Miller | ea1651c | 2010-07-16 13:58:37 +1000 | [diff] [blame] | 632 | timingsafe_bcmp(key_blob, blob, key_bloblen)) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 633 | return (0); |
| 634 | return (1); |
| 635 | } |
| 636 | |
| 637 | static void |
| 638 | monitor_reset_key_state(void) |
| 639 | { |
| 640 | /* reset state */ |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 641 | free(key_blob); |
| 642 | free(hostbased_cuser); |
| 643 | free(hostbased_chost); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 644 | key_blob = NULL; |
| 645 | key_bloblen = 0; |
| 646 | key_blobtype = MM_NOKEY; |
| 647 | hostbased_cuser = NULL; |
| 648 | hostbased_chost = NULL; |
| 649 | } |
| 650 | |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 651 | #ifdef WITH_OPENSSL |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 652 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 653 | mm_answer_moduli(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 654 | { |
| 655 | DH *dh; |
| 656 | int min, want, max; |
| 657 | |
| 658 | min = buffer_get_int(m); |
| 659 | want = buffer_get_int(m); |
| 660 | max = buffer_get_int(m); |
| 661 | |
| 662 | debug3("%s: got parameters: %d %d %d", |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 663 | __func__, min, want, max); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 664 | /* We need to check here, too, in case the child got corrupted */ |
| 665 | if (max < min || want < min || max < want) |
| 666 | fatal("%s: bad parameters: %d %d %d", |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 667 | __func__, min, want, max); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 668 | |
| 669 | buffer_clear(m); |
| 670 | |
| 671 | dh = choose_dh(min, want, max); |
| 672 | if (dh == NULL) { |
| 673 | buffer_put_char(m, 0); |
| 674 | return (0); |
| 675 | } else { |
| 676 | /* Send first bignum */ |
| 677 | buffer_put_char(m, 1); |
| 678 | buffer_put_bignum2(m, dh->p); |
| 679 | buffer_put_bignum2(m, dh->g); |
| 680 | |
| 681 | DH_free(dh); |
| 682 | } |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 683 | mm_request_send(sock, MONITOR_ANS_MODULI, m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 684 | return (0); |
| 685 | } |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 686 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 687 | |
Damien Miller | 85b45e0 | 2013-07-20 13:21:52 +1000 | [diff] [blame] | 688 | extern AuthenticationConnection *auth_conn; |
| 689 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 690 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 691 | mm_answer_sign(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 692 | { |
| 693 | Key *key; |
| 694 | u_char *p; |
| 695 | u_char *signature; |
| 696 | u_int siglen, datlen; |
| 697 | int keyid; |
| 698 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 699 | debug3("%s", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 700 | |
| 701 | keyid = buffer_get_int(m); |
| 702 | p = buffer_get_string(m, &datlen); |
| 703 | |
Damien Miller | a63128d | 2006-03-15 12:08:28 +1100 | [diff] [blame] | 704 | /* |
Damien Miller | 041ab7c | 2010-09-10 11:23:34 +1000 | [diff] [blame] | 705 | * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes), |
| 706 | * SHA384 (48 bytes) and SHA512 (64 bytes). |
Damien Miller | a63128d | 2006-03-15 12:08:28 +1100 | [diff] [blame] | 707 | */ |
Damien Miller | 041ab7c | 2010-09-10 11:23:34 +1000 | [diff] [blame] | 708 | if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64) |
Ben Lindstrom | d550218 | 2002-06-27 00:12:57 +0000 | [diff] [blame] | 709 | fatal("%s: data length incorrect: %u", __func__, datlen); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 710 | |
Ben Lindstrom | f67e077 | 2002-06-06 20:58:19 +0000 | [diff] [blame] | 711 | /* save session id, it will be passed on the first call */ |
| 712 | if (session_id2_len == 0) { |
| 713 | session_id2_len = datlen; |
| 714 | session_id2 = xmalloc(session_id2_len); |
| 715 | memcpy(session_id2, p, session_id2_len); |
| 716 | } |
| 717 | |
Damien Miller | 85b45e0 | 2013-07-20 13:21:52 +1000 | [diff] [blame] | 718 | if ((key = get_hostkey_by_index(keyid)) != NULL) { |
| 719 | if (key_sign(key, &signature, &siglen, p, datlen) < 0) |
| 720 | fatal("%s: key_sign failed", __func__); |
| 721 | } else if ((key = get_hostkey_public_by_index(keyid)) != NULL && |
| 722 | auth_conn != NULL) { |
| 723 | if (ssh_agent_sign(auth_conn, key, &signature, &siglen, p, |
| 724 | datlen) < 0) |
| 725 | fatal("%s: ssh_agent_sign failed", __func__); |
| 726 | } else |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 727 | fatal("%s: no hostkey from index %d", __func__, keyid); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 728 | |
Ben Lindstrom | d550218 | 2002-06-27 00:12:57 +0000 | [diff] [blame] | 729 | debug3("%s: signature %p(%u)", __func__, signature, siglen); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 730 | |
| 731 | buffer_clear(m); |
| 732 | buffer_put_string(m, signature, siglen); |
| 733 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 734 | free(p); |
| 735 | free(signature); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 736 | |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 737 | mm_request_send(sock, MONITOR_ANS_SIGN, m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 738 | |
| 739 | /* Turn on permissions for getpwnam */ |
| 740 | monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1); |
| 741 | |
| 742 | return (0); |
| 743 | } |
| 744 | |
| 745 | /* Retrieves the password entry and also checks if the user is permitted */ |
| 746 | |
| 747 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 748 | mm_answer_pwnamallow(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 749 | { |
Darren Tucker | b09b677 | 2004-06-22 15:06:46 +1000 | [diff] [blame] | 750 | char *username; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 751 | struct passwd *pwent; |
| 752 | int allowed = 0; |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 753 | u_int i; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 754 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 755 | debug3("%s", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 756 | |
| 757 | if (authctxt->attempt++ != 0) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 758 | fatal("%s: multiple attempts for getpwnam", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 759 | |
Darren Tucker | b09b677 | 2004-06-22 15:06:46 +1000 | [diff] [blame] | 760 | username = buffer_get_string(m, NULL); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 761 | |
Darren Tucker | b09b677 | 2004-06-22 15:06:46 +1000 | [diff] [blame] | 762 | pwent = getpwnamallow(username); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 763 | |
Darren Tucker | b09b677 | 2004-06-22 15:06:46 +1000 | [diff] [blame] | 764 | authctxt->user = xstrdup(username); |
| 765 | setproctitle("%s [priv]", pwent ? username : "unknown"); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 766 | free(username); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 767 | |
| 768 | buffer_clear(m); |
| 769 | |
| 770 | if (pwent == NULL) { |
| 771 | buffer_put_char(m, 0); |
Damien Miller | f96d183 | 2003-11-18 22:01:48 +1100 | [diff] [blame] | 772 | authctxt->pw = fakepw(); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 773 | goto out; |
| 774 | } |
| 775 | |
| 776 | allowed = 1; |
| 777 | authctxt->pw = pwent; |
| 778 | authctxt->valid = 1; |
| 779 | |
| 780 | buffer_put_char(m, 1); |
| 781 | buffer_put_string(m, pwent, sizeof(struct passwd)); |
| 782 | buffer_put_cstring(m, pwent->pw_name); |
| 783 | buffer_put_cstring(m, "*"); |
Damien Miller | 6332da2 | 2013-04-23 14:25:52 +1000 | [diff] [blame] | 784 | #ifdef HAVE_STRUCT_PASSWD_PW_GECOS |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 785 | buffer_put_cstring(m, pwent->pw_gecos); |
Damien Miller | 6332da2 | 2013-04-23 14:25:52 +1000 | [diff] [blame] | 786 | #endif |
| 787 | #ifdef HAVE_STRUCT_PASSWD_PW_CLASS |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 788 | buffer_put_cstring(m, pwent->pw_class); |
Kevin Steves | 7e14760 | 2002-03-22 18:07:17 +0000 | [diff] [blame] | 789 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 790 | buffer_put_cstring(m, pwent->pw_dir); |
| 791 | buffer_put_cstring(m, pwent->pw_shell); |
Darren Tucker | 2f8b3d9 | 2007-12-02 23:02:15 +1100 | [diff] [blame] | 792 | |
| 793 | out: |
Darren Tucker | 1629c07 | 2007-02-19 22:25:37 +1100 | [diff] [blame] | 794 | buffer_put_string(m, &options, sizeof(options)); |
Damien Miller | f2e407e | 2011-05-20 19:04:14 +1000 | [diff] [blame] | 795 | |
| 796 | #define M_CP_STROPT(x) do { \ |
| 797 | if (options.x != NULL) \ |
| 798 | buffer_put_cstring(m, options.x); \ |
| 799 | } while (0) |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 800 | #define M_CP_STRARRAYOPT(x, nx) do { \ |
| 801 | for (i = 0; i < options.nx; i++) \ |
| 802 | buffer_put_cstring(m, options.x[i]); \ |
| 803 | } while (0) |
Damien Miller | f2e407e | 2011-05-20 19:04:14 +1000 | [diff] [blame] | 804 | /* See comment in servconf.h */ |
| 805 | COPY_MATCH_STRING_OPTS(); |
| 806 | #undef M_CP_STROPT |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 807 | #undef M_CP_STRARRAYOPT |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 808 | |
| 809 | /* Create valid auth method lists */ |
| 810 | if (compat20 && auth2_setup_methods_lists(authctxt) != 0) { |
| 811 | /* |
| 812 | * The monitor will continue long enough to let the child |
| 813 | * run to it's packet_disconnect(), but it must not allow any |
| 814 | * authentication to succeed. |
| 815 | */ |
| 816 | debug("%s: no valid authentication method lists", __func__); |
| 817 | } |
| 818 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 819 | debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 820 | mm_request_send(sock, MONITOR_ANS_PWNAM, m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 821 | |
| 822 | /* For SSHv1 allow authentication now */ |
| 823 | if (!compat20) |
| 824 | monitor_permit_authentications(1); |
Damien Miller | 5ad9fd9 | 2002-05-13 11:07:41 +1000 | [diff] [blame] | 825 | else { |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 826 | /* Allow service/style information on the auth context */ |
| 827 | monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1); |
Damien Miller | 5ad9fd9 | 2002-05-13 11:07:41 +1000 | [diff] [blame] | 828 | monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1); |
| 829 | } |
Damien Miller | a33501b | 2002-05-08 12:24:42 +1000 | [diff] [blame] | 830 | #ifdef USE_PAM |
Damien Miller | 4e448a3 | 2003-05-14 15:11:48 +1000 | [diff] [blame] | 831 | if (options.use_pam) |
| 832 | monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1); |
Damien Miller | a33501b | 2002-05-08 12:24:42 +1000 | [diff] [blame] | 833 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 834 | |
| 835 | return (0); |
| 836 | } |
| 837 | |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 838 | int mm_answer_auth2_read_banner(int sock, Buffer *m) |
Damien Miller | 5ad9fd9 | 2002-05-13 11:07:41 +1000 | [diff] [blame] | 839 | { |
| 840 | char *banner; |
| 841 | |
| 842 | buffer_clear(m); |
| 843 | banner = auth2_read_banner(); |
| 844 | buffer_put_cstring(m, banner != NULL ? banner : ""); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 845 | mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 846 | free(banner); |
Damien Miller | 5ad9fd9 | 2002-05-13 11:07:41 +1000 | [diff] [blame] | 847 | |
| 848 | return (0); |
| 849 | } |
| 850 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 851 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 852 | mm_answer_authserv(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 853 | { |
| 854 | monitor_permit_authentications(1); |
| 855 | |
| 856 | authctxt->service = buffer_get_string(m, NULL); |
| 857 | authctxt->style = buffer_get_string(m, NULL); |
| 858 | debug3("%s: service=%s, style=%s", |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 859 | __func__, authctxt->service, authctxt->style); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 860 | |
| 861 | if (strlen(authctxt->style) == 0) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 862 | free(authctxt->style); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 863 | authctxt->style = NULL; |
| 864 | } |
| 865 | |
| 866 | return (0); |
| 867 | } |
| 868 | |
| 869 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 870 | mm_answer_authpassword(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 871 | { |
| 872 | static int call_count; |
| 873 | char *passwd; |
Ben Lindstrom | 7cea16b | 2002-07-23 21:13:40 +0000 | [diff] [blame] | 874 | int authenticated; |
| 875 | u_int plen; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 876 | |
| 877 | passwd = buffer_get_string(m, &plen); |
| 878 | /* Only authenticate if the context is valid */ |
Ben Lindstrom | dcf6bfb | 2002-06-06 20:57:17 +0000 | [diff] [blame] | 879 | authenticated = options.password_authentication && |
Damien Miller | 856f0be | 2003-09-03 07:32:45 +1000 | [diff] [blame] | 880 | auth_password(authctxt, passwd); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 881 | explicit_bzero(passwd, strlen(passwd)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 882 | free(passwd); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 883 | |
| 884 | buffer_clear(m); |
| 885 | buffer_put_int(m, authenticated); |
| 886 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 887 | debug3("%s: sending result %d", __func__, authenticated); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 888 | mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 889 | |
| 890 | call_count++; |
| 891 | if (plen == 0 && call_count == 1) |
| 892 | auth_method = "none"; |
| 893 | else |
| 894 | auth_method = "password"; |
| 895 | |
| 896 | /* Causes monitor loop to terminate if authenticated */ |
| 897 | return (authenticated); |
| 898 | } |
| 899 | |
| 900 | #ifdef BSD_AUTH |
| 901 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 902 | mm_answer_bsdauthquery(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 903 | { |
| 904 | char *name, *infotxt; |
| 905 | u_int numprompts; |
| 906 | u_int *echo_on; |
| 907 | char **prompts; |
Damien Miller | b7df3af | 2003-02-24 11:55:46 +1100 | [diff] [blame] | 908 | u_int success; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 909 | |
Damien Miller | b7df3af | 2003-02-24 11:55:46 +1100 | [diff] [blame] | 910 | success = bsdauth_query(authctxt, &name, &infotxt, &numprompts, |
| 911 | &prompts, &echo_on) < 0 ? 0 : 1; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 912 | |
| 913 | buffer_clear(m); |
Damien Miller | b7df3af | 2003-02-24 11:55:46 +1100 | [diff] [blame] | 914 | buffer_put_int(m, success); |
| 915 | if (success) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 916 | buffer_put_cstring(m, prompts[0]); |
| 917 | |
Damien Miller | b7df3af | 2003-02-24 11:55:46 +1100 | [diff] [blame] | 918 | debug3("%s: sending challenge success: %u", __func__, success); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 919 | mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 920 | |
Damien Miller | b7df3af | 2003-02-24 11:55:46 +1100 | [diff] [blame] | 921 | if (success) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 922 | free(name); |
| 923 | free(infotxt); |
| 924 | free(prompts); |
| 925 | free(echo_on); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | return (0); |
| 929 | } |
| 930 | |
| 931 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 932 | mm_answer_bsdauthrespond(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 933 | { |
| 934 | char *response; |
| 935 | int authok; |
| 936 | |
| 937 | if (authctxt->as == 0) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 938 | fatal("%s: no bsd auth session", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 939 | |
| 940 | response = buffer_get_string(m, NULL); |
Ben Lindstrom | dcf6bfb | 2002-06-06 20:57:17 +0000 | [diff] [blame] | 941 | authok = options.challenge_response_authentication && |
| 942 | auth_userresponse(authctxt->as, response, 0); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 943 | authctxt->as = NULL; |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 944 | debug3("%s: <%s> = <%d>", __func__, response, authok); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 945 | free(response); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 946 | |
| 947 | buffer_clear(m); |
| 948 | buffer_put_int(m, authok); |
| 949 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 950 | debug3("%s: sending authenticated: %d", __func__, authok); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 951 | mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 952 | |
Damien Miller | 91a55f2 | 2013-04-23 15:18:10 +1000 | [diff] [blame] | 953 | if (compat20) { |
| 954 | auth_method = "keyboard-interactive"; |
| 955 | auth_submethod = "bsdauth"; |
| 956 | } else |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 957 | auth_method = "bsdauth"; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 958 | |
| 959 | return (authok != 0); |
| 960 | } |
| 961 | #endif |
| 962 | |
| 963 | #ifdef SKEY |
| 964 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 965 | mm_answer_skeyquery(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 966 | { |
| 967 | struct skey skey; |
| 968 | char challenge[1024]; |
Damien Miller | b7df3af | 2003-02-24 11:55:46 +1100 | [diff] [blame] | 969 | u_int success; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 970 | |
Darren Tucker | 06a8cfe | 2004-04-14 17:24:30 +1000 | [diff] [blame] | 971 | success = _compat_skeychallenge(&skey, authctxt->user, challenge, |
| 972 | sizeof(challenge)) < 0 ? 0 : 1; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 973 | |
| 974 | buffer_clear(m); |
Damien Miller | b7df3af | 2003-02-24 11:55:46 +1100 | [diff] [blame] | 975 | buffer_put_int(m, success); |
| 976 | if (success) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 977 | buffer_put_cstring(m, challenge); |
| 978 | |
Damien Miller | b7df3af | 2003-02-24 11:55:46 +1100 | [diff] [blame] | 979 | debug3("%s: sending challenge success: %u", __func__, success); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 980 | mm_request_send(sock, MONITOR_ANS_SKEYQUERY, m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 981 | |
| 982 | return (0); |
| 983 | } |
| 984 | |
| 985 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 986 | mm_answer_skeyrespond(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 987 | { |
| 988 | char *response; |
| 989 | int authok; |
| 990 | |
| 991 | response = buffer_get_string(m, NULL); |
| 992 | |
Ben Lindstrom | dcf6bfb | 2002-06-06 20:57:17 +0000 | [diff] [blame] | 993 | authok = (options.challenge_response_authentication && |
| 994 | authctxt->valid && |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 995 | skey_haskey(authctxt->pw->pw_name) == 0 && |
| 996 | skey_passcheck(authctxt->pw->pw_name, response) != -1); |
| 997 | |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 998 | free(response); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 999 | |
| 1000 | buffer_clear(m); |
| 1001 | buffer_put_int(m, authok); |
| 1002 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1003 | debug3("%s: sending authenticated: %d", __func__, authok); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1004 | mm_request_send(sock, MONITOR_ANS_SKEYRESPOND, m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1005 | |
| 1006 | auth_method = "skey"; |
| 1007 | |
| 1008 | return (authok != 0); |
| 1009 | } |
| 1010 | #endif |
| 1011 | |
Damien Miller | 7941855 | 2002-04-23 20:28:48 +1000 | [diff] [blame] | 1012 | #ifdef USE_PAM |
| 1013 | int |
Darren Tucker | 7eda592 | 2004-06-22 13:22:50 +1000 | [diff] [blame] | 1014 | mm_answer_pam_start(int sock, Buffer *m) |
Damien Miller | 7941855 | 2002-04-23 20:28:48 +1000 | [diff] [blame] | 1015 | { |
Damien Miller | 4e448a3 | 2003-05-14 15:11:48 +1000 | [diff] [blame] | 1016 | if (!options.use_pam) |
| 1017 | fatal("UsePAM not set, but ended up in %s anyway", __func__); |
| 1018 | |
Darren Tucker | dbf7a74 | 2004-03-08 23:04:06 +1100 | [diff] [blame] | 1019 | start_pam(authctxt); |
Damien Miller | 7941855 | 2002-04-23 20:28:48 +1000 | [diff] [blame] | 1020 | |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 1021 | monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1); |
| 1022 | |
Damien Miller | 7941855 | 2002-04-23 20:28:48 +1000 | [diff] [blame] | 1023 | return (0); |
| 1024 | } |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1025 | |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 1026 | int |
Darren Tucker | 7eda592 | 2004-06-22 13:22:50 +1000 | [diff] [blame] | 1027 | mm_answer_pam_account(int sock, Buffer *m) |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 1028 | { |
| 1029 | u_int ret; |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 1030 | |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 1031 | if (!options.use_pam) |
| 1032 | fatal("UsePAM not set, but ended up in %s anyway", __func__); |
| 1033 | |
| 1034 | ret = do_pam_account(); |
| 1035 | |
| 1036 | buffer_put_int(m, ret); |
Darren Tucker | d4f04ae | 2005-09-30 10:23:21 +1000 | [diff] [blame] | 1037 | buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg)); |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 1038 | |
Darren Tucker | 7eda592 | 2004-06-22 13:22:50 +1000 | [diff] [blame] | 1039 | mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m); |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 1040 | |
| 1041 | return (ret); |
| 1042 | } |
| 1043 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1044 | static void *sshpam_ctxt, *sshpam_authok; |
| 1045 | extern KbdintDevice sshpam_device; |
| 1046 | |
| 1047 | int |
Darren Tucker | 7eda592 | 2004-06-22 13:22:50 +1000 | [diff] [blame] | 1048 | mm_answer_pam_init_ctx(int sock, Buffer *m) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1049 | { |
| 1050 | |
| 1051 | debug3("%s", __func__); |
| 1052 | authctxt->user = buffer_get_string(m, NULL); |
| 1053 | sshpam_ctxt = (sshpam_device.init_ctx)(authctxt); |
| 1054 | sshpam_authok = NULL; |
| 1055 | buffer_clear(m); |
| 1056 | if (sshpam_ctxt != NULL) { |
| 1057 | monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1); |
| 1058 | buffer_put_int(m, 1); |
| 1059 | } else { |
| 1060 | buffer_put_int(m, 0); |
| 1061 | } |
Darren Tucker | 7eda592 | 2004-06-22 13:22:50 +1000 | [diff] [blame] | 1062 | mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1063 | return (0); |
| 1064 | } |
| 1065 | |
| 1066 | int |
Darren Tucker | 7eda592 | 2004-06-22 13:22:50 +1000 | [diff] [blame] | 1067 | mm_answer_pam_query(int sock, Buffer *m) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1068 | { |
Darren Tucker | 8b7a055 | 2010-08-03 15:50:16 +1000 | [diff] [blame] | 1069 | char *name = NULL, *info = NULL, **prompts = NULL; |
| 1070 | u_int i, num = 0, *echo_on = 0; |
Damien Miller | 04b6533 | 2005-07-17 17:53:31 +1000 | [diff] [blame] | 1071 | int ret; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1072 | |
| 1073 | debug3("%s", __func__); |
| 1074 | sshpam_authok = NULL; |
| 1075 | ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on); |
| 1076 | if (ret == 0 && num == 0) |
| 1077 | sshpam_authok = sshpam_ctxt; |
| 1078 | if (num > 1 || name == NULL || info == NULL) |
| 1079 | ret = -1; |
| 1080 | buffer_clear(m); |
| 1081 | buffer_put_int(m, ret); |
| 1082 | buffer_put_cstring(m, name); |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 1083 | free(name); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1084 | buffer_put_cstring(m, info); |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 1085 | free(info); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1086 | buffer_put_int(m, num); |
| 1087 | for (i = 0; i < num; ++i) { |
| 1088 | buffer_put_cstring(m, prompts[i]); |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 1089 | free(prompts[i]); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1090 | buffer_put_int(m, echo_on[i]); |
| 1091 | } |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 1092 | free(prompts); |
| 1093 | free(echo_on); |
Damien Miller | 15b05cf | 2012-12-03 09:53:20 +1100 | [diff] [blame] | 1094 | auth_method = "keyboard-interactive"; |
| 1095 | auth_submethod = "pam"; |
Darren Tucker | 7eda592 | 2004-06-22 13:22:50 +1000 | [diff] [blame] | 1096 | mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1097 | return (0); |
| 1098 | } |
| 1099 | |
| 1100 | int |
Darren Tucker | 7eda592 | 2004-06-22 13:22:50 +1000 | [diff] [blame] | 1101 | mm_answer_pam_respond(int sock, Buffer *m) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1102 | { |
| 1103 | char **resp; |
Damien Miller | 04b6533 | 2005-07-17 17:53:31 +1000 | [diff] [blame] | 1104 | u_int i, num; |
| 1105 | int ret; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1106 | |
| 1107 | debug3("%s", __func__); |
| 1108 | sshpam_authok = NULL; |
| 1109 | num = buffer_get_int(m); |
| 1110 | if (num > 0) { |
Darren Tucker | d8093e4 | 2006-05-04 16:24:34 +1000 | [diff] [blame] | 1111 | resp = xcalloc(num, sizeof(char *)); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1112 | for (i = 0; i < num; ++i) |
| 1113 | resp[i] = buffer_get_string(m, NULL); |
| 1114 | ret = (sshpam_device.respond)(sshpam_ctxt, num, resp); |
| 1115 | for (i = 0; i < num; ++i) |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 1116 | free(resp[i]); |
| 1117 | free(resp); |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1118 | } else { |
| 1119 | ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL); |
| 1120 | } |
| 1121 | buffer_clear(m); |
| 1122 | buffer_put_int(m, ret); |
Darren Tucker | 7eda592 | 2004-06-22 13:22:50 +1000 | [diff] [blame] | 1123 | mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m); |
Damien Miller | 15b05cf | 2012-12-03 09:53:20 +1100 | [diff] [blame] | 1124 | auth_method = "keyboard-interactive"; |
| 1125 | auth_submethod = "pam"; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1126 | if (ret == 0) |
| 1127 | sshpam_authok = sshpam_ctxt; |
| 1128 | return (0); |
| 1129 | } |
| 1130 | |
| 1131 | int |
Darren Tucker | 7eda592 | 2004-06-22 13:22:50 +1000 | [diff] [blame] | 1132 | mm_answer_pam_free_ctx(int sock, Buffer *m) |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1133 | { |
| 1134 | |
| 1135 | debug3("%s", __func__); |
| 1136 | (sshpam_device.free_ctx)(sshpam_ctxt); |
| 1137 | buffer_clear(m); |
Darren Tucker | 7eda592 | 2004-06-22 13:22:50 +1000 | [diff] [blame] | 1138 | mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m); |
Damien Miller | 15b05cf | 2012-12-03 09:53:20 +1100 | [diff] [blame] | 1139 | auth_method = "keyboard-interactive"; |
| 1140 | auth_submethod = "pam"; |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 1141 | return (sshpam_authok == sshpam_ctxt); |
| 1142 | } |
Damien Miller | 7941855 | 2002-04-23 20:28:48 +1000 | [diff] [blame] | 1143 | #endif |
| 1144 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1145 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1146 | mm_answer_keyallowed(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1147 | { |
| 1148 | Key *key; |
Damien Miller | a10f561 | 2002-09-12 09:49:15 +1000 | [diff] [blame] | 1149 | char *cuser, *chost; |
| 1150 | u_char *blob; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1151 | u_int bloblen; |
| 1152 | enum mm_keytype type = 0; |
| 1153 | int allowed = 0; |
| 1154 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1155 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1156 | |
| 1157 | type = buffer_get_int(m); |
| 1158 | cuser = buffer_get_string(m, NULL); |
| 1159 | chost = buffer_get_string(m, NULL); |
| 1160 | blob = buffer_get_string(m, &bloblen); |
| 1161 | |
| 1162 | key = key_from_blob(blob, bloblen); |
| 1163 | |
| 1164 | if ((compat20 && type == MM_RSAHOSTKEY) || |
| 1165 | (!compat20 && type != MM_RSAHOSTKEY)) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1166 | fatal("%s: key type and protocol mismatch", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1167 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1168 | debug3("%s: key_from_blob: %p", __func__, key); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1169 | |
Damien Miller | 3e3b514 | 2003-11-17 21:13:40 +1100 | [diff] [blame] | 1170 | if (key != NULL && authctxt->valid) { |
djm@openbsd.org | 1f729f0 | 2015-01-13 07:39:19 +0000 | [diff] [blame] | 1171 | /* These should not make it past the privsep child */ |
| 1172 | if (key_type_plain(key->type) == KEY_RSA && |
| 1173 | (datafellows & SSH_BUG_RSASIGMD5) != 0) |
| 1174 | fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__); |
| 1175 | |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 1176 | switch (type) { |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1177 | case MM_USERKEY: |
Ben Lindstrom | dcf6bfb | 2002-06-06 20:57:17 +0000 | [diff] [blame] | 1178 | allowed = options.pubkey_authentication && |
djm@openbsd.org | f69b69b | 2014-12-22 07:51:30 +0000 | [diff] [blame] | 1179 | !auth2_userkey_already_used(authctxt, key) && |
djm@openbsd.org | 1f729f0 | 2015-01-13 07:39:19 +0000 | [diff] [blame] | 1180 | match_pattern_list(sshkey_ssh_name(key), |
| 1181 | options.pubkey_key_types, |
| 1182 | strlen(options.pubkey_key_types), 0) == 1 && |
Ben Lindstrom | dcf6bfb | 2002-06-06 20:57:17 +0000 | [diff] [blame] | 1183 | user_key_allowed(authctxt->pw, key); |
Damien Miller | 20bdcd7 | 2013-07-18 16:10:09 +1000 | [diff] [blame] | 1184 | pubkey_auth_info(authctxt, key, NULL); |
Damien Miller | 7a8f5b3 | 2006-03-31 23:14:23 +1100 | [diff] [blame] | 1185 | auth_method = "publickey"; |
Darren Tucker | f2c16d3 | 2008-06-14 08:59:49 +1000 | [diff] [blame] | 1186 | if (options.pubkey_authentication && allowed != 1) |
| 1187 | auth_clear_options(); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1188 | break; |
| 1189 | case MM_HOSTKEY: |
Ben Lindstrom | dcf6bfb | 2002-06-06 20:57:17 +0000 | [diff] [blame] | 1190 | allowed = options.hostbased_authentication && |
djm@openbsd.org | 1f729f0 | 2015-01-13 07:39:19 +0000 | [diff] [blame] | 1191 | match_pattern_list(sshkey_ssh_name(key), |
| 1192 | options.hostbased_key_types, |
| 1193 | strlen(options.hostbased_key_types), 0) == 1 && |
Ben Lindstrom | dcf6bfb | 2002-06-06 20:57:17 +0000 | [diff] [blame] | 1194 | hostbased_key_allowed(authctxt->pw, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1195 | cuser, chost, key); |
Damien Miller | 20bdcd7 | 2013-07-18 16:10:09 +1000 | [diff] [blame] | 1196 | pubkey_auth_info(authctxt, key, |
| 1197 | "client user \"%.100s\", client host \"%.100s\"", |
| 1198 | cuser, chost); |
Damien Miller | 7a8f5b3 | 2006-03-31 23:14:23 +1100 | [diff] [blame] | 1199 | auth_method = "hostbased"; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1200 | break; |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 1201 | #ifdef WITH_SSH1 |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1202 | case MM_RSAHOSTKEY: |
| 1203 | key->type = KEY_RSA1; /* XXX */ |
Ben Lindstrom | dcf6bfb | 2002-06-06 20:57:17 +0000 | [diff] [blame] | 1204 | allowed = options.rhosts_rsa_authentication && |
| 1205 | auth_rhosts_rsa_key_allowed(authctxt->pw, |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1206 | cuser, chost, key); |
Darren Tucker | f2c16d3 | 2008-06-14 08:59:49 +1000 | [diff] [blame] | 1207 | if (options.rhosts_rsa_authentication && allowed != 1) |
| 1208 | auth_clear_options(); |
Damien Miller | 7a8f5b3 | 2006-03-31 23:14:23 +1100 | [diff] [blame] | 1209 | auth_method = "rsa"; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1210 | break; |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 1211 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1212 | default: |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1213 | fatal("%s: unknown key type %d", __func__, type); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1214 | break; |
| 1215 | } |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1216 | } |
Damien Miller | 0011138 | 2003-03-10 11:21:17 +1100 | [diff] [blame] | 1217 | if (key != NULL) |
| 1218 | key_free(key); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1219 | |
| 1220 | /* clear temporarily storage (used by verify) */ |
| 1221 | monitor_reset_key_state(); |
| 1222 | |
| 1223 | if (allowed) { |
| 1224 | /* Save temporarily for comparison in verify */ |
| 1225 | key_blob = blob; |
| 1226 | key_bloblen = bloblen; |
| 1227 | key_blobtype = type; |
| 1228 | hostbased_cuser = cuser; |
| 1229 | hostbased_chost = chost; |
Damien Miller | 96937bd | 2006-03-26 14:01:54 +1100 | [diff] [blame] | 1230 | } else { |
Damien Miller | 7a8f5b3 | 2006-03-31 23:14:23 +1100 | [diff] [blame] | 1231 | /* Log failed attempt */ |
Darren Tucker | 0acca37 | 2013-06-02 07:41:51 +1000 | [diff] [blame] | 1232 | auth_log(authctxt, 0, 0, auth_method, NULL); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1233 | free(blob); |
| 1234 | free(cuser); |
| 1235 | free(chost); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | debug3("%s: key %p is %s", |
Darren Tucker | 2784f1f | 2008-07-04 13:51:45 +1000 | [diff] [blame] | 1239 | __func__, key, allowed ? "allowed" : "not allowed"); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1240 | |
| 1241 | buffer_clear(m); |
| 1242 | buffer_put_int(m, allowed); |
Damien Miller | 06ebedf | 2003-02-24 12:03:38 +1100 | [diff] [blame] | 1243 | buffer_put_int(m, forced_command != NULL); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1244 | |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1245 | mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1246 | |
| 1247 | if (type == MM_RSAHOSTKEY) |
| 1248 | monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed); |
| 1249 | |
| 1250 | return (0); |
| 1251 | } |
| 1252 | |
| 1253 | static int |
| 1254 | monitor_valid_userblob(u_char *data, u_int datalen) |
| 1255 | { |
| 1256 | Buffer b; |
Damien Miller | 4ce189d | 2013-04-23 15:17:52 +1000 | [diff] [blame] | 1257 | char *p, *userstyle; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1258 | u_int len; |
| 1259 | int fail = 0; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1260 | |
| 1261 | buffer_init(&b); |
| 1262 | buffer_append(&b, data, datalen); |
| 1263 | |
| 1264 | if (datafellows & SSH_OLD_SESSIONID) { |
Ben Lindstrom | f67e077 | 2002-06-06 20:58:19 +0000 | [diff] [blame] | 1265 | p = buffer_ptr(&b); |
| 1266 | len = buffer_len(&b); |
| 1267 | if ((session_id2 == NULL) || |
| 1268 | (len < session_id2_len) || |
Damien Miller | ea1651c | 2010-07-16 13:58:37 +1000 | [diff] [blame] | 1269 | (timingsafe_bcmp(p, session_id2, session_id2_len) != 0)) |
Ben Lindstrom | f67e077 | 2002-06-06 20:58:19 +0000 | [diff] [blame] | 1270 | fail++; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1271 | buffer_consume(&b, session_id2_len); |
| 1272 | } else { |
Ben Lindstrom | f67e077 | 2002-06-06 20:58:19 +0000 | [diff] [blame] | 1273 | p = buffer_get_string(&b, &len); |
| 1274 | if ((session_id2 == NULL) || |
| 1275 | (len != session_id2_len) || |
Damien Miller | ea1651c | 2010-07-16 13:58:37 +1000 | [diff] [blame] | 1276 | (timingsafe_bcmp(p, session_id2, session_id2_len) != 0)) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1277 | fail++; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1278 | free(p); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1279 | } |
| 1280 | if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST) |
| 1281 | fail++; |
Damien Miller | 4ce189d | 2013-04-23 15:17:52 +1000 | [diff] [blame] | 1282 | p = buffer_get_cstring(&b, NULL); |
| 1283 | xasprintf(&userstyle, "%s%s%s", authctxt->user, |
| 1284 | authctxt->style ? ":" : "", |
| 1285 | authctxt->style ? authctxt->style : ""); |
| 1286 | if (strcmp(userstyle, p) != 0) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1287 | logit("wrong user name passed to monitor: expected %s != %.100s", |
Damien Miller | 4ce189d | 2013-04-23 15:17:52 +1000 | [diff] [blame] | 1288 | userstyle, p); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1289 | fail++; |
| 1290 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1291 | free(userstyle); |
| 1292 | free(p); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1293 | buffer_skip_string(&b); |
| 1294 | if (datafellows & SSH_BUG_PKAUTH) { |
| 1295 | if (!buffer_get_char(&b)) |
| 1296 | fail++; |
| 1297 | } else { |
Damien Miller | 4ce189d | 2013-04-23 15:17:52 +1000 | [diff] [blame] | 1298 | p = buffer_get_cstring(&b, NULL); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1299 | if (strcmp("publickey", p) != 0) |
| 1300 | fail++; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1301 | free(p); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1302 | if (!buffer_get_char(&b)) |
| 1303 | fail++; |
| 1304 | buffer_skip_string(&b); |
| 1305 | } |
| 1306 | buffer_skip_string(&b); |
| 1307 | if (buffer_len(&b) != 0) |
| 1308 | fail++; |
| 1309 | buffer_free(&b); |
| 1310 | return (fail == 0); |
| 1311 | } |
| 1312 | |
| 1313 | static int |
Damien Miller | a10f561 | 2002-09-12 09:49:15 +1000 | [diff] [blame] | 1314 | monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser, |
| 1315 | char *chost) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1316 | { |
| 1317 | Buffer b; |
Damien Miller | 4ce189d | 2013-04-23 15:17:52 +1000 | [diff] [blame] | 1318 | char *p, *userstyle; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1319 | u_int len; |
| 1320 | int fail = 0; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1321 | |
| 1322 | buffer_init(&b); |
| 1323 | buffer_append(&b, data, datalen); |
| 1324 | |
Ben Lindstrom | f67e077 | 2002-06-06 20:58:19 +0000 | [diff] [blame] | 1325 | p = buffer_get_string(&b, &len); |
| 1326 | if ((session_id2 == NULL) || |
| 1327 | (len != session_id2_len) || |
Damien Miller | ea1651c | 2010-07-16 13:58:37 +1000 | [diff] [blame] | 1328 | (timingsafe_bcmp(p, session_id2, session_id2_len) != 0)) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1329 | fail++; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1330 | free(p); |
Ben Lindstrom | f67e077 | 2002-06-06 20:58:19 +0000 | [diff] [blame] | 1331 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1332 | if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST) |
| 1333 | fail++; |
Damien Miller | 4ce189d | 2013-04-23 15:17:52 +1000 | [diff] [blame] | 1334 | p = buffer_get_cstring(&b, NULL); |
| 1335 | xasprintf(&userstyle, "%s%s%s", authctxt->user, |
| 1336 | authctxt->style ? ":" : "", |
| 1337 | authctxt->style ? authctxt->style : ""); |
| 1338 | if (strcmp(userstyle, p) != 0) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 1339 | logit("wrong user name passed to monitor: expected %s != %.100s", |
Damien Miller | 4ce189d | 2013-04-23 15:17:52 +1000 | [diff] [blame] | 1340 | userstyle, p); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1341 | fail++; |
| 1342 | } |
Damien Miller | 4ce189d | 2013-04-23 15:17:52 +1000 | [diff] [blame] | 1343 | free(userstyle); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1344 | free(p); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1345 | buffer_skip_string(&b); /* service */ |
Damien Miller | 4ce189d | 2013-04-23 15:17:52 +1000 | [diff] [blame] | 1346 | p = buffer_get_cstring(&b, NULL); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1347 | if (strcmp(p, "hostbased") != 0) |
| 1348 | fail++; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1349 | free(p); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1350 | buffer_skip_string(&b); /* pkalg */ |
| 1351 | buffer_skip_string(&b); /* pkblob */ |
| 1352 | |
| 1353 | /* verify client host, strip trailing dot if necessary */ |
| 1354 | p = buffer_get_string(&b, NULL); |
| 1355 | if (((len = strlen(p)) > 0) && p[len - 1] == '.') |
| 1356 | p[len - 1] = '\0'; |
| 1357 | if (strcmp(p, chost) != 0) |
| 1358 | fail++; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1359 | free(p); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1360 | |
| 1361 | /* verify client user */ |
| 1362 | p = buffer_get_string(&b, NULL); |
| 1363 | if (strcmp(p, cuser) != 0) |
| 1364 | fail++; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1365 | free(p); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1366 | |
| 1367 | if (buffer_len(&b) != 0) |
| 1368 | fail++; |
| 1369 | buffer_free(&b); |
| 1370 | return (fail == 0); |
| 1371 | } |
| 1372 | |
| 1373 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1374 | mm_answer_keyverify(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1375 | { |
| 1376 | Key *key; |
| 1377 | u_char *signature, *data, *blob; |
| 1378 | u_int signaturelen, datalen, bloblen; |
| 1379 | int verified = 0; |
| 1380 | int valid_data = 0; |
| 1381 | |
| 1382 | blob = buffer_get_string(m, &bloblen); |
| 1383 | signature = buffer_get_string(m, &signaturelen); |
| 1384 | data = buffer_get_string(m, &datalen); |
| 1385 | |
| 1386 | if (hostbased_cuser == NULL || hostbased_chost == NULL || |
Ben Lindstrom | b57a4bf | 2002-03-27 18:00:59 +0000 | [diff] [blame] | 1387 | !monitor_allowed_key(blob, bloblen)) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1388 | fatal("%s: bad key, not previously allowed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1389 | |
| 1390 | key = key_from_blob(blob, bloblen); |
| 1391 | if (key == NULL) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1392 | fatal("%s: bad public key blob", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1393 | |
| 1394 | switch (key_blobtype) { |
| 1395 | case MM_USERKEY: |
| 1396 | valid_data = monitor_valid_userblob(data, datalen); |
| 1397 | break; |
| 1398 | case MM_HOSTKEY: |
| 1399 | valid_data = monitor_valid_hostbasedblob(data, datalen, |
| 1400 | hostbased_cuser, hostbased_chost); |
| 1401 | break; |
| 1402 | default: |
| 1403 | valid_data = 0; |
| 1404 | break; |
| 1405 | } |
| 1406 | if (!valid_data) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1407 | fatal("%s: bad signature data blob", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1408 | |
| 1409 | verified = key_verify(key, signature, signaturelen, data, datalen); |
| 1410 | debug3("%s: key %p signature %s", |
Darren Tucker | fbba735 | 2006-11-07 23:16:08 +1100 | [diff] [blame] | 1411 | __func__, key, (verified == 1) ? "verified" : "unverified"); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1412 | |
djm@openbsd.org | f69b69b | 2014-12-22 07:51:30 +0000 | [diff] [blame] | 1413 | /* If auth was successful then record key to ensure it isn't reused */ |
| 1414 | if (verified == 1) |
| 1415 | auth2_record_userkey(authctxt, key); |
| 1416 | else |
| 1417 | key_free(key); |
| 1418 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1419 | free(blob); |
| 1420 | free(signature); |
| 1421 | free(data); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1422 | |
Ben Lindstrom | e1c0912 | 2002-06-23 00:38:24 +0000 | [diff] [blame] | 1423 | auth_method = key_blobtype == MM_USERKEY ? "publickey" : "hostbased"; |
| 1424 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1425 | monitor_reset_key_state(); |
| 1426 | |
| 1427 | buffer_clear(m); |
| 1428 | buffer_put_int(m, verified); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1429 | mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1430 | |
Darren Tucker | fbba735 | 2006-11-07 23:16:08 +1100 | [diff] [blame] | 1431 | return (verified == 1); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1432 | } |
| 1433 | |
| 1434 | static void |
| 1435 | mm_record_login(Session *s, struct passwd *pw) |
| 1436 | { |
| 1437 | socklen_t fromlen; |
| 1438 | struct sockaddr_storage from; |
| 1439 | |
| 1440 | /* |
| 1441 | * Get IP address of client. If the connection is not a socket, let |
| 1442 | * the address be 0.0.0.0. |
| 1443 | */ |
| 1444 | memset(&from, 0, sizeof(from)); |
Damien Miller | ebc2306 | 2002-09-04 16:45:09 +1000 | [diff] [blame] | 1445 | fromlen = sizeof(from); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1446 | if (packet_connection_is_on_socket()) { |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1447 | if (getpeername(packet_get_connection_in(), |
Damien Miller | 9f3bd53 | 2006-03-26 14:07:52 +1100 | [diff] [blame] | 1448 | (struct sockaddr *)&from, &fromlen) < 0) { |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1449 | debug("getpeername: %.100s", strerror(errno)); |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 1450 | cleanup_exit(255); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1451 | } |
| 1452 | } |
| 1453 | /* Record that there was a login on that tty from the remote host. */ |
| 1454 | record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid, |
Damien Miller | 3a961dc | 2003-06-03 10:25:48 +1000 | [diff] [blame] | 1455 | get_remote_name_or_ip(utmp_len, options.use_dns), |
Damien Miller | ebc2306 | 2002-09-04 16:45:09 +1000 | [diff] [blame] | 1456 | (struct sockaddr *)&from, fromlen); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | static void |
| 1460 | mm_session_close(Session *s) |
| 1461 | { |
Damien Miller | 04bd8b0 | 2003-05-25 14:38:33 +1000 | [diff] [blame] | 1462 | debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1463 | if (s->ttyfd != -1) { |
Damien Miller | 9ab00b4 | 2006-08-05 12:40:11 +1000 | [diff] [blame] | 1464 | debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1465 | session_pty_cleanup2(s); |
| 1466 | } |
Damien Miller | 7207f64 | 2008-05-19 15:34:50 +1000 | [diff] [blame] | 1467 | session_unused(s->self); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1471 | mm_answer_pty(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1472 | { |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 1473 | extern struct monitor *pmonitor; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1474 | Session *s; |
| 1475 | int res, fd0; |
| 1476 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1477 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1478 | |
| 1479 | buffer_clear(m); |
| 1480 | s = session_new(); |
| 1481 | if (s == NULL) |
| 1482 | goto error; |
| 1483 | s->authctxt = authctxt; |
| 1484 | s->pw = authctxt->pw; |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 1485 | s->pid = pmonitor->m_pid; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1486 | res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty)); |
| 1487 | if (res == 0) |
| 1488 | goto error; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1489 | pty_setowner(authctxt->pw, s->tty); |
| 1490 | |
| 1491 | buffer_put_int(m, 1); |
| 1492 | buffer_put_cstring(m, s->tty); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1493 | |
| 1494 | /* We need to trick ttyslot */ |
| 1495 | if (dup2(s->ttyfd, 0) == -1) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1496 | fatal("%s: dup2", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1497 | |
| 1498 | mm_record_login(s, authctxt->pw); |
| 1499 | |
| 1500 | /* Now we can close the file descriptor again */ |
| 1501 | close(0); |
| 1502 | |
Darren Tucker | 0999174 | 2004-07-17 17:05:14 +1000 | [diff] [blame] | 1503 | /* send messages generated by record_login */ |
| 1504 | buffer_put_string(m, buffer_ptr(&loginmsg), buffer_len(&loginmsg)); |
| 1505 | buffer_clear(&loginmsg); |
| 1506 | |
| 1507 | mm_request_send(sock, MONITOR_ANS_PTY, m); |
| 1508 | |
Damien Miller | 54fd7cf | 2007-09-17 12:04:08 +1000 | [diff] [blame] | 1509 | if (mm_send_fd(sock, s->ptyfd) == -1 || |
| 1510 | mm_send_fd(sock, s->ttyfd) == -1) |
| 1511 | fatal("%s: send fds failed", __func__); |
Darren Tucker | 0999174 | 2004-07-17 17:05:14 +1000 | [diff] [blame] | 1512 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1513 | /* make sure nothing uses fd 0 */ |
| 1514 | if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) < 0) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1515 | fatal("%s: open(/dev/null): %s", __func__, strerror(errno)); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1516 | if (fd0 != 0) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1517 | error("%s: fd0 %d != 0", __func__, fd0); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1518 | |
| 1519 | /* slave is not needed */ |
| 1520 | close(s->ttyfd); |
| 1521 | s->ttyfd = s->ptyfd; |
| 1522 | /* no need to dup() because nobody closes ptyfd */ |
| 1523 | s->ptymaster = s->ptyfd; |
| 1524 | |
Damien Miller | 9ab00b4 | 2006-08-05 12:40:11 +1000 | [diff] [blame] | 1525 | debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1526 | |
| 1527 | return (0); |
| 1528 | |
| 1529 | error: |
| 1530 | if (s != NULL) |
| 1531 | mm_session_close(s); |
| 1532 | buffer_put_int(m, 0); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1533 | mm_request_send(sock, MONITOR_ANS_PTY, m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1534 | return (0); |
| 1535 | } |
| 1536 | |
| 1537 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1538 | mm_answer_pty_cleanup(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1539 | { |
| 1540 | Session *s; |
| 1541 | char *tty; |
| 1542 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1543 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1544 | |
| 1545 | tty = buffer_get_string(m, NULL); |
| 1546 | if ((s = session_by_tty(tty)) != NULL) |
| 1547 | mm_session_close(s); |
| 1548 | buffer_clear(m); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1549 | free(tty); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1550 | return (0); |
| 1551 | } |
| 1552 | |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 1553 | #ifdef WITH_SSH1 |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1554 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1555 | mm_answer_sesskey(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1556 | { |
| 1557 | BIGNUM *p; |
| 1558 | int rsafail; |
| 1559 | |
| 1560 | /* Turn off permissions */ |
Darren Tucker | 5b53026 | 2005-02-09 09:52:17 +1100 | [diff] [blame] | 1561 | monitor_permit(mon_dispatch, MONITOR_REQ_SESSKEY, 0); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1562 | |
| 1563 | if ((p = BN_new()) == NULL) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1564 | fatal("%s: BN_new", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1565 | |
| 1566 | buffer_get_bignum2(m, p); |
| 1567 | |
| 1568 | rsafail = ssh1_session_key(p); |
| 1569 | |
| 1570 | buffer_clear(m); |
| 1571 | buffer_put_int(m, rsafail); |
| 1572 | buffer_put_bignum2(m, p); |
| 1573 | |
| 1574 | BN_clear_free(p); |
| 1575 | |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1576 | mm_request_send(sock, MONITOR_ANS_SESSKEY, m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1577 | |
| 1578 | /* Turn on permissions for sessid passing */ |
| 1579 | monitor_permit(mon_dispatch, MONITOR_REQ_SESSID, 1); |
| 1580 | |
| 1581 | return (0); |
| 1582 | } |
| 1583 | |
| 1584 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1585 | mm_answer_sessid(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1586 | { |
| 1587 | int i; |
| 1588 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1589 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1590 | |
| 1591 | if (buffer_len(m) != 16) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1592 | fatal("%s: bad ssh1 session id", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1593 | for (i = 0; i < 16; i++) |
| 1594 | session_id[i] = buffer_get_char(m); |
| 1595 | |
| 1596 | /* Turn on permissions for getpwnam */ |
| 1597 | monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1); |
| 1598 | |
| 1599 | return (0); |
| 1600 | } |
| 1601 | |
| 1602 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1603 | mm_answer_rsa_keyallowed(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1604 | { |
| 1605 | BIGNUM *client_n; |
| 1606 | Key *key = NULL; |
| 1607 | u_char *blob = NULL; |
| 1608 | u_int blen = 0; |
| 1609 | int allowed = 0; |
| 1610 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1611 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1612 | |
Damien Miller | 7a8f5b3 | 2006-03-31 23:14:23 +1100 | [diff] [blame] | 1613 | auth_method = "rsa"; |
Ben Lindstrom | dcf6bfb | 2002-06-06 20:57:17 +0000 | [diff] [blame] | 1614 | if (options.rsa_authentication && authctxt->valid) { |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1615 | if ((client_n = BN_new()) == NULL) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1616 | fatal("%s: BN_new", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1617 | buffer_get_bignum2(m, client_n); |
| 1618 | allowed = auth_rsa_key_allowed(authctxt->pw, client_n, &key); |
| 1619 | BN_clear_free(client_n); |
| 1620 | } |
| 1621 | buffer_clear(m); |
| 1622 | buffer_put_int(m, allowed); |
Damien Miller | 06ebedf | 2003-02-24 12:03:38 +1100 | [diff] [blame] | 1623 | buffer_put_int(m, forced_command != NULL); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1624 | |
| 1625 | /* clear temporarily storage (used by generate challenge) */ |
| 1626 | monitor_reset_key_state(); |
| 1627 | |
| 1628 | if (allowed && key != NULL) { |
| 1629 | key->type = KEY_RSA; /* cheat for key_to_blob */ |
| 1630 | if (key_to_blob(key, &blob, &blen) == 0) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1631 | fatal("%s: key_to_blob failed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1632 | buffer_put_string(m, blob, blen); |
| 1633 | |
| 1634 | /* Save temporarily for comparison in verify */ |
| 1635 | key_blob = blob; |
| 1636 | key_bloblen = blen; |
| 1637 | key_blobtype = MM_RSAUSERKEY; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1638 | } |
Damien Miller | 0011138 | 2003-03-10 11:21:17 +1100 | [diff] [blame] | 1639 | if (key != NULL) |
| 1640 | key_free(key); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1641 | |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1642 | mm_request_send(sock, MONITOR_ANS_RSAKEYALLOWED, m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1643 | |
| 1644 | monitor_permit(mon_dispatch, MONITOR_REQ_RSACHALLENGE, allowed); |
| 1645 | monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 0); |
| 1646 | return (0); |
| 1647 | } |
| 1648 | |
| 1649 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1650 | mm_answer_rsa_challenge(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1651 | { |
| 1652 | Key *key = NULL; |
| 1653 | u_char *blob; |
| 1654 | u_int blen; |
| 1655 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1656 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1657 | |
| 1658 | if (!authctxt->valid) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1659 | fatal("%s: authctxt not valid", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1660 | blob = buffer_get_string(m, &blen); |
| 1661 | if (!monitor_allowed_key(blob, blen)) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1662 | fatal("%s: bad key, not previously allowed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1663 | if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1664 | fatal("%s: key type mismatch", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1665 | if ((key = key_from_blob(blob, blen)) == NULL) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1666 | fatal("%s: received bad key", __func__); |
Damien Miller | 923e8bb | 2009-02-14 16:33:31 +1100 | [diff] [blame] | 1667 | if (key->type != KEY_RSA) |
| 1668 | fatal("%s: received bad key type %d", __func__, key->type); |
| 1669 | key->type = KEY_RSA1; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1670 | if (ssh1_challenge) |
| 1671 | BN_clear_free(ssh1_challenge); |
| 1672 | ssh1_challenge = auth_rsa_generate_challenge(key); |
| 1673 | |
| 1674 | buffer_clear(m); |
| 1675 | buffer_put_bignum2(m, ssh1_challenge); |
| 1676 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1677 | debug3("%s sending reply", __func__); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1678 | mm_request_send(sock, MONITOR_ANS_RSACHALLENGE, m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1679 | |
| 1680 | monitor_permit(mon_dispatch, MONITOR_REQ_RSARESPONSE, 1); |
Damien Miller | 0011138 | 2003-03-10 11:21:17 +1100 | [diff] [blame] | 1681 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1682 | free(blob); |
Damien Miller | 0011138 | 2003-03-10 11:21:17 +1100 | [diff] [blame] | 1683 | key_free(key); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1684 | return (0); |
| 1685 | } |
| 1686 | |
| 1687 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1688 | mm_answer_rsa_response(int sock, Buffer *m) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1689 | { |
| 1690 | Key *key = NULL; |
| 1691 | u_char *blob, *response; |
| 1692 | u_int blen, len; |
| 1693 | int success; |
| 1694 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1695 | debug3("%s entering", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1696 | |
| 1697 | if (!authctxt->valid) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1698 | fatal("%s: authctxt not valid", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1699 | if (ssh1_challenge == NULL) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1700 | fatal("%s: no ssh1_challenge", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1701 | |
| 1702 | blob = buffer_get_string(m, &blen); |
| 1703 | if (!monitor_allowed_key(blob, blen)) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1704 | fatal("%s: bad key, not previously allowed", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1705 | if (key_blobtype != MM_RSAUSERKEY && key_blobtype != MM_RSAHOSTKEY) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1706 | fatal("%s: key type mismatch: %d", __func__, key_blobtype); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1707 | if ((key = key_from_blob(blob, blen)) == NULL) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1708 | fatal("%s: received bad key", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1709 | response = buffer_get_string(m, &len); |
| 1710 | if (len != 16) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1711 | fatal("%s: received bad response to challenge", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1712 | success = auth_rsa_verify_response(key, ssh1_challenge, response); |
| 1713 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1714 | free(blob); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1715 | key_free(key); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1716 | free(response); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1717 | |
| 1718 | auth_method = key_blobtype == MM_RSAUSERKEY ? "rsa" : "rhosts-rsa"; |
| 1719 | |
| 1720 | /* reset state */ |
| 1721 | BN_clear_free(ssh1_challenge); |
| 1722 | ssh1_challenge = NULL; |
| 1723 | monitor_reset_key_state(); |
| 1724 | |
| 1725 | buffer_clear(m); |
| 1726 | buffer_put_int(m, success); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1727 | mm_request_send(sock, MONITOR_ANS_RSARESPONSE, m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1728 | |
| 1729 | return (success); |
| 1730 | } |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 1731 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1732 | |
| 1733 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1734 | mm_answer_term(int sock, Buffer *req) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1735 | { |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 1736 | extern struct monitor *pmonitor; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1737 | int res, status; |
| 1738 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1739 | debug3("%s: tearing down sessions", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1740 | |
| 1741 | /* The child is terminating */ |
| 1742 | session_destroy_all(&mm_session_close); |
| 1743 | |
Darren Tucker | 52358d6 | 2008-03-11 22:58:25 +1100 | [diff] [blame] | 1744 | #ifdef USE_PAM |
| 1745 | if (options.use_pam) |
| 1746 | sshpam_cleanup(); |
| 1747 | #endif |
| 1748 | |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 1749 | while (waitpid(pmonitor->m_pid, &status, 0) == -1) |
Ben Lindstrom | 47fd811 | 2002-04-02 20:48:19 +0000 | [diff] [blame] | 1750 | if (errno != EINTR) |
| 1751 | exit(1); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1752 | |
| 1753 | res = WIFEXITED(status) ? WEXITSTATUS(status) : 1; |
| 1754 | |
| 1755 | /* Terminate process */ |
Darren Tucker | 1f8311c | 2004-05-13 16:39:33 +1000 | [diff] [blame] | 1756 | exit(res); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
Darren Tucker | 2e0cf0d | 2005-02-08 21:52:47 +1100 | [diff] [blame] | 1759 | #ifdef SSH_AUDIT_EVENTS |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 1760 | /* Report that an audit event occurred */ |
| 1761 | int |
| 1762 | mm_answer_audit_event(int socket, Buffer *m) |
| 1763 | { |
| 1764 | ssh_audit_event_t event; |
| 1765 | |
| 1766 | debug3("%s entering", __func__); |
| 1767 | |
| 1768 | event = buffer_get_int(m); |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 1769 | switch(event) { |
Darren Tucker | 2e0cf0d | 2005-02-08 21:52:47 +1100 | [diff] [blame] | 1770 | case SSH_AUTH_FAIL_PUBKEY: |
| 1771 | case SSH_AUTH_FAIL_HOSTBASED: |
| 1772 | case SSH_AUTH_FAIL_GSSAPI: |
| 1773 | case SSH_LOGIN_EXCEED_MAXTRIES: |
| 1774 | case SSH_LOGIN_ROOT_DENIED: |
| 1775 | case SSH_CONNECTION_CLOSE: |
| 1776 | case SSH_INVALID_USER: |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 1777 | audit_event(event); |
| 1778 | break; |
| 1779 | default: |
| 1780 | fatal("Audit event type %d not permitted", event); |
| 1781 | } |
| 1782 | |
| 1783 | return (0); |
| 1784 | } |
| 1785 | |
| 1786 | int |
| 1787 | mm_answer_audit_command(int socket, Buffer *m) |
| 1788 | { |
| 1789 | u_int len; |
| 1790 | char *cmd; |
| 1791 | |
| 1792 | debug3("%s entering", __func__); |
| 1793 | cmd = buffer_get_string(m, &len); |
| 1794 | /* sanity check command, if so how? */ |
| 1795 | audit_run_command(cmd); |
Darren Tucker | f60845f | 2013-06-02 08:07:31 +1000 | [diff] [blame] | 1796 | free(cmd); |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 1797 | return (0); |
| 1798 | } |
Darren Tucker | 2e0cf0d | 2005-02-08 21:52:47 +1100 | [diff] [blame] | 1799 | #endif /* SSH_AUDIT_EVENTS */ |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 1800 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1801 | void |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 1802 | monitor_apply_keystate(struct monitor *pmonitor) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1803 | { |
| 1804 | if (compat20) { |
| 1805 | set_newkeys(MODE_IN); |
| 1806 | set_newkeys(MODE_OUT); |
| 1807 | } else { |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1808 | packet_set_protocol_flags(child_state.ssh1protoflags); |
Ben Lindstrom | 402c6cc | 2002-06-21 00:43:42 +0000 | [diff] [blame] | 1809 | packet_set_encryption_key(child_state.ssh1key, |
| 1810 | child_state.ssh1keylen, child_state.ssh1cipher); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1811 | free(child_state.ssh1key); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1812 | } |
| 1813 | |
Ben Lindstrom | 402c6cc | 2002-06-21 00:43:42 +0000 | [diff] [blame] | 1814 | /* for rc4 and other stateful ciphers */ |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1815 | packet_set_keycontext(MODE_OUT, child_state.keyout); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1816 | free(child_state.keyout); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1817 | packet_set_keycontext(MODE_IN, child_state.keyin); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1818 | free(child_state.keyin); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1819 | |
| 1820 | if (!compat20) { |
| 1821 | packet_set_iv(MODE_OUT, child_state.ivout); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1822 | free(child_state.ivout); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1823 | packet_set_iv(MODE_IN, child_state.ivin); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1824 | free(child_state.ivin); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1825 | } |
| 1826 | |
| 1827 | memcpy(&incoming_stream, &child_state.incoming, |
| 1828 | sizeof(incoming_stream)); |
| 1829 | memcpy(&outgoing_stream, &child_state.outgoing, |
| 1830 | sizeof(outgoing_stream)); |
| 1831 | |
| 1832 | /* Update with new address */ |
Damien Miller | 2d6b833 | 2002-06-21 15:59:49 +1000 | [diff] [blame] | 1833 | if (options.compression) |
| 1834 | mm_init_compression(pmonitor->m_zlib); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1835 | |
Damien Miller | c31a0cd | 2014-05-15 14:37:39 +1000 | [diff] [blame] | 1836 | packet_set_postauth(); |
| 1837 | |
Darren Tucker | 5f96f3b | 2013-05-16 20:29:28 +1000 | [diff] [blame] | 1838 | if (options.rekey_limit || options.rekey_interval) |
| 1839 | packet_set_rekey_limits((u_int32_t)options.rekey_limit, |
| 1840 | (time_t)options.rekey_interval); |
| 1841 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1842 | /* Network I/O buffers */ |
| 1843 | /* XXX inefficient for large buffers, need: buffer_init_from_string */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1844 | buffer_clear(packet_get_input()); |
| 1845 | buffer_append(packet_get_input(), child_state.input, child_state.ilen); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 1846 | explicit_bzero(child_state.input, child_state.ilen); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1847 | free(child_state.input); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1848 | |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 1849 | buffer_clear(packet_get_output()); |
| 1850 | buffer_append(packet_get_output(), child_state.output, |
| 1851 | child_state.olen); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 1852 | explicit_bzero(child_state.output, child_state.olen); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1853 | free(child_state.output); |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 1854 | |
| 1855 | /* Roaming */ |
| 1856 | if (compat20) |
| 1857 | roam_set_bytes(child_state.sent_bytes, child_state.recv_bytes); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1858 | } |
| 1859 | |
| 1860 | static Kex * |
| 1861 | mm_get_kex(Buffer *m) |
| 1862 | { |
| 1863 | Kex *kex; |
| 1864 | void *blob; |
| 1865 | u_int bloblen; |
| 1866 | |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 1867 | kex = xcalloc(1, sizeof(*kex)); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1868 | kex->session_id = buffer_get_string(m, &kex->session_id_len); |
Damien Miller | 8a0268f | 2010-07-16 13:57:51 +1000 | [diff] [blame] | 1869 | if (session_id2 == NULL || |
| 1870 | kex->session_id_len != session_id2_len || |
Damien Miller | ea1651c | 2010-07-16 13:58:37 +1000 | [diff] [blame] | 1871 | timingsafe_bcmp(kex->session_id, session_id2, session_id2_len) != 0) |
Ben Lindstrom | f67e077 | 2002-06-06 20:58:19 +0000 | [diff] [blame] | 1872 | fatal("mm_get_get: internal error: bad session id"); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1873 | kex->we_need = buffer_get_int(m); |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 1874 | #ifdef WITH_OPENSSL |
Damien Miller | b062c29 | 2003-03-24 09:12:09 +1100 | [diff] [blame] | 1875 | kex->kex[KEX_DH_GRP1_SHA1] = kexdh_server; |
Damien Miller | f675fc4 | 2004-06-15 10:30:09 +1000 | [diff] [blame] | 1876 | kex->kex[KEX_DH_GRP14_SHA1] = kexdh_server; |
Damien Miller | b062c29 | 2003-03-24 09:12:09 +1100 | [diff] [blame] | 1877 | kex->kex[KEX_DH_GEX_SHA1] = kexgex_server; |
Damien Miller | a63128d | 2006-03-15 12:08:28 +1100 | [diff] [blame] | 1878 | kex->kex[KEX_DH_GEX_SHA256] = kexgex_server; |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1879 | kex->kex[KEX_ECDH_SHA2] = kexecdh_server; |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 1880 | #endif |
Damien Miller | c983197 | 2013-11-07 12:00:23 +1100 | [diff] [blame] | 1881 | kex->kex[KEX_C25519_SHA256] = kexc25519_server; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1882 | kex->server = 1; |
| 1883 | kex->hostkey_type = buffer_get_int(m); |
| 1884 | kex->kex_type = buffer_get_int(m); |
| 1885 | blob = buffer_get_string(m, &bloblen); |
| 1886 | buffer_init(&kex->my); |
| 1887 | buffer_append(&kex->my, blob, bloblen); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1888 | free(blob); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1889 | blob = buffer_get_string(m, &bloblen); |
| 1890 | buffer_init(&kex->peer); |
| 1891 | buffer_append(&kex->peer, blob, bloblen); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1892 | free(blob); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1893 | kex->done = 1; |
| 1894 | kex->flags = buffer_get_int(m); |
| 1895 | kex->client_version_string = buffer_get_string(m, NULL); |
| 1896 | kex->server_version_string = buffer_get_string(m, NULL); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1897 | kex->load_host_public_key=&get_hostkey_public_by_type; |
| 1898 | kex->load_host_private_key=&get_hostkey_private_by_type; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1899 | kex->host_key_index=&get_hostkey_index; |
Damien Miller | 85b45e0 | 2013-07-20 13:21:52 +1000 | [diff] [blame] | 1900 | kex->sign = sshd_hostkey_sign; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1901 | |
| 1902 | return (kex); |
| 1903 | } |
| 1904 | |
| 1905 | /* This function requries careful sanity checking */ |
| 1906 | |
| 1907 | void |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 1908 | mm_get_keystate(struct monitor *pmonitor) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1909 | { |
| 1910 | Buffer m; |
| 1911 | u_char *blob, *p; |
| 1912 | u_int bloblen, plen; |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1913 | u_int32_t seqnr, packets; |
Damien Miller | b61f3fc | 2008-07-11 17:36:48 +1000 | [diff] [blame] | 1914 | u_int64_t blocks, bytes; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1915 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1916 | debug3("%s: Waiting for new keys", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1917 | |
| 1918 | buffer_init(&m); |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 1919 | mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT, &m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1920 | if (!compat20) { |
| 1921 | child_state.ssh1protoflags = buffer_get_int(&m); |
| 1922 | child_state.ssh1cipher = buffer_get_int(&m); |
Ben Lindstrom | 402c6cc | 2002-06-21 00:43:42 +0000 | [diff] [blame] | 1923 | child_state.ssh1key = buffer_get_string(&m, |
| 1924 | &child_state.ssh1keylen); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1925 | child_state.ivout = buffer_get_string(&m, |
| 1926 | &child_state.ivoutlen); |
| 1927 | child_state.ivin = buffer_get_string(&m, &child_state.ivinlen); |
| 1928 | goto skip; |
| 1929 | } else { |
| 1930 | /* Get the Kex for rekeying */ |
Ben Lindstrom | 7339b2a | 2002-05-15 16:25:01 +0000 | [diff] [blame] | 1931 | *pmonitor->m_pkex = mm_get_kex(&m); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1932 | } |
| 1933 | |
| 1934 | blob = buffer_get_string(&m, &bloblen); |
| 1935 | current_keys[MODE_OUT] = mm_newkeys_from_blob(blob, bloblen); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1936 | free(blob); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1937 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1938 | debug3("%s: Waiting for second key", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1939 | blob = buffer_get_string(&m, &bloblen); |
| 1940 | current_keys[MODE_IN] = mm_newkeys_from_blob(blob, bloblen); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1941 | free(blob); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1942 | |
| 1943 | /* Now get sequence numbers for the packets */ |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1944 | seqnr = buffer_get_int(&m); |
| 1945 | blocks = buffer_get_int64(&m); |
| 1946 | packets = buffer_get_int(&m); |
Damien Miller | b61f3fc | 2008-07-11 17:36:48 +1000 | [diff] [blame] | 1947 | bytes = buffer_get_int64(&m); |
| 1948 | packet_set_state(MODE_OUT, seqnr, blocks, packets, bytes); |
Damien Miller | a5539d2 | 2003-04-09 20:50:06 +1000 | [diff] [blame] | 1949 | seqnr = buffer_get_int(&m); |
| 1950 | blocks = buffer_get_int64(&m); |
| 1951 | packets = buffer_get_int(&m); |
Damien Miller | b61f3fc | 2008-07-11 17:36:48 +1000 | [diff] [blame] | 1952 | bytes = buffer_get_int64(&m); |
| 1953 | packet_set_state(MODE_IN, seqnr, blocks, packets, bytes); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1954 | |
| 1955 | skip: |
| 1956 | /* Get the key context */ |
| 1957 | child_state.keyout = buffer_get_string(&m, &child_state.keyoutlen); |
| 1958 | child_state.keyin = buffer_get_string(&m, &child_state.keyinlen); |
| 1959 | |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1960 | debug3("%s: Getting compression state", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1961 | /* Get compression state */ |
| 1962 | p = buffer_get_string(&m, &plen); |
| 1963 | if (plen != sizeof(child_state.outgoing)) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1964 | fatal("%s: bad request size", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1965 | memcpy(&child_state.outgoing, p, sizeof(child_state.outgoing)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1966 | free(p); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1967 | |
| 1968 | p = buffer_get_string(&m, &plen); |
| 1969 | if (plen != sizeof(child_state.incoming)) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1970 | fatal("%s: bad request size", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1971 | memcpy(&child_state.incoming, p, sizeof(child_state.incoming)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1972 | free(p); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1973 | |
| 1974 | /* Network I/O buffers */ |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 1975 | debug3("%s: Getting Network I/O buffers", __func__); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1976 | child_state.input = buffer_get_string(&m, &child_state.ilen); |
| 1977 | child_state.output = buffer_get_string(&m, &child_state.olen); |
| 1978 | |
Darren Tucker | c5564e1 | 2009-06-21 18:53:53 +1000 | [diff] [blame] | 1979 | /* Roaming */ |
| 1980 | if (compat20) { |
| 1981 | child_state.sent_bytes = buffer_get_int64(&m); |
| 1982 | child_state.recv_bytes = buffer_get_int64(&m); |
| 1983 | } |
| 1984 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1985 | buffer_free(&m); |
| 1986 | } |
| 1987 | |
| 1988 | |
| 1989 | /* Allocation functions for zlib */ |
| 1990 | void * |
| 1991 | mm_zalloc(struct mm_master *mm, u_int ncount, u_int size) |
| 1992 | { |
Ben Lindstrom | 41ee2b0 | 2002-11-09 15:47:47 +0000 | [diff] [blame] | 1993 | size_t len = (size_t) size * ncount; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 1994 | void *address; |
| 1995 | |
Ben Lindstrom | 0a4f754 | 2002-08-20 18:36:25 +0000 | [diff] [blame] | 1996 | if (len == 0 || ncount > SIZE_T_MAX / size) |
Damien Miller | 530a754 | 2002-06-26 23:27:11 +1000 | [diff] [blame] | 1997 | fatal("%s: mm_zalloc(%u, %u)", __func__, ncount, size); |
| 1998 | |
| 1999 | address = mm_malloc(mm, len); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 2000 | |
| 2001 | return (address); |
| 2002 | } |
| 2003 | |
| 2004 | void |
| 2005 | mm_zfree(struct mm_master *mm, void *address) |
| 2006 | { |
| 2007 | mm_free(mm, address); |
| 2008 | } |
| 2009 | |
| 2010 | void |
| 2011 | mm_init_compression(struct mm_master *mm) |
| 2012 | { |
| 2013 | outgoing_stream.zalloc = (alloc_func)mm_zalloc; |
| 2014 | outgoing_stream.zfree = (free_func)mm_zfree; |
| 2015 | outgoing_stream.opaque = mm; |
| 2016 | |
| 2017 | incoming_stream.zalloc = (alloc_func)mm_zalloc; |
| 2018 | incoming_stream.zfree = (free_func)mm_zfree; |
| 2019 | incoming_stream.opaque = mm; |
| 2020 | } |
| 2021 | |
| 2022 | /* XXX */ |
| 2023 | |
| 2024 | #define FD_CLOSEONEXEC(x) do { \ |
Damien Miller | 814ace0 | 2011-05-20 19:02:47 +1000 | [diff] [blame] | 2025 | if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \ |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 2026 | fatal("fcntl(%d, F_SETFD)", x); \ |
| 2027 | } while (0) |
| 2028 | |
| 2029 | static void |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 2030 | monitor_openfds(struct monitor *mon, int do_logfds) |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 2031 | { |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 2032 | int pair[2]; |
| 2033 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 2034 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 2035 | fatal("%s: socketpair: %s", __func__, strerror(errno)); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 2036 | FD_CLOSEONEXEC(pair[0]); |
| 2037 | FD_CLOSEONEXEC(pair[1]); |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 2038 | mon->m_recvfd = pair[0]; |
| 2039 | mon->m_sendfd = pair[1]; |
| 2040 | |
| 2041 | if (do_logfds) { |
| 2042 | if (pipe(pair) == -1) |
| 2043 | fatal("%s: pipe: %s", __func__, strerror(errno)); |
| 2044 | FD_CLOSEONEXEC(pair[0]); |
| 2045 | FD_CLOSEONEXEC(pair[1]); |
| 2046 | mon->m_log_recvfd = pair[0]; |
| 2047 | mon->m_log_sendfd = pair[1]; |
| 2048 | } else |
| 2049 | mon->m_log_recvfd = mon->m_log_sendfd = -1; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | #define MM_MEMSIZE 65536 |
| 2053 | |
| 2054 | struct monitor * |
| 2055 | monitor_init(void) |
| 2056 | { |
| 2057 | struct monitor *mon; |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 2058 | |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 2059 | mon = xcalloc(1, sizeof(*mon)); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 2060 | |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 2061 | monitor_openfds(mon, 1); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 2062 | |
| 2063 | /* Used to share zlib space across processes */ |
Damien Miller | 2d6b833 | 2002-06-21 15:59:49 +1000 | [diff] [blame] | 2064 | if (options.compression) { |
| 2065 | mon->m_zback = mm_create(NULL, MM_MEMSIZE); |
| 2066 | mon->m_zlib = mm_create(mon->m_zback, 20 * MM_MEMSIZE); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 2067 | |
Damien Miller | 2d6b833 | 2002-06-21 15:59:49 +1000 | [diff] [blame] | 2068 | /* Compression needs to share state across borders */ |
| 2069 | mm_init_compression(mon->m_zlib); |
| 2070 | } |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 2071 | |
| 2072 | return mon; |
| 2073 | } |
| 2074 | |
| 2075 | void |
| 2076 | monitor_reinit(struct monitor *mon) |
| 2077 | { |
Damien Miller | 8f0bf23 | 2011-06-20 14:42:23 +1000 | [diff] [blame] | 2078 | monitor_openfds(mon, 0); |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 2079 | } |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 2080 | |
| 2081 | #ifdef GSSAPI |
| 2082 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 2083 | mm_answer_gss_setup_ctx(int sock, Buffer *m) |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 2084 | { |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 2085 | gss_OID_desc goid; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 2086 | OM_uint32 major; |
| 2087 | u_int len; |
| 2088 | |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 2089 | goid.elements = buffer_get_string(m, &len); |
| 2090 | goid.length = len; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 2091 | |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 2092 | major = ssh_gssapi_server_ctx(&gsscontext, &goid); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 2093 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 2094 | free(goid.elements); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 2095 | |
| 2096 | buffer_clear(m); |
| 2097 | buffer_put_int(m, major); |
| 2098 | |
Damien Miller | 6fd6def | 2005-11-05 15:07:05 +1100 | [diff] [blame] | 2099 | mm_request_send(sock, MONITOR_ANS_GSSSETUP, m); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 2100 | |
| 2101 | /* Now we have a context, enable the step */ |
| 2102 | monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1); |
| 2103 | |
| 2104 | return (0); |
| 2105 | } |
| 2106 | |
| 2107 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 2108 | mm_answer_gss_accept_ctx(int sock, Buffer *m) |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 2109 | { |
| 2110 | gss_buffer_desc in; |
| 2111 | gss_buffer_desc out = GSS_C_EMPTY_BUFFER; |
Damien Miller | 6fd6def | 2005-11-05 15:07:05 +1100 | [diff] [blame] | 2112 | OM_uint32 major, minor; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 2113 | OM_uint32 flags = 0; /* GSI needs this */ |
Darren Tucker | 600ad8d | 2003-08-26 12:10:48 +1000 | [diff] [blame] | 2114 | u_int len; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 2115 | |
Darren Tucker | 600ad8d | 2003-08-26 12:10:48 +1000 | [diff] [blame] | 2116 | in.value = buffer_get_string(m, &len); |
| 2117 | in.length = len; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 2118 | major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 2119 | free(in.value); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 2120 | |
| 2121 | buffer_clear(m); |
| 2122 | buffer_put_int(m, major); |
| 2123 | buffer_put_string(m, out.value, out.length); |
| 2124 | buffer_put_int(m, flags); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 2125 | mm_request_send(sock, MONITOR_ANS_GSSSTEP, m); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 2126 | |
| 2127 | gss_release_buffer(&minor, &out); |
| 2128 | |
Damien Miller | 6fd6def | 2005-11-05 15:07:05 +1100 | [diff] [blame] | 2129 | if (major == GSS_S_COMPLETE) { |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 2130 | monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0); |
| 2131 | monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1); |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 2132 | monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 2133 | } |
| 2134 | return (0); |
| 2135 | } |
| 2136 | |
| 2137 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 2138 | mm_answer_gss_checkmic(int sock, Buffer *m) |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 2139 | { |
| 2140 | gss_buffer_desc gssbuf, mic; |
| 2141 | OM_uint32 ret; |
| 2142 | u_int len; |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 2143 | |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 2144 | gssbuf.value = buffer_get_string(m, &len); |
| 2145 | gssbuf.length = len; |
| 2146 | mic.value = buffer_get_string(m, &len); |
| 2147 | mic.length = len; |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 2148 | |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 2149 | ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 2150 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 2151 | free(gssbuf.value); |
| 2152 | free(mic.value); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 2153 | |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 2154 | buffer_clear(m); |
| 2155 | buffer_put_int(m, ret); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 2156 | |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 2157 | mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 2158 | |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 2159 | if (!GSS_ERROR(ret)) |
| 2160 | monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 2161 | |
Damien Miller | 0425d40 | 2003-11-17 22:18:21 +1100 | [diff] [blame] | 2162 | return (0); |
| 2163 | } |
| 2164 | |
| 2165 | int |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 2166 | mm_answer_gss_userok(int sock, Buffer *m) |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 2167 | { |
| 2168 | int authenticated; |
| 2169 | |
| 2170 | authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user); |
| 2171 | |
| 2172 | buffer_clear(m); |
| 2173 | buffer_put_int(m, authenticated); |
| 2174 | |
| 2175 | debug3("%s: sending result %d", __func__, authenticated); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 2176 | mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m); |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 2177 | |
Damien Miller | 6fd6def | 2005-11-05 15:07:05 +1100 | [diff] [blame] | 2178 | auth_method = "gssapi-with-mic"; |
Darren Tucker | 0efd155 | 2003-08-26 11:49:55 +1000 | [diff] [blame] | 2179 | |
| 2180 | /* Monitor loop will terminate if authenticated */ |
| 2181 | return (authenticated); |
| 2182 | } |
| 2183 | #endif /* GSSAPI */ |
Damien Miller | 01ed227 | 2008-11-05 16:20:46 +1100 | [diff] [blame] | 2184 | |