-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathRequestRootWindowController.m
261 lines (194 loc) · 6.23 KB
/
RequestRootWindowController.m
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
250
251
252
253
254
255
256
257
258
259
260
261
//
// RequestRootWindowController.m
// TaskExplorer
//
// Created by Patrick Wardle on 2/6/15.
// Copyright (c) 2015 Objective-See, LLC. All rights reserved.
//
#import "Utilities.h"
#import "AppDelegate.h"
#import "RequestRootWindowController.h"
#import <syslog.h>
@implementation RequestRootWindowController
@synthesize statusMsg;
@synthesize authButton;
@synthesize shouldExit;
@synthesize cancelButton;
//automatically called when nib is loaded
// ->center window
-(void)awakeFromNib
{
//center
[self.window center];
//make auth button one in focus
[self.window makeFirstResponder:self.authButton];
//default to exit app
// ->unset if auth is succesful
self.shouldExit = YES;
return;
}
//automatically invoked when window is loaded
// ->set to white
-(void)windowDidLoad
{
//super
[super windowDidLoad];
//no dark mode?
// make window white
if(YES != isDarkMode())
{
//make white
self.window.backgroundColor = NSColor.whiteColor;
}
//set version sting
//[self.versionLabel setStringValue:[NSString stringWithFormat:@"version: %@", getAppVersion()]];
return;
}
//automatically invoked when window is closing
// ->make ourselves unmodal and possibly exit app
-(void)windowWillClose:(NSNotification *)notification
{
//make un-modal
[[NSApplication sharedApplication] stopModal];
//on errors, cancels
// ->exit app
if(YES == self.shouldExit)
{
//exit
[NSApp terminate:self];
}
return;
}
//invoked when user clicks 'auth' button
// ->auth user, then set XPC service as root/setuid
-(IBAction)authenticate:(id)sender
{
//authorization ref
AuthorizationRef authorizationRef = {0};
//args
const char* installArgs[0x10] = {0};
//status code
OSStatus osStatus = -1;
//path to XPC service
NSString* xpcService = nil;
//hide arrow icon
self.arrowIcon.hidden = YES;
//hide help button
self.helpButton.hidden = YES;
//get path to XPC service
xpcService = getPath2XPC();
if(nil == xpcService)
{
//bail
goto bail;
}
/* first chown as root */
//1st arg: recursive flag
installArgs[0] = "-R";
//2nd arg: group/owner
installArgs[1] = "root:wheel";
//3rd arg: XPC service
installArgs[2] = [xpcService UTF8String];
//end w/ NULL
installArgs[3] = NULL;
//reset text color
self.statusMsg.textColor = NSColor.controlTextColor;
//create authorization ref
osStatus = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &authorizationRef);
if(errAuthorizationSuccess != osStatus)
{
//err msg
syslog(LOG_ERR, "OBJECTIVE-SEE ERROR: AuthorizationCreate() failed with %d", osStatus);
//bail
goto bail;
}
//chown XPC service as r00t
osStatus = AuthorizationExecuteWithPrivileges(authorizationRef, "/usr/sbin/chown", 0, (char* const*)installArgs, NULL);
if(errAuthorizationSuccess != osStatus)
{
//err msg
syslog(LOG_ERR, "OBJECTIVE-SEE ERROR: AuthorizationExecuteWithPrivileges() failed with %d", osStatus);
//set result msg
[self.statusMsg setStringValue: [NSString stringWithFormat:@"error: failed with %d", osStatus]];
//set font to red
self.statusMsg.textColor = [NSColor redColor];
//bail
goto bail;
}
/* then setuid */
//1st arg: recursive flag
installArgs[0] = "-R";
//2nd arg: permissions
// ->note: 4 at front is setuid
installArgs[1] = "4755";
//3rd arg: XPC service
installArgs[2] = [xpcService UTF8String];
//end w/ NULL
installArgs[3] = NULL;
//chmod XPC service w/ setuid
osStatus = AuthorizationExecuteWithPrivileges(authorizationRef, "/bin/chmod", 0, (char* const*)installArgs, NULL);
if(errAuthorizationSuccess != osStatus)
{
//err msg
syslog(LOG_ERR, "OBJECTIVE-SEE ERROR: AuthorizationExecuteWithPrivileges() failed with %d", osStatus);
//set result msg
[self.statusMsg setStringValue: [NSString stringWithFormat:@"error: failed with %d", osStatus]];
//set font to red
self.statusMsg.textColor = [NSColor redColor];
//bail
goto bail;
}
//no exit
self.shouldExit = NO;
//disable auth button
self.authButton.enabled = NO;
//disable cancel button
self.cancelButton.enabled = NO;
//update auth message
self.statusMsg.stringValue = @"...authorization successful!";
//wait a bit
// ->then hide window & kick off action
{
//dispatch, then action
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.50 * NSEC_PER_SEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//close window
[self.window close];
//exec UI actions etc on main thread
dispatch_async(dispatch_get_main_queue(), ^{
//make sure app's key window is (still) front
[((AppDelegate*)[[NSApplication sharedApplication] delegate]).window makeKeyAndOrderFront:self];
//make sure app is (still) front
[NSApp activateIgnoringOtherApps:YES];
//call back into app delegate
// ->kick off task enum, etc
[((AppDelegate*)[[NSApplication sharedApplication] delegate]) go];
});
});
}
//bail
bail:
//free auth ref
if(0 != authorizationRef)
{
//free
AuthorizationFree(authorizationRef, kAuthorizationFlagDestroyRights);
}
return;
}
//invoked when user clicks 'cancel' button
// ->close window, which will trigger app exit
-(IBAction)close:(id)sender
{
//exit
[self.window close];
}
//invoked when user clicks 'help' button
// ->open product's page w/ anchor to help
-(IBAction)help:(id)sender
{
//open URL
// ->invokes user's default browser
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"https://objective-see.com/products/taskexplorer.html#help"]];
return;
}
@end