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