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