2 /* $OpenBSD: auth-passwd.c,v 1.43 2007/09/21 08:15:29 djm Exp $ */
4 * Author: Tatu Ylonen <ylo@cs.hut.fi>
5 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
7 * Password authentication. This file contains the functions to check whether
8 * the password is valid for the user.
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".
16 * Copyright (c) 1999 Dug Song. All rights reserved.
17 * Copyright (c) 2000 Markus Friedl. All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
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.
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.
41 __RCSID("$NetBSD: auth-passwd.c,v 1.17 2008/04/06 23:38:19 christos Exp $");
42 #include <sys/types.h>
44 #include <login_cap.h>
58 #include "auth-options.h"
60 extern Buffer loginmsg
;
61 extern ServerOptions options
;
62 int sys_auth_passwd(Authctxt
*, const char *);
65 extern login_cap_t
*lc
;
68 #define DAY (24L * 60 * 60) /* 1 day in seconds */
69 #define TWO_WEEKS (2L * 7 * DAY) /* 2 weeks in seconds */
71 #if defined(BSD_AUTH) || defined(USE_PAM)
73 disable_forwarding(void)
75 no_port_forwarding_flag
= 1;
76 no_agent_forwarding_flag
= 1;
77 no_x11_forwarding_flag
= 1;
82 * Tries to authenticate the user using password. Returns true if
83 * authentication succeeds.
86 auth_password(Authctxt
*authctxt
, const char *password
)
88 struct passwd
* pw
= authctxt
->pw
;
89 int ok
= authctxt
->valid
;
91 if (pw
->pw_uid
== 0 && options
.permit_root_login
!= PERMIT_YES
)
93 if (*password
== '\0' && options
.permit_empty_passwd
== 0)
97 return (sshpam_auth_passwd(authctxt
, password
) && ok
);
100 if (options
.kerberos_authentication
== 1) {
101 int ret
= auth_krb5_password(authctxt
, password
);
102 if (ret
== 1 || ret
== 0)
104 /* Fall back to ordinary passwd authentication. */
107 return (sys_auth_passwd(authctxt
, password
) && ok
);
112 warn_expiry(Authctxt
*authctxt
, auth_session_t
*as
)
115 quad_t pwtimeleft
, actimeleft
, daysleft
, pwwarntime
, acwarntime
;
117 pwwarntime
= acwarntime
= TWO_WEEKS
;
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
,
125 acwarntime
= login_getcaptime(lc
, "expire-warn", TWO_WEEKS
,
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
));
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
));
146 sys_auth_passwd(Authctxt
*authctxt
, const char *password
)
148 struct passwd
*pw
= authctxt
->pw
;
150 static int expire_checked
= 0;
152 as
= auth_usercheck(pw
->pw_name
, authctxt
->style
, "auth-ssh",
156 if (auth_getstate(as
) & AUTH_PWEXPIRED
) {
158 disable_forwarding();
159 authctxt
->force_pwchange
= 1;
162 if (!expire_checked
) {
164 warn_expiry(authctxt
, as
);
166 return (auth_close(as
));
171 sys_auth_passwd(Authctxt
*authctxt
, const char *password
)
173 struct passwd
*pw
= authctxt
->pw
;
174 char *encrypted_password
;
176 /* Check for users with no password. */
177 if (strcmp(password
, "") == 0 && strcmp(pw
->pw_passwd
, "") == 0)
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");
186 * Authentication is accepted if the encrypted passwords
189 return (strcmp(encrypted_password
, pw
->pw_passwd
) == 0);