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 #ifndef OMAHA_GOOPDATE_CRED_DIALOG_H_ | |
17 #define OMAHA_GOOPDATE_CRED_DIALOG_H_ | |
18 | |
19 #include <atlbase.h> | |
20 #include <atlcom.h> | |
21 #include "goopdate/omaha3_idl.h" | |
22 #include "omaha/base/atlregmapex.h" | |
23 #include "omaha/common/const_goopdate.h" | |
24 #include "omaha/common/goopdate_utils.h" | |
25 #include "omaha/goopdate/com_proxy.h" | |
26 #include "omaha/goopdate/non_localized_resource.h" | |
27 | |
28 namespace omaha { | |
29 | |
30 #pragma warning(push) | |
31 // Construction of local static object is not thread-safe | |
32 #pragma warning(disable:4640) | |
33 | |
34 class ATL_NO_VTABLE CredentialDialogBase | |
35 : public CComObjectRootEx<CComObjectThreadModel>, | |
36 public ICredentialDialog, | |
37 public StdMarshalInfo { | |
38 public: | |
39 explicit CredentialDialogBase(bool is_machine) | |
40 : StdMarshalInfo(is_machine), | |
41 is_machine_(is_machine) {} | |
42 | |
43 BEGIN_COM_MAP(CredentialDialogBase) | |
44 COM_INTERFACE_ENTRY(IStdMarshalInfo) | |
45 COM_INTERFACE_ENTRY(ICredentialDialog) | |
46 END_COM_MAP() | |
47 | |
48 // ICredentialDialog methods. | |
49 STDMETHOD(QueryUserForCredentials)(ULONG_PTR owner_hwnd, | |
50 BSTR server, | |
51 BSTR caption, | |
52 BSTR* username, | |
53 BSTR* password); | |
54 | |
55 protected: | |
56 virtual ~CredentialDialogBase() {} | |
57 | |
58 private: | |
59 bool is_machine_; | |
60 | |
61 static HRESULT DoQueryUserForCredentials( | |
62 HWND hwnd, | |
63 BSTR server, | |
64 BSTR caption, | |
65 BSTR* username, | |
66 BSTR* password); | |
67 | |
68 static DWORD DisplayDialog( | |
69 HWND hwnd, | |
70 LPCTSTR server, | |
71 LPCTSTR message, | |
72 LPCTSTR caption, | |
73 CString* username_out, | |
74 CString* password_out); | |
75 | |
76 DISALLOW_COPY_AND_ASSIGN(CredentialDialogBase); | |
77 }; | |
78 | |
79 template <typename T> | |
80 class ATL_NO_VTABLE CredentialDialog | |
81 : public CredentialDialogBase, | |
82 public CComCoClass<CredentialDialog<T> > { | |
83 public: | |
84 CredentialDialog() : CredentialDialogBase(T::is_machine()) {} | |
85 | |
86 DECLARE_NOT_AGGREGATABLE(CredentialDialog); | |
87 DECLARE_REGISTRY_RESOURCEID_EX(T::registry_res_id()) | |
88 | |
89 BEGIN_REGISTRY_MAP() | |
90 REGMAP_ENTRY(_T("HKROOT"), T::hk_root()) | |
91 REGMAP_ENTRY(_T("VERSION"), _T("1.0")) | |
92 REGMAP_ENTRY(_T("PROGID"), T::prog_id()) | |
93 REGMAP_ENTRY(_T("DESCRIPTION"), _T("GoogleUpdate CredentialDialog")) | |
94 REGMAP_ENTRY(_T("CLSID"), T::class_id()) | |
95 REGMAP_MODULE2(_T("MODULE"), kOmahaOnDemandFileName) | |
96 END_REGISTRY_MAP() | |
97 | |
98 protected: | |
99 virtual ~CredentialDialog() {} | |
100 | |
101 private: | |
102 DISALLOW_COPY_AND_ASSIGN(CredentialDialog); | |
103 }; | |
104 | |
105 | |
106 struct CredentialDialogModeUser { | |
107 static bool is_machine() { return false; } | |
108 static const TCHAR* const prog_id() { return kProgIDCredentialDialogUser; } | |
109 static GUID class_id() { return __uuidof(CredentialDialogUserClass); } | |
110 static UINT registry_res_id() { return IDR_LOCAL_SERVER_RGS; } | |
111 static const TCHAR* const hk_root() { return _T("HKCU"); } | |
112 }; | |
113 | |
114 struct CredentialDialogModeMachine { | |
115 static bool is_machine() { return true; } | |
116 static const TCHAR* const prog_id() { return kProgIDCredentialDialogMachine; } | |
117 static GUID class_id() { return __uuidof(CredentialDialogMachineClass); } | |
118 static UINT registry_res_id() { return IDR_LOCAL_SERVER_RGS; } | |
119 static const TCHAR* const hk_root() { return _T("HKLM"); } | |
120 }; | |
121 | |
122 typedef CredentialDialog<CredentialDialogModeUser> CredentialDialogUser; | |
123 typedef CredentialDialog<CredentialDialogModeMachine> CredentialDialogMachine; | |
124 | |
125 // A static function that decides whether to display the dialog in-process | |
126 // or launch an out-of-process COM server for showing it, and automatically | |
127 // handles BSTR/CString conversion. | |
128 inline HRESULT LaunchCredentialDialog( | |
129 bool is_machine, | |
130 HWND owner_hwnd, | |
131 const CString& server, | |
132 const CString& caption, | |
133 CString* username_out, | |
134 CString* password_out) { | |
135 ASSERT1(username_out); | |
136 ASSERT1(password_out); | |
137 | |
138 CAccessToken access_token; | |
139 if (!access_token.GetThreadToken(TOKEN_READ)) { | |
140 // If this thread is currently impersonating a user, that's perfect, as the | |
141 // COM server will be started under that user. If not, verify that the | |
142 // process isn't running as LocalSystem/LocalService - we cannot show UI | |
143 // in that scenario without impersonating. | |
144 bool is_system = true; | |
145 HRESULT hr = IsSystemProcess(&is_system); | |
146 if (FAILED(hr)) { | |
147 CORE_LOG(LE, (_T("[CredDialog][IsSystemProcess failed][0x%08x]"), hr)); | |
148 return hr; | |
149 } | |
150 if (is_system) { | |
151 CORE_LOG(LE, (_T("[CredDialog][Process running as SYSTEM - aborting]"))); | |
152 return E_ABORT; | |
153 } | |
154 } | |
155 | |
156 CComPtr<ICredentialDialog> dialog; | |
157 REFCLSID clsid = is_machine ? __uuidof(CredentialDialogMachineClass) : | |
158 __uuidof(CredentialDialogUserClass); | |
159 HRESULT hr = dialog.CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER); | |
160 if (FAILED(hr)) { | |
161 CORE_LOG(LE, (_T("[LaunchCredentialDialog][CoCreate failed][0x%08x]"), hr)); | |
162 return hr; | |
163 } | |
164 | |
165 CComBSTR server_bstr(server); | |
166 CComBSTR caption_bstr(caption); | |
167 CComBSTR username_bstr; | |
168 CComBSTR password_bstr; | |
169 hr = dialog->QueryUserForCredentials(reinterpret_cast<ULONG_PTR>(owner_hwnd), | |
170 server_bstr, | |
171 caption_bstr, | |
172 &username_bstr, | |
173 &password_bstr); | |
174 | |
175 if (SUCCEEDED(hr)) { | |
176 username_out->SetString(username_bstr); | |
177 password_out->SetString(password_bstr); | |
178 } | |
179 ::SecureZeroMemory(username_bstr.m_str, username_bstr.ByteLength()); | |
180 ::SecureZeroMemory(password_bstr.m_str, password_bstr.ByteLength()); | |
181 | |
182 return hr; | |
183 } | |
184 | |
185 #pragma warning(pop) | |
186 | |
187 } // namespace omaha | |
188 | |
189 #endif // OMAHA_GOOPDATE_CRED_DIALOG_H_ | |
190 | |
OLD | NEW |