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