auth-passwd.c revision 137019
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"
39126277SdesRCSID("$OpenBSD: auth-passwd.c,v 1.31 2004/01/30 09:48:57 markus Exp $");
4099748SdesRCSID("$FreeBSD: head/crypto/openssh/auth-passwd.c 137019 2004-10-28 16:11:31Z des $");
4157429Smarkm
4257429Smarkm#include "packet.h"
4376262Sgreen#include "log.h"
4457429Smarkm#include "servconf.h"
4576262Sgreen#include "auth.h"
46126277Sdes#include "auth-options.h"
4776262Sgreen
48124211Sdesextern ServerOptions options;
49126277Sdesint sys_auth_passwd(Authctxt *, const char *);
50124211Sdes
51126277Sdesvoid
52126277Sdesdisable_forwarding(void)
53126277Sdes{
54126277Sdes	no_port_forwarding_flag = 1;
55126277Sdes	no_agent_forwarding_flag = 1;
56126277Sdes	no_x11_forwarding_flag = 1;
57126277Sdes}
58126277Sdes
5957429Smarkm/*
6057429Smarkm * Tries to authenticate the user using password.  Returns true if
6157429Smarkm * authentication succeeds.
6257429Smarkm */
6365674Skrisint
6476262Sgreenauth_password(Authctxt *authctxt, const char *password)
6557429Smarkm{
6676262Sgreen	struct passwd * pw = authctxt->pw;
67124211Sdes	int ok = authctxt->valid;
68137019Sdes#if defined(USE_SHADOW) && defined(HAS_SHADOW_EXPIRE)
69126277Sdes	static int expire_checked = 0;
70137019Sdes#endif
7157429Smarkm
7298941Sdes#ifndef HAVE_CYGWIN
73126277Sdes	if (pw->pw_uid == 0 && options.permit_root_login != PERMIT_YES)
74124211Sdes		ok = 0;
7598941Sdes#endif
7657429Smarkm	if (*password == '\0' && options.permit_empty_passwd == 0)
7757429Smarkm		return 0;
78113911Sdes
79126277Sdes#ifdef KRB5
8073400Sassar	if (options.kerberos_authentication == 1) {
8192559Sdes		int ret = auth_krb5_password(authctxt, password);
8292559Sdes		if (ret == 1 || ret == 0)
83124211Sdes			return ret && ok;
8457565Smarkm		/* Fall back to ordinary passwd authentication. */
8557565Smarkm	}
86126277Sdes#endif
87126277Sdes#ifdef HAVE_CYGWIN
8898941Sdes	if (is_winnt) {
8998941Sdes		HANDLE hToken = cygwin_logon_user(pw, password);
9098941Sdes
9198941Sdes		if (hToken == INVALID_HANDLE_VALUE)
9298941Sdes			return 0;
9398941Sdes		cygwin_set_impersonation_token(hToken);
94124211Sdes		return ok;
9598941Sdes	}
96126277Sdes#endif
97137019Sdes#ifdef USE_PAM
98137019Sdes	if (options.use_pam)
99137019Sdes		return (sshpam_auth_passwd(authctxt, password) && ok);
100137019Sdes#endif
101126277Sdes#if defined(USE_SHADOW) && defined(HAS_SHADOW_EXPIRE)
102126277Sdes	if (!expire_checked) {
103126277Sdes		expire_checked = 1;
104126277Sdes		if (auth_shadow_pwexpired(authctxt)) {
105126277Sdes			disable_forwarding();
106126277Sdes			authctxt->force_pwchange = 1;
107124211Sdes		}
108126277Sdes	}
109126277Sdes#endif
110126277Sdes
111126277Sdes	return (sys_auth_passwd(authctxt, password) && ok);
112126277Sdes}
113124211Sdes
114126277Sdes#ifdef BSD_AUTH
115126277Sdesint
116126277Sdessys_auth_passwd(Authctxt *authctxt, const char *password)
117126277Sdes{
118126277Sdes	struct passwd *pw = authctxt->pw;
119126277Sdes	auth_session_t *as;
120124211Sdes
121126277Sdes	as = auth_usercheck(pw->pw_name, authctxt->style, "auth-ssh",
122126277Sdes	    (char *)password);
123126277Sdes	if (auth_getstate(as) & AUTH_PWEXPIRED) {
124126277Sdes		auth_close(as);
125126277Sdes		disable_forwarding();
126126277Sdes		authctxt->force_pwchange = 1;
127126277Sdes		return (1);
128126277Sdes	} else {
129126277Sdes		return (auth_close(as));
13057429Smarkm	}
131126277Sdes}
132126277Sdes#elif !defined(CUSTOM_SYS_AUTH_PASSWD)
133126277Sdesint
134126277Sdessys_auth_passwd(Authctxt *authctxt, const char *password)
135126277Sdes{
136126277Sdes	struct passwd *pw = authctxt->pw;
137126277Sdes	char *encrypted_password;
138126277Sdes
139124211Sdes	/* Just use the supplied fake password if authctxt is invalid */
140124211Sdes	char *pw_password = authctxt->valid ? shadow_pw(pw) : pw->pw_passwd;
14198941Sdes
14257429Smarkm	/* Check for users with no password. */
143124211Sdes	if (strcmp(pw_password, "") == 0 && strcmp(password, "") == 0)
144126277Sdes		return (1);
14598941Sdes
146126277Sdes	/* Encrypt the candidate password using the proper salt. */
147126277Sdes	encrypted_password = xcrypt(password,
148126277Sdes	    (pw_password[0] && pw_password[1]) ? pw_password : "xx");
14998941Sdes
150126277Sdes	/*
151126277Sdes	 * Authentication is accepted if the encrypted passwords
152126277Sdes	 * are identical.
153126277Sdes	 */
154126277Sdes	return (strcmp(encrypted_password, pw_password) == 0);
15557429Smarkm}
156126277Sdes#endif
157