xref: /netbsd-src/crypto/external/bsd/openssh/dist/auth-passwd.c (revision a5847cc334d9a7029f6352b847e9e8d71a0f9e0c)
1 /*	$NetBSD: auth-passwd.c,v 1.2 2009/06/07 22:38:46 christos Exp $	*/
2 /* $OpenBSD: auth-passwd.c,v 1.43 2007/09/21 08:15:29 djm Exp $ */
3 /*
4  * Author: Tatu Ylonen <ylo@cs.hut.fi>
5  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6  *                    All rights reserved
7  * Password authentication.  This file contains the functions to check whether
8  * the password is valid for the user.
9  *
10  * As far as I am concerned, the code I have written for this software
11  * can be used freely for any purpose.  Any derived versions of this
12  * software must be clearly marked as such, and if the derived work is
13  * incompatible with the protocol description in the RFC file, it must be
14  * called by a name other than "ssh" or "Secure Shell".
15  *
16  * Copyright (c) 1999 Dug Song.  All rights reserved.
17  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions
21  * are met:
22  * 1. Redistributions of source code must retain the above copyright
23  *    notice, this list of conditions and the following disclaimer.
24  * 2. Redistributions in binary form must reproduce the above copyright
25  *    notice, this list of conditions and the following disclaimer in the
26  *    documentation and/or other materials provided with the distribution.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
29  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
33  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
37  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 #include "includes.h"
41 __RCSID("$NetBSD: auth-passwd.c,v 1.2 2009/06/07 22:38:46 christos Exp $");
42 #include <sys/types.h>
43 
44 #include <login_cap.h>
45 #include <pwd.h>
46 #include <stdio.h>
47 #include <string.h>
48 #include <stdarg.h>
49 #include <unistd.h>
50 
51 #include "packet.h"
52 #include "buffer.h"
53 #include "log.h"
54 #include "servconf.h"
55 #include "key.h"
56 #include "hostfile.h"
57 #include "auth.h"
58 #include "auth-options.h"
59 
60 extern Buffer loginmsg;
61 extern ServerOptions options;
62 int sys_auth_passwd(Authctxt *, const char *);
63 
64 #ifdef HAVE_LOGIN_CAP
65 extern login_cap_t *lc;
66 #endif
67 
68 #define DAY		(24L * 60 * 60) /* 1 day in seconds */
69 #define TWO_WEEKS	(2L * 7 * DAY)	/* 2 weeks in seconds */
70 
71 #if defined(BSD_AUTH) || defined(USE_PAM)
72 void
73 disable_forwarding(void)
74 {
75 	no_port_forwarding_flag = 1;
76 	no_agent_forwarding_flag = 1;
77 	no_x11_forwarding_flag = 1;
78 }
79 #endif
80 
81 /*
82  * Tries to authenticate the user using password.  Returns true if
83  * authentication succeeds.
84  */
85 int
86 auth_password(Authctxt *authctxt, const char *password)
87 {
88 	struct passwd * pw = authctxt->pw;
89 	int ok = authctxt->valid;
90 
91 	if (pw->pw_uid == 0 && options.permit_root_login != PERMIT_YES)
92 		ok = 0;
93 	if (*password == '\0' && options.permit_empty_passwd == 0)
94 		return 0;
95 #ifdef USE_PAM
96 	if (options.use_pam)
97 		return (sshpam_auth_passwd(authctxt, password) && ok);
98 #endif
99 #ifdef KRB5
100 	if (options.kerberos_authentication == 1) {
101 		int ret = auth_krb5_password(authctxt, password);
102 		if (ret == 1 || ret == 0)
103 			return ret && ok;
104 		/* Fall back to ordinary passwd authentication. */
105 	}
106 #endif
107 	return (sys_auth_passwd(authctxt, password) && ok);
108 }
109 
110 #ifdef BSD_AUTH
111 static void
112 warn_expiry(Authctxt *authctxt, auth_session_t *as)
113 {
114 	char buf[256];
115 	quad_t pwtimeleft, actimeleft, daysleft, pwwarntime, acwarntime;
116 
117 	pwwarntime = acwarntime = TWO_WEEKS;
118 
119 	pwtimeleft = auth_check_change(as);
120 	actimeleft = auth_check_expire(as);
121 #ifdef HAVE_LOGIN_CAP
122 	if (authctxt->valid) {
123 		pwwarntime = login_getcaptime(lc, "password-warn", TWO_WEEKS,
124 		    TWO_WEEKS);
125 		acwarntime = login_getcaptime(lc, "expire-warn", TWO_WEEKS,
126 		    TWO_WEEKS);
127 	}
128 #endif
129 	if (pwtimeleft != 0 && pwtimeleft < pwwarntime) {
130 		daysleft = pwtimeleft / DAY + 1;
131 		snprintf(buf, sizeof(buf),
132 		    "Your password will expire in %lld day%s.\n",
133 		    daysleft, daysleft == 1 ? "" : "s");
134 		buffer_append(&loginmsg, buf, strlen(buf));
135 	}
136 	if (actimeleft != 0 && actimeleft < acwarntime) {
137 		daysleft = actimeleft / DAY + 1;
138 		snprintf(buf, sizeof(buf),
139 		    "Your account will expire in %lld day%s.\n",
140 		    daysleft, daysleft == 1 ? "" : "s");
141 		buffer_append(&loginmsg, buf, strlen(buf));
142 	}
143 }
144 
145 int
146 sys_auth_passwd(Authctxt *authctxt, const char *password)
147 {
148 	struct passwd *pw = authctxt->pw;
149 	auth_session_t *as;
150 	static int expire_checked = 0;
151 
152 	as = auth_usercheck(pw->pw_name, authctxt->style, "auth-ssh",
153 	    (char *)password);
154 	if (as == NULL)
155 		return (0);
156 	if (auth_getstate(as) & AUTH_PWEXPIRED) {
157 		auth_close(as);
158 		disable_forwarding();
159 		authctxt->force_pwchange = 1;
160 		return (1);
161 	} else {
162 		if (!expire_checked) {
163 			expire_checked = 1;
164 			warn_expiry(authctxt, as);
165 		}
166 		return (auth_close(as));
167 	}
168 }
169 #else
170 int
171 sys_auth_passwd(Authctxt *authctxt, const char *password)
172 {
173 	struct passwd *pw = authctxt->pw;
174 	char *encrypted_password;
175 
176 	/* Check for users with no password. */
177 	if (strcmp(password, "") == 0 && strcmp(pw->pw_passwd, "") == 0)
178 		return (1);
179 
180 	/* Encrypt the candidate password using the proper salt. */
181 	encrypted_password = crypt(password,
182 	    (pw->pw_passwd[0] && pw->pw_passwd[1]) ?
183 	    pw->pw_passwd : "xx");
184 
185 	/*
186 	 * Authentication is accepted if the encrypted passwords
187 	 * are identical.
188 	 */
189 	return (strcmp(encrypted_password, pw->pw_passwd) == 0);
190 }
191 #endif
192