Changeset 119
- Timestamp:
- 08/22/07 18:55:19 (1 year ago)
- Files:
-
- trunk/AgentController.m (modified) (7 diffs)
- trunk/Controller.m (modified) (5 diffs)
- trunk/Libs/SSHKeychain.h (modified) (2 diffs)
- trunk/Libs/SSHKeychain.m (modified) (7 diffs)
- trunk/SSHKeychain.xcodeproj/project.pbxproj (modified) (2 diffs)
- trunk/TokenController.h (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/AgentController.m
r118 r119 65 65 selector:@selector(agentStatusChange:) name:@"AgentStopped" object:nil]; 66 66 67 [ NSThread detachNewThreadSelector:@selector(checkForScreenSaver:)68 toTarget:self withObject:self];67 [[NSDistributedNotificationCenter defaultCenter] addObserver:self 68 selector:@selector(onScreenSaver:) name:@"com.apple.screensaver.didstart" object:nil]; 69 69 70 70 allKeysOnAgentLock = [[NSLock alloc] init]; … … 144 144 if((status & 1) && ([agent isRunning])) 145 145 { 146 [NSThread detachNewThreadSelector:@selector(addKeysToAgentWithoutInteractionInNewThread) 147 toTarget:self withObject:self]; 146 [keychain addKeysToAgentWithInteraction:NO]; 148 147 } 149 148 } … … 223 222 224 223 [allKeysOnAgentLock unlock]; 225 226 [NSThread detachNewThreadSelector:@selector(addKeysToAgentWithoutInteractionInNewThread) 227 toTarget:self withObject:self]; 224 225 [keychain addKeysToAgentWithInteraction:NO]; 228 226 } 229 227 } … … 282 280 allKeysOnAgent = YES; 283 281 [allKeysOnAgentLock unlock]; 282 283 if ([[NSUserDefaults standardUserDefaults] integerForKey:KeyTimeoutString] > 0) 284 { 285 // Self firing timer with reset 286 [[self class] cancelPreviousPerformRequestsWithTarget: self ]; 287 [self performSelector: @selector(removeKeysFromAgent:) 288 withObject: nil 289 afterDelay: [[NSUserDefaults standardUserDefaults] 290 integerForKey:KeyTimeoutString] * 60.00 ]; 291 } 284 292 285 293 [[Controller sharedController] setStatus:YES]; … … 337 345 && (![keychain addingKeys]) && (status & 1) && ([agent isRunning])) 338 346 { 339 [NSThread detachNewThreadSelector:@selector(addKeysToAgentWithoutInteractionInNewThread) 340 toTarget:self withObject:self]; 347 [keychain addKeysToAgentWithInteraction:NO]; 341 348 } 342 349 … … 466 473 inMainThread:YES]; 467 474 } 468 469 [pool release];470 }471 472 - (void)addKeysToAgentWithoutInteractionInNewThread473 {474 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];475 476 [keychain addKeysToAgentWithInteraction:NO];477 475 478 476 [pool release]; … … 681 679 } 682 680 683 - (void)checkForScreenSaver:(id)object 684 { 685 NSAutoreleasePool *pool; 686 NSTask *task; 687 NSPipe *thePipe; 688 NSString *theOutput; 689 int interval; 690 691 while(1) 692 { 693 pool = [[NSAutoreleasePool alloc] init]; 694 695 if(([[NSUserDefaults standardUserDefaults] integerForKey:OnScreensaverString] > 1) 696 && ([[NSFileManager defaultManager] isExecutableFileAtPath:@"/bin/ps"])) 697 { 698 task = [[[NSTask alloc] init] autorelease]; 699 thePipe = [[[NSPipe alloc] init] autorelease]; 700 701 [task setLaunchPath:@"/bin/ps"]; 702 [task setArguments:[NSArray arrayWithObject:@"wxo command"]]; 703 [task setStandardOutput:thePipe]; 704 705 [task launch]; 706 [task waitUntilExit]; 707 708 /* Put the data from thePipe to theOutput. */ 709 theOutput = [[[NSString alloc] initWithData:[[thePipe fileHandleForReading] readDataToEndOfFile] encoding:NSASCIIStringEncoding] autorelease]; 710 711 if ([theOutput rangeOfString:@"ScreenSaverEngine.app"].location != NSNotFound) 712 { 713 if((([[NSUserDefaults standardUserDefaults] integerForKey:OnScreensaverString] == 2) 714 || ([[NSUserDefaults standardUserDefaults] integerForKey:OnScreensaverString] == 4)) 715 && ([[agent keysOnAgent] count] > 0)) 716 { 717 [object removeKeysFromAgent:nil]; 718 } 719 720 if(([[NSUserDefaults standardUserDefaults] integerForKey:OnScreensaverString] == 3) 721 || ([[NSUserDefaults standardUserDefaults] integerForKey:OnScreensaverString] == 4)) 722 { 723 SecKeychainLockAll(); 724 } 725 726 } 727 } 728 729 interval = [[NSUserDefaults standardUserDefaults] integerForKey:CheckScreensaverIntervalString]; 730 731 if(interval < 5) 732 { 733 interval = 5; 734 } 735 736 if(interval > 100) 737 { 738 interval = 100; 739 } 740 741 sleep(interval); 742 [pool release]; 681 - (void)onScreenSaver:(NSNotification *)notification 682 { 683 if((([[NSUserDefaults standardUserDefaults] integerForKey:OnScreensaverString] == 2) 684 || ([[NSUserDefaults standardUserDefaults] integerForKey:OnScreensaverString] == 4)) 685 && ([[agent keysOnAgent] count] > 0)) 686 { 687 [self removeKeysFromAgent:nil]; 688 } 689 690 if(([[NSUserDefaults standardUserDefaults] integerForKey:OnScreensaverString] == 3) 691 || ([[NSUserDefaults standardUserDefaults] integerForKey:OnScreensaverString] == 4)) 692 { 693 SecKeychainLockAll(); 743 694 } 744 695 } trunk/Controller.m
r118 r119 256 256 } 257 257 258 /* If ~/.MacOSX is a file, instead of a directory, remove it and create a directory.*/258 /* If ~/.MacOSX is a file, log and error and return */ 259 259 else if(isDirectory == NO) 260 260 { 261 [[NSFileManager defaultManager] removeFileAtPath:macOSXDir handler:nil]; 262 [[NSFileManager defaultManager] createDirectoryAtPath:macOSXDir attributes:nil]; 261 NSLog(@"~/.MacOSX is a file, can not create environemnt variables"); 262 return; 263 /* [[NSFileManager defaultManager] removeFileAtPath:macOSXDir handler:nil]; 264 [[NSFileManager defaultManager] createDirectoryAtPath:macOSXDir attributes:nil]; */ 263 265 } 264 266 … … 442 444 443 445 if(keychainStatus & 1) { 444 returnStatus = SecKeychainFindGenericPassword(keychain, strlen(serviceName), serviceName, strlen(accountName), accountName, &passwordLength, (void **)&kcPassword, nil); 446 returnStatus = SecKeychainFindGenericPassword( 447 keychain, strlen(serviceName), serviceName, 448 strlen(accountName), accountName, &passwordLength, 449 (void **)&kcPassword, nil); 445 450 446 451 if(returnStatus == 0) { … … 455 460 else 456 461 { 457 returnStatus = SecKeychainFindGenericPassword(nil, strlen(serviceName), serviceName, strlen(accountName), accountName, &passwordLength, (void **)&kcPassword, nil); 462 returnStatus = SecKeychainFindGenericPassword( 463 nil, strlen(serviceName), serviceName, strlen(accountName), 464 accountName, &passwordLength, (void **)&kcPassword, nil); 458 465 } 459 466 … … 466 473 if(returnStatus == 0) 467 474 { 468 kcPassword[passwordLength] = '\0'; 469 470 NSString *returnString = [NSString stringWithCString:kcPassword]; 471 472 SecKeychainItemFreeContent(NULL, kcPassword); 475 NSString *returnString; 476 477 if ( kcPassword[passwordLength] != 0 ) { 478 /* Don't trust memory allocated from system, copy it over 479 First before making it a CString */ 480 481 NSLog(@"Buggy password in keycahin workaround"); 482 char * buffer = (char*)malloc((passwordLength+1)*sizeof(char)); 483 strncpy(buffer, kcPassword, passwordLength); 484 buffer[passwordLength] = '\0'; 485 486 487 returnString = [NSString stringWithUTF8String:buffer]; 488 489 SecKeychainItemFreeContent(NULL, kcPassword); 490 free(buffer); 491 } else { 492 returnString = [NSString stringWithUTF8String:kcPassword]; 493 494 SecKeychainItemFreeContent(NULL, kcPassword); 495 } 473 496 474 497 return returnString; … … 542 565 serviceName = "SSHKeychain"; 543 566 544 SecKeychainAddGenericPassword(nil, strlen(serviceName), serviceName, strlen(accountName), accountName, [passphrase length], (const void *)[passphrase UTF8String], nil); 567 const char * utf8password = [passphrase UTF8String]; 568 569 SecKeychainAddGenericPassword(nil, strlen(serviceName), 570 serviceName, strlen(accountName), accountName, 571 strlen(utf8password) + 1, 572 (const void *)utf8password, nil); 545 573 } 546 574 trunk/Libs/SSHKeychain.h
r91 r119 10 10 BOOL addingKeys; 11 11 12 int lastScheduled;13 14 12 /* Locks */ 15 13 NSLock *keychainLock; 16 14 NSLock *addingKeysLock; 17 NSLock *lastScheduledLock;18 15 } 19 16 … … 40 37 - (BOOL)addKeysToAgent; 41 38 - (BOOL)addKeysToAgentWithInteraction:(BOOL)interaction; 42 - (void)removeKeysAfterTimeout:(id)object;43 39 - (BOOL)removeKeysFromAgent; 44 40 trunk/Libs/SSHKeychain.m
r118 r119 5 5 #import "SSHKey.h" 6 6 #import "SSHTool.h" 7 #import "SSHAgent.h" 7 8 8 9 #include <unistd.h> … … 34 35 keychainLock = [[NSLock alloc] init]; 35 36 addingKeysLock = [[NSLock alloc] init]; 36 lastScheduledLock = [[NSLock alloc] init];37 lastScheduled = -1;38 37 39 38 [self resetToKeysWithPaths:paths]; … … 52 51 [keychainLock release]; 53 52 [addingKeysLock release]; 54 [lastScheduledLock release];55 53 [agentSocketPath release]; 56 54 … … 110 108 } 111 109 112 - (int) lastScheduled113 {114 [lastScheduledLock lock];115 int returnInt = lastScheduled;116 [lastScheduledLock unlock];117 118 return returnInt;119 }120 121 - (void) setLastScheduled:(int) scheduledTime122 {123 [lastScheduledLock lock];124 lastScheduled = scheduledTime;125 [lastScheduledLock unlock];126 }127 128 110 /* Returns the SSHKey at Index nr. */ 129 111 - (SSHKey *)keyAtIndex:(int)nr … … 209 191 return YES; 210 192 211 if (! agentSocketPath || ![[NSFileManager defaultManager] isReadableFileAtPath:agentSocketPath])193 if (![[SSHAgent currentAgent] isRunning]) 212 194 return NO; 213 195 … … 255 237 } 256 238 257 if ([[NSUserDefaults standardUserDefaults] integerForKey:KeyTimeoutString] > 0)258 {259 int timeScheduled = time(nil);260 [self setLastScheduled:timeScheduled];261 262 [NSThread detachNewThreadSelector:@selector(removeKeysAfterTimeout:) toTarget:self263 withObject:[NSNumber numberWithInt:timeScheduled]];264 }265 266 239 [[NSNotificationCenter defaultCenter] postNotificationName:@"AgentFilled" object:nil]; 267 240 268 241 [self setAddingKeys:NO]; 269 242 return YES; 270 }271 272 /* Remove all keys from the ssh-agent from a NSTimer object. */273 - (void)removeKeysAfterTimeout:(id)object274 {275 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];276 int timeScheduled = [object intValue];277 278 sleep([[NSUserDefaults standardUserDefaults] integerForKey:KeyTimeoutString] * 60);279 280 /* If the time this timeout was scheduled is still the most recent, go ahead and remove the keys */281 if (timeScheduled == [self lastScheduled])282 [self removeKeysFromAgent];283 284 [pool release];285 243 } 286 244 … … 290 248 SSHTool *theTool = [SSHTool toolWithName:@"ssh-add"]; 291 249 292 [self setLastScheduled:-1]; 293 294 if (!agentSocketPath || ![[NSFileManager defaultManager] isReadableFileAtPath:agentSocketPath]) 250 if (![[SSHAgent currentAgent] isRunning]) 295 251 return NO; 296 252 trunk/SSHKeychain.xcodeproj/project.pbxproj
r118 r119 79 79 CCFAAB480C7C870900AD9093 /* SSHToken.m in Sources */ = {isa = PBXBuildFile; fileRef = CCFAAB460C7C870900AD9093 /* SSHToken.m */; }; 80 80 /* End PBXBuildFile section */ 81 82 /* Begin PBXBuildStyle section */83 CCE37CF00C606DDA0073E776 /* Development */ = {84 isa = PBXBuildStyle;85 buildSettings = {86 COPY_PHASE_STRIP = NO;87 };88 name = Development;89 };90 CCE37CF10C606DDA0073E776 /* Deployment */ = {91 isa = PBXBuildStyle;92 buildSettings = {93 COPY_PHASE_STRIP = YES;94 };95 name = Deployment;96 };97 /* End PBXBuildStyle section */98 81 99 82 /* Begin PBXContainerItemProxy section */ … … 498 481 isa = PBXProject; 499 482 buildConfigurationList = CC39D6370921118A00FE3BC5 /* Build configuration list for PBXProject "SSHKeychain" */; 500 buildSettings = {501 };502 buildStyles = (503 CCE37CF00C606DDA0073E776 /* Development */,504 CCE37CF10C606DDA0073E776 /* Deployment */,505 );506 483 hasScannedForEncodings = 1; 507 484 knownRegions = ( trunk/TokenController.h
r118 r119 1 1 #import <Cocoa/Cocoa.h> 2 #import " SSHToken.h"2 #import "Libs/SSHToken.h" 3 3 #import "SSHTool.h" 4 4
