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