blob: 38940a6d958f0f4bc124ca3695b9bbb28c204bfa [file] [log] [blame]
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001/* $OpenBSD: auth2-pubkey.c,v 1.63 2017/05/30 08:52:19 markus 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;
markus@openbsd.org54d90ac2017-05-30 08:52:19 +000081 struct sshkey *key = NULL;
djm@openbsd.orgebacd372016-01-27 00:53:12 +000082 char *pkalg, *userstyle, *fp = NULL;
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) {
djm@openbsd.orgebacd372016-01-27 00:53:12 +000089 debug2("%s: disabled because of invalid user", __func__);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000090 return 0;
91 }
92 have_sig = packet_get_char();
93 if (datafellows & SSH_BUG_PKAUTH) {
djm@openbsd.orgebacd372016-01-27 00:53:12 +000094 debug2("%s: SSH_BUG_PKAUTH", __func__);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000095 /* 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 */
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000109 logit("%s: unsupported public key algorithm: %s",
110 __func__, pkalg);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000111 goto done;
112 }
113 key = key_from_blob(pkblob, blen);
114 if (key == NULL) {
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000115 error("%s: cannot decode key: %s", __func__, pkalg);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000116 goto done;
117 }
118 if (key->type != pktype) {
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000119 error("%s: type mismatch for decoded key "
120 "(received %d, expected %d)", __func__, key->type, pktype);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000121 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.orgebacd372016-01-27 00:53:12 +0000129 fp = sshkey_fingerprint(key, options.fingerprint_hash, SSH_FP_DEFAULT);
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +0000130 if (auth2_userkey_already_used(authctxt, key)) {
131 logit("refusing previously-used %s key", key_type(key));
132 goto done;
133 }
djm@openbsd.orge661a862015-05-04 06:10:48 +0000134 if (match_pattern_list(sshkey_ssh_name(key),
135 options.pubkey_key_types, 0) != 1) {
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000136 logit("%s: key type %s not in PubkeyAcceptedKeyTypes",
137 __func__, sshkey_ssh_name(key));
138 goto done;
139 }
140
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000141 if (have_sig) {
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000142 debug3("%s: have signature for %s %s",
143 __func__, sshkey_type(key), fp);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000144 sig = packet_get_string(&slen);
145 packet_check_eom();
146 buffer_init(&b);
147 if (datafellows & SSH_OLD_SESSIONID) {
148 buffer_append(&b, session_id2, session_id2_len);
149 } else {
150 buffer_put_string(&b, session_id2, session_id2_len);
151 }
152 /* reconstruct packet */
153 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller4ce189d2013-04-23 15:17:52 +1000154 xasprintf(&userstyle, "%s%s%s", authctxt->user,
155 authctxt->style ? ":" : "",
156 authctxt->style ? authctxt->style : "");
157 buffer_put_cstring(&b, userstyle);
158 free(userstyle);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000159 buffer_put_cstring(&b,
160 datafellows & SSH_BUG_PKSERVICE ?
161 "ssh-userauth" :
162 authctxt->service);
163 if (datafellows & SSH_BUG_PKAUTH) {
164 buffer_put_char(&b, have_sig);
165 } else {
166 buffer_put_cstring(&b, "publickey");
167 buffer_put_char(&b, have_sig);
168 buffer_put_cstring(&b, pkalg);
169 }
170 buffer_put_string(&b, pkblob, blen);
171#ifdef DEBUG_PK
172 buffer_dump(&b);
173#endif
Damien Miller20bdcd72013-07-18 16:10:09 +1000174 pubkey_auth_info(authctxt, key, NULL);
Darren Tucker74836ae2013-06-02 07:32:00 +1000175
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000176 /* test for correct signature */
177 authenticated = 0;
djm@openbsd.org179be0f2015-05-01 03:23:51 +0000178 if (PRIVSEP(user_key_allowed(authctxt->pw, key, 1)) &&
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000179 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +0000180 buffer_len(&b))) == 1) {
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000181 authenticated = 1;
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +0000182 /* Record the successful key to prevent reuse */
183 auth2_record_userkey(authctxt, key);
184 key = NULL; /* Don't free below */
185 }
Damien Millerfb1310e2004-01-21 11:02:50 +1100186 buffer_free(&b);
Darren Tuckera627d422013-06-02 07:31:17 +1000187 free(sig);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000188 } else {
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000189 debug("%s: test whether pkalg/pkblob are acceptable for %s %s",
190 __func__, sshkey_type(key), fp);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000191 packet_check_eom();
192
193 /* XXX fake reply and always send PK_OK ? */
194 /*
195 * XXX this allows testing whether a user is allowed
196 * to login: if you happen to have a valid pubkey this
197 * message is sent. the message is NEVER sent at all
198 * if a user is not allowed to login. is this an
199 * issue? -markus
200 */
djm@openbsd.org179be0f2015-05-01 03:23:51 +0000201 if (PRIVSEP(user_key_allowed(authctxt->pw, key, 0))) {
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000202 packet_start(SSH2_MSG_USERAUTH_PK_OK);
203 packet_put_string(pkalg, alen);
204 packet_put_string(pkblob, blen);
205 packet_send();
206 packet_write_wait();
207 authctxt->postponed = 1;
208 }
209 }
210 if (authenticated != 1)
211 auth_clear_options();
212done:
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000213 debug2("%s: authenticated %d pkalg %s", __func__, authenticated, pkalg);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000214 if (key != NULL)
215 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +1000216 free(pkalg);
217 free(pkblob);
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000218 free(fp);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000219 return authenticated;
220}
221
Darren Tucker74836ae2013-06-02 07:32:00 +1000222void
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000223pubkey_auth_info(Authctxt *authctxt, const struct sshkey *key,
224 const char *fmt, ...)
Darren Tucker74836ae2013-06-02 07:32:00 +1000225{
Damien Miller20bdcd72013-07-18 16:10:09 +1000226 char *fp, *extra;
227 va_list ap;
228 int i;
229
230 extra = NULL;
231 if (fmt != NULL) {
232 va_start(ap, fmt);
233 i = vasprintf(&extra, fmt, ap);
234 va_end(ap);
235 if (i < 0 || extra == NULL)
236 fatal("%s: vasprintf failed", __func__);
237 }
Darren Tucker74836ae2013-06-02 07:32:00 +1000238
239 if (key_is_cert(key)) {
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000240 fp = sshkey_fingerprint(key->cert->signature_key,
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000241 options.fingerprint_hash, SSH_FP_DEFAULT);
Damien Miller20bdcd72013-07-18 16:10:09 +1000242 auth_info(authctxt, "%s ID %s (serial %llu) CA %s %s%s%s",
Darren Tucker74836ae2013-06-02 07:32:00 +1000243 key_type(key), key->cert->key_id,
244 (unsigned long long)key->cert->serial,
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000245 key_type(key->cert->signature_key),
djm@openbsd.org13a39412015-02-17 00:14:05 +0000246 fp == NULL ? "(null)" : fp,
Damien Miller20bdcd72013-07-18 16:10:09 +1000247 extra == NULL ? "" : ", ", extra == NULL ? "" : extra);
Darren Tucker74836ae2013-06-02 07:32:00 +1000248 free(fp);
249 } else {
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000250 fp = sshkey_fingerprint(key, options.fingerprint_hash,
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000251 SSH_FP_DEFAULT);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000252 auth_info(authctxt, "%s %s%s%s", key_type(key),
djm@openbsd.org13a39412015-02-17 00:14:05 +0000253 fp == NULL ? "(null)" : fp,
Damien Miller20bdcd72013-07-18 16:10:09 +1000254 extra == NULL ? "" : ", ", extra == NULL ? "" : extra);
Darren Tucker74836ae2013-06-02 07:32:00 +1000255 free(fp);
256 }
Damien Miller20bdcd72013-07-18 16:10:09 +1000257 free(extra);
Darren Tucker74836ae2013-06-02 07:32:00 +1000258}
259
djm@openbsd.org24232a32015-05-21 06:38:35 +0000260/*
261 * Splits 's' into an argument vector. Handles quoted string and basic
262 * escape characters (\\, \", \'). Caller must free the argument vector
263 * and its members.
264 */
265static int
266split_argv(const char *s, int *argcp, char ***argvp)
267{
268 int r = SSH_ERR_INTERNAL_ERROR;
269 int argc = 0, quote, i, j;
270 char *arg, **argv = xcalloc(1, sizeof(*argv));
271
272 *argvp = NULL;
273 *argcp = 0;
274
275 for (i = 0; s[i] != '\0'; i++) {
276 /* Skip leading whitespace */
277 if (s[i] == ' ' || s[i] == '\t')
278 continue;
279
280 /* Start of a token */
281 quote = 0;
282 if (s[i] == '\\' &&
283 (s[i + 1] == '\'' || s[i + 1] == '\"' || s[i + 1] == '\\'))
284 i++;
285 else if (s[i] == '\'' || s[i] == '"')
286 quote = s[i++];
287
288 argv = xreallocarray(argv, (argc + 2), sizeof(*argv));
289 arg = argv[argc++] = xcalloc(1, strlen(s + i) + 1);
290 argv[argc] = NULL;
291
292 /* Copy the token in, removing escapes */
293 for (j = 0; s[i] != '\0'; i++) {
294 if (s[i] == '\\') {
295 if (s[i + 1] == '\'' ||
296 s[i + 1] == '\"' ||
297 s[i + 1] == '\\') {
298 i++; /* Skip '\' */
299 arg[j++] = s[i];
300 } else {
301 /* Unrecognised escape */
302 arg[j++] = s[i];
303 }
304 } else if (quote == 0 && (s[i] == ' ' || s[i] == '\t'))
305 break; /* done */
306 else if (quote != 0 && s[i] == quote)
307 break; /* done */
308 else
309 arg[j++] = s[i];
310 }
311 if (s[i] == '\0') {
312 if (quote != 0) {
313 /* Ran out of string looking for close quote */
314 r = SSH_ERR_INVALID_FORMAT;
315 goto out;
316 }
317 break;
318 }
319 }
320 /* Success */
321 *argcp = argc;
322 *argvp = argv;
323 argc = 0;
324 argv = NULL;
325 r = 0;
326 out:
327 if (argc != 0 && argv != NULL) {
328 for (i = 0; i < argc; i++)
329 free(argv[i]);
330 free(argv);
331 }
332 return r;
333}
334
335/*
336 * Reassemble an argument vector into a string, quoting and escaping as
337 * necessary. Caller must free returned string.
338 */
339static char *
340assemble_argv(int argc, char **argv)
341{
342 int i, j, ws, r;
343 char c, *ret;
344 struct sshbuf *buf, *arg;
345
346 if ((buf = sshbuf_new()) == NULL || (arg = sshbuf_new()) == NULL)
347 fatal("%s: sshbuf_new failed", __func__);
348
349 for (i = 0; i < argc; i++) {
350 ws = 0;
351 sshbuf_reset(arg);
352 for (j = 0; argv[i][j] != '\0'; j++) {
353 r = 0;
354 c = argv[i][j];
355 switch (c) {
356 case ' ':
357 case '\t':
358 ws = 1;
359 r = sshbuf_put_u8(arg, c);
360 break;
361 case '\\':
362 case '\'':
363 case '"':
364 if ((r = sshbuf_put_u8(arg, '\\')) != 0)
365 break;
366 /* FALLTHROUGH */
367 default:
368 r = sshbuf_put_u8(arg, c);
369 break;
370 }
371 if (r != 0)
372 fatal("%s: sshbuf_put_u8: %s",
373 __func__, ssh_err(r));
374 }
375 if ((i != 0 && (r = sshbuf_put_u8(buf, ' ')) != 0) ||
376 (ws != 0 && (r = sshbuf_put_u8(buf, '"')) != 0) ||
377 (r = sshbuf_putb(buf, arg)) != 0 ||
378 (ws != 0 && (r = sshbuf_put_u8(buf, '"')) != 0))
379 fatal("%s: buffer error: %s", __func__, ssh_err(r));
380 }
381 if ((ret = malloc(sshbuf_len(buf) + 1)) == NULL)
382 fatal("%s: malloc failed", __func__);
383 memcpy(ret, sshbuf_ptr(buf), sshbuf_len(buf));
384 ret[sshbuf_len(buf)] = '\0';
385 sshbuf_free(buf);
386 sshbuf_free(arg);
387 return ret;
388}
389
390/*
391 * Runs command in a subprocess. Returns pid on success and a FILE* to the
392 * subprocess' stdout or 0 on failure.
393 * NB. "command" is only used for logging.
394 */
395static pid_t
396subprocess(const char *tag, struct passwd *pw, const char *command,
397 int ac, char **av, FILE **child)
398{
399 FILE *f;
400 struct stat st;
401 int devnull, p[2], i;
402 pid_t pid;
403 char *cp, errmsg[512];
404 u_int envsize;
405 char **child_env;
406
407 *child = NULL;
408
409 debug3("%s: %s command \"%s\" running as %s", __func__,
410 tag, command, pw->pw_name);
411
412 /* Verify the path exists and is safe-ish to execute */
413 if (*av[0] != '/') {
414 error("%s path is not absolute", tag);
415 return 0;
416 }
417 temporarily_use_uid(pw);
418 if (stat(av[0], &st) < 0) {
419 error("Could not stat %s \"%s\": %s", tag,
420 av[0], strerror(errno));
421 restore_uid();
422 return 0;
423 }
424 if (auth_secure_path(av[0], &st, NULL, 0,
425 errmsg, sizeof(errmsg)) != 0) {
426 error("Unsafe %s \"%s\": %s", tag, av[0], errmsg);
427 restore_uid();
428 return 0;
429 }
430
431 /*
432 * Run the command; stderr is left in place, stdout is the
433 * authorized_keys output.
434 */
435 if (pipe(p) != 0) {
436 error("%s: pipe: %s", tag, strerror(errno));
437 restore_uid();
438 return 0;
439 }
440
441 /*
442 * Don't want to call this in the child, where it can fatal() and
443 * run cleanup_exit() code.
444 */
445 restore_uid();
446
447 switch ((pid = fork())) {
448 case -1: /* error */
449 error("%s: fork: %s", tag, strerror(errno));
450 close(p[0]);
451 close(p[1]);
452 return 0;
453 case 0: /* child */
454 /* Prepare a minimal environment for the child. */
455 envsize = 5;
456 child_env = xcalloc(sizeof(*child_env), envsize);
457 child_set_env(&child_env, &envsize, "PATH", _PATH_STDPATH);
458 child_set_env(&child_env, &envsize, "USER", pw->pw_name);
459 child_set_env(&child_env, &envsize, "LOGNAME", pw->pw_name);
460 child_set_env(&child_env, &envsize, "HOME", pw->pw_dir);
461 if ((cp = getenv("LANG")) != NULL)
462 child_set_env(&child_env, &envsize, "LANG", cp);
463
464 for (i = 0; i < NSIG; i++)
465 signal(i, SIG_DFL);
466
467 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) {
468 error("%s: open %s: %s", tag, _PATH_DEVNULL,
469 strerror(errno));
470 _exit(1);
471 }
472 /* Keep stderr around a while longer to catch errors */
473 if (dup2(devnull, STDIN_FILENO) == -1 ||
474 dup2(p[1], STDOUT_FILENO) == -1) {
475 error("%s: dup2: %s", tag, strerror(errno));
476 _exit(1);
477 }
478 closefrom(STDERR_FILENO + 1);
479
480 /* Don't use permanently_set_uid() here to avoid fatal() */
481 if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0) {
482 error("%s: setresgid %u: %s", tag, (u_int)pw->pw_gid,
483 strerror(errno));
484 _exit(1);
485 }
486 if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) != 0) {
487 error("%s: setresuid %u: %s", tag, (u_int)pw->pw_uid,
488 strerror(errno));
489 _exit(1);
490 }
491 /* stdin is pointed to /dev/null at this point */
492 if (dup2(STDIN_FILENO, STDERR_FILENO) == -1) {
493 error("%s: dup2: %s", tag, strerror(errno));
494 _exit(1);
495 }
496
497 execve(av[0], av, child_env);
498 error("%s exec \"%s\": %s", tag, command, strerror(errno));
499 _exit(127);
500 default: /* parent */
501 break;
502 }
503
504 close(p[1]);
505 if ((f = fdopen(p[0], "r")) == NULL) {
506 error("%s: fdopen: %s", tag, strerror(errno));
507 close(p[0]);
508 /* Don't leave zombie child */
509 kill(pid, SIGTERM);
510 while (waitpid(pid, NULL, 0) == -1 && errno == EINTR)
511 ;
512 return 0;
513 }
514 /* Success */
515 debug3("%s: %s pid %ld", __func__, tag, (long)pid);
516 *child = f;
517 return pid;
518}
519
520/* Returns 0 if pid exited cleanly, non-zero otherwise */
521static int
522exited_cleanly(pid_t pid, const char *tag, const char *cmd)
523{
524 int status;
525
526 while (waitpid(pid, &status, 0) == -1) {
527 if (errno != EINTR) {
528 error("%s: waitpid: %s", tag, strerror(errno));
529 return -1;
530 }
531 }
532 if (WIFSIGNALED(status)) {
533 error("%s %s exited on signal %d", tag, cmd, WTERMSIG(status));
534 return -1;
535 } else if (WEXITSTATUS(status) != 0) {
536 error("%s %s failed, status %d", tag, cmd, WEXITSTATUS(status));
537 return -1;
538 }
539 return 0;
540}
541
Damien Miller30da3442010-05-10 11:58:03 +1000542static int
Damien Miller86687062014-07-02 15:28:02 +1000543match_principals_option(const char *principal_list, struct sshkey_cert *cert)
Damien Miller30da3442010-05-10 11:58:03 +1000544{
545 char *result;
546 u_int i;
547
548 /* XXX percent_expand() sequences for authorized_principals? */
549
550 for (i = 0; i < cert->nprincipals; i++) {
551 if ((result = match_list(cert->principals[i],
552 principal_list, NULL)) != NULL) {
553 debug3("matched principal from key options \"%.100s\"",
554 result);
Darren Tuckera627d422013-06-02 07:31:17 +1000555 free(result);
Damien Miller30da3442010-05-10 11:58:03 +1000556 return 1;
557 }
558 }
559 return 0;
560}
561
562static int
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000563process_principals(FILE *f, char *file, struct passwd *pw,
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000564 const struct sshkey_cert *cert)
Damien Miller30da3442010-05-10 11:58:03 +1000565{
Damien Miller6018a362010-07-02 13:35:19 +1000566 char line[SSH_MAX_PUBKEY_BYTES], *cp, *ep, *line_opts;
Damien Miller30da3442010-05-10 11:58:03 +1000567 u_long linenum = 0;
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000568 u_int i, found_principal = 0;
Damien Miller30da3442010-05-10 11:58:03 +1000569
Damien Miller30da3442010-05-10 11:58:03 +1000570 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000571 /* Always consume entire input */
572 if (found_principal)
573 continue;
Damien Miller6018a362010-07-02 13:35:19 +1000574 /* Skip leading whitespace. */
Damien Miller30da3442010-05-10 11:58:03 +1000575 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
576 ;
Damien Miller6018a362010-07-02 13:35:19 +1000577 /* Skip blank and comment lines. */
578 if ((ep = strchr(cp, '#')) != NULL)
579 *ep = '\0';
580 if (!*cp || *cp == '\n')
Damien Miller30da3442010-05-10 11:58:03 +1000581 continue;
Damien Miller6018a362010-07-02 13:35:19 +1000582 /* Trim trailing whitespace. */
583 ep = cp + strlen(cp) - 1;
584 while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t'))
585 *ep-- = '\0';
586 /*
587 * If the line has internal whitespace then assume it has
588 * key options.
589 */
590 line_opts = NULL;
591 if ((ep = strrchr(cp, ' ')) != NULL ||
592 (ep = strrchr(cp, '\t')) != NULL) {
593 for (; *ep == ' ' || *ep == '\t'; ep++)
Damien Miller188ea812010-12-01 11:50:14 +1100594 ;
Damien Miller6018a362010-07-02 13:35:19 +1000595 line_opts = cp;
596 cp = ep;
597 }
Damien Miller30da3442010-05-10 11:58:03 +1000598 for (i = 0; i < cert->nprincipals; i++) {
599 if (strcmp(cp, cert->principals[i]) == 0) {
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000600 debug3("%s:%lu: matched principal \"%.100s\"",
601 file == NULL ? "(command)" : file,
602 linenum, cert->principals[i]);
Damien Miller6018a362010-07-02 13:35:19 +1000603 if (auth_parse_options(pw, line_opts,
604 file, linenum) != 1)
605 continue;
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000606 found_principal = 1;
607 continue;
Damien Miller30da3442010-05-10 11:58:03 +1000608 }
609 }
610 }
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000611 return found_principal;
Damien Miller09d3e122012-10-31 08:58:58 +1100612}
Damien Miller30da3442010-05-10 11:58:03 +1000613
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000614static int
615match_principals_file(char *file, struct passwd *pw, struct sshkey_cert *cert)
616{
617 FILE *f;
618 int success;
619
620 temporarily_use_uid(pw);
621 debug("trying authorized principals file %s", file);
622 if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) {
623 restore_uid();
624 return 0;
625 }
626 success = process_principals(f, file, pw, cert);
627 fclose(f);
628 restore_uid();
629 return success;
630}
631
632/*
633 * Checks whether principal is allowed in output of command.
634 * returns 1 if the principal is allowed or 0 otherwise.
635 */
636static int
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000637match_principals_command(struct passwd *user_pw, const struct sshkey *key)
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000638{
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000639 const struct sshkey_cert *cert = key->cert;
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000640 FILE *f = NULL;
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000641 int r, ok, found_principal = 0;
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000642 struct passwd *pw;
643 int i, ac = 0, uid_swapped = 0;
644 pid_t pid;
645 char *tmp, *username = NULL, *command = NULL, **av = NULL;
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000646 char *ca_fp = NULL, *key_fp = NULL, *catext = NULL, *keytext = NULL;
djm@openbsd.orgbfa9d962016-09-21 01:34:45 +0000647 char serial_s[16];
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000648 void (*osigchld)(int);
649
650 if (options.authorized_principals_command == NULL)
651 return 0;
652 if (options.authorized_principals_command_user == NULL) {
653 error("No user for AuthorizedPrincipalsCommand specified, "
654 "skipping");
655 return 0;
656 }
657
658 /*
659 * NB. all returns later this function should go via "out" to
660 * ensure the original SIGCHLD handler is restored properly.
661 */
662 osigchld = signal(SIGCHLD, SIG_DFL);
663
664 /* Prepare and verify the user for the command */
665 username = percent_expand(options.authorized_principals_command_user,
666 "u", user_pw->pw_name, (char *)NULL);
667 pw = getpwnam(username);
668 if (pw == NULL) {
669 error("AuthorizedPrincipalsCommandUser \"%s\" not found: %s",
670 username, strerror(errno));
671 goto out;
672 }
673
674 /* Turn the command into an argument vector */
675 if (split_argv(options.authorized_principals_command, &ac, &av) != 0) {
676 error("AuthorizedPrincipalsCommand \"%s\" contains "
677 "invalid quotes", command);
678 goto out;
679 }
680 if (ac == 0) {
681 error("AuthorizedPrincipalsCommand \"%s\" yielded no arguments",
682 command);
683 goto out;
684 }
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000685 if ((ca_fp = sshkey_fingerprint(cert->signature_key,
686 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
687 error("%s: sshkey_fingerprint failed", __func__);
688 goto out;
689 }
djm@openbsd.org00df97f2016-09-14 20:11:26 +0000690 if ((key_fp = sshkey_fingerprint(key,
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000691 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
692 error("%s: sshkey_fingerprint failed", __func__);
693 goto out;
694 }
695 if ((r = sshkey_to_base64(cert->signature_key, &catext)) != 0) {
696 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r));
697 goto out;
698 }
699 if ((r = sshkey_to_base64(key, &keytext)) != 0) {
700 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r));
701 goto out;
702 }
djm@openbsd.orgf83a0cf2016-09-21 17:44:20 +0000703 snprintf(serial_s, sizeof(serial_s), "%llu",
704 (unsigned long long)cert->serial);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000705 for (i = 1; i < ac; i++) {
706 tmp = percent_expand(av[i],
707 "u", user_pw->pw_name,
708 "h", user_pw->pw_dir,
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000709 "t", sshkey_ssh_name(key),
710 "T", sshkey_ssh_name(cert->signature_key),
711 "f", key_fp,
712 "F", ca_fp,
713 "k", keytext,
714 "K", catext,
djm@openbsd.orgbfa9d962016-09-21 01:34:45 +0000715 "i", cert->key_id,
716 "s", serial_s,
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000717 (char *)NULL);
718 if (tmp == NULL)
719 fatal("%s: percent_expand failed", __func__);
720 free(av[i]);
721 av[i] = tmp;
722 }
723 /* Prepare a printable command for logs, etc. */
724 command = assemble_argv(ac, av);
725
726 if ((pid = subprocess("AuthorizedPrincipalsCommand", pw, command,
727 ac, av, &f)) == 0)
728 goto out;
729
730 uid_swapped = 1;
731 temporarily_use_uid(pw);
732
jsing@openbsd.orgaff3e942015-06-15 18:42:19 +0000733 ok = process_principals(f, NULL, pw, cert);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000734
djm@openbsd.orgddd3d342016-12-30 22:08:02 +0000735 fclose(f);
736 f = NULL;
737
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000738 if (exited_cleanly(pid, "AuthorizedPrincipalsCommand", command) != 0)
739 goto out;
740
741 /* Read completed successfully */
742 found_principal = ok;
743 out:
744 if (f != NULL)
745 fclose(f);
746 signal(SIGCHLD, osigchld);
747 for (i = 0; i < ac; i++)
748 free(av[i]);
749 free(av);
750 if (uid_swapped)
751 restore_uid();
752 free(command);
753 free(username);
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000754 free(ca_fp);
755 free(key_fp);
756 free(catext);
757 free(keytext);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000758 return found_principal;
759}
Damien Miller09d3e122012-10-31 08:58:58 +1100760/*
761 * Checks whether key is allowed in authorized_keys-format file,
762 * returns 1 if the key is allowed or 0 otherwise.
763 */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000764static int
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000765check_authkeys_file(FILE *f, char *file, struct sshkey* key, struct passwd *pw)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000766{
Darren Tucker22cc7412004-12-06 22:47:41 +1100767 char line[SSH_MAX_PUBKEY_BYTES];
Darren Tucker33c787f2008-07-02 22:37:30 +1000768 int found_key = 0;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000769 u_long linenum = 0;
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000770 struct sshkey *found;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000771
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000772 found_key = 0;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000773
Darren Tucker74836ae2013-06-02 07:32:00 +1000774 found = NULL;
Darren Tucker22cc7412004-12-06 22:47:41 +1100775 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
djm@openbsd.orgfd6dcef2016-11-30 02:57:40 +0000776 char *cp, *key_options = NULL, *fp = NULL;
777 const char *reason = NULL;
778
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000779 /* Always consume entrire file */
780 if (found_key)
781 continue;
Darren Tucker74836ae2013-06-02 07:32:00 +1000782 if (found != NULL)
783 key_free(found);
784 found = key_new(key_is_cert(key) ? KEY_UNSPEC : key->type);
Damien Miller0a80ca12010-02-27 07:55:05 +1100785 auth_clear_options();
786
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000787 /* Skip leading whitespace, empty and comment lines. */
788 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
789 ;
790 if (!*cp || *cp == '\n' || *cp == '#')
791 continue;
792
793 if (key_read(found, &cp) != 1) {
794 /* no key? check if there are options for this key */
795 int quoted = 0;
796 debug2("user_key_allowed: check options: '%s'", cp);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000797 key_options = cp;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000798 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
799 if (*cp == '\\' && cp[1] == '"')
800 cp++; /* Skip both */
801 else if (*cp == '"')
802 quoted = !quoted;
803 }
804 /* Skip remaining whitespace. */
805 for (; *cp == ' ' || *cp == '\t'; cp++)
806 ;
807 if (key_read(found, &cp) != 1) {
808 debug2("user_key_allowed: advance: '%s'", cp);
809 /* still no key? advance to next line*/
810 continue;
811 }
812 }
Damien Miller4e270b02010-04-16 15:56:21 +1000813 if (key_is_cert(key)) {
Damien Miller0a80ca12010-02-27 07:55:05 +1100814 if (!key_equal(found, key->cert->signature_key))
815 continue;
Damien Miller84399552010-05-21 14:58:12 +1000816 if (auth_parse_options(pw, key_options, file,
817 linenum) != 1)
818 continue;
819 if (!key_is_cert_authority)
820 continue;
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000821 if ((fp = sshkey_fingerprint(found,
822 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
823 continue;
Damien Millere513a912010-03-22 05:51:21 +1100824 debug("matching CA found: file %s, line %lu, %s %s",
825 file, linenum, key_type(found), fp);
Damien Miller30da3442010-05-10 11:58:03 +1000826 /*
827 * If the user has specified a list of principals as
828 * a key option, then prefer that list to matching
829 * their username in the certificate principals list.
830 */
831 if (authorized_principals != NULL &&
832 !match_principals_option(authorized_principals,
833 key->cert)) {
834 reason = "Certificate does not contain an "
835 "authorized principal";
836 fail_reason:
Darren Tuckera627d422013-06-02 07:31:17 +1000837 free(fp);
Damien Miller0a80ca12010-02-27 07:55:05 +1100838 error("%s", reason);
839 auth_debug_add("%s", reason);
840 continue;
841 }
Damien Miller30da3442010-05-10 11:58:03 +1000842 if (key_cert_check_authority(key, 0, 0,
843 authorized_principals == NULL ? pw->pw_name : NULL,
844 &reason) != 0)
845 goto fail_reason;
djm@openbsd.orgfd6dcef2016-11-30 02:57:40 +0000846 if (auth_cert_options(key, pw, &reason) != 0)
847 goto fail_reason;
djm@openbsd.org63d18812015-10-27 01:44:45 +0000848 verbose("Accepted certificate ID \"%s\" (serial %llu) "
Damien Millere513a912010-03-22 05:51:21 +1100849 "signed by %s CA %s via %s", key->cert->key_id,
djm@openbsd.org63d18812015-10-27 01:44:45 +0000850 (unsigned long long)key->cert->serial,
Damien Millere513a912010-03-22 05:51:21 +1100851 key_type(found), fp, file);
Darren Tuckera627d422013-06-02 07:31:17 +1000852 free(fp);
Damien Miller0a80ca12010-02-27 07:55:05 +1100853 found_key = 1;
854 break;
Damien Miller84399552010-05-21 14:58:12 +1000855 } else if (key_equal(found, key)) {
856 if (auth_parse_options(pw, key_options, file,
857 linenum) != 1)
858 continue;
859 if (key_is_cert_authority)
860 continue;
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000861 if ((fp = sshkey_fingerprint(found,
862 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
863 continue;
Darren Tucker74836ae2013-06-02 07:32:00 +1000864 debug("matching key found: file %s, line %lu %s %s",
865 file, linenum, key_type(found), fp);
Darren Tuckera627d422013-06-02 07:31:17 +1000866 free(fp);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000867 found_key = 1;
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000868 continue;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000869 }
870 }
Darren Tucker74836ae2013-06-02 07:32:00 +1000871 if (found != NULL)
872 key_free(found);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000873 if (!found_key)
874 debug2("key not found");
875 return found_key;
876}
877
Damien Miller1aed65e2010-03-04 21:53:35 +1100878/* Authenticate a certificate key against TrustedUserCAKeys */
879static int
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000880user_cert_trusted_ca(struct passwd *pw, struct sshkey *key)
Damien Miller1aed65e2010-03-04 21:53:35 +1100881{
Damien Miller30da3442010-05-10 11:58:03 +1000882 char *ca_fp, *principals_file = NULL;
Damien Miller1aed65e2010-03-04 21:53:35 +1100883 const char *reason;
jsing@openbsd.org596dbca2015-06-15 18:44:22 +0000884 int ret = 0, found_principal = 0, use_authorized_principals;
Damien Miller1aed65e2010-03-04 21:53:35 +1100885
886 if (!key_is_cert(key) || options.trusted_user_ca_keys == NULL)
887 return 0;
888
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000889 if ((ca_fp = sshkey_fingerprint(key->cert->signature_key,
890 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
891 return 0;
Damien Miller1aed65e2010-03-04 21:53:35 +1100892
djm@openbsd.org5e39a492014-12-04 02:24:32 +0000893 if (sshkey_in_file(key->cert->signature_key,
894 options.trusted_user_ca_keys, 1, 0) != 0) {
Damien Miller1aed65e2010-03-04 21:53:35 +1100895 debug2("%s: CA %s %s is not listed in %s", __func__,
896 key_type(key->cert->signature_key), ca_fp,
897 options.trusted_user_ca_keys);
898 goto out;
899 }
Damien Miller30da3442010-05-10 11:58:03 +1000900 /*
901 * If AuthorizedPrincipals is in use, then compare the certificate
902 * principals against the names in that file rather than matching
903 * against the username.
904 */
905 if ((principals_file = authorized_principals_file(pw)) != NULL) {
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000906 if (match_principals_file(principals_file, pw, key->cert))
907 found_principal = 1;
908 }
909 /* Try querying command if specified */
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000910 if (!found_principal && match_principals_command(pw, key))
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000911 found_principal = 1;
jsing@openbsd.org596dbca2015-06-15 18:44:22 +0000912 /* If principals file or command is specified, then require a match */
913 use_authorized_principals = principals_file != NULL ||
914 options.authorized_principals_command != NULL;
915 if (!found_principal && use_authorized_principals) {
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000916 reason = "Certificate does not contain an authorized principal";
Damien Miller30da3442010-05-10 11:58:03 +1000917 fail_reason:
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000918 error("%s", reason);
919 auth_debug_add("%s", reason);
920 goto out;
Damien Miller1aed65e2010-03-04 21:53:35 +1100921 }
Damien Miller30da3442010-05-10 11:58:03 +1000922 if (key_cert_check_authority(key, 0, 1,
jsing@openbsd.org596dbca2015-06-15 18:44:22 +0000923 use_authorized_principals ? NULL : pw->pw_name, &reason) != 0)
Damien Miller30da3442010-05-10 11:58:03 +1000924 goto fail_reason;
djm@openbsd.orgfd6dcef2016-11-30 02:57:40 +0000925 if (auth_cert_options(key, pw, &reason) != 0)
926 goto fail_reason;
Damien Miller1aed65e2010-03-04 21:53:35 +1100927
djm@openbsd.org63d18812015-10-27 01:44:45 +0000928 verbose("Accepted certificate ID \"%s\" (serial %llu) signed by "
929 "%s CA %s via %s", key->cert->key_id,
930 (unsigned long long)key->cert->serial,
931 key_type(key->cert->signature_key), ca_fp,
Damien Millere513a912010-03-22 05:51:21 +1100932 options.trusted_user_ca_keys);
Damien Miller1aed65e2010-03-04 21:53:35 +1100933 ret = 1;
934
935 out:
Darren Tuckera627d422013-06-02 07:31:17 +1000936 free(principals_file);
937 free(ca_fp);
Damien Miller1aed65e2010-03-04 21:53:35 +1100938 return ret;
939}
940
Damien Miller09d3e122012-10-31 08:58:58 +1100941/*
942 * Checks whether key is allowed in file.
943 * returns 1 if the key is allowed or 0 otherwise.
944 */
945static int
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000946user_key_allowed2(struct passwd *pw, struct sshkey *key, char *file)
Damien Miller09d3e122012-10-31 08:58:58 +1100947{
948 FILE *f;
949 int found_key = 0;
950
951 /* Temporarily use the user's uid. */
952 temporarily_use_uid(pw);
953
954 debug("trying public key file %s", file);
955 if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) {
956 found_key = check_authkeys_file(f, file, key, pw);
957 fclose(f);
958 }
959
960 restore_uid();
961 return found_key;
962}
963
964/*
965 * Checks whether key is allowed in output of command.
966 * returns 1 if the key is allowed or 0 otherwise.
967 */
968static int
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000969user_key_command_allowed2(struct passwd *user_pw, struct sshkey *key)
Damien Miller09d3e122012-10-31 08:58:58 +1100970{
djm@openbsd.org24232a32015-05-21 06:38:35 +0000971 FILE *f = NULL;
972 int r, ok, found_key = 0;
Damien Miller09d3e122012-10-31 08:58:58 +1100973 struct passwd *pw;
djm@openbsd.org24232a32015-05-21 06:38:35 +0000974 int i, uid_swapped = 0, ac = 0;
Damien Miller09d3e122012-10-31 08:58:58 +1100975 pid_t pid;
djm@openbsd.org24232a32015-05-21 06:38:35 +0000976 char *username = NULL, *key_fp = NULL, *keytext = NULL;
977 char *tmp, *command = NULL, **av = NULL;
978 void (*osigchld)(int);
Damien Miller09d3e122012-10-31 08:58:58 +1100979
djm@openbsd.org24232a32015-05-21 06:38:35 +0000980 if (options.authorized_keys_command == NULL)
Damien Miller09d3e122012-10-31 08:58:58 +1100981 return 0;
Damien Millerd0d10992012-11-04 22:23:14 +1100982 if (options.authorized_keys_command_user == NULL) {
983 error("No user for AuthorizedKeysCommand specified, skipping");
984 return 0;
Damien Miller09d3e122012-10-31 08:58:58 +1100985 }
986
djm@openbsd.org24232a32015-05-21 06:38:35 +0000987 /*
988 * NB. all returns later this function should go via "out" to
989 * ensure the original SIGCHLD handler is restored properly.
990 */
991 osigchld = signal(SIGCHLD, SIG_DFL);
992
993 /* Prepare and verify the user for the command */
Damien Millerd0d10992012-11-04 22:23:14 +1100994 username = percent_expand(options.authorized_keys_command_user,
995 "u", user_pw->pw_name, (char *)NULL);
996 pw = getpwnam(username);
997 if (pw == NULL) {
Damien Miller4018dc02013-02-15 10:28:55 +1100998 error("AuthorizedKeysCommandUser \"%s\" not found: %s",
999 username, strerror(errno));
Damien Miller09d3e122012-10-31 08:58:58 +11001000 goto out;
1001 }
1002
djm@openbsd.org24232a32015-05-21 06:38:35 +00001003 /* Prepare AuthorizedKeysCommand */
1004 if ((key_fp = sshkey_fingerprint(key, options.fingerprint_hash,
1005 SSH_FP_DEFAULT)) == NULL) {
1006 error("%s: sshkey_fingerprint failed", __func__);
1007 goto out;
1008 }
1009 if ((r = sshkey_to_base64(key, &keytext)) != 0) {
1010 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r));
Damien Miller09d3e122012-10-31 08:58:58 +11001011 goto out;
1012 }
1013
djm@openbsd.org24232a32015-05-21 06:38:35 +00001014 /* Turn the command into an argument vector */
1015 if (split_argv(options.authorized_keys_command, &ac, &av) != 0) {
1016 error("AuthorizedKeysCommand \"%s\" contains invalid quotes",
1017 command);
1018 goto out;
1019 }
1020 if (ac == 0) {
1021 error("AuthorizedKeysCommand \"%s\" yielded no arguments",
1022 command);
1023 goto out;
1024 }
1025 for (i = 1; i < ac; i++) {
1026 tmp = percent_expand(av[i],
1027 "u", user_pw->pw_name,
1028 "h", user_pw->pw_dir,
1029 "t", sshkey_ssh_name(key),
1030 "f", key_fp,
1031 "k", keytext,
1032 (char *)NULL);
1033 if (tmp == NULL)
1034 fatal("%s: percent_expand failed", __func__);
1035 free(av[i]);
1036 av[i] = tmp;
1037 }
1038 /* Prepare a printable command for logs, etc. */
1039 command = assemble_argv(ac, av);
Damien Miller09d3e122012-10-31 08:58:58 +11001040
1041 /*
djm@openbsd.org24232a32015-05-21 06:38:35 +00001042 * If AuthorizedKeysCommand was run without arguments
1043 * then fall back to the old behaviour of passing the
1044 * target username as a single argument.
Damien Miller09d3e122012-10-31 08:58:58 +11001045 */
djm@openbsd.org24232a32015-05-21 06:38:35 +00001046 if (ac == 1) {
1047 av = xreallocarray(av, ac + 2, sizeof(*av));
1048 av[1] = xstrdup(user_pw->pw_name);
1049 av[2] = NULL;
1050 /* Fix up command too, since it is used in log messages */
1051 free(command);
1052 xasprintf(&command, "%s %s", av[0], av[1]);
Damien Miller09d3e122012-10-31 08:58:58 +11001053 }
1054
djm@openbsd.org24232a32015-05-21 06:38:35 +00001055 if ((pid = subprocess("AuthorizedKeysCommand", pw, command,
1056 ac, av, &f)) == 0)
1057 goto out;
1058
1059 uid_swapped = 1;
Damien Miller09d3e122012-10-31 08:58:58 +11001060 temporarily_use_uid(pw);
1061
Damien Miller09d3e122012-10-31 08:58:58 +11001062 ok = check_authkeys_file(f, options.authorized_keys_command, key, pw);
Damien Miller09d3e122012-10-31 08:58:58 +11001063
djm@openbsd.orgddd3d342016-12-30 22:08:02 +00001064 fclose(f);
1065 f = NULL;
1066
djm@openbsd.org24232a32015-05-21 06:38:35 +00001067 if (exited_cleanly(pid, "AuthorizedKeysCommand", command) != 0)
Damien Miller09d3e122012-10-31 08:58:58 +11001068 goto out;
djm@openbsd.org24232a32015-05-21 06:38:35 +00001069
1070 /* Read completed successfully */
Damien Miller09d3e122012-10-31 08:58:58 +11001071 found_key = ok;
1072 out:
djm@openbsd.org24232a32015-05-21 06:38:35 +00001073 if (f != NULL)
1074 fclose(f);
1075 signal(SIGCHLD, osigchld);
1076 for (i = 0; i < ac; i++)
1077 free(av[i]);
1078 free(av);
1079 if (uid_swapped)
1080 restore_uid();
1081 free(command);
1082 free(username);
1083 free(key_fp);
1084 free(keytext);
Damien Miller09d3e122012-10-31 08:58:58 +11001085 return found_key;
1086}
1087
1088/*
1089 * Check whether key authenticates and authorises the user.
1090 */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001091int
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001092user_key_allowed(struct passwd *pw, struct sshkey *key, int auth_attempt)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001093{
Damien Millerd8478b62011-05-29 21:39:36 +10001094 u_int success, i;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001095 char *file;
1096
Damien Miller1aed65e2010-03-04 21:53:35 +11001097 if (auth_key_is_revoked(key))
1098 return 0;
1099 if (key_is_cert(key) && auth_key_is_revoked(key->cert->signature_key))
1100 return 0;
1101
1102 success = user_cert_trusted_ca(pw, key);
1103 if (success)
1104 return success;
1105
Damien Miller09d3e122012-10-31 08:58:58 +11001106 success = user_key_command_allowed2(pw, key);
1107 if (success > 0)
1108 return success;
1109
Damien Millerd8478b62011-05-29 21:39:36 +10001110 for (i = 0; !success && i < options.num_authkeys_files; i++) {
Damien Miller09d3e122012-10-31 08:58:58 +11001111
1112 if (strcasecmp(options.authorized_keys_files[i], "none") == 0)
1113 continue;
Damien Millerd8478b62011-05-29 21:39:36 +10001114 file = expand_authorized_keys(
1115 options.authorized_keys_files[i], pw);
Damien Miller09d3e122012-10-31 08:58:58 +11001116
Damien Millerd8478b62011-05-29 21:39:36 +10001117 success = user_key_allowed2(pw, key, file);
Darren Tuckera627d422013-06-02 07:31:17 +10001118 free(file);
Damien Millerd8478b62011-05-29 21:39:36 +10001119 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001120
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001121 return success;
1122}
1123
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001124/* Records a public key in the list of previously-successful keys */
1125void
1126auth2_record_userkey(Authctxt *authctxt, struct sshkey *key)
1127{
1128 struct sshkey **tmp;
1129
1130 if (authctxt->nprev_userkeys >= INT_MAX ||
1131 (tmp = reallocarray(authctxt->prev_userkeys,
1132 authctxt->nprev_userkeys + 1, sizeof(*tmp))) == NULL)
1133 fatal("%s: reallocarray failed", __func__);
1134 authctxt->prev_userkeys = tmp;
1135 authctxt->prev_userkeys[authctxt->nprev_userkeys] = key;
1136 authctxt->nprev_userkeys++;
1137}
1138
1139/* Checks whether a key has already been used successfully for authentication */
1140int
1141auth2_userkey_already_used(Authctxt *authctxt, struct sshkey *key)
1142{
1143 u_int i;
1144
1145 for (i = 0; i < authctxt->nprev_userkeys; i++) {
1146 if (sshkey_equal_public(key, authctxt->prev_userkeys[i])) {
1147 return 1;
1148 }
1149 }
1150 return 0;
1151}
1152
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001153Authmethod method_pubkey = {
1154 "publickey",
1155 userauth_pubkey,
1156 &options.pubkey_authentication
1157};