OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 #include "chrome/browser/prefs/tracked/device_id.h" |
| 6 |
| 7 #include <windows.h> |
| 8 #include <sddl.h> // For ConvertSidToStringSidA. |
| 9 |
| 10 #include "base/macros.h" |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/logging.h" |
| 13 |
| 14 namespace tracked_prefs { |
| 15 |
| 16 MachineIdStatus GetDeterministicMachineSpecificId(std::string* machine_id) { |
| 17 DCHECK(machine_id); |
| 18 |
| 19 wchar_t computer_name[MAX_COMPUTERNAME_LENGTH + 1] = {}; |
| 20 DWORD computer_name_size = arraysize(computer_name); |
| 21 |
| 22 if (!::GetComputerNameW(computer_name, &computer_name_size)) |
| 23 return MachineIdStatus::FAILURE; |
| 24 |
| 25 DWORD sid_size = SECURITY_MAX_SID_SIZE; |
| 26 char sid_buffer[SECURITY_MAX_SID_SIZE]; |
| 27 SID* sid = reinterpret_cast<SID*>(sid_buffer); |
| 28 DWORD domain_size = 128; // Will expand below if needed. |
| 29 scoped_ptr<wchar_t[]> domain_buffer(new wchar_t[domain_size]); |
| 30 SID_NAME_USE sid_name_use; |
| 31 |
| 32 // Although the fifth argument to |LookupAccountNameW()|, |
| 33 // |ReferencedDomainName|, is annotated as |_Out_opt_|, if a NULL |
| 34 // value is passed in, zero is returned and |GetLastError()| will |
| 35 // return |ERROR_INSUFFICIENT_BUFFER| (assuming that nothing else went |
| 36 // wrong). In order to ensure that the call to |LookupAccountNameW()| |
| 37 // has succeeded, it is necessary to include the following logic and |
| 38 // obtain the domain name. |
| 39 if (!::LookupAccountNameW(NULL, computer_name, sid, &sid_size, |
| 40 domain_buffer.get(), &domain_size, |
| 41 &sid_name_use)) { |
| 42 // If the initial size of |domain_buffer| was too small, the |
| 43 // required size is now found in |domain_size|. Resize and try |
| 44 // again. |
| 45 if (::GetLastError() != ERROR_INSUFFICIENT_BUFFER) |
| 46 return MachineIdStatus::FAILURE; |
| 47 |
| 48 domain_buffer.reset(new wchar_t[domain_size]); |
| 49 if (!::LookupAccountNameW(NULL, computer_name, sid, &sid_size, |
| 50 domain_buffer.get(), &domain_size, |
| 51 &sid_name_use)) { |
| 52 return MachineIdStatus::FAILURE; |
| 53 } |
| 54 } |
| 55 |
| 56 // Ensure that the correct type of SID was obtained. The |
| 57 // |LookupAccountNameW()| function seems to always return |
| 58 // |SidTypeDomain| instead of |SidTypeComputer| when the computer name |
| 59 // is passed in as its second argument and therefore both enum values |
| 60 // will be considered acceptable. If the computer name and user name |
| 61 // coincide, |LookupAccountNameW()| seems to always return the machine |
| 62 // SID and set the returned enum to |SidTypeDomain|. |
| 63 DCHECK(sid_name_use == SID_NAME_USE::SidTypeComputer || |
| 64 sid_name_use == SID_NAME_USE::SidTypeDomain); |
| 65 |
| 66 char* sid_string = NULL; |
| 67 if (!::ConvertSidToStringSidA(sid, &sid_string)) |
| 68 return MachineIdStatus::FAILURE; |
| 69 |
| 70 *machine_id = sid_string; |
| 71 ::LocalFree(sid_string); |
| 72 |
| 73 return MachineIdStatus::SUCCESS; |
| 74 } |
| 75 |
| 76 } // namespace tracked_prefs |
OLD | NEW |