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