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