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