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