[CREDUI] Sync with Wine Staging 3.17. CORE-15127
[reactos.git] / dll / win32 / credui / credui_main.c
index 96f4d64..bf9f7d1 100644 (file)
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  */
 
-#define WIN32_NO_STATUS
-
 #include <stdarg.h>
 
-#include <windef.h>
-#include <winbase.h>
-//#include "winnt.h"
-#include <winuser.h>
-#include <wincred.h>
-#include <commctrl.h>
+#include "windef.h"
+#include "winbase.h"
+#include "winnt.h"
+#include "winuser.h"
+#include "wincred.h"
+#include "rpc.h"
+#include "sspi.h"
+#include "commctrl.h"
 
 #include "credui_resources.h"
 
-#include <wine/debug.h>
-#include <wine/unicode.h>
-#include <wine/list.h>
+#include "wine/debug.h"
+#include "wine/unicode.h"
+#include "wine/list.h"
 
 WINE_DEFAULT_DEBUG_CHANNEL(credui);
 
@@ -82,6 +82,7 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
         break;
 
     case DLL_PROCESS_DETACH:
+        if (lpvReserved) break;
         LIST_FOR_EACH_ENTRY_SAFE(entry, cursor2, &pending_credentials_list, struct pending_credentials, entry)
         {
             list_remove(&entry->entry);
@@ -205,7 +206,7 @@ static void CredDialogCreateBalloonTip(HWND hwndDlg, struct cred_dialog_params *
     SetWindowPos(params->hwndBalloonTip, HWND_TOPMOST, 0, 0, 0, 0,
                  SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
 
-    if (!LoadStringW(hinstCredUI, IDS_INCORRECTPASSWORD, wszText, sizeof(wszText)/sizeof(wszText[0])))
+    if (!LoadStringW(hinstCredUI, IDS_INCORRECTPASSWORD, wszText, ARRAY_SIZE(wszText)))
     {
         ERR("failed to load IDS_INCORRECTPASSWORD\n");
         return;
@@ -215,14 +216,14 @@ static void CredDialogCreateBalloonTip(HWND hwndDlg, struct cred_dialog_params *
     toolinfo.uFlags = TTF_TRACK;
     toolinfo.hwnd = hwndDlg;
     toolinfo.uId = TOOLID_INCORRECTPASSWORD;
-    memset(&toolinfo.rect, 0, sizeof(toolinfo.rect));
+    SetRectEmpty(&toolinfo.rect);
     toolinfo.hinst = NULL;
     toolinfo.lpszText = wszText;
     toolinfo.lParam = 0;
     toolinfo.lpReserved = NULL;
     SendMessageW(params->hwndBalloonTip, TTM_ADDTOOLW, 0, (LPARAM)&toolinfo);
 
-    if (!LoadStringW(hinstCredUI, IDS_CAPSLOCKON, wszText, sizeof(wszText)/sizeof(wszText[0])))
+    if (!LoadStringW(hinstCredUI, IDS_CAPSLOCKON, wszText, ARRAY_SIZE(wszText)))
     {
         ERR("failed to load IDS_CAPSLOCKON\n");
         return;
@@ -250,7 +251,7 @@ static void CredDialogShowIncorrectPasswordBalloon(HWND hwndDlg, struct cred_dia
     if (params->fBalloonTipActive)
         return;
 
-    if (!LoadStringW(hinstCredUI, IDS_INCORRECTPASSWORDTITLE, wszTitle, sizeof(wszTitle)/sizeof(wszTitle[0])))
+    if (!LoadStringW(hinstCredUI, IDS_INCORRECTPASSWORDTITLE, wszTitle, ARRAY_SIZE(wszTitle)))
     {
         ERR("failed to load IDS_INCORRECTPASSWORDTITLE\n");
         return;
@@ -288,7 +289,7 @@ static void CredDialogShowCapsLockBalloon(HWND hwndDlg, struct cred_dialog_param
     if (params->fBalloonTipActive)
         return;
 
-    if (!LoadStringW(hinstCredUI, IDS_CAPSLOCKONTITLE, wszTitle, sizeof(wszTitle)/sizeof(wszTitle[0])))
+    if (!LoadStringW(hinstCredUI, IDS_CAPSLOCKONTITLE, wszTitle, ARRAY_SIZE(wszTitle)))
     {
         ERR("failed to load IDS_IDSCAPSLOCKONTITLE\n");
         return;
@@ -339,7 +340,7 @@ static void CredDialogHideBalloonTip(HWND hwndDlg, struct cred_dialog_params *pa
 
 static inline BOOL CredDialogCapsLockOn(void)
 {
-    return GetKeyState(VK_CAPITAL) & 0x1 ? TRUE : FALSE;
+    return (GetKeyState(VK_CAPITAL) & 0x1) != 0;
 }
 
 static LRESULT CALLBACK CredDialogPasswordSubclassProc(HWND hwnd, UINT uMsg,
@@ -382,8 +383,8 @@ static BOOL CredDialogInit(HWND hwndDlg, struct cred_dialog_params *params)
     {
         WCHAR format[256];
         WCHAR message[256];
-        LoadStringW(hinstCredUI, IDS_MESSAGEFORMAT, format, sizeof(format)/sizeof(format[0]));
-        snprintfW(message, sizeof(message)/sizeof(message[0]), format, params->pszTargetName);
+        LoadStringW(hinstCredUI, IDS_MESSAGEFORMAT, format, ARRAY_SIZE(format));
+        snprintfW(message, ARRAY_SIZE(message), format, params->pszTargetName);
         SetDlgItemTextW(hwndDlg, IDC_MESSAGE, message);
     }
     SetWindowTextW(hwndUsername, params->pszUsername);
@@ -407,12 +408,14 @@ static BOOL CredDialogInit(HWND hwndDlg, struct cred_dialog_params *params)
     {
         WCHAR format[256];
         WCHAR title[256];
-        LoadStringW(hinstCredUI, IDS_TITLEFORMAT, format, sizeof(format)/sizeof(format[0]));
-        snprintfW(title, sizeof(title)/sizeof(title[0]), format, params->pszTargetName);
+        LoadStringW(hinstCredUI, IDS_TITLEFORMAT, format, ARRAY_SIZE(format));
+        snprintfW(title, ARRAY_SIZE(title), format, params->pszTargetName);
         SetWindowTextW(hwndDlg, title);
     }
 
-    if (params->dwFlags & (CREDUI_FLAGS_DO_NOT_PERSIST|CREDUI_FLAGS_PERSIST))
+    if (params->dwFlags & CREDUI_FLAGS_PERSIST ||
+        (params->dwFlags & CREDUI_FLAGS_DO_NOT_PERSIST &&
+         !(params->dwFlags & CREDUI_FLAGS_SHOW_SAVE_CHECK_BOX)))
         ShowWindow(GetDlgItem(hwndDlg, IDC_SAVE), SW_HIDE);
     else if (params->fSave)
         CheckDlgButton(hwndDlg, IDC_SAVE, BST_CHECKED);
@@ -548,6 +551,39 @@ static INT_PTR CALLBACK CredDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,
     }
 }
 
+static BOOL find_existing_credential(const WCHAR *target, WCHAR *username, ULONG len_username,
+                                     WCHAR *password, ULONG len_password)
+{
+    DWORD count, i;
+    CREDENTIALW **credentials;
+
+    if (!CredEnumerateW(target, 0, &count, &credentials)) return FALSE;
+    for (i = 0; i < count; i++)
+    {
+        if (credentials[i]->Type != CRED_TYPE_DOMAIN_PASSWORD &&
+            credentials[i]->Type != CRED_TYPE_GENERIC)
+        {
+            FIXME("no support for type %u credentials\n", credentials[i]->Type);
+            continue;
+        }
+        if ((!*username || !strcmpW(username, credentials[i]->UserName)) &&
+            strlenW(credentials[i]->UserName) < len_username &&
+            credentials[i]->CredentialBlobSize / sizeof(WCHAR) < len_password)
+        {
+            TRACE("found existing credential for %s\n", debugstr_w(credentials[i]->UserName));
+
+            strcpyW(username, credentials[i]->UserName);
+            memcpy(password, credentials[i]->CredentialBlob, credentials[i]->CredentialBlobSize);
+            password[credentials[i]->CredentialBlobSize / sizeof(WCHAR)] = 0;
+
+            CredFree(credentials);
+            return TRUE;
+        }
+    }
+    CredFree(credentials);
+    return FALSE;
+}
+
 /******************************************************************************
  *           CredUIPromptForCredentialsW [CREDUI.@]
  */
@@ -578,6 +614,11 @@ DWORD WINAPI CredUIPromptForCredentialsW(PCREDUI_INFOW pUIInfo,
     if ((dwFlags & CREDUI_FLAGS_SHOW_SAVE_CHECK_BOX) && !pfSave)
         return ERROR_INVALID_PARAMETER;
 
+    if (!(dwFlags & CREDUI_FLAGS_ALWAYS_SHOW_UI) &&
+        !(dwFlags & CREDUI_FLAGS_INCORRECT_PASSWORD) &&
+        find_existing_credential(pszTargetName, pszUsername, ulUsernameMaxChars, pszPassword, ulPasswordMaxChars))
+        return ERROR_SUCCESS;
+
     params.pszTargetName = pszTargetName;
     if (pUIInfo)
     {
@@ -651,13 +692,13 @@ DWORD WINAPI CredUIPromptForCredentialsW(PCREDUI_INFOW pUIInfo,
             len = strlenW(params.pszPassword);
             entry->pszPassword = HeapAlloc(GetProcessHeap(), 0, (len + 1)*sizeof(WCHAR));
             memcpy(entry->pszPassword, params.pszPassword, (len + 1)*sizeof(WCHAR));
-            entry->generic = dwFlags & CREDUI_FLAGS_GENERIC_CREDENTIALS ? TRUE : FALSE;
+            entry->generic = (dwFlags & CREDUI_FLAGS_GENERIC_CREDENTIALS) != 0;
 
             LeaveCriticalSection(&csPendingCredentials);
         }
-        else
+        else if (!(dwFlags & CREDUI_FLAGS_DO_NOT_PERSIST))
             result = save_credentials(pszTargetName, pszUsername, pszPassword,
-                                      dwFlags & CREDUI_FLAGS_GENERIC_CREDENTIALS ? TRUE : FALSE);
+                                      (dwFlags & CREDUI_FLAGS_GENERIC_CREDENTIALS) != 0);
     }
 
     return result;
@@ -802,3 +843,153 @@ DWORD WINAPI CredUIReadSSOCredW(PCWSTR pszRealm, PWSTR *ppszUsername)
         *ppszUsername = NULL;
     return ERROR_NOT_FOUND;
 }
+
+/******************************************************************************
+ * CredUIInitControls [CREDUI.@]
+ */
+BOOL WINAPI CredUIInitControls(void)
+{
+    FIXME("() stub\n");
+    return TRUE;
+}
+
+/******************************************************************************
+ * SspiPromptForCredentialsW [CREDUI.@]
+ */
+ULONG SEC_ENTRY SspiPromptForCredentialsW( PCWSTR target, void *info,
+                                           DWORD error, PCWSTR package,
+                                           PSEC_WINNT_AUTH_IDENTITY_OPAQUE input_id,
+                                           PSEC_WINNT_AUTH_IDENTITY_OPAQUE *output_id,
+                                           BOOL *save, DWORD sspi_flags )
+{
+    static const WCHAR basicW[] = {'B','a','s','i','c',0};
+    static const WCHAR ntlmW[] = {'N','T','L','M',0};
+    static const WCHAR negotiateW[] = {'N','e','g','o','t','i','a','t','e',0};
+    WCHAR username[CREDUI_MAX_USERNAME_LENGTH + 1] = {0};
+    WCHAR password[CREDUI_MAX_PASSWORD_LENGTH + 1] = {0};
+    DWORD len_username = ARRAY_SIZE(username);
+    DWORD len_password = ARRAY_SIZE(password);
+    DWORD ret, flags;
+    CREDUI_INFOW *cred_info = info;
+    SEC_WINNT_AUTH_IDENTITY_W *id = input_id;
+
+    FIXME( "(%s, %p, %u, %s, %p, %p, %p, %x) stub\n", debugstr_w(target), info,
+           error, debugstr_w(package), input_id, output_id, save, sspi_flags );
+
+    if (!target) return ERROR_INVALID_PARAMETER;
+    if (!package || (strcmpiW( package, basicW ) && strcmpiW( package, ntlmW ) &&
+                     strcmpiW( package, negotiateW )))
+    {
+        FIXME( "package %s not supported\n", debugstr_w(package) );
+        return ERROR_NO_SUCH_PACKAGE;
+    }
+
+    flags = CREDUI_FLAGS_ALWAYS_SHOW_UI | CREDUI_FLAGS_GENERIC_CREDENTIALS;
+
+    if (sspi_flags & SSPIPFC_CREDPROV_DO_NOT_SAVE)
+        flags |= CREDUI_FLAGS_DO_NOT_PERSIST;
+
+    if (!(sspi_flags & SSPIPFC_NO_CHECKBOX))
+        flags |= CREDUI_FLAGS_SHOW_SAVE_CHECK_BOX;
+
+    if (!id) find_existing_credential( target, username, len_username, password, len_password );
+    else
+    {
+        if (id->User && id->UserLength > 0 && id->UserLength <= CREDUI_MAX_USERNAME_LENGTH)
+        {
+            memcpy( username, id->User, id->UserLength * sizeof(WCHAR) );
+            username[id->UserLength] = 0;
+        }
+        if (id->Password && id->PasswordLength > 0 && id->PasswordLength <= CREDUI_MAX_PASSWORD_LENGTH)
+        {
+            memcpy( password, id->Password, id->PasswordLength * sizeof(WCHAR) );
+            password[id->PasswordLength] = 0;
+        }
+    }
+
+    if (!(ret = CredUIPromptForCredentialsW( cred_info, target, NULL, error, username,
+                                             len_username, password, len_password, save, flags )))
+    {
+        DWORD size = sizeof(*id), len_domain = 0;
+        WCHAR *ptr, *user = username, *domain = NULL;
+
+        if ((ptr = strchrW( username, '\\' )))
+        {
+            user = ptr + 1;
+            len_username = strlenW( user );
+            if (!strcmpiW( package, ntlmW ) || !strcmpiW( package, negotiateW ))
+            {
+                domain = username;
+                len_domain = ptr - username;
+            }
+            *ptr = 0;
+        }
+        else len_username = strlenW( username );
+        len_password = strlenW( password );
+
+        size += (len_username + 1) * sizeof(WCHAR);
+        size += (len_domain + 1) * sizeof(WCHAR);
+        size += (len_password + 1) * sizeof(WCHAR);
+        if (!(id = HeapAlloc( GetProcessHeap(), 0, size ))) return ERROR_OUTOFMEMORY;
+        ptr = (WCHAR *)(id + 1);
+
+        memcpy( ptr, user, (len_username + 1) * sizeof(WCHAR) );
+        id->User           = ptr;
+        id->UserLength     = len_username;
+        ptr += len_username + 1;
+        if (len_domain)
+        {
+            memcpy( ptr, domain, (len_domain + 1) * sizeof(WCHAR) );
+            id->Domain         = ptr;
+            id->DomainLength   = len_domain;
+            ptr += len_domain + 1;
+        }
+        else
+        {
+            id->Domain         = NULL;
+            id->DomainLength   = 0;
+        }
+        memcpy( ptr, password, (len_password + 1) * sizeof(WCHAR) );
+        id->Password       = ptr;
+        id->PasswordLength = len_password;
+        id->Flags          = 0;
+
+        *output_id = id;
+    }
+
+    return ret;
+}
+
+/******************************************************************************
+ * CredUIPromptForWindowsCredentialsW [CREDUI.@]
+ */
+DWORD WINAPI CredUIPromptForWindowsCredentialsW( CREDUI_INFOW *info, DWORD error, ULONG *package,
+                                                 const void *in_buf, ULONG in_buf_size, void **out_buf,
+                                                 ULONG *out_buf_size, BOOL *save, DWORD flags )
+{
+    FIXME( "(%p, %u, %p, %p, %u, %p, %p, %p, %08x) stub\n", info, error, package, in_buf, in_buf_size,
+           out_buf, out_buf_size, save, flags );
+    return ERROR_CALL_NOT_IMPLEMENTED;
+}
+
+/******************************************************************************
+ * CredPackAuthenticationBufferW [CREDUI.@]
+ */
+BOOL  WINAPI CredPackAuthenticationBufferW( DWORD flags, WCHAR *username, WCHAR *password, BYTE *buf,
+                                            DWORD *size )
+{
+    FIXME( "(%08x, %s, %p, %p, %p) stub\n", flags, debugstr_w(username), password, buf, size );
+    return ERROR_CALL_NOT_IMPLEMENTED;
+}
+
+/******************************************************************************
+ * CredUnPackAuthenticationBufferW [CREDUI.@]
+ */
+BOOL  WINAPI CredUnPackAuthenticationBufferW( DWORD flags, void *buf, DWORD size, WCHAR *username,
+                                              DWORD *len_username, WCHAR *domain, DWORD *len_domain,
+                                              WCHAR *password, DWORD *len_password )
+{
+    FIXME( "(%08x, %p, %u, %p, %p, %p, %p, %p, %p) stub\n", flags, buf, size, username, len_username,
+           domain, len_domain, password, len_password );
+    return ERROR_CALL_NOT_IMPLEMENTED;
+}