blob: f9e4e2e7f579e070b8aea2b66e87d11729f9c316 [file] [log] [blame]
mestre@openbsd.orgdb8bb802018-08-28 12:25:53 +00001/* $OpenBSD: auth2-pubkey.c,v 1.85 2018/08/28 12:25:53 mestre 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>
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000030
Damien Miller09d3e122012-10-31 08:58:58 +110031#include <errno.h>
Darren Tucker06db5842008-06-13 14:51:28 +100032#include <fcntl.h>
Darren Tucker737f7af2012-11-05 17:07:43 +110033#ifdef HAVE_PATHS_H
34# include <paths.h>
35#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100036#include <pwd.h>
Damien Miller09d3e122012-10-31 08:58:58 +110037#include <signal.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100038#include <stdio.h>
Damien Millerd7834352006-08-05 12:39:39 +100039#include <stdarg.h>
Damien Miller0a80ca12010-02-27 07:55:05 +110040#include <string.h>
41#include <time.h>
Darren Tuckerd9526a52008-06-14 09:01:24 +100042#include <unistd.h>
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +000043#include <limits.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100044
Damien Millerd7834352006-08-05 12:39:39 +100045#include "xmalloc.h"
Darren Tucker22cc7412004-12-06 22:47:41 +110046#include "ssh.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000047#include "ssh2.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000048#include "packet.h"
markus@openbsd.orgc7d39ac2018-07-09 21:35:50 +000049#include "sshbuf.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000050#include "log.h"
Damien Miller7acefbb2014-07-18 14:11:24 +100051#include "misc.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000052#include "servconf.h"
53#include "compat.h"
markus@openbsd.org00ed75c2017-05-30 14:10:53 +000054#include "sshkey.h"
Damien Millerd7834352006-08-05 12:39:39 +100055#include "hostfile.h"
56#include "auth.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000057#include "pathnames.h"
58#include "uidswap.h"
59#include "auth-options.h"
60#include "canohost.h"
Damien Millerd7834352006-08-05 12:39:39 +100061#ifdef GSSAPI
62#include "ssh-gss.h"
63#endif
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000064#include "monitor_wrap.h"
Damien Miller1aed65e2010-03-04 21:53:35 +110065#include "authfile.h"
Damien Miller30da3442010-05-10 11:58:03 +100066#include "match.h"
djm@openbsd.org24232a32015-05-21 06:38:35 +000067#include "ssherr.h"
68#include "channels.h" /* XXX for session.h */
69#include "session.h" /* XXX for child_set_env(); refactor? */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000070
71/* import */
72extern ServerOptions options;
73extern u_char *session_id2;
Darren Tucker502d3842003-06-28 12:38:01 +100074extern u_int session_id2_len;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000075
djm@openbsd.org27885632017-12-19 00:24:34 +000076static char *
77format_key(const struct sshkey *key)
78{
79 char *ret, *fp = sshkey_fingerprint(key,
80 options.fingerprint_hash, SSH_FP_DEFAULT);
81
82 xasprintf(&ret, "%s %s", sshkey_type(key), fp);
83 free(fp);
84 return ret;
85}
86
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000087static int
markus@openbsd.orgeb272ea2017-05-30 14:29:59 +000088userauth_pubkey(struct ssh *ssh)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000089{
markus@openbsd.orgeb272ea2017-05-30 14:29:59 +000090 Authctxt *authctxt = ssh->authctxt;
djm@openbsd.org7c856852018-03-03 03:15:51 +000091 struct passwd *pw = authctxt->pw;
djm@openbsd.org74287f52018-07-31 03:10:27 +000092 struct sshbuf *b = NULL;
markus@openbsd.org54d90ac2017-05-30 08:52:19 +000093 struct sshkey *key = NULL;
djm@openbsd.org74287f52018-07-31 03:10:27 +000094 char *pkalg = NULL, *userstyle = NULL, *key_s = NULL, *ca_s = NULL;
95 u_char *pkblob = NULL, *sig = NULL, have_sig;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +000096 size_t blen, slen;
97 int r, pktype;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000098 int authenticated = 0;
djm@openbsd.org7c856852018-03-03 03:15:51 +000099 struct sshauthopt *authopts = NULL;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000100
djm@openbsd.org14b5c632018-01-23 05:27:21 +0000101 if ((r = sshpkt_get_u8(ssh, &have_sig)) != 0 ||
102 (r = sshpkt_get_cstring(ssh, &pkalg, NULL)) != 0 ||
103 (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0)
104 fatal("%s: parse request failed: %s", __func__, ssh_err(r));
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000105 pktype = sshkey_type_from_name(pkalg);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000106 if (pktype == KEY_UNSPEC) {
107 /* this is perfectly legal */
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000108 verbose("%s: unsupported public key algorithm: %s",
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000109 __func__, pkalg);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000110 goto done;
111 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000112 if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
113 error("%s: could not parse key: %s", __func__, ssh_err(r));
114 goto done;
115 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000116 if (key == NULL) {
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000117 error("%s: cannot decode key: %s", __func__, pkalg);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000118 goto done;
119 }
120 if (key->type != pktype) {
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000121 error("%s: type mismatch for decoded key "
122 "(received %d, expected %d)", __func__, key->type, pktype);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000123 goto done;
124 }
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000125 if (sshkey_type_plain(key->type) == KEY_RSA &&
126 (ssh->compat & SSH_BUG_RSASIGMD5) != 0) {
Damien Miller324541e2013-12-31 12:25:40 +1100127 logit("Refusing RSA key because client uses unsafe "
128 "signature scheme");
129 goto done;
130 }
djm@openbsd.org8f574952017-06-24 06:34:38 +0000131 if (auth2_key_already_used(authctxt, key)) {
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000132 logit("refusing previously-used %s key", sshkey_type(key));
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +0000133 goto done;
134 }
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000135 if (match_pattern_list(pkalg, options.pubkey_key_types, 0) != 1) {
djm@openbsd.org1f729f02015-01-13 07:39:19 +0000136 logit("%s: key type %s not in PubkeyAcceptedKeyTypes",
137 __func__, sshkey_ssh_name(key));
138 goto done;
139 }
140
djm@openbsd.org27885632017-12-19 00:24:34 +0000141 key_s = format_key(key);
142 if (sshkey_is_cert(key))
143 ca_s = format_key(key->cert->signature_key);
144
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000145 if (have_sig) {
djm@openbsd.org27885632017-12-19 00:24:34 +0000146 debug3("%s: have %s signature for %s%s%s",
147 __func__, pkalg, key_s,
148 ca_s == NULL ? "" : " CA ",
149 ca_s == NULL ? "" : ca_s);
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000150 if ((r = sshpkt_get_string(ssh, &sig, &slen)) != 0 ||
151 (r = sshpkt_get_end(ssh)) != 0)
152 fatal("%s: %s", __func__, ssh_err(r));
153 if ((b = sshbuf_new()) == NULL)
154 fatal("%s: sshbuf_new failed", __func__);
155 if (ssh->compat & SSH_OLD_SESSIONID) {
156 if ((r = sshbuf_put(b, session_id2,
157 session_id2_len)) != 0)
158 fatal("%s: sshbuf_put session id: %s",
159 __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000160 } else {
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000161 if ((r = sshbuf_put_string(b, session_id2,
162 session_id2_len)) != 0)
163 fatal("%s: sshbuf_put_string session id: %s",
164 __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000165 }
djm@openbsd.org74287f52018-07-31 03:10:27 +0000166 if (!authctxt->valid || authctxt->user == NULL) {
167 debug2("%s: disabled because of invalid user",
168 __func__);
169 goto done;
170 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000171 /* reconstruct packet */
Damien Miller4ce189d2013-04-23 15:17:52 +1000172 xasprintf(&userstyle, "%s%s%s", authctxt->user,
173 authctxt->style ? ":" : "",
174 authctxt->style ? authctxt->style : "");
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000175 if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
176 (r = sshbuf_put_cstring(b, userstyle)) != 0 ||
djm@openbsd.org14b5c632018-01-23 05:27:21 +0000177 (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
178 (r = sshbuf_put_cstring(b, "publickey")) != 0 ||
179 (r = sshbuf_put_u8(b, have_sig)) != 0 ||
mestre@openbsd.orgdb8bb802018-08-28 12:25:53 +0000180 (r = sshbuf_put_cstring(b, pkalg)) != 0 ||
djm@openbsd.org14b5c632018-01-23 05:27:21 +0000181 (r = sshbuf_put_string(b, pkblob, blen)) != 0)
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000182 fatal("%s: build packet failed: %s",
183 __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000184#ifdef DEBUG_PK
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000185 sshbuf_dump(b, stderr);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000186#endif
187 /* test for correct signature */
188 authenticated = 0;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000189 if (PRIVSEP(user_key_allowed(ssh, pw, key, 1, &authopts)) &&
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000190 PRIVSEP(sshkey_verify(key, sig, slen,
191 sshbuf_ptr(b), sshbuf_len(b),
192 (ssh->compat & SSH_BUG_SIGTYPE) == 0 ? pkalg : NULL,
193 ssh->compat)) == 0) {
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000194 authenticated = 1;
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +0000195 }
djm@openbsd.org8f574952017-06-24 06:34:38 +0000196 auth2_record_key(authctxt, authenticated, key);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000197 } else {
djm@openbsd.org27885632017-12-19 00:24:34 +0000198 debug("%s: test pkalg %s pkblob %s%s%s",
199 __func__, pkalg, key_s,
200 ca_s == NULL ? "" : " CA ",
201 ca_s == NULL ? "" : ca_s);
202
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000203 if ((r = sshpkt_get_end(ssh)) != 0)
204 fatal("%s: %s", __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000205
djm@openbsd.org74287f52018-07-31 03:10:27 +0000206 if (!authctxt->valid || authctxt->user == NULL) {
207 debug2("%s: disabled because of invalid user",
208 __func__);
209 goto done;
210 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000211 /* XXX fake reply and always send PK_OK ? */
212 /*
213 * XXX this allows testing whether a user is allowed
214 * to login: if you happen to have a valid pubkey this
215 * message is sent. the message is NEVER sent at all
216 * if a user is not allowed to login. is this an
217 * issue? -markus
218 */
djm@openbsd.org7c856852018-03-03 03:15:51 +0000219 if (PRIVSEP(user_key_allowed(ssh, pw, key, 0, NULL))) {
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000220 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_PK_OK))
221 != 0 ||
222 (r = sshpkt_put_cstring(ssh, pkalg)) != 0 ||
223 (r = sshpkt_put_string(ssh, pkblob, blen)) != 0 ||
markus@openbsd.org394a8422018-07-11 18:55:11 +0000224 (r = sshpkt_send(ssh)) != 0 ||
225 (r = ssh_packet_write_wait(ssh)) != 0)
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000226 fatal("%s: %s", __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000227 authctxt->postponed = 1;
228 }
229 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000230done:
djm@openbsd.org7c856852018-03-03 03:15:51 +0000231 if (authenticated == 1 && auth_activate_options(ssh, authopts) != 0) {
232 debug("%s: key options inconsistent with existing", __func__);
233 authenticated = 0;
234 }
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000235 debug2("%s: authenticated %d pkalg %s", __func__, authenticated, pkalg);
djm@openbsd.org7c856852018-03-03 03:15:51 +0000236
djm@openbsd.org7fef1732018-08-23 03:01:08 +0000237 sshbuf_free(b);
djm@openbsd.org7c856852018-03-03 03:15:51 +0000238 sshauthopt_free(authopts);
djm@openbsd.org8f574952017-06-24 06:34:38 +0000239 sshkey_free(key);
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000240 free(userstyle);
Darren Tuckera627d422013-06-02 07:31:17 +1000241 free(pkalg);
242 free(pkblob);
djm@openbsd.org27885632017-12-19 00:24:34 +0000243 free(key_s);
244 free(ca_s);
djm@openbsd.org74287f52018-07-31 03:10:27 +0000245 free(sig);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000246 return authenticated;
247}
248
Damien Miller30da3442010-05-10 11:58:03 +1000249static int
Damien Miller86687062014-07-02 15:28:02 +1000250match_principals_option(const char *principal_list, struct sshkey_cert *cert)
Damien Miller30da3442010-05-10 11:58:03 +1000251{
252 char *result;
253 u_int i;
254
255 /* XXX percent_expand() sequences for authorized_principals? */
256
257 for (i = 0; i < cert->nprincipals; i++) {
258 if ((result = match_list(cert->principals[i],
259 principal_list, NULL)) != NULL) {
260 debug3("matched principal from key options \"%.100s\"",
261 result);
Darren Tuckera627d422013-06-02 07:31:17 +1000262 free(result);
Damien Miller30da3442010-05-10 11:58:03 +1000263 return 1;
264 }
265 }
266 return 0;
267}
268
djm@openbsd.org7c856852018-03-03 03:15:51 +0000269/*
270 * Process a single authorized_principals format line. Returns 0 and sets
271 * authoptsp is principal is authorised, -1 otherwise. "loc" is used as a
272 * log preamble for file/line information.
273 */
Damien Miller30da3442010-05-10 11:58:03 +1000274static int
djm@openbsd.org7c856852018-03-03 03:15:51 +0000275check_principals_line(struct ssh *ssh, char *cp, const struct sshkey_cert *cert,
276 const char *loc, struct sshauthopt **authoptsp)
Damien Miller30da3442010-05-10 11:58:03 +1000277{
djm@openbsd.org7c856852018-03-03 03:15:51 +0000278 u_int i, found = 0;
279 char *ep, *line_opts;
280 const char *reason = NULL;
281 struct sshauthopt *opts = NULL;
282
283 if (authoptsp != NULL)
284 *authoptsp = NULL;
285
286 /* Trim trailing whitespace. */
287 ep = cp + strlen(cp) - 1;
288 while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t'))
289 *ep-- = '\0';
290
291 /*
292 * If the line has internal whitespace then assume it has
293 * key options.
294 */
295 line_opts = NULL;
296 if ((ep = strrchr(cp, ' ')) != NULL ||
297 (ep = strrchr(cp, '\t')) != NULL) {
298 for (; *ep == ' ' || *ep == '\t'; ep++)
299 ;
300 line_opts = cp;
301 cp = ep;
302 }
303 if ((opts = sshauthopt_parse(line_opts, &reason)) == NULL) {
304 debug("%s: bad principals options: %s", loc, reason);
305 auth_debug_add("%s: bad principals options: %s", loc, reason);
306 return -1;
307 }
308 /* Check principals in cert against those on line */
309 for (i = 0; i < cert->nprincipals; i++) {
310 if (strcmp(cp, cert->principals[i]) != 0)
311 continue;
312 debug3("%s: matched principal \"%.100s\"",
313 loc, cert->principals[i]);
314 found = 1;
315 }
316 if (found && authoptsp != NULL) {
317 *authoptsp = opts;
318 opts = NULL;
319 }
320 sshauthopt_free(opts);
321 return found ? 0 : -1;
322}
323
324static int
325process_principals(struct ssh *ssh, FILE *f, const char *file,
326 const struct sshkey_cert *cert, struct sshauthopt **authoptsp)
327{
markus@openbsd.org7f906352018-06-06 18:29:18 +0000328 char loc[256], *line = NULL, *cp, *ep;
329 size_t linesize = 0;
Damien Miller30da3442010-05-10 11:58:03 +1000330 u_long linenum = 0;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000331 u_int found_principal = 0;
332
333 if (authoptsp != NULL)
334 *authoptsp = NULL;
Damien Miller30da3442010-05-10 11:58:03 +1000335
markus@openbsd.org7f906352018-06-06 18:29:18 +0000336 while (getline(&line, &linesize, f) != -1) {
337 linenum++;
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000338 /* Always consume entire input */
339 if (found_principal)
340 continue;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000341
Damien Miller6018a362010-07-02 13:35:19 +1000342 /* Skip leading whitespace. */
Damien Miller30da3442010-05-10 11:58:03 +1000343 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
344 ;
Damien Miller6018a362010-07-02 13:35:19 +1000345 /* Skip blank and comment lines. */
346 if ((ep = strchr(cp, '#')) != NULL)
347 *ep = '\0';
348 if (!*cp || *cp == '\n')
Damien Miller30da3442010-05-10 11:58:03 +1000349 continue;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000350
351 snprintf(loc, sizeof(loc), "%.200s:%lu", file, linenum);
352 if (check_principals_line(ssh, cp, cert, loc, authoptsp) == 0)
353 found_principal = 1;
Damien Miller30da3442010-05-10 11:58:03 +1000354 }
markus@openbsd.org7f906352018-06-06 18:29:18 +0000355 free(line);
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000356 return found_principal;
Damien Miller09d3e122012-10-31 08:58:58 +1100357}
Damien Miller30da3442010-05-10 11:58:03 +1000358
djm@openbsd.org7c856852018-03-03 03:15:51 +0000359/* XXX remove pw args here and elsewhere once ssh->authctxt is guaranteed */
360
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000361static int
djm@openbsd.org7c856852018-03-03 03:15:51 +0000362match_principals_file(struct ssh *ssh, struct passwd *pw, char *file,
363 struct sshkey_cert *cert, struct sshauthopt **authoptsp)
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000364{
365 FILE *f;
366 int success;
367
djm@openbsd.org7c856852018-03-03 03:15:51 +0000368 if (authoptsp != NULL)
369 *authoptsp = NULL;
370
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000371 temporarily_use_uid(pw);
372 debug("trying authorized principals file %s", file);
373 if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) {
374 restore_uid();
375 return 0;
376 }
djm@openbsd.org7c856852018-03-03 03:15:51 +0000377 success = process_principals(ssh, f, file, cert, authoptsp);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000378 fclose(f);
379 restore_uid();
380 return success;
381}
382
383/*
384 * Checks whether principal is allowed in output of command.
385 * returns 1 if the principal is allowed or 0 otherwise.
386 */
387static int
djm@openbsd.org7c856852018-03-03 03:15:51 +0000388match_principals_command(struct ssh *ssh, struct passwd *user_pw,
389 const struct sshkey *key, struct sshauthopt **authoptsp)
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000390{
djm@openbsd.org7c856852018-03-03 03:15:51 +0000391 struct passwd *runas_pw = NULL;
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000392 const struct sshkey_cert *cert = key->cert;
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000393 FILE *f = NULL;
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000394 int r, ok, found_principal = 0;
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000395 int i, ac = 0, uid_swapped = 0;
396 pid_t pid;
397 char *tmp, *username = NULL, *command = NULL, **av = NULL;
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000398 char *ca_fp = NULL, *key_fp = NULL, *catext = NULL, *keytext = NULL;
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000399 char serial_s[16], uidstr[32];
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000400 void (*osigchld)(int);
401
djm@openbsd.org7c856852018-03-03 03:15:51 +0000402 if (authoptsp != NULL)
403 *authoptsp = NULL;
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000404 if (options.authorized_principals_command == NULL)
405 return 0;
406 if (options.authorized_principals_command_user == NULL) {
407 error("No user for AuthorizedPrincipalsCommand specified, "
408 "skipping");
409 return 0;
410 }
411
412 /*
413 * NB. all returns later this function should go via "out" to
414 * ensure the original SIGCHLD handler is restored properly.
415 */
416 osigchld = signal(SIGCHLD, SIG_DFL);
417
418 /* Prepare and verify the user for the command */
419 username = percent_expand(options.authorized_principals_command_user,
420 "u", user_pw->pw_name, (char *)NULL);
djm@openbsd.org7c856852018-03-03 03:15:51 +0000421 runas_pw = getpwnam(username);
422 if (runas_pw == NULL) {
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000423 error("AuthorizedPrincipalsCommandUser \"%s\" not found: %s",
424 username, strerror(errno));
425 goto out;
426 }
427
428 /* Turn the command into an argument vector */
djm@openbsd.orgde4ae072017-08-18 05:36:45 +0000429 if (argv_split(options.authorized_principals_command, &ac, &av) != 0) {
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000430 error("AuthorizedPrincipalsCommand \"%s\" contains "
431 "invalid quotes", command);
432 goto out;
433 }
434 if (ac == 0) {
435 error("AuthorizedPrincipalsCommand \"%s\" yielded no arguments",
436 command);
437 goto out;
438 }
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000439 if ((ca_fp = sshkey_fingerprint(cert->signature_key,
440 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
441 error("%s: sshkey_fingerprint failed", __func__);
442 goto out;
443 }
djm@openbsd.org00df97f2016-09-14 20:11:26 +0000444 if ((key_fp = sshkey_fingerprint(key,
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000445 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
446 error("%s: sshkey_fingerprint failed", __func__);
447 goto out;
448 }
449 if ((r = sshkey_to_base64(cert->signature_key, &catext)) != 0) {
450 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r));
451 goto out;
452 }
453 if ((r = sshkey_to_base64(key, &keytext)) != 0) {
454 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r));
455 goto out;
456 }
djm@openbsd.orgf83a0cf2016-09-21 17:44:20 +0000457 snprintf(serial_s, sizeof(serial_s), "%llu",
458 (unsigned long long)cert->serial);
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000459 snprintf(uidstr, sizeof(uidstr), "%llu",
460 (unsigned long long)user_pw->pw_uid);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000461 for (i = 1; i < ac; i++) {
462 tmp = percent_expand(av[i],
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000463 "U", uidstr,
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000464 "u", user_pw->pw_name,
465 "h", user_pw->pw_dir,
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000466 "t", sshkey_ssh_name(key),
467 "T", sshkey_ssh_name(cert->signature_key),
468 "f", key_fp,
469 "F", ca_fp,
470 "k", keytext,
471 "K", catext,
djm@openbsd.orgbfa9d962016-09-21 01:34:45 +0000472 "i", cert->key_id,
473 "s", serial_s,
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000474 (char *)NULL);
475 if (tmp == NULL)
476 fatal("%s: percent_expand failed", __func__);
477 free(av[i]);
478 av[i] = tmp;
479 }
480 /* Prepare a printable command for logs, etc. */
djm@openbsd.orgde4ae072017-08-18 05:36:45 +0000481 command = argv_assemble(ac, av);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000482
djm@openbsd.org7c856852018-03-03 03:15:51 +0000483 if ((pid = subprocess("AuthorizedPrincipalsCommand", runas_pw, command,
djm@openbsd.orgde4ae072017-08-18 05:36:45 +0000484 ac, av, &f,
485 SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD)) == 0)
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000486 goto out;
487
488 uid_swapped = 1;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000489 temporarily_use_uid(runas_pw);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000490
djm@openbsd.org7c856852018-03-03 03:15:51 +0000491 ok = process_principals(ssh, f, "(command)", cert, authoptsp);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000492
djm@openbsd.orgddd3d342016-12-30 22:08:02 +0000493 fclose(f);
494 f = NULL;
495
djm@openbsd.orgb074c3c2017-08-18 05:48:04 +0000496 if (exited_cleanly(pid, "AuthorizedPrincipalsCommand", command, 0) != 0)
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000497 goto out;
498
499 /* Read completed successfully */
500 found_principal = ok;
501 out:
502 if (f != NULL)
503 fclose(f);
504 signal(SIGCHLD, osigchld);
505 for (i = 0; i < ac; i++)
506 free(av[i]);
507 free(av);
508 if (uid_swapped)
509 restore_uid();
510 free(command);
511 free(username);
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000512 free(ca_fp);
513 free(key_fp);
514 free(catext);
515 free(keytext);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000516 return found_principal;
517}
djm@openbsd.org7c856852018-03-03 03:15:51 +0000518
519static void
520skip_space(char **cpp)
521{
522 char *cp;
523
524 for (cp = *cpp; *cp == ' ' || *cp == '\t'; cp++)
525 ;
526 *cpp = cp;
527}
528
529/*
530 * Advanced *cpp past the end of key options, defined as the first unquoted
531 * whitespace character. Returns 0 on success or -1 on failure (e.g.
532 * unterminated quotes).
533 */
534static int
535advance_past_options(char **cpp)
536{
537 char *cp = *cpp;
538 int quoted = 0;
539
540 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
541 if (*cp == '\\' && cp[1] == '"')
542 cp++; /* Skip both */
543 else if (*cp == '"')
544 quoted = !quoted;
545 }
546 *cpp = cp;
547 /* return failure for unterminated quotes */
548 return (*cp == '\0' && quoted) ? -1 : 0;
549}
550
551/*
552 * Check a single line of an authorized_keys-format file. Returns 0 if key
553 * matches, -1 otherwise. Will return key/cert options via *authoptsp
554 * on success. "loc" is used as file/line location in log messages.
555 */
556static int
557check_authkey_line(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
558 char *cp, const char *loc, struct sshauthopt **authoptsp)
559{
560 int want_keytype = sshkey_is_cert(key) ? KEY_UNSPEC : key->type;
561 struct sshkey *found = NULL;
562 struct sshauthopt *keyopts = NULL, *certopts = NULL, *finalopts = NULL;
563 char *key_options = NULL, *fp = NULL;
564 const char *reason = NULL;
565 int ret = -1;
566
567 if (authoptsp != NULL)
568 *authoptsp = NULL;
569
570 if ((found = sshkey_new(want_keytype)) == NULL) {
571 debug3("%s: keytype %d failed", __func__, want_keytype);
572 goto out;
573 }
574
575 /* XXX djm: peek at key type in line and skip if unwanted */
576
577 if (sshkey_read(found, &cp) != 0) {
578 /* no key? check for options */
579 debug2("%s: check options: '%s'", loc, cp);
580 key_options = cp;
581 if (advance_past_options(&cp) != 0) {
582 reason = "invalid key option string";
583 goto fail_reason;
584 }
585 skip_space(&cp);
586 if (sshkey_read(found, &cp) != 0) {
587 /* still no key? advance to next line*/
588 debug2("%s: advance: '%s'", loc, cp);
589 goto out;
590 }
591 }
592 /* Parse key options now; we need to know if this is a CA key */
593 if ((keyopts = sshauthopt_parse(key_options, &reason)) == NULL) {
594 debug("%s: bad key options: %s", loc, reason);
595 auth_debug_add("%s: bad key options: %s", loc, reason);
596 goto out;
597 }
598 /* Ignore keys that don't match or incorrectly marked as CAs */
599 if (sshkey_is_cert(key)) {
600 /* Certificate; check signature key against CA */
601 if (!sshkey_equal(found, key->cert->signature_key) ||
602 !keyopts->cert_authority)
603 goto out;
604 } else {
605 /* Plain key: check it against key found in file */
606 if (!sshkey_equal(found, key) || keyopts->cert_authority)
607 goto out;
608 }
609
610 /* We have a candidate key, perform authorisation checks */
611 if ((fp = sshkey_fingerprint(found,
612 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
613 fatal("%s: fingerprint failed", __func__);
614
615 debug("%s: matching %s found: %s %s", loc,
616 sshkey_is_cert(key) ? "CA" : "key", sshkey_type(found), fp);
617
618 if (auth_authorise_keyopts(ssh, pw, keyopts,
619 sshkey_is_cert(key), loc) != 0) {
620 reason = "Refused by key options";
621 goto fail_reason;
622 }
623 /* That's all we need for plain keys. */
624 if (!sshkey_is_cert(key)) {
625 verbose("Accepted key %s %s found at %s",
626 sshkey_type(found), fp, loc);
627 finalopts = keyopts;
628 keyopts = NULL;
629 goto success;
630 }
631
632 /*
633 * Additional authorisation for certificates.
634 */
635
636 /* Parse and check options present in certificate */
637 if ((certopts = sshauthopt_from_cert(key)) == NULL) {
638 reason = "Invalid certificate options";
639 goto fail_reason;
640 }
641 if (auth_authorise_keyopts(ssh, pw, certopts, 0, loc) != 0) {
642 reason = "Refused by certificate options";
643 goto fail_reason;
644 }
645 if ((finalopts = sshauthopt_merge(keyopts, certopts, &reason)) == NULL)
646 goto fail_reason;
647
648 /*
649 * If the user has specified a list of principals as
650 * a key option, then prefer that list to matching
651 * their username in the certificate principals list.
652 */
653 if (keyopts->cert_principals != NULL &&
654 !match_principals_option(keyopts->cert_principals, key->cert)) {
655 reason = "Certificate does not contain an authorized principal";
656 goto fail_reason;
657 }
658 if (sshkey_cert_check_authority(key, 0, 0,
659 keyopts->cert_principals == NULL ? pw->pw_name : NULL, &reason) != 0)
660 goto fail_reason;
661
662 verbose("Accepted certificate ID \"%s\" (serial %llu) "
663 "signed by CA %s %s found at %s",
664 key->cert->key_id,
665 (unsigned long long)key->cert->serial,
666 sshkey_type(found), fp, loc);
667
668 success:
669 if (finalopts == NULL)
670 fatal("%s: internal error: missing options", __func__);
671 if (authoptsp != NULL) {
672 *authoptsp = finalopts;
673 finalopts = NULL;
674 }
675 /* success */
676 ret = 0;
677 goto out;
678
679 fail_reason:
680 error("%s", reason);
681 auth_debug_add("%s", reason);
682 out:
683 free(fp);
684 sshauthopt_free(keyopts);
685 sshauthopt_free(certopts);
686 sshauthopt_free(finalopts);
687 sshkey_free(found);
688 return ret;
689}
690
Damien Miller09d3e122012-10-31 08:58:58 +1100691/*
692 * Checks whether key is allowed in authorized_keys-format file,
693 * returns 1 if the key is allowed or 0 otherwise.
694 */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000695static int
djm@openbsd.org7c856852018-03-03 03:15:51 +0000696check_authkeys_file(struct ssh *ssh, struct passwd *pw, FILE *f,
697 char *file, struct sshkey *key, struct sshauthopt **authoptsp)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000698{
markus@openbsd.org7f906352018-06-06 18:29:18 +0000699 char *cp, *line = NULL, loc[256];
700 size_t linesize = 0;
Darren Tucker33c787f2008-07-02 22:37:30 +1000701 int found_key = 0;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000702 u_long linenum = 0;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000703
704 if (authoptsp != NULL)
705 *authoptsp = NULL;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000706
markus@openbsd.org7f906352018-06-06 18:29:18 +0000707 while (getline(&line, &linesize, f) != -1) {
708 linenum++;
djm@openbsd.orgabd59662017-09-07 23:48:09 +0000709 /* Always consume entire file */
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000710 if (found_key)
711 continue;
Damien Miller0a80ca12010-02-27 07:55:05 +1100712
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000713 /* Skip leading whitespace, empty and comment lines. */
djm@openbsd.org7c856852018-03-03 03:15:51 +0000714 cp = line;
715 skip_space(&cp);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000716 if (!*cp || *cp == '\n' || *cp == '#')
717 continue;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000718 snprintf(loc, sizeof(loc), "%.200s:%lu", file, linenum);
719 if (check_authkey_line(ssh, pw, key, cp, loc, authoptsp) == 0)
Damien Miller0a80ca12010-02-27 07:55:05 +1100720 found_key = 1;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000721 }
markus@openbsd.org7f906352018-06-06 18:29:18 +0000722 free(line);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000723 return found_key;
724}
725
Damien Miller1aed65e2010-03-04 21:53:35 +1100726/* Authenticate a certificate key against TrustedUserCAKeys */
727static int
djm@openbsd.org7c856852018-03-03 03:15:51 +0000728user_cert_trusted_ca(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
729 struct sshauthopt **authoptsp)
Damien Miller1aed65e2010-03-04 21:53:35 +1100730{
Damien Miller30da3442010-05-10 11:58:03 +1000731 char *ca_fp, *principals_file = NULL;
Damien Miller1aed65e2010-03-04 21:53:35 +1100732 const char *reason;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000733 struct sshauthopt *principals_opts = NULL, *cert_opts = NULL;
734 struct sshauthopt *final_opts = NULL;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000735 int r, ret = 0, found_principal = 0, use_authorized_principals;
Damien Miller1aed65e2010-03-04 21:53:35 +1100736
djm@openbsd.org7c856852018-03-03 03:15:51 +0000737 if (authoptsp != NULL)
738 *authoptsp = NULL;
739
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000740 if (!sshkey_is_cert(key) || options.trusted_user_ca_keys == NULL)
Damien Miller1aed65e2010-03-04 21:53:35 +1100741 return 0;
742
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000743 if ((ca_fp = sshkey_fingerprint(key->cert->signature_key,
744 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
745 return 0;
Damien Miller1aed65e2010-03-04 21:53:35 +1100746
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000747 if ((r = sshkey_in_file(key->cert->signature_key,
748 options.trusted_user_ca_keys, 1, 0)) != 0) {
749 debug2("%s: CA %s %s is not listed in %s: %s", __func__,
750 sshkey_type(key->cert->signature_key), ca_fp,
751 options.trusted_user_ca_keys, ssh_err(r));
Damien Miller1aed65e2010-03-04 21:53:35 +1100752 goto out;
753 }
Damien Miller30da3442010-05-10 11:58:03 +1000754 /*
755 * If AuthorizedPrincipals is in use, then compare the certificate
756 * principals against the names in that file rather than matching
757 * against the username.
758 */
759 if ((principals_file = authorized_principals_file(pw)) != NULL) {
djm@openbsd.org7c856852018-03-03 03:15:51 +0000760 if (match_principals_file(ssh, pw, principals_file,
761 key->cert, &principals_opts))
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000762 found_principal = 1;
763 }
764 /* Try querying command if specified */
djm@openbsd.org7c856852018-03-03 03:15:51 +0000765 if (!found_principal && match_principals_command(ssh, pw, key,
766 &principals_opts))
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000767 found_principal = 1;
jsing@openbsd.org596dbca2015-06-15 18:44:22 +0000768 /* If principals file or command is specified, then require a match */
769 use_authorized_principals = principals_file != NULL ||
770 options.authorized_principals_command != NULL;
771 if (!found_principal && use_authorized_principals) {
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000772 reason = "Certificate does not contain an authorized principal";
djm@openbsd.org7c856852018-03-03 03:15:51 +0000773 goto fail_reason;
Damien Miller1aed65e2010-03-04 21:53:35 +1100774 }
djm@openbsd.org7c856852018-03-03 03:15:51 +0000775 if (use_authorized_principals && principals_opts == NULL)
776 fatal("%s: internal error: missing principals_opts", __func__);
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000777 if (sshkey_cert_check_authority(key, 0, 1,
jsing@openbsd.org596dbca2015-06-15 18:44:22 +0000778 use_authorized_principals ? NULL : pw->pw_name, &reason) != 0)
Damien Miller30da3442010-05-10 11:58:03 +1000779 goto fail_reason;
Damien Miller1aed65e2010-03-04 21:53:35 +1100780
djm@openbsd.org7c856852018-03-03 03:15:51 +0000781 /* Check authority from options in key and from principals file/cmd */
782 if ((cert_opts = sshauthopt_from_cert(key)) == NULL) {
783 reason = "Invalid certificate options";
784 goto fail_reason;
785 }
786 if (auth_authorise_keyopts(ssh, pw, cert_opts, 0, "cert") != 0) {
787 reason = "Refused by certificate options";
788 goto fail_reason;
789 }
790 if (principals_opts == NULL) {
791 final_opts = cert_opts;
792 cert_opts = NULL;
793 } else {
794 if (auth_authorise_keyopts(ssh, pw, principals_opts, 0,
795 "principals") != 0) {
796 reason = "Refused by certificate principals options";
797 goto fail_reason;
798 }
799 if ((final_opts = sshauthopt_merge(principals_opts,
800 cert_opts, &reason)) == NULL) {
801 fail_reason:
802 error("%s", reason);
803 auth_debug_add("%s", reason);
804 goto out;
805 }
806 }
807
808 /* Success */
djm@openbsd.org63d18812015-10-27 01:44:45 +0000809 verbose("Accepted certificate ID \"%s\" (serial %llu) signed by "
810 "%s CA %s via %s", key->cert->key_id,
811 (unsigned long long)key->cert->serial,
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000812 sshkey_type(key->cert->signature_key), ca_fp,
Damien Millere513a912010-03-22 05:51:21 +1100813 options.trusted_user_ca_keys);
djm@openbsd.org7c856852018-03-03 03:15:51 +0000814 if (authoptsp != NULL) {
815 *authoptsp = final_opts;
816 final_opts = NULL;
817 }
Damien Miller1aed65e2010-03-04 21:53:35 +1100818 ret = 1;
Damien Miller1aed65e2010-03-04 21:53:35 +1100819 out:
djm@openbsd.org7c856852018-03-03 03:15:51 +0000820 sshauthopt_free(principals_opts);
821 sshauthopt_free(cert_opts);
822 sshauthopt_free(final_opts);
Darren Tuckera627d422013-06-02 07:31:17 +1000823 free(principals_file);
824 free(ca_fp);
Damien Miller1aed65e2010-03-04 21:53:35 +1100825 return ret;
826}
827
Damien Miller09d3e122012-10-31 08:58:58 +1100828/*
829 * Checks whether key is allowed in file.
830 * returns 1 if the key is allowed or 0 otherwise.
831 */
832static int
djm@openbsd.org7c856852018-03-03 03:15:51 +0000833user_key_allowed2(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
834 char *file, struct sshauthopt **authoptsp)
Damien Miller09d3e122012-10-31 08:58:58 +1100835{
836 FILE *f;
837 int found_key = 0;
838
djm@openbsd.org7c856852018-03-03 03:15:51 +0000839 if (authoptsp != NULL)
840 *authoptsp = NULL;
841
Damien Miller09d3e122012-10-31 08:58:58 +1100842 /* Temporarily use the user's uid. */
843 temporarily_use_uid(pw);
844
845 debug("trying public key file %s", file);
846 if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) {
djm@openbsd.org7c856852018-03-03 03:15:51 +0000847 found_key = check_authkeys_file(ssh, pw, f, file,
848 key, authoptsp);
Damien Miller09d3e122012-10-31 08:58:58 +1100849 fclose(f);
850 }
851
852 restore_uid();
853 return found_key;
854}
855
856/*
857 * Checks whether key is allowed in output of command.
858 * returns 1 if the key is allowed or 0 otherwise.
859 */
860static int
djm@openbsd.org7c856852018-03-03 03:15:51 +0000861user_key_command_allowed2(struct ssh *ssh, struct passwd *user_pw,
862 struct sshkey *key, struct sshauthopt **authoptsp)
Damien Miller09d3e122012-10-31 08:58:58 +1100863{
djm@openbsd.org7c856852018-03-03 03:15:51 +0000864 struct passwd *runas_pw = NULL;
djm@openbsd.org24232a32015-05-21 06:38:35 +0000865 FILE *f = NULL;
866 int r, ok, found_key = 0;
djm@openbsd.org24232a32015-05-21 06:38:35 +0000867 int i, uid_swapped = 0, ac = 0;
Damien Miller09d3e122012-10-31 08:58:58 +1100868 pid_t pid;
djm@openbsd.org24232a32015-05-21 06:38:35 +0000869 char *username = NULL, *key_fp = NULL, *keytext = NULL;
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000870 char uidstr[32], *tmp, *command = NULL, **av = NULL;
djm@openbsd.org24232a32015-05-21 06:38:35 +0000871 void (*osigchld)(int);
Damien Miller09d3e122012-10-31 08:58:58 +1100872
djm@openbsd.org7c856852018-03-03 03:15:51 +0000873 if (authoptsp != NULL)
874 *authoptsp = NULL;
djm@openbsd.org24232a32015-05-21 06:38:35 +0000875 if (options.authorized_keys_command == NULL)
Damien Miller09d3e122012-10-31 08:58:58 +1100876 return 0;
Damien Millerd0d10992012-11-04 22:23:14 +1100877 if (options.authorized_keys_command_user == NULL) {
878 error("No user for AuthorizedKeysCommand specified, skipping");
879 return 0;
Damien Miller09d3e122012-10-31 08:58:58 +1100880 }
881
djm@openbsd.org24232a32015-05-21 06:38:35 +0000882 /*
883 * NB. all returns later this function should go via "out" to
884 * ensure the original SIGCHLD handler is restored properly.
885 */
886 osigchld = signal(SIGCHLD, SIG_DFL);
887
888 /* Prepare and verify the user for the command */
Damien Millerd0d10992012-11-04 22:23:14 +1100889 username = percent_expand(options.authorized_keys_command_user,
890 "u", user_pw->pw_name, (char *)NULL);
djm@openbsd.org7c856852018-03-03 03:15:51 +0000891 runas_pw = getpwnam(username);
892 if (runas_pw == NULL) {
Damien Miller4018dc02013-02-15 10:28:55 +1100893 error("AuthorizedKeysCommandUser \"%s\" not found: %s",
894 username, strerror(errno));
Damien Miller09d3e122012-10-31 08:58:58 +1100895 goto out;
896 }
897
djm@openbsd.org24232a32015-05-21 06:38:35 +0000898 /* Prepare AuthorizedKeysCommand */
899 if ((key_fp = sshkey_fingerprint(key, options.fingerprint_hash,
900 SSH_FP_DEFAULT)) == NULL) {
901 error("%s: sshkey_fingerprint failed", __func__);
902 goto out;
903 }
904 if ((r = sshkey_to_base64(key, &keytext)) != 0) {
905 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r));
Damien Miller09d3e122012-10-31 08:58:58 +1100906 goto out;
907 }
908
djm@openbsd.org24232a32015-05-21 06:38:35 +0000909 /* Turn the command into an argument vector */
djm@openbsd.orgde4ae072017-08-18 05:36:45 +0000910 if (argv_split(options.authorized_keys_command, &ac, &av) != 0) {
djm@openbsd.org24232a32015-05-21 06:38:35 +0000911 error("AuthorizedKeysCommand \"%s\" contains invalid quotes",
912 command);
913 goto out;
914 }
915 if (ac == 0) {
916 error("AuthorizedKeysCommand \"%s\" yielded no arguments",
917 command);
918 goto out;
919 }
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000920 snprintf(uidstr, sizeof(uidstr), "%llu",
921 (unsigned long long)user_pw->pw_uid);
djm@openbsd.org24232a32015-05-21 06:38:35 +0000922 for (i = 1; i < ac; i++) {
923 tmp = percent_expand(av[i],
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000924 "U", uidstr,
djm@openbsd.org24232a32015-05-21 06:38:35 +0000925 "u", user_pw->pw_name,
926 "h", user_pw->pw_dir,
927 "t", sshkey_ssh_name(key),
928 "f", key_fp,
929 "k", keytext,
930 (char *)NULL);
931 if (tmp == NULL)
932 fatal("%s: percent_expand failed", __func__);
933 free(av[i]);
934 av[i] = tmp;
935 }
936 /* Prepare a printable command for logs, etc. */
djm@openbsd.orgde4ae072017-08-18 05:36:45 +0000937 command = argv_assemble(ac, av);
Damien Miller09d3e122012-10-31 08:58:58 +1100938
939 /*
djm@openbsd.org24232a32015-05-21 06:38:35 +0000940 * If AuthorizedKeysCommand was run without arguments
941 * then fall back to the old behaviour of passing the
942 * target username as a single argument.
Damien Miller09d3e122012-10-31 08:58:58 +1100943 */
djm@openbsd.org24232a32015-05-21 06:38:35 +0000944 if (ac == 1) {
945 av = xreallocarray(av, ac + 2, sizeof(*av));
946 av[1] = xstrdup(user_pw->pw_name);
947 av[2] = NULL;
948 /* Fix up command too, since it is used in log messages */
949 free(command);
950 xasprintf(&command, "%s %s", av[0], av[1]);
Damien Miller09d3e122012-10-31 08:58:58 +1100951 }
952
djm@openbsd.org7c856852018-03-03 03:15:51 +0000953 if ((pid = subprocess("AuthorizedKeysCommand", runas_pw, command,
djm@openbsd.orgde4ae072017-08-18 05:36:45 +0000954 ac, av, &f,
955 SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD)) == 0)
djm@openbsd.org24232a32015-05-21 06:38:35 +0000956 goto out;
957
958 uid_swapped = 1;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000959 temporarily_use_uid(runas_pw);
Damien Miller09d3e122012-10-31 08:58:58 +1100960
djm@openbsd.org7c856852018-03-03 03:15:51 +0000961 ok = check_authkeys_file(ssh, user_pw, f,
962 options.authorized_keys_command, key, authoptsp);
Damien Miller09d3e122012-10-31 08:58:58 +1100963
djm@openbsd.orgddd3d342016-12-30 22:08:02 +0000964 fclose(f);
965 f = NULL;
966
djm@openbsd.orgb074c3c2017-08-18 05:48:04 +0000967 if (exited_cleanly(pid, "AuthorizedKeysCommand", command, 0) != 0)
Damien Miller09d3e122012-10-31 08:58:58 +1100968 goto out;
djm@openbsd.org24232a32015-05-21 06:38:35 +0000969
970 /* Read completed successfully */
Damien Miller09d3e122012-10-31 08:58:58 +1100971 found_key = ok;
972 out:
djm@openbsd.org24232a32015-05-21 06:38:35 +0000973 if (f != NULL)
974 fclose(f);
975 signal(SIGCHLD, osigchld);
976 for (i = 0; i < ac; i++)
977 free(av[i]);
978 free(av);
979 if (uid_swapped)
980 restore_uid();
981 free(command);
982 free(username);
983 free(key_fp);
984 free(keytext);
Damien Miller09d3e122012-10-31 08:58:58 +1100985 return found_key;
986}
987
988/*
989 * Check whether key authenticates and authorises the user.
990 */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000991int
djm@openbsd.org7c856852018-03-03 03:15:51 +0000992user_key_allowed(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
993 int auth_attempt, struct sshauthopt **authoptsp)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000994{
Damien Millerd8478b62011-05-29 21:39:36 +1000995 u_int success, i;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000996 char *file;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000997 struct sshauthopt *opts = NULL;
998 if (authoptsp != NULL)
999 *authoptsp = NULL;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001000
Damien Miller1aed65e2010-03-04 21:53:35 +11001001 if (auth_key_is_revoked(key))
1002 return 0;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001003 if (sshkey_is_cert(key) &&
1004 auth_key_is_revoked(key->cert->signature_key))
Damien Miller1aed65e2010-03-04 21:53:35 +11001005 return 0;
1006
djm@openbsd.org7c856852018-03-03 03:15:51 +00001007 if ((success = user_cert_trusted_ca(ssh, pw, key, &opts)) != 0)
1008 goto out;
1009 sshauthopt_free(opts);
1010 opts = NULL;
Damien Miller1aed65e2010-03-04 21:53:35 +11001011
djm@openbsd.org7c856852018-03-03 03:15:51 +00001012 if ((success = user_key_command_allowed2(ssh, pw, key, &opts)) != 0)
1013 goto out;
1014 sshauthopt_free(opts);
1015 opts = NULL;
Damien Miller09d3e122012-10-31 08:58:58 +11001016
Damien Millerd8478b62011-05-29 21:39:36 +10001017 for (i = 0; !success && i < options.num_authkeys_files; i++) {
Damien Miller09d3e122012-10-31 08:58:58 +11001018 if (strcasecmp(options.authorized_keys_files[i], "none") == 0)
1019 continue;
Damien Millerd8478b62011-05-29 21:39:36 +10001020 file = expand_authorized_keys(
1021 options.authorized_keys_files[i], pw);
djm@openbsd.org7c856852018-03-03 03:15:51 +00001022 success = user_key_allowed2(ssh, pw, key, file, &opts);
Darren Tuckera627d422013-06-02 07:31:17 +10001023 free(file);
Damien Millerd8478b62011-05-29 21:39:36 +10001024 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001025
djm@openbsd.org7c856852018-03-03 03:15:51 +00001026 out:
1027 if (success && authoptsp != NULL) {
1028 *authoptsp = opts;
1029 opts = NULL;
1030 }
1031 sshauthopt_free(opts);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001032 return success;
1033}
1034
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001035Authmethod method_pubkey = {
1036 "publickey",
1037 userauth_pubkey,
1038 &options.pubkey_authentication
1039};