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