credential-store.c 5 KB
Newer Older
Jeff King's avatar
Jeff King committed
1
#include "cache.h"
2
#include "lockfile.h"
Jeff King's avatar
Jeff King committed
3 4 5 6 7 8
#include "credential.h"
#include "string-list.h"
#include "parse-options.h"

static struct lock_file credential_lock;

9
static int parse_credential_file(const char *fn,
Jeff King's avatar
Jeff King committed
10 11 12 13 14 15 16
				  struct credential *c,
				  void (*match_cb)(struct credential *),
				  void (*other_cb)(struct strbuf *))
{
	FILE *fh;
	struct strbuf line = STRBUF_INIT;
	struct credential entry = CREDENTIAL_INIT;
17
	int found_credential = 0;
Jeff King's avatar
Jeff King committed
18 19 20

	fh = fopen(fn, "r");
	if (!fh) {
21
		if (errno != ENOENT && errno != EACCES)
Jeff King's avatar
Jeff King committed
22
			die_errno("unable to open %s", fn);
23
		return found_credential;
Jeff King's avatar
Jeff King committed
24 25
	}

26
	while (strbuf_getline_lf(&line, fh) != EOF) {
Jeff King's avatar
Jeff King committed
27 28 29
		credential_from_url(&entry, line.buf);
		if (entry.username && entry.password &&
		    credential_match(c, &entry)) {
30
			found_credential = 1;
Jeff King's avatar
Jeff King committed
31 32 33 34 35 36 37 38 39 40 41 42
			if (match_cb) {
				match_cb(&entry);
				break;
			}
		}
		else if (other_cb)
			other_cb(&line);
	}

	credential_clear(&entry);
	strbuf_release(&line);
	fclose(fh);
43
	return found_credential;
Jeff King's avatar
Jeff King committed
44 45 46 47 48 49 50 51 52 53 54
}

static void print_entry(struct credential *c)
{
	printf("username=%s\n", c->username);
	printf("password=%s\n", c->password);
}

static void print_line(struct strbuf *buf)
{
	strbuf_addch(buf, '\n');
55
	write_or_die(get_lock_file_fd(&credential_lock), buf->buf, buf->len);
Jeff King's avatar
Jeff King committed
56 57 58 59 60 61 62 63 64 65 66
}

static void rewrite_credential_file(const char *fn, struct credential *c,
				    struct strbuf *extra)
{
	if (hold_lock_file_for_update(&credential_lock, fn, 0) < 0)
		die_errno("unable to get credential storage lock");
	if (extra)
		print_line(extra);
	parse_credential_file(fn, c, NULL, print_line);
	if (commit_lock_file(&credential_lock) < 0)
67
		die_errno("unable to write credential store");
Jeff King's avatar
Jeff King committed
68 69
}

70
static void store_credential_file(const char *fn, struct credential *c)
Jeff King's avatar
Jeff King committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
{
	struct strbuf buf = STRBUF_INIT;

	strbuf_addf(&buf, "%s://", c->protocol);
	strbuf_addstr_urlencode(&buf, c->username, 1);
	strbuf_addch(&buf, ':');
	strbuf_addstr_urlencode(&buf, c->password, 1);
	strbuf_addch(&buf, '@');
	if (c->host)
		strbuf_addstr_urlencode(&buf, c->host, 1);
	if (c->path) {
		strbuf_addch(&buf, '/');
		strbuf_addstr_urlencode(&buf, c->path, 0);
	}

	rewrite_credential_file(fn, c, &buf);
	strbuf_release(&buf);
}

