From b5dd96b70aca364f163f0b3743418779cfe062e6 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Thu, 13 Aug 2020 10:58:55 -0400 Subject: make credential helpers builtins There's no real reason for credential helpers to be separate binaries. I did them this way originally under the notion that helper don't _need_ to be part of Git, and so can be built totally separately (and indeed, the ones in contrib/credential are). But the ones in our main Makefile build on libgit.a, and the resulting binaries are reasonably large. We can slim down our total disk footprint by just making them builtins. This reduces the size of: make strip install from 29MB to 24MB on my Debian system. Note that credential-cache can't operate without support for Unix sockets. Currently we just don't build it at all when NO_UNIX_SOCKETS is set. We could continue that with conditionals in the Makefile and our list of builtins. But instead, let's build a dummy implementation that dies with an informative message. That has two advantages: - it's simpler, because the conditional bits are all kept inside the credential-cache source - a user who is expecting it to exist will be told _why_ they can't use it, rather than getting the "credential-cache is not a git command" error which makes it look like the Git install is broken. Note that our dummy implementation does still respond to "-h" in order to appease t0012 (and this may be a little friendlier for users, as well). Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- Makefile | 8 +- builtin.h | 3 + builtin/credential-cache--daemon.c | 318 ++++++++++++++++++++++++++++++++++++ builtin/credential-cache.c | 157 ++++++++++++++++++ builtin/credential-store.c | 195 ++++++++++++++++++++++ contrib/buildsystems/CMakeLists.txt | 20 +-- credential-cache--daemon.c | 297 --------------------------------- credential-cache.c | 136 --------------- credential-store.c | 195 ---------------------- git.c | 3 + 10 files changed, 680 insertions(+), 652 deletions(-) create mode 100644 builtin/credential-cache--daemon.c create mode 100644 builtin/credential-cache.c create mode 100644 builtin/credential-store.c delete mode 100644 credential-cache--daemon.c delete mode 100644 credential-cache.c delete mode 100644 credential-store.c diff --git a/Makefile b/Makefile index 271b96348e..5b43c0fafb 100644 --- a/Makefile +++ b/Makefile @@ -672,7 +672,6 @@ EXTRA_PROGRAMS = PROGRAMS += $(EXTRA_PROGRAMS) PROGRAM_OBJS += bugreport.o -PROGRAM_OBJS += credential-store.o PROGRAM_OBJS += daemon.o PROGRAM_OBJS += fast-import.o PROGRAM_OBJS += http-backend.o @@ -1052,6 +1051,9 @@ BUILTIN_OBJS += builtin/checkout-index.o BUILTIN_OBJS += builtin/checkout.o BUILTIN_OBJS += builtin/clean.o BUILTIN_OBJS += builtin/clone.o +BUILTIN_OBJS += builtin/credential-cache.o +BUILTIN_OBJS += builtin/credential-cache--daemon.o +BUILTIN_OBJS += builtin/credential-store.o BUILTIN_OBJS += builtin/column.o BUILTIN_OBJS += builtin/commit-graph.o BUILTIN_OBJS += builtin/commit-tree.o @@ -1634,11 +1636,8 @@ ifdef NO_INET_PTON endif ifdef NO_UNIX_SOCKETS BASIC_CFLAGS += -DNO_UNIX_SOCKETS - EXCLUDED_PROGRAMS += git-credential-cache git-credential-cache--daemon else LIB_OBJS += unix-socket.o - PROGRAM_OBJS += credential-cache.o - PROGRAM_OBJS += credential-cache--daemon.o endif ifdef NO_ICONV @@ -2901,7 +2900,6 @@ ifdef MSVC # because it is just a copy/hardlink of git.exe, rather than a unique binary. $(INSTALL) git.pdb '$(DESTDIR_SQ)$(bindir_SQ)' $(INSTALL) git-shell.pdb '$(DESTDIR_SQ)$(bindir_SQ)' - $(INSTALL) git-credential-store.pdb '$(DESTDIR_SQ)$(gitexec_instdir_SQ)' $(INSTALL) git-daemon.pdb '$(DESTDIR_SQ)$(gitexec_instdir_SQ)' $(INSTALL) git-fast-import.pdb '$(DESTDIR_SQ)$(gitexec_instdir_SQ)' $(INSTALL) git-http-backend.pdb '$(DESTDIR_SQ)$(gitexec_instdir_SQ)' diff --git a/builtin.h b/builtin.h index a5ae15bfe5..4a0aed5448 100644 --- a/builtin.h +++ b/builtin.h @@ -138,6 +138,9 @@ int cmd_commit_tree(int argc, const char **argv, const char *prefix); int cmd_config(int argc, const char **argv, const char *prefix); int cmd_count_objects(int argc, const char **argv, const char *prefix); int cmd_credential(int argc, const char **argv, const char *prefix); +int cmd_credential_cache(int argc, const char **argv, const char *prefix); +int cmd_credential_cache_daemon(int argc, const char **argv, const char *prefix); +int cmd_credential_store(int argc, const char **argv, const char *prefix); int cmd_describe(int argc, const char **argv, const char *prefix); int cmd_diff_files(int argc, const char **argv, const char *prefix); int cmd_diff_index(int argc, const char **argv, const char *prefix); diff --git a/builtin/credential-cache--daemon.c b/builtin/credential-cache--daemon.c new file mode 100644 index 0000000000..c61f123a3b --- /dev/null +++ b/builtin/credential-cache--daemon.c @@ -0,0 +1,318 @@ +#include "builtin.h" +#include "parse-options.h" + +#ifndef NO_UNIX_SOCKETS + +#include "config.h" +#include "tempfile.h" +#include "credential.h" +#include "unix-socket.h" + +struct credential_cache_entry { + struct credential item; + timestamp_t expiration; +}; +static struct credential_cache_entry *entries; +static int entries_nr; +static int entries_alloc; + +static void cache_credential(struct credential *c, int timeout) +{ + struct credential_cache_entry *e; + + ALLOC_GROW(entries, entries_nr + 1, entries_alloc); + e = &entries[entries_nr++]; + + /* take ownership of pointers */ + memcpy(&e->item, c, sizeof(*c)); + memset(c, 0, sizeof(*c)); + e->expiration = time(NULL) + timeout; +} + +static struct credential_cache_entry *lookup_credential(const struct credential *c) +{ + int i; + for (i = 0; i < entries_nr; i++) { + struct credential *e = &entries[i].item; + if (credential_match(c, e)) + return &entries[i]; + } + return NULL; +} + +static void remove_credential(const struct credential *c) +{ + struct credential_cache_entry *e; + + e = lookup_credential(c); + if (e) + e->expiration = 0; +} + +static timestamp_t check_expirations(void) +{ + static timestamp_t wait_for_entry_until; + int i = 0; + timestamp_t now = time(NULL); + timestamp_t next = TIME_MAX; + + /* + * Initially give the client 30 seconds to actually contact us + * and store a credential before we decide there's no point in + * keeping the daemon around. + */ + if (!wait_for_entry_until) + wait_for_entry_until = now + 30; + + while (i < entries_nr) { + if (entries[i].expiration <= now) { + entries_nr--; + credential_clear(&entries[i].item); + if (i != entries_nr) + memcpy(&entries[i], &entries[entries_nr], sizeof(*entries)); + /* + * Stick around 30 seconds in case a new credential + * shows up (e.g., because we just removed a failed + * one, and we will soon get the correct one). + */ + wait_for_entry_until = now + 30; + } + else { + if (entries[i].expiration < next) + next = entries[i].expiration; + i++; + } + } + + if (!entries_nr) { + if (wait_for_entry_until <= now) + return 0; + next = wait_for_entry_until; + } + + return next - now; +} + +static int read_request(FILE *fh, struct credential *c, + struct strbuf *action, int *timeout) +{ + static struct strbuf item = STRBUF_INIT; + const char *p; + + strbuf_getline_lf(&item, fh); + if (!skip_prefix(item.buf, "action=", &p)) + return error("client sent bogus action line: %s", item.buf); + strbuf_addstr(action, p); + + strbuf_getline_lf(&item, fh); + if (!skip_prefix(item.buf, "timeout=", &p)) + return error("client sent bogus timeout line: %s", item.buf); + *timeout = atoi(p); + + if (credential_read(c, fh) < 0) + return -1; + return 0; +} + +static void serve_one_client(FILE *in, FILE *out) +{ + struct credential c = CREDENTIAL_INIT; + struct strbuf action = STRBUF_INIT; + int timeout = -1; + + if (read_request(in, &c, &action, &timeout) < 0) + /* ignore error */ ; + else if (!strcmp(action.buf, "get")) { + struct credential_cache_entry *e = lookup_credential(&c); + if (e) { + fprintf(out, "username=%s\n", e->item.username); + fprintf(out, "password=%s\n", e->item.password); + } + } + else if (!strcmp(action.buf, "exit")) { + /* + * It's important that we clean up our socket first, and then + * signal the client only once we have finished the cleanup. + * Calling exit() directly does this, because we clean up in + * our atexit() handler, and then signal the client when our + * process actually ends, which closes the socket and gives + * them EOF. + */ + exit(0); + } + else if (!strcmp(action.buf, "erase")) + remove_credential(&c); + else if (!strcmp(action.buf, "store")) { + if (timeout < 0) + warning("cache client didn't specify a timeout"); + else if (!c.username || !c.password) + warning("cache client gave us a partial credential"); + else { + remove_credential(&c); + cache_credential(&c, timeout); + } + } + else + warning("cache client sent unknown action: %s", action.buf); + + credential_clear(&c); + strbuf_release(&action); +} + +static int serve_cache_loop(int fd) +{ + struct pollfd pfd; + timestamp_t wakeup; + + wakeup = check_expirations(); + if (!wakeup) + return 0; + + pfd.fd = fd; + pfd.events = POLLIN; + if (poll(&pfd, 1, 1000 * wakeup) < 0) { + if (errno != EINTR) + die_errno("poll failed"); + return 1; + } + + if (pfd.revents & POLLIN) { + int client, client2; + FILE *in, *out; + + client = accept(fd, NULL, NULL); + if (client < 0) { + warning_errno("accept failed"); + return 1; + } + client2 = dup(client); + if (client2 < 0) { + warning_errno("dup failed"); + close(client); + return 1; + } + + in = xfdopen(client, "r"); + out = xfdopen(client2, "w"); + serve_one_client(in, out); + fclose(in); + fclose(out); + } + return 1; +} + +static void serve_cache(const char *socket_path, int debug) +{ + int fd; + + fd = unix_stream_listen(socket_path); + if (fd < 0) + die_errno("unable to bind to '%s'", socket_path); + + printf("ok\n"); + fclose(stdout); + if (!debug) { + if (!freopen("/dev/null", "w", stderr)) + die_errno("unable to point stderr to /dev/null"); + } + + while (serve_cache_loop(fd)) + ; /* nothing */ + + close(fd); +} + +static const char permissions_advice[] = N_( +"The permissions on your socket directory are too loose; other\n" +"users may be able to read your cached credentials. Consider running:\n" +"\n" +" chmod 0700 %s"); +static void init_socket_directory(const char *path) +{ + struct stat st; + char *path_copy = xstrdup(path); + char *dir = dirname(path_copy); + + if (!stat(dir, &st)) { + if (st.st_mode & 077) + die(_(permissions_advice), dir); + } else { + /* + * We must be sure to create the directory with the correct mode, + * not just chmod it after the fact; otherwise, there is a race + * condition in which somebody can chdir to it, sleep, then try to open + * our protected socket. + */ + if (safe_create_leading_directories_const(dir) < 0) + die_errno("unable to create directories for '%s'", dir); + if (mkdir(dir, 0700) < 0) + die_errno("unable to mkdir '%s'", dir); + } + + if (chdir(dir)) + /* + * We don't actually care what our cwd is; we chdir here just to + * be a friendly daemon and avoid tying up our original cwd. + * If this fails, it's OK to just continue without that benefit. + */ + ; + + free(path_copy); +} + +int cmd_credential_cache_daemon(int argc, const char **argv, const char *prefix) +{ + struct tempfile *socket_file; + const char *socket_path; + int ignore_sighup = 0; + static const char *usage[] = { + "git-credential-cache--daemon [opts] ", + NULL + }; + int debug = 0; + const struct option options[] = { + OPT_BOOL(0, "debug", &debug, + N_("print debugging messages to stderr")), + OPT_END() + }; + + git_config_get_bool("credentialcache.ignoresighup", &ignore_sighup); + + argc = parse_options(argc, argv, prefix, options, usage, 0); + socket_path = argv[0]; + + if (!socket_path) + usage_with_options(usage, options); + + if (!is_absolute_path(socket_path)) + die("socket directory must be an absolute path"); + + init_socket_directory(socket_path); + socket_file = register_tempfile(socket_path); + + if (ignore_sighup) + signal(SIGHUP, SIG_IGN); + + serve_cache(socket_path, debug); + delete_tempfile(&socket_file); + + return 0; +} + +#else + +int cmd_credential_cache_daemon(int argc, const char **argv, const char *prefix) +{ + const char * const usage[] = { + "git credential-cache--daemon [options] ", + "", + "credential-cache--daemon is disabled in this build of Git", + NULL + }; + struct option options[] = { OPT_END() }; + + argc = parse_options(argc, argv, prefix, options, usage, 0); + die(_("credential-cache--daemon unavailable; no unix socket support")); +} + +#endif /* NO_UNIX_SOCKET */ diff --git a/builtin/credential-cache.c b/builtin/credential-cache.c new file mode 100644 index 0000000000..d0fafdeb9e --- /dev/null +++ b/builtin/credential-cache.c @@ -0,0 +1,157 @@ +#include "builtin.h" +#include "parse-options.h" + +#ifndef NO_UNIX_SOCKETS + +#include "credential.h" +#include "string-list.h" +#include "unix-socket.h" +#include "run-command.h" + +#define FLAG_SPAWN 0x1 +#define FLAG_RELAY 0x2 + +static int send_request(const char *socket, const struct strbuf *out) +{ + int got_data = 0; + int fd = unix_stream_connect(socket); + + if (fd < 0) + return -1; + + if (write_in_full(fd, out->buf, out->len) < 0) + die_errno("unable to write to cache daemon"); + shutdown(fd, SHUT_WR); + + while (1) { + char in[1024]; + int r; + + r = read_in_full(fd, in, sizeof(in)); + if (r == 0 || (r < 0 && errno == ECONNRESET)) + break; + if (r < 0) + die_errno("read error from cache daemon"); + write_or_die(1, in, r); + got_data = 1; + } + close(fd); + return got_data; +} + +static void spawn_daemon(const char *socket) +{ + struct child_process daemon = CHILD_PROCESS_INIT; + const char *argv[] = { NULL, NULL, NULL }; + char buf[128]; + int r; + + argv[0] = "git-credential-cache--daemon"; + argv[1] = socket; + daemon.argv = argv; + daemon.no_stdin = 1; + daemon.out = -1; + + if (start_command(&daemon)) + die_errno("unable to start cache daemon"); + r = read_in_full(daemon.out, buf, sizeof(buf)); + if (r < 0) + die_errno("unable to read result code from cache daemon"); + if (r != 3 || memcmp(buf, "ok\n", 3)) + die("cache daemon did not start: %.*s", r, buf); + close(daemon.out); +} + +static void do_cache(const char *socket, const char *action, int timeout, + int flags) +{ + struct strbuf buf = STRBUF_INIT; + + strbuf_addf(&buf, "action=%s\n", action); + strbuf_addf(&buf, "timeout=%d\n", timeout); + if (flags & FLAG_RELAY) { + if (strbuf_read(&buf, 0, 0) < 0) + die_errno("unable to relay credential"); + } + + if (send_request(socket, &buf) < 0) { + if (errno != ENOENT && errno != ECONNREFUSED) + die_errno("unable to connect to cache daemon"); + if (flags & FLAG_SPAWN) { + spawn_daemon(socket); + if (send_request(socket, &buf) < 0) + die_errno("unable to connect to cache daemon"); + } + } + strbuf_release(&buf); +} + +static char *get_socket_path(void) +{ + struct stat sb; + char *old_dir, *socket; + old_dir = expand_user_path("~/.git-credential-cache", 0); + if (old_dir && !stat(old_dir, &sb) && S_ISDIR(sb.st_mode)) + socket = xstrfmt("%s/socket", old_dir); + else + socket = xdg_cache_home("credential/socket"); + free(old_dir); + return socket; +} + +int cmd_credential_cache(int argc, const char **argv, const char *prefix) +{ + char *socket_path = NULL; + int timeout = 900; + const char *op; + const char * const usage[] = { + "git credential-cache [] ", + NULL + }; + struct option options[] = { + OPT_INTEGER(0, "timeout", &timeout, + "number of seconds to cache credentials"), + OPT_STRING(0, "socket", &socket_path, "path", + "path of cache-daemon socket"), + OPT_END() + }; + + argc = parse_options(argc, argv, prefix, options, usage, 0); + if (!argc) + usage_with_options(usage, options); + op = argv[0]; + + if (!socket_path) + socket_path = get_socket_path(); + if (!socket_path) + die("unable to find a suitable socket path; use --socket"); + + if (!strcmp(op, "exit")) + do_cache(socket_path, op, timeout, 0); + else if (!strcmp(op, "get") || !strcmp(op, "erase")) + do_cache(socket_path, op, timeout, FLAG_RELAY); + else if (!strcmp(op, "store")) + do_cache(socket_path, op, timeout, FLAG_RELAY|FLAG_SPAWN); + else + ; /* ignore unknown operation */ + + return 0; +} + +#else + +int cmd_credential_cache(int argc, const char **argv, const char *prefix) +{ + const char * const usage[] = { + "git credential-cache [options] ", + "", + "credential-cache is disabled in this build of Git", + NULL + }; + struct option options[] = { OPT_END() }; + + argc = parse_options(argc, argv, prefix, options, usage, 0); + die(_("credential-cache unavailable; no unix socket support")); +} + +#endif /* NO_UNIX_SOCKETS */ diff --git a/builtin/credential-store.c b/builtin/credential-store.c new file mode 100644 index 0000000000..5331ab151a --- /dev/null +++ b/builtin/credential-store.c @@ -0,0 +1,195 @@ +#include "builtin.h" +#include "lockfile.h" +#include "credential.h" +#include "string-list.h" +#include "parse-options.h" + +static struct lock_file credential_lock; + +static int parse_credential_file(const char *fn, + 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; + int found_credential = 0; + + fh = fopen(fn, "r"); + if (!fh) { + if (errno != ENOENT && errno != EACCES) + die_errno("unable to open %s", fn); + return found_credential; + } + + while (strbuf_getline_lf(&line, fh) != EOF) { + if (!credential_from_url_gently(&entry, line.buf, 1) && + entry.username && entry.password && + credential_match(c, &entry)) { + found_credential = 1; + if (match_cb) { + match_cb(&entry); + break; + } + } + else if (other_cb) + other_cb(&line); + } + + credential_clear(&entry); + strbuf_release(&line); + fclose(fh); + return found_credential; +} + +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'); + write_or_die(get_lock_file_fd(&credential_lock), buf->buf, buf->len); +} + +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) + die_errno("unable to write credential store"); +} + +static void store_credential_file(const char *fn, struct credential *c) +{ + struct strbuf buf = STRBUF_INIT; + + strbuf_addf(&buf, "%s://", c->protocol); + strbuf_addstr_urlencode(&buf, c->username, is_rfc3986_unreserved); + strbuf_addch(&buf, ':'); + strbuf_addstr_urlencode(&buf, c->password, is_rfc3986_unreserved); + strbuf_addch(&buf, '@'); + if (c->host) + strbuf_addstr_urlencode(&buf, c->host, is_rfc3986_unreserved); + if (c->path) { + strbuf_addch(&buf, '/'); + strbuf_addstr_urlencode(&buf, c->path, + is_rfc3986_reserved_or_unreserved); + } + + rewrite_credential_file(fn, c, &buf); + strbuf_release(&buf); +} + +static void store_credential(const struct string_list *fns, struct credential *c) +{ + 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) +{ + struct string_list_item *fn; + + /* + * 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. + */ + 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); +} + +static void lookup_credential(const struct string_list *fns, struct credential *c) +{ + 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 */ +} + +int cmd_credential_store(int argc, const char **argv, const char *prefix) +{ + const char * const usage[] = { + "git credential-store [] ", + NULL + }; + const char *op; + struct credential c = CREDENTIAL_INIT; + struct string_list fns = STRING_LIST_INIT_DUP; + char *file = NULL; + struct option options[] = { + OPT_STRING(0, "file", &file, "path", + "fetch and store credentials in "), + OPT_END() + }; + + umask(077); + + argc = parse_options(argc, (const char **)argv, prefix, options, usage, 0); + if (argc != 1) + usage_with_options(usage, options); + op = argv[0]; + + if (file) { + string_list_append(&fns, file); + } else { + if ((file = expand_user_path("~/.git-credentials", 0))) + string_list_append_nodup(&fns, file); + file = xdg_config_home("credentials"); + if (file) + string_list_append_nodup(&fns, file); + } + if (!fns.nr) + die("unable to set up default path; use --file"); + + if (credential_read(&c, stdin) < 0) + die("unable to read credential"); + + if (!strcmp(op, "get")) + lookup_credential(&fns, &c); + else if (!strcmp(op, "erase")) + remove_credential(&fns, &c); + else if (!strcmp(op, "store")) + store_credential(&fns, &c); + else + ; /* Ignore unknown operation. */ + + string_list_clear(&fns, 0); + return 0; +} diff --git a/contrib/buildsystems/CMakeLists.txt b/contrib/buildsystems/CMakeLists.txt index 47215df25b..4be61247e5 100644 --- a/contrib/buildsystems/CMakeLists.txt +++ b/contrib/buildsystems/CMakeLists.txt @@ -501,15 +501,9 @@ unset(CMAKE_REQUIRED_INCLUDES) #programs set(PROGRAMS_BUILT - git git-bugreport git-credential-store git-daemon git-fast-import git-http-backend git-sh-i18n--envsubst + git git-bugreport git-daemon git-fast-import git-http-backend git-sh-i18n--envsubst git-shell git-remote-testsvn) -if(NO_UNIX_SOCKETS) - list(APPEND excluded_progs git-credential-cache git-credential-cache--daemon) -else() - list(APPEND PROGRAMS_BUILT git-credential-cache git-credential-cache--daemon) -endif() - if(NOT CURL_FOUND) list(APPEND excluded_progs git-http-fetch git-http-push) add_compile_definitions(NO_CURL) @@ -633,9 +627,6 @@ target_link_libraries(git common-main) add_executable(git-bugreport ${CMAKE_SOURCE_DIR}/bugreport.c) target_link_libraries(git-bugreport common-main) -add_executable(git-credential-store ${CMAKE_SOURCE_DIR}/credential-store.c) -target_link_libraries(git-credential-store common-main) - add_executable(git-daemon ${CMAKE_SOURCE_DIR}/daemon.c) target_link_libraries(git-daemon common-main) @@ -672,15 +663,6 @@ endif() add_executable(git-remote-testsvn ${CMAKE_SOURCE_DIR}/remote-testsvn.c) target_link_libraries(git-remote-testsvn common-main vcs-svn) -if(NOT NO_UNIX_SOCKETS) - add_executable(git-credential-cache ${CMAKE_SOURCE_DIR}/credential-cache.c) - target_link_libraries(git-credential-cache common-main) - - add_executable(git-credential-cache--daemon ${CMAKE_SOURCE_DIR}/credential-cache--daemon.c) - target_link_libraries(git-credential-cache--daemon common-main) -endif() - - set(git_builtin_extra cherry cherry-pick format-patch fsck-objects init merge-subtree restore show diff --git a/credential-cache--daemon.c b/credential-cache--daemon.c deleted file mode 100644 index ec1271f89c..0000000000 --- a/credential-cache--daemon.c +++ /dev/null @@ -1,297 +0,0 @@ -#include "cache.h" -#include "config.h" -#include "tempfile.h" -#include "credential.h" -#include "unix-socket.h" -#include "parse-options.h" - -struct credential_cache_entry { - struct credential item; - timestamp_t expiration; -}; -static struct credential_cache_entry *entries; -static int entries_nr; -static int entries_alloc; - -static void cache_credential(struct credential *c, int timeout) -{ - struct credential_cache_entry *e; - - ALLOC_GROW(entries, entries_nr + 1, entries_alloc); - e = &entries[entries_nr++]; - - /* take ownership of pointers */ - memcpy(&e->item, c, sizeof(*c)); - memset(c, 0, sizeof(*c)); - e->expiration = time(NULL) + timeout; -} - -static struct credential_cache_entry *lookup_credential(const struct credential *c) -{ - int i; - for (i = 0; i < entries_nr; i++) { - struct credential *e = &entries[i].item; - if (credential_match(c, e)) - return &entries[i]; - } - return NULL; -} - -static void remove_credential(const struct credential *c) -{ - struct credential_cache_entry *e; - - e = lookup_credential(c); - if (e) - e->expiration = 0; -} - -static timestamp_t check_expirations(void) -{ - static timestamp_t wait_for_entry_until; - int i = 0; - timestamp_t now = time(NULL); - timestamp_t next = TIME_MAX; - - /* - * Initially give the client 30 seconds to actually contact us - * and store a credential before we decide there's no point in - * keeping the daemon around. - */ - if (!wait_for_entry_until) - wait_for_entry_until = now + 30; - - while (i < entries_nr) { - if (entries[i].expiration <= now) { - entries_nr--; - credential_clear(&entries[i].item); - if (i != entries_nr) - memcpy(&entries[i], &entries[entries_nr], sizeof(*entries)); - /* - * Stick around 30 seconds in case a new credential - * shows up (e.g., because we just removed a failed - * one, and we will soon get the correct one). - */ - wait_for_entry_until = now + 30; - } - else { - if (entries[i].expiration < next) - next = entries[i].expiration; - i++; - } - } - - if (!entries_nr) { - if (wait_for_entry_until <= now) - return 0; - next = wait_for_entry_until; - } - - return next - now; -} - -static int read_request(FILE *fh, struct credential *c, - struct strbuf *action, int *timeout) -{ - static struct strbuf item = STRBUF_INIT; - const char *p; - - strbuf_getline_lf(&item, fh); - if (!skip_prefix(item.buf, "action=", &p)) - return error("client sent bogus action line: %s", item.buf); - strbuf_addstr(action, p); - - strbuf_getline_lf(&item, fh); - if (!skip_prefix(item.buf, "timeout=", &p)) - return error("client sent bogus timeout line: %s", item.buf); - *timeout = atoi(p); - - if (credential_read(c, fh) < 0) - return -1; - return 0; -} - -static void serve_one_client(FILE *in, FILE *out) -{ - struct credential c = CREDENTIAL_INIT; - struct strbuf action = STRBUF_INIT; - int timeout = -1; - - if (read_request(in, &c, &action, &timeout) < 0) - /* ignore error */ ; - else if (!strcmp(action.buf, "get")) { - struct credential_cache_entry *e = lookup_credential(&c); - if (e) { - fprintf(out, "username=%s\n", e->item.username); - fprintf(out, "password=%s\n", e->item.password); - } - } - else if (!strcmp(action.buf, "exit")) { - /* - * It's important that we clean up our socket first, and then - * signal the client only once we have finished the cleanup. - * Calling exit() directly does this, because we clean up in - * our atexit() handler, and then signal the client when our - * process actually ends, which closes the socket and gives - * them EOF. - */ - exit(0); - } - else if (!strcmp(action.buf, "erase")) - remove_credential(&c); - else if (!strcmp(action.buf, "store")) { - if (timeout < 0) - warning("cache client didn't specify a timeout"); - else if (!c.username || !c.password) - warning("cache client gave us a partial credential"); - else { - remove_credential(&c); - cache_credential(&c, timeout); - } - } - else - warning("cache client sent unknown action: %s", action.buf); - - credential_clear(&c); - strbuf_release(&action); -} - -static int serve_cache_loop(int fd) -{ - struct pollfd pfd; - timestamp_t wakeup; - - wakeup = check_expirations(); - if (!wakeup) - return 0; - - pfd.fd = fd; - pfd.events = POLLIN; - if (poll(&pfd, 1, 1000 * wakeup) < 0) { - if (errno != EINTR) - die_errno("poll failed"); - return 1; - } - - if (pfd.revents & POLLIN) { - int client, client2; - FILE *in, *out; - - client = accept(fd, NULL, NULL); - if (client < 0) { - warning_errno("accept failed"); - return 1; - } - client2 = dup(client); - if (client2 < 0) { - warning_errno("dup failed"); - close(client); - return 1; - } - - in = xfdopen(client, "r"); - out = xfdopen(client2, "w"); - serve_one_client(in, out); - fclose(in); - fclose(out); - } - return 1; -} - -static void serve_cache(const char *socket_path, int debug) -{ - int fd; - - fd = unix_stream_listen(socket_path); - if (fd < 0) - die_errno("unable to bind to '%s'", socket_path); - - printf("ok\n"); - fclose(stdout); - if (!debug) { - if (!freopen("/dev/null", "w", stderr)) - die_errno("unable to point stderr to /dev/null"); - } - - while (serve_cache_loop(fd)) - ; /* nothing */ - - close(fd); -} - -static const char permissions_advice[] = N_( -"The permissions on your socket directory are too loose; other\n" -"users may be able to read your cached credentials. Consider running:\n" -"\n" -" chmod 0700 %s"); -static void init_socket_directory(const char *path) -{ - struct stat st; - char *path_copy = xstrdup(path); - char *dir = dirname(path_copy); - - if (!stat(dir, &st)) { - if (st.st_mode & 077) - die(_(permissions_advice), dir); - } else { - /* - * We must be sure to create the directory with the correct mode, - * not just chmod it after the fact; otherwise, there is a race - * condition in which somebody can chdir to it, sleep, then try to open - * our protected socket. - */ - if (safe_create_leading_directories_const(dir) < 0) - die_errno("unable to create directories for '%s'", dir); - if (mkdir(dir, 0700) < 0) - die_errno("unable to mkdir '%s'", dir); - } - - if (chdir(dir)) - /* - * We don't actually care what our cwd is; we chdir here just to - * be a friendly daemon and avoid tying up our original cwd. - * If this fails, it's OK to just continue without that benefit. - */ - ; - - free(path_copy); -} - -int cmd_main(int argc, const char **argv) -{ - struct tempfile *socket_file; - const char *socket_path; - int ignore_sighup = 0; - static const char *usage[] = { - "git-credential-cache--daemon [opts] ", - NULL - }; - int debug = 0; - const struct option options[] = { - OPT_BOOL(0, "debug", &debug, - N_("print debugging messages to stderr")), - OPT_END() - }; - - git_config_get_bool("credentialcache.ignoresighup", &ignore_sighup); - - argc = parse_options(argc, argv, NULL, options, usage, 0); - socket_path = argv[0]; - - if (!socket_path) - usage_with_options(usage, options); - - if (!is_absolute_path(socket_path)) - die("socket directory must be an absolute path"); - - init_socket_directory(socket_path); - socket_file = register_tempfile(socket_path); - - if (ignore_sighup) - signal(SIGHUP, SIG_IGN); - - serve_cache(socket_path, debug); - delete_tempfile(&socket_file); - - return 0; -} diff --git a/credential-cache.c b/credential-cache.c deleted file mode 100644 index 1cccc3a0b9..0000000000 --- a/credential-cache.c +++ /dev/null @@ -1,136 +0,0 @@ -#include "cache.h" -#include "credential.h" -#include "string-list.h" -#include "parse-options.h" -#include "unix-socket.h" -#include "run-command.h" - -#define FLAG_SPAWN 0x1 -#define FLAG_RELAY 0x2 - -static int send_request(const char *socket, const struct strbuf *out) -{ - int got_data = 0; - int fd = unix_stream_connect(socket); - - if (fd < 0) - return -1; - - if (write_in_full(fd, out->buf, out->len) < 0) - die_errno("unable to write to cache daemon"); - shutdown(fd, SHUT_WR); - - while (1) { - char in[1024]; - int r; - - r = read_in_full(fd, in, sizeof(in)); - if (r == 0 || (r < 0 && errno == ECONNRESET)) - break; - if (r < 0) - die_errno("read error from cache daemon"); - write_or_die(1, in, r); - got_data = 1; - } - close(fd); - return got_data; -} - -static void spawn_daemon(const char *socket) -{ - struct child_process daemon = CHILD_PROCESS_INIT; - const char *argv[] = { NULL, NULL, NULL }; - char buf[128]; - int r; - - argv[0] = "git-credential-cache--daemon"; - argv[1] = socket; - daemon.argv = argv; - daemon.no_stdin = 1; - daemon.out = -1; - - if (start_command(&daemon)) - die_errno("unable to start cache daemon"); - r = read_in_full(daemon.out, buf, sizeof(buf)); - if (r < 0) - die_errno("unable to read result code from cache daemon"); - if (r != 3 || memcmp(buf, "ok\n", 3)) - die("cache daemon did not start: %.*s", r, buf); - close(daemon.out); -} - -static void do_cache(const char *socket, const char *action, int timeout, - int flags) -{ - struct strbuf buf = STRBUF_INIT; - - strbuf_addf(&buf, "action=%s\n", action); - strbuf_addf(&buf, "timeout=%d\n", timeout); - if (flags & FLAG_RELAY) { - if (strbuf_read(&buf, 0, 0) < 0) - die_errno("unable to relay credential"); - } - - if (send_request(socket, &buf) < 0) { - if (errno != ENOENT && errno != ECONNREFUSED) - die_errno("unable to connect to cache daemon"); - if (flags & FLAG_SPAWN) { - spawn_daemon(socket); - if (send_request(socket, &buf) < 0) - die_errno("unable to connect to cache daemon"); - } - } - strbuf_release(&buf); -} - -static char *get_socket_path(void) -{ - struct stat sb; - char *old_dir, *socket; - old_dir = expand_user_path("~/.git-credential-cache", 0); - if (old_dir && !stat(old_dir, &sb) && S_ISDIR(sb.st_mode)) - socket = xstrfmt("%s/socket", old_dir); - else - socket = xdg_cache_home("credential/socket"); - free(old_dir); - return socket; -} - -int cmd_main(int argc, const char **argv) -{ - char *socket_path = NULL; - int timeout = 900; - const char *op; - const char * const usage[] = { - "git credential-cache [] ", - NULL - }; - struct option options[] = { - OPT_INTEGER(0, "timeout", &timeout, - "number of seconds to cache credentials"), - OPT_STRING(0, "socket", &socket_path, "path", - "path of cache-daemon socket"), - OPT_END() - }; - - argc = parse_options(argc, argv, NULL, options, usage, 0); - if (!argc) - usage_with_options(usage, options); - op = argv[0]; - - if (!socket_path) - socket_path = get_socket_path(); - if (!socket_path) - die("unable to find a suitable socket path; use --socket"); - - if (!strcmp(op, "exit")) - do_cache(socket_path, op, timeout, 0); - else if (!strcmp(op, "get") || !strcmp(op, "erase")) - do_cache(socket_path, op, timeout, FLAG_RELAY); - else if (!strcmp(op, "store")) - do_cache(socket_path, op, timeout, FLAG_RELAY|FLAG_SPAWN); - else - ; /* ignore unknown operation */ - - return 0; -} diff --git a/credential-store.c b/credential-store.c deleted file mode 100644 index 294e771681..0000000000 --- a/credential-store.c +++ /dev/null @@ -1,195 +0,0 @@ -#include "cache.h" -#include "lockfile.h" -#include "credential.h" -#include "string-list.h" -#include "parse-options.h" - -static struct lock_file credential_lock; - -static int parse_credential_file(const char *fn, - 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; - int found_credential = 0; - - fh = fopen(fn, "r"); - if (!fh) { - if (errno != ENOENT && errno != EACCES) - die_errno("unable to open %s", fn); - return found_credential; - } - - while (strbuf_getline_lf(&line, fh) != EOF) { - if (!credential_from_url_gently(&entry, line.buf, 1) && - entry.username && entry.password && - credential_match(c, &entry)) { - found_credential = 1; - if (match_cb) { - match_cb(&entry); - break; - } - } - else if (other_cb) - other_cb(&line); - } - - credential_clear(&entry); - strbuf_release(&line); - fclose(fh); - return found_credential; -} - -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'); - write_or_die(get_lock_file_fd(&credential_lock), buf->buf, buf->len); -} - -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) - die_errno("unable to write credential store"); -} - -static void store_credential_file(const char *fn, struct credential *c) -{ - struct strbuf buf = STRBUF_INIT; - - strbuf_addf(&buf, "%s://", c->protocol); - strbuf_addstr_urlencode(&buf, c->username, is_rfc3986_unreserved); - strbuf_addch(&buf, ':'); - strbuf_addstr_urlencode(&buf, c->password, is_rfc3986_unreserved); - strbuf_addch(&buf, '@'); - if (c->host) - strbuf_addstr_urlencode(&buf, c->host, is_rfc3986_unreserved); - if (c->path) { - strbuf_addch(&buf, '/'); - strbuf_addstr_urlencode(&buf, c->path, - is_rfc3986_reserved_or_unreserved); - } - - rewrite_credential_file(fn, c, &buf); - strbuf_release(&buf); -} - -static void store_credential(const struct string_list *fns, struct credential *c) -{ - 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) -{ - struct string_list_item *fn; - - /* - * 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. - */ - 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); -} - -static void lookup_credential(const struct string_list *fns, struct credential *c) -{ - 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 */ -} - -int cmd_main(int argc, const char **argv) -{ - const char * const usage[] = { - "git credential-store [] ", - NULL - }; - const char *op; - struct credential c = CREDENTIAL_INIT; - struct string_list fns = STRING_LIST_INIT_DUP; - char *file = NULL; - struct option options[] = { - OPT_STRING(0, "file", &file, "path", - "fetch and store credentials in "), - OPT_END() - }; - - umask(077); - - argc = parse_options(argc, (const char **)argv, NULL, options, usage, 0); - if (argc != 1) - usage_with_options(usage, options); - op = argv[0]; - - if (file) { - string_list_append(&fns, file); - } else { - if ((file = expand_user_path("~/.git-credentials", 0))) - string_list_append_nodup(&fns, file); - file = xdg_config_home("credentials"); - if (file) - string_list_append_nodup(&fns, file); - } - if (!fns.nr) - die("unable to set up default path; use --file"); - - if (credential_read(&c, stdin) < 0) - die("unable to read credential"); - - if (!strcmp(op, "get")) - lookup_credential(&fns, &c); - else if (!strcmp(op, "erase")) - remove_credential(&fns, &c); - else if (!strcmp(op, "store")) - store_credential(&fns, &c); - else - ; /* Ignore unknown operation. */ - - string_list_clear(&fns, 0); - return 0; -} diff --git a/git.c b/git.c index 8bd1d7551d..39a160fa52 100644 --- a/git.c +++ b/git.c @@ -499,6 +499,9 @@ static struct cmd_struct commands[] = { { "config", cmd_config, RUN_SETUP_GENTLY | DELAY_PAGER_CONFIG }, { "count-objects", cmd_count_objects, RUN_SETUP }, { "credential", cmd_credential, RUN_SETUP_GENTLY | NO_PARSEOPT }, + { "credential-cache", cmd_credential_cache }, + { "credential-cache--daemon", cmd_credential_cache_daemon }, + { "credential-store", cmd_credential_store }, { "describe", cmd_describe, RUN_SETUP }, { "diff", cmd_diff, NO_PARSEOPT }, { "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT }, -- cgit v1.3-5-g9baa