blob: a4a0911130978a8ccf3e8f84aa7881170f4661c4 [file] [log] [blame]
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001/* $OpenBSD: auth2-pubkey.c,v 1.64 2017/05/30 14:10:53 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"
markus@openbsd.org00ed75c2017-05-30 14:10:53 +000055#include "sshkey.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{
markus@openbsd.org00ed75c2017-05-30 14:10:53 +000080 struct ssh *ssh = active_state; /* XXX */
81 struct sshbuf *b;
markus@openbsd.org54d90ac2017-05-30 08:52:19 +000082 struct sshkey *key = NULL;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +000083 char *pkalg, *userstyle = NULL, *fp = NULL;
84 u_char *pkblob, *sig, have_sig;
85 size_t blen, slen;
86 int r, pktype;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000087 int authenticated = 0;
88
89 if (!authctxt->valid) {
djm@openbsd.orgebacd372016-01-27 00:53:12 +000090 debug2("%s: disabled because of invalid user", __func__);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000091 return 0;
92 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +000093 if ((r = sshpkt_get_u8(ssh, &have_sig)) != 0)
94 fatal("%s: sshpkt_get_u8 failed: %s", __func__, ssh_err(r));
95 if (ssh->compat & SSH_BUG_PKAUTH) {
djm@openbsd.orgebacd372016-01-27 00:53:12 +000096 debug2("%s: SSH_BUG_PKAUTH", __func__);
markus@openbsd.org00ed75c2017-05-30 14:10:53 +000097 if ((b = sshbuf_new()) == NULL)
98 fatal("%s: sshbuf_new failed", __func__);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000099 /* no explicit pkalg given */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000100 /* so we have to extract the pkalg from the pkblob */
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000101 /* XXX use sshbuf_from() */
102 if ((r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0 ||
103 (r = sshbuf_put(b, pkblob, blen)) != 0 ||
104 (r = sshbuf_get_cstring(b, &pkalg, NULL)) != 0)
105 fatal("%s: failed: %s", __func__, ssh_err(r));
106 sshbuf_free(b);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000107 } else {
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000108 if ((r = sshpkt_get_cstring(ssh, &pkalg, NULL)) != 0 ||
109 (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0)
110 fatal("%s: sshpkt_get_cstring failed: %s",
111 __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000112 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000113 pktype = sshkey_type_from_name(pkalg);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000114 if (pktype == KEY_UNSPEC) {
115 /* this is perfectly legal */
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000116 logit("%s: unsupported public key algorithm: %s",
117 __func__, pkalg);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000118 goto done;
119 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000120 if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
121 error("%s: could not parse key: %s", __func__, ssh_err(r));
122 goto done;
123 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000124 if (key == NULL) {
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000125 error("%s: cannot decode key: %s", __func__, pkalg);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000126 goto done;
127 }
128 if (key->type != pktype) {
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000129 error("%s: type mismatch for decoded key "
130 "(received %d, expected %d)", __func__, key->type, pktype);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000131 goto done;
132 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000133 if (sshkey_type_plain(key->type) == KEY_RSA &&
134 (ssh->compat & SSH_BUG_RSASIGMD5) != 0) {
Damien Miller324541e2013-12-31 12:25:40 +1100135 logit("Refusing RSA key because client uses unsafe "
136 "signature scheme");
137 goto done;
138 }
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000139 fp = sshkey_fingerprint(key, options.fingerprint_hash, SSH_FP_DEFAULT);
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +0000140 if (auth2_userkey_already_used(authctxt, key)) {
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000141 logit("refusing previously-used %s key", sshkey_type(key));
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +0000142 goto done;
143 }
djm@openbsd.orge661a862015-05-04 06:10:48 +0000144 if (match_pattern_list(sshkey_ssh_name(key),
145 options.pubkey_key_types, 0) != 1) {
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000146 logit("%s: key type %s not in PubkeyAcceptedKeyTypes",
147 __func__, sshkey_ssh_name(key));
148 goto done;
149 }
150
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000151 if (have_sig) {
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000152 debug3("%s: have signature for %s %s",
153 __func__, sshkey_type(key), fp);
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000154 if ((r = sshpkt_get_string(ssh, &sig, &slen)) != 0 ||
155 (r = sshpkt_get_end(ssh)) != 0)
156 fatal("%s: %s", __func__, ssh_err(r));
157 if ((b = sshbuf_new()) == NULL)
158 fatal("%s: sshbuf_new failed", __func__);
159 if (ssh->compat & SSH_OLD_SESSIONID) {
160 if ((r = sshbuf_put(b, session_id2,
161 session_id2_len)) != 0)
162 fatal("%s: sshbuf_put session id: %s",
163 __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000164 } else {
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000165 if ((r = sshbuf_put_string(b, session_id2,
166 session_id2_len)) != 0)
167 fatal("%s: sshbuf_put_string session id: %s",
168 __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000169 }
170 /* reconstruct packet */
Damien Miller4ce189d2013-04-23 15:17:52 +1000171 xasprintf(&userstyle, "%s%s%s", authctxt->user,
172 authctxt->style ? ":" : "",
173 authctxt->style ? authctxt->style : "");
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000174 if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
175 (r = sshbuf_put_cstring(b, userstyle)) != 0 ||
176 (r = sshbuf_put_cstring(b, ssh->compat & SSH_BUG_PKSERVICE ?
177 "ssh-userauth" : authctxt->service)) != 0)
178 fatal("%s: build packet failed: %s",
179 __func__, ssh_err(r));
180 if (ssh->compat & SSH_BUG_PKAUTH) {
181 if ((r = sshbuf_put_u8(b, have_sig)) != 0)
182 fatal("%s: build packet failed: %s",
183 __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000184 } else {
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000185 if ((r = sshbuf_put_cstring(b, "publickey")) != 0 ||
186 (r = sshbuf_put_u8(b, have_sig)) != 0 ||
187 (r = sshbuf_put_cstring(b, pkalg) != 0))
188 fatal("%s: build packet failed: %s",
189 __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000190 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000191 if ((r = sshbuf_put_string(b, pkblob, blen)) != 0)
192 fatal("%s: build packet failed: %s",
193 __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000194#ifdef DEBUG_PK
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000195 sshbuf_dump(b, stderr);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000196#endif
Damien Miller20bdcd72013-07-18 16:10:09 +1000197 pubkey_auth_info(authctxt, key, NULL);
Darren Tucker74836ae2013-06-02 07:32:00 +1000198
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000199 /* test for correct signature */
200 authenticated = 0;
djm@openbsd.org179be0f2015-05-01 03:23:51 +0000201 if (PRIVSEP(user_key_allowed(authctxt->pw, key, 1)) &&
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000202 PRIVSEP(sshkey_verify(key, sig, slen, sshbuf_ptr(b),
203 sshbuf_len(b), ssh->compat)) == 0) {
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000204 authenticated = 1;
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +0000205 /* Record the successful key to prevent reuse */
206 auth2_record_userkey(authctxt, key);
207 key = NULL; /* Don't free below */
208 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000209 sshbuf_free(b);
Darren Tuckera627d422013-06-02 07:31:17 +1000210 free(sig);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000211 } else {
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000212 debug("%s: test whether pkalg/pkblob are acceptable for %s %s",
213 __func__, sshkey_type(key), fp);
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000214 if ((r = sshpkt_get_end(ssh)) != 0)
215 fatal("%s: %s", __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000216
217 /* XXX fake reply and always send PK_OK ? */
218 /*
219 * XXX this allows testing whether a user is allowed
220 * to login: if you happen to have a valid pubkey this
221 * message is sent. the message is NEVER sent at all
222 * if a user is not allowed to login. is this an
223 * issue? -markus
224 */
djm@openbsd.org179be0f2015-05-01 03:23:51 +0000225 if (PRIVSEP(user_key_allowed(authctxt->pw, key, 0))) {
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000226 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_PK_OK))
227 != 0 ||
228 (r = sshpkt_put_cstring(ssh, pkalg)) != 0 ||
229 (r = sshpkt_put_string(ssh, pkblob, blen)) != 0 ||
230 (r = sshpkt_send(ssh)) != 0)
231 fatal("%s: %s", __func__, ssh_err(r));
232 ssh_packet_write_wait(ssh);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000233 authctxt->postponed = 1;
234 }
235 }
236 if (authenticated != 1)
237 auth_clear_options();
238done:
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000239 debug2("%s: authenticated %d pkalg %s", __func__, authenticated, pkalg);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000240 if (key != NULL)
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000241 sshkey_free(key);
242 free(userstyle);
Darren Tuckera627d422013-06-02 07:31:17 +1000243 free(pkalg);
244 free(pkblob);
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000245 free(fp);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000246 return authenticated;
247}
248
Darren Tucker74836ae2013-06-02 07:32:00 +1000249void
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000250pubkey_auth_info(Authctxt *authctxt, const struct sshkey *key,
251 const char *fmt, ...)
Darren Tucker74836ae2013-06-02 07:32:00 +1000252{
Damien Miller20bdcd72013-07-18 16:10:09 +1000253 char *fp, *extra;
254 va_list ap;
255 int i;
256
257 extra = NULL;
258 if (fmt != NULL) {
259 va_start(ap, fmt);
260 i = vasprintf(&extra, fmt, ap);
261 va_end(ap);
262 if (i < 0 || extra == NULL)
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000263 fatal("%s: vasprintf failed", __func__);
Damien Miller20bdcd72013-07-18 16:10:09 +1000264 }
Darren Tucker74836ae2013-06-02 07:32:00 +1000265
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000266 if (sshkey_is_cert(key)) {
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000267 fp = sshkey_fingerprint(key->cert->signature_key,
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000268 options.fingerprint_hash, SSH_FP_DEFAULT);
Damien Miller20bdcd72013-07-18 16:10:09 +1000269 auth_info(authctxt, "%s ID %s (serial %llu) CA %s %s%s%s",
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000270 sshkey_type(key), key->cert->key_id,
Darren Tucker74836ae2013-06-02 07:32:00 +1000271 (unsigned long long)key->cert->serial,
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000272 sshkey_type(key->cert->signature_key),
djm@openbsd.org13a39412015-02-17 00:14:05 +0000273 fp == NULL ? "(null)" : fp,
Damien Miller20bdcd72013-07-18 16:10:09 +1000274 extra == NULL ? "" : ", ", extra == NULL ? "" : extra);
Darren Tucker74836ae2013-06-02 07:32:00 +1000275 free(fp);
276 } else {
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000277 fp = sshkey_fingerprint(key, options.fingerprint_hash,
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000278 SSH_FP_DEFAULT);
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000279 auth_info(authctxt, "%s %s%s%s", sshkey_type(key),
djm@openbsd.org13a39412015-02-17 00:14:05 +0000280 fp == NULL ? "(null)" : fp,
Damien Miller20bdcd72013-07-18 16:10:09 +1000281 extra == NULL ? "" : ", ", extra == NULL ? "" : extra);
Darren Tucker74836ae2013-06-02 07:32:00 +1000282 free(fp);
283 }
Damien Miller20bdcd72013-07-18 16:10:09 +1000284 free(extra);
Darren Tucker74836ae2013-06-02 07:32:00 +1000285}
286
djm@openbsd.org24232a32015-05-21 06:38:35 +0000287/*
288 * Splits 's' into an argument vector. Handles quoted string and basic
289 * escape characters (\\, \", \'). Caller must free the argument vector
290 * and its members.
291 */
292static int
293split_argv(const char *s, int *argcp, char ***argvp)
294{
295 int r = SSH_ERR_INTERNAL_ERROR;
296 int argc = 0, quote, i, j;
297 char *arg, **argv = xcalloc(1, sizeof(*argv));
298
299 *argvp = NULL;
300 *argcp = 0;
301
302 for (i = 0; s[i] != '\0'; i++) {
303 /* Skip leading whitespace */
304 if (s[i] == ' ' || s[i] == '\t')
305 continue;
306
307 /* Start of a token */
308 quote = 0;
309 if (s[i] == '\\' &&
310 (s[i + 1] == '\'' || s[i + 1] == '\"' || s[i + 1] == '\\'))
311 i++;
312 else if (s[i] == '\'' || s[i] == '"')
313 quote = s[i++];
314
315 argv = xreallocarray(argv, (argc + 2), sizeof(*argv));
316 arg = argv[argc++] = xcalloc(1, strlen(s + i) + 1);
317 argv[argc] = NULL;
318
319 /* Copy the token in, removing escapes */
320 for (j = 0; s[i] != '\0'; i++) {
321 if (s[i] == '\\') {
322 if (s[i + 1] == '\'' ||
323 s[i + 1] == '\"' ||
324 s[i + 1] == '\\') {
325 i++; /* Skip '\' */
326 arg[j++] = s[i];
327 } else {
328 /* Unrecognised escape */
329 arg[j++] = s[i];
330 }
331 } else if (quote == 0 && (s[i] == ' ' || s[i] == '\t'))
332 break; /* done */
333 else if (quote != 0 && s[i] == quote)
334 break; /* done */
335 else
336 arg[j++] = s[i];
337 }
338 if (s[i] == '\0') {
339 if (quote != 0) {
340 /* Ran out of string looking for close quote */
341 r = SSH_ERR_INVALID_FORMAT;
342 goto out;
343 }
344 break;
345 }
346 }
347 /* Success */
348 *argcp = argc;
349 *argvp = argv;
350 argc = 0;
351 argv = NULL;
352 r = 0;
353 out:
354 if (argc != 0 && argv != NULL) {
355 for (i = 0; i < argc; i++)
356 free(argv[i]);
357 free(argv);
358 }
359 return r;
360}
361
362/*
363 * Reassemble an argument vector into a string, quoting and escaping as
364 * necessary. Caller must free returned string.
365 */
366static char *
367assemble_argv(int argc, char **argv)
368{
369 int i, j, ws, r;
370 char c, *ret;
371 struct sshbuf *buf, *arg;
372
373 if ((buf = sshbuf_new()) == NULL || (arg = sshbuf_new()) == NULL)
374 fatal("%s: sshbuf_new failed", __func__);
375
376 for (i = 0; i < argc; i++) {
377 ws = 0;
378 sshbuf_reset(arg);
379 for (j = 0; argv[i][j] != '\0'; j++) {
380 r = 0;
381 c = argv[i][j];
382 switch (c) {
383 case ' ':
384 case '\t':
385 ws = 1;
386 r = sshbuf_put_u8(arg, c);
387 break;
388 case '\\':
389 case '\'':
390 case '"':
391 if ((r = sshbuf_put_u8(arg, '\\')) != 0)
392 break;
393 /* FALLTHROUGH */
394 default:
395 r = sshbuf_put_u8(arg, c);
396 break;
397 }
398 if (r != 0)
399 fatal("%s: sshbuf_put_u8: %s",
400 __func__, ssh_err(r));
401 }
402 if ((i != 0 && (r = sshbuf_put_u8(buf, ' ')) != 0) ||
403 (ws != 0 && (r = sshbuf_put_u8(buf, '"')) != 0) ||
404 (r = sshbuf_putb(buf, arg)) != 0 ||
405 (ws != 0 && (r = sshbuf_put_u8(buf, '"')) != 0))
406 fatal("%s: buffer error: %s", __func__, ssh_err(r));
407 }
408 if ((ret = malloc(sshbuf_len(buf) + 1)) == NULL)
409 fatal("%s: malloc failed", __func__);
410 memcpy(ret, sshbuf_ptr(buf), sshbuf_len(buf));
411 ret[sshbuf_len(buf)] = '\0';
412 sshbuf_free(buf);
413 sshbuf_free(arg);
414 return ret;
415}
416
417/*
418 * Runs command in a subprocess. Returns pid on success and a FILE* to the
419 * subprocess' stdout or 0 on failure.
420 * NB. "command" is only used for logging.
421 */
422static pid_t
423subprocess(const char *tag, struct passwd *pw, const char *command,
424 int ac, char **av, FILE **child)
425{
426 FILE *f;
427 struct stat st;
428 int devnull, p[2], i;
429 pid_t pid;
430 char *cp, errmsg[512];
431 u_int envsize;
432 char **child_env;
433
434 *child = NULL;
435
436 debug3("%s: %s command \"%s\" running as %s", __func__,
437 tag, command, pw->pw_name);
438
439 /* Verify the path exists and is safe-ish to execute */
440 if (*av[0] != '/') {
441 error("%s path is not absolute", tag);
442 return 0;
443 }
444 temporarily_use_uid(pw);
445 if (stat(av[0], &st) < 0) {
446 error("Could not stat %s \"%s\": %s", tag,
447 av[0], strerror(errno));
448 restore_uid();
449 return 0;
450 }
451 if (auth_secure_path(av[0], &st, NULL, 0,
452 errmsg, sizeof(errmsg)) != 0) {
453 error("Unsafe %s \"%s\": %s", tag, av[0], errmsg);
454 restore_uid();
455 return 0;
456 }
457
458 /*
459 * Run the command; stderr is left in place, stdout is the
460 * authorized_keys output.
461 */
462 if (pipe(p) != 0) {
463 error("%s: pipe: %s", tag, strerror(errno));
464 restore_uid();
465 return 0;
466 }
467
468 /*
469 * Don't want to call this in the child, where it can fatal() and
470 * run cleanup_exit() code.
471 */
472 restore_uid();
473
474 switch ((pid = fork())) {
475 case -1: /* error */
476 error("%s: fork: %s", tag, strerror(errno));
477 close(p[0]);
478 close(p[1]);
479 return 0;
480 case 0: /* child */
481 /* Prepare a minimal environment for the child. */
482 envsize = 5;
483 child_env = xcalloc(sizeof(*child_env), envsize);
484 child_set_env(&child_env, &envsize, "PATH", _PATH_STDPATH);
485 child_set_env(&child_env, &envsize, "USER", pw->pw_name);
486 child_set_env(&child_env, &envsize, "LOGNAME", pw->pw_name);
487 child_set_env(&child_env, &envsize, "HOME", pw->pw_dir);
488 if ((cp = getenv("LANG")) != NULL)
489 child_set_env(&child_env, &envsize, "LANG", cp);
490
491 for (i = 0; i < NSIG; i++)
492 signal(i, SIG_DFL);
493
494 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) {
495 error("%s: open %s: %s", tag, _PATH_DEVNULL,
496 strerror(errno));
497 _exit(1);
498 }
499 /* Keep stderr around a while longer to catch errors */
500 if (dup2(devnull, STDIN_FILENO) == -1 ||
501 dup2(p[1], STDOUT_FILENO) == -1) {
502 error("%s: dup2: %s", tag, strerror(errno));
503 _exit(1);
504 }
505 closefrom(STDERR_FILENO + 1);
506
507 /* Don't use permanently_set_uid() here to avoid fatal() */
508 if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0) {
509 error("%s: setresgid %u: %s", tag, (u_int)pw->pw_gid,
510 strerror(errno));
511 _exit(1);
512 }
513 if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) != 0) {
514 error("%s: setresuid %u: %s", tag, (u_int)pw->pw_uid,
515 strerror(errno));
516 _exit(1);
517 }
518 /* stdin is pointed to /dev/null at this point */
519 if (dup2(STDIN_FILENO, STDERR_FILENO) == -1) {
520 error("%s: dup2: %s", tag, strerror(errno));
521 _exit(1);
522 }
523
524 execve(av[0], av, child_env);
525 error("%s exec \"%s\": %s", tag, command, strerror(errno));
526 _exit(127);
527 default: /* parent */
528 break;
529 }
530
531 close(p[1]);
532 if ((f = fdopen(p[0], "r")) == NULL) {
533 error("%s: fdopen: %s", tag, strerror(errno));
534 close(p[0]);
535 /* Don't leave zombie child */
536 kill(pid, SIGTERM);
537 while (waitpid(pid, NULL, 0) == -1 && errno == EINTR)
538 ;
539 return 0;
540 }
541 /* Success */
542 debug3("%s: %s pid %ld", __func__, tag, (long)pid);
543 *child = f;
544 return pid;
545}
546
547/* Returns 0 if pid exited cleanly, non-zero otherwise */
548static int
549exited_cleanly(pid_t pid, const char *tag, const char *cmd)
550{
551 int status;
552
553 while (waitpid(pid, &status, 0) == -1) {
554 if (errno != EINTR) {
555 error("%s: waitpid: %s", tag, strerror(errno));
556 return -1;
557 }
558 }
559 if (WIFSIGNALED(status)) {
560 error("%s %s exited on signal %d", tag, cmd, WTERMSIG(status));
561 return -1;
562 } else if (WEXITSTATUS(status) != 0) {
563 error("%s %s failed, status %d", tag, cmd, WEXITSTATUS(status));
564 return -1;
565 }
566 return 0;
567}
568
Damien Miller30da3442010-05-10 11:58:03 +1000569static int
Damien Miller86687062014-07-02 15:28:02 +1000570match_principals_option(const char *principal_list, struct sshkey_cert *cert)
Damien Miller30da3442010-05-10 11:58:03 +1000571{
572 char *result;
573 u_int i;
574
575 /* XXX percent_expand() sequences for authorized_principals? */
576
577 for (i = 0; i < cert->nprincipals; i++) {
578 if ((result = match_list(cert->principals[i],
579 principal_list, NULL)) != NULL) {
580 debug3("matched principal from key options \"%.100s\"",
581 result);
Darren Tuckera627d422013-06-02 07:31:17 +1000582 free(result);
Damien Miller30da3442010-05-10 11:58:03 +1000583 return 1;
584 }
585 }
586 return 0;
587}
588
589static int
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000590process_principals(FILE *f, char *file, struct passwd *pw,
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000591 const struct sshkey_cert *cert)
Damien Miller30da3442010-05-10 11:58:03 +1000592{
Damien Miller6018a362010-07-02 13:35:19 +1000593 char line[SSH_MAX_PUBKEY_BYTES], *cp, *ep, *line_opts;
Damien Miller30da3442010-05-10 11:58:03 +1000594 u_long linenum = 0;
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000595 u_int i, found_principal = 0;
Damien Miller30da3442010-05-10 11:58:03 +1000596
Damien Miller30da3442010-05-10 11:58:03 +1000597 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000598 /* Always consume entire input */
599 if (found_principal)
600 continue;
Damien Miller6018a362010-07-02 13:35:19 +1000601 /* Skip leading whitespace. */
Damien Miller30da3442010-05-10 11:58:03 +1000602 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
603 ;
Damien Miller6018a362010-07-02 13:35:19 +1000604 /* Skip blank and comment lines. */
605 if ((ep = strchr(cp, '#')) != NULL)
606 *ep = '\0';
607 if (!*cp || *cp == '\n')
Damien Miller30da3442010-05-10 11:58:03 +1000608 continue;
Damien Miller6018a362010-07-02 13:35:19 +1000609 /* Trim trailing whitespace. */
610 ep = cp + strlen(cp) - 1;
611 while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t'))
612 *ep-- = '\0';
613 /*
614 * If the line has internal whitespace then assume it has
615 * key options.
616 */
617 line_opts = NULL;
618 if ((ep = strrchr(cp, ' ')) != NULL ||
619 (ep = strrchr(cp, '\t')) != NULL) {
620 for (; *ep == ' ' || *ep == '\t'; ep++)
Damien Miller188ea812010-12-01 11:50:14 +1100621 ;
Damien Miller6018a362010-07-02 13:35:19 +1000622 line_opts = cp;
623 cp = ep;
624 }
Damien Miller30da3442010-05-10 11:58:03 +1000625 for (i = 0; i < cert->nprincipals; i++) {
626 if (strcmp(cp, cert->principals[i]) == 0) {
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000627 debug3("%s:%lu: matched principal \"%.100s\"",
628 file == NULL ? "(command)" : file,
629 linenum, cert->principals[i]);
Damien Miller6018a362010-07-02 13:35:19 +1000630 if (auth_parse_options(pw, line_opts,
631 file, linenum) != 1)
632 continue;
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000633 found_principal = 1;
634 continue;
Damien Miller30da3442010-05-10 11:58:03 +1000635 }
636 }
637 }
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000638 return found_principal;
Damien Miller09d3e122012-10-31 08:58:58 +1100639}
Damien Miller30da3442010-05-10 11:58:03 +1000640
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000641static int
642match_principals_file(char *file, struct passwd *pw, struct sshkey_cert *cert)
643{
644 FILE *f;
645 int success;
646
647 temporarily_use_uid(pw);
648 debug("trying authorized principals file %s", file);
649 if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) {
650 restore_uid();
651 return 0;
652 }
653 success = process_principals(f, file, pw, cert);
654 fclose(f);
655 restore_uid();
656 return success;
657}
658
659/*
660 * Checks whether principal is allowed in output of command.
661 * returns 1 if the principal is allowed or 0 otherwise.
662 */
663static int
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000664match_principals_command(struct passwd *user_pw, const struct sshkey *key)
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000665{
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000666 const struct sshkey_cert *cert = key->cert;
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000667 FILE *f = NULL;
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000668 int r, ok, found_principal = 0;
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000669 struct passwd *pw;
670 int i, ac = 0, uid_swapped = 0;
671 pid_t pid;
672 char *tmp, *username = NULL, *command = NULL, **av = NULL;
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000673 char *ca_fp = NULL, *key_fp = NULL, *catext = NULL, *keytext = NULL;
djm@openbsd.orgbfa9d962016-09-21 01:34:45 +0000674 char serial_s[16];
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000675 void (*osigchld)(int);
676
677 if (options.authorized_principals_command == NULL)
678 return 0;
679 if (options.authorized_principals_command_user == NULL) {
680 error("No user for AuthorizedPrincipalsCommand specified, "
681 "skipping");
682 return 0;
683 }
684
685 /*
686 * NB. all returns later this function should go via "out" to
687 * ensure the original SIGCHLD handler is restored properly.
688 */
689 osigchld = signal(SIGCHLD, SIG_DFL);
690
691 /* Prepare and verify the user for the command */
692 username = percent_expand(options.authorized_principals_command_user,
693 "u", user_pw->pw_name, (char *)NULL);
694 pw = getpwnam(username);
695 if (pw == NULL) {
696 error("AuthorizedPrincipalsCommandUser \"%s\" not found: %s",
697 username, strerror(errno));
698 goto out;
699 }
700
701 /* Turn the command into an argument vector */
702 if (split_argv(options.authorized_principals_command, &ac, &av) != 0) {
703 error("AuthorizedPrincipalsCommand \"%s\" contains "
704 "invalid quotes", command);
705 goto out;
706 }
707 if (ac == 0) {
708 error("AuthorizedPrincipalsCommand \"%s\" yielded no arguments",
709 command);
710 goto out;
711 }
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000712 if ((ca_fp = sshkey_fingerprint(cert->signature_key,
713 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
714 error("%s: sshkey_fingerprint failed", __func__);
715 goto out;
716 }
djm@openbsd.org00df97f2016-09-14 20:11:26 +0000717 if ((key_fp = sshkey_fingerprint(key,
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000718 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
719 error("%s: sshkey_fingerprint failed", __func__);
720 goto out;
721 }
722 if ((r = sshkey_to_base64(cert->signature_key, &catext)) != 0) {
723 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r));
724 goto out;
725 }
726 if ((r = sshkey_to_base64(key, &keytext)) != 0) {
727 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r));
728 goto out;
729 }
djm@openbsd.orgf83a0cf2016-09-21 17:44:20 +0000730 snprintf(serial_s, sizeof(serial_s), "%llu",
731 (unsigned long long)cert->serial);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000732 for (i = 1; i < ac; i++) {
733 tmp = percent_expand(av[i],
734 "u", user_pw->pw_name,
735 "h", user_pw->pw_dir,
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000736 "t", sshkey_ssh_name(key),
737 "T", sshkey_ssh_name(cert->signature_key),
738 "f", key_fp,
739 "F", ca_fp,
740 "k", keytext,
741 "K", catext,
djm@openbsd.orgbfa9d962016-09-21 01:34:45 +0000742 "i", cert->key_id,
743 "s", serial_s,
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000744 (char *)NULL);
745 if (tmp == NULL)
746 fatal("%s: percent_expand failed", __func__);
747 free(av[i]);
748 av[i] = tmp;
749 }
750 /* Prepare a printable command for logs, etc. */
751 command = assemble_argv(ac, av);
752
753 if ((pid = subprocess("AuthorizedPrincipalsCommand", pw, command,
754 ac, av, &f)) == 0)
755 goto out;
756
757 uid_swapped = 1;
758 temporarily_use_uid(pw);
759
jsing@openbsd.orgaff3e942015-06-15 18:42:19 +0000760 ok = process_principals(f, NULL, pw, cert);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000761
djm@openbsd.orgddd3d342016-12-30 22:08:02 +0000762 fclose(f);
763 f = NULL;
764
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000765 if (exited_cleanly(pid, "AuthorizedPrincipalsCommand", command) != 0)
766 goto out;
767
768 /* Read completed successfully */
769 found_principal = ok;
770 out:
771 if (f != NULL)
772 fclose(f);
773 signal(SIGCHLD, osigchld);
774 for (i = 0; i < ac; i++)
775 free(av[i]);
776 free(av);
777 if (uid_swapped)
778 restore_uid();
779 free(command);
780 free(username);
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000781 free(ca_fp);
782 free(key_fp);
783 free(catext);
784 free(keytext);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000785 return found_principal;
786}
Damien Miller09d3e122012-10-31 08:58:58 +1100787/*
788 * Checks whether key is allowed in authorized_keys-format file,
789 * returns 1 if the key is allowed or 0 otherwise.
790 */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000791static int
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000792check_authkeys_file(FILE *f, char *file, struct sshkey *key, struct passwd *pw)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000793{
Darren Tucker22cc7412004-12-06 22:47:41 +1100794 char line[SSH_MAX_PUBKEY_BYTES];
Darren Tucker33c787f2008-07-02 22:37:30 +1000795 int found_key = 0;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000796 u_long linenum = 0;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000797 struct sshkey *found = NULL;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000798
Darren Tucker22cc7412004-12-06 22:47:41 +1100799 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
djm@openbsd.orgfd6dcef2016-11-30 02:57:40 +0000800 char *cp, *key_options = NULL, *fp = NULL;
801 const char *reason = NULL;
802
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000803 /* Always consume entrire file */
804 if (found_key)
805 continue;
Darren Tucker74836ae2013-06-02 07:32:00 +1000806 if (found != NULL)
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000807 sshkey_free(found);
808 found = sshkey_new(sshkey_is_cert(key) ? KEY_UNSPEC : key->type);
809 if (found == NULL)
810 goto done;
Damien Miller0a80ca12010-02-27 07:55:05 +1100811 auth_clear_options();
812
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000813 /* Skip leading whitespace, empty and comment lines. */
814 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
815 ;
816 if (!*cp || *cp == '\n' || *cp == '#')
817 continue;
818
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000819 if (sshkey_read(found, &cp) != 0) {
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000820 /* no key? check if there are options for this key */
821 int quoted = 0;
822 debug2("user_key_allowed: check options: '%s'", cp);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000823 key_options = cp;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000824 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
825 if (*cp == '\\' && cp[1] == '"')
826 cp++; /* Skip both */
827 else if (*cp == '"')
828 quoted = !quoted;
829 }
830 /* Skip remaining whitespace. */
831 for (; *cp == ' ' || *cp == '\t'; cp++)
832 ;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000833 if (sshkey_read(found, &cp) != 0) {
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000834 debug2("user_key_allowed: advance: '%s'", cp);
835 /* still no key? advance to next line*/
836 continue;
837 }
838 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000839 if (sshkey_is_cert(key)) {
840 if (!sshkey_equal(found, key->cert->signature_key))
Damien Miller0a80ca12010-02-27 07:55:05 +1100841 continue;
Damien Miller84399552010-05-21 14:58:12 +1000842 if (auth_parse_options(pw, key_options, file,
843 linenum) != 1)
844 continue;
845 if (!key_is_cert_authority)
846 continue;
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000847 if ((fp = sshkey_fingerprint(found,
848 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
849 continue;
Damien Millere513a912010-03-22 05:51:21 +1100850 debug("matching CA found: file %s, line %lu, %s %s",
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000851 file, linenum, sshkey_type(found), fp);
Damien Miller30da3442010-05-10 11:58:03 +1000852 /*
853 * If the user has specified a list of principals as
854 * a key option, then prefer that list to matching
855 * their username in the certificate principals list.
856 */
857 if (authorized_principals != NULL &&
858 !match_principals_option(authorized_principals,
859 key->cert)) {
860 reason = "Certificate does not contain an "
861 "authorized principal";
862 fail_reason:
Darren Tuckera627d422013-06-02 07:31:17 +1000863 free(fp);
Damien Miller0a80ca12010-02-27 07:55:05 +1100864 error("%s", reason);
865 auth_debug_add("%s", reason);
866 continue;
867 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000868 if (sshkey_cert_check_authority(key, 0, 0,
Damien Miller30da3442010-05-10 11:58:03 +1000869 authorized_principals == NULL ? pw->pw_name : NULL,
870 &reason) != 0)
871 goto fail_reason;
djm@openbsd.orgfd6dcef2016-11-30 02:57:40 +0000872 if (auth_cert_options(key, pw, &reason) != 0)
873 goto fail_reason;
djm@openbsd.org63d18812015-10-27 01:44:45 +0000874 verbose("Accepted certificate ID \"%s\" (serial %llu) "
Damien Millere513a912010-03-22 05:51:21 +1100875 "signed by %s CA %s via %s", key->cert->key_id,
djm@openbsd.org63d18812015-10-27 01:44:45 +0000876 (unsigned long long)key->cert->serial,
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000877 sshkey_type(found), fp, file);
Darren Tuckera627d422013-06-02 07:31:17 +1000878 free(fp);
Damien Miller0a80ca12010-02-27 07:55:05 +1100879 found_key = 1;
880 break;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000881 } else if (sshkey_equal(found, key)) {
Damien Miller84399552010-05-21 14:58:12 +1000882 if (auth_parse_options(pw, key_options, file,
883 linenum) != 1)
884 continue;
885 if (key_is_cert_authority)
886 continue;
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000887 if ((fp = sshkey_fingerprint(found,
888 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
889 continue;
Darren Tucker74836ae2013-06-02 07:32:00 +1000890 debug("matching key found: file %s, line %lu %s %s",
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000891 file, linenum, sshkey_type(found), fp);
Darren Tuckera627d422013-06-02 07:31:17 +1000892 free(fp);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000893 found_key = 1;
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000894 continue;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000895 }
896 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000897 done:
Darren Tucker74836ae2013-06-02 07:32:00 +1000898 if (found != NULL)
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000899 sshkey_free(found);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000900 if (!found_key)
901 debug2("key not found");
902 return found_key;
903}
904
Damien Miller1aed65e2010-03-04 21:53:35 +1100905/* Authenticate a certificate key against TrustedUserCAKeys */
906static int
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000907user_cert_trusted_ca(struct passwd *pw, struct sshkey *key)
Damien Miller1aed65e2010-03-04 21:53:35 +1100908{
Damien Miller30da3442010-05-10 11:58:03 +1000909 char *ca_fp, *principals_file = NULL;
Damien Miller1aed65e2010-03-04 21:53:35 +1100910 const char *reason;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000911 int r, ret = 0, found_principal = 0, use_authorized_principals;
Damien Miller1aed65e2010-03-04 21:53:35 +1100912
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000913 if (!sshkey_is_cert(key) || options.trusted_user_ca_keys == NULL)
Damien Miller1aed65e2010-03-04 21:53:35 +1100914 return 0;
915
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000916 if ((ca_fp = sshkey_fingerprint(key->cert->signature_key,
917 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
918 return 0;
Damien Miller1aed65e2010-03-04 21:53:35 +1100919
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000920 if ((r = sshkey_in_file(key->cert->signature_key,
921 options.trusted_user_ca_keys, 1, 0)) != 0) {
922 debug2("%s: CA %s %s is not listed in %s: %s", __func__,
923 sshkey_type(key->cert->signature_key), ca_fp,
924 options.trusted_user_ca_keys, ssh_err(r));
Damien Miller1aed65e2010-03-04 21:53:35 +1100925 goto out;
926 }
Damien Miller30da3442010-05-10 11:58:03 +1000927 /*
928 * If AuthorizedPrincipals is in use, then compare the certificate
929 * principals against the names in that file rather than matching
930 * against the username.
931 */
932 if ((principals_file = authorized_principals_file(pw)) != NULL) {
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000933 if (match_principals_file(principals_file, pw, key->cert))
934 found_principal = 1;
935 }
936 /* Try querying command if specified */
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000937 if (!found_principal && match_principals_command(pw, key))
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000938 found_principal = 1;
jsing@openbsd.org596dbca2015-06-15 18:44:22 +0000939 /* If principals file or command is specified, then require a match */
940 use_authorized_principals = principals_file != NULL ||
941 options.authorized_principals_command != NULL;
942 if (!found_principal && use_authorized_principals) {
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000943 reason = "Certificate does not contain an authorized principal";
Damien Miller30da3442010-05-10 11:58:03 +1000944 fail_reason:
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000945 error("%s", reason);
946 auth_debug_add("%s", reason);
947 goto out;
Damien Miller1aed65e2010-03-04 21:53:35 +1100948 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000949 if (sshkey_cert_check_authority(key, 0, 1,
jsing@openbsd.org596dbca2015-06-15 18:44:22 +0000950 use_authorized_principals ? NULL : pw->pw_name, &reason) != 0)
Damien Miller30da3442010-05-10 11:58:03 +1000951 goto fail_reason;
djm@openbsd.orgfd6dcef2016-11-30 02:57:40 +0000952 if (auth_cert_options(key, pw, &reason) != 0)
953 goto fail_reason;
Damien Miller1aed65e2010-03-04 21:53:35 +1100954
djm@openbsd.org63d18812015-10-27 01:44:45 +0000955 verbose("Accepted certificate ID \"%s\" (serial %llu) signed by "
956 "%s CA %s via %s", key->cert->key_id,
957 (unsigned long long)key->cert->serial,
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000958 sshkey_type(key->cert->signature_key), ca_fp,
Damien Millere513a912010-03-22 05:51:21 +1100959 options.trusted_user_ca_keys);
Damien Miller1aed65e2010-03-04 21:53:35 +1100960 ret = 1;
961
962 out:
Darren Tuckera627d422013-06-02 07:31:17 +1000963 free(principals_file);
964 free(ca_fp);
Damien Miller1aed65e2010-03-04 21:53:35 +1100965 return ret;
966}
967
Damien Miller09d3e122012-10-31 08:58:58 +1100968/*
969 * Checks whether key is allowed in file.
970 * returns 1 if the key is allowed or 0 otherwise.
971 */
972static int
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000973user_key_allowed2(struct passwd *pw, struct sshkey *key, char *file)
Damien Miller09d3e122012-10-31 08:58:58 +1100974{
975 FILE *f;
976 int found_key = 0;
977
978 /* Temporarily use the user's uid. */
979 temporarily_use_uid(pw);
980
981 debug("trying public key file %s", file);
982 if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) {
983 found_key = check_authkeys_file(f, file, key, pw);
984 fclose(f);
985 }
986
987 restore_uid();
988 return found_key;
989}
990
991/*
992 * Checks whether key is allowed in output of command.
993 * returns 1 if the key is allowed or 0 otherwise.
994 */
995static int
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000996user_key_command_allowed2(struct passwd *user_pw, struct sshkey *key)
Damien Miller09d3e122012-10-31 08:58:58 +1100997{
djm@openbsd.org24232a32015-05-21 06:38:35 +0000998 FILE *f = NULL;
999 int r, ok, found_key = 0;
Damien Miller09d3e122012-10-31 08:58:58 +11001000 struct passwd *pw;
djm@openbsd.org24232a32015-05-21 06:38:35 +00001001 int i, uid_swapped = 0, ac = 0;
Damien Miller09d3e122012-10-31 08:58:58 +11001002 pid_t pid;
djm@openbsd.org24232a32015-05-21 06:38:35 +00001003 char *username = NULL, *key_fp = NULL, *keytext = NULL;
1004 char *tmp, *command = NULL, **av = NULL;
1005 void (*osigchld)(int);
Damien Miller09d3e122012-10-31 08:58:58 +11001006
djm@openbsd.org24232a32015-05-21 06:38:35 +00001007 if (options.authorized_keys_command == NULL)
Damien Miller09d3e122012-10-31 08:58:58 +11001008 return 0;
Damien Millerd0d10992012-11-04 22:23:14 +11001009 if (options.authorized_keys_command_user == NULL) {
1010 error("No user for AuthorizedKeysCommand specified, skipping");
1011 return 0;
Damien Miller09d3e122012-10-31 08:58:58 +11001012 }
1013
djm@openbsd.org24232a32015-05-21 06:38:35 +00001014 /*
1015 * NB. all returns later this function should go via "out" to
1016 * ensure the original SIGCHLD handler is restored properly.
1017 */
1018 osigchld = signal(SIGCHLD, SIG_DFL);
1019
1020 /* Prepare and verify the user for the command */
Damien Millerd0d10992012-11-04 22:23:14 +11001021 username = percent_expand(options.authorized_keys_command_user,
1022 "u", user_pw->pw_name, (char *)NULL);
1023 pw = getpwnam(username);
1024 if (pw == NULL) {
Damien Miller4018dc02013-02-15 10:28:55 +11001025 error("AuthorizedKeysCommandUser \"%s\" not found: %s",
1026 username, strerror(errno));
Damien Miller09d3e122012-10-31 08:58:58 +11001027 goto out;
1028 }
1029
djm@openbsd.org24232a32015-05-21 06:38:35 +00001030 /* Prepare AuthorizedKeysCommand */
1031 if ((key_fp = sshkey_fingerprint(key, options.fingerprint_hash,
1032 SSH_FP_DEFAULT)) == NULL) {
1033 error("%s: sshkey_fingerprint failed", __func__);
1034 goto out;
1035 }
1036 if ((r = sshkey_to_base64(key, &keytext)) != 0) {
1037 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r));
Damien Miller09d3e122012-10-31 08:58:58 +11001038 goto out;
1039 }
1040
djm@openbsd.org24232a32015-05-21 06:38:35 +00001041 /* Turn the command into an argument vector */
1042 if (split_argv(options.authorized_keys_command, &ac, &av) != 0) {
1043 error("AuthorizedKeysCommand \"%s\" contains invalid quotes",
1044 command);
1045 goto out;
1046 }
1047 if (ac == 0) {
1048 error("AuthorizedKeysCommand \"%s\" yielded no arguments",
1049 command);
1050 goto out;
1051 }
1052 for (i = 1; i < ac; i++) {
1053 tmp = percent_expand(av[i],
1054 "u", user_pw->pw_name,
1055 "h", user_pw->pw_dir,
1056 "t", sshkey_ssh_name(key),
1057 "f", key_fp,
1058 "k", keytext,
1059 (char *)NULL);
1060 if (tmp == NULL)
1061 fatal("%s: percent_expand failed", __func__);
1062 free(av[i]);
1063 av[i] = tmp;
1064 }
1065 /* Prepare a printable command for logs, etc. */
1066 command = assemble_argv(ac, av);
Damien Miller09d3e122012-10-31 08:58:58 +11001067
1068 /*
djm@openbsd.org24232a32015-05-21 06:38:35 +00001069 * If AuthorizedKeysCommand was run without arguments
1070 * then fall back to the old behaviour of passing the
1071 * target username as a single argument.
Damien Miller09d3e122012-10-31 08:58:58 +11001072 */
djm@openbsd.org24232a32015-05-21 06:38:35 +00001073 if (ac == 1) {
1074 av = xreallocarray(av, ac + 2, sizeof(*av));
1075 av[1] = xstrdup(user_pw->pw_name);
1076 av[2] = NULL;
1077 /* Fix up command too, since it is used in log messages */
1078 free(command);
1079 xasprintf(&command, "%s %s", av[0], av[1]);
Damien Miller09d3e122012-10-31 08:58:58 +11001080 }
1081
djm@openbsd.org24232a32015-05-21 06:38:35 +00001082 if ((pid = subprocess("AuthorizedKeysCommand", pw, command,
1083 ac, av, &f)) == 0)
1084 goto out;
1085
1086 uid_swapped = 1;
Damien Miller09d3e122012-10-31 08:58:58 +11001087 temporarily_use_uid(pw);
1088
Damien Miller09d3e122012-10-31 08:58:58 +11001089 ok = check_authkeys_file(f, options.authorized_keys_command, key, pw);
Damien Miller09d3e122012-10-31 08:58:58 +11001090
djm@openbsd.orgddd3d342016-12-30 22:08:02 +00001091 fclose(f);
1092 f = NULL;
1093
djm@openbsd.org24232a32015-05-21 06:38:35 +00001094 if (exited_cleanly(pid, "AuthorizedKeysCommand", command) != 0)
Damien Miller09d3e122012-10-31 08:58:58 +11001095 goto out;
djm@openbsd.org24232a32015-05-21 06:38:35 +00001096
1097 /* Read completed successfully */
Damien Miller09d3e122012-10-31 08:58:58 +11001098 found_key = ok;
1099 out:
djm@openbsd.org24232a32015-05-21 06:38:35 +00001100 if (f != NULL)
1101 fclose(f);
1102 signal(SIGCHLD, osigchld);
1103 for (i = 0; i < ac; i++)
1104 free(av[i]);
1105 free(av);
1106 if (uid_swapped)
1107 restore_uid();
1108 free(command);
1109 free(username);
1110 free(key_fp);
1111 free(keytext);
Damien Miller09d3e122012-10-31 08:58:58 +11001112 return found_key;
1113}
1114
1115/*
1116 * Check whether key authenticates and authorises the user.
1117 */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001118int
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001119user_key_allowed(struct passwd *pw, struct sshkey *key, int auth_attempt)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001120{
Damien Millerd8478b62011-05-29 21:39:36 +10001121 u_int success, i;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001122 char *file;
1123
Damien Miller1aed65e2010-03-04 21:53:35 +11001124 if (auth_key_is_revoked(key))
1125 return 0;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001126 if (sshkey_is_cert(key) &&
1127 auth_key_is_revoked(key->cert->signature_key))
Damien Miller1aed65e2010-03-04 21:53:35 +11001128 return 0;
1129
1130 success = user_cert_trusted_ca(pw, key);
1131 if (success)
1132 return success;
1133
Damien Miller09d3e122012-10-31 08:58:58 +11001134 success = user_key_command_allowed2(pw, key);
1135 if (success > 0)
1136 return success;
1137
Damien Millerd8478b62011-05-29 21:39:36 +10001138 for (i = 0; !success && i < options.num_authkeys_files; i++) {
Damien Miller09d3e122012-10-31 08:58:58 +11001139
1140 if (strcasecmp(options.authorized_keys_files[i], "none") == 0)
1141 continue;
Damien Millerd8478b62011-05-29 21:39:36 +10001142 file = expand_authorized_keys(
1143 options.authorized_keys_files[i], pw);
Damien Miller09d3e122012-10-31 08:58:58 +11001144
Damien Millerd8478b62011-05-29 21:39:36 +10001145 success = user_key_allowed2(pw, key, file);
Darren Tuckera627d422013-06-02 07:31:17 +10001146 free(file);
Damien Millerd8478b62011-05-29 21:39:36 +10001147 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001148
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001149 return success;
1150}
1151
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001152/* Records a public key in the list of previously-successful keys */
1153void
1154auth2_record_userkey(Authctxt *authctxt, struct sshkey *key)
1155{
1156 struct sshkey **tmp;
1157
1158 if (authctxt->nprev_userkeys >= INT_MAX ||
1159 (tmp = reallocarray(authctxt->prev_userkeys,
1160 authctxt->nprev_userkeys + 1, sizeof(*tmp))) == NULL)
1161 fatal("%s: reallocarray failed", __func__);
1162 authctxt->prev_userkeys = tmp;
1163 authctxt->prev_userkeys[authctxt->nprev_userkeys] = key;
1164 authctxt->nprev_userkeys++;
1165}
1166
1167/* Checks whether a key has already been used successfully for authentication */
1168int
1169auth2_userkey_already_used(Authctxt *authctxt, struct sshkey *key)
1170{
1171 u_int i;
1172
1173 for (i = 0; i < authctxt->nprev_userkeys; i++) {
1174 if (sshkey_equal_public(key, authctxt->prev_userkeys[i])) {
1175 return 1;
1176 }
1177 }
1178 return 0;
1179}
1180
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001181Authmethod method_pubkey = {
1182 "publickey",
1183 userauth_pubkey,
1184 &options.pubkey_authentication
1185};