blob: 6a62170172255f78861864290e7523b78ea01943 [file] [log] [blame]
Damien Millerd0d10992012-11-04 22:23:14 +11001/* $OpenBSD: auth2-pubkey.c,v 1.32 2012/11/04 10:38:43 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>
Damien Miller09d3e122012-10-31 08:58:58 +110034#include <paths.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100035#include <pwd.h>
Damien Miller09d3e122012-10-31 08:58:58 +110036#include <signal.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100037#include <stdio.h>
Damien Millerd7834352006-08-05 12:39:39 +100038#include <stdarg.h>
Damien Miller0a80ca12010-02-27 07:55:05 +110039#include <string.h>
40#include <time.h>
Darren Tuckerd9526a52008-06-14 09:01:24 +100041#include <unistd.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100042
Damien Millerd7834352006-08-05 12:39:39 +100043#include "xmalloc.h"
Darren Tucker22cc7412004-12-06 22:47:41 +110044#include "ssh.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000045#include "ssh2.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000046#include "packet.h"
47#include "buffer.h"
48#include "log.h"
49#include "servconf.h"
50#include "compat.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000051#include "key.h"
Damien Millerd7834352006-08-05 12:39:39 +100052#include "hostfile.h"
53#include "auth.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000054#include "pathnames.h"
55#include "uidswap.h"
56#include "auth-options.h"
57#include "canohost.h"
Damien Millerd7834352006-08-05 12:39:39 +100058#ifdef GSSAPI
59#include "ssh-gss.h"
60#endif
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000061#include "monitor_wrap.h"
Darren Tuckerf0f90982004-12-11 13:39:50 +110062#include "misc.h"
Damien Miller1aed65e2010-03-04 21:53:35 +110063#include "authfile.h"
Damien Miller30da3442010-05-10 11:58:03 +100064#include "match.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000065
66/* import */
67extern ServerOptions options;
68extern u_char *session_id2;
Darren Tucker502d3842003-06-28 12:38:01 +100069extern u_int session_id2_len;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000070
71static int
72userauth_pubkey(Authctxt *authctxt)
73{
74 Buffer b;
75 Key *key = NULL;
76 char *pkalg;
77 u_char *pkblob, *sig;
78 u_int alen, blen, slen;
79 int have_sig, pktype;
80 int authenticated = 0;
81
82 if (!authctxt->valid) {
83 debug2("userauth_pubkey: disabled because of invalid user");
84 return 0;
85 }
86 have_sig = packet_get_char();
87 if (datafellows & SSH_BUG_PKAUTH) {
88 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
89 /* no explicit pkalg given */
90 pkblob = packet_get_string(&blen);
91 buffer_init(&b);
92 buffer_append(&b, pkblob, blen);
93 /* so we have to extract the pkalg from the pkblob */
94 pkalg = buffer_get_string(&b, &alen);
95 buffer_free(&b);
96 } else {
97 pkalg = packet_get_string(&alen);
98 pkblob = packet_get_string(&blen);
99 }
100 pktype = key_type_from_name(pkalg);
101 if (pktype == KEY_UNSPEC) {
102 /* this is perfectly legal */
Damien Miller996acd22003-04-09 20:59:48 +1000103 logit("userauth_pubkey: unsupported public key algorithm: %s",
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000104 pkalg);
105 goto done;
106 }
107 key = key_from_blob(pkblob, blen);
108 if (key == NULL) {
109 error("userauth_pubkey: cannot decode key: %s", pkalg);
110 goto done;
111 }
112 if (key->type != pktype) {
113 error("userauth_pubkey: type mismatch for decoded key "
114 "(received %d, expected %d)", key->type, pktype);
115 goto done;
116 }
117 if (have_sig) {
118 sig = packet_get_string(&slen);
119 packet_check_eom();
120 buffer_init(&b);
121 if (datafellows & SSH_OLD_SESSIONID) {
122 buffer_append(&b, session_id2, session_id2_len);
123 } else {
124 buffer_put_string(&b, session_id2, session_id2_len);
125 }
126 /* reconstruct packet */
127 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
128 buffer_put_cstring(&b, authctxt->user);
129 buffer_put_cstring(&b,
130 datafellows & SSH_BUG_PKSERVICE ?
131 "ssh-userauth" :
132 authctxt->service);
133 if (datafellows & SSH_BUG_PKAUTH) {
134 buffer_put_char(&b, have_sig);
135 } else {
136 buffer_put_cstring(&b, "publickey");
137 buffer_put_char(&b, have_sig);
138 buffer_put_cstring(&b, pkalg);
139 }
140 buffer_put_string(&b, pkblob, blen);
141#ifdef DEBUG_PK
142 buffer_dump(&b);
143#endif
144 /* test for correct signature */
145 authenticated = 0;
146 if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
147 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
Damien Millerfb1310e2004-01-21 11:02:50 +1100148 buffer_len(&b))) == 1)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000149 authenticated = 1;
Damien Millerfb1310e2004-01-21 11:02:50 +1100150 buffer_free(&b);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000151 xfree(sig);
152 } else {
153 debug("test whether pkalg/pkblob are acceptable");
154 packet_check_eom();
155
156 /* XXX fake reply and always send PK_OK ? */
157 /*
158 * XXX this allows testing whether a user is allowed
159 * to login: if you happen to have a valid pubkey this
160 * message is sent. the message is NEVER sent at all
161 * if a user is not allowed to login. is this an
162 * issue? -markus
163 */
164 if (PRIVSEP(user_key_allowed(authctxt->pw, key))) {
165 packet_start(SSH2_MSG_USERAUTH_PK_OK);
166 packet_put_string(pkalg, alen);
167 packet_put_string(pkblob, blen);
168 packet_send();
169 packet_write_wait();
170 authctxt->postponed = 1;
171 }
172 }
173 if (authenticated != 1)
174 auth_clear_options();
175done:
176 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
177 if (key != NULL)
178 key_free(key);
179 xfree(pkalg);
180 xfree(pkblob);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000181 return authenticated;
182}
183
Damien Miller30da3442010-05-10 11:58:03 +1000184static int
185match_principals_option(const char *principal_list, struct KeyCert *cert)
186{
187 char *result;
188 u_int i;
189
190 /* XXX percent_expand() sequences for authorized_principals? */
191
192 for (i = 0; i < cert->nprincipals; i++) {
193 if ((result = match_list(cert->principals[i],
194 principal_list, NULL)) != NULL) {
195 debug3("matched principal from key options \"%.100s\"",
196 result);
197 xfree(result);
198 return 1;
199 }
200 }
201 return 0;
202}
203
204static int
Damien Miller6018a362010-07-02 13:35:19 +1000205match_principals_file(char *file, struct passwd *pw, struct KeyCert *cert)
Damien Miller30da3442010-05-10 11:58:03 +1000206{
207 FILE *f;
Damien Miller6018a362010-07-02 13:35:19 +1000208 char line[SSH_MAX_PUBKEY_BYTES], *cp, *ep, *line_opts;
Damien Miller30da3442010-05-10 11:58:03 +1000209 u_long linenum = 0;
210 u_int i;
211
212 temporarily_use_uid(pw);
213 debug("trying authorized principals file %s", file);
214 if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) {
215 restore_uid();
216 return 0;
217 }
218 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
Damien Miller6018a362010-07-02 13:35:19 +1000219 /* Skip leading whitespace. */
Damien Miller30da3442010-05-10 11:58:03 +1000220 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
221 ;
Damien Miller6018a362010-07-02 13:35:19 +1000222 /* Skip blank and comment lines. */
223 if ((ep = strchr(cp, '#')) != NULL)
224 *ep = '\0';
225 if (!*cp || *cp == '\n')
Damien Miller30da3442010-05-10 11:58:03 +1000226 continue;
Damien Miller6018a362010-07-02 13:35:19 +1000227 /* Trim trailing whitespace. */
228 ep = cp + strlen(cp) - 1;
229 while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t'))
230 *ep-- = '\0';
231 /*
232 * If the line has internal whitespace then assume it has
233 * key options.
234 */
235 line_opts = NULL;
236 if ((ep = strrchr(cp, ' ')) != NULL ||
237 (ep = strrchr(cp, '\t')) != NULL) {
238 for (; *ep == ' ' || *ep == '\t'; ep++)
Damien Miller188ea812010-12-01 11:50:14 +1100239 ;
Damien Miller6018a362010-07-02 13:35:19 +1000240 line_opts = cp;
241 cp = ep;
242 }
Damien Miller30da3442010-05-10 11:58:03 +1000243 for (i = 0; i < cert->nprincipals; i++) {
244 if (strcmp(cp, cert->principals[i]) == 0) {
Darren Tuckeraf1a60e2011-10-02 18:59:59 +1100245 debug3("matched principal \"%.100s\" "
246 "from file \"%s\" on line %lu",
Damien Miller09d3e122012-10-31 08:58:58 +1100247 cert->principals[i], file, linenum);
Damien Miller6018a362010-07-02 13:35:19 +1000248 if (auth_parse_options(pw, line_opts,
249 file, linenum) != 1)
250 continue;
Damien Miller30da3442010-05-10 11:58:03 +1000251 fclose(f);
252 restore_uid();
253 return 1;
254 }
255 }
256 }
257 fclose(f);
258 restore_uid();
259 return 0;
Damien Miller09d3e122012-10-31 08:58:58 +1100260}
Damien Miller30da3442010-05-10 11:58:03 +1000261
Damien Miller09d3e122012-10-31 08:58:58 +1100262/*
263 * Checks whether key is allowed in authorized_keys-format file,
264 * returns 1 if the key is allowed or 0 otherwise.
265 */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000266static int
Damien Miller09d3e122012-10-31 08:58:58 +1100267check_authkeys_file(FILE *f, char *file, Key* key, struct passwd *pw)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000268{
Darren Tucker22cc7412004-12-06 22:47:41 +1100269 char line[SSH_MAX_PUBKEY_BYTES];
Damien Miller0a80ca12010-02-27 07:55:05 +1100270 const char *reason;
Darren Tucker33c787f2008-07-02 22:37:30 +1000271 int found_key = 0;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000272 u_long linenum = 0;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000273 Key *found;
274 char *fp;
275
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000276 found_key = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +1100277 found = key_new(key_is_cert(key) ? KEY_UNSPEC : key->type);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000278
Darren Tucker22cc7412004-12-06 22:47:41 +1100279 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000280 char *cp, *key_options = NULL;
Darren Tucker22cc7412004-12-06 22:47:41 +1100281
Damien Miller0a80ca12010-02-27 07:55:05 +1100282 auth_clear_options();
283
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000284 /* Skip leading whitespace, empty and comment lines. */
285 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
286 ;
287 if (!*cp || *cp == '\n' || *cp == '#')
288 continue;
289
290 if (key_read(found, &cp) != 1) {
291 /* no key? check if there are options for this key */
292 int quoted = 0;
293 debug2("user_key_allowed: check options: '%s'", cp);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000294 key_options = cp;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000295 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
296 if (*cp == '\\' && cp[1] == '"')
297 cp++; /* Skip both */
298 else if (*cp == '"')
299 quoted = !quoted;
300 }
301 /* Skip remaining whitespace. */
302 for (; *cp == ' ' || *cp == '\t'; cp++)
303 ;
304 if (key_read(found, &cp) != 1) {
305 debug2("user_key_allowed: advance: '%s'", cp);
306 /* still no key? advance to next line*/
307 continue;
308 }
309 }
Damien Miller4e270b02010-04-16 15:56:21 +1000310 if (key_is_cert(key)) {
Damien Miller0a80ca12010-02-27 07:55:05 +1100311 if (!key_equal(found, key->cert->signature_key))
312 continue;
Damien Miller84399552010-05-21 14:58:12 +1000313 if (auth_parse_options(pw, key_options, file,
314 linenum) != 1)
315 continue;
316 if (!key_is_cert_authority)
317 continue;
Damien Miller0a80ca12010-02-27 07:55:05 +1100318 fp = key_fingerprint(found, SSH_FP_MD5,
319 SSH_FP_HEX);
Damien Millere513a912010-03-22 05:51:21 +1100320 debug("matching CA found: file %s, line %lu, %s %s",
321 file, linenum, key_type(found), fp);
Damien Miller30da3442010-05-10 11:58:03 +1000322 /*
323 * If the user has specified a list of principals as
324 * a key option, then prefer that list to matching
325 * their username in the certificate principals list.
326 */
327 if (authorized_principals != NULL &&
328 !match_principals_option(authorized_principals,
329 key->cert)) {
330 reason = "Certificate does not contain an "
331 "authorized principal";
332 fail_reason:
Damien Millere513a912010-03-22 05:51:21 +1100333 xfree(fp);
Damien Miller0a80ca12010-02-27 07:55:05 +1100334 error("%s", reason);
335 auth_debug_add("%s", reason);
336 continue;
337 }
Damien Miller30da3442010-05-10 11:58:03 +1000338 if (key_cert_check_authority(key, 0, 0,
339 authorized_principals == NULL ? pw->pw_name : NULL,
340 &reason) != 0)
341 goto fail_reason;
Damien Miller4e270b02010-04-16 15:56:21 +1000342 if (auth_cert_options(key, pw) != 0) {
Damien Millere513a912010-03-22 05:51:21 +1100343 xfree(fp);
Damien Miller0a80ca12010-02-27 07:55:05 +1100344 continue;
Damien Millere513a912010-03-22 05:51:21 +1100345 }
346 verbose("Accepted certificate ID \"%s\" "
347 "signed by %s CA %s via %s", key->cert->key_id,
348 key_type(found), fp, file);
349 xfree(fp);
Damien Miller0a80ca12010-02-27 07:55:05 +1100350 found_key = 1;
351 break;
Damien Miller84399552010-05-21 14:58:12 +1000352 } else if (key_equal(found, key)) {
353 if (auth_parse_options(pw, key_options, file,
354 linenum) != 1)
355 continue;
356 if (key_is_cert_authority)
357 continue;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000358 found_key = 1;
359 debug("matching key found: file %s, line %lu",
360 file, linenum);
361 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
362 verbose("Found matching %s key: %s",
363 key_type(found), fp);
364 xfree(fp);
365 break;
366 }
367 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000368 key_free(found);
369 if (!found_key)
370 debug2("key not found");
371 return found_key;
372}
373
Damien Miller1aed65e2010-03-04 21:53:35 +1100374/* Authenticate a certificate key against TrustedUserCAKeys */
375static int
376user_cert_trusted_ca(struct passwd *pw, Key *key)
377{
Damien Miller30da3442010-05-10 11:58:03 +1000378 char *ca_fp, *principals_file = NULL;
Damien Miller1aed65e2010-03-04 21:53:35 +1100379 const char *reason;
380 int ret = 0;
381
382 if (!key_is_cert(key) || options.trusted_user_ca_keys == NULL)
383 return 0;
384
Damien Millere513a912010-03-22 05:51:21 +1100385 ca_fp = key_fingerprint(key->cert->signature_key,
386 SSH_FP_MD5, SSH_FP_HEX);
Damien Miller1aed65e2010-03-04 21:53:35 +1100387
388 if (key_in_file(key->cert->signature_key,
389 options.trusted_user_ca_keys, 1) != 1) {
390 debug2("%s: CA %s %s is not listed in %s", __func__,
391 key_type(key->cert->signature_key), ca_fp,
392 options.trusted_user_ca_keys);
393 goto out;
394 }
Damien Miller30da3442010-05-10 11:58:03 +1000395 /*
396 * If AuthorizedPrincipals is in use, then compare the certificate
397 * principals against the names in that file rather than matching
398 * against the username.
399 */
400 if ((principals_file = authorized_principals_file(pw)) != NULL) {
401 if (!match_principals_file(principals_file, pw, key->cert)) {
402 reason = "Certificate does not contain an "
403 "authorized principal";
404 fail_reason:
405 error("%s", reason);
406 auth_debug_add("%s", reason);
407 goto out;
408 }
Damien Miller1aed65e2010-03-04 21:53:35 +1100409 }
Damien Miller30da3442010-05-10 11:58:03 +1000410 if (key_cert_check_authority(key, 0, 1,
411 principals_file == NULL ? pw->pw_name : NULL, &reason) != 0)
412 goto fail_reason;
Damien Miller4e270b02010-04-16 15:56:21 +1000413 if (auth_cert_options(key, pw) != 0)
Damien Miller1aed65e2010-03-04 21:53:35 +1100414 goto out;
415
Damien Millere513a912010-03-22 05:51:21 +1100416 verbose("Accepted certificate ID \"%s\" signed by %s CA %s via %s",
417 key->cert->key_id, key_type(key->cert->signature_key), ca_fp,
418 options.trusted_user_ca_keys);
Damien Miller1aed65e2010-03-04 21:53:35 +1100419 ret = 1;
420
421 out:
Damien Miller30da3442010-05-10 11:58:03 +1000422 if (principals_file != NULL)
423 xfree(principals_file);
Damien Miller1aed65e2010-03-04 21:53:35 +1100424 if (ca_fp != NULL)
425 xfree(ca_fp);
426 return ret;
427}
428
Damien Miller09d3e122012-10-31 08:58:58 +1100429/*
430 * Checks whether key is allowed in file.
431 * returns 1 if the key is allowed or 0 otherwise.
432 */
433static int
434user_key_allowed2(struct passwd *pw, Key *key, char *file)
435{
436 FILE *f;
437 int found_key = 0;
438
439 /* Temporarily use the user's uid. */
440 temporarily_use_uid(pw);
441
442 debug("trying public key file %s", file);
443 if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) {
444 found_key = check_authkeys_file(f, file, key, pw);
445 fclose(f);
446 }
447
448 restore_uid();
449 return found_key;
450}
451
452/*
453 * Checks whether key is allowed in output of command.
454 * returns 1 if the key is allowed or 0 otherwise.
455 */
456static int
457user_key_command_allowed2(struct passwd *user_pw, Key *key)
458{
459 FILE *f;
460 int ok, found_key = 0;
461 struct passwd *pw;
462 struct stat st;
463 int status, devnull, p[2], i;
464 pid_t pid;
Damien Millerd0d10992012-11-04 22:23:14 +1100465 char *username, errmsg[512];
Damien Miller09d3e122012-10-31 08:58:58 +1100466
467 if (options.authorized_keys_command == NULL ||
468 options.authorized_keys_command[0] != '/')
469 return 0;
470
Damien Millerd0d10992012-11-04 22:23:14 +1100471 if (options.authorized_keys_command_user == NULL) {
472 error("No user for AuthorizedKeysCommand specified, skipping");
473 return 0;
Damien Miller09d3e122012-10-31 08:58:58 +1100474 }
475
Damien Millerd0d10992012-11-04 22:23:14 +1100476 username = percent_expand(options.authorized_keys_command_user,
477 "u", user_pw->pw_name, (char *)NULL);
478 pw = getpwnam(username);
479 if (pw == NULL) {
480 error("AuthorizedKeyCommandUser \"%s\" not found: %s",
481 options.authorized_keys_command, strerror(errno));
482 free(username);
483 return 0;
484 }
485 free(username);
486
Damien Miller09d3e122012-10-31 08:58:58 +1100487 temporarily_use_uid(pw);
488
489 if (stat(options.authorized_keys_command, &st) < 0) {
490 error("Could not stat AuthorizedKeysCommand \"%s\": %s",
491 options.authorized_keys_command, strerror(errno));
492 goto out;
493 }
494 if (auth_secure_path(options.authorized_keys_command, &st, NULL, 0,
495 errmsg, sizeof(errmsg)) != 0) {
496 error("Unsafe AuthorizedKeysCommand: %s", errmsg);
497 goto out;
498 }
499
500 if (pipe(p) != 0) {
501 error("%s: pipe: %s", __func__, strerror(errno));
502 goto out;
503 }
504
505 debug3("Running AuthorizedKeysCommand: \"%s\" as \"%s\"",
506 options.authorized_keys_command, pw->pw_name);
507
508 /*
509 * Don't want to call this in the child, where it can fatal() and
510 * run cleanup_exit() code.
511 */
512 restore_uid();
513
514 switch ((pid = fork())) {
515 case -1: /* error */
516 error("%s: fork: %s", __func__, strerror(errno));
517 close(p[0]);
518 close(p[1]);
519 return 0;
520 case 0: /* child */
521 for (i = 0; i < NSIG; i++)
522 signal(i, SIG_DFL);
523
Damien Millerd0d10992012-11-04 22:23:14 +1100524 closefrom(STDERR_FILENO + 1);
Damien Miller09d3e122012-10-31 08:58:58 +1100525 /* Don't use permanently_set_uid() here to avoid fatal() */
526 if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0) {
527 error("setresgid %u: %s", (u_int)pw->pw_gid,
528 strerror(errno));
529 _exit(1);
530 }
531 if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) != 0) {
532 error("setresuid %u: %s", (u_int)pw->pw_uid,
533 strerror(errno));
534 _exit(1);
535 }
536
537 close(p[0]);
538 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) {
539 error("%s: open %s: %s", __func__, _PATH_DEVNULL,
540 strerror(errno));
541 _exit(1);
542 }
543 if (dup2(devnull, STDIN_FILENO) == -1 ||
544 dup2(p[1], STDOUT_FILENO) == -1 ||
545 dup2(devnull, STDERR_FILENO) == -1) {
546 error("%s: dup2: %s", __func__, strerror(errno));
547 _exit(1);
548 }
Damien Miller09d3e122012-10-31 08:58:58 +1100549
550 execl(options.authorized_keys_command,
551 options.authorized_keys_command, pw->pw_name, NULL);
552
553 error("AuthorizedKeysCommand %s exec failed: %s",
554 options.authorized_keys_command, strerror(errno));
555 _exit(127);
556 default: /* parent */
557 break;
558 }
559
560 temporarily_use_uid(pw);
561
562 close(p[1]);
563 if ((f = fdopen(p[0], "r")) == NULL) {
564 error("%s: fdopen: %s", __func__, strerror(errno));
565 close(p[0]);
566 /* Don't leave zombie child */
567 kill(pid, SIGTERM);
568 while (waitpid(pid, NULL, 0) == -1 && errno == EINTR)
569 ;
570 goto out;
571 }
572 ok = check_authkeys_file(f, options.authorized_keys_command, key, pw);
573 fclose(f);
574
575 while (waitpid(pid, &status, 0) == -1) {
576 if (errno != EINTR) {
577 error("%s: waitpid: %s", __func__, strerror(errno));
578 goto out;
579 }
580 }
581 if (WIFSIGNALED(status)) {
582 error("AuthorizedKeysCommand %s exited on signal %d",
583 options.authorized_keys_command, WTERMSIG(status));
584 goto out;
585 } else if (WEXITSTATUS(status) != 0) {
586 error("AuthorizedKeysCommand %s returned status %d",
587 options.authorized_keys_command, WEXITSTATUS(status));
588 goto out;
589 }
590 found_key = ok;
591 out:
592 restore_uid();
593 return found_key;
594}
595
596/*
597 * Check whether key authenticates and authorises the user.
598 */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000599int
600user_key_allowed(struct passwd *pw, Key *key)
601{
Damien Millerd8478b62011-05-29 21:39:36 +1000602 u_int success, i;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000603 char *file;
604
Damien Miller1aed65e2010-03-04 21:53:35 +1100605 if (auth_key_is_revoked(key))
606 return 0;
607 if (key_is_cert(key) && auth_key_is_revoked(key->cert->signature_key))
608 return 0;
609
610 success = user_cert_trusted_ca(pw, key);
611 if (success)
612 return success;
613
Damien Miller09d3e122012-10-31 08:58:58 +1100614 success = user_key_command_allowed2(pw, key);
615 if (success > 0)
616 return success;
617
Damien Millerd8478b62011-05-29 21:39:36 +1000618 for (i = 0; !success && i < options.num_authkeys_files; i++) {
Damien Miller09d3e122012-10-31 08:58:58 +1100619
620 if (strcasecmp(options.authorized_keys_files[i], "none") == 0)
621 continue;
Damien Millerd8478b62011-05-29 21:39:36 +1000622 file = expand_authorized_keys(
623 options.authorized_keys_files[i], pw);
Damien Miller09d3e122012-10-31 08:58:58 +1100624
Damien Millerd8478b62011-05-29 21:39:36 +1000625 success = user_key_allowed2(pw, key, file);
626 xfree(file);
627 }
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000628
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000629 return success;
630}
631
632Authmethod method_pubkey = {
633 "publickey",
634 userauth_pubkey,
635 &options.pubkey_authentication
636};