blob: 0713a9de81d0fd5ef21b673c59056cd0760e80da [file] [log] [blame]
djm@openbsd.orgd45d69f2017-12-21 00:00:28 +00001/* $OpenBSD: auth2-pubkey.c,v 1.74 2017/12/21 00:00:28 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
djm@openbsd.org27885632017-12-19 00:24:34 +000076static char *
77format_key(const struct sshkey *key)
78{
79 char *ret, *fp = sshkey_fingerprint(key,
80 options.fingerprint_hash, SSH_FP_DEFAULT);
81
82 xasprintf(&ret, "%s %s", sshkey_type(key), fp);
83 free(fp);
84 return ret;
85}
86
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000087static int
markus@openbsd.orgeb272ea2017-05-30 14:29:59 +000088userauth_pubkey(struct ssh *ssh)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000089{
markus@openbsd.orgeb272ea2017-05-30 14:29:59 +000090 Authctxt *authctxt = ssh->authctxt;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +000091 struct sshbuf *b;
markus@openbsd.org54d90ac2017-05-30 08:52:19 +000092 struct sshkey *key = NULL;
djm@openbsd.org27885632017-12-19 00:24:34 +000093 char *pkalg, *userstyle = NULL, *key_s = NULL, *ca_s = NULL;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +000094 u_char *pkblob, *sig, have_sig;
95 size_t blen, slen;
96 int r, pktype;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000097 int authenticated = 0;
98
99 if (!authctxt->valid) {
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000100 debug2("%s: disabled because of invalid user", __func__);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000101 return 0;
102 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000103 if ((r = sshpkt_get_u8(ssh, &have_sig)) != 0)
104 fatal("%s: sshpkt_get_u8 failed: %s", __func__, ssh_err(r));
105 if (ssh->compat & SSH_BUG_PKAUTH) {
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000106 debug2("%s: SSH_BUG_PKAUTH", __func__);
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000107 if ((b = sshbuf_new()) == NULL)
108 fatal("%s: sshbuf_new failed", __func__);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000109 /* no explicit pkalg given */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000110 /* so we have to extract the pkalg from the pkblob */
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000111 /* XXX use sshbuf_from() */
112 if ((r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0 ||
113 (r = sshbuf_put(b, pkblob, blen)) != 0 ||
114 (r = sshbuf_get_cstring(b, &pkalg, NULL)) != 0)
115 fatal("%s: failed: %s", __func__, ssh_err(r));
116 sshbuf_free(b);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000117 } else {
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000118 if ((r = sshpkt_get_cstring(ssh, &pkalg, NULL)) != 0 ||
119 (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0)
120 fatal("%s: sshpkt_get_cstring failed: %s",
121 __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000122 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000123 pktype = sshkey_type_from_name(pkalg);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000124 if (pktype == KEY_UNSPEC) {
125 /* this is perfectly legal */
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000126 logit("%s: unsupported public key algorithm: %s",
127 __func__, pkalg);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000128 goto done;
129 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000130 if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
131 error("%s: could not parse key: %s", __func__, ssh_err(r));
132 goto done;
133 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000134 if (key == NULL) {
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000135 error("%s: cannot decode key: %s", __func__, pkalg);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000136 goto done;
137 }
138 if (key->type != pktype) {
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000139 error("%s: type mismatch for decoded key "
140 "(received %d, expected %d)", __func__, key->type, pktype);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000141 goto done;
142 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000143 if (sshkey_type_plain(key->type) == KEY_RSA &&
144 (ssh->compat & SSH_BUG_RSASIGMD5) != 0) {
Damien Miller324541e2013-12-31 12:25:40 +1100145 logit("Refusing RSA key because client uses unsafe "
146 "signature scheme");
147 goto done;
148 }
djm@openbsd.org8f574952017-06-24 06:34:38 +0000149 if (auth2_key_already_used(authctxt, key)) {
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000150 logit("refusing previously-used %s key", sshkey_type(key));
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +0000151 goto done;
152 }
djm@openbsd.orge661a862015-05-04 06:10:48 +0000153 if (match_pattern_list(sshkey_ssh_name(key),
154 options.pubkey_key_types, 0) != 1) {
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000155 logit("%s: key type %s not in PubkeyAcceptedKeyTypes",
156 __func__, sshkey_ssh_name(key));
157 goto done;
158 }
159
djm@openbsd.org27885632017-12-19 00:24:34 +0000160 key_s = format_key(key);
161 if (sshkey_is_cert(key))
162 ca_s = format_key(key->cert->signature_key);
163
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000164 if (have_sig) {
djm@openbsd.org27885632017-12-19 00:24:34 +0000165 debug3("%s: have %s signature for %s%s%s",
166 __func__, pkalg, key_s,
167 ca_s == NULL ? "" : " CA ",
168 ca_s == NULL ? "" : ca_s);
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000169 if ((r = sshpkt_get_string(ssh, &sig, &slen)) != 0 ||
170 (r = sshpkt_get_end(ssh)) != 0)
171 fatal("%s: %s", __func__, ssh_err(r));
172 if ((b = sshbuf_new()) == NULL)
173 fatal("%s: sshbuf_new failed", __func__);
174 if (ssh->compat & SSH_OLD_SESSIONID) {
175 if ((r = sshbuf_put(b, session_id2,
176 session_id2_len)) != 0)
177 fatal("%s: sshbuf_put session id: %s",
178 __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000179 } else {
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000180 if ((r = sshbuf_put_string(b, session_id2,
181 session_id2_len)) != 0)
182 fatal("%s: sshbuf_put_string session id: %s",
183 __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000184 }
185 /* reconstruct packet */
Damien Miller4ce189d2013-04-23 15:17:52 +1000186 xasprintf(&userstyle, "%s%s%s", authctxt->user,
187 authctxt->style ? ":" : "",
188 authctxt->style ? authctxt->style : "");
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000189 if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
190 (r = sshbuf_put_cstring(b, userstyle)) != 0 ||
191 (r = sshbuf_put_cstring(b, ssh->compat & SSH_BUG_PKSERVICE ?
192 "ssh-userauth" : authctxt->service)) != 0)
193 fatal("%s: build packet failed: %s",
194 __func__, ssh_err(r));
195 if (ssh->compat & SSH_BUG_PKAUTH) {
196 if ((r = sshbuf_put_u8(b, have_sig)) != 0)
197 fatal("%s: build packet failed: %s",
198 __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000199 } else {
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000200 if ((r = sshbuf_put_cstring(b, "publickey")) != 0 ||
201 (r = sshbuf_put_u8(b, have_sig)) != 0 ||
202 (r = sshbuf_put_cstring(b, pkalg) != 0))
203 fatal("%s: build packet failed: %s",
204 __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000205 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000206 if ((r = sshbuf_put_string(b, pkblob, blen)) != 0)
207 fatal("%s: build packet failed: %s",
208 __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000209#ifdef DEBUG_PK
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000210 sshbuf_dump(b, stderr);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000211#endif
Darren Tucker74836ae2013-06-02 07:32:00 +1000212
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000213 /* test for correct signature */
214 authenticated = 0;
djm@openbsd.org179be0f2015-05-01 03:23:51 +0000215 if (PRIVSEP(user_key_allowed(authctxt->pw, key, 1)) &&
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000216 PRIVSEP(sshkey_verify(key, sig, slen, sshbuf_ptr(b),
djm@openbsd.orgd45d69f2017-12-21 00:00:28 +0000217 sshbuf_len(b), NULL, ssh->compat)) == 0) {
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000218 authenticated = 1;
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +0000219 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000220 sshbuf_free(b);
Darren Tuckera627d422013-06-02 07:31:17 +1000221 free(sig);
djm@openbsd.org8f574952017-06-24 06:34:38 +0000222 auth2_record_key(authctxt, authenticated, key);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000223 } else {
djm@openbsd.org27885632017-12-19 00:24:34 +0000224 debug("%s: test pkalg %s pkblob %s%s%s",
225 __func__, pkalg, key_s,
226 ca_s == NULL ? "" : " CA ",
227 ca_s == NULL ? "" : ca_s);
228
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000229 if ((r = sshpkt_get_end(ssh)) != 0)
230 fatal("%s: %s", __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000231
232 /* XXX fake reply and always send PK_OK ? */
233 /*
234 * XXX this allows testing whether a user is allowed
235 * to login: if you happen to have a valid pubkey this
236 * message is sent. the message is NEVER sent at all
237 * if a user is not allowed to login. is this an
238 * issue? -markus
239 */
djm@openbsd.org179be0f2015-05-01 03:23:51 +0000240 if (PRIVSEP(user_key_allowed(authctxt->pw, key, 0))) {
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000241 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_PK_OK))
242 != 0 ||
243 (r = sshpkt_put_cstring(ssh, pkalg)) != 0 ||
244 (r = sshpkt_put_string(ssh, pkblob, blen)) != 0 ||
245 (r = sshpkt_send(ssh)) != 0)
246 fatal("%s: %s", __func__, ssh_err(r));
247 ssh_packet_write_wait(ssh);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000248 authctxt->postponed = 1;
249 }
250 }
251 if (authenticated != 1)
252 auth_clear_options();
253done:
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000254 debug2("%s: authenticated %d pkalg %s", __func__, authenticated, pkalg);
djm@openbsd.org8f574952017-06-24 06:34:38 +0000255 sshkey_free(key);
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000256 free(userstyle);
Darren Tuckera627d422013-06-02 07:31:17 +1000257 free(pkalg);
258 free(pkblob);
djm@openbsd.org27885632017-12-19 00:24:34 +0000259 free(key_s);
260 free(ca_s);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000261 return authenticated;
262}
263
Damien Miller30da3442010-05-10 11:58:03 +1000264static int
Damien Miller86687062014-07-02 15:28:02 +1000265match_principals_option(const char *principal_list, struct sshkey_cert *cert)
Damien Miller30da3442010-05-10 11:58:03 +1000266{
267 char *result;
268 u_int i;
269
270 /* XXX percent_expand() sequences for authorized_principals? */
271
272 for (i = 0; i < cert->nprincipals; i++) {
273 if ((result = match_list(cert->principals[i],
274 principal_list, NULL)) != NULL) {
275 debug3("matched principal from key options \"%.100s\"",
276 result);
Darren Tuckera627d422013-06-02 07:31:17 +1000277 free(result);
Damien Miller30da3442010-05-10 11:58:03 +1000278 return 1;
279 }
280 }
281 return 0;
282}
283
284static int
markus@openbsd.org75b8af82017-05-31 10:54:00 +0000285process_principals(FILE *f, const char *file, struct passwd *pw,
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000286 const struct sshkey_cert *cert)
Damien Miller30da3442010-05-10 11:58:03 +1000287{
Damien Miller6018a362010-07-02 13:35:19 +1000288 char line[SSH_MAX_PUBKEY_BYTES], *cp, *ep, *line_opts;
Damien Miller30da3442010-05-10 11:58:03 +1000289 u_long linenum = 0;
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000290 u_int i, found_principal = 0;
Damien Miller30da3442010-05-10 11:58:03 +1000291
Damien Miller30da3442010-05-10 11:58:03 +1000292 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000293 /* Always consume entire input */
294 if (found_principal)
295 continue;
Damien Miller6018a362010-07-02 13:35:19 +1000296 /* Skip leading whitespace. */
Damien Miller30da3442010-05-10 11:58:03 +1000297 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
298 ;
Damien Miller6018a362010-07-02 13:35:19 +1000299 /* Skip blank and comment lines. */
300 if ((ep = strchr(cp, '#')) != NULL)
301 *ep = '\0';
302 if (!*cp || *cp == '\n')
Damien Miller30da3442010-05-10 11:58:03 +1000303 continue;
Damien Miller6018a362010-07-02 13:35:19 +1000304 /* Trim trailing whitespace. */
305 ep = cp + strlen(cp) - 1;
306 while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t'))
307 *ep-- = '\0';
308 /*
309 * If the line has internal whitespace then assume it has
310 * key options.
311 */
312 line_opts = NULL;
313 if ((ep = strrchr(cp, ' ')) != NULL ||
314 (ep = strrchr(cp, '\t')) != NULL) {
315 for (; *ep == ' ' || *ep == '\t'; ep++)
Damien Miller188ea812010-12-01 11:50:14 +1100316 ;
Damien Miller6018a362010-07-02 13:35:19 +1000317 line_opts = cp;
318 cp = ep;
319 }
Damien Miller30da3442010-05-10 11:58:03 +1000320 for (i = 0; i < cert->nprincipals; i++) {
321 if (strcmp(cp, cert->principals[i]) == 0) {
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000322 debug3("%s:%lu: matched principal \"%.100s\"",
markus@openbsd.org75b8af82017-05-31 10:54:00 +0000323 file, linenum, cert->principals[i]);
Damien Miller6018a362010-07-02 13:35:19 +1000324 if (auth_parse_options(pw, line_opts,
325 file, linenum) != 1)
326 continue;
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000327 found_principal = 1;
328 continue;
Damien Miller30da3442010-05-10 11:58:03 +1000329 }
330 }
331 }
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000332 return found_principal;
Damien Miller09d3e122012-10-31 08:58:58 +1100333}
Damien Miller30da3442010-05-10 11:58:03 +1000334
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000335static int
336match_principals_file(char *file, struct passwd *pw, struct sshkey_cert *cert)
337{
338 FILE *f;
339 int success;
340
341 temporarily_use_uid(pw);
342 debug("trying authorized principals file %s", file);
343 if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) {
344 restore_uid();
345 return 0;
346 }
347 success = process_principals(f, file, pw, cert);
348 fclose(f);
349 restore_uid();
350 return success;
351}
352
353/*
354 * Checks whether principal is allowed in output of command.
355 * returns 1 if the principal is allowed or 0 otherwise.
356 */
357static int
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000358match_principals_command(struct passwd *user_pw, const struct sshkey *key)
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000359{
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000360 const struct sshkey_cert *cert = key->cert;
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000361 FILE *f = NULL;
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000362 int r, ok, found_principal = 0;
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000363 struct passwd *pw;
364 int i, ac = 0, uid_swapped = 0;
365 pid_t pid;
366 char *tmp, *username = NULL, *command = NULL, **av = NULL;
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000367 char *ca_fp = NULL, *key_fp = NULL, *catext = NULL, *keytext = NULL;
djm@openbsd.orgbfa9d962016-09-21 01:34:45 +0000368 char serial_s[16];
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000369 void (*osigchld)(int);
370
371 if (options.authorized_principals_command == NULL)
372 return 0;
373 if (options.authorized_principals_command_user == NULL) {
374 error("No user for AuthorizedPrincipalsCommand specified, "
375 "skipping");
376 return 0;
377 }
378
379 /*
380 * NB. all returns later this function should go via "out" to
381 * ensure the original SIGCHLD handler is restored properly.
382 */
383 osigchld = signal(SIGCHLD, SIG_DFL);
384
385 /* Prepare and verify the user for the command */
386 username = percent_expand(options.authorized_principals_command_user,
387 "u", user_pw->pw_name, (char *)NULL);
388 pw = getpwnam(username);
389 if (pw == NULL) {
390 error("AuthorizedPrincipalsCommandUser \"%s\" not found: %s",
391 username, strerror(errno));
392 goto out;
393 }
394
395 /* Turn the command into an argument vector */
djm@openbsd.orgde4ae072017-08-18 05:36:45 +0000396 if (argv_split(options.authorized_principals_command, &ac, &av) != 0) {
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000397 error("AuthorizedPrincipalsCommand \"%s\" contains "
398 "invalid quotes", command);
399 goto out;
400 }
401 if (ac == 0) {
402 error("AuthorizedPrincipalsCommand \"%s\" yielded no arguments",
403 command);
404 goto out;
405 }
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000406 if ((ca_fp = sshkey_fingerprint(cert->signature_key,
407 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
408 error("%s: sshkey_fingerprint failed", __func__);
409 goto out;
410 }
djm@openbsd.org00df97f2016-09-14 20:11:26 +0000411 if ((key_fp = sshkey_fingerprint(key,
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000412 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
413 error("%s: sshkey_fingerprint failed", __func__);
414 goto out;
415 }
416 if ((r = sshkey_to_base64(cert->signature_key, &catext)) != 0) {
417 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r));
418 goto out;
419 }
420 if ((r = sshkey_to_base64(key, &keytext)) != 0) {
421 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r));
422 goto out;
423 }
djm@openbsd.orgf83a0cf2016-09-21 17:44:20 +0000424 snprintf(serial_s, sizeof(serial_s), "%llu",
425 (unsigned long long)cert->serial);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000426 for (i = 1; i < ac; i++) {
427 tmp = percent_expand(av[i],
428 "u", user_pw->pw_name,
429 "h", user_pw->pw_dir,
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000430 "t", sshkey_ssh_name(key),
431 "T", sshkey_ssh_name(cert->signature_key),
432 "f", key_fp,
433 "F", ca_fp,
434 "k", keytext,
435 "K", catext,
djm@openbsd.orgbfa9d962016-09-21 01:34:45 +0000436 "i", cert->key_id,
437 "s", serial_s,
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000438 (char *)NULL);
439 if (tmp == NULL)
440 fatal("%s: percent_expand failed", __func__);
441 free(av[i]);
442 av[i] = tmp;
443 }
444 /* Prepare a printable command for logs, etc. */
djm@openbsd.orgde4ae072017-08-18 05:36:45 +0000445 command = argv_assemble(ac, av);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000446
447 if ((pid = subprocess("AuthorizedPrincipalsCommand", pw, command,
djm@openbsd.orgde4ae072017-08-18 05:36:45 +0000448 ac, av, &f,
449 SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD)) == 0)
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000450 goto out;
451
452 uid_swapped = 1;
453 temporarily_use_uid(pw);
454
markus@openbsd.org75b8af82017-05-31 10:54:00 +0000455 ok = process_principals(f, "(command)", pw, cert);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000456
djm@openbsd.orgddd3d342016-12-30 22:08:02 +0000457 fclose(f);
458 f = NULL;
459
djm@openbsd.orgb074c3c2017-08-18 05:48:04 +0000460 if (exited_cleanly(pid, "AuthorizedPrincipalsCommand", command, 0) != 0)
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000461 goto out;
462
463 /* Read completed successfully */
464 found_principal = ok;
465 out:
466 if (f != NULL)
467 fclose(f);
468 signal(SIGCHLD, osigchld);
469 for (i = 0; i < ac; i++)
470 free(av[i]);
471 free(av);
472 if (uid_swapped)
473 restore_uid();
474 free(command);
475 free(username);
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000476 free(ca_fp);
477 free(key_fp);
478 free(catext);
479 free(keytext);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000480 return found_principal;
481}
Damien Miller09d3e122012-10-31 08:58:58 +1100482/*
483 * Checks whether key is allowed in authorized_keys-format file,
484 * returns 1 if the key is allowed or 0 otherwise.
485 */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000486static int
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000487check_authkeys_file(FILE *f, char *file, struct sshkey *key, struct passwd *pw)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000488{
Darren Tucker22cc7412004-12-06 22:47:41 +1100489 char line[SSH_MAX_PUBKEY_BYTES];
Darren Tucker33c787f2008-07-02 22:37:30 +1000490 int found_key = 0;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000491 u_long linenum = 0;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000492 struct sshkey *found = NULL;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000493
Darren Tucker22cc7412004-12-06 22:47:41 +1100494 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
djm@openbsd.orgfd6dcef2016-11-30 02:57:40 +0000495 char *cp, *key_options = NULL, *fp = NULL;
496 const char *reason = NULL;
497
djm@openbsd.orgabd59662017-09-07 23:48:09 +0000498 /* Always consume entire file */
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000499 if (found_key)
500 continue;
Darren Tucker74836ae2013-06-02 07:32:00 +1000501 if (found != NULL)
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000502 sshkey_free(found);
503 found = sshkey_new(sshkey_is_cert(key) ? KEY_UNSPEC : key->type);
504 if (found == NULL)
505 goto done;
Damien Miller0a80ca12010-02-27 07:55:05 +1100506 auth_clear_options();
507
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000508 /* Skip leading whitespace, empty and comment lines. */
509 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
510 ;
511 if (!*cp || *cp == '\n' || *cp == '#')
512 continue;
513
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000514 if (sshkey_read(found, &cp) != 0) {
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000515 /* no key? check if there are options for this key */
516 int quoted = 0;
517 debug2("user_key_allowed: check options: '%s'", cp);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000518 key_options = cp;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000519 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
520 if (*cp == '\\' && cp[1] == '"')
521 cp++; /* Skip both */
522 else if (*cp == '"')
523 quoted = !quoted;
524 }
525 /* Skip remaining whitespace. */
526 for (; *cp == ' ' || *cp == '\t'; cp++)
527 ;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000528 if (sshkey_read(found, &cp) != 0) {
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000529 debug2("user_key_allowed: advance: '%s'", cp);
530 /* still no key? advance to next line*/
531 continue;
532 }
533 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000534 if (sshkey_is_cert(key)) {
535 if (!sshkey_equal(found, key->cert->signature_key))
Damien Miller0a80ca12010-02-27 07:55:05 +1100536 continue;
Damien Miller84399552010-05-21 14:58:12 +1000537 if (auth_parse_options(pw, key_options, file,
538 linenum) != 1)
539 continue;
540 if (!key_is_cert_authority)
541 continue;
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000542 if ((fp = sshkey_fingerprint(found,
543 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
544 continue;
Damien Millere513a912010-03-22 05:51:21 +1100545 debug("matching CA found: file %s, line %lu, %s %s",
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000546 file, linenum, sshkey_type(found), fp);
Damien Miller30da3442010-05-10 11:58:03 +1000547 /*
548 * If the user has specified a list of principals as
549 * a key option, then prefer that list to matching
550 * their username in the certificate principals list.
551 */
552 if (authorized_principals != NULL &&
553 !match_principals_option(authorized_principals,
554 key->cert)) {
555 reason = "Certificate does not contain an "
556 "authorized principal";
557 fail_reason:
Darren Tuckera627d422013-06-02 07:31:17 +1000558 free(fp);
Damien Miller0a80ca12010-02-27 07:55:05 +1100559 error("%s", reason);
560 auth_debug_add("%s", reason);
561 continue;
562 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000563 if (sshkey_cert_check_authority(key, 0, 0,
Damien Miller30da3442010-05-10 11:58:03 +1000564 authorized_principals == NULL ? pw->pw_name : NULL,
565 &reason) != 0)
566 goto fail_reason;
djm@openbsd.orgfd6dcef2016-11-30 02:57:40 +0000567 if (auth_cert_options(key, pw, &reason) != 0)
568 goto fail_reason;
djm@openbsd.org63d18812015-10-27 01:44:45 +0000569 verbose("Accepted certificate ID \"%s\" (serial %llu) "
Damien Millere513a912010-03-22 05:51:21 +1100570 "signed by %s CA %s via %s", key->cert->key_id,
djm@openbsd.org63d18812015-10-27 01:44:45 +0000571 (unsigned long long)key->cert->serial,
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000572 sshkey_type(found), fp, file);
Darren Tuckera627d422013-06-02 07:31:17 +1000573 free(fp);
Damien Miller0a80ca12010-02-27 07:55:05 +1100574 found_key = 1;
575 break;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000576 } else if (sshkey_equal(found, key)) {
Damien Miller84399552010-05-21 14:58:12 +1000577 if (auth_parse_options(pw, key_options, file,
578 linenum) != 1)
579 continue;
580 if (key_is_cert_authority)
581 continue;
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000582 if ((fp = sshkey_fingerprint(found,
583 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
584 continue;
Darren Tucker74836ae2013-06-02 07:32:00 +1000585 debug("matching key found: file %s, line %lu %s %s",
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000586 file, linenum, sshkey_type(found), fp);
Darren Tuckera627d422013-06-02 07:31:17 +1000587 free(fp);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000588 found_key = 1;
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000589 continue;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000590 }
591 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000592 done:
Darren Tucker74836ae2013-06-02 07:32:00 +1000593 if (found != NULL)
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000594 sshkey_free(found);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000595 if (!found_key)
596 debug2("key not found");
597 return found_key;
598}
599
Damien Miller1aed65e2010-03-04 21:53:35 +1100600/* Authenticate a certificate key against TrustedUserCAKeys */
601static int
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000602user_cert_trusted_ca(struct passwd *pw, struct sshkey *key)
Damien Miller1aed65e2010-03-04 21:53:35 +1100603{
Damien Miller30da3442010-05-10 11:58:03 +1000604 char *ca_fp, *principals_file = NULL;
Damien Miller1aed65e2010-03-04 21:53:35 +1100605 const char *reason;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000606 int r, ret = 0, found_principal = 0, use_authorized_principals;
Damien Miller1aed65e2010-03-04 21:53:35 +1100607
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000608 if (!sshkey_is_cert(key) || options.trusted_user_ca_keys == NULL)
Damien Miller1aed65e2010-03-04 21:53:35 +1100609 return 0;
610
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000611 if ((ca_fp = sshkey_fingerprint(key->cert->signature_key,
612 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
613 return 0;
Damien Miller1aed65e2010-03-04 21:53:35 +1100614
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000615 if ((r = sshkey_in_file(key->cert->signature_key,
616 options.trusted_user_ca_keys, 1, 0)) != 0) {
617 debug2("%s: CA %s %s is not listed in %s: %s", __func__,
618 sshkey_type(key->cert->signature_key), ca_fp,
619 options.trusted_user_ca_keys, ssh_err(r));
Damien Miller1aed65e2010-03-04 21:53:35 +1100620 goto out;
621 }
Damien Miller30da3442010-05-10 11:58:03 +1000622 /*
623 * If AuthorizedPrincipals is in use, then compare the certificate
624 * principals against the names in that file rather than matching
625 * against the username.
626 */
627 if ((principals_file = authorized_principals_file(pw)) != NULL) {
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000628 if (match_principals_file(principals_file, pw, key->cert))
629 found_principal = 1;
630 }
631 /* Try querying command if specified */
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000632 if (!found_principal && match_principals_command(pw, key))
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000633 found_principal = 1;
jsing@openbsd.org596dbca2015-06-15 18:44:22 +0000634 /* If principals file or command is specified, then require a match */
635 use_authorized_principals = principals_file != NULL ||
636 options.authorized_principals_command != NULL;
637 if (!found_principal && use_authorized_principals) {
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000638 reason = "Certificate does not contain an authorized principal";
Damien Miller30da3442010-05-10 11:58:03 +1000639 fail_reason:
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000640 error("%s", reason);
641 auth_debug_add("%s", reason);
642 goto out;
Damien Miller1aed65e2010-03-04 21:53:35 +1100643 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000644 if (sshkey_cert_check_authority(key, 0, 1,
jsing@openbsd.org596dbca2015-06-15 18:44:22 +0000645 use_authorized_principals ? NULL : pw->pw_name, &reason) != 0)
Damien Miller30da3442010-05-10 11:58:03 +1000646 goto fail_reason;
djm@openbsd.orgfd6dcef2016-11-30 02:57:40 +0000647 if (auth_cert_options(key, pw, &reason) != 0)
648 goto fail_reason;
Damien Miller1aed65e2010-03-04 21:53:35 +1100649
djm@openbsd.org63d18812015-10-27 01:44:45 +0000650 verbose("Accepted certificate ID \"%s\" (serial %llu) signed by "
651 "%s CA %s via %s", key->cert->key_id,
652 (unsigned long long)key->cert->serial,
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000653 sshkey_type(key->cert->signature_key), ca_fp,
Damien Millere513a912010-03-22 05:51:21 +1100654 options.trusted_user_ca_keys);
Damien Miller1aed65e2010-03-04 21:53:35 +1100655 ret = 1;
656
657 out:
Darren Tuckera627d422013-06-02 07:31:17 +1000658 free(principals_file);
659 free(ca_fp);
Damien Miller1aed65e2010-03-04 21:53:35 +1100660 return ret;
661}
662
Damien Miller09d3e122012-10-31 08:58:58 +1100663/*
664 * Checks whether key is allowed in file.
665 * returns 1 if the key is allowed or 0 otherwise.
666 */
667static int
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000668user_key_allowed2(struct passwd *pw, struct sshkey *key, char *file)
Damien Miller09d3e122012-10-31 08:58:58 +1100669{
670 FILE *f;
671 int found_key = 0;
672
673 /* Temporarily use the user's uid. */
674 temporarily_use_uid(pw);
675
676 debug("trying public key file %s", file);
677 if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) {
678 found_key = check_authkeys_file(f, file, key, pw);
679 fclose(f);
680 }
681
682 restore_uid();
683 return found_key;
684}
685
686/*
687 * Checks whether key is allowed in output of command.
688 * returns 1 if the key is allowed or 0 otherwise.
689 */
690static int
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000691user_key_command_allowed2(struct passwd *user_pw, struct sshkey *key)
Damien Miller09d3e122012-10-31 08:58:58 +1100692{
djm@openbsd.org24232a32015-05-21 06:38:35 +0000693 FILE *f = NULL;
694 int r, ok, found_key = 0;
Damien Miller09d3e122012-10-31 08:58:58 +1100695 struct passwd *pw;
djm@openbsd.org24232a32015-05-21 06:38:35 +0000696 int i, uid_swapped = 0, ac = 0;
Damien Miller09d3e122012-10-31 08:58:58 +1100697 pid_t pid;
djm@openbsd.org24232a32015-05-21 06:38:35 +0000698 char *username = NULL, *key_fp = NULL, *keytext = NULL;
699 char *tmp, *command = NULL, **av = NULL;
700 void (*osigchld)(int);
Damien Miller09d3e122012-10-31 08:58:58 +1100701
djm@openbsd.org24232a32015-05-21 06:38:35 +0000702 if (options.authorized_keys_command == NULL)
Damien Miller09d3e122012-10-31 08:58:58 +1100703 return 0;
Damien Millerd0d10992012-11-04 22:23:14 +1100704 if (options.authorized_keys_command_user == NULL) {
705 error("No user for AuthorizedKeysCommand specified, skipping");
706 return 0;
Damien Miller09d3e122012-10-31 08:58:58 +1100707 }
708
djm@openbsd.org24232a32015-05-21 06:38:35 +0000709 /*
710 * NB. all returns later this function should go via "out" to
711 * ensure the original SIGCHLD handler is restored properly.
712 */
713 osigchld = signal(SIGCHLD, SIG_DFL);
714
715 /* Prepare and verify the user for the command */
Damien Millerd0d10992012-11-04 22:23:14 +1100716 username = percent_expand(options.authorized_keys_command_user,
717 "u", user_pw->pw_name, (char *)NULL);
718 pw = getpwnam(username);
719 if (pw == NULL) {
Damien Miller4018dc02013-02-15 10:28:55 +1100720 error("AuthorizedKeysCommandUser \"%s\" not found: %s",
721 username, strerror(errno));
Damien Miller09d3e122012-10-31 08:58:58 +1100722 goto out;
723 }
724
djm@openbsd.org24232a32015-05-21 06:38:35 +0000725 /* Prepare AuthorizedKeysCommand */
726 if ((key_fp = sshkey_fingerprint(key, options.fingerprint_hash,
727 SSH_FP_DEFAULT)) == NULL) {
728 error("%s: sshkey_fingerprint failed", __func__);
729 goto out;
730 }
731 if ((r = sshkey_to_base64(key, &keytext)) != 0) {
732 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r));
Damien Miller09d3e122012-10-31 08:58:58 +1100733 goto out;
734 }
735
djm@openbsd.org24232a32015-05-21 06:38:35 +0000736 /* Turn the command into an argument vector */
djm@openbsd.orgde4ae072017-08-18 05:36:45 +0000737 if (argv_split(options.authorized_keys_command, &ac, &av) != 0) {
djm@openbsd.org24232a32015-05-21 06:38:35 +0000738 error("AuthorizedKeysCommand \"%s\" contains invalid quotes",
739 command);
740 goto out;
741 }
742 if (ac == 0) {
743 error("AuthorizedKeysCommand \"%s\" yielded no arguments",
744 command);
745 goto out;
746 }
747 for (i = 1; i < ac; i++) {
748 tmp = percent_expand(av[i],
749 "u", user_pw->pw_name,
750 "h", user_pw->pw_dir,
751 "t", sshkey_ssh_name(key),
752 "f", key_fp,
753 "k", keytext,
754 (char *)NULL);
755 if (tmp == NULL)
756 fatal("%s: percent_expand failed", __func__);
757 free(av[i]);
758 av[i] = tmp;
759 }
760 /* Prepare a printable command for logs, etc. */
djm@openbsd.orgde4ae072017-08-18 05:36:45 +0000761 command = argv_assemble(ac, av);
Damien Miller09d3e122012-10-31 08:58:58 +1100762
763 /*
djm@openbsd.org24232a32015-05-21 06:38:35 +0000764 * If AuthorizedKeysCommand was run without arguments
765 * then fall back to the old behaviour of passing the
766 * target username as a single argument.
Damien Miller09d3e122012-10-31 08:58:58 +1100767 */
djm@openbsd.org24232a32015-05-21 06:38:35 +0000768 if (ac == 1) {
769 av = xreallocarray(av, ac + 2, sizeof(*av));
770 av[1] = xstrdup(user_pw->pw_name);
771 av[2] = NULL;
772 /* Fix up command too, since it is used in log messages */
773 free(command);
774 xasprintf(&command, "%s %s", av[0], av[1]);
Damien Miller09d3e122012-10-31 08:58:58 +1100775 }
776
djm@openbsd.org24232a32015-05-21 06:38:35 +0000777 if ((pid = subprocess("AuthorizedKeysCommand", pw, command,
djm@openbsd.orgde4ae072017-08-18 05:36:45 +0000778 ac, av, &f,
779 SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD)) == 0)
djm@openbsd.org24232a32015-05-21 06:38:35 +0000780 goto out;
781
782 uid_swapped = 1;
Damien Miller09d3e122012-10-31 08:58:58 +1100783 temporarily_use_uid(pw);
784
Damien Miller09d3e122012-10-31 08:58:58 +1100785 ok = check_authkeys_file(f, options.authorized_keys_command, key, pw);
Damien Miller09d3e122012-10-31 08:58:58 +1100786
djm@openbsd.orgddd3d342016-12-30 22:08:02 +0000787 fclose(f);
788 f = NULL;
789
djm@openbsd.orgb074c3c2017-08-18 05:48:04 +0000790 if (exited_cleanly(pid, "AuthorizedKeysCommand", command, 0) != 0)
Damien Miller09d3e122012-10-31 08:58:58 +1100791 goto out;
djm@openbsd.org24232a32015-05-21 06:38:35 +0000792
793 /* Read completed successfully */
Damien Miller09d3e122012-10-31 08:58:58 +1100794 found_key = ok;
795 out:
djm@openbsd.org24232a32015-05-21 06:38:35 +0000796 if (f != NULL)
797 fclose(f);
798 signal(SIGCHLD, osigchld);
799 for (i = 0; i < ac; i++)
800 free(av[i]);
801 free(av);
802 if (uid_swapped)
803 restore_uid();
804 free(command);
805 free(username);
806 free(key_fp);
807 free(keytext);
Damien Miller09d3e122012-10-31 08:58:58 +1100808 return found_key;
809}
810
811/*
812 * Check whether key authenticates and authorises the user.
813 */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000814int
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000815user_key_allowed(struct passwd *pw, struct sshkey *key, int auth_attempt)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000816{
Damien Millerd8478b62011-05-29 21:39:36 +1000817 u_int success, i;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000818 char *file;
819
Damien Miller1aed65e2010-03-04 21:53:35 +1100820 if (auth_key_is_revoked(key))
821 return 0;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000822 if (sshkey_is_cert(key) &&
823 auth_key_is_revoked(key->cert->signature_key))
Damien Miller1aed65e2010-03-04 21:53:35 +1100824 return 0;
825
826 success = user_cert_trusted_ca(pw, key);
827 if (success)
828 return success;
829
Damien Miller09d3e122012-10-31 08:58:58 +1100830 success = user_key_command_allowed2(pw, key);
831 if (success > 0)
832 return success;
833
Damien Millerd8478b62011-05-29 21:39:36 +1000834 for (i = 0; !success && i < options.num_authkeys_files; i++) {
Damien Miller09d3e122012-10-31 08:58:58 +1100835
836 if (strcasecmp(options.authorized_keys_files[i], "none") == 0)
837 continue;
Damien Millerd8478b62011-05-29 21:39:36 +1000838 file = expand_authorized_keys(
839 options.authorized_keys_files[i], pw);
Damien Miller09d3e122012-10-31 08:58:58 +1100840
Damien Millerd8478b62011-05-29 21:39:36 +1000841 success = user_key_allowed2(pw, key, file);
Darren Tuckera627d422013-06-02 07:31:17 +1000842 free(file);
Damien Millerd8478b62011-05-29 21:39:36 +1000843 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000844
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000845 return success;
846}
847
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000848Authmethod method_pubkey = {
849 "publickey",
850 userauth_pubkey,
851 &options.pubkey_authentication
852};