Blame


1 50b0790e 2020-09-11 stsp /*
2 50b0790e 2020-09-11 stsp * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
3 50b0790e 2020-09-11 stsp *
4 50b0790e 2020-09-11 stsp * Permission to use, copy, modify, and distribute this software for any
5 50b0790e 2020-09-11 stsp * purpose with or without fee is hereby granted, provided that the above
6 50b0790e 2020-09-11 stsp * copyright notice and this permission notice appear in all copies.
7 50b0790e 2020-09-11 stsp *
8 50b0790e 2020-09-11 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 50b0790e 2020-09-11 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 50b0790e 2020-09-11 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 50b0790e 2020-09-11 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 50b0790e 2020-09-11 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 50b0790e 2020-09-11 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 50b0790e 2020-09-11 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 50b0790e 2020-09-11 stsp */
16 50b0790e 2020-09-11 stsp
17 50b0790e 2020-09-11 stsp #include <sys/types.h>
18 50b0790e 2020-09-11 stsp #include <sys/queue.h>
19 50b0790e 2020-09-11 stsp #include <sys/uio.h>
20 50b0790e 2020-09-11 stsp #include <sys/socket.h>
21 50b0790e 2020-09-11 stsp
22 50b0790e 2020-09-11 stsp #include <unistd.h>
23 50b0790e 2020-09-11 stsp #include <fcntl.h>
24 50b0790e 2020-09-11 stsp #include <errno.h>
25 50b0790e 2020-09-11 stsp #include <stdlib.h>
26 50b0790e 2020-09-11 stsp #include <stdio.h>
27 50b0790e 2020-09-11 stsp #include <stdint.h>
28 50b0790e 2020-09-11 stsp #include <imsg.h>
29 50b0790e 2020-09-11 stsp #include <sha1.h>
30 50b0790e 2020-09-11 stsp #include <limits.h>
31 50b0790e 2020-09-11 stsp
32 50b0790e 2020-09-11 stsp #include "got_error.h"
33 50b0790e 2020-09-11 stsp #include "got_object.h"
34 50b0790e 2020-09-11 stsp #include "got_repository.h"
35 50b0790e 2020-09-11 stsp
36 50b0790e 2020-09-11 stsp #include "got_lib_delta.h"
37 50b0790e 2020-09-11 stsp #include "got_lib_object.h"
38 50b0790e 2020-09-11 stsp #include "got_lib_privsep.h"
39 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
40 50b0790e 2020-09-11 stsp
41 50b0790e 2020-09-11 stsp const struct got_error *
42 50b0790e 2020-09-11 stsp got_gotconfig_read(struct got_gotconfig **conf, const char *gotconfig_path)
43 50b0790e 2020-09-11 stsp {
44 50b0790e 2020-09-11 stsp const struct got_error *err = NULL, *child_err = NULL;
45 50b0790e 2020-09-11 stsp int fd = -1;
46 50b0790e 2020-09-11 stsp int imsg_fds[2] = { -1, -1 };
47 50b0790e 2020-09-11 stsp pid_t pid;
48 50b0790e 2020-09-11 stsp struct imsgbuf *ibuf;
49 50b0790e 2020-09-11 stsp
50 50b0790e 2020-09-11 stsp *conf = calloc(1, sizeof(**conf));
51 50b0790e 2020-09-11 stsp if (*conf == NULL)
52 50b0790e 2020-09-11 stsp return got_error_from_errno("calloc");
53 50b0790e 2020-09-11 stsp
54 8bd0cdad 2021-12-31 stsp fd = open(gotconfig_path, O_RDONLY | O_CLOEXEC);
55 50b0790e 2020-09-11 stsp if (fd == -1) {
56 50b0790e 2020-09-11 stsp if (errno == ENOENT)
57 50b0790e 2020-09-11 stsp return NULL;
58 50b0790e 2020-09-11 stsp return got_error_from_errno2("open", gotconfig_path);
59 50b0790e 2020-09-11 stsp }
60 50b0790e 2020-09-11 stsp
61 50b0790e 2020-09-11 stsp ibuf = calloc(1, sizeof(*ibuf));
62 50b0790e 2020-09-11 stsp if (ibuf == NULL) {
63 50b0790e 2020-09-11 stsp err = got_error_from_errno("calloc");
64 50b0790e 2020-09-11 stsp goto done;
65 50b0790e 2020-09-11 stsp }
66 50b0790e 2020-09-11 stsp
67 50b0790e 2020-09-11 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
68 50b0790e 2020-09-11 stsp err = got_error_from_errno("socketpair");
69 50b0790e 2020-09-11 stsp goto done;
70 50b0790e 2020-09-11 stsp }
71 50b0790e 2020-09-11 stsp
72 50b0790e 2020-09-11 stsp pid = fork();
73 50b0790e 2020-09-11 stsp if (pid == -1) {
74 50b0790e 2020-09-11 stsp err = got_error_from_errno("fork");
75 50b0790e 2020-09-11 stsp goto done;
76 50b0790e 2020-09-11 stsp } else if (pid == 0) {
77 50b0790e 2020-09-11 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GOTCONFIG,
78 50b0790e 2020-09-11 stsp gotconfig_path);
79 50b0790e 2020-09-11 stsp /* not reached */
80 50b0790e 2020-09-11 stsp }
81 50b0790e 2020-09-11 stsp
82 50b0790e 2020-09-11 stsp if (close(imsg_fds[1]) == -1) {
83 50b0790e 2020-09-11 stsp err = got_error_from_errno("close");
84 50b0790e 2020-09-11 stsp goto done;
85 50b0790e 2020-09-11 stsp }
86 50b0790e 2020-09-11 stsp imsg_fds[1] = -1;
87 50b0790e 2020-09-11 stsp imsg_init(ibuf, imsg_fds[0]);
88 50b0790e 2020-09-11 stsp
89 50b0790e 2020-09-11 stsp err = got_privsep_send_gotconfig_parse_req(ibuf, fd);
90 50b0790e 2020-09-11 stsp if (err)
91 50b0790e 2020-09-11 stsp goto done;
92 50b0790e 2020-09-11 stsp fd = -1;
93 50b0790e 2020-09-11 stsp
94 50b0790e 2020-09-11 stsp err = got_privsep_send_gotconfig_author_req(ibuf);
95 50b0790e 2020-09-11 stsp if (err)
96 50b0790e 2020-09-11 stsp goto done;
97 50b0790e 2020-09-11 stsp
98 50b0790e 2020-09-11 stsp err = got_privsep_recv_gotconfig_str(&(*conf)->author, ibuf);
99 50b0790e 2020-09-11 stsp if (err)
100 50b0790e 2020-09-11 stsp goto done;
101 50b0790e 2020-09-11 stsp
102 50b0790e 2020-09-11 stsp err = got_privsep_send_gotconfig_remotes_req(ibuf);
103 50b0790e 2020-09-11 stsp if (err)
104 50b0790e 2020-09-11 stsp goto done;
105 50b0790e 2020-09-11 stsp
106 50b0790e 2020-09-11 stsp err = got_privsep_recv_gotconfig_remotes(&(*conf)->remotes,
107 50b0790e 2020-09-11 stsp &(*conf)->nremotes, ibuf);
108 50b0790e 2020-09-11 stsp if (err)
109 50b0790e 2020-09-11 stsp goto done;
110 50b0790e 2020-09-11 stsp
111 50b0790e 2020-09-11 stsp err = got_privsep_send_stop(imsg_fds[0]);
112 50b0790e 2020-09-11 stsp child_err = got_privsep_wait_for_child(pid);
113 50b0790e 2020-09-11 stsp if (child_err && err == NULL)
114 50b0790e 2020-09-11 stsp err = child_err;
115 50b0790e 2020-09-11 stsp done:
116 50b0790e 2020-09-11 stsp if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
117 50b0790e 2020-09-11 stsp err = got_error_from_errno("close");
118 50b0790e 2020-09-11 stsp if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
119 50b0790e 2020-09-11 stsp err = got_error_from_errno("close");
120 50b0790e 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
121 50b0790e 2020-09-11 stsp err = got_error_from_errno2("close", gotconfig_path);
122 50b0790e 2020-09-11 stsp if (err) {
123 50b0790e 2020-09-11 stsp got_gotconfig_free(*conf);
124 50b0790e 2020-09-11 stsp *conf = NULL;
125 50b0790e 2020-09-11 stsp }
126 50b0790e 2020-09-11 stsp free(ibuf);
127 50b0790e 2020-09-11 stsp return err;
128 50b0790e 2020-09-11 stsp }
129 50b0790e 2020-09-11 stsp
130 50b0790e 2020-09-11 stsp void
131 50b0790e 2020-09-11 stsp got_gotconfig_free(struct got_gotconfig *conf)
132 50b0790e 2020-09-11 stsp {
133 50b0790e 2020-09-11 stsp int i;
134 50b0790e 2020-09-11 stsp
135 a9705505 2020-09-18 stsp if (conf == NULL)
136 a9705505 2020-09-18 stsp return;
137 a9705505 2020-09-18 stsp
138 50b0790e 2020-09-11 stsp free(conf->author);
139 50b0790e 2020-09-11 stsp
140 b8adfa55 2020-09-25 stsp for (i = 0; i < conf->nremotes; i++)
141 b8adfa55 2020-09-25 stsp got_repo_free_remote_repo_data(&conf->remotes[i]);
142 50b0790e 2020-09-11 stsp free(conf->remotes);
143 50b0790e 2020-09-11 stsp free(conf);
144 50b0790e 2020-09-11 stsp }
145 50b0790e 2020-09-11 stsp
146 50b0790e 2020-09-11 stsp const char *
147 50b0790e 2020-09-11 stsp got_gotconfig_get_author(const struct got_gotconfig *conf)
148 50b0790e 2020-09-11 stsp {
149 50b0790e 2020-09-11 stsp return conf->author;
150 50b0790e 2020-09-11 stsp }
151 50b0790e 2020-09-11 stsp
152 50b0790e 2020-09-11 stsp void
153 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(int *nremotes, const struct got_remote_repo **remotes,
154 50b0790e 2020-09-11 stsp const struct got_gotconfig *conf)
155 50b0790e 2020-09-11 stsp {
156 50b0790e 2020-09-11 stsp *nremotes = conf->nremotes;
157 50b0790e 2020-09-11 stsp *remotes = conf->remotes;
158 50b0790e 2020-09-11 stsp }