naddy@openbsd.org | a47f6a6 | 2020-02-06 22:30:54 +0000 | [diff] [blame] | 1 | /* $OpenBSD: auth2-pubkey.c,v 1.99 2020/02/06 22:30:54 naddy Exp $ */ |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2000 Markus Friedl. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 15 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 16 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 17 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 18 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 19 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 20 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 21 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 23 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "includes.h" |
Damien Miller | f17883e | 2006-03-15 11:45:54 +1100 | [diff] [blame] | 27 | |
| 28 | #include <sys/types.h> |
| 29 | #include <sys/stat.h> |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 30 | |
djm@openbsd.org | be02d7c | 2019-09-06 04:53:27 +0000 | [diff] [blame] | 31 | #include <stdlib.h> |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 32 | #include <errno.h> |
Darren Tucker | 06db584 | 2008-06-13 14:51:28 +1000 | [diff] [blame] | 33 | #include <fcntl.h> |
Darren Tucker | 737f7af | 2012-11-05 17:07:43 +1100 | [diff] [blame] | 34 | #ifdef HAVE_PATHS_H |
| 35 | # include <paths.h> |
| 36 | #endif |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 37 | #include <pwd.h> |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 38 | #include <signal.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 39 | #include <stdio.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 40 | #include <stdarg.h> |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 41 | #include <string.h> |
| 42 | #include <time.h> |
Darren Tucker | d9526a5 | 2008-06-14 09:01:24 +1000 | [diff] [blame] | 43 | #include <unistd.h> |
djm@openbsd.org | f69b69b | 2014-12-22 07:51:30 +0000 | [diff] [blame] | 44 | #include <limits.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 45 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 46 | #include "xmalloc.h" |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 47 | #include "ssh.h" |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 48 | #include "ssh2.h" |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 49 | #include "packet.h" |
markus@openbsd.org | c7d39ac | 2018-07-09 21:35:50 +0000 | [diff] [blame] | 50 | #include "sshbuf.h" |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 51 | #include "log.h" |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 52 | #include "misc.h" |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 53 | #include "servconf.h" |
| 54 | #include "compat.h" |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 55 | #include "sshkey.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 56 | #include "hostfile.h" |
| 57 | #include "auth.h" |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 58 | #include "pathnames.h" |
| 59 | #include "uidswap.h" |
| 60 | #include "auth-options.h" |
| 61 | #include "canohost.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 62 | #ifdef GSSAPI |
| 63 | #include "ssh-gss.h" |
| 64 | #endif |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 65 | #include "monitor_wrap.h" |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 66 | #include "authfile.h" |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 67 | #include "match.h" |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 68 | #include "ssherr.h" |
| 69 | #include "channels.h" /* XXX for session.h */ |
| 70 | #include "session.h" /* XXX for child_set_env(); refactor? */ |
djm@openbsd.org | 0fddf29 | 2019-11-25 00:52:46 +0000 | [diff] [blame] | 71 | #include "sk-api.h" |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 72 | |
| 73 | /* import */ |
| 74 | extern ServerOptions options; |
| 75 | extern u_char *session_id2; |
Darren Tucker | 502d384 | 2003-06-28 12:38:01 +1000 | [diff] [blame] | 76 | extern u_int session_id2_len; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 77 | |
djm@openbsd.org | 2788563 | 2017-12-19 00:24:34 +0000 | [diff] [blame] | 78 | static char * |
| 79 | format_key(const struct sshkey *key) |
| 80 | { |
| 81 | char *ret, *fp = sshkey_fingerprint(key, |
| 82 | options.fingerprint_hash, SSH_FP_DEFAULT); |
| 83 | |
| 84 | xasprintf(&ret, "%s %s", sshkey_type(key), fp); |
| 85 | free(fp); |
| 86 | return ret; |
| 87 | } |
| 88 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 89 | static int |
markus@openbsd.org | eb272ea | 2017-05-30 14:29:59 +0000 | [diff] [blame] | 90 | userauth_pubkey(struct ssh *ssh) |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 91 | { |
markus@openbsd.org | eb272ea | 2017-05-30 14:29:59 +0000 | [diff] [blame] | 92 | Authctxt *authctxt = ssh->authctxt; |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 93 | struct passwd *pw = authctxt->pw; |
djm@openbsd.org | 74287f5 | 2018-07-31 03:10:27 +0000 | [diff] [blame] | 94 | struct sshbuf *b = NULL; |
markus@openbsd.org | 54d90ac | 2017-05-30 08:52:19 +0000 | [diff] [blame] | 95 | struct sshkey *key = NULL; |
djm@openbsd.org | 74287f5 | 2018-07-31 03:10:27 +0000 | [diff] [blame] | 96 | char *pkalg = NULL, *userstyle = NULL, *key_s = NULL, *ca_s = NULL; |
| 97 | u_char *pkblob = NULL, *sig = NULL, have_sig; |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 98 | size_t blen, slen; |
| 99 | int r, pktype; |
djm@openbsd.org | 0fddf29 | 2019-11-25 00:52:46 +0000 | [diff] [blame] | 100 | int req_presence = 0, authenticated = 0; |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 101 | struct sshauthopt *authopts = NULL; |
djm@openbsd.org | b7e74ea | 2019-11-25 00:51:37 +0000 | [diff] [blame] | 102 | struct sshkey_sig_details *sig_details = NULL; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 103 | |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 104 | if ((r = sshpkt_get_u8(ssh, &have_sig)) != 0 || |
| 105 | (r = sshpkt_get_cstring(ssh, &pkalg, NULL)) != 0 || |
| 106 | (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0) |
| 107 | fatal("%s: parse request failed: %s", __func__, ssh_err(r)); |
djm@openbsd.org | ff5d2cf | 2019-01-22 11:26:16 +0000 | [diff] [blame] | 108 | |
| 109 | if (log_level_get() >= SYSLOG_LEVEL_DEBUG2) { |
| 110 | char *keystring; |
| 111 | struct sshbuf *pkbuf; |
| 112 | |
| 113 | if ((pkbuf = sshbuf_from(pkblob, blen)) == NULL) |
| 114 | fatal("%s: sshbuf_from failed", __func__); |
djm@openbsd.org | 16dd8b2 | 2019-07-16 13:18:39 +0000 | [diff] [blame] | 115 | if ((keystring = sshbuf_dtob64_string(pkbuf, 0)) == NULL) |
djm@openbsd.org | ff5d2cf | 2019-01-22 11:26:16 +0000 | [diff] [blame] | 116 | fatal("%s: sshbuf_dtob64 failed", __func__); |
| 117 | debug2("%s: %s user %s %s public key %s %s", __func__, |
| 118 | authctxt->valid ? "valid" : "invalid", authctxt->user, |
| 119 | have_sig ? "attempting" : "querying", pkalg, keystring); |
| 120 | sshbuf_free(pkbuf); |
| 121 | free(keystring); |
| 122 | } |
| 123 | |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 124 | pktype = sshkey_type_from_name(pkalg); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 125 | if (pktype == KEY_UNSPEC) { |
| 126 | /* this is perfectly legal */ |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 127 | verbose("%s: unsupported public key algorithm: %s", |
djm@openbsd.org | ebacd37 | 2016-01-27 00:53:12 +0000 | [diff] [blame] | 128 | __func__, pkalg); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 129 | goto done; |
| 130 | } |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 131 | if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) { |
| 132 | error("%s: could not parse key: %s", __func__, ssh_err(r)); |
| 133 | goto done; |
| 134 | } |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 135 | if (key == NULL) { |
djm@openbsd.org | ebacd37 | 2016-01-27 00:53:12 +0000 | [diff] [blame] | 136 | error("%s: cannot decode key: %s", __func__, pkalg); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 137 | goto done; |
| 138 | } |
| 139 | if (key->type != pktype) { |
djm@openbsd.org | ebacd37 | 2016-01-27 00:53:12 +0000 | [diff] [blame] | 140 | error("%s: type mismatch for decoded key " |
| 141 | "(received %d, expected %d)", __func__, key->type, pktype); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 142 | goto done; |
| 143 | } |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 144 | if (sshkey_type_plain(key->type) == KEY_RSA && |
| 145 | (ssh->compat & SSH_BUG_RSASIGMD5) != 0) { |
Damien Miller | 324541e | 2013-12-31 12:25:40 +1100 | [diff] [blame] | 146 | logit("Refusing RSA key because client uses unsafe " |
| 147 | "signature scheme"); |
| 148 | goto done; |
| 149 | } |
djm@openbsd.org | 8f57495 | 2017-06-24 06:34:38 +0000 | [diff] [blame] | 150 | if (auth2_key_already_used(authctxt, key)) { |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 151 | logit("refusing previously-used %s key", sshkey_type(key)); |
djm@openbsd.org | f69b69b | 2014-12-22 07:51:30 +0000 | [diff] [blame] | 152 | goto done; |
| 153 | } |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 154 | if (match_pattern_list(pkalg, options.pubkey_key_types, 0) != 1) { |
djm@openbsd.org | 1f729f0 | 2015-01-13 07:39:19 +0000 | [diff] [blame] | 155 | logit("%s: key type %s not in PubkeyAcceptedKeyTypes", |
| 156 | __func__, sshkey_ssh_name(key)); |
| 157 | goto done; |
| 158 | } |
djm@openbsd.org | 86e5737 | 2018-09-20 03:28:06 +0000 | [diff] [blame] | 159 | if ((r = sshkey_check_cert_sigtype(key, |
| 160 | options.ca_sign_algorithms)) != 0) { |
| 161 | logit("%s: certificate signature algorithm %s: %s", __func__, |
| 162 | (key->cert == NULL || key->cert->signature_type == NULL) ? |
| 163 | "(null)" : key->cert->signature_type, ssh_err(r)); |
| 164 | goto done; |
| 165 | } |
djm@openbsd.org | 2788563 | 2017-12-19 00:24:34 +0000 | [diff] [blame] | 166 | key_s = format_key(key); |
| 167 | if (sshkey_is_cert(key)) |
| 168 | ca_s = format_key(key->cert->signature_key); |
| 169 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 170 | if (have_sig) { |
djm@openbsd.org | 2788563 | 2017-12-19 00:24:34 +0000 | [diff] [blame] | 171 | debug3("%s: have %s signature for %s%s%s", |
| 172 | __func__, pkalg, key_s, |
| 173 | ca_s == NULL ? "" : " CA ", |
| 174 | ca_s == NULL ? "" : ca_s); |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 175 | if ((r = sshpkt_get_string(ssh, &sig, &slen)) != 0 || |
| 176 | (r = sshpkt_get_end(ssh)) != 0) |
| 177 | fatal("%s: %s", __func__, ssh_err(r)); |
| 178 | if ((b = sshbuf_new()) == NULL) |
| 179 | fatal("%s: sshbuf_new failed", __func__); |
| 180 | if (ssh->compat & SSH_OLD_SESSIONID) { |
| 181 | if ((r = sshbuf_put(b, session_id2, |
| 182 | session_id2_len)) != 0) |
| 183 | fatal("%s: sshbuf_put session id: %s", |
| 184 | __func__, ssh_err(r)); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 185 | } else { |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 186 | if ((r = sshbuf_put_string(b, session_id2, |
| 187 | session_id2_len)) != 0) |
| 188 | fatal("%s: sshbuf_put_string session id: %s", |
| 189 | __func__, ssh_err(r)); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 190 | } |
djm@openbsd.org | 74287f5 | 2018-07-31 03:10:27 +0000 | [diff] [blame] | 191 | if (!authctxt->valid || authctxt->user == NULL) { |
| 192 | debug2("%s: disabled because of invalid user", |
| 193 | __func__); |
| 194 | goto done; |
| 195 | } |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 196 | /* reconstruct packet */ |
Damien Miller | 4ce189d | 2013-04-23 15:17:52 +1000 | [diff] [blame] | 197 | xasprintf(&userstyle, "%s%s%s", authctxt->user, |
| 198 | authctxt->style ? ":" : "", |
| 199 | authctxt->style ? authctxt->style : ""); |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 200 | if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 || |
| 201 | (r = sshbuf_put_cstring(b, userstyle)) != 0 || |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 202 | (r = sshbuf_put_cstring(b, authctxt->service)) != 0 || |
| 203 | (r = sshbuf_put_cstring(b, "publickey")) != 0 || |
| 204 | (r = sshbuf_put_u8(b, have_sig)) != 0 || |
mestre@openbsd.org | db8bb80 | 2018-08-28 12:25:53 +0000 | [diff] [blame] | 205 | (r = sshbuf_put_cstring(b, pkalg)) != 0 || |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 206 | (r = sshbuf_put_string(b, pkblob, blen)) != 0) |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 207 | fatal("%s: build packet failed: %s", |
| 208 | __func__, ssh_err(r)); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 209 | #ifdef DEBUG_PK |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 210 | sshbuf_dump(b, stderr); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 211 | #endif |
| 212 | /* test for correct signature */ |
| 213 | authenticated = 0; |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 214 | if (PRIVSEP(user_key_allowed(ssh, pw, key, 1, &authopts)) && |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 215 | PRIVSEP(sshkey_verify(key, sig, slen, |
| 216 | sshbuf_ptr(b), sshbuf_len(b), |
| 217 | (ssh->compat & SSH_BUG_SIGTYPE) == 0 ? pkalg : NULL, |
djm@openbsd.org | b7e74ea | 2019-11-25 00:51:37 +0000 | [diff] [blame] | 218 | ssh->compat, &sig_details)) == 0) { |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 219 | authenticated = 1; |
djm@openbsd.org | f69b69b | 2014-12-22 07:51:30 +0000 | [diff] [blame] | 220 | } |
djm@openbsd.org | 0fddf29 | 2019-11-25 00:52:46 +0000 | [diff] [blame] | 221 | if (authenticated == 1 && sig_details != NULL) { |
| 222 | auth2_record_info(authctxt, "signature count = %u", |
| 223 | sig_details->sk_counter); |
djm@openbsd.org | b7e74ea | 2019-11-25 00:51:37 +0000 | [diff] [blame] | 224 | debug("%s: sk_counter = %u, sk_flags = 0x%02x", |
| 225 | __func__, sig_details->sk_counter, |
| 226 | sig_details->sk_flags); |
djm@openbsd.org | 0fddf29 | 2019-11-25 00:52:46 +0000 | [diff] [blame] | 227 | req_presence = (options.pubkey_auth_options & |
djm@openbsd.org | 2e71263 | 2019-11-25 00:54:23 +0000 | [diff] [blame] | 228 | PUBKEYAUTH_TOUCH_REQUIRED) || |
| 229 | !authopts->no_require_user_presence; |
djm@openbsd.org | 0fddf29 | 2019-11-25 00:52:46 +0000 | [diff] [blame] | 230 | if (req_presence && (sig_details->sk_flags & |
| 231 | SSH_SK_USER_PRESENCE_REQD) == 0) { |
| 232 | error("public key %s signature for %s%s from " |
| 233 | "%.128s port %d rejected: user presence " |
naddy@openbsd.org | a47f6a6 | 2020-02-06 22:30:54 +0000 | [diff] [blame] | 234 | "(authenticator touch) requirement " |
| 235 | "not met ", key_s, |
djm@openbsd.org | 0fddf29 | 2019-11-25 00:52:46 +0000 | [diff] [blame] | 236 | authctxt->valid ? "" : "invalid user ", |
| 237 | authctxt->user, ssh_remote_ipaddr(ssh), |
| 238 | ssh_remote_port(ssh)); |
| 239 | authenticated = 0; |
| 240 | goto done; |
| 241 | } |
djm@openbsd.org | b7e74ea | 2019-11-25 00:51:37 +0000 | [diff] [blame] | 242 | } |
djm@openbsd.org | 8f57495 | 2017-06-24 06:34:38 +0000 | [diff] [blame] | 243 | auth2_record_key(authctxt, authenticated, key); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 244 | } else { |
djm@openbsd.org | 2788563 | 2017-12-19 00:24:34 +0000 | [diff] [blame] | 245 | debug("%s: test pkalg %s pkblob %s%s%s", |
| 246 | __func__, pkalg, key_s, |
| 247 | ca_s == NULL ? "" : " CA ", |
| 248 | ca_s == NULL ? "" : ca_s); |
| 249 | |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 250 | if ((r = sshpkt_get_end(ssh)) != 0) |
| 251 | fatal("%s: %s", __func__, ssh_err(r)); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 252 | |
djm@openbsd.org | 74287f5 | 2018-07-31 03:10:27 +0000 | [diff] [blame] | 253 | if (!authctxt->valid || authctxt->user == NULL) { |
| 254 | debug2("%s: disabled because of invalid user", |
| 255 | __func__); |
| 256 | goto done; |
| 257 | } |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 258 | /* XXX fake reply and always send PK_OK ? */ |
| 259 | /* |
| 260 | * XXX this allows testing whether a user is allowed |
| 261 | * to login: if you happen to have a valid pubkey this |
| 262 | * message is sent. the message is NEVER sent at all |
| 263 | * if a user is not allowed to login. is this an |
| 264 | * issue? -markus |
| 265 | */ |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 266 | if (PRIVSEP(user_key_allowed(ssh, pw, key, 0, NULL))) { |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 267 | if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_PK_OK)) |
| 268 | != 0 || |
| 269 | (r = sshpkt_put_cstring(ssh, pkalg)) != 0 || |
| 270 | (r = sshpkt_put_string(ssh, pkblob, blen)) != 0 || |
markus@openbsd.org | 394a842 | 2018-07-11 18:55:11 +0000 | [diff] [blame] | 271 | (r = sshpkt_send(ssh)) != 0 || |
| 272 | (r = ssh_packet_write_wait(ssh)) != 0) |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 273 | fatal("%s: %s", __func__, ssh_err(r)); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 274 | authctxt->postponed = 1; |
| 275 | } |
| 276 | } |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 277 | done: |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 278 | if (authenticated == 1 && auth_activate_options(ssh, authopts) != 0) { |
| 279 | debug("%s: key options inconsistent with existing", __func__); |
| 280 | authenticated = 0; |
| 281 | } |
djm@openbsd.org | ebacd37 | 2016-01-27 00:53:12 +0000 | [diff] [blame] | 282 | debug2("%s: authenticated %d pkalg %s", __func__, authenticated, pkalg); |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 283 | |
djm@openbsd.org | 7fef173 | 2018-08-23 03:01:08 +0000 | [diff] [blame] | 284 | sshbuf_free(b); |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 285 | sshauthopt_free(authopts); |
djm@openbsd.org | 8f57495 | 2017-06-24 06:34:38 +0000 | [diff] [blame] | 286 | sshkey_free(key); |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 287 | free(userstyle); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 288 | free(pkalg); |
| 289 | free(pkblob); |
djm@openbsd.org | 2788563 | 2017-12-19 00:24:34 +0000 | [diff] [blame] | 290 | free(key_s); |
| 291 | free(ca_s); |
djm@openbsd.org | 74287f5 | 2018-07-31 03:10:27 +0000 | [diff] [blame] | 292 | free(sig); |
djm@openbsd.org | b7e74ea | 2019-11-25 00:51:37 +0000 | [diff] [blame] | 293 | sshkey_sig_details_free(sig_details); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 294 | return authenticated; |
| 295 | } |
| 296 | |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 297 | static int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 298 | match_principals_option(const char *principal_list, struct sshkey_cert *cert) |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 299 | { |
| 300 | char *result; |
| 301 | u_int i; |
| 302 | |
| 303 | /* XXX percent_expand() sequences for authorized_principals? */ |
| 304 | |
| 305 | for (i = 0; i < cert->nprincipals; i++) { |
| 306 | if ((result = match_list(cert->principals[i], |
| 307 | principal_list, NULL)) != NULL) { |
| 308 | debug3("matched principal from key options \"%.100s\"", |
| 309 | result); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 310 | free(result); |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 311 | return 1; |
| 312 | } |
| 313 | } |
| 314 | return 0; |
| 315 | } |
| 316 | |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 317 | /* |
| 318 | * Process a single authorized_principals format line. Returns 0 and sets |
| 319 | * authoptsp is principal is authorised, -1 otherwise. "loc" is used as a |
| 320 | * log preamble for file/line information. |
| 321 | */ |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 322 | static int |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 323 | check_principals_line(struct ssh *ssh, char *cp, const struct sshkey_cert *cert, |
| 324 | const char *loc, struct sshauthopt **authoptsp) |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 325 | { |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 326 | u_int i, found = 0; |
| 327 | char *ep, *line_opts; |
| 328 | const char *reason = NULL; |
| 329 | struct sshauthopt *opts = NULL; |
| 330 | |
| 331 | if (authoptsp != NULL) |
| 332 | *authoptsp = NULL; |
| 333 | |
| 334 | /* Trim trailing whitespace. */ |
| 335 | ep = cp + strlen(cp) - 1; |
| 336 | while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t')) |
| 337 | *ep-- = '\0'; |
| 338 | |
| 339 | /* |
| 340 | * If the line has internal whitespace then assume it has |
| 341 | * key options. |
| 342 | */ |
| 343 | line_opts = NULL; |
| 344 | if ((ep = strrchr(cp, ' ')) != NULL || |
| 345 | (ep = strrchr(cp, '\t')) != NULL) { |
| 346 | for (; *ep == ' ' || *ep == '\t'; ep++) |
| 347 | ; |
| 348 | line_opts = cp; |
| 349 | cp = ep; |
| 350 | } |
| 351 | if ((opts = sshauthopt_parse(line_opts, &reason)) == NULL) { |
| 352 | debug("%s: bad principals options: %s", loc, reason); |
| 353 | auth_debug_add("%s: bad principals options: %s", loc, reason); |
| 354 | return -1; |
| 355 | } |
| 356 | /* Check principals in cert against those on line */ |
| 357 | for (i = 0; i < cert->nprincipals; i++) { |
| 358 | if (strcmp(cp, cert->principals[i]) != 0) |
| 359 | continue; |
| 360 | debug3("%s: matched principal \"%.100s\"", |
| 361 | loc, cert->principals[i]); |
| 362 | found = 1; |
| 363 | } |
| 364 | if (found && authoptsp != NULL) { |
| 365 | *authoptsp = opts; |
| 366 | opts = NULL; |
| 367 | } |
| 368 | sshauthopt_free(opts); |
| 369 | return found ? 0 : -1; |
| 370 | } |
| 371 | |
| 372 | static int |
| 373 | process_principals(struct ssh *ssh, FILE *f, const char *file, |
| 374 | const struct sshkey_cert *cert, struct sshauthopt **authoptsp) |
| 375 | { |
markus@openbsd.org | 7f90635 | 2018-06-06 18:29:18 +0000 | [diff] [blame] | 376 | char loc[256], *line = NULL, *cp, *ep; |
| 377 | size_t linesize = 0; |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 378 | u_long linenum = 0; |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 379 | u_int found_principal = 0; |
| 380 | |
| 381 | if (authoptsp != NULL) |
| 382 | *authoptsp = NULL; |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 383 | |
markus@openbsd.org | 7f90635 | 2018-06-06 18:29:18 +0000 | [diff] [blame] | 384 | while (getline(&line, &linesize, f) != -1) { |
| 385 | linenum++; |
djm@openbsd.org | 52763dd | 2017-01-30 01:03:00 +0000 | [diff] [blame] | 386 | /* Always consume entire input */ |
| 387 | if (found_principal) |
| 388 | continue; |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 389 | |
Damien Miller | 6018a36 | 2010-07-02 13:35:19 +1000 | [diff] [blame] | 390 | /* Skip leading whitespace. */ |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 391 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) |
| 392 | ; |
Damien Miller | 6018a36 | 2010-07-02 13:35:19 +1000 | [diff] [blame] | 393 | /* Skip blank and comment lines. */ |
| 394 | if ((ep = strchr(cp, '#')) != NULL) |
| 395 | *ep = '\0'; |
| 396 | if (!*cp || *cp == '\n') |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 397 | continue; |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 398 | |
| 399 | snprintf(loc, sizeof(loc), "%.200s:%lu", file, linenum); |
| 400 | if (check_principals_line(ssh, cp, cert, loc, authoptsp) == 0) |
| 401 | found_principal = 1; |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 402 | } |
markus@openbsd.org | 7f90635 | 2018-06-06 18:29:18 +0000 | [diff] [blame] | 403 | free(line); |
djm@openbsd.org | 52763dd | 2017-01-30 01:03:00 +0000 | [diff] [blame] | 404 | return found_principal; |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 405 | } |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 406 | |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 407 | /* XXX remove pw args here and elsewhere once ssh->authctxt is guaranteed */ |
| 408 | |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 409 | static int |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 410 | match_principals_file(struct ssh *ssh, struct passwd *pw, char *file, |
| 411 | struct sshkey_cert *cert, struct sshauthopt **authoptsp) |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 412 | { |
| 413 | FILE *f; |
| 414 | int success; |
| 415 | |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 416 | if (authoptsp != NULL) |
| 417 | *authoptsp = NULL; |
| 418 | |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 419 | temporarily_use_uid(pw); |
| 420 | debug("trying authorized principals file %s", file); |
| 421 | if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) { |
| 422 | restore_uid(); |
| 423 | return 0; |
| 424 | } |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 425 | success = process_principals(ssh, f, file, cert, authoptsp); |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 426 | fclose(f); |
| 427 | restore_uid(); |
| 428 | return success; |
| 429 | } |
| 430 | |
| 431 | /* |
| 432 | * Checks whether principal is allowed in output of command. |
| 433 | * returns 1 if the principal is allowed or 0 otherwise. |
| 434 | */ |
| 435 | static int |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 436 | match_principals_command(struct ssh *ssh, struct passwd *user_pw, |
| 437 | const struct sshkey *key, struct sshauthopt **authoptsp) |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 438 | { |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 439 | struct passwd *runas_pw = NULL; |
djm@openbsd.org | e7907c1 | 2016-09-14 05:42:25 +0000 | [diff] [blame] | 440 | const struct sshkey_cert *cert = key->cert; |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 441 | FILE *f = NULL; |
djm@openbsd.org | e7907c1 | 2016-09-14 05:42:25 +0000 | [diff] [blame] | 442 | int r, ok, found_principal = 0; |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 443 | int i, ac = 0, uid_swapped = 0; |
| 444 | pid_t pid; |
| 445 | char *tmp, *username = NULL, *command = NULL, **av = NULL; |
djm@openbsd.org | e7907c1 | 2016-09-14 05:42:25 +0000 | [diff] [blame] | 446 | char *ca_fp = NULL, *key_fp = NULL, *catext = NULL, *keytext = NULL; |
djm@openbsd.org | 3061529 | 2019-05-20 00:25:55 +0000 | [diff] [blame] | 447 | char serial_s[32], uidstr[32]; |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 448 | void (*osigchld)(int); |
| 449 | |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 450 | if (authoptsp != NULL) |
| 451 | *authoptsp = NULL; |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 452 | if (options.authorized_principals_command == NULL) |
| 453 | return 0; |
| 454 | if (options.authorized_principals_command_user == NULL) { |
| 455 | error("No user for AuthorizedPrincipalsCommand specified, " |
| 456 | "skipping"); |
| 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | /* |
| 461 | * NB. all returns later this function should go via "out" to |
| 462 | * ensure the original SIGCHLD handler is restored properly. |
| 463 | */ |
dtucker@openbsd.org | 3bf2a6a | 2020-01-23 07:10:22 +0000 | [diff] [blame] | 464 | osigchld = ssh_signal(SIGCHLD, SIG_DFL); |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 465 | |
| 466 | /* Prepare and verify the user for the command */ |
| 467 | username = percent_expand(options.authorized_principals_command_user, |
| 468 | "u", user_pw->pw_name, (char *)NULL); |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 469 | runas_pw = getpwnam(username); |
| 470 | if (runas_pw == NULL) { |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 471 | error("AuthorizedPrincipalsCommandUser \"%s\" not found: %s", |
| 472 | username, strerror(errno)); |
| 473 | goto out; |
| 474 | } |
| 475 | |
| 476 | /* Turn the command into an argument vector */ |
djm@openbsd.org | de4ae07 | 2017-08-18 05:36:45 +0000 | [diff] [blame] | 477 | if (argv_split(options.authorized_principals_command, &ac, &av) != 0) { |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 478 | error("AuthorizedPrincipalsCommand \"%s\" contains " |
djm@openbsd.org | 4cd6b12 | 2019-06-21 03:19:59 +0000 | [diff] [blame] | 479 | "invalid quotes", options.authorized_principals_command); |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 480 | goto out; |
| 481 | } |
| 482 | if (ac == 0) { |
| 483 | error("AuthorizedPrincipalsCommand \"%s\" yielded no arguments", |
djm@openbsd.org | 4cd6b12 | 2019-06-21 03:19:59 +0000 | [diff] [blame] | 484 | options.authorized_principals_command); |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 485 | goto out; |
| 486 | } |
djm@openbsd.org | e7907c1 | 2016-09-14 05:42:25 +0000 | [diff] [blame] | 487 | if ((ca_fp = sshkey_fingerprint(cert->signature_key, |
| 488 | options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) { |
| 489 | error("%s: sshkey_fingerprint failed", __func__); |
| 490 | goto out; |
| 491 | } |
djm@openbsd.org | 00df97f | 2016-09-14 20:11:26 +0000 | [diff] [blame] | 492 | if ((key_fp = sshkey_fingerprint(key, |
djm@openbsd.org | e7907c1 | 2016-09-14 05:42:25 +0000 | [diff] [blame] | 493 | options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) { |
| 494 | error("%s: sshkey_fingerprint failed", __func__); |
| 495 | goto out; |
| 496 | } |
| 497 | if ((r = sshkey_to_base64(cert->signature_key, &catext)) != 0) { |
| 498 | error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r)); |
| 499 | goto out; |
| 500 | } |
| 501 | if ((r = sshkey_to_base64(key, &keytext)) != 0) { |
| 502 | error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r)); |
| 503 | goto out; |
| 504 | } |
djm@openbsd.org | f83a0cf | 2016-09-21 17:44:20 +0000 | [diff] [blame] | 505 | snprintf(serial_s, sizeof(serial_s), "%llu", |
| 506 | (unsigned long long)cert->serial); |
djm@openbsd.org | 9c935dd | 2018-06-01 03:33:53 +0000 | [diff] [blame] | 507 | snprintf(uidstr, sizeof(uidstr), "%llu", |
| 508 | (unsigned long long)user_pw->pw_uid); |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 509 | for (i = 1; i < ac; i++) { |
| 510 | tmp = percent_expand(av[i], |
djm@openbsd.org | 9c935dd | 2018-06-01 03:33:53 +0000 | [diff] [blame] | 511 | "U", uidstr, |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 512 | "u", user_pw->pw_name, |
| 513 | "h", user_pw->pw_dir, |
djm@openbsd.org | e7907c1 | 2016-09-14 05:42:25 +0000 | [diff] [blame] | 514 | "t", sshkey_ssh_name(key), |
| 515 | "T", sshkey_ssh_name(cert->signature_key), |
| 516 | "f", key_fp, |
| 517 | "F", ca_fp, |
| 518 | "k", keytext, |
| 519 | "K", catext, |
djm@openbsd.org | bfa9d96 | 2016-09-21 01:34:45 +0000 | [diff] [blame] | 520 | "i", cert->key_id, |
| 521 | "s", serial_s, |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 522 | (char *)NULL); |
| 523 | if (tmp == NULL) |
| 524 | fatal("%s: percent_expand failed", __func__); |
| 525 | free(av[i]); |
| 526 | av[i] = tmp; |
| 527 | } |
| 528 | /* Prepare a printable command for logs, etc. */ |
djm@openbsd.org | de4ae07 | 2017-08-18 05:36:45 +0000 | [diff] [blame] | 529 | command = argv_assemble(ac, av); |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 530 | |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 531 | if ((pid = subprocess("AuthorizedPrincipalsCommand", runas_pw, command, |
djm@openbsd.org | de4ae07 | 2017-08-18 05:36:45 +0000 | [diff] [blame] | 532 | ac, av, &f, |
| 533 | SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD)) == 0) |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 534 | goto out; |
| 535 | |
| 536 | uid_swapped = 1; |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 537 | temporarily_use_uid(runas_pw); |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 538 | |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 539 | ok = process_principals(ssh, f, "(command)", cert, authoptsp); |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 540 | |
djm@openbsd.org | ddd3d34 | 2016-12-30 22:08:02 +0000 | [diff] [blame] | 541 | fclose(f); |
| 542 | f = NULL; |
| 543 | |
djm@openbsd.org | b074c3c | 2017-08-18 05:48:04 +0000 | [diff] [blame] | 544 | if (exited_cleanly(pid, "AuthorizedPrincipalsCommand", command, 0) != 0) |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 545 | goto out; |
| 546 | |
| 547 | /* Read completed successfully */ |
| 548 | found_principal = ok; |
| 549 | out: |
| 550 | if (f != NULL) |
| 551 | fclose(f); |
dtucker@openbsd.org | 3bf2a6a | 2020-01-23 07:10:22 +0000 | [diff] [blame] | 552 | ssh_signal(SIGCHLD, osigchld); |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 553 | for (i = 0; i < ac; i++) |
| 554 | free(av[i]); |
| 555 | free(av); |
| 556 | if (uid_swapped) |
| 557 | restore_uid(); |
| 558 | free(command); |
| 559 | free(username); |
djm@openbsd.org | e7907c1 | 2016-09-14 05:42:25 +0000 | [diff] [blame] | 560 | free(ca_fp); |
| 561 | free(key_fp); |
| 562 | free(catext); |
| 563 | free(keytext); |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 564 | return found_principal; |
| 565 | } |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 566 | |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 567 | /* |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 568 | * Check a single line of an authorized_keys-format file. Returns 0 if key |
| 569 | * matches, -1 otherwise. Will return key/cert options via *authoptsp |
| 570 | * on success. "loc" is used as file/line location in log messages. |
| 571 | */ |
| 572 | static int |
| 573 | check_authkey_line(struct ssh *ssh, struct passwd *pw, struct sshkey *key, |
| 574 | char *cp, const char *loc, struct sshauthopt **authoptsp) |
| 575 | { |
| 576 | int want_keytype = sshkey_is_cert(key) ? KEY_UNSPEC : key->type; |
| 577 | struct sshkey *found = NULL; |
| 578 | struct sshauthopt *keyopts = NULL, *certopts = NULL, *finalopts = NULL; |
| 579 | char *key_options = NULL, *fp = NULL; |
| 580 | const char *reason = NULL; |
| 581 | int ret = -1; |
| 582 | |
| 583 | if (authoptsp != NULL) |
| 584 | *authoptsp = NULL; |
| 585 | |
| 586 | if ((found = sshkey_new(want_keytype)) == NULL) { |
| 587 | debug3("%s: keytype %d failed", __func__, want_keytype); |
| 588 | goto out; |
| 589 | } |
| 590 | |
| 591 | /* XXX djm: peek at key type in line and skip if unwanted */ |
| 592 | |
| 593 | if (sshkey_read(found, &cp) != 0) { |
| 594 | /* no key? check for options */ |
| 595 | debug2("%s: check options: '%s'", loc, cp); |
| 596 | key_options = cp; |
djm@openbsd.org | dd8002f | 2019-09-03 08:30:47 +0000 | [diff] [blame] | 597 | if (sshkey_advance_past_options(&cp) != 0) { |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 598 | reason = "invalid key option string"; |
| 599 | goto fail_reason; |
| 600 | } |
| 601 | skip_space(&cp); |
| 602 | if (sshkey_read(found, &cp) != 0) { |
| 603 | /* still no key? advance to next line*/ |
| 604 | debug2("%s: advance: '%s'", loc, cp); |
| 605 | goto out; |
| 606 | } |
| 607 | } |
| 608 | /* Parse key options now; we need to know if this is a CA key */ |
| 609 | if ((keyopts = sshauthopt_parse(key_options, &reason)) == NULL) { |
| 610 | debug("%s: bad key options: %s", loc, reason); |
| 611 | auth_debug_add("%s: bad key options: %s", loc, reason); |
| 612 | goto out; |
| 613 | } |
| 614 | /* Ignore keys that don't match or incorrectly marked as CAs */ |
| 615 | if (sshkey_is_cert(key)) { |
| 616 | /* Certificate; check signature key against CA */ |
| 617 | if (!sshkey_equal(found, key->cert->signature_key) || |
| 618 | !keyopts->cert_authority) |
| 619 | goto out; |
| 620 | } else { |
| 621 | /* Plain key: check it against key found in file */ |
| 622 | if (!sshkey_equal(found, key) || keyopts->cert_authority) |
| 623 | goto out; |
| 624 | } |
| 625 | |
| 626 | /* We have a candidate key, perform authorisation checks */ |
| 627 | if ((fp = sshkey_fingerprint(found, |
| 628 | options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) |
| 629 | fatal("%s: fingerprint failed", __func__); |
| 630 | |
| 631 | debug("%s: matching %s found: %s %s", loc, |
| 632 | sshkey_is_cert(key) ? "CA" : "key", sshkey_type(found), fp); |
| 633 | |
| 634 | if (auth_authorise_keyopts(ssh, pw, keyopts, |
| 635 | sshkey_is_cert(key), loc) != 0) { |
| 636 | reason = "Refused by key options"; |
| 637 | goto fail_reason; |
| 638 | } |
| 639 | /* That's all we need for plain keys. */ |
| 640 | if (!sshkey_is_cert(key)) { |
| 641 | verbose("Accepted key %s %s found at %s", |
| 642 | sshkey_type(found), fp, loc); |
| 643 | finalopts = keyopts; |
| 644 | keyopts = NULL; |
| 645 | goto success; |
| 646 | } |
| 647 | |
| 648 | /* |
| 649 | * Additional authorisation for certificates. |
| 650 | */ |
| 651 | |
| 652 | /* Parse and check options present in certificate */ |
| 653 | if ((certopts = sshauthopt_from_cert(key)) == NULL) { |
| 654 | reason = "Invalid certificate options"; |
| 655 | goto fail_reason; |
| 656 | } |
| 657 | if (auth_authorise_keyopts(ssh, pw, certopts, 0, loc) != 0) { |
| 658 | reason = "Refused by certificate options"; |
| 659 | goto fail_reason; |
| 660 | } |
| 661 | if ((finalopts = sshauthopt_merge(keyopts, certopts, &reason)) == NULL) |
| 662 | goto fail_reason; |
| 663 | |
| 664 | /* |
| 665 | * If the user has specified a list of principals as |
| 666 | * a key option, then prefer that list to matching |
| 667 | * their username in the certificate principals list. |
| 668 | */ |
| 669 | if (keyopts->cert_principals != NULL && |
| 670 | !match_principals_option(keyopts->cert_principals, key->cert)) { |
| 671 | reason = "Certificate does not contain an authorized principal"; |
| 672 | goto fail_reason; |
| 673 | } |
| 674 | if (sshkey_cert_check_authority(key, 0, 0, |
| 675 | keyopts->cert_principals == NULL ? pw->pw_name : NULL, &reason) != 0) |
| 676 | goto fail_reason; |
| 677 | |
| 678 | verbose("Accepted certificate ID \"%s\" (serial %llu) " |
| 679 | "signed by CA %s %s found at %s", |
| 680 | key->cert->key_id, |
| 681 | (unsigned long long)key->cert->serial, |
| 682 | sshkey_type(found), fp, loc); |
| 683 | |
| 684 | success: |
| 685 | if (finalopts == NULL) |
| 686 | fatal("%s: internal error: missing options", __func__); |
| 687 | if (authoptsp != NULL) { |
| 688 | *authoptsp = finalopts; |
| 689 | finalopts = NULL; |
| 690 | } |
| 691 | /* success */ |
| 692 | ret = 0; |
| 693 | goto out; |
| 694 | |
| 695 | fail_reason: |
| 696 | error("%s", reason); |
| 697 | auth_debug_add("%s", reason); |
| 698 | out: |
| 699 | free(fp); |
| 700 | sshauthopt_free(keyopts); |
| 701 | sshauthopt_free(certopts); |
| 702 | sshauthopt_free(finalopts); |
| 703 | sshkey_free(found); |
| 704 | return ret; |
| 705 | } |
| 706 | |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 707 | /* |
| 708 | * Checks whether key is allowed in authorized_keys-format file, |
| 709 | * returns 1 if the key is allowed or 0 otherwise. |
| 710 | */ |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 711 | static int |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 712 | check_authkeys_file(struct ssh *ssh, struct passwd *pw, FILE *f, |
| 713 | char *file, struct sshkey *key, struct sshauthopt **authoptsp) |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 714 | { |
markus@openbsd.org | 7f90635 | 2018-06-06 18:29:18 +0000 | [diff] [blame] | 715 | char *cp, *line = NULL, loc[256]; |
| 716 | size_t linesize = 0; |
Darren Tucker | 33c787f | 2008-07-02 22:37:30 +1000 | [diff] [blame] | 717 | int found_key = 0; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 718 | u_long linenum = 0; |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 719 | |
| 720 | if (authoptsp != NULL) |
| 721 | *authoptsp = NULL; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 722 | |
markus@openbsd.org | 7f90635 | 2018-06-06 18:29:18 +0000 | [diff] [blame] | 723 | while (getline(&line, &linesize, f) != -1) { |
| 724 | linenum++; |
djm@openbsd.org | abd5966 | 2017-09-07 23:48:09 +0000 | [diff] [blame] | 725 | /* Always consume entire file */ |
djm@openbsd.org | 52763dd | 2017-01-30 01:03:00 +0000 | [diff] [blame] | 726 | if (found_key) |
| 727 | continue; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 728 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 729 | /* Skip leading whitespace, empty and comment lines. */ |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 730 | cp = line; |
| 731 | skip_space(&cp); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 732 | if (!*cp || *cp == '\n' || *cp == '#') |
| 733 | continue; |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 734 | snprintf(loc, sizeof(loc), "%.200s:%lu", file, linenum); |
| 735 | if (check_authkey_line(ssh, pw, key, cp, loc, authoptsp) == 0) |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 736 | found_key = 1; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 737 | } |
markus@openbsd.org | 7f90635 | 2018-06-06 18:29:18 +0000 | [diff] [blame] | 738 | free(line); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 739 | return found_key; |
| 740 | } |
| 741 | |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 742 | /* Authenticate a certificate key against TrustedUserCAKeys */ |
| 743 | static int |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 744 | user_cert_trusted_ca(struct ssh *ssh, struct passwd *pw, struct sshkey *key, |
| 745 | struct sshauthopt **authoptsp) |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 746 | { |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 747 | char *ca_fp, *principals_file = NULL; |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 748 | const char *reason; |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 749 | struct sshauthopt *principals_opts = NULL, *cert_opts = NULL; |
| 750 | struct sshauthopt *final_opts = NULL; |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 751 | int r, ret = 0, found_principal = 0, use_authorized_principals; |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 752 | |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 753 | if (authoptsp != NULL) |
| 754 | *authoptsp = NULL; |
| 755 | |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 756 | if (!sshkey_is_cert(key) || options.trusted_user_ca_keys == NULL) |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 757 | return 0; |
| 758 | |
djm@openbsd.org | 9ce86c9 | 2015-01-28 22:36:00 +0000 | [diff] [blame] | 759 | if ((ca_fp = sshkey_fingerprint(key->cert->signature_key, |
| 760 | options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) |
| 761 | return 0; |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 762 | |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 763 | if ((r = sshkey_in_file(key->cert->signature_key, |
| 764 | options.trusted_user_ca_keys, 1, 0)) != 0) { |
| 765 | debug2("%s: CA %s %s is not listed in %s: %s", __func__, |
| 766 | sshkey_type(key->cert->signature_key), ca_fp, |
| 767 | options.trusted_user_ca_keys, ssh_err(r)); |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 768 | goto out; |
| 769 | } |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 770 | /* |
| 771 | * If AuthorizedPrincipals is in use, then compare the certificate |
| 772 | * principals against the names in that file rather than matching |
| 773 | * against the username. |
| 774 | */ |
| 775 | if ((principals_file = authorized_principals_file(pw)) != NULL) { |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 776 | if (match_principals_file(ssh, pw, principals_file, |
| 777 | key->cert, &principals_opts)) |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 778 | found_principal = 1; |
| 779 | } |
| 780 | /* Try querying command if specified */ |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 781 | if (!found_principal && match_principals_command(ssh, pw, key, |
| 782 | &principals_opts)) |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 783 | found_principal = 1; |
jsing@openbsd.org | 596dbca | 2015-06-15 18:44:22 +0000 | [diff] [blame] | 784 | /* If principals file or command is specified, then require a match */ |
| 785 | use_authorized_principals = principals_file != NULL || |
| 786 | options.authorized_principals_command != NULL; |
| 787 | if (!found_principal && use_authorized_principals) { |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 788 | reason = "Certificate does not contain an authorized principal"; |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 789 | goto fail_reason; |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 790 | } |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 791 | if (use_authorized_principals && principals_opts == NULL) |
| 792 | fatal("%s: internal error: missing principals_opts", __func__); |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 793 | if (sshkey_cert_check_authority(key, 0, 1, |
jsing@openbsd.org | 596dbca | 2015-06-15 18:44:22 +0000 | [diff] [blame] | 794 | use_authorized_principals ? NULL : pw->pw_name, &reason) != 0) |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 795 | goto fail_reason; |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 796 | |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 797 | /* Check authority from options in key and from principals file/cmd */ |
| 798 | if ((cert_opts = sshauthopt_from_cert(key)) == NULL) { |
| 799 | reason = "Invalid certificate options"; |
| 800 | goto fail_reason; |
| 801 | } |
| 802 | if (auth_authorise_keyopts(ssh, pw, cert_opts, 0, "cert") != 0) { |
| 803 | reason = "Refused by certificate options"; |
| 804 | goto fail_reason; |
| 805 | } |
| 806 | if (principals_opts == NULL) { |
| 807 | final_opts = cert_opts; |
| 808 | cert_opts = NULL; |
| 809 | } else { |
| 810 | if (auth_authorise_keyopts(ssh, pw, principals_opts, 0, |
| 811 | "principals") != 0) { |
| 812 | reason = "Refused by certificate principals options"; |
| 813 | goto fail_reason; |
| 814 | } |
| 815 | if ((final_opts = sshauthopt_merge(principals_opts, |
| 816 | cert_opts, &reason)) == NULL) { |
| 817 | fail_reason: |
| 818 | error("%s", reason); |
| 819 | auth_debug_add("%s", reason); |
| 820 | goto out; |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | /* Success */ |
djm@openbsd.org | 63d1881 | 2015-10-27 01:44:45 +0000 | [diff] [blame] | 825 | verbose("Accepted certificate ID \"%s\" (serial %llu) signed by " |
| 826 | "%s CA %s via %s", key->cert->key_id, |
| 827 | (unsigned long long)key->cert->serial, |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 828 | sshkey_type(key->cert->signature_key), ca_fp, |
Damien Miller | e513a91 | 2010-03-22 05:51:21 +1100 | [diff] [blame] | 829 | options.trusted_user_ca_keys); |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 830 | if (authoptsp != NULL) { |
| 831 | *authoptsp = final_opts; |
| 832 | final_opts = NULL; |
| 833 | } |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 834 | ret = 1; |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 835 | out: |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 836 | sshauthopt_free(principals_opts); |
| 837 | sshauthopt_free(cert_opts); |
| 838 | sshauthopt_free(final_opts); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 839 | free(principals_file); |
| 840 | free(ca_fp); |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 841 | return ret; |
| 842 | } |
| 843 | |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 844 | /* |
| 845 | * Checks whether key is allowed in file. |
| 846 | * returns 1 if the key is allowed or 0 otherwise. |
| 847 | */ |
| 848 | static int |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 849 | user_key_allowed2(struct ssh *ssh, struct passwd *pw, struct sshkey *key, |
| 850 | char *file, struct sshauthopt **authoptsp) |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 851 | { |
| 852 | FILE *f; |
| 853 | int found_key = 0; |
| 854 | |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 855 | if (authoptsp != NULL) |
| 856 | *authoptsp = NULL; |
| 857 | |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 858 | /* Temporarily use the user's uid. */ |
| 859 | temporarily_use_uid(pw); |
| 860 | |
| 861 | debug("trying public key file %s", file); |
| 862 | if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) { |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 863 | found_key = check_authkeys_file(ssh, pw, f, file, |
| 864 | key, authoptsp); |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 865 | fclose(f); |
| 866 | } |
| 867 | |
| 868 | restore_uid(); |
| 869 | return found_key; |
| 870 | } |
| 871 | |
| 872 | /* |
| 873 | * Checks whether key is allowed in output of command. |
| 874 | * returns 1 if the key is allowed or 0 otherwise. |
| 875 | */ |
| 876 | static int |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 877 | user_key_command_allowed2(struct ssh *ssh, struct passwd *user_pw, |
| 878 | struct sshkey *key, struct sshauthopt **authoptsp) |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 879 | { |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 880 | struct passwd *runas_pw = NULL; |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 881 | FILE *f = NULL; |
| 882 | int r, ok, found_key = 0; |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 883 | int i, uid_swapped = 0, ac = 0; |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 884 | pid_t pid; |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 885 | char *username = NULL, *key_fp = NULL, *keytext = NULL; |
djm@openbsd.org | 9c935dd | 2018-06-01 03:33:53 +0000 | [diff] [blame] | 886 | char uidstr[32], *tmp, *command = NULL, **av = NULL; |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 887 | void (*osigchld)(int); |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 888 | |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 889 | if (authoptsp != NULL) |
| 890 | *authoptsp = NULL; |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 891 | if (options.authorized_keys_command == NULL) |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 892 | return 0; |
Damien Miller | d0d1099 | 2012-11-04 22:23:14 +1100 | [diff] [blame] | 893 | if (options.authorized_keys_command_user == NULL) { |
| 894 | error("No user for AuthorizedKeysCommand specified, skipping"); |
| 895 | return 0; |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 896 | } |
| 897 | |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 898 | /* |
| 899 | * NB. all returns later this function should go via "out" to |
| 900 | * ensure the original SIGCHLD handler is restored properly. |
| 901 | */ |
dtucker@openbsd.org | 3bf2a6a | 2020-01-23 07:10:22 +0000 | [diff] [blame] | 902 | osigchld = ssh_signal(SIGCHLD, SIG_DFL); |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 903 | |
| 904 | /* Prepare and verify the user for the command */ |
Damien Miller | d0d1099 | 2012-11-04 22:23:14 +1100 | [diff] [blame] | 905 | username = percent_expand(options.authorized_keys_command_user, |
| 906 | "u", user_pw->pw_name, (char *)NULL); |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 907 | runas_pw = getpwnam(username); |
| 908 | if (runas_pw == NULL) { |
Damien Miller | 4018dc0 | 2013-02-15 10:28:55 +1100 | [diff] [blame] | 909 | error("AuthorizedKeysCommandUser \"%s\" not found: %s", |
| 910 | username, strerror(errno)); |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 911 | goto out; |
| 912 | } |
| 913 | |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 914 | /* Prepare AuthorizedKeysCommand */ |
| 915 | if ((key_fp = sshkey_fingerprint(key, options.fingerprint_hash, |
| 916 | SSH_FP_DEFAULT)) == NULL) { |
| 917 | error("%s: sshkey_fingerprint failed", __func__); |
| 918 | goto out; |
| 919 | } |
| 920 | if ((r = sshkey_to_base64(key, &keytext)) != 0) { |
| 921 | error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r)); |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 922 | goto out; |
| 923 | } |
| 924 | |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 925 | /* Turn the command into an argument vector */ |
djm@openbsd.org | de4ae07 | 2017-08-18 05:36:45 +0000 | [diff] [blame] | 926 | if (argv_split(options.authorized_keys_command, &ac, &av) != 0) { |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 927 | error("AuthorizedKeysCommand \"%s\" contains invalid quotes", |
| 928 | command); |
| 929 | goto out; |
| 930 | } |
| 931 | if (ac == 0) { |
| 932 | error("AuthorizedKeysCommand \"%s\" yielded no arguments", |
| 933 | command); |
| 934 | goto out; |
| 935 | } |
djm@openbsd.org | 9c935dd | 2018-06-01 03:33:53 +0000 | [diff] [blame] | 936 | snprintf(uidstr, sizeof(uidstr), "%llu", |
| 937 | (unsigned long long)user_pw->pw_uid); |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 938 | for (i = 1; i < ac; i++) { |
| 939 | tmp = percent_expand(av[i], |
djm@openbsd.org | 9c935dd | 2018-06-01 03:33:53 +0000 | [diff] [blame] | 940 | "U", uidstr, |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 941 | "u", user_pw->pw_name, |
| 942 | "h", user_pw->pw_dir, |
| 943 | "t", sshkey_ssh_name(key), |
| 944 | "f", key_fp, |
| 945 | "k", keytext, |
| 946 | (char *)NULL); |
| 947 | if (tmp == NULL) |
| 948 | fatal("%s: percent_expand failed", __func__); |
| 949 | free(av[i]); |
| 950 | av[i] = tmp; |
| 951 | } |
| 952 | /* Prepare a printable command for logs, etc. */ |
djm@openbsd.org | de4ae07 | 2017-08-18 05:36:45 +0000 | [diff] [blame] | 953 | command = argv_assemble(ac, av); |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 954 | |
| 955 | /* |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 956 | * If AuthorizedKeysCommand was run without arguments |
| 957 | * then fall back to the old behaviour of passing the |
| 958 | * target username as a single argument. |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 959 | */ |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 960 | if (ac == 1) { |
| 961 | av = xreallocarray(av, ac + 2, sizeof(*av)); |
| 962 | av[1] = xstrdup(user_pw->pw_name); |
| 963 | av[2] = NULL; |
| 964 | /* Fix up command too, since it is used in log messages */ |
| 965 | free(command); |
| 966 | xasprintf(&command, "%s %s", av[0], av[1]); |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 967 | } |
| 968 | |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 969 | if ((pid = subprocess("AuthorizedKeysCommand", runas_pw, command, |
djm@openbsd.org | de4ae07 | 2017-08-18 05:36:45 +0000 | [diff] [blame] | 970 | ac, av, &f, |
| 971 | SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD)) == 0) |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 972 | goto out; |
| 973 | |
| 974 | uid_swapped = 1; |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 975 | temporarily_use_uid(runas_pw); |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 976 | |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 977 | ok = check_authkeys_file(ssh, user_pw, f, |
| 978 | options.authorized_keys_command, key, authoptsp); |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 979 | |
djm@openbsd.org | ddd3d34 | 2016-12-30 22:08:02 +0000 | [diff] [blame] | 980 | fclose(f); |
| 981 | f = NULL; |
| 982 | |
djm@openbsd.org | b074c3c | 2017-08-18 05:48:04 +0000 | [diff] [blame] | 983 | if (exited_cleanly(pid, "AuthorizedKeysCommand", command, 0) != 0) |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 984 | goto out; |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 985 | |
| 986 | /* Read completed successfully */ |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 987 | found_key = ok; |
| 988 | out: |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 989 | if (f != NULL) |
| 990 | fclose(f); |
dtucker@openbsd.org | 3bf2a6a | 2020-01-23 07:10:22 +0000 | [diff] [blame] | 991 | ssh_signal(SIGCHLD, osigchld); |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 992 | for (i = 0; i < ac; i++) |
| 993 | free(av[i]); |
| 994 | free(av); |
| 995 | if (uid_swapped) |
| 996 | restore_uid(); |
| 997 | free(command); |
| 998 | free(username); |
| 999 | free(key_fp); |
| 1000 | free(keytext); |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 1001 | return found_key; |
| 1002 | } |
| 1003 | |
| 1004 | /* |
| 1005 | * Check whether key authenticates and authorises the user. |
| 1006 | */ |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 1007 | int |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 1008 | user_key_allowed(struct ssh *ssh, struct passwd *pw, struct sshkey *key, |
| 1009 | int auth_attempt, struct sshauthopt **authoptsp) |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 1010 | { |
djm@openbsd.org | c95b90d | 2019-06-14 03:39:59 +0000 | [diff] [blame] | 1011 | u_int success = 0, i; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 1012 | char *file; |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 1013 | struct sshauthopt *opts = NULL; |
djm@openbsd.org | c95b90d | 2019-06-14 03:39:59 +0000 | [diff] [blame] | 1014 | |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 1015 | if (authoptsp != NULL) |
| 1016 | *authoptsp = NULL; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 1017 | |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 1018 | if (auth_key_is_revoked(key)) |
| 1019 | return 0; |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 1020 | if (sshkey_is_cert(key) && |
| 1021 | auth_key_is_revoked(key->cert->signature_key)) |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 1022 | return 0; |
| 1023 | |
djm@openbsd.org | c95b90d | 2019-06-14 03:39:59 +0000 | [diff] [blame] | 1024 | for (i = 0; !success && i < options.num_authkeys_files; i++) { |
| 1025 | if (strcasecmp(options.authorized_keys_files[i], "none") == 0) |
| 1026 | continue; |
| 1027 | file = expand_authorized_keys( |
| 1028 | options.authorized_keys_files[i], pw); |
| 1029 | success = user_key_allowed2(ssh, pw, key, file, &opts); |
| 1030 | free(file); |
| 1031 | if (!success) { |
| 1032 | sshauthopt_free(opts); |
| 1033 | opts = NULL; |
| 1034 | } |
| 1035 | } |
| 1036 | if (success) |
| 1037 | goto out; |
| 1038 | |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 1039 | if ((success = user_cert_trusted_ca(ssh, pw, key, &opts)) != 0) |
| 1040 | goto out; |
| 1041 | sshauthopt_free(opts); |
| 1042 | opts = NULL; |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 1043 | |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 1044 | if ((success = user_key_command_allowed2(ssh, pw, key, &opts)) != 0) |
| 1045 | goto out; |
| 1046 | sshauthopt_free(opts); |
| 1047 | opts = NULL; |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 1048 | |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 1049 | out: |
| 1050 | if (success && authoptsp != NULL) { |
| 1051 | *authoptsp = opts; |
| 1052 | opts = NULL; |
| 1053 | } |
| 1054 | sshauthopt_free(opts); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 1055 | return success; |
| 1056 | } |
| 1057 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 1058 | Authmethod method_pubkey = { |
| 1059 | "publickey", |
| 1060 | userauth_pubkey, |
| 1061 | &options.pubkey_authentication |
| 1062 | }; |