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