2 * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
3 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
19 #include <sys/types.h>
26 #include "got_error.h"
29 #include "got_lib_gitproto.h"
32 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
36 free_tokens(char **tokens, size_t ntokens)
40 for (i = 0; i < ntokens; i++) {
46 static const struct got_error *
47 tokenize_line(char **tokens, char *line, int len, int mintokens, int maxtokens)
49 const struct got_error *err = NULL;
53 for (i = 0; i < maxtokens; i++)
56 for (i = 0; n < len && i < maxtokens; i++) {
57 while (n < len && isspace((unsigned char)*line)) {
62 while (*line != '\0' && n < len &&
63 (!isspace((unsigned char)*line) || i == maxtokens - 1)) {
67 tokens[i] = strndup(p, line - p);
68 if (tokens[i] == NULL) {
69 err = got_error_from_errno("strndup");
72 /* Skip \0 field-delimiter at end of token. */
73 while (line[0] == '\0' && n < len) {
79 err = got_error_msg(GOT_ERR_BAD_PACKET,
80 "pkt-line contains too few tokens");
83 free_tokens(tokens, i);
87 const struct got_error *
88 got_gitproto_parse_refline(char **id_str, char **refname,
89 char **server_capabilities, char *line, int len)
91 const struct got_error *err = NULL;
96 /* don't reset *server_capabilities */
98 err = tokenize_line(tokens, line, len, 2, nitems(tokens));
105 *refname = tokens[1];
107 if (*server_capabilities == NULL) {
109 *server_capabilities = tokens[2];
110 p = strrchr(*server_capabilities, '\n');
120 const struct got_error *
121 got_gitproto_parse_want_line(char **id_str,
122 char **capabilities, char *line, int len)
124 const struct got_error *err = NULL;
128 /* don't reset *capabilities */
130 err = tokenize_line(tokens, line, len, 2, nitems(tokens));
134 if (tokens[0] == NULL) {
135 free_tokens(tokens, nitems(tokens));
136 return got_error_msg(GOT_ERR_BAD_PACKET, "empty want-line");
139 if (strcmp(tokens[0], "want") != 0) {
140 free_tokens(tokens, nitems(tokens));
141 return got_error_msg(GOT_ERR_BAD_PACKET, "bad want-line");
148 if (*capabilities == NULL) {
150 *capabilities = tokens[2];
151 p = strrchr(*capabilities, '\n');
161 const struct got_error *
162 got_gitproto_parse_have_line(char **id_str, char *line, int len)
164 const struct got_error *err = NULL;
169 err = tokenize_line(tokens, line, len, 2, nitems(tokens));
173 if (tokens[0] == NULL) {
174 free_tokens(tokens, nitems(tokens));
175 return got_error_msg(GOT_ERR_BAD_PACKET, "empty have-line");
178 if (strcmp(tokens[0], "have") != 0) {
179 free_tokens(tokens, nitems(tokens));
180 return got_error_msg(GOT_ERR_BAD_PACKET, "bad have-line");
190 const struct got_error *
191 got_gitproto_parse_ref_update_line(char **old_id_str, char **new_id_str,
192 char **refname, char **capabilities, char *line, size_t len)
194 const struct got_error *err = NULL;
201 /* don't reset *capabilities */
203 err = tokenize_line(tokens, line, len, 3, nitems(tokens));
207 if (tokens[0] == NULL || tokens[1] == NULL || tokens[2] == NULL) {
208 free_tokens(tokens, nitems(tokens));
209 return got_error_msg(GOT_ERR_BAD_PACKET, "empty ref-update");
212 *old_id_str = tokens[0];
213 *new_id_str = tokens[1];
214 *refname = tokens[2];
216 if (*capabilities == NULL) {
218 *capabilities = tokens[3];
219 p = strrchr(*capabilities, '\n');
229 static const struct got_error *
230 match_capability(char **my_capabilities, const char *capa,
231 const struct got_capability *mycapa)
236 equalsign = strchr(capa, '=');
238 if (strncmp(capa, mycapa->key, equalsign - capa) != 0)
241 if (strcmp(capa, mycapa->key) != 0)
245 if (asprintf(&s, "%s %s%s%s",
246 *my_capabilities != NULL ? *my_capabilities : "",
248 mycapa->value != NULL ? "=" : "",
249 mycapa->value != NULL ? mycapa->value : "") == -1)
250 return got_error_from_errno("asprintf");
252 free(*my_capabilities);
253 *my_capabilities = s;
257 static const struct got_error *
258 add_symref(struct got_pathlist_head *symrefs, char *capa)
260 const struct got_error *err = NULL;
261 char *colon, *name = NULL, *target = NULL;
263 /* Need at least "A:B" */
264 if (strlen(capa) < 3)
267 colon = strchr(capa, ':');
274 return got_error_from_errno("strdup");
276 target = strdup(colon + 1);
277 if (target == NULL) {
278 err = got_error_from_errno("strdup");
282 /* We can't validate the ref itself here. The main process will. */
283 err = got_pathlist_append(symrefs, name, target);
292 const struct got_error *
293 got_gitproto_match_capabilities(char **common_capabilities,
294 struct got_pathlist_head *symrefs, char *capabilities,
295 const struct got_capability my_capabilities[], size_t ncapa)
297 const struct got_error *err = NULL;
298 char *capa, *equalsign;
301 *common_capabilities = NULL;
303 capa = strsep(&capabilities, " ");
307 equalsign = strchr(capa, '=');
308 if (equalsign != NULL && symrefs != NULL &&
309 strncmp(capa, "symref", equalsign - capa) == 0) {
310 err = add_symref(symrefs, equalsign + 1);
316 for (i = 0; i < ncapa; i++) {
317 err = match_capability(common_capabilities,
318 capa, &my_capabilities[i]);
324 if (*common_capabilities == NULL) {
325 *common_capabilities = strdup("");
326 if (*common_capabilities == NULL)
327 err = got_error_from_errno("strdup");
332 const struct got_error *
333 got_gitproto_append_capabilities(size_t *capalen, char *buf, size_t offset,
334 size_t bufsize, const struct got_capability my_capabilities[], size_t ncapa)
336 char *p = buf + offset;
337 size_t i, len, remain = bufsize - offset;
341 if (offset >= bufsize || remain < 1)
342 return got_error(GOT_ERR_NO_SPACE);
344 /* Capabilities are hidden behind a NUL byte. */
350 for (i = 0; i < ncapa; i++) {
351 len = strlcat(p, " ", remain);
353 return got_error(GOT_ERR_NO_SPACE);
357 len = strlcat(p, my_capabilities[i].key, remain);
359 return got_error(GOT_ERR_NO_SPACE);
361 *capalen += strlen(my_capabilities[i].key);
363 if (my_capabilities[i].value == NULL)
366 len = strlcat(p, "=", remain);
368 return got_error(GOT_ERR_NO_SPACE);
372 len = strlcat(p, my_capabilities[i].value, remain);
374 return got_error(GOT_ERR_NO_SPACE);
376 *capalen += strlen(my_capabilities[i].value);
382 const struct got_error *
383 got_gitproto_split_capabilities_str(struct got_capability **capabilities,
384 size_t *ncapabilities, char *capabilities_str)
386 char *capastr, *capa;
389 *capabilities = NULL;
392 /* Compute number of capabilities on a copy of the input string. */
393 capastr = strdup(capabilities_str);
395 return got_error_from_errno("strdup");
397 capa = strsep(&capastr, " ");
398 if (capa && *capa != '\0')
403 *capabilities = calloc(*ncapabilities, sizeof(**capabilities));
404 if (*capabilities == NULL)
405 return got_error_from_errno("calloc");
407 /* Modify input string in place, splitting it into key/value tuples. */
410 char *key = NULL, *value = NULL, *equalsign;
412 capa = strsep(&capabilities_str, " ");
418 if (i >= *ncapabilities) { /* should not happen */
420 *capabilities = NULL;
422 return got_error(GOT_ERR_NO_SPACE);
427 equalsign = strchr(capa, '=');
428 if (equalsign != NULL) {
430 value = equalsign + 1;
433 (*capabilities)[i].key = key;
434 (*capabilities)[i].value = value;