Skip to content

Commit 8c6fda2

Browse files
committed
initial test
1 parent 27faaf8 commit 8c6fda2

File tree

2 files changed

+193
-11
lines changed

2 files changed

+193
-11
lines changed

GitX.xcodeproj/project.pbxproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@
8282
91B103CC0E898EC300C84364 /* PBIconAndTextCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 91B103CB0E898EC300C84364 /* PBIconAndTextCell.m */; };
8383
93CB42C20EAB7B2200530609 /* PBGitDefaults.m in Sources */ = {isa = PBXBuildFile; fileRef = 93CB42C10EAB7B2200530609 /* PBGitDefaults.m */; };
8484
93F7857F0EA3ABF100C1F443 /* PBCommitMessageView.m in Sources */ = {isa = PBXBuildFile; fileRef = 93F7857E0EA3ABF100C1F443 /* PBCommitMessageView.m */; };
85+
C3B13DD812F3A89D0045437C /* SecurityFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C3B13DD712F3A89D0045437C /* SecurityFoundation.framework */; };
86+
C3B13DE412F3A8D70045437C /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C3B13DE312F3A8D70045437C /* Security.framework */; };
87+
C3B13E2312F3A9130045437C /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C3B13DE312F3A8D70045437C /* Security.framework */; };
8588
D26DC6450E782C9000C777B2 /* gitx.icns in Resources */ = {isa = PBXBuildFile; fileRef = D26DC6440E782C9000C777B2 /* gitx.icns */; };
8689
D8022FE811E124A0003C21F6 /* PBGitXMessageSheet.xib in Resources */ = {isa = PBXBuildFile; fileRef = D8022FE711E124A0003C21F6 /* PBGitXMessageSheet.xib */; };
8790
D8022FED11E124C8003C21F6 /* PBGitXMessageSheet.m in Sources */ = {isa = PBXBuildFile; fileRef = D8022FEC11E124C8003C21F6 /* PBGitXMessageSheet.m */; };
@@ -375,6 +378,8 @@
375378
93F7857D0EA3ABF100C1F443 /* PBCommitMessageView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBCommitMessageView.h; sourceTree = "<group>"; };
376379
93F7857E0EA3ABF100C1F443 /* PBCommitMessageView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBCommitMessageView.m; sourceTree = "<group>"; };
377380
93FCCBA80EA8AF450061B02B /* PBGitConfig.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBGitConfig.m; sourceTree = "<group>"; };
381+
C3B13DD712F3A89D0045437C /* SecurityFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SecurityFoundation.framework; path = System/Library/Frameworks/SecurityFoundation.framework; sourceTree = SDKROOT; };
382+
C3B13DE312F3A8D70045437C /* Security.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Security.framework; path = System/Library/Frameworks/Security.framework; sourceTree = SDKROOT; };
378383
D26DC6440E782C9000C777B2 /* gitx.icns */ = {isa = PBXFileReference; lastKnownFileType = image.icns; path = gitx.icns; sourceTree = "<group>"; };
379384
D8022A3411DFCCA5003C21F6 /* build_libgit2.sh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; path = build_libgit2.sh; sourceTree = "<group>"; };
380385
D8022FE711E124A0003C21F6 /* PBGitXMessageSheet.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = PBGitXMessageSheet.xib; sourceTree = "<group>"; };
@@ -573,6 +578,7 @@
573578
isa = PBXFrameworksBuildPhase;
574579
buildActionMask = 2147483647;
575580
files = (
581+
C3B13E2312F3A9130045437C /* Security.framework in Frameworks */,
576582
);
577583
runOnlyForDeploymentPostprocessing = 0;
578584
};
@@ -588,6 +594,8 @@
588594
F5C580E50EDA250900995434 /* libgit2.a in Frameworks */,
589595
D8E3B2B810DC9FB2001096A3 /* ScriptingBridge.framework in Frameworks */,
590596
D87127011229A21C00012334 /* QuartzCore.framework in Frameworks */,
597+
C3B13DD812F3A89D0045437C /* SecurityFoundation.framework in Frameworks */,
598+
C3B13DE412F3A8D70045437C /* Security.framework in Frameworks */,
591599
);
592600
runOnlyForDeploymentPostprocessing = 0;
593601
};
@@ -759,6 +767,7 @@
759767
F5886A120ED5D33D0066E74C /* SpeedTest-Info.plist */,
760768
F567CC3A106E6B910059BB9D /* GitXTesting-Info.plist */,
761769
D89E9AB21218A9DA0097A90B /* ScriptingBridge.framework */,
770+
C3B13DD712F3A89D0045437C /* SecurityFoundation.framework */,
762771
);
763772
name = GitTest;
764773
sourceTree = "<group>";
@@ -838,6 +847,7 @@
838847
F5C580E40EDA250900995434 /* libgit2.a */,
839848
1058C7A0FEA54F0111CA2CBB /* Linked Frameworks */,
840849
1058C7A2FEA54F0111CA2CBB /* Other Frameworks */,
850+
C3B13DE312F3A8D70045437C /* Security.framework */,
841851
);
842852
name = Frameworks;
843853
sourceTree = "<group>";

