4 #include "credential.h"
5 #include "string-list.h"
6 #include "parse-options.h"
8 static struct lock_file credential_lock
;
10 static int parse_credential_file(const char *fn
,
12 void (*match_cb
)(struct credential
*),
13 void (*other_cb
)(struct strbuf
*))
16 struct strbuf line
= STRBUF_INIT
;
17 struct credential entry
= CREDENTIAL_INIT
;
18 int found_credential
= 0;
22 if (errno
!= ENOENT
&& errno
!= EACCES
)
23 die_errno("unable to open %s", fn
);
24 return found_credential
;
27 while (strbuf_getline_lf(&line
, fh
) != EOF
) {
28 if (!credential_from_url_gently(&entry
, line
.buf
, 1) &&
29 entry
.username
&& entry
.password
&&
30 credential_match(c
, &entry
)) {
41 credential_clear(&entry
);
42 strbuf_release(&line
);
44 return found_credential
;
47 static void print_entry(struct credential
*c
)
49 printf("username=%s\n", c
->username
);
50 printf("password=%s\n", c
->password
);
53 static void print_line(struct strbuf
*buf
)
55 strbuf_addch(buf
, '\n');
56 write_or_die(get_lock_file_fd(&credential_lock
), buf
->buf
, buf
->len
);
59 static void rewrite_credential_file(const char *fn
, struct credential
*c
,
62 int timeout_ms
= 1000;
64 git_config_get_int("credentialstore.locktimeoutms", &timeout_ms
);
65 if (hold_lock_file_for_update_timeout(&credential_lock
, fn
, 0, timeout_ms
) < 0)
66 die_errno(_("unable to get credential storage lock in %d ms"), timeout_ms
);
69 parse_credential_file(fn
, c
, NULL
, print_line
);
70 if (commit_lock_file(&credential_lock
) < 0)
71 die_errno("unable to write credential store");
74 static void store_credential_file(const char *fn
, struct credential
*c
)
76 struct strbuf buf
= STRBUF_INIT
;
78 strbuf_addf(&buf
, "%s://", c
->protocol
);
79 strbuf_addstr_urlencode(&buf
, c
->username
, is_rfc3986_unreserved
);
80 strbuf_addch(&buf
, ':');
81 strbuf_addstr_urlencode(&buf
, c
->password
, is_rfc3986_unreserved
);
82 strbuf_addch(&buf
, '@');
84 strbuf_addstr_urlencode(&buf
, c
->host
, is_rfc3986_unreserved
);
86 strbuf_addch(&buf
, '/');
87 strbuf_addstr_urlencode(&buf
, c
->path
,
88 is_rfc3986_reserved_or_unreserved
);
91 rewrite_credential_file(fn
, c
, &buf
);
95 static void store_credential(const struct string_list
*fns
, struct credential
*c
)
97 struct string_list_item
*fn
;
100 * Sanity check that what we are storing is actually sensible.
101 * In particular, we can't make a URL without a protocol field.
102 * Without either a host or pathname (depending on the scheme),
103 * we have no primary key. And without a username and password,
104 * we are not actually storing a credential.
106 if (!c
->protocol
|| !(c
->host
|| c
->path
) || !c
->username
|| !c
->password
)
109 for_each_string_list_item(fn
, fns
)
110 if (!access(fn
->string
, F_OK
)) {
111 store_credential_file(fn
->string
, c
);
115 * Write credential to the filename specified by fns->items[0], thus
119 store_credential_file(fns
->items
[0].string
, c
);
122 static void remove_credential(const struct string_list
*fns
, struct credential
*c
)
124 struct string_list_item
*fn
;
127 * Sanity check that we actually have something to match
128 * against. The input we get is a restrictive pattern,
129 * so technically a blank credential means "erase everything".
130 * But it is too easy to accidentally send this, since it is equivalent
131 * to empty input. So explicitly disallow it, and require that the
132 * pattern have some actual content to match.
134 if (!c
->protocol
&& !c
->host
&& !c
->path
&& !c
->username
)
136 for_each_string_list_item(fn
, fns
)
137 if (!access(fn
->string
, F_OK
))
138 rewrite_credential_file(fn
->string
, c
, NULL
);
141 static void lookup_credential(const struct string_list
*fns
, struct credential
*c
)
143 struct string_list_item
*fn
;
145 for_each_string_list_item(fn
, fns
)
146 if (parse_credential_file(fn
->string
, c
, print_entry
, NULL
))
147 return; /* Found credential */
150 int cmd_credential_store(int argc
, const char **argv
, const char *prefix
)
152 const char * const usage
[] = {
153 "git credential-store [<options>] <action>",
157 struct credential c
= CREDENTIAL_INIT
;
158 struct string_list fns
= STRING_LIST_INIT_DUP
;
160 struct option options
[] = {
161 OPT_STRING(0, "file", &file
, "path",
162 "fetch and store credentials in <path>"),
168 argc
= parse_options(argc
, (const char **)argv
, prefix
, options
, usage
, 0);
170 usage_with_options(usage
, options
);
174 string_list_append(&fns
, file
);
176 if ((file
= interpolate_path("~/.git-credentials", 0)))
177 string_list_append_nodup(&fns
, file
);
178 file
= xdg_config_home("credentials");
180 string_list_append_nodup(&fns
, file
);
183 die("unable to set up default path; use --file");
185 if (credential_read(&c
, stdin
) < 0)
186 die("unable to read credential");
188 if (!strcmp(op
, "get"))
189 lookup_credential(&fns
, &c
);
190 else if (!strcmp(op
, "erase"))
191 remove_credential(&fns
, &c
);
192 else if (!strcmp(op
, "store"))
193 store_credential(&fns
, &c
);
195 ; /* Ignore unknown operation. */
197 string_list_clear(&fns
, 0);