| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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 <CoreFoundation/CoreFoundation.h> | |
| 6 #include <IOKit/IOKitLib.h> | |
| 7 #include <IOKit/network/IOEthernetInterface.h> | |
| 8 #include <IOKit/network/IONetworkInterface.h> | |
| 9 #include <IOKit/network/IOEthernetController.h> | |
| 10 | |
| 11 #include "base/logging.h" | |
| 12 #include "base/mac/foundation_util.h" | |
| 13 #include "base/mac/scoped_cftyperef.h" | |
| 14 #include "base/mac/scoped_ioobject.h" | |
| 15 #include "base/string16.h" | |
| 16 #include "base/stringprintf.h" | |
| 17 #include "base/sys_string_conversions.h" | |
| 18 #include "base/utf_string_conversions.h" | |
| 19 | |
| 20 namespace rlz_lib { | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 // See http://developer.apple.com/library/mac/#technotes/tn1103/_index.html | |
| 25 | |
| 26 // The caller is responsible for freeing |matching_services|. | |
| 27 bool FindEthernetInterfaces(io_iterator_t* matching_services) { | |
| 28 base::mac::ScopedCFTypeRef<CFMutableDictionaryRef> matching_dict( | |
| 29 IOServiceMatching(kIOEthernetInterfaceClass)); | |
| 30 if (!matching_dict) | |
| 31 return false; | |
| 32 | |
| 33 base::mac::ScopedCFTypeRef<CFMutableDictionaryRef> primary_interface( | |
| 34 CFDictionaryCreateMutable(kCFAllocatorDefault, 0, | |
| 35 &kCFTypeDictionaryKeyCallBacks, | |
| 36 &kCFTypeDictionaryValueCallBacks)); | |
| 37 if (!primary_interface) | |
| 38 return false; | |
| 39 | |
| 40 CFDictionarySetValue( | |
| 41 primary_interface, CFSTR(kIOPrimaryInterface), kCFBooleanTrue); | |
| 42 CFDictionarySetValue( | |
| 43 matching_dict, CFSTR(kIOPropertyMatchKey), primary_interface); | |
| 44 | |
| 45 kern_return_t kern_result = IOServiceGetMatchingServices( | |
| 46 kIOMasterPortDefault, matching_dict.release(), matching_services); | |
| 47 | |
| 48 return kern_result == KERN_SUCCESS; | |
| 49 } | |
| 50 | |
| 51 bool GetMACAddressFromIterator(io_iterator_t primary_interface_iterator, | |
| 52 uint8_t* buffer, size_t buffer_size) { | |
| 53 if (buffer_size < kIOEthernetAddressSize) | |
| 54 return false; | |
| 55 | |
| 56 bool success = false; | |
| 57 | |
| 58 bzero(buffer, buffer_size); | |
| 59 base::mac::ScopedIOObject<io_object_t> primary_interface; | |
| 60 while (primary_interface.reset(IOIteratorNext(primary_interface_iterator)), | |
| 61 primary_interface) { | |
| 62 io_object_t primary_interface_parent; | |
| 63 kern_return_t kern_result = IORegistryEntryGetParentEntry( | |
| 64 primary_interface, kIOServicePlane, &primary_interface_parent); | |
| 65 base::mac::ScopedIOObject<io_object_t> primary_interface_parent_deleter( | |
| 66 primary_interface_parent); | |
| 67 success = kern_result == KERN_SUCCESS; | |
| 68 | |
| 69 if (!success) | |
| 70 continue; | |
| 71 | |
| 72 base::mac::ScopedCFTypeRef<CFTypeRef> mac_data( | |
| 73 IORegistryEntryCreateCFProperty(primary_interface_parent, | |
| 74 CFSTR(kIOMACAddress), | |
| 75 kCFAllocatorDefault, | |
| 76 0)); | |
| 77 CFDataRef mac_data_data = base::mac::CFCast<CFDataRef>(mac_data); | |
| 78 if (mac_data_data) { | |
| 79 CFDataGetBytes( | |
| 80 mac_data_data, CFRangeMake(0, kIOEthernetAddressSize), buffer); | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 return success; | |
| 85 } | |
| 86 | |
| 87 bool GetMacAddress(unsigned char* buffer, size_t size) { | |
| 88 io_iterator_t primary_interface_iterator; | |
| 89 if (!FindEthernetInterfaces(&primary_interface_iterator)) | |
| 90 return false; | |
| 91 bool result = GetMACAddressFromIterator( | |
| 92 primary_interface_iterator, buffer, size); | |
| 93 IOObjectRelease(primary_interface_iterator); | |
| 94 return result; | |
| 95 } | |
| 96 | |
| 97 CFStringRef CopySerialNumber() { | |
| 98 base::mac::ScopedIOObject<io_service_t> expert_device( | |
| 99 IOServiceGetMatchingService(kIOMasterPortDefault, | |
| 100 IOServiceMatching("IOPlatformExpertDevice"))); | |
| 101 if (!expert_device) | |
| 102 return NULL; | |
| 103 | |
| 104 base::mac::ScopedCFTypeRef<CFTypeRef> serial_number( | |
| 105 IORegistryEntryCreateCFProperty(expert_device, | |
| 106 CFSTR(kIOPlatformSerialNumberKey), | |
| 107 kCFAllocatorDefault, | |
| 108 0)); | |
| 109 CFStringRef serial_number_cfstring = | |
| 110 base::mac::CFCast<CFStringRef>(serial_number); | |
| 111 if (!serial_number_cfstring) | |
| 112 return NULL; | |
| 113 | |
| 114 ignore_result(serial_number.release()); | |
| 115 return serial_number_cfstring; | |
| 116 } | |
| 117 | |
| 118 } // namespace | |
| 119 | |
| 120 bool GetRawMachineId(string16* data, int* more_data) { | |
| 121 uint8_t mac_address[kIOEthernetAddressSize]; | |
| 122 | |
| 123 data->clear(); | |
| 124 if (GetMacAddress(mac_address, sizeof(mac_address))) { | |
| 125 *data += ASCIIToUTF16(StringPrintf("mac:%02x%02x%02x%02x%02x%02x", | |
| 126 mac_address[0], mac_address[1], mac_address[2], | |
| 127 mac_address[3], mac_address[4], mac_address[5])); | |
| 128 } | |
| 129 | |
| 130 // A MAC address is enough to uniquely identify a machine, but it's only 6 | |
| 131 // bytes, 3 of which are manufacturer-determined. To make brute-forcing the | |
| 132 // SHA1 of this harder, also append the system's serial number. | |
| 133 CFStringRef serial = CopySerialNumber(); | |
| 134 if (serial) { | |
| 135 if (!data->empty()) | |
| 136 *data += UTF8ToUTF16(" "); | |
| 137 *data += UTF8ToUTF16("serial:") + base::SysCFStringRefToUTF16(serial); | |
| 138 CFRelease(serial); | |
| 139 } | |
| 140 | |
| 141 // On windows, this is set to the volume id. Since it's not scrambled before | |
| 142 // being sent, just set it to 1. | |
| 143 *more_data = 1; | |
| 144 return true; | |
| 145 } | |
| 146 | |
| 147 } // namespace rlz_lib | |
| OLD | NEW |