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