auth: Remove static variable ths_user from password_check() code
[nivanova/samba-autobuild/.git] / source3 / auth / pass_check.c
index 714cc968a789376ad21f753a423fe22516de2eb8..00decc680c04a07e77c302a15b8c3ab57381b1a0 100644 (file)
 #define PASSWORD_LENGTH 8
 #endif
 
-/* these are kept here to keep the string_combinations function simple */
-static char *ths_user;
-
-static const char *get_this_user(void)
-{
-       if (!ths_user) {
-               return "";
-       }
-       return ths_user;
-}
-
-#if defined(WITH_PAM) || defined(OSF1_ENH_SEC)
-static const char *set_this_user(const char *newuser)
-{
-       char *orig_user = ths_user;
-       ths_user = SMB_STRDUP(newuser);
-       SAFE_FREE(orig_user);
-       return ths_user;
-}
-#endif
-
 #if !defined(WITH_PAM)
 static char *ths_salt;
 /* This must be writable. */
@@ -378,7 +357,7 @@ static bool dfs_auth(char *user, char *password)
        }
 
        DEBUG(0, ("DCE login succeeded for principal %s on pid %d\n",
-                 user, sys_getpid()));
+                 user, getpid()));
 
        DEBUG(3, ("DCE principal: %s\n"
                  "          uid: %d\n"
@@ -431,7 +410,7 @@ void dfs_unlogin(void)
                dce_error_inq_text(err, dce_errstr, &err2);
                DEBUG(0,
                      ("DCE purge login context failed for server instance %d: %s\n",
-                      sys_getpid(), dce_errstr));
+                      getpid(), dce_errstr));
        }
 }
 #endif
@@ -493,87 +472,25 @@ static char *osf1_bigcrypt(char *password, char *salt1)
 #endif
 
 
-/****************************************************************************
-apply a function to upper/lower case combinations
-of a string and return true if one of them returns true.
-try all combinations with N uppercase letters.
-offset is the first char to try and change (start with 0)
-it assumes the string starts lowercased
-****************************************************************************/
-static NTSTATUS string_combinations2(char *s, int offset,
-                                    NTSTATUS (*fn)(const char *s,
-                                                   const void *private_data),
-                                    int N, const void *private_data)
-{
-       int len = strlen(s);
-       int i;
-       NTSTATUS nt_status;
-
-#ifdef PASSWORD_LENGTH
-       len = MIN(len, PASSWORD_LENGTH);
-#endif
-
-       if (N <= 0 || offset >= len)
-               return (fn(s, private_data));
-
-       for (i = offset; i < (len - (N - 1)); i++) {
-               char c = s[i];
-               if (!islower_ascii(c))
-                       continue;
-               s[i] = toupper_ascii(c);
-               nt_status = string_combinations2(s, i + 1, fn, N - 1,
-                                                private_data);
-               if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD)) {
-                       return nt_status;
-               }
-               s[i] = c;
-       }
-       return (NT_STATUS_WRONG_PASSWORD);
-}
-
-/****************************************************************************
-apply a function to upper/lower case combinations
-of a string and return true if one of them returns true.
-try all combinations with up to N uppercase letters.
-offset is the first char to try and change (start with 0)
-it assumes the string starts lowercased
-****************************************************************************/
-static NTSTATUS string_combinations(char *s,
-                                   NTSTATUS (*fn)(const char *s,
-                                                  const void *private_data),
-                                   int N, const void *private_data)
-{
-       int n;
-       NTSTATUS nt_status;
-       for (n = 1; n <= N; n++) {
-               nt_status = string_combinations2(s, 0, fn, n, private_data);
-               if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD)) {
-                       return nt_status;
-               }
-       }
-       return NT_STATUS_WRONG_PASSWORD;
-}
-
-
 /****************************************************************************
 core of password checking routine
 ****************************************************************************/
