2 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
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".
13 RCSID("$OpenBSD: auth1.c,v 1.35 2002/02/03 17:53:25 markus Exp $");
32 int userauth_gssapi(Authctxt *authctxt);
33 char * gssapi_parse_userstring(char *userstring);
37 extern ServerOptions options;
41 auth1_gss_protocol_error(int type, u_int32_t plen, void *ctxt)
43 Authctxt *authctxt = ctxt;
44 /* Other side told us to abort, dont need to tell him */
45 /* maybe we can us some other method. */
46 if (type == SSH_MSG_AUTH_GSSAPI_ABORT) {
47 log("auth1: GSSAPI aborting");
48 dispatch_set(SSH_MSG_AUTH_GSSAPI_TOKEN, NULL);
49 authctxt->success = 1; /* get out of loop*/
53 log("auth1: protocol error: type %d plen %d", type, plen);
54 packet_disconnect("Protocol error during GSSAPI authentication: "
55 "Unknown packet type %d", type);
60 * convert ssh auth msg type into description
63 get_authname(int type)
65 static char buf[1024];
67 case SSH_CMSG_AUTH_PASSWORD:
69 case SSH_CMSG_AUTH_RSA:
71 case SSH_CMSG_AUTH_RHOSTS_RSA:
73 case SSH_CMSG_AUTH_RHOSTS:
75 case SSH_CMSG_AUTH_TIS:
76 case SSH_CMSG_AUTH_TIS_RESPONSE:
77 return "challenge-response";
78 #if defined(KRB4) || defined(KRB5)
79 case SSH_CMSG_AUTH_KERBEROS:
83 case SSH_CMSG_AUTH_GSSAPI:
87 snprintf(buf, sizeof buf, "bad-auth-msg-%d", type);
92 * read packets, try to authenticate the user and
93 * return only if authentication is successful
96 do_authloop(Authctxt *authctxt)
98 int authenticated = 0;
100 Key *client_host_key;
102 char *client_user, *password;
107 struct passwd *pw = authctxt->pw;
109 debug("Attempting authentication for %s%.100s.",
110 authctxt->valid ? "" : "illegal user ", authctxt->user);
112 /* If the user has no password, accept authentication immediately. */
113 if (options.password_authentication &&
114 #if defined(KRB4) || defined(KRB5)
115 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
118 auth_pam_password(pw, "")) {
119 #elif defined(HAVE_OSF_SIA)
122 auth_password(authctxt, "")) {
124 auth_log(authctxt, 1, "without authentication", "");
128 /* Indicate that authentication is needed. */
129 packet_start(SSH_SMSG_FAILURE);
136 /* default to fail */
141 /* Get a packet from the client. */
142 type = packet_read();
144 /* Process the packet. */
147 #if defined(KRB4) || defined(KRB5)
148 case SSH_CMSG_AUTH_KERBEROS:
149 if (!options.kerberos_authentication) {
150 verbose("Kerberos authentication disabled.");
152 char *kdata = packet_get_string(&dlen);
155 if (kdata[0] == 4) { /* KRB_PROT_VERSION */
160 if (tkt.length < MAX_KTXT_LEN)
161 memcpy(tkt.dat, kdata, tkt.length);
163 if (auth_krb4(authctxt, &tkt, &client_user)) {
165 snprintf(info, sizeof(info),
176 if (auth_krb5(authctxt, &tkt, &client_user)) {
178 snprintf(info, sizeof(info),
187 #endif /* KRB4 || KRB5 */
190 case SSH_CMSG_AUTH_GSSAPI:
191 if (!options.gss_authentication) {
192 verbose("GSSAPI authentication disabled.");
196 * GSSAPI was first added to ssh1 in ssh-1.2.27, and
197 * was added to the SecurtCRT product. In order
198 * to continue operating with these, we will add
199 * the equivelent GSSAPI support to SSH1.
200 * Will use the gssapi routines from the ssh2 as
201 * they are almost identical. But they use dispatch
202 * so we need to setup the dispatch tables here
203 * auth1.c for use only by the gssapi code.
204 * Since we already have the packet, we will call
205 * userauth_gssapi then start the dispatch loop.
207 if (!authctxt->valid) {
208 packet_disconnect("Authentication rejected for invalid user");
210 dispatch_init(&auth1_gss_protocol_error);
211 userauth_gssapi(authctxt);
212 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
213 if (authctxt->postponed) { /* failed, try other methods */
214 authctxt->success = 0;
215 authctxt->postponed = 0;
218 do_authenticated(authctxt);
219 /* will only return if authenticated */
224 #if defined(AFS) || defined(KRB5)
225 /* XXX - punt on backward compatibility here. */
226 case SSH_CMSG_HAVE_KERBEROS_TGT:
227 packet_send_debug("Kerberos TGT passing disabled before authentication.");
230 case SSH_CMSG_HAVE_AFS_TOKEN:
231 packet_send_debug("AFS token passing disabled before authentication.");
234 #endif /* AFS || KRB5 */
236 case SSH_CMSG_AUTH_RHOSTS:
237 if (!options.rhosts_authentication) {
238 verbose("Rhosts authentication disabled.");
242 * Get client user name. Note that we just have to
243 * trust the client; this is one reason why rhosts
244 * authentication is insecure. (Another is
245 * IP-spoofing on a local network.)
247 client_user = packet_get_string(&ulen);
250 /* Try to authenticate using /etc/hosts.equiv and .rhosts. */
251 authenticated = auth_rhosts(pw, client_user);
253 snprintf(info, sizeof info, " ruser %.100s", client_user);
256 case SSH_CMSG_AUTH_RHOSTS_RSA:
257 if (!options.rhosts_rsa_authentication) {
258 verbose("Rhosts with RSA authentication disabled.");
262 * Get client user name. Note that we just have to
263 * trust the client; root on the client machine can
264 * claim to be any user.
266 client_user = packet_get_string(&ulen);
268 /* Get the client host key. */
269 client_host_key = key_new(KEY_RSA1);
270 bits = packet_get_int();
271 packet_get_bignum(client_host_key->rsa->e);
272 packet_get_bignum(client_host_key->rsa->n);
274 if (bits != BN_num_bits(client_host_key->rsa->n))
275 verbose("Warning: keysize mismatch for client_host_key: "
276 "actual %d, announced %d",
277 BN_num_bits(client_host_key->rsa->n), bits);
280 authenticated = auth_rhosts_rsa(pw, client_user,
282 key_free(client_host_key);
284 snprintf(info, sizeof info, " ruser %.100s", client_user);
287 case SSH_CMSG_AUTH_RSA:
288 if (!options.rsa_authentication) {
289 verbose("RSA authentication disabled.");
292 /* RSA authentication requested. */
293 if ((n = BN_new()) == NULL)
294 fatal("do_authloop: BN_new failed");
295 packet_get_bignum(n);
297 authenticated = auth_rsa(pw, n);
301 case SSH_CMSG_AUTH_PASSWORD:
302 if (!options.password_authentication) {
303 verbose("Password authentication disabled.");
307 * Read user password. It is in plain text, but was
308 * transmitted over the encrypted channel so it is
309 * not visible to an outside observer.
311 password = packet_get_string(&dlen);
315 /* Do PAM auth with password */
316 authenticated = auth_pam_password(pw, password);
317 #elif defined(HAVE_OSF_SIA)
318 /* Do SIA auth with password */
319 authenticated = auth_sia_password(authctxt->user,
321 #else /* !USE_PAM && !HAVE_OSF_SIA */
322 /* Try authentication with the password. */
323 authenticated = auth_password(authctxt, password);
326 memset(password, 0, strlen(password));
330 case SSH_CMSG_AUTH_TIS:
331 debug("rcvd SSH_CMSG_AUTH_TIS");
332 if (options.challenge_response_authentication == 1) {
333 char *challenge = get_challenge(authctxt);
334 if (challenge != NULL) {
335 debug("sending challenge '%s'", challenge);
336 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
337 packet_put_cstring(challenge);
345 case SSH_CMSG_AUTH_TIS_RESPONSE:
346 debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
347 if (options.challenge_response_authentication == 1) {
348 char *response = packet_get_string(&dlen);
349 debug("got response '%s'", response);
351 authenticated = verify_response(authctxt, response);
352 memset(response, 'r', dlen);
359 * Any unknown messages will be ignored (and failure
360 * returned) during authentication.
362 log("Unknown message during authentication: type %d", type);
367 auth_close(authctxt->as);
371 if (!authctxt->valid && authenticated)
372 fatal("INTERNAL ERROR: authenticated invalid user %s",
377 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD, pw)) {
378 packet_disconnect("Authentication rejected for uid %d.",
379 pw == NULL ? -1 : pw->pw_uid);
383 /* Special handling for root */
384 if (authenticated && authctxt->pw->pw_uid == 0 &&
385 !auth_root_allowed(get_authname(type)))
389 if (authenticated && !do_pam_account(pw->pw_name, client_user))
393 /* Log before sending the reply */
394 auth_log(authctxt, authenticated, get_authname(type), info);
396 if (client_user != NULL) {
404 if (authctxt->failures++ > AUTH_FAIL_MAX) {
405 #ifdef WITH_AIXAUTHENTICATE
406 loginfailed(authctxt->user,
407 get_canonical_hostname(options.verify_reverse_mapping),
409 #endif /* WITH_AIXAUTHENTICATE */
410 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
413 packet_start(SSH_SMSG_FAILURE);
420 * Performs authentication of an incoming connection. Session key has already
421 * been exchanged and encryption is enabled.
424 do_authentication(void)
429 char *p, *user, *style = NULL;
431 /* Get the name of the user that we wish to log in as. */
432 packet_read_expect(SSH_CMSG_USER);
434 /* Get the user name. */
435 user = packet_get_string(&ulen);
438 /*modified by binhe*/
440 /* GSSAPI clients may legitimately send long names (see below) */
442 if (strlen(user) > 255)
443 do_authentication_fail_loop();
447 /* Parse GSSAPI identity from userstring */
448 user = gssapi_parse_userstring(user);
451 /*end of modification*/
453 if ((style = strchr(user, ':')) != NULL)
456 /* XXX - SSH.com Kerberos v5 braindeath. */
457 if ((p = strchr(user, '@')) != NULL)
460 authctxt = authctxt_new();
461 authctxt->user = user;
462 authctxt->style = style;
464 /* Verify that the user is a valid user. */
466 if (pw && allowed_user(pw)) {
470 debug("do_authentication: illegal user %s", user);
475 setproctitle("%s", pw ? user : "unknown");
478 start_pam(pw == NULL ? "NOUSER" : user);
482 * If we are not running as root, the user must have the same uid as
483 * the server. (Unless you are running Windows)
486 if (getuid() != 0 && pw && pw->pw_uid != getuid())
487 packet_disconnect("Cannot change user when server not running as root.");
491 * Loop until the user has been authenticated or the connection is
492 * closed, do_authloop() returns only if authentication is successful
494 do_authloop(authctxt);
496 /* The user has been authenticated and accepted. */
497 packet_start(SSH_SMSG_SUCCESS);
501 /* Perform session preparation. */
502 do_authenticated(authctxt);