From 8c6fda23c69273df7f474e538b8592dfa3c5e0f4 Mon Sep 17 00:00:00 2001 From: German Laullom Date: Wed, 9 Feb 2011 15:44:22 -0800 Subject: [PATCH] initial test --- GitX.xcodeproj/project.pbxproj | 10 ++ gitx_askpasswd_main.m | 194 +++++++++++++++++++++++++++++++-- 2 files changed, 193 insertions(+), 11 deletions(-) diff --git a/GitX.xcodeproj/project.pbxproj b/GitX.xcodeproj/project.pbxproj index fbaf896..4b04dce 100644 --- a/GitX.xcodeproj/project.pbxproj +++ b/GitX.xcodeproj/project.pbxproj @@ -82,6 +82,9 @@ 91B103CC0E898EC300C84364 /* PBIconAndTextCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 91B103CB0E898EC300C84364 /* PBIconAndTextCell.m */; }; 93CB42C20EAB7B2200530609 /* PBGitDefaults.m in Sources */ = {isa = PBXBuildFile; fileRef = 93CB42C10EAB7B2200530609 /* PBGitDefaults.m */; }; 93F7857F0EA3ABF100C1F443 /* PBCommitMessageView.m in Sources */ = {isa = PBXBuildFile; fileRef = 93F7857E0EA3ABF100C1F443 /* PBCommitMessageView.m */; }; + C3B13DD812F3A89D0045437C /* SecurityFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C3B13DD712F3A89D0045437C /* SecurityFoundation.framework */; }; + C3B13DE412F3A8D70045437C /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C3B13DE312F3A8D70045437C /* Security.framework */; }; + C3B13E2312F3A9130045437C /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C3B13DE312F3A8D70045437C /* Security.framework */; }; D26DC6450E782C9000C777B2 /* gitx.icns in Resources */ = {isa = PBXBuildFile; fileRef = D26DC6440E782C9000C777B2 /* gitx.icns */; }; D8022FE811E124A0003C21F6 /* PBGitXMessageSheet.xib in Resources */ = {isa = PBXBuildFile; fileRef = D8022FE711E124A0003C21F6 /* PBGitXMessageSheet.xib */; }; D8022FED11E124C8003C21F6 /* PBGitXMessageSheet.m in Sources */ = {isa = PBXBuildFile; fileRef = D8022FEC11E124C8003C21F6 /* PBGitXMessageSheet.m */; }; @@ -375,6 +378,8 @@ 93F7857D0EA3ABF100C1F443 /* PBCommitMessageView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBCommitMessageView.h; sourceTree = ""; }; 93F7857E0EA3ABF100C1F443 /* PBCommitMessageView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBCommitMessageView.m; sourceTree = ""; }; 93FCCBA80EA8AF450061B02B /* PBGitConfig.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBGitConfig.m; sourceTree = ""; }; + C3B13DD712F3A89D0045437C /* SecurityFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SecurityFoundation.framework; path = System/Library/Frameworks/SecurityFoundation.framework; sourceTree = SDKROOT; }; + C3B13DE312F3A8D70045437C /* Security.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Security.framework; path = System/Library/Frameworks/Security.framework; sourceTree = SDKROOT; }; D26DC6440E782C9000C777B2 /* gitx.icns */ = {isa = PBXFileReference; lastKnownFileType = image.icns; path = gitx.icns; sourceTree = ""; }; D8022A3411DFCCA5003C21F6 /* build_libgit2.sh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; path = build_libgit2.sh; sourceTree = ""; }; D8022FE711E124A0003C21F6 /* PBGitXMessageSheet.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = PBGitXMessageSheet.xib; sourceTree = ""; }; @@ -573,6 +578,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + C3B13E2312F3A9130045437C /* Security.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -588,6 +594,8 @@ F5C580E50EDA250900995434 /* libgit2.a in Frameworks */, D8E3B2B810DC9FB2001096A3 /* ScriptingBridge.framework in Frameworks */, D87127011229A21C00012334 /* QuartzCore.framework in Frameworks */, + C3B13DD812F3A89D0045437C /* SecurityFoundation.framework in Frameworks */, + C3B13DE412F3A8D70045437C /* Security.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -759,6 +767,7 @@ F5886A120ED5D33D0066E74C /* SpeedTest-Info.plist */, F567CC3A106E6B910059BB9D /* GitXTesting-Info.plist */, D89E9AB21218A9DA0097A90B /* ScriptingBridge.framework */, + C3B13DD712F3A89D0045437C /* SecurityFoundation.framework */, ); name = GitTest; sourceTree = ""; @@ -838,6 +847,7 @@ F5C580E40EDA250900995434 /* libgit2.a */, 1058C7A0FEA54F0111CA2CBB /* Linked Frameworks */, 1058C7A2FEA54F0111CA2CBB /* Other Frameworks */, + C3B13DE312F3A8D70045437C /* Security.framework */, ); name = Frameworks; sourceTree = ""; diff --git a/gitx_askpasswd_main.m b/gitx_askpasswd_main.m index 6c8c2af..4206bd9 100644 --- a/gitx_askpasswd_main.m +++ b/gitx_askpasswd_main.m @@ -9,6 +9,17 @@ #include #import +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #define OKBUTTONWIDTH 100.0 #define OKBUTTONHEIGHT 24.0 @@ -23,19 +34,22 @@ { NSPanel* mPasswordPanel; NSSecureTextField* mPasswordField; + NSButton* rememberCheck; } --(NSPanel*) passwordPanel; +-(NSPanel*) passwordPanel; -(IBAction) doOKButton: (id)sender; -(IBAction) doCancelButton: (id)sender; @end +NSString* url; + @implementation GAPAppDelegate --(NSPanel*) passwordPanel +-(NSPanel*)passwordPanel:(NSString *)prompt remember:(BOOL)remember { if( !mPasswordPanel ) { @@ -67,7 +81,7 @@ [okButton setBordered: YES]; [okButton setBezelStyle: NSRoundedBezelStyle]; [[mPasswordPanel contentView] addSubview: okButton]; - + // Cancel: NSRect cancelBox = box; cancelBox.origin.x = NSMinX( okBox ) -CANCELBUTTONWIDTH -6; @@ -109,9 +123,24 @@ [passwordLabel setBordered: NO]; [passwordLabel setBezeled: NO]; [passwordLabel setDrawsBackground: NO]; - [passwordLabel setStringValue: @"Please enter your password:"]; // +++ Localize. + [passwordLabel setStringValue: prompt]; [[mPasswordPanel contentView] addSubview: passwordLabel]; + // remember buton: + if(remember){ + NSRect rememberBox = box; + rememberBox.origin.x = 100; + rememberBox.size.width = CANCELBUTTONWIDTH; + rememberBox.origin.y += 20; + rememberBox.size.height = CANCELBUTTONHEIGHT; + rememberCheck = [[NSButton alloc] initWithFrame: rememberBox]; + [rememberCheck setButtonType:NSSwitchButton]; + [rememberCheck setTarget: self]; + [rememberCheck setTitle: @"Remenber"]; // +++ Localize. + [rememberCheck setKeyEquivalent: @"\r"]; + [[mPasswordPanel contentView] addSubview: rememberCheck]; + } + // GitX icon: NSRect gitxIconBox = box; gitxIconBox.origin.y = NSMaxY(box) - 78; @@ -132,7 +161,15 @@ -(IBAction) doOKButton: (id)sender { - printf( "%s\n", [[mPasswordField stringValue] UTF8String] ); + NSString *pas=[mPasswordField stringValue]; + printf( "%s\n", [pas UTF8String] ); + + StorePasswordKeychain ([url cStringUsingEncoding:NSASCIIStringEncoding], + [url lengthOfBytesUsingEncoding:NSASCIIStringEncoding], + [pas cStringUsingEncoding:NSASCIIStringEncoding], + [pas lengthOfBytesUsingEncoding:NSASCIIStringEncoding]); //Call + + [[NSApplication sharedApplication] stopModalWithCode: 0]; } @@ -147,9 +184,111 @@ @end +void getproclline(pid_t pid, char *command_name); + +void getproclline(pid_t pid, char *command_name) +{ + int mib[3], argmax, nargs, c = 0; + size_t size; + char *procargs, *sp, *np, *cp; + + mib[0] = CTL_KERN; + mib[1] = KERN_ARGMAX; + + size = sizeof(argmax); + if (sysctl(mib, 2, &argmax, &size, NULL, 0) == -1) { + return; + } + + /* Allocate space for the arguments. */ + procargs = (char *)malloc(argmax); + if (procargs == NULL) { + return; + } + + mib[0] = CTL_KERN; + mib[1] = KERN_PROCARGS2; + mib[2] = pid; + + size = (size_t)argmax; + if (sysctl(mib, 3, procargs, &size, NULL, 0) == -1) { + return; + } + + memcpy(&nargs, procargs, sizeof(nargs)); + cp = procargs + sizeof(nargs); + + /* Skip the saved exec_path. */ + for (; cp < &procargs[size]; cp++) { + if (*cp == '\0') { + /* End of exec_path reached. */ + break; + } + } + if (cp == &procargs[size]) { + return; + } + + /* Skip trailing '\0' characters. */ + for (; cp < &procargs[size]; cp++) { + if (*cp != '\0') { + /* Beginning of first argument reached. */ + break; + } + } + if (cp == &procargs[size]) { + return; + } + /* Save where the argv[0] string starts. */ + sp = cp; + + for (np = NULL; c < nargs && cp < &procargs[size]; cp++) { + if (*cp == '\0') { + c++; + if (np != NULL) { + *np = ' '; + } + np = cp; + } + } + sprintf(command_name, "%s",sp); +} + +OSStatus StorePasswordKeychain (const char *url, UInt32 urlLength, void* password,UInt32 passwordLength) +{ + OSStatus status; + status = SecKeychainAddGenericPassword ( + NULL, // default keychain + 4, // length of service name + "GitX", // service name + urlLength, // length of account name + url, // account name + passwordLength, // length of password + password, // pointer to password data + NULL // the item reference + ); + return (status); +} + +OSStatus GetPasswordKeychain (const char *url, UInt32 urlLength ,void *passwordData,UInt32 *passwordLength, + SecKeychainItemRef *itemRef) +{ + OSStatus status ; + status = SecKeychainFindGenericPassword ( + NULL, // default keychain + 4, // length of service name + "GitX", // service name + urlLength, // length of account name + url, // account name + passwordLength, // length of password + passwordData, // pointer to password data + itemRef // the item reference + ); + return (status); +} -int main( int argc, const char** argv ) +int main( int argc, const char* argv[] ) { // close stderr to stop cocoa log messages from being picked up by GitX close(STDERR_FILENO); @@ -162,14 +301,47 @@ int main( int argc, const char** argv ) NSApplication *app = [NSApplication sharedApplication]; GAPAppDelegate *appDel = [[GAPAppDelegate alloc] init]; [app setDelegate: appDel]; - NSWindow *passPanel = [appDel passwordPanel]; - [app activateIgnoringOtherApps: YES]; - [passPanel makeKeyAndOrderFront: nil]; - NSInteger code = [app runModalForWindow: passPanel]; + char args[4024]; + getproclline(getppid(),args); + NSString *cmd=[NSString stringWithFormat:@"%@",[NSString stringWithUTF8String:args]]; + + NSString *prompt=@"???"; + + url=@"poipoi"; + + if([cmd hasPrefix:@"git-remote-https"]){ + NSArray *args=[cmd componentsSeparatedByString:@" "]; + url=[args objectAtIndex:[args count]-1]; + }else if((sizeof(argv)/sizeof(char*))>1){ + prompt=[NSString stringWithCString:argv[1] encoding:NSASCIIStringEncoding]; + }else{ + prompt=@"null"; + } + + + OSStatus status,status1; + void *passwordData = nil; + SecKeychainItemRef itemRef = nil; + UInt32 passwordLength = nil; + + status1 = GetPasswordKeychain ([url cStringUsingEncoding:NSASCIIStringEncoding],[url lengthOfBytesUsingEncoding:NSASCIIStringEncoding],&passwordData,&passwordLength,&itemRef); + if (status1 == noErr) { + SecKeychainItemFreeContent (NULL,passwordData); + } + + NSInteger code; + + if(passwordLength>0){ + NSString *pass=[NSString stringWithCString:passwordData encoding:NSASCIIStringEncoding]; + }else { + NSWindow *passPanel = [appDel passwordPanel:prompt remember:NO]; + [app activateIgnoringOtherApps: YES]; + [passPanel makeKeyAndOrderFront: nil]; + code = [app runModalForWindow: passPanel]; + } [defaults synchronize]; return code; } -