Jeff King | b5dd96b | 2020-08-13 10:58:55 -0400 | [diff] [blame] | 1 | #include "builtin.h" |
| 2 | #include "parse-options.h" |
| 3 | |
| 4 | #ifndef NO_UNIX_SOCKETS |
| 5 | |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 6 | #include "credential.h" |
| 7 | #include "string-list.h" |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 8 | #include "unix-socket.h" |
| 9 | #include "run-command.h" |
| 10 | |
| 11 | #define FLAG_SPAWN 0x1 |
| 12 | #define FLAG_RELAY 0x2 |
| 13 | |
Carlo Marcelo Arenas Belón | 245670c | 2021-09-14 00:25:59 -0700 | [diff] [blame] | 14 | #ifdef GIT_WINDOWS_NATIVE |
| 15 | |
| 16 | static int connection_closed(int error) |
| 17 | { |
| 18 | return (error == EINVAL); |
| 19 | } |
| 20 | |
| 21 | static int connection_fatally_broken(int error) |
| 22 | { |
| 23 | return (error != ENOENT) && (error != ENETDOWN); |
| 24 | } |
| 25 | |
| 26 | #else |
| 27 | |
| 28 | static int connection_closed(int error) |
| 29 | { |
| 30 | return (error == ECONNRESET); |
| 31 | } |
| 32 | |
| 33 | static int connection_fatally_broken(int error) |
| 34 | { |
| 35 | return (error != ENOENT) && (error != ECONNREFUSED); |
| 36 | } |
| 37 | |
| 38 | #endif |
| 39 | |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 40 | static int send_request(const char *socket, const struct strbuf *out) |
| 41 | { |
| 42 | int got_data = 0; |
Jeff Hostetler | 77e522c | 2021-03-15 21:08:26 +0000 | [diff] [blame] | 43 | int fd = unix_stream_connect(socket, 0); |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 44 | |
| 45 | if (fd < 0) |
| 46 | return -1; |
| 47 | |
| 48 | if (write_in_full(fd, out->buf, out->len) < 0) |
| 49 | die_errno("unable to write to cache daemon"); |
| 50 | shutdown(fd, SHUT_WR); |
| 51 | |
| 52 | while (1) { |
| 53 | char in[1024]; |
| 54 | int r; |
| 55 | |
| 56 | r = read_in_full(fd, in, sizeof(in)); |
Carlo Marcelo Arenas Belón | 245670c | 2021-09-14 00:25:59 -0700 | [diff] [blame] | 57 | if (r == 0 || (r < 0 && connection_closed(errno))) |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 58 | break; |
| 59 | if (r < 0) |
| 60 | die_errno("read error from cache daemon"); |
| 61 | write_or_die(1, in, r); |
| 62 | got_data = 1; |
| 63 | } |
Stefan Beller | 9c60d9f | 2016-03-31 17:35:46 -0700 | [diff] [blame] | 64 | close(fd); |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 65 | return got_data; |
| 66 | } |
| 67 | |
| 68 | static void spawn_daemon(const char *socket) |
| 69 | { |
René Scharfe | d318027 | 2014-08-19 21:09:35 +0200 | [diff] [blame] | 70 | struct child_process daemon = CHILD_PROCESS_INIT; |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 71 | char buf[128]; |
| 72 | int r; |
| 73 | |
Junio C Hamano | c0e190c | 2020-08-26 14:37:39 -0700 | [diff] [blame] | 74 | strvec_pushl(&daemon.args, |
| 75 | "credential-cache--daemon", socket, |
| 76 | NULL); |
| 77 | daemon.git_cmd = 1; |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 78 | daemon.no_stdin = 1; |
| 79 | daemon.out = -1; |
| 80 | |
| 81 | if (start_command(&daemon)) |
| 82 | die_errno("unable to start cache daemon"); |
| 83 | r = read_in_full(daemon.out, buf, sizeof(buf)); |
| 84 | if (r < 0) |
| 85 | die_errno("unable to read result code from cache daemon"); |
| 86 | if (r != 3 || memcmp(buf, "ok\n", 3)) |
| 87 | die("cache daemon did not start: %.*s", r, buf); |
| 88 | close(daemon.out); |
| 89 | } |
| 90 | |
| 91 | static void do_cache(const char *socket, const char *action, int timeout, |
| 92 | int flags) |
| 93 | { |
| 94 | struct strbuf buf = STRBUF_INIT; |
| 95 | |
| 96 | strbuf_addf(&buf, "action=%s\n", action); |
| 97 | strbuf_addf(&buf, "timeout=%d\n", timeout); |
| 98 | if (flags & FLAG_RELAY) { |
| 99 | if (strbuf_read(&buf, 0, 0) < 0) |
| 100 | die_errno("unable to relay credential"); |
| 101 | } |
| 102 | |
Jeff King | 8ec6c8d | 2012-01-09 23:57:33 -0500 | [diff] [blame] | 103 | if (send_request(socket, &buf) < 0) { |
Carlo Marcelo Arenas Belón | 245670c | 2021-09-14 00:25:59 -0700 | [diff] [blame] | 104 | if (connection_fatally_broken(errno)) |
Clemens Buchacher | 98c2924 | 2012-01-07 12:54:36 +0100 | [diff] [blame] | 105 | die_errno("unable to connect to cache daemon"); |
Jeff King | 8ec6c8d | 2012-01-09 23:57:33 -0500 | [diff] [blame] | 106 | if (flags & FLAG_SPAWN) { |
| 107 | spawn_daemon(socket); |
| 108 | if (send_request(socket, &buf) < 0) |
| 109 | die_errno("unable to connect to cache daemon"); |
| 110 | } |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 111 | } |
| 112 | strbuf_release(&buf); |
| 113 | } |
| 114 | |
Devin Lehmacher | 60759ba | 2017-03-17 08:36:33 -0400 | [diff] [blame] | 115 | static char *get_socket_path(void) |
| 116 | { |
| 117 | struct stat sb; |
| 118 | char *old_dir, *socket; |
Johannes Schindelin | a03b097 | 2021-07-24 22:06:52 +0000 | [diff] [blame] | 119 | old_dir = interpolate_path("~/.git-credential-cache", 0); |
Devin Lehmacher | 60759ba | 2017-03-17 08:36:33 -0400 | [diff] [blame] | 120 | if (old_dir && !stat(old_dir, &sb) && S_ISDIR(sb.st_mode)) |
| 121 | socket = xstrfmt("%s/socket", old_dir); |
| 122 | else |
| 123 | socket = xdg_cache_home("credential/socket"); |
| 124 | free(old_dir); |
| 125 | return socket; |
| 126 | } |
| 127 | |
Jeff King | b5dd96b | 2020-08-13 10:58:55 -0400 | [diff] [blame] | 128 | int cmd_credential_cache(int argc, const char **argv, const char *prefix) |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 129 | { |
| 130 | char *socket_path = NULL; |
| 131 | int timeout = 900; |
| 132 | const char *op; |
| 133 | const char * const usage[] = { |
Ralf Thielow | d96a031 | 2015-10-16 19:09:57 +0200 | [diff] [blame] | 134 | "git credential-cache [<options>] <action>", |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 135 | NULL |
| 136 | }; |
| 137 | struct option options[] = { |
| 138 | OPT_INTEGER(0, "timeout", &timeout, |
| 139 | "number of seconds to cache credentials"), |
| 140 | OPT_STRING(0, "socket", &socket_path, "path", |
| 141 | "path of cache-daemon socket"), |
| 142 | OPT_END() |
| 143 | }; |
| 144 | |
Jeff King | b5dd96b | 2020-08-13 10:58:55 -0400 | [diff] [blame] | 145 | argc = parse_options(argc, argv, prefix, options, usage, 0); |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 146 | if (!argc) |
| 147 | usage_with_options(usage, options); |
| 148 | op = argv[0]; |
| 149 | |
| 150 | if (!socket_path) |
Devin Lehmacher | 60759ba | 2017-03-17 08:36:33 -0400 | [diff] [blame] | 151 | socket_path = get_socket_path(); |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 152 | if (!socket_path) |
| 153 | die("unable to find a suitable socket path; use --socket"); |
| 154 | |
| 155 | if (!strcmp(op, "exit")) |
| 156 | do_cache(socket_path, op, timeout, 0); |
| 157 | else if (!strcmp(op, "get") || !strcmp(op, "erase")) |
| 158 | do_cache(socket_path, op, timeout, FLAG_RELAY); |
| 159 | else if (!strcmp(op, "store")) |
| 160 | do_cache(socket_path, op, timeout, FLAG_RELAY|FLAG_SPAWN); |
| 161 | else |
| 162 | ; /* ignore unknown operation */ |
| 163 | |
| 164 | return 0; |
| 165 | } |
Jeff King | b5dd96b | 2020-08-13 10:58:55 -0400 | [diff] [blame] | 166 | |
| 167 | #else |
| 168 | |
| 169 | int cmd_credential_cache(int argc, const char **argv, const char *prefix) |
| 170 | { |
| 171 | const char * const usage[] = { |
| 172 | "git credential-cache [options] <action>", |
| 173 | "", |
| 174 | "credential-cache is disabled in this build of Git", |
| 175 | NULL |
| 176 | }; |
| 177 | struct option options[] = { OPT_END() }; |
| 178 | |
| 179 | argc = parse_options(argc, argv, prefix, options, usage, 0); |
| 180 | die(_("credential-cache unavailable; no unix socket support")); |
| 181 | } |
| 182 | |
| 183 | #endif /* NO_UNIX_SOCKETS */ |