18 # identical token maps, e.g. host -> host, will be inserted later
25 password => 'password',
29 # Map each credential protocol token to itself on the netrc side.
30 foreach (values %{$options{tmap}}) {
31 $options{tmap}->{$_} = $_;
34 # Now, $options{tmap} has a mapping from the netrc format to the Git credential
37 # Next, we build the reverse token map.
39 # When $rmap{foo} contains 'bar', that means that what the Git credential helper
40 # protocol calls 'bar' is found as 'foo' in the netrc/authinfo file. Keys in
41 # %rmap are what we expect to read from the netrc/authinfo file.
44 foreach my $k (keys %{$options{tmap}}) {
45 push @{$rmap{$options{tmap}->{$k}}}, $k;
48 Getopt::Long::Configure("bundling");
50 # TODO: maybe allow the token map $options{tmap} to be configurable.
60 my $shortname = basename($0);
61 $shortname =~ s/git-credential-//;
65 $0 [-f AUTHFILE1] [-f AUTHFILEN] [-d] [-v] [-k] get
67 Version $VERSION by tzz\@lifelogs.com. License: BSD.
71 -f|--file AUTHFILE : specify netrc-style files. Files with the .gpg extension
72 will be decrypted by GPG before parsing. Multiple -f
73 arguments are OK. They are processed in order, and the
74 first matching entry found is returned via the credential
75 helper protocol (see below).
77 When no -f option is given, .authinfo.gpg, .netrc.gpg,
78 .authinfo, and .netrc files in your home directory are used
81 -k|--insecure : ignore bad file ownership or permissions
83 -d|--debug : turn on debugging (developer info)
85 -v|--verbose : be more verbose (show files and information found)
87 To enable this credential helper:
89 git config credential.helper '$shortname -f AUTHFILE1 -f AUTHFILE2'
91 (Note that Git will prepend "git-credential-" to the helper name and look for it
94 ...and if you want lots of debugging info:
96 git config credential.helper '$shortname -f AUTHFILE -d'
98 ...or to see the files opened and data found:
100 git config credential.helper '$shortname -f AUTHFILE -v'
102 Only "get" mode is supported by this credential helper. It opens every AUTHFILE
103 and looks for the first entry that matches the requested search criteria:
106 The protocol that will be used (e.g., https). (protocol=X)
109 The remote hostname for a network credential. (host=X)
112 The path with which the credential will be used. (path=X)
114 'login|user|username':
115 The credential’s username, if we already have one. (username=X)
117 Thus, when we get this query on STDIN:
123 this credential helper will look for the first entry in every AUTHFILE that
126 machine github.com port https login tzz
130 machine github.com protocol https login tzz
132 OR... etc. acceptable tokens as listed above. Any unknown tokens are
135 Then, the helper will print out whatever tokens it got from the entry, including
136 "password" tokens, mapping back to Git's helper protocol; e.g. "port" is mapped
137 back to "protocol". Any redundant entry tokens (part of the original query) are
140 Again, note that only the first matching entry from all the AUTHFILEs, processed
141 in the sequence given on the command line, is used.
143 Netrc/authinfo tokens can be quoted as 'STRING' or "STRING".
145 No caching is performed by this credential helper.
152 my $mode = shift @ARGV;
154 # Credentials must get a parameter, so die if it's missing.
155 die "Syntax: $0 [-f AUTHFILE1] [-f AUTHFILEN] [-d] get" unless defined $mode;
157 # Only support 'get' mode; with any other unsupported ones we just exit.
158 exit 0 unless $mode eq 'get';
160 my $files = $options{file};
162 # if no files were given, use a predefined list.
163 # note that .gpg files come first
164 unless (scalar @$files) {
172 $files = $options{file} = [ map { glob $_ } @candidates ];
175 my $query = read_credential_data_from_stdin();
178 foreach my $file (@$files) {
179 my $gpgmode = $file =~ m/\.gpg$/;
181 log_verbose("Unable to read $file; skipping it");
185 # the following check is copied from Net::Netrc, for non-GPG files
186 # OS/2 and Win32 do not handle stat in a way compatible with this check :-(
187 unless ($gpgmode || $options{insecure} ||
191 || $^O =~ /^cygwin/) {
192 my @stat = stat($file);
195 if ($stat[2] & 077) {
196 log_verbose("Insecure $file (mode=%04o); skipping it",
201 if ($stat[4] != $<) {
202 log_verbose("Not owner of $file; skipping it");
208 my @entries = load_netrc($file, $gpgmode);
210 unless (scalar @entries) {
212 log_verbose("Unable to open $file: $!");
214 log_verbose("No netrc entries found in $file");
220 my $entry = find_netrc_entry($query, @entries);
222 print_credential_data($entry, $query);
232 my $gpgmode = shift @_;
236 my @cmd = (qw(gpg --decrypt), $file);
237 log_verbose("Using GPG to open $file: [@cmd]");
238 open $io, "-|", @cmd;
240 log_verbose("Opening $file...");
241 open $io, '<', $file;
244 # nothing to do if the open failed (we log the error later)
247 # Net::Netrc does this, but the functionality is merged with the file
248 # detection logic, so we have to extract just the part we need
249 my @netrc_entries = net_netrc_loader($io);
251 # these entries will use the credential helper protocol token names
254 foreach my $nentry (@netrc_entries) {
258 if (!defined $nentry->{machine}) {
261 if (defined $nentry->{port} && $nentry->{port} =~ m/^\d+$/) {
262 $num_port = $nentry->{port};
263 delete $nentry->{port};
266 # create the new entry for the credential helper protocol
267 $entry{$options{tmap}->{$_}} = $nentry->{$_} foreach keys %$nentry;
269 # for "host X port Y" where Y is an integer (captured by
270 # $num_port above), set the host to "X:Y"
271 if (defined $entry{host} && defined $num_port) {
272 $entry{host} = join(':', $entry{host}, $num_port);
275 push @entries, \%entry;
281 sub net_netrc_loader {
284 my ($mach, $macdef, $tok, @tok);
288 undef $macdef if /\A\n\Z/;
297 while (length && s/^("((?:[^"]+|\\.)*)"|((?:[^\\\s]+|\\.)*))\s*//) {
298 (my $tok = $+) =~ s/\\(.)/$1/g;
304 if ($tok[0] eq "default") {
306 $mach = { machine => undef };
312 if ($tok eq "machine") {
313 my $host = shift @tok;
314 $mach = { machine => $host };
315 push @entries, $mach;
316 } elsif (exists $options{tmap}->{$tok}) {
318 log_debug("Skipping token $tok because no machine was given");
322 my $value = shift @tok;
323 unless (defined $value) {
324 log_debug("Token $tok had no value, skipping it.");
328 # Following line added by rmerrell to remove '/' escape char in .netrc
329 $value =~ s/\/\\/\\/g;
330 $mach->{$tok} = $value;
331 } elsif ($tok eq "macdef") { # we ignore macros
332 next TOKEN unless $mach;
333 my $value = shift @tok;
342 sub read_credential_data_from_stdin {
343 # the query: start with every token with no value
344 my %q = map { $_ => undef } values(%{$options{tmap}});
347 next unless m/^([^=]+)=(.+)/;
349 my ($token, $value) = ($1, $2);
350 die "Unknown search token $token" unless exists $q{$token};
352 log_debug("We were given search token $token and value $value");
355 foreach (sort keys %q) {
356 log_debug("Searching for %s = %s", $_, $q{$_} || '(any value)');
362 # takes the search tokens and then a list of entries
363 # each entry is a hash reference
364 sub find_netrc_entry {
365 my $query = shift @_;
368 foreach my $entry (@_)
370 my $entry_text = join ', ', map { "$_=$entry->{$_}" } keys %$entry;
371 foreach my $check (sort keys %$query) {
372 if (defined $query->{$check}) {
373 log_debug("compare %s [%s] to [%s] (entry: %s)",
378 unless ($query->{$check} eq $entry->{$check}) {
382 log_debug("OK: any value satisfies check $check");
393 sub print_credential_data {
394 my $entry = shift @_;
395 my $query = shift @_;
397 log_debug("entry has passed all the search checks");
399 foreach my $git_token (sort keys %$entry) {
400 log_debug("looking for useful token $git_token");
401 # don't print unknown (to the credential helper protocol) tokens
402 next TOKEN unless exists $query->{$git_token};
404 # don't print things asked in the query (the entry matches them)
405 next TOKEN if defined $query->{$git_token};
407 log_debug("FOUND: $git_token=$entry->{$git_token}");
408 printf "%s=%s\n", $git_token, $entry->{$git_token};
412 return unless $options{verbose};
418 return unless $options{debug};