blob: 45306f839b0a58f13c31f3ca09559c72a8a95e78 [file] [log] [blame]
Darren Tucker74836ae2013-06-02 07:32:00 +10001/* $OpenBSD: auth2-pubkey.c,v 1.37 2013/05/19 02:38:28 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>
Damien Miller09d3e122012-10-31 08:58:58 +110030#include <sys/wait.h>
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000031
Damien Miller09d3e122012-10-31 08:58:58 +110032#include <errno.h>
Darren Tucker06db5842008-06-13 14:51:28 +100033#include <fcntl.h>
Darren Tucker737f7af2012-11-05 17:07:43 +110034#ifdef HAVE_PATHS_H
35# include <paths.h>
36#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100037#include <pwd.h>
Damien Miller09d3e122012-10-31 08:58:58 +110038#include <signal.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100039#include <stdio.h>
Damien Millerd7834352006-08-05 12:39:39 +100040#include <stdarg.h>
Damien Miller0a80ca12010-02-27 07:55:05 +110041#include <string.h>
42#include <time.h>
Darren Tuckerd9526a52008-06-14 09:01:24 +100043#include <unistd.h>
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"
49#include "buffer.h"
50#include "log.h"
51#include "servconf.h"
52#include "compat.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000053#include "key.h"
Damien Millerd7834352006-08-05 12:39:39 +100054#include "hostfile.h"
55#include "auth.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000056#include "pathnames.h"
57#include "uidswap.h"
58#include "auth-options.h"
59#include "canohost.h"
Damien Millerd7834352006-08-05 12:39:39 +100060#ifdef GSSAPI
61#include "ssh-gss.h"
62#endif
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000063#include "monitor_wrap.h"
Darren Tuckerf0f90982004-12-11 13:39:50 +110064#include "misc.h"
Damien Miller1aed65e2010-03-04 21:53:35 +110065#include "authfile.h"
Damien Miller30da3442010-05-10 11:58:03 +100066#include "match.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000067
68/* import */
69extern ServerOptions options;
70extern u_char *session_id2;
Darren Tucker502d3842003-06-28 12:38:01 +100071extern u_int session_id2_len;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000072
73static int
74userauth_pubkey(Authctxt *authctxt)
75{
76 Buffer b;
77 Key *key = NULL;
Damien Miller4ce189d2013-04-23 15:17:52 +100078 char *pkalg, *userstyle;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000079 u_char *pkblob, *sig;
80 u_int alen, blen, slen;
81 int have_sig, pktype;
82 int authenticated = 0;
83
84 if (!authctxt->valid) {
85 debug2("userauth_pubkey: disabled because of invalid user");
86 return 0;
87 }
88 have_sig = packet_get_char();
89 if (datafellows & SSH_BUG_PKAUTH) {
90 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
91 /* no explicit pkalg given */
92 pkblob = packet_get_string(&blen);
93 buffer_init(&b);
94 buffer_append(&b, pkblob, blen);
95 /* so we have to extract the pkalg from the pkblob */
96 pkalg = buffer_get_string(&b, &alen);
97 buffer_free(&b);
98 } else {
99 pkalg = packet_get_string(&alen);
100 pkblob = packet_get_string(&blen);
101 }
102 pktype = key_type_from_name(pkalg);
103 if (pktype == KEY_UNSPEC) {
104 /* this is perfectly legal */
Damien Miller996acd22003-04-09 20:59:48 +1000105 logit("userauth_pubkey: unsupported public key algorithm: %s",
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000106 pkalg);
107 goto done;
108 }
109 key = key_from_blob(pkblob, blen);
110 if (key == NULL) {
111 error("userauth_pubkey: cannot decode key: %s", pkalg);
112 goto done;
113 }
114 if (key->type != pktype) {
115 error("userauth_pubkey: type mismatch for decoded key "
116 "(received %d, expected %d)", key->type, pktype);
117 goto done;
118 }
119 if (have_sig) {
120 sig = packet_get_string(&slen);
121 packet_check_eom();
122 buffer_init(&b);
123 if (datafellows & SSH_OLD_SESSIONID) {
124 buffer_append(&b, session_id2, session_id2_len);
125 } else {
126 buffer_put_string(&b, session_id2, session_id2_len);
127 }
128 /* reconstruct packet */
129 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
Damien Miller4ce189d2013-04-23 15:17:52 +1000130 xasprintf(&userstyle, "%s%s%s", authctxt->user,
131 authctxt->style ? ":" : "",
132 authctxt->style ? authctxt->style : "");
133 buffer_put_cstring(&b, userstyle);
134 free(userstyle);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000135 buffer_put_cstring(&b,
136 datafellows & SSH_BUG_PKSERVICE ?
137 "ssh-userauth" :
138 authctxt->service);
139 if (datafellows & SSH_BUG_PKAUTH) {
140 buffer_put_char(&b, have_sig);
141 } else {
142 buffer_put_cstring(&b, "publickey");
143 buffer_put_char(&b, have_sig);
144 buffer_put_cstring(&b, pkalg);
145 }
146 buffer_put_string(&b, pkblob, blen);
147#ifdef DEBUG_PK
148 buffer_dump(&b);
149#endif
Darren Tucker74836ae2013-06-02 07:32:00 +1000150 pubkey_auth_info(authctxt, key);
151
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000152 /* test for correct signature */
153 authenticated = 0;
154 if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
155 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
Damien Millerfb1310e2004-01-21 11:02:50 +1100156 buffer_len(&b))) == 1)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000157 authenticated = 1;
Damien Millerfb1310e2004-01-21 11:02:50 +1100158 buffer_free(&b);
Darren Tuckera627d422013-06-02 07:31:17 +1000159 free(sig);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000160 } else {
161 debug("test whether pkalg/pkblob are acceptable");
162 packet_check_eom();
163
164 /* XXX fake reply and always send PK_OK ? */
165 /*
166 * XXX this allows testing whether a user is allowed
167 * to login: if you happen to have a valid pubkey this
168 * message is sent. the message is NEVER sent at all
169 * if a user is not allowed to login. is this an
170 * issue? -markus
171 */
172 if (PRIVSEP(user_key_allowed(authctxt->pw, key))) {
173 packet_start(SSH2_MSG_USERAUTH_PK_OK);
174 packet_put_string(pkalg, alen);
175 packet_put_string(pkblob, blen);
176 packet_send();
177 packet_write_wait();
178 authctxt->postponed = 1;
179 }
180 }
181 if (authenticated != 1)
182 auth_clear_options();
183done:
184 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
185 if (key != NULL)
186 key_free(key);
Darren Tuckera627d422013-06-02 07:31:17 +1000187 free(pkalg);
188 free(pkblob);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000189 return authenticated;
190}
191
Darren Tucker74836ae2013-06-02 07:32:00 +1000192void
193pubkey_auth_info(Authctxt *authctxt, const Key *key)
194{
195 char *fp;
196
197 if (key_is_cert(key)) {
198 fp = key_fingerprint(key->cert->signature_key,
199 SSH_FP_MD5, SSH_FP_HEX);
200 auth_info(authctxt, "%s ID %s (serial %llu) CA %s %s",
201 key_type(key), key->cert->key_id,
202 (unsigned long long)key->cert->serial,
203 key_type(key->cert->signature_key), fp);
204 free(fp);
205 } else {
206 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
207 auth_info(authctxt, "%s %s", key_type(key), fp);
208 free(fp);
209 }
210}
211
Damien Miller30da3442010-05-10 11:58:03 +1000212static int
213match_principals_option(const char *principal_list, struct KeyCert *cert)
214{
215 char *result;
216 u_int i;
217
218 /* XXX percent_expand() sequences for authorized_principals? */
219
220 for (i = 0; i < cert->nprincipals; i++) {
221 if ((result = match_list(cert->principals[i],
222 principal_list, NULL)) != NULL) {
223 debug3("matched principal from key options \"%.100s\"",
224 result);
Darren Tuckera627d422013-06-02 07:31:17 +1000225 free(result);
Damien Miller30da3442010-05-10 11:58:03 +1000226 return 1;
227 }
228 }
229 return 0;
230}
231
232static int
Damien Miller6018a362010-07-02 13:35:19 +1000233match_principals_file(char *file, struct passwd *pw, struct KeyCert *cert)
Damien Miller30da3442010-05-10 11:58:03 +1000234{
235 FILE *f;
Damien Miller6018a362010-07-02 13:35:19 +1000236 char line[SSH_MAX_PUBKEY_BYTES], *cp, *ep, *line_opts;
Damien Miller30da3442010-05-10 11:58:03 +1000237 u_long linenum = 0;
238 u_int i;
239
240 temporarily_use_uid(pw);
241 debug("trying authorized principals file %s", file);
242 if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) {
243 restore_uid();
244 return 0;
245 }
246 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
Damien Miller6018a362010-07-02 13:35:19 +1000247 /* Skip leading whitespace. */
Damien Miller30da3442010-05-10 11:58:03 +1000248 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
249 ;
Damien Miller6018a362010-07-02 13:35:19 +1000250 /* Skip blank and comment lines. */
251 if ((ep = strchr(cp, '#')) != NULL)
252 *ep = '\0';
253 if (!*cp || *cp == '\n')
Damien Miller30da3442010-05-10 11:58:03 +1000254 continue;
Damien Miller6018a362010-07-02 13:35:19 +1000255 /* Trim trailing whitespace. */
256 ep = cp + strlen(cp) - 1;
257 while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t'))
258 *ep-- = '\0';
259 /*
260 * If the line has internal whitespace then assume it has
261 * key options.
262 */
263 line_opts = NULL;
264 if ((ep = strrchr(cp, ' ')) != NULL ||
265 (ep = strrchr(cp, '\t')) != NULL) {
266 for (; *ep == ' ' || *ep == '\t'; ep++)
Damien Miller188ea812010-12-01 11:50:14 +1100267 ;
Damien Miller6018a362010-07-02 13:35:19 +1000268 line_opts = cp;
269 cp = ep;
270 }
Damien Miller30da3442010-05-10 11:58:03 +1000271 for (i = 0; i < cert->nprincipals; i++) {
272 if (strcmp(cp, cert->principals[i]) == 0) {
Darren Tuckeraf1a60e2011-10-02 18:59:59 +1100273 debug3("matched principal \"%.100s\" "
274 "from file \"%s\" on line %lu",
Damien Miller09d3e122012-10-31 08:58:58 +1100275 cert->principals[i], file, linenum);
Damien Miller6018a362010-07-02 13:35:19 +1000276 if (auth_parse_options(pw, line_opts,
277 file, linenum) != 1)
278 continue;
Damien Miller30da3442010-05-10 11:58:03 +1000279 fclose(f);
280 restore_uid();
281 return 1;
282 }
283 }
284 }
285 fclose(f);
286 restore_uid();
287 return 0;
Damien Miller09d3e122012-10-31 08:58:58 +1100288}
Damien Miller30da3442010-05-10 11:58:03 +1000289
Damien Miller09d3e122012-10-31 08:58:58 +1100290/*
291 * Checks whether key is allowed in authorized_keys-format file,
292 * returns 1 if the key is allowed or 0 otherwise.
293 */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000294static int
Damien Miller09d3e122012-10-31 08:58:58 +1100295check_authkeys_file(FILE *f, char *file, Key* key, struct passwd *pw)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000296{
Darren Tucker22cc7412004-12-06 22:47:41 +1100297 char line[SSH_MAX_PUBKEY_BYTES];
Damien Miller0a80ca12010-02-27 07:55:05 +1100298 const char *reason;
Darren Tucker33c787f2008-07-02 22:37:30 +1000299 int found_key = 0;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000300 u_long linenum = 0;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000301 Key *found;
302 char *fp;
303
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000304 found_key = 0;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000305
Darren Tucker74836ae2013-06-02 07:32:00 +1000306 found = NULL;
Darren Tucker22cc7412004-12-06 22:47:41 +1100307 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000308 char *cp, *key_options = NULL;
Darren Tucker74836ae2013-06-02 07:32:00 +1000309 if (found != NULL)
310 key_free(found);
311 found = key_new(key_is_cert(key) ? KEY_UNSPEC : key->type);
Damien Miller0a80ca12010-02-27 07:55:05 +1100312 auth_clear_options();
313
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000314 /* Skip leading whitespace, empty and comment lines. */
315 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
316 ;
317 if (!*cp || *cp == '\n' || *cp == '#')
318 continue;
319
320 if (key_read(found, &cp) != 1) {
321 /* no key? check if there are options for this key */
322 int quoted = 0;
323 debug2("user_key_allowed: check options: '%s'", cp);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000324 key_options = cp;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000325 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
326 if (*cp == '\\' && cp[1] == '"')
327 cp++; /* Skip both */
328 else if (*cp == '"')
329 quoted = !quoted;
330 }
331 /* Skip remaining whitespace. */
332 for (; *cp == ' ' || *cp == '\t'; cp++)
333 ;
334 if (key_read(found, &cp) != 1) {
335 debug2("user_key_allowed: advance: '%s'", cp);
336 /* still no key? advance to next line*/
337 continue;
338 }
339 }
Damien Miller4e270b02010-04-16 15:56:21 +1000340 if (key_is_cert(key)) {
Damien Miller0a80ca12010-02-27 07:55:05 +1100341 if (!key_equal(found, key->cert->signature_key))
342 continue;
Damien Miller84399552010-05-21 14:58:12 +1000343 if (auth_parse_options(pw, key_options, file,
344 linenum) != 1)
345 continue;
346 if (!key_is_cert_authority)
347 continue;
Damien Miller0a80ca12010-02-27 07:55:05 +1100348 fp = key_fingerprint(found, SSH_FP_MD5,
349 SSH_FP_HEX);
Damien Millere513a912010-03-22 05:51:21 +1100350 debug("matching CA found: file %s, line %lu, %s %s",
351 file, linenum, key_type(found), fp);
Damien Miller30da3442010-05-10 11:58:03 +1000352 /*
353 * If the user has specified a list of principals as
354 * a key option, then prefer that list to matching
355 * their username in the certificate principals list.
356 */
357 if (authorized_principals != NULL &&
358 !match_principals_option(authorized_principals,
359 key->cert)) {
360 reason = "Certificate does not contain an "
361 "authorized principal";
362 fail_reason:
Darren Tuckera627d422013-06-02 07:31:17 +1000363 free(fp);
Damien Miller0a80ca12010-02-27 07:55:05 +1100364 error("%s", reason);
365 auth_debug_add("%s", reason);
366 continue;
367 }
Damien Miller30da3442010-05-10 11:58:03 +1000368 if (key_cert_check_authority(key, 0, 0,
369 authorized_principals == NULL ? pw->pw_name : NULL,
370 &reason) != 0)
371 goto fail_reason;
Damien Miller4e270b02010-04-16 15:56:21 +1000372 if (auth_cert_options(key, pw) != 0) {
Darren Tuckera627d422013-06-02 07:31:17 +1000373 free(fp);
Damien Miller0a80ca12010-02-27 07:55:05 +1100374 continue;
Damien Millere513a912010-03-22 05:51:21 +1100375 }
376 verbose("Accepted certificate ID \"%s\" "
377 "signed by %s CA %s via %s", key->cert->key_id,
378 key_type(found), fp, file);
Darren Tuckera627d422013-06-02 07:31:17 +1000379 free(fp);
Damien Miller0a80ca12010-02-27 07:55:05 +1100380 found_key = 1;
381 break;
Damien Miller84399552010-05-21 14:58:12 +1000382 } else if (key_equal(found, key)) {
383 if (auth_parse_options(pw, key_options, file,
384 linenum) != 1)
385 continue;
386 if (key_is_cert_authority)
387 continue;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000388 found_key = 1;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000389 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
Darren Tucker74836ae2013-06-02 07:32:00 +1000390 debug("matching key found: file %s, line %lu %s %s",
391 file, linenum, key_type(found), fp);
Darren Tuckera627d422013-06-02 07:31:17 +1000392 free(fp);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000393 break;
394 }
395 }
Darren Tucker74836ae2013-06-02 07:32:00 +1000396 if (found != NULL)
397 key_free(found);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000398 if (!found_key)
399 debug2("key not found");
400 return found_key;
401}
402
Damien Miller1aed65e2010-03-04 21:53:35 +1100403/* Authenticate a certificate key against TrustedUserCAKeys */
404static int
405user_cert_trusted_ca(struct passwd *pw, Key *key)
406{
Damien Miller30da3442010-05-10 11:58:03 +1000407 char *ca_fp, *principals_file = NULL;
Damien Miller1aed65e2010-03-04 21:53:35 +1100408 const char *reason;
409 int ret = 0;
410
411 if (!key_is_cert(key) || options.trusted_user_ca_keys == NULL)
412 return 0;
413
Damien Millere513a912010-03-22 05:51:21 +1100414 ca_fp = key_fingerprint(key->cert->signature_key,
415 SSH_FP_MD5, SSH_FP_HEX);
Damien Miller1aed65e2010-03-04 21:53:35 +1100416
417 if (key_in_file(key->cert->signature_key,
418 options.trusted_user_ca_keys, 1) != 1) {
419 debug2("%s: CA %s %s is not listed in %s", __func__,
420 key_type(key->cert->signature_key), ca_fp,
421 options.trusted_user_ca_keys);
422 goto out;
423 }
Damien Miller30da3442010-05-10 11:58:03 +1000424 /*
425 * If AuthorizedPrincipals is in use, then compare the certificate
426 * principals against the names in that file rather than matching
427 * against the username.
428 */
429 if ((principals_file = authorized_principals_file(pw)) != NULL) {
430 if (!match_principals_file(principals_file, pw, key->cert)) {
431 reason = "Certificate does not contain an "
432 "authorized principal";
433 fail_reason:
434 error("%s", reason);
435 auth_debug_add("%s", reason);
436 goto out;
437 }
Damien Miller1aed65e2010-03-04 21:53:35 +1100438 }
Damien Miller30da3442010-05-10 11:58:03 +1000439 if (key_cert_check_authority(key, 0, 1,
440 principals_file == NULL ? pw->pw_name : NULL, &reason) != 0)
441 goto fail_reason;
Damien Miller4e270b02010-04-16 15:56:21 +1000442 if (auth_cert_options(key, pw) != 0)
Damien Miller1aed65e2010-03-04 21:53:35 +1100443 goto out;
444
Damien Millere513a912010-03-22 05:51:21 +1100445 verbose("Accepted certificate ID \"%s\" signed by %s CA %s via %s",
446 key->cert->key_id, key_type(key->cert->signature_key), ca_fp,
447 options.trusted_user_ca_keys);
Damien Miller1aed65e2010-03-04 21:53:35 +1100448 ret = 1;
449
450 out:
Darren Tuckera627d422013-06-02 07:31:17 +1000451 free(principals_file);
452 free(ca_fp);
Damien Miller1aed65e2010-03-04 21:53:35 +1100453 return ret;
454}
455
Damien Miller09d3e122012-10-31 08:58:58 +1100456/*
457 * Checks whether key is allowed in file.
458 * returns 1 if the key is allowed or 0 otherwise.
459 */
460static int
461user_key_allowed2(struct passwd *pw, Key *key, char *file)
462{
463 FILE *f;
464 int found_key = 0;
465
466 /* Temporarily use the user's uid. */
467 temporarily_use_uid(pw);
468
469 debug("trying public key file %s", file);
470 if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) {
471 found_key = check_authkeys_file(f, file, key, pw);
472 fclose(f);
473 }
474
475 restore_uid();
476 return found_key;
477}
478
479/*
480 * Checks whether key is allowed in output of command.
481 * returns 1 if the key is allowed or 0 otherwise.
482 */
483static int
484user_key_command_allowed2(struct passwd *user_pw, Key *key)
485{
486 FILE *f;
487 int ok, found_key = 0;
488 struct passwd *pw;
489 struct stat st;
490 int status, devnull, p[2], i;
491 pid_t pid;
Damien Millerd0d10992012-11-04 22:23:14 +1100492 char *username, errmsg[512];
Damien Miller09d3e122012-10-31 08:58:58 +1100493
494 if (options.authorized_keys_command == NULL ||
495 options.authorized_keys_command[0] != '/')
496 return 0;
497
Damien Millerd0d10992012-11-04 22:23:14 +1100498 if (options.authorized_keys_command_user == NULL) {
499 error("No user for AuthorizedKeysCommand specified, skipping");
500 return 0;
Damien Miller09d3e122012-10-31 08:58:58 +1100501 }
502
Damien Millerd0d10992012-11-04 22:23:14 +1100503 username = percent_expand(options.authorized_keys_command_user,
504 "u", user_pw->pw_name, (char *)NULL);
505 pw = getpwnam(username);
506 if (pw == NULL) {
Damien Miller4018dc02013-02-15 10:28:55 +1100507 error("AuthorizedKeysCommandUser \"%s\" not found: %s",
508 username, strerror(errno));
Damien Millerd0d10992012-11-04 22:23:14 +1100509 free(username);
510 return 0;
511 }
512 free(username);
513
Damien Miller09d3e122012-10-31 08:58:58 +1100514 temporarily_use_uid(pw);
515
516 if (stat(options.authorized_keys_command, &st) < 0) {
517 error("Could not stat AuthorizedKeysCommand \"%s\": %s",
518 options.authorized_keys_command, strerror(errno));
519 goto out;
520 }
521 if (auth_secure_path(options.authorized_keys_command, &st, NULL, 0,
522 errmsg, sizeof(errmsg)) != 0) {
523 error("Unsafe AuthorizedKeysCommand: %s", errmsg);
524 goto out;
525 }
526
527 if (pipe(p) != 0) {
528 error("%s: pipe: %s", __func__, strerror(errno));
529 goto out;
530 }
531
Damien Miller1e854692012-11-14 19:04:02 +1100532 debug3("Running AuthorizedKeysCommand: \"%s %s\" as \"%s\"",
533 options.authorized_keys_command, user_pw->pw_name, pw->pw_name);
Damien Miller09d3e122012-10-31 08:58:58 +1100534
535 /*
536 * Don't want to call this in the child, where it can fatal() and
537 * run cleanup_exit() code.
538 */
539 restore_uid();
540
541 switch ((pid = fork())) {
542 case -1: /* error */
543 error("%s: fork: %s", __func__, strerror(errno));
544 close(p[0]);
545 close(p[1]);
546 return 0;
547 case 0: /* child */
548 for (i = 0; i < NSIG; i++)
549 signal(i, SIG_DFL);
550
Damien Miller1e854692012-11-14 19:04:02 +1100551 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) {
552 error("%s: open %s: %s", __func__, _PATH_DEVNULL,
553 strerror(errno));
554 _exit(1);
555 }
556 /* Keep stderr around a while longer to catch errors */
557 if (dup2(devnull, STDIN_FILENO) == -1 ||
558 dup2(p[1], STDOUT_FILENO) == -1) {
559 error("%s: dup2: %s", __func__, strerror(errno));
560 _exit(1);
561 }
Damien Millerd0d10992012-11-04 22:23:14 +1100562 closefrom(STDERR_FILENO + 1);
Damien Miller1e854692012-11-14 19:04:02 +1100563
Damien Miller09d3e122012-10-31 08:58:58 +1100564 /* Don't use permanently_set_uid() here to avoid fatal() */
565 if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0) {
566 error("setresgid %u: %s", (u_int)pw->pw_gid,
567 strerror(errno));
568 _exit(1);
569 }
570 if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) != 0) {
571 error("setresuid %u: %s", (u_int)pw->pw_uid,
572 strerror(errno));
573 _exit(1);
574 }
Damien Miller1e854692012-11-14 19:04:02 +1100575 /* stdin is pointed to /dev/null at this point */
576 if (dup2(STDIN_FILENO, STDERR_FILENO) == -1) {
Damien Miller09d3e122012-10-31 08:58:58 +1100577 error("%s: dup2: %s", __func__, strerror(errno));
578 _exit(1);
579 }
Damien Miller09d3e122012-10-31 08:58:58 +1100580
581 execl(options.authorized_keys_command,
Damien Miller1e854692012-11-14 19:04:02 +1100582 options.authorized_keys_command, user_pw->pw_name, NULL);
Damien Miller09d3e122012-10-31 08:58:58 +1100583
584 error("AuthorizedKeysCommand %s exec failed: %s",
585 options.authorized_keys_command, strerror(errno));
586 _exit(127);
587 default: /* parent */
588 break;
589 }
590
591 temporarily_use_uid(pw);
592
593 close(p[1]);
594 if ((f = fdopen(p[0], "r")) == NULL) {
595 error("%s: fdopen: %s", __func__, strerror(errno));
596 close(p[0]);
597 /* Don't leave zombie child */
598 kill(pid, SIGTERM);
599 while (waitpid(pid, NULL, 0) == -1 && errno == EINTR)
600 ;
601 goto out;
602 }
603 ok = check_authkeys_file(f, options.authorized_keys_command, key, pw);
604 fclose(f);
605
606 while (waitpid(pid, &status, 0) == -1) {
607 if (errno != EINTR) {
608 error("%s: waitpid: %s", __func__, strerror(errno));
609 goto out;
610 }
611 }
612 if (WIFSIGNALED(status)) {
613 error("AuthorizedKeysCommand %s exited on signal %d",
614 options.authorized_keys_command, WTERMSIG(status));
615 goto out;
616 } else if (WEXITSTATUS(status) != 0) {
617 error("AuthorizedKeysCommand %s returned status %d",
618 options.authorized_keys_command, WEXITSTATUS(status));
619 goto out;
620 }
621 found_key = ok;
622 out:
623 restore_uid();
624 return found_key;
625}
626
627/*
628 * Check whether key authenticates and authorises the user.
629 */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000630int
631user_key_allowed(struct passwd *pw, Key *key)
632{
Damien Millerd8478b62011-05-29 21:39:36 +1000633 u_int success, i;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000634 char *file;
635
Damien Miller1aed65e2010-03-04 21:53:35 +1100636 if (auth_key_is_revoked(key))
637 return 0;
638 if (key_is_cert(key) && auth_key_is_revoked(key->cert->signature_key))
639 return 0;
640
641 success = user_cert_trusted_ca(pw, key);
642 if (success)
643 return success;
644
Damien Miller09d3e122012-10-31 08:58:58 +1100645 success = user_key_command_allowed2(pw, key);
646 if (success > 0)
647 return success;
648
Damien Millerd8478b62011-05-29 21:39:36 +1000649 for (i = 0; !success && i < options.num_authkeys_files; i++) {
Damien Miller09d3e122012-10-31 08:58:58 +1100650
651 if (strcasecmp(options.authorized_keys_files[i], "none") == 0)
652 continue;
Damien Millerd8478b62011-05-29 21:39:36 +1000653 file = expand_authorized_keys(
654 options.authorized_keys_files[i], pw);
Damien Miller09d3e122012-10-31 08:58:58 +1100655
Damien Millerd8478b62011-05-29 21:39:36 +1000656 success = user_key_allowed2(pw, key, file);
Darren Tuckera627d422013-06-02 07:31:17 +1000657 free(file);
Damien Millerd8478b62011-05-29 21:39:36 +1000658 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000659
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000660 return success;
661}
662
663Authmethod method_pubkey = {
664 "publickey",
665 userauth_pubkey,
666 &options.pubkey_authentication
667};