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