auth-passwd.c revision 162856
1162856Sdes/* $OpenBSD: auth-passwd.c,v 1.40 2006/08/03 03:34:41 deraadt Exp $ */
257429Smarkm/*
357429Smarkm * Author: Tatu Ylonen <ylo@cs.hut.fi>
457429Smarkm * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
557429Smarkm *                    All rights reserved
657429Smarkm * Password authentication.  This file contains the functions to check whether
757429Smarkm * the password is valid for the user.
865674Skris *
965674Skris * As far as I am concerned, the code I have written for this software
1065674Skris * can be used freely for any purpose.  Any derived versions of this
1165674Skris * software must be clearly marked as such, and if the derived work is
1265674Skris * incompatible with the protocol description in the RFC file, it must be
1365674Skris * called by a name other than "ssh" or "Secure Shell".
1465674Skris *
1565674Skris * Copyright (c) 1999 Dug Song.  All rights reserved.
1665674Skris * Copyright (c) 2000 Markus Friedl.  All rights reserved.
1765674Skris *
1865674Skris * Redistribution and use in source and binary forms, with or without
1965674Skris * modification, are permitted provided that the following conditions
2065674Skris * are met:
2165674Skris * 1. Redistributions of source code must retain the above copyright
2265674Skris *    notice, this list of conditions and the following disclaimer.
2365674Skris * 2. Redistributions in binary form must reproduce the above copyright
2465674Skris *    notice, this list of conditions and the following disclaimer in the
2565674Skris *    documentation and/or other materials provided with the distribution.
2665674Skris *
2765674Skris * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2865674Skris * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2965674Skris * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
3065674Skris * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
3165674Skris * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
3265674Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3365674Skris * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3465674Skris * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3565674Skris * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3665674Skris * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3757429Smarkm */
3857429Smarkm
3957429Smarkm#include "includes.h"
40162856Sdes__RCSID("$FreeBSD: head/crypto/openssh/auth-passwd.c 162856 2006-09-30 13:38:06Z des $");
4157429Smarkm
42162856Sdes#include <sys/types.h>
43162856Sdes
44162856Sdes#include <pwd.h>
45162856Sdes#include <stdio.h>
46162856Sdes#include <string.h>
47162856Sdes#include <stdarg.h>
48162856Sdes
4957429Smarkm#include "packet.h"
50147005Sdes#include "buffer.h"
5176262Sgreen#include "log.h"
5257429Smarkm#include "servconf.h"
53162856Sdes#include "key.h"
54162856Sdes#include "hostfile.h"
5576262Sgreen#include "auth.h"
56126277Sdes#include "auth-options.h"
5776262Sgreen
58147005Sdesextern Buffer loginmsg;
59124211Sdesextern ServerOptions options;
60124211Sdes
61147005Sdes#ifdef HAVE_LOGIN_CAP
62147005Sdesextern login_cap_t *lc;
63147005Sdes#endif
64147005Sdes
65147005Sdes
66147005Sdes#define DAY		(24L * 60 * 60) /* 1 day in seconds */
67147005Sdes#define TWO_WEEKS	(2L * 7 * DAY)	/* 2 weeks in seconds */
68147005Sdes
69126277Sdesvoid
70126277Sdesdisable_forwarding(void)
71126277Sdes{
72126277Sdes	no_port_forwarding_flag = 1;
73126277Sdes	no_agent_forwarding_flag = 1;
74126277Sdes	no_x11_forwarding_flag = 1;
75126277Sdes}
76126277Sdes
7757429Smarkm/*
7857429Smarkm * Tries to authenticate the user using password.  Returns true if
7957429Smarkm * authentication succeeds.
8057429Smarkm */
8165674Skrisint
8276262Sgreenauth_password(Authctxt *authctxt, const char *password)
8357429Smarkm{
8476262Sgreen	struct passwd * pw = authctxt->pw;
85147005Sdes	int result, ok = authctxt->valid;
86137019Sdes#if defined(USE_SHADOW) && defined(HAS_SHADOW_EXPIRE)
87126277Sdes	static int expire_checked = 0;
88137019Sdes#endif
8957429Smarkm
9098941Sdes#ifndef HAVE_CYGWIN
91126277Sdes	if (pw->pw_uid == 0 && options.permit_root_login != PERMIT_YES)
92124211Sdes		ok = 0;
9398941Sdes#endif
9457429Smarkm	if (*password == '\0' && options.permit_empty_passwd == 0)
9557429Smarkm		return 0;
96113911Sdes
97126277Sdes#ifdef KRB5
9873400Sassar	if (options.kerberos_authentication == 1) {
9992559Sdes		int ret = auth_krb5_password(authctxt, password);
10092559Sdes		if (ret == 1 || ret == 0)
101124211Sdes			return ret && ok;
10257565Smarkm		/* Fall back to ordinary passwd authentication. */
10357565Smarkm	}
104126277Sdes#endif
105126277Sdes#ifdef HAVE_CYGWIN
10698941Sdes	if (is_winnt) {
10798941Sdes		HANDLE hToken = cygwin_logon_user(pw, password);
10898941Sdes
10998941Sdes		if (hToken == INVALID_HANDLE_VALUE)
11098941Sdes			return 0;
11198941Sdes		cygwin_set_impersonation_token(hToken);
112124211Sdes		return ok;
11398941Sdes	}
114126277Sdes#endif
115137019Sdes#ifdef USE_PAM
116137019Sdes	if (options.use_pam)
117137019Sdes		return (sshpam_auth_passwd(authctxt, password) && ok);
118137019Sdes#endif
119126277Sdes#if defined(USE_SHADOW) && defined(HAS_SHADOW_EXPIRE)
120126277Sdes	if (!expire_checked) {
121126277Sdes		expire_checked = 1;
122147005Sdes		if (auth_shadow_pwexpired(authctxt))
123126277Sdes			authctxt->force_pwchange = 1;
124126277Sdes	}
125126277Sdes#endif
126147005Sdes	result = sys_auth_passwd(authctxt, password);
127147005Sdes	if (authctxt->force_pwchange)
128147005Sdes		disable_forwarding();
129147005Sdes	return (result && ok);
130126277Sdes}
131124211Sdes
132126277Sdes#ifdef BSD_AUTH
133147005Sdesstatic void
134147005Sdeswarn_expiry(Authctxt *authctxt, auth_session_t *as)
135147005Sdes{
136147005Sdes	char buf[256];
137147005Sdes	quad_t pwtimeleft, actimeleft, daysleft, pwwarntime, acwarntime;
138147005Sdes
139147005Sdes	pwwarntime = acwarntime = TWO_WEEKS;
140147005Sdes
141147005Sdes	pwtimeleft = auth_check_change(as);
142147005Sdes	actimeleft = auth_check_expire(as);
143147005Sdes#ifdef HAVE_LOGIN_CAP
144147005Sdes	if (authctxt->valid) {
145147005Sdes		pwwarntime = login_getcaptime(lc, "password-warn", TWO_WEEKS,
146147005Sdes		    TWO_WEEKS);
147147005Sdes		acwarntime = login_getcaptime(lc, "expire-warn", TWO_WEEKS,
148147005Sdes		    TWO_WEEKS);
149147005Sdes	}
150147005Sdes#endif
151147005Sdes	if (pwtimeleft != 0 && pwtimeleft < pwwarntime) {
152147005Sdes		daysleft = pwtimeleft / DAY + 1;
153147005Sdes		snprintf(buf, sizeof(buf),
154147005Sdes		    "Your password will expire in %lld day%s.\n",
155147005Sdes		    daysleft, daysleft == 1 ? "" : "s");
156147005Sdes		buffer_append(&loginmsg, buf, strlen(buf));
157147005Sdes	}
158147005Sdes	if (actimeleft != 0 && actimeleft < acwarntime) {
159147005Sdes		daysleft = actimeleft / DAY + 1;
160147005Sdes		snprintf(buf, sizeof(buf),
161147005Sdes		    "Your account will expire in %lld day%s.\n",
162147005Sdes		    daysleft, daysleft == 1 ? "" : "s");
163147005Sdes		buffer_append(&loginmsg, buf, strlen(buf));
164147005Sdes	}
165147005Sdes}
166147005Sdes
167126277Sdesint
168126277Sdessys_auth_passwd(Authctxt *authctxt, const char *password)
169126277Sdes{
170126277Sdes	struct passwd *pw = authctxt->pw;
171126277Sdes	auth_session_t *as;
172147005Sdes	static int expire_checked = 0;
173124211Sdes
174126277Sdes	as = auth_usercheck(pw->pw_name, authctxt->style, "auth-ssh",
175126277Sdes	    (char *)password);
176149753Sdes	if (as == NULL)
177149753Sdes		return (0);
178126277Sdes	if (auth_getstate(as) & AUTH_PWEXPIRED) {
179126277Sdes		auth_close(as);
180126277Sdes		disable_forwarding();
181126277Sdes		authctxt->force_pwchange = 1;
182126277Sdes		return (1);
183126277Sdes	} else {
184147005Sdes		if (!expire_checked) {
185147005Sdes			expire_checked = 1;
186147005Sdes			warn_expiry(authctxt, as);
187147005Sdes		}
188126277Sdes		return (auth_close(as));
18957429Smarkm	}
190126277Sdes}
191126277Sdes#elif !defined(CUSTOM_SYS_AUTH_PASSWD)
192126277Sdesint
193126277Sdessys_auth_passwd(Authctxt *authctxt, const char *password)
194126277Sdes{
195126277Sdes	struct passwd *pw = authctxt->pw;
196126277Sdes	char *encrypted_password;
197126277Sdes
198124211Sdes	/* Just use the supplied fake password if authctxt is invalid */
199124211Sdes	char *pw_password = authctxt->valid ? shadow_pw(pw) : pw->pw_passwd;
20098941Sdes
20157429Smarkm	/* Check for users with no password. */
202124211Sdes	if (strcmp(pw_password, "") == 0 && strcmp(password, "") == 0)
203126277Sdes		return (1);
20498941Sdes
205126277Sdes	/* Encrypt the candidate password using the proper salt. */
206126277Sdes	encrypted_password = xcrypt(password,
207126277Sdes	    (pw_password[0] && pw_password[1]) ? pw_password : "xx");
20898941Sdes
209126277Sdes	/*
210126277Sdes	 * Authentication is accepted if the encrypted passwords
211126277Sdes	 * are identical.
212126277Sdes	 */
213126277Sdes	return (strcmp(encrypted_password, pw_password) == 0);
21457429Smarkm}
215126277Sdes#endif
216