1 /* $OpenBSD: auth2-pubkey.c,v 1.34 2013/02/14 21:35:59 djm Exp $ */
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 #include <sys/types.h>
56 #include "pathnames.h"
58 #include "auth-options.h"
63 #include "monitor_wrap.h"
69 extern ServerOptions options;
70 extern u_char *session_id2;
71 extern u_int session_id2_len;
74 userauth_pubkey(Authctxt *authctxt)
80 u_int alen, blen, slen;
82 int authenticated = 0;
84 if (!authctxt->valid) {
85 debug2("userauth_pubkey: disabled because of invalid user");
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);
94 buffer_append(&b, pkblob, blen);
95 /* so we have to extract the pkalg from the pkblob */
96 pkalg = buffer_get_string(&b, &alen);
99 pkalg = packet_get_string(&alen);
100 pkblob = packet_get_string(&blen);
102 pktype = key_type_from_name(pkalg);
103 if (pktype == KEY_UNSPEC) {
104 /* this is perfectly legal */
105 logit("userauth_pubkey: unsupported public key algorithm: %s",
109 key = key_from_blob(pkblob, blen);
111 error("userauth_pubkey: cannot decode key: %s", pkalg);
114 if (key->type != pktype) {
115 error("userauth_pubkey: type mismatch for decoded key "
116 "(received %d, expected %d)", key->type, pktype);
120 sig = packet_get_string(&slen);
123 if (datafellows & SSH_OLD_SESSIONID) {
124 buffer_append(&b, session_id2, session_id2_len);
126 buffer_put_string(&b, session_id2, session_id2_len);
128 /* reconstruct packet */
129 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
130 buffer_put_cstring(&b, authctxt->user);
131 buffer_put_cstring(&b,
132 datafellows & SSH_BUG_PKSERVICE ?
135 if (datafellows & SSH_BUG_PKAUTH) {
136 buffer_put_char(&b, have_sig);
138 buffer_put_cstring(&b, "publickey");
139 buffer_put_char(&b, have_sig);
140 buffer_put_cstring(&b, pkalg);
142 buffer_put_string(&b, pkblob, blen);
146 /* test for correct signature */
148 if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
149 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
150 buffer_len(&b))) == 1)
155 debug("test whether pkalg/pkblob are acceptable");
158 /* XXX fake reply and always send PK_OK ? */
160 * XXX this allows testing whether a user is allowed
161 * to login: if you happen to have a valid pubkey this
162 * message is sent. the message is NEVER sent at all
163 * if a user is not allowed to login. is this an
166 if (PRIVSEP(user_key_allowed(authctxt->pw, key))) {
167 packet_start(SSH2_MSG_USERAUTH_PK_OK);
168 packet_put_string(pkalg, alen);
169 packet_put_string(pkblob, blen);
172 authctxt->postponed = 1;
175 if (authenticated != 1)
176 auth_clear_options();
178 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
183 return authenticated;
187 match_principals_option(const char *principal_list, struct KeyCert *cert)
192 /* XXX percent_expand() sequences for authorized_principals? */
194 for (i = 0; i < cert->nprincipals; i++) {
195 if ((result = match_list(cert->principals[i],
196 principal_list, NULL)) != NULL) {
197 debug3("matched principal from key options \"%.100s\"",
207 match_principals_file(char *file, struct passwd *pw, struct KeyCert *cert)
210 char line[SSH_MAX_PUBKEY_BYTES], *cp, *ep, *line_opts;
214 temporarily_use_uid(pw);
215 debug("trying authorized principals file %s", file);
216 if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) {
220 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
221 /* Skip leading whitespace. */
222 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
224 /* Skip blank and comment lines. */
225 if ((ep = strchr(cp, '#')) != NULL)
227 if (!*cp || *cp == '\n')
229 /* Trim trailing whitespace. */
230 ep = cp + strlen(cp) - 1;
231 while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t'))
234 * If the line has internal whitespace then assume it has
238 if ((ep = strrchr(cp, ' ')) != NULL ||
239 (ep = strrchr(cp, '\t')) != NULL) {
240 for (; *ep == ' ' || *ep == '\t'; ep++)
245 for (i = 0; i < cert->nprincipals; i++) {
246 if (strcmp(cp, cert->principals[i]) == 0) {
247 debug3("matched principal \"%.100s\" "
248 "from file \"%s\" on line %lu",
249 cert->principals[i], file, linenum);
250 if (auth_parse_options(pw, line_opts,
265 * Checks whether key is allowed in authorized_keys-format file,
266 * returns 1 if the key is allowed or 0 otherwise.
269 check_authkeys_file(FILE *f, char *file, Key* key, struct passwd *pw)
271 char line[SSH_MAX_PUBKEY_BYTES];
279 found = key_new(key_is_cert(key) ? KEY_UNSPEC : key->type);
281 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
282 char *cp, *key_options = NULL;
284 auth_clear_options();
286 /* Skip leading whitespace, empty and comment lines. */
287 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
289 if (!*cp || *cp == '\n' || *cp == '#')
292 if (key_read(found, &cp) != 1) {
293 /* no key? check if there are options for this key */
295 debug2("user_key_allowed: check options: '%s'", cp);
297 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
298 if (*cp == '\\' && cp[1] == '"')
299 cp++; /* Skip both */
303 /* Skip remaining whitespace. */
304 for (; *cp == ' ' || *cp == '\t'; cp++)
306 if (key_read(found, &cp) != 1) {
307 debug2("user_key_allowed: advance: '%s'", cp);
308 /* still no key? advance to next line*/
312 if (key_is_cert(key)) {
313 if (!key_equal(found, key->cert->signature_key))
315 if (auth_parse_options(pw, key_options, file,
318 if (!key_is_cert_authority)
320 fp = key_fingerprint(found, SSH_FP_MD5,
322 debug("matching CA found: file %s, line %lu, %s %s",
323 file, linenum, key_type(found), fp);
325 * If the user has specified a list of principals as
326 * a key option, then prefer that list to matching
327 * their username in the certificate principals list.
329 if (authorized_principals != NULL &&
330 !match_principals_option(authorized_principals,
332 reason = "Certificate does not contain an "
333 "authorized principal";
337 auth_debug_add("%s", reason);
340 if (key_cert_check_authority(key, 0, 0,
341 authorized_principals == NULL ? pw->pw_name : NULL,
344 if (auth_cert_options(key, pw) != 0) {
348 verbose("Accepted certificate ID \"%s\" "
349 "signed by %s CA %s via %s", key->cert->key_id,
350 key_type(found), fp, file);
354 } else if (key_equal(found, key)) {
355 if (auth_parse_options(pw, key_options, file,
358 if (key_is_cert_authority)
361 debug("matching key found: file %s, line %lu",
363 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
364 verbose("Found matching %s key: %s",
365 key_type(found), fp);
372 debug2("key not found");
376 /* Authenticate a certificate key against TrustedUserCAKeys */
378 user_cert_trusted_ca(struct passwd *pw, Key *key)
380 char *ca_fp, *principals_file = NULL;
384 if (!key_is_cert(key) || options.trusted_user_ca_keys == NULL)
387 ca_fp = key_fingerprint(key->cert->signature_key,
388 SSH_FP_MD5, SSH_FP_HEX);
390 if (key_in_file(key->cert->signature_key,
391 options.trusted_user_ca_keys, 1) != 1) {
392 debug2("%s: CA %s %s is not listed in %s", __func__,
393 key_type(key->cert->signature_key), ca_fp,
394 options.trusted_user_ca_keys);
398 * If AuthorizedPrincipals is in use, then compare the certificate
399 * principals against the names in that file rather than matching
400 * against the username.
402 if ((principals_file = authorized_principals_file(pw)) != NULL) {
403 if (!match_principals_file(principals_file, pw, key->cert)) {
404 reason = "Certificate does not contain an "
405 "authorized principal";
408 auth_debug_add("%s", reason);
412 if (key_cert_check_authority(key, 0, 1,
413 principals_file == NULL ? pw->pw_name : NULL, &reason) != 0)
415 if (auth_cert_options(key, pw) != 0)
418 verbose("Accepted certificate ID \"%s\" signed by %s CA %s via %s",
419 key->cert->key_id, key_type(key->cert->signature_key), ca_fp,
420 options.trusted_user_ca_keys);
424 if (principals_file != NULL)
425 xfree(principals_file);
432 * Checks whether key is allowed in file.
433 * returns 1 if the key is allowed or 0 otherwise.
436 user_key_allowed2(struct passwd *pw, Key *key, char *file)
441 /* Temporarily use the user's uid. */
442 temporarily_use_uid(pw);
444 debug("trying public key file %s", file);
445 if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) {
446 found_key = check_authkeys_file(f, file, key, pw);
455 * Checks whether key is allowed in output of command.
456 * returns 1 if the key is allowed or 0 otherwise.
459 user_key_command_allowed2(struct passwd *user_pw, Key *key)
462 int ok, found_key = 0;
465 int status, devnull, p[2], i;
467 char *username, errmsg[512];
469 if (options.authorized_keys_command == NULL ||
470 options.authorized_keys_command[0] != '/')
473 if (options.authorized_keys_command_user == NULL) {
474 error("No user for AuthorizedKeysCommand specified, skipping");
478 username = percent_expand(options.authorized_keys_command_user,
479 "u", user_pw->pw_name, (char *)NULL);
480 pw = getpwnam(username);
482 error("AuthorizedKeysCommandUser \"%s\" not found: %s",
483 username, strerror(errno));
489 temporarily_use_uid(pw);
491 if (stat(options.authorized_keys_command, &st) < 0) {
492 error("Could not stat AuthorizedKeysCommand \"%s\": %s",
493 options.authorized_keys_command, strerror(errno));
496 if (auth_secure_path(options.authorized_keys_command, &st, NULL, 0,
497 errmsg, sizeof(errmsg)) != 0) {
498 error("Unsafe AuthorizedKeysCommand: %s", errmsg);
503 error("%s: pipe: %s", __func__, strerror(errno));
507 debug3("Running AuthorizedKeysCommand: \"%s %s\" as \"%s\"",
508 options.authorized_keys_command, user_pw->pw_name, pw->pw_name);
511 * Don't want to call this in the child, where it can fatal() and
512 * run cleanup_exit() code.
516 switch ((pid = fork())) {
518 error("%s: fork: %s", __func__, strerror(errno));
523 for (i = 0; i < NSIG; i++)
526 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) {
527 error("%s: open %s: %s", __func__, _PATH_DEVNULL,
531 /* Keep stderr around a while longer to catch errors */
532 if (dup2(devnull, STDIN_FILENO) == -1 ||
533 dup2(p[1], STDOUT_FILENO) == -1) {
534 error("%s: dup2: %s", __func__, strerror(errno));
537 closefrom(STDERR_FILENO + 1);
539 /* Don't use permanently_set_uid() here to avoid fatal() */
540 if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0) {
541 error("setresgid %u: %s", (u_int)pw->pw_gid,
545 if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) != 0) {
546 error("setresuid %u: %s", (u_int)pw->pw_uid,
550 /* stdin is pointed to /dev/null at this point */
551 if (dup2(STDIN_FILENO, STDERR_FILENO) == -1) {
552 error("%s: dup2: %s", __func__, strerror(errno));
556 execl(options.authorized_keys_command,
557 options.authorized_keys_command, user_pw->pw_name, NULL);
559 error("AuthorizedKeysCommand %s exec failed: %s",
560 options.authorized_keys_command, strerror(errno));
562 default: /* parent */
566 temporarily_use_uid(pw);
569 if ((f = fdopen(p[0], "r")) == NULL) {
570 error("%s: fdopen: %s", __func__, strerror(errno));
572 /* Don't leave zombie child */
574 while (waitpid(pid, NULL, 0) == -1 && errno == EINTR)
578 ok = check_authkeys_file(f, options.authorized_keys_command, key, pw);
581 while (waitpid(pid, &status, 0) == -1) {
582 if (errno != EINTR) {
583 error("%s: waitpid: %s", __func__, strerror(errno));
587 if (WIFSIGNALED(status)) {
588 error("AuthorizedKeysCommand %s exited on signal %d",
589 options.authorized_keys_command, WTERMSIG(status));
591 } else if (WEXITSTATUS(status) != 0) {
592 error("AuthorizedKeysCommand %s returned status %d",
593 options.authorized_keys_command, WEXITSTATUS(status));
603 * Check whether key authenticates and authorises the user.
606 user_key_allowed(struct passwd *pw, Key *key)
611 if (auth_key_is_revoked(key))
613 if (key_is_cert(key) && auth_key_is_revoked(key->cert->signature_key))
616 success = user_cert_trusted_ca(pw, key);
620 success = user_key_command_allowed2(pw, key);
624 for (i = 0; !success && i < options.num_authkeys_files; i++) {
626 if (strcasecmp(options.authorized_keys_files[i], "none") == 0)
628 file = expand_authorized_keys(
629 options.authorized_keys_files[i], pw);
631 success = user_key_allowed2(pw, key, file);
638 Authmethod method_pubkey = {
641 &options.pubkey_authentication