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