OLD | NEW |
| (Empty) |
1 // Copyright 2011 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 | |
16 #include <wincred.h> | |
17 #include "omaha/base/debug.h" | |
18 #include "omaha/base/logging.h" | |
19 #include "omaha/base/scoped_impersonation.h" | |
20 #include "omaha/base/system_info.h" | |
21 #include "omaha/common/goopdate_utils.h" | |
22 #include "omaha/client/resource.h" | |
23 #include "omaha/goopdate/cred_dialog.h" | |
24 | |
25 namespace omaha { | |
26 | |
27 STDMETHODIMP CredentialDialogBase::QueryUserForCredentials( | |
28 ULONG_PTR owner_hwnd, | |
29 BSTR server, | |
30 BSTR caption, | |
31 BSTR* username, | |
32 BSTR* password) { | |
33 ASSERT1(server && wcslen(server)); | |
34 ASSERT1(caption); | |
35 ASSERT1(username); | |
36 ASSERT1(password); | |
37 | |
38 CORE_LOG(L3, (_T("[LaunchCredentialDialog][COM server launching dialog]"))); | |
39 | |
40 if (!::IsWindow(reinterpret_cast<HWND>(owner_hwnd))) { | |
41 return E_INVALIDARG; | |
42 } | |
43 | |
44 if (!server || !wcslen(server) || !caption) { | |
45 return E_INVALIDARG; | |
46 } | |
47 | |
48 if (!username || !password) { | |
49 return E_POINTER; | |
50 } | |
51 | |
52 CString message; | |
53 message.LoadString(IDS_PROXY_PROMPT_MESSAGE); | |
54 message.FormatMessage(message, server); | |
55 | |
56 CString capt_cstr(caption); | |
57 if (capt_cstr.IsEmpty()) { | |
58 capt_cstr.LoadString(IDS_PRODUCT_DISPLAY_NAME); | |
59 } | |
60 | |
61 CString user; | |
62 CString pass; | |
63 DWORD result = DisplayDialog(reinterpret_cast<HWND>(owner_hwnd), | |
64 server, | |
65 message, | |
66 capt_cstr, | |
67 &user, | |
68 &pass); | |
69 | |
70 if (result == NO_ERROR) { | |
71 user.SetSysString(username); | |
72 ::SecureZeroMemory(user.GetBuffer(), user.GetAllocLength() * sizeof(TCHAR)); | |
73 pass.SetSysString(password); | |
74 ::SecureZeroMemory(pass.GetBuffer(), pass.GetAllocLength() * sizeof(TCHAR)); | |
75 return S_OK; | |
76 } | |
77 | |
78 return HRESULT_FROM_WIN32(result); | |
79 } | |
80 | |
81 DWORD CredentialDialogBase::DisplayDialog( | |
82 HWND hwnd, | |
83 LPCTSTR server, | |
84 LPCTSTR message, | |
85 LPCTSTR caption, | |
86 CString* username_out, | |
87 CString* password_out) { | |
88 scoped_library credui_lib(::LoadLibrary(L"credui.dll")); | |
89 ASSERT1(credui_lib); | |
90 if (!credui_lib) { | |
91 CORE_LOG(L3, (_T("[CredUIPromptForCredentialsW not available]"))); | |
92 return ERROR_NOT_READY; | |
93 } | |
94 | |
95 typedef BOOL (__stdcall *CredUIPromptForCredentialsW_type)( | |
96 PCREDUI_INFO pUiInfo, | |
97 PCTSTR pszTargetName, | |
98 PCtxtHandle Reserved, | |
99 DWORD dwAuthError, | |
100 PCTSTR pszUserName, | |
101 ULONG ulUserNameMaxChars, | |
102 PCTSTR pszPassword, | |
103 ULONG ulPasswordMaxChars, | |
104 PBOOL pfSave, | |
105 DWORD dwFlags); | |
106 CredUIPromptForCredentialsW_type CredUIPromptForCredentialsW_fn = | |
107 reinterpret_cast<CredUIPromptForCredentialsW_type>( | |
108 GetProcAddress(get(credui_lib), "CredUIPromptForCredentialsW")); | |
109 ASSERT1(CredUIPromptForCredentialsW_fn || SystemInfo::IsRunningOnW2K()); | |
110 if (!CredUIPromptForCredentialsW_fn) { | |
111 CORE_LOG(L3, (_T("[CredUIPromptForCredentialsW not available]"))); | |
112 return ERROR_NOT_READY; | |
113 } | |
114 | |
115 wchar_t temp_username[CREDUI_MAX_USERNAME_LENGTH + 1] = {}; | |
116 wchar_t temp_password[CREDUI_MAX_PASSWORD_LENGTH + 1] = {}; | |
117 BOOL check; | |
118 CREDUI_INFO info = {0}; | |
119 info.cbSize = sizeof(info); | |
120 info.hwndParent = hwnd; | |
121 info.pszMessageText = message; | |
122 info.pszCaptionText = caption; | |
123 | |
124 DWORD result; | |
125 do { | |
126 temp_username[0] = L'\0'; | |
127 temp_password[0] = L'\0'; | |
128 result = CredUIPromptForCredentialsW_fn( | |
129 &info, | |
130 server, | |
131 NULL, | |
132 0, | |
133 temp_username, | |
134 CREDUI_MAX_USERNAME_LENGTH, | |
135 temp_password, | |
136 CREDUI_MAX_PASSWORD_LENGTH, | |
137 &check, | |
138 CREDUI_FLAGS_ALWAYS_SHOW_UI | CREDUI_FLAGS_GENERIC_CREDENTIALS | | |
139 CREDUI_FLAGS_DO_NOT_PERSIST); | |
140 CORE_LOG(L3, (_T("[CredUIPromptForCredentialsW returned %d]"), result)); | |
141 } while (result == NO_ERROR && (!temp_username[0] || !temp_password[0])); | |
142 | |
143 if (result == NO_ERROR) { | |
144 username_out->SetString(temp_username); | |
145 password_out->SetString(temp_password); | |
146 } | |
147 | |
148 ::SecureZeroMemory(temp_username, sizeof(temp_username)); | |
149 ::SecureZeroMemory(temp_password, sizeof(temp_password)); | |
150 | |
151 return result; | |
152 } | |
153 | |
154 } // namespace omaha | |
155 | |
OLD | NEW |