djm@openbsd.org | 1f729f0 | 2015-01-13 07:39:19 +0000 | [diff] [blame] | 1 | /* $OpenBSD: auth2-pubkey.c,v 1.45 2015/01/13 07:39:19 djm Exp $ */ |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 2 | /* |
| 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 Miller | f17883e | 2006-03-15 11:45:54 +1100 | [diff] [blame] | 27 | |
| 28 | #include <sys/types.h> |
| 29 | #include <sys/stat.h> |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 30 | #include <sys/wait.h> |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 31 | |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 32 | #include <errno.h> |
Darren Tucker | 06db584 | 2008-06-13 14:51:28 +1000 | [diff] [blame] | 33 | #include <fcntl.h> |
Darren Tucker | 737f7af | 2012-11-05 17:07:43 +1100 | [diff] [blame] | 34 | #ifdef HAVE_PATHS_H |
| 35 | # include <paths.h> |
| 36 | #endif |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 37 | #include <pwd.h> |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 38 | #include <signal.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 39 | #include <stdio.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 40 | #include <stdarg.h> |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 41 | #include <string.h> |
| 42 | #include <time.h> |
Darren Tucker | d9526a5 | 2008-06-14 09:01:24 +1000 | [diff] [blame] | 43 | #include <unistd.h> |
djm@openbsd.org | f69b69b | 2014-12-22 07:51:30 +0000 | [diff] [blame] | 44 | #include <limits.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 45 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 46 | #include "xmalloc.h" |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 47 | #include "ssh.h" |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 48 | #include "ssh2.h" |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 49 | #include "packet.h" |
| 50 | #include "buffer.h" |
| 51 | #include "log.h" |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 52 | #include "misc.h" |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 53 | #include "servconf.h" |
| 54 | #include "compat.h" |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 55 | #include "key.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 56 | #include "hostfile.h" |
| 57 | #include "auth.h" |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 58 | #include "pathnames.h" |
| 59 | #include "uidswap.h" |
| 60 | #include "auth-options.h" |
| 61 | #include "canohost.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 62 | #ifdef GSSAPI |
| 63 | #include "ssh-gss.h" |
| 64 | #endif |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 65 | #include "monitor_wrap.h" |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 66 | #include "authfile.h" |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 67 | #include "match.h" |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 68 | |
| 69 | /* import */ |
| 70 | extern ServerOptions options; |
| 71 | extern u_char *session_id2; |
Darren Tucker | 502d384 | 2003-06-28 12:38:01 +1000 | [diff] [blame] | 72 | extern u_int session_id2_len; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 73 | |
| 74 | static int |
| 75 | userauth_pubkey(Authctxt *authctxt) |
| 76 | { |
| 77 | Buffer b; |
| 78 | Key *key = NULL; |
Damien Miller | 4ce189d | 2013-04-23 15:17:52 +1000 | [diff] [blame] | 79 | char *pkalg, *userstyle; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 80 | u_char *pkblob, *sig; |
| 81 | u_int alen, blen, slen; |
| 82 | int have_sig, pktype; |
| 83 | int authenticated = 0; |
| 84 | |
| 85 | if (!authctxt->valid) { |
| 86 | debug2("userauth_pubkey: disabled because of invalid user"); |
| 87 | return 0; |
| 88 | } |
| 89 | have_sig = packet_get_char(); |
| 90 | if (datafellows & SSH_BUG_PKAUTH) { |
| 91 | debug2("userauth_pubkey: SSH_BUG_PKAUTH"); |
| 92 | /* no explicit pkalg given */ |
| 93 | pkblob = packet_get_string(&blen); |
| 94 | buffer_init(&b); |
| 95 | buffer_append(&b, pkblob, blen); |
| 96 | /* so we have to extract the pkalg from the pkblob */ |
| 97 | pkalg = buffer_get_string(&b, &alen); |
| 98 | buffer_free(&b); |
| 99 | } else { |
| 100 | pkalg = packet_get_string(&alen); |
| 101 | pkblob = packet_get_string(&blen); |
| 102 | } |
| 103 | pktype = key_type_from_name(pkalg); |
| 104 | if (pktype == KEY_UNSPEC) { |
| 105 | /* this is perfectly legal */ |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 106 | logit("userauth_pubkey: unsupported public key algorithm: %s", |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 107 | pkalg); |
| 108 | goto done; |
| 109 | } |
| 110 | key = key_from_blob(pkblob, blen); |
| 111 | if (key == NULL) { |
| 112 | error("userauth_pubkey: cannot decode key: %s", pkalg); |
| 113 | goto done; |
| 114 | } |
| 115 | if (key->type != pktype) { |
| 116 | error("userauth_pubkey: type mismatch for decoded key " |
| 117 | "(received %d, expected %d)", key->type, pktype); |
| 118 | goto done; |
| 119 | } |
Damien Miller | 324541e | 2013-12-31 12:25:40 +1100 | [diff] [blame] | 120 | if (key_type_plain(key->type) == KEY_RSA && |
| 121 | (datafellows & SSH_BUG_RSASIGMD5) != 0) { |
| 122 | logit("Refusing RSA key because client uses unsafe " |
| 123 | "signature scheme"); |
| 124 | goto done; |
| 125 | } |
djm@openbsd.org | f69b69b | 2014-12-22 07:51:30 +0000 | [diff] [blame] | 126 | if (auth2_userkey_already_used(authctxt, key)) { |
| 127 | logit("refusing previously-used %s key", key_type(key)); |
| 128 | goto done; |
| 129 | } |
djm@openbsd.org | 1f729f0 | 2015-01-13 07:39:19 +0000 | [diff] [blame] | 130 | if (match_pattern_list(sshkey_ssh_name(key), options.pubkey_key_types, |
| 131 | strlen(options.pubkey_key_types), 0) != 1) { |
| 132 | logit("%s: key type %s not in PubkeyAcceptedKeyTypes", |
| 133 | __func__, sshkey_ssh_name(key)); |
| 134 | goto done; |
| 135 | } |
| 136 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 137 | if (have_sig) { |
| 138 | sig = packet_get_string(&slen); |
| 139 | packet_check_eom(); |
| 140 | buffer_init(&b); |
| 141 | if (datafellows & SSH_OLD_SESSIONID) { |
| 142 | buffer_append(&b, session_id2, session_id2_len); |
| 143 | } else { |
| 144 | buffer_put_string(&b, session_id2, session_id2_len); |
| 145 | } |
| 146 | /* reconstruct packet */ |
| 147 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
Damien Miller | 4ce189d | 2013-04-23 15:17:52 +1000 | [diff] [blame] | 148 | xasprintf(&userstyle, "%s%s%s", authctxt->user, |
| 149 | authctxt->style ? ":" : "", |
| 150 | authctxt->style ? authctxt->style : ""); |
| 151 | buffer_put_cstring(&b, userstyle); |
| 152 | free(userstyle); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 153 | buffer_put_cstring(&b, |
| 154 | datafellows & SSH_BUG_PKSERVICE ? |
| 155 | "ssh-userauth" : |
| 156 | authctxt->service); |
| 157 | if (datafellows & SSH_BUG_PKAUTH) { |
| 158 | buffer_put_char(&b, have_sig); |
| 159 | } else { |
| 160 | buffer_put_cstring(&b, "publickey"); |
| 161 | buffer_put_char(&b, have_sig); |
| 162 | buffer_put_cstring(&b, pkalg); |
| 163 | } |
| 164 | buffer_put_string(&b, pkblob, blen); |
| 165 | #ifdef DEBUG_PK |
| 166 | buffer_dump(&b); |
| 167 | #endif |
Damien Miller | 20bdcd7 | 2013-07-18 16:10:09 +1000 | [diff] [blame] | 168 | pubkey_auth_info(authctxt, key, NULL); |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 169 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 170 | /* test for correct signature */ |
| 171 | authenticated = 0; |
| 172 | if (PRIVSEP(user_key_allowed(authctxt->pw, key)) && |
| 173 | PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b), |
djm@openbsd.org | f69b69b | 2014-12-22 07:51:30 +0000 | [diff] [blame] | 174 | buffer_len(&b))) == 1) { |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 175 | authenticated = 1; |
djm@openbsd.org | f69b69b | 2014-12-22 07:51:30 +0000 | [diff] [blame] | 176 | /* Record the successful key to prevent reuse */ |
| 177 | auth2_record_userkey(authctxt, key); |
| 178 | key = NULL; /* Don't free below */ |
| 179 | } |
Damien Miller | fb1310e | 2004-01-21 11:02:50 +1100 | [diff] [blame] | 180 | buffer_free(&b); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 181 | free(sig); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 182 | } else { |
| 183 | debug("test whether pkalg/pkblob are acceptable"); |
| 184 | packet_check_eom(); |
| 185 | |
| 186 | /* XXX fake reply and always send PK_OK ? */ |
| 187 | /* |
| 188 | * XXX this allows testing whether a user is allowed |
| 189 | * to login: if you happen to have a valid pubkey this |
| 190 | * message is sent. the message is NEVER sent at all |
| 191 | * if a user is not allowed to login. is this an |
| 192 | * issue? -markus |
| 193 | */ |
| 194 | if (PRIVSEP(user_key_allowed(authctxt->pw, key))) { |
| 195 | packet_start(SSH2_MSG_USERAUTH_PK_OK); |
| 196 | packet_put_string(pkalg, alen); |
| 197 | packet_put_string(pkblob, blen); |
| 198 | packet_send(); |
| 199 | packet_write_wait(); |
| 200 | authctxt->postponed = 1; |
| 201 | } |
| 202 | } |
| 203 | if (authenticated != 1) |
| 204 | auth_clear_options(); |
| 205 | done: |
| 206 | debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg); |
| 207 | if (key != NULL) |
| 208 | key_free(key); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 209 | free(pkalg); |
| 210 | free(pkblob); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 211 | return authenticated; |
| 212 | } |
| 213 | |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 214 | void |
Damien Miller | 20bdcd7 | 2013-07-18 16:10:09 +1000 | [diff] [blame] | 215 | pubkey_auth_info(Authctxt *authctxt, const Key *key, const char *fmt, ...) |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 216 | { |
Damien Miller | 20bdcd7 | 2013-07-18 16:10:09 +1000 | [diff] [blame] | 217 | char *fp, *extra; |
| 218 | va_list ap; |
| 219 | int i; |
| 220 | |
| 221 | extra = NULL; |
| 222 | if (fmt != NULL) { |
| 223 | va_start(ap, fmt); |
| 224 | i = vasprintf(&extra, fmt, ap); |
| 225 | va_end(ap); |
| 226 | if (i < 0 || extra == NULL) |
| 227 | fatal("%s: vasprintf failed", __func__); |
| 228 | } |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 229 | |
| 230 | if (key_is_cert(key)) { |
| 231 | fp = key_fingerprint(key->cert->signature_key, |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 232 | options.fingerprint_hash, SSH_FP_DEFAULT); |
Damien Miller | 20bdcd7 | 2013-07-18 16:10:09 +1000 | [diff] [blame] | 233 | auth_info(authctxt, "%s ID %s (serial %llu) CA %s %s%s%s", |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 234 | key_type(key), key->cert->key_id, |
| 235 | (unsigned long long)key->cert->serial, |
Damien Miller | 20bdcd7 | 2013-07-18 16:10:09 +1000 | [diff] [blame] | 236 | key_type(key->cert->signature_key), fp, |
| 237 | extra == NULL ? "" : ", ", extra == NULL ? "" : extra); |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 238 | free(fp); |
| 239 | } else { |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 240 | fp = key_fingerprint(key, options.fingerprint_hash, |
| 241 | SSH_FP_DEFAULT); |
Damien Miller | 20bdcd7 | 2013-07-18 16:10:09 +1000 | [diff] [blame] | 242 | auth_info(authctxt, "%s %s%s%s", key_type(key), fp, |
| 243 | extra == NULL ? "" : ", ", extra == NULL ? "" : extra); |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 244 | free(fp); |
| 245 | } |
Damien Miller | 20bdcd7 | 2013-07-18 16:10:09 +1000 | [diff] [blame] | 246 | free(extra); |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 247 | } |
| 248 | |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 249 | static int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 250 | match_principals_option(const char *principal_list, struct sshkey_cert *cert) |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 251 | { |
| 252 | char *result; |
| 253 | u_int i; |
| 254 | |
| 255 | /* XXX percent_expand() sequences for authorized_principals? */ |
| 256 | |
| 257 | for (i = 0; i < cert->nprincipals; i++) { |
| 258 | if ((result = match_list(cert->principals[i], |
| 259 | principal_list, NULL)) != NULL) { |
| 260 | debug3("matched principal from key options \"%.100s\"", |
| 261 | result); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 262 | free(result); |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 263 | return 1; |
| 264 | } |
| 265 | } |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | static int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 270 | match_principals_file(char *file, struct passwd *pw, struct sshkey_cert *cert) |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 271 | { |
| 272 | FILE *f; |
Damien Miller | 6018a36 | 2010-07-02 13:35:19 +1000 | [diff] [blame] | 273 | char line[SSH_MAX_PUBKEY_BYTES], *cp, *ep, *line_opts; |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 274 | u_long linenum = 0; |
| 275 | u_int i; |
| 276 | |
| 277 | temporarily_use_uid(pw); |
| 278 | debug("trying authorized principals file %s", file); |
| 279 | if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) { |
| 280 | restore_uid(); |
| 281 | return 0; |
| 282 | } |
| 283 | while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) { |
Damien Miller | 6018a36 | 2010-07-02 13:35:19 +1000 | [diff] [blame] | 284 | /* Skip leading whitespace. */ |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 285 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) |
| 286 | ; |
Damien Miller | 6018a36 | 2010-07-02 13:35:19 +1000 | [diff] [blame] | 287 | /* Skip blank and comment lines. */ |
| 288 | if ((ep = strchr(cp, '#')) != NULL) |
| 289 | *ep = '\0'; |
| 290 | if (!*cp || *cp == '\n') |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 291 | continue; |
Damien Miller | 6018a36 | 2010-07-02 13:35:19 +1000 | [diff] [blame] | 292 | /* Trim trailing whitespace. */ |
| 293 | ep = cp + strlen(cp) - 1; |
| 294 | while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t')) |
| 295 | *ep-- = '\0'; |
| 296 | /* |
| 297 | * If the line has internal whitespace then assume it has |
| 298 | * key options. |
| 299 | */ |
| 300 | line_opts = NULL; |
| 301 | if ((ep = strrchr(cp, ' ')) != NULL || |
| 302 | (ep = strrchr(cp, '\t')) != NULL) { |
| 303 | for (; *ep == ' ' || *ep == '\t'; ep++) |
Damien Miller | 188ea81 | 2010-12-01 11:50:14 +1100 | [diff] [blame] | 304 | ; |
Damien Miller | 6018a36 | 2010-07-02 13:35:19 +1000 | [diff] [blame] | 305 | line_opts = cp; |
| 306 | cp = ep; |
| 307 | } |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 308 | for (i = 0; i < cert->nprincipals; i++) { |
| 309 | if (strcmp(cp, cert->principals[i]) == 0) { |
Darren Tucker | af1a60e | 2011-10-02 18:59:59 +1100 | [diff] [blame] | 310 | debug3("matched principal \"%.100s\" " |
| 311 | "from file \"%s\" on line %lu", |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 312 | cert->principals[i], file, linenum); |
Damien Miller | 6018a36 | 2010-07-02 13:35:19 +1000 | [diff] [blame] | 313 | if (auth_parse_options(pw, line_opts, |
| 314 | file, linenum) != 1) |
| 315 | continue; |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 316 | fclose(f); |
| 317 | restore_uid(); |
| 318 | return 1; |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | fclose(f); |
| 323 | restore_uid(); |
| 324 | return 0; |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 325 | } |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 326 | |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 327 | /* |
| 328 | * Checks whether key is allowed in authorized_keys-format file, |
| 329 | * returns 1 if the key is allowed or 0 otherwise. |
| 330 | */ |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 331 | static int |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 332 | check_authkeys_file(FILE *f, char *file, Key* key, struct passwd *pw) |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 333 | { |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 334 | char line[SSH_MAX_PUBKEY_BYTES]; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 335 | const char *reason; |
Darren Tucker | 33c787f | 2008-07-02 22:37:30 +1000 | [diff] [blame] | 336 | int found_key = 0; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 337 | u_long linenum = 0; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 338 | Key *found; |
| 339 | char *fp; |
| 340 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 341 | found_key = 0; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 342 | |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 343 | found = NULL; |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 344 | while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) { |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 345 | char *cp, *key_options = NULL; |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 346 | if (found != NULL) |
| 347 | key_free(found); |
| 348 | found = key_new(key_is_cert(key) ? KEY_UNSPEC : key->type); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 349 | auth_clear_options(); |
| 350 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 351 | /* Skip leading whitespace, empty and comment lines. */ |
| 352 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) |
| 353 | ; |
| 354 | if (!*cp || *cp == '\n' || *cp == '#') |
| 355 | continue; |
| 356 | |
| 357 | if (key_read(found, &cp) != 1) { |
| 358 | /* no key? check if there are options for this key */ |
| 359 | int quoted = 0; |
| 360 | debug2("user_key_allowed: check options: '%s'", cp); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 361 | key_options = cp; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 362 | for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) { |
| 363 | if (*cp == '\\' && cp[1] == '"') |
| 364 | cp++; /* Skip both */ |
| 365 | else if (*cp == '"') |
| 366 | quoted = !quoted; |
| 367 | } |
| 368 | /* Skip remaining whitespace. */ |
| 369 | for (; *cp == ' ' || *cp == '\t'; cp++) |
| 370 | ; |
| 371 | if (key_read(found, &cp) != 1) { |
| 372 | debug2("user_key_allowed: advance: '%s'", cp); |
| 373 | /* still no key? advance to next line*/ |
| 374 | continue; |
| 375 | } |
| 376 | } |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 377 | if (key_is_cert(key)) { |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 378 | if (!key_equal(found, key->cert->signature_key)) |
| 379 | continue; |
Damien Miller | 8439955 | 2010-05-21 14:58:12 +1000 | [diff] [blame] | 380 | if (auth_parse_options(pw, key_options, file, |
| 381 | linenum) != 1) |
| 382 | continue; |
| 383 | if (!key_is_cert_authority) |
| 384 | continue; |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 385 | fp = key_fingerprint(found, options.fingerprint_hash, |
| 386 | SSH_FP_DEFAULT); |
Damien Miller | e513a91 | 2010-03-22 05:51:21 +1100 | [diff] [blame] | 387 | debug("matching CA found: file %s, line %lu, %s %s", |
| 388 | file, linenum, key_type(found), fp); |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 389 | /* |
| 390 | * If the user has specified a list of principals as |
| 391 | * a key option, then prefer that list to matching |
| 392 | * their username in the certificate principals list. |
| 393 | */ |
| 394 | if (authorized_principals != NULL && |
| 395 | !match_principals_option(authorized_principals, |
| 396 | key->cert)) { |
| 397 | reason = "Certificate does not contain an " |
| 398 | "authorized principal"; |
| 399 | fail_reason: |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 400 | free(fp); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 401 | error("%s", reason); |
| 402 | auth_debug_add("%s", reason); |
| 403 | continue; |
| 404 | } |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 405 | if (key_cert_check_authority(key, 0, 0, |
| 406 | authorized_principals == NULL ? pw->pw_name : NULL, |
| 407 | &reason) != 0) |
| 408 | goto fail_reason; |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 409 | if (auth_cert_options(key, pw) != 0) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 410 | free(fp); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 411 | continue; |
Damien Miller | e513a91 | 2010-03-22 05:51:21 +1100 | [diff] [blame] | 412 | } |
| 413 | verbose("Accepted certificate ID \"%s\" " |
| 414 | "signed by %s CA %s via %s", key->cert->key_id, |
| 415 | key_type(found), fp, file); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 416 | free(fp); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 417 | found_key = 1; |
| 418 | break; |
Damien Miller | 8439955 | 2010-05-21 14:58:12 +1000 | [diff] [blame] | 419 | } else if (key_equal(found, key)) { |
| 420 | if (auth_parse_options(pw, key_options, file, |
| 421 | linenum) != 1) |
| 422 | continue; |
| 423 | if (key_is_cert_authority) |
| 424 | continue; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 425 | found_key = 1; |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 426 | fp = key_fingerprint(found, options.fingerprint_hash, |
| 427 | SSH_FP_DEFAULT); |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 428 | debug("matching key found: file %s, line %lu %s %s", |
| 429 | file, linenum, key_type(found), fp); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 430 | free(fp); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 431 | break; |
| 432 | } |
| 433 | } |
Darren Tucker | 74836ae | 2013-06-02 07:32:00 +1000 | [diff] [blame] | 434 | if (found != NULL) |
| 435 | key_free(found); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 436 | if (!found_key) |
| 437 | debug2("key not found"); |
| 438 | return found_key; |
| 439 | } |
| 440 | |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 441 | /* Authenticate a certificate key against TrustedUserCAKeys */ |
| 442 | static int |
| 443 | user_cert_trusted_ca(struct passwd *pw, Key *key) |
| 444 | { |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 445 | char *ca_fp, *principals_file = NULL; |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 446 | const char *reason; |
| 447 | int ret = 0; |
| 448 | |
| 449 | if (!key_is_cert(key) || options.trusted_user_ca_keys == NULL) |
| 450 | return 0; |
| 451 | |
Damien Miller | e513a91 | 2010-03-22 05:51:21 +1100 | [diff] [blame] | 452 | ca_fp = key_fingerprint(key->cert->signature_key, |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 453 | options.fingerprint_hash, SSH_FP_DEFAULT); |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 454 | |
djm@openbsd.org | 5e39a49 | 2014-12-04 02:24:32 +0000 | [diff] [blame] | 455 | if (sshkey_in_file(key->cert->signature_key, |
| 456 | options.trusted_user_ca_keys, 1, 0) != 0) { |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 457 | debug2("%s: CA %s %s is not listed in %s", __func__, |
| 458 | key_type(key->cert->signature_key), ca_fp, |
| 459 | options.trusted_user_ca_keys); |
| 460 | goto out; |
| 461 | } |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 462 | /* |
| 463 | * If AuthorizedPrincipals is in use, then compare the certificate |
| 464 | * principals against the names in that file rather than matching |
| 465 | * against the username. |
| 466 | */ |
| 467 | if ((principals_file = authorized_principals_file(pw)) != NULL) { |
| 468 | if (!match_principals_file(principals_file, pw, key->cert)) { |
| 469 | reason = "Certificate does not contain an " |
| 470 | "authorized principal"; |
| 471 | fail_reason: |
| 472 | error("%s", reason); |
| 473 | auth_debug_add("%s", reason); |
| 474 | goto out; |
| 475 | } |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 476 | } |
Damien Miller | 30da344 | 2010-05-10 11:58:03 +1000 | [diff] [blame] | 477 | if (key_cert_check_authority(key, 0, 1, |
| 478 | principals_file == NULL ? pw->pw_name : NULL, &reason) != 0) |
| 479 | goto fail_reason; |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 480 | if (auth_cert_options(key, pw) != 0) |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 481 | goto out; |
| 482 | |
Damien Miller | e513a91 | 2010-03-22 05:51:21 +1100 | [diff] [blame] | 483 | verbose("Accepted certificate ID \"%s\" signed by %s CA %s via %s", |
| 484 | key->cert->key_id, key_type(key->cert->signature_key), ca_fp, |
| 485 | options.trusted_user_ca_keys); |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 486 | ret = 1; |
| 487 | |
| 488 | out: |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 489 | free(principals_file); |
| 490 | free(ca_fp); |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 491 | return ret; |
| 492 | } |
| 493 | |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 494 | /* |
| 495 | * Checks whether key is allowed in file. |
| 496 | * returns 1 if the key is allowed or 0 otherwise. |
| 497 | */ |
| 498 | static int |
| 499 | user_key_allowed2(struct passwd *pw, Key *key, char *file) |
| 500 | { |
| 501 | FILE *f; |
| 502 | int found_key = 0; |
| 503 | |
| 504 | /* Temporarily use the user's uid. */ |
| 505 | temporarily_use_uid(pw); |
| 506 | |
| 507 | debug("trying public key file %s", file); |
| 508 | if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) { |
| 509 | found_key = check_authkeys_file(f, file, key, pw); |
| 510 | fclose(f); |
| 511 | } |
| 512 | |
| 513 | restore_uid(); |
| 514 | return found_key; |
| 515 | } |
| 516 | |
| 517 | /* |
| 518 | * Checks whether key is allowed in output of command. |
| 519 | * returns 1 if the key is allowed or 0 otherwise. |
| 520 | */ |
| 521 | static int |
| 522 | user_key_command_allowed2(struct passwd *user_pw, Key *key) |
| 523 | { |
| 524 | FILE *f; |
| 525 | int ok, found_key = 0; |
| 526 | struct passwd *pw; |
| 527 | struct stat st; |
| 528 | int status, devnull, p[2], i; |
| 529 | pid_t pid; |
Damien Miller | d0d1099 | 2012-11-04 22:23:14 +1100 | [diff] [blame] | 530 | char *username, errmsg[512]; |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 531 | |
| 532 | if (options.authorized_keys_command == NULL || |
| 533 | options.authorized_keys_command[0] != '/') |
| 534 | return 0; |
| 535 | |
Damien Miller | d0d1099 | 2012-11-04 22:23:14 +1100 | [diff] [blame] | 536 | if (options.authorized_keys_command_user == NULL) { |
| 537 | error("No user for AuthorizedKeysCommand specified, skipping"); |
| 538 | return 0; |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 539 | } |
| 540 | |
Damien Miller | d0d1099 | 2012-11-04 22:23:14 +1100 | [diff] [blame] | 541 | username = percent_expand(options.authorized_keys_command_user, |
| 542 | "u", user_pw->pw_name, (char *)NULL); |
| 543 | pw = getpwnam(username); |
| 544 | if (pw == NULL) { |
Damien Miller | 4018dc0 | 2013-02-15 10:28:55 +1100 | [diff] [blame] | 545 | error("AuthorizedKeysCommandUser \"%s\" not found: %s", |
| 546 | username, strerror(errno)); |
Damien Miller | d0d1099 | 2012-11-04 22:23:14 +1100 | [diff] [blame] | 547 | free(username); |
| 548 | return 0; |
| 549 | } |
| 550 | free(username); |
| 551 | |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 552 | temporarily_use_uid(pw); |
| 553 | |
| 554 | if (stat(options.authorized_keys_command, &st) < 0) { |
| 555 | error("Could not stat AuthorizedKeysCommand \"%s\": %s", |
| 556 | options.authorized_keys_command, strerror(errno)); |
| 557 | goto out; |
| 558 | } |
| 559 | if (auth_secure_path(options.authorized_keys_command, &st, NULL, 0, |
| 560 | errmsg, sizeof(errmsg)) != 0) { |
| 561 | error("Unsafe AuthorizedKeysCommand: %s", errmsg); |
| 562 | goto out; |
| 563 | } |
| 564 | |
| 565 | if (pipe(p) != 0) { |
| 566 | error("%s: pipe: %s", __func__, strerror(errno)); |
| 567 | goto out; |
| 568 | } |
| 569 | |
Damien Miller | 1e85469 | 2012-11-14 19:04:02 +1100 | [diff] [blame] | 570 | debug3("Running AuthorizedKeysCommand: \"%s %s\" as \"%s\"", |
| 571 | options.authorized_keys_command, user_pw->pw_name, pw->pw_name); |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 572 | |
| 573 | /* |
| 574 | * Don't want to call this in the child, where it can fatal() and |
| 575 | * run cleanup_exit() code. |
| 576 | */ |
| 577 | restore_uid(); |
| 578 | |
| 579 | switch ((pid = fork())) { |
| 580 | case -1: /* error */ |
| 581 | error("%s: fork: %s", __func__, strerror(errno)); |
| 582 | close(p[0]); |
| 583 | close(p[1]); |
| 584 | return 0; |
| 585 | case 0: /* child */ |
| 586 | for (i = 0; i < NSIG; i++) |
| 587 | signal(i, SIG_DFL); |
| 588 | |
Damien Miller | 1e85469 | 2012-11-14 19:04:02 +1100 | [diff] [blame] | 589 | if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) { |
| 590 | error("%s: open %s: %s", __func__, _PATH_DEVNULL, |
| 591 | strerror(errno)); |
| 592 | _exit(1); |
| 593 | } |
| 594 | /* Keep stderr around a while longer to catch errors */ |
| 595 | if (dup2(devnull, STDIN_FILENO) == -1 || |
| 596 | dup2(p[1], STDOUT_FILENO) == -1) { |
| 597 | error("%s: dup2: %s", __func__, strerror(errno)); |
| 598 | _exit(1); |
| 599 | } |
Damien Miller | d0d1099 | 2012-11-04 22:23:14 +1100 | [diff] [blame] | 600 | closefrom(STDERR_FILENO + 1); |
Damien Miller | 1e85469 | 2012-11-14 19:04:02 +1100 | [diff] [blame] | 601 | |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 602 | /* Don't use permanently_set_uid() here to avoid fatal() */ |
| 603 | if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0) { |
| 604 | error("setresgid %u: %s", (u_int)pw->pw_gid, |
| 605 | strerror(errno)); |
| 606 | _exit(1); |
| 607 | } |
| 608 | if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) != 0) { |
| 609 | error("setresuid %u: %s", (u_int)pw->pw_uid, |
| 610 | strerror(errno)); |
| 611 | _exit(1); |
| 612 | } |
Damien Miller | 1e85469 | 2012-11-14 19:04:02 +1100 | [diff] [blame] | 613 | /* stdin is pointed to /dev/null at this point */ |
| 614 | if (dup2(STDIN_FILENO, STDERR_FILENO) == -1) { |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 615 | error("%s: dup2: %s", __func__, strerror(errno)); |
| 616 | _exit(1); |
| 617 | } |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 618 | |
| 619 | execl(options.authorized_keys_command, |
Damien Miller | 1e85469 | 2012-11-14 19:04:02 +1100 | [diff] [blame] | 620 | options.authorized_keys_command, user_pw->pw_name, NULL); |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 621 | |
| 622 | error("AuthorizedKeysCommand %s exec failed: %s", |
| 623 | options.authorized_keys_command, strerror(errno)); |
| 624 | _exit(127); |
| 625 | default: /* parent */ |
| 626 | break; |
| 627 | } |
| 628 | |
| 629 | temporarily_use_uid(pw); |
| 630 | |
| 631 | close(p[1]); |
| 632 | if ((f = fdopen(p[0], "r")) == NULL) { |
| 633 | error("%s: fdopen: %s", __func__, strerror(errno)); |
| 634 | close(p[0]); |
| 635 | /* Don't leave zombie child */ |
| 636 | kill(pid, SIGTERM); |
| 637 | while (waitpid(pid, NULL, 0) == -1 && errno == EINTR) |
| 638 | ; |
| 639 | goto out; |
| 640 | } |
| 641 | ok = check_authkeys_file(f, options.authorized_keys_command, key, pw); |
| 642 | fclose(f); |
| 643 | |
| 644 | while (waitpid(pid, &status, 0) == -1) { |
| 645 | if (errno != EINTR) { |
| 646 | error("%s: waitpid: %s", __func__, strerror(errno)); |
| 647 | goto out; |
| 648 | } |
| 649 | } |
| 650 | if (WIFSIGNALED(status)) { |
| 651 | error("AuthorizedKeysCommand %s exited on signal %d", |
| 652 | options.authorized_keys_command, WTERMSIG(status)); |
| 653 | goto out; |
| 654 | } else if (WEXITSTATUS(status) != 0) { |
| 655 | error("AuthorizedKeysCommand %s returned status %d", |
| 656 | options.authorized_keys_command, WEXITSTATUS(status)); |
| 657 | goto out; |
| 658 | } |
| 659 | found_key = ok; |
| 660 | out: |
| 661 | restore_uid(); |
| 662 | return found_key; |
| 663 | } |
| 664 | |
| 665 | /* |
| 666 | * Check whether key authenticates and authorises the user. |
| 667 | */ |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 668 | int |
| 669 | user_key_allowed(struct passwd *pw, Key *key) |
| 670 | { |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 671 | u_int success, i; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 672 | char *file; |
| 673 | |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 674 | if (auth_key_is_revoked(key)) |
| 675 | return 0; |
| 676 | if (key_is_cert(key) && auth_key_is_revoked(key->cert->signature_key)) |
| 677 | return 0; |
| 678 | |
| 679 | success = user_cert_trusted_ca(pw, key); |
| 680 | if (success) |
| 681 | return success; |
| 682 | |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 683 | success = user_key_command_allowed2(pw, key); |
| 684 | if (success > 0) |
| 685 | return success; |
| 686 | |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 687 | for (i = 0; !success && i < options.num_authkeys_files; i++) { |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 688 | |
| 689 | if (strcasecmp(options.authorized_keys_files[i], "none") == 0) |
| 690 | continue; |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 691 | file = expand_authorized_keys( |
| 692 | options.authorized_keys_files[i], pw); |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 693 | |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 694 | success = user_key_allowed2(pw, key, file); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 695 | free(file); |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 696 | } |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 697 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 698 | return success; |
| 699 | } |
| 700 | |
djm@openbsd.org | f69b69b | 2014-12-22 07:51:30 +0000 | [diff] [blame] | 701 | /* Records a public key in the list of previously-successful keys */ |
| 702 | void |
| 703 | auth2_record_userkey(Authctxt *authctxt, struct sshkey *key) |
| 704 | { |
| 705 | struct sshkey **tmp; |
| 706 | |
| 707 | if (authctxt->nprev_userkeys >= INT_MAX || |
| 708 | (tmp = reallocarray(authctxt->prev_userkeys, |
| 709 | authctxt->nprev_userkeys + 1, sizeof(*tmp))) == NULL) |
| 710 | fatal("%s: reallocarray failed", __func__); |
| 711 | authctxt->prev_userkeys = tmp; |
| 712 | authctxt->prev_userkeys[authctxt->nprev_userkeys] = key; |
| 713 | authctxt->nprev_userkeys++; |
| 714 | } |
| 715 | |
| 716 | /* Checks whether a key has already been used successfully for authentication */ |
| 717 | int |
| 718 | auth2_userkey_already_used(Authctxt *authctxt, struct sshkey *key) |
| 719 | { |
| 720 | u_int i; |
| 721 | |
| 722 | for (i = 0; i < authctxt->nprev_userkeys; i++) { |
| 723 | if (sshkey_equal_public(key, authctxt->prev_userkeys[i])) { |
| 724 | return 1; |
| 725 | } |
| 726 | } |
| 727 | return 0; |
| 728 | } |
| 729 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 730 | Authmethod method_pubkey = { |
| 731 | "publickey", |
| 732 | userauth_pubkey, |
| 733 | &options.pubkey_authentication |
| 734 | }; |