Skip to contentSkip to conversation

contrib: add a credential helper for Mac OS X's keychain

PATCH In review Jay Soffian 3 participants 7 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 contrib: add a credential helper for Mac OS X's keychain

This credential helper adds, searches, and removes entries from the Mac OS X keychain via OS X's Security Framework.

Tested with 10.6.8.

---
And here's a C version. I cargo-culted the Makefile from contrib/svn-fe/Makefile. Sadly, linking against git bloats the binary quite a bit which is dissappointing since this helper won't be installed as a hard-link. Hmm, maybe it can be if I dlopen the security framework instead of linking against it.

 contrib/credential-osxkeychain/Makefile            |   35 ++++
 .../git-credential-osxkeychain.c                   |  205 ++++++++++++++++++++
 2 files changed, 240 insertions(+), 0 deletions(-)
 create mode 100644 contrib/credential-osxkeychain/Makefile
 create mode 100644 contrib/credential-osxkeychain/git-credential-osxkeychain.c
contrib/credential-osxkeychain/Makefile new file +35 -0
new file mode 100644
index 0000000000..dc6bbbc3f9
@@ -0,0 +1,35 @@
+all:: git-credential-osxkeychain
+
+CC = gcc
+RM = rm -f
+CFLAGS = -O2 -Wall -I../.. -DSHA1_HEADER='<openssl/sha.h>'
+GIT_LIBS = ../../libgit.a ../../xdiff/lib.a
+LIBS = $(GIT_LIBS) -lz -liconv -lcrypto -lssl
+
+QUIET_SUBDIR0 = +$(MAKE) -C # space to separate -C and subdir
+QUIET_SUBDIR1 =
+
+ifneq ($(findstring $(MAKEFLAGS),w),w)
+PRINT_DIR = --no-print-directory
+else # "make -w"
+NO_SUBDIR = :
+endif
+
+ifneq ($(findstring $(MAKEFLAGS),s),s)
+ifndef V
+	QUIET_CC      = @echo '   ' CC $@;
+	QUIET_LINK    = @echo '   ' LINK $@;
+	QUIET_SUBDIR0 = +@subdir=
+	QUIET_SUBDIR1 = ;$(NO_SUBDIR) echo '   ' SUBDIR $$subdir; \
+	                $(MAKE) $(PRINT_DIR) -C $$subdir
+endif
+endif
+
+git-credential-osxkeychain: git-credential-osxkeychain.o $(GIT_LIBS)
+	$(QUIET_LINK)$(CC) -o $@ $< $(LIBS) -Wl,-framework -Wl,Security
+
+git-credential-osxkeychain.o: git-credential-osxkeychain.c
+	$(QUIET_CC)$(CC) -c $(CFLAGS) $<
+
+clean:
+	$(RM) git-credential-osxkeychain git-credential-osxkeychain.o
contrib/credential-osxkeychain/git-credential-osxkeychain.c new file +205 -0

205 lines not on this page · raw message

Signature · 1 line
1.7.7.rc1.1.g011e1
#1 lore raw

PATCH 1/1 credential-osxkeychain: load Security framework dynamically

Use dlopen() / dysym() instead of dynmically linking to the Security framework. A followup commit will refactor things such that git-credential-osxkeychain can be hardlinked to git.

---

Quoted 2 lines

Hmm, maybe it can be if I dlopen the security framework instead of linking against it.

Something like this. I'm going to pause here for feedback. Is the (not yet existant) followup commit referenced above allowing git-credential-osxkeychain to be a hard link to git a worthwhile endeavor? Or would a better approach be to make git-credential-osxkeychain.c not use any git code?

 contrib/credential-osxkeychain/Makefile            |   12 +++-
 .../credential-osxkeychain/generate_security.py    |   73 ++++++++++++++++++++
 2 files changed, 82 insertions(+), 3 deletions(-)
 create mode 100755 contrib/credential-osxkeychain/generate_security.py
contrib/credential-osxkeychain/Makefile +9 -3
index dc6bbbc3f9..001d695cb8 100644
@@ -25,11 +25,17 @@ ifndef V
 endif
 endif
 
-git-credential-osxkeychain: git-credential-osxkeychain.o $(GIT_LIBS)
-	$(QUIET_LINK)$(CC) -o $@ $< $(LIBS) -Wl,-framework -Wl,Security
+git-credential-osxkeychain: git-credential-osxkeychain.o security.o $(GIT_LIBS)
+	$(QUIET_LINK)$(CC) -o $@ $< security.o $(LIBS)
 
 git-credential-osxkeychain.o: git-credential-osxkeychain.c
 	$(QUIET_CC)$(CC) -c $(CFLAGS) $<
 
+security.o: security.c
+	$(QUIET_CC)$(CC) -c $(CFLAGS) $<
+
+security.c: generate_security.py
+	python generate_security.py
+
 clean:
-	$(RM) git-credential-osxkeychain git-credential-osxkeychain.o
+	$(RM) git-credential-osxkeychain git-credential-osxkeychain.o security.?
contrib/credential-osxkeychain/generate_security.py new file +73 -0

73 lines not on this page · raw message

Signature · 1 line
1.7.7.rc1.1.g011e1
#2 lore raw

Re: [PATCH] credential-osxkeychain: load Security framework dynamically

Quoted 4 lines · Jay Soffian

