blob: 5794f1f4d76cf3403f0a34126de6a5b2d77708ac [file] [log] [blame]
markus@openbsd.org75b8af82017-05-31 10:54:00 +00001/* $OpenBSD: auth2-pubkey.c,v 1.67 2017/05/31 10:54:00 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
markus@openbsd.orgeb272ea2017-05-30 14:29:59 +000078userauth_pubkey(struct ssh *ssh)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000079{
markus@openbsd.orgeb272ea2017-05-30 14:29:59 +000080 Authctxt *authctxt = ssh->authctxt;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +000081 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
markus@openbsd.org75b8af82017-05-31 10:54:00 +0000590process_principals(FILE *f, const 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\"",
markus@openbsd.org75b8af82017-05-31 10:54:00 +0000628 file, linenum, cert->principals[i]);
Damien Miller6018a362010-07-02 13:35:19 +1000629 if (auth_parse_options(pw, line_opts,
630 file, linenum) != 1)
631 continue;
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000632 found_principal = 1;
633 continue;
Damien Miller30da3442010-05-10 11:58:03 +1000634 }
635 }
636 }
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000637 return found_principal;
Damien Miller09d3e122012-10-31 08:58:58 +1100638}
Damien Miller30da3442010-05-10 11:58:03 +1000639
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000640static int
641match_principals_file(char *file, struct passwd *pw, struct sshkey_cert *cert)
642{
643 FILE *f;
644 int success;
645
646 temporarily_use_uid(pw);
647 debug("trying authorized principals file %s", file);
648 if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) {
649 restore_uid();
650 return 0;
651 }
652 success = process_principals(f, file, pw, cert);
653 fclose(f);
654 restore_uid();
655 return success;
656}
657
658/*
659 * Checks whether principal is allowed in output of command.
660 * returns 1 if the principal is allowed or 0 otherwise.
661 */
662static int
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000663match_principals_command(struct passwd *user_pw, const struct sshkey *key)
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000664{
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000665 const struct sshkey_cert *cert = key->cert;
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000666 FILE *f = NULL;
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000667 int r, ok, found_principal = 0;
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000668 struct passwd *pw;
669 int i, ac = 0, uid_swapped = 0;
670 pid_t pid;
671 char *tmp, *username = NULL, *command = NULL, **av = NULL;
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000672 char *ca_fp = NULL, *key_fp = NULL, *catext = NULL, *keytext = NULL;
djm@openbsd.orgbfa9d962016-09-21 01:34:45 +0000673 char serial_s[16];
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000674 void (*osigchld)(int);
675
676 if (options.authorized_principals_command == NULL)
677 return 0;
678 if (options.authorized_principals_command_user == NULL) {
679 error("No user for AuthorizedPrincipalsCommand specified, "
680 "skipping");
681 return 0;
682 }
683
684 /*
685 * NB. all returns later this function should go via "out" to
686 * ensure the original SIGCHLD handler is restored properly.
687 */
688 osigchld = signal(SIGCHLD, SIG_DFL);
689
690 /* Prepare and verify the user for the command */
691 username = percent_expand(options.authorized_principals_command_user,
692 "u", user_pw->pw_name, (char *)NULL);
693 pw = getpwnam(username);
694 if (pw == NULL) {
695 error("AuthorizedPrincipalsCommandUser \"%s\" not found: %s",
696 username, strerror(errno));
697 goto out;
698 }
699
700 /* Turn the command into an argument vector */
701 if (split_argv(options.authorized_principals_command, &ac, &av) != 0) {
702 error("AuthorizedPrincipalsCommand \"%s\" contains "
703 "invalid quotes", command);
704 goto out;
705 }
706 if (ac == 0) {
707 error("AuthorizedPrincipalsCommand \"%s\" yielded no arguments",
708 command);
709 goto out;
710 }
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000711 if ((ca_fp = sshkey_fingerprint(cert->signature_key,
712 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
713 error("%s: sshkey_fingerprint failed", __func__);
714 goto out;
715 }
djm@openbsd.org00df97f2016-09-14 20:11:26 +0000716 if ((key_fp = sshkey_fingerprint(key,
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000717 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
718 error("%s: sshkey_fingerprint failed", __func__);
719 goto out;
720 }
721 if ((r = sshkey_to_base64(cert->signature_key, &catext)) != 0) {
722 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r));
723 goto out;
724 }
725 if ((r = sshkey_to_base64(key, &keytext)) != 0) {
726 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r));
727 goto out;
728 }
djm@openbsd.orgf83a0cf2016-09-21 17:44:20 +0000729 snprintf(serial_s, sizeof(serial_s), "%llu",
730 (unsigned long long)cert->serial);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000731 for (i = 1; i < ac; i++) {
732 tmp = percent_expand(av[i],
733 "u", user_pw->pw_name,
734 "h", user_pw->pw_dir,
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000735 "t", sshkey_ssh_name(key),
736 "T", sshkey_ssh_name(cert->signature_key),
737 "f", key_fp,
738 "F", ca_fp,
739 "k", keytext,
740 "K", catext,
djm@openbsd.orgbfa9d962016-09-21 01:34:45 +0000741 "i", cert->key_id,
742 "s", serial_s,
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000743 (char *)NULL);
744 if (tmp == NULL)
745 fatal("%s: percent_expand failed", __func__);
746 free(av[i]);
747 av[i] = tmp;
748 }
749 /* Prepare a printable command for logs, etc. */
750 command = assemble_argv(ac, av);
751
752 if ((pid = subprocess("AuthorizedPrincipalsCommand", pw, command,
753 ac, av, &f)) == 0)
754 goto out;
755
756 uid_swapped = 1;
757 temporarily_use_uid(pw);
758
markus@openbsd.org75b8af82017-05-31 10:54:00 +0000759 ok = process_principals(f, "(command)", pw, cert);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000760
djm@openbsd.orgddd3d342016-12-30 22:08:02 +0000761 fclose(f);
762 f = NULL;
763
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000764 if (exited_cleanly(pid, "AuthorizedPrincipalsCommand", command) != 0)
765 goto out;
766
767 /* Read completed successfully */
768 found_principal = ok;
769 out:
770 if (f != NULL)
771 fclose(f);
772 signal(SIGCHLD, osigchld);
773 for (i = 0; i < ac; i++)
774 free(av[i]);
775 free(av);
776 if (uid_swapped)
777 restore_uid();
778 free(command);
779 free(username);
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000780 free(ca_fp);
781 free(key_fp);
782 free(catext);
783 free(keytext);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000784 return found_principal;
785}
Damien Miller09d3e122012-10-31 08:58:58 +1100786/*
787 * Checks whether key is allowed in authorized_keys-format file,
788 * returns 1 if the key is allowed or 0 otherwise.
789 */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000790static int
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000791check_authkeys_file(FILE *f, char *file, struct sshkey *key, struct passwd *pw)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000792{
Darren Tucker22cc7412004-12-06 22:47:41 +1100793 char line[SSH_MAX_PUBKEY_BYTES];
Darren Tucker33c787f2008-07-02 22:37:30 +1000794 int found_key = 0;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000795 u_long linenum = 0;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000796 struct sshkey *found = NULL;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000797
Darren Tucker22cc7412004-12-06 22:47:41 +1100798 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
djm@openbsd.orgfd6dcef2016-11-30 02:57:40 +0000799 char *cp, *key_options = NULL, *fp = NULL;
800 const char *reason = NULL;
801
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000802 /* Always consume entrire file */
803 if (found_key)
804 continue;
Darren Tucker74836ae2013-06-02 07:32:00 +1000805 if (found != NULL)
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000806 sshkey_free(found);
807 found = sshkey_new(sshkey_is_cert(key) ? KEY_UNSPEC : key->type);
808 if (found == NULL)
809 goto done;
Damien Miller0a80ca12010-02-27 07:55:05 +1100810 auth_clear_options();
811
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000812 /* Skip leading whitespace, empty and comment lines. */
813 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
814 ;
815 if (!*cp || *cp == '\n' || *cp == '#')
816 continue;
817
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000818 if (sshkey_read(found, &cp) != 0) {
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000819 /* no key? check if there are options for this key */
820 int quoted = 0;
821 debug2("user_key_allowed: check options: '%s'", cp);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000822 key_options = cp;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000823 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
824 if (*cp == '\\' && cp[1] == '"')
825 cp++; /* Skip both */
826 else if (*cp == '"')
827 quoted = !quoted;
828 }
829 /* Skip remaining whitespace. */
830 for (; *cp == ' ' || *cp == '\t'; cp++)
831 ;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000832 if (sshkey_read(found, &cp) != 0) {
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000833 debug2("user_key_allowed: advance: '%s'", cp);
834 /* still no key? advance to next line*/
835 continue;
836 }
837 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000838 if (sshkey_is_cert(key)) {
839 if (!sshkey_equal(found, key->cert->signature_key))
Damien Miller0a80ca12010-02-27 07:55:05 +1100840 continue;
Damien Miller84399552010-05-21 14:58:12 +1000841 if (auth_parse_options(pw, key_options, file,
842 linenum) != 1)
843 continue;
844 if (!key_is_cert_authority)
845 continue;
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000846 if ((fp = sshkey_fingerprint(found,
847 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
848 continue;
Damien Millere513a912010-03-22 05:51:21 +1100849 debug("matching CA found: file %s, line %lu, %s %s",
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000850 file, linenum, sshkey_type(found), fp);
Damien Miller30da3442010-05-10 11:58:03 +1000851 /*
852 * If the user has specified a list of principals as
853 * a key option, then prefer that list to matching
854 * their username in the certificate principals list.
855 */
856 if (authorized_principals != NULL &&
857 !match_principals_option(authorized_principals,
858 key->cert)) {
859 reason = "Certificate does not contain an "
860 "authorized principal";
861 fail_reason:
Darren Tuckera627d422013-06-02 07:31:17 +1000862 free(fp);
Damien Miller0a80ca12010-02-27 07:55:05 +1100863 error("%s", reason);
864 auth_debug_add("%s", reason);
865 continue;
866 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000867 if (sshkey_cert_check_authority(key, 0, 0,
Damien Miller30da3442010-05-10 11:58:03 +1000868 authorized_principals == NULL ? pw->pw_name : NULL,
869 &reason) != 0)
870 goto fail_reason;
djm@openbsd.orgfd6dcef2016-11-30 02:57:40 +0000871 if (auth_cert_options(key, pw, &reason) != 0)
872 goto fail_reason;
djm@openbsd.org63d18812015-10-27 01:44:45 +0000873 verbose("Accepted certificate ID \"%s\" (serial %llu) "
Damien Millere513a912010-03-22 05:51:21 +1100874 "signed by %s CA %s via %s", key->cert->key_id,
djm@openbsd.org63d18812015-10-27 01:44:45 +0000875 (unsigned long long)key->cert->serial,
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000876 sshkey_type(found), fp, file);
Darren Tuckera627d422013-06-02 07:31:17 +1000877 free(fp);
Damien Miller0a80ca12010-02-27 07:55:05 +1100878 found_key = 1;
879 break;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000880 } else if (sshkey_equal(found, key)) {
Damien Miller84399552010-05-21 14:58:12 +1000881 if (auth_parse_options(pw, key_options, file,
882 linenum) != 1)
883 continue;
884 if (key_is_cert_authority)
885 continue;
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000886 if ((fp = sshkey_fingerprint(found,
887 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
888 continue;
Darren Tucker74836ae2013-06-02 07:32:00 +1000889 debug("matching key found: file %s, line %lu %s %s",
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000890 file, linenum, sshkey_type(found), fp);
Darren Tuckera627d422013-06-02 07:31:17 +1000891 free(fp);
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000892 found_key = 1;
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000893 continue;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000894 }
895 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000896 done:
Darren Tucker74836ae2013-06-02 07:32:00 +1000897 if (found != NULL)
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000898 sshkey_free(found);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000899 if (!found_key)
900 debug2("key not found");
901 return found_key;
902}
903
Damien Miller1aed65e2010-03-04 21:53:35 +1100904/* Authenticate a certificate key against TrustedUserCAKeys */
905static int
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000906user_cert_trusted_ca(struct passwd *pw, struct sshkey *key)
Damien Miller1aed65e2010-03-04 21:53:35 +1100907{
Damien Miller30da3442010-05-10 11:58:03 +1000908 char *ca_fp, *principals_file = NULL;
Damien Miller1aed65e2010-03-04 21:53:35 +1100909 const char *reason;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000910 int r, ret = 0, found_principal = 0, use_authorized_principals;
Damien Miller1aed65e2010-03-04 21:53:35 +1100911
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000912 if (!sshkey_is_cert(key) || options.trusted_user_ca_keys == NULL)
Damien Miller1aed65e2010-03-04 21:53:35 +1100913 return 0;
914
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000915 if ((ca_fp = sshkey_fingerprint(key->cert->signature_key,
916 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
917 return 0;
Damien Miller1aed65e2010-03-04 21:53:35 +1100918
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000919 if ((r = sshkey_in_file(key->cert->signature_key,
920 options.trusted_user_ca_keys, 1, 0)) != 0) {
921 debug2("%s: CA %s %s is not listed in %s: %s", __func__,
922 sshkey_type(key->cert->signature_key), ca_fp,
923 options.trusted_user_ca_keys, ssh_err(r));
Damien Miller1aed65e2010-03-04 21:53:35 +1100924 goto out;
925 }
Damien Miller30da3442010-05-10 11:58:03 +1000926 /*
927 * If AuthorizedPrincipals is in use, then compare the certificate
928 * principals against the names in that file rather than matching
929 * against the username.
930 */
931 if ((principals_file = authorized_principals_file(pw)) != NULL) {
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000932 if (match_principals_file(principals_file, pw, key->cert))
933 found_principal = 1;
934 }
935 /* Try querying command if specified */
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000936 if (!found_principal && match_principals_command(pw, key))
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000937 found_principal = 1;
jsing@openbsd.org596dbca2015-06-15 18:44:22 +0000938 /* If principals file or command is specified, then require a match */
939 use_authorized_principals = principals_file != NULL ||
940 options.authorized_principals_command != NULL;
941 if (!found_principal && use_authorized_principals) {
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000942 reason = "Certificate does not contain an authorized principal";
Damien Miller30da3442010-05-10 11:58:03 +1000943 fail_reason:
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000944 error("%s", reason);
945 auth_debug_add("%s", reason);
946 goto out;
Damien Miller1aed65e2010-03-04 21:53:35 +1100947 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000948 if (sshkey_cert_check_authority(key, 0, 1,
jsing@openbsd.org596dbca2015-06-15 18:44:22 +0000949 use_authorized_principals ? NULL : pw->pw_name, &reason) != 0)
Damien Miller30da3442010-05-10 11:58:03 +1000950 goto fail_reason;
djm@openbsd.orgfd6dcef2016-11-30 02:57:40 +0000951 if (auth_cert_options(key, pw, &reason) != 0)
952 goto fail_reason;
Damien Miller1aed65e2010-03-04 21:53:35 +1100953
djm@openbsd.org63d18812015-10-27 01:44:45 +0000954 verbose("Accepted certificate ID \"%s\" (serial %llu) signed by "
955 "%s CA %s via %s", key->cert->key_id,
956 (unsigned long long)key->cert->serial,
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000957 sshkey_type(key->cert->signature_key), ca_fp,
Damien Millere513a912010-03-22 05:51:21 +1100958 options.trusted_user_ca_keys);
Damien Miller1aed65e2010-03-04 21:53:35 +1100959 ret = 1;
960
961 out:
Darren Tuckera627d422013-06-02 07:31:17 +1000962 free(principals_file);
963 free(ca_fp);
Damien Miller1aed65e2010-03-04 21:53:35 +1100964 return ret;
965}
966
Damien Miller09d3e122012-10-31 08:58:58 +1100967/*
968 * Checks whether key is allowed in file.
969 * returns 1 if the key is allowed or 0 otherwise.
970 */
971static int
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000972user_key_allowed2(struct passwd *pw, struct sshkey *key, char *file)
Damien Miller09d3e122012-10-31 08:58:58 +1100973{
974 FILE *f;
975 int found_key = 0;
976
977 /* Temporarily use the user's uid. */
978 temporarily_use_uid(pw);
979
980 debug("trying public key file %s", file);
981 if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) {
982 found_key = check_authkeys_file(f, file, key, pw);
983 fclose(f);
984 }
985
986 restore_uid();
987 return found_key;
988}
989
990/*
991 * Checks whether key is allowed in output of command.
992 * returns 1 if the key is allowed or 0 otherwise.
993 */
994static int
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000995user_key_command_allowed2(struct passwd *user_pw, struct sshkey *key)
Damien Miller09d3e122012-10-31 08:58:58 +1100996{
djm@openbsd.org24232a32015-05-21 06:38:35 +0000997 FILE *f = NULL;
998 int r, ok, found_key = 0;
Damien Miller09d3e122012-10-31 08:58:58 +1100999 struct passwd *pw;
djm@openbsd.org24232a32015-05-21 06:38:35 +00001000 int i, uid_swapped = 0, ac = 0;
Damien Miller09d3e122012-10-31 08:58:58 +11001001 pid_t pid;
djm@openbsd.org24232a32015-05-21 06:38:35 +00001002 char *username = NULL, *key_fp = NULL, *keytext = NULL;
1003 char *tmp, *command = NULL, **av = NULL;
1004 void (*osigchld)(int);
Damien Miller09d3e122012-10-31 08:58:58 +11001005
djm@openbsd.org24232a32015-05-21 06:38:35 +00001006 if (options.authorized_keys_command == NULL)
Damien Miller09d3e122012-10-31 08:58:58 +11001007 return 0;
Damien Millerd0d10992012-11-04 22:23:14 +11001008 if (options.authorized_keys_command_user == NULL) {
1009 error("No user for AuthorizedKeysCommand specified, skipping");
1010 return 0;
Damien Miller09d3e122012-10-31 08:58:58 +11001011 }
1012
djm@openbsd.org24232a32015-05-21 06:38:35 +00001013 /*
1014 * NB. all returns later this function should go via "out" to
1015 * ensure the original SIGCHLD handler is restored properly.
1016 */
1017 osigchld = signal(SIGCHLD, SIG_DFL);
1018
1019 /* Prepare and verify the user for the command */
Damien Millerd0d10992012-11-04 22:23:14 +11001020 username = percent_expand(options.authorized_keys_command_user,
1021 "u", user_pw->pw_name, (char *)NULL);
1022 pw = getpwnam(username);
1023 if (pw == NULL) {
Damien Miller4018dc02013-02-15 10:28:55 +11001024 error("AuthorizedKeysCommandUser \"%s\" not found: %s",
1025 username, strerror(errno));
Damien Miller09d3e122012-10-31 08:58:58 +11001026 goto out;
1027 }
1028
djm@openbsd.org24232a32015-05-21 06:38:35 +00001029 /* Prepare AuthorizedKeysCommand */
1030 if ((key_fp = sshkey_fingerprint(key, options.fingerprint_hash,
1031 SSH_FP_DEFAULT)) == NULL) {
1032 error("%s: sshkey_fingerprint failed", __func__);
1033 goto out;
1034 }
1035 if ((r = sshkey_to_base64(key, &keytext)) != 0) {
1036 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r));
Damien Miller09d3e122012-10-31 08:58:58 +11001037 goto out;
1038 }
1039
djm@openbsd.org24232a32015-05-21 06:38:35 +00001040 /* Turn the command into an argument vector */
1041 if (split_argv(options.authorized_keys_command, &ac, &av) != 0) {
1042 error("AuthorizedKeysCommand \"%s\" contains invalid quotes",
1043 command);
1044 goto out;
1045 }
1046 if (ac == 0) {
1047 error("AuthorizedKeysCommand \"%s\" yielded no arguments",
1048 command);
1049 goto out;
1050 }
1051 for (i = 1; i < ac; i++) {
1052 tmp = percent_expand(av[i],
1053 "u", user_pw->pw_name,
1054 "h", user_pw->pw_dir,
1055 "t", sshkey_ssh_name(key),
1056 "f", key_fp,
1057 "k", keytext,
1058 (char *)NULL);
1059 if (tmp == NULL)
1060 fatal("%s: percent_expand failed", __func__);
1061 free(av[i]);
1062 av[i] = tmp;
1063 }
1064 /* Prepare a printable command for logs, etc. */
1065 command = assemble_argv(ac, av);
Damien Miller09d3e122012-10-31 08:58:58 +11001066
1067 /*
djm@openbsd.org24232a32015-05-21 06:38:35 +00001068 * If AuthorizedKeysCommand was run without arguments
1069 * then fall back to the old behaviour of passing the
1070 * target username as a single argument.
Damien Miller09d3e122012-10-31 08:58:58 +11001071 */
djm@openbsd.org24232a32015-05-21 06:38:35 +00001072 if (ac == 1) {
1073 av = xreallocarray(av, ac + 2, sizeof(*av));
1074 av[1] = xstrdup(user_pw->pw_name);
1075 av[2] = NULL;
1076 /* Fix up command too, since it is used in log messages */
1077 free(command);
1078 xasprintf(&command, "%s %s", av[0], av[1]);
Damien Miller09d3e122012-10-31 08:58:58 +11001079 }
1080
djm@openbsd.org24232a32015-05-21 06:38:35 +00001081 if ((pid = subprocess("AuthorizedKeysCommand", pw, command,
1082 ac, av, &f)) == 0)
1083 goto out;
1084
1085 uid_swapped = 1;
Damien Miller09d3e122012-10-31 08:58:58 +11001086 temporarily_use_uid(pw);
1087
Damien Miller09d3e122012-10-31 08:58:58 +11001088 ok = check_authkeys_file(f, options.authorized_keys_command, key, pw);
Damien Miller09d3e122012-10-31 08:58:58 +11001089
djm@openbsd.orgddd3d342016-12-30 22:08:02 +00001090 fclose(f);
1091 f = NULL;
1092
djm@openbsd.org24232a32015-05-21 06:38:35 +00001093 if (exited_cleanly(pid, "AuthorizedKeysCommand", command) != 0)
Damien Miller09d3e122012-10-31 08:58:58 +11001094 goto out;
djm@openbsd.org24232a32015-05-21 06:38:35 +00001095
1096 /* Read completed successfully */
Damien Miller09d3e122012-10-31 08:58:58 +11001097 found_key = ok;
1098 out:
djm@openbsd.org24232a32015-05-21 06:38:35 +00001099 if (f != NULL)
1100 fclose(f);
1101 signal(SIGCHLD, osigchld);
1102 for (i = 0; i < ac; i++)
1103 free(av[i]);
1104 free(av);
1105 if (uid_swapped)
1106 restore_uid();
1107 free(command);
1108 free(username);
1109 free(key_fp);
1110 free(keytext);
Damien Miller09d3e122012-10-31 08:58:58 +11001111 return found_key;
1112}
1113
1114/*
1115 * Check whether key authenticates and authorises the user.
1116 */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001117int
markus@openbsd.org54d90ac2017-05-30 08:52:19 +00001118user_key_allowed(struct passwd *pw, struct sshkey *key, int auth_attempt)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001119{
Damien Millerd8478b62011-05-29 21:39:36 +10001120 u_int success, i;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001121 char *file;
1122
Damien Miller1aed65e2010-03-04 21:53:35 +11001123 if (auth_key_is_revoked(key))
1124 return 0;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001125 if (sshkey_is_cert(key) &&
1126 auth_key_is_revoked(key->cert->signature_key))
Damien Miller1aed65e2010-03-04 21:53:35 +11001127 return 0;
1128
1129 success = user_cert_trusted_ca(pw, key);
1130 if (success)
1131 return success;
1132
Damien Miller09d3e122012-10-31 08:58:58 +11001133 success = user_key_command_allowed2(pw, key);
1134 if (success > 0)
1135 return success;
1136
Damien Millerd8478b62011-05-29 21:39:36 +10001137 for (i = 0; !success && i < options.num_authkeys_files; i++) {
Damien Miller09d3e122012-10-31 08:58:58 +11001138
1139 if (strcasecmp(options.authorized_keys_files[i], "none") == 0)
1140 continue;
Damien Millerd8478b62011-05-29 21:39:36 +10001141 file = expand_authorized_keys(
1142 options.authorized_keys_files[i], pw);
Damien Miller09d3e122012-10-31 08:58:58 +11001143
Damien Millerd8478b62011-05-29 21:39:36 +10001144 success = user_key_allowed2(pw, key, file);
Darren Tuckera627d422013-06-02 07:31:17 +10001145 free(file);
Damien Millerd8478b62011-05-29 21:39:36 +10001146 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001147
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001148 return success;
1149}
1150
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001151/* Records a public key in the list of previously-successful keys */
1152void
1153auth2_record_userkey(Authctxt *authctxt, struct sshkey *key)
1154{
1155 struct sshkey **tmp;
1156
1157 if (authctxt->nprev_userkeys >= INT_MAX ||
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00001158 (tmp = recallocarray(authctxt->prev_userkeys,
1159 authctxt->nprev_userkeys, authctxt->nprev_userkeys + 1,
1160 sizeof(*tmp))) == NULL)
1161 fatal("%s: recallocarray failed", __func__);
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +00001162 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};