1 /* $OpenBSD: auth2-pubkeyfile.c,v 1.4 2023/03/05 05:34:09 dtucker Exp $ */
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Copyright (c) 2010 Damien Miller. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/types.h>
49 #include "auth-options.h"
55 auth_authorise_keyopts(struct passwd *pw, struct sshauthopt *opts,
56 int allow_cert_authority, const char *remote_ip, const char *remote_host,
59 time_t now = time(NULL);
63 * Check keys/principals file expiry time.
64 * NB. validity interval in certificate is handled elsewhere.
66 if (opts->valid_before && now > 0 &&
67 opts->valid_before < (uint64_t)now) {
68 format_absolute_time(opts->valid_before, buf, sizeof(buf));
69 debug("%s: entry expired at %s", loc, buf);
70 auth_debug_add("%s: entry expired at %s", loc, buf);
73 /* Consistency checks */
74 if (opts->cert_principals != NULL && !opts->cert_authority) {
75 debug("%s: principals on non-CA key", loc);
76 auth_debug_add("%s: principals on non-CA key", loc);
80 /* cert-authority flag isn't valid in authorized_principals files */
81 if (!allow_cert_authority && opts->cert_authority) {
82 debug("%s: cert-authority flag invalid here", loc);
83 auth_debug_add("%s: cert-authority flag invalid here", loc);
88 /* Perform from= checks */
89 if (opts->required_from_host_keys != NULL) {
90 switch (match_host_and_ip(remote_host, remote_ip,
91 opts->required_from_host_keys )) {
93 /* Host name matches. */
97 debug("%s: invalid from criteria", loc);
98 auth_debug_add("%s: invalid from criteria", loc);
101 logit("%s: Authentication tried for %.100s with "
102 "correct key but not from a permitted "
103 "host (host=%.200s, ip=%.200s, required=%.200s).",
104 loc, pw->pw_name, remote_host, remote_ip,
105 opts->required_from_host_keys);
106 auth_debug_add("%s: Your host '%.200s' is not "
107 "permitted to use this key for login.",
113 /* Check source-address restriction from certificate */
114 if (opts->required_from_host_cert != NULL) {
115 switch (addr_match_cidr_list(remote_ip,
116 opts->required_from_host_cert)) {
123 error("%s: Certificate source-address invalid", loc);
126 logit("%s: Authentication tried for %.100s with valid "
127 "certificate but not from a permitted source "
128 "address (%.200s).", loc, pw->pw_name, remote_ip);
129 auth_debug_add("%s: Your address '%.200s' is not "
130 "permitted to use this certificate for login.",
137 * XXX this is spammy. We should report remotely only for keys
138 * that are successful in actual auth attempts, and not PK_OK
141 auth_log_authopts(loc, opts, 1);
147 match_principals_option(const char *principal_list, struct sshkey_cert *cert)
152 /* XXX percent_expand() sequences for authorized_principals? */
154 for (i = 0; i < cert->nprincipals; i++) {
155 if ((result = match_list(cert->principals[i],
156 principal_list, NULL)) != NULL) {
157 debug3("matched principal from key options \"%.100s\"",
167 * Process a single authorized_principals format line. Returns 0 and sets
168 * authoptsp is principal is authorised, -1 otherwise. "loc" is used as a
169 * log preamble for file/line information.
172 auth_check_principals_line(char *cp, const struct sshkey_cert *cert,
173 const char *loc, struct sshauthopt **authoptsp)
176 char *ep, *line_opts;
177 const char *reason = NULL;
178 struct sshauthopt *opts = NULL;
180 if (authoptsp != NULL)
183 /* Trim trailing whitespace. */
184 ep = cp + strlen(cp) - 1;
185 while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t'))
189 * If the line has internal whitespace then assume it has
193 if ((ep = strrchr(cp, ' ')) != NULL ||
194 (ep = strrchr(cp, '\t')) != NULL) {
195 for (; *ep == ' ' || *ep == '\t'; ep++)
200 if ((opts = sshauthopt_parse(line_opts, &reason)) == NULL) {
201 debug("%s: bad principals options: %s", loc, reason);
202 auth_debug_add("%s: bad principals options: %s", loc, reason);
205 /* Check principals in cert against those on line */
206 for (i = 0; i < cert->nprincipals; i++) {
207 if (strcmp(cp, cert->principals[i]) != 0)
209 debug3("%s: matched principal \"%.100s\"",
210 loc, cert->principals[i]);
213 if (found && authoptsp != NULL) {
217 sshauthopt_free(opts);
218 return found ? 0 : -1;
222 auth_process_principals(FILE *f, const char *file,
223 const struct sshkey_cert *cert, struct sshauthopt **authoptsp)
225 char loc[256], *line = NULL, *cp, *ep;
227 u_long linenum = 0, nonblank = 0;
228 u_int found_principal = 0;
230 if (authoptsp != NULL)
233 while (getline(&line, &linesize, f) != -1) {
235 /* Always consume entire input */
239 /* Skip leading whitespace. */
240 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
242 /* Skip blank and comment lines. */
243 if ((ep = strchr(cp, '#')) != NULL)
245 if (!*cp || *cp == '\n')
249 snprintf(loc, sizeof(loc), "%.200s:%lu", file, linenum);
250 if (auth_check_principals_line(cp, cert, loc, authoptsp) == 0)
253 debug2_f("%s: processed %lu/%lu lines", file, nonblank, linenum);
255 return found_principal;
259 * Check a single line of an authorized_keys-format file. Returns 0 if key
260 * matches, -1 otherwise. Will return key/cert options via *authoptsp
261 * on success. "loc" is used as file/line location in log messages.
264 auth_check_authkey_line(struct passwd *pw, struct sshkey *key,
265 char *cp, const char *remote_ip, const char *remote_host, const char *loc,
266 struct sshauthopt **authoptsp)
268 int want_keytype = sshkey_is_cert(key) ? KEY_UNSPEC : key->type;
269 struct sshkey *found = NULL;
270 struct sshauthopt *keyopts = NULL, *certopts = NULL, *finalopts = NULL;
271 char *key_options = NULL, *fp = NULL;
272 const char *reason = NULL;
275 if (authoptsp != NULL)
278 if ((found = sshkey_new(want_keytype)) == NULL) {
279 debug3_f("keytype %d failed", want_keytype);
283 /* XXX djm: peek at key type in line and skip if unwanted */
285 if (sshkey_read(found, &cp) != 0) {
286 /* no key? check for options */
287 debug2("%s: check options: '%s'", loc, cp);
289 if (sshkey_advance_past_options(&cp) != 0) {
290 reason = "invalid key option string";
294 if (sshkey_read(found, &cp) != 0) {
295 /* still no key? advance to next line*/
296 debug2("%s: advance: '%s'", loc, cp);
300 /* Parse key options now; we need to know if this is a CA key */
301 if ((keyopts = sshauthopt_parse(key_options, &reason)) == NULL) {
302 debug("%s: bad key options: %s", loc, reason);
303 auth_debug_add("%s: bad key options: %s", loc, reason);
306 /* Ignore keys that don't match or incorrectly marked as CAs */
307 if (sshkey_is_cert(key)) {
308 /* Certificate; check signature key against CA */
309 if (!sshkey_equal(found, key->cert->signature_key) ||
310 !keyopts->cert_authority)
313 /* Plain key: check it against key found in file */
314 if (!sshkey_equal(found, key) || keyopts->cert_authority)
318 /* We have a candidate key, perform authorisation checks */
319 if ((fp = sshkey_fingerprint(found,
320 SSH_FP_HASH_DEFAULT, SSH_FP_DEFAULT)) == NULL)
321 fatal_f("fingerprint failed");
323 debug("%s: matching %s found: %s %s", loc,
324 sshkey_is_cert(key) ? "CA" : "key", sshkey_type(found), fp);
326 if (auth_authorise_keyopts(pw, keyopts,
327 sshkey_is_cert(key), remote_ip, remote_host, loc) != 0) {
328 reason = "Refused by key options";
331 /* That's all we need for plain keys. */
332 if (!sshkey_is_cert(key)) {
333 verbose("Accepted key %s %s found at %s",
334 sshkey_type(found), fp, loc);
341 * Additional authorisation for certificates.
344 /* Parse and check options present in certificate */
345 if ((certopts = sshauthopt_from_cert(key)) == NULL) {
346 reason = "Invalid certificate options";
349 if (auth_authorise_keyopts(pw, certopts, 0,
350 remote_ip, remote_host, loc) != 0) {
351 reason = "Refused by certificate options";
354 if ((finalopts = sshauthopt_merge(keyopts, certopts, &reason)) == NULL)
358 * If the user has specified a list of principals as
359 * a key option, then prefer that list to matching
360 * their username in the certificate principals list.
362 if (keyopts->cert_principals != NULL &&
363 !match_principals_option(keyopts->cert_principals, key->cert)) {
364 reason = "Certificate does not contain an authorized principal";
367 if (sshkey_cert_check_authority_now(key, 0, 0, 0,
368 keyopts->cert_principals == NULL ? pw->pw_name : NULL,
372 verbose("Accepted certificate ID \"%s\" (serial %llu) "
373 "signed by CA %s %s found at %s",
375 (unsigned long long)key->cert->serial,
376 sshkey_type(found), fp, loc);
379 if (finalopts == NULL)
380 fatal_f("internal error: missing options");
381 if (authoptsp != NULL) {
382 *authoptsp = finalopts;
391 auth_debug_add("%s", reason);
394 sshauthopt_free(keyopts);
395 sshauthopt_free(certopts);
396 sshauthopt_free(finalopts);
402 * Checks whether key is allowed in authorized_keys-format file,
403 * returns 1 if the key is allowed or 0 otherwise.
406 auth_check_authkeys_file(struct passwd *pw, FILE *f, char *file,
407 struct sshkey *key, const char *remote_ip,
408 const char *remote_host, struct sshauthopt **authoptsp)
410 char *cp, *line = NULL, loc[256];
413 u_long linenum = 0, nonblank = 0;
415 if (authoptsp != NULL)
418 while (getline(&line, &linesize, f) != -1) {
420 /* Always consume entire file */
424 /* Skip leading whitespace, empty and comment lines. */
427 if (!*cp || *cp == '\n' || *cp == '#')
431 snprintf(loc, sizeof(loc), "%.200s:%lu", file, linenum);
432 if (auth_check_authkey_line(pw, key, cp,
433 remote_ip, remote_host, loc, authoptsp) == 0)
437 debug2_f("%s: processed %lu/%lu lines", file, nonblank, linenum);
442 auth_openfile(const char *file, struct passwd *pw, int strict_modes,
443 int log_missing, char *file_type)
450 if ((fd = open(file, O_RDONLY|O_NONBLOCK)) == -1) {
451 if (errno != ENOENT) {
452 logit("Could not open user '%s' %s '%s': %s",
453 pw->pw_name, file_type, file, strerror(errno));
454 } else if (log_missing) {
455 debug("Could not open user '%s' %s '%s': %s",
456 pw->pw_name, file_type, file, strerror(errno));
461 if (fstat(fd, &st) == -1) {
465 if (!S_ISREG(st.st_mode)) {
466 logit("User '%s' %s '%s' is not a regular file",
467 pw->pw_name, file_type, file);
472 if ((f = fdopen(fd, "r")) == NULL) {
477 safe_path_fd(fileno(f), file, pw, line, sizeof(line)) != 0) {
479 logit("Authentication refused: %s", line);
480 auth_debug_add("Ignored %s: %s", file_type, line);
489 auth_openkeyfile(const char *file, struct passwd *pw, int strict_modes)
491 return auth_openfile(file, pw, strict_modes, 1, "authorized keys");
495 auth_openprincipals(const char *file, struct passwd *pw, int strict_modes)
497 return auth_openfile(file, pw, strict_modes, 0,
498 "authorized principals");