Blame


1 a596b957 2022-07-14 tracey /*
2 a596b957 2022-07-14 tracey * Copyright (c) 2016, 2019, 2020-2021 Tracey Emery <tracey@traceyemery.net>
3 a596b957 2022-07-14 tracey * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 a596b957 2022-07-14 tracey * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
5 a596b957 2022-07-14 tracey * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
6 a596b957 2022-07-14 tracey *
7 a596b957 2022-07-14 tracey * Permission to use, copy, modify, and distribute this software for any
8 a596b957 2022-07-14 tracey * purpose with or without fee is hereby granted, provided that the above
9 a596b957 2022-07-14 tracey * copyright notice and this permission notice appear in all copies.
10 a596b957 2022-07-14 tracey *
11 a596b957 2022-07-14 tracey * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 a596b957 2022-07-14 tracey * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 a596b957 2022-07-14 tracey * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 a596b957 2022-07-14 tracey * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 a596b957 2022-07-14 tracey * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 a596b957 2022-07-14 tracey * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 a596b957 2022-07-14 tracey * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 a596b957 2022-07-14 tracey */
19 a596b957 2022-07-14 tracey
20 a596b957 2022-07-14 tracey #include <sys/param.h>
21 a596b957 2022-07-14 tracey #include <sys/ioctl.h>
22 a596b957 2022-07-14 tracey #include <sys/queue.h>
23 a596b957 2022-07-14 tracey #include <sys/wait.h>
24 a596b957 2022-07-14 tracey #include <sys/uio.h>
25 a596b957 2022-07-14 tracey #include <sys/resource.h>
26 a596b957 2022-07-14 tracey #include <sys/socket.h>
27 a596b957 2022-07-14 tracey #include <sys/stat.h>
28 a596b957 2022-07-14 tracey #include <sys/time.h>
29 a596b957 2022-07-14 tracey #include <sys/types.h>
30 a596b957 2022-07-14 tracey #include <sys/mman.h>
31 a596b957 2022-07-14 tracey #include <sys/un.h>
32 a596b957 2022-07-14 tracey
33 a596b957 2022-07-14 tracey #include <net/if.h>
34 a596b957 2022-07-14 tracey #include <netinet/in.h>
35 a596b957 2022-07-14 tracey
36 a596b957 2022-07-14 tracey #include <errno.h>
37 a596b957 2022-07-14 tracey #include <event.h>
38 a596b957 2022-07-14 tracey #include <fcntl.h>
39 a596b957 2022-07-14 tracey #include <ifaddrs.h>
40 a596b957 2022-07-14 tracey #include <imsg.h>
41 a596b957 2022-07-14 tracey #include <limits.h>
42 a596b957 2022-07-14 tracey #include <netdb.h>
43 a596b957 2022-07-14 tracey #include <poll.h>
44 a596b957 2022-07-14 tracey #include <pwd.h>
45 a596b957 2022-07-14 tracey #include <stddef.h>
46 a596b957 2022-07-14 tracey #include <stdio.h>
47 a596b957 2022-07-14 tracey #include <stdlib.h>
48 a596b957 2022-07-14 tracey #include <string.h>
49 a596b957 2022-07-14 tracey #include <unistd.h>
50 a596b957 2022-07-14 tracey #include <util.h>
51 a596b957 2022-07-14 tracey
52 a596b957 2022-07-14 tracey #include "got_error.h"
53 a596b957 2022-07-14 tracey #include "got_opentemp.h"
54 df2d3cd2 2023-03-11 op #include "got_reference.h"
55 b5c757f5 2022-09-01 stsp #include "got_repository.h"
56 6fe3b58a 2023-11-14 stsp #include "got_privsep.h"
57 a596b957 2022-07-14 tracey
58 a596b957 2022-07-14 tracey #include "gotwebd.h"
59 1220d7ea 2024-05-21 op #include "log.h"
60 ed619ca0 2022-12-14 op #include "tmpl.h"
61 a596b957 2022-07-14 tracey
62 a596b957 2022-07-14 tracey #define SOCKS_BACKLOG 5
63 a596b957 2022-07-14 tracey #define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b))
64 a596b957 2022-07-14 tracey
65 a596b957 2022-07-14 tracey volatile int client_cnt;
66 a596b957 2022-07-14 tracey
67 026ac2c4 2022-08-20 stsp static struct timeval timeout = { TIMEOUT_DEFAULT, 0 };
68 a596b957 2022-07-14 tracey
69 026ac2c4 2022-08-20 stsp static void sockets_sighdlr(int, short, void *);
70 26678add 2023-11-16 op static void sockets_shutdown(void);
71 026ac2c4 2022-08-20 stsp static void sockets_launch(void);
72 026ac2c4 2022-08-20 stsp static void sockets_purge(struct gotwebd *);
73 026ac2c4 2022-08-20 stsp static void sockets_accept_paused(int, short, void *);
74 026ac2c4 2022-08-20 stsp static void sockets_rlimit(int);
75 a596b957 2022-07-14 tracey
76 26678add 2023-11-16 op static void sockets_dispatch_main(int, short, void *);
77 26678add 2023-11-16 op static int sockets_unix_socket_listen(struct gotwebd *, struct socket *);
78 0c64c2f8 2023-11-15 op static int sockets_create_socket(struct address *);
79 026ac2c4 2022-08-20 stsp static int sockets_accept_reserve(int, struct sockaddr *, socklen_t *,
80 026ac2c4 2022-08-20 stsp int, volatile int *);
81 a596b957 2022-07-14 tracey
82 36079424 2024-05-16 op static struct socket *sockets_conf_new_socket(struct gotwebd *,
83 36079424 2024-05-16 op int, struct address *);
84 a596b957 2022-07-14 tracey
85 a596b957 2022-07-14 tracey int cgi_inflight = 0;
86 a596b957 2022-07-14 tracey
87 a596b957 2022-07-14 tracey void
88 26678add 2023-11-16 op sockets(struct gotwebd *env, int fd)
89 a596b957 2022-07-14 tracey {
90 736fac90 2024-02-10 op struct event sighup, sigint, sigusr1, sigchld, sigterm;
91 a596b957 2022-07-14 tracey
92 26678add 2023-11-16 op event_init();
93 a596b957 2022-07-14 tracey
94 a596b957 2022-07-14 tracey sockets_rlimit(-1);
95 a596b957 2022-07-14 tracey
96 26678add 2023-11-16 op if (config_init(env) == -1)
97 26678add 2023-11-16 op fatal("failed to initialize configuration");
98 a596b957 2022-07-14 tracey
99 26678add 2023-11-16 op if ((env->iev_parent = malloc(sizeof(*env->iev_parent))) == NULL)
100 26678add 2023-11-16 op fatal("malloc");
101 26678add 2023-11-16 op imsg_init(&env->iev_parent->ibuf, fd);
102 26678add 2023-11-16 op env->iev_parent->handler = sockets_dispatch_main;
103 26678add 2023-11-16 op env->iev_parent->data = env->iev_parent;
104 26678add 2023-11-16 op event_set(&env->iev_parent->ev, fd, EV_READ, sockets_dispatch_main,
105 26678add 2023-11-16 op env->iev_parent);
106 26678add 2023-11-16 op event_add(&env->iev_parent->ev, NULL);
107 8def9425 2023-11-16 op
108 8def9425 2023-11-16 op signal(SIGPIPE, SIG_IGN);
109 26678add 2023-11-16 op
110 56a9d315 2023-11-18 op signal_set(&sighup, SIGHUP, sockets_sighdlr, env);
111 26678add 2023-11-16 op signal_add(&sighup, NULL);
112 736fac90 2024-02-10 op signal_set(&sigint, SIGINT, sockets_sighdlr, env);
113 736fac90 2024-02-10 op signal_add(&sigint, NULL);
114 56a9d315 2023-11-18 op signal_set(&sigusr1, SIGUSR1, sockets_sighdlr, env);
115 26678add 2023-11-16 op signal_add(&sigusr1, NULL);
116 26678add 2023-11-16 op signal_set(&sigchld, SIGCHLD, sockets_sighdlr, env);
117 26678add 2023-11-16 op signal_add(&sigchld, NULL);
118 736fac90 2024-02-10 op signal_set(&sigterm, SIGTERM, sockets_sighdlr, env);
119 736fac90 2024-02-10 op signal_add(&sigterm, NULL);
120 26678add 2023-11-16 op
121 a596b957 2022-07-14 tracey #ifndef PROFILE
122 63c6b10f 2023-11-14 stsp if (pledge("stdio rpath inet recvfd proc exec sendfd unveil",
123 63c6b10f 2023-11-14 stsp NULL) == -1)
124 a596b957 2022-07-14 tracey fatal("pledge");
125 a596b957 2022-07-14 tracey #endif
126 26678add 2023-11-16 op
127 26678add 2023-11-16 op event_dispatch();
128 26678add 2023-11-16 op sockets_shutdown();
129 a596b957 2022-07-14 tracey }
130 a596b957 2022-07-14 tracey
131 a596b957 2022-07-14 tracey void
132 a596b957 2022-07-14 tracey sockets_parse_sockets(struct gotwebd *env)
133 a596b957 2022-07-14 tracey {
134 610dd8c9 2022-08-19 stsp struct address *a;
135 4fcc9f74 2022-08-16 stsp struct socket *new_sock = NULL;
136 610dd8c9 2022-08-19 stsp int sock_id = 1;
137 a596b957 2022-07-14 tracey
138 36079424 2024-05-16 op TAILQ_FOREACH(a, &env->addresses, entry) {
139 36079424 2024-05-16 op new_sock = sockets_conf_new_socket(env, sock_id, a);
140 36079424 2024-05-16 op if (new_sock) {
141 36079424 2024-05-16 op sock_id++;
142 36079424 2024-05-16 op TAILQ_INSERT_TAIL(&env->sockets,
143 36079424 2024-05-16 op new_sock, entry);
144 610dd8c9 2022-08-19 stsp }
145 a596b957 2022-07-14 tracey }
146 a596b957 2022-07-14 tracey }
147 a596b957 2022-07-14 tracey
148 026ac2c4 2022-08-20 stsp static struct socket *
149 36079424 2024-05-16 op sockets_conf_new_socket(struct gotwebd *env, int id, struct address *a)
150 610dd8c9 2022-08-19 stsp {
151 610dd8c9 2022-08-19 stsp struct socket *sock;
152 610dd8c9 2022-08-19 stsp struct address *acp;
153 a596b957 2022-07-14 tracey
154 610dd8c9 2022-08-19 stsp if ((sock = calloc(1, sizeof(*sock))) == NULL)
155 610dd8c9 2022-08-19 stsp fatalx("%s: calloc", __func__);
156 4fcc9f74 2022-08-16 stsp
157 610dd8c9 2022-08-19 stsp sock->conf.id = id;
158 610dd8c9 2022-08-19 stsp sock->fd = -1;
159 610dd8c9 2022-08-19 stsp sock->conf.af_type = a->ss.ss_family;
160 610dd8c9 2022-08-19 stsp
161 36079424 2024-05-16 op if (a->ss.ss_family == AF_UNIX) {
162 36079424 2024-05-16 op struct sockaddr_un *sun;
163 610dd8c9 2022-08-19 stsp
164 36079424 2024-05-16 op sun = (struct sockaddr_un *)&a->ss;
165 36079424 2024-05-16 op if (strlcpy(sock->conf.unix_socket_name, sun->sun_path,
166 36079424 2024-05-16 op sizeof(sock->conf.unix_socket_name)) >=
167 36079424 2024-05-16 op sizeof(sock->conf.unix_socket_name))
168 36079424 2024-05-16 op fatalx("unix socket path too long: %s", sun->sun_path);
169 4fcc9f74 2022-08-16 stsp }
170 4fcc9f74 2022-08-16 stsp
171 36079424 2024-05-16 op sock->conf.fcgi_socket_port = a->port;
172 610dd8c9 2022-08-19 stsp
173 610dd8c9 2022-08-19 stsp acp = &sock->conf.addr;
174 610dd8c9 2022-08-19 stsp
175 610dd8c9 2022-08-19 stsp memcpy(&acp->ss, &a->ss, sizeof(acp->ss));
176 cdfd248a 2023-11-15 op acp->slen = a->slen;
177 0c64c2f8 2023-11-15 op acp->ai_family = a->ai_family;
178 0c64c2f8 2023-11-15 op acp->ai_socktype = a->ai_socktype;
179 0c64c2f8 2023-11-15 op acp->ai_protocol = a->ai_protocol;
180 610dd8c9 2022-08-19 stsp acp->port = a->port;
181 4448825a 2023-06-16 op if (*a->ifname != '\0') {
182 610dd8c9 2022-08-19 stsp if (strlcpy(acp->ifname, a->ifname,
183 610dd8c9 2022-08-19 stsp sizeof(acp->ifname)) >= sizeof(acp->ifname)) {
184 610dd8c9 2022-08-19 stsp fatalx("%s: interface name truncated",
185 610dd8c9 2022-08-19 stsp __func__);
186 610dd8c9 2022-08-19 stsp }
187 610dd8c9 2022-08-19 stsp }
188 610dd8c9 2022-08-19 stsp
189 a596b957 2022-07-14 tracey return (sock);
190 a596b957 2022-07-14 tracey }
191 a596b957 2022-07-14 tracey
192 026ac2c4 2022-08-20 stsp static void
193 a596b957 2022-07-14 tracey sockets_launch(void)
194 a596b957 2022-07-14 tracey {
195 a596b957 2022-07-14 tracey struct socket *sock;
196 6fe3b58a 2023-11-14 stsp struct server *srv;
197 6fe3b58a 2023-11-14 stsp const struct got_error *error;
198 a596b957 2022-07-14 tracey
199 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(sock, &gotwebd_env->sockets, entry) {
200 a596b957 2022-07-14 tracey log_debug("%s: configuring socket %d (%d)", __func__,
201 a596b957 2022-07-14 tracey sock->conf.id, sock->fd);
202 a596b957 2022-07-14 tracey
203 a596b957 2022-07-14 tracey event_set(&sock->ev, sock->fd, EV_READ | EV_PERSIST,
204 a596b957 2022-07-14 tracey sockets_socket_accept, sock);
205 a596b957 2022-07-14 tracey
206 a596b957 2022-07-14 tracey if (event_add(&sock->ev, NULL))
207 a596b957 2022-07-14 tracey fatalx("event add sock");
208 a596b957 2022-07-14 tracey
209 a596b957 2022-07-14 tracey evtimer_set(&sock->pause, sockets_accept_paused, sock);
210 a596b957 2022-07-14 tracey
211 a596b957 2022-07-14 tracey log_debug("%s: running socket listener %d", __func__,
212 a596b957 2022-07-14 tracey sock->conf.id);
213 a596b957 2022-07-14 tracey }
214 6fe3b58a 2023-11-14 stsp
215 6fe3b58a 2023-11-14 stsp TAILQ_FOREACH(srv, &gotwebd_env->servers, entry) {
216 6fe3b58a 2023-11-14 stsp if (unveil(srv->repos_path, "r") == -1)
217 6fe3b58a 2023-11-14 stsp fatal("unveil %s", srv->repos_path);
218 6fe3b58a 2023-11-14 stsp }
219 6fe3b58a 2023-11-14 stsp
220 6fe3b58a 2023-11-14 stsp error = got_privsep_unveil_exec_helpers();
221 6fe3b58a 2023-11-14 stsp if (error)
222 6fe3b58a 2023-11-14 stsp fatal("%s", error->msg);
223 6fe3b58a 2023-11-14 stsp
224 6fe3b58a 2023-11-14 stsp if (unveil(NULL, NULL) == -1)
225 6fe3b58a 2023-11-14 stsp fatal("unveil");
226 a596b957 2022-07-14 tracey }
227 a596b957 2022-07-14 tracey
228 026ac2c4 2022-08-20 stsp static void
229 a596b957 2022-07-14 tracey sockets_purge(struct gotwebd *env)
230 a596b957 2022-07-14 tracey {
231 a596b957 2022-07-14 tracey struct socket *sock, *tsock;
232 a596b957 2022-07-14 tracey
233 a596b957 2022-07-14 tracey /* shutdown and remove sockets */
234 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH_SAFE(sock, &env->sockets, entry, tsock) {
235 a596b957 2022-07-14 tracey if (event_initialized(&sock->ev))
236 a596b957 2022-07-14 tracey event_del(&sock->ev);
237 a596b957 2022-07-14 tracey if (evtimer_initialized(&sock->evt))
238 a596b957 2022-07-14 tracey evtimer_del(&sock->evt);
239 a596b957 2022-07-14 tracey if (evtimer_initialized(&sock->pause))
240 a596b957 2022-07-14 tracey evtimer_del(&sock->pause);
241 a596b957 2022-07-14 tracey if (sock->fd != -1)
242 a596b957 2022-07-14 tracey close(sock->fd);
243 2ad48e9a 2022-08-16 stsp TAILQ_REMOVE(&env->sockets, sock, entry);
244 a596b957 2022-07-14 tracey }
245 a596b957 2022-07-14 tracey }
246 a596b957 2022-07-14 tracey
247 26678add 2023-11-16 op static void
248 26678add 2023-11-16 op sockets_dispatch_main(int fd, short event, void *arg)
249 a596b957 2022-07-14 tracey {
250 26678add 2023-11-16 op struct imsgev *iev = arg;
251 26678add 2023-11-16 op struct imsgbuf *ibuf;
252 26678add 2023-11-16 op struct imsg imsg;
253 26678add 2023-11-16 op struct gotwebd *env = gotwebd_env;
254 26678add 2023-11-16 op ssize_t n;
255 26678add 2023-11-16 op int shut = 0;
256 a596b957 2022-07-14 tracey
257 26678add 2023-11-16 op ibuf = &iev->ibuf;
258 26678add 2023-11-16 op
259 26678add 2023-11-16 op if (event & EV_READ) {
260 26678add 2023-11-16 op if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
261 26678add 2023-11-16 op fatal("imsg_read error");
262 26678add 2023-11-16 op if (n == 0) /* Connection closed */
263 26678add 2023-11-16 op shut = 1;
264 a596b957 2022-07-14 tracey }
265 26678add 2023-11-16 op if (event & EV_WRITE) {
266 26678add 2023-11-16 op if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
267 26678add 2023-11-16 op fatal("msgbuf_write");
268 26678add 2023-11-16 op if (n == 0) /* Connection closed */
269 26678add 2023-11-16 op shut = 1;
270 26678add 2023-11-16 op }
271 a596b957 2022-07-14 tracey
272 26678add 2023-11-16 op for (;;) {
273 26678add 2023-11-16 op if ((n = imsg_get(ibuf, &imsg)) == -1)
274 26678add 2023-11-16 op fatal("imsg_get");
275 26678add 2023-11-16 op if (n == 0) /* No more messages. */
276 26678add 2023-11-16 op break;
277 26678add 2023-11-16 op
278 26678add 2023-11-16 op switch (imsg.hdr.type) {
279 26678add 2023-11-16 op case IMSG_CFG_SRV:
280 26678add 2023-11-16 op config_getserver(env, &imsg);
281 26678add 2023-11-16 op break;
282 26678add 2023-11-16 op case IMSG_CFG_SOCK:
283 26678add 2023-11-16 op config_getsock(env, &imsg);
284 26678add 2023-11-16 op break;
285 26678add 2023-11-16 op case IMSG_CFG_FD:
286 26678add 2023-11-16 op config_getfd(env, &imsg);
287 26678add 2023-11-16 op break;
288 26678add 2023-11-16 op case IMSG_CFG_DONE:
289 26678add 2023-11-16 op config_getcfg(env, &imsg);
290 26678add 2023-11-16 op break;
291 26678add 2023-11-16 op case IMSG_CTL_START:
292 26678add 2023-11-16 op sockets_launch();
293 26678add 2023-11-16 op break;
294 26678add 2023-11-16 op default:
295 26678add 2023-11-16 op fatalx("%s: unknown imsg type %d", __func__,
296 26678add 2023-11-16 op imsg.hdr.type);
297 26678add 2023-11-16 op }
298 26678add 2023-11-16 op
299 26678add 2023-11-16 op imsg_free(&imsg);
300 a596b957 2022-07-14 tracey }
301 a596b957 2022-07-14 tracey
302 26678add 2023-11-16 op if (!shut)
303 26678add 2023-11-16 op imsg_event_add(iev);
304 26678add 2023-11-16 op else {
305 26678add 2023-11-16 op /* This pipe is dead. Remove its event handler */
306 26678add 2023-11-16 op event_del(&iev->ev);
307 26678add 2023-11-16 op event_loopexit(NULL);
308 26678add 2023-11-16 op }
309 a596b957 2022-07-14 tracey }
310 a596b957 2022-07-14 tracey
311 026ac2c4 2022-08-20 stsp static void
312 a596b957 2022-07-14 tracey sockets_sighdlr(int sig, short event, void *arg)
313 a596b957 2022-07-14 tracey {
314 a596b957 2022-07-14 tracey switch (sig) {
315 a596b957 2022-07-14 tracey case SIGHUP:
316 a596b957 2022-07-14 tracey log_info("%s: ignoring SIGHUP", __func__);
317 a596b957 2022-07-14 tracey break;
318 a596b957 2022-07-14 tracey case SIGPIPE:
319 a596b957 2022-07-14 tracey log_info("%s: ignoring SIGPIPE", __func__);
320 a596b957 2022-07-14 tracey break;
321 a596b957 2022-07-14 tracey case SIGUSR1:
322 a596b957 2022-07-14 tracey log_info("%s: ignoring SIGUSR1", __func__);
323 a596b957 2022-07-14 tracey break;
324 a596b957 2022-07-14 tracey case SIGCHLD:
325 736fac90 2024-02-10 op break;
326 736fac90 2024-02-10 op case SIGINT:
327 736fac90 2024-02-10 op case SIGTERM:
328 736fac90 2024-02-10 op sockets_shutdown();
329 a596b957 2022-07-14 tracey break;
330 a596b957 2022-07-14 tracey default:
331 a596b957 2022-07-14 tracey log_info("SIGNAL: %d", sig);
332 a596b957 2022-07-14 tracey fatalx("unexpected signal");
333 a596b957 2022-07-14 tracey }
334 a596b957 2022-07-14 tracey }
335 a596b957 2022-07-14 tracey
336 26678add 2023-11-16 op static void
337 a596b957 2022-07-14 tracey sockets_shutdown(void)
338 a596b957 2022-07-14 tracey {
339 a596b957 2022-07-14 tracey struct server *srv, *tsrv;
340 a596b957 2022-07-14 tracey struct socket *sock, *tsock;
341 a596b957 2022-07-14 tracey
342 a596b957 2022-07-14 tracey sockets_purge(gotwebd_env);
343 a596b957 2022-07-14 tracey
344 a596b957 2022-07-14 tracey /* clean sockets */
345 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH_SAFE(sock, &gotwebd_env->sockets, entry, tsock) {
346 2ad48e9a 2022-08-16 stsp TAILQ_REMOVE(&gotwebd_env->sockets, sock, entry);
347 a596b957 2022-07-14 tracey close(sock->fd);
348 a596b957 2022-07-14 tracey free(sock);
349 a596b957 2022-07-14 tracey }
350 a596b957 2022-07-14 tracey
351 a596b957 2022-07-14 tracey /* clean servers */
352 1632f50a 2023-11-17 stsp TAILQ_FOREACH_SAFE(srv, &gotwebd_env->servers, entry, tsrv)
353 a596b957 2022-07-14 tracey free(srv);
354 a596b957 2022-07-14 tracey
355 a596b957 2022-07-14 tracey free(gotwebd_env);
356 736fac90 2024-02-10 op
357 736fac90 2024-02-10 op exit(0);
358 a596b957 2022-07-14 tracey }
359 a596b957 2022-07-14 tracey
360 a596b957 2022-07-14 tracey int
361 a596b957 2022-07-14 tracey sockets_privinit(struct gotwebd *env, struct socket *sock)
362 a596b957 2022-07-14 tracey {
363 4fcc9f74 2022-08-16 stsp if (sock->conf.af_type == AF_UNIX) {
364 a596b957 2022-07-14 tracey log_debug("%s: initializing unix socket %s", __func__,
365 a596b957 2022-07-14 tracey sock->conf.unix_socket_name);
366 26678add 2023-11-16 op sock->fd = sockets_unix_socket_listen(env, sock);
367 a596b957 2022-07-14 tracey if (sock->fd == -1) {
368 a596b957 2022-07-14 tracey log_warnx("%s: create unix socket failed", __func__);
369 a596b957 2022-07-14 tracey return -1;
370 a596b957 2022-07-14 tracey }
371 a596b957 2022-07-14 tracey }
372 a596b957 2022-07-14 tracey
373 4fcc9f74 2022-08-16 stsp if (sock->conf.af_type == AF_INET || sock->conf.af_type == AF_INET6) {
374 36079424 2024-05-16 op log_debug("%s: initializing %s FCGI socket on port %d",
375 4fcc9f74 2022-08-16 stsp __func__, sock->conf.af_type == AF_INET ? "inet" : "inet6",
376 36079424 2024-05-16 op sock->conf.fcgi_socket_port);
377 0c64c2f8 2023-11-15 op sock->fd = sockets_create_socket(&sock->conf.addr);
378 a596b957 2022-07-14 tracey if (sock->fd == -1) {
379 610dd8c9 2022-08-19 stsp log_warnx("%s: create FCGI socket failed", __func__);
380 a596b957 2022-07-14 tracey return -1;
381 a596b957 2022-07-14 tracey }
382 a596b957 2022-07-14 tracey }
383 a596b957 2022-07-14 tracey
384 a596b957 2022-07-14 tracey return 0;
385 a596b957 2022-07-14 tracey }
386 a596b957 2022-07-14 tracey
387 026ac2c4 2022-08-20 stsp static int
388 26678add 2023-11-16 op sockets_unix_socket_listen(struct gotwebd *env, struct socket *sock)
389 a596b957 2022-07-14 tracey {
390 a596b957 2022-07-14 tracey int u_fd = -1;
391 a596b957 2022-07-14 tracey mode_t old_umask, mode;
392 a596b957 2022-07-14 tracey
393 a596b957 2022-07-14 tracey u_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
394 a596b957 2022-07-14 tracey if (u_fd == -1) {
395 a596b957 2022-07-14 tracey log_warn("%s: socket", __func__);
396 a596b957 2022-07-14 tracey return -1;
397 a596b957 2022-07-14 tracey }
398 a596b957 2022-07-14 tracey
399 a596b957 2022-07-14 tracey if (unlink(sock->conf.unix_socket_name) == -1) {
400 a596b957 2022-07-14 tracey if (errno != ENOENT) {
401 a596b957 2022-07-14 tracey log_warn("%s: unlink %s", __func__,
402 a596b957 2022-07-14 tracey sock->conf.unix_socket_name);
403 a596b957 2022-07-14 tracey close(u_fd);
404 a596b957 2022-07-14 tracey return -1;
405 a596b957 2022-07-14 tracey }
406 a596b957 2022-07-14 tracey }
407 a596b957 2022-07-14 tracey
408 a596b957 2022-07-14 tracey old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
409 a596b957 2022-07-14 tracey mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
410 a596b957 2022-07-14 tracey
411 36079424 2024-05-16 op if (bind(u_fd, (struct sockaddr *)&sock->conf.addr.ss,
412 36079424 2024-05-16 op sock->conf.addr.slen) == -1) {
413 a596b957 2022-07-14 tracey log_warn("%s: bind: %s", __func__, sock->conf.unix_socket_name);
414 a596b957 2022-07-14 tracey close(u_fd);
415 a596b957 2022-07-14 tracey (void)umask(old_umask);
416 a596b957 2022-07-14 tracey return -1;
417 a596b957 2022-07-14 tracey }
418 a596b957 2022-07-14 tracey
419 a596b957 2022-07-14 tracey (void)umask(old_umask);
420 a596b957 2022-07-14 tracey
421 a596b957 2022-07-14 tracey if (chmod(sock->conf.unix_socket_name, mode) == -1) {
422 a596b957 2022-07-14 tracey log_warn("%s: chmod", __func__);
423 a596b957 2022-07-14 tracey close(u_fd);
424 a596b957 2022-07-14 tracey (void)unlink(sock->conf.unix_socket_name);
425 a596b957 2022-07-14 tracey return -1;
426 a596b957 2022-07-14 tracey }
427 a596b957 2022-07-14 tracey
428 26678add 2023-11-16 op if (chown(sock->conf.unix_socket_name, env->pw->pw_uid,
429 26678add 2023-11-16 op env->pw->pw_gid) == -1) {
430 a596b957 2022-07-14 tracey log_warn("%s: chown", __func__);
431 a596b957 2022-07-14 tracey close(u_fd);
432 a596b957 2022-07-14 tracey (void)unlink(sock->conf.unix_socket_name);
433 a596b957 2022-07-14 tracey return -1;
434 a596b957 2022-07-14 tracey }
435 a596b957 2022-07-14 tracey
436 a596b957 2022-07-14 tracey if (listen(u_fd, SOCKS_BACKLOG) == -1) {
437 a596b957 2022-07-14 tracey log_warn("%s: listen", __func__);
438 a596b957 2022-07-14 tracey return -1;
439 a596b957 2022-07-14 tracey }
440 a596b957 2022-07-14 tracey
441 a596b957 2022-07-14 tracey return u_fd;
442 a596b957 2022-07-14 tracey }
443 a596b957 2022-07-14 tracey
444 026ac2c4 2022-08-20 stsp static int
445 0c64c2f8 2023-11-15 op sockets_create_socket(struct address *a)
446 a596b957 2022-07-14 tracey {
447 a596b957 2022-07-14 tracey int fd = -1, o_val = 1, flags;
448 a596b957 2022-07-14 tracey
449 0c64c2f8 2023-11-15 op fd = socket(a->ai_family, a->ai_socktype, a->ai_protocol);
450 610dd8c9 2022-08-19 stsp if (fd == -1)
451 610dd8c9 2022-08-19 stsp return -1;
452 a596b957 2022-07-14 tracey
453 610dd8c9 2022-08-19 stsp log_debug("%s: opened socket (%d) for %s", __func__,
454 610dd8c9 2022-08-19 stsp fd, a->ifname);
455 a596b957 2022-07-14 tracey
456 610dd8c9 2022-08-19 stsp if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &o_val,
457 610dd8c9 2022-08-19 stsp sizeof(int)) == -1) {
458 610dd8c9 2022-08-19 stsp log_warn("%s: setsockopt error", __func__);
459 610dd8c9 2022-08-19 stsp close(fd);
460 610dd8c9 2022-08-19 stsp return -1;
461 610dd8c9 2022-08-19 stsp }
462 a596b957 2022-07-14 tracey
463 610dd8c9 2022-08-19 stsp /* non-blocking */
464 610dd8c9 2022-08-19 stsp flags = fcntl(fd, F_GETFL);
465 610dd8c9 2022-08-19 stsp flags |= O_NONBLOCK;
466 610dd8c9 2022-08-19 stsp if (fcntl(fd, F_SETFL, flags) == -1) {
467 610dd8c9 2022-08-19 stsp log_info("%s: could not enable non-blocking I/O", __func__);
468 610dd8c9 2022-08-19 stsp close(fd);
469 610dd8c9 2022-08-19 stsp return -1;
470 610dd8c9 2022-08-19 stsp }
471 a596b957 2022-07-14 tracey
472 cdfd248a 2023-11-15 op if (bind(fd, (struct sockaddr *)&a->ss, a->slen) == -1) {
473 610dd8c9 2022-08-19 stsp close(fd);
474 0c64c2f8 2023-11-15 op log_info("%s: can't bind to port %d", __func__, a->port);
475 610dd8c9 2022-08-19 stsp return -1;
476 a596b957 2022-07-14 tracey }
477 a596b957 2022-07-14 tracey
478 610dd8c9 2022-08-19 stsp if (listen(fd, SOMAXCONN) == -1) {
479 610dd8c9 2022-08-19 stsp log_warn("%s, unable to listen on socket", __func__);
480 610dd8c9 2022-08-19 stsp close(fd);
481 610dd8c9 2022-08-19 stsp return -1;
482 610dd8c9 2022-08-19 stsp }
483 610dd8c9 2022-08-19 stsp
484 a596b957 2022-07-14 tracey return (fd);
485 a596b957 2022-07-14 tracey }
486 a596b957 2022-07-14 tracey
487 026ac2c4 2022-08-20 stsp static int
488 a596b957 2022-07-14 tracey sockets_accept_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
489 a596b957 2022-07-14 tracey int reserve, volatile int *counter)
490 a596b957 2022-07-14 tracey {
491 a596b957 2022-07-14 tracey int ret;
492 a596b957 2022-07-14 tracey
493 a596b957 2022-07-14 tracey if (getdtablecount() + reserve +
494 a596b957 2022-07-14 tracey ((*counter + 1) * FD_NEEDED) >= getdtablesize()) {
495 a596b957 2022-07-14 tracey log_debug("inflight fds exceeded");
496 a596b957 2022-07-14 tracey errno = EMFILE;
497 a596b957 2022-07-14 tracey return -1;
498 a596b957 2022-07-14 tracey }
499 a596b957 2022-07-14 tracey
500 a596b957 2022-07-14 tracey if ((ret = accept4(sockfd, addr, addrlen,
501 a596b957 2022-07-14 tracey SOCK_NONBLOCK | SOCK_CLOEXEC)) > -1) {
502 a596b957 2022-07-14 tracey (*counter)++;
503 a596b957 2022-07-14 tracey log_debug("inflight incremented, now %d", *counter);
504 a596b957 2022-07-14 tracey }
505 a596b957 2022-07-14 tracey
506 a596b957 2022-07-14 tracey return ret;
507 a596b957 2022-07-14 tracey }
508 a596b957 2022-07-14 tracey
509 026ac2c4 2022-08-20 stsp static void
510 a596b957 2022-07-14 tracey sockets_accept_paused(int fd, short events, void *arg)
511 a596b957 2022-07-14 tracey {
512 a596b957 2022-07-14 tracey struct socket *sock = (struct socket *)arg;
513 a596b957 2022-07-14 tracey
514 a596b957 2022-07-14 tracey event_add(&sock->ev, NULL);
515 a596b957 2022-07-14 tracey }
516 a596b957 2022-07-14 tracey
517 a596b957 2022-07-14 tracey void
518 a596b957 2022-07-14 tracey sockets_socket_accept(int fd, short event, void *arg)
519 a596b957 2022-07-14 tracey {
520 a596b957 2022-07-14 tracey struct socket *sock = (struct socket *)arg;
521 a596b957 2022-07-14 tracey struct sockaddr_storage ss;
522 a596b957 2022-07-14 tracey struct timeval backoff;
523 a596b957 2022-07-14 tracey struct request *c = NULL;
524 a596b957 2022-07-14 tracey socklen_t len;
525 a596b957 2022-07-14 tracey int s;
526 a596b957 2022-07-14 tracey
527 a596b957 2022-07-14 tracey backoff.tv_sec = 1;
528 a596b957 2022-07-14 tracey backoff.tv_usec = 0;
529 a596b957 2022-07-14 tracey
530 a596b957 2022-07-14 tracey event_add(&sock->ev, NULL);
531 a596b957 2022-07-14 tracey if (event & EV_TIMEOUT)
532 a596b957 2022-07-14 tracey return;
533 a596b957 2022-07-14 tracey
534 a596b957 2022-07-14 tracey len = sizeof(ss);
535 a596b957 2022-07-14 tracey
536 a596b957 2022-07-14 tracey s = sockets_accept_reserve(fd, (struct sockaddr *)&ss, &len,
537 a596b957 2022-07-14 tracey FD_RESERVE, &cgi_inflight);
538 a596b957 2022-07-14 tracey
539 a596b957 2022-07-14 tracey if (s == -1) {
540 a596b957 2022-07-14 tracey switch (errno) {
541 a596b957 2022-07-14 tracey case EINTR:
542 a596b957 2022-07-14 tracey case EWOULDBLOCK:
543 a596b957 2022-07-14 tracey case ECONNABORTED:
544 a596b957 2022-07-14 tracey return;
545 a596b957 2022-07-14 tracey case EMFILE:
546 a596b957 2022-07-14 tracey case ENFILE:
547 a596b957 2022-07-14 tracey event_del(&sock->ev);
548 a596b957 2022-07-14 tracey evtimer_add(&sock->pause, &backoff);
549 a596b957 2022-07-14 tracey return;
550 a596b957 2022-07-14 tracey default:
551 a596b957 2022-07-14 tracey log_warn("%s: accept", __func__);
552 a596b957 2022-07-14 tracey }
553 a596b957 2022-07-14 tracey }
554 a596b957 2022-07-14 tracey
555 a596b957 2022-07-14 tracey if (client_cnt > GOTWEBD_MAXCLIENTS)
556 a596b957 2022-07-14 tracey goto err;
557 a596b957 2022-07-14 tracey
558 a596b957 2022-07-14 tracey c = calloc(1, sizeof(struct request));
559 a596b957 2022-07-14 tracey if (c == NULL) {
560 ed619ca0 2022-12-14 op log_warn("%s", __func__);
561 ed619ca0 2022-12-14 op close(s);
562 ed619ca0 2022-12-14 op cgi_inflight--;
563 ed619ca0 2022-12-14 op return;
564 ed619ca0 2022-12-14 op }
565 ed619ca0 2022-12-14 op
566 62eab86e 2023-09-13 op c->tp = template(c, &fcgi_write, c->outbuf, sizeof(c->outbuf));
567 ed619ca0 2022-12-14 op if (c->tp == NULL) {
568 a596b957 2022-07-14 tracey log_warn("%s", __func__);
569 a596b957 2022-07-14 tracey close(s);
570 a596b957 2022-07-14 tracey cgi_inflight--;
571 ed619ca0 2022-12-14 op free(c);
572 a596b957 2022-07-14 tracey return;
573 a596b957 2022-07-14 tracey }
574 a596b957 2022-07-14 tracey
575 a596b957 2022-07-14 tracey c->fd = s;
576 a596b957 2022-07-14 tracey c->sock = sock;
577 2a83fef7 2024-05-27 op memcpy(c->priv_fd, gotwebd_env->priv_fd, sizeof(c->priv_fd));
578 a596b957 2022-07-14 tracey c->buf_pos = 0;
579 a596b957 2022-07-14 tracey c->buf_len = 0;
580 a596b957 2022-07-14 tracey c->request_started = 0;
581 a596b957 2022-07-14 tracey c->sock->client_status = CLIENT_CONNECT;
582 a596b957 2022-07-14 tracey
583 5add7f42 2023-03-10 op event_set(&c->ev, s, EV_READ|EV_PERSIST, fcgi_request, c);
584 a596b957 2022-07-14 tracey event_add(&c->ev, NULL);
585 a596b957 2022-07-14 tracey
586 a596b957 2022-07-14 tracey evtimer_set(&c->tmo, fcgi_timeout, c);
587 a596b957 2022-07-14 tracey evtimer_add(&c->tmo, &timeout);
588 a596b957 2022-07-14 tracey
589 a596b957 2022-07-14 tracey client_cnt++;
590 a596b957 2022-07-14 tracey
591 a596b957 2022-07-14 tracey return;
592 a596b957 2022-07-14 tracey err:
593 a596b957 2022-07-14 tracey cgi_inflight--;
594 a596b957 2022-07-14 tracey close(s);
595 a596b957 2022-07-14 tracey if (c != NULL)
596 a596b957 2022-07-14 tracey free(c);
597 a596b957 2022-07-14 tracey }
598 a596b957 2022-07-14 tracey
599 026ac2c4 2022-08-20 stsp static void
600 a596b957 2022-07-14 tracey sockets_rlimit(int maxfd)
601 a596b957 2022-07-14 tracey {
602 a596b957 2022-07-14 tracey struct rlimit rl;
603 a596b957 2022-07-14 tracey
604 a596b957 2022-07-14 tracey if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
605 a596b957 2022-07-14 tracey fatal("%s: failed to get resource limit", __func__);
606 d6267a62 2022-07-17 naddy log_debug("%s: max open files %llu", __func__,
607 d6267a62 2022-07-17 naddy (unsigned long long)rl.rlim_max);
608 a596b957 2022-07-14 tracey
609 a596b957 2022-07-14 tracey /*
610 a596b957 2022-07-14 tracey * Allow the maximum number of open file descriptors for this
611 a596b957 2022-07-14 tracey * login class (which should be the class "daemon" by default).
612 a596b957 2022-07-14 tracey */
613 a596b957 2022-07-14 tracey if (maxfd == -1)
614 a596b957 2022-07-14 tracey rl.rlim_cur = rl.rlim_max;
615 a596b957 2022-07-14 tracey else
616 a596b957 2022-07-14 tracey rl.rlim_cur = MAXIMUM(rl.rlim_max, (rlim_t)maxfd);
617 a596b957 2022-07-14 tracey if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
618 a596b957 2022-07-14 tracey fatal("%s: failed to set resource limit", __func__);
619 d6267a62 2022-07-17 naddy }