3 #include "credential.h"
4 #include "string-list.h"
5 #include "parse-options.h"
7 static struct lock_file credential_lock
;
9 static int parse_credential_file(const char *fn
,
11 void (*match_cb
)(struct credential
*),
12 void (*other_cb
)(struct strbuf
*))
15 struct strbuf line
= STRBUF_INIT
;
16 struct credential entry
= CREDENTIAL_INIT
;
17 int found_credential
= 0;
21 if (errno
!= ENOENT
&& errno
!= EACCES
)
22 die_errno("unable to open %s", fn
);
23 return found_credential
;
26 while (strbuf_getline_lf(&line
, fh
) != EOF
) {
27 if (!credential_from_url_gently(&entry
, line
.buf
, 1) &&
28 entry
.username
&& entry
.password
&&
29 credential_match(c
, &entry
)) {
40 credential_clear(&entry
);
41 strbuf_release(&line
);
43 return found_credential
;
46 static void print_entry(struct credential
*c
)
48 printf("username=%s\n", c
->username
);
49 printf("password=%s\n", c
->password
);
52 static void print_line(struct strbuf
*buf
)
54 strbuf_addch(buf
, '\n');
55 write_or_die(get_lock_file_fd(&credential_lock
), buf
->buf
, buf
->len
);
58 static void rewrite_credential_file(const char *fn
, struct credential
*c
,
61 if (hold_lock_file_for_update(&credential_lock
, fn
, 0) < 0)
62 die_errno("unable to get credential storage lock");
65 parse_credential_file(fn
, c
, NULL
, print_line
);
66 if (commit_lock_file(&credential_lock
) < 0)
67 die_errno("unable to write credential store");
70 static void store_credential_file(const char *fn
, struct credential
*c
)
72 struct strbuf buf
= STRBUF_INIT
;
74 strbuf_addf(&buf
, "%s://", c
->protocol
);
75 strbuf_addstr_urlencode(&buf
, c
->username
, is_rfc3986_unreserved
);
76 strbuf_addch(&buf
, ':');
77 strbuf_addstr_urlencode(&buf
, c
->password
, is_rfc3986_unreserved
);
78 strbuf_addch(&buf
, '@');
80 strbuf_addstr_urlencode(&buf
, c
->host
, is_rfc3986_unreserved
);
82 strbuf_addch(&buf
, '/');
83 strbuf_addstr_urlencode(&buf
, c
->path
,
84 is_rfc3986_reserved_or_unreserved
);
87 rewrite_credential_file(fn
, c
, &buf
);
91 static void store_credential(const struct string_list
*fns
, struct credential
*c
)
93 struct string_list_item
*fn
;
96 * Sanity check that what we are storing is actually sensible.
97 * In particular, we can't make a URL without a protocol field.
98 * Without either a host or pathname (depending on the scheme),
99 * we have no primary key. And without a username and password,
100 * we are not actually storing a credential.
102 if (!c
->protocol
|| !(c
->host
|| c
->path
) || !c
->username
|| !c
->password
)
105 for_each_string_list_item(fn
, fns
)
106 if (!access(fn
->string
, F_OK
)) {
107 store_credential_file(fn
->string
, c
);
111 * Write credential to the filename specified by fns->items[0], thus
115 store_credential_file(fns
->items
[0].string
, c
);
118 static void remove_credential(const struct string_list
*fns
, struct credential
*c
)
120 struct string_list_item
*fn
;
123 * Sanity check that we actually have something to match
124 * against. The input we get is a restrictive pattern,
125 * so technically a blank credential means "erase everything".
126 * But it is too easy to accidentally send this, since it is equivalent
127 * to empty input. So explicitly disallow it, and require that the
128 * pattern have some actual content to match.
130 if (!c
->protocol
&& !c
->host
&& !c
->path
&& !c
->username
)
132 for_each_string_list_item(fn
, fns
)
133 if (!access(fn
->string
, F_OK
))
134 rewrite_credential_file(fn
->string
, c
, NULL
);
137 static void lookup_credential(const struct string_list
*fns
, struct credential
*c
)
139 struct string_list_item
*fn
;
141 for_each_string_list_item(fn
, fns
)
142 if (parse_credential_file(fn
->string
, c
, print_entry
, NULL
))
143 return; /* Found credential */
146 int cmd_main(int argc
, const char **argv
)
148 const char * const usage
[] = {
149 "git credential-store [<options>] <action>",
153 struct credential c
= CREDENTIAL_INIT
;
154 struct string_list fns
= STRING_LIST_INIT_DUP
;
156 struct option options
[] = {
157 OPT_STRING(0, "file", &file
, "path",
158 "fetch and store credentials in <path>"),
164 argc
= parse_options(argc
, (const char **)argv
, NULL
, options
, usage
, 0);
166 usage_with_options(usage
, options
);
170 string_list_append(&fns
, file
);
172 if ((file
= expand_user_path("~/.git-credentials", 0)))
173 string_list_append_nodup(&fns
, file
);
174 file
= xdg_config_home("credentials");
176 string_list_append_nodup(&fns
, file
);
179 die("unable to set up default path; use --file");
181 if (credential_read(&c
, stdin
) < 0)
182 die("unable to read credential");
184 if (!strcmp(op
, "get"))
185 lookup_credential(&fns
, &c
);
186 else if (!strcmp(op
, "erase"))
187 remove_credential(&fns
, &c
);
188 else if (!strcmp(op
, "store"))
189 store_credential(&fns
, &c
);
191 ; /* Ignore unknown operation. */
193 string_list_clear(&fns
, 0);