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