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