Blame


1 13b2bc37 2022-10-23 stsp /*
2 13b2bc37 2022-10-23 stsp * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 13b2bc37 2022-10-23 stsp *
4 13b2bc37 2022-10-23 stsp * Permission to use, copy, modify, and distribute this software for any
5 13b2bc37 2022-10-23 stsp * purpose with or without fee is hereby granted, provided that the above
6 13b2bc37 2022-10-23 stsp * copyright notice and this permission notice appear in all copies.
7 13b2bc37 2022-10-23 stsp *
8 13b2bc37 2022-10-23 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 13b2bc37 2022-10-23 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 13b2bc37 2022-10-23 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 13b2bc37 2022-10-23 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 13b2bc37 2022-10-23 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 13b2bc37 2022-10-23 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 13b2bc37 2022-10-23 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 13b2bc37 2022-10-23 stsp */
16 13b2bc37 2022-10-23 stsp
17 13b2bc37 2022-10-23 stsp #include <sys/queue.h>
18 13b2bc37 2022-10-23 stsp #include <sys/types.h>
19 13b2bc37 2022-10-23 stsp #include <sys/uio.h>
20 13b2bc37 2022-10-23 stsp
21 13b2bc37 2022-10-23 stsp #include <errno.h>
22 13b2bc37 2022-10-23 stsp #include <event.h>
23 13b2bc37 2022-10-23 stsp #include <imsg.h>
24 13b2bc37 2022-10-23 stsp #include <limits.h>
25 13b2bc37 2022-10-23 stsp #include <poll.h>
26 13b2bc37 2022-10-23 stsp #include <sha1.h>
27 5822e79e 2023-02-23 op #include <sha2.h>
28 13b2bc37 2022-10-23 stsp #include <stdio.h>
29 13b2bc37 2022-10-23 stsp #include <string.h>
30 13b2bc37 2022-10-23 stsp #include <unistd.h>
31 13b2bc37 2022-10-23 stsp
32 13b2bc37 2022-10-23 stsp #include "got_error.h"
33 9afa3de2 2023-04-04 stsp #include "got_path.h"
34 13b2bc37 2022-10-23 stsp
35 13b2bc37 2022-10-23 stsp #include "got_lib_poll.h"
36 13b2bc37 2022-10-23 stsp
37 13b2bc37 2022-10-23 stsp #include "gotd.h"
38 13b2bc37 2022-10-23 stsp
39 13b2bc37 2022-10-23 stsp const struct got_error *
40 13b2bc37 2022-10-23 stsp gotd_imsg_recv_error(uint32_t *client_id, struct imsg *imsg)
41 13b2bc37 2022-10-23 stsp {
42 13b2bc37 2022-10-23 stsp struct gotd_imsg_error ierr;
43 13b2bc37 2022-10-23 stsp size_t datalen;
44 13b2bc37 2022-10-23 stsp
45 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
46 13b2bc37 2022-10-23 stsp if (datalen != sizeof(ierr))
47 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
48 13b2bc37 2022-10-23 stsp memcpy(&ierr, imsg->data, sizeof(ierr));
49 13b2bc37 2022-10-23 stsp
50 13b2bc37 2022-10-23 stsp if (client_id)
51 13b2bc37 2022-10-23 stsp *client_id = ierr.client_id;
52 13b2bc37 2022-10-23 stsp
53 13b2bc37 2022-10-23 stsp if (ierr.code == GOT_ERR_ERRNO)
54 13b2bc37 2022-10-23 stsp errno = ierr.errno_code;
55 13b2bc37 2022-10-23 stsp
56 13b2bc37 2022-10-23 stsp return got_error_msg(ierr.code, ierr.msg);
57 13b2bc37 2022-10-23 stsp }
58 13b2bc37 2022-10-23 stsp
59 13b2bc37 2022-10-23 stsp const struct got_error *
60 13b2bc37 2022-10-23 stsp gotd_imsg_flush(struct imsgbuf *ibuf)
61 13b2bc37 2022-10-23 stsp {
62 522b5488 2022-12-03 stsp const struct got_error *err = NULL;
63 13b2bc37 2022-10-23 stsp
64 522b5488 2022-12-03 stsp while (ibuf->w.queued > 0) {
65 522b5488 2022-12-03 stsp err = got_poll_fd(ibuf->fd, POLLOUT, INFTIM);
66 522b5488 2022-12-03 stsp if (err)
67 522b5488 2022-12-03 stsp break;
68 13b2bc37 2022-10-23 stsp
69 522b5488 2022-12-03 stsp if (imsg_flush(ibuf) == -1) {
70 522b5488 2022-12-03 stsp if (errno != EAGAIN) {
71 522b5488 2022-12-03 stsp imsg_clear(ibuf);
72 522b5488 2022-12-03 stsp err = got_error_from_errno("imsg_flush");
73 522b5488 2022-12-03 stsp break;
74 522b5488 2022-12-03 stsp }
75 522b5488 2022-12-03 stsp }
76 da76b651 2023-02-03 mark }
77 13b2bc37 2022-10-23 stsp
78 522b5488 2022-12-03 stsp return err;
79 13b2bc37 2022-10-23 stsp }
80 13b2bc37 2022-10-23 stsp
81 13b2bc37 2022-10-23 stsp const struct got_error *
82 13b2bc37 2022-10-23 stsp gotd_imsg_recv(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
83 13b2bc37 2022-10-23 stsp {
84 13b2bc37 2022-10-23 stsp ssize_t n;
85 13b2bc37 2022-10-23 stsp
86 13b2bc37 2022-10-23 stsp n = imsg_get(ibuf, imsg);
87 13b2bc37 2022-10-23 stsp if (n == -1)
88 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_get");
89 13b2bc37 2022-10-23 stsp
90 13b2bc37 2022-10-23 stsp if (n == 0) {
91 13b2bc37 2022-10-23 stsp n = imsg_read(ibuf);
92 13b2bc37 2022-10-23 stsp if (n == -1) {
93 13b2bc37 2022-10-23 stsp if (errno == EAGAIN)
94 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_READ);
95 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_read");
96 13b2bc37 2022-10-23 stsp }
97 13b2bc37 2022-10-23 stsp if (n == 0)
98 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_EOF);
99 13b2bc37 2022-10-23 stsp n = imsg_get(ibuf, imsg);
100 13b2bc37 2022-10-23 stsp if (n == -1)
101 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_get");
102 13b2bc37 2022-10-23 stsp }
103 13b2bc37 2022-10-23 stsp
104 13b2bc37 2022-10-23 stsp if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
105 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
106 13b2bc37 2022-10-23 stsp
107 13b2bc37 2022-10-23 stsp return NULL;
108 13b2bc37 2022-10-23 stsp }
109 13b2bc37 2022-10-23 stsp
110 13b2bc37 2022-10-23 stsp const struct got_error *
111 13b2bc37 2022-10-23 stsp gotd_imsg_poll_recv(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
112 13b2bc37 2022-10-23 stsp {
113 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
114 13b2bc37 2022-10-23 stsp
115 13b2bc37 2022-10-23 stsp for (;;) {
116 13b2bc37 2022-10-23 stsp err = gotd_imsg_recv(imsg, ibuf, min_datalen);
117 13b2bc37 2022-10-23 stsp if (err == NULL || err->code != GOT_ERR_PRIVSEP_READ)
118 13b2bc37 2022-10-23 stsp return err;
119 13b2bc37 2022-10-23 stsp
120 13b2bc37 2022-10-23 stsp err = got_poll_fd(ibuf->fd, POLLIN, INFTIM);
121 13b2bc37 2022-10-23 stsp if (err)
122 13b2bc37 2022-10-23 stsp break;
123 13b2bc37 2022-10-23 stsp }
124 13b2bc37 2022-10-23 stsp
125 13b2bc37 2022-10-23 stsp return err;
126 13b2bc37 2022-10-23 stsp }
127 13b2bc37 2022-10-23 stsp
128 13b2bc37 2022-10-23 stsp int
129 13b2bc37 2022-10-23 stsp gotd_imsg_send_error(struct imsgbuf *ibuf, uint32_t peerid,
130 13b2bc37 2022-10-23 stsp uint32_t client_id, const struct got_error *err)
131 13b2bc37 2022-10-23 stsp {
132 13b2bc37 2022-10-23 stsp const struct got_error *flush_err;
133 13b2bc37 2022-10-23 stsp struct gotd_imsg_error ierr;
134 13b2bc37 2022-10-23 stsp int ret;
135 13b2bc37 2022-10-23 stsp
136 13b2bc37 2022-10-23 stsp ierr.code = err->code;
137 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_ERRNO)
138 13b2bc37 2022-10-23 stsp ierr.errno_code = errno;
139 13b2bc37 2022-10-23 stsp else
140 13b2bc37 2022-10-23 stsp ierr.errno_code = 0;
141 13b2bc37 2022-10-23 stsp ierr.client_id = client_id;
142 13b2bc37 2022-10-23 stsp strlcpy(ierr.msg, err->msg, sizeof(ierr.msg));
143 13b2bc37 2022-10-23 stsp
144 13b2bc37 2022-10-23 stsp ret = imsg_compose(ibuf, GOTD_IMSG_ERROR, peerid, getpid(), -1,
145 13b2bc37 2022-10-23 stsp &ierr, sizeof(ierr));
146 13b2bc37 2022-10-23 stsp if (ret == -1)
147 13b2bc37 2022-10-23 stsp return -1;
148 13b2bc37 2022-10-23 stsp
149 13b2bc37 2022-10-23 stsp flush_err = gotd_imsg_flush(ibuf);
150 13b2bc37 2022-10-23 stsp if (flush_err)
151 13b2bc37 2022-10-23 stsp return -1;
152 13b2bc37 2022-10-23 stsp
153 13b2bc37 2022-10-23 stsp return 0;
154 13b2bc37 2022-10-23 stsp }
155 13b2bc37 2022-10-23 stsp
156 13b2bc37 2022-10-23 stsp int
157 13b2bc37 2022-10-23 stsp gotd_imsg_send_error_event(struct gotd_imsgev *iev, uint32_t peerid,
158 13b2bc37 2022-10-23 stsp uint32_t client_id, const struct got_error *err)
159 13b2bc37 2022-10-23 stsp {
160 13b2bc37 2022-10-23 stsp struct gotd_imsg_error ierr;
161 13b2bc37 2022-10-23 stsp int ret;
162 13b2bc37 2022-10-23 stsp
163 13b2bc37 2022-10-23 stsp ierr.code = err->code;
164 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_ERRNO)
165 13b2bc37 2022-10-23 stsp ierr.errno_code = errno;
166 13b2bc37 2022-10-23 stsp else
167 13b2bc37 2022-10-23 stsp ierr.errno_code = 0;
168 13b2bc37 2022-10-23 stsp ierr.client_id = client_id;
169 13b2bc37 2022-10-23 stsp strlcpy(ierr.msg, err->msg, sizeof(ierr.msg));
170 13b2bc37 2022-10-23 stsp
171 13b2bc37 2022-10-23 stsp ret = gotd_imsg_compose_event(iev, GOTD_IMSG_ERROR, peerid, -1,
172 13b2bc37 2022-10-23 stsp &ierr, sizeof(ierr));
173 13b2bc37 2022-10-23 stsp if (ret == -1)
174 13b2bc37 2022-10-23 stsp return -1;
175 13b2bc37 2022-10-23 stsp
176 13b2bc37 2022-10-23 stsp return 0;
177 13b2bc37 2022-10-23 stsp }
178 13b2bc37 2022-10-23 stsp
179 13b2bc37 2022-10-23 stsp void
180 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(struct gotd_imsgev *iev)
181 13b2bc37 2022-10-23 stsp {
182 13b2bc37 2022-10-23 stsp iev->events = EV_READ;
183 13b2bc37 2022-10-23 stsp if (iev->ibuf.w.queued)
184 13b2bc37 2022-10-23 stsp iev->events |= EV_WRITE;
185 13b2bc37 2022-10-23 stsp
186 13b2bc37 2022-10-23 stsp event_del(&iev->ev);
187 13b2bc37 2022-10-23 stsp event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
188 13b2bc37 2022-10-23 stsp event_add(&iev->ev, NULL);
189 13b2bc37 2022-10-23 stsp }
190 13b2bc37 2022-10-23 stsp
191 13b2bc37 2022-10-23 stsp int
192 13b2bc37 2022-10-23 stsp gotd_imsg_compose_event(struct gotd_imsgev *iev, uint16_t type, uint32_t peerid,
193 13b2bc37 2022-10-23 stsp int fd, void *data, uint16_t datalen)
194 13b2bc37 2022-10-23 stsp {
195 13b2bc37 2022-10-23 stsp int ret;
196 13b2bc37 2022-10-23 stsp
197 13b2bc37 2022-10-23 stsp ret = imsg_compose(&iev->ibuf, type, peerid, getpid(), fd,
198 13b2bc37 2022-10-23 stsp data, datalen);
199 13b2bc37 2022-10-23 stsp if (ret != -1)
200 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(iev);
201 13b2bc37 2022-10-23 stsp
202 13b2bc37 2022-10-23 stsp return ret;
203 13b2bc37 2022-10-23 stsp }
204 13b2bc37 2022-10-23 stsp
205 13b2bc37 2022-10-23 stsp int
206 13b2bc37 2022-10-23 stsp gotd_imsg_forward(struct gotd_imsgev *iev, struct imsg *imsg, int fd)
207 13b2bc37 2022-10-23 stsp {
208 13b2bc37 2022-10-23 stsp return gotd_imsg_compose_event(iev, imsg->hdr.type, imsg->hdr.peerid,
209 13b2bc37 2022-10-23 stsp fd, imsg->data, imsg->hdr.len - IMSG_HEADER_SIZE);
210 13b2bc37 2022-10-23 stsp }