1323124Sdes/* $OpenBSD: auth-passwd.c,v 1.45 2016/07/21 01:39:35 dtucker 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"
51295367Sdes#include "misc.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
69322341Sdelphij#define MAX_PASSWORD_LEN	1024
70322341Sdelphij
71126277Sdesvoid
72126277Sdesdisable_forwarding(void)
73126277Sdes{
74126277Sdes	no_port_forwarding_flag = 1;
75126277Sdes	no_agent_forwarding_flag = 1;
76126277Sdes	no_x11_forwarding_flag = 1;
77126277Sdes}
78126277Sdes
7957429Smarkm/*
8057429Smarkm * Tries to authenticate the user using password.  Returns true if
8157429Smarkm * authentication succeeds.
8257429Smarkm */
8365674Skrisint
8476262Sgreenauth_password(Authctxt *authctxt, const char *password)
8557429Smarkm{
8676262Sgreen	struct passwd * pw = authctxt->pw;
87147005Sdes	int result, ok = authctxt->valid;
88137019Sdes#if defined(USE_SHADOW) && defined(HAS_SHADOW_EXPIRE)
89126277Sdes	static int expire_checked = 0;
90137019Sdes#endif
9157429Smarkm
92322341Sdelphij	if (strlen(password) > MAX_PASSWORD_LEN)
93322341Sdelphij		return 0;
94322341Sdelphij
9598941Sdes#ifndef HAVE_CYGWIN
96126277Sdes	if (pw->pw_uid == 0 && options.permit_root_login != PERMIT_YES)
97124211Sdes		ok = 0;
9898941Sdes#endif
9957429Smarkm	if (*password == '\0' && options.permit_empty_passwd == 0)
10057429Smarkm		return 0;
101113911Sdes
102126277Sdes#ifdef KRB5
10373400Sassar	if (options.kerberos_authentication == 1) {
10492559Sdes		int ret = auth_krb5_password(authctxt, password);
10592559Sdes		if (ret == 1 || ret == 0)
106124211Sdes			return ret && ok;
10757565Smarkm		/* Fall back to ordinary passwd authentication. */
10857565Smarkm	}
109126277Sdes#endif
110126277Sdes#ifdef HAVE_CYGWIN
111197679Sdes	{
11298941Sdes		HANDLE hToken = cygwin_logon_user(pw, password);
11398941Sdes
11498941Sdes		if (hToken == INVALID_HANDLE_VALUE)
11598941Sdes			return 0;
11698941Sdes		cygwin_set_impersonation_token(hToken);
117124211Sdes		return ok;
11898941Sdes	}
119126277Sdes#endif
120137019Sdes#ifdef USE_PAM
121137019Sdes	if (options.use_pam)
122137019Sdes		return (sshpam_auth_passwd(authctxt, password) && ok);
123137019Sdes#endif
124126277Sdes#if defined(USE_SHADOW) && defined(HAS_SHADOW_EXPIRE)
125126277Sdes	if (!expire_checked) {
126126277Sdes		expire_checked = 1;
127147005Sdes		if (auth_shadow_pwexpired(authctxt))
128126277Sdes			authctxt->force_pwchange = 1;
129126277Sdes	}
130126277Sdes#endif
131147005Sdes	result = sys_auth_passwd(authctxt, password);
132147005Sdes	if (authctxt->force_pwchange)
133147005Sdes		disable_forwarding();
134147005Sdes	return (result && ok);
135126277Sdes}
136124211Sdes
137126277Sdes#ifdef BSD_AUTH
138147005Sdesstatic void
139147005Sdeswarn_expiry(Authctxt *authctxt, auth_session_t *as)
140147005Sdes{
141147005Sdes	char buf[256];
142147005Sdes	quad_t pwtimeleft, actimeleft, daysleft, pwwarntime, acwarntime;
143147005Sdes
144147005Sdes	pwwarntime = acwarntime = TWO_WEEKS;
145147005Sdes
146147005Sdes	pwtimeleft = auth_check_change(as);
147147005Sdes	actimeleft = auth_check_expire(as);
148147005Sdes#ifdef HAVE_LOGIN_CAP
149147005Sdes	if (authctxt->valid) {
150147005Sdes		pwwarntime = login_getcaptime(lc, "password-warn", TWO_WEEKS,
151147005Sdes		    TWO_WEEKS);
152147005Sdes		acwarntime = login_getcaptime(lc, "expire-warn", TWO_WEEKS,
153147005Sdes		    TWO_WEEKS);
154147005Sdes	}
155147005Sdes#endif
156147005Sdes	if (pwtimeleft != 0 && pwtimeleft < pwwarntime) {
157147005Sdes		daysleft = pwtimeleft / DAY + 1;
158147005Sdes		snprintf(buf, sizeof(buf),
159147005Sdes		    "Your password will expire in %lld day%s.\n",
160147005Sdes		    daysleft, daysleft == 1 ? "" : "s");
161147005Sdes		buffer_append(&loginmsg, buf, strlen(buf));
162147005Sdes	}
163147005Sdes	if (actimeleft != 0 && actimeleft < acwarntime) {
164147005Sdes		daysleft = actimeleft / DAY + 1;
165147005Sdes		snprintf(buf, sizeof(buf),
166147005Sdes		    "Your account will expire in %lld day%s.\n",
167147005Sdes		    daysleft, daysleft == 1 ? "" : "s");
168147005Sdes		buffer_append(&loginmsg, buf, strlen(buf));
169147005Sdes	}
170147005Sdes}
171147005Sdes
172126277Sdesint
173126277Sdessys_auth_passwd(Authctxt *authctxt, const char *password)
174126277Sdes{
175126277Sdes	struct passwd *pw = authctxt->pw;
176126277Sdes	auth_session_t *as;
177147005Sdes	static int expire_checked = 0;
178124211Sdes
179126277Sdes	as = auth_usercheck(pw->pw_name, authctxt->style, "auth-ssh",
180126277Sdes	    (char *)password);
181149753Sdes	if (as == NULL)
182149753Sdes		return (0);
183126277Sdes	if (auth_getstate(as) & AUTH_PWEXPIRED) {
184126277Sdes		auth_close(as);
185126277Sdes		disable_forwarding();
186126277Sdes		authctxt->force_pwchange = 1;
187126277Sdes		return (1);
188126277Sdes	} else {
189147005Sdes		if (!expire_checked) {
190147005Sdes			expire_checked = 1;
191147005Sdes			warn_expiry(authctxt, as);
192147005Sdes		}
193126277Sdes		return (auth_close(as));
19457429Smarkm	}
195126277Sdes}
196126277Sdes#elif !defined(CUSTOM_SYS_AUTH_PASSWD)
197126277Sdesint
198126277Sdessys_auth_passwd(Authctxt *authctxt, const char *password)
199126277Sdes{
200126277Sdes	struct passwd *pw = authctxt->pw;
201323124Sdes	char *encrypted_password, *salt = NULL;
202126277Sdes
203124211Sdes	/* Just use the supplied fake password if authctxt is invalid */
204124211Sdes	char *pw_password = authctxt->valid ? shadow_pw(pw) : pw->pw_passwd;
20598941Sdes
20657429Smarkm	/* Check for users with no password. */
207124211Sdes	if (strcmp(pw_password, "") == 0 && strcmp(password, "") == 0)
208126277Sdes		return (1);
20998941Sdes
210323124Sdes	/*
211323124Sdes	 * Encrypt the candidate password using the proper salt, or pass a
212323124Sdes	 * NULL and let xcrypt pick one.
213323124Sdes	 */
214323124Sdes	if (authctxt->valid && pw_password[0] && pw_password[1])
215323124Sdes		salt = pw_password;
216323124Sdes	encrypted_password = xcrypt(password, salt);
21798941Sdes
218126277Sdes	/*
219126277Sdes	 * Authentication is accepted if the encrypted passwords
220126277Sdes	 * are identical.
221126277Sdes	 */
222240075Sdes	return encrypted_password != NULL &&
223240075Sdes	    strcmp(encrypted_password, pw_password) == 0;
22457429Smarkm}
225126277Sdes#endif
226