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;
35 int userauth_gssapi(Authctxt *authctxt);
38 auth1_gss_protocol_error(int type, u_int32_t plen, void *ctxt)
40 Authctxt *authctxt = ctxt;
41 /* Other side told us to abort, dont need to tell him */
42 /* maybe we can us some other method. */
43 if (type == SSH_MSG_AUTH_GSSAPI_ABORT) {
44 log("auth1: GSSAPI aborting");
45 dispatch_set(SSH_MSG_AUTH_GSSAPI_TOKEN, NULL);
46 authctxt->success = 1; /* get out of loop*/
50 log("auth1: protocol error: type %d plen %d", type, plen);
51 packet_disconnect("Protocol error during GSSAPI authentication: "
52 "Unknown packet type %d", type);
56 * SSH1 GSSAPI clients may send us a user name of the form:
58 * (1) username:x:SSL Subject Name
60 * (2) username:i:SSL Subject Name
64 * if case 1, then uname is an explicit name (ssh -l uname). Keep this
65 * name always, rewrite the user parameter to be just uname. We'll pull
66 * the GSSAPI idenity out and deal with (or skip it) later.
68 * if case 2, then uname is implicit (user didn't use the -l option), so
69 * use the default gridmap mapping and replace uname with whatever
70 * the gridmap maps to. If the gridmap mapping fails, drop down
73 * if case 3, then leave it be.
75 * This function may return the original pointer to the orginal string,
76 * the original pointer to a modified string, or a completely new pointer.
79 ssh1_gssapi_parse_userstring(char *userstring)
81 char name_type = '\0'; /* explicit 'x' or implicit 'i' */
82 char *ssl_subject_name = NULL;
85 debug("Looking at username '%s' for gssapi-ssleay type name", userstring);
86 if((delim = strchr(userstring, ':')) != NULL) {
87 /* Parse and split into components */
88 ssl_subject_name = strchr(delim + 1, ':');
90 if (ssl_subject_name) {
91 /* Successful parse, split into components */
93 name_type = *(delim + 1);
94 *ssl_subject_name = '\0';
97 debug("Name parsed. type = '%c'. ssl subject name is \"%s\"",
98 name_type, ssl_subject_name);
102 debug("Don't understand name format. Letting it pass.");
107 if(ssl_subject_name) {
108 char *gridmapped_name = NULL;
111 debug("explicit name given, using %s as username", userstring);
116 debug("implicit name given. gridmapping '%s'", ssl_subject_name);
118 if(globus_gss_assist_gridmap(ssl_subject_name,
119 &gridmapped_name) == 0) {
120 userstring = gridmapped_name;
121 debug("I gridmapped and got %s", userstring);
124 debug("I gridmapped and got null, reverting to %s", userstring);
129 debug("Unknown name type '%c'. Ignoring.", name_type);
133 debug("didn't find any :'s so I assume it's just a user name");
142 * convert ssh auth msg type into description
145 get_authname(int type)
147 static char buf[1024];
149 case SSH_CMSG_AUTH_PASSWORD:
151 case SSH_CMSG_AUTH_RSA:
153 case SSH_CMSG_AUTH_RHOSTS_RSA:
155 case SSH_CMSG_AUTH_RHOSTS:
157 case SSH_CMSG_AUTH_TIS:
158 case SSH_CMSG_AUTH_TIS_RESPONSE:
159 return "challenge-response";
160 #if defined(KRB4) || defined(KRB5)
161 case SSH_CMSG_AUTH_KERBEROS:
165 case SSH_CMSG_AUTH_GSSAPI:
169 snprintf(buf, sizeof buf, "bad-auth-msg-%d", type);
174 * read packets, try to authenticate the user and
175 * return only if authentication is successful
178 do_authloop(Authctxt *authctxt)
180 int authenticated = 0;
182 Key *client_host_key;
184 char *client_user, *password;
189 struct passwd *pw = authctxt->pw;
191 debug("Attempting authentication for %s%.100s.",
192 authctxt->valid ? "" : "illegal user ", authctxt->user);
194 /* If the user has no password, accept authentication immediately. */
195 if (options.password_authentication &&
196 #if defined(KRB4) || defined(KRB5)
197 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
200 auth_pam_password(pw, "")) {
201 #elif defined(HAVE_OSF_SIA)
204 auth_password(authctxt, "")) {
206 auth_log(authctxt, 1, "without authentication", "");
210 /* Indicate that authentication is needed. */
211 packet_start(SSH_SMSG_FAILURE);
218 /* default to fail */
223 /* Get a packet from the client. */
224 type = packet_read();
226 /* Process the packet. */
229 #if defined(KRB4) || defined(KRB5)
230 case SSH_CMSG_AUTH_KERBEROS:
231 if (!options.kerberos_authentication) {
232 verbose("Kerberos authentication disabled.");
234 char *kdata = packet_get_string(&dlen);
237 if (kdata[0] == 4) { /* KRB_PROT_VERSION */
242 if (tkt.length < MAX_KTXT_LEN)
243 memcpy(tkt.dat, kdata, tkt.length);
245 if (auth_krb4(authctxt, &tkt, &client_user)) {
247 snprintf(info, sizeof(info),
258 if (auth_krb5(authctxt, &tkt, &client_user)) {
260 snprintf(info, sizeof(info),
269 #endif /* KRB4 || KRB5 */
272 case SSH_CMSG_AUTH_GSSAPI:
273 if (!options.gss_authentication) {
274 verbose("GSSAPI authentication disabled.");
278 * GSSAPI was first added to ssh1 in ssh-1.2.27, and
279 * was added to the SecurtCRT product. In order
280 * to continue operating with these, we will add
281 * the equivelent GSSAPI support to SSH1.
282 * Will use the gssapi routines from the ssh2 as
283 * they are almost identical. But they use dispatch
284 * so we need to setup the dispatch tables here
285 * auth1.c for use only by the gssapi code.
286 * Since we already have the packet, we will call
287 * userauth_gssapi then start the dispatch loop.
289 if (!authctxt->valid) {
290 packet_disconnect("Authentication rejected for invalid user");
292 dispatch_init(&auth1_gss_protocol_error);
293 userauth_gssapi(authctxt);
294 dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
295 if (authctxt->postponed) { /* failed, try other methods */
296 authctxt->success = 0;
297 authctxt->postponed = 0;
300 do_authenticated(authctxt);
301 /* will only return if authenticated */
306 #if defined(AFS) || defined(KRB5)
307 /* XXX - punt on backward compatibility here. */
308 case SSH_CMSG_HAVE_KERBEROS_TGT:
309 packet_send_debug("Kerberos TGT passing disabled before authentication.");
312 case SSH_CMSG_HAVE_AFS_TOKEN:
313 packet_send_debug("AFS token passing disabled before authentication.");
316 #endif /* AFS || KRB5 */
318 case SSH_CMSG_AUTH_RHOSTS:
319 if (!options.rhosts_authentication) {
320 verbose("Rhosts authentication disabled.");
324 * Get client user name. Note that we just have to
325 * trust the client; this is one reason why rhosts
326 * authentication is insecure. (Another is
327 * IP-spoofing on a local network.)
329 client_user = packet_get_string(&ulen);
332 /* Try to authenticate using /etc/hosts.equiv and .rhosts. */
333 authenticated = auth_rhosts(pw, client_user);
335 snprintf(info, sizeof info, " ruser %.100s", client_user);
338 case SSH_CMSG_AUTH_RHOSTS_RSA:
339 if (!options.rhosts_rsa_authentication) {
340 verbose("Rhosts with RSA authentication disabled.");
344 * Get client user name. Note that we just have to
345 * trust the client; root on the client machine can
346 * claim to be any user.
348 client_user = packet_get_string(&ulen);
350 /* Get the client host key. */
351 client_host_key = key_new(KEY_RSA1);
352 bits = packet_get_int();
353 packet_get_bignum(client_host_key->rsa->e);
354 packet_get_bignum(client_host_key->rsa->n);
356 if (bits != BN_num_bits(client_host_key->rsa->n))
357 verbose("Warning: keysize mismatch for client_host_key: "
358 "actual %d, announced %d",
359 BN_num_bits(client_host_key->rsa->n), bits);
362 authenticated = auth_rhosts_rsa(pw, client_user,
364 key_free(client_host_key);
366 snprintf(info, sizeof info, " ruser %.100s", client_user);
369 case SSH_CMSG_AUTH_RSA:
370 if (!options.rsa_authentication) {
371 verbose("RSA authentication disabled.");
374 /* RSA authentication requested. */
375 if ((n = BN_new()) == NULL)
376 fatal("do_authloop: BN_new failed");
377 packet_get_bignum(n);
379 authenticated = auth_rsa(pw, n);
383 case SSH_CMSG_AUTH_PASSWORD:
384 if (!options.password_authentication) {
385 verbose("Password authentication disabled.");
389 * Read user password. It is in plain text, but was
390 * transmitted over the encrypted channel so it is
391 * not visible to an outside observer.
393 password = packet_get_string(&dlen);
397 /* Do PAM auth with password */
398 authenticated = auth_pam_password(pw, password);
399 #elif defined(HAVE_OSF_SIA)
400 /* Do SIA auth with password */
401 authenticated = auth_sia_password(authctxt->user,
403 #else /* !USE_PAM && !HAVE_OSF_SIA */
404 /* Try authentication with the password. */
405 authenticated = auth_password(authctxt, password);
408 memset(password, 0, strlen(password));
412 case SSH_CMSG_AUTH_TIS:
413 debug("rcvd SSH_CMSG_AUTH_TIS");
414 if (options.challenge_response_authentication == 1) {
415 char *challenge = get_challenge(authctxt);
416 if (challenge != NULL) {
417 debug("sending challenge '%s'", challenge);
418 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
419 packet_put_cstring(challenge);
427 case SSH_CMSG_AUTH_TIS_RESPONSE:
428 debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
429 if (options.challenge_response_authentication == 1) {
430 char *response = packet_get_string(&dlen);
431 debug("got response '%s'", response);
433 authenticated = verify_response(authctxt, response);
434 memset(response, 'r', dlen);
441 * Any unknown messages will be ignored (and failure
442 * returned) during authentication.
444 log("Unknown message during authentication: type %d", type);
449 auth_close(authctxt->as);
453 if (!authctxt->valid && authenticated)
454 fatal("INTERNAL ERROR: authenticated invalid user %s",
459 !check_nt_auth(type == SSH_CMSG_AUTH_PASSWORD, pw)) {
460 packet_disconnect("Authentication rejected for uid %d.",
461 pw == NULL ? -1 : pw->pw_uid);
465 /* Special handling for root */
466 if (authenticated && authctxt->pw->pw_uid == 0 &&
467 !auth_root_allowed(get_authname(type)))
471 if (authenticated && !do_pam_account(pw->pw_name, client_user))
475 /* Log before sending the reply */
476 auth_log(authctxt, authenticated, get_authname(type), info);
478 if (client_user != NULL) {
486 if (authctxt->failures++ > AUTH_FAIL_MAX) {
487 #ifdef WITH_AIXAUTHENTICATE
488 loginfailed(authctxt->user,
489 get_canonical_hostname(options.verify_reverse_mapping),
491 #endif /* WITH_AIXAUTHENTICATE */
492 packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
495 packet_start(SSH_SMSG_FAILURE);
502 * Performs authentication of an incoming connection. Session key has already
503 * been exchanged and encryption is enabled.
506 do_authentication(void)
511 char *p, *user, *style = NULL;
513 /* Get the name of the user that we wish to log in as. */
514 packet_read_expect(SSH_CMSG_USER);
516 /* Get the user name. */
517 user = packet_get_string(&ulen);
521 /* Parse GSSAPI identity from userstring */
522 user = ssh1_gssapi_parse_userstring(user);
525 if ((style = strchr(user, ':')) != NULL)
528 /* XXX - SSH.com Kerberos v5 braindeath. */
529 if ((p = strchr(user, '@')) != NULL)
532 authctxt = authctxt_new();
533 authctxt->user = user;
534 authctxt->style = style;
536 /* Verify that the user is a valid user. */
538 if (pw && allowed_user(pw)) {
542 debug("do_authentication: illegal user %s", user);
547 setproctitle("%s", pw ? user : "unknown");
550 start_pam(pw == NULL ? "NOUSER" : user);
554 * If we are not running as root, the user must have the same uid as
555 * the server. (Unless you are running Windows)
558 if (getuid() != 0 && pw && pw->pw_uid != getuid())
559 packet_disconnect("Cannot change user when server not running as root.");
563 * Loop until the user has been authenticated or the connection is
564 * closed, do_authloop() returns only if authentication is successful
566 do_authloop(authctxt);
568 /* The user has been authenticated and accepted. */
569 packet_start(SSH_SMSG_SUCCESS);
573 /* Perform session preparation. */
574 do_authenticated(authctxt);