gitx_askpasswd_main.m

Lines changed: 183 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@
99

1010
#include <ApplicationServices/ApplicationServices.h>
1111
#import <AppKit/AppKit.h>
12+
#include <errno.h>
13+
#include <string.h>
14+
#include <sys/sysctl.h>
15+
#include <Security/Security.h>
16+
#include <CoreServices/CoreServices.h>
17+
#include <Security/SecKeychain.h>
18+
#include <Security/SecKeychainItem.h>
19+
#include <Security/SecAccess.h>
20+
#include <Security/SecTrustedApplication.h>
21+
#include <Security/SecACL.h>
22+
#include <CoreFoundation/CoreFoundation.h>
1223

1324
#define OKBUTTONWIDTH 100.0
1425
#define OKBUTTONHEIGHT 24.0
@@ -23,19 +34,22 @@ @interface GAPAppDelegate : NSObject
2334
{
2435
NSPanel* mPasswordPanel;
2536
NSSecureTextField* mPasswordField;
37+
NSButton* rememberCheck;
2638
}
2739

28-
-(NSPanel*) passwordPanel;
40+
-(NSPanel*) passwordPanel;
2941

3042
-(IBAction) doOKButton: (id)sender;
3143
-(IBAction) doCancelButton: (id)sender;
3244

3345
@end
3446

47+
NSString* url;
48+
3549

3650
@implementation GAPAppDelegate
3751

