2 bd3d9e54 2021-09-05 stsp * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
3 bd3d9e54 2021-09-05 stsp * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
5 bd3d9e54 2021-09-05 stsp * Permission to use, copy, modify, and distribute this software for any
6 bd3d9e54 2021-09-05 stsp * purpose with or without fee is hereby granted, provided that the above
7 bd3d9e54 2021-09-05 stsp * copyright notice and this permission notice appear in all copies.
9 bd3d9e54 2021-09-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 bd3d9e54 2021-09-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 bd3d9e54 2021-09-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 bd3d9e54 2021-09-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 bd3d9e54 2021-09-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 bd3d9e54 2021-09-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 bd3d9e54 2021-09-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
19 8a8621c2 2021-09-06 naddy #include <sys/types.h>
21 bd3d9e54 2021-09-05 stsp #include <ctype.h>
22 bd3d9e54 2021-09-05 stsp #include <stdio.h>
23 bd3d9e54 2021-09-05 stsp #include <stdlib.h>
24 bd3d9e54 2021-09-05 stsp #include <string.h>
26 bd3d9e54 2021-09-05 stsp #include "got_error.h"
27 bd3d9e54 2021-09-05 stsp #include "got_path.h"
29 bd3d9e54 2021-09-05 stsp #include "got_lib_gitproto.h"
31 bd3d9e54 2021-09-05 stsp #ifndef nitems
32 bd3d9e54 2021-09-05 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
35 3efd8e31 2022-10-23 thomas static void
36 3efd8e31 2022-10-23 thomas free_tokens(char **tokens, size_t ntokens)
40 3efd8e31 2022-10-23 thomas for (i = 0; i < ntokens; i++) {
41 3efd8e31 2022-10-23 thomas free(tokens[i]);
42 3efd8e31 2022-10-23 thomas tokens[i] = NULL;
46 bd3d9e54 2021-09-05 stsp static const struct got_error *
47 3efd8e31 2022-10-23 thomas tokenize_line(char **tokens, char *line, int len, int mintokens, int maxtokens)
49 bd3d9e54 2021-09-05 stsp const struct got_error *err = NULL;
51 bd3d9e54 2021-09-05 stsp size_t i, n = 0;
53 bd3d9e54 2021-09-05 stsp for (i = 0; i < maxtokens; i++)
54 bd3d9e54 2021-09-05 stsp tokens[i] = NULL;
56 bd3d9e54 2021-09-05 stsp for (i = 0; n < len && i < maxtokens; i++) {
57 6771d425 2022-11-17 thomas while (isspace((unsigned char)*line)) {
62 bd3d9e54 2021-09-05 stsp while (*line != '\0' && n < len &&
63 6771d425 2022-11-17 thomas (!isspace((unsigned char)*line) || i == maxtokens - 1)) {
67 bd3d9e54 2021-09-05 stsp tokens[i] = strndup(p, line - p);
68 bd3d9e54 2021-09-05 stsp if (tokens[i] == NULL) {
69 bd3d9e54 2021-09-05 stsp err = got_error_from_errno("strndup");
72 bd3d9e54 2021-09-05 stsp /* Skip \0 field-delimiter at end of token. */
73 bd3d9e54 2021-09-05 stsp while (line[0] == '\0' && n < len) {
78 3efd8e31 2022-10-23 thomas if (i < mintokens)
79 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_PACKET,
80 3efd8e31 2022-10-23 thomas "pkt-line contains too few tokens");
83 3efd8e31 2022-10-23 thomas free_tokens(tokens, i);
87 bd3d9e54 2021-09-05 stsp const struct got_error *
88 bd3d9e54 2021-09-05 stsp got_gitproto_parse_refline(char **id_str, char **refname,
89 bd3d9e54 2021-09-05 stsp char **server_capabilities, char *line, int len)
91 bd3d9e54 2021-09-05 stsp const struct got_error *err = NULL;
92 bd3d9e54 2021-09-05 stsp char *tokens[3];
94 35add24a 2021-10-08 thomas *id_str = NULL;
95 35add24a 2021-10-08 thomas *refname = NULL;
96 35add24a 2021-10-08 thomas /* don't reset *server_capabilities */
98 3efd8e31 2022-10-23 thomas err = tokenize_line(tokens, line, len, 2, nitems(tokens));
100 bd3d9e54 2021-09-05 stsp return err;
102 bd3d9e54 2021-09-05 stsp if (tokens[0])
103 bd3d9e54 2021-09-05 stsp *id_str = tokens[0];
104 bd3d9e54 2021-09-05 stsp if (tokens[1])
105 bd3d9e54 2021-09-05 stsp *refname = tokens[1];
106 bd3d9e54 2021-09-05 stsp if (tokens[2]) {
107 35add24a 2021-10-08 thomas if (*server_capabilities == NULL) {
109 35add24a 2021-10-08 thomas *server_capabilities = tokens[2];
110 35add24a 2021-10-08 thomas p = strrchr(*server_capabilities, '\n');
112 35add24a 2021-10-08 thomas *p = '\0';
114 35add24a 2021-10-08 thomas free(tokens[2]);
117 bd3d9e54 2021-09-05 stsp return NULL;
120 3efd8e31 2022-10-23 thomas const struct got_error *
121 3efd8e31 2022-10-23 thomas got_gitproto_parse_want_line(char **id_str,
122 3efd8e31 2022-10-23 thomas char **capabilities, char *line, int len)
124 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
125 3efd8e31 2022-10-23 thomas char *tokens[3];
127 3efd8e31 2022-10-23 thomas *id_str = NULL;
128 3efd8e31 2022-10-23 thomas /* don't reset *capabilities */
130 3efd8e31 2022-10-23 thomas err = tokenize_line(tokens, line, len, 2, nitems(tokens));
132 3efd8e31 2022-10-23 thomas return err;
134 3efd8e31 2022-10-23 thomas if (tokens[0] == NULL) {
135 3efd8e31 2022-10-23 thomas free_tokens(tokens, nitems(tokens));
136 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_BAD_PACKET, "empty want-line");
139 3efd8e31 2022-10-23 thomas if (strcmp(tokens[0], "want") != 0) {
140 3efd8e31 2022-10-23 thomas free_tokens(tokens, nitems(tokens));
141 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_BAD_PACKET, "bad want-line");
144 3efd8e31 2022-10-23 thomas free(tokens[0]);
145 3efd8e31 2022-10-23 thomas if (tokens[1])
146 3efd8e31 2022-10-23 thomas *id_str = tokens[1];
147 3efd8e31 2022-10-23 thomas if (tokens[2]) {
148 3efd8e31 2022-10-23 thomas if (*capabilities == NULL) {
150 3efd8e31 2022-10-23 thomas *capabilities = tokens[2];
151 3efd8e31 2022-10-23 thomas p = strrchr(*capabilities, '\n');
153 3efd8e31 2022-10-23 thomas *p = '\0';
155 3efd8e31 2022-10-23 thomas free(tokens[2]);
158 3efd8e31 2022-10-23 thomas return NULL;
161 3efd8e31 2022-10-23 thomas const struct got_error *
162 3efd8e31 2022-10-23 thomas got_gitproto_parse_have_line(char **id_str, char *line, int len)
164 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
165 3efd8e31 2022-10-23 thomas char *tokens[2];
167 3efd8e31 2022-10-23 thomas *id_str = NULL;
169 3efd8e31 2022-10-23 thomas err = tokenize_line(tokens, line, len, 2, nitems(tokens));
171 3efd8e31 2022-10-23 thomas return err;
173 3efd8e31 2022-10-23 thomas if (tokens[0] == NULL) {
174 3efd8e31 2022-10-23 thomas free_tokens(tokens, nitems(tokens));
175 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_BAD_PACKET, "empty have-line");
178 3efd8e31 2022-10-23 thomas if (strcmp(tokens[0], "have") != 0) {
179 3efd8e31 2022-10-23 thomas free_tokens(tokens, nitems(tokens));
180 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_BAD_PACKET, "bad have-line");
183 3efd8e31 2022-10-23 thomas free(tokens[0]);
184 3efd8e31 2022-10-23 thomas if (tokens[1])
185 3efd8e31 2022-10-23 thomas *id_str = tokens[1];
187 3efd8e31 2022-10-23 thomas return NULL;
190 3efd8e31 2022-10-23 thomas const struct got_error *
191 3efd8e31 2022-10-23 thomas got_gitproto_parse_ref_update_line(char **old_id_str, char **new_id_str,
192 3efd8e31 2022-10-23 thomas char **refname, char **capabilities, char *line, size_t len)
194 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
195 3efd8e31 2022-10-23 thomas char *tokens[4];
197 3efd8e31 2022-10-23 thomas *old_id_str = NULL;
198 3efd8e31 2022-10-23 thomas *new_id_str = NULL;
199 3efd8e31 2022-10-23 thomas *refname = NULL;
201 3efd8e31 2022-10-23 thomas /* don't reset *capabilities */
203 3efd8e31 2022-10-23 thomas err = tokenize_line(tokens, line, len, 3, nitems(tokens));
205 3efd8e31 2022-10-23 thomas return err;
207 3efd8e31 2022-10-23 thomas if (tokens[0] == NULL || tokens[1] == NULL || tokens[2] == NULL) {
208 3efd8e31 2022-10-23 thomas free_tokens(tokens, nitems(tokens));
209 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_BAD_PACKET, "empty ref-update");
212 3efd8e31 2022-10-23 thomas *old_id_str = tokens[0];
213 3efd8e31 2022-10-23 thomas *new_id_str = tokens[1];
214 3efd8e31 2022-10-23 thomas *refname = tokens[2];
215 3efd8e31 2022-10-23 thomas if (tokens[3]) {
216 3efd8e31 2022-10-23 thomas if (*capabilities == NULL) {
218 3efd8e31 2022-10-23 thomas *capabilities = tokens[3];
219 3efd8e31 2022-10-23 thomas p = strrchr(*capabilities, '\n');
221 3efd8e31 2022-10-23 thomas *p = '\0';
223 3efd8e31 2022-10-23 thomas free(tokens[3]);
226 3efd8e31 2022-10-23 thomas return NULL;
229 bd3d9e54 2021-09-05 stsp static const struct got_error *
230 bd3d9e54 2021-09-05 stsp match_capability(char **my_capabilities, const char *capa,
231 bd3d9e54 2021-09-05 stsp const struct got_capability *mycapa)
233 bd3d9e54 2021-09-05 stsp char *equalsign;
236 bd3d9e54 2021-09-05 stsp equalsign = strchr(capa, '=');
237 bd3d9e54 2021-09-05 stsp if (equalsign) {
238 bd3d9e54 2021-09-05 stsp if (strncmp(capa, mycapa->key, equalsign - capa) != 0)
239 bd3d9e54 2021-09-05 stsp return NULL;
241 bd3d9e54 2021-09-05 stsp if (strcmp(capa, mycapa->key) != 0)
242 bd3d9e54 2021-09-05 stsp return NULL;
245 bd3d9e54 2021-09-05 stsp if (asprintf(&s, "%s %s%s%s",
246 bd3d9e54 2021-09-05 stsp *my_capabilities != NULL ? *my_capabilities : "",
247 bd3d9e54 2021-09-05 stsp mycapa->key,
248 bd3d9e54 2021-09-05 stsp mycapa->value != NULL ? "=" : "",
249 e33e440b 2021-09-05 stsp mycapa->value != NULL ? mycapa->value : "") == -1)
250 bd3d9e54 2021-09-05 stsp return got_error_from_errno("asprintf");
252 bd3d9e54 2021-09-05 stsp free(*my_capabilities);
253 bd3d9e54 2021-09-05 stsp *my_capabilities = s;
254 bd3d9e54 2021-09-05 stsp return NULL;
257 bd3d9e54 2021-09-05 stsp static const struct got_error *
258 bd3d9e54 2021-09-05 stsp add_symref(struct got_pathlist_head *symrefs, char *capa)
260 bd3d9e54 2021-09-05 stsp const struct got_error *err = NULL;
261 bd3d9e54 2021-09-05 stsp char *colon, *name = NULL, *target = NULL;
263 bd3d9e54 2021-09-05 stsp /* Need at least "A:B" */
264 bd3d9e54 2021-09-05 stsp if (strlen(capa) < 3)
265 bd3d9e54 2021-09-05 stsp return NULL;
267 bd3d9e54 2021-09-05 stsp colon = strchr(capa, ':');
268 bd3d9e54 2021-09-05 stsp if (colon == NULL)
269 bd3d9e54 2021-09-05 stsp return NULL;
271 bd3d9e54 2021-09-05 stsp *colon = '\0';
272 bd3d9e54 2021-09-05 stsp name = strdup(capa);
273 bd3d9e54 2021-09-05 stsp if (name == NULL)
274 bd3d9e54 2021-09-05 stsp return got_error_from_errno("strdup");
276 bd3d9e54 2021-09-05 stsp target = strdup(colon + 1);
277 bd3d9e54 2021-09-05 stsp if (target == NULL) {
278 bd3d9e54 2021-09-05 stsp err = got_error_from_errno("strdup");
282 bd3d9e54 2021-09-05 stsp /* We can't validate the ref itself here. The main process will. */
283 bd3d9e54 2021-09-05 stsp err = got_pathlist_append(symrefs, name, target);
286 bd3d9e54 2021-09-05 stsp free(name);
287 bd3d9e54 2021-09-05 stsp free(target);
289 bd3d9e54 2021-09-05 stsp return err;
292 bd3d9e54 2021-09-05 stsp const struct got_error *
293 bd3d9e54 2021-09-05 stsp got_gitproto_match_capabilities(char **common_capabilities,
294 3efd8e31 2022-10-23 thomas struct got_pathlist_head *symrefs, char *capabilities,
295 bd3d9e54 2021-09-05 stsp const struct got_capability my_capabilities[], size_t ncapa)
297 bd3d9e54 2021-09-05 stsp const struct got_error *err = NULL;
298 bd3d9e54 2021-09-05 stsp char *capa, *equalsign;
301 bd3d9e54 2021-09-05 stsp *common_capabilities = NULL;
303 3efd8e31 2022-10-23 thomas capa = strsep(&capabilities, " ");
304 bd3d9e54 2021-09-05 stsp if (capa == NULL)
305 bd3d9e54 2021-09-05 stsp return NULL;
307 bd3d9e54 2021-09-05 stsp equalsign = strchr(capa, '=');
308 bd3d9e54 2021-09-05 stsp if (equalsign != NULL && symrefs != NULL &&
309 bd3d9e54 2021-09-05 stsp strncmp(capa, "symref", equalsign - capa) == 0) {
310 bd3d9e54 2021-09-05 stsp err = add_symref(symrefs, equalsign + 1);
316 bd3d9e54 2021-09-05 stsp for (i = 0; i < ncapa; i++) {
317 bd3d9e54 2021-09-05 stsp err = match_capability(common_capabilities,
318 bd3d9e54 2021-09-05 stsp capa, &my_capabilities[i]);
322 bd3d9e54 2021-09-05 stsp } while (capa);
324 bd3d9e54 2021-09-05 stsp if (*common_capabilities == NULL) {
325 bd3d9e54 2021-09-05 stsp *common_capabilities = strdup("");
326 bd3d9e54 2021-09-05 stsp if (*common_capabilities == NULL)
327 bd3d9e54 2021-09-05 stsp err = got_error_from_errno("strdup");
329 bd3d9e54 2021-09-05 stsp return err;
332 3efd8e31 2022-10-23 thomas const struct got_error *
333 3efd8e31 2022-10-23 thomas got_gitproto_append_capabilities(size_t *capalen, char *buf, size_t offset,
334 3efd8e31 2022-10-23 thomas size_t bufsize, const struct got_capability my_capabilities[], size_t ncapa)
336 3efd8e31 2022-10-23 thomas char *p = buf + offset;
337 3efd8e31 2022-10-23 thomas size_t i, len, remain = bufsize - offset;
339 3efd8e31 2022-10-23 thomas *capalen = 0;
341 3efd8e31 2022-10-23 thomas if (offset >= bufsize || remain < 1)
342 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NO_SPACE);
344 3efd8e31 2022-10-23 thomas /* Capabilities are hidden behind a NUL byte. */
345 3efd8e31 2022-10-23 thomas *p = '\0';
347 3efd8e31 2022-10-23 thomas remain--;
348 3efd8e31 2022-10-23 thomas *capalen += 1;
350 3efd8e31 2022-10-23 thomas for (i = 0; i < ncapa; i++) {
351 3efd8e31 2022-10-23 thomas len = strlcat(p, " ", remain);
352 3efd8e31 2022-10-23 thomas if (len >= remain)
353 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NO_SPACE);
354 3efd8e31 2022-10-23 thomas remain -= len;
355 3efd8e31 2022-10-23 thomas *capalen += 1;
357 3efd8e31 2022-10-23 thomas len = strlcat(p, my_capabilities[i].key, remain);
358 3efd8e31 2022-10-23 thomas if (len >= remain)
359 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NO_SPACE);
360 3efd8e31 2022-10-23 thomas remain -= len;
361 3efd8e31 2022-10-23 thomas *capalen += strlen(my_capabilities[i].key);
363 3efd8e31 2022-10-23 thomas if (my_capabilities[i].value == NULL)
364 3efd8e31 2022-10-23 thomas continue;
366 3efd8e31 2022-10-23 thomas len = strlcat(p, "=", remain);
367 3efd8e31 2022-10-23 thomas if (len >= remain)
368 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NO_SPACE);
369 3efd8e31 2022-10-23 thomas remain -= len;
370 3efd8e31 2022-10-23 thomas *capalen += 1;
372 3efd8e31 2022-10-23 thomas len = strlcat(p, my_capabilities[i].value, remain);
373 3efd8e31 2022-10-23 thomas if (len >= remain)
374 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NO_SPACE);
375 3efd8e31 2022-10-23 thomas remain -= len;
376 3efd8e31 2022-10-23 thomas *capalen += strlen(my_capabilities[i].value);
379 3efd8e31 2022-10-23 thomas return NULL;
382 3efd8e31 2022-10-23 thomas const struct got_error *
383 3efd8e31 2022-10-23 thomas got_gitproto_split_capabilities_str(struct got_capability **capabilities,
384 3efd8e31 2022-10-23 thomas size_t *ncapabilities, char *capabilities_str)
386 3efd8e31 2022-10-23 thomas char *capastr, *capa;
387 3efd8e31 2022-10-23 thomas size_t i;
389 3efd8e31 2022-10-23 thomas *capabilities = NULL;
390 3efd8e31 2022-10-23 thomas *ncapabilities = 0;
392 3efd8e31 2022-10-23 thomas /* Compute number of capabilities on a copy of the input string. */
393 3efd8e31 2022-10-23 thomas capastr = strdup(capabilities_str);
394 3efd8e31 2022-10-23 thomas if (capastr == NULL)
395 3efd8e31 2022-10-23 thomas return got_error_from_errno("strdup");
397 3efd8e31 2022-10-23 thomas capa = strsep(&capastr, " ");
398 3efd8e31 2022-10-23 thomas if (capa && *capa != '\0')
399 3efd8e31 2022-10-23 thomas (*ncapabilities)++;
400 3efd8e31 2022-10-23 thomas } while (capa);
401 3efd8e31 2022-10-23 thomas free(capastr);
403 3efd8e31 2022-10-23 thomas *capabilities = calloc(*ncapabilities, sizeof(**capabilities));
404 3efd8e31 2022-10-23 thomas if (*capabilities == NULL)
405 3efd8e31 2022-10-23 thomas return got_error_from_errno("calloc");
407 3efd8e31 2022-10-23 thomas /* Modify input string in place, splitting it into key/value tuples. */
409 3efd8e31 2022-10-23 thomas for (;;) {
410 3efd8e31 2022-10-23 thomas char *key = NULL, *value = NULL, *equalsign;
412 3efd8e31 2022-10-23 thomas capa = strsep(&capabilities_str, " ");
413 3efd8e31 2022-10-23 thomas if (capa == NULL)
415 3efd8e31 2022-10-23 thomas if (*capa == '\0')
416 3efd8e31 2022-10-23 thomas continue;
418 3efd8e31 2022-10-23 thomas if (i >= *ncapabilities) { /* should not happen */
419 3efd8e31 2022-10-23 thomas free(*capabilities);
420 3efd8e31 2022-10-23 thomas *capabilities = NULL;
421 3efd8e31 2022-10-23 thomas *ncapabilities = 0;
422 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NO_SPACE);
425 3efd8e31 2022-10-23 thomas key = capa;
427 3efd8e31 2022-10-23 thomas equalsign = strchr(capa, '=');
428 3efd8e31 2022-10-23 thomas if (equalsign != NULL) {
429 3efd8e31 2022-10-23 thomas *equalsign = '\0';
430 3efd8e31 2022-10-23 thomas value = equalsign + 1;
433 3efd8e31 2022-10-23 thomas (*capabilities)[i].key = key;
434 3efd8e31 2022-10-23 thomas (*capabilities)[i].value = value;
438 3efd8e31 2022-10-23 thomas return NULL;