-
Notifications
You must be signed in to change notification settings - Fork 2
/
Control.m
284 lines (246 loc) · 8.69 KB
/
Control.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/** Enterprise Control Configuration and Logging
Copyright (C) 2012 Free Software Foundation, Inc.
Written by: Richard Frith-Macdonald <[email protected]>
Date: Febrary 2010
Originally developed from 1996 to 2012 by Brainstorm, and donated to
the FSF.
This file is part of the GNUstep project.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02111 USA.
*/
#import <Foundation/Foundation.h>
#import "EcProcess.h"
#include <unistd.h>
#include <stdio.h>
#if !defined(EC_DEFAULTS_PREFIX)
#define EC_DEFAULTS_PREFIX nil
#endif
#if !defined(EC_BASE_CLASS)
#define EC_BASE_CLASS EcControl
#endif
/* Create a fake interface to satisfy compiler ...
*/
@interface EC_BASE_CLASS : EcProcess
@end
int
main(int argc, char *argv[])
{
NSString *key = @"";
NSProcessInfo *pInfo;
NSArray *pArgs;
NSString *pName;
NSDictionary *defs;
NSFileHandle *null;
NSData *data;
CREATE_AUTORELEASE_POOL(pool);
pInfo = [NSProcessInfo processInfo];
pArgs = [pInfo arguments];
pName = [pInfo processName];
null = [NSFileHandle fileHandleWithNullDevice];
if (NO == [pArgs containsObject: @"--Watched"])
{
NSMutableArray *args = AUTORELEASE([pArgs mutableCopy]);
NSString *path = [[NSBundle mainBundle] executablePath];
NSEnumerator *dbg;
NSString *key;
NSAutoreleasePool *inner = nil;
BOOL done = NO;
NSUInteger index = NSNotFound;
int status = 0;
NSTask *t;
[args removeObjectAtIndex: 0];
/* Pass debug settings to child
*/
dbg = [[[NSProcessInfo processInfo] debugSet] objectEnumerator];
while (nil != (key = [dbg nextObject]))
{
NSString *arg = [@"--GNU-Debug=" stringByAppendingString: key];
[args addObject: arg];
}
if ([pArgs containsObject: @"--Watcher"] == NO)
{
NSUserDefaults *defs;
NSString *str;
defs = [NSUserDefaults standardUserDefaults];
str = [defs stringForKey: @"EcControlKey"];
if ([str length] == 32 || [str boolValue] == YES)
{
NSData *digest = nil;
if ([str length] == 32)
{
/* Check that the 32 character value is hex digits,
* in which case it should be the MD5 digest of the
* actual key to be entered.
*/
digest = AUTORELEASE([[NSData alloc]
initWithHexadecimalRepresentation: str]);
if ([digest length] != 16)
{
NSLog(@"Bad values specified in EcControlKey... abort");
exit(1);
}
}
key = [EcProcess ecGetKey: "master encryption key"
size: 32
md5: digest];
if (nil == key)
{
NSLog(@"Failed to read master key from terminal ... abort");
exit(1);
}
}
/* In the top level task ... set flags to create a subtask
* to act as a watcher for other tasks, and once that has
* been created, exit to leave it running as a daemon.
*/
[args addObject: @"--Watcher"];
t = [NSTask new];
NS_DURING
{
NSPipe *pipe = [NSPipe pipe];
NSFileHandle *rh = [pipe fileHandleForReading];
NSFileHandle *wh = [pipe fileHandleForWriting];
[t setLaunchPath: path];
[t setArguments: args];
[t setEnvironment: [pInfo environment]];
[t setStandardInput: rh];
[t setStandardOutput: null];
[t launch];
[rh closeFile];
[wh writeData: [key dataUsingEncoding: NSUTF8StringEncoding]];
[wh closeFile];
}
NS_HANDLER
{
NSLog(@"Problem creating %@ subprocess: %@",
pName, localException);
exit(1);
}
NS_ENDHANDLER
[t release];
exit(0);
}
/* This is the watcher ... its subtasks are those which are watched.
* If we are a master key vended by the Control server, it can be
* read on stdin (otherwise we should get an empty data object).
*/
data = [[NSFileHandle fileHandleWithStandardInput] readDataToEndOfFile];
if ([data length] > 0)
{
key = AUTORELEASE([[NSString alloc] initWithData: data
encoding: NSUTF8StringEncoding]);
}
(void)dup2([null fileDescriptor], 0);
/* Set args to tell subtask task not to make itself a daemon
*/
if (EC_DEFAULTS_PREFIX != nil)
{
NSString *str;
str = [NSString stringWithFormat: @"-%@Daemon", EC_DEFAULTS_PREFIX];
index = [args indexOfObject: str];
}
if (NSNotFound == index)
{
index = [args indexOfObject: @"-Daemon"];
}
if (NSNotFound != index)
{
[args replaceObjectAtIndex: index + 1 withObject: @"NO"];
}
else
{
[args addObject: @"-Daemon"];
[args addObject: @"NO"];
}
/* Set args to tell task it is being watched.
*/
[args removeObject: @"--Watcher"];
[args addObject: @"--Watched"];
while (NO == done)
{
DESTROY(inner);
inner = [NSAutoreleasePool new];
t = [[NSTask new] autorelease];
NS_DURING
{
NSPipe *pipe = [NSPipe pipe];
NSFileHandle *rh = [pipe fileHandleForReading];
NSFileHandle *wh = [pipe fileHandleForWriting];
[t setLaunchPath: path];
[t setArguments: args];
[t setEnvironment: [pInfo environment]];
[t setStandardInput: rh];
[t setStandardOutput: null];
[t launch];
[rh closeFile];
[wh writeData: [key dataUsingEncoding: NSUTF8StringEncoding]];
[wh closeFile];
[t waitUntilExit];
if (0 == [t terminationStatus])
{
done = YES;
}
else if (255 == ([t terminationStatus] & 255))
{
/* Probably a restart. try to start after 0.5 seconds
*/
[NSThread sleepForTimeInterval: 0.5];
}
else
{
/* Subprocess died ... try to restart after 30 seconds
*/
[NSThread sleepForTimeInterval: 30.0];
}
}
NS_HANDLER
{
done = YES;
status = 1;
NSLog(@"Problem creating %@ subprocess: %@",
pName, localException);
}
NS_ENDHANDLER
}
DESTROY(inner);
DESTROY(pool);
exit(status);
}
/* Read to end of standard input to get any encryption key we
* should use, then get standard input from the null device.
*/
data = [[NSFileHandle fileHandleWithStandardInput] readDataToEndOfFile];
if ([data length] > 0)
{
key = AUTORELEASE([[NSString alloc] initWithData: data
encoding: NSUTF8StringEncoding]);
}
(void)dup2([null fileDescriptor], 0);
// cmdVersion(@"$Date: 2017-02-23 12:00:00 +0000 (Fri, 23 Feb 2012) $ $Revision: 66052 $");
defs = [NSDictionary dictionaryWithObjectsAndKeys:
@"Command", @"HomeDirectory",
key, @"EcControlKey",
@"YES", @"Daemon",
#if defined(EC_REGISTRATION_DOMAIN)
EC_REGISTRATION_DOMAIN
#endif
nil];
if (nil == [[EC_BASE_CLASS alloc] initWithDefaults: defs])
{
NSLog(@"Unable to create control object.\n");
exit(1);
}
[EcProc ecRun];
RELEASE(pool);
exit(0);
}