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 extern ServerOptions options;
36 #include "globus_gss_assist.h"
39 int userauth_gssapi(Authctxt *authctxt);
42 auth1_gss_protocol_error(int type, u_int32_t plen, void *ctxt)
44 Authctxt *authctxt = ctxt;
45 /* Other side told us to abort, dont need to tell him */
46 /* maybe we can us some other method. */
47 if (type == SSH_MSG_AUTH_GSSAPI_ABORT) {
48 log("auth1: GSSAPI aborting");
49 dispatch_set(SSH_MSG_AUTH_GSSAPI_TOKEN, NULL);
50 authctxt->success = 1; /* get out of loop*/
54 log("auth1: protocol error: type %d plen %d", type, plen);
55 packet_disconnect("Protocol error during GSSAPI authentication: "
56 "Unknown packet type %d", type);
60 * SSH1 GSSAPI clients may send us a user name of the form:
62 * (1) username:x:SSL Subject Name
64 * (2) username:i:SSL Subject Name
68 * if case 1, then uname is an explicit name (ssh -l uname). Keep this
69 * name always, rewrite the user parameter to be just uname. We'll pull
70 * the GSSAPI idenity out and deal with (or skip it) later.
72 * if case 2, then uname is implicit (user didn't use the -l option), so
73 * use the default gridmap mapping and replace uname with whatever
74 * the gridmap maps to. If the gridmap mapping fails, drop down
77 * if case 3, then leave it be.
79 * This function may return the original pointer to the orginal string,
80 * the original pointer to a modified string, or a completely new pointer.
83 ssh1_gssapi_parse_userstring(char *userstring)
85 char name_type = '\0'; /* explicit 'x' or implicit 'i' */
86 char *ssl_subject_name = NULL;
89 debug("Looking at username '%s' for gssapi-ssleay type name", userstring);
90 if((delim = strchr(userstring, ':')) != NULL) {
91 /* Parse and split into components */
92 ssl_subject_name = strchr(delim + 1, ':');
94 if (ssl_subject_name) {
95 /* Successful parse, split into components */
97 name_type = *(delim + 1);
98 *ssl_subject_name = '\0';
101 debug("Name parsed. type = '%c'. ssl subject name is \"%s\"",
102 name_type, ssl_subject_name);
106 debug("Don't understand name format. Letting it pass.");
111 if(ssl_subject_name) {
112 char *gridmapped_name = NULL;
115 debug("explicit name given, using %s as username", userstring);
120 debug("implicit name given. gridmapping '%s'", ssl_subject_name);
122 if(globus_gss_assist_gridmap(ssl_subject_name,
123 &gridmapped_name) == 0) {
124 userstring = gridmapped_name;
125 debug("I gridmapped and got %s", userstring);
128 debug("I gridmapped and got null, reverting to %s", userstring);
133 debug("Unknown name type '%c'. Ignoring.", name_type);
137 debug("didn't find any :'s so I assume it's just a user name");
146 * convert ssh auth msg type into description
149 get_authname(int type)
151 static char buf[1024];
153 case SSH_CMSG_AUTH_PASSWORD:
155 case SSH_CMSG_AUTH_RSA:
157 case SSH_CMSG_AUTH_RHOSTS_RSA:
159 case SSH_CMSG_AUTH_RHOSTS:
161 case SSH_CMSG_AUTH_TIS:
162 case SSH_CMSG_AUTH_TIS_RESPONSE:
163 return "challenge-response";
164 #if defined(KRB4) || defined(KRB5)
165 case SSH_CMSG_AUTH_KERBEROS:
169 case SSH_CMSG_AUTH_GSSAPI:
173 snprintf(buf, sizeof buf, "bad-auth-msg-%d", type);
178 * read packets, try to authenticate the user and
179 * return only if authentication is successful
182 do_authloop(Authctxt *authctxt)
184 int authenticated = 0;
186 Key *client_host_key;
188 char *client_user, *password;
193 struct passwd *pw = authctxt->pw;
195 debug("Attempting authentication for %s%.100s.",
196 authctxt->valid ? "" : "illegal user ", authctxt->user);
198 /* If the user has no password, accept authentication immediately. */
199 if (options.password_authentication &&
200 #if defined(KRB4) || defined(KRB5)
201 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
204 auth_pam_password(pw, "")) {
205 #elif defined(HAVE_OSF_SIA)
208 auth_password(authctxt, "")) {
210 auth_log(authctxt, 1, "without authentication", "");
214 /* Indicate that authentication is needed. */
215 packet_start(SSH_SMSG_FAILURE);
222 /* default to fail */
227 /* Get a packet from the client. */
228 type = packet_read();
230 /* Process the packet. */
233 #if defined(KRB4) || defined(KRB5)
234 case SSH_CMSG_AUTH_KERBEROS:
235 if (!options.kerberos_authentication) {
236 verbose("Kerberos authentication disabled.");
238 char *kdata = packet_get_string(&dlen);
241 if (kdata[0] == 4) { /* KRB_PROT_VERSION */
246 if (tkt.length < MAX_KTXT_LEN)
247 memcpy(tkt.dat, kdata, tkt.length);
249 if (auth_krb4(authctxt, &tkt, &client_user)) {
251 snprintf(info, sizeof(info),
262 if (auth_krb5(authctxt, &tkt, &client_user)) {
264 snprintf(info, sizeof(info),
273 #endif /* KRB4 || KRB5 */
276 case SSH_CMSG_AUTH_GSSAPI:
277 if (!options.gss_authentication) {
278 verbose("GSSAPI authentication disabled.");
282 * GSSAPI was first added to ssh1 in ssh-1.2.27, and
283 * was added to the SecurtCRT product. In order
284 * to continue operating with these, we will add
285 * the equivelent GSSAPI support to SSH1.
286 * Will use the gssapi routines from the ssh2 as
287 * they are almost identical. But they use dispatch
288 * so we need to setup the dispatch tables here
289 * auth1.c for use only by the gssapi code.
290 * Since we already have the packet, we will call
291 * userauth_gssapi then start the dispatch loop.
293 if (!authctxt->valid) {
294 packet_disconnect("Authentication rejected for invalid user");
296 dispatch_init(&auth1_gss_protocol_error);
297 userauth_gssapi(authctxt);
298 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
299 if (authctxt->postponed) { /* failed, try other methods */
300 authctxt->success = 0;
301 authctxt->postponed = 0;
304 do_authenticated(authctxt);
305 /* will only return if authenticated */
310 #if defined(AFS) || defined(KRB5)
311 /* XXX - punt on backward compatibility here. */
312 case SSH_CMSG_HAVE_KERBEROS_TGT:
313 packet_send_debug("Kerberos TGT passing disabled before authentication.");
316 case SSH_CMSG_HAVE_AFS_TOKEN:
317 packet_send_debug("AFS token passing disabled before authentication.");
320 #endif /* AFS || KRB5 */
322 case SSH_CMSG_AUTH_RHOSTS:
323 if (!options.rhosts_authentication) {
324 verbose("Rhosts authentication disabled.");
328 * Get client user name. Note that we just have to
329 * trust the client; this is one reason why rhosts
330 * authentication is insecure. (Another is
331 * IP-spoofing on a local network.)
333 client_user = packet_get_string(&ulen);
336 /* Try to authenticate using /etc/hosts.equiv and .rhosts. */
337 authenticated = auth_rhosts(pw, client_user);
339 snprintf(info, sizeof info, " ruser %.100s", client_user);
342 case SSH_CMSG_AUTH_RHOSTS_RSA:
343 if (!options.rhosts_rsa_authentication) {
344 verbose("Rhosts with RSA authentication disabled.");
348 * Get client user name. Note that we just have to
349 * trust the client; root on the client machine can
350 * claim to be any user.
352 client_user = packet_get_string(&ulen);
354 /* Get the client host key. */
355 client_host_key = key_new(KEY_RSA1);
356 bits = packet_get_int();
357 packet_get_bignum(client_host_key->rsa->e);
358 packet_get_bignum(client_host_key->rsa->n);
360 if (bits != BN_num_bits(client_host_key->rsa->n))
361 verbose("Warning: keysize mismatch for client_host_key: "
362 "actual %d, announced %d",
363 BN_num_bits(client_host_key->rsa->n), bits);
366 authenticated = auth_rhosts_rsa(pw, client_user,
368 key_free(client_host_key);
370 snprintf(info, sizeof info, " ruser %.100s", client_user);
373 case SSH_CMSG_AUTH_RSA:
374 if (!options.rsa_authentication) {
375 verbose("RSA authentication disabled.");
378 /* RSA authentication requested. */
379 if ((n = BN_new()) == NULL)
380 fatal("do_authloop: BN_new failed");
381 packet_get_bignum(n);
383 authenticated = auth_rsa(pw, n);
387 case SSH_CMSG_AUTH_PASSWORD:
388 if (!options.password_authentication) {
389 verbose("Password authentication disabled.");
393 * Read user password. It is in plain text, but was
394 * transmitted over the encrypted channel so it is
395 * not visible to an outside observer.
397 password = packet_get_string(&dlen);
401 /* Do PAM auth with password */
402 authenticated = auth_pam_password(pw, password);
403 #elif defined(HAVE_OSF_SIA)
404 /* Do SIA auth with password */
405 authenticated = auth_sia_password(authctxt->user,
407 #else /* !USE_PAM && !HAVE_OSF_SIA */
408 /* Try authentication with the password. */
409 authenticated = auth_password(authctxt, password);
412 memset(password, 0, strlen(password));
416 case SSH_CMSG_AUTH_TIS:
417 debug("rcvd SSH_CMSG_AUTH_TIS");
418 if (options.challenge_response_authentication == 1) {
419 char *challenge = get_challenge(authctxt);
420 if (challenge != NULL) {
421 debug("sending challenge '%s'", challenge);
422 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
423 packet_put_cstring(challenge);
431 case SSH_CMSG_AUTH_TIS_RESPONSE:
432 debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
433 if (options.challenge_response_authentication == 1) {
434 char *response = packet_get_string(&dlen);
435 debug("got response '%s'", response);
437 authenticated = verify_response(authctxt, response);
438 memset(response, 'r', dlen);
445 * Any unknown messages will be ignored (and failure
446 * returned) during authentication.
448 log("Unknown message during authentication: type %d", type);
453 auth_close(authctxt->as);
457 if (!authctxt->valid && authenticated)
458 fatal("INTERNAL ERROR: authenticated invalid user %s",
463 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD, pw)) {
464 packet_disconnect("Authentication rejected for uid %d.",
465 pw == NULL ? -1 : pw->pw_uid);
469 /* Special handling for root */
470 if (authenticated && authctxt->pw->pw_uid == 0 &&
471 !auth_root_allowed(get_authname(type)))
475 if (authenticated && !do_pam_account(pw->pw_name, client_user))
479 /* Log before sending the reply */
480 auth_log(authctxt, authenticated, get_authname(type), info);
482 if (client_user != NULL) {
490 if (authctxt->failures++ > AUTH_FAIL_MAX) {
491 #ifdef WITH_AIXAUTHENTICATE
492 loginfailed(authctxt->user,
493 get_canonical_hostname(options.verify_reverse_mapping),
495 #endif /* WITH_AIXAUTHENTICATE */
496 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
499 packet_start(SSH_SMSG_FAILURE);
506 * Performs authentication of an incoming connection. Session key has already
507 * been exchanged and encryption is enabled.
510 do_authentication(void)
515 char *p, *user, *style = NULL;
517 /* Get the name of the user that we wish to log in as. */
518 packet_read_expect(SSH_CMSG_USER);
520 /* Get the user name. */
521 user = packet_get_string(&ulen);
525 /* Parse GSSAPI identity from userstring */
526 user = ssh1_gssapi_parse_userstring(user);
529 if ((style = strchr(user, ':')) != NULL)
532 /* XXX - SSH.com Kerberos v5 braindeath. */
533 if ((p = strchr(user, '@')) != NULL)
536 authctxt = authctxt_new();
537 authctxt->user = user;
538 authctxt->style = style;
540 /* Verify that the user is a valid user. */
542 if (pw && allowed_user(pw)) {
546 debug("do_authentication: illegal user %s", user);
551 setproctitle("%s", pw ? user : "unknown");
554 start_pam(pw == NULL ? "NOUSER" : user);
558 * If we are not running as root, the user must have the same uid as
559 * the server. (Unless you are running Windows)
562 if (getuid() != 0 && pw && pw->pw_uid != getuid())
563 packet_disconnect("Cannot change user when server not running as root.");
567 * Loop until the user has been authenticated or the connection is
568 * closed, do_authloop() returns only if authentication is successful
570 do_authloop(authctxt);
572 /* The user has been authenticated and accepted. */
573 packet_start(SSH_SMSG_SUCCESS);
577 /* Perform session preparation. */
578 do_authenticated(authctxt);