1 /* $OpenBSD: auth2-pubkey.c,v 1.86 2018/09/20 03:28:06 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>
57 #include "pathnames.h"
59 #include "auth-options.h"
64 #include "monitor_wrap.h"
68 #include "channels.h" /* XXX for session.h */
69 #include "session.h" /* XXX for child_set_env(); refactor? */
72 extern ServerOptions options;
73 extern u_char *session_id2;
74 extern u_int session_id2_len;
77 format_key(const struct sshkey *key)
79 char *ret, *fp = sshkey_fingerprint(key,
80 options.fingerprint_hash, SSH_FP_DEFAULT);
82 xasprintf(&ret, "%s %s", sshkey_type(key), fp);
88 userauth_pubkey(struct ssh *ssh)
90 Authctxt *authctxt = ssh->authctxt;
91 struct passwd *pw = authctxt->pw;
92 struct sshbuf *b = NULL;
93 struct sshkey *key = NULL;
94 char *pkalg = NULL, *userstyle = NULL, *key_s = NULL, *ca_s = NULL;
95 u_char *pkblob = NULL, *sig = NULL, have_sig;
98 int authenticated = 0;
99 struct sshauthopt *authopts = NULL;
101 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));
105 pktype = sshkey_type_from_name(pkalg);
106 if (pktype == KEY_UNSPEC) {
107 /* this is perfectly legal */
108 verbose("%s: unsupported public key algorithm: %s",
112 if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
113 error("%s: could not parse key: %s", __func__, ssh_err(r));
117 error("%s: cannot decode key: %s", __func__, pkalg);
120 if (key->type != pktype) {
121 error("%s: type mismatch for decoded key "
122 "(received %d, expected %d)", __func__, key->type, pktype);
125 if (sshkey_type_plain(key->type) == KEY_RSA &&
126 (ssh->compat & SSH_BUG_RSASIGMD5) != 0) {
127 logit("Refusing RSA key because client uses unsafe "
131 if (auth2_key_already_used(authctxt, key)) {
132 logit("refusing previously-used %s key", sshkey_type(key));
135 if (match_pattern_list(pkalg, options.pubkey_key_types, 0) != 1) {
136 logit("%s: key type %s not in PubkeyAcceptedKeyTypes",
137 __func__, sshkey_ssh_name(key));
140 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));
147 key_s = format_key(key);
148 if (sshkey_is_cert(key))
149 ca_s = format_key(key->cert->signature_key);
152 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);
156 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));
167 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));
172 if (!authctxt->valid || authctxt->user == NULL) {
173 debug2("%s: disabled because of invalid user",
177 /* reconstruct packet */
178 xasprintf(&userstyle, "%s%s%s", authctxt->user,
179 authctxt->style ? ":" : "",
180 authctxt->style ? authctxt->style : "");
181 if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
182 (r = sshbuf_put_cstring(b, userstyle)) != 0 ||
183 (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 ||
186 (r = sshbuf_put_cstring(b, pkalg)) != 0 ||
187 (r = sshbuf_put_string(b, pkblob, blen)) != 0)
188 fatal("%s: build packet failed: %s",
189 __func__, ssh_err(r));
191 sshbuf_dump(b, stderr);
193 /* test for correct signature */
195 if (PRIVSEP(user_key_allowed(ssh, pw, key, 1, &authopts)) &&
196 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) {
202 auth2_record_key(authctxt, authenticated, key);
204 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);
209 if ((r = sshpkt_get_end(ssh)) != 0)
210 fatal("%s: %s", __func__, ssh_err(r));
212 if (!authctxt->valid || authctxt->user == NULL) {
213 debug2("%s: disabled because of invalid user",
217 /* XXX fake reply and always send PK_OK ? */
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
225 if (PRIVSEP(user_key_allowed(ssh, pw, key, 0, NULL))) {
226 if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_PK_OK))
228 (r = sshpkt_put_cstring(ssh, pkalg)) != 0 ||
229 (r = sshpkt_put_string(ssh, pkblob, blen)) != 0 ||
230 (r = sshpkt_send(ssh)) != 0 ||
231 (r = ssh_packet_write_wait(ssh)) != 0)
232 fatal("%s: %s", __func__, ssh_err(r));
233 authctxt->postponed = 1;
237 if (authenticated == 1 && auth_activate_options(ssh, authopts) != 0) {
238 debug("%s: key options inconsistent with existing", __func__);
241 debug2("%s: authenticated %d pkalg %s", __func__, authenticated, pkalg);
244 sshauthopt_free(authopts);
252 return authenticated;
256 match_principals_option(const char *principal_list, struct sshkey_cert *cert)
261 /* XXX percent_expand() sequences for authorized_principals? */
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\"",
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.
281 check_principals_line(struct ssh *ssh, char *cp, const struct sshkey_cert *cert,
282 const char *loc, struct sshauthopt **authoptsp)
285 char *ep, *line_opts;
286 const char *reason = NULL;
287 struct sshauthopt *opts = NULL;
289 if (authoptsp != NULL)
292 /* Trim trailing whitespace. */
293 ep = cp + strlen(cp) - 1;
294 while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t'))
298 * If the line has internal whitespace then assume it has
302 if ((ep = strrchr(cp, ' ')) != NULL ||
303 (ep = strrchr(cp, '\t')) != NULL) {
304 for (; *ep == ' ' || *ep == '\t'; ep++)
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);
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)
318 debug3("%s: matched principal \"%.100s\"",
319 loc, cert->principals[i]);
322 if (found && authoptsp != NULL) {
326 sshauthopt_free(opts);
327 return found ? 0 : -1;
331 process_principals(struct ssh *ssh, FILE *f, const char *file,
332 const struct sshkey_cert *cert, struct sshauthopt **authoptsp)
334 char loc[256], *line = NULL, *cp, *ep;
337 u_int found_principal = 0;
339 if (authoptsp != NULL)
342 while (getline(&line, &linesize, f) != -1) {
344 /* Always consume entire input */
348 /* Skip leading whitespace. */
349 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
351 /* Skip blank and comment lines. */
352 if ((ep = strchr(cp, '#')) != NULL)
354 if (!*cp || *cp == '\n')
357 snprintf(loc, sizeof(loc), "%.200s:%lu", file, linenum);
358 if (check_principals_line(ssh, cp, cert, loc, authoptsp) == 0)
362 return found_principal;
365 /* XXX remove pw args here and elsewhere once ssh->authctxt is guaranteed */
368 match_principals_file(struct ssh *ssh, struct passwd *pw, char *file,
369 struct sshkey_cert *cert, struct sshauthopt **authoptsp)
374 if (authoptsp != NULL)
377 temporarily_use_uid(pw);
378 debug("trying authorized principals file %s", file);
379 if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) {
383 success = process_principals(ssh, f, file, cert, authoptsp);
390 * Checks whether principal is allowed in output of command.
391 * returns 1 if the principal is allowed or 0 otherwise.
394 match_principals_command(struct ssh *ssh, struct passwd *user_pw,
395 const struct sshkey *key, struct sshauthopt **authoptsp)
397 struct passwd *runas_pw = NULL;
398 const struct sshkey_cert *cert = key->cert;
400 int r, ok, found_principal = 0;
401 int i, ac = 0, uid_swapped = 0;
403 char *tmp, *username = NULL, *command = NULL, **av = NULL;
404 char *ca_fp = NULL, *key_fp = NULL, *catext = NULL, *keytext = NULL;
405 char serial_s[16], uidstr[32];
406 void (*osigchld)(int);
408 if (authoptsp != NULL)
410 if (options.authorized_principals_command == NULL)
412 if (options.authorized_principals_command_user == NULL) {
413 error("No user for AuthorizedPrincipalsCommand specified, "
419 * NB. all returns later this function should go via "out" to
420 * ensure the original SIGCHLD handler is restored properly.
422 osigchld = signal(SIGCHLD, SIG_DFL);
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);
427 runas_pw = getpwnam(username);
428 if (runas_pw == NULL) {
429 error("AuthorizedPrincipalsCommandUser \"%s\" not found: %s",
430 username, strerror(errno));
434 /* Turn the command into an argument vector */
435 if (argv_split(options.authorized_principals_command, &ac, &av) != 0) {
436 error("AuthorizedPrincipalsCommand \"%s\" contains "
437 "invalid quotes", command);
441 error("AuthorizedPrincipalsCommand \"%s\" yielded no arguments",
445 if ((ca_fp = sshkey_fingerprint(cert->signature_key,
446 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
447 error("%s: sshkey_fingerprint failed", __func__);
450 if ((key_fp = sshkey_fingerprint(key,
451 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
452 error("%s: sshkey_fingerprint failed", __func__);
455 if ((r = sshkey_to_base64(cert->signature_key, &catext)) != 0) {
456 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r));
459 if ((r = sshkey_to_base64(key, &keytext)) != 0) {
460 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r));
463 snprintf(serial_s, sizeof(serial_s), "%llu",
464 (unsigned long long)cert->serial);
465 snprintf(uidstr, sizeof(uidstr), "%llu",
466 (unsigned long long)user_pw->pw_uid);
467 for (i = 1; i < ac; i++) {
468 tmp = percent_expand(av[i],
470 "u", user_pw->pw_name,
471 "h", user_pw->pw_dir,
472 "t", sshkey_ssh_name(key),
473 "T", sshkey_ssh_name(cert->signature_key),
482 fatal("%s: percent_expand failed", __func__);
486 /* Prepare a printable command for logs, etc. */
487 command = argv_assemble(ac, av);
489 if ((pid = subprocess("AuthorizedPrincipalsCommand", runas_pw, command,
491 SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD)) == 0)
495 temporarily_use_uid(runas_pw);
497 ok = process_principals(ssh, f, "(command)", cert, authoptsp);
502 if (exited_cleanly(pid, "AuthorizedPrincipalsCommand", command, 0) != 0)
505 /* Read completed successfully */
506 found_principal = ok;
510 signal(SIGCHLD, osigchld);
511 for (i = 0; i < ac; i++)
522 return found_principal;
526 skip_space(char **cpp)
530 for (cp = *cpp; *cp == ' ' || *cp == '\t'; cp++)
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).
541 advance_past_options(char **cpp)
546 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
547 if (*cp == '\\' && cp[1] == '"')
548 cp++; /* Skip both */
553 /* return failure for unterminated quotes */
554 return (*cp == '\0' && quoted) ? -1 : 0;
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.
563 check_authkey_line(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
564 char *cp, const char *loc, struct sshauthopt **authoptsp)
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;
573 if (authoptsp != NULL)
576 if ((found = sshkey_new(want_keytype)) == NULL) {
577 debug3("%s: keytype %d failed", __func__, want_keytype);
581 /* XXX djm: peek at key type in line and skip if unwanted */
583 if (sshkey_read(found, &cp) != 0) {
584 /* no key? check for options */
585 debug2("%s: check options: '%s'", loc, cp);
587 if (advance_past_options(&cp) != 0) {
588 reason = "invalid key option string";
592 if (sshkey_read(found, &cp) != 0) {
593 /* still no key? advance to next line*/
594 debug2("%s: advance: '%s'", loc, cp);
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);
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)
611 /* Plain key: check it against key found in file */
612 if (!sshkey_equal(found, key) || keyopts->cert_authority)
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__);
621 debug("%s: matching %s found: %s %s", loc,
622 sshkey_is_cert(key) ? "CA" : "key", sshkey_type(found), fp);
624 if (auth_authorise_keyopts(ssh, pw, keyopts,
625 sshkey_is_cert(key), loc) != 0) {
626 reason = "Refused by key options";
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);
639 * Additional authorisation for certificates.
642 /* Parse and check options present in certificate */
643 if ((certopts = sshauthopt_from_cert(key)) == NULL) {
644 reason = "Invalid certificate options";
647 if (auth_authorise_keyopts(ssh, pw, certopts, 0, loc) != 0) {
648 reason = "Refused by certificate options";
651 if ((finalopts = sshauthopt_merge(keyopts, certopts, &reason)) == NULL)
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.
659 if (keyopts->cert_principals != NULL &&
660 !match_principals_option(keyopts->cert_principals, key->cert)) {
661 reason = "Certificate does not contain an authorized principal";
664 if (sshkey_cert_check_authority(key, 0, 0,
665 keyopts->cert_principals == NULL ? pw->pw_name : NULL, &reason) != 0)
668 verbose("Accepted certificate ID \"%s\" (serial %llu) "
669 "signed by CA %s %s found at %s",
671 (unsigned long long)key->cert->serial,
672 sshkey_type(found), fp, loc);
675 if (finalopts == NULL)
676 fatal("%s: internal error: missing options", __func__);
677 if (authoptsp != NULL) {
678 *authoptsp = finalopts;
687 auth_debug_add("%s", reason);
690 sshauthopt_free(keyopts);
691 sshauthopt_free(certopts);
692 sshauthopt_free(finalopts);
698 * Checks whether key is allowed in authorized_keys-format file,
699 * returns 1 if the key is allowed or 0 otherwise.
702 check_authkeys_file(struct ssh *ssh, struct passwd *pw, FILE *f,
703 char *file, struct sshkey *key, struct sshauthopt **authoptsp)
705 char *cp, *line = NULL, loc[256];
710 if (authoptsp != NULL)
713 while (getline(&line, &linesize, f) != -1) {
715 /* Always consume entire file */
719 /* Skip leading whitespace, empty and comment lines. */
722 if (!*cp || *cp == '\n' || *cp == '#')
724 snprintf(loc, sizeof(loc), "%.200s:%lu", file, linenum);
725 if (check_authkey_line(ssh, pw, key, cp, loc, authoptsp) == 0)
732 /* Authenticate a certificate key against TrustedUserCAKeys */
734 user_cert_trusted_ca(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
735 struct sshauthopt **authoptsp)
737 char *ca_fp, *principals_file = NULL;
739 struct sshauthopt *principals_opts = NULL, *cert_opts = NULL;
740 struct sshauthopt *final_opts = NULL;
741 int r, ret = 0, found_principal = 0, use_authorized_principals;
743 if (authoptsp != NULL)
746 if (!sshkey_is_cert(key) || options.trusted_user_ca_keys == NULL)
749 if ((ca_fp = sshkey_fingerprint(key->cert->signature_key,
750 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
753 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));
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.
765 if ((principals_file = authorized_principals_file(pw)) != NULL) {
766 if (match_principals_file(ssh, pw, principals_file,
767 key->cert, &principals_opts))
770 /* Try querying command if specified */
771 if (!found_principal && match_principals_command(ssh, pw, key,
774 /* 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) {
778 reason = "Certificate does not contain an authorized principal";
781 if (use_authorized_principals && principals_opts == NULL)
782 fatal("%s: internal error: missing principals_opts", __func__);
783 if (sshkey_cert_check_authority(key, 0, 1,
784 use_authorized_principals ? NULL : pw->pw_name, &reason) != 0)
787 /* 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";
792 if (auth_authorise_keyopts(ssh, pw, cert_opts, 0, "cert") != 0) {
793 reason = "Refused by certificate options";
796 if (principals_opts == NULL) {
797 final_opts = cert_opts;
800 if (auth_authorise_keyopts(ssh, pw, principals_opts, 0,
801 "principals") != 0) {
802 reason = "Refused by certificate principals options";
805 if ((final_opts = sshauthopt_merge(principals_opts,
806 cert_opts, &reason)) == NULL) {
809 auth_debug_add("%s", reason);
815 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,
818 sshkey_type(key->cert->signature_key), ca_fp,
819 options.trusted_user_ca_keys);
820 if (authoptsp != NULL) {
821 *authoptsp = final_opts;
826 sshauthopt_free(principals_opts);
827 sshauthopt_free(cert_opts);
828 sshauthopt_free(final_opts);
829 free(principals_file);
835 * Checks whether key is allowed in file.
836 * returns 1 if the key is allowed or 0 otherwise.
839 user_key_allowed2(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
840 char *file, struct sshauthopt **authoptsp)
845 if (authoptsp != NULL)
848 /* Temporarily use the user's uid. */
849 temporarily_use_uid(pw);
851 debug("trying public key file %s", file);
852 if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) {
853 found_key = check_authkeys_file(ssh, pw, f, file,
863 * Checks whether key is allowed in output of command.
864 * returns 1 if the key is allowed or 0 otherwise.
867 user_key_command_allowed2(struct ssh *ssh, struct passwd *user_pw,
868 struct sshkey *key, struct sshauthopt **authoptsp)
870 struct passwd *runas_pw = NULL;
872 int r, ok, found_key = 0;
873 int i, uid_swapped = 0, ac = 0;
875 char *username = NULL, *key_fp = NULL, *keytext = NULL;
876 char uidstr[32], *tmp, *command = NULL, **av = NULL;
877 void (*osigchld)(int);
879 if (authoptsp != NULL)
881 if (options.authorized_keys_command == NULL)
883 if (options.authorized_keys_command_user == NULL) {
884 error("No user for AuthorizedKeysCommand specified, skipping");
889 * NB. all returns later this function should go via "out" to
890 * ensure the original SIGCHLD handler is restored properly.
892 osigchld = signal(SIGCHLD, SIG_DFL);
894 /* Prepare and verify the user for the command */
895 username = percent_expand(options.authorized_keys_command_user,
896 "u", user_pw->pw_name, (char *)NULL);
897 runas_pw = getpwnam(username);
898 if (runas_pw == NULL) {
899 error("AuthorizedKeysCommandUser \"%s\" not found: %s",
900 username, strerror(errno));
904 /* Prepare AuthorizedKeysCommand */
905 if ((key_fp = sshkey_fingerprint(key, options.fingerprint_hash,
906 SSH_FP_DEFAULT)) == NULL) {
907 error("%s: sshkey_fingerprint failed", __func__);
910 if ((r = sshkey_to_base64(key, &keytext)) != 0) {
911 error("%s: sshkey_to_base64 failed: %s", __func__, ssh_err(r));
915 /* Turn the command into an argument vector */
916 if (argv_split(options.authorized_keys_command, &ac, &av) != 0) {
917 error("AuthorizedKeysCommand \"%s\" contains invalid quotes",
922 error("AuthorizedKeysCommand \"%s\" yielded no arguments",
926 snprintf(uidstr, sizeof(uidstr), "%llu",
927 (unsigned long long)user_pw->pw_uid);
928 for (i = 1; i < ac; i++) {
929 tmp = percent_expand(av[i],
931 "u", user_pw->pw_name,
932 "h", user_pw->pw_dir,
933 "t", sshkey_ssh_name(key),
938 fatal("%s: percent_expand failed", __func__);
942 /* Prepare a printable command for logs, etc. */
943 command = argv_assemble(ac, av);
946 * If AuthorizedKeysCommand was run without arguments
947 * then fall back to the old behaviour of passing the
948 * target username as a single argument.
951 av = xreallocarray(av, ac + 2, sizeof(*av));
952 av[1] = xstrdup(user_pw->pw_name);
954 /* Fix up command too, since it is used in log messages */
956 xasprintf(&command, "%s %s", av[0], av[1]);
959 if ((pid = subprocess("AuthorizedKeysCommand", runas_pw, command,
961 SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD)) == 0)
965 temporarily_use_uid(runas_pw);
967 ok = check_authkeys_file(ssh, user_pw, f,
968 options.authorized_keys_command, key, authoptsp);
973 if (exited_cleanly(pid, "AuthorizedKeysCommand", command, 0) != 0)
976 /* Read completed successfully */
981 signal(SIGCHLD, osigchld);
982 for (i = 0; i < ac; i++)
995 * Check whether key authenticates and authorises the user.
998 user_key_allowed(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
999 int auth_attempt, struct sshauthopt **authoptsp)
1003 struct sshauthopt *opts = NULL;
1004 if (authoptsp != NULL)
1007 if (auth_key_is_revoked(key))
1009 if (sshkey_is_cert(key) &&
1010 auth_key_is_revoked(key->cert->signature_key))
1013 if ((success = user_cert_trusted_ca(ssh, pw, key, &opts)) != 0)
1015 sshauthopt_free(opts);
1018 if ((success = user_key_command_allowed2(ssh, pw, key, &opts)) != 0)
1020 sshauthopt_free(opts);
1023 for (i = 0; !success && i < options.num_authkeys_files; i++) {
1024 if (strcasecmp(options.authorized_keys_files[i], "none") == 0)
1026 file = expand_authorized_keys(
1027 options.authorized_keys_files[i], pw);
1028 success = user_key_allowed2(ssh, pw, key, file, &opts);
1033 if (success && authoptsp != NULL) {
1037 sshauthopt_free(opts);
1041 Authmethod method_pubkey = {
1044 &options.pubkey_authentication