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 bd3d9e54 2021-09-05 stsp #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]))
36 13b2bc37 2022-10-23 stsp free_tokens(char **tokens, size_t ntokens)
40 13b2bc37 2022-10-23 stsp for (i = 0; i < ntokens; i++) {
41 13b2bc37 2022-10-23 stsp free(tokens[i]);
42 13b2bc37 2022-10-23 stsp tokens[i] = NULL;
46 bd3d9e54 2021-09-05 stsp static const struct got_error *
47 13b2bc37 2022-10-23 stsp 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 eca2b1d0 2023-01-19 stsp while (n < len && isspace((unsigned char)*line)) {
62 bd3d9e54 2021-09-05 stsp while (*line != '\0' && n < len &&
63 99fd9ff4 2022-11-17 op (!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 13b2bc37 2022-10-23 stsp if (i < mintokens)
79 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_PACKET,
80 13b2bc37 2022-10-23 stsp "pkt-line contains too few tokens");
83 13b2bc37 2022-10-23 stsp 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 e99d9267 2021-10-07 stsp *id_str = NULL;
95 e99d9267 2021-10-07 stsp *refname = NULL;
96 e99d9267 2021-10-07 stsp /* don't reset *server_capabilities */
98 13b2bc37 2022-10-23 stsp 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 e99d9267 2021-10-07 stsp if (*server_capabilities == NULL) {
109 e99d9267 2021-10-07 stsp *server_capabilities = tokens[2];
110 e99d9267 2021-10-07 stsp p = strrchr(*server_capabilities, '\n');
114 e99d9267 2021-10-07 stsp free(tokens[2]);
117 bd3d9e54 2021-09-05 stsp return NULL;
120 13b2bc37 2022-10-23 stsp const struct got_error *
121 13b2bc37 2022-10-23 stsp got_gitproto_parse_want_line(char **id_str,
122 13b2bc37 2022-10-23 stsp char **capabilities, char *line, int len)
124 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
125 13b2bc37 2022-10-23 stsp char *tokens[3];
127 13b2bc37 2022-10-23 stsp *id_str = NULL;
128 13b2bc37 2022-10-23 stsp /* don't reset *capabilities */
130 13b2bc37 2022-10-23 stsp err = tokenize_line(tokens, line, len, 2, nitems(tokens));
132 13b2bc37 2022-10-23 stsp return err;
134 13b2bc37 2022-10-23 stsp if (tokens[0] == NULL) {
135 13b2bc37 2022-10-23 stsp free_tokens(tokens, nitems(tokens));
136 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "empty want-line");
139 13b2bc37 2022-10-23 stsp if (strcmp(tokens[0], "want") != 0) {
140 13b2bc37 2022-10-23 stsp free_tokens(tokens, nitems(tokens));
141 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "bad want-line");
144 13b2bc37 2022-10-23 stsp free(tokens[0]);
145 13b2bc37 2022-10-23 stsp if (tokens[1])
146 13b2bc37 2022-10-23 stsp *id_str = tokens[1];
147 13b2bc37 2022-10-23 stsp if (tokens[2]) {
148 13b2bc37 2022-10-23 stsp if (*capabilities == NULL) {
150 13b2bc37 2022-10-23 stsp *capabilities = tokens[2];
151 13b2bc37 2022-10-23 stsp p = strrchr(*capabilities, '\n');
155 13b2bc37 2022-10-23 stsp free(tokens[2]);
158 13b2bc37 2022-10-23 stsp return NULL;
161 13b2bc37 2022-10-23 stsp const struct got_error *
162 13b2bc37 2022-10-23 stsp got_gitproto_parse_have_line(char **id_str, char *line, int len)
164 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
165 13b2bc37 2022-10-23 stsp char *tokens[2];
167 13b2bc37 2022-10-23 stsp *id_str = NULL;
169 13b2bc37 2022-10-23 stsp err = tokenize_line(tokens, line, len, 2, nitems(tokens));
171 13b2bc37 2022-10-23 stsp return err;
173 13b2bc37 2022-10-23 stsp if (tokens[0] == NULL) {
174 13b2bc37 2022-10-23 stsp free_tokens(tokens, nitems(tokens));
175 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "empty have-line");
178 13b2bc37 2022-10-23 stsp if (strcmp(tokens[0], "have") != 0) {
179 13b2bc37 2022-10-23 stsp free_tokens(tokens, nitems(tokens));
180 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "bad have-line");
183 13b2bc37 2022-10-23 stsp free(tokens[0]);
184 13b2bc37 2022-10-23 stsp if (tokens[1])
185 13b2bc37 2022-10-23 stsp *id_str = tokens[1];
187 13b2bc37 2022-10-23 stsp return NULL;
190 13b2bc37 2022-10-23 stsp const struct got_error *
191 13b2bc37 2022-10-23 stsp got_gitproto_parse_ref_update_line(char **old_id_str, char **new_id_str,
192 13b2bc37 2022-10-23 stsp char **refname, char **capabilities, char *line, size_t len)
194 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
195 13b2bc37 2022-10-23 stsp char *tokens[4];
197 13b2bc37 2022-10-23 stsp *old_id_str = NULL;
198 13b2bc37 2022-10-23 stsp *new_id_str = NULL;
199 13b2bc37 2022-10-23 stsp *refname = NULL;
201 13b2bc37 2022-10-23 stsp /* don't reset *capabilities */
203 13b2bc37 2022-10-23 stsp err = tokenize_line(tokens, line, len, 3, nitems(tokens));
205 13b2bc37 2022-10-23 stsp return err;
207 13b2bc37 2022-10-23 stsp if (tokens[0] == NULL || tokens[1] == NULL || tokens[2] == NULL) {
208 13b2bc37 2022-10-23 stsp free_tokens(tokens, nitems(tokens));
209 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_PACKET, "empty ref-update");
212 13b2bc37 2022-10-23 stsp *old_id_str = tokens[0];
213 13b2bc37 2022-10-23 stsp *new_id_str = tokens[1];
214 13b2bc37 2022-10-23 stsp *refname = tokens[2];
215 13b2bc37 2022-10-23 stsp if (tokens[3]) {
216 13b2bc37 2022-10-23 stsp if (*capabilities == NULL) {
218 13b2bc37 2022-10-23 stsp *capabilities = tokens[3];
219 13b2bc37 2022-10-23 stsp p = strrchr(*capabilities, '\n');
223 13b2bc37 2022-10-23 stsp free(tokens[3]);
226 13b2bc37 2022-10-23 stsp 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 13b2bc37 2022-10-23 stsp 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 13b2bc37 2022-10-23 stsp 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 13b2bc37 2022-10-23 stsp const struct got_error *
333 13b2bc37 2022-10-23 stsp got_gitproto_append_capabilities(size_t *capalen, char *buf, size_t offset,
334 13b2bc37 2022-10-23 stsp size_t bufsize, const struct got_capability my_capabilities[], size_t ncapa)
336 13b2bc37 2022-10-23 stsp char *p = buf + offset;
337 13b2bc37 2022-10-23 stsp size_t i, len, remain = bufsize - offset;
339 13b2bc37 2022-10-23 stsp *capalen = 0;
341 13b2bc37 2022-10-23 stsp if (offset >= bufsize || remain < 1)
342 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_NO_SPACE);
344 13b2bc37 2022-10-23 stsp /* Capabilities are hidden behind a NUL byte. */
348 13b2bc37 2022-10-23 stsp *capalen += 1;
350 13b2bc37 2022-10-23 stsp for (i = 0; i < ncapa; i++) {
351 13b2bc37 2022-10-23 stsp len = strlcat(p, " ", remain);
352 13b2bc37 2022-10-23 stsp if (len >= remain)
353 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_NO_SPACE);
354 13b2bc37 2022-10-23 stsp remain -= len;
355 13b2bc37 2022-10-23 stsp *capalen += 1;
357 13b2bc37 2022-10-23 stsp len = strlcat(p, my_capabilities[i].key, remain);
358 13b2bc37 2022-10-23 stsp if (len >= remain)
359 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_NO_SPACE);
360 13b2bc37 2022-10-23 stsp remain -= len;
361 13b2bc37 2022-10-23 stsp *capalen += strlen(my_capabilities[i].key);
363 13b2bc37 2022-10-23 stsp if (my_capabilities[i].value == NULL)
366 13b2bc37 2022-10-23 stsp len = strlcat(p, "=", remain);
367 13b2bc37 2022-10-23 stsp if (len >= remain)
368 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_NO_SPACE);
369 13b2bc37 2022-10-23 stsp remain -= len;
370 13b2bc37 2022-10-23 stsp *capalen += 1;
372 13b2bc37 2022-10-23 stsp len = strlcat(p, my_capabilities[i].value, remain);
373 13b2bc37 2022-10-23 stsp if (len >= remain)
374 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_NO_SPACE);
375 13b2bc37 2022-10-23 stsp remain -= len;
376 13b2bc37 2022-10-23 stsp *capalen += strlen(my_capabilities[i].value);
379 13b2bc37 2022-10-23 stsp return NULL;
382 13b2bc37 2022-10-23 stsp const struct got_error *
383 13b2bc37 2022-10-23 stsp got_gitproto_split_capabilities_str(struct got_capability **capabilities,
384 13b2bc37 2022-10-23 stsp size_t *ncapabilities, char *capabilities_str)
386 13b2bc37 2022-10-23 stsp char *capastr, *capa;
389 13b2bc37 2022-10-23 stsp *capabilities = NULL;
390 13b2bc37 2022-10-23 stsp *ncapabilities = 0;
392 13b2bc37 2022-10-23 stsp /* Compute number of capabilities on a copy of the input string. */
393 13b2bc37 2022-10-23 stsp capastr = strdup(capabilities_str);
394 13b2bc37 2022-10-23 stsp if (capastr == NULL)
395 13b2bc37 2022-10-23 stsp return got_error_from_errno("strdup");
397 13b2bc37 2022-10-23 stsp capa = strsep(&capastr, " ");
398 13b2bc37 2022-10-23 stsp if (capa && *capa != '\0')
399 13b2bc37 2022-10-23 stsp (*ncapabilities)++;
400 13b2bc37 2022-10-23 stsp } while (capa);
401 13b2bc37 2022-10-23 stsp free(capastr);
403 13b2bc37 2022-10-23 stsp *capabilities = calloc(*ncapabilities, sizeof(**capabilities));
404 13b2bc37 2022-10-23 stsp if (*capabilities == NULL)
405 13b2bc37 2022-10-23 stsp return got_error_from_errno("calloc");
407 13b2bc37 2022-10-23 stsp /* Modify input string in place, splitting it into key/value tuples. */
410 13b2bc37 2022-10-23 stsp char *key = NULL, *value = NULL, *equalsign;
412 13b2bc37 2022-10-23 stsp capa = strsep(&capabilities_str, " ");
413 13b2bc37 2022-10-23 stsp if (capa == NULL)
415 13b2bc37 2022-10-23 stsp if (*capa == '\0')
418 13b2bc37 2022-10-23 stsp if (i >= *ncapabilities) { /* should not happen */
419 13b2bc37 2022-10-23 stsp free(*capabilities);
420 13b2bc37 2022-10-23 stsp *capabilities = NULL;
421 13b2bc37 2022-10-23 stsp *ncapabilities = 0;
422 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_NO_SPACE);
425 13b2bc37 2022-10-23 stsp key = capa;
427 13b2bc37 2022-10-23 stsp equalsign = strchr(capa, '=');
428 13b2bc37 2022-10-23 stsp if (equalsign != NULL) {
429 13b2bc37 2022-10-23 stsp *equalsign = '\0';
430 13b2bc37 2022-10-23 stsp value = equalsign + 1;
433 13b2bc37 2022-10-23 stsp (*capabilities)[i].key = key;
434 13b2bc37 2022-10-23 stsp (*capabilities)[i].value = value;
438 13b2bc37 2022-10-23 stsp return NULL;