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)
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));
70 if (newname != NULL)
71 strlcpy(p.new, newname, sizeof(p.new));
73 if (imsg_compose(&ibuf, GOT_IMSG_PATCH, 0, 0, -1,
74 &p, sizeof(p)) == -1)
75 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH");
76 return NULL;
77 }
79 static const struct got_error *
80 send_patch_done(void)
81 {
82 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_DONE, 0, 0, -1,
83 NULL, 0) == -1)
84 return got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
85 if (imsg_flush(&ibuf) == -1)
86 return got_error_from_errno("imsg_flush");
87 return NULL;
88 }
90 /* based on fetchname from usr.bin/patch/util.c */
91 static const struct got_error *
92 filename(const char *at, char **name, int strip)
93 {
94 char *fullname, *t;
95 int l, tab;
97 *name = NULL;
98 if (*at == '\0')
99 return NULL;
101 while (isspace((unsigned char)*at))
102 at++;
104 /* files can be created or removed by diffing against /dev/null */
105 if (!strncmp(at, _PATH_DEVNULL, sizeof(_PATH_DEVNULL)-1))
106 return NULL;
108 t = strdup(at);
109 if (t == NULL)
110 return got_error_from_errno("strdup");
111 *name = fullname = t;
112 tab = strchr(t, '\t') != NULL;
114 /* strip off path components and NUL-terminate */
115 for (l = strip;
116 *t != '\0' && ((tab && *t != '\t') || !isspace((unsigned char)*t));
117 ++t) {
118 if (t[0] == '/' && t[1] != '/' && t[1] != '\0')
119 if (--l >= 0)
120 *name = t+1;
122 *t = '\0';
124 *name = strdup(*name);
125 free(fullname);
126 if (*name == NULL)
127 return got_error_from_errno("strdup");
128 return NULL;
131 static const struct got_error *
132 find_patch(FILE *fp)
134 const struct got_error *err = NULL;
135 char *old = NULL, *new = NULL;
136 char *line = NULL;
137 size_t linesize = 0;
138 ssize_t linelen;
139 int create, git = 0;
141 while ((linelen = getline(&line, &linesize, fp)) != -1) {
142 /*
143 * Ignore the Index name like GNU and larry' patch,
144 * we don't have to follow POSIX.
145 */
147 if (git && !strncmp(line, "--- a/", 6)) {
148 free(old);
149 err = filename(line+6, &old, 0);
150 } else if (!strncmp(line, "--- ", 4)) {
151 free(old);
152 err = filename(line+4, &old, 0);
153 } else if (git && !strncmp(line, "+++ b/", 6)) {
154 free(new);
155 err = filename(line+6, &new, 0);
156 } else if (!strncmp(line, "+++ ", 4)) {
157 free(new);
158 err = filename(line+4, &new, 0);
159 } else if (!strncmp(line, "diff --git a/", 13))
160 git = 1;
162 if (err)
163 break;
165 if (!strncmp(line, "@@ -", 4)) {
166 create = !strncmp(line+4, "0,0", 3);
167 if ((old == NULL && new == NULL) ||
168 (!create && old == NULL))
169 err = got_error(GOT_ERR_PATCH_MALFORMED);
170 else
171 err = send_patch(old, new);
173 if (err)
174 break;
176 /* rewind to previous line */
177 if (fseek(fp, linelen * -1, SEEK_CUR) == -1)
178 err = got_error_from_errno("fseek");
179 break;
183 free(old);
184 free(new);
185 free(line);
186 if (ferror(fp) && err == NULL)
187 err = got_error_from_errno("getline");
188 if (feof(fp) && err == NULL)
189 err = got_error(GOT_ERR_NO_PATCH);
190 return err;
193 static const struct got_error *
194 strtolnum(char **str, long *n)
196 char *p, c;
197 const char *errstr;
199 for (p = *str; isdigit((unsigned char)*p); ++p)
200 /* nop */;
202 c = *p;
203 *p = '\0';
205 *n = strtonum(*str, 0, LONG_MAX, &errstr);
206 if (errstr != NULL)
207 return got_error(GOT_ERR_PATCH_MALFORMED);
209 *p = c;
210 *str = p;
211 return NULL;
214 static const struct got_error *
215 parse_hdr(char *s, int *ok, struct got_imsg_patch_hunk *hdr)
217 static const struct got_error *err = NULL;
219 *ok = 1;
220 if (strncmp(s, "@@ -", 4)) {
221 *ok = 0;
222 return NULL;
225 s += 4;
226 if (!*s)
227 return NULL;
228 err = strtolnum(&s, &hdr->oldfrom);
229 if (err)
230 return err;
231 if (*s == ',') {
232 s++;
233 err = strtolnum(&s, &hdr->oldlines);
234 if (err)
235 return err;
236 } else
237 hdr->oldlines = 1;
239 if (*s == ' ')
240 s++;
242 if (*s != '+' || !*++s)
243 return got_error(GOT_ERR_PATCH_MALFORMED);
244 err = strtolnum(&s, &hdr->newfrom);
245 if (err)
246 return err;
247 if (*s == ',') {
248 s++;
249 err = strtolnum(&s, &hdr->newlines);
250 if (err)
251 return err;
252 } else
253 hdr->newlines = 1;
255 if (*s == ' ')
256 s++;
258 if (*s != '@')
259 return got_error(GOT_ERR_PATCH_MALFORMED);
261 if (hdr->oldfrom >= LONG_MAX - hdr->oldlines ||
262 hdr->newfrom >= LONG_MAX - hdr->newlines ||
263 /* not so sure about this one */
264 hdr->oldlines >= LONG_MAX - hdr->newlines - 1)
265 return got_error(GOT_ERR_PATCH_MALFORMED);
267 if (hdr->oldlines == 0) {
268 /* larry says to "do append rather than insert"; I don't
269 * quite get it, but i trust him.
270 */
271 hdr->oldfrom++;
274 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_HUNK, 0, 0, -1,
275 hdr, sizeof(*hdr)) == -1)
276 return got_error_from_errno(
277 "imsg_compose GOT_IMSG_PATCH_HUNK");
278 return NULL;
281 static const struct got_error *
282 send_line(const char *line)
284 static const struct got_error *err = NULL;
285 char *p = NULL;
287 if (*line != '+' && *line != '-' && *line != ' ') {
288 if (asprintf(&p, " %s", line) == -1)
289 return got_error_from_errno("asprintf");
290 line = p;
293 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_LINE, 0, 0, -1,
294 line, strlen(line)+1) == -1)
295 err = got_error_from_errno(
296 "imsg_compose GOT_IMSG_PATCH_LINE");
298 free(p);
299 return err;
302 static const struct got_error *
303 parse_hunk(FILE *fp, int *ok)
305 static const struct got_error *err = NULL;
306 struct got_imsg_patch_hunk hdr;
307 char *line = NULL, ch;
308 size_t linesize = 0;
309 ssize_t linelen;
310 long leftold, leftnew;
312 linelen = getline(&line, &linesize, fp);
313 if (linelen == -1) {
314 *ok = 0;
315 goto done;
318 err = parse_hdr(line, ok, &hdr);
319 if (err)
320 goto done;
321 if (!*ok) {
322 if (fseek(fp, linelen * -1, SEEK_CUR) == -1)
323 err = got_error_from_errno("fseek");
324 goto done;
327 leftold = hdr.oldlines;
328 leftnew = hdr.newlines;
330 while (leftold > 0 || leftnew > 0) {
331 linelen = getline(&line, &linesize, fp);
332 if (linelen == -1) {
333 if (ferror(fp)) {
334 err = got_error_from_errno("getline");
335 goto done;
338 /* trailing newlines may be chopped */
339 if (leftold < 3 && leftnew < 3) {
340 *ok = 0;
341 break;
344 err = got_error(GOT_ERR_PATCH_TRUNCATED);
345 goto done;
348 /* usr.bin/patch allows '=' as context char */
349 if (*line == '=')
350 *line = ' ';
352 ch = *line;
353 if (ch == '\t' || ch == '\n')
354 ch = ' '; /* the space got eaten */
356 switch (ch) {
357 case '-':
358 leftold--;
359 break;
360 case ' ':
361 leftold--;
362 leftnew--;
363 break;
364 case '+':
365 leftnew--;
366 break;
367 default:
368 err = got_error(GOT_ERR_PATCH_MALFORMED);
369 goto done;
372 if (leftold < 0 || leftnew < 0) {
373 err = got_error(GOT_ERR_PATCH_MALFORMED);
374 goto done;
377 err = send_line(line);
378 if (err)
379 goto done;
382 done:
383 free(line);
384 return err;
387 static const struct got_error *
388 read_patch(struct imsgbuf *ibuf, int fd)
390 const struct got_error *err = NULL;
391 FILE *fp;
392 int ok, patch_found = 0;
394 if ((fp = fdopen(fd, "r")) == NULL) {
395 err = got_error_from_errno("fdopen");
396 close(fd);
397 return err;
400 while (!feof(fp)) {
401 err = find_patch(fp);
402 if (err)
403 goto done;
405 patch_found = 1;
406 for (;;) {
407 err = parse_hunk(fp, &ok);
408 if (err)
409 goto done;
410 if (!ok) {
411 err = send_patch_done();
412 if (err)
413 goto done;
414 break;
419 done:
420 fclose(fp);
422 /* ignore trailing gibberish */
423 if (err != NULL && err->code == GOT_ERR_NO_PATCH && patch_found)
424 err = NULL;
426 return err;
429 int
430 main(int argc, char **argv)
432 const struct got_error *err = NULL;
433 struct imsg imsg;
434 #if 0
435 static int attached;
436 while (!attached)
437 sleep(1);
438 #endif
440 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
441 #ifndef PROFILE
442 /* revoke access to most system calls */
443 if (pledge("stdio recvfd", NULL) == -1) {
444 err = got_error_from_errno("pledge");
445 got_privsep_send_error(&ibuf, err);
446 return 1;
449 /* revoke fs access */
450 if (landlock_no_fs() == -1) {
451 err = got_error_from_errno("landlock_no_fs");
452 got_privsep_send_error(&ibuf, err);
453 return 1;
455 #endif
457 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
458 if (err)
459 goto done;
460 if (imsg.hdr.type != GOT_IMSG_PATCH_FILE || imsg.fd == -1) {
461 err = got_error(GOT_ERR_PRIVSEP_MSG);
462 goto done;
465 err = read_patch(&ibuf, imsg.fd);
466 if (err)
467 goto done;
468 if (imsg_compose(&ibuf, GOT_IMSG_PATCH_EOF, 0, 0, -1,
469 NULL, 0) == -1) {
470 err = got_error_from_errno("imsg_compose GOT_IMSG_PATCH_EOF");
471 goto done;
473 err = got_privsep_flush_imsg(&ibuf);
474 done:
475 imsg_free(&imsg);
476 if (err != NULL) {
477 got_privsep_send_error(&ibuf, err);
478 err = NULL;
480 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
481 err = got_error_from_errno("close");
482 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
483 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
484 return err ? 1 : 0;