Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 1 | /* $OpenBSD: auth2-pubkey.c,v 1.15 2006/08/03 03:34:41 deraadt 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> |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 30 | |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 31 | #include <pwd.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 32 | #include <stdio.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 33 | #include <stdarg.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 34 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 35 | #include "xmalloc.h" |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 36 | #include "ssh.h" |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 37 | #include "ssh2.h" |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 38 | #include "packet.h" |
| 39 | #include "buffer.h" |
| 40 | #include "log.h" |
| 41 | #include "servconf.h" |
| 42 | #include "compat.h" |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 43 | #include "key.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 44 | #include "hostfile.h" |
| 45 | #include "auth.h" |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 46 | #include "pathnames.h" |
| 47 | #include "uidswap.h" |
| 48 | #include "auth-options.h" |
| 49 | #include "canohost.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 50 | #ifdef GSSAPI |
| 51 | #include "ssh-gss.h" |
| 52 | #endif |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 53 | #include "monitor_wrap.h" |
Darren Tucker | f0f9098 | 2004-12-11 13:39:50 +1100 | [diff] [blame] | 54 | #include "misc.h" |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 55 | |
| 56 | /* import */ |
| 57 | extern ServerOptions options; |
| 58 | extern u_char *session_id2; |
Darren Tucker | 502d384 | 2003-06-28 12:38:01 +1000 | [diff] [blame] | 59 | extern u_int session_id2_len; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 60 | |
| 61 | static int |
| 62 | userauth_pubkey(Authctxt *authctxt) |
| 63 | { |
| 64 | Buffer b; |
| 65 | Key *key = NULL; |
| 66 | char *pkalg; |
| 67 | u_char *pkblob, *sig; |
| 68 | u_int alen, blen, slen; |
| 69 | int have_sig, pktype; |
| 70 | int authenticated = 0; |
| 71 | |
| 72 | if (!authctxt->valid) { |
| 73 | debug2("userauth_pubkey: disabled because of invalid user"); |
| 74 | return 0; |
| 75 | } |
| 76 | have_sig = packet_get_char(); |
| 77 | if (datafellows & SSH_BUG_PKAUTH) { |
| 78 | debug2("userauth_pubkey: SSH_BUG_PKAUTH"); |
| 79 | /* no explicit pkalg given */ |
| 80 | pkblob = packet_get_string(&blen); |
| 81 | buffer_init(&b); |
| 82 | buffer_append(&b, pkblob, blen); |
| 83 | /* so we have to extract the pkalg from the pkblob */ |
| 84 | pkalg = buffer_get_string(&b, &alen); |
| 85 | buffer_free(&b); |
| 86 | } else { |
| 87 | pkalg = packet_get_string(&alen); |
| 88 | pkblob = packet_get_string(&blen); |
| 89 | } |
| 90 | pktype = key_type_from_name(pkalg); |
| 91 | if (pktype == KEY_UNSPEC) { |
| 92 | /* this is perfectly legal */ |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 93 | logit("userauth_pubkey: unsupported public key algorithm: %s", |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 94 | pkalg); |
| 95 | goto done; |
| 96 | } |
| 97 | key = key_from_blob(pkblob, blen); |
| 98 | if (key == NULL) { |
| 99 | error("userauth_pubkey: cannot decode key: %s", pkalg); |
| 100 | goto done; |
| 101 | } |
| 102 | if (key->type != pktype) { |
| 103 | error("userauth_pubkey: type mismatch for decoded key " |
| 104 | "(received %d, expected %d)", key->type, pktype); |
| 105 | goto done; |
| 106 | } |
| 107 | if (have_sig) { |
| 108 | sig = packet_get_string(&slen); |
| 109 | packet_check_eom(); |
| 110 | buffer_init(&b); |
| 111 | if (datafellows & SSH_OLD_SESSIONID) { |
| 112 | buffer_append(&b, session_id2, session_id2_len); |
| 113 | } else { |
| 114 | buffer_put_string(&b, session_id2, session_id2_len); |
| 115 | } |
| 116 | /* reconstruct packet */ |
| 117 | buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST); |
| 118 | buffer_put_cstring(&b, authctxt->user); |
| 119 | buffer_put_cstring(&b, |
| 120 | datafellows & SSH_BUG_PKSERVICE ? |
| 121 | "ssh-userauth" : |
| 122 | authctxt->service); |
| 123 | if (datafellows & SSH_BUG_PKAUTH) { |
| 124 | buffer_put_char(&b, have_sig); |
| 125 | } else { |
| 126 | buffer_put_cstring(&b, "publickey"); |
| 127 | buffer_put_char(&b, have_sig); |
| 128 | buffer_put_cstring(&b, pkalg); |
| 129 | } |
| 130 | buffer_put_string(&b, pkblob, blen); |
| 131 | #ifdef DEBUG_PK |
| 132 | buffer_dump(&b); |
| 133 | #endif |
| 134 | /* test for correct signature */ |
| 135 | authenticated = 0; |
| 136 | if (PRIVSEP(user_key_allowed(authctxt->pw, key)) && |
| 137 | PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b), |
Damien Miller | fb1310e | 2004-01-21 11:02:50 +1100 | [diff] [blame] | 138 | buffer_len(&b))) == 1) |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 139 | authenticated = 1; |
Damien Miller | fb1310e | 2004-01-21 11:02:50 +1100 | [diff] [blame] | 140 | buffer_free(&b); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 141 | xfree(sig); |
| 142 | } else { |
| 143 | debug("test whether pkalg/pkblob are acceptable"); |
| 144 | packet_check_eom(); |
| 145 | |
| 146 | /* XXX fake reply and always send PK_OK ? */ |
| 147 | /* |
| 148 | * XXX this allows testing whether a user is allowed |
| 149 | * to login: if you happen to have a valid pubkey this |
| 150 | * message is sent. the message is NEVER sent at all |
| 151 | * if a user is not allowed to login. is this an |
| 152 | * issue? -markus |
| 153 | */ |
| 154 | if (PRIVSEP(user_key_allowed(authctxt->pw, key))) { |
| 155 | packet_start(SSH2_MSG_USERAUTH_PK_OK); |
| 156 | packet_put_string(pkalg, alen); |
| 157 | packet_put_string(pkblob, blen); |
| 158 | packet_send(); |
| 159 | packet_write_wait(); |
| 160 | authctxt->postponed = 1; |
| 161 | } |
| 162 | } |
| 163 | if (authenticated != 1) |
| 164 | auth_clear_options(); |
| 165 | done: |
| 166 | debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg); |
| 167 | if (key != NULL) |
| 168 | key_free(key); |
| 169 | xfree(pkalg); |
| 170 | xfree(pkblob); |
| 171 | #ifdef HAVE_CYGWIN |
| 172 | if (check_nt_auth(0, authctxt->pw) == 0) |
Damien Miller | 4765679 | 2004-09-11 22:42:09 +1000 | [diff] [blame] | 173 | authenticated = 0; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 174 | #endif |
| 175 | return authenticated; |
| 176 | } |
| 177 | |
| 178 | /* return 1 if user allows given key */ |
| 179 | static int |
| 180 | user_key_allowed2(struct passwd *pw, Key *key, char *file) |
| 181 | { |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 182 | char line[SSH_MAX_PUBKEY_BYTES]; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 183 | int found_key = 0; |
| 184 | FILE *f; |
| 185 | u_long linenum = 0; |
| 186 | struct stat st; |
| 187 | Key *found; |
| 188 | char *fp; |
| 189 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 190 | /* Temporarily use the user's uid. */ |
| 191 | temporarily_use_uid(pw); |
| 192 | |
| 193 | debug("trying public key file %s", file); |
| 194 | |
| 195 | /* Fail quietly if file does not exist */ |
| 196 | if (stat(file, &st) < 0) { |
| 197 | /* Restore the privileged uid. */ |
| 198 | restore_uid(); |
| 199 | return 0; |
| 200 | } |
| 201 | /* Open the file containing the authorized keys. */ |
| 202 | f = fopen(file, "r"); |
| 203 | if (!f) { |
| 204 | /* Restore the privileged uid. */ |
| 205 | restore_uid(); |
| 206 | return 0; |
| 207 | } |
| 208 | if (options.strict_modes && |
| 209 | secure_filename(f, file, pw, line, sizeof(line)) != 0) { |
| 210 | fclose(f); |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 211 | logit("Authentication refused: %s", line); |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 212 | restore_uid(); |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | found_key = 0; |
| 217 | found = key_new(key->type); |
| 218 | |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 219 | while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) { |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 220 | char *cp, *key_options = NULL; |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 221 | |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 222 | /* Skip leading whitespace, empty and comment lines. */ |
| 223 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) |
| 224 | ; |
| 225 | if (!*cp || *cp == '\n' || *cp == '#') |
| 226 | continue; |
| 227 | |
| 228 | if (key_read(found, &cp) != 1) { |
| 229 | /* no key? check if there are options for this key */ |
| 230 | int quoted = 0; |
| 231 | debug2("user_key_allowed: check options: '%s'", cp); |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 232 | key_options = cp; |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 233 | for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) { |
| 234 | if (*cp == '\\' && cp[1] == '"') |
| 235 | cp++; /* Skip both */ |
| 236 | else if (*cp == '"') |
| 237 | quoted = !quoted; |
| 238 | } |
| 239 | /* Skip remaining whitespace. */ |
| 240 | for (; *cp == ' ' || *cp == '\t'; cp++) |
| 241 | ; |
| 242 | if (key_read(found, &cp) != 1) { |
| 243 | debug2("user_key_allowed: advance: '%s'", cp); |
| 244 | /* still no key? advance to next line*/ |
| 245 | continue; |
| 246 | } |
| 247 | } |
| 248 | if (key_equal(found, key) && |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 249 | auth_parse_options(pw, key_options, file, linenum) == 1) { |
Ben Lindstrom | 855bf3a | 2002-06-06 20:27:55 +0000 | [diff] [blame] | 250 | found_key = 1; |
| 251 | debug("matching key found: file %s, line %lu", |
| 252 | file, linenum); |
| 253 | fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX); |
| 254 | verbose("Found matching %s key: %s", |
| 255 | key_type(found), fp); |
| 256 | xfree(fp); |
| 257 | break; |
| 258 | } |
| 259 | } |
| 260 | restore_uid(); |
| 261 | fclose(f); |
| 262 | key_free(found); |
| 263 | if (!found_key) |
| 264 | debug2("key not found"); |
| 265 | return found_key; |
| 266 | } |
| 267 | |
| 268 | /* check whether given key is in .ssh/authorized_keys* */ |
| 269 | int |
| 270 | user_key_allowed(struct passwd *pw, Key *key) |
| 271 | { |
| 272 | int success; |
| 273 | char *file; |
| 274 | |
| 275 | file = authorized_keys_file(pw); |
| 276 | success = user_key_allowed2(pw, key, file); |
| 277 | xfree(file); |
| 278 | if (success) |
| 279 | return success; |
| 280 | |
| 281 | /* try suffix "2" for backward compat, too */ |
| 282 | file = authorized_keys_file2(pw); |
| 283 | success = user_key_allowed2(pw, key, file); |
| 284 | xfree(file); |
| 285 | return success; |
| 286 | } |
| 287 | |
| 288 | Authmethod method_pubkey = { |
| 289 | "publickey", |
| 290 | userauth_pubkey, |
| 291 | &options.pubkey_authentication |
| 292 | }; |