From b47bbf0177e5e8338c0e254f5b233c863f65f817 Mon Sep 17 00:00:00 2001 From: Anakin Zheng Date: Thu, 27 Mar 2014 12:38:43 -0400 Subject: [PATCH 1/3] Use a function to do right check --- Classes/FanControl.m | 58 +++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 31 deletions(-) diff --git a/Classes/FanControl.m b/Classes/FanControl.m index ffb9713..9981073 100755 --- a/Classes/FanControl.m +++ b/Classes/FanControl.m @@ -31,9 +31,10 @@ #import "SystemVersion.h" @interface FanControl () -+(void)copyMachinesIfNecessary; ++ (void)copyMachinesIfNecessary; - (BOOL)isInAutoStart; -- (void) setStartAtLogin:(BOOL)enabled; +- (void)setStartAtLogin:(BOOL)enabled; ++ (void)checkRightStatus:(OSStatus)status; @end @implementation FanControl @@ -730,6 +731,20 @@ NSUserDefaults *defaults; 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** //TODO: It looks like this function is called inefficiently. //call smc binary with sudo rights and apply @@ -748,16 +763,10 @@ NSUserDefaults *defaults; AuthorizationRights gencright = { 1, &gencitem }; int flags = kAuthorizationFlagExtendRights | kAuthorizationFlagInteractionAllowed; 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) { - [[NSApplication sharedApplication] terminate:self]; - } - } - NSString *tool=@"/usr/sbin/chown"; + + [self checkRightStatus:status]; + + NSString *tool=@"/usr/sbin/chown"; NSArray *argsArray = [NSArray arrayWithObjects: @"root:admin",smcpath,nil]; int i; char *args[255]; @@ -766,16 +775,10 @@ NSUserDefaults *defaults; } args[i] = NULL; 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) { - [[NSApplication sharedApplication] terminate:self]; - } - } - //second call for suid-bit + + [self checkRightStatus:status]; + + //second call for suid-bit tool=@"/bin/chmod"; argsArray = [NSArray arrayWithObjects: @"6555",smcpath,nil]; for(i = 0;i < [argsArray count];i++){ @@ -783,15 +786,8 @@ NSUserDefaults *defaults; } args[i] = NULL; 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) { - [[NSApplication sharedApplication] terminate:self]; - } - } + + [self checkRightStatus:status]; } From 1eae87aede6840bf83459e47ee0668053a379e95 Mon Sep 17 00:00:00 2001 From: Anakin Zheng Date: Mon, 19 May 2014 16:22:44 -0400 Subject: [PATCH 2/3] Use Xcode comment syntax, introduce objective-c modern syntax and some other code rearrange --- Classes/NSFileManager+DirectoryLocations.m | 139 +++++++++------------ 1 file changed, 56 insertions(+), 83 deletions(-) diff --git a/Classes/NSFileManager+DirectoryLocations.m b/Classes/NSFileManager+DirectoryLocations.m index 4de2c36..a5f7cab 100644 --- a/Classes/NSFileManager+DirectoryLocations.m +++ b/Classes/NSFileManager+DirectoryLocations.m @@ -31,120 +31,93 @@ NSString * const DirectoryLocationDomain = @"DirectoryLocationDomain"; @implementation NSFileManager (DirectoryLocations) -// -// findOrCreateDirectory:inDomain:appendPathComponent:error: -// -// Method to tie together the steps of: -// 1) Locate a standard directory by search path and domain mask -// 2) Select the first path in the results -// 3) Append a subdirectory to that path -// 4) Create the directory and intermediate directories if needed -// 5) Handle errors by emitting a proper NSError object -// -// Parameters: -// searchPathDirectory - the search path passed to NSSearchPathForDirectoriesInDomains -// domainMask - the domain mask passed to NSSearchPathForDirectoriesInDomains -// appendComponent - the subdirectory appended -// errorOut - any error from file operations -// -// returns the path to the directory (if path found and exists), nil otherwise -// + +/*! Method to tie together the steps of: + 1) Locate a standard directory by search path and domain mask + 2) Select the first path in the results + 3) Append a subdirectory to that path + 4) Create the directory and intermediate directories if needed + 5) Handle errors by emitting a proper NSError object + +* \pararm searchPathDirectory - the search path passed to NSSearchPathForDirectoriesInDomains +* \pararm domainMask - the domain mask passed to NSSearchPathForDirectoriesInDomains +* \pararm appendComponent - the subdirectory appended +* \pararm errorOut - any error from file operations + +* \returns returns the path to the directory (if path found and exists), nil otherwise +*/ - (NSString *)findOrCreateDirectory:(NSSearchPathDirectory)searchPathDirectory inDomain:(NSSearchPathDomainMask)domainMask appendPathComponent:(NSString *)appendComponent error:(NSError **)errorOut { + // Declare an NSError first, so we don't need to check errorOut again and again + NSError *error; + + if (errorOut) { + error = *errorOut; + } + else { + error = nil; + } + // // Search for the path // - NSArray* paths = NSSearchPathForDirectoriesInDomains( - searchPathDirectory, - domainMask, - YES); + NSArray* paths = NSSearchPathForDirectoriesInDomains(searchPathDirectory,domainMask,YES); + if ([paths count] == 0) { - if (errorOut) - { - NSDictionary *userInfo = - [NSDictionary dictionaryWithObjectsAndKeys: - NSLocalizedStringFromTable( - @"No path found for directory in domain.", - @"Errors", - nil), - NSLocalizedDescriptionKey, - [NSNumber numberWithInteger:searchPathDirectory], - @"NSSearchPathDirectory", - [NSNumber numberWithInteger:domainMask], - @"NSSearchPathDomainMask", - nil]; - *errorOut = - [NSError - errorWithDomain:DirectoryLocationDomain - code:DirectoryLocationErrorNoPathFound - userInfo:userInfo]; - } + NSDictionary *userInfo = @{NSLocalizedDescriptionKey: NSLocalizedStringFromTable(@"No path found for directory in domain.",@"Errors",nil), + @"NSSearchPathDirectory":@(searchPathDirectory), + @"NSSearchPathDomainMask":@(domainMask)}; + + error = [NSError errorWithDomain:DirectoryLocationDomain + code:DirectoryLocationErrorNoPathFound + userInfo:userInfo]; return nil; } // // Normally only need the first path returned // - NSString *resolvedPath = [paths objectAtIndex:0]; + NSString *resolvedPath = paths[0]; // // Append the extra path component // if (appendComponent) { - resolvedPath = [resolvedPath - stringByAppendingPathComponent:appendComponent]; + resolvedPath = [resolvedPath stringByAppendingPathComponent:appendComponent]; } // // Create the path if it doesn't exist // - NSError *error = nil; - BOOL success = [self - createDirectoryAtPath:resolvedPath - withIntermediateDirectories:YES - attributes:nil - error:&error]; - if (!success) - { - if (errorOut) - { - *errorOut = error; - } - return nil; - } - - // - // If we've made it this far, we have a success - // - if (errorOut) - { - *errorOut = nil; - } - return resolvedPath; + + + if ([self createDirectoryAtPath:resolvedPath withIntermediateDirectories:YES + attributes:nil error:&error]) + return resolvedPath; + else + return nil; } -// -// applicationSupportDirectory -// -// Returns the path to the applicationSupportDirectory (creating it if it doesn't -// exist). -// + +/*! applicationSupportDirectory + +* \returns The path to the applicationSupportDirectory (creating it if it doesn't exist). +*/ - (NSString *)applicationSupportDirectory { - NSString *executableName = - [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleExecutable"]; - NSError *error; - NSString *result = - [self - findOrCreateDirectory:NSApplicationSupportDirectory - inDomain:NSUserDomainMask - appendPathComponent:executableName - error:&error]; + NSString *executableName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleExecutable"]; + + NSError *error = nil; + + NSString *result = [self findOrCreateDirectory:NSApplicationSupportDirectory + inDomain:NSUserDomainMask + appendPathComponent:executableName + error:&error]; if (!result) { NSLog(@"Unable to find or create application support directory:\n%@", error); From 93c7f15214a15932f3ff87fd92a4388c74d98efa Mon Sep 17 00:00:00 2001 From: Anakin Zheng Date: Mon, 19 May 2014 22:50:16 -0400 Subject: [PATCH 3/3] Use codesign verification to do dmc check --- Classes/smcWrapper.m | 37 ++++++++++++++++--------- smcFanControl.xcodeproj/project.pbxproj | 8 +++--- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/Classes/smcWrapper.m b/Classes/smcWrapper.m index 15d6e00..0477d31 100755 --- a/Classes/smcWrapper.m +++ b/Classes/smcWrapper.m @@ -24,8 +24,6 @@ #import "smcWrapper.h" #import -//TODO: This is the smcFanControl 2.5ß checksum, it needs to be updated for the next release. -NSString * const smc_checksum=@"03548c5634bd01315b19c46bf329cceb"; static NSArray *allSensors = nil; @@ -153,26 +151,39 @@ static NSArray *allSensors = nil; } -+ (NSString*)createCheckSum:(NSString*)path { - NSData *d=[NSData dataWithContentsOfMappedFile:path]; - unsigned char result[CC_MD5_DIGEST_LENGTH]; - CC_MD5((void *)[d bytes], [d length], result); - NSMutableString *ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH*2]; - int i; - for(i = 0; i