5 #include "credential.h"
6 #include "string-list.h"
7 #include "parse-options.h"
8 #include "write-or-die.h"
10 static struct lock_file credential_lock
;
12 static int parse_credential_file(const char *fn
,
14 void (*match_cb
)(struct credential
*),
15 void (*other_cb
)(struct strbuf
*))
18 struct strbuf line
= STRBUF_INIT
;
19 struct credential entry
= CREDENTIAL_INIT
;
20 int found_credential
= 0;
24 if (errno
!= ENOENT
&& errno
!= EACCES
)
25 die_errno("unable to open %s", fn
);
26 return found_credential
;
29 while (strbuf_getline_lf(&line
, fh
) != EOF
) {
30 if (!credential_from_url_gently(&entry
, line
.buf
, 1) &&
31 entry
.username
&& entry
.password
&&
32 credential_match(c
, &entry
)) {
43 credential_clear(&entry
);
44 strbuf_release(&line
);
46 return found_credential
;
49 static void print_entry(struct credential
*c
)
51 printf("username=%s\n", c
->username
);
52 printf("password=%s\n", c
->password
);
55 static void print_line(struct strbuf
*buf
)
57 strbuf_addch(buf
, '\n');
58 write_or_die(get_lock_file_fd(&credential_lock
), buf
->buf
, buf
->len
);
61 static void rewrite_credential_file(const char *fn
, struct credential
*c
,
64 int timeout_ms
= 1000;
66 git_config_get_int("credentialstore.locktimeoutms", &timeout_ms
);
67 if (hold_lock_file_for_update_timeout(&credential_lock
, fn
, 0, timeout_ms
) < 0)
68 die_errno(_("unable to get credential storage lock in %d ms"), timeout_ms
);
71 parse_credential_file(fn
, c
, NULL
, print_line
);
72 if (commit_lock_file(&credential_lock
) < 0)
73 die_errno("unable to write credential store");
76 static void store_credential_file(const char *fn
, struct credential
*c
)
78 struct strbuf buf
= STRBUF_INIT
;
80 strbuf_addf(&buf
, "%s://", c
->protocol
);
81 strbuf_addstr_urlencode(&buf
, c
->username
, is_rfc3986_unreserved
);
82 strbuf_addch(&buf
, ':');
83 strbuf_addstr_urlencode(&buf
, c
->password
, is_rfc3986_unreserved
);
84 strbuf_addch(&buf
, '@');
86 strbuf_addstr_urlencode(&buf
, c
->host
, is_rfc3986_unreserved
);
88 strbuf_addch(&buf
, '/');
89 strbuf_addstr_urlencode(&buf
, c
->path
,
90 is_rfc3986_reserved_or_unreserved
);
93 rewrite_credential_file(fn
, c
, &buf
);
97 static void store_credential(const struct string_list
*fns
, struct credential
*c
)
99 struct string_list_item
*fn
;
102 * Sanity check that what we are storing is actually sensible.
103 * In particular, we can't make a URL without a protocol field.
104 * Without either a host or pathname (depending on the scheme),
105 * we have no primary key. And without a username and password,
106 * we are not actually storing a credential.
108 if (!c
->protocol
|| !(c
->host
|| c
->path
) || !c
->username
|| !c
->password
)
111 for_each_string_list_item(fn
, fns
)
112 if (!access(fn
->string
, F_OK
)) {
113 store_credential_file(fn
->string
, c
);
117 * Write credential to the filename specified by fns->items[0], thus
121 store_credential_file(fns
->items
[0].string
, c
);
124 static void remove_credential(const struct string_list
*fns
, struct credential
*c
)
126 struct string_list_item
*fn
;
129 * Sanity check that we actually have something to match
130 * against. The input we get is a restrictive pattern,
131 * so technically a blank credential means "erase everything".
132 * But it is too easy to accidentally send this, since it is equivalent
133 * to empty input. So explicitly disallow it, and require that the
134 * pattern have some actual content to match.
136 if (!c
->protocol
&& !c
->host
&& !c
->path
&& !c
->username
)
138 for_each_string_list_item(fn
, fns
)
139 if (!access(fn
->string
, F_OK
))
140 rewrite_credential_file(fn
->string
, c
, NULL
);
143 static void lookup_credential(const struct string_list
*fns
, struct credential
*c
)
145 struct string_list_item
*fn
;
147 for_each_string_list_item(fn
, fns
)
148 if (parse_credential_file(fn
->string
, c
, print_entry
, NULL
))
149 return; /* Found credential */
152 int cmd_credential_store(int argc
, const char **argv
, const char *prefix
)
154 const char * const usage
[] = {
155 "git credential-store [<options>] <action>",
159 struct credential c
= CREDENTIAL_INIT
;
160 struct string_list fns
= STRING_LIST_INIT_DUP
;
162 struct option options
[] = {
163 OPT_STRING(0, "file", &file
, "path",
164 "fetch and store credentials in <path>"),
170 argc
= parse_options(argc
, (const char **)argv
, prefix
, options
, usage
, 0);
172 usage_with_options(usage
, options
);
176 string_list_append(&fns
, file
);
178 if ((file
= interpolate_path("~/.git-credentials", 0)))
179 string_list_append_nodup(&fns
, file
);
180 file
= xdg_config_home("credentials");
182 string_list_append_nodup(&fns
, file
);
185 die("unable to set up default path; use --file");
187 if (credential_read(&c
, stdin
) < 0)
188 die("unable to read credential");
190 if (!strcmp(op
, "get"))
191 lookup_credential(&fns
, &c
);
192 else if (!strcmp(op
, "erase"))
193 remove_credential(&fns
, &c
);
194 else if (!strcmp(op
, "store"))
195 store_credential(&fns
, &c
);
197 ; /* Ignore unknown operation. */
199 string_list_clear(&fns
, 0);