| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "AuthorizedInstall.h" | 5 #import "AuthorizedInstall.h" |
| 6 | 6 |
| 7 @interface AuthorizedInstall () { | 7 @interface AuthorizedInstall () { |
| 8 NSFileHandle* communicationFile_; | 8 NSFileHandle* communicationFile_; |
| 9 NSString* destinationAppBundlePath_; | 9 NSString* destinationAppBundlePath_; |
| 10 } | 10 } |
| 11 @end | 11 @end |
| 12 | 12 |
| 13 @implementation AuthorizedInstall | 13 @implementation AuthorizedInstall |
| 14 // Does the setup needed to authorize a tool to run as admin. | 14 // Does the setup needed to authorize a subprocess to run as root. |
| 15 - (OSStatus)setUpAuthorization:(AuthorizationRef*)authRef { | 15 - (OSStatus)setUpAuthorization:(AuthorizationRef*)authRef { |
| 16 OSStatus status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, | 16 OSStatus status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, |
| 17 kAuthorizationFlagDefaults, authRef); | 17 kAuthorizationFlagDefaults, authRef); |
| 18 | 18 |
| 19 AuthorizationItem items = {kAuthorizationRightExecute, 0, NULL, 0}; | 19 AuthorizationItem items = {kAuthorizationRightExecute, 0, NULL, 0}; |
| 20 AuthorizationRights rights = {1, &items}; | 20 AuthorizationRights rights = {1, &items}; |
| 21 AuthorizationFlags flags = | 21 AuthorizationFlags flags = |
| 22 kAuthorizationFlagDefaults | kAuthorizationFlagInteractionAllowed | | 22 kAuthorizationFlagDefaults | kAuthorizationFlagInteractionAllowed | |
| 23 kAuthorizationFlagPreAuthorize | kAuthorizationFlagExtendRights; | 23 kAuthorizationFlagPreAuthorize | kAuthorizationFlagExtendRights; |
| 24 | 24 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 if (![manager fileExistsAtPath:usersApplicationsDirectory]) { | 75 if (![manager fileExistsAtPath:usersApplicationsDirectory]) { |
| 76 [manager createDirectoryAtPath:usersApplicationsDirectory | 76 [manager createDirectoryAtPath:usersApplicationsDirectory |
| 77 withIntermediateDirectories:NO | 77 withIntermediateDirectories:NO |
| 78 attributes:nil | 78 attributes:nil |
| 79 error:nil]; | 79 error:nil]; |
| 80 } | 80 } |
| 81 return usersApplicationsDirectory; | 81 return usersApplicationsDirectory; |
| 82 } | 82 } |
| 83 } | 83 } |
| 84 | 84 |
| 85 // Attempts to gain authorization to run installation tool with elevated | |
| 86 // permissions. | |
| 87 // Then starts the tool with the appropiate paths for the tools elevation | |
| 88 // status. | |
| 89 - (BOOL)loadInstallationTool { | 85 - (BOOL)loadInstallationTool { |
| 90 AuthorizationRef authRef = NULL; | 86 AuthorizationRef authRef = NULL; |
| 91 OSStatus status = [self setUpAuthorization:&authRef]; | 87 OSStatus status = [self setUpAuthorization:&authRef]; |
| 92 BOOL isAuthorized = (status == errAuthorizationSuccess); | 88 BOOL isAuthorized = (status == errAuthorizationSuccess); |
| 93 | 89 |
| 94 NSString* toolPath = | 90 NSString* toolPath = |
| 95 [[NSBundle mainBundle] pathForResource:@"copy_to_disk" ofType:@"sh"]; | 91 [[NSBundle mainBundle] pathForResource:@"copy_to_disk" ofType:@"sh"]; |
| 96 NSFileManager* manager = [NSFileManager defaultManager]; | 92 NSFileManager* manager = [NSFileManager defaultManager]; |
| 97 if (![manager fileExistsAtPath:toolPath]) { | 93 if (![manager fileExistsAtPath:toolPath]) { |
| 98 return false; | 94 return false; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 110 status:status]; | 106 status:status]; |
| 111 } else { | 107 } else { |
| 112 NSArray* args = @[ applicationsDirectory ]; | 108 NSArray* args = @[ applicationsDirectory ]; |
| 113 [self startUnprivilegedTool:toolPath withArguments:args]; | 109 [self startUnprivilegedTool:toolPath withArguments:args]; |
| 114 } | 110 } |
| 115 | 111 |
| 116 AuthorizationFree(authRef, kAuthorizationFlagDestroyRights); | 112 AuthorizationFree(authRef, kAuthorizationFlagDestroyRights); |
| 117 return true; | 113 return true; |
| 118 } | 114 } |
| 119 | 115 |
| 120 - (NSString*)startInstall:(NSString*)appBundlePath { | |
| 121 [self sendMessageToTool:appBundlePath]; | |
| 122 return destinationAppBundlePath_; | |
| 123 } | |
| 124 | |
| 125 // Sends a message to the tool's stdin. The tool is using 'read' to wait for | 116 // Sends a message to the tool's stdin. The tool is using 'read' to wait for |
| 126 // input. 'read' adds to its buffer until it receives a newline to continue so | 117 // input. 'read' adds to its buffer until it receives a newline to continue so |
| 127 // append '\n' to the message to end the read. | 118 // append '\n' to the message to end the read. |
| 128 - (void)sendMessageToTool:(NSString*)message { | 119 - (void)sendMessageToTool:(NSString*)message { |
| 129 [communicationFile_ writeData:[[message stringByAppendingString:@"\n"] | 120 [communicationFile_ writeData:[[message stringByAppendingString:@"\n"] |
| 130 dataUsingEncoding:NSUTF8StringEncoding]]; | 121 dataUsingEncoding:NSUTF8StringEncoding]]; |
| 131 } | 122 } |
| 132 | 123 |
| 124 - (NSString*)startInstall:(NSString*)appBundlePath { |
| 125 [self sendMessageToTool:appBundlePath]; |
| 126 return destinationAppBundlePath_; |
| 127 } |
| 128 |
| 133 @end | 129 @end |
| OLD | NEW |