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