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