1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "remoting/host/installer/mac/uninstaller/remoting_uninstaller.h"
#import <Cocoa/Cocoa.h>
#include "base/mac/scoped_authorizationref.h"
#include "base/mac/scoped_cftyperef.h"
#include "remoting/host/constants_mac.h"
@implementation RemotingUninstallerAppDelegate
NSString* const kLaunchAgentsDir = @"/Library/LaunchAgents";
NSString* const kPrefPaneDir = @"/Library/PreferencePanes";
NSString* const kHelperToolsDir = @"/Library/PrivilegedHelperTools";
NSString* const kApplicationDir = @"/Applications";
NSString* const kPrefPaneName = @kServiceName ".prefPane";
NSString* const kUninstallerName =
@"Chrome Remote Desktop Host Uninstaller.app";
// Keystone
const char kKeystoneAdmin[] = "/Library/Google/GoogleSoftwareUpdate/"
"GoogleSoftwareUpdate.bundle/Contents/MacOS/"
"ksadmin";
const char kKeystonePID[] = "com.google.chrome_remote_desktop";
- (void)dealloc {
[super dealloc];
}
- (void)applicationDidFinishLaunching:(NSNotification*)aNotification {
}
- (void)logOutput:(FILE*) pipe {
char readBuffer[128];
for (;;) {
long bytesRead = read(fileno(pipe), readBuffer, sizeof(readBuffer) - 1);
if (bytesRead < 1)
break;
readBuffer[bytesRead] = '\0';
NSLog(@"%s", readBuffer);
}
}
- (void)messageBox:(const char*)message {
base::mac::ScopedCFTypeRef<CFStringRef> message_ref(
CFStringCreateWithCString(NULL, message, (int)strlen(message)));
CFOptionFlags result;
CFUserNotificationDisplayAlert(0, kCFUserNotificationNoteAlertLevel,
NULL, NULL, NULL,
CFSTR("Chrome Remote Desktop Uninstaller"),
message_ref, NULL, NULL, NULL, &result);
}
-(void)runCommand:(NSString*)cmd
withArguments:(NSArray*)args {
NSTask* task;
NSPipe* output = [NSPipe pipe];
NSString* result;
NSLog(@"Executing: %@ %@", cmd, [args componentsJoinedByString:@" "]);
@try {
task = [[[NSTask alloc] init] autorelease];
[task setLaunchPath:cmd];
[task setArguments:args];
[task setStandardInput:[NSPipe pipe]];
[task setStandardOutput:output];
[task launch];
NSData* data = [[output fileHandleForReading] readDataToEndOfFile];
[task waitUntilExit];
if ([task terminationStatus] != 0) {
// TODO(garykac): When we switch to sdk_10.6, show the
// [task terminationReason] as well.
NSLog(@"Command terminated status=%d", [task terminationStatus]);
}
result = [[[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding]
autorelease];
if ([result length] != 0) {
NSLog(@"Result: %@", result);
}
}
@catch (NSException* exception) {
NSLog(@"Exception %@ %@", [exception name], [exception reason]);
}
}
- (void)sudoCommand:(const char*)cmd
withArguments:(const char**)args
usingAuth:(AuthorizationRef)authRef {
NSMutableArray* arg_array = [[[NSMutableArray alloc] init] autorelease];
int i = 0;
const char* arg = args[i++];
while (arg != NULL) {
[arg_array addObject:[NSString stringWithUTF8String:arg]];
arg = args[i++];
}
NSLog(@"Executing (as Admin): %s %@", cmd,
[arg_array componentsJoinedByString:@" "]);
FILE* pipe = NULL;
OSStatus status;
status = AuthorizationExecuteWithPrivileges(authRef, cmd,
kAuthorizationFlagDefaults,
(char* const*)args,
&pipe);
if (status == errAuthorizationToolExecuteFailure) {
NSLog(@"Error errAuthorizationToolExecuteFailure");
} else if (status != errAuthorizationSuccess) {
NSLog(@"Error while executing %s. Status=%lx", cmd, status);
} else {
[self logOutput:pipe];
}
if (pipe != NULL)
fclose(pipe);
}
- (void)sudoDelete:(const char*)filename
usingAuth:(AuthorizationRef)authRef {
const char* args[] = { "-rf", filename, NULL };
[self sudoCommand:"/bin/rm" withArguments:args usingAuth:authRef];
}
-(void)shutdownService {
NSString* launchCtl = @"/bin/launchctl";
NSArray* argsStop = [NSArray arrayWithObjects:@"stop",
@kServiceName, nil];
[self runCommand:launchCtl withArguments:argsStop];
NSString* plist = [NSString stringWithFormat:@"%@/%@.plist",
kLaunchAgentsDir, @kServiceName];
if ([[NSFileManager defaultManager] fileExistsAtPath:plist]) {
NSArray* argsUnload = [NSArray arrayWithObjects:@"unload",
@"-w", @"-S", @"Aqua", plist, nil];
[self runCommand:launchCtl withArguments:argsUnload];
}
}
-(void)keystoneUnregisterUsingAuth:(AuthorizationRef)authRef {
const char* args[] = { "--delete", "--productid", kKeystonePID, "-S", NULL };
[self sudoCommand:kKeystoneAdmin withArguments:args usingAuth:authRef];
}
-(void)remotingUninstallUsingAuth:(AuthorizationRef)authRef {
NSString* host_enabled = [NSString stringWithFormat:@"%@/%@.me2me_enabled",
kHelperToolsDir, @kServiceName];
[self sudoDelete:[host_enabled UTF8String] usingAuth:authRef];
[self shutdownService];
NSString* plist = [NSString stringWithFormat:@"%@/%@.plist",
kLaunchAgentsDir, @kServiceName];
[self sudoDelete:[plist UTF8String] usingAuth:authRef];
NSString* host_binary = [NSString stringWithFormat:@"%@/%@.me2me_host.app",
kHelperToolsDir, @kServiceName];
[self sudoDelete:[host_binary UTF8String] usingAuth:authRef];
NSString* host_script = [NSString stringWithFormat:@"%@/%@.me2me.sh",
kHelperToolsDir, @kServiceName];
[self sudoDelete:[host_script UTF8String] usingAuth:authRef];
NSString* auth = [NSString stringWithFormat:@"%@/%@.json",
kHelperToolsDir, @kServiceName];
[self sudoDelete:[auth UTF8String] usingAuth:authRef];
NSString* prefpane = [NSString stringWithFormat:@"%@/%@",
kPrefPaneDir, kPrefPaneName];
[self sudoDelete:[prefpane UTF8String] usingAuth:authRef];
NSString* uninstaller = [NSString stringWithFormat:@"%@/%@",
kApplicationDir, kUninstallerName];
[self sudoDelete:[uninstaller UTF8String] usingAuth:authRef];
[self keystoneUnregisterUsingAuth:authRef];
}
- (IBAction)uninstall:(NSButton*)sender {
base::mac::ScopedAuthorizationRef authRef;
NSLog(@"Chrome Remote Desktop uninstall starting.");
@try {
OSStatus status;
status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment,
kAuthorizationFlagDefaults, &authRef);
if (status != errAuthorizationSuccess) {
[NSException raise:@"AuthorizationCreate Failure"
format:@"Error during AuthorizationCreate status=%ld", status];
}
AuthorizationItem right = {kAuthorizationRightExecute, 0, NULL, 0};
AuthorizationRights rights = {1, &right};
AuthorizationFlags flags = kAuthorizationFlagDefaults |
kAuthorizationFlagInteractionAllowed |
kAuthorizationFlagPreAuthorize |
kAuthorizationFlagExtendRights;
status = AuthorizationCopyRights(authRef, &rights, NULL, flags, NULL);
if (status == errAuthorizationCanceled) {
NSLog(@"Chrome Remote Desktop Host uninstall canceled.");
const char* message = "Chrome Remote Desktop Host uninstall canceled.";
[self messageBox:message];
} else if (status == errAuthorizationSuccess) {
[self remotingUninstallUsingAuth:authRef];
NSLog(@"Chrome Remote Desktop Host uninstall complete.");
const char* message =
"Chrome Remote Desktop Host was successfully uninstalled.";
[self messageBox:message];
} else {
[NSException raise:@"AuthorizationCopyRights Failure"
format:@"Error during AuthorizationCopyRights status=%ld", status];
}
}
@catch (NSException* exception) {
NSLog(@"Exception %@ %@", [exception name], [exception reason]);
const char* message =
"Error! Unable to uninstall Chrome Remote Desktop Host.";
[self messageBox:message];
}
[NSApp terminate:self];
}
- (IBAction)cancel:(id)sender {
[NSApp terminate:self];
}
- (IBAction)handleMenuClose:(NSMenuItem*)sender {
[NSApp terminate:self];
}
@end
int main(int argc, char* argv[])
{
return NSApplicationMain(argc, (const char**)argv);
}
|