This repository has been archived by the owner on Oct 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
RunAsAdmin.m
151 lines (133 loc) · 5.09 KB
/
RunAsAdmin.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
//
// RunAsAdmin.m
// Boot2EFI
//
// Created by Eduard Sionov on 1/16/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "RunAsAdmin.h"
@implementation RunAsAdmin
@synthesize delegate=_delegate;
-(id) init {
// AuthorizationCreate and pass NULL as the initial
// AuthorizationRights set so that the AuthorizationRef gets created
// successfully, and then later call AuthorizationCopyRights to
// determine or extend the allowable rights.
// http://developer.apple.com/qa/qa2001/qa1172.html
self = [super init];
assert(self);
self->_status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment,
kAuthorizationFlagDefaults, &self->_authorizationRef);
if (self->_status != errAuthorizationSuccess)
NSLog(@"Error Creating Initial Authorization: %d", self->_status);
//kAuthorizationRightExecute == "system.privilege.admin";
AuthorizationItem right = {kAuthorizationRightExecute, 0, NULL, 0};
AuthorizationRights rights = {1, &right};
AuthorizationFlags flags = kAuthorizationFlagDefaults |
kAuthorizationFlagInteractionAllowed |
kAuthorizationFlagPreAuthorize |
kAuthorizationFlagExtendRights;
// Call AuthorizationCopyRights to determine or extend the allowable rights.
self->_status = AuthorizationCopyRights(self->_authorizationRef, &rights, NULL, flags, NULL);
if (self->_status != errAuthorizationSuccess) {
NSLog(@"Copy Rights Unsuccessful: %d", self->_status);
} else {
if (self.delegate != nil) {
[self.delegate authorizationSuccess];
}
}
return self;
}
-(RunAsAdmin*) initWithDelegate:(id)delegate {
self->_delegate = delegate;
self = [self init];
assert(self);
return self;
}
-(void) setCommand:(NSString*)command {
self->_command = command;
}
-(void) setArguments:(NSMutableArray*)arguments {
self->_arguments = arguments;
}
-(NSString*)command {
return self->_command;
}
-(NSMutableArray*)arguments {
return self->_arguments;
}
-(void)runCommand {
char * a1;
char * a2;
char * a3;
char * a4;
char * a5;
char * a6;
if ([self->_arguments count] == 0) {
a1 = NULL;
a2 = NULL;
a3 = NULL;
a4 = NULL;
a5 = NULL;
a6 = NULL;
}
if ([self->_arguments count] == 1) {
a1 = (char*)[[self->_arguments objectAtIndex:0] cStringUsingEncoding:NSUTF8StringEncoding];
a2 = NULL;
a3 = NULL;
a4 = NULL;
a5 = NULL;
a6 = NULL;
}
if ([self->_arguments count] == 2) {
a1 = (char*)[[self->_arguments objectAtIndex:0] cStringUsingEncoding:NSUTF8StringEncoding];
a2 = (char*)[[self->_arguments objectAtIndex:1] cStringUsingEncoding:NSUTF8StringEncoding];
a3 = NULL;
a4 = NULL;
a5 = NULL;
a6 = NULL;
}
if ([self->_arguments count] == 3) {
a1 = (char*)[[self->_arguments objectAtIndex:0] cStringUsingEncoding:NSUTF8StringEncoding];
a2 = (char*)[[self->_arguments objectAtIndex:1] cStringUsingEncoding:NSUTF8StringEncoding];
a3 = (char*)[[self->_arguments objectAtIndex:2] cStringUsingEncoding:NSUTF8StringEncoding];
a4 = NULL;
a5 = NULL;
a6 = NULL;
}
if ([self->_arguments count] == 4) {
a1 = (char*)[[self->_arguments objectAtIndex:0] cStringUsingEncoding:NSUTF8StringEncoding];
a2 = (char*)[[self->_arguments objectAtIndex:1] cStringUsingEncoding:NSUTF8StringEncoding];
a3 = (char*)[[self->_arguments objectAtIndex:2] cStringUsingEncoding:NSUTF8StringEncoding];
a4 = (char*)[[self->_arguments objectAtIndex:3] cStringUsingEncoding:NSUTF8StringEncoding];
a5 = NULL;
a6 = NULL;
}
if ([self->_arguments count] == 5) {
a1 = (char*)[[self->_arguments objectAtIndex:0] cStringUsingEncoding:NSUTF8StringEncoding];
a2 = (char*)[[self->_arguments objectAtIndex:1] cStringUsingEncoding:NSUTF8StringEncoding];
a3 = (char*)[[self->_arguments objectAtIndex:2] cStringUsingEncoding:NSUTF8StringEncoding];
a4 = (char*)[[self->_arguments objectAtIndex:3] cStringUsingEncoding:NSUTF8StringEncoding];
a5 = (char*)[[self->_arguments objectAtIndex:4] cStringUsingEncoding:NSUTF8StringEncoding];
a6 = NULL;
}
if ([self->_arguments count] == 6) {
a1 = (char*)[[self->_arguments objectAtIndex:0] cStringUsingEncoding:NSUTF8StringEncoding];
a2 = (char*)[[self->_arguments objectAtIndex:1] cStringUsingEncoding:NSUTF8StringEncoding];
a3 = (char*)[[self->_arguments objectAtIndex:2] cStringUsingEncoding:NSUTF8StringEncoding];
a4 = (char*)[[self->_arguments objectAtIndex:3] cStringUsingEncoding:NSUTF8StringEncoding];
a5 = (char*)[[self->_arguments objectAtIndex:4] cStringUsingEncoding:NSUTF8StringEncoding];
a6 = (char*)[[self->_arguments objectAtIndex:5] cStringUsingEncoding:NSUTF8StringEncoding];
}
char * arg[] = {a1,a2,a3,a4,a5,a6};
//команда, которую надо выполнить
char * tool = (char *)[self->_command cStringUsingEncoding:NSUTF8StringEncoding];
FILE *pipe = NULL;
//выполняем команду с заданными параметрами
self->_status = AuthorizationExecuteWithPrivileges(self->_authorizationRef, tool,
kAuthorizationFlagDefaults, arg, &pipe);
if (self->_status != errAuthorizationSuccess)
NSLog(@"Error: %d ", self->_status);
[self->_arguments removeAllObjects];
}
@end