auth-passwd.c revision 126277
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 126277 2004-02-26 10:52:33Z 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;
68126277Sdes	static int expire_checked = 0;
6957429Smarkm
7098941Sdes#ifndef HAVE_CYGWIN
71126277Sdes	if (pw->pw_uid == 0 && options.permit_root_login != PERMIT_YES)
72124211Sdes		ok = 0;
7398941Sdes#endif
7457429Smarkm	if (*password == '\0' && options.permit_empty_passwd == 0)
7557429Smarkm		return 0;
76113911Sdes
77124211Sdes#if defined(HAVE_OSF_SIA)
78126277Sdes	/*
79126277Sdes	 * XXX: any reason this is before krb?  could be moved to
80126277Sdes	 * sys_auth_passwd()?  -dt
81126277Sdes	 */
82124211Sdes	return auth_sia_password(authctxt, password) && ok;
83126277Sdes#endif
84126277Sdes#ifdef KRB5
8573400Sassar	if (options.kerberos_authentication == 1) {
8692559Sdes		int ret = auth_krb5_password(authctxt, password);
8792559Sdes		if (ret == 1 || ret == 0)
88124211Sdes			return ret && ok;
8957565Smarkm		/* Fall back to ordinary passwd authentication. */
9057565Smarkm	}
91126277Sdes#endif
92126277Sdes#ifdef HAVE_CYGWIN
9398941Sdes	if (is_winnt) {
9498941Sdes		HANDLE hToken = cygwin_logon_user(pw, password);
9598941Sdes
9698941Sdes		if (hToken == INVALID_HANDLE_VALUE)
9798941Sdes			return 0;
9898941Sdes		cygwin_set_impersonation_token(hToken);
99124211Sdes		return ok;
10098941Sdes	}
101126277Sdes#endif
102126277Sdes#if defined(USE_SHADOW) && defined(HAS_SHADOW_EXPIRE)
103126277Sdes	if (!expire_checked) {
104126277Sdes		expire_checked = 1;
105126277Sdes		if (auth_shadow_pwexpired(authctxt)) {
106126277Sdes			disable_forwarding();
107126277Sdes			authctxt->force_pwchange = 1;
108124211Sdes		}
109126277Sdes	}
110126277Sdes#endif
111126277Sdes
112126277Sdes	return (sys_auth_passwd(authctxt, password) && ok);
113126277Sdes}
114124211Sdes
115126277Sdes#ifdef BSD_AUTH
116126277Sdesint
117126277Sdessys_auth_passwd(Authctxt *authctxt, const char *password)
118126277Sdes{
119126277Sdes	struct passwd *pw = authctxt->pw;
120126277Sdes	auth_session_t *as;
121124211Sdes
122126277Sdes	as = auth_usercheck(pw->pw_name, authctxt->style, "auth-ssh",
123126277Sdes	    (char *)password);
124126277Sdes	if (auth_getstate(as) & AUTH_PWEXPIRED) {
125126277Sdes		auth_close(as);
126126277Sdes		disable_forwarding();
127126277Sdes		authctxt->force_pwchange = 1;
128126277Sdes		return (1);
129126277Sdes	} else {
130126277Sdes		return (auth_close(as));
13157429Smarkm	}
132126277Sdes}
133126277Sdes#elif !defined(CUSTOM_SYS_AUTH_PASSWD)
134126277Sdesint
135126277Sdessys_auth_passwd(Authctxt *authctxt, const char *password)
136126277Sdes{
137126277Sdes	struct passwd *pw = authctxt->pw;
138126277Sdes	char *encrypted_password;
139126277Sdes
140124211Sdes	/* Just use the supplied fake password if authctxt is invalid */
141124211Sdes	char *pw_password = authctxt->valid ? shadow_pw(pw) : pw->pw_passwd;
14298941Sdes
14357429Smarkm	/* Check for users with no password. */
144124211Sdes	if (strcmp(pw_password, "") == 0 && strcmp(password, "") == 0)
145126277Sdes		return (1);
14698941Sdes
147126277Sdes	/* Encrypt the candidate password using the proper salt. */
148126277Sdes	encrypted_password = xcrypt(password,
149126277Sdes	    (pw_password[0] && pw_password[1]) ? pw_password : "xx");
15098941Sdes
151126277Sdes	/*
152126277Sdes	 * Authentication is accepted if the encrypted passwords
153126277Sdes	 * are identical.
154126277Sdes	 */
155126277Sdes	return (strcmp(encrypted_password, pw_password) == 0);
15657429Smarkm}
157126277Sdes#endif
158