Blob


1 /*
2 * Copyright 1986, Larry Wall
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following condition is met:
6 * 1. Redistributions of source code must retain the above copyright notice,
7 * this condition and the following disclaimer.
8 *
9 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
10 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
11 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
12 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
13 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
14 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
15 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
16 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
17 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
18 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
19 * SUCH DAMAGE.
20 */
22 /*
23 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
24 *
25 * Permission to use, copy, modify, and distribute this software for any
26 * purpose with or without fee is hereby granted, provided that the above
27 * copyright notice and this permission notice appear in all copies.
28 *
29 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
30 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
31 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
32 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
33 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
34 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
35 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
36 */
38 #include <sys/types.h>
39 #include <sys/uio.h>
41 #include <ctype.h>
42 #include <limits.h>
43 #include <paths.h>
44 #include <stdint.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
50 #include "got_error.h"
51 #include "got_object.h"
53 #include "got_compat.h"
55 #include "got_lib_delta.h"
56 #include "got_lib_object.h"
57 #include "got_lib_privsep.h"
58 #include "got_lib_sha1.h"
60 struct imsgbuf ibuf;
62 static const struct got_error *
63 send_patch(const char *oldname, const char *newname, const char *commitid,
64 const char *blob, int git)
65 {
66 struct got_imsg_patch p;
68 memset(&p, 0, sizeof(p));
70 if (oldname != NULL)
71 strlcpy(p.old, oldname, sizeof(p.old));
73 if (newname != NULL)
74 strlcpy(p.new, newname, sizeof(p.new));
76 if (commitid != NULL)
77 strlcpy(p.cid, commitid, sizeof(p.cid));
79 if (blob != NULL)
80 strlcpy(p.blob, blob, sizeof(p.blob));
82 p.git = git;
83 if (imsg_compose(&ibuf, GOT_IMSG_PATCH, 0, 0, -1, &p, sizeof(p)) == -1)
84 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH");
85 return NULL;
86 }
88 static const struct got_error *
89 send_patch_done(void)
90 {
91 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_DONE, 0, 0, -1,
92 NULL, 0) == -1)
93 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
94 return got_privsep_flush_imsg(&ibuf);
95 }
97 /* based on fetchname from usr.bin/patch/util.c */
98 static const struct got_error *
99 filename(const char *at, char **name)
101 char *tmp, *t;
103 *name = NULL;
104 if (*at == '\0')
105 return NULL;
107 while (isspace((unsigned char)*at))
108 at++;
110 /* files can be created or removed by diffing against /dev/null */
111 if (!strncmp(at, _PATH_DEVNULL, sizeof(_PATH_DEVNULL) - 1))
112 return NULL;
114 tmp = strdup(at);
115 if (tmp == NULL)
116 return got_error_from_errno("strdup");
117 if ((t = strchr(tmp, '\t')) != NULL)
118 *t = '\0';
119 if ((t = strchr(tmp, '\n')) != NULL)
120 *t = '\0';
122 *name = strdup(tmp);
123 free(tmp);
124 if (*name == NULL)
125 return got_error_from_errno("strdup");
126 return NULL;
129 static const struct got_error *
130 blobid(const char *line, char **blob, int git)
132 uint8_t digest[SHA1_DIGEST_LENGTH];
133 size_t len;
135 *blob = NULL;
137 len = strspn(line, "0123456789abcdefABCDEF");
138 if ((*blob = strndup(line, len)) == NULL)
139 return got_error_from_errno("strndup");
141 if (!git && !got_parse_sha1_digest(digest, *blob)) {
142 /* silently ignore invalid blob ids */
143 free(*blob);
144 *blob = NULL;
146 return NULL;
149 static const struct got_error *
150 patch_start(int *git, char **cid, FILE *fp)
152 const struct got_error *err = NULL;
153 char *line = NULL;
154 size_t linesize = 0;
155 ssize_t linelen;
157 *git = 0;
159 while ((linelen = getline(&line, &linesize, fp)) != -1) {
160 if (!strncmp(line, "diff --git ", 11)) {
161 *git = 1;
162 free(*cid);
163 *cid = NULL;
164 break;
165 } else if (!strncmp(line, "diff ", 5)) {
166 *git = 0;
167 free(*cid);
168 *cid = NULL;
169 } else if (!strncmp(line, "commit - ", 9)) {
170 free(*cid);
171 err = blobid(line + 9, cid, *git);
172 if (err)
173 break;
174 } else if (!strncmp(line, "--- ", 4) ||
175 !strncmp(line, "+++ ", 4) ||
176 !strncmp(line, "blob - ", 7)) {
177 /* rewind to previous line */
178 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
179 err = got_error_from_errno("fseeko");
180 break;
184 free(line);
185 if (ferror(fp) && err == NULL)
186 err = got_error_from_errno("getline");
187 if (feof(fp) && err == NULL)
188 err = got_error(GOT_ERR_NO_PATCH);
189 return err;
192 static const struct got_error *
193 find_diff(int *done, int *next, FILE *fp, int git, const char *commitid)
195 const struct got_error *err = NULL;
196 char *old = NULL, *new = NULL;
197 char *blob = NULL;
198 char *line = NULL;
199 size_t linesize = 0;
200 ssize_t linelen;
201 int create, rename = 0;
203 *done = 0;
204 *next = 0;
205 while ((linelen = getline(&line, &linesize, fp)) != -1) {
206 /*
207 * Ignore the Index name like GNU and larry' patch,
208 * we don't have to follow POSIX.
209 */
211 if (!strncmp(line, "--- ", 4)) {
212 free(old);
213 err = filename(line+4, &old);
214 } else if (rename && !strncmp(line, "rename from ", 12)) {
215 free(old);
216 err = filename(line+12, &old);
217 } else if (!strncmp(line, "+++ ", 4)) {
218 free(new);
219 err = filename(line+4, &new);
220 } else if (!git && !strncmp(line, "blob - ", 7)) {
221 free(blob);
222 err = blobid(line + 7, &blob, git);
223 } else if (rename && !strncmp(line, "rename to ", 10)) {
224 free(new);
225 err = filename(line + 10, &new);
226 } else if (git && !strncmp(line, "similarity index 100%", 21))
227 rename = 1;
228 else if (git && !strncmp(line, "index ", 6)) {
229 free(blob);
230 err = blobid(line + 6, &blob, git);
231 } else if (!strncmp(line, "diff ", 5)) {
232 /* rewind to previous line */
233 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
234 err = got_error_from_errno("fseeko");
235 *next = 1;
236 break;
239 if (err)
240 break;
242 /*
243 * Git-style diffs with "similarity index 100%" don't
244 * have any hunks and ends with the "rename to foobar"
245 * line.
246 */
247 if (rename && old != NULL && new != NULL) {
248 *done = 1;
249 err = send_patch(old, new, commitid,
250 blob, git);
251 break;
254 if (!strncmp(line, "@@ -", 4)) {
255 create = !strncmp(line+4, "0,0", 3);
256 if ((old == NULL && new == NULL) ||
257 (!create && old == NULL))
258 err = got_error(GOT_ERR_PATCH_MALFORMED);
259 else
260 err = send_patch(old, new, commitid,
261 blob, git);
263 if (err)
264 break;
266 /* rewind to previous line */
267 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
268 err = got_error_from_errno("fseeko");
269 break;
273 free(old);
274 free(new);
275 free(blob);
276 free(line);
277 if (ferror(fp) && err == NULL)
278 err = got_error_from_errno("getline");
279 if (feof(fp) && err == NULL)
280 err = got_error(GOT_ERR_NO_PATCH);
281 return err;
284 static const struct got_error *
285 strtolnum(char **str, int *n)
287 char *p, c;
288 const char *errstr;
290 for (p = *str; isdigit((unsigned char)*p); ++p)
291 /* nop */;
293 c = *p;
294 *p = '\0';
296 *n = strtonum(*str, 0, INT_MAX, &errstr);
297 if (errstr != NULL)
298 return got_error(GOT_ERR_PATCH_MALFORMED);
300 *p = c;
301 *str = p;
302 return NULL;
305 static const struct got_error *
306 parse_hdr(char *s, int *done, struct got_imsg_patch_hunk *hdr)
308 static const struct got_error *err = NULL;
310 if (strncmp(s, "@@ -", 4)) {
311 *done = 1;
312 return NULL;
315 s += 4;
316 if (!*s)
317 return NULL;
318 err = strtolnum(&s, &hdr->oldfrom);
319 if (err)
320 return err;
321 if (*s == ',') {
322 s++;
323 err = strtolnum(&s, &hdr->oldlines);
324 if (err)
325 return err;
326 } else
327 hdr->oldlines = 1;
329 if (*s == ' ')
330 s++;
332 if (*s != '+' || !*++s)
333 return got_error(GOT_ERR_PATCH_MALFORMED);
334 err = strtolnum(&s, &hdr->newfrom);
335 if (err)
336 return err;
337 if (*s == ',') {
338 s++;
339 err = strtolnum(&s, &hdr->newlines);
340 if (err)
341 return err;
342 } else
343 hdr->newlines = 1;
345 if (*s == ' ')
346 s++;
348 if (*s != '@')
349 return got_error(GOT_ERR_PATCH_MALFORMED);
351 if (hdr->oldfrom >= INT_MAX - hdr->oldlines ||
352 hdr->newfrom >= INT_MAX - hdr->newlines ||
353 /* not so sure about this one */
354 hdr->oldlines >= INT_MAX - hdr->newlines - 1 ||
355 (hdr->oldlines == 0 && hdr->newlines == 0))
356 return got_error(GOT_ERR_PATCH_MALFORMED);
358 if (hdr->oldlines == 0) {
359 /* larry says to "do append rather than insert"; I don't
360 * quite get it, but i trust him.
361 */
362 hdr->oldfrom++;
365 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_HUNK, 0, 0, -1,
366 hdr, sizeof(*hdr)) == -1)
367 return got_error_from_errno(
368 "imsg_compose GOT_IMSG_PATCH_HUNK");
369 return NULL;
372 static const struct got_error *
373 send_line(const char *line)
375 static const struct got_error *err = NULL;
376 char *p = NULL;
378 if (*line != '+' && *line != '-' && *line != ' ' && *line != '\\') {
379 if (asprintf(&p, " %s", line) == -1)
380 return got_error_from_errno("asprintf");
381 line = p;
384 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_LINE, 0, 0, -1,
385 line, strlen(line) + 1) == -1)
386 err = got_error_from_errno(
387 "imsg_compose GOT_IMSG_PATCH_LINE");
389 free(p);
390 return err;
393 static const struct got_error *
394 peek_special_line(FILE *fp)
396 const struct got_error *err;
397 int ch;
399 ch = fgetc(fp);
400 if (ch != EOF && ch != '\\') {
401 ungetc(ch, fp);
402 return NULL;
405 if (ch == '\\') {
406 err = send_line("\\");
407 if (err)
408 return err;
411 while (ch != EOF && ch != '\n')
412 ch = fgetc(fp);
414 if (ch != EOF || feof(fp))
415 return NULL;
416 return got_error(GOT_ERR_IO);
419 static const struct got_error *
420 parse_hunk(FILE *fp, int *done)
422 static const struct got_error *err = NULL;
423 struct got_imsg_patch_hunk hdr;
424 char *line = NULL, ch;
425 size_t linesize = 0;
426 ssize_t linelen;
427 int leftold, leftnew;
429 linelen = getline(&line, &linesize, fp);
430 if (linelen == -1) {
431 *done = 1;
432 goto done;
435 err = parse_hdr(line, done, &hdr);
436 if (err)
437 goto done;
438 if (*done) {
439 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
440 err = got_error_from_errno("fseeko");
441 goto done;
444 leftold = hdr.oldlines;
445 leftnew = hdr.newlines;
447 while (leftold > 0 || leftnew > 0) {
448 linelen = getline(&line, &linesize, fp);
449 if (linelen == -1) {
450 if (ferror(fp)) {
451 err = got_error_from_errno("getline");
452 goto done;
455 /* trailing newlines may be chopped */
456 if (leftold < 3 && leftnew < 3) {
457 *done = 1;
458 break;
461 err = got_error(GOT_ERR_PATCH_TRUNCATED);
462 goto done;
464 if (line[linelen - 1] == '\n')
465 line[linelen - 1] = '\0';
467 /* usr.bin/patch allows '=' as context char */
468 if (*line == '=')
469 *line = ' ';
471 ch = *line;
472 if (ch == '\t' || ch == '\0')
473 ch = ' '; /* the space got eaten */
475 switch (ch) {
476 case '-':
477 leftold--;
478 break;
479 case ' ':
480 leftold--;
481 leftnew--;
482 break;
483 case '+':
484 leftnew--;
485 break;
486 default:
487 err = got_error(GOT_ERR_PATCH_MALFORMED);
488 goto done;
491 if (leftold < 0 || leftnew < 0) {
492 err = got_error(GOT_ERR_PATCH_MALFORMED);
493 goto done;
496 err = send_line(line);
497 if (err)
498 goto done;
500 if ((ch == '-' && leftold == 0) ||
501 (ch == '+' && leftnew == 0)) {
502 err = peek_special_line(fp);
503 if (err)
504 goto done;
508 done:
509 free(line);
510 return err;
513 static const struct got_error *
514 read_patch(struct imsgbuf *ibuf, int fd)
516 const struct got_error *err = NULL;
517 FILE *fp;
518 int git, patch_found = 0;
519 char *cid = NULL;
521 if ((fp = fdopen(fd, "r")) == NULL) {
522 err = got_error_from_errno("fdopen");
523 close(fd);
524 return err;
527 while ((err = patch_start(&git, &cid, fp)) == NULL) {
528 int done, next;
530 err = find_diff(&done, &next, fp, git, cid);
531 if (err)
532 goto done;
533 if (next)
534 continue;
536 patch_found = 1;
538 while (!done) {
539 err = parse_hunk(fp, &done);
540 if (err)
541 goto done;
544 err = send_patch_done();
545 if (err)
546 goto done;
549 done:
550 fclose(fp);
551 free(cid);
553 /* ignore trailing gibberish */
554 if (err != NULL && err->code == GOT_ERR_NO_PATCH && patch_found)
555 err = NULL;
557 return err;
560 int
561 main(int argc, char **argv)
563 const struct got_error *err = NULL;
564 struct imsg imsg;
565 #if 0
566 static int attached;
567 while (!attached)
568 sleep(1);
569 #endif
571 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
572 #ifndef PROFILE
573 /* revoke access to most system calls */
574 if (pledge("stdio recvfd", NULL) == -1) {
575 err = got_error_from_errno("pledge");
576 got_privsep_send_error(&ibuf, err);
577 return 1;
580 /* revoke fs access */
581 if (landlock_no_fs() == -1) {
582 err = got_error_from_errno("landlock_no_fs");
583 got_privsep_send_error(&ibuf, err);
584 return 1;
586 if (cap_enter() == -1) {
587 err = got_error_from_errno("cap_enter");
588 got_privsep_send_error(&ibuf, err);
589 return 1;
591 #endif
593 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
594 if (err)
595 goto done;
596 if (imsg.hdr.type != GOT_IMSG_PATCH_FILE || imsg.fd == -1) {
597 err = got_error(GOT_ERR_PRIVSEP_MSG);
598 goto done;
601 err = read_patch(&ibuf, imsg.fd);
602 if (err)
603 goto done;
604 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_EOF, 0, 0, -1,
605 NULL, 0) == -1) {
606 err = got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
607 goto done;
609 err = got_privsep_flush_imsg(&ibuf);
610 done:
611 imsg_free(&imsg);
612 if (err != NULL) {
613 got_privsep_send_error(&ibuf, err);
614 err = NULL;
616 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
617 err = got_error_from_errno("close");
618 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
619 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
620 return err ? 1 : 0;