blob: 169839b01ed7119552176836ff6e89ec80ad5010 [file] [log] [blame]
djm@openbsd.orgabd59662017-09-07 23:48:09 +00001/* $OpenBSD: auth2-pubkey.c,v 1.71 2017/09/07 23:48:09 djm Exp $ */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00002/*
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 Millerf17883e2006-03-15 11:45:54 +110027
28#include <sys/types.h>
29#include <sys/stat.h>
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000030
Damien Miller09d3e122012-10-31 08:58:58 +110031#include <errno.h>
Darren Tucker06db5842008-06-13 14:51:28 +100032#include <fcntl.h>
Darren Tucker737f7af2012-11-05 17:07:43 +110033#ifdef HAVE_PATHS_H
34# include <paths.h>
35#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100036#include <pwd.h>
Damien Miller09d3e122012-10-31 08:58:58 +110037#include <signal.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100038#include <stdio.h>
Damien Millerd7834352006-08-05 12:39:39 +100039#include <stdarg.h>
Damien Miller0a80ca12010-02-27 07:55:05 +110040#include <string.h>
41#include <time.h>
Darren Tuckerd9526a52008-06-14 09:01:24 +100042#include <unistd.h>
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +000043#include <limits.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100044
Damien Millerd7834352006-08-05 12:39:39 +100045#include "xmalloc.h"
Darren Tucker22cc7412004-12-06 22:47:41 +110046#include "ssh.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000047#include "ssh2.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000048#include "packet.h"
49#include "buffer.h"
50#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100051#include "misc.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000052#include "servconf.h"
53#include "compat.h"
markus@openbsd.org00ed75c2017-05-30 14:10:53 +000054#include "sshkey.h"
Damien Millerd7834352006-08-05 12:39:39 +100055#include "hostfile.h"
56#include "auth.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000057#include "pathnames.h"
58#include "uidswap.h"
59#include "auth-options.h"
60#include "canohost.h"
Damien Millerd7834352006-08-05 12:39:39 +100061#ifdef GSSAPI
62#include "ssh-gss.h"
63#endif
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000064#include "monitor_wrap.h"
Damien Miller1aed65e2010-03-04 21:53:35 +110065#include "authfile.h"
Damien Miller30da3442010-05-10 11:58:03 +100066#include "match.h"
djm@openbsd.org24232a32015-05-21 06:38:35 +000067#include "ssherr.h"
68#include "channels.h" /* XXX for session.h */
69#include "session.h" /* XXX for child_set_env(); refactor? */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000070
71/* import */
72extern ServerOptions options;
73extern u_char *session_id2;
Darren Tucker502d3842003-06-28 12:38:01 +100074extern u_int session_id2_len;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000075
76static int
markus@openbsd.orgeb272ea2017-05-30 14:29:59 +000077userauth_pubkey(struct ssh *ssh)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000078{
markus@openbsd.orgeb272ea2017-05-30 14:29:59 +000079 Authctxt *authctxt = ssh->authctxt;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +000080 struct sshbuf *b;
markus@openbsd.org54d90ac2017-05-30 08:52:19 +000081 struct sshkey *key = NULL;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +000082 char *pkalg, *userstyle = NULL, *fp = NULL;
83 u_char *pkblob, *sig, have_sig;
84 size_t blen, slen;
85 int r, pktype;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000086 int authenticated = 0;
87
88 if (!authctxt->valid) {
djm@openbsd.orgebacd372016-01-27 00:53:12 +000089 debug2("%s: disabled because of invalid user", __func__);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000090 return 0;
91 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +000092 if ((r = sshpkt_get_u8(ssh, &have_sig)) != 0)
93 fatal("%s: sshpkt_get_u8 failed: %s", __func__, ssh_err(r));
94 if (ssh->compat & SSH_BUG_PKAUTH) {
djm@openbsd.orgebacd372016-01-27 00:53:12 +000095 debug2("%s: SSH_BUG_PKAUTH", __func__);
markus@openbsd.org00ed75c2017-05-30 14:10:53 +000096 if ((b = sshbuf_new()) == NULL)
97 fatal("%s: sshbuf_new failed", __func__);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000098 /* no explicit pkalg given */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000099 /* so we have to extract the pkalg from the pkblob */
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000100 /* XXX use sshbuf_from() */
101 if ((r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0 ||
102 (r = sshbuf_put(b, pkblob, blen)) != 0 ||
103 (r = sshbuf_get_cstring(b, &pkalg, NULL)) != 0)
104 fatal("%s: failed: %s", __func__, ssh_err(r));
105 sshbuf_free(b);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000106 } else {
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000107 if ((r = sshpkt_get_cstring(ssh, &pkalg, NULL)) != 0 ||
108 (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0)
109 fatal("%s: sshpkt_get_cstring failed: %s",
110 __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000111 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000112 pktype = sshkey_type_from_name(pkalg);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000113 if (pktype == KEY_UNSPEC) {
114 /* this is perfectly legal */
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000115 logit("%s: unsupported public key algorithm: %s",
116 __func__, pkalg);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000117 goto done;
118 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000119 if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
120 error("%s: could not parse key: %s", __func__, ssh_err(r));
121 goto done;
122 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000123 if (key == NULL) {
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000124 error("%s: cannot decode key: %s", __func__, pkalg);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000125 goto done;
126 }
127 if (key->type != pktype) {
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000128 error("%s: type mismatch for decoded key "
129 "(received %d, expected %d)", __func__, key->type, pktype);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000130 goto done;
131 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000132 if (sshkey_type_plain(key->type) == KEY_RSA &&
133 (ssh->compat & SSH_BUG_RSASIGMD5) != 0) {
Damien Miller324541e2013-12-31 12:25:40 +1100134 logit("Refusing RSA key because client uses unsafe "
135 "signature scheme");
136 goto done;
137 }
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000138 fp = sshkey_fingerprint(key, options.fingerprint_hash, SSH_FP_DEFAULT);
djm@openbsd.org8f574952017-06-24 06:34:38 +0000139 if (auth2_key_already_used(authctxt, key)) {
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000140 logit("refusing previously-used %s key", sshkey_type(key));
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +0000141 goto done;
142 }
djm@openbsd.orge661a862015-05-04 06:10:48 +0000143 if (match_pattern_list(sshkey_ssh_name(key),
144 options.pubkey_key_types, 0) != 1) {
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000145 logit("%s: key type %s not in PubkeyAcceptedKeyTypes",
146 __func__, sshkey_ssh_name(key));
147 goto done;
148 }
149
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000150 if (have_sig) {
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000151 debug3("%s: have signature for %s %s",
152 __func__, sshkey_type(key), fp);
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000153 if ((r = sshpkt_get_string(ssh, &sig, &slen)) != 0 ||
154 (r = sshpkt_get_end(ssh)) != 0)
155 fatal("%s: %s", __func__, ssh_err(r));
156 if ((b = sshbuf_new()) == NULL)
157 fatal("%s: sshbuf_new failed", __func__);
158 if (ssh->compat & SSH_OLD_SESSIONID) {
159 if ((r = sshbuf_put(b, session_id2,
160 session_id2_len)) != 0)
161 fatal("%s: sshbuf_put session id: %s",
162 __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000163 } else {
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000164 if ((r = sshbuf_put_string(b, session_id2,
165 session_id2_len)) != 0)
166 fatal("%s: sshbuf_put_string session id: %s",
167 __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000168 }
169 /* reconstruct packet */
Damien Miller4ce189d2013-04-23 15:17:52 +1000170 xasprintf(&userstyle, "%s%s%s", authctxt->user,
171 authctxt->style ? ":" : "",
172 authctxt->style ? authctxt->style : "");
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000173 if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
174 (r = sshbuf_put_cstring(b, userstyle)) != 0 ||
175 (r = sshbuf_put_cstring(b, ssh->compat & SSH_BUG_PKSERVICE ?
176 "ssh-userauth" : authctxt->service)) != 0)
177 fatal("%s: build packet failed: %s",
178 __func__, ssh_err(r));
179 if (ssh->compat & SSH_BUG_PKAUTH) {
180 if ((r = sshbuf_put_u8(b, have_sig)) != 0)
181 fatal("%s: build packet failed: %s",
182 __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000183 } else {
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000184 if ((r = sshbuf_put_cstring(b, "publickey")) != 0 ||
185 (r = sshbuf_put_u8(b, have_sig)) != 0 ||
186 (r = sshbuf_put_cstring(b, pkalg) != 0))
187 fatal("%s: build packet failed: %s",
188 __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000189 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000190 if ((r = sshbuf_put_string(b, pkblob, blen)) != 0)
191 fatal("%s: build packet failed: %s",
192 __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000193#ifdef DEBUG_PK
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000194 sshbuf_dump(b, stderr);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000195#endif
Darren Tucker74836ae2013-06-02 07:32:00 +1000196
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000197 /* test for correct signature */
198 authenticated = 0;
djm@openbsd.org179be0f2015-05-01 03:23:51 +0000199 if (PRIVSEP(user_key_allowed(authctxt->pw, key, 1)) &&
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000200 PRIVSEP(sshkey_verify(key, sig, slen, sshbuf_ptr(b),
201 sshbuf_len(b), ssh->compat)) == 0) {
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000202 authenticated = 1;
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +0000203 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000204 sshbuf_free(b);
Darren Tuckera627d422013-06-02 07:31:17 +1000205 free(sig);
djm@openbsd.org8f574952017-06-24 06:34:38 +0000206 auth2_record_key(authctxt, authenticated, key);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000207 } else {
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000208 debug("%s: test whether pkalg/pkblob are acceptable for %s %s",
209 __func__, sshkey_type(key), fp);
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000210 if ((r = sshpkt_get_end(ssh)) != 0)
211 fatal("%s: %s", __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000212
213 /* XXX fake reply and always send PK_OK ? */
214 /*
215 * XXX this allows testing whether a user is allowed
216 * to login: if you happen to have a valid pubkey this
217 * message is sent. the message is NEVER sent at all
218 * if a user is not allowed to login. is this an
219 * issue? -markus
220 */
djm@openbsd.org179be0f2015-05-01 03:23:51 +0000221 if (PRIVSEP(user_key_allowed(authctxt->pw, key, 0))) {
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000222 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_PK_OK))
223 != 0 ||
224 (r = sshpkt_put_cstring(ssh, pkalg)) != 0 ||
225 (r = sshpkt_put_string(ssh, pkblob, blen)) != 0 ||
226 (r = sshpkt_send(ssh)) != 0)
227 fatal("%s: %s", __func__, ssh_err(r));
228 ssh_packet_write_wait(ssh);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000229 authctxt->postponed = 1;
230 }
231 }
232 if (authenticated != 1)
233 auth_clear_options();
234done:
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000235 debug2("%s: authenticated %d pkalg %s", __func__, authenticated, pkalg);
djm@openbsd.org8f574952017-06-24 06:34:38 +0000236 sshkey_free(key);
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000237 free(userstyle);
Darren Tuckera627d422013-06-02 07:31:17 +1000238 free(pkalg);
239 free(pkblob);
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000240 free(fp);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000241 return authenticated;
242}
243
Damien Miller30da3442010-05-10 11:58:03 +1000244static int
Damien Miller86687062014-07-02 15:28:02 +1000245match_principals_option(const char *principal_list, struct sshkey_cert *cert)
Damien Miller30da3442010-05-10 11:58:03 +1000246{
247 char *result;
248 u_int i;
249
250 /* XXX percent_expand() sequences for authorized_principals? */
251
252 for (i = 0; i < cert->nprincipals; i++) {
253 if ((result = match_list(cert->principals[i],
254 principal_list, NULL)) != NULL) {
255 debug3("matched principal from key options \"%.100s\"",
256 result);
Darren Tuckera627d422013-06-02 07:31:17 +1000257 free(result);
Damien Miller30da3442010-05-10 11:58:03 +1000258 return 1;
259 }
260 }
261 return 0;
262}
263
264static int
markus@openbsd.org75b8af82017-05-31 10:54:00 +0000265process_principals(FILE *f, const char *file, struct passwd *pw,
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000266 const struct sshkey_cert *cert)
Damien Miller30da3442010-05-10 11:58:03 +1000267{
Damien Miller6018a362010-07-02 13:35:19 +1000268 char line[SSH_MAX_PUBKEY_BYTES], *cp, *ep, *line_opts;
Damien Miller30da3442010-05-10 11:58:03 +1000269 u_long linenum = 0;
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000270 u_int i, found_principal = 0;
Damien Miller30da3442010-05-10 11:58:03 +1000271
Damien Miller30da3442010-05-10 11:58:03 +1000272 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000273 /* Always consume entire input */
274 if (found_principal)
275 continue;
Damien Miller6018a362010-07-02 13:35:19 +1000276 /* Skip leading whitespace. */
Damien Miller30da3442010-05-10 11:58:03 +1000277 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
278 ;
Damien Miller6018a362010-07-02 13:35:19 +1000279 /* Skip blank and comment lines. */
280 if ((ep = strchr(cp, '#')) != NULL)
281 *ep = '\0';
282 if (!*cp || *cp == '\n')
Damien Miller30da3442010-05-10 11:58:03 +1000283 continue;
Damien Miller6018a362010-07-02 13:35:19 +1000284 /* Trim trailing whitespace. */
285 ep = cp + strlen(cp) - 1;
286 while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t'))
287 *ep-- = '\0';
288 /*
289 * If the line has internal whitespace then assume it has
290 * key options.
291 */
292 line_opts = NULL;
293 if ((ep = strrchr(cp, ' ')) != NULL ||
294 (ep = strrchr(cp, '\t')) != NULL) {
295 for (; *ep == ' ' || *ep == '\t'; ep++)
Damien Miller188ea812010-12-01 11:50:14 +1100296 ;
Damien Miller6018a362010-07-02 13:35:19 +1000297 line_opts = cp;
298 cp = ep;
299 }
Damien Miller30da3442010-05-10 11:58:03 +1000300 for (i = 0; i < cert->nprincipals; i++) {
301 if (strcmp(cp, cert->principals[i]) == 0) {
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000302 debug3("%s:%lu: matched principal \"%.100s\"",
markus@openbsd.org75b8af82017-05-31 10:54:00 +0000303 file, linenum, cert->principals[i]);
Damien Miller6018a362010-07-02 13:35:19 +1000304 if (auth_parse_options(pw, line_opts,
305 file, linenum) != 1)
306 continue;
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000307 found_principal = 1;
308 continue;
Damien Miller30da3442010-05-10 11:58:03 +1000309 }
310 }
311 }
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000312 return found_principal;
Damien Miller09d3e122012-10-31 08:58:58 +1100313}
Damien Miller30da3442010-05-10 11:58:03 +1000314
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000315static int
316match_principals_file(char *file, struct passwd *pw, struct sshkey_cert *cert)
317{
318 FILE *f;
319 int success;
320
321 temporarily_use_uid(pw);
322 debug("trying authorized principals file %s", file);
323 if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) {
324 restore_uid();
325 return 0;
326 }
327 success = process_principals(f, file, pw, cert);
328 fclose(f);
329 restore_uid();
330 return success;
331}
332
333/*
334 * Checks whether principal is allowed in output of command.
335 * returns 1 if the principal is allowed or 0 otherwise.
336 */
337static int
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000338match_principals_command(struct passwd *user_pw, const struct sshkey *key)
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000339{
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000340 const struct sshkey_cert *cert = key->cert;
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000341 FILE *f = NULL;
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000342 int r, ok, found_principal = 0;
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000343 struct passwd *pw;
344 int i, ac = 0, uid_swapped = 0;
345 pid_t pid;
346 char *tmp, *username = NULL, *command = NULL, **av = NULL;
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000347 char *ca_fp = NULL, *key_fp = NULL, *catext = NULL, *keytext = NULL;
djm@openbsd.orgbfa9d962016-09-21 01:34:45 +0000348 char serial_s[16];
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000349 void (*osigchld)(int);
350
351 if (options.authorized_principals_command == NULL)
352 return 0;
353 if (options.authorized_principals_command_user == NULL) {
354 error("No user for AuthorizedPrincipalsCommand specified, "
355 "skipping");
356 return 0;
357 }
358
359 /*
360 * NB. all returns later this function should go via "out" to
361 * ensure the original SIGCHLD handler is restored properly.
362 */
363 osigchld = signal(SIGCHLD, SIG_DFL);
364
365 /* Prepare and verify the user for the command */
366 username = percent_expand(options.authorized_principals_command_user,
367 "u", user_pw->pw_name, (char *)NULL);
368 pw = getpwnam(username);
369 if (pw == NULL) {
370 error("AuthorizedPrincipalsCommandUser \"%s\" not found: %s",
371 username, strerror(errno));
372 goto out;
373 }
374
375 /* Turn the command into an argument vector */
djm@openbsd.orgde4ae072017-08-18 05:36:45 +0000376 if (argv_split(options.authorized_principals_command, &ac, &av) != 0) {
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000377 error("AuthorizedPrincipalsCommand \"%s\" contains "
378 "invalid quotes", command);
379 goto out;
380 }
381 if (ac == 0) {
382 error("AuthorizedPrincipalsCommand \"%s\" yielded no arguments",
383 command);
384 goto out;
385 }
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000386 if ((ca_fp = sshkey_fingerprint(cert->signature_key,
387 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
388 error("%s: sshkey_fingerprint failed", __func__);
389 goto out;
390 }
djm@openbsd.org00df97f2016-09-14 20:11:26 +0000391 if ((key_fp = sshkey_fingerprint(key,
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000392 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
393 error("%s: sshkey_fingerprint failed", __func__);
394 goto out;
395 }
396 if ((r = sshkey_to_base64(cert->signature_key, &catext)) != 0) {
397 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r));
398 goto out;
399 }
400 if ((r = sshkey_to_base64(key, &keytext)) != 0) {
401 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r));
402 goto out;
403 }
djm@openbsd.orgf83a0cf2016-09-21 17:44:20 +0000404 snprintf(serial_s, sizeof(serial_s), "%llu",
405 (unsigned long long)cert->serial);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000406 for (i = 1; i < ac; i++) {
407 tmp = percent_expand(av[i],
408 "u", user_pw->pw_name,
409 "h", user_pw->pw_dir,
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000410 "t", sshkey_ssh_name(key),
411 "T", sshkey_ssh_name(cert->signature_key),
412 "f", key_fp,
413 "F", ca_fp,
414 "k", keytext,
415 "K", catext,
djm@openbsd.orgbfa9d962016-09-21 01:34:45 +0000416 "i", cert->key_id,
417 "s", serial_s,
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000418 (char *)NULL);
419 if (tmp == NULL)
420 fatal("%s: percent_expand failed", __func__);
421 free(av[i]);
422 av[i] = tmp;
423 }
424 /* Prepare a printable command for logs, etc. */
djm@openbsd.orgde4ae072017-08-18 05:36:45 +0000425 command = argv_assemble(ac, av);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000426
427 if ((pid = subprocess("AuthorizedPrincipalsCommand", pw, command,
djm@openbsd.orgde4ae072017-08-18 05:36:45 +0000428 ac, av, &f,
429 SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD)) == 0)
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000430 goto out;
431
432 uid_swapped = 1;
433 temporarily_use_uid(pw);
434
markus@openbsd.org75b8af82017-05-31 10:54:00 +0000435 ok = process_principals(f, "(command)", pw, cert);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000436
djm@openbsd.orgddd3d342016-12-30 22:08:02 +0000437 fclose(f);
438 f = NULL;
439
djm@openbsd.orgb074c3c2017-08-18 05:48:04 +0000440 if (exited_cleanly(pid, "AuthorizedPrincipalsCommand", command, 0) != 0)
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000441 goto out;
442
443 /* Read completed successfully */
444 found_principal = ok;
445 out:
446 if (f != NULL)
447 fclose(f);
448 signal(SIGCHLD, osigchld);
449 for (i = 0; i < ac; i++)
450 free(av[i]);
451 free(av);
452 if (uid_swapped)
453 restore_uid();
454 free(command);
455 free(username);
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000456 free(ca_fp);
457 free(key_fp);
458 free(catext);
459 free(keytext);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000460 return found_principal;
461}
Damien Miller09d3e122012-10-31 08:58:58 +1100462/*
463 * Checks whether key is allowed in authorized_keys-format file,
464 * returns 1 if the key is allowed or 0 otherwise.
465 */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000466static int
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000467check_authkeys_file(FILE *f, char *file, struct sshkey *key, struct passwd *pw)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000468{
Darren Tucker22cc7412004-12-06 22:47:41 +1100469 char line[SSH_MAX_PUBKEY_BYTES];
Darren Tucker33c787f2008-07-02 22:37:30 +1000470 int found_key = 0;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000471 u_long linenum = 0;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000472 struct sshkey *found = NULL;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000473
Darren Tucker22cc7412004-12-06 22:47:41 +1100474 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
djm@openbsd.orgfd6dcef2016-11-30 02:57:40 +0000475 char *cp, *key_options = NULL, *fp = NULL;
476 const char *reason = NULL;
477
djm@openbsd.orgabd59662017-09-07 23:48:09 +0000478 /* Always consume entire file */
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000479 if (found_key)
480 continue;
Darren Tucker74836ae2013-06-02 07:32:00 +1000481 if (found != NULL)
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000482 sshkey_free(found);
483 found = sshkey_new(sshkey_is_cert(key) ? KEY_UNSPEC : key->type);
484 if (found == NULL)
485 goto done;
Damien Miller0a80ca12010-02-27 07:55:05 +1100486 auth_clear_options();
487
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000488 /* Skip leading whitespace, empty and comment lines. */
489 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
490 ;
491 if (!*cp || *cp == '\n' || *cp == '#')
492 continue;
493
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000494 if (sshkey_read(found, &cp) != 0) {
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000495 /* no key? check if there are options for this key */
496 int quoted = 0;
497 debug2("user_key_allowed: check options: '%s'", cp);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000498 key_options = cp;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000499 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
500 if (*cp == '\\' && cp[1] == '"')
501 cp++; /* Skip both */
502 else if (*cp == '"')
503 quoted = !quoted;
504 }
505 /* Skip remaining whitespace. */
506 for (; *cp == ' ' || *cp == '\t'; cp++)
507 ;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000508 if (sshkey_read(found, &cp) != 0) {
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000509 debug2("user_key_allowed: advance: '%s'", cp);
510 /* still no key? advance to next line*/
511 continue;
512 }
513 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000514 if (sshkey_is_cert(key)) {
515 if (!sshkey_equal(found, key->cert->signature_key))
Damien Miller0a80ca12010-02-27 07:55:05 +1100516 continue;
Damien Miller84399552010-05-21 14:58:12 +1000517 if (auth_parse_options(pw, key_options, file,
518 linenum) != 1)
519 continue;
520 if (!key_is_cert_authority)
521 continue;
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000522 if ((fp = sshkey_fingerprint(found,
523 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
524 continue;
Damien Millere513a912010-03-22 05:51:21 +1100525 debug("matching CA found: file %s, line %lu, %s %s",
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000526 file, linenum, sshkey_type(found), fp);
Damien Miller30da3442010-05-10 11:58:03 +1000527 /*
528 * If the user has specified a list of principals as
529 * a key option, then prefer that list to matching
530 * their username in the certificate principals list.
531 */
532 if (authorized_principals != NULL &&
533 !match_principals_option(authorized_principals,
534 key->cert)) {
535 reason = "Certificate does not contain an "
536 "authorized principal";
537 fail_reason:
Darren Tuckera627d422013-06-02 07:31:17 +1000538 free(fp);
Damien Miller0a80ca12010-02-27 07:55:05 +1100539 error("%s", reason);
540 auth_debug_add("%s", reason);
541 continue;
542 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000543 if (sshkey_cert_check_authority(key, 0, 0,
Damien Miller30da3442010-05-10 11:58:03 +1000544 authorized_principals == NULL ? pw->pw_name : NULL,
545 &reason) != 0)
546 goto fail_reason;
djm@openbsd.orgfd6dcef2016-11-30 02:57:40 +0000547 if (auth_cert_options(key, pw, &reason) != 0)
548 goto fail_reason;
djm@openbsd.org63d18812015-10-27 01:44:45 +0000549 verbose("Accepted certificate ID \"%s\" (serial %llu) "
Damien Millere513a912010-03-22 05:51:21 +1100550 "signed by %s CA %s via %s", key->cert->key_id,
djm@openbsd.org63d18812015-10-27 01:44:45 +0000551 (unsigned long long)key->cert->serial,
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000552 sshkey_type(found), fp, file);
Darren Tuckera627d422013-06-02 07:31:17 +1000553 free(fp);
Damien Miller0a80ca12010-02-27 07:55:05 +1100554 found_key = 1;
555 break;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000556 } else if (sshkey_equal(found, key)) {
Damien Miller84399552010-05-21 14:58:12 +1000557 if (auth_parse_options(pw, key_options, file,
558 linenum) != 1)
559 continue;
560 if (key_is_cert_authority)
561 continue;
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000562 if ((fp = sshkey_fingerprint(found,
563 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
564 continue;
Darren Tucker74836ae2013-06-02 07:32:00 +1000565 debug("matching key found: file %s, line %lu %s %s",
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000566 file, linenum, sshkey_type(found), fp);
Darren Tuckera627d422013-06-02 07:31:17 +1000567 free(fp);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000568 found_key = 1;
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000569 continue;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000570 }
571 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000572 done:
Darren Tucker74836ae2013-06-02 07:32:00 +1000573 if (found != NULL)
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000574 sshkey_free(found);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000575 if (!found_key)
576 debug2("key not found");
577 return found_key;
578}
579
Damien Miller1aed65e2010-03-04 21:53:35 +1100580/* Authenticate a certificate key against TrustedUserCAKeys */
581static int
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000582user_cert_trusted_ca(struct passwd *pw, struct sshkey *key)
Damien Miller1aed65e2010-03-04 21:53:35 +1100583{
Damien Miller30da3442010-05-10 11:58:03 +1000584 char *ca_fp, *principals_file = NULL;
Damien Miller1aed65e2010-03-04 21:53:35 +1100585 const char *reason;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000586 int r, ret = 0, found_principal = 0, use_authorized_principals;
Damien Miller1aed65e2010-03-04 21:53:35 +1100587
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000588 if (!sshkey_is_cert(key) || options.trusted_user_ca_keys == NULL)
Damien Miller1aed65e2010-03-04 21:53:35 +1100589 return 0;
590
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000591 if ((ca_fp = sshkey_fingerprint(key->cert->signature_key,
592 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
593 return 0;
Damien Miller1aed65e2010-03-04 21:53:35 +1100594
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000595 if ((r = sshkey_in_file(key->cert->signature_key,
596 options.trusted_user_ca_keys, 1, 0)) != 0) {
597 debug2("%s: CA %s %s is not listed in %s: %s", __func__,
598 sshkey_type(key->cert->signature_key), ca_fp,
599 options.trusted_user_ca_keys, ssh_err(r));
Damien Miller1aed65e2010-03-04 21:53:35 +1100600 goto out;
601 }
Damien Miller30da3442010-05-10 11:58:03 +1000602 /*
603 * If AuthorizedPrincipals is in use, then compare the certificate
604 * principals against the names in that file rather than matching
605 * against the username.
606 */
607 if ((principals_file = authorized_principals_file(pw)) != NULL) {
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000608 if (match_principals_file(principals_file, pw, key->cert))
609 found_principal = 1;
610 }
611 /* Try querying command if specified */
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000612 if (!found_principal && match_principals_command(pw, key))
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000613 found_principal = 1;
jsing@openbsd.org596dbca2015-06-15 18:44:22 +0000614 /* If principals file or command is specified, then require a match */
615 use_authorized_principals = principals_file != NULL ||
616 options.authorized_principals_command != NULL;
617 if (!found_principal && use_authorized_principals) {
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000618 reason = "Certificate does not contain an authorized principal";
Damien Miller30da3442010-05-10 11:58:03 +1000619 fail_reason:
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000620 error("%s", reason);
621 auth_debug_add("%s", reason);
622 goto out;
Damien Miller1aed65e2010-03-04 21:53:35 +1100623 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000624 if (sshkey_cert_check_authority(key, 0, 1,
jsing@openbsd.org596dbca2015-06-15 18:44:22 +0000625 use_authorized_principals ? NULL : pw->pw_name, &reason) != 0)
Damien Miller30da3442010-05-10 11:58:03 +1000626 goto fail_reason;
djm@openbsd.orgfd6dcef2016-11-30 02:57:40 +0000627 if (auth_cert_options(key, pw, &reason) != 0)
628 goto fail_reason;
Damien Miller1aed65e2010-03-04 21:53:35 +1100629
djm@openbsd.org63d18812015-10-27 01:44:45 +0000630 verbose("Accepted certificate ID \"%s\" (serial %llu) signed by "
631 "%s CA %s via %s", key->cert->key_id,
632 (unsigned long long)key->cert->serial,
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000633 sshkey_type(key->cert->signature_key), ca_fp,
Damien Millere513a912010-03-22 05:51:21 +1100634 options.trusted_user_ca_keys);
Damien Miller1aed65e2010-03-04 21:53:35 +1100635 ret = 1;
636
637 out:
Darren Tuckera627d422013-06-02 07:31:17 +1000638 free(principals_file);
639 free(ca_fp);
Damien Miller1aed65e2010-03-04 21:53:35 +1100640 return ret;
641}
642
Damien Miller09d3e122012-10-31 08:58:58 +1100643/*
644 * Checks whether key is allowed in file.
645 * returns 1 if the key is allowed or 0 otherwise.
646 */
647static int
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000648user_key_allowed2(struct passwd *pw, struct sshkey *key, char *file)
Damien Miller09d3e122012-10-31 08:58:58 +1100649{
650 FILE *f;
651 int found_key = 0;
652
653 /* Temporarily use the user's uid. */
654 temporarily_use_uid(pw);
655
656 debug("trying public key file %s", file);
657 if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) {
658 found_key = check_authkeys_file(f, file, key, pw);
659 fclose(f);
660 }
661
662 restore_uid();
663 return found_key;
664}
665
666/*
667 * Checks whether key is allowed in output of command.
668 * returns 1 if the key is allowed or 0 otherwise.
669 */
670static int
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000671user_key_command_allowed2(struct passwd *user_pw, struct sshkey *key)
Damien Miller09d3e122012-10-31 08:58:58 +1100672{
djm@openbsd.org24232a32015-05-21 06:38:35 +0000673 FILE *f = NULL;
674 int r, ok, found_key = 0;
Damien Miller09d3e122012-10-31 08:58:58 +1100675 struct passwd *pw;
djm@openbsd.org24232a32015-05-21 06:38:35 +0000676 int i, uid_swapped = 0, ac = 0;
Damien Miller09d3e122012-10-31 08:58:58 +1100677 pid_t pid;
djm@openbsd.org24232a32015-05-21 06:38:35 +0000678 char *username = NULL, *key_fp = NULL, *keytext = NULL;
679 char *tmp, *command = NULL, **av = NULL;
680 void (*osigchld)(int);
Damien Miller09d3e122012-10-31 08:58:58 +1100681
djm@openbsd.org24232a32015-05-21 06:38:35 +0000682 if (options.authorized_keys_command == NULL)
Damien Miller09d3e122012-10-31 08:58:58 +1100683 return 0;
Damien Millerd0d10992012-11-04 22:23:14 +1100684 if (options.authorized_keys_command_user == NULL) {
685 error("No user for AuthorizedKeysCommand specified, skipping");
686 return 0;
Damien Miller09d3e122012-10-31 08:58:58 +1100687 }
688
djm@openbsd.org24232a32015-05-21 06:38:35 +0000689 /*
690 * NB. all returns later this function should go via "out" to
691 * ensure the original SIGCHLD handler is restored properly.
692 */
693 osigchld = signal(SIGCHLD, SIG_DFL);
694
695 /* Prepare and verify the user for the command */
Damien Millerd0d10992012-11-04 22:23:14 +1100696 username = percent_expand(options.authorized_keys_command_user,
697 "u", user_pw->pw_name, (char *)NULL);
698 pw = getpwnam(username);
699 if (pw == NULL) {
Damien Miller4018dc02013-02-15 10:28:55 +1100700 error("AuthorizedKeysCommandUser \"%s\" not found: %s",
701 username, strerror(errno));
Damien Miller09d3e122012-10-31 08:58:58 +1100702 goto out;
703 }
704
djm@openbsd.org24232a32015-05-21 06:38:35 +0000705 /* Prepare AuthorizedKeysCommand */
706 if ((key_fp = sshkey_fingerprint(key, options.fingerprint_hash,
707 SSH_FP_DEFAULT)) == NULL) {
708 error("%s: sshkey_fingerprint failed", __func__);
709 goto out;
710 }
711 if ((r = sshkey_to_base64(key, &keytext)) != 0) {
712 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r));
Damien Miller09d3e122012-10-31 08:58:58 +1100713 goto out;
714 }
715
djm@openbsd.org24232a32015-05-21 06:38:35 +0000716 /* Turn the command into an argument vector */
djm@openbsd.orgde4ae072017-08-18 05:36:45 +0000717 if (argv_split(options.authorized_keys_command, &ac, &av) != 0) {
djm@openbsd.org24232a32015-05-21 06:38:35 +0000718 error("AuthorizedKeysCommand \"%s\" contains invalid quotes",
719 command);
720 goto out;
721 }
722 if (ac == 0) {
723 error("AuthorizedKeysCommand \"%s\" yielded no arguments",
724 command);
725 goto out;
726 }
727 for (i = 1; i < ac; i++) {
728 tmp = percent_expand(av[i],
729 "u", user_pw->pw_name,
730 "h", user_pw->pw_dir,
731 "t", sshkey_ssh_name(key),
732 "f", key_fp,
733 "k", keytext,
734 (char *)NULL);
735 if (tmp == NULL)
736 fatal("%s: percent_expand failed", __func__);
737 free(av[i]);
738 av[i] = tmp;
739 }
740 /* Prepare a printable command for logs, etc. */
djm@openbsd.orgde4ae072017-08-18 05:36:45 +0000741 command = argv_assemble(ac, av);
Damien Miller09d3e122012-10-31 08:58:58 +1100742
743 /*
djm@openbsd.org24232a32015-05-21 06:38:35 +0000744 * If AuthorizedKeysCommand was run without arguments
745 * then fall back to the old behaviour of passing the
746 * target username as a single argument.
Damien Miller09d3e122012-10-31 08:58:58 +1100747 */
djm@openbsd.org24232a32015-05-21 06:38:35 +0000748 if (ac == 1) {
749 av = xreallocarray(av, ac + 2, sizeof(*av));
750 av[1] = xstrdup(user_pw->pw_name);
751 av[2] = NULL;
752 /* Fix up command too, since it is used in log messages */
753 free(command);
754 xasprintf(&command, "%s %s", av[0], av[1]);
Damien Miller09d3e122012-10-31 08:58:58 +1100755 }
756
djm@openbsd.org24232a32015-05-21 06:38:35 +0000757 if ((pid = subprocess("AuthorizedKeysCommand", pw, command,
djm@openbsd.orgde4ae072017-08-18 05:36:45 +0000758 ac, av, &f,
759 SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD)) == 0)
djm@openbsd.org24232a32015-05-21 06:38:35 +0000760 goto out;
761
762 uid_swapped = 1;
Damien Miller09d3e122012-10-31 08:58:58 +1100763 temporarily_use_uid(pw);
764
Damien Miller09d3e122012-10-31 08:58:58 +1100765 ok = check_authkeys_file(f, options.authorized_keys_command, key, pw);
Damien Miller09d3e122012-10-31 08:58:58 +1100766
djm@openbsd.orgddd3d342016-12-30 22:08:02 +0000767 fclose(f);
768 f = NULL;
769
djm@openbsd.orgb074c3c2017-08-18 05:48:04 +0000770 if (exited_cleanly(pid, "AuthorizedKeysCommand", command, 0) != 0)
Damien Miller09d3e122012-10-31 08:58:58 +1100771 goto out;
djm@openbsd.org24232a32015-05-21 06:38:35 +0000772
773 /* Read completed successfully */
Damien Miller09d3e122012-10-31 08:58:58 +1100774 found_key = ok;
775 out:
djm@openbsd.org24232a32015-05-21 06:38:35 +0000776 if (f != NULL)
777 fclose(f);
778 signal(SIGCHLD, osigchld);
779 for (i = 0; i < ac; i++)
780 free(av[i]);
781 free(av);
782 if (uid_swapped)
783 restore_uid();
784 free(command);
785 free(username);
786 free(key_fp);
787 free(keytext);
Damien Miller09d3e122012-10-31 08:58:58 +1100788 return found_key;
789}
790
791/*
792 * Check whether key authenticates and authorises the user.
793 */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000794int
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000795user_key_allowed(struct passwd *pw, struct sshkey *key, int auth_attempt)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000796{
Damien Millerd8478b62011-05-29 21:39:36 +1000797 u_int success, i;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000798 char *file;
799
Damien Miller1aed65e2010-03-04 21:53:35 +1100800 if (auth_key_is_revoked(key))
801 return 0;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000802 if (sshkey_is_cert(key) &&
803 auth_key_is_revoked(key->cert->signature_key))
Damien Miller1aed65e2010-03-04 21:53:35 +1100804 return 0;
805
806 success = user_cert_trusted_ca(pw, key);
807 if (success)
808 return success;
809
Damien Miller09d3e122012-10-31 08:58:58 +1100810 success = user_key_command_allowed2(pw, key);
811 if (success > 0)
812 return success;
813
Damien Millerd8478b62011-05-29 21:39:36 +1000814 for (i = 0; !success && i < options.num_authkeys_files; i++) {
Damien Miller09d3e122012-10-31 08:58:58 +1100815
816 if (strcasecmp(options.authorized_keys_files[i], "none") == 0)
817 continue;
Damien Millerd8478b62011-05-29 21:39:36 +1000818 file = expand_authorized_keys(
819 options.authorized_keys_files[i], pw);
Damien Miller09d3e122012-10-31 08:58:58 +1100820
Damien Millerd8478b62011-05-29 21:39:36 +1000821 success = user_key_allowed2(pw, key, file);
Darren Tuckera627d422013-06-02 07:31:17 +1000822 free(file);
Damien Millerd8478b62011-05-29 21:39:36 +1000823 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000824
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000825 return success;
826}
827
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000828Authmethod method_pubkey = {
829 "publickey",
830 userauth_pubkey,
831 &options.pubkey_authentication
832};