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"
59 struct imsgbuf ibuf;
61 static const struct got_error *
62 send_patch(const char *oldname, const char *newname, int git)
63 {
64 struct got_imsg_patch p;
66 memset(&p, 0, sizeof(p));
68 if (oldname != NULL)
69 strlcpy(p.old, oldname, sizeof(p.old));
71 if (newname != NULL)
72 strlcpy(p.new, newname, sizeof(p.new));
74 p.git = git;
75 if (imsg_compose(&ibuf, GOT_IMSG_PATCH, 0, 0, -1,
76 &p, sizeof(p)) == -1)
77 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH");
78 return NULL;
79 }
81 static const struct got_error *
82 send_patch_done(void)
83 {
84 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_DONE, 0, 0, -1,
85 NULL, 0) == -1)
86 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
87 if (imsg_flush(&ibuf) == -1)
88 return got_error_from_errno("imsg_flush");
89 return NULL;
90 }
92 /* based on fetchname from usr.bin/patch/util.c */
93 static const struct got_error *
94 filename(const char *at, char **name)
95 {
96 char *tmp, *t;
98 *name = NULL;
99 if (*at == '\0')
100 return NULL;
102 while (isspace((unsigned char)*at))
103 at++;
105 /* files can be created or removed by diffing against /dev/null */
106 if (!strncmp(at, _PATH_DEVNULL, sizeof(_PATH_DEVNULL) - 1))
107 return NULL;
109 tmp = strdup(at);
110 if (tmp == NULL)
111 return got_error_from_errno("strdup");
112 if ((t = strchr(tmp, '\t')) != NULL)
113 *t = '\0';
114 if ((t = strchr(tmp, '\n')) != NULL)
115 *t = '\0';
117 *name = strdup(tmp);
118 free(tmp);
119 if (*name == NULL)
120 return got_error_from_errno("strdup");
121 return NULL;
124 static const struct got_error *
125 find_patch(FILE *fp)
127 const struct got_error *err = NULL;
128 char *old = NULL, *new = NULL;
129 char *line = NULL;
130 size_t linesize = 0;
131 ssize_t linelen;
132 int create, git = 0;
134 while ((linelen = getline(&line, &linesize, fp)) != -1) {
135 /*
136 * Ignore the Index name like GNU and larry' patch,
137 * we don't have to follow POSIX.
138 */
140 if (!strncmp(line, "--- ", 4)) {
141 free(old);
142 err = filename(line+4, &old);
143 } else if (!strncmp(line, "+++ ", 4)) {
144 free(new);
145 err = filename(line+4, &new);
146 } else if (!strncmp(line, "diff --git a/", 13))
147 git = 1;
149 if (err)
150 break;
152 if (!strncmp(line, "@@ -", 4)) {
153 create = !strncmp(line+4, "0,0", 3);
154 if ((old == NULL && new == NULL) ||
155 (!create && old == NULL))
156 err = got_error(GOT_ERR_PATCH_MALFORMED);
157 else
158 err = send_patch(old, new, git);
160 if (err)
161 break;
163 /* rewind to previous line */
164 if (fseek(fp, linelen * -1, SEEK_CUR) == -1)
165 err = got_error_from_errno("fseek");
166 break;
170 free(old);
171 free(new);
172 free(line);
173 if (ferror(fp) && err == NULL)
174 err = got_error_from_errno("getline");
175 if (feof(fp) && err == NULL)
176 err = got_error(GOT_ERR_NO_PATCH);
177 return err;
180 static const struct got_error *
181 strtolnum(char **str, long *n)
183 char *p, c;
184 const char *errstr;
186 for (p = *str; isdigit((unsigned char)*p); ++p)
187 /* nop */;
189 c = *p;
190 *p = '\0';
192 *n = strtonum(*str, 0, LONG_MAX, &errstr);
193 if (errstr != NULL)
194 return got_error(GOT_ERR_PATCH_MALFORMED);
196 *p = c;
197 *str = p;
198 return NULL;
201 static const struct got_error *
202 parse_hdr(char *s, int *ok, struct got_imsg_patch_hunk *hdr)
204 static const struct got_error *err = NULL;
206 *ok = 1;
207 if (strncmp(s, "@@ -", 4)) {
208 *ok = 0;
209 return NULL;
212 s += 4;
213 if (!*s)
214 return NULL;
215 err = strtolnum(&s, &hdr->oldfrom);
216 if (err)
217 return err;
218 if (*s == ',') {
219 s++;
220 err = strtolnum(&s, &hdr->oldlines);
221 if (err)
222 return err;
223 } else
224 hdr->oldlines = 1;
226 if (*s == ' ')
227 s++;
229 if (*s != '+' || !*++s)
230 return got_error(GOT_ERR_PATCH_MALFORMED);
231 err = strtolnum(&s, &hdr->newfrom);
232 if (err)
233 return err;
234 if (*s == ',') {
235 s++;
236 err = strtolnum(&s, &hdr->newlines);
237 if (err)
238 return err;
239 } else
240 hdr->newlines = 1;
242 if (*s == ' ')
243 s++;
245 if (*s != '@')
246 return got_error(GOT_ERR_PATCH_MALFORMED);
248 if (hdr->oldfrom >= LONG_MAX - hdr->oldlines ||
249 hdr->newfrom >= LONG_MAX - hdr->newlines ||
250 /* not so sure about this one */
251 hdr->oldlines >= LONG_MAX - hdr->newlines - 1)
252 return got_error(GOT_ERR_PATCH_MALFORMED);
254 if (hdr->oldlines == 0) {
255 /* larry says to "do append rather than insert"; I don't
256 * quite get it, but i trust him.
257 */
258 hdr->oldfrom++;
261 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_HUNK, 0, 0, -1,
262 hdr, sizeof(*hdr)) == -1)
263 return got_error_from_errno(
264 "imsg_compose GOT_IMSG_PATCH_HUNK");
265 return NULL;
268 static const struct got_error *
269 send_line(const char *line)
271 static const struct got_error *err = NULL;
272 char *p = NULL;
274 if (*line != '+' && *line != '-' && *line != ' ' && *line != '\\') {
275 if (asprintf(&p, " %s", line) == -1)
276 return got_error_from_errno("asprintf");
277 line = p;
280 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_LINE, 0, 0, -1,
281 line, strlen(line) + 1) == -1)
282 err = got_error_from_errno(
283 "imsg_compose GOT_IMSG_PATCH_LINE");
285 free(p);
286 return err;
289 static const struct got_error *
290 peek_special_line(FILE *fp, int send)
292 const struct got_error *err;
293 int ch;
295 ch = fgetc(fp);
296 if (ch != EOF && ch != '\\') {
297 ungetc(ch, fp);
298 return NULL;
301 if (ch == '\\' && send) {
302 err = send_line("\\");
303 if (err)
304 return err;
307 while (ch != EOF && ch != '\n')
308 ch = fgetc(fp);
310 if (ch != EOF || feof(fp))
311 return NULL;
312 return got_error(GOT_ERR_IO);
315 static const struct got_error *
316 parse_hunk(FILE *fp, int *ok)
318 static const struct got_error *err = NULL;
319 struct got_imsg_patch_hunk hdr;
320 char *line = NULL, ch;
321 size_t linesize = 0;
322 ssize_t linelen;
323 long leftold, leftnew;
325 linelen = getline(&line, &linesize, fp);
326 if (linelen == -1) {
327 *ok = 0;
328 goto done;
331 err = parse_hdr(line, ok, &hdr);
332 if (err)
333 goto done;
334 if (!*ok) {
335 if (fseek(fp, linelen * -1, SEEK_CUR) == -1)
336 err = got_error_from_errno("fseek");
337 goto done;
340 leftold = hdr.oldlines;
341 leftnew = hdr.newlines;
343 while (leftold > 0 || leftnew > 0) {
344 linelen = getline(&line, &linesize, fp);
345 if (linelen == -1) {
346 if (ferror(fp)) {
347 err = got_error_from_errno("getline");
348 goto done;
351 /* trailing newlines may be chopped */
352 if (leftold < 3 && leftnew < 3) {
353 *ok = 0;
354 break;
357 err = got_error(GOT_ERR_PATCH_TRUNCATED);
358 goto done;
360 if (line[linelen - 1] == '\n')
361 line[linelen - 1] = '\0';
363 /* usr.bin/patch allows '=' as context char */
364 if (*line == '=')
365 *line = ' ';
367 ch = *line;
368 if (ch == '\t' || ch == '\0')
369 ch = ' '; /* the space got eaten */
371 switch (ch) {
372 case '-':
373 leftold--;
374 break;
375 case ' ':
376 leftold--;
377 leftnew--;
378 break;
379 case '+':
380 leftnew--;
381 break;
382 default:
383 err = got_error(GOT_ERR_PATCH_MALFORMED);
384 goto done;
387 if (leftold < 0 || leftnew < 0) {
388 err = got_error(GOT_ERR_PATCH_MALFORMED);
389 goto done;
392 err = send_line(line);
393 if (err)
394 goto done;
396 if ((ch == '-' && leftold == 0) ||
397 (ch == '+' && leftnew == 0)) {
398 err = peek_special_line(fp, ch == '+');
399 if (err)
400 goto done;
404 done:
405 free(line);
406 return err;
409 static const struct got_error *
410 read_patch(struct imsgbuf *ibuf, int fd)
412 const struct got_error *err = NULL;
413 FILE *fp;
414 int ok, patch_found = 0;
416 if ((fp = fdopen(fd, "r")) == NULL) {
417 err = got_error_from_errno("fdopen");
418 close(fd);
419 return err;
422 while (!feof(fp)) {
423 err = find_patch(fp);
424 if (err)
425 goto done;
427 patch_found = 1;
428 for (;;) {
429 err = parse_hunk(fp, &ok);
430 if (err)
431 goto done;
432 if (!ok) {
433 err = send_patch_done();
434 if (err)
435 goto done;
436 break;
441 done:
442 fclose(fp);
444 /* ignore trailing gibberish */
445 if (err != NULL && err->code == GOT_ERR_NO_PATCH && patch_found)
446 err = NULL;
448 return err;
451 int
452 main(int argc, char **argv)
454 const struct got_error *err = NULL;
455 struct imsg imsg;
456 #if 0
457 static int attached;
458 while (!attached)
459 sleep(1);
460 #endif
462 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
463 #ifndef PROFILE
464 /* revoke access to most system calls */
465 if (pledge("stdio recvfd", NULL) == -1) {
466 err = got_error_from_errno("pledge");
467 got_privsep_send_error(&ibuf, err);
468 return 1;
471 /* revoke fs access */
472 if (landlock_no_fs() == -1) {
473 err = got_error_from_errno("landlock_no_fs");
474 got_privsep_send_error(&ibuf, err);
475 return 1;
477 #endif
479 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
480 if (err)
481 goto done;
482 if (imsg.hdr.type != GOT_IMSG_PATCH_FILE || imsg.fd == -1) {
483 err = got_error(GOT_ERR_PRIVSEP_MSG);
484 goto done;
487 err = read_patch(&ibuf, imsg.fd);
488 if (err)
489 goto done;
490 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_EOF, 0, 0, -1,
491 NULL, 0) == -1) {
492 err = got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
493 goto done;
495 err = got_privsep_flush_imsg(&ibuf);
496 done:
497 imsg_free(&imsg);
498 if (err != NULL) {
499 got_privsep_send_error(&ibuf, err);
500 err = NULL;
502 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
503 err = got_error_from_errno("close");
504 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
505 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
506 return err ? 1 : 0;