2 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * Copyright (c) 2000 Markus Friedl. All rights reserved.
8 RCSID("$OpenBSD: auth.c,v 1.2 2000/04/06 08:55:22 markus Exp $");
31 extern ServerOptions options;
32 extern char *forced_command;
35 * Check if the user is allowed to log in via ssh. If user is listed in
36 * DenyUsers or user's primary group is listed in DenyGroups, false will
37 * be returned. If AllowUsers isn't empty and user isn't listed there, or
38 * if AllowGroups isn't empty and user isn't listed there, false will be
40 * If the user's shell is not executable, false will be returned.
41 * Otherwise true is returned.
44 allowed_user(struct passwd * pw)
49 #ifdef WITH_AIXAUTHENTICATE
51 #endif /* WITH_AIXAUTHENTICATE */
53 /* Shouldn't be called if pw is NULL, but better safe than sorry... */
57 /* deny if shell does not exists or is not executable */
58 if (stat(pw->pw_shell, &st) != 0)
60 if (!((st.st_mode & S_IFREG) && (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP))))
63 /* Return false if user is listed in DenyUsers */
64 if (options.num_deny_users > 0) {
67 for (i = 0; i < options.num_deny_users; i++)
68 if (match_pattern(pw->pw_name, options.deny_users[i]))
71 /* Return false if AllowUsers isn't empty and user isn't listed there */
72 if (options.num_allow_users > 0) {
75 for (i = 0; i < options.num_allow_users; i++)
76 if (match_pattern(pw->pw_name, options.allow_users[i]))
78 /* i < options.num_allow_users iff we break for loop */
79 if (i >= options.num_allow_users)
82 /* Get the primary group name if we need it. Return false if it fails */
83 if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
84 grp = getgrgid(pw->pw_gid);
88 /* Return false if user's group is listed in DenyGroups */
89 if (options.num_deny_groups > 0) {
92 for (i = 0; i < options.num_deny_groups; i++)
93 if (match_pattern(grp->gr_name, options.deny_groups[i]))
97 * Return false if AllowGroups isn't empty and user's group
100 if (options.num_allow_groups > 0) {
103 for (i = 0; i < options.num_allow_groups; i++)
104 if (match_pattern(grp->gr_name, options.allow_groups[i]))
106 /* i < options.num_allow_groups iff we break for
108 if (i >= options.num_allow_groups)
113 #ifdef WITH_AIXAUTHENTICATE
114 if (loginrestrictions(pw->pw_name,S_LOGIN,NULL,&loginmsg) != 0)
116 #endif /* WITH_AIXAUTHENTICATE */
118 /* We found no reason not to let this user try to log on... */
123 * convert ssh auth msg type into description
126 get_authname(int type)
128 static char buf[1024];
130 case SSH_CMSG_AUTH_PASSWORD:
132 case SSH_CMSG_AUTH_RSA:
134 case SSH_CMSG_AUTH_RHOSTS_RSA:
136 case SSH_CMSG_AUTH_RHOSTS:
139 case SSH_CMSG_AUTH_KERBEROS:
143 case SSH_CMSG_AUTH_TIS_RESPONSE:
147 snprintf(buf, sizeof buf, "bad-auth-msg-%d", type);
151 #define AUTH_FAIL_MAX 6
152 #define AUTH_FAIL_LOG (AUTH_FAIL_MAX/2)
153 #define AUTH_FAIL_MSG "Too many authentication failures for %.100s"
156 * The user does not exist or access is denied,
157 * but fake indication that authentication is needed.
160 do_fake_authloop1(char *user)
164 log("Faking authloop for illegal user %.200s from %.200s port %d",
169 #ifdef WITH_AIXAUTHENTICATE
170 if (strncmp(get_authname(type),"password",
171 strlen(get_authname(type))) == 0)
172 loginfailed(pw->pw_name,get_canonical_hostname(),"ssh");
173 #endif /* WITH_AIXAUTHENTICATE */
175 /* Indicate that authentication is needed. */
176 packet_start(SSH_SMSG_FAILURE);
181 * Keep reading packets, and always respond with a failure. This is
182 * to avoid disclosing whether such a user really exists.
184 for (attempt = 1;; attempt++) {
185 /* Read a packet. This will not return if the client disconnects. */
188 (void)packet_read(&plen);
190 int type = packet_read(&plen);
192 char *password, *skeyinfo;
193 /* Try to send a fake s/key challenge. */
194 if (options.skey_authentication == 1 &&
195 (skeyinfo = skey_fake_keyinfo(user)) != NULL) {
197 if (type == SSH_CMSG_AUTH_TIS) {
198 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
199 packet_put_string(skeyinfo, strlen(skeyinfo));
203 } else if (type == SSH_CMSG_AUTH_PASSWORD &&
204 options.password_authentication &&
205 (password = packet_get_string(&dlen)) != NULL &&
207 strncasecmp(password, "s/key", 5) == 0 ) {
208 packet_send_debug(skeyinfo);
210 if (password != NULL)
214 if (attempt > AUTH_FAIL_MAX)
215 packet_disconnect(AUTH_FAIL_MSG, user);
218 * Send failure. This should be indistinguishable from a
219 * failed authentication.
221 packet_start(SSH_SMSG_FAILURE);
230 * read packets and try to authenticate local user *pw.
231 * return if authentication is successfull
234 do_authloop(struct passwd * pw)
238 RSA *client_host_key;
240 char *client_user = NULL, *password = NULL;
243 int plen, nlen, elen;
246 void (*authlog) (const char *fmt,...) = verbose;
248 /* Indicate that authentication is needed. */
249 packet_start(SSH_SMSG_FAILURE);
253 for (attempt = 1;; attempt++) {
254 int authenticated = 0;
255 strlcpy(user, "", sizeof user);
257 /* Get a packet from the client. */
258 type = packet_read(&plen);
260 /* Process the packet. */
263 case SSH_CMSG_HAVE_KERBEROS_TGT:
264 if (!options.kerberos_tgt_passing) {
265 /* packet_get_all(); */
266 verbose("Kerberos tgt passing disabled.");
269 /* Accept Kerberos tgt. */
270 char *tgt = packet_get_string(&dlen);
271 packet_integrity_check(plen, 4 + dlen, type);
272 if (!auth_kerberos_tgt(pw, tgt))
273 verbose("Kerberos tgt REFUSED for %s", pw->pw_name);
278 case SSH_CMSG_HAVE_AFS_TOKEN:
279 if (!options.afs_token_passing || !k_hasafs()) {
280 /* packet_get_all(); */
281 verbose("AFS token passing disabled.");
284 /* Accept AFS token. */
285 char *token_string = packet_get_string(&dlen);
286 packet_integrity_check(plen, 4 + dlen, type);
287 if (!auth_afs_token(pw, token_string))
288 verbose("AFS token REFUSED for %s", pw->pw_name);
294 case SSH_CMSG_AUTH_KERBEROS:
295 if (!options.kerberos_authentication) {
296 /* packet_get_all(); */
297 verbose("Kerberos authentication disabled.");
300 /* Try Kerberos v4 authentication. */
302 char *tkt_user = NULL;
303 char *kdata = packet_get_string((unsigned int *) &auth.length);
304 packet_integrity_check(plen, 4 + auth.length, type);
306 if (auth.length < MAX_KTXT_LEN)
307 memcpy(auth.dat, kdata, auth.length);
310 authenticated = auth_krb4(pw->pw_name, &auth, &tkt_user);
313 snprintf(user, sizeof user, " tktuser %s", tkt_user);
320 case SSH_CMSG_AUTH_RHOSTS:
321 if (!options.rhosts_authentication) {
322 verbose("Rhosts authentication disabled.");
326 * Get client user name. Note that we just have to
327 * trust the client; this is one reason why rhosts
328 * authentication is insecure. (Another is
329 * IP-spoofing on a local network.)
331 client_user = packet_get_string(&ulen);
332 packet_integrity_check(plen, 4 + ulen, type);
334 /* Try to authenticate using /etc/hosts.equiv and
336 authenticated = auth_rhosts(pw, client_user);
338 snprintf(user, sizeof user, " ruser %s", client_user);
341 case SSH_CMSG_AUTH_RHOSTS_RSA:
342 if (!options.rhosts_rsa_authentication) {
343 verbose("Rhosts with RSA authentication disabled.");
347 * Get client user name. Note that we just have to
348 * trust the client; root on the client machine can
349 * claim to be any user.
351 client_user = packet_get_string(&ulen);
353 /* Get the client host key. */
354 client_host_key = RSA_new();
355 if (client_host_key == NULL)
356 fatal("RSA_new failed");
357 client_host_key->e = BN_new();
358 client_host_key->n = BN_new();
359 if (client_host_key->e == NULL || client_host_key->n == NULL)
360 fatal("BN_new failed");
361 bits = packet_get_int();
362 packet_get_bignum(client_host_key->e, &elen);
363 packet_get_bignum(client_host_key->n, &nlen);
365 if (bits != BN_num_bits(client_host_key->n))
366 error("Warning: keysize mismatch for client_host_key: "
367 "actual %d, announced %d", BN_num_bits(client_host_key->n), bits);
368 packet_integrity_check(plen, (4 + ulen) + 4 + elen + nlen, type);
370 authenticated = auth_rhosts_rsa(pw, client_user, client_host_key);
371 RSA_free(client_host_key);
373 snprintf(user, sizeof user, " ruser %s", client_user);
376 case SSH_CMSG_AUTH_RSA:
377 if (!options.rsa_authentication) {
378 verbose("RSA authentication disabled.");
381 /* RSA authentication requested. */
383 packet_get_bignum(n, &nlen);
384 packet_integrity_check(plen, nlen, type);
385 authenticated = auth_rsa(pw, n);
389 case SSH_CMSG_AUTH_PASSWORD:
390 if (!options.password_authentication) {
391 verbose("Password authentication disabled.");
395 * Read user password. It is in plain text, but was
396 * transmitted over the encrypted channel so it is
397 * not visible to an outside observer.
399 password = packet_get_string(&dlen);
400 packet_integrity_check(plen, 4 + dlen, type);
403 /* Do PAM auth with password */
404 authenticated = auth_pam_password(pw, password);
406 /* Try authentication with the password. */
407 authenticated = auth_password(pw, password);
409 memset(password, 0, strlen(password));
414 case SSH_CMSG_AUTH_TIS:
415 debug("rcvd SSH_CMSG_AUTH_TIS");
416 if (options.skey_authentication == 1) {
417 char *skeyinfo = skey_keyinfo(pw->pw_name);
418 if (skeyinfo == NULL) {
419 debug("generating fake skeyinfo for %.100s.", pw->pw_name);
420 skeyinfo = skey_fake_keyinfo(pw->pw_name);
422 if (skeyinfo != NULL) {
423 /* we send our s/key- in tis-challenge messages */
424 debug("sending challenge '%s'", skeyinfo);
425 packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
426 packet_put_string(skeyinfo, strlen(skeyinfo));
433 case SSH_CMSG_AUTH_TIS_RESPONSE:
434 debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
435 if (options.skey_authentication == 1) {
436 char *response = packet_get_string(&dlen);
437 debug("skey response == '%s'", response);
438 packet_integrity_check(plen, 4 + dlen, type);
439 authenticated = (skey_haskey(pw->pw_name) == 0 &&
440 skey_passcheck(pw->pw_name, response) != -1);
445 case SSH_CMSG_AUTH_TIS:
446 /* TIS Authentication is unsupported */
447 log("TIS authentication unsupported.");
453 * Any unknown messages will be ignored (and failure
454 * returned) during authentication.
456 log("Unknown message during authentication: type %d", type);
461 * Check if the user is logging in as root and root logins
463 * Note that root login is allowed for forced commands.
465 if (authenticated && pw->pw_uid == 0 && !options.permit_root_login) {
466 if (forced_command) {
467 log("Root login accepted for forced command.");
470 log("ROOT LOGIN REFUSED FROM %.200s",
471 get_canonical_hostname());
475 /* Raise logging level */
477 attempt == AUTH_FAIL_LOG ||
478 type == SSH_CMSG_AUTH_PASSWORD)
481 authlog("%s %s for %.200s from %.200s port %d%s",
482 authenticated ? "Accepted" : "Failed",
484 pw->pw_uid == 0 ? "ROOT" : pw->pw_name,
491 if (!do_pam_account(pw->pw_name, client_user)) {
492 if (client_user != NULL) {
496 do_fake_authloop1(pw->pw_name);
506 if (client_user != NULL) {
511 if (attempt > AUTH_FAIL_MAX)
512 packet_disconnect(AUTH_FAIL_MSG, pw->pw_name);
514 /* Send a message indicating that the authentication attempt failed. */
515 packet_start(SSH_SMSG_FAILURE);
522 * Performs authentication of an incoming connection. Session key has already
523 * been exchanged and encryption is enabled.
528 struct passwd *pw, pwcopy;
532 #ifdef WITH_AIXAUTHENTICATE
534 #endif /* WITH_AIXAUTHENTICATE */
536 /* Get the name of the user that we wish to log in as. */
537 packet_read_expect(&plen, SSH_CMSG_USER);
539 /* Get the user name. */
540 user = packet_get_string(&ulen);
541 packet_integrity_check(plen, (4 + ulen), SSH_CMSG_USER);
543 setproctitle("%s", user);
546 /* If machine has AFS, set process authentication group. */
553 /* Verify that the user is a valid user. */
555 if (!pw || !allowed_user(pw))
556 do_fake_authloop1(user);
559 /* Take a copy of the returned structure. */
560 memset(&pwcopy, 0, sizeof(pwcopy));
561 pwcopy.pw_name = xstrdup(pw->pw_name);
562 pwcopy.pw_passwd = xstrdup(pw->pw_passwd);
563 pwcopy.pw_uid = pw->pw_uid;
564 pwcopy.pw_gid = pw->pw_gid;
565 pwcopy.pw_dir = xstrdup(pw->pw_dir);
566 pwcopy.pw_shell = xstrdup(pw->pw_shell);
574 * If we are not running as root, the user must have the same uid as
577 if (getuid() != 0 && pw->pw_uid != getuid())
578 packet_disconnect("Cannot change user when server not running as root.");
580 debug("Attempting authentication for %.100s.", pw->pw_name);
582 /* If the user has no password, accept authentication immediately. */
583 if (options.password_authentication &&
585 (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
588 auth_pam_password(pw, "")) {
590 auth_password(pw, "")) {
592 /* Authentication with empty password succeeded. */
593 log("Login for user %s from %.100s, accepted without authentication.",
594 pw->pw_name, get_remote_ipaddr());
596 /* Loop until the user has been authenticated or the
597 connection is closed, do_authloop() returns only if
598 authentication is successfull */
602 /* The user has been authenticated and accepted. */
603 #ifdef WITH_AIXAUTHENTICATE
604 loginsuccess(user,get_canonical_hostname(),"ssh",&loginmsg);
605 #endif /* WITH_AIXAUTHENTICATE */
606 packet_start(SSH_SMSG_SUCCESS);
610 /* Perform session preparation. */
611 do_authenticated(pw);
615 void input_service_request(int type, int plen);
616 void input_userauth_request(int type, int plen);
617 void ssh2_pty_cleanup(void);
619 typedef struct Authctxt Authctxt;
626 static Authctxt *authctxt = NULL;
627 static int userauth_success = 0;
632 return (authctxt != NULL && authctxt->valid) ? &authctxt->pw : NULL;
635 auth_set_user(char *u, char *s)
637 struct passwd *pw, *copy;
639 if (authctxt == NULL) {
640 authctxt = xmalloc(sizeof(*authctxt));
642 authctxt->user = xstrdup(u);
643 authctxt->service = xstrdup(s);
644 setproctitle("%s", u);
646 if (!pw || !allowed_user(pw)) {
647 log("auth_set_user: bad user %s", u);
653 copy = &authctxt->pw;
654 memset(copy, 0, sizeof(*copy));
655 copy->pw_name = xstrdup(pw->pw_name);
656 copy->pw_passwd = xstrdup(pw->pw_passwd);
657 copy->pw_uid = pw->pw_uid;
658 copy->pw_gid = pw->pw_gid;
659 copy->pw_dir = xstrdup(pw->pw_dir);
660 copy->pw_shell = xstrdup(pw->pw_shell);
663 if (strcmp(u, authctxt->user) != 0 ||
664 strcmp(s, authctxt->service) != 0) {
665 log("auth_set_user: missmatch: (%s,%s)!=(%s,%s)",
666 u, s, authctxt->user, authctxt->service);
670 return auth_get_user();
674 protocol_error(int type, int plen)
676 log("auth: protocol error: type %d plen %d", type, plen);
677 packet_start(SSH2_MSG_UNIMPLEMENTED);
683 input_service_request(int type, int plen)
687 char *service = packet_get_string(&len);
689 if (strcmp(service, "ssh-userauth") == 0) {
690 if (!userauth_success) {
692 /* now we can handle user-auth requests */
693 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
696 /* XXX all other service requests are denied */
699 packet_start(SSH2_MSG_SERVICE_ACCEPT);
700 packet_put_cstring(service);
704 debug("bad service request %s", service);
705 packet_disconnect("bad service request %s", service);
710 input_userauth_request(int type, int plen)
714 int c, authenticated = 0;
715 char *user, *service, *method;
718 if (++try == AUTH_FAIL_MAX)
719 packet_disconnect("too many failed userauth_requests");
721 user = packet_get_string(&len);
722 service = packet_get_string(&len);
723 method = packet_get_string(&len);
724 debug("userauth-request for user %s service %s method %s", user, service, method);
726 /* XXX we only allow the ssh-connection service */
727 pw = auth_set_user(user, service);
728 if (pw && strcmp(service, "ssh-connection")==0) {
729 if (strcmp(method, "none") == 0 && try == 1) {
731 /* Do PAM auth with password */
732 authenticated = auth_pam_password(pw, "");
734 /* Try authentication with the password. */
735 authenticated = auth_password(pw, "");
737 } else if (strcmp(method, "password") == 0) {
739 c = packet_get_char();
741 debug("password change not supported");
742 password = packet_get_string(&len);
744 /* Do PAM auth with password */
745 authenticated = auth_pam_password(pw, password);
747 /* Try authentication with the password. */
748 authenticated = auth_password(pw, password);
750 memset(password, 0, len);
752 } else if (strcmp(method, "publickey") == 0) {
756 c = packet_get_char();
757 pkalg = packet_get_string(&len);
758 pkblob = packet_get_string(&len);
763 /* XXX check if other auth methods are needed */
765 /* turn off userauth */
766 dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &protocol_error);
768 packet_start(SSH2_MSG_USERAUTH_SUCCESS);
771 log("userauth success for %s", user);
772 /* now we can break out */
773 userauth_success = 1;
775 packet_start(SSH2_MSG_USERAUTH_FAILURE);
776 packet_put_cstring("password");
777 packet_put_char(0); /* partial success */
788 dispatch_init(&protocol_error);
789 dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
790 dispatch_run(DISPATCH_BLOCK, &userauth_success);