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