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