auth-passwd.c revision 124211
157429Smarkm/*
257429Smarkm * Author: Tatu Ylonen <ylo@cs.hut.fi>
357429Smarkm * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
457429Smarkm *                    All rights reserved
557429Smarkm * Password authentication.  This file contains the functions to check whether
657429Smarkm * the password is valid for the user.
765674Skris *
865674Skris * As far as I am concerned, the code I have written for this software
965674Skris * can be used freely for any purpose.  Any derived versions of this
1065674Skris * software must be clearly marked as such, and if the derived work is
1165674Skris * incompatible with the protocol description in the RFC file, it must be
1265674Skris * called by a name other than "ssh" or "Secure Shell".
1365674Skris *
1465674Skris * Copyright (c) 1999 Dug Song.  All rights reserved.
1565674Skris * Copyright (c) 2000 Markus Friedl.  All rights reserved.
1665674Skris *
1765674Skris * Redistribution and use in source and binary forms, with or without
1865674Skris * modification, are permitted provided that the following conditions
1965674Skris * are met:
2065674Skris * 1. Redistributions of source code must retain the above copyright
2165674Skris *    notice, this list of conditions and the following disclaimer.
2265674Skris * 2. Redistributions in binary form must reproduce the above copyright
2365674Skris *    notice, this list of conditions and the following disclaimer in the
2465674Skris *    documentation and/or other materials provided with the distribution.
2565674Skris *
2665674Skris * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2765674Skris * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2865674Skris * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2965674Skris * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
3065674Skris * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
3165674Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3265674Skris * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3365674Skris * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3465674Skris * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3565674Skris * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3657429Smarkm */
3757429Smarkm
3857429Smarkm#include "includes.h"
39124211SdesRCSID("$OpenBSD: auth-passwd.c,v 1.29 2003/08/26 09:58:43 markus Exp $");
4099748SdesRCSID("$FreeBSD: head/crypto/openssh/auth-passwd.c 124211 2004-01-07 11:16:27Z des $");
4157429Smarkm
4257429Smarkm#include "packet.h"
4376262Sgreen#include "log.h"
4457429Smarkm#include "servconf.h"
4576262Sgreen#include "auth.h"
46106130Sdes#ifdef WITH_AIXAUTHENTICATE
47124211Sdes# include "buffer.h"
48124211Sdes# include "canohost.h"
49124211Sdesextern Buffer loginmsg;
50106130Sdes#endif
5176262Sgreen
52124211Sdesextern ServerOptions options;
53124211Sdes
5457429Smarkm/*
5557429Smarkm * Tries to authenticate the user using password.  Returns true if
5657429Smarkm * authentication succeeds.
5757429Smarkm */
5865674Skrisint
5976262Sgreenauth_password(Authctxt *authctxt, const char *password)
6057429Smarkm{
6176262Sgreen	struct passwd * pw = authctxt->pw;
62124211Sdes	int ok = authctxt->valid;
6357429Smarkm
6457429Smarkm	/* deny if no user. */
6557429Smarkm	if (pw == NULL)
6657429Smarkm		return 0;
6798941Sdes#ifndef HAVE_CYGWIN
68124211Sdes	if (pw && pw->pw_uid == 0 && options.permit_root_login != PERMIT_YES)
69124211Sdes		ok = 0;
7098941Sdes#endif
7157429Smarkm	if (*password == '\0' && options.permit_empty_passwd == 0)
7257429Smarkm		return 0;
73113911Sdes
74124211Sdes#if defined(HAVE_OSF_SIA)
75124211Sdes	return auth_sia_password(authctxt, password) && ok;
76113911Sdes#else
77113911Sdes# ifdef KRB5
7873400Sassar	if (options.kerberos_authentication == 1) {
7992559Sdes		int ret = auth_krb5_password(authctxt, password);
8092559Sdes		if (ret == 1 || ret == 0)
81124211Sdes			return ret && ok;
8257565Smarkm		/* Fall back to ordinary passwd authentication. */
8357565Smarkm	}
84113911Sdes# endif
85113911Sdes# ifdef HAVE_CYGWIN
8698941Sdes	if (is_winnt) {
8798941Sdes		HANDLE hToken = cygwin_logon_user(pw, password);
8898941Sdes
8998941Sdes		if (hToken == INVALID_HANDLE_VALUE)
9098941Sdes			return 0;
9198941Sdes		cygwin_set_impersonation_token(hToken);
92124211Sdes		return ok;
9398941Sdes	}
94113911Sdes# endif
95113911Sdes# ifdef WITH_AIXAUTHENTICATE
96124211Sdes	{
97124211Sdes		char *authmsg = NULL;
98124211Sdes		int reenter = 1;
99124211Sdes		int authsuccess = 0;
100106130Sdes
101124211Sdes		if (authenticate(pw->pw_name, password, &reenter,
102124211Sdes		    &authmsg) == 0 && ok) {
103124211Sdes			char *msg;
104124211Sdes			char *host =
105124211Sdes			    (char *)get_canonical_hostname(options.use_dns);
106106130Sdes
107124211Sdes			authsuccess = 1;
108124211Sdes			aix_remove_embedded_newlines(authmsg);
109124211Sdes
110124211Sdes			debug3("AIX/authenticate succeeded for user %s: %.100s",
111124211Sdes				pw->pw_name, authmsg);
112124211Sdes
113124211Sdes	        	/* No pty yet, so just label the line as "ssh" */
114124211Sdes			aix_setauthdb(authctxt->user);
115124211Sdes	        	if (loginsuccess(authctxt->user, host, "ssh",
116124211Sdes			    &msg) == 0) {
117124211Sdes				if (msg != NULL) {
118124211Sdes					debug("%s: msg %s", __func__, msg);
119124211Sdes					buffer_append(&loginmsg, msg,
120124211Sdes					    strlen(msg));
121124211Sdes					xfree(msg);
122124211Sdes				}
123124211Sdes			}
124124211Sdes		} else {
125124211Sdes			debug3("AIX/authenticate failed for user %s: %.100s",
126124211Sdes			    pw->pw_name, authmsg);
127124211Sdes		}
128124211Sdes
129124211Sdes		if (authmsg != NULL)
130124211Sdes			xfree(authmsg);
131124211Sdes
132124211Sdes		return authsuccess;
13357429Smarkm	}
134113911Sdes# endif
135113911Sdes# ifdef BSD_AUTH
13692559Sdes	if (auth_userokay(pw->pw_name, authctxt->style, "auth-ssh",
13792559Sdes	    (char *)password) == 0)
13892559Sdes		return 0;
13992559Sdes	else
140124211Sdes		return ok;
141124211Sdes# else
142124211Sdes	{
143124211Sdes	/* Just use the supplied fake password if authctxt is invalid */
144124211Sdes	char *pw_password = authctxt->valid ? shadow_pw(pw) : pw->pw_passwd;
14598941Sdes
14657429Smarkm	/* Check for users with no password. */
147124211Sdes	if (strcmp(pw_password, "") == 0 && strcmp(password, "") == 0)
148124211Sdes		return ok;
149124211Sdes	else {
150124211Sdes		/* Encrypt the candidate password using the proper salt. */
151124211Sdes		char *encrypted_password = xcrypt(password,
152124211Sdes		    (pw_password[0] && pw_password[1]) ? pw_password : "xx");
15398941Sdes
154124211Sdes		/*
155124211Sdes		 * Authentication is accepted if the encrypted passwords
156124211Sdes		 * are identical.
157124211Sdes		 */
158124211Sdes		return (strcmp(encrypted_password, pw_password) == 0) && ok;
159124211Sdes	}
16098941Sdes
161124211Sdes	}
162124211Sdes# endif
163124211Sdes#endif /* !HAVE_OSF_SIA */
16457429Smarkm}
165