Darren Tucker | 0acca37 | 2013-06-02 07:41:51 +1000 | [diff] [blame] | 1 | /* $OpenBSD: auth1.c,v 1.79 2013/05/19 02:42:42 djm Exp $ */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 4 | * All rights reserved |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 5 | * |
| 6 | * As far as I am concerned, the code I have written for this software |
| 7 | * can be used freely for any purpose. Any derived versions of this |
| 8 | * software must be clearly marked as such, and if the derived work is |
| 9 | * incompatible with the protocol description in the RFC file, it must be |
| 10 | * called by a name other than "ssh" or "Secure Shell". |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include "includes.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 14 | |
Damien Miller | 1cdde6f | 2006-07-24 14:07:35 +1000 | [diff] [blame] | 15 | #include <sys/types.h> |
| 16 | |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 17 | #include <stdarg.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 18 | #include <stdio.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 19 | #include <string.h> |
Damien Miller | 1cdde6f | 2006-07-24 14:07:35 +1000 | [diff] [blame] | 20 | #include <unistd.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 21 | #include <pwd.h> |
Damien Miller | 1cdde6f | 2006-07-24 14:07:35 +1000 | [diff] [blame] | 22 | |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 23 | #include "openbsd-compat/sys-queue.h" |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 24 | #include "xmalloc.h" |
| 25 | #include "rsa.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 26 | #include "ssh1.h" |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 27 | #include "packet.h" |
| 28 | #include "buffer.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 29 | #include "log.h" |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 30 | #include "servconf.h" |
| 31 | #include "compat.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 32 | #include "key.h" |
| 33 | #include "hostfile.h" |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 34 | #include "auth.h" |
Damien Miller | c7ef63d | 2002-02-05 12:21:42 +1100 | [diff] [blame] | 35 | #include "channels.h" |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 36 | #include "session.h" |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 37 | #include "uidswap.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 38 | #ifdef GSSAPI |
| 39 | #include "ssh-gss.h" |
| 40 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 41 | #include "monitor_wrap.h" |
Darren Tucker | c138667 | 2004-12-03 14:33:47 +1100 | [diff] [blame] | 42 | #include "buffer.h" |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 43 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 44 | /* import */ |
| 45 | extern ServerOptions options; |
Darren Tucker | c138667 | 2004-12-03 14:33:47 +1100 | [diff] [blame] | 46 | extern Buffer loginmsg; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 47 | |
Darren Tucker | 0acca37 | 2013-06-02 07:41:51 +1000 | [diff] [blame] | 48 | static int auth1_process_password(Authctxt *); |
| 49 | static int auth1_process_rsa(Authctxt *); |
| 50 | static int auth1_process_rhosts_rsa(Authctxt *); |
| 51 | static int auth1_process_tis_challenge(Authctxt *); |
| 52 | static int auth1_process_tis_response(Authctxt *); |
Damien Miller | 6abf57c | 2005-06-19 07:31:37 +1000 | [diff] [blame] | 53 | |
| 54 | static char *client_user = NULL; /* Used to fill in remote user for PAM */ |
| 55 | |
| 56 | struct AuthMethod1 { |
| 57 | int type; |
| 58 | char *name; |
| 59 | int *enabled; |
Darren Tucker | 12f6533 | 2013-06-02 08:01:24 +1000 | [diff] [blame] | 60 | int (*method)(Authctxt *); |
Damien Miller | 6abf57c | 2005-06-19 07:31:37 +1000 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | const struct AuthMethod1 auth1_methods[] = { |
| 64 | { |
| 65 | SSH_CMSG_AUTH_PASSWORD, "password", |
| 66 | &options.password_authentication, auth1_process_password |
| 67 | }, |
| 68 | { |
| 69 | SSH_CMSG_AUTH_RSA, "rsa", |
| 70 | &options.rsa_authentication, auth1_process_rsa |
| 71 | }, |
| 72 | { |
| 73 | SSH_CMSG_AUTH_RHOSTS_RSA, "rhosts-rsa", |
| 74 | &options.rhosts_rsa_authentication, auth1_process_rhosts_rsa |
| 75 | }, |
| 76 | { |
| 77 | SSH_CMSG_AUTH_TIS, "challenge-response", |
| 78 | &options.challenge_response_authentication, |
| 79 | auth1_process_tis_challenge |
| 80 | }, |
| 81 | { |
| 82 | SSH_CMSG_AUTH_TIS_RESPONSE, "challenge-response", |
| 83 | &options.challenge_response_authentication, |
| 84 | auth1_process_tis_response |
| 85 | }, |
| 86 | { -1, NULL, NULL, NULL} |
| 87 | }; |
| 88 | |
| 89 | static const struct AuthMethod1 |
| 90 | *lookup_authmethod1(int type) |
| 91 | { |
| 92 | int i; |
| 93 | |
Damien Miller | d62f2ca | 2006-03-26 13:57:41 +1100 | [diff] [blame] | 94 | for (i = 0; auth1_methods[i].name != NULL; i++) |
Damien Miller | 6abf57c | 2005-06-19 07:31:37 +1000 | [diff] [blame] | 95 | if (auth1_methods[i].type == type) |
| 96 | return (&(auth1_methods[i])); |
| 97 | |
| 98 | return (NULL); |
| 99 | } |
| 100 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 101 | static char * |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 102 | get_authname(int type) |
| 103 | { |
Damien Miller | 6abf57c | 2005-06-19 07:31:37 +1000 | [diff] [blame] | 104 | const struct AuthMethod1 *a; |
| 105 | static char buf[64]; |
| 106 | |
| 107 | if ((a = lookup_authmethod1(type)) != NULL) |
| 108 | return (a->name); |
| 109 | snprintf(buf, sizeof(buf), "bad-auth-msg-%d", type); |
| 110 | return (buf); |
| 111 | } |
| 112 | |
Damien Miller | 91d4b12 | 2006-03-26 14:05:20 +1100 | [diff] [blame] | 113 | /*ARGSUSED*/ |
Damien Miller | 6abf57c | 2005-06-19 07:31:37 +1000 | [diff] [blame] | 114 | static int |
Darren Tucker | 0acca37 | 2013-06-02 07:41:51 +1000 | [diff] [blame] | 115 | auth1_process_password(Authctxt *authctxt) |
Damien Miller | 6abf57c | 2005-06-19 07:31:37 +1000 | [diff] [blame] | 116 | { |
| 117 | int authenticated = 0; |
| 118 | char *password; |
| 119 | u_int dlen; |
| 120 | |
| 121 | /* |
| 122 | * Read user password. It is in plain text, but was |
| 123 | * transmitted over the encrypted channel so it is |
| 124 | * not visible to an outside observer. |
| 125 | */ |
| 126 | password = packet_get_string(&dlen); |
| 127 | packet_check_eom(); |
| 128 | |
| 129 | /* Try authentication with the password. */ |
| 130 | authenticated = PRIVSEP(auth_password(authctxt, password)); |
| 131 | |
| 132 | memset(password, 0, dlen); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 133 | free(password); |
Damien Miller | 6abf57c | 2005-06-19 07:31:37 +1000 | [diff] [blame] | 134 | |
| 135 | return (authenticated); |
| 136 | } |
| 137 | |
Damien Miller | 91d4b12 | 2006-03-26 14:05:20 +1100 | [diff] [blame] | 138 | /*ARGSUSED*/ |
Damien Miller | 6abf57c | 2005-06-19 07:31:37 +1000 | [diff] [blame] | 139 | static int |
Darren Tucker | 0acca37 | 2013-06-02 07:41:51 +1000 | [diff] [blame] | 140 | auth1_process_rsa(Authctxt *authctxt) |
Damien Miller | 6abf57c | 2005-06-19 07:31:37 +1000 | [diff] [blame] | 141 | { |
| 142 | int authenticated = 0; |
| 143 | BIGNUM *n; |
| 144 | |
| 145 | /* RSA authentication requested. */ |
| 146 | if ((n = BN_new()) == NULL) |
| 147 | fatal("do_authloop: BN_new failed"); |
| 148 | packet_get_bignum(n); |
| 149 | packet_check_eom(); |
| 150 | authenticated = auth_rsa(authctxt, n); |
| 151 | BN_clear_free(n); |
| 152 | |
| 153 | return (authenticated); |
| 154 | } |
| 155 | |
Damien Miller | 91d4b12 | 2006-03-26 14:05:20 +1100 | [diff] [blame] | 156 | /*ARGSUSED*/ |
Damien Miller | 6abf57c | 2005-06-19 07:31:37 +1000 | [diff] [blame] | 157 | static int |
Darren Tucker | 0acca37 | 2013-06-02 07:41:51 +1000 | [diff] [blame] | 158 | auth1_process_rhosts_rsa(Authctxt *authctxt) |
Damien Miller | 6abf57c | 2005-06-19 07:31:37 +1000 | [diff] [blame] | 159 | { |
Damien Miller | 06221f1 | 2005-06-19 07:36:10 +1000 | [diff] [blame] | 160 | int keybits, authenticated = 0; |
Damien Miller | 6abf57c | 2005-06-19 07:31:37 +1000 | [diff] [blame] | 161 | u_int bits; |
| 162 | Key *client_host_key; |
| 163 | u_int ulen; |
| 164 | |
| 165 | /* |
| 166 | * Get client user name. Note that we just have to |
| 167 | * trust the client; root on the client machine can |
| 168 | * claim to be any user. |
| 169 | */ |
Damien Miller | da108ec | 2010-08-31 22:36:39 +1000 | [diff] [blame] | 170 | client_user = packet_get_cstring(&ulen); |
Damien Miller | 6abf57c | 2005-06-19 07:31:37 +1000 | [diff] [blame] | 171 | |
| 172 | /* Get the client host key. */ |
| 173 | client_host_key = key_new(KEY_RSA1); |
| 174 | bits = packet_get_int(); |
| 175 | packet_get_bignum(client_host_key->rsa->e); |
| 176 | packet_get_bignum(client_host_key->rsa->n); |
| 177 | |
Damien Miller | 06221f1 | 2005-06-19 07:36:10 +1000 | [diff] [blame] | 178 | keybits = BN_num_bits(client_host_key->rsa->n); |
| 179 | if (keybits < 0 || bits != (u_int)keybits) { |
Damien Miller | 6abf57c | 2005-06-19 07:31:37 +1000 | [diff] [blame] | 180 | verbose("Warning: keysize mismatch for client_host_key: " |
| 181 | "actual %d, announced %d", |
| 182 | BN_num_bits(client_host_key->rsa->n), bits); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 183 | } |
Damien Miller | 6abf57c | 2005-06-19 07:31:37 +1000 | [diff] [blame] | 184 | packet_check_eom(); |
| 185 | |
| 186 | authenticated = auth_rhosts_rsa(authctxt, client_user, |
| 187 | client_host_key); |
| 188 | key_free(client_host_key); |
| 189 | |
Darren Tucker | 0acca37 | 2013-06-02 07:41:51 +1000 | [diff] [blame] | 190 | auth_info(authctxt, "ruser %.100s", client_user); |
Damien Miller | 94cf4c8 | 2005-07-17 17:04:47 +1000 | [diff] [blame] | 191 | |
Damien Miller | 6abf57c | 2005-06-19 07:31:37 +1000 | [diff] [blame] | 192 | return (authenticated); |
| 193 | } |
| 194 | |
Damien Miller | 91d4b12 | 2006-03-26 14:05:20 +1100 | [diff] [blame] | 195 | /*ARGSUSED*/ |
Damien Miller | 6abf57c | 2005-06-19 07:31:37 +1000 | [diff] [blame] | 196 | static int |
Darren Tucker | 0acca37 | 2013-06-02 07:41:51 +1000 | [diff] [blame] | 197 | auth1_process_tis_challenge(Authctxt *authctxt) |
Damien Miller | 6abf57c | 2005-06-19 07:31:37 +1000 | [diff] [blame] | 198 | { |
| 199 | char *challenge; |
Damien Miller | 46d38de | 2005-07-17 17:02:09 +1000 | [diff] [blame] | 200 | |
Damien Miller | 6abf57c | 2005-06-19 07:31:37 +1000 | [diff] [blame] | 201 | if ((challenge = get_challenge(authctxt)) == NULL) |
| 202 | return (0); |
| 203 | |
| 204 | debug("sending challenge '%s'", challenge); |
| 205 | packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE); |
| 206 | packet_put_cstring(challenge); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 207 | free(challenge); |
Damien Miller | 6abf57c | 2005-06-19 07:31:37 +1000 | [diff] [blame] | 208 | packet_send(); |
| 209 | packet_write_wait(); |
| 210 | |
| 211 | return (-1); |
| 212 | } |
| 213 | |
Damien Miller | 91d4b12 | 2006-03-26 14:05:20 +1100 | [diff] [blame] | 214 | /*ARGSUSED*/ |
Damien Miller | 6abf57c | 2005-06-19 07:31:37 +1000 | [diff] [blame] | 215 | static int |
Darren Tucker | 0acca37 | 2013-06-02 07:41:51 +1000 | [diff] [blame] | 216 | auth1_process_tis_response(Authctxt *authctxt) |
Damien Miller | 6abf57c | 2005-06-19 07:31:37 +1000 | [diff] [blame] | 217 | { |
| 218 | int authenticated = 0; |
| 219 | char *response; |
| 220 | u_int dlen; |
| 221 | |
| 222 | response = packet_get_string(&dlen); |
| 223 | packet_check_eom(); |
| 224 | authenticated = verify_response(authctxt, response); |
| 225 | memset(response, 'r', dlen); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 226 | free(response); |
Damien Miller | 6abf57c | 2005-06-19 07:31:37 +1000 | [diff] [blame] | 227 | |
| 228 | return (authenticated); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /* |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 232 | * read packets, try to authenticate the user and |
| 233 | * return only if authentication is successful |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 234 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 235 | static void |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 236 | do_authloop(Authctxt *authctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 237 | { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 238 | int authenticated = 0; |
Damien Miller | 6abf57c | 2005-06-19 07:31:37 +1000 | [diff] [blame] | 239 | int prev = 0, type = 0; |
| 240 | const struct AuthMethod1 *meth; |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 241 | |
| 242 | debug("Attempting authentication for %s%.100s.", |
Darren Tucker | 5cb30ad | 2004-08-12 22:40:24 +1000 | [diff] [blame] | 243 | authctxt->valid ? "" : "invalid user ", authctxt->user); |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 244 | |
| 245 | /* If the user has no password, accept authentication immediately. */ |
Damien Miller | bda3eca | 2010-06-26 10:01:33 +1000 | [diff] [blame] | 246 | if (options.permit_empty_passwd && options.password_authentication && |
Darren Tucker | 6aaa58c | 2003-08-02 22:24:49 +1000 | [diff] [blame] | 247 | #ifdef KRB5 |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 248 | (!options.kerberos_authentication || options.kerberos_or_local_passwd) && |
| 249 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 250 | PRIVSEP(auth_password(authctxt, ""))) { |
Darren Tucker | a8c73d3 | 2004-06-23 09:17:54 +1000 | [diff] [blame] | 251 | #ifdef USE_PAM |
| 252 | if (options.use_pam && (PRIVSEP(do_pam_account()))) |
| 253 | #endif |
| 254 | { |
Damien Miller | 15b05cf | 2012-12-03 09:53:20 +1100 | [diff] [blame] | 255 | auth_log(authctxt, 1, 0, "without authentication", |
Darren Tucker | 0acca37 | 2013-06-02 07:41:51 +1000 | [diff] [blame] | 256 | NULL); |
Darren Tucker | a8c73d3 | 2004-06-23 09:17:54 +1000 | [diff] [blame] | 257 | return; |
| 258 | } |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 259 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 260 | |
| 261 | /* Indicate that authentication is needed. */ |
| 262 | packet_start(SSH_SMSG_FAILURE); |
| 263 | packet_send(); |
| 264 | packet_write_wait(); |
| 265 | |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 266 | for (;;) { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 267 | /* default to fail */ |
| 268 | authenticated = 0; |
| 269 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 270 | |
| 271 | /* Get a packet from the client. */ |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 272 | prev = type; |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 273 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 274 | |
Damien Miller | 4f9f42a | 2003-05-10 19:28:02 +1000 | [diff] [blame] | 275 | /* |
| 276 | * If we started challenge-response authentication but the |
| 277 | * next packet is not a response to our challenge, release |
| 278 | * the resources allocated by get_challenge() (which would |
| 279 | * normally have been released by verify_response() had we |
| 280 | * received such a response) |
| 281 | */ |
| 282 | if (prev == SSH_CMSG_AUTH_TIS && |
| 283 | type != SSH_CMSG_AUTH_TIS_RESPONSE) |
| 284 | abandon_challenge_response(authctxt); |
| 285 | |
Damien Miller | 0b4d48b | 2008-07-05 09:44:53 +1000 | [diff] [blame] | 286 | if (authctxt->failures >= options.max_authtries) |
| 287 | goto skip; |
Damien Miller | 6abf57c | 2005-06-19 07:31:37 +1000 | [diff] [blame] | 288 | if ((meth = lookup_authmethod1(type)) == NULL) { |
| 289 | logit("Unknown message during authentication: " |
| 290 | "type %d", type); |
| 291 | goto skip; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 292 | } |
Damien Miller | 6abf57c | 2005-06-19 07:31:37 +1000 | [diff] [blame] | 293 | |
| 294 | if (!*(meth->enabled)) { |
| 295 | verbose("%s authentication disabled.", meth->name); |
| 296 | goto skip; |
| 297 | } |
| 298 | |
Darren Tucker | 0acca37 | 2013-06-02 07:41:51 +1000 | [diff] [blame] | 299 | authenticated = meth->method(authctxt); |
Damien Miller | 6abf57c | 2005-06-19 07:31:37 +1000 | [diff] [blame] | 300 | if (authenticated == -1) |
| 301 | continue; /* "postponed" */ |
| 302 | |
Damien Miller | 60396b0 | 2001-02-18 17:01:00 +1100 | [diff] [blame] | 303 | #ifdef BSD_AUTH |
| 304 | if (authctxt->as) { |
| 305 | auth_close(authctxt->as); |
| 306 | authctxt->as = NULL; |
| 307 | } |
| 308 | #endif |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 309 | if (!authctxt->valid && authenticated) |
| 310 | fatal("INTERNAL ERROR: authenticated invalid user %s", |
| 311 | authctxt->user); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 312 | |
Tim Rice | 81ed518 | 2002-09-25 17:38:46 -0700 | [diff] [blame] | 313 | #ifdef _UNICOS |
Tim Rice | 81ed518 | 2002-09-25 17:38:46 -0700 | [diff] [blame] | 314 | if (authenticated && cray_access_denied(authctxt->user)) { |
| 315 | authenticated = 0; |
| 316 | fatal("Access denied for user %s.",authctxt->user); |
| 317 | } |
| 318 | #endif /* _UNICOS */ |
| 319 | |
Darren Tucker | 9d86e5d | 2009-03-08 11:40:27 +1100 | [diff] [blame] | 320 | #ifndef HAVE_CYGWIN |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 321 | /* Special handling for root */ |
Damien Miller | 556f931 | 2003-02-24 11:59:26 +1100 | [diff] [blame] | 322 | if (authenticated && authctxt->pw->pw_uid == 0 && |
Damien Miller | 6abf57c | 2005-06-19 07:31:37 +1000 | [diff] [blame] | 323 | !auth_root_allowed(meth->name)) { |
| 324 | authenticated = 0; |
Darren Tucker | 2e0cf0d | 2005-02-08 21:52:47 +1100 | [diff] [blame] | 325 | # ifdef SSH_AUDIT_EVENTS |
| 326 | PRIVSEP(audit_event(SSH_LOGIN_ROOT_DENIED)); |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 327 | # endif |
| 328 | } |
Ben Lindstrom | 5dc8150 | 2001-01-19 06:10:29 +0000 | [diff] [blame] | 329 | #endif |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 330 | |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 331 | #ifdef USE_PAM |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 332 | if (options.use_pam && authenticated && |
Darren Tucker | c138667 | 2004-12-03 14:33:47 +1100 | [diff] [blame] | 333 | !PRIVSEP(do_pam_account())) { |
| 334 | char *msg; |
| 335 | size_t len; |
| 336 | |
| 337 | error("Access denied for user %s by PAM account " |
Damien Miller | b6f72f5 | 2005-07-17 17:26:43 +1000 | [diff] [blame] | 338 | "configuration", authctxt->user); |
Darren Tucker | c138667 | 2004-12-03 14:33:47 +1100 | [diff] [blame] | 339 | len = buffer_len(&loginmsg); |
| 340 | buffer_append(&loginmsg, "\0", 1); |
| 341 | msg = buffer_ptr(&loginmsg); |
| 342 | /* strip trailing newlines */ |
| 343 | if (len > 0) |
| 344 | while (len > 0 && msg[--len] == '\n') |
| 345 | msg[len] = '\0'; |
| 346 | else |
| 347 | msg = "Access denied."; |
Damien Miller | 773a7b9 | 2008-07-09 20:54:05 +1000 | [diff] [blame] | 348 | packet_disconnect("%s", msg); |
Darren Tucker | c138667 | 2004-12-03 14:33:47 +1100 | [diff] [blame] | 349 | } |
Damien Miller | 1f499fd | 2003-08-25 13:08:49 +1000 | [diff] [blame] | 350 | #endif |
| 351 | |
Damien Miller | 6abf57c | 2005-06-19 07:31:37 +1000 | [diff] [blame] | 352 | skip: |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 353 | /* Log before sending the reply */ |
Darren Tucker | 0acca37 | 2013-06-02 07:41:51 +1000 | [diff] [blame] | 354 | auth_log(authctxt, authenticated, 0, get_authname(type), NULL); |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 355 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 356 | free(client_user); |
| 357 | client_user = NULL; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 358 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 359 | if (authenticated) |
| 360 | return; |
| 361 | |
Damien Miller | 0b4d48b | 2008-07-05 09:44:53 +1000 | [diff] [blame] | 362 | if (++authctxt->failures >= options.max_authtries) { |
Darren Tucker | 2e0cf0d | 2005-02-08 21:52:47 +1100 | [diff] [blame] | 363 | #ifdef SSH_AUDIT_EVENTS |
| 364 | PRIVSEP(audit_event(SSH_LOGIN_EXCEED_MAXTRIES)); |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 365 | #endif |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 366 | packet_disconnect(AUTH_FAIL_MSG, authctxt->user); |
Darren Tucker | 269a1ea | 2005-02-03 00:20:53 +1100 | [diff] [blame] | 367 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 368 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 369 | packet_start(SSH_SMSG_FAILURE); |
| 370 | packet_send(); |
| 371 | packet_write_wait(); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | /* |
| 376 | * Performs authentication of an incoming connection. Session key has already |
| 377 | * been exchanged and encryption is enabled. |
| 378 | */ |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 379 | void |
| 380 | do_authentication(Authctxt *authctxt) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 381 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 382 | u_int ulen; |
Ben Lindstrom | c822638 | 2002-04-10 16:22:09 +0000 | [diff] [blame] | 383 | char *user, *style = NULL; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 384 | |
| 385 | /* Get the name of the user that we wish to log in as. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 386 | packet_read_expect(SSH_CMSG_USER); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 387 | |
| 388 | /* Get the user name. */ |
Damien Miller | da108ec | 2010-08-31 22:36:39 +1000 | [diff] [blame] | 389 | user = packet_get_cstring(&ulen); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 390 | packet_check_eom(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 391 | |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 392 | if ((style = strchr(user, ':')) != NULL) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 393 | *style++ = '\0'; |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 394 | |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 395 | authctxt->user = user; |
| 396 | authctxt->style = style; |
| 397 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 398 | /* Verify that the user is a valid user. */ |
Ben Lindstrom | 7ebb635 | 2002-03-22 03:04:08 +0000 | [diff] [blame] | 399 | if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL) |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 400 | authctxt->valid = 1; |
Damien Miller | 856f0be | 2003-09-03 07:32:45 +1000 | [diff] [blame] | 401 | else { |
Darren Tucker | 5cb30ad | 2004-08-12 22:40:24 +1000 | [diff] [blame] | 402 | debug("do_authentication: invalid user %s", user); |
Damien Miller | 856f0be | 2003-09-03 07:32:45 +1000 | [diff] [blame] | 403 | authctxt->pw = fakepw(); |
| 404 | } |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 405 | |
Damien Miller | a6e3f01 | 2012-11-04 23:21:40 +1100 | [diff] [blame] | 406 | /* Configuration may have changed as a result of Match */ |
| 407 | if (options.num_auth_methods != 0) |
| 408 | fatal("AuthenticationMethods is not supported with SSH " |
| 409 | "protocol 1"); |
| 410 | |
Damien Miller | 30d1f84 | 2004-07-21 20:48:53 +1000 | [diff] [blame] | 411 | setproctitle("%s%s", authctxt->valid ? user : "unknown", |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 412 | use_privsep ? " [net]" : ""); |
Ben Lindstrom | c1ba31f | 2001-02-15 03:14:11 +0000 | [diff] [blame] | 413 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 414 | #ifdef USE_PAM |
Damien Miller | 4e448a3 | 2003-05-14 15:11:48 +1000 | [diff] [blame] | 415 | if (options.use_pam) |
Darren Tucker | dbf7a74 | 2004-03-08 23:04:06 +1100 | [diff] [blame] | 416 | PRIVSEP(start_pam(authctxt)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 417 | #endif |
| 418 | |
| 419 | /* |
| 420 | * If we are not running as root, the user must have the same uid as |
Damien Miller | 6abf57c | 2005-06-19 07:31:37 +1000 | [diff] [blame] | 421 | * the server. |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 422 | */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 423 | #ifndef HAVE_CYGWIN |
Ben Lindstrom | 7ebb635 | 2002-03-22 03:04:08 +0000 | [diff] [blame] | 424 | if (!use_privsep && getuid() != 0 && authctxt->pw && |
| 425 | authctxt->pw->pw_uid != getuid()) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 426 | packet_disconnect("Cannot change user when server not running as root."); |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 427 | #endif |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 428 | |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 429 | /* |
Kevin Steves | 12aaa04 | 2001-01-24 21:23:39 +0000 | [diff] [blame] | 430 | * Loop until the user has been authenticated or the connection is |
| 431 | * closed, do_authloop() returns only if authentication is successful |
| 432 | */ |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 433 | do_authloop(authctxt); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 434 | |
| 435 | /* The user has been authenticated and accepted. */ |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 436 | packet_start(SSH_SMSG_SUCCESS); |
| 437 | packet_send(); |
| 438 | packet_write_wait(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 439 | } |