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