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