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);
71 /****************************************************************************
72 an enhanced crypt for Linux to handle password longer than 8 characters
73 ****************************************************************************/
74 static int linux_bigcrypt(char *password, char *salt1, char *crypted)
76 #define LINUX_PASSWORD_SEG_CHARS 8
80 StrnCpy(salt, salt1, 2);
83 for (i = strlen(password); i > 0; i -= LINUX_PASSWORD_SEG_CHARS) {
84 char *p = crypt(password, salt) + 2;
85 if (strncmp(p, crypted, LINUX_PASSWORD_SEG_CHARS) != 0)
87 password += LINUX_PASSWORD_SEG_CHARS;
97 /****************************************************************************
98 core of password checking routine
99 ****************************************************************************/
100 static NTSTATUS password_check(const char *user, const char *password, const void *private_data)
103 const char *rhost = (const char *)private_data;
104 return smb_pam_passcheck(user, rhost, password);
113 ret = (strcmp((char *)crypt16(password, get_this_salt()), get_this_crypted()) == 0);
117 return NT_STATUS_WRONG_PASSWORD;
120 #endif /* ULTRIX_AUTH */
122 #ifdef LINUX_BIGCRYPT
123 ret = (linux_bigcrypt(password, get_this_salt(), get_this_crypted()));
127 return NT_STATUS_WRONG_PASSWORD;
129 #endif /* LINUX_BIGCRYPT */
131 #if defined(HAVE_BIGCRYPT) && defined(HAVE_CRYPT) && defined(USE_BOTH_CRYPT_CALLS)
134 * Some systems have bigcrypt in the C library but might not
135 * actually use it for the password hashes (HPUX 10.20) is
136 * a noteable example. So we try bigcrypt first, followed
140 if (strcmp(bigcrypt(password, get_this_salt()), get_this_crypted()) == 0)
143 ret = (strcmp((char *)crypt(password, get_this_salt()), get_this_crypted()) == 0);
147 return NT_STATUS_WRONG_PASSWORD;
149 #else /* HAVE_BIGCRYPT && HAVE_CRYPT && USE_BOTH_CRYPT_CALLS */
152 ret = (strcmp(bigcrypt(password, get_this_salt()), get_this_crypted()) == 0);
156 return NT_STATUS_WRONG_PASSWORD;
158 #endif /* HAVE_BIGCRYPT */
161 DEBUG(1, ("Warning - no crypt available\n"));
162 return NT_STATUS_LOGON_FAILURE;
163 #else /* HAVE_CRYPT */
164 ret = (strcmp((char *)crypt(password, get_this_salt()), get_this_crypted()) == 0);
168 return NT_STATUS_WRONG_PASSWORD;
170 #endif /* HAVE_CRYPT */
171 #endif /* HAVE_BIGCRYPT && HAVE_CRYPT && USE_BOTH_CRYPT_CALLS */
172 #endif /* WITH_PAM */
177 /****************************************************************************
178 CHECK if a username/password is OK
179 the function pointer fn() points to a function to call when a successful
180 match is found and is used to update the encrypted password file
181 return NT_STATUS_OK on correct match, appropriate error otherwise
182 ****************************************************************************/
184 NTSTATUS pass_check(const struct passwd *pass,
187 const char *password,
194 #ifdef DEBUG_PASSWORD
195 DEBUG(100, ("checking user=[%s] pass=[%s]\n", user, password));
199 return NT_STATUS_LOGON_FAILURE;
201 if ((!*password) && !lp_null_passwords())
202 return NT_STATUS_LOGON_FAILURE;
204 #if defined(WITH_PAM)
207 * If we're using PAM we want to short-circuit all the
208 * checks below and dive straight into the PAM code.
211 DEBUG(4, ("pass_check: Checking (PAM) password for user %s\n", user));
213 #else /* Not using PAM */
215 DEBUG(4, ("pass_check: Checking password for user %s\n", user));
218 DEBUG(3, ("Couldn't find user %s\n", user));
219 return NT_STATUS_NO_SUCH_USER;
223 /* Copy into global for the convenience of looping code */
224 /* Also the place to keep the 'password' no matter what
225 crazy struct it started in... */
226 if (set_this_crypted(pass->pw_passwd) == NULL) {
227 return NT_STATUS_NO_MEMORY;
229 if (set_this_salt(pass->pw_passwd) == NULL) {
230 return NT_STATUS_NO_MEMORY;
237 /* many shadow systems require you to be root to get
238 the password, in most cases this should already be
239 the case when this function is called, except
240 perhaps for IPC password changing requests */
242 spass = getspnam(pass->pw_name);
243 if (spass && spass->sp_pwdp) {
244 if (set_this_crypted(spass->sp_pwdp) == NULL) {
245 return NT_STATUS_NO_MEMORY;
247 if (set_this_salt(spass->sp_pwdp) == NULL) {
248 return NT_STATUS_NO_MEMORY;
252 #elif defined(IA_UINFO)
254 /* Need to get password with SVR4.2's ia_ functions
255 instead of get{sp,pw}ent functions. Required by
256 UnixWare 2.x, tested on version
257 2.1. (tangent@cyberport.com) */
259 if (ia_openinfo(pass->pw_name, &uinfo) != -1)
260 ia_get_logpwd(uinfo, &(pass->pw_passwd));
265 #ifdef HAVE_GETPWANAM
267 struct passwd_adjunct *pwret;
268 pwret = getpwanam(s);
269 if (pwret && pwret->pwa_passwd) {
270 if (set_this_crypted(pwret->pwa_passwd) == NULL) {
271 return NT_STATUS_NO_MEMORY;
280 AUTHORIZATION *ap = getauthuid(pass->pw_uid);
282 if (set_this_crypted(ap->a_password) == NULL) {
284 return NT_STATUS_NO_MEMORY;
291 #if defined(HAVE_TRUNCATED_SALT)
292 /* crypt on some platforms (HPUX in particular)
293 won't work with more than 2 salt characters. */
295 char *trunc_salt = get_this_salt();
296 if (!trunc_salt || strlen(trunc_salt) < 2) {
297 return NT_STATUS_LOGON_FAILURE;
300 if (set_this_salt(trunc_salt) == NULL) {
301 return NT_STATUS_NO_MEMORY;
306 if (!get_this_crypted() || !*get_this_crypted()) {
307 if (!lp_null_passwords()) {
308 DEBUG(2, ("Disallowing %s with null password\n",
310 return NT_STATUS_LOGON_FAILURE;
314 ("Allowing access to %s with null password\n",
320 #endif /* defined(WITH_PAM) */
322 /* try it as it came to us */
323 nt_status = password_check(user, password, (const void *)rhost);
324 if NT_STATUS_IS_OK(nt_status) {
326 } else if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD)) {
327 /* No point continuing if its not the password thats to blame (ie PAM disabled). */
335 /* if the password was given to us with mixed case then we don't
336 * need to proceed as we know it hasn't been case modified by the
338 if (strhasupper(password) && strhaslower(password)) {
342 /* make a copy of it */
343 pass2 = talloc_strdup(talloc_tos(), password);
345 return NT_STATUS_NO_MEMORY;
348 /* try all lowercase if it's currently all uppercase */
349 if (strhasupper(pass2)) {
350 if (!strlower_m(pass2)) {
351 return NT_STATUS_INVALID_PARAMETER;
353 nt_status = password_check(user, pass2, (const void *)rhost);
354 if (NT_STATUS_IS_OK(nt_status)) {
359 return NT_STATUS_WRONG_PASSWORD;