| 1 |
/* $Id$ */ |
|---|
| 2 |
|
|---|
| 3 |
/* |
|---|
| 4 |
* Copyright (c) 2003-2006 Axel Andersson |
|---|
| 5 |
* All rights reserved. |
|---|
| 6 |
* |
|---|
| 7 |
* Redistribution and use in source and binary forms, with or without |
|---|
| 8 |
* modification, are permitted provided that the following conditions |
|---|
| 9 |
* are met: |
|---|
| 10 |
* 1. Redistributions of source code must retain the above copyright |
|---|
| 11 |
* notice, this list of conditions and the following disclaimer. |
|---|
| 12 |
* 2. Redistributions in binary form must reproduce the above copyright |
|---|
| 13 |
* notice, this list of conditions and the following disclaimer in the |
|---|
| 14 |
* documentation and/or other materials provided with the distribution. |
|---|
| 15 |
* |
|---|
| 16 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
|---|
| 17 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|---|
| 18 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|---|
| 19 |
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
|---|
| 20 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
|---|
| 21 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|---|
| 22 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|---|
| 23 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
|---|
| 24 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
|---|
| 25 |
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|---|
| 26 |
* POSSIBILITY OF SUCH DAMAGE. |
|---|
| 27 |
*/ |
|---|
| 28 |
|
|---|
| 29 |
#import "WCAccountsController.h" |
|---|
| 30 |
#import "WCConfigController.h" |
|---|
| 31 |
#import "WCDashboardController.h" |
|---|
| 32 |
#import "WCLogController.h" |
|---|
| 33 |
#import "WCSettings.h" |
|---|
| 34 |
#import "WCStatusController.h" |
|---|
| 35 |
|
|---|
| 36 |
static WCDashboardController *sharedDashboardController; |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
@interface WCDashboardController(Private) |
|---|
| 40 |
|
|---|
| 41 |
- (void)_update; |
|---|
| 42 |
|
|---|
| 43 |
@end |
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
@implementation WCDashboardController(Private) |
|---|
| 47 |
|
|---|
| 48 |
- (void)_update { |
|---|
| 49 |
BOOL enabled; |
|---|
| 50 |
|
|---|
| 51 |
if([_statusController isRunning]) |
|---|
| 52 |
[_startButton setTitle:WCLS(@"Stop", "Stop button title")]; |
|---|
| 53 |
else |
|---|
| 54 |
[_startButton setTitle:WCLS(@"Start", "Start button title")]; |
|---|
| 55 |
|
|---|
| 56 |
enabled = [self isAuthorized] && [_statusController isAvailable]; |
|---|
| 57 |
[_startButton setEnabled:enabled]; |
|---|
| 58 |
enabled = [self isAuthorized] && [_statusController isAvailable] && [_statusController isRunning]; |
|---|
| 59 |
[_restartButton setEnabled:enabled]; |
|---|
| 60 |
[_reloadButton setEnabled:enabled]; |
|---|
| 61 |
[_registerButton setEnabled:enabled]; |
|---|
| 62 |
[_indexButton setEnabled:enabled]; |
|---|
| 63 |
} |
|---|
| 64 |
|
|---|
| 65 |
@end |
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
@implementation WCDashboardController |
|---|
| 69 |
|
|---|
| 70 |
+ (WCDashboardController *)dashboardController { |
|---|
| 71 |
return sharedDashboardController; |
|---|
| 72 |
} |
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
- (id)init { |
|---|
| 77 |
self = [super init]; |
|---|
| 78 |
|
|---|
| 79 |
sharedDashboardController = self; |
|---|
| 80 |
|
|---|
| 81 |
return self; |
|---|
| 82 |
} |
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
- (void)awakeFromNib { |
|---|
| 87 |
NSArray *tasks; |
|---|
| 88 |
AuthorizationRights rights; |
|---|
| 89 |
unsigned int i; |
|---|
| 90 |
|
|---|
| 91 |
tasks = [NSArray arrayWithObjects: |
|---|
| 92 |
WCExpandWiredPath(@"wiredctl"), |
|---|
| 93 |
@"/usr/bin/touch", |
|---|
| 94 |
@"/bin/mv", |
|---|
| 95 |
@"/bin/rm", |
|---|
| 96 |
@"/usr/bin/openssl", |
|---|
| 97 |
@"/usr/sbin/chown", |
|---|
| 98 |
NULL]; |
|---|
| 99 |
|
|---|
| 100 |
rights.count = [tasks count]; |
|---|
| 101 |
rights.items = (AuthorizationItem *) malloc(rights.count * sizeof(AuthorizationItem)); |
|---|
| 102 |
|
|---|
| 103 |
for(i = 0; i < rights.count; i++) { |
|---|
| 104 |
rights.items[i].name = kAuthorizationRightExecute; |
|---|
| 105 |
rights.items[i].flags = 0; |
|---|
| 106 |
rights.items[i].value = (char *) [[tasks objectAtIndex:i] UTF8String]; |
|---|
| 107 |
rights.items[i].valueLength = strlen(rights.items[i].value); |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
[_authorizationView setAuthorizationRights:&rights]; |
|---|
| 111 |
[_authorizationView setDelegate:self]; |
|---|
| 112 |
[_authorizationView updateStatus:self]; |
|---|
| 113 |
|
|---|
| 114 |
[[NSNotificationCenter defaultCenter] |
|---|
| 115 |
addObserver:self |
|---|
| 116 |
selector:@selector(wiredStatusDidChange:) |
|---|
| 117 |
name:WCWiredStatusDidChange |
|---|
| 118 |
object:NULL]; |
|---|
| 119 |
|
|---|
| 120 |
[[NSNotificationCenter defaultCenter] |
|---|
| 121 |
addObserver:self |
|---|
| 122 |
selector:@selector(configDidChange:) |
|---|
| 123 |
name:WCConfigDidChange |
|---|
| 124 |
object:NULL]; |
|---|
| 125 |
|
|---|
| 126 |
[[NSNotificationCenter defaultCenter] |
|---|
| 127 |
addObserver:self |
|---|
| 128 |
selector:@selector(accountsDidChange:) |
|---|
| 129 |
name:WCAccountsDidChange |
|---|
| 130 |
object:NULL]; |
|---|
| 131 |
} |
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
- (void)awakeFromController { |
|---|
| 136 |
[self _update]; |
|---|
| 137 |
} |
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
#pragma mark - |
|---|
| 142 |
|
|---|
| 143 |
- (void)authorizationViewDidAuthorize:(SFAuthorizationView *)view { |
|---|
| 144 |
_authorized = YES; |
|---|
| 145 |
|
|---|
| 146 |
[self _update]; |
|---|
| 147 |
|
|---|
| 148 |
[[NSNotificationCenter defaultCenter] postNotificationName:WCAuthorizationStatusDidChange object:[NSNumber numberWithBool:_authorized]]; |
|---|
| 149 |
} |
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
- (void)authorizationViewDidDeauthorize:(SFAuthorizationView *)view { |
|---|
| 154 |
_authorized = NO; |
|---|
| 155 |
|
|---|
| 156 |
[self _update]; |
|---|
| 157 |
|
|---|
| 158 |
[[NSNotificationCenter defaultCenter] postNotificationName:WCAuthorizationStatusDidChange object:[NSNumber numberWithBool:_authorized]]; |
|---|
| 159 |
} |
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
- (void)wiredStatusDidChange:(NSNotification *)notification { |
|---|
| 164 |
[self _update]; |
|---|
| 165 |
} |
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 |
- (void)configDidChange:(NSNotification *)notification { |
|---|
| 170 |
if([_statusController isAvailable] && [_statusController isRunning]) |
|---|
| 171 |
[self reload:self]; |
|---|
| 172 |
} |
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
- (void)accountsDidChange:(NSNotification *)notification { |
|---|
| 177 |
if([_statusController isAvailable] && [_statusController isRunning]) |
|---|
| 178 |
[self reload:self]; |
|---|
| 179 |
} |
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 |
#pragma mark - |
|---|
| 184 |
|
|---|
| 185 |
- (NSArray *)launchArguments { |
|---|
| 186 |
NSMutableArray *arguments; |
|---|
| 187 |
|
|---|
| 188 |
arguments = [NSMutableArray array]; |
|---|
| 189 |
|
|---|
| 190 |
switch([[WCSettings objectForKey:WCLogMethod] intValue]) { |
|---|
| 191 |
case WCLogMethodSyslog: |
|---|
| 192 |
[arguments addObject:@"-s"]; |
|---|
| 193 |
[arguments addObject:[WCSettings objectForKey:WCSyslogFacility]]; |
|---|
| 194 |
break; |
|---|
| 195 |
|
|---|
| 196 |
case WCLogMethodFile: |
|---|
| 197 |
[arguments addObject:@"-L"]; |
|---|
| 198 |
[arguments addObject:[[WCSettings objectForKey:WCLogFile] stringByExpandingTildeInPath]]; |
|---|
| 199 |
|
|---|
| 200 |
if([WCSettings boolForKey:WCLimitLogFile]) { |
|---|
| 201 |
[arguments addObject:@"-i"]; |
|---|
| 202 |
[arguments addObject:[NSSWF:@"%u", [WCSettings intForKey:WCLimitLogFileLines]]]; |
|---|
| 203 |
} |
|---|
| 204 |
break; |
|---|
| 205 |
} |
|---|
| 206 |
|
|---|
| 207 |
return arguments; |
|---|
| 208 |
} |
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 |
#pragma mark - |
|---|
| 213 |
|
|---|
| 214 |
- (BOOL)isAuthorized { |
|---|
| 215 |
return _authorized; |
|---|
| 216 |
} |
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 |
- (BOOL)launchTaskWithPath:(NSString *)path arguments:(NSArray *)arguments { |
|---|
| 221 |
NSFileHandle *fileHandle; |
|---|
| 222 |
NSData *data; |
|---|
| 223 |
OSStatus err; |
|---|
| 224 |
FILE *fp = NULL; |
|---|
| 225 |
char **argv; |
|---|
| 226 |
NSUInteger i, argc; |
|---|
| 227 |
int status; |
|---|
| 228 |
pid_t pid; |
|---|
| 229 |
|
|---|
| 230 |
argc = [arguments count]; |
|---|
| 231 |
argv = (char **) malloc(sizeof(char *) * (argc + 1)); |
|---|
| 232 |
|
|---|
| 233 |
for(i = 0; i < argc; i++) |
|---|
| 234 |
argv[i] = (char *) [[[arguments objectAtIndex:i] description] UTF8String]; |
|---|
| 235 |
|
|---|
| 236 |
argv[i] = NULL; |
|---|
| 237 |
|
|---|
| 238 |
err = AuthorizationExecuteWithPrivileges([[_authorizationView authorization] authorizationRef], |
|---|
| 239 |
[path UTF8String], |
|---|
| 240 |
kAuthorizationFlagDefaults, |
|---|
| 241 |
argv, |
|---|
| 242 |
&fp); |
|---|
| 243 |
free(argv); |
|---|
| 244 |
|
|---|
| 245 |
if(err != errAuthorizationSuccess) { |
|---|
| 246 |
NSLog(@"AuthorizationExecuteWithPrivileges: %d", err); |
|---|
| 247 |
|
|---|
| 248 |
return NO; |
|---|
| 249 |
} |
|---|
| 250 |
|
|---|
| 251 |
pid = wait3(&status, WNOHANG, NULL); |
|---|
| 252 |
|
|---|
| 253 |
if(pid > 0) { |
|---|
| 254 |
fileHandle = [[NSFileHandle alloc] initWithFileDescriptor:fileno(fp) closeOnDealloc:NO]; |
|---|
| 255 |
data = [fileHandle availableData]; |
|---|
| 256 |
[[WCLogController logController] log:[NSString stringWithData:data encoding:NSUTF8StringEncoding]]; |
|---|
| 257 |
[fileHandle release]; |
|---|
| 258 |
} |
|---|
| 259 |
|
|---|
| 260 |
fclose(fp); |
|---|
| 261 |
|
|---|
| 262 |
return YES; |
|---|
| 263 |
} |
|---|
| 264 |
|
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 |
- (BOOL)createFileAtPath:(NSString *)path { |
|---|
| 268 |
return [self launchTaskWithPath:@"/usr/bin/touch" arguments:[NSArray arrayWithObject:path]]; |
|---|
| 269 |
} |
|---|
| 270 |
|
|---|
| 271 |
|
|---|
| 272 |
|
|---|
| 273 |
- (BOOL)movePath:(NSString *)fromPath toPath:(NSString *)toPath { |
|---|
| 274 |
NSArray *arguments; |
|---|
| 275 |
|
|---|
| 276 |
arguments = [NSArray arrayWithObjects: |
|---|
| 277 |
@"-f", |
|---|
| 278 |
fromPath, |
|---|
| 279 |
toPath, |
|---|
| 280 |
NULL]; |
|---|
| 281 |
|
|---|
| 282 |
return [self launchTaskWithPath:@"/bin/mv" arguments:arguments]; |
|---|
| 283 |
} |
|---|
| 284 |
|
|---|
| 285 |
|
|---|
| 286 |
|
|---|
| 287 |
- (BOOL)removeFileAtPath:(NSString *)path { |
|---|
| 288 |
NSArray *arguments; |
|---|
| 289 |
|
|---|
| 290 |
arguments = [NSArray arrayWithObjects: |
|---|
| 291 |
@"-rf", |
|---|
| 292 |
path, |
|---|
| 293 |
NULL]; |
|---|
| 294 |
|
|---|
| 295 |
return [self launchTaskWithPath:@"/bin/rm" arguments:arguments]; |
|---|
| 296 |
} |
|---|
| 297 |
|
|---|
| 298 |
|
|---|
| 299 |
|
|---|
| 300 |
- (BOOL)changeOwnerOfPath:(NSString *)path toUser:(NSString *)user group:(NSString *)group { |
|---|
| 301 |
NSArray *arguments; |
|---|
| 302 |
|
|---|
| 303 |
arguments = [NSArray arrayWithObjects: |
|---|
| 304 |
@"-h", |
|---|
| 305 |
[NSSWF:@"%@:%@", user, group], |
|---|
| 306 |
path, |
|---|
| 307 |
NULL]; |
|---|
| 308 |
|
|---|
| 309 |
return [self launchTaskWithPath:@"/usr/sbin/chown" arguments:arguments]; |
|---|
| 310 |
} |
|---|
| 311 |
|
|---|
| 312 |
|
|---|
| 313 |
|
|---|
| 314 |
#pragma mark - |
|---|
| 315 |
|
|---|
| 316 |
- (IBAction)start:(id)sender { |
|---|
| 317 |
NSMutableArray *arguments; |
|---|
| 318 |
|
|---|
| 319 |
arguments = [NSMutableArray array]; |
|---|
| 320 |
|
|---|
| 321 |
if([_statusController isRunning]) |
|---|
| 322 |
[arguments addObject:@"stop"]; |
|---|
| 323 |
else |
|---|
| 324 |
[arguments addObject:@"start"]; |
|---|
| 325 |
|
|---|
| 326 |
[self launchTaskWithPath:WCExpandWiredPath(@"wiredctl") |
|---|
| 327 |
arguments:arguments]; |
|---|
| 328 |
} |
|---|
| 329 |
|
|---|
| 330 |
|
|---|
| 331 |
|
|---|
| 332 |
- (IBAction)restart:(id)sender { |
|---|
| 333 |
NSMutableArray *arguments; |
|---|
| 334 |
|
|---|
| 335 |
arguments = [NSMutableArray array]; |
|---|
| 336 |
[arguments addObject:@"restart"]; |
|---|
| 337 |
|
|---|
| 338 |
[self launchTaskWithPath:WCExpandWiredPath(@"wiredctl") |
|---|
| 339 |
arguments:arguments]; |
|---|
| 340 |
} |
|---|
| 341 |
|
|---|
| 342 |
|
|---|
| 343 |
|
|---|
| 344 |
- (IBAction)reload:(id)sender { |
|---|
| 345 |
[self launchTaskWithPath:WCExpandWiredPath(@"wiredctl") |
|---|
| 346 |
arguments:[NSArray arrayWithObject:@"reload"]]; |
|---|
| 347 |
} |
|---|
| 348 |
|
|---|
| 349 |
|
|---|
| 350 |
|
|---|
| 351 |
- (IBAction)registerWithTracker:(id)sender { |
|---|
| 352 |
[self launchTaskWithPath:WCExpandWiredPath(@"wiredctl") |
|---|
| 353 |
arguments:[NSArray arrayWithObject:@"register"]]; |
|---|
| 354 |
} |
|---|
| 355 |
|
|---|
| 356 |
|
|---|
| 357 |
|
|---|
| 358 |
- (IBAction)indexFiles:(id)sender { |
|---|
| 359 |
[self launchTaskWithPath:WCExpandWiredPath(@"wiredctl") |
|---|
| 360 |
arguments:[NSArray arrayWithObject:@"index"]]; |
|---|
| 361 |
} |
|---|
| 362 |
|
|---|
| 363 |
@end |
|---|