blob: 2b04862223c02f62f16e2c8715973d17f4f5bf3b [file] [log] [blame]
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001/* $OpenBSD: auth2-pubkey.c,v 1.44 2014/12/22 07:51:30 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>
Damien Miller09d3e122012-10-31 08:58:58 +110030#include <sys/wait.h>
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000031
Damien Miller09d3e122012-10-31 08:58:58 +110032#include <errno.h>
Darren Tucker06db5842008-06-13 14:51:28 +100033#include <fcntl.h>
Darren Tucker737f7af2012-11-05 17:07:43 +110034#ifdef HAVE_PATHS_H
35# include <paths.h>
36#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100037#include <pwd.h>
Damien Miller09d3e122012-10-31 08:58:58 +110038#include <signal.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100039#include <stdio.h>
Damien Millerd7834352006-08-05 12:39:39 +100040#include <stdarg.h>
Damien Miller0a80ca12010-02-27 07:55:05 +110041#include <string.h>
42#include <time.h>
Darren Tuckerd9526a52008-06-14 09:01:24 +100043#include <unistd.h>
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +000044#include <limits.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100045
Damien Millerd7834352006-08-05 12:39:39 +100046#include "xmalloc.h"
Darren Tucker22cc7412004-12-06 22:47:41 +110047#include "ssh.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000048#include "ssh2.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000049#include "packet.h"
50#include "buffer.h"
51#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100052#include "misc.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000053#include "servconf.h"
54#include "compat.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000055#include "key.h"
Damien Millerd7834352006-08-05 12:39:39 +100056#include "hostfile.h"
57#include "auth.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000058#include "pathnames.h"
59#include "uidswap.h"
60#include "auth-options.h"
61#include "canohost.h"
Damien Millerd7834352006-08-05 12:39:39 +100062#ifdef GSSAPI
63#include "ssh-gss.h"
64#endif
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000065#include "monitor_wrap.h"
Damien Miller1aed65e2010-03-04 21:53:35 +110066#include "authfile.h"
Damien Miller30da3442010-05-10 11:58:03 +100067#include "match.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000068
69/* import */
70extern ServerOptions options;
71extern u_char *session_id2;
Darren Tucker502d3842003-06-28 12:38:01 +100072extern u_int session_id2_len;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000073
74static int
75userauth_pubkey(Authctxt *authctxt)
76{
77 Buffer b;
78 Key *key = NULL;
Damien Miller4ce189d2013-04-23 15:17:52 +100079 char *pkalg, *userstyle;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000080 u_char *pkblob, *sig;
81 u_int alen, blen, slen;
82 int have_sig, pktype;
83 int authenticated = 0;
84
85 if (!authctxt->valid) {
86 debug2("userauth_pubkey: disabled because of invalid user");
87 return 0;
88 }
89 have_sig = packet_get_char();
90 if (datafellows & SSH_BUG_PKAUTH) {
91 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
92 /* no explicit pkalg given */
93 pkblob = packet_get_string(&blen);
94 buffer_init(&b);
95 buffer_append(&b, pkblob, blen);
96 /* so we have to extract the pkalg from the pkblob */
97 pkalg = buffer_get_string(&b, &alen);
98 buffer_free(&b);
99 } else {
100 pkalg = packet_get_string(&alen);
101 pkblob = packet_get_string(&blen);
102 }
103 pktype = key_type_from_name(pkalg);
104 if (pktype == KEY_UNSPEC) {
105 /* this is perfectly legal */
Damien Miller996acd22003-04-09 20:59:48 +1000106 logit("userauth_pubkey: unsupported public key algorithm: %s",
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000107 pkalg);
108 goto done;
109 }
110 key = key_from_blob(pkblob, blen);
111 if (key == NULL) {
112 error("userauth_pubkey: cannot decode key: %s", pkalg);
113 goto done;
114 }
115 if (key->type != pktype) {
116 error("userauth_pubkey: type mismatch for decoded key "
117 "(received %d, expected %d)", key->type, pktype);
118 goto done;
119 }
Damien Miller324541e2013-12-31 12:25:40 +1100120 if (key_type_plain(key->type) == KEY_RSA &&
121 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
122 logit("Refusing RSA key because client uses unsafe "
123 "signature scheme");
124 goto done;
125 }
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +0000126 if (auth2_userkey_already_used(authctxt, key)) {
127 logit("refusing previously-used %s key", key_type(key));
128 goto done;
129 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000130 if (have_sig) {
131 sig = packet_get_string(&slen);
132 packet_check_eom();
133 buffer_init(&b);
134 if (datafellows & SSH_OLD_SESSIONID) {
135 buffer_append(&b, session_id2, session_id2_len);
136 } else {
137 buffer_put_string(&b, session_id2, session_id2_len);
138 }
139 /* reconstruct packet */
140 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller4ce189d2013-04-23 15:17:52 +1000141 xasprintf(&userstyle, "%s%s%s", authctxt->user,
142 authctxt->style ? ":" : "",
143 authctxt->style ? authctxt->style : "");
144 buffer_put_cstring(&b, userstyle);
145 free(userstyle);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000146 buffer_put_cstring(&b,
147 datafellows & SSH_BUG_PKSERVICE ?
148 "ssh-userauth" :
149 authctxt->service);
150 if (datafellows & SSH_BUG_PKAUTH) {
151 buffer_put_char(&b, have_sig);
152 } else {
153 buffer_put_cstring(&b, "publickey");
154 buffer_put_char(&b, have_sig);
155 buffer_put_cstring(&b, pkalg);
156 }
157 buffer_put_string(&b, pkblob, blen);
158#ifdef DEBUG_PK
159 buffer_dump(&b);
160#endif
Damien Miller20bdcd72013-07-18 16:10:09 +1000161 pubkey_auth_info(authctxt, key, NULL);
Darren Tucker74836ae2013-06-02 07:32:00 +1000162
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000163 /* test for correct signature */
164 authenticated = 0;
165 if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
166 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +0000167 buffer_len(&b))) == 1) {
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000168 authenticated = 1;
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +0000169 /* Record the successful key to prevent reuse */
170 auth2_record_userkey(authctxt, key);
171 key = NULL; /* Don't free below */
172 }
Damien Millerfb1310e2004-01-21 11:02:50 +1100173 buffer_free(&b);
Darren Tuckera627d422013-06-02 07:31:17 +1000174 free(sig);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000175 } else {
176 debug("test whether pkalg/pkblob are acceptable");
177 packet_check_eom();
178
179 /* XXX fake reply and always send PK_OK ? */
180 /*
181 * XXX this allows testing whether a user is allowed
182 * to login: if you happen to have a valid pubkey this
183 * message is sent. the message is NEVER sent at all
184 * if a user is not allowed to login. is this an
185 * issue? -markus
186 */
187 if (PRIVSEP(user_key_allowed(authctxt->pw, key))) {
188 packet_start(SSH2_MSG_USERAUTH_PK_OK);
189 packet_put_string(pkalg, alen);
190 packet_put_string(pkblob, blen);
191 packet_send();
192 packet_write_wait();
193 authctxt->postponed = 1;
194 }
195 }
196 if (authenticated != 1)
197 auth_clear_options();
198done:
199 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
200 if (key != NULL)
201 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +1000202 free(pkalg);
203 free(pkblob);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000204 return authenticated;
205}
206
Darren Tucker74836ae2013-06-02 07:32:00 +1000207void
Damien Miller20bdcd72013-07-18 16:10:09 +1000208pubkey_auth_info(Authctxt *authctxt, const Key *key, const char *fmt, ...)
Darren Tucker74836ae2013-06-02 07:32:00 +1000209{
Damien Miller20bdcd72013-07-18 16:10:09 +1000210 char *fp, *extra;
211 va_list ap;
212 int i;
213
214 extra = NULL;
215 if (fmt != NULL) {
216 va_start(ap, fmt);
217 i = vasprintf(&extra, fmt, ap);
218 va_end(ap);
219 if (i < 0 || extra == NULL)
220 fatal("%s: vasprintf failed", __func__);
221 }
Darren Tucker74836ae2013-06-02 07:32:00 +1000222
223 if (key_is_cert(key)) {
224 fp = key_fingerprint(key->cert->signature_key,
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000225 options.fingerprint_hash, SSH_FP_DEFAULT);
Damien Miller20bdcd72013-07-18 16:10:09 +1000226 auth_info(authctxt, "%s ID %s (serial %llu) CA %s %s%s%s",
Darren Tucker74836ae2013-06-02 07:32:00 +1000227 key_type(key), key->cert->key_id,
228 (unsigned long long)key->cert->serial,
Damien Miller20bdcd72013-07-18 16:10:09 +1000229 key_type(key->cert->signature_key), fp,
230 extra == NULL ? "" : ", ", extra == NULL ? "" : extra);
Darren Tucker74836ae2013-06-02 07:32:00 +1000231 free(fp);
232 } else {
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000233 fp = key_fingerprint(key, options.fingerprint_hash,
234 SSH_FP_DEFAULT);
Damien Miller20bdcd72013-07-18 16:10:09 +1000235 auth_info(authctxt, "%s %s%s%s", key_type(key), fp,
236 extra == NULL ? "" : ", ", extra == NULL ? "" : extra);
Darren Tucker74836ae2013-06-02 07:32:00 +1000237 free(fp);
238 }
Damien Miller20bdcd72013-07-18 16:10:09 +1000239 free(extra);
Darren Tucker74836ae2013-06-02 07:32:00 +1000240}
241
Damien Miller30da3442010-05-10 11:58:03 +1000242static int
Damien Miller86687062014-07-02 15:28:02 +1000243match_principals_option(const char *principal_list, struct sshkey_cert *cert)
Damien Miller30da3442010-05-10 11:58:03 +1000244{
245 char *result;
246 u_int i;
247
248 /* XXX percent_expand() sequences for authorized_principals? */
249
250 for (i = 0; i < cert->nprincipals; i++) {
251 if ((result = match_list(cert->principals[i],
252 principal_list, NULL)) != NULL) {
253 debug3("matched principal from key options \"%.100s\"",
254 result);
Darren Tuckera627d422013-06-02 07:31:17 +1000255 free(result);
Damien Miller30da3442010-05-10 11:58:03 +1000256 return 1;
257 }
258 }
259 return 0;
260}
261
262static int
Damien Miller86687062014-07-02 15:28:02 +1000263match_principals_file(char *file, struct passwd *pw, struct sshkey_cert *cert)
Damien Miller30da3442010-05-10 11:58:03 +1000264{
265 FILE *f;
Damien Miller6018a362010-07-02 13:35:19 +1000266 char line[SSH_MAX_PUBKEY_BYTES], *cp, *ep, *line_opts;
Damien Miller30da3442010-05-10 11:58:03 +1000267 u_long linenum = 0;
268 u_int i;
269
270 temporarily_use_uid(pw);
271 debug("trying authorized principals file %s", file);
272 if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) {
273 restore_uid();
274 return 0;
275 }
276 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
Damien Miller6018a362010-07-02 13:35:19 +1000277 /* Skip leading whitespace. */
Damien Miller30da3442010-05-10 11:58:03 +1000278 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
279 ;
Damien Miller6018a362010-07-02 13:35:19 +1000280 /* Skip blank and comment lines. */
281 if ((ep = strchr(cp, '#')) != NULL)
282 *ep = '\0';
283 if (!*cp || *cp == '\n')
Damien Miller30da3442010-05-10 11:58:03 +1000284 continue;
Damien Miller6018a362010-07-02 13:35:19 +1000285 /* Trim trailing whitespace. */
286 ep = cp + strlen(cp) - 1;
287 while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t'))
288 *ep-- = '\0';
289 /*
290 * If the line has internal whitespace then assume it has
291 * key options.
292 */
293 line_opts = NULL;
294 if ((ep = strrchr(cp, ' ')) != NULL ||
295 (ep = strrchr(cp, '\t')) != NULL) {
296 for (; *ep == ' ' || *ep == '\t'; ep++)
Damien Miller188ea812010-12-01 11:50:14 +1100297 ;
Damien Miller6018a362010-07-02 13:35:19 +1000298 line_opts = cp;
299 cp = ep;
300 }
Damien Miller30da3442010-05-10 11:58:03 +1000301 for (i = 0; i < cert->nprincipals; i++) {
302 if (strcmp(cp, cert->principals[i]) == 0) {
Darren Tuckeraf1a60e2011-10-02 18:59:59 +1100303 debug3("matched principal \"%.100s\" "
304 "from file \"%s\" on line %lu",
Damien Miller09d3e122012-10-31 08:58:58 +1100305 cert->principals[i], file, linenum);
Damien Miller6018a362010-07-02 13:35:19 +1000306 if (auth_parse_options(pw, line_opts,
307 file, linenum) != 1)
308 continue;
Damien Miller30da3442010-05-10 11:58:03 +1000309 fclose(f);
310 restore_uid();
311 return 1;
312 }
313 }
314 }
315 fclose(f);
316 restore_uid();
317 return 0;
Damien Miller09d3e122012-10-31 08:58:58 +1100318}
Damien Miller30da3442010-05-10 11:58:03 +1000319
Damien Miller09d3e122012-10-31 08:58:58 +1100320/*
321 * Checks whether key is allowed in authorized_keys-format file,
322 * returns 1 if the key is allowed or 0 otherwise.
323 */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000324static int
Damien Miller09d3e122012-10-31 08:58:58 +1100325check_authkeys_file(FILE *f, char *file, Key* key, struct passwd *pw)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000326{
Darren Tucker22cc7412004-12-06 22:47:41 +1100327 char line[SSH_MAX_PUBKEY_BYTES];
Damien Miller0a80ca12010-02-27 07:55:05 +1100328 const char *reason;
Darren Tucker33c787f2008-07-02 22:37:30 +1000329 int found_key = 0;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000330 u_long linenum = 0;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000331 Key *found;
332 char *fp;
333
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000334 found_key = 0;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000335
Darren Tucker74836ae2013-06-02 07:32:00 +1000336 found = NULL;
Darren Tucker22cc7412004-12-06 22:47:41 +1100337 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000338 char *cp, *key_options = NULL;
Darren Tucker74836ae2013-06-02 07:32:00 +1000339 if (found != NULL)
340 key_free(found);
341 found = key_new(key_is_cert(key) ? KEY_UNSPEC : key->type);
Damien Miller0a80ca12010-02-27 07:55:05 +1100342 auth_clear_options();
343
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000344 /* Skip leading whitespace, empty and comment lines. */
345 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
346 ;
347 if (!*cp || *cp == '\n' || *cp == '#')
348 continue;
349
350 if (key_read(found, &cp) != 1) {
351 /* no key? check if there are options for this key */
352 int quoted = 0;
353 debug2("user_key_allowed: check options: '%s'", cp);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000354 key_options = cp;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000355 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
356 if (*cp == '\\' && cp[1] == '"')
357 cp++; /* Skip both */
358 else if (*cp == '"')
359 quoted = !quoted;
360 }
361 /* Skip remaining whitespace. */
362 for (; *cp == ' ' || *cp == '\t'; cp++)
363 ;
364 if (key_read(found, &cp) != 1) {
365 debug2("user_key_allowed: advance: '%s'", cp);
366 /* still no key? advance to next line*/
367 continue;
368 }
369 }
Damien Miller4e270b02010-04-16 15:56:21 +1000370 if (key_is_cert(key)) {
Damien Miller0a80ca12010-02-27 07:55:05 +1100371 if (!key_equal(found, key->cert->signature_key))
372 continue;
Damien Miller84399552010-05-21 14:58:12 +1000373 if (auth_parse_options(pw, key_options, file,
374 linenum) != 1)
375 continue;
376 if (!key_is_cert_authority)
377 continue;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000378 fp = key_fingerprint(found, options.fingerprint_hash,
379 SSH_FP_DEFAULT);
Damien Millere513a912010-03-22 05:51:21 +1100380 debug("matching CA found: file %s, line %lu, %s %s",
381 file, linenum, key_type(found), fp);
Damien Miller30da3442010-05-10 11:58:03 +1000382 /*
383 * If the user has specified a list of principals as
384 * a key option, then prefer that list to matching
385 * their username in the certificate principals list.
386 */
387 if (authorized_principals != NULL &&
388 !match_principals_option(authorized_principals,
389 key->cert)) {
390 reason = "Certificate does not contain an "
391 "authorized principal";
392 fail_reason:
Darren Tuckera627d422013-06-02 07:31:17 +1000393 free(fp);
Damien Miller0a80ca12010-02-27 07:55:05 +1100394 error("%s", reason);
395 auth_debug_add("%s", reason);
396 continue;
397 }
Damien Miller30da3442010-05-10 11:58:03 +1000398 if (key_cert_check_authority(key, 0, 0,
399 authorized_principals == NULL ? pw->pw_name : NULL,
400 &reason) != 0)
401 goto fail_reason;
Damien Miller4e270b02010-04-16 15:56:21 +1000402 if (auth_cert_options(key, pw) != 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000403 free(fp);
Damien Miller0a80ca12010-02-27 07:55:05 +1100404 continue;
Damien Millere513a912010-03-22 05:51:21 +1100405 }
406 verbose("Accepted certificate ID \"%s\" "
407 "signed by %s CA %s via %s", key->cert->key_id,
408 key_type(found), fp, file);
Darren Tuckera627d422013-06-02 07:31:17 +1000409 free(fp);
Damien Miller0a80ca12010-02-27 07:55:05 +1100410 found_key = 1;
411 break;
Damien Miller84399552010-05-21 14:58:12 +1000412 } else if (key_equal(found, key)) {
413 if (auth_parse_options(pw, key_options, file,
414 linenum) != 1)
415 continue;
416 if (key_is_cert_authority)
417 continue;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000418 found_key = 1;
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000419 fp = key_fingerprint(found, options.fingerprint_hash,
420 SSH_FP_DEFAULT);
Darren Tucker74836ae2013-06-02 07:32:00 +1000421 debug("matching key found: file %s, line %lu %s %s",
422 file, linenum, key_type(found), fp);
Darren Tuckera627d422013-06-02 07:31:17 +1000423 free(fp);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000424 break;
425 }
426 }
Darren Tucker74836ae2013-06-02 07:32:00 +1000427 if (found != NULL)
428 key_free(found);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000429 if (!found_key)
430 debug2("key not found");
431 return found_key;
432}
433
Damien Miller1aed65e2010-03-04 21:53:35 +1100434/* Authenticate a certificate key against TrustedUserCAKeys */
435static int
436user_cert_trusted_ca(struct passwd *pw, Key *key)
437{
Damien Miller30da3442010-05-10 11:58:03 +1000438 char *ca_fp, *principals_file = NULL;
Damien Miller1aed65e2010-03-04 21:53:35 +1100439 const char *reason;
440 int ret = 0;
441
442 if (!key_is_cert(key) || options.trusted_user_ca_keys == NULL)
443 return 0;
444
Damien Millere513a912010-03-22 05:51:21 +1100445 ca_fp = key_fingerprint(key->cert->signature_key,
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000446 options.fingerprint_hash, SSH_FP_DEFAULT);
Damien Miller1aed65e2010-03-04 21:53:35 +1100447
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000448 if (sshkey_in_file(key->cert->signature_key,
449 options.trusted_user_ca_keys, 1, 0) != 0) {
Damien Miller1aed65e2010-03-04 21:53:35 +1100450 debug2("%s: CA %s %s is not listed in %s", __func__,
451 key_type(key->cert->signature_key), ca_fp,
452 options.trusted_user_ca_keys);
453 goto out;
454 }
Damien Miller30da3442010-05-10 11:58:03 +1000455 /*
456 * If AuthorizedPrincipals is in use, then compare the certificate
457 * principals against the names in that file rather than matching
458 * against the username.
459 */
460 if ((principals_file = authorized_principals_file(pw)) != NULL) {
461 if (!match_principals_file(principals_file, pw, key->cert)) {
462 reason = "Certificate does not contain an "
463 "authorized principal";
464 fail_reason:
465 error("%s", reason);
466 auth_debug_add("%s", reason);
467 goto out;
468 }
Damien Miller1aed65e2010-03-04 21:53:35 +1100469 }
Damien Miller30da3442010-05-10 11:58:03 +1000470 if (key_cert_check_authority(key, 0, 1,
471 principals_file == NULL ? pw->pw_name : NULL, &reason) != 0)
472 goto fail_reason;
Damien Miller4e270b02010-04-16 15:56:21 +1000473 if (auth_cert_options(key, pw) != 0)
Damien Miller1aed65e2010-03-04 21:53:35 +1100474 goto out;
475
Damien Millere513a912010-03-22 05:51:21 +1100476 verbose("Accepted certificate ID \"%s\" signed by %s CA %s via %s",
477 key->cert->key_id, key_type(key->cert->signature_key), ca_fp,
478 options.trusted_user_ca_keys);
Damien Miller1aed65e2010-03-04 21:53:35 +1100479 ret = 1;
480
481 out:
Darren Tuckera627d422013-06-02 07:31:17 +1000482 free(principals_file);
483 free(ca_fp);
Damien Miller1aed65e2010-03-04 21:53:35 +1100484 return ret;
485}
486
Damien Miller09d3e122012-10-31 08:58:58 +1100487/*
488 * Checks whether key is allowed in file.
489 * returns 1 if the key is allowed or 0 otherwise.
490 */
491static int
492user_key_allowed2(struct passwd *pw, Key *key, char *file)
493{
494 FILE *f;
495 int found_key = 0;
496
497 /* Temporarily use the user's uid. */
498 temporarily_use_uid(pw);
499
500 debug("trying public key file %s", file);
501 if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) {
502 found_key = check_authkeys_file(f, file, key, pw);
503 fclose(f);
504 }
505
506 restore_uid();
507 return found_key;
508}
509
510/*
511 * Checks whether key is allowed in output of command.
512 * returns 1 if the key is allowed or 0 otherwise.
513 */
514static int
515user_key_command_allowed2(struct passwd *user_pw, Key *key)
516{
517 FILE *f;
518 int ok, found_key = 0;
519 struct passwd *pw;
520 struct stat st;
521 int status, devnull, p[2], i;
522 pid_t pid;
Damien Millerd0d10992012-11-04 22:23:14 +1100523 char *username, errmsg[512];
Damien Miller09d3e122012-10-31 08:58:58 +1100524
525 if (options.authorized_keys_command == NULL ||
526 options.authorized_keys_command[0] != '/')
527 return 0;
528
Damien Millerd0d10992012-11-04 22:23:14 +1100529 if (options.authorized_keys_command_user == NULL) {
530 error("No user for AuthorizedKeysCommand specified, skipping");
531 return 0;
Damien Miller09d3e122012-10-31 08:58:58 +1100532 }
533
Damien Millerd0d10992012-11-04 22:23:14 +1100534 username = percent_expand(options.authorized_keys_command_user,
535 "u", user_pw->pw_name, (char *)NULL);
536 pw = getpwnam(username);
537 if (pw == NULL) {
Damien Miller4018dc02013-02-15 10:28:55 +1100538 error("AuthorizedKeysCommandUser \"%s\" not found: %s",
539 username, strerror(errno));
Damien Millerd0d10992012-11-04 22:23:14 +1100540 free(username);
541 return 0;
542 }
543 free(username);
544
Damien Miller09d3e122012-10-31 08:58:58 +1100545 temporarily_use_uid(pw);
546
547 if (stat(options.authorized_keys_command, &st) < 0) {
548 error("Could not stat AuthorizedKeysCommand \"%s\": %s",
549 options.authorized_keys_command, strerror(errno));
550 goto out;
551 }
552 if (auth_secure_path(options.authorized_keys_command, &st, NULL, 0,
553 errmsg, sizeof(errmsg)) != 0) {
554 error("Unsafe AuthorizedKeysCommand: %s", errmsg);
555 goto out;
556 }
557
558 if (pipe(p) != 0) {
559 error("%s: pipe: %s", __func__, strerror(errno));
560 goto out;
561 }
562
Damien Miller1e854692012-11-14 19:04:02 +1100563 debug3("Running AuthorizedKeysCommand: \"%s %s\" as \"%s\"",
564 options.authorized_keys_command, user_pw->pw_name, pw->pw_name);
Damien Miller09d3e122012-10-31 08:58:58 +1100565
566 /*
567 * Don't want to call this in the child, where it can fatal() and
568 * run cleanup_exit() code.
569 */
570 restore_uid();
571
572 switch ((pid = fork())) {
573 case -1: /* error */
574 error("%s: fork: %s", __func__, strerror(errno));
575 close(p[0]);
576 close(p[1]);
577 return 0;
578 case 0: /* child */
579 for (i = 0; i < NSIG; i++)
580 signal(i, SIG_DFL);
581
Damien Miller1e854692012-11-14 19:04:02 +1100582 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) {
583 error("%s: open %s: %s", __func__, _PATH_DEVNULL,
584 strerror(errno));
585 _exit(1);
586 }
587 /* Keep stderr around a while longer to catch errors */
588 if (dup2(devnull, STDIN_FILENO) == -1 ||
589 dup2(p[1], STDOUT_FILENO) == -1) {
590 error("%s: dup2: %s", __func__, strerror(errno));
591 _exit(1);
592 }
Damien Millerd0d10992012-11-04 22:23:14 +1100593 closefrom(STDERR_FILENO + 1);
Damien Miller1e854692012-11-14 19:04:02 +1100594
Damien Miller09d3e122012-10-31 08:58:58 +1100595 /* Don't use permanently_set_uid() here to avoid fatal() */
596 if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0) {
597 error("setresgid %u: %s", (u_int)pw->pw_gid,
598 strerror(errno));
599 _exit(1);
600 }
601 if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) != 0) {
602 error("setresuid %u: %s", (u_int)pw->pw_uid,
603 strerror(errno));
604 _exit(1);
605 }
Damien Miller1e854692012-11-14 19:04:02 +1100606 /* stdin is pointed to /dev/null at this point */
607 if (dup2(STDIN_FILENO, STDERR_FILENO) == -1) {
Damien Miller09d3e122012-10-31 08:58:58 +1100608 error("%s: dup2: %s", __func__, strerror(errno));
609 _exit(1);
610 }
Damien Miller09d3e122012-10-31 08:58:58 +1100611
612 execl(options.authorized_keys_command,
Damien Miller1e854692012-11-14 19:04:02 +1100613 options.authorized_keys_command, user_pw->pw_name, NULL);
Damien Miller09d3e122012-10-31 08:58:58 +1100614
615 error("AuthorizedKeysCommand %s exec failed: %s",
616 options.authorized_keys_command, strerror(errno));
617 _exit(127);
618 default: /* parent */
619 break;
620 }
621
622 temporarily_use_uid(pw);
623
624 close(p[1]);
625 if ((f = fdopen(p[0], "r")) == NULL) {
626 error("%s: fdopen: %s", __func__, strerror(errno));
627 close(p[0]);
628 /* Don't leave zombie child */
629 kill(pid, SIGTERM);
630 while (waitpid(pid, NULL, 0) == -1 && errno == EINTR)
631 ;
632 goto out;
633 }
634 ok = check_authkeys_file(f, options.authorized_keys_command, key, pw);
635 fclose(f);
636
637 while (waitpid(pid, &status, 0) == -1) {
638 if (errno != EINTR) {
639 error("%s: waitpid: %s", __func__, strerror(errno));
640 goto out;
641 }
642 }
643 if (WIFSIGNALED(status)) {
644 error("AuthorizedKeysCommand %s exited on signal %d",
645 options.authorized_keys_command, WTERMSIG(status));
646 goto out;
647 } else if (WEXITSTATUS(status) != 0) {
648 error("AuthorizedKeysCommand %s returned status %d",
649 options.authorized_keys_command, WEXITSTATUS(status));
650 goto out;
651 }
652 found_key = ok;
653 out:
654 restore_uid();
655 return found_key;
656}
657
658/*
659 * Check whether key authenticates and authorises the user.
660 */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000661int
662user_key_allowed(struct passwd *pw, Key *key)
663{
Damien Millerd8478b62011-05-29 21:39:36 +1000664 u_int success, i;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000665 char *file;
666
Damien Miller1aed65e2010-03-04 21:53:35 +1100667 if (auth_key_is_revoked(key))
668 return 0;
669 if (key_is_cert(key) && auth_key_is_revoked(key->cert->signature_key))
670 return 0;
671
672 success = user_cert_trusted_ca(pw, key);
673 if (success)
674 return success;
675
Damien Miller09d3e122012-10-31 08:58:58 +1100676 success = user_key_command_allowed2(pw, key);
677 if (success > 0)
678 return success;
679
Damien Millerd8478b62011-05-29 21:39:36 +1000680 for (i = 0; !success && i < options.num_authkeys_files; i++) {
Damien Miller09d3e122012-10-31 08:58:58 +1100681
682 if (strcasecmp(options.authorized_keys_files[i], "none") == 0)
683 continue;
Damien Millerd8478b62011-05-29 21:39:36 +1000684 file = expand_authorized_keys(
685 options.authorized_keys_files[i], pw);
Damien Miller09d3e122012-10-31 08:58:58 +1100686
Damien Millerd8478b62011-05-29 21:39:36 +1000687 success = user_key_allowed2(pw, key, file);
Darren Tuckera627d422013-06-02 07:31:17 +1000688 free(file);
Damien Millerd8478b62011-05-29 21:39:36 +1000689 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000690
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000691 return success;
692}
693
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +0000694/* Records a public key in the list of previously-successful keys */
695void
696auth2_record_userkey(Authctxt *authctxt, struct sshkey *key)
697{
698 struct sshkey **tmp;
699
700 if (authctxt->nprev_userkeys >= INT_MAX ||
701 (tmp = reallocarray(authctxt->prev_userkeys,
702 authctxt->nprev_userkeys + 1, sizeof(*tmp))) == NULL)
703 fatal("%s: reallocarray failed", __func__);
704 authctxt->prev_userkeys = tmp;
705 authctxt->prev_userkeys[authctxt->nprev_userkeys] = key;
706 authctxt->nprev_userkeys++;
707}
708
709/* Checks whether a key has already been used successfully for authentication */
710int
711auth2_userkey_already_used(Authctxt *authctxt, struct sshkey *key)
712{
713 u_int i;
714
715 for (i = 0; i < authctxt->nprev_userkeys; i++) {
716 if (sshkey_equal_public(key, authctxt->prev_userkeys[i])) {
717 return 1;
718 }
719 }
720 return 0;
721}
722
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000723Authmethod method_pubkey = {
724 "publickey",
725 userauth_pubkey,
726 &options.pubkey_authentication
727};