Raw
1 #include "git-compat-util.h"
2 #include "repository.h"
3 #include "config.h"
4 #include "hash.h"
5 #include "pkt-line.h"
6 #include "version.h"
7 #include "ls-refs.h"
8 #include "protocol-caps.h"
9 #include "serve.h"
10 #include "upload-pack.h"
11 #include "bundle-uri.h"
12 #include "trace2.h"
13 #include "promisor-remote.h"
14
15 static int advertise_sid = -1;
16 static int advertise_object_info = -1;
17 static uint32_t client_hash_algo = GIT_HASH_SHA1_LEGACY;
18
19 static int always_advertise(struct repository *r UNUSED,
20 struct strbuf *value UNUSED)
21 {
22 return 1;
23 }
24
25 static int agent_advertise(struct repository *r UNUSED,
26 struct strbuf *value)
27 {
28 if (value)
29 strbuf_addstr(value, git_user_agent_sanitized());
30 return 1;
31 }
32
33 static int promisor_remote_advertise(struct repository *r,
34 struct strbuf *value)
35 {
36 if (value) {
37 char *info = promisor_remote_info(r);
38 if (!info)
39 return 0;
40 strbuf_addstr(value, info);
41 free(info);
42 }
43 return 1;
44 }
45
46 static void promisor_remote_receive(struct repository *r,
47 const char *remotes)
48 {
49 mark_promisor_remotes_as_accepted(r, remotes);
50 }
51
52
53 static int object_format_advertise(struct repository *r,
54 struct strbuf *value)
55 {
56 if (value)
57 strbuf_addstr(value, r->hash_algo->name);
58 return 1;
59 }
60
61 static void object_format_receive(struct repository *r UNUSED,
62 const char *algo_name)
63 {
64 if (!algo_name)
65 die("object-format capability requires an argument");
66
67 client_hash_algo = hash_algo_by_name(algo_name);
68 if (client_hash_algo == GIT_HASH_UNKNOWN)
69 die("unknown object format '%s'", algo_name);
70 }
71
72 static int session_id_advertise(struct repository *r, struct strbuf *value)
73 {
74 if (advertise_sid == -1 &&
75 repo_config_get_bool(r, "transfer.advertisesid", &advertise_sid))
76 advertise_sid = 0;
77 if (!advertise_sid)
78 return 0;
79 if (value)
80 strbuf_addstr(value, trace2_session_id());
81 return 1;
82 }
83
84 static void session_id_receive(struct repository *r UNUSED,
85 const char *client_sid)
86 {
87 if (!client_sid)
88 client_sid = "";
89 trace2_data_string("transfer", NULL, "client-sid", client_sid);
90 }
91
92 static int object_info_advertise(struct repository *r, struct strbuf *value)
93 {
94 if (advertise_object_info == -1 &&
95 repo_config_get_bool(r, "transfer.advertiseobjectinfo",
96 &advertise_object_info)) {
97 /* disabled by default */
98 advertise_object_info = 0;
99 }
100 /* Currently only size is supported */
101 if (value && advertise_object_info)
102 strbuf_addstr(value, "size");
103 return advertise_object_info;
104 }
105
106 struct protocol_capability {
107 /*
108 * The name of the capability. The server uses this name when
109 * advertising this capability, and the client uses this name to
110 * specify this capability.
111 */
112 const char *name;
113
114 /*
115 * Function queried to see if a capability should be advertised.
116 * Optionally a value can be specified by adding it to 'value'.
117 * If a value is added to 'value', the server will advertise this
118 * capability as "<name>=<value>" instead of "<name>".
119 */
120 int (*advertise)(struct repository *r, struct strbuf *value);
121
122 /*
123 * Function called when a client requests the capability as a command.
124 * Will be provided a struct packet_reader 'request' which it should
125 * use to read the command specific part of the request. Every command
126 * MUST read until a flush packet is seen before sending a response.
127 *
128 * This field should be NULL for capabilities which are not commands.
129 */
130 int (*command)(struct repository *r, struct packet_reader *request);
131
132 /*
133 * Function called when a client requests the capability as a
134 * non-command. This may be NULL if the capability does nothing.
135 *
136 * For a capability of the form "foo=bar", the value string points to
137 * the content after the "=" (i.e., "bar"). For simple capabilities
138 * (just "foo"), it is NULL.
139 */
140 void (*receive)(struct repository *r, const char *value);
141 };
142
143 static struct protocol_capability capabilities[] = {
144 {
145 .name = "agent",
146 .advertise = agent_advertise,
147 },
148 {
149 .name = "ls-refs",
150 .advertise = ls_refs_advertise,
151 .command = ls_refs,
152 },
153 {
154 .name = "fetch",
155 .advertise = upload_pack_advertise,
156 .command = upload_pack_v2,
157 },
158 {
159 .name = "server-option",
160 .advertise = always_advertise,
161 },
162 {
163 .name = "object-format",
164 .advertise = object_format_advertise,
165 .receive = object_format_receive,
166 },
167 {
168 .name = "session-id",
169 .advertise = session_id_advertise,
170 .receive = session_id_receive,
171 },
172 {
173 .name = "object-info",
174 .advertise = object_info_advertise,
175 .command = cap_object_info,
176 },
177 {
178 .name = "bundle-uri",
179 .advertise = bundle_uri_advertise,
180 .command = bundle_uri_command,
181 },
182 {
183 .name = "promisor-remote",
184 .advertise = promisor_remote_advertise,
185 .receive = promisor_remote_receive,
186 },
187 };
188
189 void protocol_v2_advertise_capabilities(struct repository *r)
190 {
191 struct strbuf capability = STRBUF_INIT;
192 struct strbuf value = STRBUF_INIT;
193
194 /* serve by default supports v2 */
195 packet_write_fmt(1, "version 2\n");
196
197 for (size_t i = 0; i < ARRAY_SIZE(capabilities); i++) {
198 struct protocol_capability *c = &capabilities[i];
199
200 if (c->advertise(r, &value)) {
201 strbuf_addstr(&capability, c->name);
202
203 if (value.len) {
204 strbuf_addch(&capability, '=');
205 strbuf_addbuf(&capability, &value);
206 }
207
208 strbuf_addch(&capability, '\n');
209 packet_write(1, capability.buf, capability.len);
210 }
211
212 strbuf_reset(&capability);
213 strbuf_reset(&value);
214 }
215
216 packet_flush(1);
217 strbuf_release(&capability);
218 strbuf_release(&value);
219 }
220
221 static struct protocol_capability *get_capability(const char *key, const char **value)
222 {
223 if (!key)
224 return NULL;
225
226 for (size_t i = 0; i < ARRAY_SIZE(capabilities); i++) {
227 struct protocol_capability *c = &capabilities[i];
228 const char *out;
229 if (!skip_prefix(key, c->name, &out))
230 continue;
231 if (!*out) {
232 *value = NULL;
233 return c;
234 }
235 if (*out++ == '=') {
236 *value = out;
237 return c;
238 }
239 }
240
241 return NULL;
242 }
243
244 static int receive_client_capability(struct repository *r, const char *key)
245 {
246 const char *value;
247 const struct protocol_capability *c = get_capability(key, &value);
248
249 if (!c || c->command || !c->advertise(r, NULL))
250 return 0;
251
252 if (c->receive)
253 c->receive(r, value);
254 return 1;
255 }
256
257 static int parse_command(struct repository *r, const char *key, struct protocol_capability **command)
258 {
259 const char *out;
260
261 if (skip_prefix(key, "command=", &out)) {
262 const char *value;
263 struct protocol_capability *cmd = get_capability(out, &value);
264
265 if (*command)
266 die("command '%s' requested after already requesting command '%s'",
267 out, (*command)->name);
268 if (!cmd || !cmd->advertise(r, NULL) || !cmd->command || value)
269 die("invalid command '%s'", out);
270
271 *command = cmd;
272 return 1;
273 }
274
275 return 0;
276 }
277
278 enum request_state {
279 PROCESS_REQUEST_KEYS,
280 PROCESS_REQUEST_DONE,
281 };
282
283 static int process_request(struct repository *r)
284 {
285 enum request_state state = PROCESS_REQUEST_KEYS;
286 struct packet_reader reader;
287 int seen_capability_or_command = 0;
288 struct protocol_capability *command = NULL;
289
290 packet_reader_init(&reader, 0, NULL, 0,
291 PACKET_READ_CHOMP_NEWLINE |
292 PACKET_READ_GENTLE_ON_EOF |
293 PACKET_READ_DIE_ON_ERR_PACKET);
294
295 /*
296 * Check to see if the client closed their end before sending another
297 * request. If so we can terminate the connection.
298 */
299 if (packet_reader_peek(&reader) == PACKET_READ_EOF)
300 return 1;
301 reader.options &= ~PACKET_READ_GENTLE_ON_EOF;
302
303 while (state != PROCESS_REQUEST_DONE) {
304 switch (packet_reader_peek(&reader)) {
305 case PACKET_READ_EOF:
306 BUG("Should have already died when seeing EOF");
307 case PACKET_READ_NORMAL:
308 if (parse_command(r, reader.line, &command) ||
309 receive_client_capability(r, reader.line))
310 seen_capability_or_command = 1;
311 else
312 die("unknown capability '%s'", reader.line);
313
314 /* Consume the peeked line */
315 packet_reader_read(&reader);
316 break;
317 case PACKET_READ_FLUSH:
318 /*
319 * If no command and no keys were given then the client
320 * wanted to terminate the connection.
321 */
322 if (!seen_capability_or_command)
323 return 1;
324
325 /*
326 * The flush packet isn't consume here like it is in
327 * the other parts of this switch statement. This is
328 * so that the command can read the flush packet and
329 * see the end of the request in the same way it would
330 * if command specific arguments were provided after a
331 * delim packet.
332 */
333 state = PROCESS_REQUEST_DONE;
334 break;
335 case PACKET_READ_DELIM:
336 /* Consume the peeked line */
337 packet_reader_read(&reader);
338
339 state = PROCESS_REQUEST_DONE;
340 break;
341 case PACKET_READ_RESPONSE_END:
342 BUG("unexpected response end packet");
343 }
344 }
345
346 if (!command)
347 die("no command requested");
348
349 if (client_hash_algo != hash_algo_by_ptr(r->hash_algo))
350 die("mismatched object format: server %s; client %s",
351 r->hash_algo->name,
352 hash_algos[client_hash_algo].name);
353
354 command->command(r, &reader);
355
356 return 0;
357 }
358
359 void protocol_v2_serve_loop(struct repository *r, int stateless_rpc)
360 {
361 if (!stateless_rpc)
362 protocol_v2_advertise_capabilities(r);
363
364 /*
365 * If stateless-rpc was requested then exit after
366 * a single request/response exchange
367 */
368 if (stateless_rpc) {
369 process_request(r);
370 } else {
371 for (;;)
372 if (process_request(r))
373 break;
374 }
375 }