-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuthorizedTaskManager.m
executable file
·206 lines (168 loc) · 6.71 KB
/
AuthorizedTaskManager.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
// sshfs.app
// Copyright 2007, Google Inc.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// 3. The name of the author may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#import "AuthorizedTaskManager.h"
@interface AuthorizedTaskManager (PrivateMethods)
- (BOOL)copyRights;
- (int)runTaskForPath:(const char *)path withArguments:(const char **)arguments;
@end
@implementation AuthorizedTaskManager
+ (AuthorizedTaskManager *)sharedAuthorizedTaskManager {
// a simple singleton pattern; replace with something more heavy-duty
// if desired
static AuthorizedTaskManager *manager = nil;
if (!manager)
manager = [[AuthorizedTaskManager alloc] init];
return manager;
}
- (void)dealloc {
[self deauthorize];
[super dealloc];
}
#pragma mark -
// authorize creates and copies admin rights, raising the admin name/password
// dialog if necessary (usually not necessary if this object has been authorized
// recently and not deauthorized)
- (BOOL)authorize {
BOOL isAuthorized = NO;
if (commonAuthorizationRef_) {
isAuthorized = [self copyRights];
} else {
const AuthorizationRights* kNoRightsSpecified = NULL;
OSStatus err = AuthorizationCreate(kNoRightsSpecified, kAuthorizationEmptyEnvironment,
kAuthorizationFlagDefaults, &commonAuthorizationRef_);
if (err == errAuthorizationSuccess) {
isAuthorized = [self copyRights];
}
}
return isAuthorized;
}
// deauthorize dumps any existing authorization. Calling authorize afterwards
// will raise the admin password dialog
- (void)deauthorize {
if (commonAuthorizationRef_) {
AuthorizationFree(commonAuthorizationRef_, kAuthorizationFlagDefaults);
commonAuthorizationRef_ = 0;
}
}
// copyRights actually acquires the admin rights. |commonAuthorizationRef_| must
// have been allocated previously
- (BOOL)copyRights {
NSParameterAssert(commonAuthorizationRef_);
OSStatus err;
const AuthorizationEnvironment* kDefaultAuthEnvironment = NULL;
AuthorizationFlags theFlags = kAuthorizationFlagDefaults
| kAuthorizationFlagPreAuthorize
| kAuthorizationFlagExtendRights
| kAuthorizationFlagInteractionAllowed;
AuthorizationItem theItems = {kAuthorizationRightExecute, 0, NULL, 0};
AuthorizationRights theRights = {1, &theItems};
err = AuthorizationCopyRights(commonAuthorizationRef_, &theRights,
kDefaultAuthEnvironment, theFlags, NULL);
if (err == errAuthorizationSuccess) {
return YES;
}
[self deauthorize];
return NO;
}
- (BOOL)isAuthorized {
return (commonAuthorizationRef_ != 0);
}
// runTaskForPath:withArguments executes the tool at the |path| with |arguments|
//
// The tool's exit value is returned
//
// If |commonAuthorizationRef_| is non-nil, then AuthorizationExecuteWithPrivileges
// is used.
//
// runTaskForPath calls AuthorizationExecuteWithPrivileges if the user has
// authenticated, or calls NSTask if the user has not authenticated
//
// NOTE: when running the task with admin privileges, this waits on any child
// process, since AEWP doesn't tell us the child's pid. This could be fooled
// by any other child process that quits in the window between launch and
// completion of our actual tool.
//
- (int)runTaskForPath:(const char *)path withArguments:(const char **)arguments {
// waitUntilExit polls the run loop, which could end up calling this reentrantly,
// (like through networking callbacks) and that's bad if another caller tries
// to use a file with the same path
static int zReentrancyCount = 0;
assert(zReentrancyCount == 0);// runTaskForPath called reentrantly
zReentrancyCount++;
int result;
if (!commonAuthorizationRef_) {
// non-authorized
// the params are char* since AuthorizationExecuteWithPrivileges needs
// those, but NSTask wants NSStrings, so convert the char*'s to an
// array of NSStrings here
NSMutableArray* argsArray = [NSMutableArray array];
int idx;
for (idx = 0; arguments[idx] != NULL; idx++) {
[argsArray addObject:[NSString stringWithUTF8String:arguments[idx]]];
}
NSString* pathStr = [NSString stringWithUTF8String:path];
NSTask* task = [NSTask launchedTaskWithLaunchPath:pathStr
arguments:argsArray];
[task waitUntilExit];
result = [task terminationStatus];
} else {
// authorized
FILE **kNoPipe = NULL;
AuthorizationFlags myFlags = kAuthorizationFlagDefaults;
result = AuthorizationExecuteWithPrivileges(commonAuthorizationRef_,
path, myFlags, (char *const*) arguments, kNoPipe);
if (result == 0) {
int wait_status;
int pid = wait(&wait_status);
if (pid == -1 || !WIFEXITED(wait_status)) {
result = -1;
} else {
result = WEXITSTATUS(wait_status);
}
}
}
if (zReentrancyCount > 0) {
zReentrancyCount--;
}
return result;
}
// copyPath calls ditto
- (BOOL)copyPath:(NSString *)src toPath:(NSString *)dest {
char taskPath[] = "/usr/bin/ditto";
const char* arguments[] = {
"-rsrcFork", // 0: copy resource forks; --rsrc requires 10.3
NULL, // 1: src path
NULL, // 2: dest path
NULL
};
arguments[1] = [src fileSystemRepresentation];
arguments[2] = [dest fileSystemRepresentation];
int status = [self runTaskForPath:taskPath withArguments:arguments];
return (status == 0);
}
@end