1181111Sdes/* $OpenBSD: auth-passwd.c,v 1.43 2007/09/21 08:15:29 djm 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"
4057429Smarkm
41162856Sdes#include <sys/types.h>
42162856Sdes
43162856Sdes#include <pwd.h>
44162856Sdes#include <stdio.h>
45162856Sdes#include <string.h>
46162856Sdes#include <stdarg.h>
47162856Sdes
4857429Smarkm#include "packet.h"
49147005Sdes#include "buffer.h"
5076262Sgreen#include "log.h"
5157429Smarkm#include "servconf.h"
52162856Sdes#include "key.h"
53162856Sdes#include "hostfile.h"
5476262Sgreen#include "auth.h"
55126277Sdes#include "auth-options.h"
5676262Sgreen
57147005Sdesextern Buffer loginmsg;
58124211Sdesextern ServerOptions options;
59124211Sdes
60147005Sdes#ifdef HAVE_LOGIN_CAP
61147005Sdesextern login_cap_t *lc;
62147005Sdes#endif
63147005Sdes
64147005Sdes
65147005Sdes#define DAY		(24L * 60 * 60) /* 1 day in seconds */
66147005Sdes#define TWO_WEEKS	(2L * 7 * DAY)	/* 2 weeks in seconds */
67147005Sdes
68126277Sdesvoid
69126277Sdesdisable_forwarding(void)
70126277Sdes{
71126277Sdes	no_port_forwarding_flag = 1;
72126277Sdes	no_agent_forwarding_flag = 1;
73126277Sdes	no_x11_forwarding_flag = 1;
74126277Sdes}
75126277Sdes
7657429Smarkm/*
7757429Smarkm * Tries to authenticate the user using password.  Returns true if
7857429Smarkm * authentication succeeds.
7957429Smarkm */
8065674Skrisint
8176262Sgreenauth_password(Authctxt *authctxt, const char *password)
8257429Smarkm{
8376262Sgreen	struct passwd * pw = authctxt->pw;
84147005Sdes	int result, ok = authctxt->valid;
85137019Sdes#if defined(USE_SHADOW) && defined(HAS_SHADOW_EXPIRE)
86126277Sdes	static int expire_checked = 0;
87137019Sdes#endif
8857429Smarkm
8998941Sdes#ifndef HAVE_CYGWIN
90126277Sdes	if (pw->pw_uid == 0 && options.permit_root_login != PERMIT_YES)
91124211Sdes		ok = 0;
9298941Sdes#endif
9357429Smarkm	if (*password == '\0' && options.permit_empty_passwd == 0)
9457429Smarkm		return 0;
95113911Sdes
96126277Sdes#ifdef KRB5
9773400Sassar	if (options.kerberos_authentication == 1) {
9892559Sdes		int ret = auth_krb5_password(authctxt, password);
9992559Sdes		if (ret == 1 || ret == 0)
100124211Sdes			return ret && ok;
10157565Smarkm		/* Fall back to ordinary passwd authentication. */
10257565Smarkm	}
103126277Sdes#endif
104126277Sdes#ifdef HAVE_CYGWIN
105197679Sdes	{
10698941Sdes		HANDLE hToken = cygwin_logon_user(pw, password);
10798941Sdes
10898941Sdes		if (hToken == INVALID_HANDLE_VALUE)
10998941Sdes			return 0;
11098941Sdes		cygwin_set_impersonation_token(hToken);
111124211Sdes		return ok;
11298941Sdes	}
113126277Sdes#endif
114137019Sdes#ifdef USE_PAM
115137019Sdes	if (options.use_pam)
116137019Sdes		return (sshpam_auth_passwd(authctxt, password) && ok);
117137019Sdes#endif
118126277Sdes#if defined(USE_SHADOW) && defined(HAS_SHADOW_EXPIRE)
119126277Sdes	if (!expire_checked) {
120126277Sdes		expire_checked = 1;
121147005Sdes		if (auth_shadow_pwexpired(authctxt))
122126277Sdes			authctxt->force_pwchange = 1;
123126277Sdes	}
124126277Sdes#endif
125147005Sdes	result = sys_auth_passwd(authctxt, password);
126147005Sdes	if (authctxt->force_pwchange)
127147005Sdes		disable_forwarding();
128147005Sdes	return (result && ok);
129126277Sdes}
130124211Sdes
131126277Sdes#ifdef BSD_AUTH
132147005Sdesstatic void
133147005Sdeswarn_expiry(Authctxt *authctxt, auth_session_t *as)
134147005Sdes{
135147005Sdes	char buf[256];
136147005Sdes	quad_t pwtimeleft, actimeleft, daysleft, pwwarntime, acwarntime;
137147005Sdes
138147005Sdes	pwwarntime = acwarntime = TWO_WEEKS;
139147005Sdes
140147005Sdes	pwtimeleft = auth_check_change(as);
141147005Sdes	actimeleft = auth_check_expire(as);
142147005Sdes#ifdef HAVE_LOGIN_CAP
143147005Sdes	if (authctxt->valid) {
144147005Sdes		pwwarntime = login_getcaptime(lc, "password-warn", TWO_WEEKS,
145147005Sdes		    TWO_WEEKS);
146147005Sdes		acwarntime = login_getcaptime(lc, "expire-warn", TWO_WEEKS,
147147005Sdes		    TWO_WEEKS);
148147005Sdes	}
149147005Sdes#endif
150147005Sdes	if (pwtimeleft != 0 && pwtimeleft < pwwarntime) {
151147005Sdes		daysleft = pwtimeleft / DAY + 1;
152147005Sdes		snprintf(buf, sizeof(buf),
153147005Sdes		    "Your password will expire in %lld day%s.\n",
154147005Sdes		    daysleft, daysleft == 1 ? "" : "s");
155147005Sdes		buffer_append(&loginmsg, buf, strlen(buf));
156147005Sdes	}
157147005Sdes	if (actimeleft != 0 && actimeleft < acwarntime) {
158147005Sdes		daysleft = actimeleft / DAY + 1;
159147005Sdes		snprintf(buf, sizeof(buf),
160147005Sdes		    "Your account will expire in %lld day%s.\n",
161147005Sdes		    daysleft, daysleft == 1 ? "" : "s");
162147005Sdes		buffer_append(&loginmsg, buf, strlen(buf));
163147005Sdes	}
164147005Sdes}
165147005Sdes
166126277Sdesint
167126277Sdessys_auth_passwd(Authctxt *authctxt, const char *password)
168126277Sdes{
169126277Sdes	struct passwd *pw = authctxt->pw;
170126277Sdes	auth_session_t *as;
171147005Sdes	static int expire_checked = 0;
172124211Sdes
173126277Sdes	as = auth_usercheck(pw->pw_name, authctxt->style, "auth-ssh",
174126277Sdes	    (char *)password);
175149753Sdes	if (as == NULL)
176149753Sdes		return (0);
177126277Sdes	if (auth_getstate(as) & AUTH_PWEXPIRED) {
178126277Sdes		auth_close(as);
179126277Sdes		disable_forwarding();
180126277Sdes		authctxt->force_pwchange = 1;
181126277Sdes		return (1);
182126277Sdes	} else {
183147005Sdes		if (!expire_checked) {
184147005Sdes			expire_checked = 1;
185147005Sdes			warn_expiry(authctxt, as);
186147005Sdes		}
187126277Sdes		return (auth_close(as));
18857429Smarkm	}
189126277Sdes}
190126277Sdes#elif !defined(CUSTOM_SYS_AUTH_PASSWD)
191126277Sdesint
192126277Sdessys_auth_passwd(Authctxt *authctxt, const char *password)
193126277Sdes{
194126277Sdes	struct passwd *pw = authctxt->pw;
195126277Sdes	char *encrypted_password;
196126277Sdes
197124211Sdes	/* Just use the supplied fake password if authctxt is invalid */
198124211Sdes	char *pw_password = authctxt->valid ? shadow_pw(pw) : pw->pw_passwd;
19998941Sdes
20057429Smarkm	/* Check for users with no password. */
201124211Sdes	if (strcmp(pw_password, "") == 0 && strcmp(password, "") == 0)
202126277Sdes		return (1);
20398941Sdes
204126277Sdes	/* Encrypt the candidate password using the proper salt. */
205126277Sdes	encrypted_password = xcrypt(password,
206126277Sdes	    (pw_password[0] && pw_password[1]) ? pw_password : "xx");
20798941Sdes
208126277Sdes	/*
209126277Sdes	 * Authentication is accepted if the encrypted passwords
210126277Sdes	 * are identical.
211126277Sdes	 */
212240075Sdes	return encrypted_password != NULL &&
213240075Sdes	    strcmp(encrypted_password, pw_password) == 0;
21457429Smarkm}
215126277Sdes#endif
216