Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #import "AuthorizedInstall.h" | |
| 6 | |
| 7 @implementation AuthorizedInstall | |
| 8 | |
| 9 // Does the setup needed to authorize a tool to run as admin. | |
| 10 - (BOOL)setUpAuthorization { | |
| 11 status_ = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, | |
| 12 kAuthorizationFlagDefaults, &authRef_); | |
| 13 | |
| 14 AuthorizationItem items = {kAuthorizationRightExecute, 0, NULL, 0}; | |
| 15 AuthorizationRights rights = {1, &items}; | |
| 16 AuthorizationFlags flags = | |
| 17 kAuthorizationFlagDefaults | kAuthorizationFlagInteractionAllowed | | |
| 18 kAuthorizationFlagPreAuthorize | kAuthorizationFlagExtendRights; | |
| 19 | |
| 20 status_ = AuthorizationCopyRights(authRef_, &rights, NULL, flags, NULL); | |
| 21 return (status_ == errAuthorizationSuccess); | |
| 22 } | |
| 23 | |
| 24 // Starts up the proccess with privileged permissios. | |
| 25 - (void)startAuthorizedTool:(const char*)toolPath | |
| 26 withArguments:(const char**)args { | |
| 27 if (status_ != errAuthorizationSuccess) | |
| 28 return; | |
| 29 | |
| 30 #pragma clang diagnostic push | |
| 31 #pragma clang diagnostic ignored "-Wdeprecated-declarations" | |
|
Anna Zeng
2016/08/18 15:57:16
Mark (mark@chromium.org): Have we tried NSAppleScr
| |
| 32 status_ = AuthorizationExecuteWithPrivileges( | |
| 33 authRef_, toolPath, kAuthorizationFlagDefaults, (char* const*)args, | |
| 34 &communicationPipe_); | |
| 35 #pragma clang diagnostic pop | |
| 36 } | |
| 37 | |
| 38 // Finds the tool to be authorized, Finds the paths needed for the tool to do | |
| 39 // its job, and starts the process. | |
| 40 // TODO: Move attemptAuthorizedInstall body into Unpacker method which will | |
|
Anna Zeng
2016/08/18 15:57:16
As discussed, we're actually going to have Authori
ivanhernandez
2016/08/22 15:34:49
Done.
| |
| 41 // provide alternative in the case where authorization failed. | |
| 42 - (BOOL)authorizeInstallationTool { | |
| 43 NSString* toolPath = | |
| 44 [[NSBundle mainBundle] pathForResource:@"copy_to_disk" ofType:@"sh"]; | |
| 45 NSFileManager* manager = [NSFileManager defaultManager]; | |
| 46 if (![manager fileExistsAtPath:toolPath]) { | |
| 47 return false; | |
| 48 } | |
| 49 | |
| 50 NSString* appBundleName = @"Google Chrome.app"; | |
| 51 | |
| 52 NSArray* applicationDirectories = NSSearchPathForDirectoriesInDomains( | |
| 53 NSApplicationDirectory, NSLocalDomainMask, YES); | |
| 54 NSString* applicationDirectory = applicationDirectories.firstObject; | |
| 55 | |
| 56 // TODO: Change sourceAppBundlePath to the temporary folder that chrome will | |
| 57 // be downloaded and extracted to. | |
| 58 NSArray* downloadDirectories = NSSearchPathForDirectoriesInDomains( | |
| 59 NSDownloadsDirectory, NSUserDomainMask, YES); | |
| 60 NSString* sourceAppBundlePath = [NSString | |
|
Anna Zeng
2016/08/18 15:57:16
We can make this a parameter.
ivanhernandez
2016/08/22 15:34:49
We could but we would need to make some changes to
| |
| 61 pathWithComponents:@[ downloadDirectories.firstObject, appBundleName ]]; | |
| 62 NSString* destinationAppBundlePath = | |
|
Anna Zeng
2016/08/18 15:57:16
If root, this can just be @"/Applications/Google C
ivanhernandez
2016/08/22 15:34:49
I don't know actually. Its true the path will alwa
| |
| 63 [NSString pathWithComponents:@[ applicationDirectory, appBundleName ]]; | |
| 64 | |
| 65 const char* sourcePath = [sourceAppBundlePath UTF8String]; | |
| 66 const char* destinationPath = [applicationDirectory UTF8String]; | |
| 67 const char* fullDestinationPath = [destinationAppBundlePath UTF8String]; | |
| 68 const char* args[] = {sourcePath, destinationPath, fullDestinationPath, NULL}; | |
| 69 | |
| 70 if ([self setUpAuthorization]) { | |
| 71 [self startAuthorizedTool:[toolPath UTF8String] withArguments:args]; | |
| 72 AuthorizationFree(authRef_, kAuthorizationFlagDestroyRights); | |
| 73 } | |
| 74 return (status_ == errAuthorizationSuccess); | |
| 75 } | |
| 76 | |
| 77 // Sends a message to the auhtorized tool's stdin. | |
| 78 - (void)sendMessageToTool:(NSString*)message { | |
| 79 if (communicationPipe_ != NULL) { | |
| 80 const char* readyState = message.UTF8String; | |
| 81 write(fileno(communicationPipe_), readyState, sizeof(readyState)); | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 @end | |
| OLD | NEW |