blob: 2fb5950ea6088ff8754e3e024549fb91ecea2479 [file] [log] [blame]
djm@openbsd.org86e57372018-09-20 03:28:06 +00001/* $OpenBSD: auth2-pubkey.c,v 1.86 2018/09/20 03:28:06 djm 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 }
djm@openbsd.org86e57372018-09-20 03:28:06 +0000140 if ((r = sshkey_check_cert_sigtype(key,
141 options.ca_sign_algorithms)) != 0) {
142 logit("%s: certificate signature algorithm %s: %s", __func__,
143 (key->cert == NULL || key->cert->signature_type == NULL) ?
144 "(null)" : key->cert->signature_type, ssh_err(r));
145 goto done;
146 }
djm@openbsd.org27885632017-12-19 00:24:34 +0000147 key_s = format_key(key);
148 if (sshkey_is_cert(key))
149 ca_s = format_key(key->cert->signature_key);
150
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000151 if (have_sig) {
djm@openbsd.org27885632017-12-19 00:24:34 +0000152 debug3("%s: have %s signature for %s%s%s",
153 __func__, pkalg, key_s,
154 ca_s == NULL ? "" : " CA ",
155 ca_s == NULL ? "" : ca_s);
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000156 if ((r = sshpkt_get_string(ssh, &sig, &slen)) != 0 ||
157 (r = sshpkt_get_end(ssh)) != 0)
158 fatal("%s: %s", __func__, ssh_err(r));
159 if ((b = sshbuf_new()) == NULL)
160 fatal("%s: sshbuf_new failed", __func__);
161 if (ssh->compat & SSH_OLD_SESSIONID) {
162 if ((r = sshbuf_put(b, session_id2,
163 session_id2_len)) != 0)
164 fatal("%s: sshbuf_put session id: %s",
165 __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000166 } else {
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000167 if ((r = sshbuf_put_string(b, session_id2,
168 session_id2_len)) != 0)
169 fatal("%s: sshbuf_put_string session id: %s",
170 __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000171 }
djm@openbsd.org74287f52018-07-31 03:10:27 +0000172 if (!authctxt->valid || authctxt->user == NULL) {
173 debug2("%s: disabled because of invalid user",
174 __func__);
175 goto done;
176 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000177 /* reconstruct packet */
Damien Miller4ce189d2013-04-23 15:17:52 +1000178 xasprintf(&userstyle, "%s%s%s", authctxt->user,
179 authctxt->style ? ":" : "",
180 authctxt->style ? authctxt->style : "");
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000181 if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
182 (r = sshbuf_put_cstring(b, userstyle)) != 0 ||
djm@openbsd.org14b5c632018-01-23 05:27:21 +0000183 (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
184 (r = sshbuf_put_cstring(b, "publickey")) != 0 ||
185 (r = sshbuf_put_u8(b, have_sig)) != 0 ||
mestre@openbsd.orgdb8bb802018-08-28 12:25:53 +0000186 (r = sshbuf_put_cstring(b, pkalg)) != 0 ||
djm@openbsd.org14b5c632018-01-23 05:27:21 +0000187 (r = sshbuf_put_string(b, pkblob, blen)) != 0)
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000188 fatal("%s: build packet failed: %s",
189 __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000190#ifdef DEBUG_PK
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000191 sshbuf_dump(b, stderr);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000192#endif
193 /* test for correct signature */
194 authenticated = 0;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000195 if (PRIVSEP(user_key_allowed(ssh, pw, key, 1, &authopts)) &&
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000196 PRIVSEP(sshkey_verify(key, sig, slen,
197 sshbuf_ptr(b), sshbuf_len(b),
198 (ssh->compat & SSH_BUG_SIGTYPE) == 0 ? pkalg : NULL,
199 ssh->compat)) == 0) {
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000200 authenticated = 1;
djm@openbsd.orgf69b69b2014-12-22 07:51:30 +0000201 }
djm@openbsd.org8f574952017-06-24 06:34:38 +0000202 auth2_record_key(authctxt, authenticated, key);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000203 } else {
djm@openbsd.org27885632017-12-19 00:24:34 +0000204 debug("%s: test pkalg %s pkblob %s%s%s",
205 __func__, pkalg, key_s,
206 ca_s == NULL ? "" : " CA ",
207 ca_s == NULL ? "" : ca_s);
208
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000209 if ((r = sshpkt_get_end(ssh)) != 0)
210 fatal("%s: %s", __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000211
djm@openbsd.org74287f52018-07-31 03:10:27 +0000212 if (!authctxt->valid || authctxt->user == NULL) {
213 debug2("%s: disabled because of invalid user",
214 __func__);
215 goto done;
216 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000217 /* 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.org7c856852018-03-03 03:15:51 +0000225 if (PRIVSEP(user_key_allowed(ssh, pw, key, 0, NULL))) {
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 ||
markus@openbsd.org394a8422018-07-11 18:55:11 +0000230 (r = sshpkt_send(ssh)) != 0 ||
231 (r = ssh_packet_write_wait(ssh)) != 0)
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000232 fatal("%s: %s", __func__, ssh_err(r));
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000233 authctxt->postponed = 1;
234 }
235 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000236done:
djm@openbsd.org7c856852018-03-03 03:15:51 +0000237 if (authenticated == 1 && auth_activate_options(ssh, authopts) != 0) {
238 debug("%s: key options inconsistent with existing", __func__);
239 authenticated = 0;
240 }
djm@openbsd.orgebacd372016-01-27 00:53:12 +0000241 debug2("%s: authenticated %d pkalg %s", __func__, authenticated, pkalg);
djm@openbsd.org7c856852018-03-03 03:15:51 +0000242
djm@openbsd.org7fef1732018-08-23 03:01:08 +0000243 sshbuf_free(b);
djm@openbsd.org7c856852018-03-03 03:15:51 +0000244 sshauthopt_free(authopts);
djm@openbsd.org8f574952017-06-24 06:34:38 +0000245 sshkey_free(key);
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000246 free(userstyle);
Darren Tuckera627d422013-06-02 07:31:17 +1000247 free(pkalg);
248 free(pkblob);
djm@openbsd.org27885632017-12-19 00:24:34 +0000249 free(key_s);
250 free(ca_s);
djm@openbsd.org74287f52018-07-31 03:10:27 +0000251 free(sig);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000252 return authenticated;
253}
254
Damien Miller30da3442010-05-10 11:58:03 +1000255static int
Damien Miller86687062014-07-02 15:28:02 +1000256match_principals_option(const char *principal_list, struct sshkey_cert *cert)
Damien Miller30da3442010-05-10 11:58:03 +1000257{
258 char *result;
259 u_int i;
260
261 /* XXX percent_expand() sequences for authorized_principals? */
262
263 for (i = 0; i < cert->nprincipals; i++) {
264 if ((result = match_list(cert->principals[i],
265 principal_list, NULL)) != NULL) {
266 debug3("matched principal from key options \"%.100s\"",
267 result);
Darren Tuckera627d422013-06-02 07:31:17 +1000268 free(result);
Damien Miller30da3442010-05-10 11:58:03 +1000269 return 1;
270 }
271 }
272 return 0;
273}
274
djm@openbsd.org7c856852018-03-03 03:15:51 +0000275/*
276 * Process a single authorized_principals format line. Returns 0 and sets
277 * authoptsp is principal is authorised, -1 otherwise. "loc" is used as a
278 * log preamble for file/line information.
279 */
Damien Miller30da3442010-05-10 11:58:03 +1000280static int
djm@openbsd.org7c856852018-03-03 03:15:51 +0000281check_principals_line(struct ssh *ssh, char *cp, const struct sshkey_cert *cert,
282 const char *loc, struct sshauthopt **authoptsp)
Damien Miller30da3442010-05-10 11:58:03 +1000283{
djm@openbsd.org7c856852018-03-03 03:15:51 +0000284 u_int i, found = 0;
285 char *ep, *line_opts;
286 const char *reason = NULL;
287 struct sshauthopt *opts = NULL;
288
289 if (authoptsp != NULL)
290 *authoptsp = NULL;
291
292 /* Trim trailing whitespace. */
293 ep = cp + strlen(cp) - 1;
294 while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t'))
295 *ep-- = '\0';
296
297 /*
298 * If the line has internal whitespace then assume it has
299 * key options.
300 */
301 line_opts = NULL;
302 if ((ep = strrchr(cp, ' ')) != NULL ||
303 (ep = strrchr(cp, '\t')) != NULL) {
304 for (; *ep == ' ' || *ep == '\t'; ep++)
305 ;
306 line_opts = cp;
307 cp = ep;
308 }
309 if ((opts = sshauthopt_parse(line_opts, &reason)) == NULL) {
310 debug("%s: bad principals options: %s", loc, reason);
311 auth_debug_add("%s: bad principals options: %s", loc, reason);
312 return -1;
313 }
314 /* Check principals in cert against those on line */
315 for (i = 0; i < cert->nprincipals; i++) {
316 if (strcmp(cp, cert->principals[i]) != 0)
317 continue;
318 debug3("%s: matched principal \"%.100s\"",
319 loc, cert->principals[i]);
320 found = 1;
321 }
322 if (found && authoptsp != NULL) {
323 *authoptsp = opts;
324 opts = NULL;
325 }
326 sshauthopt_free(opts);
327 return found ? 0 : -1;
328}
329
330static int
331process_principals(struct ssh *ssh, FILE *f, const char *file,
332 const struct sshkey_cert *cert, struct sshauthopt **authoptsp)
333{
markus@openbsd.org7f906352018-06-06 18:29:18 +0000334 char loc[256], *line = NULL, *cp, *ep;
335 size_t linesize = 0;
Damien Miller30da3442010-05-10 11:58:03 +1000336 u_long linenum = 0;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000337 u_int found_principal = 0;
338
339 if (authoptsp != NULL)
340 *authoptsp = NULL;
Damien Miller30da3442010-05-10 11:58:03 +1000341
markus@openbsd.org7f906352018-06-06 18:29:18 +0000342 while (getline(&line, &linesize, f) != -1) {
343 linenum++;
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000344 /* Always consume entire input */
345 if (found_principal)
346 continue;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000347
Damien Miller6018a362010-07-02 13:35:19 +1000348 /* Skip leading whitespace. */
Damien Miller30da3442010-05-10 11:58:03 +1000349 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
350 ;
Damien Miller6018a362010-07-02 13:35:19 +1000351 /* Skip blank and comment lines. */
352 if ((ep = strchr(cp, '#')) != NULL)
353 *ep = '\0';
354 if (!*cp || *cp == '\n')
Damien Miller30da3442010-05-10 11:58:03 +1000355 continue;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000356
357 snprintf(loc, sizeof(loc), "%.200s:%lu", file, linenum);
358 if (check_principals_line(ssh, cp, cert, loc, authoptsp) == 0)
359 found_principal = 1;
Damien Miller30da3442010-05-10 11:58:03 +1000360 }
markus@openbsd.org7f906352018-06-06 18:29:18 +0000361 free(line);
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000362 return found_principal;
Damien Miller09d3e122012-10-31 08:58:58 +1100363}
Damien Miller30da3442010-05-10 11:58:03 +1000364
djm@openbsd.org7c856852018-03-03 03:15:51 +0000365/* XXX remove pw args here and elsewhere once ssh->authctxt is guaranteed */
366
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000367static int
djm@openbsd.org7c856852018-03-03 03:15:51 +0000368match_principals_file(struct ssh *ssh, struct passwd *pw, char *file,
369 struct sshkey_cert *cert, struct sshauthopt **authoptsp)
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000370{
371 FILE *f;
372 int success;
373
djm@openbsd.org7c856852018-03-03 03:15:51 +0000374 if (authoptsp != NULL)
375 *authoptsp = NULL;
376
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000377 temporarily_use_uid(pw);
378 debug("trying authorized principals file %s", file);
379 if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) {
380 restore_uid();
381 return 0;
382 }
djm@openbsd.org7c856852018-03-03 03:15:51 +0000383 success = process_principals(ssh, f, file, cert, authoptsp);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000384 fclose(f);
385 restore_uid();
386 return success;
387}
388
389/*
390 * Checks whether principal is allowed in output of command.
391 * returns 1 if the principal is allowed or 0 otherwise.
392 */
393static int
djm@openbsd.org7c856852018-03-03 03:15:51 +0000394match_principals_command(struct ssh *ssh, struct passwd *user_pw,
395 const struct sshkey *key, struct sshauthopt **authoptsp)
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000396{
djm@openbsd.org7c856852018-03-03 03:15:51 +0000397 struct passwd *runas_pw = NULL;
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000398 const struct sshkey_cert *cert = key->cert;
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000399 FILE *f = NULL;
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000400 int r, ok, found_principal = 0;
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000401 int i, ac = 0, uid_swapped = 0;
402 pid_t pid;
403 char *tmp, *username = NULL, *command = NULL, **av = NULL;
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000404 char *ca_fp = NULL, *key_fp = NULL, *catext = NULL, *keytext = NULL;
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000405 char serial_s[16], uidstr[32];
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000406 void (*osigchld)(int);
407
djm@openbsd.org7c856852018-03-03 03:15:51 +0000408 if (authoptsp != NULL)
409 *authoptsp = NULL;
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000410 if (options.authorized_principals_command == NULL)
411 return 0;
412 if (options.authorized_principals_command_user == NULL) {
413 error("No user for AuthorizedPrincipalsCommand specified, "
414 "skipping");
415 return 0;
416 }
417
418 /*
419 * NB. all returns later this function should go via "out" to
420 * ensure the original SIGCHLD handler is restored properly.
421 */
422 osigchld = signal(SIGCHLD, SIG_DFL);
423
424 /* Prepare and verify the user for the command */
425 username = percent_expand(options.authorized_principals_command_user,
426 "u", user_pw->pw_name, (char *)NULL);
djm@openbsd.org7c856852018-03-03 03:15:51 +0000427 runas_pw = getpwnam(username);
428 if (runas_pw == NULL) {
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000429 error("AuthorizedPrincipalsCommandUser \"%s\" not found: %s",
430 username, strerror(errno));
431 goto out;
432 }
433
434 /* Turn the command into an argument vector */
djm@openbsd.orgde4ae072017-08-18 05:36:45 +0000435 if (argv_split(options.authorized_principals_command, &ac, &av) != 0) {
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000436 error("AuthorizedPrincipalsCommand \"%s\" contains "
437 "invalid quotes", command);
438 goto out;
439 }
440 if (ac == 0) {
441 error("AuthorizedPrincipalsCommand \"%s\" yielded no arguments",
442 command);
443 goto out;
444 }
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000445 if ((ca_fp = sshkey_fingerprint(cert->signature_key,
446 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
447 error("%s: sshkey_fingerprint failed", __func__);
448 goto out;
449 }
djm@openbsd.org00df97f2016-09-14 20:11:26 +0000450 if ((key_fp = sshkey_fingerprint(key,
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000451 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
452 error("%s: sshkey_fingerprint failed", __func__);
453 goto out;
454 }
455 if ((r = sshkey_to_base64(cert->signature_key, &catext)) != 0) {
456 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r));
457 goto out;
458 }
459 if ((r = sshkey_to_base64(key, &keytext)) != 0) {
460 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r));
461 goto out;
462 }
djm@openbsd.orgf83a0cf2016-09-21 17:44:20 +0000463 snprintf(serial_s, sizeof(serial_s), "%llu",
464 (unsigned long long)cert->serial);
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000465 snprintf(uidstr, sizeof(uidstr), "%llu",
466 (unsigned long long)user_pw->pw_uid);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000467 for (i = 1; i < ac; i++) {
468 tmp = percent_expand(av[i],
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000469 "U", uidstr,
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000470 "u", user_pw->pw_name,
471 "h", user_pw->pw_dir,
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000472 "t", sshkey_ssh_name(key),
473 "T", sshkey_ssh_name(cert->signature_key),
474 "f", key_fp,
475 "F", ca_fp,
476 "k", keytext,
477 "K", catext,
djm@openbsd.orgbfa9d962016-09-21 01:34:45 +0000478 "i", cert->key_id,
479 "s", serial_s,
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000480 (char *)NULL);
481 if (tmp == NULL)
482 fatal("%s: percent_expand failed", __func__);
483 free(av[i]);
484 av[i] = tmp;
485 }
486 /* Prepare a printable command for logs, etc. */
djm@openbsd.orgde4ae072017-08-18 05:36:45 +0000487 command = argv_assemble(ac, av);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000488
djm@openbsd.org7c856852018-03-03 03:15:51 +0000489 if ((pid = subprocess("AuthorizedPrincipalsCommand", runas_pw, command,
djm@openbsd.orgde4ae072017-08-18 05:36:45 +0000490 ac, av, &f,
491 SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD)) == 0)
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000492 goto out;
493
494 uid_swapped = 1;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000495 temporarily_use_uid(runas_pw);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000496
djm@openbsd.org7c856852018-03-03 03:15:51 +0000497 ok = process_principals(ssh, f, "(command)", cert, authoptsp);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000498
djm@openbsd.orgddd3d342016-12-30 22:08:02 +0000499 fclose(f);
500 f = NULL;
501
djm@openbsd.orgb074c3c2017-08-18 05:48:04 +0000502 if (exited_cleanly(pid, "AuthorizedPrincipalsCommand", command, 0) != 0)
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000503 goto out;
504
505 /* Read completed successfully */
506 found_principal = ok;
507 out:
508 if (f != NULL)
509 fclose(f);
510 signal(SIGCHLD, osigchld);
511 for (i = 0; i < ac; i++)
512 free(av[i]);
513 free(av);
514 if (uid_swapped)
515 restore_uid();
516 free(command);
517 free(username);
djm@openbsd.orge7907c12016-09-14 05:42:25 +0000518 free(ca_fp);
519 free(key_fp);
520 free(catext);
521 free(keytext);
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000522 return found_principal;
523}
djm@openbsd.org7c856852018-03-03 03:15:51 +0000524
525static void
526skip_space(char **cpp)
527{
528 char *cp;
529
530 for (cp = *cpp; *cp == ' ' || *cp == '\t'; cp++)
531 ;
532 *cpp = cp;
533}
534
535/*
536 * Advanced *cpp past the end of key options, defined as the first unquoted
537 * whitespace character. Returns 0 on success or -1 on failure (e.g.
538 * unterminated quotes).
539 */
540static int
541advance_past_options(char **cpp)
542{
543 char *cp = *cpp;
544 int quoted = 0;
545
546 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
547 if (*cp == '\\' && cp[1] == '"')
548 cp++; /* Skip both */
549 else if (*cp == '"')
550 quoted = !quoted;
551 }
552 *cpp = cp;
553 /* return failure for unterminated quotes */
554 return (*cp == '\0' && quoted) ? -1 : 0;
555}
556
557/*
558 * Check a single line of an authorized_keys-format file. Returns 0 if key
559 * matches, -1 otherwise. Will return key/cert options via *authoptsp
560 * on success. "loc" is used as file/line location in log messages.
561 */
562static int
563check_authkey_line(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
564 char *cp, const char *loc, struct sshauthopt **authoptsp)
565{
566 int want_keytype = sshkey_is_cert(key) ? KEY_UNSPEC : key->type;
567 struct sshkey *found = NULL;
568 struct sshauthopt *keyopts = NULL, *certopts = NULL, *finalopts = NULL;
569 char *key_options = NULL, *fp = NULL;
570 const char *reason = NULL;
571 int ret = -1;
572
573 if (authoptsp != NULL)
574 *authoptsp = NULL;
575
576 if ((found = sshkey_new(want_keytype)) == NULL) {
577 debug3("%s: keytype %d failed", __func__, want_keytype);
578 goto out;
579 }
580
581 /* XXX djm: peek at key type in line and skip if unwanted */
582
583 if (sshkey_read(found, &cp) != 0) {
584 /* no key? check for options */
585 debug2("%s: check options: '%s'", loc, cp);
586 key_options = cp;
587 if (advance_past_options(&cp) != 0) {
588 reason = "invalid key option string";
589 goto fail_reason;
590 }
591 skip_space(&cp);
592 if (sshkey_read(found, &cp) != 0) {
593 /* still no key? advance to next line*/
594 debug2("%s: advance: '%s'", loc, cp);
595 goto out;
596 }
597 }
598 /* Parse key options now; we need to know if this is a CA key */
599 if ((keyopts = sshauthopt_parse(key_options, &reason)) == NULL) {
600 debug("%s: bad key options: %s", loc, reason);
601 auth_debug_add("%s: bad key options: %s", loc, reason);
602 goto out;
603 }
604 /* Ignore keys that don't match or incorrectly marked as CAs */
605 if (sshkey_is_cert(key)) {
606 /* Certificate; check signature key against CA */
607 if (!sshkey_equal(found, key->cert->signature_key) ||
608 !keyopts->cert_authority)
609 goto out;
610 } else {
611 /* Plain key: check it against key found in file */
612 if (!sshkey_equal(found, key) || keyopts->cert_authority)
613 goto out;
614 }
615
616 /* We have a candidate key, perform authorisation checks */
617 if ((fp = sshkey_fingerprint(found,
618 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
619 fatal("%s: fingerprint failed", __func__);
620
621 debug("%s: matching %s found: %s %s", loc,
622 sshkey_is_cert(key) ? "CA" : "key", sshkey_type(found), fp);
623
624 if (auth_authorise_keyopts(ssh, pw, keyopts,
625 sshkey_is_cert(key), loc) != 0) {
626 reason = "Refused by key options";
627 goto fail_reason;
628 }
629 /* That's all we need for plain keys. */
630 if (!sshkey_is_cert(key)) {
631 verbose("Accepted key %s %s found at %s",
632 sshkey_type(found), fp, loc);
633 finalopts = keyopts;
634 keyopts = NULL;
635 goto success;
636 }
637
638 /*
639 * Additional authorisation for certificates.
640 */
641
642 /* Parse and check options present in certificate */
643 if ((certopts = sshauthopt_from_cert(key)) == NULL) {
644 reason = "Invalid certificate options";
645 goto fail_reason;
646 }
647 if (auth_authorise_keyopts(ssh, pw, certopts, 0, loc) != 0) {
648 reason = "Refused by certificate options";
649 goto fail_reason;
650 }
651 if ((finalopts = sshauthopt_merge(keyopts, certopts, &reason)) == NULL)
652 goto fail_reason;
653
654 /*
655 * If the user has specified a list of principals as
656 * a key option, then prefer that list to matching
657 * their username in the certificate principals list.
658 */
659 if (keyopts->cert_principals != NULL &&
660 !match_principals_option(keyopts->cert_principals, key->cert)) {
661 reason = "Certificate does not contain an authorized principal";
662 goto fail_reason;
663 }
664 if (sshkey_cert_check_authority(key, 0, 0,
665 keyopts->cert_principals == NULL ? pw->pw_name : NULL, &reason) != 0)
666 goto fail_reason;
667
668 verbose("Accepted certificate ID \"%s\" (serial %llu) "
669 "signed by CA %s %s found at %s",
670 key->cert->key_id,
671 (unsigned long long)key->cert->serial,
672 sshkey_type(found), fp, loc);
673
674 success:
675 if (finalopts == NULL)
676 fatal("%s: internal error: missing options", __func__);
677 if (authoptsp != NULL) {
678 *authoptsp = finalopts;
679 finalopts = NULL;
680 }
681 /* success */
682 ret = 0;
683 goto out;
684
685 fail_reason:
686 error("%s", reason);
687 auth_debug_add("%s", reason);
688 out:
689 free(fp);
690 sshauthopt_free(keyopts);
691 sshauthopt_free(certopts);
692 sshauthopt_free(finalopts);
693 sshkey_free(found);
694 return ret;
695}
696
Damien Miller09d3e122012-10-31 08:58:58 +1100697/*
698 * Checks whether key is allowed in authorized_keys-format file,
699 * returns 1 if the key is allowed or 0 otherwise.
700 */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000701static int
djm@openbsd.org7c856852018-03-03 03:15:51 +0000702check_authkeys_file(struct ssh *ssh, struct passwd *pw, FILE *f,
703 char *file, struct sshkey *key, struct sshauthopt **authoptsp)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000704{
markus@openbsd.org7f906352018-06-06 18:29:18 +0000705 char *cp, *line = NULL, loc[256];
706 size_t linesize = 0;
Darren Tucker33c787f2008-07-02 22:37:30 +1000707 int found_key = 0;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000708 u_long linenum = 0;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000709
710 if (authoptsp != NULL)
711 *authoptsp = NULL;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000712
markus@openbsd.org7f906352018-06-06 18:29:18 +0000713 while (getline(&line, &linesize, f) != -1) {
714 linenum++;
djm@openbsd.orgabd59662017-09-07 23:48:09 +0000715 /* Always consume entire file */
djm@openbsd.org52763dd2017-01-30 01:03:00 +0000716 if (found_key)
717 continue;
Damien Miller0a80ca12010-02-27 07:55:05 +1100718
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000719 /* Skip leading whitespace, empty and comment lines. */
djm@openbsd.org7c856852018-03-03 03:15:51 +0000720 cp = line;
721 skip_space(&cp);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000722 if (!*cp || *cp == '\n' || *cp == '#')
723 continue;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000724 snprintf(loc, sizeof(loc), "%.200s:%lu", file, linenum);
725 if (check_authkey_line(ssh, pw, key, cp, loc, authoptsp) == 0)
Damien Miller0a80ca12010-02-27 07:55:05 +1100726 found_key = 1;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000727 }
markus@openbsd.org7f906352018-06-06 18:29:18 +0000728 free(line);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000729 return found_key;
730}
731
Damien Miller1aed65e2010-03-04 21:53:35 +1100732/* Authenticate a certificate key against TrustedUserCAKeys */
733static int
djm@openbsd.org7c856852018-03-03 03:15:51 +0000734user_cert_trusted_ca(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
735 struct sshauthopt **authoptsp)
Damien Miller1aed65e2010-03-04 21:53:35 +1100736{
Damien Miller30da3442010-05-10 11:58:03 +1000737 char *ca_fp, *principals_file = NULL;
Damien Miller1aed65e2010-03-04 21:53:35 +1100738 const char *reason;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000739 struct sshauthopt *principals_opts = NULL, *cert_opts = NULL;
740 struct sshauthopt *final_opts = NULL;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000741 int r, ret = 0, found_principal = 0, use_authorized_principals;
Damien Miller1aed65e2010-03-04 21:53:35 +1100742
djm@openbsd.org7c856852018-03-03 03:15:51 +0000743 if (authoptsp != NULL)
744 *authoptsp = NULL;
745
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000746 if (!sshkey_is_cert(key) || options.trusted_user_ca_keys == NULL)
Damien Miller1aed65e2010-03-04 21:53:35 +1100747 return 0;
748
djm@openbsd.org9ce86c92015-01-28 22:36:00 +0000749 if ((ca_fp = sshkey_fingerprint(key->cert->signature_key,
750 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
751 return 0;
Damien Miller1aed65e2010-03-04 21:53:35 +1100752
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000753 if ((r = sshkey_in_file(key->cert->signature_key,
754 options.trusted_user_ca_keys, 1, 0)) != 0) {
755 debug2("%s: CA %s %s is not listed in %s: %s", __func__,
756 sshkey_type(key->cert->signature_key), ca_fp,
757 options.trusted_user_ca_keys, ssh_err(r));
Damien Miller1aed65e2010-03-04 21:53:35 +1100758 goto out;
759 }
Damien Miller30da3442010-05-10 11:58:03 +1000760 /*
761 * If AuthorizedPrincipals is in use, then compare the certificate
762 * principals against the names in that file rather than matching
763 * against the username.
764 */
765 if ((principals_file = authorized_principals_file(pw)) != NULL) {
djm@openbsd.org7c856852018-03-03 03:15:51 +0000766 if (match_principals_file(ssh, pw, principals_file,
767 key->cert, &principals_opts))
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000768 found_principal = 1;
769 }
770 /* Try querying command if specified */
djm@openbsd.org7c856852018-03-03 03:15:51 +0000771 if (!found_principal && match_principals_command(ssh, pw, key,
772 &principals_opts))
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000773 found_principal = 1;
jsing@openbsd.org596dbca2015-06-15 18:44:22 +0000774 /* If principals file or command is specified, then require a match */
775 use_authorized_principals = principals_file != NULL ||
776 options.authorized_principals_command != NULL;
777 if (!found_principal && use_authorized_principals) {
djm@openbsd.orgbcc50d82015-05-21 06:43:30 +0000778 reason = "Certificate does not contain an authorized principal";
djm@openbsd.org7c856852018-03-03 03:15:51 +0000779 goto fail_reason;
Damien Miller1aed65e2010-03-04 21:53:35 +1100780 }
djm@openbsd.org7c856852018-03-03 03:15:51 +0000781 if (use_authorized_principals && principals_opts == NULL)
782 fatal("%s: internal error: missing principals_opts", __func__);
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000783 if (sshkey_cert_check_authority(key, 0, 1,
jsing@openbsd.org596dbca2015-06-15 18:44:22 +0000784 use_authorized_principals ? NULL : pw->pw_name, &reason) != 0)
Damien Miller30da3442010-05-10 11:58:03 +1000785 goto fail_reason;
Damien Miller1aed65e2010-03-04 21:53:35 +1100786
djm@openbsd.org7c856852018-03-03 03:15:51 +0000787 /* Check authority from options in key and from principals file/cmd */
788 if ((cert_opts = sshauthopt_from_cert(key)) == NULL) {
789 reason = "Invalid certificate options";
790 goto fail_reason;
791 }
792 if (auth_authorise_keyopts(ssh, pw, cert_opts, 0, "cert") != 0) {
793 reason = "Refused by certificate options";
794 goto fail_reason;
795 }
796 if (principals_opts == NULL) {
797 final_opts = cert_opts;
798 cert_opts = NULL;
799 } else {
800 if (auth_authorise_keyopts(ssh, pw, principals_opts, 0,
801 "principals") != 0) {
802 reason = "Refused by certificate principals options";
803 goto fail_reason;
804 }
805 if ((final_opts = sshauthopt_merge(principals_opts,
806 cert_opts, &reason)) == NULL) {
807 fail_reason:
808 error("%s", reason);
809 auth_debug_add("%s", reason);
810 goto out;
811 }
812 }
813
814 /* Success */
djm@openbsd.org63d18812015-10-27 01:44:45 +0000815 verbose("Accepted certificate ID \"%s\" (serial %llu) signed by "
816 "%s CA %s via %s", key->cert->key_id,
817 (unsigned long long)key->cert->serial,
markus@openbsd.org00ed75c2017-05-30 14:10:53 +0000818 sshkey_type(key->cert->signature_key), ca_fp,
Damien Millere513a912010-03-22 05:51:21 +1100819 options.trusted_user_ca_keys);
djm@openbsd.org7c856852018-03-03 03:15:51 +0000820 if (authoptsp != NULL) {
821 *authoptsp = final_opts;
822 final_opts = NULL;
823 }
Damien Miller1aed65e2010-03-04 21:53:35 +1100824 ret = 1;
Damien Miller1aed65e2010-03-04 21:53:35 +1100825 out:
djm@openbsd.org7c856852018-03-03 03:15:51 +0000826 sshauthopt_free(principals_opts);
827 sshauthopt_free(cert_opts);
828 sshauthopt_free(final_opts);
Darren Tuckera627d422013-06-02 07:31:17 +1000829 free(principals_file);
830 free(ca_fp);
Damien Miller1aed65e2010-03-04 21:53:35 +1100831 return ret;
832}
833
Damien Miller09d3e122012-10-31 08:58:58 +1100834/*
835 * Checks whether key is allowed in file.
836 * returns 1 if the key is allowed or 0 otherwise.
837 */
838static int
djm@openbsd.org7c856852018-03-03 03:15:51 +0000839user_key_allowed2(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
840 char *file, struct sshauthopt **authoptsp)
Damien Miller09d3e122012-10-31 08:58:58 +1100841{
842 FILE *f;
843 int found_key = 0;
844
djm@openbsd.org7c856852018-03-03 03:15:51 +0000845 if (authoptsp != NULL)
846 *authoptsp = NULL;
847
Damien Miller09d3e122012-10-31 08:58:58 +1100848 /* Temporarily use the user's uid. */
849 temporarily_use_uid(pw);
850
851 debug("trying public key file %s", file);
852 if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) {
djm@openbsd.org7c856852018-03-03 03:15:51 +0000853 found_key = check_authkeys_file(ssh, pw, f, file,
854 key, authoptsp);
Damien Miller09d3e122012-10-31 08:58:58 +1100855 fclose(f);
856 }
857
858 restore_uid();
859 return found_key;
860}
861
862/*
863 * Checks whether key is allowed in output of command.
864 * returns 1 if the key is allowed or 0 otherwise.
865 */
866static int
djm@openbsd.org7c856852018-03-03 03:15:51 +0000867user_key_command_allowed2(struct ssh *ssh, struct passwd *user_pw,
868 struct sshkey *key, struct sshauthopt **authoptsp)
Damien Miller09d3e122012-10-31 08:58:58 +1100869{
djm@openbsd.org7c856852018-03-03 03:15:51 +0000870 struct passwd *runas_pw = NULL;
djm@openbsd.org24232a32015-05-21 06:38:35 +0000871 FILE *f = NULL;
872 int r, ok, found_key = 0;
djm@openbsd.org24232a32015-05-21 06:38:35 +0000873 int i, uid_swapped = 0, ac = 0;
Damien Miller09d3e122012-10-31 08:58:58 +1100874 pid_t pid;
djm@openbsd.org24232a32015-05-21 06:38:35 +0000875 char *username = NULL, *key_fp = NULL, *keytext = NULL;
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000876 char uidstr[32], *tmp, *command = NULL, **av = NULL;
djm@openbsd.org24232a32015-05-21 06:38:35 +0000877 void (*osigchld)(int);
Damien Miller09d3e122012-10-31 08:58:58 +1100878
djm@openbsd.org7c856852018-03-03 03:15:51 +0000879 if (authoptsp != NULL)
880 *authoptsp = NULL;
djm@openbsd.org24232a32015-05-21 06:38:35 +0000881 if (options.authorized_keys_command == NULL)
Damien Miller09d3e122012-10-31 08:58:58 +1100882 return 0;
Damien Millerd0d10992012-11-04 22:23:14 +1100883 if (options.authorized_keys_command_user == NULL) {
884 error("No user for AuthorizedKeysCommand specified, skipping");
885 return 0;
Damien Miller09d3e122012-10-31 08:58:58 +1100886 }
887
djm@openbsd.org24232a32015-05-21 06:38:35 +0000888 /*
889 * NB. all returns later this function should go via "out" to
890 * ensure the original SIGCHLD handler is restored properly.
891 */
892 osigchld = signal(SIGCHLD, SIG_DFL);
893
894 /* Prepare and verify the user for the command */
Damien Millerd0d10992012-11-04 22:23:14 +1100895 username = percent_expand(options.authorized_keys_command_user,
896 "u", user_pw->pw_name, (char *)NULL);
djm@openbsd.org7c856852018-03-03 03:15:51 +0000897 runas_pw = getpwnam(username);
898 if (runas_pw == NULL) {
Damien Miller4018dc02013-02-15 10:28:55 +1100899 error("AuthorizedKeysCommandUser \"%s\" not found: %s",
900 username, strerror(errno));
Damien Miller09d3e122012-10-31 08:58:58 +1100901 goto out;
902 }
903
djm@openbsd.org24232a32015-05-21 06:38:35 +0000904 /* Prepare AuthorizedKeysCommand */
905 if ((key_fp = sshkey_fingerprint(key, options.fingerprint_hash,
906 SSH_FP_DEFAULT)) == NULL) {
907 error("%s: sshkey_fingerprint failed", __func__);
908 goto out;
909 }
910 if ((r = sshkey_to_base64(key, &keytext)) != 0) {
911 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r));
Damien Miller09d3e122012-10-31 08:58:58 +1100912 goto out;
913 }
914
djm@openbsd.org24232a32015-05-21 06:38:35 +0000915 /* Turn the command into an argument vector */
djm@openbsd.orgde4ae072017-08-18 05:36:45 +0000916 if (argv_split(options.authorized_keys_command, &ac, &av) != 0) {
djm@openbsd.org24232a32015-05-21 06:38:35 +0000917 error("AuthorizedKeysCommand \"%s\" contains invalid quotes",
918 command);
919 goto out;
920 }
921 if (ac == 0) {
922 error("AuthorizedKeysCommand \"%s\" yielded no arguments",
923 command);
924 goto out;
925 }
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000926 snprintf(uidstr, sizeof(uidstr), "%llu",
927 (unsigned long long)user_pw->pw_uid);
djm@openbsd.org24232a32015-05-21 06:38:35 +0000928 for (i = 1; i < ac; i++) {
929 tmp = percent_expand(av[i],
djm@openbsd.org9c935dd2018-06-01 03:33:53 +0000930 "U", uidstr,
djm@openbsd.org24232a32015-05-21 06:38:35 +0000931 "u", user_pw->pw_name,
932 "h", user_pw->pw_dir,
933 "t", sshkey_ssh_name(key),
934 "f", key_fp,
935 "k", keytext,
936 (char *)NULL);
937 if (tmp == NULL)
938 fatal("%s: percent_expand failed", __func__);
939 free(av[i]);
940 av[i] = tmp;
941 }
942 /* Prepare a printable command for logs, etc. */
djm@openbsd.orgde4ae072017-08-18 05:36:45 +0000943 command = argv_assemble(ac, av);
Damien Miller09d3e122012-10-31 08:58:58 +1100944
945 /*
djm@openbsd.org24232a32015-05-21 06:38:35 +0000946 * If AuthorizedKeysCommand was run without arguments
947 * then fall back to the old behaviour of passing the
948 * target username as a single argument.
Damien Miller09d3e122012-10-31 08:58:58 +1100949 */
djm@openbsd.org24232a32015-05-21 06:38:35 +0000950 if (ac == 1) {
951 av = xreallocarray(av, ac + 2, sizeof(*av));
952 av[1] = xstrdup(user_pw->pw_name);
953 av[2] = NULL;
954 /* Fix up command too, since it is used in log messages */
955 free(command);
956 xasprintf(&command, "%s %s", av[0], av[1]);
Damien Miller09d3e122012-10-31 08:58:58 +1100957 }
958
djm@openbsd.org7c856852018-03-03 03:15:51 +0000959 if ((pid = subprocess("AuthorizedKeysCommand", runas_pw, command,
djm@openbsd.orgde4ae072017-08-18 05:36:45 +0000960 ac, av, &f,
961 SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD)) == 0)
djm@openbsd.org24232a32015-05-21 06:38:35 +0000962 goto out;
963
964 uid_swapped = 1;
djm@openbsd.org7c856852018-03-03 03:15:51 +0000965 temporarily_use_uid(runas_pw);
Damien Miller09d3e122012-10-31 08:58:58 +1100966
djm@openbsd.org7c856852018-03-03 03:15:51 +0000967 ok = check_authkeys_file(ssh, user_pw, f,
968 options.authorized_keys_command, key, authoptsp);
Damien Miller09d3e122012-10-31 08:58:58 +1100969
djm@openbsd.orgddd3d342016-12-30 22:08:02 +0000970 fclose(f);
971 f = NULL;
972
djm@openbsd.orgb074c3c2017-08-18 05:48:04 +0000973 if (exited_cleanly(pid, "AuthorizedKeysCommand", command, 0) != 0)
Damien Miller09d3e122012-10-31 08:58:58 +1100974 goto out;
djm@openbsd.org24232a32015-05-21 06:38:35 +0000975
976 /* Read completed successfully */
Damien Miller09d3e122012-10-31 08:58:58 +1100977 found_key = ok;
978 out:
djm@openbsd.org24232a32015-05-21 06:38:35 +0000979 if (f != NULL)
980 fclose(f);
981 signal(SIGCHLD, osigchld);
982 for (i = 0; i < ac; i++)
983 free(av[i]);
984 free(av);
985 if (uid_swapped)
986 restore_uid();
987 free(command);
988 free(username);
989 free(key_fp);
990 free(keytext);
Damien Miller09d3e122012-10-31 08:58:58 +1100991 return found_key;
992}
993
994/*
995 * Check whether key authenticates and authorises the user.
996 */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000997int
djm@openbsd.org7c856852018-03-03 03:15:51 +0000998user_key_allowed(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
999 int auth_attempt, struct sshauthopt **authoptsp)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001000{
Damien Millerd8478b62011-05-29 21:39:36 +10001001 u_int success, i;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001002 char *file;
djm@openbsd.org7c856852018-03-03 03:15:51 +00001003 struct sshauthopt *opts = NULL;
1004 if (authoptsp != NULL)
1005 *authoptsp = NULL;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001006
Damien Miller1aed65e2010-03-04 21:53:35 +11001007 if (auth_key_is_revoked(key))
1008 return 0;
markus@openbsd.org00ed75c2017-05-30 14:10:53 +00001009 if (sshkey_is_cert(key) &&
1010 auth_key_is_revoked(key->cert->signature_key))
Damien Miller1aed65e2010-03-04 21:53:35 +11001011 return 0;
1012
djm@openbsd.org7c856852018-03-03 03:15:51 +00001013 if ((success = user_cert_trusted_ca(ssh, pw, key, &opts)) != 0)
1014 goto out;
1015 sshauthopt_free(opts);
1016 opts = NULL;
Damien Miller1aed65e2010-03-04 21:53:35 +11001017
djm@openbsd.org7c856852018-03-03 03:15:51 +00001018 if ((success = user_key_command_allowed2(ssh, pw, key, &opts)) != 0)
1019 goto out;
1020 sshauthopt_free(opts);
1021 opts = NULL;
Damien Miller09d3e122012-10-31 08:58:58 +11001022
Damien Millerd8478b62011-05-29 21:39:36 +10001023 for (i = 0; !success && i < options.num_authkeys_files; i++) {
Damien Miller09d3e122012-10-31 08:58:58 +11001024 if (strcasecmp(options.authorized_keys_files[i], "none") == 0)
1025 continue;
Damien Millerd8478b62011-05-29 21:39:36 +10001026 file = expand_authorized_keys(
1027 options.authorized_keys_files[i], pw);
djm@openbsd.org7c856852018-03-03 03:15:51 +00001028 success = user_key_allowed2(ssh, pw, key, file, &opts);
Darren Tuckera627d422013-06-02 07:31:17 +10001029 free(file);
Damien Millerd8478b62011-05-29 21:39:36 +10001030 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001031
djm@openbsd.org7c856852018-03-03 03:15:51 +00001032 out:
1033 if (success && authoptsp != NULL) {
1034 *authoptsp = opts;
1035 opts = NULL;
1036 }
1037 sshauthopt_free(opts);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001038 return success;
1039}
1040
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001041Authmethod method_pubkey = {
1042 "publickey",
1043 userauth_pubkey,
1044 &options.pubkey_authentication
1045};