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