Something like this. I'm going to pause here for feedback. Is the (not yet existant) followup commit referenced above allowing git-credential-osxkeychain to be a hard link to git a worthwhile endeavor? Or would a better approach be to make git-credential-osxkeychain.c not use any git code?

To be honest, I was surprised to see you linking against git. I had always envisioned OS-specific helpers as living outside of the git.git repo. That's why I provided git-credential-getpass; it should be the only part of git that helpers really want to reuse.

What are you getting from git that is useful? From my skim of your patch, it looks like xmalloc/die, parse_options, and credential_getpass.

The first can be replaced with a few trivial lines of code. The second is overkill, I think. The helper code will always hand you the "--option=value" form, and I always intended it to stay that way (whether that is well documented, I'm not sure). But a simple loop with strncmps would be fine.

The hardest part is credential_getpass. You can call "git credential-getpass", but you'll have to read the output yourself (though it's quite simple to parse; see read_credential_response).

I'm not a fan of cutting and pasting code, and this will add a number of lines to your C program. But I feel like keeping the build completely separate from core git is probably a good boundary (especially because this will not be getting built or tested all the time, as most of the core code is).

-Peff

#3 lore raw

Re: [PATCH] credential-osxkeychain: load Security framework dynamically

Quoted 6 lines · Jeff King

On Wed, Sep 14, 2011 at 06:55:26PM -0400, Jay Soffian wrote:

To be honest, I was surprised to see you linking against git. I had always envisioned OS-specific helpers as living outside of the git.git repo.  That's why I provided git-credential-getpass; it should be the only part of git that helpers really want to reuse.

Okay, I think I was led astray by the fact that credential-{cache,store}.c (at least the latter of which is meant as nothing more than an example helper right?) links with git.

Quoted 2 lines

What are you getting from git that is useful? From my skim of your patch, it looks like xmalloc/die, parse_options, and credential_getpass.

Correct.

Quoted 10 lines

The first can be replaced with a few trivial lines of code. The second is overkill, I think. The helper code will always hand you the "--option=value" form, and I always intended it to stay that way (whether that is well documented, I'm not sure). But a simple loop with strncmps would be fine.

The hardest part is credential_getpass. You can call "git credential-getpass", but you'll have to read the output yourself (though it's quite simple to parse; see read_credential_response).

Quoted 5 lines

I'm not a fan of cutting and pasting code, and this will add a number of lines to your C program. But I feel like keeping the build completely separate from core git is probably a good boundary (especially because this will not be getting built or tested all the time, as most of the core code is).

Okay.

j.

#4 lore raw

Re: [PATCH] credential-osxkeychain: load Security framework dynamically

Quoted 3 lines · Jay Soffian

Okay, I think I was led astray by the fact that credential-{cache,store}.c (at least the latter of which is meant as nothing more than an example helper right?) links with git.

No, credential-store is meant to be used. It's just that it has a security tradeoff that makes it the wrong choice for most cases. So it's meant to be used sparingly. :)

As for those helpers being linked against git, I guess it doesn't make them the best example code. But I wanted them to be always available as a lowest common denominator (because even if you have a fancy local keychain, it is likely that you'll end up at some point using git across an ssh connection, and I wanted to provide _something_ there).

Not having any external dependencies, those helpers don't pollute our code base too much. Building and testing them with the rest of git keeps the code fresh and unbroken. Maybe it would be better if they provided a clearer separation as an example. I'm open to that if people think it's worth splitting them out. I suspect I could write credential-store as something like 10 lines of perl.

-Peff

#5 lore raw

Re: [PATCH] credential-osxkeychain: load Security framework dynamically

In reply to Jay Soffian
Quoted 4 lines · Jay Soffian

Something like this. I'm going to pause here for feedback. Is the (not yet existant) followup commit referenced above allowing git-credential-osxkeychain to be a hard link to git a worthwhile endeavor? Or would a better approach be to make git-credential-osxkeychain.c not use any git code?

Most definitely the latter, I would think.

The whole point of making the Git credential code talk with a defined interface with external programs is so that these keychain helpers can be written independently from the rest of Git.

If the reason why your keychain helper benefits from linking with the rest of Git is because some pieces of information you need in order to respond to the requests from credential interface is hard to get if your helper is built as an independent program, that is a sign that we are not exposing enough information to scripts, iow, the failure in the design of the credential interface. If that is the case (and I doubt it is), we would need to fix the interface (either the credential interface, or perhaps "git config") so that such an independent program does not have to peek inside the internals of Git.

If the reason is because you want to reuse some generic C API we have that are not necessarily tied to Git (e.g. strbuf, string-list, etc.), on the other hand, please resist the temptation to do so. It would not help your program to serve as an example of independent external keychain helpers, i.e. a demonstration of how simple to write them.

#6 lore raw
In reply to Jay Soffian
Quoted 7 lines · Jay Soffian
contrib/credential-osxkeychain/git-credential-osxkeychain.c new file +0 -0
new file mode 100644
index 0000000000..64bcc636cb
 
[...]

contrib/credential-osxkeychain/git-credential-osxkeychain.c line 49

Quoted 10 lines
contrib/credential-osxkeychain/git-credential-osxkeychain.c new file +9 -0
 
+int find_internet_password(SecProtocolType protocol,
+                          char *hostname,
+                          char *username)
+{
+       void *password_buf;
+       UInt32 password_len;
+       OSStatus status;
+       char *password;
+       int free_username;

int free_username = 0;

j.