blob: d567f527c9619804590121071dcd975c5a561aad [file] [log] [blame]
djm@openbsd.orgdd8002f2019-09-03 08:30:47 +00001/* $OpenBSD: auth2-pubkey.c,v 1.93 2019/09/03 08:30:47 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"
markus@openbsd.orgc7d39ac2018-07-09 21:35:50 +000049#include "sshbuf.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000050#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;
djm@openbsd.org7c856852018-03-03 03:15:51 +000091 struct passwd *pw = authctxt->pw;
djm@openbsd.org74287f52018-07-31 03:10:27 +000092 struct sshbuf *b = NULL;
markus@openbsd.org54d90ac2017-05-30 08:52:19 +000093 struct sshkey *key = NULL;
djm@openbsd.org74287f52018-07-31 03:10:27 +000094 char *pkalg = NULL, *userstyle = NULL, *key_s = NULL, *ca_s = NULL;
95 u_char *pkblob = NULL, *sig = NULL, have_sig;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +000096 size_t blen, slen;
97 int r, pktype;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000098 int authenticated = 0;
djm@openbsd.org7c856852018-03-03 03:15:51 +000099 struct sshauthopt *authopts = NULL;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000100
djm@openbsd.org14b5c632018-01-23 05:27:21 +0000101 if ((r = sshpkt_get_u8(ssh, &have_sig)) != 0 ||
102 (r = sshpkt_get_cstring(ssh, &pkalg, NULL)) != 0 ||
103 (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0)
104 fatal("%s: parse request failed: %s", __func__, ssh_err(r));
djm@openbsd.orgff5d2cf2019-01-22 11:26:16 +0000105
106 if (log_level_get() >= SYSLOG_LEVEL_DEBUG2) {
107 char *keystring;
108 struct sshbuf *pkbuf;
109
110 if ((pkbuf = sshbuf_from(pkblob, blen)) == NULL)
111 fatal("%s: sshbuf_from failed", __func__);
djm@openbsd.org16dd8b22019-07-16 13:18:39 +0000112 if ((keystring = sshbuf_dtob64_string(pkbuf, 0)) == NULL)
djm@openbsd.orgff5d2cf2019-01-22 11:26:16 +0000113 fatal("%s: sshbuf_dtob64 failed", __func__);
114 debug2("%s: %s user %s %s public key %s %s", __func__,
115 authctxt->valid ? "valid" : "invalid", authctxt->user,
116 have_sig ? "attempting" : "querying", pkalg, keystring);
117 sshbuf_free(pkbuf);
118 free(keystring);
119 }
120
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000121 pktype = sshkey_type_from_name(pkalg);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000122 if (pktype == KEY_UNSPEC) {
123 /* this is perfectly legal */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000124 verbose("%s: unsupported public key algorithm: %s",
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000125 __func__, pkalg);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000126 goto done;
127 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000128 if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
129 error("%s: could not parse key: %s", __func__, ssh_err(r));
130 goto done;
131 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000132 if (key == NULL) {
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000133 error("%s: cannot decode key: %s", __func__, pkalg);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000134 goto done;
135 }
136 if (key->type != pktype) {
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000137 error("%s: type mismatch for decoded key "
138 "(received %d, expected %d)", __func__, key->type, pktype);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000139 goto done;
140 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000141 if (sshkey_type_plain(key->type) == KEY_RSA &&
142 (ssh->compat & SSH_BUG_RSASIGMD5) != 0) {
Damien Miller324541e2013-12-31 12:25:40 +1100143 logit("Refusing RSA key because client uses unsafe "
144 "signature scheme");
145 goto done;
146 }
djm@openbsd.org8f574952017-06-24 06:34:38 +0000147 if (auth2_key_already_used(authctxt, key)) {
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000148 logit("refusing previously-used %s key", sshkey_type(key));
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +0000149 goto done;
150 }
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000151 if (match_pattern_list(pkalg, options.pubkey_key_types, 0) != 1) {
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000152 logit("%s: key type %s not in PubkeyAcceptedKeyTypes",
153 __func__, sshkey_ssh_name(key));
154 goto done;
155 }
djm@openbsd.org86e57372018-09-20 03:28:06 +0000156 if ((r = sshkey_check_cert_sigtype(key,
157 options.ca_sign_algorithms)) != 0) {
158 logit("%s: certificate signature algorithm %s: %s", __func__,
159 (key->cert == NULL || key->cert->signature_type == NULL) ?
160 "(null)" : key->cert->signature_type, ssh_err(r));
161 goto done;
162 }
djm@openbsd.org27885632017-12-19 00:24:34 +0000163 key_s = format_key(key);
164 if (sshkey_is_cert(key))
165 ca_s = format_key(key->cert->signature_key);
166
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000167 if (have_sig) {
djm@openbsd.org27885632017-12-19 00:24:34 +0000168 debug3("%s: have %s signature for %s%s%s",
169 __func__, pkalg, key_s,
170 ca_s == NULL ? "" : " CA ",
171 ca_s == NULL ? "" : ca_s);
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000172 if ((r = sshpkt_get_string(ssh, &sig, &slen)) != 0 ||
173 (r = sshpkt_get_end(ssh)) != 0)
174 fatal("%s: %s", __func__, ssh_err(r));
175 if ((b = sshbuf_new()) == NULL)
176 fatal("%s: sshbuf_new failed", __func__);
177 if (ssh->compat & SSH_OLD_SESSIONID) {
178 if ((r = sshbuf_put(b, session_id2,
179 session_id2_len)) != 0)
180 fatal("%s: sshbuf_put session id: %s",
181 __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000182 } else {
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000183 if ((r = sshbuf_put_string(b, session_id2,
184 session_id2_len)) != 0)
185 fatal("%s: sshbuf_put_string session id: %s",
186 __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000187 }
djm@openbsd.org74287f52018-07-31 03:10:27 +0000188 if (!authctxt->valid || authctxt->user == NULL) {
189 debug2("%s: disabled because of invalid user",
190 __func__);
191 goto done;
192 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000193 /* reconstruct packet */
Damien Miller4ce189d2013-04-23 15:17:52 +1000194 xasprintf(&userstyle, "%s%s%s", authctxt->user,
195 authctxt->style ? ":" : "",
196 authctxt->style ? authctxt->style : "");
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000197 if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
198 (r = sshbuf_put_cstring(b, userstyle)) != 0 ||
djm@openbsd.org14b5c632018-01-23 05:27:21 +0000199 (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
200 (r = sshbuf_put_cstring(b, "publickey")) != 0 ||
201 (r = sshbuf_put_u8(b, have_sig)) != 0 ||
mestre@openbsd.orgdb8bb802018-08-28 12:25:53 +0000202 (r = sshbuf_put_cstring(b, pkalg)) != 0 ||
djm@openbsd.org14b5c632018-01-23 05:27:21 +0000203 (r = sshbuf_put_string(b, pkblob, blen)) != 0)
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000204 fatal("%s: build packet failed: %s",
205 __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000206#ifdef DEBUG_PK
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000207 sshbuf_dump(b, stderr);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000208#endif
209 /* test for correct signature */
210 authenticated = 0;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000211 if (PRIVSEP(user_key_allowed(ssh, pw, key, 1, &authopts)) &&
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000212 PRIVSEP(sshkey_verify(key, sig, slen,
213 sshbuf_ptr(b), sshbuf_len(b),
214 (ssh->compat & SSH_BUG_SIGTYPE) == 0 ? pkalg : NULL,
215 ssh->compat)) == 0) {
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000216 authenticated = 1;
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +0000217 }
djm@openbsd.org8f574952017-06-24 06:34:38 +0000218 auth2_record_key(authctxt, authenticated, key);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000219 } else {
djm@openbsd.org27885632017-12-19 00:24:34 +0000220 debug("%s: test pkalg %s pkblob %s%s%s",
221 __func__, pkalg, key_s,
222 ca_s == NULL ? "" : " CA ",
223 ca_s == NULL ? "" : ca_s);
224
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000225 if ((r = sshpkt_get_end(ssh)) != 0)
226 fatal("%s: %s", __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000227
djm@openbsd.org74287f52018-07-31 03:10:27 +0000228 if (!authctxt->valid || authctxt->user == NULL) {
229 debug2("%s: disabled because of invalid user",
230 __func__);
231 goto done;
232 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000233 /* XXX fake reply and always send PK_OK ? */
234 /*
235 * XXX this allows testing whether a user is allowed
236 * to login: if you happen to have a valid pubkey this
237 * message is sent. the message is NEVER sent at all
238 * if a user is not allowed to login. is this an
239 * issue? -markus
240 */
djm@openbsd.org7c856852018-03-03 03:15:51 +0000241 if (PRIVSEP(user_key_allowed(ssh, pw, key, 0, NULL))) {
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000242 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_PK_OK))
243 != 0 ||
244 (r = sshpkt_put_cstring(ssh, pkalg)) != 0 ||
245 (r = sshpkt_put_string(ssh, pkblob, blen)) != 0 ||
markus@openbsd.org394a8422018-07-11 18:55:11 +0000246 (r = sshpkt_send(ssh)) != 0 ||
247 (r = ssh_packet_write_wait(ssh)) != 0)
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000248 fatal("%s: %s", __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000249 authctxt->postponed = 1;
250 }
251 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000252done:
djm@openbsd.org7c856852018-03-03 03:15:51 +0000253 if (authenticated == 1 && auth_activate_options(ssh, authopts) != 0) {
254 debug("%s: key options inconsistent with existing", __func__);
255 authenticated = 0;
256 }
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000257 debug2("%s: authenticated %d pkalg %s", __func__, authenticated, pkalg);
djm@openbsd.org7c856852018-03-03 03:15:51 +0000258
djm@openbsd.org7fef1732018-08-23 03:01:08 +0000259 sshbuf_free(b);
djm@openbsd.org7c856852018-03-03 03:15:51 +0000260 sshauthopt_free(authopts);
djm@openbsd.org8f574952017-06-24 06:34:38 +0000261 sshkey_free(key);
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000262 free(userstyle);
Darren Tuckera627d422013-06-02 07:31:17 +1000263 free(pkalg);
264 free(pkblob);
djm@openbsd.org27885632017-12-19 00:24:34 +0000265 free(key_s);
266 free(ca_s);
djm@openbsd.org74287f52018-07-31 03:10:27 +0000267 free(sig);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000268 return authenticated;
269}
270
Damien Miller30da3442010-05-10 11:58:03 +1000271static int
Damien Miller86687062014-07-02 15:28:02 +1000272match_principals_option(const char *principal_list, struct sshkey_cert *cert)
Damien Miller30da3442010-05-10 11:58:03 +1000273{
274 char *result;
275 u_int i;
276
277 /* XXX percent_expand() sequences for authorized_principals? */
278
279 for (i = 0; i < cert->nprincipals; i++) {
280 if ((result = match_list(cert->principals[i],
281 principal_list, NULL)) != NULL) {
282 debug3("matched principal from key options \"%.100s\"",
283 result);
Darren Tuckera627d422013-06-02 07:31:17 +1000284 free(result);
Damien Miller30da3442010-05-10 11:58:03 +1000285 return 1;
286 }
287 }
288 return 0;
289}
290
djm@openbsd.org7c856852018-03-03 03:15:51 +0000291/*
292 * Process a single authorized_principals format line. Returns 0 and sets
293 * authoptsp is principal is authorised, -1 otherwise. "loc" is used as a
294 * log preamble for file/line information.
295 */
Damien Miller30da3442010-05-10 11:58:03 +1000296static int
djm@openbsd.org7c856852018-03-03 03:15:51 +0000297check_principals_line(struct ssh *ssh, char *cp, const struct sshkey_cert *cert,
298 const char *loc, struct sshauthopt **authoptsp)
Damien Miller30da3442010-05-10 11:58:03 +1000299{
djm@openbsd.org7c856852018-03-03 03:15:51 +0000300 u_int i, found = 0;
301 char *ep, *line_opts;
302 const char *reason = NULL;
303 struct sshauthopt *opts = NULL;
304
305 if (authoptsp != NULL)
306 *authoptsp = NULL;
307
308 /* Trim trailing whitespace. */
309 ep = cp + strlen(cp) - 1;
310 while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t'))
311 *ep-- = '\0';
312
313 /*
314 * If the line has internal whitespace then assume it has
315 * key options.
316 */
317 line_opts = NULL;
318 if ((ep = strrchr(cp, ' ')) != NULL ||
319 (ep = strrchr(cp, '\t')) != NULL) {
320 for (; *ep == ' ' || *ep == '\t'; ep++)
321 ;
322 line_opts = cp;
323 cp = ep;
324 }
325 if ((opts = sshauthopt_parse(line_opts, &reason)) == NULL) {
326 debug("%s: bad principals options: %s", loc, reason);
327 auth_debug_add("%s: bad principals options: %s", loc, reason);
328 return -1;
329 }
330 /* Check principals in cert against those on line */
331 for (i = 0; i < cert->nprincipals; i++) {
332 if (strcmp(cp, cert->principals[i]) != 0)
333 continue;
334 debug3("%s: matched principal \"%.100s\"",
335 loc, cert->principals[i]);
336 found = 1;
337 }
338 if (found && authoptsp != NULL) {
339 *authoptsp = opts;
340 opts = NULL;
341 }
342 sshauthopt_free(opts);
343 return found ? 0 : -1;
344}
345
346static int
347process_principals(struct ssh *ssh, FILE *f, const char *file,
348 const struct sshkey_cert *cert, struct sshauthopt **authoptsp)
349{
markus@openbsd.org7f906352018-06-06 18:29:18 +0000350 char loc[256], *line = NULL, *cp, *ep;
351 size_t linesize = 0;
Damien Miller30da3442010-05-10 11:58:03 +1000352 u_long linenum = 0;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000353 u_int found_principal = 0;
354
355 if (authoptsp != NULL)
356 *authoptsp = NULL;
Damien Miller30da3442010-05-10 11:58:03 +1000357
markus@openbsd.org7f906352018-06-06 18:29:18 +0000358 while (getline(&line, &linesize, f) != -1) {
359 linenum++;
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000360 /* Always consume entire input */
361 if (found_principal)
362 continue;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000363
Damien Miller6018a362010-07-02 13:35:19 +1000364 /* Skip leading whitespace. */
Damien Miller30da3442010-05-10 11:58:03 +1000365 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
366 ;
Damien Miller6018a362010-07-02 13:35:19 +1000367 /* Skip blank and comment lines. */
368 if ((ep = strchr(cp, '#')) != NULL)
369 *ep = '\0';
370 if (!*cp || *cp == '\n')
Damien Miller30da3442010-05-10 11:58:03 +1000371 continue;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000372
373 snprintf(loc, sizeof(loc), "%.200s:%lu", file, linenum);
374 if (check_principals_line(ssh, cp, cert, loc, authoptsp) == 0)
375 found_principal = 1;
Damien Miller30da3442010-05-10 11:58:03 +1000376 }
markus@openbsd.org7f906352018-06-06 18:29:18 +0000377 free(line);
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000378 return found_principal;
Damien Miller09d3e122012-10-31 08:58:58 +1100379}
Damien Miller30da3442010-05-10 11:58:03 +1000380
djm@openbsd.org7c856852018-03-03 03:15:51 +0000381/* XXX remove pw args here and elsewhere once ssh->authctxt is guaranteed */
382
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000383static int
djm@openbsd.org7c856852018-03-03 03:15:51 +0000384match_principals_file(struct ssh *ssh, struct passwd *pw, char *file,
385 struct sshkey_cert *cert, struct sshauthopt **authoptsp)
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000386{
387 FILE *f;
388 int success;
389
djm@openbsd.org7c856852018-03-03 03:15:51 +0000390 if (authoptsp != NULL)
391 *authoptsp = NULL;
392
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000393 temporarily_use_uid(pw);
394 debug("trying authorized principals file %s", file);
395 if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) {
396 restore_uid();
397 return 0;
398 }
djm@openbsd.org7c856852018-03-03 03:15:51 +0000399 success = process_principals(ssh, f, file, cert, authoptsp);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000400 fclose(f);
401 restore_uid();
402 return success;
403}
404
405/*
406 * Checks whether principal is allowed in output of command.
407 * returns 1 if the principal is allowed or 0 otherwise.
408 */
409static int
djm@openbsd.org7c856852018-03-03 03:15:51 +0000410match_principals_command(struct ssh *ssh, struct passwd *user_pw,
411 const struct sshkey *key, struct sshauthopt **authoptsp)
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000412{
djm@openbsd.org7c856852018-03-03 03:15:51 +0000413 struct passwd *runas_pw = NULL;
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000414 const struct sshkey_cert *cert = key->cert;
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000415 FILE *f = NULL;
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000416 int r, ok, found_principal = 0;
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000417 int i, ac = 0, uid_swapped = 0;
418 pid_t pid;
419 char *tmp, *username = NULL, *command = NULL, **av = NULL;
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000420 char *ca_fp = NULL, *key_fp = NULL, *catext = NULL, *keytext = NULL;
djm@openbsd.org30615292019-05-20 00:25:55 +0000421 char serial_s[32], uidstr[32];
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000422 void (*osigchld)(int);
423
djm@openbsd.org7c856852018-03-03 03:15:51 +0000424 if (authoptsp != NULL)
425 *authoptsp = NULL;
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000426 if (options.authorized_principals_command == NULL)
427 return 0;
428 if (options.authorized_principals_command_user == NULL) {
429 error("No user for AuthorizedPrincipalsCommand specified, "
430 "skipping");
431 return 0;
432 }
433
434 /*
435 * NB. all returns later this function should go via "out" to
436 * ensure the original SIGCHLD handler is restored properly.
437 */
438 osigchld = signal(SIGCHLD, SIG_DFL);
439
440 /* Prepare and verify the user for the command */
441 username = percent_expand(options.authorized_principals_command_user,
442 "u", user_pw->pw_name, (char *)NULL);
djm@openbsd.org7c856852018-03-03 03:15:51 +0000443 runas_pw = getpwnam(username);
444 if (runas_pw == NULL) {
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000445 error("AuthorizedPrincipalsCommandUser \"%s\" not found: %s",
446 username, strerror(errno));
447 goto out;
448 }
449
450 /* Turn the command into an argument vector */
djm@openbsd.orgde4ae072017-08-18 05:36:45 +0000451 if (argv_split(options.authorized_principals_command, &ac, &av) != 0) {
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000452 error("AuthorizedPrincipalsCommand \"%s\" contains "
djm@openbsd.org4cd6b122019-06-21 03:19:59 +0000453 "invalid quotes", options.authorized_principals_command);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000454 goto out;
455 }
456 if (ac == 0) {
457 error("AuthorizedPrincipalsCommand \"%s\" yielded no arguments",
djm@openbsd.org4cd6b122019-06-21 03:19:59 +0000458 options.authorized_principals_command);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000459 goto out;
460 }
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000461 if ((ca_fp = sshkey_fingerprint(cert->signature_key,
462 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
463 error("%s: sshkey_fingerprint failed", __func__);
464 goto out;
465 }
djm@openbsd.org00df97f2016-09-14 20:11:26 +0000466 if ((key_fp = sshkey_fingerprint(key,
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000467 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
468 error("%s: sshkey_fingerprint failed", __func__);
469 goto out;
470 }
471 if ((r = sshkey_to_base64(cert->signature_key, &catext)) != 0) {
472 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r));
473 goto out;
474 }
475 if ((r = sshkey_to_base64(key, &keytext)) != 0) {
476 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r));
477 goto out;
478 }
djm@openbsd.orgf83a0cf2016-09-21 17:44:20 +0000479 snprintf(serial_s, sizeof(serial_s), "%llu",
480 (unsigned long long)cert->serial);
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000481 snprintf(uidstr, sizeof(uidstr), "%llu",
482 (unsigned long long)user_pw->pw_uid);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000483 for (i = 1; i < ac; i++) {
484 tmp = percent_expand(av[i],
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000485 "U", uidstr,
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000486 "u", user_pw->pw_name,
487 "h", user_pw->pw_dir,
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000488 "t", sshkey_ssh_name(key),
489 "T", sshkey_ssh_name(cert->signature_key),
490 "f", key_fp,
491 "F", ca_fp,
492 "k", keytext,
493 "K", catext,
djm@openbsd.orgbfa9d962016-09-21 01:34:45 +0000494 "i", cert->key_id,
495 "s", serial_s,
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000496 (char *)NULL);
497 if (tmp == NULL)
498 fatal("%s: percent_expand failed", __func__);
499 free(av[i]);
500 av[i] = tmp;
501 }
502 /* Prepare a printable command for logs, etc. */
djm@openbsd.orgde4ae072017-08-18 05:36:45 +0000503 command = argv_assemble(ac, av);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000504
djm@openbsd.org7c856852018-03-03 03:15:51 +0000505 if ((pid = subprocess("AuthorizedPrincipalsCommand", runas_pw, command,
djm@openbsd.orgde4ae072017-08-18 05:36:45 +0000506 ac, av, &f,
507 SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD)) == 0)
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000508 goto out;
509
510 uid_swapped = 1;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000511 temporarily_use_uid(runas_pw);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000512
djm@openbsd.org7c856852018-03-03 03:15:51 +0000513 ok = process_principals(ssh, f, "(command)", cert, authoptsp);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000514
djm@openbsd.orgddd3d342016-12-30 22:08:02 +0000515 fclose(f);
516 f = NULL;
517
djm@openbsd.orgb074c3c2017-08-18 05:48:04 +0000518 if (exited_cleanly(pid, "AuthorizedPrincipalsCommand", command, 0) != 0)
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000519 goto out;
520
521 /* Read completed successfully */
522 found_principal = ok;
523 out:
524 if (f != NULL)
525 fclose(f);
526 signal(SIGCHLD, osigchld);
527 for (i = 0; i < ac; i++)
528 free(av[i]);
529 free(av);
530 if (uid_swapped)
531 restore_uid();
532 free(command);
533 free(username);
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000534 free(ca_fp);
535 free(key_fp);
536 free(catext);
537 free(keytext);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000538 return found_principal;
539}
djm@openbsd.org7c856852018-03-03 03:15:51 +0000540
djm@openbsd.org7c856852018-03-03 03:15:51 +0000541/*
djm@openbsd.org7c856852018-03-03 03:15:51 +0000542 * Check a single line of an authorized_keys-format file. Returns 0 if key
543 * matches, -1 otherwise. Will return key/cert options via *authoptsp
544 * on success. "loc" is used as file/line location in log messages.
545 */
546static int
547check_authkey_line(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
548 char *cp, const char *loc, struct sshauthopt **authoptsp)
549{
550 int want_keytype = sshkey_is_cert(key) ? KEY_UNSPEC : key->type;
551 struct sshkey *found = NULL;
552 struct sshauthopt *keyopts = NULL, *certopts = NULL, *finalopts = NULL;
553 char *key_options = NULL, *fp = NULL;
554 const char *reason = NULL;
555 int ret = -1;
556
557 if (authoptsp != NULL)
558 *authoptsp = NULL;
559
560 if ((found = sshkey_new(want_keytype)) == NULL) {
561 debug3("%s: keytype %d failed", __func__, want_keytype);
562 goto out;
563 }
564
565 /* XXX djm: peek at key type in line and skip if unwanted */
566
567 if (sshkey_read(found, &cp) != 0) {
568 /* no key? check for options */
569 debug2("%s: check options: '%s'", loc, cp);
570 key_options = cp;
djm@openbsd.orgdd8002f2019-09-03 08:30:47 +0000571 if (sshkey_advance_past_options(&cp) != 0) {
djm@openbsd.org7c856852018-03-03 03:15:51 +0000572 reason = "invalid key option string";
573 goto fail_reason;
574 }
575 skip_space(&cp);
576 if (sshkey_read(found, &cp) != 0) {
577 /* still no key? advance to next line*/
578 debug2("%s: advance: '%s'", loc, cp);
579 goto out;
580 }
581 }
582 /* Parse key options now; we need to know if this is a CA key */
583 if ((keyopts = sshauthopt_parse(key_options, &reason)) == NULL) {
584 debug("%s: bad key options: %s", loc, reason);
585 auth_debug_add("%s: bad key options: %s", loc, reason);
586 goto out;
587 }
588 /* Ignore keys that don't match or incorrectly marked as CAs */
589 if (sshkey_is_cert(key)) {
590 /* Certificate; check signature key against CA */
591 if (!sshkey_equal(found, key->cert->signature_key) ||
592 !keyopts->cert_authority)
593 goto out;
594 } else {
595 /* Plain key: check it against key found in file */
596 if (!sshkey_equal(found, key) || keyopts->cert_authority)
597 goto out;
598 }
599
600 /* We have a candidate key, perform authorisation checks */
601 if ((fp = sshkey_fingerprint(found,
602 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
603 fatal("%s: fingerprint failed", __func__);
604
605 debug("%s: matching %s found: %s %s", loc,
606 sshkey_is_cert(key) ? "CA" : "key", sshkey_type(found), fp);
607
608 if (auth_authorise_keyopts(ssh, pw, keyopts,
609 sshkey_is_cert(key), loc) != 0) {
610 reason = "Refused by key options";
611 goto fail_reason;
612 }
613 /* That's all we need for plain keys. */
614 if (!sshkey_is_cert(key)) {
615 verbose("Accepted key %s %s found at %s",
616 sshkey_type(found), fp, loc);
617 finalopts = keyopts;
618 keyopts = NULL;
619 goto success;
620 }
621
622 /*
623 * Additional authorisation for certificates.
624 */
625
626 /* Parse and check options present in certificate */
627 if ((certopts = sshauthopt_from_cert(key)) == NULL) {
628 reason = "Invalid certificate options";
629 goto fail_reason;
630 }
631 if (auth_authorise_keyopts(ssh, pw, certopts, 0, loc) != 0) {
632 reason = "Refused by certificate options";
633 goto fail_reason;
634 }
635 if ((finalopts = sshauthopt_merge(keyopts, certopts, &reason)) == NULL)
636 goto fail_reason;
637
638 /*
639 * If the user has specified a list of principals as
640 * a key option, then prefer that list to matching
641 * their username in the certificate principals list.
642 */
643 if (keyopts->cert_principals != NULL &&
644 !match_principals_option(keyopts->cert_principals, key->cert)) {
645 reason = "Certificate does not contain an authorized principal";
646 goto fail_reason;
647 }
648 if (sshkey_cert_check_authority(key, 0, 0,
649 keyopts->cert_principals == NULL ? pw->pw_name : NULL, &reason) != 0)
650 goto fail_reason;
651
652 verbose("Accepted certificate ID \"%s\" (serial %llu) "
653 "signed by CA %s %s found at %s",
654 key->cert->key_id,
655 (unsigned long long)key->cert->serial,
656 sshkey_type(found), fp, loc);
657
658 success:
659 if (finalopts == NULL)
660 fatal("%s: internal error: missing options", __func__);
661 if (authoptsp != NULL) {
662 *authoptsp = finalopts;
663 finalopts = NULL;
664 }
665 /* success */
666 ret = 0;
667 goto out;
668
669 fail_reason:
670 error("%s", reason);
671 auth_debug_add("%s", reason);
672 out:
673 free(fp);
674 sshauthopt_free(keyopts);
675 sshauthopt_free(certopts);
676 sshauthopt_free(finalopts);
677 sshkey_free(found);
678 return ret;
679}
680
Damien Miller09d3e122012-10-31 08:58:58 +1100681/*
682 * Checks whether key is allowed in authorized_keys-format file,
683 * returns 1 if the key is allowed or 0 otherwise.
684 */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000685static int
djm@openbsd.org7c856852018-03-03 03:15:51 +0000686check_authkeys_file(struct ssh *ssh, struct passwd *pw, FILE *f,
687 char *file, struct sshkey *key, struct sshauthopt **authoptsp)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000688{
markus@openbsd.org7f906352018-06-06 18:29:18 +0000689 char *cp, *line = NULL, loc[256];
690 size_t linesize = 0;
Darren Tucker33c787f2008-07-02 22:37:30 +1000691 int found_key = 0;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000692 u_long linenum = 0;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000693
694 if (authoptsp != NULL)
695 *authoptsp = NULL;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000696
markus@openbsd.org7f906352018-06-06 18:29:18 +0000697 while (getline(&line, &linesize, f) != -1) {
698 linenum++;
djm@openbsd.orgabd59662017-09-07 23:48:09 +0000699 /* Always consume entire file */
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000700 if (found_key)
701 continue;
Damien Miller0a80ca12010-02-27 07:55:05 +1100702
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000703 /* Skip leading whitespace, empty and comment lines. */
djm@openbsd.org7c856852018-03-03 03:15:51 +0000704 cp = line;
705 skip_space(&cp);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000706 if (!*cp || *cp == '\n' || *cp == '#')
707 continue;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000708 snprintf(loc, sizeof(loc), "%.200s:%lu", file, linenum);
709 if (check_authkey_line(ssh, pw, key, cp, loc, authoptsp) == 0)
Damien Miller0a80ca12010-02-27 07:55:05 +1100710 found_key = 1;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000711 }
markus@openbsd.org7f906352018-06-06 18:29:18 +0000712 free(line);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000713 return found_key;
714}
715
Damien Miller1aed65e2010-03-04 21:53:35 +1100716/* Authenticate a certificate key against TrustedUserCAKeys */
717static int
djm@openbsd.org7c856852018-03-03 03:15:51 +0000718user_cert_trusted_ca(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
719 struct sshauthopt **authoptsp)
Damien Miller1aed65e2010-03-04 21:53:35 +1100720{
Damien Miller30da3442010-05-10 11:58:03 +1000721 char *ca_fp, *principals_file = NULL;
Damien Miller1aed65e2010-03-04 21:53:35 +1100722 const char *reason;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000723 struct sshauthopt *principals_opts = NULL, *cert_opts = NULL;
724 struct sshauthopt *final_opts = NULL;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000725 int r, ret = 0, found_principal = 0, use_authorized_principals;
Damien Miller1aed65e2010-03-04 21:53:35 +1100726
djm@openbsd.org7c856852018-03-03 03:15:51 +0000727 if (authoptsp != NULL)
728 *authoptsp = NULL;
729
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000730 if (!sshkey_is_cert(key) || options.trusted_user_ca_keys == NULL)
Damien Miller1aed65e2010-03-04 21:53:35 +1100731 return 0;
732
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000733 if ((ca_fp = sshkey_fingerprint(key->cert->signature_key,
734 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
735 return 0;
Damien Miller1aed65e2010-03-04 21:53:35 +1100736
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000737 if ((r = sshkey_in_file(key->cert->signature_key,
738 options.trusted_user_ca_keys, 1, 0)) != 0) {
739 debug2("%s: CA %s %s is not listed in %s: %s", __func__,
740 sshkey_type(key->cert->signature_key), ca_fp,
741 options.trusted_user_ca_keys, ssh_err(r));
Damien Miller1aed65e2010-03-04 21:53:35 +1100742 goto out;
743 }
Damien Miller30da3442010-05-10 11:58:03 +1000744 /*
745 * If AuthorizedPrincipals is in use, then compare the certificate
746 * principals against the names in that file rather than matching
747 * against the username.
748 */
749 if ((principals_file = authorized_principals_file(pw)) != NULL) {
djm@openbsd.org7c856852018-03-03 03:15:51 +0000750 if (match_principals_file(ssh, pw, principals_file,
751 key->cert, &principals_opts))
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000752 found_principal = 1;
753 }
754 /* Try querying command if specified */
djm@openbsd.org7c856852018-03-03 03:15:51 +0000755 if (!found_principal && match_principals_command(ssh, pw, key,
756 &principals_opts))
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000757 found_principal = 1;
jsing@openbsd.org596dbca2015-06-15 18:44:22 +0000758 /* If principals file or command is specified, then require a match */
759 use_authorized_principals = principals_file != NULL ||
760 options.authorized_principals_command != NULL;
761 if (!found_principal && use_authorized_principals) {
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000762 reason = "Certificate does not contain an authorized principal";
djm@openbsd.org7c856852018-03-03 03:15:51 +0000763 goto fail_reason;
Damien Miller1aed65e2010-03-04 21:53:35 +1100764 }
djm@openbsd.org7c856852018-03-03 03:15:51 +0000765 if (use_authorized_principals && principals_opts == NULL)
766 fatal("%s: internal error: missing principals_opts", __func__);
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000767 if (sshkey_cert_check_authority(key, 0, 1,
jsing@openbsd.org596dbca2015-06-15 18:44:22 +0000768 use_authorized_principals ? NULL : pw->pw_name, &reason) != 0)
Damien Miller30da3442010-05-10 11:58:03 +1000769 goto fail_reason;
Damien Miller1aed65e2010-03-04 21:53:35 +1100770
djm@openbsd.org7c856852018-03-03 03:15:51 +0000771 /* Check authority from options in key and from principals file/cmd */
772 if ((cert_opts = sshauthopt_from_cert(key)) == NULL) {
773 reason = "Invalid certificate options";
774 goto fail_reason;
775 }
776 if (auth_authorise_keyopts(ssh, pw, cert_opts, 0, "cert") != 0) {
777 reason = "Refused by certificate options";
778 goto fail_reason;
779 }
780 if (principals_opts == NULL) {
781 final_opts = cert_opts;
782 cert_opts = NULL;
783 } else {
784 if (auth_authorise_keyopts(ssh, pw, principals_opts, 0,
785 "principals") != 0) {
786 reason = "Refused by certificate principals options";
787 goto fail_reason;
788 }
789 if ((final_opts = sshauthopt_merge(principals_opts,
790 cert_opts, &reason)) == NULL) {
791 fail_reason:
792 error("%s", reason);
793 auth_debug_add("%s", reason);
794 goto out;
795 }
796 }
797
798 /* Success */
djm@openbsd.org63d18812015-10-27 01:44:45 +0000799 verbose("Accepted certificate ID \"%s\" (serial %llu) signed by "
800 "%s CA %s via %s", key->cert->key_id,
801 (unsigned long long)key->cert->serial,
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000802 sshkey_type(key->cert->signature_key), ca_fp,
Damien Millere513a912010-03-22 05:51:21 +1100803 options.trusted_user_ca_keys);
djm@openbsd.org7c856852018-03-03 03:15:51 +0000804 if (authoptsp != NULL) {
805 *authoptsp = final_opts;
806 final_opts = NULL;
807 }
Damien Miller1aed65e2010-03-04 21:53:35 +1100808 ret = 1;
Damien Miller1aed65e2010-03-04 21:53:35 +1100809 out:
djm@openbsd.org7c856852018-03-03 03:15:51 +0000810 sshauthopt_free(principals_opts);
811 sshauthopt_free(cert_opts);
812 sshauthopt_free(final_opts);
Darren Tuckera627d422013-06-02 07:31:17 +1000813 free(principals_file);
814 free(ca_fp);
Damien Miller1aed65e2010-03-04 21:53:35 +1100815 return ret;
816}
817
Damien Miller09d3e122012-10-31 08:58:58 +1100818/*
819 * Checks whether key is allowed in file.
820 * returns 1 if the key is allowed or 0 otherwise.
821 */
822static int
djm@openbsd.org7c856852018-03-03 03:15:51 +0000823user_key_allowed2(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
824 char *file, struct sshauthopt **authoptsp)
Damien Miller09d3e122012-10-31 08:58:58 +1100825{
826 FILE *f;
827 int found_key = 0;
828
djm@openbsd.org7c856852018-03-03 03:15:51 +0000829 if (authoptsp != NULL)
830 *authoptsp = NULL;
831
Damien Miller09d3e122012-10-31 08:58:58 +1100832 /* Temporarily use the user's uid. */
833 temporarily_use_uid(pw);
834
835 debug("trying public key file %s", file);
836 if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) {
djm@openbsd.org7c856852018-03-03 03:15:51 +0000837 found_key = check_authkeys_file(ssh, pw, f, file,
838 key, authoptsp);
Damien Miller09d3e122012-10-31 08:58:58 +1100839 fclose(f);
840 }
841
842 restore_uid();
843 return found_key;
844}
845
846/*
847 * Checks whether key is allowed in output of command.
848 * returns 1 if the key is allowed or 0 otherwise.
849 */
850static int
djm@openbsd.org7c856852018-03-03 03:15:51 +0000851user_key_command_allowed2(struct ssh *ssh, struct passwd *user_pw,
852 struct sshkey *key, struct sshauthopt **authoptsp)
Damien Miller09d3e122012-10-31 08:58:58 +1100853{
djm@openbsd.org7c856852018-03-03 03:15:51 +0000854 struct passwd *runas_pw = NULL;
djm@openbsd.org24232a32015-05-21 06:38:35 +0000855 FILE *f = NULL;
856 int r, ok, found_key = 0;
djm@openbsd.org24232a32015-05-21 06:38:35 +0000857 int i, uid_swapped = 0, ac = 0;
Damien Miller09d3e122012-10-31 08:58:58 +1100858 pid_t pid;
djm@openbsd.org24232a32015-05-21 06:38:35 +0000859 char *username = NULL, *key_fp = NULL, *keytext = NULL;
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000860 char uidstr[32], *tmp, *command = NULL, **av = NULL;
djm@openbsd.org24232a32015-05-21 06:38:35 +0000861 void (*osigchld)(int);
Damien Miller09d3e122012-10-31 08:58:58 +1100862
djm@openbsd.org7c856852018-03-03 03:15:51 +0000863 if (authoptsp != NULL)
864 *authoptsp = NULL;
djm@openbsd.org24232a32015-05-21 06:38:35 +0000865 if (options.authorized_keys_command == NULL)
Damien Miller09d3e122012-10-31 08:58:58 +1100866 return 0;
Damien Millerd0d10992012-11-04 22:23:14 +1100867 if (options.authorized_keys_command_user == NULL) {
868 error("No user for AuthorizedKeysCommand specified, skipping");
869 return 0;
Damien Miller09d3e122012-10-31 08:58:58 +1100870 }
871
djm@openbsd.org24232a32015-05-21 06:38:35 +0000872 /*
873 * NB. all returns later this function should go via "out" to
874 * ensure the original SIGCHLD handler is restored properly.
875 */
876 osigchld = signal(SIGCHLD, SIG_DFL);
877
878 /* Prepare and verify the user for the command */
Damien Millerd0d10992012-11-04 22:23:14 +1100879 username = percent_expand(options.authorized_keys_command_user,
880 "u", user_pw->pw_name, (char *)NULL);
djm@openbsd.org7c856852018-03-03 03:15:51 +0000881 runas_pw = getpwnam(username);
882 if (runas_pw == NULL) {
Damien Miller4018dc02013-02-15 10:28:55 +1100883 error("AuthorizedKeysCommandUser \"%s\" not found: %s",
884 username, strerror(errno));
Damien Miller09d3e122012-10-31 08:58:58 +1100885 goto out;
886 }
887
djm@openbsd.org24232a32015-05-21 06:38:35 +0000888 /* Prepare AuthorizedKeysCommand */
889 if ((key_fp = sshkey_fingerprint(key, options.fingerprint_hash,
890 SSH_FP_DEFAULT)) == NULL) {
891 error("%s: sshkey_fingerprint failed", __func__);
892 goto out;
893 }
894 if ((r = sshkey_to_base64(key, &keytext)) != 0) {
895 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r));
Damien Miller09d3e122012-10-31 08:58:58 +1100896 goto out;
897 }
898
djm@openbsd.org24232a32015-05-21 06:38:35 +0000899 /* Turn the command into an argument vector */
djm@openbsd.orgde4ae072017-08-18 05:36:45 +0000900 if (argv_split(options.authorized_keys_command, &ac, &av) != 0) {
djm@openbsd.org24232a32015-05-21 06:38:35 +0000901 error("AuthorizedKeysCommand \"%s\" contains invalid quotes",
902 command);
903 goto out;
904 }
905 if (ac == 0) {
906 error("AuthorizedKeysCommand \"%s\" yielded no arguments",
907 command);
908 goto out;
909 }
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000910 snprintf(uidstr, sizeof(uidstr), "%llu",
911 (unsigned long long)user_pw->pw_uid);
djm@openbsd.org24232a32015-05-21 06:38:35 +0000912 for (i = 1; i < ac; i++) {
913 tmp = percent_expand(av[i],
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000914 "U", uidstr,
djm@openbsd.org24232a32015-05-21 06:38:35 +0000915 "u", user_pw->pw_name,
916 "h", user_pw->pw_dir,
917 "t", sshkey_ssh_name(key),
918 "f", key_fp,
919 "k", keytext,
920 (char *)NULL);
921 if (tmp == NULL)
922 fatal("%s: percent_expand failed", __func__);
923 free(av[i]);
924 av[i] = tmp;
925 }
926 /* Prepare a printable command for logs, etc. */
djm@openbsd.orgde4ae072017-08-18 05:36:45 +0000927 command = argv_assemble(ac, av);
Damien Miller09d3e122012-10-31 08:58:58 +1100928
929 /*
djm@openbsd.org24232a32015-05-21 06:38:35 +0000930 * If AuthorizedKeysCommand was run without arguments
931 * then fall back to the old behaviour of passing the
932 * target username as a single argument.
Damien Miller09d3e122012-10-31 08:58:58 +1100933 */
djm@openbsd.org24232a32015-05-21 06:38:35 +0000934 if (ac == 1) {
935 av = xreallocarray(av, ac + 2, sizeof(*av));
936 av[1] = xstrdup(user_pw->pw_name);
937 av[2] = NULL;
938 /* Fix up command too, since it is used in log messages */
939 free(command);
940 xasprintf(&command, "%s %s", av[0], av[1]);
Damien Miller09d3e122012-10-31 08:58:58 +1100941 }
942
djm@openbsd.org7c856852018-03-03 03:15:51 +0000943 if ((pid = subprocess("AuthorizedKeysCommand", runas_pw, command,
djm@openbsd.orgde4ae072017-08-18 05:36:45 +0000944 ac, av, &f,
945 SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD)) == 0)
djm@openbsd.org24232a32015-05-21 06:38:35 +0000946 goto out;
947
948 uid_swapped = 1;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000949 temporarily_use_uid(runas_pw);
Damien Miller09d3e122012-10-31 08:58:58 +1100950
djm@openbsd.org7c856852018-03-03 03:15:51 +0000951 ok = check_authkeys_file(ssh, user_pw, f,
952 options.authorized_keys_command, key, authoptsp);
Damien Miller09d3e122012-10-31 08:58:58 +1100953
djm@openbsd.orgddd3d342016-12-30 22:08:02 +0000954 fclose(f);
955 f = NULL;
956
djm@openbsd.orgb074c3c2017-08-18 05:48:04 +0000957 if (exited_cleanly(pid, "AuthorizedKeysCommand", command, 0) != 0)
Damien Miller09d3e122012-10-31 08:58:58 +1100958 goto out;
djm@openbsd.org24232a32015-05-21 06:38:35 +0000959
960 /* Read completed successfully */
Damien Miller09d3e122012-10-31 08:58:58 +1100961 found_key = ok;
962 out:
djm@openbsd.org24232a32015-05-21 06:38:35 +0000963 if (f != NULL)
964 fclose(f);
965 signal(SIGCHLD, osigchld);
966 for (i = 0; i < ac; i++)
967 free(av[i]);
968 free(av);
969 if (uid_swapped)
970 restore_uid();
971 free(command);
972 free(username);
973 free(key_fp);
974 free(keytext);
Damien Miller09d3e122012-10-31 08:58:58 +1100975 return found_key;
976}
977
978/*
979 * Check whether key authenticates and authorises the user.
980 */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000981int
djm@openbsd.org7c856852018-03-03 03:15:51 +0000982user_key_allowed(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
983 int auth_attempt, struct sshauthopt **authoptsp)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000984{
djm@openbsd.orgc95b90d2019-06-14 03:39:59 +0000985 u_int success = 0, i;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000986 char *file;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000987 struct sshauthopt *opts = NULL;
djm@openbsd.orgc95b90d2019-06-14 03:39:59 +0000988
djm@openbsd.org7c856852018-03-03 03:15:51 +0000989 if (authoptsp != NULL)
990 *authoptsp = NULL;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000991
Damien Miller1aed65e2010-03-04 21:53:35 +1100992 if (auth_key_is_revoked(key))
993 return 0;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000994 if (sshkey_is_cert(key) &&
995 auth_key_is_revoked(key->cert->signature_key))
Damien Miller1aed65e2010-03-04 21:53:35 +1100996 return 0;
997
djm@openbsd.orgc95b90d2019-06-14 03:39:59 +0000998 for (i = 0; !success && i < options.num_authkeys_files; i++) {
999 if (strcasecmp(options.authorized_keys_files[i], "none") == 0)
1000 continue;
1001 file = expand_authorized_keys(
1002 options.authorized_keys_files[i], pw);
1003 success = user_key_allowed2(ssh, pw, key, file, &opts);
1004 free(file);
1005 if (!success) {
1006 sshauthopt_free(opts);
1007 opts = NULL;
1008 }
1009 }
1010 if (success)
1011 goto out;
1012
djm@openbsd.org7c856852018-03-03 03:15:51 +00001013 if ((success = user_cert_trusted_ca(ssh, pw, key, &opts)) != 0)
1014 goto out;
1015 sshauthopt_free(opts);
1016 opts = NULL;
Damien Miller1aed65e2010-03-04 21:53:35 +11001017
djm@openbsd.org7c856852018-03-03 03:15:51 +00001018 if ((success = user_key_command_allowed2(ssh, pw, key, &opts)) != 0)
1019 goto out;
1020 sshauthopt_free(opts);
1021 opts = NULL;
Damien Miller09d3e122012-10-31 08:58:58 +11001022
djm@openbsd.org7c856852018-03-03 03:15:51 +00001023 out:
1024 if (success && authoptsp != NULL) {
1025 *authoptsp = opts;
1026 opts = NULL;
1027 }
1028 sshauthopt_free(opts);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001029 return success;
1030}
1031
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001032Authmethod method_pubkey = {
1033 "publickey",
1034 userauth_pubkey,
1035 &options.pubkey_authentication
1036};