OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 #import <Foundation/Foundation.h> |
| 6 #import <OpenDirectory/OpenDirectory.h> |
| 7 |
| 8 #include "base/files/file_path.h" |
| 9 #include "base/files/file_util.h" |
| 10 #include "base/mac/foundation_util.h" |
| 11 #include "base/mac/scoped_nsautorelease_pool.h" |
| 12 #include "base/mac/scoped_nsobject.h" |
| 13 #include "base/strings/sys_string_conversions.h" |
| 14 #include "base/version.h" |
| 15 #include "components/update_client/updater_state.h" |
| 16 |
| 17 namespace update_client { |
| 18 |
| 19 namespace { |
| 20 |
| 21 const base::FilePath::CharType kKeystonePlist[] = FILE_PATH_LITERAL( |
| 22 "Google/GoogleSoftwareUpdate/GoogleSoftwareUpdate.bundle/" |
| 23 "Contents/Info.plist"); |
| 24 |
| 25 // Gets a value from the updater settings. Returns a retained object. |
| 26 // T should be a toll-free Foundation framework type. See Apple's |
| 27 // documentation for toll-free bridging. |
| 28 template<class T> |
| 29 base::scoped_nsobject<T> GetUpdaterSettingsValue(NSString* value_name) { |
| 30 CFStringRef app_id = CFSTR("com.google.Keystone.Agent"); |
| 31 base::ScopedCFTypeRef<CFPropertyListRef> plist( |
| 32 CFPreferencesCopyAppValue(base::mac::NSToCFCast(value_name), app_id)); |
| 33 T* value = base::mac::ObjCCastStrict<T>(static_cast<id>(plist.get())); |
| 34 return base::scoped_nsobject<T>(value); |
| 35 } |
| 36 |
| 37 base::Time GetUpdaterSettingsTime(NSString* value_name) { |
| 38 base::scoped_nsobject<NSDate> date = |
| 39 GetUpdaterSettingsValue<NSDate>(value_name); |
| 40 base::Time result = |
| 41 base::Time::FromCFAbsoluteTime([date timeIntervalSinceReferenceDate]); |
| 42 |
| 43 return result; |
| 44 } |
| 45 |
| 46 base::Version GetVersionFromPlist(const base::FilePath& info_plist) { |
| 47 base::mac::ScopedNSAutoreleasePool scoped_pool; |
| 48 NSData* data = |
| 49 [NSData dataWithContentsOfFile: |
| 50 base::mac::FilePathToNSString(info_plist)]; |
| 51 if ([data length] == 0) { |
| 52 return base::Version(); |
| 53 } |
| 54 NSDictionary* all_keys = base::mac::ObjCCastStrict<NSDictionary>( |
| 55 [NSPropertyListSerialization propertyListWithData:data |
| 56 options:NSPropertyListImmutable |
| 57 format:nil |
| 58 error:nil]); |
| 59 if (all_keys == nil) { |
| 60 return base::Version(); |
| 61 } |
| 62 CFStringRef version = |
| 63 base::mac::GetValueFromDictionary<CFStringRef>( |
| 64 base::mac::NSToCFCast(all_keys), |
| 65 kCFBundleVersionKey); |
| 66 if (version == NULL) { |
| 67 return base::Version(); |
| 68 } |
| 69 return base::Version(base::SysCFStringRefToUTF8(version)); |
| 70 } |
| 71 |
| 72 } // namespace |
| 73 |
| 74 std::string UpdaterState::GetUpdaterName() { |
| 75 return std::string("Keystone"); |
| 76 } |
| 77 |
| 78 base::Version UpdaterState::GetUpdaterVersion(bool /*is_machine*/) { |
| 79 // System Keystone trumps user one, so check this one first |
| 80 base::FilePath local_library; |
| 81 bool success = base::mac::GetLocalDirectory(NSLibraryDirectory, |
| 82 &local_library); |
| 83 DCHECK(success); |
| 84 base::FilePath system_bundle_plist = local_library.Append(kKeystonePlist); |
| 85 base::Version system_keystone = GetVersionFromPlist(system_bundle_plist); |
| 86 if (system_keystone.IsValid()) { |
| 87 return system_keystone; |
| 88 } |
| 89 |
| 90 base::FilePath user_bundle_plist = |
| 91 base::mac::GetUserLibraryPath().Append(kKeystonePlist); |
| 92 return GetVersionFromPlist(user_bundle_plist); |
| 93 } |
| 94 |
| 95 base::Time UpdaterState::GetUpdaterLastStartedAU(bool /*is_machine*/) { |
| 96 return GetUpdaterSettingsTime(@"lastCheckStartDate"); |
| 97 } |
| 98 |
| 99 base::Time UpdaterState::GetUpdaterLastChecked(bool /*is_machine*/) { |
| 100 return GetUpdaterSettingsTime(@"lastServerCheckDate"); |
| 101 } |
| 102 |
| 103 bool UpdaterState::IsAutoupdateCheckEnabled() { |
| 104 // Auto-update check period override (in seconds). |
| 105 // Applies only to older versions of Keystone. |
| 106 base::scoped_nsobject<NSNumber> timeInterval = |
| 107 GetUpdaterSettingsValue<NSNumber>(@"checkInterval"); |
| 108 if (!timeInterval.get()) return true; |
| 109 int value = [timeInterval intValue]; |
| 110 |
| 111 return 0 < value && value < (24 * 60 * 60); |
| 112 } |
| 113 |
| 114 int UpdaterState::GetUpdatePolicy() { |
| 115 return -1; // Keystone does not support update policies. |
| 116 } |
| 117 |
| 118 bool UpdaterState::IsEnterpriseManaged() { |
| 119 base::mac::ScopedNSAutoreleasePool scoped_pool; |
| 120 |
| 121 ODSession* session = [ODSession defaultSession]; |
| 122 if (session == nil) { |
| 123 DLOG(WARNING) << "ODSession defult session is nil."; |
| 124 return false; |
| 125 } |
| 126 |
| 127 NSError* error = nil; |
| 128 ODNode* node = [ODNode nodeWithSession:session |
| 129 type:kODNodeTypeAuthentication |
| 130 error:&error]; |
| 131 if (node == nil) { |
| 132 DLOG(WARNING) << "ODSession cannot obtain the authentication node: " |
| 133 << base::mac::NSToCFCast(error); |
| 134 return false; |
| 135 } |
| 136 |
| 137 ODQuery* query = [ODQuery queryWithNode:node |
| 138 forRecordTypes:kODRecordTypeUsers |
| 139 attribute:kODAttributeTypeRecordName |
| 140 matchType:kODMatchEqualTo |
| 141 queryValues:NSUserName() |
| 142 returnAttributes:kODAttributeTypeAllAttributes |
| 143 maximumResults:0 |
| 144 error:&error]; |
| 145 if (query == nil) { |
| 146 DLOG(WARNING) << "ODSession cannot create user query: " |
| 147 << base::mac::NSToCFCast(error); |
| 148 return false; |
| 149 } |
| 150 |
| 151 NSArray* results = [query resultsAllowingPartial:NO error:&error]; |
| 152 if (!results) { |
| 153 DLOG(WARNING) << "ODSession cannot obtain current user node: " |
| 154 << base::mac::NSToCFCast(error); |
| 155 return false; |
| 156 } |
| 157 if (results.count != 1) { |
| 158 DLOG(WARNING) << @"ODSession unexpected number of nodes: " |
| 159 << results.count; |
| 160 } |
| 161 for (id element in results) { |
| 162 ODRecord* record = base::mac::ObjCCastStrict<ODRecord>(element); |
| 163 NSArray* attributes = |
| 164 [record valuesForAttribute:kODAttributeTypeMetaRecordName |
| 165 error:NULL]; |
| 166 for (id attribute in attributes) { |
| 167 NSString* attribute_value = |
| 168 base::mac::ObjCCastStrict<NSString>(attribute); |
| 169 // Example: "uid=johnsmith,ou=People,dc=chromium,dc=org |
| 170 NSRange dc = [attribute_value rangeOfString:@"(^|,)\\s*dc=" |
| 171 options:NSRegularExpressionSearch]; |
| 172 if (dc.length > 0) { |
| 173 return true; |
| 174 } |
| 175 } |
| 176 } |
| 177 return false; |
| 178 } |
| 179 |
| 180 } // namespace update_client |
OLD | NEW |