90
static void store_credential(const struct string_list *fns, struct credential *c)
Jeff King's avatar
Jeff King committed
91
{
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
	struct string_list_item *fn;

	/*
	 * Sanity check that what we are storing is actually sensible.
	 * In particular, we can't make a URL without a protocol field.
	 * Without either a host or pathname (depending on the scheme),
	 * we have no primary key. And without a username and password,
	 * we are not actually storing a credential.
	 */
	if (!c->protocol || !(c->host || c->path) || !c->username || !c->password)
		return;

	for_each_string_list_item(fn, fns)
		if (!access(fn->string, F_OK)) {
			store_credential_file(fn->string, c);
			return;
		}
	/*
	 * Write credential to the filename specified by fns->items[0], thus
	 * creating it
	 */
	if (fns->nr)
		store_credential_file(fns->items[0].string, c);
}

static void remove_credential(const struct string_list *fns, struct credential *c)
Jeff King's avatar
Jeff King committed
118
{
119 120
	struct string_list_item *fn;

Jeff King's avatar
Jeff King committed
121 122 123 124 125 126 127 128
	/*
	 * Sanity check that we actually have something to match
	 * against. The input we get is a restrictive pattern,
	 * so technically a blank credential means "erase everything".
	 * But it is too easy to accidentally send this, since it is equivalent
	 * to empty input. So explicitly disallow it, and require that the
	 * pattern have some actual content to match.
	 */
129 130 131 132 133
	if (!c->protocol && !c->host && !c->path && !c->username)
		return;
	for_each_string_list_item(fn, fns)
		if (!access(fn->string, F_OK))
			rewrite_credential_file(fn->string, c, NULL);
Jeff King's avatar
Jeff King committed
134 135
}

136
static void lookup_credential(const struct string_list *fns, struct credential *c)
Jeff King's avatar
Jeff King committed
137
{
138 139 140 141 142
	struct string_list_item *fn;

	for_each_string_list_item(fn, fns)
		if (parse_credential_file(fn->string, c, print_entry, NULL))
			return; /* Found credential */
Jeff King's avatar
Jeff King committed
143 144
}

145
int cmd_main(int argc, const char **argv)
Jeff King's avatar
Jeff King committed
146 147
{
	const char * const usage[] = {
148
		"git credential-store [<options>] <action>",
Jeff King's avatar
Jeff King committed
149 150 151 152
		NULL
	};
	const char *op;
	struct credential c = CREDENTIAL_INIT;
153
	struct string_list fns = STRING_LIST_INIT_DUP;
Jeff King's avatar
Jeff King committed
154 155 156 157 158 159 160 161 162
	char *file = NULL;
	struct option options[] = {
		OPT_STRING(0, "file", &file, "path",
			   "fetch and store credentials in <path>"),
		OPT_END()
	};

	umask(077);

163
	argc = parse_options(argc, (const char **)argv, NULL, options, usage, 0);
Jeff King's avatar
Jeff King committed
164 165 166 167
	if (argc != 1)
		usage_with_options(usage, options);
	op = argv[0];

168
	if (file) {
169
		string_list_append(&fns, file);
170
	} else {
171
		if ((file = expand_user_path("~/.git-credentials", 0)))
172
			string_list_append_nodup(&fns, file);
173
		file = xdg_config_home("credentials");
174 175 176 177
		if (file)
			string_list_append_nodup(&fns, file);
	}
	if (!fns.nr)
Jeff King's avatar
Jeff King committed
178 179 180 181 182 183
		die("unable to set up default path; use --file");

	if (credential_read(&c, stdin) < 0)
		die("unable to read credential");

	if (!strcmp(op, "get"))
184
		lookup_credential(&fns, &c);
Jeff King's avatar
Jeff King committed
185
	else if (!strcmp(op, "erase"))
186
		remove_credential(&fns, &c);
Jeff King's avatar
Jeff King committed
187
	else if (!strcmp(op, "store"))
188
		store_credential(&fns, &c);
Jeff King's avatar
Jeff King committed
189 190 191
	else
		; /* Ignore unknown operation. */

192
	string_list_clear(&fns, 0);
Jeff King's avatar
Jeff King committed
193 194
	return 0;
}