2 #include "credential.h"
3 #include "string-list.h"
4 #include "parse-options.h"
6 static struct lock_file credential_lock
;
8 static void parse_credential_file(const char *fn
,
10 void (*match_cb
)(struct credential
*),
11 void (*other_cb
)(struct strbuf
*))
14 struct strbuf line
= STRBUF_INIT
;
15 struct credential entry
= CREDENTIAL_INIT
;
20 die_errno("unable to open %s", fn
);
24 while (strbuf_getline(&line
, fh
, '\n') != EOF
) {
25 credential_from_url(&entry
, line
.buf
);
26 if (entry
.username
&& entry
.password
&&
27 credential_match(c
, &entry
)) {
37 credential_clear(&entry
);
38 strbuf_release(&line
);
42 static void print_entry(struct credential
*c
)
44 printf("username=%s\n", c
->username
);
45 printf("password=%s\n", c
->password
);
48 static void print_line(struct strbuf
*buf
)
50 strbuf_addch(buf
, '\n');
51 write_or_die(credential_lock
.fd
, buf
->buf
, buf
->len
);
54 static void rewrite_credential_file(const char *fn
, struct credential
*c
,
57 if (hold_lock_file_for_update(&credential_lock
, fn
, 0) < 0)
58 die_errno("unable to get credential storage lock");
61 parse_credential_file(fn
, c
, NULL
, print_line
);
62 if (commit_lock_file(&credential_lock
) < 0)
63 die_errno("unable to commit credential store");
66 static void store_credential(const char *fn
, struct credential
*c
)
68 struct strbuf buf
= STRBUF_INIT
;
71 * Sanity check that what we are storing is actually sensible.
72 * In particular, we can't make a URL without a protocol field.
73 * Without either a host or pathname (depending on the scheme),
74 * we have no primary key. And without a username and password,
75 * we are not actually storing a credential.
77 if (!c
->protocol
|| !(c
->host
|| c
->path
) ||
78 !c
->username
|| !c
->password
)
81 strbuf_addf(&buf
, "%s://", c
->protocol
);
82 strbuf_addstr_urlencode(&buf
, c
->username
, 1);
83 strbuf_addch(&buf
, ':');
84 strbuf_addstr_urlencode(&buf
, c
->password
, 1);
85 strbuf_addch(&buf
, '@');
87 strbuf_addstr_urlencode(&buf
, c
->host
, 1);
89 strbuf_addch(&buf
, '/');
90 strbuf_addstr_urlencode(&buf
, c
->path
, 0);
93 rewrite_credential_file(fn
, c
, &buf
);
97 static void remove_credential(const char *fn
, struct credential
*c
)
100 * Sanity check that we actually have something to match
101 * against. The input we get is a restrictive pattern,
102 * so technically a blank credential means "erase everything".
103 * But it is too easy to accidentally send this, since it is equivalent
104 * to empty input. So explicitly disallow it, and require that the
105 * pattern have some actual content to match.
107 if (c
->protocol
|| c
->host
|| c
->path
|| c
->username
)
108 rewrite_credential_file(fn
, c
, NULL
);
111 static int lookup_credential(const char *fn
, struct credential
*c
)
113 parse_credential_file(fn
, c
, print_entry
, NULL
);
114 return c
->username
&& c
->password
;
117 int main(int argc
, char **argv
)
119 const char * const usage
[] = {
120 "git credential-store [options] <action>",
124 struct credential c
= CREDENTIAL_INIT
;
126 struct option options
[] = {
127 OPT_STRING(0, "file", &file
, "path",
128 "fetch and store credentials in <path>"),
134 argc
= parse_options(argc
, (const char **)argv
, NULL
, options
, usage
, 0);
136 usage_with_options(usage
, options
);
140 file
= expand_user_path("~/.git-credentials");
142 die("unable to set up default path; use --file");
144 if (credential_read(&c
, stdin
) < 0)
145 die("unable to read credential");
147 if (!strcmp(op
, "get"))
148 lookup_credential(file
, &c
);
149 else if (!strcmp(op
, "erase"))
150 remove_credential(file
, &c
);
151 else if (!strcmp(op
, "store"))
152 store_credential(file
, &c
);
154 ; /* Ignore unknown operation. */