blob: 5aa319ccc117a6af1e8ef7671463e46051270079 [file] [log] [blame]
jsing@openbsd.org596dbca2015-06-15 18:44:22 +00001/* $OpenBSD: auth2-pubkey.c,v 1.53 2015/06/15 18:44:22 jsing 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"
djm@openbsd.org24232a32015-05-21 06:38:35 +000068#include "ssherr.h"
69#include "channels.h" /* XXX for session.h */
70#include "session.h" /* XXX for child_set_env(); refactor? */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000071
72/* import */
73extern ServerOptions options;
74extern u_char *session_id2;
Darren Tucker502d3842003-06-28 12:38:01 +100075extern u_int session_id2_len;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000076
77static int
78userauth_pubkey(Authctxt *authctxt)
79{
80 Buffer b;
81 Key *key = NULL;
Damien Miller4ce189d2013-04-23 15:17:52 +100082 char *pkalg, *userstyle;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000083 u_char *pkblob, *sig;
84 u_int alen, blen, slen;
85 int have_sig, pktype;
86 int authenticated = 0;
87
88 if (!authctxt->valid) {
89 debug2("userauth_pubkey: disabled because of invalid user");
90 return 0;
91 }
92 have_sig = packet_get_char();
93 if (datafellows & SSH_BUG_PKAUTH) {
94 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
95 /* no explicit pkalg given */
96 pkblob = packet_get_string(&blen);
97 buffer_init(&b);
98 buffer_append(&b, pkblob, blen);
99 /* so we have to extract the pkalg from the pkblob */
100 pkalg = buffer_get_string(&b, &alen);
101 buffer_free(&b);
102 } else {
103 pkalg = packet_get_string(&alen);
104 pkblob = packet_get_string(&blen);
105 }
106 pktype = key_type_from_name(pkalg);
107 if (pktype == KEY_UNSPEC) {
108 /* this is perfectly legal */
Damien Miller996acd22003-04-09 20:59:48 +1000109 logit("userauth_pubkey: unsupported public key algorithm: %s",
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000110 pkalg);
111 goto done;
112 }
113 key = key_from_blob(pkblob, blen);
114 if (key == NULL) {
115 error("userauth_pubkey: cannot decode key: %s", pkalg);
116 goto done;
117 }
118 if (key->type != pktype) {
119 error("userauth_pubkey: type mismatch for decoded key "
120 "(received %d, expected %d)", key->type, pktype);
121 goto done;
122 }
Damien Miller324541e2013-12-31 12:25:40 +1100123 if (key_type_plain(key->type) == KEY_RSA &&
124 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
125 logit("Refusing RSA key because client uses unsafe "
126 "signature scheme");
127 goto done;
128 }
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +0000129 if (auth2_userkey_already_used(authctxt, key)) {
130 logit("refusing previously-used %s key", key_type(key));
131 goto done;
132 }
djm@openbsd.orge661a862015-05-04 06:10:48 +0000133 if (match_pattern_list(sshkey_ssh_name(key),
134 options.pubkey_key_types, 0) != 1) {
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000135 logit("%s: key type %s not in PubkeyAcceptedKeyTypes",
136 __func__, sshkey_ssh_name(key));
137 goto done;
138 }
139
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000140 if (have_sig) {
141 sig = packet_get_string(&slen);
142 packet_check_eom();
143 buffer_init(&b);
144 if (datafellows & SSH_OLD_SESSIONID) {
145 buffer_append(&b, session_id2, session_id2_len);
146 } else {
147 buffer_put_string(&b, session_id2, session_id2_len);
148 }
149 /* reconstruct packet */
150 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller4ce189d2013-04-23 15:17:52 +1000151 xasprintf(&userstyle, "%s%s%s", authctxt->user,
152 authctxt->style ? ":" : "",
153 authctxt->style ? authctxt->style : "");
154 buffer_put_cstring(&b, userstyle);
155 free(userstyle);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000156 buffer_put_cstring(&b,
157 datafellows & SSH_BUG_PKSERVICE ?
158 "ssh-userauth" :
159 authctxt->service);
160 if (datafellows & SSH_BUG_PKAUTH) {
161 buffer_put_char(&b, have_sig);
162 } else {
163 buffer_put_cstring(&b, "publickey");
164 buffer_put_char(&b, have_sig);
165 buffer_put_cstring(&b, pkalg);
166 }
167 buffer_put_string(&b, pkblob, blen);
168#ifdef DEBUG_PK
169 buffer_dump(&b);
170#endif
Damien Miller20bdcd72013-07-18 16:10:09 +1000171 pubkey_auth_info(authctxt, key, NULL);
Darren Tucker74836ae2013-06-02 07:32:00 +1000172
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000173 /* test for correct signature */
174 authenticated = 0;
djm@openbsd.org179be0f2015-05-01 03:23:51 +0000175 if (PRIVSEP(user_key_allowed(authctxt->pw, key, 1)) &&
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000176 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +0000177 buffer_len(&b))) == 1) {
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000178 authenticated = 1;
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +0000179 /* Record the successful key to prevent reuse */
180 auth2_record_userkey(authctxt, key);
181 key = NULL; /* Don't free below */
182 }
Damien Millerfb1310e2004-01-21 11:02:50 +1100183 buffer_free(&b);
Darren Tuckera627d422013-06-02 07:31:17 +1000184 free(sig);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000185 } else {
186 debug("test whether pkalg/pkblob are acceptable");
187 packet_check_eom();
188
189 /* XXX fake reply and always send PK_OK ? */
190 /*
191 * XXX this allows testing whether a user is allowed
192 * to login: if you happen to have a valid pubkey this
193 * message is sent. the message is NEVER sent at all
194 * if a user is not allowed to login. is this an
195 * issue? -markus
196 */
djm@openbsd.org179be0f2015-05-01 03:23:51 +0000197 if (PRIVSEP(user_key_allowed(authctxt->pw, key, 0))) {
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000198 packet_start(SSH2_MSG_USERAUTH_PK_OK);
199 packet_put_string(pkalg, alen);
200 packet_put_string(pkblob, blen);
201 packet_send();
202 packet_write_wait();
203 authctxt->postponed = 1;
204 }
205 }
206 if (authenticated != 1)
207 auth_clear_options();
208done:
209 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
210 if (key != NULL)
211 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +1000212 free(pkalg);
213 free(pkblob);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000214 return authenticated;
215}
216
Darren Tucker74836ae2013-06-02 07:32:00 +1000217void
Damien Miller20bdcd72013-07-18 16:10:09 +1000218pubkey_auth_info(Authctxt *authctxt, const Key *key, const char *fmt, ...)
Darren Tucker74836ae2013-06-02 07:32:00 +1000219{
Damien Miller20bdcd72013-07-18 16:10:09 +1000220 char *fp, *extra;
221 va_list ap;
222 int i;
223
224 extra = NULL;
225 if (fmt != NULL) {
226 va_start(ap, fmt);
227 i = vasprintf(&extra, fmt, ap);
228 va_end(ap);
229 if (i < 0 || extra == NULL)
230 fatal("%s: vasprintf failed", __func__);
231 }
Darren Tucker74836ae2013-06-02 07:32:00 +1000232
233 if (key_is_cert(key)) {
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000234 fp = sshkey_fingerprint(key->cert->signature_key,
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000235 options.fingerprint_hash, SSH_FP_DEFAULT);
Damien Miller20bdcd72013-07-18 16:10:09 +1000236 auth_info(authctxt, "%s ID %s (serial %llu) CA %s %s%s%s",
Darren Tucker74836ae2013-06-02 07:32:00 +1000237 key_type(key), key->cert->key_id,
238 (unsigned long long)key->cert->serial,
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000239 key_type(key->cert->signature_key),
djm@openbsd.org13a39412015-02-17 00:14:05 +0000240 fp == NULL ? "(null)" : fp,
Damien Miller20bdcd72013-07-18 16:10:09 +1000241 extra == NULL ? "" : ", ", extra == NULL ? "" : extra);
Darren Tucker74836ae2013-06-02 07:32:00 +1000242 free(fp);
243 } else {
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000244 fp = sshkey_fingerprint(key, options.fingerprint_hash,
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000245 SSH_FP_DEFAULT);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000246 auth_info(authctxt, "%s %s%s%s", key_type(key),
djm@openbsd.org13a39412015-02-17 00:14:05 +0000247 fp == NULL ? "(null)" : fp,
Damien Miller20bdcd72013-07-18 16:10:09 +1000248 extra == NULL ? "" : ", ", extra == NULL ? "" : extra);
Darren Tucker74836ae2013-06-02 07:32:00 +1000249 free(fp);
250 }
Damien Miller20bdcd72013-07-18 16:10:09 +1000251 free(extra);
Darren Tucker74836ae2013-06-02 07:32:00 +1000252}
253
djm@openbsd.org24232a32015-05-21 06:38:35 +0000254/*
255 * Splits 's' into an argument vector. Handles quoted string and basic
256 * escape characters (\\, \", \'). Caller must free the argument vector
257 * and its members.
258 */
259static int
260split_argv(const char *s, int *argcp, char ***argvp)
261{
262 int r = SSH_ERR_INTERNAL_ERROR;
263 int argc = 0, quote, i, j;
264 char *arg, **argv = xcalloc(1, sizeof(*argv));
265
266 *argvp = NULL;
267 *argcp = 0;
268
269 for (i = 0; s[i] != '\0'; i++) {
270 /* Skip leading whitespace */
271 if (s[i] == ' ' || s[i] == '\t')
272 continue;
273
274 /* Start of a token */
275 quote = 0;
276 if (s[i] == '\\' &&
277 (s[i + 1] == '\'' || s[i + 1] == '\"' || s[i + 1] == '\\'))
278 i++;
279 else if (s[i] == '\'' || s[i] == '"')
280 quote = s[i++];
281
282 argv = xreallocarray(argv, (argc + 2), sizeof(*argv));
283 arg = argv[argc++] = xcalloc(1, strlen(s + i) + 1);
284 argv[argc] = NULL;
285
286 /* Copy the token in, removing escapes */
287 for (j = 0; s[i] != '\0'; i++) {
288 if (s[i] == '\\') {
289 if (s[i + 1] == '\'' ||
290 s[i + 1] == '\"' ||
291 s[i + 1] == '\\') {
292 i++; /* Skip '\' */
293 arg[j++] = s[i];
294 } else {
295 /* Unrecognised escape */
296 arg[j++] = s[i];
297 }
298 } else if (quote == 0 && (s[i] == ' ' || s[i] == '\t'))
299 break; /* done */
300 else if (quote != 0 && s[i] == quote)
301 break; /* done */
302 else
303 arg[j++] = s[i];
304 }
305 if (s[i] == '\0') {
306 if (quote != 0) {
307 /* Ran out of string looking for close quote */
308 r = SSH_ERR_INVALID_FORMAT;
309 goto out;
310 }
311 break;
312 }
313 }
314 /* Success */
315 *argcp = argc;
316 *argvp = argv;
317 argc = 0;
318 argv = NULL;
319 r = 0;
320 out:
321 if (argc != 0 && argv != NULL) {
322 for (i = 0; i < argc; i++)
323 free(argv[i]);
324 free(argv);
325 }
326 return r;
327}
328
329/*
330 * Reassemble an argument vector into a string, quoting and escaping as
331 * necessary. Caller must free returned string.
332 */
333static char *
334assemble_argv(int argc, char **argv)
335{
336 int i, j, ws, r;
337 char c, *ret;
338 struct sshbuf *buf, *arg;
339
340 if ((buf = sshbuf_new()) == NULL || (arg = sshbuf_new()) == NULL)
341 fatal("%s: sshbuf_new failed", __func__);
342
343 for (i = 0; i < argc; i++) {
344 ws = 0;
345 sshbuf_reset(arg);
346 for (j = 0; argv[i][j] != '\0'; j++) {
347 r = 0;
348 c = argv[i][j];
349 switch (c) {
350 case ' ':
351 case '\t':
352 ws = 1;
353 r = sshbuf_put_u8(arg, c);
354 break;
355 case '\\':
356 case '\'':
357 case '"':
358 if ((r = sshbuf_put_u8(arg, '\\')) != 0)
359 break;
360 /* FALLTHROUGH */
361 default:
362 r = sshbuf_put_u8(arg, c);
363 break;
364 }
365 if (r != 0)
366 fatal("%s: sshbuf_put_u8: %s",
367 __func__, ssh_err(r));
368 }
369 if ((i != 0 && (r = sshbuf_put_u8(buf, ' ')) != 0) ||
370 (ws != 0 && (r = sshbuf_put_u8(buf, '"')) != 0) ||
371 (r = sshbuf_putb(buf, arg)) != 0 ||
372 (ws != 0 && (r = sshbuf_put_u8(buf, '"')) != 0))
373 fatal("%s: buffer error: %s", __func__, ssh_err(r));
374 }
375 if ((ret = malloc(sshbuf_len(buf) + 1)) == NULL)
376 fatal("%s: malloc failed", __func__);
377 memcpy(ret, sshbuf_ptr(buf), sshbuf_len(buf));
378 ret[sshbuf_len(buf)] = '\0';
379 sshbuf_free(buf);
380 sshbuf_free(arg);
381 return ret;
382}
383
384/*
385 * Runs command in a subprocess. Returns pid on success and a FILE* to the
386 * subprocess' stdout or 0 on failure.
387 * NB. "command" is only used for logging.
388 */
389static pid_t
390subprocess(const char *tag, struct passwd *pw, const char *command,
391 int ac, char **av, FILE **child)
392{
393 FILE *f;
394 struct stat st;
395 int devnull, p[2], i;
396 pid_t pid;
397 char *cp, errmsg[512];
398 u_int envsize;
399 char **child_env;
400
401 *child = NULL;
402
403 debug3("%s: %s command \"%s\" running as %s", __func__,
404 tag, command, pw->pw_name);
405
406 /* Verify the path exists and is safe-ish to execute */
407 if (*av[0] != '/') {
408 error("%s path is not absolute", tag);
409 return 0;
410 }
411 temporarily_use_uid(pw);
412 if (stat(av[0], &st) < 0) {
413 error("Could not stat %s \"%s\": %s", tag,
414 av[0], strerror(errno));
415 restore_uid();
416 return 0;
417 }
418 if (auth_secure_path(av[0], &st, NULL, 0,
419 errmsg, sizeof(errmsg)) != 0) {
420 error("Unsafe %s \"%s\": %s", tag, av[0], errmsg);
421 restore_uid();
422 return 0;
423 }
424
425 /*
426 * Run the command; stderr is left in place, stdout is the
427 * authorized_keys output.
428 */
429 if (pipe(p) != 0) {
430 error("%s: pipe: %s", tag, strerror(errno));
431 restore_uid();
432 return 0;
433 }
434
435 /*
436 * Don't want to call this in the child, where it can fatal() and
437 * run cleanup_exit() code.
438 */
439 restore_uid();
440
441 switch ((pid = fork())) {
442 case -1: /* error */
443 error("%s: fork: %s", tag, strerror(errno));
444 close(p[0]);
445 close(p[1]);
446 return 0;
447 case 0: /* child */
448 /* Prepare a minimal environment for the child. */
449 envsize = 5;
450 child_env = xcalloc(sizeof(*child_env), envsize);
451 child_set_env(&child_env, &envsize, "PATH", _PATH_STDPATH);
452 child_set_env(&child_env, &envsize, "USER", pw->pw_name);
453 child_set_env(&child_env, &envsize, "LOGNAME", pw->pw_name);
454 child_set_env(&child_env, &envsize, "HOME", pw->pw_dir);
455 if ((cp = getenv("LANG")) != NULL)
456 child_set_env(&child_env, &envsize, "LANG", cp);
457
458 for (i = 0; i < NSIG; i++)
459 signal(i, SIG_DFL);
460
461 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) {
462 error("%s: open %s: %s", tag, _PATH_DEVNULL,
463 strerror(errno));
464 _exit(1);
465 }
466 /* Keep stderr around a while longer to catch errors */
467 if (dup2(devnull, STDIN_FILENO) == -1 ||
468 dup2(p[1], STDOUT_FILENO) == -1) {
469 error("%s: dup2: %s", tag, strerror(errno));
470 _exit(1);
471 }
472 closefrom(STDERR_FILENO + 1);
473
474 /* Don't use permanently_set_uid() here to avoid fatal() */
475 if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0) {
476 error("%s: setresgid %u: %s", tag, (u_int)pw->pw_gid,
477 strerror(errno));
478 _exit(1);
479 }
480 if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) != 0) {
481 error("%s: setresuid %u: %s", tag, (u_int)pw->pw_uid,
482 strerror(errno));
483 _exit(1);
484 }
485 /* stdin is pointed to /dev/null at this point */
486 if (dup2(STDIN_FILENO, STDERR_FILENO) == -1) {
487 error("%s: dup2: %s", tag, strerror(errno));
488 _exit(1);
489 }
490
491 execve(av[0], av, child_env);
492 error("%s exec \"%s\": %s", tag, command, strerror(errno));
493 _exit(127);
494 default: /* parent */
495 break;
496 }
497
498 close(p[1]);
499 if ((f = fdopen(p[0], "r")) == NULL) {
500 error("%s: fdopen: %s", tag, strerror(errno));
501 close(p[0]);
502 /* Don't leave zombie child */
503 kill(pid, SIGTERM);
504 while (waitpid(pid, NULL, 0) == -1 && errno == EINTR)
505 ;
506 return 0;
507 }
508 /* Success */
509 debug3("%s: %s pid %ld", __func__, tag, (long)pid);
510 *child = f;
511 return pid;
512}
513
514/* Returns 0 if pid exited cleanly, non-zero otherwise */
515static int
516exited_cleanly(pid_t pid, const char *tag, const char *cmd)
517{
518 int status;
519
520 while (waitpid(pid, &status, 0) == -1) {
521 if (errno != EINTR) {
522 error("%s: waitpid: %s", tag, strerror(errno));
523 return -1;
524 }
525 }
526 if (WIFSIGNALED(status)) {
527 error("%s %s exited on signal %d", tag, cmd, WTERMSIG(status));
528 return -1;
529 } else if (WEXITSTATUS(status) != 0) {
530 error("%s %s failed, status %d", tag, cmd, WEXITSTATUS(status));
531 return -1;
532 }
533 return 0;
534}
535
Damien Miller30da3442010-05-10 11:58:03 +1000536static int
Damien Miller86687062014-07-02 15:28:02 +1000537match_principals_option(const char *principal_list, struct sshkey_cert *cert)
Damien Miller30da3442010-05-10 11:58:03 +1000538{
539 char *result;
540 u_int i;
541
542 /* XXX percent_expand() sequences for authorized_principals? */
543
544 for (i = 0; i < cert->nprincipals; i++) {
545 if ((result = match_list(cert->principals[i],
546 principal_list, NULL)) != NULL) {
547 debug3("matched principal from key options \"%.100s\"",
548 result);
Darren Tuckera627d422013-06-02 07:31:17 +1000549 free(result);
Damien Miller30da3442010-05-10 11:58:03 +1000550 return 1;
551 }
552 }
553 return 0;
554}
555
556static int
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000557process_principals(FILE *f, char *file, struct passwd *pw,
558 struct sshkey_cert *cert)
Damien Miller30da3442010-05-10 11:58:03 +1000559{
Damien Miller6018a362010-07-02 13:35:19 +1000560 char line[SSH_MAX_PUBKEY_BYTES], *cp, *ep, *line_opts;
Damien Miller30da3442010-05-10 11:58:03 +1000561 u_long linenum = 0;
562 u_int i;
563
Damien Miller30da3442010-05-10 11:58:03 +1000564 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
Damien Miller6018a362010-07-02 13:35:19 +1000565 /* Skip leading whitespace. */
Damien Miller30da3442010-05-10 11:58:03 +1000566 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
567 ;
Damien Miller6018a362010-07-02 13:35:19 +1000568 /* Skip blank and comment lines. */
569 if ((ep = strchr(cp, '#')) != NULL)
570 *ep = '\0';
571 if (!*cp || *cp == '\n')
Damien Miller30da3442010-05-10 11:58:03 +1000572 continue;
Damien Miller6018a362010-07-02 13:35:19 +1000573 /* Trim trailing whitespace. */
574 ep = cp + strlen(cp) - 1;
575 while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t'))
576 *ep-- = '\0';
577 /*
578 * If the line has internal whitespace then assume it has
579 * key options.
580 */
581 line_opts = NULL;
582 if ((ep = strrchr(cp, ' ')) != NULL ||
583 (ep = strrchr(cp, '\t')) != NULL) {
584 for (; *ep == ' ' || *ep == '\t'; ep++)
Damien Miller188ea812010-12-01 11:50:14 +1100585 ;
Damien Miller6018a362010-07-02 13:35:19 +1000586 line_opts = cp;
587 cp = ep;
588 }
Damien Miller30da3442010-05-10 11:58:03 +1000589 for (i = 0; i < cert->nprincipals; i++) {
590 if (strcmp(cp, cert->principals[i]) == 0) {
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000591 debug3("%s:%lu: matched principal \"%.100s\"",
592 file == NULL ? "(command)" : file,
593 linenum, cert->principals[i]);
Damien Miller6018a362010-07-02 13:35:19 +1000594 if (auth_parse_options(pw, line_opts,
595 file, linenum) != 1)
596 continue;
Damien Miller30da3442010-05-10 11:58:03 +1000597 return 1;
598 }
599 }
600 }
Damien Miller30da3442010-05-10 11:58:03 +1000601 return 0;
Damien Miller09d3e122012-10-31 08:58:58 +1100602}
Damien Miller30da3442010-05-10 11:58:03 +1000603
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000604static int
605match_principals_file(char *file, struct passwd *pw, struct sshkey_cert *cert)
606{
607 FILE *f;
608 int success;
609
610 temporarily_use_uid(pw);
611 debug("trying authorized principals file %s", file);
612 if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) {
613 restore_uid();
614 return 0;
615 }
616 success = process_principals(f, file, pw, cert);
617 fclose(f);
618 restore_uid();
619 return success;
620}
621
622/*
623 * Checks whether principal is allowed in output of command.
624 * returns 1 if the principal is allowed or 0 otherwise.
625 */
626static int
jsing@openbsd.orgaff3e942015-06-15 18:42:19 +0000627match_principals_command(struct passwd *user_pw, struct sshkey_cert *cert)
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000628{
629 FILE *f = NULL;
630 int ok, found_principal = 0;
631 struct passwd *pw;
632 int i, ac = 0, uid_swapped = 0;
633 pid_t pid;
634 char *tmp, *username = NULL, *command = NULL, **av = NULL;
635 void (*osigchld)(int);
636
637 if (options.authorized_principals_command == NULL)
638 return 0;
639 if (options.authorized_principals_command_user == NULL) {
640 error("No user for AuthorizedPrincipalsCommand specified, "
641 "skipping");
642 return 0;
643 }
644
645 /*
646 * NB. all returns later this function should go via "out" to
647 * ensure the original SIGCHLD handler is restored properly.
648 */
649 osigchld = signal(SIGCHLD, SIG_DFL);
650
651 /* Prepare and verify the user for the command */
652 username = percent_expand(options.authorized_principals_command_user,
653 "u", user_pw->pw_name, (char *)NULL);
654 pw = getpwnam(username);
655 if (pw == NULL) {
656 error("AuthorizedPrincipalsCommandUser \"%s\" not found: %s",
657 username, strerror(errno));
658 goto out;
659 }
660
661 /* Turn the command into an argument vector */
662 if (split_argv(options.authorized_principals_command, &ac, &av) != 0) {
663 error("AuthorizedPrincipalsCommand \"%s\" contains "
664 "invalid quotes", command);
665 goto out;
666 }
667 if (ac == 0) {
668 error("AuthorizedPrincipalsCommand \"%s\" yielded no arguments",
669 command);
670 goto out;
671 }
672 for (i = 1; i < ac; i++) {
673 tmp = percent_expand(av[i],
674 "u", user_pw->pw_name,
675 "h", user_pw->pw_dir,
676 (char *)NULL);
677 if (tmp == NULL)
678 fatal("%s: percent_expand failed", __func__);
679 free(av[i]);
680 av[i] = tmp;
681 }
682 /* Prepare a printable command for logs, etc. */
683 command = assemble_argv(ac, av);
684
685 if ((pid = subprocess("AuthorizedPrincipalsCommand", pw, command,
686 ac, av, &f)) == 0)
687 goto out;
688
689 uid_swapped = 1;
690 temporarily_use_uid(pw);
691
jsing@openbsd.orgaff3e942015-06-15 18:42:19 +0000692 ok = process_principals(f, NULL, pw, cert);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000693
694 if (exited_cleanly(pid, "AuthorizedPrincipalsCommand", command) != 0)
695 goto out;
696
697 /* Read completed successfully */
698 found_principal = ok;
699 out:
700 if (f != NULL)
701 fclose(f);
702 signal(SIGCHLD, osigchld);
703 for (i = 0; i < ac; i++)
704 free(av[i]);
705 free(av);
706 if (uid_swapped)
707 restore_uid();
708 free(command);
709 free(username);
710 return found_principal;
711}
Damien Miller09d3e122012-10-31 08:58:58 +1100712/*
713 * Checks whether key is allowed in authorized_keys-format file,
714 * returns 1 if the key is allowed or 0 otherwise.
715 */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000716static int
Damien Miller09d3e122012-10-31 08:58:58 +1100717check_authkeys_file(FILE *f, char *file, Key* key, struct passwd *pw)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000718{
Darren Tucker22cc7412004-12-06 22:47:41 +1100719 char line[SSH_MAX_PUBKEY_BYTES];
Damien Miller0a80ca12010-02-27 07:55:05 +1100720 const char *reason;
Darren Tucker33c787f2008-07-02 22:37:30 +1000721 int found_key = 0;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000722 u_long linenum = 0;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000723 Key *found;
724 char *fp;
725
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000726 found_key = 0;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000727
Darren Tucker74836ae2013-06-02 07:32:00 +1000728 found = NULL;
Darren Tucker22cc7412004-12-06 22:47:41 +1100729 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000730 char *cp, *key_options = NULL;
Darren Tucker74836ae2013-06-02 07:32:00 +1000731 if (found != NULL)
732 key_free(found);
733 found = key_new(key_is_cert(key) ? KEY_UNSPEC : key->type);
Damien Miller0a80ca12010-02-27 07:55:05 +1100734 auth_clear_options();
735
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000736 /* Skip leading whitespace, empty and comment lines. */
737 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
738 ;
739 if (!*cp || *cp == '\n' || *cp == '#')
740 continue;
741
742 if (key_read(found, &cp) != 1) {
743 /* no key? check if there are options for this key */
744 int quoted = 0;
745 debug2("user_key_allowed: check options: '%s'", cp);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000746 key_options = cp;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000747 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
748 if (*cp == '\\' && cp[1] == '"')
749 cp++; /* Skip both */
750 else if (*cp == '"')
751 quoted = !quoted;
752 }
753 /* Skip remaining whitespace. */
754 for (; *cp == ' ' || *cp == '\t'; cp++)
755 ;
756 if (key_read(found, &cp) != 1) {
757 debug2("user_key_allowed: advance: '%s'", cp);
758 /* still no key? advance to next line*/
759 continue;
760 }
761 }
Damien Miller4e270b02010-04-16 15:56:21 +1000762 if (key_is_cert(key)) {
Damien Miller0a80ca12010-02-27 07:55:05 +1100763 if (!key_equal(found, key->cert->signature_key))
764 continue;
Damien Miller84399552010-05-21 14:58:12 +1000765 if (auth_parse_options(pw, key_options, file,
766 linenum) != 1)
767 continue;
768 if (!key_is_cert_authority)
769 continue;
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000770 if ((fp = sshkey_fingerprint(found,
771 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
772 continue;
Damien Millere513a912010-03-22 05:51:21 +1100773 debug("matching CA found: file %s, line %lu, %s %s",
774 file, linenum, key_type(found), fp);
Damien Miller30da3442010-05-10 11:58:03 +1000775 /*
776 * If the user has specified a list of principals as
777 * a key option, then prefer that list to matching
778 * their username in the certificate principals list.
779 */
780 if (authorized_principals != NULL &&
781 !match_principals_option(authorized_principals,
782 key->cert)) {
783 reason = "Certificate does not contain an "
784 "authorized principal";
785 fail_reason:
Darren Tuckera627d422013-06-02 07:31:17 +1000786 free(fp);
Damien Miller0a80ca12010-02-27 07:55:05 +1100787 error("%s", reason);
788 auth_debug_add("%s", reason);
789 continue;
790 }
Damien Miller30da3442010-05-10 11:58:03 +1000791 if (key_cert_check_authority(key, 0, 0,
792 authorized_principals == NULL ? pw->pw_name : NULL,
793 &reason) != 0)
794 goto fail_reason;
Damien Miller4e270b02010-04-16 15:56:21 +1000795 if (auth_cert_options(key, pw) != 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000796 free(fp);
Damien Miller0a80ca12010-02-27 07:55:05 +1100797 continue;
Damien Millere513a912010-03-22 05:51:21 +1100798 }
799 verbose("Accepted certificate ID \"%s\" "
800 "signed by %s CA %s via %s", key->cert->key_id,
801 key_type(found), fp, file);
Darren Tuckera627d422013-06-02 07:31:17 +1000802 free(fp);
Damien Miller0a80ca12010-02-27 07:55:05 +1100803 found_key = 1;
804 break;
Damien Miller84399552010-05-21 14:58:12 +1000805 } else if (key_equal(found, key)) {
806 if (auth_parse_options(pw, key_options, file,
807 linenum) != 1)
808 continue;
809 if (key_is_cert_authority)
810 continue;
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000811 if ((fp = sshkey_fingerprint(found,
812 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
813 continue;
Darren Tucker74836ae2013-06-02 07:32:00 +1000814 debug("matching key found: file %s, line %lu %s %s",
815 file, linenum, key_type(found), fp);
Darren Tuckera627d422013-06-02 07:31:17 +1000816 free(fp);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000817 found_key = 1;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000818 break;
819 }
820 }
Darren Tucker74836ae2013-06-02 07:32:00 +1000821 if (found != NULL)
822 key_free(found);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000823 if (!found_key)
824 debug2("key not found");
825 return found_key;
826}
827
Damien Miller1aed65e2010-03-04 21:53:35 +1100828/* Authenticate a certificate key against TrustedUserCAKeys */
829static int
830user_cert_trusted_ca(struct passwd *pw, Key *key)
831{
Damien Miller30da3442010-05-10 11:58:03 +1000832 char *ca_fp, *principals_file = NULL;
Damien Miller1aed65e2010-03-04 21:53:35 +1100833 const char *reason;
jsing@openbsd.org596dbca2015-06-15 18:44:22 +0000834 int ret = 0, found_principal = 0, use_authorized_principals;
Damien Miller1aed65e2010-03-04 21:53:35 +1100835
836 if (!key_is_cert(key) || options.trusted_user_ca_keys == NULL)
837 return 0;
838
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000839 if ((ca_fp = sshkey_fingerprint(key->cert->signature_key,
840 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
841 return 0;
Damien Miller1aed65e2010-03-04 21:53:35 +1100842
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000843 if (sshkey_in_file(key->cert->signature_key,
844 options.trusted_user_ca_keys, 1, 0) != 0) {
Damien Miller1aed65e2010-03-04 21:53:35 +1100845 debug2("%s: CA %s %s is not listed in %s", __func__,
846 key_type(key->cert->signature_key), ca_fp,
847 options.trusted_user_ca_keys);
848 goto out;
849 }
Damien Miller30da3442010-05-10 11:58:03 +1000850 /*
851 * If AuthorizedPrincipals is in use, then compare the certificate
852 * principals against the names in that file rather than matching
853 * against the username.
854 */
855 if ((principals_file = authorized_principals_file(pw)) != NULL) {
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000856 if (match_principals_file(principals_file, pw, key->cert))
857 found_principal = 1;
858 }
859 /* Try querying command if specified */
jsing@openbsd.orgaff3e942015-06-15 18:42:19 +0000860 if (!found_principal && match_principals_command(pw, key->cert))
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000861 found_principal = 1;
jsing@openbsd.org596dbca2015-06-15 18:44:22 +0000862 /* If principals file or command is specified, then require a match */
863 use_authorized_principals = principals_file != NULL ||
864 options.authorized_principals_command != NULL;
865 if (!found_principal && use_authorized_principals) {
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000866 reason = "Certificate does not contain an authorized principal";
Damien Miller30da3442010-05-10 11:58:03 +1000867 fail_reason:
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000868 error("%s", reason);
869 auth_debug_add("%s", reason);
870 goto out;
Damien Miller1aed65e2010-03-04 21:53:35 +1100871 }
Damien Miller30da3442010-05-10 11:58:03 +1000872 if (key_cert_check_authority(key, 0, 1,
jsing@openbsd.org596dbca2015-06-15 18:44:22 +0000873 use_authorized_principals ? NULL : pw->pw_name, &reason) != 0)
Damien Miller30da3442010-05-10 11:58:03 +1000874 goto fail_reason;
Damien Miller4e270b02010-04-16 15:56:21 +1000875 if (auth_cert_options(key, pw) != 0)
Damien Miller1aed65e2010-03-04 21:53:35 +1100876 goto out;
877
Damien Millere513a912010-03-22 05:51:21 +1100878 verbose("Accepted certificate ID \"%s\" signed by %s CA %s via %s",
879 key->cert->key_id, key_type(key->cert->signature_key), ca_fp,
880 options.trusted_user_ca_keys);
Damien Miller1aed65e2010-03-04 21:53:35 +1100881 ret = 1;
882
883 out:
Darren Tuckera627d422013-06-02 07:31:17 +1000884 free(principals_file);
885 free(ca_fp);
Damien Miller1aed65e2010-03-04 21:53:35 +1100886 return ret;
887}
888
Damien Miller09d3e122012-10-31 08:58:58 +1100889/*
890 * Checks whether key is allowed in file.
891 * returns 1 if the key is allowed or 0 otherwise.
892 */
893static int
894user_key_allowed2(struct passwd *pw, Key *key, char *file)
895{
896 FILE *f;
897 int found_key = 0;
898
899 /* Temporarily use the user's uid. */
900 temporarily_use_uid(pw);
901
902 debug("trying public key file %s", file);
903 if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) {
904 found_key = check_authkeys_file(f, file, key, pw);
905 fclose(f);
906 }
907
908 restore_uid();
909 return found_key;
910}
911
912/*
913 * Checks whether key is allowed in output of command.
914 * returns 1 if the key is allowed or 0 otherwise.
915 */
916static int
917user_key_command_allowed2(struct passwd *user_pw, Key *key)
918{
djm@openbsd.org24232a32015-05-21 06:38:35 +0000919 FILE *f = NULL;
920 int r, ok, found_key = 0;
Damien Miller09d3e122012-10-31 08:58:58 +1100921 struct passwd *pw;
djm@openbsd.org24232a32015-05-21 06:38:35 +0000922 int i, uid_swapped = 0, ac = 0;
Damien Miller09d3e122012-10-31 08:58:58 +1100923 pid_t pid;
djm@openbsd.org24232a32015-05-21 06:38:35 +0000924 char *username = NULL, *key_fp = NULL, *keytext = NULL;
925 char *tmp, *command = NULL, **av = NULL;
926 void (*osigchld)(int);
Damien Miller09d3e122012-10-31 08:58:58 +1100927
djm@openbsd.org24232a32015-05-21 06:38:35 +0000928 if (options.authorized_keys_command == NULL)
Damien Miller09d3e122012-10-31 08:58:58 +1100929 return 0;
Damien Millerd0d10992012-11-04 22:23:14 +1100930 if (options.authorized_keys_command_user == NULL) {
931 error("No user for AuthorizedKeysCommand specified, skipping");
932 return 0;
Damien Miller09d3e122012-10-31 08:58:58 +1100933 }
934
djm@openbsd.org24232a32015-05-21 06:38:35 +0000935 /*
936 * NB. all returns later this function should go via "out" to
937 * ensure the original SIGCHLD handler is restored properly.
938 */
939 osigchld = signal(SIGCHLD, SIG_DFL);
940
941 /* Prepare and verify the user for the command */
Damien Millerd0d10992012-11-04 22:23:14 +1100942 username = percent_expand(options.authorized_keys_command_user,
943 "u", user_pw->pw_name, (char *)NULL);
944 pw = getpwnam(username);
945 if (pw == NULL) {
Damien Miller4018dc02013-02-15 10:28:55 +1100946 error("AuthorizedKeysCommandUser \"%s\" not found: %s",
947 username, strerror(errno));
Damien Miller09d3e122012-10-31 08:58:58 +1100948 goto out;
949 }
950
djm@openbsd.org24232a32015-05-21 06:38:35 +0000951 /* Prepare AuthorizedKeysCommand */
952 if ((key_fp = sshkey_fingerprint(key, options.fingerprint_hash,
953 SSH_FP_DEFAULT)) == NULL) {
954 error("%s: sshkey_fingerprint failed", __func__);
955 goto out;
956 }
957 if ((r = sshkey_to_base64(key, &keytext)) != 0) {
958 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r));
Damien Miller09d3e122012-10-31 08:58:58 +1100959 goto out;
960 }
961
djm@openbsd.org24232a32015-05-21 06:38:35 +0000962 /* Turn the command into an argument vector */
963 if (split_argv(options.authorized_keys_command, &ac, &av) != 0) {
964 error("AuthorizedKeysCommand \"%s\" contains invalid quotes",
965 command);
966 goto out;
967 }
968 if (ac == 0) {
969 error("AuthorizedKeysCommand \"%s\" yielded no arguments",
970 command);
971 goto out;
972 }
973 for (i = 1; i < ac; i++) {
974 tmp = percent_expand(av[i],
975 "u", user_pw->pw_name,
976 "h", user_pw->pw_dir,
977 "t", sshkey_ssh_name(key),
978 "f", key_fp,
979 "k", keytext,
980 (char *)NULL);
981 if (tmp == NULL)
982 fatal("%s: percent_expand failed", __func__);
983 free(av[i]);
984 av[i] = tmp;
985 }
986 /* Prepare a printable command for logs, etc. */
987 command = assemble_argv(ac, av);
Damien Miller09d3e122012-10-31 08:58:58 +1100988
989 /*
djm@openbsd.org24232a32015-05-21 06:38:35 +0000990 * If AuthorizedKeysCommand was run without arguments
991 * then fall back to the old behaviour of passing the
992 * target username as a single argument.
Damien Miller09d3e122012-10-31 08:58:58 +1100993 */
djm@openbsd.org24232a32015-05-21 06:38:35 +0000994 if (ac == 1) {
995 av = xreallocarray(av, ac + 2, sizeof(*av));
996 av[1] = xstrdup(user_pw->pw_name);
997 av[2] = NULL;
998 /* Fix up command too, since it is used in log messages */
999 free(command);
1000 xasprintf(&command, "%s %s", av[0], av[1]);
Damien Miller09d3e122012-10-31 08:58:58 +11001001 }
1002
djm@openbsd.org24232a32015-05-21 06:38:35 +00001003 if ((pid = subprocess("AuthorizedKeysCommand", pw, command,
1004 ac, av, &f)) == 0)
1005 goto out;
1006
1007 uid_swapped = 1;
Damien Miller09d3e122012-10-31 08:58:58 +11001008 temporarily_use_uid(pw);
1009
Damien Miller09d3e122012-10-31 08:58:58 +11001010 ok = check_authkeys_file(f, options.authorized_keys_command, key, pw);
Damien Miller09d3e122012-10-31 08:58:58 +11001011
djm@openbsd.org24232a32015-05-21 06:38:35 +00001012 if (exited_cleanly(pid, "AuthorizedKeysCommand", command) != 0)
Damien Miller09d3e122012-10-31 08:58:58 +11001013 goto out;
djm@openbsd.org24232a32015-05-21 06:38:35 +00001014
1015 /* Read completed successfully */
Damien Miller09d3e122012-10-31 08:58:58 +11001016 found_key = ok;
1017 out:
djm@openbsd.org24232a32015-05-21 06:38:35 +00001018 if (f != NULL)
1019 fclose(f);
1020 signal(SIGCHLD, osigchld);
1021 for (i = 0; i < ac; i++)
1022 free(av[i]);
1023 free(av);
1024 if (uid_swapped)
1025 restore_uid();
1026 free(command);
1027 free(username);
1028 free(key_fp);
1029 free(keytext);
Damien Miller09d3e122012-10-31 08:58:58 +11001030 return found_key;
1031}
1032
1033/*
1034 * Check whether key authenticates and authorises the user.
1035 */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001036int
djm@openbsd.org179be0f2015-05-01 03:23:51 +00001037user_key_allowed(struct passwd *pw, Key *key, int auth_attempt)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001038{
Damien Millerd8478b62011-05-29 21:39:36 +10001039 u_int success, i;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001040 char *file;
1041
Damien Miller1aed65e2010-03-04 21:53:35 +11001042 if (auth_key_is_revoked(key))
1043 return 0;
1044 if (key_is_cert(key) && auth_key_is_revoked(key->cert->signature_key))
1045 return 0;
1046
1047 success = user_cert_trusted_ca(pw, key);
1048 if (success)
1049 return success;
1050
Damien Miller09d3e122012-10-31 08:58:58 +11001051 success = user_key_command_allowed2(pw, key);
1052 if (success > 0)
1053 return success;
1054
Damien Millerd8478b62011-05-29 21:39:36 +10001055 for (i = 0; !success && i < options.num_authkeys_files; i++) {
Damien Miller09d3e122012-10-31 08:58:58 +11001056
1057 if (strcasecmp(options.authorized_keys_files[i], "none") == 0)
1058 continue;
Damien Millerd8478b62011-05-29 21:39:36 +10001059 file = expand_authorized_keys(
1060 options.authorized_keys_files[i], pw);
Damien Miller09d3e122012-10-31 08:58:58 +11001061
Damien Millerd8478b62011-05-29 21:39:36 +10001062 success = user_key_allowed2(pw, key, file);
Darren Tuckera627d422013-06-02 07:31:17 +10001063 free(file);
Damien Millerd8478b62011-05-29 21:39:36 +10001064 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001065
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001066 return success;
1067}
1068
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001069/* Records a public key in the list of previously-successful keys */
1070void
1071auth2_record_userkey(Authctxt *authctxt, struct sshkey *key)
1072{
1073 struct sshkey **tmp;
1074
1075 if (authctxt->nprev_userkeys >= INT_MAX ||
1076 (tmp = reallocarray(authctxt->prev_userkeys,
1077 authctxt->nprev_userkeys + 1, sizeof(*tmp))) == NULL)
1078 fatal("%s: reallocarray failed", __func__);
1079 authctxt->prev_userkeys = tmp;
1080 authctxt->prev_userkeys[authctxt->nprev_userkeys] = key;
1081 authctxt->nprev_userkeys++;
1082}
1083
1084/* Checks whether a key has already been used successfully for authentication */
1085int
1086auth2_userkey_already_used(Authctxt *authctxt, struct sshkey *key)
1087{
1088 u_int i;
1089
1090 for (i = 0; i < authctxt->nprev_userkeys; i++) {
1091 if (sshkey_equal_public(key, authctxt->prev_userkeys[i])) {
1092 return 1;
1093 }
1094 }
1095 return 0;
1096}
1097
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001098Authmethod method_pubkey = {
1099 "publickey",
1100 userauth_pubkey,
1101 &options.pubkey_authentication
1102};