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 69c6accf 2023-02-04 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 13b2bc37 2022-10-23 stsp
34 13b2bc37 2022-10-23 stsp #include "got_lib_poll.h"
35 13b2bc37 2022-10-23 stsp
36 13b2bc37 2022-10-23 stsp #include "gotd.h"
37 13b2bc37 2022-10-23 stsp
38 13b2bc37 2022-10-23 stsp const struct got_error *
39 13b2bc37 2022-10-23 stsp gotd_imsg_recv_error(uint32_t *client_id, struct imsg *imsg)
40 13b2bc37 2022-10-23 stsp {
41 13b2bc37 2022-10-23 stsp struct gotd_imsg_error ierr;
42 13b2bc37 2022-10-23 stsp size_t datalen;
43 13b2bc37 2022-10-23 stsp
44 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
45 13b2bc37 2022-10-23 stsp if (datalen != sizeof(ierr))
46 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
47 13b2bc37 2022-10-23 stsp memcpy(&ierr, imsg->data, sizeof(ierr));
48 13b2bc37 2022-10-23 stsp
49 13b2bc37 2022-10-23 stsp if (client_id)
50 13b2bc37 2022-10-23 stsp *client_id = ierr.client_id;
51 13b2bc37 2022-10-23 stsp
52 13b2bc37 2022-10-23 stsp if (ierr.code == GOT_ERR_ERRNO)
53 13b2bc37 2022-10-23 stsp errno = ierr.errno_code;
54 13b2bc37 2022-10-23 stsp
55 13b2bc37 2022-10-23 stsp return got_error_msg(ierr.code, ierr.msg);
56 13b2bc37 2022-10-23 stsp }
57 13b2bc37 2022-10-23 stsp
58 13b2bc37 2022-10-23 stsp const struct got_error *
59 13b2bc37 2022-10-23 stsp gotd_imsg_flush(struct imsgbuf *ibuf)
60 13b2bc37 2022-10-23 stsp {
61 522b5488 2022-12-03 stsp const struct got_error *err = NULL;
62 13b2bc37 2022-10-23 stsp
63 522b5488 2022-12-03 stsp while (ibuf->w.queued > 0) {
64 522b5488 2022-12-03 stsp err = got_poll_fd(ibuf->fd, POLLOUT, INFTIM);
65 522b5488 2022-12-03 stsp if (err)
66 522b5488 2022-12-03 stsp break;
67 13b2bc37 2022-10-23 stsp
68 522b5488 2022-12-03 stsp if (imsg_flush(ibuf) == -1) {
69 522b5488 2022-12-03 stsp if (errno != EAGAIN) {
70 522b5488 2022-12-03 stsp imsg_clear(ibuf);
71 522b5488 2022-12-03 stsp err = got_error_from_errno("imsg_flush");
72 522b5488 2022-12-03 stsp break;
73 522b5488 2022-12-03 stsp }
74 522b5488 2022-12-03 stsp }
75 da76b651 2023-02-03 mark }
76 13b2bc37 2022-10-23 stsp
77 522b5488 2022-12-03 stsp return err;
78 13b2bc37 2022-10-23 stsp }
79 13b2bc37 2022-10-23 stsp
80 13b2bc37 2022-10-23 stsp const struct got_error *
81 13b2bc37 2022-10-23 stsp gotd_imsg_recv(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
82 13b2bc37 2022-10-23 stsp {
83 13b2bc37 2022-10-23 stsp ssize_t n;
84 13b2bc37 2022-10-23 stsp
85 13b2bc37 2022-10-23 stsp n = imsg_get(ibuf, imsg);
86 13b2bc37 2022-10-23 stsp if (n == -1)
87 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_get");
88 13b2bc37 2022-10-23 stsp
89 13b2bc37 2022-10-23 stsp if (n == 0) {
90 13b2bc37 2022-10-23 stsp n = imsg_read(ibuf);
91 13b2bc37 2022-10-23 stsp if (n == -1) {
92 13b2bc37 2022-10-23 stsp if (errno == EAGAIN)
93 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_READ);
94 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_read");
95 13b2bc37 2022-10-23 stsp }
96 13b2bc37 2022-10-23 stsp if (n == 0)
97 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_EOF);
98 13b2bc37 2022-10-23 stsp n = imsg_get(ibuf, imsg);
99 13b2bc37 2022-10-23 stsp if (n == -1)
100 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_get");
101 13b2bc37 2022-10-23 stsp }
102 13b2bc37 2022-10-23 stsp
103 13b2bc37 2022-10-23 stsp if (imsg->hdr.len < IMSG_HEADER_SIZE + min_datalen)
104 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
105 13b2bc37 2022-10-23 stsp
106 13b2bc37 2022-10-23 stsp return NULL;
107 13b2bc37 2022-10-23 stsp }
108 13b2bc37 2022-10-23 stsp
109 13b2bc37 2022-10-23 stsp const struct got_error *
110 13b2bc37 2022-10-23 stsp gotd_imsg_poll_recv(struct imsg *imsg, struct imsgbuf *ibuf, size_t min_datalen)
111 13b2bc37 2022-10-23 stsp {
112 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
113 13b2bc37 2022-10-23 stsp
114 13b2bc37 2022-10-23 stsp for (;;) {
115 13b2bc37 2022-10-23 stsp err = gotd_imsg_recv(imsg, ibuf, min_datalen);
116 13b2bc37 2022-10-23 stsp if (err == NULL || err->code != GOT_ERR_PRIVSEP_READ)
117 13b2bc37 2022-10-23 stsp return err;
118 13b2bc37 2022-10-23 stsp
119 13b2bc37 2022-10-23 stsp err = got_poll_fd(ibuf->fd, POLLIN, INFTIM);
120 13b2bc37 2022-10-23 stsp if (err)
121 13b2bc37 2022-10-23 stsp break;
122 13b2bc37 2022-10-23 stsp }
123 13b2bc37 2022-10-23 stsp
124 13b2bc37 2022-10-23 stsp return err;
125 13b2bc37 2022-10-23 stsp }
126 13b2bc37 2022-10-23 stsp
127 13b2bc37 2022-10-23 stsp int
128 13b2bc37 2022-10-23 stsp gotd_imsg_send_error(struct imsgbuf *ibuf, uint32_t peerid,
129 13b2bc37 2022-10-23 stsp uint32_t client_id, const struct got_error *err)
130 13b2bc37 2022-10-23 stsp {
131 13b2bc37 2022-10-23 stsp const struct got_error *flush_err;
132 13b2bc37 2022-10-23 stsp struct gotd_imsg_error ierr;
133 13b2bc37 2022-10-23 stsp int ret;
134 13b2bc37 2022-10-23 stsp
135 13b2bc37 2022-10-23 stsp ierr.code = err->code;
136 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_ERRNO)
137 13b2bc37 2022-10-23 stsp ierr.errno_code = errno;
138 13b2bc37 2022-10-23 stsp else
139 13b2bc37 2022-10-23 stsp ierr.errno_code = 0;
140 13b2bc37 2022-10-23 stsp ierr.client_id = client_id;
141 13b2bc37 2022-10-23 stsp strlcpy(ierr.msg, err->msg, sizeof(ierr.msg));
142 13b2bc37 2022-10-23 stsp
143 13b2bc37 2022-10-23 stsp ret = imsg_compose(ibuf, GOTD_IMSG_ERROR, peerid, getpid(), -1,
144 13b2bc37 2022-10-23 stsp &ierr, sizeof(ierr));
145 13b2bc37 2022-10-23 stsp if (ret == -1)
146 13b2bc37 2022-10-23 stsp return -1;
147 13b2bc37 2022-10-23 stsp
148 13b2bc37 2022-10-23 stsp flush_err = gotd_imsg_flush(ibuf);
149 13b2bc37 2022-10-23 stsp if (flush_err)
150 13b2bc37 2022-10-23 stsp return -1;
151 13b2bc37 2022-10-23 stsp
152 13b2bc37 2022-10-23 stsp return 0;
153 13b2bc37 2022-10-23 stsp }
154 13b2bc37 2022-10-23 stsp
155 13b2bc37 2022-10-23 stsp int
156 13b2bc37 2022-10-23 stsp gotd_imsg_send_error_event(struct gotd_imsgev *iev, uint32_t peerid,
157 13b2bc37 2022-10-23 stsp uint32_t client_id, const struct got_error *err)
158 13b2bc37 2022-10-23 stsp {
159 13b2bc37 2022-10-23 stsp struct gotd_imsg_error ierr;
160 13b2bc37 2022-10-23 stsp int ret;
161 13b2bc37 2022-10-23 stsp
162 13b2bc37 2022-10-23 stsp ierr.code = err->code;
163 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_ERRNO)
164 13b2bc37 2022-10-23 stsp ierr.errno_code = errno;
165 13b2bc37 2022-10-23 stsp else
166 13b2bc37 2022-10-23 stsp ierr.errno_code = 0;
167 13b2bc37 2022-10-23 stsp ierr.client_id = client_id;
168 13b2bc37 2022-10-23 stsp strlcpy(ierr.msg, err->msg, sizeof(ierr.msg));
169 13b2bc37 2022-10-23 stsp
170 13b2bc37 2022-10-23 stsp ret = gotd_imsg_compose_event(iev, GOTD_IMSG_ERROR, peerid, -1,
171 13b2bc37 2022-10-23 stsp &ierr, sizeof(ierr));
172 13b2bc37 2022-10-23 stsp if (ret == -1)
173 13b2bc37 2022-10-23 stsp return -1;
174 13b2bc37 2022-10-23 stsp
175 13b2bc37 2022-10-23 stsp return 0;
176 13b2bc37 2022-10-23 stsp }
177 13b2bc37 2022-10-23 stsp
178 13b2bc37 2022-10-23 stsp void
179 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(struct gotd_imsgev *iev)
180 13b2bc37 2022-10-23 stsp {
181 13b2bc37 2022-10-23 stsp iev->events = EV_READ;
182 13b2bc37 2022-10-23 stsp if (iev->ibuf.w.queued)
183 13b2bc37 2022-10-23 stsp iev->events |= EV_WRITE;
184 13b2bc37 2022-10-23 stsp
185 13b2bc37 2022-10-23 stsp event_del(&iev->ev);
186 13b2bc37 2022-10-23 stsp event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
187 13b2bc37 2022-10-23 stsp event_add(&iev->ev, NULL);
188 13b2bc37 2022-10-23 stsp }
189 13b2bc37 2022-10-23 stsp
190 13b2bc37 2022-10-23 stsp int
191 13b2bc37 2022-10-23 stsp gotd_imsg_compose_event(struct gotd_imsgev *iev, uint16_t type, uint32_t peerid,
192 13b2bc37 2022-10-23 stsp int fd, void *data, uint16_t datalen)
193 13b2bc37 2022-10-23 stsp {
194 13b2bc37 2022-10-23 stsp int ret;
195 13b2bc37 2022-10-23 stsp
196 13b2bc37 2022-10-23 stsp ret = imsg_compose(&iev->ibuf, type, peerid, getpid(), fd,
197 13b2bc37 2022-10-23 stsp data, datalen);
198 13b2bc37 2022-10-23 stsp if (ret != -1)
199 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(iev);
200 13b2bc37 2022-10-23 stsp
201 13b2bc37 2022-10-23 stsp return ret;
202 13b2bc37 2022-10-23 stsp }
203 13b2bc37 2022-10-23 stsp
204 13b2bc37 2022-10-23 stsp int
205 13b2bc37 2022-10-23 stsp gotd_imsg_forward(struct gotd_imsgev *iev, struct imsg *imsg, int fd)
206 13b2bc37 2022-10-23 stsp {
207 13b2bc37 2022-10-23 stsp return gotd_imsg_compose_event(iev, imsg->hdr.type, imsg->hdr.peerid,
208 13b2bc37 2022-10-23 stsp fd, imsg->data, imsg->hdr.len - IMSG_HEADER_SIZE);
209 13b2bc37 2022-10-23 stsp }