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