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.40 2002/04/10 08:21:47 markus Exp $");
29 #include "monitor_wrap.h"
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) &&
203 PRIVSEP(auth_password(authctxt, ""))) {
204 auth_log(authctxt, 1, "without authentication", "");
208 /* Indicate that authentication is needed. */
209 packet_start(SSH_SMSG_FAILURE);
216 /* default to fail */
221 /* Get a packet from the client. */
222 type = packet_read();
224 /* Process the packet. */
227 #if defined(KRB4) || defined(KRB5)
228 case SSH_CMSG_AUTH_KERBEROS:
229 if (!options.kerberos_authentication) {
230 verbose("Kerberos authentication disabled.");
232 char *kdata = packet_get_string(&dlen);
235 if (kdata[0] == 4) { /* KRB_PROT_VERSION */
240 if (tkt.length < MAX_KTXT_LEN)
241 memcpy(tkt.dat, kdata, tkt.length);
243 if (auth_krb4(authctxt, &tkt, &client_user)) {
245 snprintf(info, sizeof(info),
256 if (auth_krb5(authctxt, &tkt, &client_user)) {
258 snprintf(info, sizeof(info),
267 #endif /* KRB4 || KRB5 */
270 case SSH_CMSG_AUTH_GSSAPI:
271 if (!options.gss_authentication) {
272 verbose("GSSAPI authentication disabled.");
276 * GSSAPI was first added to ssh1 in ssh-1.2.27, and
277 * was added to the SecurtCRT product. In order
278 * to continue operating with these, we will add
279 * the equivelent GSSAPI support to SSH1.
280 * Will use the gssapi routines from the ssh2 as
281 * they are almost identical. But they use dispatch
282 * so we need to setup the dispatch tables here
283 * auth1.c for use only by the gssapi code.
284 * Since we already have the packet, we will call
285 * userauth_gssapi then start the dispatch loop.
287 if (!authctxt->valid) {
288 packet_disconnect("Authentication rejected for invalid user");
290 dispatch_init(&auth1_gss_protocol_error);
291 userauth_gssapi(authctxt);
292 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
293 if (authctxt->postponed) { /* failed, try other methods */
294 authctxt->success = 0;
295 authctxt->postponed = 0;
298 do_authenticated(authctxt);
299 /* will only return if authenticated */
304 #if defined(AFS) || defined(KRB5)
305 /* XXX - punt on backward compatibility here. */
306 case SSH_CMSG_HAVE_KERBEROS_TGT:
307 packet_send_debug("Kerberos TGT passing disabled before authentication.");
310 case SSH_CMSG_HAVE_AFS_TOKEN:
311 packet_send_debug("AFS token passing disabled before authentication.");
314 #endif /* AFS || KRB5 */
316 case SSH_CMSG_AUTH_RHOSTS:
317 if (!options.rhosts_authentication) {
318 verbose("Rhosts authentication disabled.");
322 * Get client user name. Note that we just have to
323 * trust the client; this is one reason why rhosts
324 * authentication is insecure. (Another is
325 * IP-spoofing on a local network.)
327 client_user = packet_get_string(&ulen);
330 /* Try to authenticate using /etc/hosts.equiv and .rhosts. */
331 authenticated = auth_rhosts(pw, client_user);
333 snprintf(info, sizeof info, " ruser %.100s", client_user);
336 case SSH_CMSG_AUTH_RHOSTS_RSA:
337 if (!options.rhosts_rsa_authentication) {
338 verbose("Rhosts with RSA authentication disabled.");
342 * Get client user name. Note that we just have to
343 * trust the client; root on the client machine can
344 * claim to be any user.
346 client_user = packet_get_string(&ulen);
348 /* Get the client host key. */
349 client_host_key = key_new(KEY_RSA1);
350 bits = packet_get_int();
351 packet_get_bignum(client_host_key->rsa->e);
352 packet_get_bignum(client_host_key->rsa->n);
354 if (bits != BN_num_bits(client_host_key->rsa->n))
355 verbose("Warning: keysize mismatch for client_host_key: "
356 "actual %d, announced %d",
357 BN_num_bits(client_host_key->rsa->n), bits);
360 authenticated = auth_rhosts_rsa(pw, client_user,
362 key_free(client_host_key);
364 snprintf(info, sizeof info, " ruser %.100s", client_user);
367 case SSH_CMSG_AUTH_RSA:
368 if (!options.rsa_authentication) {
369 verbose("RSA authentication disabled.");
372 /* RSA authentication requested. */
373 if ((n = BN_new()) == NULL)
374 fatal("do_authloop: BN_new failed");
375 packet_get_bignum(n);
377 authenticated = auth_rsa(pw, n);
381 case SSH_CMSG_AUTH_PASSWORD:
382 if (!options.password_authentication) {
383 verbose("Password authentication disabled.");
387 * Read user password. It is in plain text, but was
388 * transmitted over the encrypted channel so it is
389 * not visible to an outside observer.
391 password = packet_get_string(&dlen);
394 /* Try authentication with the password. */
395 authenticated = PRIVSEP(auth_password(authctxt, password));
397 memset(password, 0, strlen(password));
401 case SSH_CMSG_AUTH_TIS:
402 debug("rcvd SSH_CMSG_AUTH_TIS");
403 if (options.challenge_response_authentication == 1) {
404 char *challenge = get_challenge(authctxt);
405 if (challenge != NULL) {
406 debug("sending challenge '%s'", challenge);
407 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
408 packet_put_cstring(challenge);
416 case SSH_CMSG_AUTH_TIS_RESPONSE:
417 debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
418 if (options.challenge_response_authentication == 1) {
419 char *response = packet_get_string(&dlen);
420 debug("got response '%s'", response);
422 authenticated = verify_response(authctxt, response);
423 memset(response, 'r', dlen);
430 * Any unknown messages will be ignored (and failure
431 * returned) during authentication.
433 log("Unknown message during authentication: type %d", type);
438 auth_close(authctxt->as);
442 if (!authctxt->valid && authenticated)
443 fatal("INTERNAL ERROR: authenticated invalid user %s",
448 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD, pw)) {
449 packet_disconnect("Authentication rejected for uid %d.",
450 pw == NULL ? -1 : pw->pw_uid);
454 /* Special handling for root */
455 if (authenticated && authctxt->pw->pw_uid == 0 &&
456 !auth_root_allowed(get_authname(type)))
460 if (!use_privsep && authenticated &&
461 !do_pam_account(pw->pw_name, client_user))
465 /* Log before sending the reply */
466 auth_log(authctxt, authenticated, get_authname(type), info);
468 if (client_user != NULL) {
476 if (authctxt->failures++ > AUTH_FAIL_MAX) {
477 #ifdef WITH_AIXAUTHENTICATE
478 loginfailed(authctxt->user,
479 get_canonical_hostname(options.verify_reverse_mapping),
481 #endif /* WITH_AIXAUTHENTICATE */
482 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
485 packet_start(SSH_SMSG_FAILURE);
492 * Performs authentication of an incoming connection. Session key has already
493 * been exchanged and encryption is enabled.
496 do_authentication(void)
500 char *user, *style = NULL;
502 /* Get the name of the user that we wish to log in as. */
503 packet_read_expect(SSH_CMSG_USER);
505 /* Get the user name. */
506 user = packet_get_string(&ulen);
510 /* Parse GSSAPI identity from userstring */
511 user = ssh1_gssapi_parse_userstring(user);
514 if ((style = strchr(user, ':')) != NULL)
518 /* XXX - SSH.com Kerberos v5 braindeath. */
519 if ((datafellows & SSH_BUG_K5USER) &&
520 options.kerberos_authentication) {
522 if ((p = strchr(user, '@')) != NULL)
527 authctxt = authctxt_new();
528 authctxt->user = user;
529 authctxt->style = style;
531 /* Verify that the user is a valid user. */
532 if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
535 debug("do_authentication: illegal user %s", user);
537 setproctitle("%s%s", authctxt->pw ? user : "unknown",
538 use_privsep ? " [net]" : "");
541 PRIVSEP(start_pam(authctxt->pw == NULL ? "NOUSER" : user));
545 * If we are not running as root, the user must have the same uid as
546 * the server. (Unless you are running Windows)
549 if (!use_privsep && getuid() != 0 && authctxt->pw &&
550 authctxt->pw->pw_uid != getuid())
551 packet_disconnect("Cannot change user when server not running as root.");
555 * Loop until the user has been authenticated or the connection is
556 * closed, do_authloop() returns only if authentication is successful
558 do_authloop(authctxt);
560 /* The user has been authenticated and accepted. */
561 packet_start(SSH_SMSG_SUCCESS);