@@ -246,7 +246,10 @@ - (void)connect
246
246
[debugMessagesLock unlock ];
247
247
taskExitedUnexpectedly = NO ;
248
248
249
- [NSThread detachNewThreadWithName: @" SPSSHTunnel SSH binary communication task" target: self selector: @selector (launchTask: ) object: nil ];
249
+ [NSThread detachNewThreadWithName: @" SPSSHTunnel SSH binary communication task"
250
+ target: self
251
+ selector: @selector (launchTask: )
252
+ object: nil ];
250
253
}
251
254
252
255
/*
@@ -317,7 +320,9 @@ - (void)launchTask:(id) dummy
317
320
318
321
// Prepare to set up the arguments for the task
319
322
taskArguments = [[NSMutableArray alloc ] init ];
320
- #define TA (_name,_value ) [taskArguments addObjectsFromArray: @[_name,_value]]
323
+ void (^TA)(NSString *, NSString *) = ^(NSString *_name, NSString *_value) {
324
+ [taskArguments addObjectsFromArray: @[_name,_value]];
325
+ };
321
326
322
327
// Enable verbose mode for message parsing
323
328
[taskArguments addObject: @" -v" ];
@@ -328,7 +333,6 @@ - (void)launchTask:(id) dummy
328
333
// If explicitly enabled, activate connection multiplexing - note that this can cause connection
329
334
// instability on some setups, so is currently disabled by default.
330
335
if (connectionMuxingEnabled) {
331
-
332
336
// Enable automatic connection muxing/sharing, for faster connections
333
337
TA (@" -o" ,@" ControlMaster=auto" );
334
338
@@ -340,8 +344,8 @@ - (void)launchTask:(id) dummy
340
344
CC_MD5 ([pathString UTF8String ], (unsigned int )strlen ([pathString UTF8String ]), hashedPathResult);
341
345
NSString *hashedString = [[[NSData dataWithBytes: hashedPathResult length: 16 ] dataToHexString ] substringToIndex: 8 ];
342
346
TA (@" -o" ,([NSString stringWithFormat: @" ControlPath=%@ /SPSSH-%@ " , [NSFileManager temporaryDirectory ], hashedString]));
343
- } else {
344
-
347
+ }
348
+ else {
345
349
// Disable muxing if requested
346
350
TA (@" -S" , @" none" );
347
351
TA (@" -o" , @" ControlMaster=no" );
@@ -374,16 +378,17 @@ - (void)launchTask:(id) dummy
374
378
}
375
379
if ([sshLogin length ]) {
376
380
[taskArguments addObject: [NSString stringWithFormat: @" %@ @%@ " , sshLogin, sshHost]];
377
- } else {
381
+ }
382
+ else {
378
383
[taskArguments addObject: sshHost];
379
384
}
380
385
if (useHostFallback) {
381
386
TA (@" -L" ,([NSString stringWithFormat: @" %ld :127.0.0.1:%ld " , (long )localPort, (long )remotePort]));
382
387
TA (@" -L" ,([NSString stringWithFormat: @" %ld :%@ :%ld " , (long )localPortFallback, remoteHost, (long )remotePort]));
383
- } else {
388
+ }
389
+ else {
384
390
TA (@" -L" , ([NSString stringWithFormat: @" %ld :%@ :%ld " , (long )localPort, remoteHost, (long )remotePort]));
385
391
}
386
- #undef TA
387
392
388
393
[task setArguments: taskArguments];
389
394
@@ -412,11 +417,11 @@ - (void)launchTask:(id) dummy
412
417
413
418
// Set up the standard error pipe
414
419
standardError = [[NSPipe alloc ] init ];
415
- [task setStandardError: standardError];
416
- [[ NSNotificationCenter defaultCenter ] addObserver: self
417
- selector: @selector (standardErrorHandler: )
418
- name: NSFileHandleDataAvailableNotification
419
- object: [standardError fileHandleForReading ]];
420
+ [task setStandardError: standardError];
421
+ [[ NSNotificationCenter defaultCenter ] addObserver: self
422
+ selector: @selector (standardErrorHandler: )
423
+ name: NSFileHandleDataAvailableNotification
424
+ object: [standardError fileHandleForReading ]];
420
425
[[standardError fileHandleForReading ] waitForDataInBackgroundAndNotify ];
421
426
422
427
{
@@ -481,9 +486,9 @@ - (void)launchTask:(id) dummy
481
486
// On tunnel close, clean up, ready for re-use if the delegate reconnects.
482
487
SPClear (task);
483
488
SPClear (standardError);
484
- [[NSNotificationCenter defaultCenter ] removeObserver: self
485
- name: @" NSFileHandleDataAvailableNotification"
486
- object: nil ];
489
+ [[NSNotificationCenter defaultCenter ] removeObserver: self
490
+ name: NSFileHandleDataAvailableNotification
491
+ object: nil ];
487
492
488
493
// If the task closed unexpectedly, alert appropriately
489
494
if (connectionState != SPMySQLProxyIdle) {
@@ -497,7 +502,6 @@ - (void)launchTask:(id) dummy
497
502
[[NSRunLoop currentRunLoop ] runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 1.0 ]];
498
503
499
504
SPClear (taskEnvironment);
500
- #undef TA
501
505
SPClear (taskArguments);
502
506
503
507
[pool release ];
@@ -508,7 +512,7 @@ - (void)launchTask:(id) dummy
508
512
*/
509
513
- (void )disconnect
510
514
{
511
- if (connectionState == SPMySQLProxyIdle) return ;
515
+ if (connectionState == SPMySQLProxyIdle) return ;
512
516
513
517
// If there's a delegate set, clear it to prevent unexpected state change messaging
514
518
if (delegate) {
@@ -518,9 +522,9 @@ - (void)disconnect
518
522
519
523
// Before terminating the tunnel, check that it's actually running. This is to accommodate tunnels which
520
524
// suddenly disappear as a result of network disconnections.
521
- if ([task isRunning ]) [task terminate ];
525
+ if ([task isRunning ]) [task terminate ];
522
526
}
523
-
527
+
524
528
/*
525
529
* Processes messages recieved from the SSH task. These may be received singly
526
530
* or several stuck together.
@@ -537,7 +541,7 @@ - (void)standardErrorHandler:(NSNotification*)aNotification
537
541
if ([notificationText length ]) {
538
542
messages = [notificationText componentsSeparatedByString: @" \n " ];
539
543
enumerator = [messages objectEnumerator ];
540
- while ((message = [[enumerator nextObject ] stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet ]])) {
544
+ while ((message = [[enumerator nextObject ] stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet ]])) {
541
545
if (![message length ]) continue ;
542
546
[debugMessagesLock lock ];
543
547
[debugMessages addObject: [NSString stringWithString: message]];
@@ -590,7 +594,7 @@ - (void)standardErrorHandler:(NSNotification*)aNotification
590
594
if (connectionState != SPMySQLProxyIdle) {
591
595
[[standardError fileHandleForReading ] waitForDataInBackgroundAndNotify ];
592
596
}
593
-
597
+
594
598
[notificationText release ];
595
599
}
596
600
@@ -631,27 +635,27 @@ - (NSString *)getPasswordWithVerificationHash:(NSString *)theHash
631
635
*/
632
636
- (BOOL )getResponseForQuestion : (NSString *)theQuestion
633
637
{
634
- // Lock the answer available lock
635
- [[answerAvailableLock onMainThread ] lock ];
636
-
637
- // Request an answer on the main thread (UI stuff must be done on main thread)
638
+ // Lock the answer available lock
639
+ [[answerAvailableLock onMainThread ] lock ];
640
+
641
+ // Request an answer on the main thread (UI stuff must be done on main thread)
638
642
[self performSelectorOnMainThread: @selector (workerGetResponseForQuestion: ) withObject: theQuestion waitUntilDone: YES ];
639
-
640
- // Wait for closeSSHQuestionSheet: to unlock the lock, indicating an answer is available
643
+
644
+ // Wait for closeSSHQuestionSheet: to unlock the lock, indicating an answer is available
641
645
while (![answerAvailableLock tryLock ]) usleep (25000 );
642
-
643
- // Save the answer
644
- BOOL response = requestedResponse;
645
-
646
- // Unlock the lock again
647
- [answerAvailableLock unlock ];
648
-
649
- // Return the answer
646
+
647
+ // Save the answer
648
+ BOOL response = requestedResponse;
649
+
650
+ // Unlock the lock again
651
+ [answerAvailableLock unlock ];
652
+
653
+ // Return the answer
650
654
return response;
651
655
}
652
656
653
657
- (void )workerGetResponseForQuestion : (NSString *)theQuestion
654
- {
658
+ {
655
659
NSSize questionTextSize;
656
660
NSRect windowFrameRect;
657
661
@@ -661,9 +665,13 @@ - (void)workerGetResponseForQuestion:(NSString *)theQuestion
661
665
windowFrameRect = [sshQuestionDialog frame ];
662
666
windowFrameRect.size .height = ((questionTextSize.height < 100 )?100 :questionTextSize.height ) + 70 + ([sshPasswordDialog isSheet ]?0 :22 );
663
667
[sshQuestionDialog setFrame: windowFrameRect display: NO ];
664
-
665
- // show the question window
666
- [NSApp beginSheet: sshQuestionDialog modalForWindow: parentWindow modalDelegate: self didEndSelector: nil contextInfo: nil ];
668
+
669
+ // show the question window
670
+ [NSApp beginSheet: sshQuestionDialog
671
+ modalForWindow: parentWindow
672
+ modalDelegate: nil
673
+ didEndSelector: NULL
674
+ contextInfo: NULL ];
667
675
[parentWindow makeKeyAndOrderFront: self ];
668
676
}
669
677
@@ -672,10 +680,10 @@ - (void)workerGetResponseForQuestion:(NSString *)theQuestion
672
680
*/
673
681
- (IBAction )closeSSHQuestionSheet : (id )sender
674
682
{
675
- requestedResponse = [sender tag ]== 1 ? YES : NO ;
676
- [NSApp endSheet: sshQuestionDialog];
683
+ requestedResponse = [sender tag ] == 1 ? YES : NO ;
684
+ [NSApp endSheet: sshQuestionDialog];
677
685
[sshQuestionDialog orderOut: nil ];
678
- [[answerAvailableLock onMainThread ] unlock ];
686
+ [[answerAvailableLock onMainThread ] unlock ];
679
687
}
680
688
681
689
/*
@@ -688,26 +696,26 @@ - (NSString *)getPasswordForQuery:(NSString *)theQuery verificationHash:(NSStrin
688
696
689
697
if (passwordPromptCancelled) return nil ;
690
698
691
- // Lock the answer available lock
692
- [[answerAvailableLock onMainThread ] lock ];
693
-
694
- // Request password on the main thread (UI stuff must be done on main thread)
699
+ // Lock the answer available lock
700
+ [[answerAvailableLock onMainThread ] lock ];
701
+
702
+ // Request password on the main thread (UI stuff must be done on main thread)
695
703
[self performSelectorOnMainThread: @selector (workerGetPasswordForQuery: ) withObject: theQuery waitUntilDone: YES ];
696
704
697
- // Wait for closeSSHPasswordSheet: to unlock the lock, indicating an answer is available
705
+ // Wait for closeSSHPasswordSheet: to unlock the lock, indicating an answer is available
698
706
while (![answerAvailableLock tryLock ]) usleep (25000 );
699
707
700
- // Save the answer
708
+ // Save the answer
701
709
NSString *thePassword = nil ;
702
- if (requestedPassphrase) {
703
- thePassword = [NSString stringWithString: requestedPassphrase];
704
- SPClear (requestedPassphrase);
705
- }
706
-
707
- // Unlock the lock again
708
- [answerAvailableLock unlock ];
709
-
710
- // Return the answer
710
+ if (requestedPassphrase) {
711
+ thePassword = [NSString stringWithString: requestedPassphrase];
712
+ SPClear (requestedPassphrase);
713
+ }
714
+
715
+ // Unlock the lock again
716
+ [answerAvailableLock unlock ];
717
+
718
+ // Return the answer
711
719
return thePassword;
712
720
}
713
721
@@ -722,12 +730,12 @@ - (void)workerGetPasswordForQuery:(NSString *)theQuery
722
730
if (keyName) {
723
731
[sshPasswordText setStringValue: [NSString stringWithFormat: NSLocalizedString(@" Enter your password for the SSH key\n\" %@ \" " , @" SSH key password prompt" ), keyName]];
724
732
[sshPasswordKeychainCheckbox setHidden: NO ];
725
- currentKeyName = [keyName retain ];
733
+ currentKeyName = [keyName retain ];
726
734
}
727
735
else {
728
736
[sshPasswordText setStringValue: theQuery];
729
737
[sshPasswordKeychainCheckbox setHidden: YES ];
730
- currentKeyName = nil ;
738
+ currentKeyName = nil ;
731
739
}
732
740
733
741
// Request the password, sizing the window appropriately to fit the query
@@ -736,7 +744,11 @@ - (void)workerGetPasswordForQuery:(NSString *)theQuery
736
744
windowFrameRect.size .height = ((queryTextSize.height < 40 )?40 :queryTextSize.height ) + 140 + ([sshPasswordDialog isSheet ]?0 :22 );
737
745
738
746
[sshPasswordDialog setFrame: windowFrameRect display: NO ];
739
- [NSApp beginSheet: sshPasswordDialog modalForWindow: parentWindow modalDelegate: self didEndSelector: nil contextInfo: nil ];
747
+ [NSApp beginSheet: sshPasswordDialog
748
+ modalForWindow: parentWindow
749
+ modalDelegate: nil
750
+ didEndSelector: NULL
751
+ contextInfo: NULL ];
740
752
[parentWindow makeKeyAndOrderFront: self ];
741
753
}
742
754
@@ -745,33 +757,33 @@ - (void)workerGetPasswordForQuery:(NSString *)theQuery
745
757
*/
746
758
- (IBAction )closeSSHPasswordSheet : (id )sender
747
759
{
748
- requestedResponse = [sender tag ]==1 ? YES : NO ;
760
+ requestedResponse = [sender tag ]==1 ? YES : NO ;
749
761
750
762
[NSApp endSheet: sshPasswordDialog];
751
763
[sshPasswordDialog orderOut: nil ];
752
-
753
- if (requestedResponse) {
754
- NSString *thePassword = [NSString stringWithString: [sshPasswordField stringValue ]];
755
- [sshPasswordField setStringValue: @" " ];
756
- if ([delegate respondsToSelector: @selector (undoManager )] && [delegate undoManager ]) {
757
- [[delegate undoManager ] removeAllActionsWithTarget: sshPasswordField];
758
- } else if ([[parentWindow windowController ] document ] && [[[parentWindow windowController ] document ] undoManager ]) {
759
- [[[[parentWindow windowController ] document ] undoManager ] removeAllActionsWithTarget: sshPasswordField];
760
- }
761
- requestedPassphrase = [[NSString alloc ] initWithString: thePassword];
762
-
763
- // Add to keychain if appropriate
764
- if (currentKeyName && [sshPasswordKeychainCheckbox state ] == NSOnState ) {
765
- SPKeychain *keychain = [[SPKeychain alloc ] init ];
766
- [keychain addPassword: thePassword forName: @" SSH" account: currentKeyName withLabel: [NSString stringWithFormat: @" SSH: %@ " , currentKeyName]];
767
- [keychain release ];
768
- SPClear (currentKeyName);
769
- }
770
- }
764
+
765
+ if (requestedResponse) {
766
+ NSString *thePassword = [NSString stringWithString: [sshPasswordField stringValue ]];
767
+ [sshPasswordField setStringValue: @" " ];
768
+ if ([delegate respondsToSelector: @selector (undoManager )] && [delegate undoManager ]) {
769
+ [[delegate undoManager ] removeAllActionsWithTarget: sshPasswordField];
770
+ } else if ([[parentWindow windowController ] document ] && [[[parentWindow windowController ] document ] undoManager ]) {
771
+ [[[[parentWindow windowController ] document ] undoManager ] removeAllActionsWithTarget: sshPasswordField];
772
+ }
773
+ requestedPassphrase = [[NSString alloc ] initWithString: thePassword];
774
+
775
+ // Add to keychain if appropriate
776
+ if (currentKeyName && [sshPasswordKeychainCheckbox state ] == NSOnState ) {
777
+ SPKeychain *keychain = [[SPKeychain alloc ] init ];
778
+ [keychain addPassword: thePassword forName: @" SSH" account: currentKeyName withLabel: [NSString stringWithFormat: @" SSH: %@ " , currentKeyName]];
779
+ [keychain release ];
780
+ SPClear (currentKeyName);
781
+ }
782
+ }
771
783
772
784
if (!requestedPassphrase) passwordPromptCancelled = YES ;
773
-
774
- [[answerAvailableLock onMainThread ] unlock ];
785
+
786
+ [[answerAvailableLock onMainThread ] unlock ];
775
787
}
776
788
777
789
#pragma mark -
@@ -796,14 +808,14 @@ - (void)dealloc
796
808
[answerAvailableLock tryLock ];
797
809
[answerAvailableLock unlock ];
798
810
SPClear (answerAvailableLock);
799
- if (password) SPClear (password);
800
- if (keychainName) SPClear (keychainName);
801
- if (keychainAccount) SPClear (keychainAccount);
802
- if (identityFilePath) SPClear (identityFilePath);
811
+ SPClear (password);
812
+ SPClear (keychainName);
813
+ SPClear (keychainAccount);
814
+ SPClear (identityFilePath);
803
815
804
816
// As this object is not a NSWindowController, use manual top-level nib item management
805
- if (sshQuestionDialog) SPClear (sshQuestionDialog);
806
- if (sshPasswordDialog) SPClear (sshPasswordDialog);
817
+ SPClear (sshQuestionDialog);
818
+ SPClear (sshPasswordDialog);
807
819
808
820
[super dealloc ];
809
821
}
0 commit comments