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