Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / password_manager / password_manager_util_win.cc
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // windows.h must be first otherwise Win8 SDK breaks.
6 #include <windows.h>
7 #include <LM.h>
8 #include <wincred.h>
9
10 // SECURITY_WIN32 must be defined in order to get
11 // EXTENDED_NAME_FORMAT enumeration.
12 #define SECURITY_WIN32 1
13 #include <security.h>
14 #undef SECURITY_WIN32
15
16 #include "chrome/browser/password_manager/password_manager_util.h"
17
18 #include "base/prefs/pref_service.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "base/time/time.h"
21 #include "base/win/windows_version.h"
22 #include "chrome/browser/browser_process.h"
23 #include "chrome/grit/chromium_strings.h"
24 #include "components/password_manager/core/browser/password_manager.h"
25 #include "components/password_manager/core/common/password_manager_pref_names.h"
26 #include "content/public/browser/render_view_host.h"
27 #include "content/public/browser/render_widget_host_view.h"
28 #include "ui/base/l10n/l10n_util.h"
29
30 #if defined(USE_AURA)
31 #include "ui/aura/window.h"
32 #include "ui/aura/window_tree_host.h"
33 #endif
34
35 namespace password_manager_util {
36
37 const unsigned kMaxPasswordRetries = 3;
38
39 const unsigned kCredUiDefaultFlags =
40     CREDUI_FLAGS_GENERIC_CREDENTIALS |
41     CREDUI_FLAGS_EXCLUDE_CERTIFICATES |
42     CREDUI_FLAGS_KEEP_USERNAME |
43     CREDUI_FLAGS_ALWAYS_SHOW_UI |
44     CREDUI_FLAGS_DO_NOT_PERSIST;
45
46 static int64 GetPasswordLastChanged(WCHAR* username) {
47   LPUSER_INFO_1 user_info = NULL;
48   DWORD age = 0;
49
50   NET_API_STATUS ret = NetUserGetInfo(NULL, username, 1, (LPBYTE*) &user_info);
51
52   if (ret == NERR_Success) {
53     // Returns seconds since last password change.
54     age = user_info->usri1_password_age;
55     NetApiBufferFree(user_info);
56   } else {
57     return -1;
58   }
59
60   base::Time changed = base::Time::Now() - base::TimeDelta::FromSeconds(age);
61
62   return changed.ToInternalValue();
63 }
64
65 static bool CheckBlankPassword(WCHAR* username) {
66   PrefService* local_state = g_browser_process->local_state();
67   int64 last_changed = GetPasswordLastChanged(username);
68   bool need_recheck = true;
69   bool blank_password = false;
70
71   // If we cannot determine when the password was last changed
72   // then assume the password is not blank
73   if (last_changed == -1)
74     return false;
75
76   blank_password =
77       local_state->GetBoolean(password_manager::prefs::kOsPasswordBlank);
78   int64 pref_last_changed =
79       local_state->GetInt64(password_manager::prefs::kOsPasswordLastChanged);
80   if (pref_last_changed > 0 && last_changed <= pref_last_changed) {
81     need_recheck = false;
82   }
83
84   if (need_recheck) {
85     HANDLE handle = INVALID_HANDLE_VALUE;
86
87     // Attempt to login using blank password.
88     DWORD logon_result = LogonUser(username,
89                                    L".",
90                                    L"",
91                                    LOGON32_LOGON_NETWORK,
92                                    LOGON32_PROVIDER_DEFAULT,
93                                    &handle);
94
95     // Win XP and later return ERROR_ACCOUNT_RESTRICTION for blank password.
96     if (logon_result)
97       CloseHandle(handle);
98
99     // In the case the password is blank, then LogonUser returns a failure,
100     // handle is INVALID_HANDLE_VALUE, and GetLastError() is
101     // ERROR_ACCOUNT_RESTRICTION.
102     // http://msdn.microsoft.com/en-us/library/windows/desktop/ms681385
103     blank_password = (logon_result ||
104                       GetLastError() == ERROR_ACCOUNT_RESTRICTION);
105   }
106
107   // Account for clock skew between pulling the password age and
108   // writing to the preferences by adding a small skew factor here.
109   last_changed += base::Time::kMicrosecondsPerSecond;
110
111   // Save the blank password status for later.
112   local_state->SetBoolean(password_manager::prefs::kOsPasswordBlank,
113                           blank_password);
114   local_state->SetInt64(password_manager::prefs::kOsPasswordLastChanged,
115                         last_changed);
116
117   return blank_password;
118 }
119
120 OsPasswordStatus GetOsPasswordStatus() {
121   DWORD username_length = CREDUI_MAX_USERNAME_LENGTH;
122   WCHAR username[CREDUI_MAX_USERNAME_LENGTH+1] = {};
123   OsPasswordStatus retVal = PASSWORD_STATUS_UNKNOWN;
124
125   if (GetUserNameEx(NameUserPrincipal, username, &username_length)) {
126     // If we are on a domain, it is almost certain that the password is not
127     // blank, but we do not actively check any further than this to avoid any
128     // failed login attempts hitting the domain controller.
129     retVal = PASSWORD_STATUS_WIN_DOMAIN;
130   } else {
131     username_length = CREDUI_MAX_USERNAME_LENGTH;
132     if (GetUserName(username, &username_length)) {
133       retVal = CheckBlankPassword(username) ? PASSWORD_STATUS_BLANK :
134           PASSWORD_STATUS_NONBLANK;
135     }
136   }
137
138   return retVal;
139 }
140
141 bool AuthenticateUser(gfx::NativeWindow window) {
142   bool retval = false;
143   CREDUI_INFO cui = {};
144   WCHAR username[CREDUI_MAX_USERNAME_LENGTH+1] = {};
145   WCHAR displayname[CREDUI_MAX_USERNAME_LENGTH+1] = {};
146   WCHAR password[CREDUI_MAX_PASSWORD_LENGTH+1] = {};
147   DWORD username_length = CREDUI_MAX_USERNAME_LENGTH;
148   base::string16 product_name = l10n_util::GetStringUTF16(IDS_PRODUCT_NAME);
149   base::string16 password_prompt =
150       l10n_util::GetStringUTF16(IDS_PASSWORDS_PAGE_AUTHENTICATION_PROMPT);
151   HANDLE handle = INVALID_HANDLE_VALUE;
152   int tries = 0;
153   bool use_displayname = false;
154   bool use_principalname = false;
155   DWORD logon_result = 0;
156
157   // Disable password manager reauthentication before Windows 7.
158   // This is because of an interaction between LogonUser() and the sandbox.
159   // http://crbug.com/345916
160   if (base::win::GetVersion() < base::win::VERSION_WIN7)
161     return true;
162
163   // On a domain, we obtain the User Principal Name
164   // for domain authentication.
165   if (GetUserNameEx(NameUserPrincipal, username, &username_length)) {
166     use_principalname = true;
167   } else {
168     username_length = CREDUI_MAX_USERNAME_LENGTH;
169     // Otherwise, we're a workstation, use the plain local username.
170     if (!GetUserName(username, &username_length)) {
171       DLOG(ERROR) << "Unable to obtain username " << GetLastError();
172       return false;
173     } else {
174       // As we are on a workstation, it's possible the user
175       // has no password, so check here.
176       if (CheckBlankPassword(username))
177         return true;
178     }
179   }
180
181   // Try and obtain a friendly display name.
182   username_length = CREDUI_MAX_USERNAME_LENGTH;
183   if (GetUserNameEx(NameDisplay, displayname, &username_length))
184     use_displayname = true;
185
186   cui.cbSize = sizeof(CREDUI_INFO);
187   cui.hwndParent = NULL;
188 #if defined(USE_AURA)
189   cui.hwndParent = window->GetHost()->GetAcceleratedWidget();
190 #else
191   cui.hwndParent = window;
192 #endif
193
194   cui.pszMessageText = password_prompt.c_str();
195   cui.pszCaptionText = product_name.c_str();
196
197   cui.hbmBanner = NULL;
198   BOOL save_password = FALSE;
199   DWORD credErr = NO_ERROR;
200
201   do {
202     tries++;
203
204     // TODO(wfh) Make sure we support smart cards here.
205     credErr = CredUIPromptForCredentials(
206         &cui,
207         product_name.c_str(),
208         NULL,
209         0,
210         use_displayname ? displayname : username,
211         CREDUI_MAX_USERNAME_LENGTH+1,
212         password,
213         CREDUI_MAX_PASSWORD_LENGTH+1,
214         &save_password,
215         kCredUiDefaultFlags |
216         (tries > 1 ? CREDUI_FLAGS_INCORRECT_PASSWORD : 0));
217
218     if (credErr == NO_ERROR) {
219       logon_result = LogonUser(username,
220                                use_principalname ? NULL : L".",
221                                password,
222                                LOGON32_LOGON_NETWORK,
223                                LOGON32_PROVIDER_DEFAULT,
224                                &handle);
225       if (logon_result) {
226         retval = true;
227         CloseHandle(handle);
228       } else {
229         if (GetLastError() == ERROR_ACCOUNT_RESTRICTION &&
230             wcslen(password) == 0) {
231           // Password is blank, so permit.
232           retval = true;
233         } else {
234           DLOG(WARNING) << "Unable to authenticate " << GetLastError();
235         }
236       }
237       SecureZeroMemory(password, sizeof(password));
238     }
239   } while (credErr == NO_ERROR &&
240            (retval == false && tries < kMaxPasswordRetries));
241   return retval;
242 }
243
244 }  // namespace password_manager_util