Use a function to do right check

This commit is contained in:
Anakin Zheng
2014-03-27 12:38:43 -04:00
parent 3dbea4bd98
commit b47bbf0177

View File

@ -34,6 +34,7 @@
+ (void)copyMachinesIfNecessary; + (void)copyMachinesIfNecessary;
- (BOOL)isInAutoStart; - (BOOL)isInAutoStart;
- (void)setStartAtLogin:(BOOL)enabled; - (void)setStartAtLogin:(BOOL)enabled;
+ (void)checkRightStatus:(OSStatus)status;
@end @end
@implementation FanControl @implementation FanControl
@ -730,6 +731,20 @@ NSUserDefaults *defaults;
LSSharedFileListItemRemove(loginItems, existingItem); LSSharedFileListItemRemove(loginItems, existingItem);
} }
+(void) checkRightStatus:(OSStatus) status
{
if (status != errAuthorizationSuccess) {
NSAlert *alert = [NSAlert alertWithMessageText:@"Authorization failed" defaultButton:@"Quit" alternateButton:nil otherButton:nil informativeTextWithFormat:[NSString stringWithFormat:@"Authorization failed with code %d",status]];
[alert setAlertStyle:2];
NSInteger result = [alert runModal];
if (result == NSAlertDefaultReturn) {
[[NSApplication sharedApplication] terminate:self];
}
}
}
#pragma mark **SMC-Binary Owner/Right Check** #pragma mark **SMC-Binary Owner/Right Check**
//TODO: It looks like this function is called inefficiently. //TODO: It looks like this function is called inefficiently.
//call smc binary with sudo rights and apply //call smc binary with sudo rights and apply
@ -748,15 +763,9 @@ NSUserDefaults *defaults;
AuthorizationRights gencright = { 1, &gencitem }; AuthorizationRights gencright = { 1, &gencitem };
int flags = kAuthorizationFlagExtendRights | kAuthorizationFlagInteractionAllowed; int flags = kAuthorizationFlagExtendRights | kAuthorizationFlagInteractionAllowed;
OSStatus status = AuthorizationCreate(&gencright, kAuthorizationEmptyEnvironment, flags, &authorizationRef); OSStatus status = AuthorizationCreate(&gencright, kAuthorizationEmptyEnvironment, flags, &authorizationRef);
if (status != errAuthorizationSuccess) {
NSAlert *alert = [NSAlert alertWithMessageText:@"Authorization failed" defaultButton:@"Quit" alternateButton:nil otherButton:nil informativeTextWithFormat:[NSString stringWithFormat:@"Authorization failed with code %d",status]];
[alert setAlertStyle:2];
NSInteger result = [alert runModal];
if (result == NSAlertDefaultReturn) { [self checkRightStatus:status];
[[NSApplication sharedApplication] terminate:self];
}
}
NSString *tool=@"/usr/sbin/chown"; NSString *tool=@"/usr/sbin/chown";
NSArray *argsArray = [NSArray arrayWithObjects: @"root:admin",smcpath,nil]; NSArray *argsArray = [NSArray arrayWithObjects: @"root:admin",smcpath,nil];
int i; int i;
@ -766,15 +775,9 @@ NSUserDefaults *defaults;
} }
args[i] = NULL; args[i] = NULL;
status=AuthorizationExecuteWithPrivileges(authorizationRef,[tool UTF8String],0,args,&commPipe); status=AuthorizationExecuteWithPrivileges(authorizationRef,[tool UTF8String],0,args,&commPipe);
if (status != errAuthorizationSuccess) {
NSAlert *alert = [NSAlert alertWithMessageText:@"Authorization failed" defaultButton:@"Quit" alternateButton:nil otherButton:nil informativeTextWithFormat:[NSString stringWithFormat:@"Authorization failed with code %d",status]];
[alert setAlertStyle:2];
NSInteger result = [alert runModal];
if (result == NSAlertDefaultReturn) { [self checkRightStatus:status];
[[NSApplication sharedApplication] terminate:self];
}
}
//second call for suid-bit //second call for suid-bit
tool=@"/bin/chmod"; tool=@"/bin/chmod";
argsArray = [NSArray arrayWithObjects: @"6555",smcpath,nil]; argsArray = [NSArray arrayWithObjects: @"6555",smcpath,nil];
@ -783,15 +786,8 @@ NSUserDefaults *defaults;
} }
args[i] = NULL; args[i] = NULL;
status=AuthorizationExecuteWithPrivileges(authorizationRef,[tool UTF8String],0,args,&commPipe); status=AuthorizationExecuteWithPrivileges(authorizationRef,[tool UTF8String],0,args,&commPipe);
if (status != errAuthorizationSuccess) {
NSAlert *alert = [NSAlert alertWithMessageText:@"Authorization failed" defaultButton:@"Quit" alternateButton:nil otherButton:nil informativeTextWithFormat:[NSString stringWithFormat:@"Authorization failed with code %d",status]];
[alert setAlertStyle:2];
NSInteger result = [alert runModal];
if (result == NSAlertDefaultReturn) { [self checkRightStatus:status];
[[NSApplication sharedApplication] terminate:self];
}
}
} }