]>
Commit | Line | Data |
---|---|---|
b5dd96b7 | 1 | #include "builtin.h" |
ffff4ac0 | 2 | #include "credential.h" |
f394e093 | 3 | #include "gettext.h" |
b5dd96b7 | 4 | #include "parse-options.h" |
d1cbe1e6 | 5 | #include "path.h" |
88e4e183 | 6 | #include "strbuf.h" |
d48be35c | 7 | #include "write-or-die.h" |
b5dd96b7 JK |
8 | |
9 | #ifndef NO_UNIX_SOCKETS | |
10 | ||
e2770979 JK |
11 | #include "unix-socket.h" |
12 | #include "run-command.h" | |
13 | ||
14 | #define FLAG_SPAWN 0x1 | |
15 | #define FLAG_RELAY 0x2 | |
16 | ||
245670cd CMAB |
17 | #ifdef GIT_WINDOWS_NATIVE |
18 | ||
19 | static int connection_closed(int error) | |
20 | { | |
21 | return (error == EINVAL); | |
22 | } | |
23 | ||
24 | static int connection_fatally_broken(int error) | |
25 | { | |
26 | return (error != ENOENT) && (error != ENETDOWN); | |
27 | } | |
28 | ||
29 | #else | |
30 | ||
31 | static int connection_closed(int error) | |
32 | { | |
33 | return (error == ECONNRESET); | |
34 | } | |
35 | ||
36 | static int connection_fatally_broken(int error) | |
37 | { | |
38 | return (error != ENOENT) && (error != ECONNREFUSED); | |
39 | } | |
40 | ||
41 | #endif | |
42 | ||
e2770979 JK |
43 | static int send_request(const char *socket, const struct strbuf *out) |
44 | { | |
45 | int got_data = 0; | |
77e522ca | 46 | int fd = unix_stream_connect(socket, 0); |
e2770979 JK |
47 | |
48 | if (fd < 0) | |
49 | return -1; | |
50 | ||
51 | if (write_in_full(fd, out->buf, out->len) < 0) | |
52 | die_errno("unable to write to cache daemon"); | |
53 | shutdown(fd, SHUT_WR); | |
54 | ||
55 | while (1) { | |
56 | char in[1024]; | |
57 | int r; | |
58 | ||
59 | r = read_in_full(fd, in, sizeof(in)); | |
245670cd | 60 | if (r == 0 || (r < 0 && connection_closed(errno))) |
e2770979 JK |
61 | break; |
62 | if (r < 0) | |
63 | die_errno("read error from cache daemon"); | |
64 | write_or_die(1, in, r); | |
65 | got_data = 1; | |
66 | } | |
9c60d9fa | 67 | close(fd); |
e2770979 JK |
68 | return got_data; |
69 | } | |
70 | ||
71 | static void spawn_daemon(const char *socket) | |
72 | { | |
d3180279 | 73 | struct child_process daemon = CHILD_PROCESS_INIT; |
e2770979 JK |
74 | char buf[128]; |
75 | int r; | |
76 | ||
c0e190c1 JH |
77 | strvec_pushl(&daemon.args, |
78 | "credential-cache--daemon", socket, | |
79 | NULL); | |
80 | daemon.git_cmd = 1; | |
e2770979 JK |
81 | daemon.no_stdin = 1; |
82 | daemon.out = -1; | |
83 | ||
84 | if (start_command(&daemon)) | |
85 | die_errno("unable to start cache daemon"); | |
86 | r = read_in_full(daemon.out, buf, sizeof(buf)); | |
87 | if (r < 0) | |
88 | die_errno("unable to read result code from cache daemon"); | |
89 | if (r != 3 || memcmp(buf, "ok\n", 3)) | |
90 | die("cache daemon did not start: %.*s", r, buf); | |
91 | close(daemon.out); | |
92 | } | |
93 | ||
94 | static void do_cache(const char *socket, const char *action, int timeout, | |
95 | int flags) | |
96 | { | |
97 | struct strbuf buf = STRBUF_INIT; | |
98 | ||
99 | strbuf_addf(&buf, "action=%s\n", action); | |
100 | strbuf_addf(&buf, "timeout=%d\n", timeout); | |
101 | if (flags & FLAG_RELAY) { | |
102 | if (strbuf_read(&buf, 0, 0) < 0) | |
103 | die_errno("unable to relay credential"); | |
104 | } | |
105 | ||
8ec6c8d7 | 106 | if (send_request(socket, &buf) < 0) { |
245670cd | 107 | if (connection_fatally_broken(errno)) |
98c2924c | 108 | die_errno("unable to connect to cache daemon"); |
8ec6c8d7 JK |
109 | if (flags & FLAG_SPAWN) { |
110 | spawn_daemon(socket); | |
111 | if (send_request(socket, &buf) < 0) | |
112 | die_errno("unable to connect to cache daemon"); | |
113 | } | |
e2770979 JK |
114 | } |
115 | strbuf_release(&buf); | |
116 | } | |
117 | ||
60759baa DL |
118 | static char *get_socket_path(void) |
119 | { | |
120 | struct stat sb; | |
121 | char *old_dir, *socket; | |
a03b097d | 122 | old_dir = interpolate_path("~/.git-credential-cache", 0); |
60759baa DL |
123 | if (old_dir && !stat(old_dir, &sb) && S_ISDIR(sb.st_mode)) |
124 | socket = xstrfmt("%s/socket", old_dir); | |
125 | else | |
126 | socket = xdg_cache_home("credential/socket"); | |
127 | free(old_dir); | |
128 | return socket; | |
129 | } | |
130 | ||
ffff4ac0 | 131 | static void announce_capabilities(void) |
132 | { | |
133 | struct credential c = CREDENTIAL_INIT; | |
134 | c.capa_authtype.request_initial = 1; | |
135 | credential_announce_capabilities(&c, stdout); | |
136 | } | |
137 | ||
b5dd96b7 | 138 | int cmd_credential_cache(int argc, const char **argv, const char *prefix) |
e2770979 JK |
139 | { |
140 | char *socket_path = NULL; | |
141 | int timeout = 900; | |
142 | const char *op; | |
143 | const char * const usage[] = { | |
d96a0313 | 144 | "git credential-cache [<options>] <action>", |
e2770979 JK |
145 | NULL |
146 | }; | |
147 | struct option options[] = { | |
148 | OPT_INTEGER(0, "timeout", &timeout, | |
149 | "number of seconds to cache credentials"), | |
150 | OPT_STRING(0, "socket", &socket_path, "path", | |
151 | "path of cache-daemon socket"), | |
152 | OPT_END() | |
153 | }; | |
154 | ||
b5dd96b7 | 155 | argc = parse_options(argc, argv, prefix, options, usage, 0); |
e2770979 JK |
156 | if (!argc) |
157 | usage_with_options(usage, options); | |
158 | op = argv[0]; | |
159 | ||
2406bf5f MA |
160 | if (!have_unix_sockets()) |
161 | die(_("credential-cache unavailable; no unix socket support")); | |
162 | ||
e2770979 | 163 | if (!socket_path) |
60759baa | 164 | socket_path = get_socket_path(); |
e2770979 JK |
165 | if (!socket_path) |
166 | die("unable to find a suitable socket path; use --socket"); | |
167 | ||
168 | if (!strcmp(op, "exit")) | |
169 | do_cache(socket_path, op, timeout, 0); | |
170 | else if (!strcmp(op, "get") || !strcmp(op, "erase")) | |
171 | do_cache(socket_path, op, timeout, FLAG_RELAY); | |
172 | else if (!strcmp(op, "store")) | |
173 | do_cache(socket_path, op, timeout, FLAG_RELAY|FLAG_SPAWN); | |
ffff4ac0 | 174 | else if (!strcmp(op, "capability")) |
175 | announce_capabilities(); | |
e2770979 JK |
176 | else |
177 | ; /* ignore unknown operation */ | |
178 | ||
179 | return 0; | |
180 | } | |
b5dd96b7 JK |
181 | |
182 | #else | |
183 | ||
184 | int cmd_credential_cache(int argc, const char **argv, const char *prefix) | |
185 | { | |
186 | const char * const usage[] = { | |
187 | "git credential-cache [options] <action>", | |
188 | "", | |
189 | "credential-cache is disabled in this build of Git", | |
190 | NULL | |
191 | }; | |
192 | struct option options[] = { OPT_END() }; | |
193 | ||
194 | argc = parse_options(argc, argv, prefix, options, usage, 0); | |
195 | die(_("credential-cache unavailable; no unix socket support")); | |
196 | } | |
197 | ||
198 | #endif /* NO_UNIX_SOCKETS */ |