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 */
37 #include "got_compat.h"
39 #include <sys/types.h>
40 #include <sys/queue.h>
41 #include <sys/uio.h>
43 #include <ctype.h>
44 #include <limits.h>
45 #include <paths.h>
46 #include <stdint.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
52 #include "got_error.h"
53 #include "got_object.h"
55 #include "got_lib_delta.h"
56 #include "got_lib_object.h"
57 #include "got_lib_privsep.h"
58 #include "got_lib_hash.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, const int xbit, 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.xbit = xbit;
83 p.git = git;
84 if (imsg_compose(&ibuf, GOT_IMSG_PATCH, 0, 0, -1, &p, sizeof(p)) == -1)
85 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH");
86 return NULL;
87 }
89 static const struct got_error *
90 send_patch_done(void)
91 {
92 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_DONE, 0, 0, -1,
93 NULL, 0) == -1)
94 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
95 return got_privsep_flush_imsg(&ibuf);
96 }
98 /* based on fetchname from usr.bin/patch/util.c */
99 static const struct got_error *
100 filename(const char *at, char **name)
102 char *tmp, *t;
104 *name = NULL;
105 if (*at == '\0')
106 return NULL;
108 while (isspace((unsigned char)*at))
109 at++;
111 /* files can be created or removed by diffing against /dev/null */
112 if (!strncmp(at, _PATH_DEVNULL, sizeof(_PATH_DEVNULL) - 1))
113 return NULL;
115 tmp = strdup(at);
116 if (tmp == NULL)
117 return got_error_from_errno("strdup");
118 if ((t = strchr(tmp, '\t')) != NULL)
119 *t = '\0';
120 if ((t = strchr(tmp, '\n')) != NULL)
121 *t = '\0';
123 *name = strdup(tmp);
124 free(tmp);
125 if (*name == NULL)
126 return got_error_from_errno("strdup");
127 return NULL;
130 static int
131 binary_deleted(const char *line)
133 const char *prefix = "Binary files ";
134 const char *suffix = " and /dev/null differ\n";
135 size_t len, d;
137 if (strncmp(line, prefix, strlen(prefix)) != 0)
138 return 0;
139 line += strlen(prefix);
141 len = strlen(line);
142 if (len <= strlen(suffix))
143 return 0;
144 d = len - strlen(suffix);
145 return (strcmp(line + d, suffix) == 0);
148 static const struct got_error *
149 binaryfilename(const char *at, char **name)
151 const char *suffix = " and /dev/null differ\n";
152 size_t len, d;
154 *name = NULL;
156 len = strlen(at);
157 if (len <= strlen(suffix))
158 return NULL;
160 d = len - strlen(suffix);
161 if (strcmp(at + d, suffix) != 0)
162 return NULL;
164 *name = strndup(at, d);
165 if (*name == NULL)
166 return got_error_from_errno("strndup");
167 return NULL;
170 static int
171 filexbit(const char *line)
173 char *m;
175 m = strchr(line, '(');
176 if (m && !strncmp(m + 1, "mode ", 5))
177 return strncmp(m + 6, "755", 3) == 0;
179 return 0;
182 static const struct got_error *
183 blobid(const char *line, char **blob, int git)
185 uint8_t digest[SHA1_DIGEST_LENGTH];
186 size_t len;
188 *blob = NULL;
190 len = strspn(line, "0123456789abcdefABCDEF");
191 if ((*blob = strndup(line, len)) == NULL)
192 return got_error_from_errno("strndup");
194 if (!git && !got_parse_hash_digest(digest, *blob, GOT_HASH_SHA1)) {
195 /* silently ignore invalid blob ids */
196 free(*blob);
197 *blob = NULL;
199 return NULL;
202 static const struct got_error *
203 patch_start(int *git, char **cid, FILE *fp)
205 const struct got_error *err = NULL;
206 char *line = NULL;
207 size_t linesize = 0;
208 ssize_t linelen;
210 *git = 0;
212 while ((linelen = getline(&line, &linesize, fp)) != -1) {
213 if (!strncmp(line, "diff --git ", 11)) {
214 *git = 1;
215 free(*cid);
216 *cid = NULL;
217 break;
218 } else if (!strncmp(line, "diff ", 5)) {
219 *git = 0;
220 free(*cid);
221 *cid = NULL;
222 } else if (!strncmp(line, "commit - ", 9)) {
223 free(*cid);
224 err = blobid(line + 9, cid, *git);
225 if (err)
226 break;
227 } else if (!strncmp(line, "--- ", 4) ||
228 !strncmp(line, "+++ ", 4) ||
229 !strncmp(line, "blob - ", 7) ||
230 binary_deleted(line)) {
231 /* rewind to previous line */
232 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
233 err = got_error_from_errno("fseeko");
234 break;
238 free(line);
239 if (ferror(fp) && err == NULL)
240 err = got_error_from_errno("getline");
241 if (feof(fp) && err == NULL)
242 err = got_error(GOT_ERR_NO_PATCH);
243 return err;
246 static const struct got_error *
247 find_diff(int *done, int *next, FILE *fp, int git, const char *commitid)
249 const struct got_error *err = NULL;
250 char *old = NULL, *new = NULL;
251 char *blob = NULL;
252 char *line = NULL;
253 size_t linesize = 0;
254 ssize_t linelen;
255 int create, delete_binary = 0, rename = 0, xbit = 0;
257 *done = 0;
258 *next = 0;
259 while ((linelen = getline(&line, &linesize, fp)) != -1) {
260 /*
261 * Ignore the Index name like GNU and larry' patch,
262 * we don't have to follow POSIX.
263 */
265 if (!strncmp(line, "--- ", 4)) {
266 free(old);
267 err = filename(line+4, &old);
268 } else if (rename && !strncmp(line, "rename from ", 12)) {
269 free(old);
270 err = filename(line+12, &old);
271 } else if (!strncmp(line, "+++ ", 4)) {
272 free(new);
273 err = filename(line+4, &new);
274 } else if (!strncmp(line, "blob + ", 7) ||
275 !strncmp(line, "file + ", 7)) {
276 xbit = filexbit(line);
277 } else if (!git && !strncmp(line, "blob - ", 7)) {
278 free(blob);
279 err = blobid(line + 7, &blob, git);
280 } else if (!strncmp(line, "Binary files ", 13)) {
281 delete_binary = 1;
282 free(old);
283 err = binaryfilename(line + 13, &old);
284 } else if (rename && !strncmp(line, "rename to ", 10)) {
285 free(new);
286 err = filename(line + 10, &new);
287 } else if (git && !strncmp(line, "similarity index 100%", 21))
288 rename = 1;
289 else if (git && !strncmp(line, "new file mode 100", 17))
290 xbit = strncmp(line + 17, "755", 3) == 0;
291 else if (git && !strncmp(line, "index ", 6)) {
292 free(blob);
293 err = blobid(line + 6, &blob, git);
294 } else if (!strncmp(line, "diff ", 5)) {
295 /* rewind to previous line */
296 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
297 err = got_error_from_errno("fseeko");
298 *next = 1;
299 break;
302 if (err)
303 break;
305 /*
306 * Git-style diffs with "similarity index 100%" don't
307 * have any hunks and ends with the "rename to foobar"
308 * line.
309 */
310 if (rename && old != NULL && new != NULL) {
311 *done = 1;
312 err = send_patch(old, new, commitid,
313 blob, xbit, git);
314 break;
317 /*
318 * Diffs that remove binary files have no hunks.
319 */
320 if (delete_binary && old != NULL) {
321 *done = 1;
322 err = send_patch(old, new, commitid,
323 blob, xbit, git);
324 break;
327 if (!strncmp(line, "@@ -", 4)) {
328 create = !strncmp(line+4, "0,0", 3);
329 if ((old == NULL && new == NULL) ||
330 (!create && old == NULL))
331 err = got_error(GOT_ERR_PATCH_MALFORMED);
332 else
333 err = send_patch(old, new, commitid,
334 blob, xbit, git);
336 if (err)
337 break;
339 /* rewind to previous line */
340 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
341 err = got_error_from_errno("fseeko");
342 break;
346 free(old);
347 free(new);
348 free(blob);
349 free(line);
350 if (ferror(fp) && err == NULL)
351 err = got_error_from_errno("getline");
352 if (feof(fp) && err == NULL)
353 err = got_error(GOT_ERR_NO_PATCH);
354 return err;
357 static const struct got_error *
358 strtolnum(char **str, int *n)
360 char *p, c;
361 const char *errstr;
363 for (p = *str; isdigit((unsigned char)*p); ++p)
364 /* nop */;
366 c = *p;
367 *p = '\0';
369 *n = strtonum(*str, 0, INT_MAX, &errstr);
370 if (errstr != NULL)
371 return got_error(GOT_ERR_PATCH_MALFORMED);
373 *p = c;
374 *str = p;
375 return NULL;
378 static const struct got_error *
379 parse_hdr(char *s, int *done, struct got_imsg_patch_hunk *hdr)
381 static const struct got_error *err = NULL;
383 if (strncmp(s, "@@ -", 4)) {
384 *done = 1;
385 return NULL;
388 s += 4;
389 if (!*s)
390 return NULL;
391 err = strtolnum(&s, &hdr->oldfrom);
392 if (err)
393 return err;
394 if (*s == ',') {
395 s++;
396 err = strtolnum(&s, &hdr->oldlines);
397 if (err)
398 return err;
399 } else
400 hdr->oldlines = 1;
402 if (*s == ' ')
403 s++;
405 if (*s != '+' || !*++s)
406 return got_error(GOT_ERR_PATCH_MALFORMED);
407 err = strtolnum(&s, &hdr->newfrom);
408 if (err)
409 return err;
410 if (*s == ',') {
411 s++;
412 err = strtolnum(&s, &hdr->newlines);
413 if (err)
414 return err;
415 } else
416 hdr->newlines = 1;
418 if (*s == ' ')
419 s++;
421 if (*s != '@')
422 return got_error(GOT_ERR_PATCH_MALFORMED);
424 if (hdr->oldfrom >= INT_MAX - hdr->oldlines ||
425 hdr->newfrom >= INT_MAX - hdr->newlines ||
426 /* not so sure about this one */
427 hdr->oldlines >= INT_MAX - hdr->newlines - 1 ||
428 (hdr->oldlines == 0 && hdr->newlines == 0))
429 return got_error(GOT_ERR_PATCH_MALFORMED);
431 if (hdr->oldlines == 0) {
432 /* larry says to "do append rather than insert"; I don't
433 * quite get it, but i trust him.
434 */
435 hdr->oldfrom++;
438 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_HUNK, 0, 0, -1,
439 hdr, sizeof(*hdr)) == -1)
440 return got_error_from_errno(
441 "imsg_compose GOT_IMSG_PATCH_HUNK");
442 return NULL;
445 static const struct got_error *
446 send_line(const char *line, size_t len)
448 static const struct got_error *err = NULL;
449 struct iovec iov[2];
450 int iovcnt = 0;
452 memset(&iov, 0, sizeof(iov));
454 if (*line != '+' && *line != '-' && *line != ' ' && *line != '\\') {
455 iov[iovcnt].iov_base = (void *)" ";
456 iov[iovcnt].iov_len = 1;
457 iovcnt++;
460 iov[iovcnt].iov_base = (void *)line;
461 iov[iovcnt].iov_len = len;
462 iovcnt++;
464 if (imsg_composev(&ibuf, GOT_IMSG_PATCH_LINE, 0, 0, -1,
465 iov, iovcnt) == -1)
466 err = got_error_from_errno(
467 "imsg_compose GOT_IMSG_PATCH_LINE");
469 return err;
472 static const struct got_error *
473 peek_special_line(FILE *fp)
475 const struct got_error *err;
476 int ch;
478 ch = fgetc(fp);
479 if (ch != EOF && ch != '\\') {
480 ungetc(ch, fp);
481 return NULL;
484 if (ch == '\\') {
485 err = send_line("\\", 2);
486 if (err)
487 return err;
490 while (ch != EOF && ch != '\n')
491 ch = fgetc(fp);
493 if (ch != EOF || feof(fp))
494 return NULL;
495 return got_error(GOT_ERR_IO);
498 static const struct got_error *
499 parse_hunk(FILE *fp, int *done)
501 static const struct got_error *err = NULL;
502 struct got_imsg_patch_hunk hdr;
503 char *line = NULL, ch;
504 size_t linesize = 0;
505 ssize_t linelen;
506 int leftold, leftnew;
508 linelen = getline(&line, &linesize, fp);
509 if (linelen == -1) {
510 *done = 1;
511 goto done;
514 err = parse_hdr(line, done, &hdr);
515 if (err)
516 goto done;
517 if (*done) {
518 if (fseeko(fp, -linelen, SEEK_CUR) == -1)
519 err = got_error_from_errno("fseeko");
520 goto done;
523 leftold = hdr.oldlines;
524 leftnew = hdr.newlines;
526 while (leftold > 0 || leftnew > 0) {
527 linelen = getline(&line, &linesize, fp);
528 if (linelen == -1) {
529 if (ferror(fp)) {
530 err = got_error_from_errno("getline");
531 goto done;
534 /* trailing newlines may be chopped */
535 if (leftold < 3 && leftnew < 3) {
536 *done = 1;
537 break;
540 err = got_error(GOT_ERR_PATCH_TRUNCATED);
541 goto done;
543 if (line[linelen - 1] == '\n')
544 line[linelen - 1] = '\0';
546 /* usr.bin/patch allows '=' as context char */
547 if (*line == '=')
548 *line = ' ';
550 ch = *line;
551 if (ch == '\t' || ch == '\0')
552 ch = ' '; /* the space got eaten */
554 switch (ch) {
555 case '-':
556 leftold--;
557 break;
558 case ' ':
559 leftold--;
560 leftnew--;
561 break;
562 case '+':
563 leftnew--;
564 break;
565 default:
566 err = got_error(GOT_ERR_PATCH_MALFORMED);
567 goto done;
570 if (leftold < 0 || leftnew < 0) {
571 err = got_error(GOT_ERR_PATCH_MALFORMED);
572 goto done;
575 err = send_line(line, linelen);
576 if (err)
577 goto done;
579 if ((ch == '-' && leftold == 0) ||
580 (ch == '+' && leftnew == 0)) {
581 err = peek_special_line(fp);
582 if (err)
583 goto done;
587 done:
588 free(line);
589 return err;
592 static const struct got_error *
593 read_patch(struct imsgbuf *ibuf, FILE *fp)
595 const struct got_error *err = NULL;
596 int git, patch_found = 0;
597 char *cid = NULL;
599 while ((err = patch_start(&git, &cid, fp)) == NULL) {
600 int done, next;
602 err = find_diff(&done, &next, fp, git, cid);
603 if (err)
604 goto done;
605 if (next)
606 continue;
608 patch_found = 1;
610 while (!done) {
611 err = parse_hunk(fp, &done);
612 if (err)
613 goto done;
616 err = send_patch_done();
617 if (err)
618 goto done;
621 done:
622 free(cid);
624 /* ignore trailing gibberish */
625 if (err != NULL && err->code == GOT_ERR_NO_PATCH && patch_found)
626 err = NULL;
628 return err;
631 int
632 main(int argc, char **argv)
634 const struct got_error *err = NULL;
635 struct imsg imsg;
636 FILE *fp = NULL;
637 int fd = -1;
638 #if 0
639 static int attached;
640 while (!attached)
641 sleep(1);
642 #endif
644 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
645 #ifndef PROFILE
646 /* revoke access to most system calls */
647 if (pledge("stdio recvfd", NULL) == -1) {
648 err = got_error_from_errno("pledge");
649 got_privsep_send_error(&ibuf, err);
650 return 1;
653 /* revoke fs access */
654 if (landlock_no_fs() == -1) {
655 err = got_error_from_errno("landlock_no_fs");
656 got_privsep_send_error(&ibuf, err);
657 return 1;
659 if (cap_enter() == -1) {
660 err = got_error_from_errno("cap_enter");
661 got_privsep_send_error(&ibuf, err);
662 return 1;
664 #endif
666 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
667 if (err)
668 goto done;
669 if (imsg.hdr.type != GOT_IMSG_PATCH_FILE) {
670 err = got_error(GOT_ERR_PRIVSEP_MSG);
671 goto done;
673 fd = imsg_get_fd(&imsg);
674 if (fd == -1) {
675 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
676 goto done;
679 fp = fdopen(fd, "r");
680 if (fp == NULL) {
681 err = got_error_from_errno("fdopen");
682 goto done;
684 fd = -1;
686 err = read_patch(&ibuf, fp);
687 if (err)
688 goto done;
689 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_EOF, 0, 0, -1,
690 NULL, 0) == -1) {
691 err = got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
692 goto done;
694 err = got_privsep_flush_imsg(&ibuf);
695 imsg_free(&imsg);
696 done:
697 if (fd != -1 && close(fd) == -1 && err == NULL)
698 err = got_error_from_errno("close");
699 if (fp != NULL && fclose(fp) == EOF && err == NULL)
700 err = got_error_from_errno("fclose");
701 if (err != NULL) {
702 got_privsep_send_error(&ibuf, err);
703 err = NULL;
705 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
706 err = got_error_from_errno("close");
707 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
708 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
709 return err ? 1 : 0;