Code Search for Developers
 
 
  

SSHPassKeyController.m from redshed at Krugle


Show SSHPassKeyController.m syntax highlighted

#import "SSHPassKeyController.h"
#import <CoreServices/CoreServices.h>
#import <Carbon/Carbon.h>

@implementation SSHPassKeyController
OSStatus SSHPassKeyKCCallback ( KCEvent keychainEvent, KCCallbackInfo *info, void *userContext ) {
    SSHPassKeyController *controller = (SSHPassKeyController *) userContext;

    return [controller handleKCCallbackEvent: keychainEvent withInfo: info];
}

- (void) parseRequest
{
    NSArray *atSplit = [requestedPassword componentsSeparatedByString: @"@"];
    NSArray *quoteSplit;
    
    if ([atSplit count] == 1) {
        serviceName = [requestedPassword retain];
        [accountName release]; accountName = nil;
        return;
    }

    accountName = [[atSplit objectAtIndex: 0] retain];
    quoteSplit = [[[atSplit subarrayWithRange: NSMakeRange(1, [atSplit count] - 1)] componentsJoinedByString: @"@"] componentsSeparatedByString: @"'"];
    if ([quoteSplit count] != 2) {
        serviceName = [[quoteSplit componentsJoinedByString: @"'"] retain];
        return;
    }

    serviceName = [[quoteSplit objectAtIndex: 0] retain];
}

- (void)applicationDidFinishLaunching:(NSNotification *)notification;
{
    NSProcessInfo *processInfo = [NSProcessInfo processInfo];
    NSArray *args = [processInfo arguments];
    NSString *password;
    OSStatus coreStatus;

    [requestedPassword release];
    requestedPassword = nil;

    if ([args count] == 2) {
        requestedPassword = [[args objectAtIndex: 1] retain];
        if ([requestedPassword hasPrefix: @"-psn_"]) {
            [requestedPassword release];
            requestedPassword = nil;
        }
    }

    if (requestedPassword == nil) {
        [NSException raise: NSInternalInconsistencyException
                    format: @"Application should have displayed info panel.  It failed to do so.  Why? (%@)", args];
    }

    [self parseRequest];

    kcCallbackFuncUPP = NewKCCallbackUPP(SSHPassKeyKCCallback);
    coreStatus = KCAddCallback(kcCallbackFuncUPP, kAddKCEventMask | kUpdateKCEventMask | kPasswordChangedKCEventMask, self);
    switch (coreStatus) {
        case 0: break;
        case errKCDuplicateCallback:
            [NSException raise: NSInternalInconsistencyException
                        format: @"Callback function already registered.  How could that happen??"];
            break;
        default:
            [NSException raise: NSInternalInconsistencyException
                        format: @"Unknown core error code: %d", coreStatus];            
    }

    password = [self obtainPassword: YES];
    if (password != nil) {
        fprintf(stdout, "%s\n", [password cString]);
        [[NSApplication sharedApplication] terminate:self];
    }
}

- (void)applicationWillTerminate:(NSNotification *)notification;
{
    KCRemoveCallback(kcCallbackFuncUPP);
}

- (OSStatus) handleKCCallbackEvent: (KCEvent) keyChainEvent withInfo: (KCCallbackInfo *)info;
{
    NSString *possiblePassword = [self obtainPassword: NO];
    if (possiblePassword != nil) {
        fprintf(stdout, "%s\n", [possiblePassword cString]);
        [[NSApplication sharedApplication] terminate:self];
    }
    return 0;
}

- (Str255 *) pascalStringFromString: (NSString *) aString
{
    if (aString) {
        Str255 *returnString = malloc(sizeof(Str255));
        if ( !CFStringGetPascalString( (CFStringRef) aString, *returnString, sizeof(*returnString), CFStringGetSystemEncoding())) {
            free(returnString);
            [NSException raise: NSInternalInconsistencyException
                        format: @"Failed to convert NSString '%@' to pascal string.", aString];
        }

        return returnString;
    }
    return NULL;
}

- (NSString *) obtainPassword: (BOOL) bringWindowForward;
{
    OSStatus coreStatus;
    Str255 *serviceNamePtr;
    Str255 *accountNamePtr;
    unsigned char password[4001];
    UInt32 actualPasswordLength;

    serviceNamePtr = [self pascalStringFromString: serviceName];
    accountNamePtr = [self pascalStringFromString: accountName];
    
    coreStatus = KCFindGenericPassword(*serviceNamePtr, *accountNamePtr, sizeof(password)-1, &password, &actualPasswordLength, NULL);

    free(serviceNamePtr);
    free(accountNamePtr);

    switch(coreStatus) {
        case 0:
            break;
        case errKCItemNotFound:
            if (bringWindowForward) {
                [requestTextField setStringValue: requestedPassword];
                [[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
                [panel makeKeyAndOrderFront:self];
            }
            return nil;
            break;
        case errKCNoDefaultKeychain:
            [NSException raise: NSInternalInconsistencyException
                        format: @"No default keychain found. (Request was %@).", requestedPassword];
            break;
        case errKCBufferTooSmall:
            [NSException raise: NSInternalInconsistencyException
                        format: @"Keychain claims password is larger than %d bytes.  Do you normally type a novel for each password request (%@)?", sizeof(password) - 1, requestedPassword];
            break;
    }

    password[actualPasswordLength] = '\0';

    return [NSString stringWithCString:(char*)password];
}


- (IBAction)cancelAction:(id)sender
{
    [[NSApplication sharedApplication] terminate:self];
}

- (IBAction)okAction:(id)sender
{
    NSString *passwordString = [passwordSecureTextField stringValue];
    OSStatus coreStatus;
    const char *password;
    Str255 *serviceNamePtr;
    Str255 *accountNamePtr;

    if ( (!passwordString) || ([passwordString length] == 0) )
        return;

    
    password = [passwordString cString];

    if ([storeInKeychainCheckbox state] == NSOnState) {
        serviceNamePtr = [self pascalStringFromString: serviceName];
        accountNamePtr = [self pascalStringFromString: accountName];

        coreStatus = KCAddGenericPassword(*serviceNamePtr, *accountNamePtr, strlen(password), password, NULL);

        free(serviceNamePtr);
        free(accountNamePtr);

        if (coreStatus != 0)
            [NSException raise: NSInternalInconsistencyException
                        format: @"Failed to add password to keychain: %@ / %@ for %@", serviceName, accountName, requestedPassword];
    }

    fprintf(stdout, "%s\n", password);
    [[NSApplication sharedApplication] terminate:self];
}
@end

/*
 Copyright (c) 2002 CodeFab, Inc.

 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */




See more files for this project here

redshed

Code for Mac+WebObjects.

Project homepage: http://sourceforge.net/projects/redshed
Programming language(s): C,Java,Objective C
License: other

  English.lproj/
    InfoWindow.nib/
      classes.nib
      info.nib
      objects.nib
    MainMenu.nib/
      classes.nib
      info.nib
      objects.nib
    InfoPlist.strings
  SSHPassKey.xcodeproj/
    project.pbxproj
    wolf.mode1
    wolf.pbxuser
  Info.plist
  InfoWindowController.h
  InfoWindowController.m
  KeychainAccess.icns
  LICENSE.txt
  README.html
  SSHPassKeyController.h
  SSHPassKeyController.m
  SSHPassKey_Prefix.pch
  main.m