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