2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1992-1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 /* this module is for checking a username/password against a system
21 password database. The SMB encrypted password support is elsewhere */
24 #include "system/passwd.h"
28 #define DBGC_CLASS DBGC_AUTH
30 #if !defined(WITH_PAM)
31 static char *ths_salt
;
32 /* This must be writable. */
33 static char *get_this_salt(void)
38 /* We may be setting a modified version of the same
39 * string, so don't free before use. */
41 static const char *set_this_salt(const char *newsalt
)
43 char *orig_salt
= ths_salt
;
44 ths_salt
= SMB_STRDUP(newsalt
);
49 static char *ths_crypted
;
50 static const char *get_this_crypted(void)
58 static const char *set_this_crypted(const char *newcrypted
)
60 char *orig_crypted
= ths_crypted
;
61 ths_crypted
= SMB_STRDUP(newcrypted
);
62 SAFE_FREE(orig_crypted
);
73 /****************************************************************************
74 core of password checking routine
75 ****************************************************************************/
76 static NTSTATUS
password_check(const char *user
, const char *password
, const void *private_data
)
79 const char *rhost
= (const char *)private_data
;
80 return smb_pam_passcheck(user
, rhost
, password
);
89 ret
= (strcmp((char *)crypt16(password
, get_this_salt()), get_this_crypted()) == 0);
93 return NT_STATUS_WRONG_PASSWORD
;
96 #endif /* ULTRIX_AUTH */
101 ret
= (strcmp(bigcrypt(password
, get_this_salt()), get_this_crypted()) == 0);
105 return NT_STATUS_WRONG_PASSWORD
;
107 #endif /* HAVE_BIGCRYPT */
110 DEBUG(1, ("Warning - no crypt available\n"));
111 return NT_STATUS_LOGON_FAILURE
;
112 #else /* HAVE_CRYPT */
113 ret
= (strcmp((char *)crypt(password
, get_this_salt()), get_this_crypted()) == 0);
117 return NT_STATUS_WRONG_PASSWORD
;
119 #endif /* HAVE_CRYPT */
120 #endif /* WITH_PAM */
125 /****************************************************************************
126 CHECK if a username/password is OK
127 the function pointer fn() points to a function to call when a successful
128 match is found and is used to update the encrypted password file
129 return NT_STATUS_OK on correct match, appropriate error otherwise
130 ****************************************************************************/
132 NTSTATUS
pass_check(const struct passwd
*pass
,
135 const char *password
,
142 #ifdef DEBUG_PASSWORD
143 DEBUG(100, ("checking user=[%s] pass=[%s]\n", user
, password
));
147 return NT_STATUS_LOGON_FAILURE
;
149 if ((!*password
) && !lp_null_passwords())
150 return NT_STATUS_LOGON_FAILURE
;
152 #if defined(WITH_PAM)
155 * If we're using PAM we want to short-circuit all the
156 * checks below and dive straight into the PAM code.
159 DEBUG(4, ("pass_check: Checking (PAM) password for user %s\n", user
));
161 #else /* Not using PAM */
163 DEBUG(4, ("pass_check: Checking password for user %s\n", user
));
166 DEBUG(3, ("Couldn't find user %s\n", user
));
167 return NT_STATUS_NO_SUCH_USER
;
171 /* Copy into global for the convenience of looping code */
172 /* Also the place to keep the 'password' no matter what
173 crazy struct it started in... */
174 if (set_this_crypted(pass
->pw_passwd
) == NULL
) {
175 return NT_STATUS_NO_MEMORY
;
177 if (set_this_salt(pass
->pw_passwd
) == NULL
) {
178 return NT_STATUS_NO_MEMORY
;
185 /* many shadow systems require you to be root to get
186 the password, in most cases this should already be
187 the case when this function is called, except
188 perhaps for IPC password changing requests */
190 spass
= getspnam(pass
->pw_name
);
191 if (spass
&& spass
->sp_pwdp
) {
192 if (set_this_crypted(spass
->sp_pwdp
) == NULL
) {
193 return NT_STATUS_NO_MEMORY
;
195 if (set_this_salt(spass
->sp_pwdp
) == NULL
) {
196 return NT_STATUS_NO_MEMORY
;
200 #elif defined(IA_UINFO)
202 /* Need to get password with SVR4.2's ia_ functions
203 instead of get{sp,pw}ent functions. Required by
204 UnixWare 2.x, tested on version
205 2.1. (tangent@cyberport.com) */
207 if (ia_openinfo(pass
->pw_name
, &uinfo
) != -1)
208 ia_get_logpwd(uinfo
, &(pass
->pw_passwd
));
213 #ifdef HAVE_GETPWANAM
215 struct passwd_adjunct
*pwret
;
216 pwret
= getpwanam(s
);
217 if (pwret
&& pwret
->pwa_passwd
) {
218 if (set_this_crypted(pwret
->pwa_passwd
) == NULL
) {
219 return NT_STATUS_NO_MEMORY
;
228 AUTHORIZATION
*ap
= getauthuid(pass
->pw_uid
);
230 if (set_this_crypted(ap
->a_password
) == NULL
) {
232 return NT_STATUS_NO_MEMORY
;
240 if (!get_this_crypted() || !*get_this_crypted()) {
241 if (!lp_null_passwords()) {
242 DEBUG(2, ("Disallowing %s with null password\n",
244 return NT_STATUS_LOGON_FAILURE
;
248 ("Allowing access to %s with null password\n",
254 #endif /* defined(WITH_PAM) */
256 /* try it as it came to us */
257 nt_status
= password_check(user
, password
, (const void *)rhost
);
258 if NT_STATUS_IS_OK(nt_status
) {
260 } else if (!NT_STATUS_EQUAL(nt_status
, NT_STATUS_WRONG_PASSWORD
)) {
261 /* No point continuing if its not the password thats to blame (ie PAM disabled). */
269 /* if the password was given to us with mixed case then we don't
270 * need to proceed as we know it hasn't been case modified by the
272 if (strhasupper(password
) && strhaslower(password
)) {
276 /* make a copy of it */
277 pass2
= talloc_strdup(talloc_tos(), password
);
279 return NT_STATUS_NO_MEMORY
;
282 /* try all lowercase if it's currently all uppercase */
283 if (strhasupper(pass2
)) {
284 if (!strlower_m(pass2
)) {
285 return NT_STATUS_INVALID_PARAMETER
;
287 nt_status
= password_check(user
, pass2
, (const void *)rhost
);
288 if (NT_STATUS_IS_OK(nt_status
)) {
293 return NT_STATUS_WRONG_PASSWORD
;