markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 1 | /* $OpenBSD: auth2-pubkey.c,v 1.64 2017/05/30 14:10:53 markus 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> |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 30 | #include <sys/wait.h> |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 31 | |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 32 | #include <errno.h> |
Darren Tucker | 06db584 | 2008-06-13 14:51:28 +1000 | [diff] [blame] | 33 | #include <fcntl.h> |
Darren Tucker | 737f7af | 2012-11-05 17:07:43 +1100 | [diff] [blame] | 34 | #ifdef HAVE_PATHS_H |
| 35 | # include <paths.h> |
| 36 | #endif |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 37 | #include <pwd.h> |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 38 | #include <signal.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 39 | #include <stdio.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 40 | #include <stdarg.h> |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 41 | #include <string.h> |
| 42 | #include <time.h> |
Darren Tucker | d9526a5 | 2008-06-14 09:01:24 +1000 | [diff] [blame] | 43 | #include <unistd.h> |
djm@openbsd.org | f69b69b | 2014-12-22 07:51:30 +0000 | [diff] [blame] | 44 | #include <limits.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 45 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 46 | #include "xmalloc.h" |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 47 | #include "ssh.h" |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 48 | #include "ssh2.h" |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 49 | #include "packet.h" |
| 50 | #include "buffer.h" |
| 51 | #include "log.h" |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 52 | #include "misc.h" |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 53 | #include "servconf.h" |
| 54 | #include "compat.h" |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 55 | #include "sshkey.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 56 | #include "hostfile.h" |
| 57 | #include "auth.h" |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 58 | #include "pathnames.h" |
| 59 | #include "uidswap.h" |
| 60 | #include "auth-options.h" |
| 61 | #include "canohost.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 62 | #ifdef GSSAPI |
| 63 | #include "ssh-gss.h" |
| 64 | #endif |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 65 | #include "monitor_wrap.h" |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 66 | #include "authfile.h" |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 67 | #include "match.h" |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 68 | #include "ssherr.h" |
| 69 | #include "channels.h" /* XXX for session.h */ |
| 70 | #include "session.h" /* XXX for child_set_env(); refactor? */ |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 71 | |
| 72 | /* import */ |
| 73 | extern ServerOptions options; |
| 74 | extern u_char *session_id2; |
Darren Tucker | 502d384 | 2003-06-28 12:38:01 +1000 | [diff] [blame] | 75 | extern u_int session_id2_len; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 76 | |
| 77 | static int |
| 78 | userauth_pubkey(Authctxt *authctxt) |
| 79 | { |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 80 | struct ssh *ssh = active_state; /* XXX */ |
| 81 | struct sshbuf *b; |
markus@openbsd.org | 54d90ac | 2017-05-30 08:52:19 +0000 | [diff] [blame] | 82 | struct sshkey *key = NULL; |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 83 | char *pkalg, *userstyle = NULL, *fp = NULL; |
| 84 | u_char *pkblob, *sig, have_sig; |
| 85 | size_t blen, slen; |
| 86 | int r, pktype; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 87 | int authenticated = 0; |
| 88 | |
| 89 | if (!authctxt->valid) { |
djm@openbsd.org | ebacd37 | 2016-01-27 00:53:12 +0000 | [diff] [blame] | 90 | debug2("%s: disabled because of invalid user", __func__); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 91 | return 0; |
| 92 | } |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 93 | if ((r = sshpkt_get_u8(ssh, &have_sig)) != 0) |
| 94 | fatal("%s: sshpkt_get_u8 failed: %s", __func__, ssh_err(r)); |
| 95 | if (ssh->compat & SSH_BUG_PKAUTH) { |
djm@openbsd.org | ebacd37 | 2016-01-27 00:53:12 +0000 | [diff] [blame] | 96 | debug2("%s: SSH_BUG_PKAUTH", __func__); |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 97 | if ((b = sshbuf_new()) == NULL) |
| 98 | fatal("%s: sshbuf_new failed", __func__); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 99 | /* no explicit pkalg given */ |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 100 | /* so we have to extract the pkalg from the pkblob */ |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 101 | /* XXX use sshbuf_from() */ |
| 102 | if ((r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0 || |
| 103 | (r = sshbuf_put(b, pkblob, blen)) != 0 || |
| 104 | (r = sshbuf_get_cstring(b, &pkalg, NULL)) != 0) |
| 105 | fatal("%s: failed: %s", __func__, ssh_err(r)); |
| 106 | sshbuf_free(b); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 107 | } else { |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 108 | if ((r = sshpkt_get_cstring(ssh, &pkalg, NULL)) != 0 || |
| 109 | (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0) |
| 110 | fatal("%s: sshpkt_get_cstring failed: %s", |
| 111 | __func__, ssh_err(r)); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 112 | } |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 113 | pktype = sshkey_type_from_name(pkalg); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 114 | if (pktype == KEY_UNSPEC) { |
| 115 | /* this is perfectly legal */ |
djm@openbsd.org | ebacd37 | 2016-01-27 00:53:12 +0000 | [diff] [blame] | 116 | logit("%s: unsupported public key algorithm: %s", |
| 117 | __func__, pkalg); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 118 | goto done; |
| 119 | } |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 120 | if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) { |
| 121 | error("%s: could not parse key: %s", __func__, ssh_err(r)); |
| 122 | goto done; |
| 123 | } |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 124 | if (key == NULL) { |
djm@openbsd.org | ebacd37 | 2016-01-27 00:53:12 +0000 | [diff] [blame] | 125 | error("%s: cannot decode key: %s", __func__, pkalg); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 126 | goto done; |
| 127 | } |
| 128 | if (key->type != pktype) { |
djm@openbsd.org | ebacd37 | 2016-01-27 00:53:12 +0000 | [diff] [blame] | 129 | error("%s: type mismatch for decoded key " |
| 130 | "(received %d, expected %d)", __func__, key->type, pktype); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 131 | goto done; |
| 132 | } |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 133 | if (sshkey_type_plain(key->type) == KEY_RSA && |
| 134 | (ssh->compat & SSH_BUG_RSASIGMD5) != 0) { |
Damien Miller | 324541e | 2013-12-31 12:25:40 +1100 | [diff] [blame] | 135 | logit("Refusing RSA key because client uses unsafe " |
| 136 | "signature scheme"); |
| 137 | goto done; |
| 138 | } |
djm@openbsd.org | ebacd37 | 2016-01-27 00:53:12 +0000 | [diff] [blame] | 139 | fp = sshkey_fingerprint(key, options.fingerprint_hash, SSH_FP_DEFAULT); |
djm@openbsd.org | f69b69b | 2014-12-22 07:51:30 +0000 | [diff] [blame] | 140 | if (auth2_userkey_already_used(authctxt, key)) { |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 141 | logit("refusing previously-used %s key", sshkey_type(key)); |
djm@openbsd.org | f69b69b | 2014-12-22 07:51:30 +0000 | [diff] [blame] | 142 | goto done; |
| 143 | } |
djm@openbsd.org | e661a86 | 2015-05-04 06:10:48 +0000 | [diff] [blame] | 144 | if (match_pattern_list(sshkey_ssh_name(key), |
| 145 | options.pubkey_key_types, 0) != 1) { |
djm@openbsd.org | 1f729f0 | 2015-01-13 07:39:19 +0000 | [diff] [blame] | 146 | logit("%s: key type %s not in PubkeyAcceptedKeyTypes", |
| 147 | __func__, sshkey_ssh_name(key)); |
| 148 | goto done; |
| 149 | } |
| 150 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 151 | if (have_sig) { |
djm@openbsd.org | ebacd37 | 2016-01-27 00:53:12 +0000 | [diff] [blame] | 152 | debug3("%s: have signature for %s %s", |
| 153 | __func__, sshkey_type(key), fp); |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 154 | if ((r = sshpkt_get_string(ssh, &sig, &slen)) != 0 || |
| 155 | (r = sshpkt_get_end(ssh)) != 0) |
| 156 | fatal("%s: %s", __func__, ssh_err(r)); |
| 157 | if ((b = sshbuf_new()) == NULL) |
| 158 | fatal("%s: sshbuf_new failed", __func__); |
| 159 | if (ssh->compat & SSH_OLD_SESSIONID) { |
| 160 | if ((r = sshbuf_put(b, session_id2, |
| 161 | session_id2_len)) != 0) |
| 162 | fatal("%s: sshbuf_put session id: %s", |
| 163 | __func__, ssh_err(r)); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 164 | } else { |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 165 | if ((r = sshbuf_put_string(b, session_id2, |
| 166 | session_id2_len)) != 0) |
| 167 | fatal("%s: sshbuf_put_string session id: %s", |
| 168 | __func__, ssh_err(r)); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 169 | } |
| 170 | /* reconstruct packet */ |
Damien Miller | 4ce189d | 2013-04-23 15:17:52 +1000 | [diff] [blame] | 171 | xasprintf(&userstyle, "%s%s%s", authctxt->user, |
| 172 | authctxt->style ? ":" : "", |
| 173 | authctxt->style ? authctxt->style : ""); |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 174 | if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 || |
| 175 | (r = sshbuf_put_cstring(b, userstyle)) != 0 || |
| 176 | (r = sshbuf_put_cstring(b, ssh->compat & SSH_BUG_PKSERVICE ? |
| 177 | "ssh-userauth" : authctxt->service)) != 0) |
| 178 | fatal("%s: build packet failed: %s", |
| 179 | __func__, ssh_err(r)); |
| 180 | if (ssh->compat & SSH_BUG_PKAUTH) { |
| 181 | if ((r = sshbuf_put_u8(b, have_sig)) != 0) |
| 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 | } else { |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 185 | if ((r = sshbuf_put_cstring(b, "publickey")) != 0 || |
| 186 | (r = sshbuf_put_u8(b, have_sig)) != 0 || |
| 187 | (r = sshbuf_put_cstring(b, pkalg) != 0)) |
| 188 | fatal("%s: build packet failed: %s", |
| 189 | __func__, ssh_err(r)); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 190 | } |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 191 | if ((r = sshbuf_put_string(b, pkblob, blen)) != 0) |
| 192 | fatal("%s: build packet failed: %s", |
| 193 | __func__, ssh_err(r)); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 194 | #ifdef DEBUG_PK |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 195 | sshbuf_dump(b, stderr); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 196 | #endif |
Damien Miller | 20bdcd7 | 2013-07-18 16:10:09 +1000 | [diff] [blame] | 197 | pubkey_auth_info(authctxt, key, NULL); |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 198 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 199 | /* test for correct signature */ |
| 200 | authenticated = 0; |
djm@openbsd.org | 179be0f | 2015-05-01 03:23:51 +0000 | [diff] [blame] | 201 | if (PRIVSEP(user_key_allowed(authctxt->pw, key, 1)) && |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 202 | PRIVSEP(sshkey_verify(key, sig, slen, sshbuf_ptr(b), |
| 203 | sshbuf_len(b), ssh->compat)) == 0) { |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 204 | authenticated = 1; |
djm@openbsd.org | f69b69b | 2014-12-22 07:51:30 +0000 | [diff] [blame] | 205 | /* Record the successful key to prevent reuse */ |
| 206 | auth2_record_userkey(authctxt, key); |
| 207 | key = NULL; /* Don't free below */ |
| 208 | } |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 209 | sshbuf_free(b); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 210 | free(sig); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 211 | } else { |
djm@openbsd.org | ebacd37 | 2016-01-27 00:53:12 +0000 | [diff] [blame] | 212 | debug("%s: test whether pkalg/pkblob are acceptable for %s %s", |
| 213 | __func__, sshkey_type(key), fp); |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 214 | if ((r = sshpkt_get_end(ssh)) != 0) |
| 215 | fatal("%s: %s", __func__, ssh_err(r)); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 216 | |
| 217 | /* XXX fake reply and always send PK_OK ? */ |
| 218 | /* |
| 219 | * XXX this allows testing whether a user is allowed |
| 220 | * to login: if you happen to have a valid pubkey this |
| 221 | * message is sent. the message is NEVER sent at all |
| 222 | * if a user is not allowed to login. is this an |
| 223 | * issue? -markus |
| 224 | */ |
djm@openbsd.org | 179be0f | 2015-05-01 03:23:51 +0000 | [diff] [blame] | 225 | if (PRIVSEP(user_key_allowed(authctxt->pw, key, 0))) { |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 226 | if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_PK_OK)) |
| 227 | != 0 || |
| 228 | (r = sshpkt_put_cstring(ssh, pkalg)) != 0 || |
| 229 | (r = sshpkt_put_string(ssh, pkblob, blen)) != 0 || |
| 230 | (r = sshpkt_send(ssh)) != 0) |
| 231 | fatal("%s: %s", __func__, ssh_err(r)); |
| 232 | ssh_packet_write_wait(ssh); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 233 | authctxt->postponed = 1; |
| 234 | } |
| 235 | } |
| 236 | if (authenticated != 1) |
| 237 | auth_clear_options(); |
| 238 | done: |
djm@openbsd.org | ebacd37 | 2016-01-27 00:53:12 +0000 | [diff] [blame] | 239 | debug2("%s: authenticated %d pkalg %s", __func__, authenticated, pkalg); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 240 | if (key != NULL) |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 241 | sshkey_free(key); |
| 242 | free(userstyle); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 243 | free(pkalg); |
| 244 | free(pkblob); |
djm@openbsd.org | ebacd37 | 2016-01-27 00:53:12 +0000 | [diff] [blame] | 245 | free(fp); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 246 | return authenticated; |
| 247 | } |
| 248 | |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 249 | void |
markus@openbsd.org | 54d90ac | 2017-05-30 08:52:19 +0000 | [diff] [blame] | 250 | pubkey_auth_info(Authctxt *authctxt, const struct sshkey *key, |
| 251 | const char *fmt, ...) |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 252 | { |
Damien Miller | 20bdcd7 | 2013-07-18 16:10:09 +1000 | [diff] [blame] | 253 | char *fp, *extra; |
| 254 | va_list ap; |
| 255 | int i; |
| 256 | |
| 257 | extra = NULL; |
| 258 | if (fmt != NULL) { |
| 259 | va_start(ap, fmt); |
| 260 | i = vasprintf(&extra, fmt, ap); |
| 261 | va_end(ap); |
| 262 | if (i < 0 || extra == NULL) |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 263 | fatal("%s: vasprintf failed", __func__); |
Damien Miller | 20bdcd7 | 2013-07-18 16:10:09 +1000 | [diff] [blame] | 264 | } |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 265 | |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 266 | if (sshkey_is_cert(key)) { |
djm@openbsd.org | 9ce86c9 | 2015-01-28 22:36:00 +0000 | [diff] [blame] | 267 | fp = sshkey_fingerprint(key->cert->signature_key, |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 268 | options.fingerprint_hash, SSH_FP_DEFAULT); |
Damien Miller | 20bdcd7 | 2013-07-18 16:10:09 +1000 | [diff] [blame] | 269 | auth_info(authctxt, "%s ID %s (serial %llu) CA %s %s%s%s", |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 270 | sshkey_type(key), key->cert->key_id, |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 271 | (unsigned long long)key->cert->serial, |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 272 | sshkey_type(key->cert->signature_key), |
djm@openbsd.org | 13a3941 | 2015-02-17 00:14:05 +0000 | [diff] [blame] | 273 | fp == NULL ? "(null)" : fp, |
Damien Miller | 20bdcd7 | 2013-07-18 16:10:09 +1000 | [diff] [blame] | 274 | extra == NULL ? "" : ", ", extra == NULL ? "" : extra); |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 275 | free(fp); |
| 276 | } else { |
djm@openbsd.org | 9ce86c9 | 2015-01-28 22:36:00 +0000 | [diff] [blame] | 277 | fp = sshkey_fingerprint(key, options.fingerprint_hash, |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 278 | SSH_FP_DEFAULT); |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 279 | auth_info(authctxt, "%s %s%s%s", sshkey_type(key), |
djm@openbsd.org | 13a3941 | 2015-02-17 00:14:05 +0000 | [diff] [blame] | 280 | fp == NULL ? "(null)" : fp, |
Damien Miller | 20bdcd7 | 2013-07-18 16:10:09 +1000 | [diff] [blame] | 281 | extra == NULL ? "" : ", ", extra == NULL ? "" : extra); |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 282 | free(fp); |
| 283 | } |
Damien Miller | 20bdcd7 | 2013-07-18 16:10:09 +1000 | [diff] [blame] | 284 | free(extra); |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 285 | } |
| 286 | |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 287 | /* |
| 288 | * Splits 's' into an argument vector. Handles quoted string and basic |
| 289 | * escape characters (\\, \", \'). Caller must free the argument vector |
| 290 | * and its members. |
| 291 | */ |
| 292 | static int |
| 293 | split_argv(const char *s, int *argcp, char ***argvp) |
| 294 | { |
| 295 | int r = SSH_ERR_INTERNAL_ERROR; |
| 296 | int argc = 0, quote, i, j; |
| 297 | char *arg, **argv = xcalloc(1, sizeof(*argv)); |
| 298 | |
| 299 | *argvp = NULL; |
| 300 | *argcp = 0; |
| 301 | |
| 302 | for (i = 0; s[i] != '\0'; i++) { |
| 303 | /* Skip leading whitespace */ |
| 304 | if (s[i] == ' ' || s[i] == '\t') |
| 305 | continue; |
| 306 | |
| 307 | /* Start of a token */ |
| 308 | quote = 0; |
| 309 | if (s[i] == '\\' && |
| 310 | (s[i + 1] == '\'' || s[i + 1] == '\"' || s[i + 1] == '\\')) |
| 311 | i++; |
| 312 | else if (s[i] == '\'' || s[i] == '"') |
| 313 | quote = s[i++]; |
| 314 | |
| 315 | argv = xreallocarray(argv, (argc + 2), sizeof(*argv)); |
| 316 | arg = argv[argc++] = xcalloc(1, strlen(s + i) + 1); |
| 317 | argv[argc] = NULL; |
| 318 | |
| 319 | /* Copy the token in, removing escapes */ |
| 320 | for (j = 0; s[i] != '\0'; i++) { |
| 321 | if (s[i] == '\\') { |
| 322 | if (s[i + 1] == '\'' || |
| 323 | s[i + 1] == '\"' || |
| 324 | s[i + 1] == '\\') { |
| 325 | i++; /* Skip '\' */ |
| 326 | arg[j++] = s[i]; |
| 327 | } else { |
| 328 | /* Unrecognised escape */ |
| 329 | arg[j++] = s[i]; |
| 330 | } |
| 331 | } else if (quote == 0 && (s[i] == ' ' || s[i] == '\t')) |
| 332 | break; /* done */ |
| 333 | else if (quote != 0 && s[i] == quote) |
| 334 | break; /* done */ |
| 335 | else |
| 336 | arg[j++] = s[i]; |
| 337 | } |
| 338 | if (s[i] == '\0') { |
| 339 | if (quote != 0) { |
| 340 | /* Ran out of string looking for close quote */ |
| 341 | r = SSH_ERR_INVALID_FORMAT; |
| 342 | goto out; |
| 343 | } |
| 344 | break; |
| 345 | } |
| 346 | } |
| 347 | /* Success */ |
| 348 | *argcp = argc; |
| 349 | *argvp = argv; |
| 350 | argc = 0; |
| 351 | argv = NULL; |
| 352 | r = 0; |
| 353 | out: |
| 354 | if (argc != 0 && argv != NULL) { |
| 355 | for (i = 0; i < argc; i++) |
| 356 | free(argv[i]); |
| 357 | free(argv); |
| 358 | } |
| 359 | return r; |
| 360 | } |
| 361 | |
| 362 | /* |
| 363 | * Reassemble an argument vector into a string, quoting and escaping as |
| 364 | * necessary. Caller must free returned string. |
| 365 | */ |
| 366 | static char * |
| 367 | assemble_argv(int argc, char **argv) |
| 368 | { |
| 369 | int i, j, ws, r; |
| 370 | char c, *ret; |
| 371 | struct sshbuf *buf, *arg; |
| 372 | |
| 373 | if ((buf = sshbuf_new()) == NULL || (arg = sshbuf_new()) == NULL) |
| 374 | fatal("%s: sshbuf_new failed", __func__); |
| 375 | |
| 376 | for (i = 0; i < argc; i++) { |
| 377 | ws = 0; |
| 378 | sshbuf_reset(arg); |
| 379 | for (j = 0; argv[i][j] != '\0'; j++) { |
| 380 | r = 0; |
| 381 | c = argv[i][j]; |
| 382 | switch (c) { |
| 383 | case ' ': |
| 384 | case '\t': |
| 385 | ws = 1; |
| 386 | r = sshbuf_put_u8(arg, c); |
| 387 | break; |
| 388 | case '\\': |
| 389 | case '\'': |
| 390 | case '"': |
| 391 | if ((r = sshbuf_put_u8(arg, '\\')) != 0) |
| 392 | break; |
| 393 | /* FALLTHROUGH */ |
| 394 | default: |
| 395 | r = sshbuf_put_u8(arg, c); |
| 396 | break; |
| 397 | } |
| 398 | if (r != 0) |
| 399 | fatal("%s: sshbuf_put_u8: %s", |
| 400 | __func__, ssh_err(r)); |
| 401 | } |
| 402 | if ((i != 0 && (r = sshbuf_put_u8(buf, ' ')) != 0) || |
| 403 | (ws != 0 && (r = sshbuf_put_u8(buf, '"')) != 0) || |
| 404 | (r = sshbuf_putb(buf, arg)) != 0 || |
| 405 | (ws != 0 && (r = sshbuf_put_u8(buf, '"')) != 0)) |
| 406 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 407 | } |
| 408 | if ((ret = malloc(sshbuf_len(buf) + 1)) == NULL) |
| 409 | fatal("%s: malloc failed", __func__); |
| 410 | memcpy(ret, sshbuf_ptr(buf), sshbuf_len(buf)); |
| 411 | ret[sshbuf_len(buf)] = '\0'; |
| 412 | sshbuf_free(buf); |
| 413 | sshbuf_free(arg); |
| 414 | return ret; |
| 415 | } |
| 416 | |
| 417 | /* |
| 418 | * Runs command in a subprocess. Returns pid on success and a FILE* to the |
| 419 | * subprocess' stdout or 0 on failure. |
| 420 | * NB. "command" is only used for logging. |
| 421 | */ |
| 422 | static pid_t |
| 423 | subprocess(const char *tag, struct passwd *pw, const char *command, |
| 424 | int ac, char **av, FILE **child) |
| 425 | { |
| 426 | FILE *f; |
| 427 | struct stat st; |
| 428 | int devnull, p[2], i; |
| 429 | pid_t pid; |
| 430 | char *cp, errmsg[512]; |
| 431 | u_int envsize; |
| 432 | char **child_env; |
| 433 | |
| 434 | *child = NULL; |
| 435 | |
| 436 | debug3("%s: %s command \"%s\" running as %s", __func__, |
| 437 | tag, command, pw->pw_name); |
| 438 | |
| 439 | /* Verify the path exists and is safe-ish to execute */ |
| 440 | if (*av[0] != '/') { |
| 441 | error("%s path is not absolute", tag); |
| 442 | return 0; |
| 443 | } |
| 444 | temporarily_use_uid(pw); |
| 445 | if (stat(av[0], &st) < 0) { |
| 446 | error("Could not stat %s \"%s\": %s", tag, |
| 447 | av[0], strerror(errno)); |
| 448 | restore_uid(); |
| 449 | return 0; |
| 450 | } |
| 451 | if (auth_secure_path(av[0], &st, NULL, 0, |
| 452 | errmsg, sizeof(errmsg)) != 0) { |
| 453 | error("Unsafe %s \"%s\": %s", tag, av[0], errmsg); |
| 454 | restore_uid(); |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | /* |
| 459 | * Run the command; stderr is left in place, stdout is the |
| 460 | * authorized_keys output. |
| 461 | */ |
| 462 | if (pipe(p) != 0) { |
| 463 | error("%s: pipe: %s", tag, strerror(errno)); |
| 464 | restore_uid(); |
| 465 | return 0; |
| 466 | } |
| 467 | |
| 468 | /* |
| 469 | * Don't want to call this in the child, where it can fatal() and |
| 470 | * run cleanup_exit() code. |
| 471 | */ |
| 472 | restore_uid(); |
| 473 | |
| 474 | switch ((pid = fork())) { |
| 475 | case -1: /* error */ |
| 476 | error("%s: fork: %s", tag, strerror(errno)); |
| 477 | close(p[0]); |
| 478 | close(p[1]); |
| 479 | return 0; |
| 480 | case 0: /* child */ |
| 481 | /* Prepare a minimal environment for the child. */ |
| 482 | envsize = 5; |
| 483 | child_env = xcalloc(sizeof(*child_env), envsize); |
| 484 | child_set_env(&child_env, &envsize, "PATH", _PATH_STDPATH); |
| 485 | child_set_env(&child_env, &envsize, "USER", pw->pw_name); |
| 486 | child_set_env(&child_env, &envsize, "LOGNAME", pw->pw_name); |
| 487 | child_set_env(&child_env, &envsize, "HOME", pw->pw_dir); |
| 488 | if ((cp = getenv("LANG")) != NULL) |
| 489 | child_set_env(&child_env, &envsize, "LANG", cp); |
| 490 | |
| 491 | for (i = 0; i < NSIG; i++) |
| 492 | signal(i, SIG_DFL); |
| 493 | |
| 494 | if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) { |
| 495 | error("%s: open %s: %s", tag, _PATH_DEVNULL, |
| 496 | strerror(errno)); |
| 497 | _exit(1); |
| 498 | } |
| 499 | /* Keep stderr around a while longer to catch errors */ |
| 500 | if (dup2(devnull, STDIN_FILENO) == -1 || |
| 501 | dup2(p[1], STDOUT_FILENO) == -1) { |
| 502 | error("%s: dup2: %s", tag, strerror(errno)); |
| 503 | _exit(1); |
| 504 | } |
| 505 | closefrom(STDERR_FILENO + 1); |
| 506 | |
| 507 | /* Don't use permanently_set_uid() here to avoid fatal() */ |
| 508 | if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0) { |
| 509 | error("%s: setresgid %u: %s", tag, (u_int)pw->pw_gid, |
| 510 | strerror(errno)); |
| 511 | _exit(1); |
| 512 | } |
| 513 | if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) != 0) { |
| 514 | error("%s: setresuid %u: %s", tag, (u_int)pw->pw_uid, |
| 515 | strerror(errno)); |
| 516 | _exit(1); |
| 517 | } |
| 518 | /* stdin is pointed to /dev/null at this point */ |
| 519 | if (dup2(STDIN_FILENO, STDERR_FILENO) == -1) { |
| 520 | error("%s: dup2: %s", tag, strerror(errno)); |
| 521 | _exit(1); |
| 522 | } |
| 523 | |
| 524 | execve(av[0], av, child_env); |
| 525 | error("%s exec \"%s\": %s", tag, command, strerror(errno)); |
| 526 | _exit(127); |
| 527 | default: /* parent */ |
| 528 | break; |
| 529 | } |
| 530 | |
| 531 | close(p[1]); |
| 532 | if ((f = fdopen(p[0], "r")) == NULL) { |
| 533 | error("%s: fdopen: %s", tag, strerror(errno)); |
| 534 | close(p[0]); |
| 535 | /* Don't leave zombie child */ |
| 536 | kill(pid, SIGTERM); |
| 537 | while (waitpid(pid, NULL, 0) == -1 && errno == EINTR) |
| 538 | ; |
| 539 | return 0; |
| 540 | } |
| 541 | /* Success */ |
| 542 | debug3("%s: %s pid %ld", __func__, tag, (long)pid); |
| 543 | *child = f; |
| 544 | return pid; |
| 545 | } |
| 546 | |
| 547 | /* Returns 0 if pid exited cleanly, non-zero otherwise */ |
| 548 | static int |
| 549 | exited_cleanly(pid_t pid, const char *tag, const char *cmd) |
| 550 | { |
| 551 | int status; |
| 552 | |
| 553 | while (waitpid(pid, &status, 0) == -1) { |
| 554 | if (errno != EINTR) { |
| 555 | error("%s: waitpid: %s", tag, strerror(errno)); |
| 556 | return -1; |
| 557 | } |
| 558 | } |
| 559 | if (WIFSIGNALED(status)) { |
| 560 | error("%s %s exited on signal %d", tag, cmd, WTERMSIG(status)); |
| 561 | return -1; |
| 562 | } else if (WEXITSTATUS(status) != 0) { |
| 563 | error("%s %s failed, status %d", tag, cmd, WEXITSTATUS(status)); |
| 564 | return -1; |
| 565 | } |
| 566 | return 0; |
| 567 | } |
| 568 | |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 569 | static int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 570 | match_principals_option(const char *principal_list, struct sshkey_cert *cert) |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 571 | { |
| 572 | char *result; |
| 573 | u_int i; |
| 574 | |
| 575 | /* XXX percent_expand() sequences for authorized_principals? */ |
| 576 | |
| 577 | for (i = 0; i < cert->nprincipals; i++) { |
| 578 | if ((result = match_list(cert->principals[i], |
| 579 | principal_list, NULL)) != NULL) { |
| 580 | debug3("matched principal from key options \"%.100s\"", |
| 581 | result); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 582 | free(result); |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 583 | return 1; |
| 584 | } |
| 585 | } |
| 586 | return 0; |
| 587 | } |
| 588 | |
| 589 | static int |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 590 | process_principals(FILE *f, char *file, struct passwd *pw, |
djm@openbsd.org | e7907c1 | 2016-09-14 05:42:25 +0000 | [diff] [blame] | 591 | const struct sshkey_cert *cert) |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 592 | { |
Damien Miller | 6018a36 | 2010-07-02 13:35:19 +1000 | [diff] [blame] | 593 | char line[SSH_MAX_PUBKEY_BYTES], *cp, *ep, *line_opts; |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 594 | u_long linenum = 0; |
djm@openbsd.org | 52763dd | 2017-01-30 01:03:00 +0000 | [diff] [blame] | 595 | u_int i, found_principal = 0; |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 596 | |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 597 | while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) { |
djm@openbsd.org | 52763dd | 2017-01-30 01:03:00 +0000 | [diff] [blame] | 598 | /* Always consume entire input */ |
| 599 | if (found_principal) |
| 600 | continue; |
Damien Miller | 6018a36 | 2010-07-02 13:35:19 +1000 | [diff] [blame] | 601 | /* Skip leading whitespace. */ |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 602 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) |
| 603 | ; |
Damien Miller | 6018a36 | 2010-07-02 13:35:19 +1000 | [diff] [blame] | 604 | /* Skip blank and comment lines. */ |
| 605 | if ((ep = strchr(cp, '#')) != NULL) |
| 606 | *ep = '\0'; |
| 607 | if (!*cp || *cp == '\n') |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 608 | continue; |
Damien Miller | 6018a36 | 2010-07-02 13:35:19 +1000 | [diff] [blame] | 609 | /* Trim trailing whitespace. */ |
| 610 | ep = cp + strlen(cp) - 1; |
| 611 | while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t')) |
| 612 | *ep-- = '\0'; |
| 613 | /* |
| 614 | * If the line has internal whitespace then assume it has |
| 615 | * key options. |
| 616 | */ |
| 617 | line_opts = NULL; |
| 618 | if ((ep = strrchr(cp, ' ')) != NULL || |
| 619 | (ep = strrchr(cp, '\t')) != NULL) { |
| 620 | for (; *ep == ' ' || *ep == '\t'; ep++) |
Damien Miller | 188ea81 | 2010-12-01 11:50:14 +1100 | [diff] [blame] | 621 | ; |
Damien Miller | 6018a36 | 2010-07-02 13:35:19 +1000 | [diff] [blame] | 622 | line_opts = cp; |
| 623 | cp = ep; |
| 624 | } |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 625 | for (i = 0; i < cert->nprincipals; i++) { |
| 626 | if (strcmp(cp, cert->principals[i]) == 0) { |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 627 | debug3("%s:%lu: matched principal \"%.100s\"", |
| 628 | file == NULL ? "(command)" : file, |
| 629 | linenum, cert->principals[i]); |
Damien Miller | 6018a36 | 2010-07-02 13:35:19 +1000 | [diff] [blame] | 630 | if (auth_parse_options(pw, line_opts, |
| 631 | file, linenum) != 1) |
| 632 | continue; |
djm@openbsd.org | 52763dd | 2017-01-30 01:03:00 +0000 | [diff] [blame] | 633 | found_principal = 1; |
| 634 | continue; |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 635 | } |
| 636 | } |
| 637 | } |
djm@openbsd.org | 52763dd | 2017-01-30 01:03:00 +0000 | [diff] [blame] | 638 | return found_principal; |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 639 | } |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 640 | |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 641 | static int |
| 642 | match_principals_file(char *file, struct passwd *pw, struct sshkey_cert *cert) |
| 643 | { |
| 644 | FILE *f; |
| 645 | int success; |
| 646 | |
| 647 | temporarily_use_uid(pw); |
| 648 | debug("trying authorized principals file %s", file); |
| 649 | if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) { |
| 650 | restore_uid(); |
| 651 | return 0; |
| 652 | } |
| 653 | success = process_principals(f, file, pw, cert); |
| 654 | fclose(f); |
| 655 | restore_uid(); |
| 656 | return success; |
| 657 | } |
| 658 | |
| 659 | /* |
| 660 | * Checks whether principal is allowed in output of command. |
| 661 | * returns 1 if the principal is allowed or 0 otherwise. |
| 662 | */ |
| 663 | static int |
djm@openbsd.org | e7907c1 | 2016-09-14 05:42:25 +0000 | [diff] [blame] | 664 | match_principals_command(struct passwd *user_pw, const struct sshkey *key) |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 665 | { |
djm@openbsd.org | e7907c1 | 2016-09-14 05:42:25 +0000 | [diff] [blame] | 666 | const struct sshkey_cert *cert = key->cert; |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 667 | FILE *f = NULL; |
djm@openbsd.org | e7907c1 | 2016-09-14 05:42:25 +0000 | [diff] [blame] | 668 | int r, ok, found_principal = 0; |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 669 | struct passwd *pw; |
| 670 | int i, ac = 0, uid_swapped = 0; |
| 671 | pid_t pid; |
| 672 | char *tmp, *username = NULL, *command = NULL, **av = NULL; |
djm@openbsd.org | e7907c1 | 2016-09-14 05:42:25 +0000 | [diff] [blame] | 673 | char *ca_fp = NULL, *key_fp = NULL, *catext = NULL, *keytext = NULL; |
djm@openbsd.org | bfa9d96 | 2016-09-21 01:34:45 +0000 | [diff] [blame] | 674 | char serial_s[16]; |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 675 | void (*osigchld)(int); |
| 676 | |
| 677 | if (options.authorized_principals_command == NULL) |
| 678 | return 0; |
| 679 | if (options.authorized_principals_command_user == NULL) { |
| 680 | error("No user for AuthorizedPrincipalsCommand specified, " |
| 681 | "skipping"); |
| 682 | return 0; |
| 683 | } |
| 684 | |
| 685 | /* |
| 686 | * NB. all returns later this function should go via "out" to |
| 687 | * ensure the original SIGCHLD handler is restored properly. |
| 688 | */ |
| 689 | osigchld = signal(SIGCHLD, SIG_DFL); |
| 690 | |
| 691 | /* Prepare and verify the user for the command */ |
| 692 | username = percent_expand(options.authorized_principals_command_user, |
| 693 | "u", user_pw->pw_name, (char *)NULL); |
| 694 | pw = getpwnam(username); |
| 695 | if (pw == NULL) { |
| 696 | error("AuthorizedPrincipalsCommandUser \"%s\" not found: %s", |
| 697 | username, strerror(errno)); |
| 698 | goto out; |
| 699 | } |
| 700 | |
| 701 | /* Turn the command into an argument vector */ |
| 702 | if (split_argv(options.authorized_principals_command, &ac, &av) != 0) { |
| 703 | error("AuthorizedPrincipalsCommand \"%s\" contains " |
| 704 | "invalid quotes", command); |
| 705 | goto out; |
| 706 | } |
| 707 | if (ac == 0) { |
| 708 | error("AuthorizedPrincipalsCommand \"%s\" yielded no arguments", |
| 709 | command); |
| 710 | goto out; |
| 711 | } |
djm@openbsd.org | e7907c1 | 2016-09-14 05:42:25 +0000 | [diff] [blame] | 712 | if ((ca_fp = sshkey_fingerprint(cert->signature_key, |
| 713 | options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) { |
| 714 | error("%s: sshkey_fingerprint failed", __func__); |
| 715 | goto out; |
| 716 | } |
djm@openbsd.org | 00df97f | 2016-09-14 20:11:26 +0000 | [diff] [blame] | 717 | if ((key_fp = sshkey_fingerprint(key, |
djm@openbsd.org | e7907c1 | 2016-09-14 05:42:25 +0000 | [diff] [blame] | 718 | options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) { |
| 719 | error("%s: sshkey_fingerprint failed", __func__); |
| 720 | goto out; |
| 721 | } |
| 722 | if ((r = sshkey_to_base64(cert->signature_key, &catext)) != 0) { |
| 723 | error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r)); |
| 724 | goto out; |
| 725 | } |
| 726 | if ((r = sshkey_to_base64(key, &keytext)) != 0) { |
| 727 | error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r)); |
| 728 | goto out; |
| 729 | } |
djm@openbsd.org | f83a0cf | 2016-09-21 17:44:20 +0000 | [diff] [blame] | 730 | snprintf(serial_s, sizeof(serial_s), "%llu", |
| 731 | (unsigned long long)cert->serial); |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 732 | for (i = 1; i < ac; i++) { |
| 733 | tmp = percent_expand(av[i], |
| 734 | "u", user_pw->pw_name, |
| 735 | "h", user_pw->pw_dir, |
djm@openbsd.org | e7907c1 | 2016-09-14 05:42:25 +0000 | [diff] [blame] | 736 | "t", sshkey_ssh_name(key), |
| 737 | "T", sshkey_ssh_name(cert->signature_key), |
| 738 | "f", key_fp, |
| 739 | "F", ca_fp, |
| 740 | "k", keytext, |
| 741 | "K", catext, |
djm@openbsd.org | bfa9d96 | 2016-09-21 01:34:45 +0000 | [diff] [blame] | 742 | "i", cert->key_id, |
| 743 | "s", serial_s, |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 744 | (char *)NULL); |
| 745 | if (tmp == NULL) |
| 746 | fatal("%s: percent_expand failed", __func__); |
| 747 | free(av[i]); |
| 748 | av[i] = tmp; |
| 749 | } |
| 750 | /* Prepare a printable command for logs, etc. */ |
| 751 | command = assemble_argv(ac, av); |
| 752 | |
| 753 | if ((pid = subprocess("AuthorizedPrincipalsCommand", pw, command, |
| 754 | ac, av, &f)) == 0) |
| 755 | goto out; |
| 756 | |
| 757 | uid_swapped = 1; |
| 758 | temporarily_use_uid(pw); |
| 759 | |
jsing@openbsd.org | aff3e94 | 2015-06-15 18:42:19 +0000 | [diff] [blame] | 760 | ok = process_principals(f, NULL, pw, cert); |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 761 | |
djm@openbsd.org | ddd3d34 | 2016-12-30 22:08:02 +0000 | [diff] [blame] | 762 | fclose(f); |
| 763 | f = NULL; |
| 764 | |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 765 | if (exited_cleanly(pid, "AuthorizedPrincipalsCommand", command) != 0) |
| 766 | goto out; |
| 767 | |
| 768 | /* Read completed successfully */ |
| 769 | found_principal = ok; |
| 770 | out: |
| 771 | if (f != NULL) |
| 772 | fclose(f); |
| 773 | signal(SIGCHLD, osigchld); |
| 774 | for (i = 0; i < ac; i++) |
| 775 | free(av[i]); |
| 776 | free(av); |
| 777 | if (uid_swapped) |
| 778 | restore_uid(); |
| 779 | free(command); |
| 780 | free(username); |
djm@openbsd.org | e7907c1 | 2016-09-14 05:42:25 +0000 | [diff] [blame] | 781 | free(ca_fp); |
| 782 | free(key_fp); |
| 783 | free(catext); |
| 784 | free(keytext); |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 785 | return found_principal; |
| 786 | } |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 787 | /* |
| 788 | * Checks whether key is allowed in authorized_keys-format file, |
| 789 | * returns 1 if the key is allowed or 0 otherwise. |
| 790 | */ |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 791 | static int |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 792 | check_authkeys_file(FILE *f, char *file, struct sshkey *key, struct passwd *pw) |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 793 | { |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 794 | char line[SSH_MAX_PUBKEY_BYTES]; |
Darren Tucker | 33c787f | 2008-07-02 22:37:30 +1000 | [diff] [blame] | 795 | int found_key = 0; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 796 | u_long linenum = 0; |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 797 | struct sshkey *found = NULL; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 798 | |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 799 | while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) { |
djm@openbsd.org | fd6dcef | 2016-11-30 02:57:40 +0000 | [diff] [blame] | 800 | char *cp, *key_options = NULL, *fp = NULL; |
| 801 | const char *reason = NULL; |
| 802 | |
djm@openbsd.org | 52763dd | 2017-01-30 01:03:00 +0000 | [diff] [blame] | 803 | /* Always consume entrire file */ |
| 804 | if (found_key) |
| 805 | continue; |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 806 | if (found != NULL) |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 807 | sshkey_free(found); |
| 808 | found = sshkey_new(sshkey_is_cert(key) ? KEY_UNSPEC : key->type); |
| 809 | if (found == NULL) |
| 810 | goto done; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 811 | auth_clear_options(); |
| 812 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 813 | /* Skip leading whitespace, empty and comment lines. */ |
| 814 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) |
| 815 | ; |
| 816 | if (!*cp || *cp == '\n' || *cp == '#') |
| 817 | continue; |
| 818 | |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 819 | if (sshkey_read(found, &cp) != 0) { |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 820 | /* no key? check if there are options for this key */ |
| 821 | int quoted = 0; |
| 822 | debug2("user_key_allowed: check options: '%s'", cp); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 823 | key_options = cp; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 824 | for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) { |
| 825 | if (*cp == '\\' && cp[1] == '"') |
| 826 | cp++; /* Skip both */ |
| 827 | else if (*cp == '"') |
| 828 | quoted = !quoted; |
| 829 | } |
| 830 | /* Skip remaining whitespace. */ |
| 831 | for (; *cp == ' ' || *cp == '\t'; cp++) |
| 832 | ; |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 833 | if (sshkey_read(found, &cp) != 0) { |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 834 | debug2("user_key_allowed: advance: '%s'", cp); |
| 835 | /* still no key? advance to next line*/ |
| 836 | continue; |
| 837 | } |
| 838 | } |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 839 | if (sshkey_is_cert(key)) { |
| 840 | if (!sshkey_equal(found, key->cert->signature_key)) |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 841 | continue; |
Damien Miller | 8439955 | 2010-05-21 14:58:12 +1000 | [diff] [blame] | 842 | if (auth_parse_options(pw, key_options, file, |
| 843 | linenum) != 1) |
| 844 | continue; |
| 845 | if (!key_is_cert_authority) |
| 846 | continue; |
djm@openbsd.org | 9ce86c9 | 2015-01-28 22:36:00 +0000 | [diff] [blame] | 847 | if ((fp = sshkey_fingerprint(found, |
| 848 | options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) |
| 849 | continue; |
Damien Miller | e513a91 | 2010-03-22 05:51:21 +1100 | [diff] [blame] | 850 | debug("matching CA found: file %s, line %lu, %s %s", |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 851 | file, linenum, sshkey_type(found), fp); |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 852 | /* |
| 853 | * If the user has specified a list of principals as |
| 854 | * a key option, then prefer that list to matching |
| 855 | * their username in the certificate principals list. |
| 856 | */ |
| 857 | if (authorized_principals != NULL && |
| 858 | !match_principals_option(authorized_principals, |
| 859 | key->cert)) { |
| 860 | reason = "Certificate does not contain an " |
| 861 | "authorized principal"; |
| 862 | fail_reason: |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 863 | free(fp); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 864 | error("%s", reason); |
| 865 | auth_debug_add("%s", reason); |
| 866 | continue; |
| 867 | } |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 868 | if (sshkey_cert_check_authority(key, 0, 0, |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 869 | authorized_principals == NULL ? pw->pw_name : NULL, |
| 870 | &reason) != 0) |
| 871 | goto fail_reason; |
djm@openbsd.org | fd6dcef | 2016-11-30 02:57:40 +0000 | [diff] [blame] | 872 | if (auth_cert_options(key, pw, &reason) != 0) |
| 873 | goto fail_reason; |
djm@openbsd.org | 63d1881 | 2015-10-27 01:44:45 +0000 | [diff] [blame] | 874 | verbose("Accepted certificate ID \"%s\" (serial %llu) " |
Damien Miller | e513a91 | 2010-03-22 05:51:21 +1100 | [diff] [blame] | 875 | "signed by %s CA %s via %s", key->cert->key_id, |
djm@openbsd.org | 63d1881 | 2015-10-27 01:44:45 +0000 | [diff] [blame] | 876 | (unsigned long long)key->cert->serial, |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 877 | sshkey_type(found), fp, file); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 878 | free(fp); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 879 | found_key = 1; |
| 880 | break; |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 881 | } else if (sshkey_equal(found, key)) { |
Damien Miller | 8439955 | 2010-05-21 14:58:12 +1000 | [diff] [blame] | 882 | if (auth_parse_options(pw, key_options, file, |
| 883 | linenum) != 1) |
| 884 | continue; |
| 885 | if (key_is_cert_authority) |
| 886 | continue; |
djm@openbsd.org | 9ce86c9 | 2015-01-28 22:36:00 +0000 | [diff] [blame] | 887 | if ((fp = sshkey_fingerprint(found, |
| 888 | options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) |
| 889 | continue; |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 890 | debug("matching key found: file %s, line %lu %s %s", |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 891 | file, linenum, sshkey_type(found), fp); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 892 | free(fp); |
djm@openbsd.org | 9ce86c9 | 2015-01-28 22:36:00 +0000 | [diff] [blame] | 893 | found_key = 1; |
djm@openbsd.org | 52763dd | 2017-01-30 01:03:00 +0000 | [diff] [blame] | 894 | continue; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 895 | } |
| 896 | } |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 897 | done: |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 898 | if (found != NULL) |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 899 | sshkey_free(found); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 900 | if (!found_key) |
| 901 | debug2("key not found"); |
| 902 | return found_key; |
| 903 | } |
| 904 | |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 905 | /* Authenticate a certificate key against TrustedUserCAKeys */ |
| 906 | static int |
markus@openbsd.org | 54d90ac | 2017-05-30 08:52:19 +0000 | [diff] [blame] | 907 | user_cert_trusted_ca(struct passwd *pw, struct sshkey *key) |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 908 | { |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 909 | char *ca_fp, *principals_file = NULL; |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 910 | const char *reason; |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 911 | int r, ret = 0, found_principal = 0, use_authorized_principals; |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 912 | |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 913 | if (!sshkey_is_cert(key) || options.trusted_user_ca_keys == NULL) |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 914 | return 0; |
| 915 | |
djm@openbsd.org | 9ce86c9 | 2015-01-28 22:36:00 +0000 | [diff] [blame] | 916 | if ((ca_fp = sshkey_fingerprint(key->cert->signature_key, |
| 917 | options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) |
| 918 | return 0; |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 919 | |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 920 | if ((r = sshkey_in_file(key->cert->signature_key, |
| 921 | options.trusted_user_ca_keys, 1, 0)) != 0) { |
| 922 | debug2("%s: CA %s %s is not listed in %s: %s", __func__, |
| 923 | sshkey_type(key->cert->signature_key), ca_fp, |
| 924 | options.trusted_user_ca_keys, ssh_err(r)); |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 925 | goto out; |
| 926 | } |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 927 | /* |
| 928 | * If AuthorizedPrincipals is in use, then compare the certificate |
| 929 | * principals against the names in that file rather than matching |
| 930 | * against the username. |
| 931 | */ |
| 932 | if ((principals_file = authorized_principals_file(pw)) != NULL) { |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 933 | if (match_principals_file(principals_file, pw, key->cert)) |
| 934 | found_principal = 1; |
| 935 | } |
| 936 | /* Try querying command if specified */ |
djm@openbsd.org | e7907c1 | 2016-09-14 05:42:25 +0000 | [diff] [blame] | 937 | if (!found_principal && match_principals_command(pw, key)) |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 938 | found_principal = 1; |
jsing@openbsd.org | 596dbca | 2015-06-15 18:44:22 +0000 | [diff] [blame] | 939 | /* If principals file or command is specified, then require a match */ |
| 940 | use_authorized_principals = principals_file != NULL || |
| 941 | options.authorized_principals_command != NULL; |
| 942 | if (!found_principal && use_authorized_principals) { |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 943 | reason = "Certificate does not contain an authorized principal"; |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 944 | fail_reason: |
djm@openbsd.org | bcc50d8 | 2015-05-21 06:43:30 +0000 | [diff] [blame] | 945 | error("%s", reason); |
| 946 | auth_debug_add("%s", reason); |
| 947 | goto out; |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 948 | } |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 949 | if (sshkey_cert_check_authority(key, 0, 1, |
jsing@openbsd.org | 596dbca | 2015-06-15 18:44:22 +0000 | [diff] [blame] | 950 | use_authorized_principals ? NULL : pw->pw_name, &reason) != 0) |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 951 | goto fail_reason; |
djm@openbsd.org | fd6dcef | 2016-11-30 02:57:40 +0000 | [diff] [blame] | 952 | if (auth_cert_options(key, pw, &reason) != 0) |
| 953 | goto fail_reason; |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 954 | |
djm@openbsd.org | 63d1881 | 2015-10-27 01:44:45 +0000 | [diff] [blame] | 955 | verbose("Accepted certificate ID \"%s\" (serial %llu) signed by " |
| 956 | "%s CA %s via %s", key->cert->key_id, |
| 957 | (unsigned long long)key->cert->serial, |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 958 | sshkey_type(key->cert->signature_key), ca_fp, |
Damien Miller | e513a91 | 2010-03-22 05:51:21 +1100 | [diff] [blame] | 959 | options.trusted_user_ca_keys); |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 960 | ret = 1; |
| 961 | |
| 962 | out: |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 963 | free(principals_file); |
| 964 | free(ca_fp); |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 965 | return ret; |
| 966 | } |
| 967 | |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 968 | /* |
| 969 | * Checks whether key is allowed in file. |
| 970 | * returns 1 if the key is allowed or 0 otherwise. |
| 971 | */ |
| 972 | static int |
markus@openbsd.org | 54d90ac | 2017-05-30 08:52:19 +0000 | [diff] [blame] | 973 | user_key_allowed2(struct passwd *pw, struct sshkey *key, char *file) |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 974 | { |
| 975 | FILE *f; |
| 976 | int found_key = 0; |
| 977 | |
| 978 | /* Temporarily use the user's uid. */ |
| 979 | temporarily_use_uid(pw); |
| 980 | |
| 981 | debug("trying public key file %s", file); |
| 982 | if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) { |
| 983 | found_key = check_authkeys_file(f, file, key, pw); |
| 984 | fclose(f); |
| 985 | } |
| 986 | |
| 987 | restore_uid(); |
| 988 | return found_key; |
| 989 | } |
| 990 | |
| 991 | /* |
| 992 | * Checks whether key is allowed in output of command. |
| 993 | * returns 1 if the key is allowed or 0 otherwise. |
| 994 | */ |
| 995 | static int |
markus@openbsd.org | 54d90ac | 2017-05-30 08:52:19 +0000 | [diff] [blame] | 996 | user_key_command_allowed2(struct passwd *user_pw, struct sshkey *key) |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 997 | { |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 998 | FILE *f = NULL; |
| 999 | int r, ok, found_key = 0; |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 1000 | struct passwd *pw; |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 1001 | int i, uid_swapped = 0, ac = 0; |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 1002 | pid_t pid; |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 1003 | char *username = NULL, *key_fp = NULL, *keytext = NULL; |
| 1004 | char *tmp, *command = NULL, **av = NULL; |
| 1005 | void (*osigchld)(int); |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 1006 | |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 1007 | if (options.authorized_keys_command == NULL) |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 1008 | return 0; |
Damien Miller | d0d1099 | 2012-11-04 22:23:14 +1100 | [diff] [blame] | 1009 | if (options.authorized_keys_command_user == NULL) { |
| 1010 | error("No user for AuthorizedKeysCommand specified, skipping"); |
| 1011 | return 0; |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 1012 | } |
| 1013 | |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 1014 | /* |
| 1015 | * NB. all returns later this function should go via "out" to |
| 1016 | * ensure the original SIGCHLD handler is restored properly. |
| 1017 | */ |
| 1018 | osigchld = signal(SIGCHLD, SIG_DFL); |
| 1019 | |
| 1020 | /* Prepare and verify the user for the command */ |
Damien Miller | d0d1099 | 2012-11-04 22:23:14 +1100 | [diff] [blame] | 1021 | username = percent_expand(options.authorized_keys_command_user, |
| 1022 | "u", user_pw->pw_name, (char *)NULL); |
| 1023 | pw = getpwnam(username); |
| 1024 | if (pw == NULL) { |
Damien Miller | 4018dc0 | 2013-02-15 10:28:55 +1100 | [diff] [blame] | 1025 | error("AuthorizedKeysCommandUser \"%s\" not found: %s", |
| 1026 | username, strerror(errno)); |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 1027 | goto out; |
| 1028 | } |
| 1029 | |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 1030 | /* Prepare AuthorizedKeysCommand */ |
| 1031 | if ((key_fp = sshkey_fingerprint(key, options.fingerprint_hash, |
| 1032 | SSH_FP_DEFAULT)) == NULL) { |
| 1033 | error("%s: sshkey_fingerprint failed", __func__); |
| 1034 | goto out; |
| 1035 | } |
| 1036 | if ((r = sshkey_to_base64(key, &keytext)) != 0) { |
| 1037 | error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r)); |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 1038 | goto out; |
| 1039 | } |
| 1040 | |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 1041 | /* Turn the command into an argument vector */ |
| 1042 | if (split_argv(options.authorized_keys_command, &ac, &av) != 0) { |
| 1043 | error("AuthorizedKeysCommand \"%s\" contains invalid quotes", |
| 1044 | command); |
| 1045 | goto out; |
| 1046 | } |
| 1047 | if (ac == 0) { |
| 1048 | error("AuthorizedKeysCommand \"%s\" yielded no arguments", |
| 1049 | command); |
| 1050 | goto out; |
| 1051 | } |
| 1052 | for (i = 1; i < ac; i++) { |
| 1053 | tmp = percent_expand(av[i], |
| 1054 | "u", user_pw->pw_name, |
| 1055 | "h", user_pw->pw_dir, |
| 1056 | "t", sshkey_ssh_name(key), |
| 1057 | "f", key_fp, |
| 1058 | "k", keytext, |
| 1059 | (char *)NULL); |
| 1060 | if (tmp == NULL) |
| 1061 | fatal("%s: percent_expand failed", __func__); |
| 1062 | free(av[i]); |
| 1063 | av[i] = tmp; |
| 1064 | } |
| 1065 | /* Prepare a printable command for logs, etc. */ |
| 1066 | command = assemble_argv(ac, av); |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 1067 | |
| 1068 | /* |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 1069 | * If AuthorizedKeysCommand was run without arguments |
| 1070 | * then fall back to the old behaviour of passing the |
| 1071 | * target username as a single argument. |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 1072 | */ |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 1073 | if (ac == 1) { |
| 1074 | av = xreallocarray(av, ac + 2, sizeof(*av)); |
| 1075 | av[1] = xstrdup(user_pw->pw_name); |
| 1076 | av[2] = NULL; |
| 1077 | /* Fix up command too, since it is used in log messages */ |
| 1078 | free(command); |
| 1079 | xasprintf(&command, "%s %s", av[0], av[1]); |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 1080 | } |
| 1081 | |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 1082 | if ((pid = subprocess("AuthorizedKeysCommand", pw, command, |
| 1083 | ac, av, &f)) == 0) |
| 1084 | goto out; |
| 1085 | |
| 1086 | uid_swapped = 1; |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 1087 | temporarily_use_uid(pw); |
| 1088 | |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 1089 | ok = check_authkeys_file(f, options.authorized_keys_command, key, pw); |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 1090 | |
djm@openbsd.org | ddd3d34 | 2016-12-30 22:08:02 +0000 | [diff] [blame] | 1091 | fclose(f); |
| 1092 | f = NULL; |
| 1093 | |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 1094 | if (exited_cleanly(pid, "AuthorizedKeysCommand", command) != 0) |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 1095 | goto out; |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 1096 | |
| 1097 | /* Read completed successfully */ |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 1098 | found_key = ok; |
| 1099 | out: |
djm@openbsd.org | 24232a3 | 2015-05-21 06:38:35 +0000 | [diff] [blame] | 1100 | if (f != NULL) |
| 1101 | fclose(f); |
| 1102 | signal(SIGCHLD, osigchld); |
| 1103 | for (i = 0; i < ac; i++) |
| 1104 | free(av[i]); |
| 1105 | free(av); |
| 1106 | if (uid_swapped) |
| 1107 | restore_uid(); |
| 1108 | free(command); |
| 1109 | free(username); |
| 1110 | free(key_fp); |
| 1111 | free(keytext); |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 1112 | return found_key; |
| 1113 | } |
| 1114 | |
| 1115 | /* |
| 1116 | * Check whether key authenticates and authorises the user. |
| 1117 | */ |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 1118 | int |
markus@openbsd.org | 54d90ac | 2017-05-30 08:52:19 +0000 | [diff] [blame] | 1119 | user_key_allowed(struct passwd *pw, struct sshkey *key, int auth_attempt) |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 1120 | { |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 1121 | u_int success, i; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 1122 | char *file; |
| 1123 | |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 1124 | if (auth_key_is_revoked(key)) |
| 1125 | return 0; |
markus@openbsd.org | 00ed75c | 2017-05-30 14:10:53 +0000 | [diff] [blame] | 1126 | if (sshkey_is_cert(key) && |
| 1127 | auth_key_is_revoked(key->cert->signature_key)) |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 1128 | return 0; |
| 1129 | |
| 1130 | success = user_cert_trusted_ca(pw, key); |
| 1131 | if (success) |
| 1132 | return success; |
| 1133 | |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 1134 | success = user_key_command_allowed2(pw, key); |
| 1135 | if (success > 0) |
| 1136 | return success; |
| 1137 | |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 1138 | for (i = 0; !success && i < options.num_authkeys_files; i++) { |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 1139 | |
| 1140 | if (strcasecmp(options.authorized_keys_files[i], "none") == 0) |
| 1141 | continue; |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 1142 | file = expand_authorized_keys( |
| 1143 | options.authorized_keys_files[i], pw); |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 1144 | |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 1145 | success = user_key_allowed2(pw, key, file); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1146 | free(file); |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 1147 | } |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 1148 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 1149 | return success; |
| 1150 | } |
| 1151 | |
djm@openbsd.org | f69b69b | 2014-12-22 07:51:30 +0000 | [diff] [blame] | 1152 | /* Records a public key in the list of previously-successful keys */ |
| 1153 | void |
| 1154 | auth2_record_userkey(Authctxt *authctxt, struct sshkey *key) |
| 1155 | { |
| 1156 | struct sshkey **tmp; |
| 1157 | |
| 1158 | if (authctxt->nprev_userkeys >= INT_MAX || |
| 1159 | (tmp = reallocarray(authctxt->prev_userkeys, |
| 1160 | authctxt->nprev_userkeys + 1, sizeof(*tmp))) == NULL) |
| 1161 | fatal("%s: reallocarray failed", __func__); |
| 1162 | authctxt->prev_userkeys = tmp; |
| 1163 | authctxt->prev_userkeys[authctxt->nprev_userkeys] = key; |
| 1164 | authctxt->nprev_userkeys++; |
| 1165 | } |
| 1166 | |
| 1167 | /* Checks whether a key has already been used successfully for authentication */ |
| 1168 | int |
| 1169 | auth2_userkey_already_used(Authctxt *authctxt, struct sshkey *key) |
| 1170 | { |
| 1171 | u_int i; |
| 1172 | |
| 1173 | for (i = 0; i < authctxt->nprev_userkeys; i++) { |
| 1174 | if (sshkey_equal_public(key, authctxt->prev_userkeys[i])) { |
| 1175 | return 1; |
| 1176 | } |
| 1177 | } |
| 1178 | return 0; |
| 1179 | } |
| 1180 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 1181 | Authmethod method_pubkey = { |
| 1182 | "publickey", |
| 1183 | userauth_pubkey, |
| 1184 | &options.pubkey_authentication |
| 1185 | }; |