-static NTSTATUS password_check(const char *password, const void *private_data)
+static NTSTATUS password_check(const char *user, const char *password, const void *private_data)
 {
 #ifdef WITH_PAM
        const char *rhost = (const char *)private_data;
-       return smb_pam_passcheck(get_this_user(), rhost, password);
+       return smb_pam_passcheck(user, rhost, password);
 #else
 
        bool ret;
 
 #ifdef WITH_AFS
-       if (afs_auth(get_this_user(), password))
+       if (afs_auth(user, password))
                return NT_STATUS_OK;
 #endif /* WITH_AFS */
 
 #ifdef WITH_DFS
-       if (dfs_auth(get_this_user(), password))
+       if (dfs_auth(user, password))
                return NT_STATUS_OK;
 #endif /* WITH_DFS */
 
@@ -673,7 +590,6 @@ NTSTATUS pass_check(const struct passwd *pass,
                    bool run_cracker)
 {
        char *pass2 = NULL;
-       int level = lp_passwordlevel();
 
        NTSTATUS nt_status;
 
@@ -694,10 +610,6 @@ NTSTATUS pass_check(const struct passwd *pass,
         * checks below and dive straight into the PAM code.
         */
 
-       if (set_this_user(user) == NULL) {
-               return NT_STATUS_NO_MEMORY;
-       }
-
        DEBUG(4, ("pass_check: Checking (PAM) password for user %s\n", user));
 
 #else /* Not using PAM */
@@ -781,9 +693,7 @@ NTSTATUS pass_check(const struct passwd *pass,
                          user));
                mypasswd = getprpwnam(user);
                if (mypasswd) {
-                       if (set_this_user(mypasswd->ufld.fd_name) == NULL) {
-                               return NT_STATUS_NO_MEMORY;
-                       }
+                       user = mypasswd->ufld.fd_name;
                        if (set_this_crypted(mypasswd->ufld.fd_encrypt) == NULL) {
                                return NT_STATUS_NO_MEMORY;
                        }
@@ -826,13 +736,13 @@ NTSTATUS pass_check(const struct passwd *pass,
        if (!get_this_crypted() || !*get_this_crypted()) {
                if (!lp_null_passwords()) {
                        DEBUG(2, ("Disallowing %s with null password\n",
-                                 get_this_user()));
+                                 user));
                        return NT_STATUS_LOGON_FAILURE;
                }
                if (!*password) {
                        DEBUG(3,
                              ("Allowing access to %s with null password\n",
-                              get_this_user()));
+                              user));
                        return NT_STATUS_OK;
                }
        }
@@ -840,7 +750,7 @@ NTSTATUS pass_check(const struct passwd *pass,
 #endif /* defined(WITH_PAM) */
 
        /* try it as it came to us */
-       nt_status = password_check(password, (const void *)rhost);
+       nt_status = password_check(user, password, (const void *)rhost);
         if NT_STATUS_IS_OK(nt_status) {
                return (nt_status);
        } else if (!NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD)) {
@@ -867,26 +777,14 @@ NTSTATUS pass_check(const struct passwd *pass,
 
        /* try all lowercase if it's currently all uppercase */
        if (strhasupper(pass2)) {
-               strlower_m(pass2);
-               nt_status = password_check(pass2, (const void *)rhost);
+               if (!strlower_m(pass2)) {
+                       return NT_STATUS_INVALID_PARAMETER;
+               }
+               nt_status = password_check(user, pass2, (const void *)rhost);
                if (NT_STATUS_IS_OK(nt_status)) {
                        return (nt_status);
                }
        }
 
-       /* give up? */
-       if (level < 1) {
-               return NT_STATUS_WRONG_PASSWORD;
-       }
-
-       /* last chance - all combinations of up to level chars upper! */
-       strlower_m(pass2);
-
-       nt_status = string_combinations(pass2, password_check, level,
-                                       (const void *)rhost);
-        if (NT_STATUS_IS_OK(nt_status)) {
-               return nt_status;
-       }
-
        return NT_STATUS_WRONG_PASSWORD;
 }