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