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 | |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 6 | #include "config.h" |
Michael Haggerty | 9e90331 | 2015-08-10 11:47:51 +0200 | [diff] [blame] | 7 | #include "tempfile.h" |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 8 | #include "credential.h" |
| 9 | #include "unix-socket.h" |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 10 | |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 11 | struct credential_cache_entry { |
| 12 | struct credential item; |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 13 | timestamp_t expiration; |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 14 | }; |
| 15 | static struct credential_cache_entry *entries; |
| 16 | static int entries_nr; |
| 17 | static int entries_alloc; |
| 18 | |
| 19 | static void cache_credential(struct credential *c, int timeout) |
| 20 | { |
| 21 | struct credential_cache_entry *e; |
| 22 | |
| 23 | ALLOC_GROW(entries, entries_nr + 1, entries_alloc); |
| 24 | e = &entries[entries_nr++]; |
| 25 | |
| 26 | /* take ownership of pointers */ |
| 27 | memcpy(&e->item, c, sizeof(*c)); |
| 28 | memset(c, 0, sizeof(*c)); |
| 29 | e->expiration = time(NULL) + timeout; |
| 30 | } |
| 31 | |
| 32 | static struct credential_cache_entry *lookup_credential(const struct credential *c) |
| 33 | { |
| 34 | int i; |
| 35 | for (i = 0; i < entries_nr; i++) { |
| 36 | struct credential *e = &entries[i].item; |
| 37 | if (credential_match(c, e)) |
| 38 | return &entries[i]; |
| 39 | } |
| 40 | return NULL; |
| 41 | } |
| 42 | |
| 43 | static void remove_credential(const struct credential *c) |
| 44 | { |
| 45 | struct credential_cache_entry *e; |
| 46 | |
| 47 | e = lookup_credential(c); |
| 48 | if (e) |
| 49 | e->expiration = 0; |
| 50 | } |
| 51 | |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 52 | static timestamp_t check_expirations(void) |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 53 | { |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 54 | static timestamp_t wait_for_entry_until; |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 55 | int i = 0; |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 56 | timestamp_t now = time(NULL); |
| 57 | timestamp_t next = TIME_MAX; |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 58 | |
| 59 | /* |
| 60 | * Initially give the client 30 seconds to actually contact us |
| 61 | * and store a credential before we decide there's no point in |
| 62 | * keeping the daemon around. |
| 63 | */ |
| 64 | if (!wait_for_entry_until) |
| 65 | wait_for_entry_until = now + 30; |
| 66 | |
| 67 | while (i < entries_nr) { |
| 68 | if (entries[i].expiration <= now) { |
| 69 | entries_nr--; |
| 70 | credential_clear(&entries[i].item); |
| 71 | if (i != entries_nr) |
| 72 | memcpy(&entries[i], &entries[entries_nr], sizeof(*entries)); |
| 73 | /* |
| 74 | * Stick around 30 seconds in case a new credential |
| 75 | * shows up (e.g., because we just removed a failed |
| 76 | * one, and we will soon get the correct one). |
| 77 | */ |
| 78 | wait_for_entry_until = now + 30; |
| 79 | } |
| 80 | else { |
| 81 | if (entries[i].expiration < next) |
| 82 | next = entries[i].expiration; |
| 83 | i++; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | if (!entries_nr) { |
| 88 | if (wait_for_entry_until <= now) |
| 89 | return 0; |
| 90 | next = wait_for_entry_until; |
| 91 | } |
| 92 | |
| 93 | return next - now; |
| 94 | } |
| 95 | |
| 96 | static int read_request(FILE *fh, struct credential *c, |
Nguyễn Thái Ngọc Duy | 3b33576 | 2018-12-09 11:25:21 +0100 | [diff] [blame] | 97 | struct strbuf *action, int *timeout) |
| 98 | { |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 99 | static struct strbuf item = STRBUF_INIT; |
| 100 | const char *p; |
| 101 | |
Junio C Hamano | 8f309ae | 2016-01-13 15:31:17 -0800 | [diff] [blame] | 102 | strbuf_getline_lf(&item, fh); |
Jeff King | cf4fff5 | 2014-06-18 15:44:19 -0400 | [diff] [blame] | 103 | if (!skip_prefix(item.buf, "action=", &p)) |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 104 | return error("client sent bogus action line: %s", item.buf); |
| 105 | strbuf_addstr(action, p); |
| 106 | |
Junio C Hamano | 8f309ae | 2016-01-13 15:31:17 -0800 | [diff] [blame] | 107 | strbuf_getline_lf(&item, fh); |
Jeff King | cf4fff5 | 2014-06-18 15:44:19 -0400 | [diff] [blame] | 108 | if (!skip_prefix(item.buf, "timeout=", &p)) |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 109 | return error("client sent bogus timeout line: %s", item.buf); |
| 110 | *timeout = atoi(p); |
| 111 | |
| 112 | if (credential_read(c, fh) < 0) |
| 113 | return -1; |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | static void serve_one_client(FILE *in, FILE *out) |
| 118 | { |
| 119 | struct credential c = CREDENTIAL_INIT; |
| 120 | struct strbuf action = STRBUF_INIT; |
| 121 | int timeout = -1; |
| 122 | |
| 123 | if (read_request(in, &c, &action, &timeout) < 0) |
| 124 | /* ignore error */ ; |
| 125 | else if (!strcmp(action.buf, "get")) { |
| 126 | struct credential_cache_entry *e = lookup_credential(&c); |
| 127 | if (e) { |
| 128 | fprintf(out, "username=%s\n", e->item.username); |
| 129 | fprintf(out, "password=%s\n", e->item.password); |
| 130 | } |
| 131 | } |
Jeff King | 7d5e9c9 | 2016-03-18 02:12:01 -0400 | [diff] [blame] | 132 | else if (!strcmp(action.buf, "exit")) { |
| 133 | /* |
| 134 | * It's important that we clean up our socket first, and then |
| 135 | * signal the client only once we have finished the cleanup. |
| 136 | * Calling exit() directly does this, because we clean up in |
| 137 | * our atexit() handler, and then signal the client when our |
| 138 | * process actually ends, which closes the socket and gives |
| 139 | * them EOF. |
| 140 | */ |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 141 | exit(0); |
Jeff King | 7d5e9c9 | 2016-03-18 02:12:01 -0400 | [diff] [blame] | 142 | } |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 143 | else if (!strcmp(action.buf, "erase")) |
| 144 | remove_credential(&c); |
| 145 | else if (!strcmp(action.buf, "store")) { |
| 146 | if (timeout < 0) |
| 147 | warning("cache client didn't specify a timeout"); |
| 148 | else if (!c.username || !c.password) |
| 149 | warning("cache client gave us a partial credential"); |
| 150 | else { |
| 151 | remove_credential(&c); |
| 152 | cache_credential(&c, timeout); |
| 153 | } |
| 154 | } |
| 155 | else |
| 156 | warning("cache client sent unknown action: %s", action.buf); |
| 157 | |
| 158 | credential_clear(&c); |
| 159 | strbuf_release(&action); |
| 160 | } |
| 161 | |
| 162 | static int serve_cache_loop(int fd) |
| 163 | { |
| 164 | struct pollfd pfd; |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 165 | timestamp_t wakeup; |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 166 | |
| 167 | wakeup = check_expirations(); |
| 168 | if (!wakeup) |
| 169 | return 0; |
| 170 | |
| 171 | pfd.fd = fd; |
| 172 | pfd.events = POLLIN; |
| 173 | if (poll(&pfd, 1, 1000 * wakeup) < 0) { |
| 174 | if (errno != EINTR) |
| 175 | die_errno("poll failed"); |
| 176 | return 1; |
| 177 | } |
| 178 | |
| 179 | if (pfd.revents & POLLIN) { |
| 180 | int client, client2; |
| 181 | FILE *in, *out; |
| 182 | |
| 183 | client = accept(fd, NULL, NULL); |
| 184 | if (client < 0) { |
Nguyễn Thái Ngọc Duy | 26604f9 | 2016-05-08 16:47:41 +0700 | [diff] [blame] | 185 | warning_errno("accept failed"); |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 186 | return 1; |
| 187 | } |
| 188 | client2 = dup(client); |
| 189 | if (client2 < 0) { |
Nguyễn Thái Ngọc Duy | 26604f9 | 2016-05-08 16:47:41 +0700 | [diff] [blame] | 190 | warning_errno("dup failed"); |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 191 | close(client); |
| 192 | return 1; |
| 193 | } |
| 194 | |
| 195 | in = xfdopen(client, "r"); |
| 196 | out = xfdopen(client2, "w"); |
| 197 | serve_one_client(in, out); |
| 198 | fclose(in); |
| 199 | fclose(out); |
| 200 | } |
| 201 | return 1; |
| 202 | } |
| 203 | |
Jeff King | f5e3c0b | 2014-09-14 03:35:06 -0400 | [diff] [blame] | 204 | static void serve_cache(const char *socket_path, int debug) |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 205 | { |
Jeff Hostetler | 55144cc | 2021-03-15 21:08:25 +0000 | [diff] [blame] | 206 | struct unix_stream_listen_opts opts = UNIX_STREAM_LISTEN_OPTS_INIT; |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 207 | int fd; |
| 208 | |
Jeff Hostetler | 55144cc | 2021-03-15 21:08:25 +0000 | [diff] [blame] | 209 | fd = unix_stream_listen(socket_path, &opts); |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 210 | if (fd < 0) |
| 211 | die_errno("unable to bind to '%s'", socket_path); |
| 212 | |
| 213 | printf("ok\n"); |
| 214 | fclose(stdout); |
Jeff King | f5e3c0b | 2014-09-14 03:35:06 -0400 | [diff] [blame] | 215 | if (!debug) { |
| 216 | if (!freopen("/dev/null", "w", stderr)) |
| 217 | die_errno("unable to point stderr to /dev/null"); |
| 218 | } |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 219 | |
| 220 | while (serve_cache_loop(fd)) |
| 221 | ; /* nothing */ |
| 222 | |
| 223 | close(fd); |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 224 | } |
| 225 | |
Vasco Almeida | af64f20 | 2016-10-17 13:15:28 +0000 | [diff] [blame] | 226 | static const char permissions_advice[] = N_( |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 227 | "The permissions on your socket directory are too loose; other\n" |
| 228 | "users may be able to read your cached credentials. Consider running:\n" |
| 229 | "\n" |
Vasco Almeida | af64f20 | 2016-10-17 13:15:28 +0000 | [diff] [blame] | 230 | " chmod 0700 %s"); |
Jon Griffiths | a6e5e28 | 2016-02-23 02:15:15 -0500 | [diff] [blame] | 231 | static void init_socket_directory(const char *path) |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 232 | { |
| 233 | struct stat st; |
| 234 | char *path_copy = xstrdup(path); |
| 235 | char *dir = dirname(path_copy); |
| 236 | |
| 237 | if (!stat(dir, &st)) { |
| 238 | if (st.st_mode & 077) |
Vasco Almeida | af64f20 | 2016-10-17 13:15:28 +0000 | [diff] [blame] | 239 | die(_(permissions_advice), dir); |
Jon Griffiths | a6e5e28 | 2016-02-23 02:15:15 -0500 | [diff] [blame] | 240 | } else { |
| 241 | /* |
| 242 | * We must be sure to create the directory with the correct mode, |
| 243 | * not just chmod it after the fact; otherwise, there is a race |
| 244 | * condition in which somebody can chdir to it, sleep, then try to open |
| 245 | * our protected socket. |
| 246 | */ |
| 247 | if (safe_create_leading_directories_const(dir) < 0) |
| 248 | die_errno("unable to create directories for '%s'", dir); |
| 249 | if (mkdir(dir, 0700) < 0) |
| 250 | die_errno("unable to mkdir '%s'", dir); |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 251 | } |
| 252 | |
Jon Griffiths | 6e61449 | 2016-02-23 02:16:04 -0500 | [diff] [blame] | 253 | if (chdir(dir)) |
| 254 | /* |
| 255 | * We don't actually care what our cwd is; we chdir here just to |
| 256 | * be a friendly daemon and avoid tying up our original cwd. |
| 257 | * If this fails, it's OK to just continue without that benefit. |
| 258 | */ |
| 259 | ; |
| 260 | |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 261 | free(path_copy); |
| 262 | } |
| 263 | |
Jeff King | b5dd96b | 2020-08-13 10:58:55 -0400 | [diff] [blame] | 264 | int cmd_credential_cache_daemon(int argc, const char **argv, const char *prefix) |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 265 | { |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 266 | struct tempfile *socket_file; |
Michael Haggerty | 9e90331 | 2015-08-10 11:47:51 +0200 | [diff] [blame] | 267 | const char *socket_path; |
Noam Postavsky | 7f4d474 | 2015-11-09 19:26:29 -0500 | [diff] [blame] | 268 | int ignore_sighup = 0; |
Jeff King | f5e3c0b | 2014-09-14 03:35:06 -0400 | [diff] [blame] | 269 | static const char *usage[] = { |
Ævar Arnfjörð Bjarmason | 3e4ebe3 | 2022-10-13 17:39:17 +0200 | [diff] [blame] | 270 | "git credential-cache--daemon [--debug] <socket-path>", |
Jeff King | f5e3c0b | 2014-09-14 03:35:06 -0400 | [diff] [blame] | 271 | NULL |
| 272 | }; |
| 273 | int debug = 0; |
| 274 | const struct option options[] = { |
| 275 | OPT_BOOL(0, "debug", &debug, |
| 276 | N_("print debugging messages to stderr")), |
| 277 | OPT_END() |
| 278 | }; |
| 279 | |
Noam Postavsky | 7f4d474 | 2015-11-09 19:26:29 -0500 | [diff] [blame] | 280 | git_config_get_bool("credentialcache.ignoresighup", &ignore_sighup); |
| 281 | |
Jeff King | b5dd96b | 2020-08-13 10:58:55 -0400 | [diff] [blame] | 282 | argc = parse_options(argc, argv, prefix, options, usage, 0); |
Jeff King | f5e3c0b | 2014-09-14 03:35:06 -0400 | [diff] [blame] | 283 | socket_path = argv[0]; |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 284 | |
| 285 | if (!socket_path) |
Jeff King | f5e3c0b | 2014-09-14 03:35:06 -0400 | [diff] [blame] | 286 | usage_with_options(usage, options); |
Michael Haggerty | 9e90331 | 2015-08-10 11:47:51 +0200 | [diff] [blame] | 287 | |
Jon Griffiths | bd93b8d | 2016-02-23 02:15:41 -0500 | [diff] [blame] | 288 | if (!is_absolute_path(socket_path)) |
| 289 | die("socket directory must be an absolute path"); |
| 290 | |
Jon Griffiths | a6e5e28 | 2016-02-23 02:15:15 -0500 | [diff] [blame] | 291 | init_socket_directory(socket_path); |
Jeff King | 076aa2c | 2017-09-05 08:15:08 -0400 | [diff] [blame] | 292 | socket_file = register_tempfile(socket_path); |
Noam Postavsky | 7f4d474 | 2015-11-09 19:26:29 -0500 | [diff] [blame] | 293 | |
| 294 | if (ignore_sighup) |
| 295 | signal(SIGHUP, SIG_IGN); |
| 296 | |
Jeff King | f5e3c0b | 2014-09-14 03:35:06 -0400 | [diff] [blame] | 297 | serve_cache(socket_path, debug); |
Michael Haggerty | 9e90331 | 2015-08-10 11:47:51 +0200 | [diff] [blame] | 298 | delete_tempfile(&socket_file); |
Michael Haggerty | 18a3de4 | 2015-08-10 11:47:50 +0200 | [diff] [blame] | 299 | |
Jeff King | e277097 | 2011-12-10 05:34:14 -0500 | [diff] [blame] | 300 | return 0; |
| 301 | } |
Jeff King | b5dd96b | 2020-08-13 10:58:55 -0400 | [diff] [blame] | 302 | |
| 303 | #else |
| 304 | |
| 305 | int cmd_credential_cache_daemon(int argc, const char **argv, const char *prefix) |
| 306 | { |
| 307 | const char * const usage[] = { |
Ævar Arnfjörð Bjarmason | 3e4ebe3 | 2022-10-13 17:39:17 +0200 | [diff] [blame] | 308 | "git credential-cache--daemon [--debug] <socket-path>", |
Jeff King | b5dd96b | 2020-08-13 10:58:55 -0400 | [diff] [blame] | 309 | "", |
| 310 | "credential-cache--daemon is disabled in this build of Git", |
| 311 | NULL |
| 312 | }; |
| 313 | struct option options[] = { OPT_END() }; |
| 314 | |
| 315 | argc = parse_options(argc, argv, prefix, options, usage, 0); |
| 316 | die(_("credential-cache--daemon unavailable; no unix socket support")); |
| 317 | } |
| 318 | |
| 319 | #endif /* NO_UNIX_SOCKET */ |