38-
-(NSPanel*) passwordPanel
52+
-(NSPanel*)passwordPanel:(NSString *)prompt remember:(BOOL)remember
3953
{
4054
if( !mPasswordPanel )
4155
{
@@ -67,7 +81,7 @@ -(NSPanel*) passwordPanel
6781
[okButton setBordered: YES];
6882
[okButton setBezelStyle: NSRoundedBezelStyle];
6983
[[mPasswordPanel contentView] addSubview: okButton];
70-
84+
7185
// Cancel:
7286
NSRect cancelBox = box;
7387
cancelBox.origin.x = NSMinX( okBox ) -CANCELBUTTONWIDTH -6;
@@ -109,9 +123,24 @@ -(NSPanel*) passwordPanel
109123
[passwordLabel setBordered: NO];
110124
[passwordLabel setBezeled: NO];
111125
[passwordLabel setDrawsBackground: NO];
112-
[passwordLabel setStringValue: @"Please enter your password:"]; // +++ Localize.
126+
[passwordLabel setStringValue: prompt];
113127
[[mPasswordPanel contentView] addSubview: passwordLabel];
114128

129+
// remember buton:
130+
if(remember){
131+
NSRect rememberBox = box;
132+
rememberBox.origin.x = 100;
133+
rememberBox.size.width = CANCELBUTTONWIDTH;
134+
rememberBox.origin.y += 20;
135+
rememberBox.size.height = CANCELBUTTONHEIGHT;
136+
rememberCheck = [[NSButton alloc] initWithFrame: rememberBox];
137+
[rememberCheck setButtonType:NSSwitchButton];
138+
[rememberCheck setTarget: self];
139+
[rememberCheck setTitle: @"Remenber"]; // +++ Localize.
140+
[rememberCheck setKeyEquivalent: @"\r"];
141+
[[mPasswordPanel contentView] addSubview: rememberCheck];
142+
}
143+
115144
// GitX icon:
116145
NSRect gitxIconBox = box;
117146
gitxIconBox.origin.y = NSMaxY(box) - 78;
@@ -132,7 +161,15 @@ -(NSPanel*) passwordPanel
132161

133162
-(IBAction) doOKButton: (id)sender
134163
{
135-
printf( "%s\n", [[mPasswordField stringValue] UTF8String] );
164+
NSString *pas=[mPasswordField stringValue];
165+
printf( "%s\n", [pas UTF8String] );
166+
167+
StorePasswordKeychain ([url cStringUsingEncoding:NSASCIIStringEncoding],
168+
[url lengthOfBytesUsingEncoding:NSASCIIStringEncoding],
169+
[pas cStringUsingEncoding:NSASCIIStringEncoding],
170+
[pas lengthOfBytesUsingEncoding:NSASCIIStringEncoding]); //Call
171+
172+
136173
[[NSApplication sharedApplication] stopModalWithCode: 0];
137174
}
138175

@@ -147,9 +184,111 @@ -(IBAction) doCancelButton: (id)sender
147184

148185
@end
149186

187+
void getproclline(pid_t pid, char *command_name);
188+
189+
void getproclline(pid_t pid, char *command_name)
190+
{
191+
int mib[3], argmax, nargs, c = 0;
192+
size_t size;
193+
char *procargs, *sp, *np, *cp;
194+
195+
mib[0] = CTL_KERN;
196+
mib[1] = KERN_ARGMAX;
197+
198+
size = sizeof(argmax);
199+
if (sysctl(mib, 2, &argmax, &size, NULL, 0) == -1) {
200+
return;
201+
}
202+
203+
/* Allocate space for the arguments. */
204+
procargs = (char *)malloc(argmax);
205+
if (procargs == NULL) {
206+
return;
207+
}
208+
209+
mib[0] = CTL_KERN;
210+
mib[1] = KERN_PROCARGS2;
211+
mib[2] = pid;
212+
213+
size = (size_t)argmax;
214+
if (sysctl(mib, 3, procargs, &size, NULL, 0) == -1) {
215+
return;
216+
}
217+
218+
memcpy(&nargs, procargs, sizeof(nargs));
219+
cp = procargs + sizeof(nargs);
220+
221+
/* Skip the saved exec_path. */
222+
for (; cp < &procargs[size]; cp++) {
223+
if (*cp == '\0') {
224+
/* End of exec_path reached. */
225+
break;
226+
}
227+
}
228+
if (cp == &procargs[size]) {
229+
return;
230+
}
231+
232+
/* Skip trailing '\0' characters. */
233+
for (; cp < &procargs[size]; cp++) {
234+
if (*cp != '\0') {
235+
/* Beginning of first argument reached. */
236+
break;
237+
}
238+
}
239+
if (cp == &procargs[size]) {
240+
return;
241+
}
242+
/* Save where the argv[0] string starts. */
243+
sp = cp;
244+
245+
for (np = NULL; c < nargs && cp < &procargs[size]; cp++) {
246+
if (*cp == '\0') {
247+
c++;
248+
if (np != NULL) {
249+
*np = ' ';
250+
}
251+
np = cp;
252+
}
253+
}
254+
sprintf(command_name, "%s",sp);
255+
}
256+
257+
OSStatus StorePasswordKeychain (const char *url, UInt32 urlLength, void* password,UInt32 passwordLength)
258+
{
259+
OSStatus status;
260+
status = SecKeychainAddGenericPassword (
261+
NULL, // default keychain
262+
4, // length of service name
263+
"GitX", // service name
264+
urlLength, // length of account name
265+
url, // account name
266+
passwordLength, // length of password
267+
password, // pointer to password data
268+
NULL // the item reference
269+
);
270+
return (status);
271+
}
150272

273+
OSStatus GetPasswordKeychain (const char *url, UInt32 urlLength ,void *passwordData,UInt32 *passwordLength,
274+
SecKeychainItemRef *itemRef)
275+
{
276+
OSStatus status ;
277+
status = SecKeychainFindGenericPassword (
278+
NULL, // default keychain
279+
4, // length of service name
280+
"GitX", // service name
281+
urlLength, // length of account name
282+
url, // account name
283+
passwordLength, // length of password
284+
passwordData, // pointer to password data
285+
itemRef // the item reference
286+
);
287+
return (status);
288+
}
151289

152-
int main( int argc, const char** argv )
290+
291+
int main( int argc, const char* argv[] )
153292
{
154293
// close stderr to stop cocoa log messages from being picked up by GitX
155294
close(STDERR_FILENO);
@@ -162,14 +301,47 @@ int main( int argc, const char** argv )
162301
NSApplication *app = [NSApplication sharedApplication];
163302
GAPAppDelegate *appDel = [[GAPAppDelegate alloc] init];
164303
[app setDelegate: appDel];
165-
NSWindow *passPanel = [appDel passwordPanel];
166304

167-
[app activateIgnoringOtherApps: YES];
168-
[passPanel makeKeyAndOrderFront: nil];
169-
NSInteger code = [app runModalForWindow: passPanel];
305+
char args[4024];
306+
getproclline(getppid(),args);
307+
NSString *cmd=[NSString stringWithFormat:@"%@",[NSString stringWithUTF8String:args]];
308+
309+
NSString *prompt=@"???";
310+
311+
url=@"poipoi";
312+
313+
if([cmd hasPrefix:@"git-remote-https"]){
314+
NSArray *args=[cmd componentsSeparatedByString:@" "];
315+
url=[args objectAtIndex:[args count]-1];
316+
}else if((sizeof(argv)/sizeof(char*))>1){
317+
prompt=[NSString stringWithCString:argv[1] encoding:NSASCIIStringEncoding];
318+
}else{
319+
prompt=@"null";
320+
}
321+
322+
323+
OSStatus status,status1;
324+
void *passwordData = nil;
325+
SecKeychainItemRef itemRef = nil;
326+
UInt32 passwordLength = nil;
327+
328+
status1 = GetPasswordKeychain ([url cStringUsingEncoding:NSASCIIStringEncoding],[url lengthOfBytesUsingEncoding:NSASCIIStringEncoding],&passwordData,&passwordLength,&itemRef);
329+
if (status1 == noErr) {
330+
SecKeychainItemFreeContent (NULL,passwordData);
331+
}
332+
333+
NSInteger code;
334+
335+
if(passwordLength>0){
336+
NSString *pass=[NSString stringWithCString:passwordData encoding:NSASCIIStringEncoding];
337+
}else {
338+
NSWindow *passPanel = [appDel passwordPanel:prompt remember:NO];
339+
[app activateIgnoringOtherApps: YES];
340+
[passPanel makeKeyAndOrderFront: nil];
341+
code = [app runModalForWindow: passPanel];
342+
}
170343

171344
[defaults synchronize];
172345

173346
return code;
174347
}
175-

0 commit comments

Comments
 (0)