Blame


1 8a35f56c 2022-07-16 thomas /*
2 8a35f56c 2022-07-16 thomas * Copyright (c) 2016, 2019, 2020-2021 Tracey Emery <tracey@traceyemery.net>
3 8a35f56c 2022-07-16 thomas * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 8a35f56c 2022-07-16 thomas * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
5 8a35f56c 2022-07-16 thomas * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
6 8a35f56c 2022-07-16 thomas *
7 8a35f56c 2022-07-16 thomas * Permission to use, copy, modify, and distribute this software for any
8 8a35f56c 2022-07-16 thomas * purpose with or without fee is hereby granted, provided that the above
9 8a35f56c 2022-07-16 thomas * copyright notice and this permission notice appear in all copies.
10 8a35f56c 2022-07-16 thomas *
11 8a35f56c 2022-07-16 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 8a35f56c 2022-07-16 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 8a35f56c 2022-07-16 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 8a35f56c 2022-07-16 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 8a35f56c 2022-07-16 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 8a35f56c 2022-07-16 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 8a35f56c 2022-07-16 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 8a35f56c 2022-07-16 thomas */
19 8a35f56c 2022-07-16 thomas
20 8a35f56c 2022-07-16 thomas #include <sys/param.h>
21 8a35f56c 2022-07-16 thomas #include <sys/ioctl.h>
22 8a35f56c 2022-07-16 thomas #include <sys/wait.h>
23 8a35f56c 2022-07-16 thomas #include <sys/uio.h>
24 8a35f56c 2022-07-16 thomas #include <sys/resource.h>
25 8a35f56c 2022-07-16 thomas #include <sys/socket.h>
26 8a35f56c 2022-07-16 thomas #include <sys/stat.h>
27 8a35f56c 2022-07-16 thomas #include <sys/time.h>
28 8a35f56c 2022-07-16 thomas #include <sys/types.h>
29 8a35f56c 2022-07-16 thomas #include <sys/mman.h>
30 8a35f56c 2022-07-16 thomas #include <sys/un.h>
31 8a35f56c 2022-07-16 thomas
32 8a35f56c 2022-07-16 thomas #include <net/if.h>
33 8a35f56c 2022-07-16 thomas #include <netinet/in.h>
34 8a35f56c 2022-07-16 thomas
35 8a35f56c 2022-07-16 thomas #include <errno.h>
36 8a35f56c 2022-07-16 thomas #include <event.h>
37 8a35f56c 2022-07-16 thomas #include <fcntl.h>
38 8a35f56c 2022-07-16 thomas #include <ifaddrs.h>
39 8a35f56c 2022-07-16 thomas #include <limits.h>
40 8a35f56c 2022-07-16 thomas #include <netdb.h>
41 8a35f56c 2022-07-16 thomas #include <poll.h>
42 8a35f56c 2022-07-16 thomas #include <pwd.h>
43 8a35f56c 2022-07-16 thomas #include <stddef.h>
44 8a35f56c 2022-07-16 thomas #include <stdio.h>
45 8a35f56c 2022-07-16 thomas #include <stdlib.h>
46 8a35f56c 2022-07-16 thomas #include <string.h>
47 8a35f56c 2022-07-16 thomas #include <unistd.h>
48 8a35f56c 2022-07-16 thomas
49 8a35f56c 2022-07-16 thomas #include "got_error.h"
50 8a35f56c 2022-07-16 thomas #include "got_opentemp.h"
51 8a35f56c 2022-07-16 thomas
52 8a35f56c 2022-07-16 thomas #include "proc.h"
53 8a35f56c 2022-07-16 thomas #include "gotwebd.h"
54 ff36aeea 2022-07-16 thomas
55 ff36aeea 2022-07-16 thomas #include "got_compat.h"
56 8a35f56c 2022-07-16 thomas
57 8a35f56c 2022-07-16 thomas #define SOCKS_BACKLOG 5
58 8a35f56c 2022-07-16 thomas #define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b))
59 8a35f56c 2022-07-16 thomas
60 8a35f56c 2022-07-16 thomas
61 8a35f56c 2022-07-16 thomas volatile int client_cnt;
62 8a35f56c 2022-07-16 thomas
63 8a35f56c 2022-07-16 thomas struct timeval timeout = { TIMEOUT_DEFAULT, 0 };
64 8a35f56c 2022-07-16 thomas
65 8a35f56c 2022-07-16 thomas void sockets_sighdlr(int, short, void *);
66 8a35f56c 2022-07-16 thomas void sockets_run(struct privsep *, struct privsep_proc *, void *);
67 8a35f56c 2022-07-16 thomas void sockets_launch(void);
68 8a35f56c 2022-07-16 thomas void sockets_purge(struct gotwebd *);
69 8a35f56c 2022-07-16 thomas void sockets_accept_paused(int, short, void *);
70 8a35f56c 2022-07-16 thomas void sockets_dup_new_socket(struct socket *, struct socket *);
71 8a35f56c 2022-07-16 thomas void sockets_rlimit(int);
72 8a35f56c 2022-07-16 thomas
73 8a35f56c 2022-07-16 thomas
74 8a35f56c 2022-07-16 thomas int sockets_dispatch_gotwebd(int, struct privsep_proc *, struct imsg *);
75 8a35f56c 2022-07-16 thomas int sockets_unix_socket_listen(struct privsep *, struct socket *);
76 8a35f56c 2022-07-16 thomas int sockets_create_socket(struct addresslist *, in_port_t);
77 8a35f56c 2022-07-16 thomas int sockets_socket_af(struct sockaddr_storage *, in_port_t);
78 8a35f56c 2022-07-16 thomas int sockets_accept_reserve(int, struct sockaddr *, socklen_t *, int,
79 8a35f56c 2022-07-16 thomas volatile int *);
80 8a35f56c 2022-07-16 thomas
81 8a35f56c 2022-07-16 thomas struct socket
82 8a35f56c 2022-07-16 thomas *sockets_conf_new_socket(struct gotwebd *, struct server *, int, int,
83 8a35f56c 2022-07-16 thomas int);
84 8a35f56c 2022-07-16 thomas
85 8a35f56c 2022-07-16 thomas int cgi_inflight = 0;
86 8a35f56c 2022-07-16 thomas
87 8a35f56c 2022-07-16 thomas static struct privsep_proc procs[] = {
88 8a35f56c 2022-07-16 thomas { "gotwebd", PROC_GOTWEBD, sockets_dispatch_gotwebd },
89 8a35f56c 2022-07-16 thomas };
90 8a35f56c 2022-07-16 thomas
91 8a35f56c 2022-07-16 thomas void
92 8a35f56c 2022-07-16 thomas sockets(struct privsep *ps, struct privsep_proc *p)
93 8a35f56c 2022-07-16 thomas {
94 8a35f56c 2022-07-16 thomas proc_run(ps, p, procs, nitems(procs), sockets_run, NULL);
95 8a35f56c 2022-07-16 thomas }
96 8a35f56c 2022-07-16 thomas
97 8a35f56c 2022-07-16 thomas void
98 8a35f56c 2022-07-16 thomas sockets_run(struct privsep *ps, struct privsep_proc *p, void *arg)
99 8a35f56c 2022-07-16 thomas {
100 8a35f56c 2022-07-16 thomas if (config_init(ps->ps_env) == -1)
101 8a35f56c 2022-07-16 thomas fatal("failed to initialize configuration");
102 8a35f56c 2022-07-16 thomas
103 8a35f56c 2022-07-16 thomas p->p_shutdown = sockets_shutdown;
104 8a35f56c 2022-07-16 thomas
105 8a35f56c 2022-07-16 thomas sockets_rlimit(-1);
106 8a35f56c 2022-07-16 thomas
107 8a35f56c 2022-07-16 thomas signal_del(&ps->ps_evsigchld);
108 8a35f56c 2022-07-16 thomas signal_set(&ps->ps_evsigchld, SIGCHLD, sockets_sighdlr, ps);
109 8a35f56c 2022-07-16 thomas signal_add(&ps->ps_evsigchld, NULL);
110 8a35f56c 2022-07-16 thomas
111 8a35f56c 2022-07-16 thomas #ifndef PROFILE
112 8a35f56c 2022-07-16 thomas if (pledge("stdio rpath wpath cpath inet recvfd proc exec sendfd",
113 8a35f56c 2022-07-16 thomas NULL) == -1)
114 8a35f56c 2022-07-16 thomas fatal("pledge");
115 8a35f56c 2022-07-16 thomas #endif
116 8a35f56c 2022-07-16 thomas }
117 8a35f56c 2022-07-16 thomas
118 8a35f56c 2022-07-16 thomas void
119 8a35f56c 2022-07-16 thomas sockets_parse_sockets(struct gotwebd *env)
120 8a35f56c 2022-07-16 thomas {
121 8a35f56c 2022-07-16 thomas struct server *srv;
122 8a35f56c 2022-07-16 thomas struct socket *sock, *new_sock = NULL;
123 8a35f56c 2022-07-16 thomas struct address *a;
124 8a35f56c 2022-07-16 thomas int sock_id = 0, ipv4 = 0, ipv6 = 0;
125 8a35f56c 2022-07-16 thomas
126 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(srv, env->servers, entry) {
127 8a35f56c 2022-07-16 thomas if (srv->unix_socket) {
128 8a35f56c 2022-07-16 thomas sock_id++;
129 8a35f56c 2022-07-16 thomas new_sock = sockets_conf_new_socket(env, srv,
130 8a35f56c 2022-07-16 thomas sock_id, UNIX, 0);
131 8a35f56c 2022-07-16 thomas TAILQ_INSERT_TAIL(env->sockets, new_sock, entry);
132 8a35f56c 2022-07-16 thomas }
133 8a35f56c 2022-07-16 thomas
134 8a35f56c 2022-07-16 thomas if (srv->fcgi_socket) {
135 8a35f56c 2022-07-16 thomas sock_id++;
136 8a35f56c 2022-07-16 thomas new_sock = sockets_conf_new_socket(env, srv,
137 8a35f56c 2022-07-16 thomas sock_id, FCGI, 0);
138 8a35f56c 2022-07-16 thomas TAILQ_INSERT_TAIL(env->sockets, new_sock, entry);
139 8a35f56c 2022-07-16 thomas
140 8a35f56c 2022-07-16 thomas /* add ipv6 children */
141 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(sock, env->sockets, entry) {
142 8a35f56c 2022-07-16 thomas ipv4 = ipv6 = 0;
143 8a35f56c 2022-07-16 thomas
144 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(a, sock->conf.al, entry) {
145 8a35f56c 2022-07-16 thomas if (a->ss.ss_family == AF_INET)
146 8a35f56c 2022-07-16 thomas ipv4 = 1;
147 8a35f56c 2022-07-16 thomas if (a->ss.ss_family == AF_INET6)
148 8a35f56c 2022-07-16 thomas ipv6 = 1;
149 8a35f56c 2022-07-16 thomas }
150 8a35f56c 2022-07-16 thomas
151 8a35f56c 2022-07-16 thomas /* create ipv6 sock */
152 8a35f56c 2022-07-16 thomas if (ipv4 == 1 && ipv6 == 1) {
153 8a35f56c 2022-07-16 thomas sock_id++;
154 8a35f56c 2022-07-16 thomas sock->conf.child_id = sock_id;
155 8a35f56c 2022-07-16 thomas new_sock = sockets_conf_new_socket(env,
156 8a35f56c 2022-07-16 thomas srv, sock_id, FCGI, 1);
157 8a35f56c 2022-07-16 thomas sockets_dup_new_socket(sock, new_sock);
158 8a35f56c 2022-07-16 thomas TAILQ_INSERT_TAIL(env->sockets,
159 8a35f56c 2022-07-16 thomas new_sock, entry);
160 8a35f56c 2022-07-16 thomas continue;
161 8a35f56c 2022-07-16 thomas }
162 8a35f56c 2022-07-16 thomas }
163 8a35f56c 2022-07-16 thomas }
164 8a35f56c 2022-07-16 thomas }
165 8a35f56c 2022-07-16 thomas }
166 8a35f56c 2022-07-16 thomas
167 8a35f56c 2022-07-16 thomas void
168 8a35f56c 2022-07-16 thomas sockets_dup_new_socket(struct socket *p_sock, struct socket *sock)
169 8a35f56c 2022-07-16 thomas {
170 8a35f56c 2022-07-16 thomas struct address *a, *acp;
171 8a35f56c 2022-07-16 thomas int n;
172 8a35f56c 2022-07-16 thomas
173 8a35f56c 2022-07-16 thomas sock->conf.parent_id = p_sock->conf.id;
174 8a35f56c 2022-07-16 thomas sock->conf.ipv4 = 0;
175 8a35f56c 2022-07-16 thomas sock->conf.ipv6 = 1;
176 8a35f56c 2022-07-16 thomas
177 8a35f56c 2022-07-16 thomas memcpy(&sock->conf.srv_name, p_sock->conf.srv_name,
178 8a35f56c 2022-07-16 thomas sizeof(sock->conf.srv_name));
179 8a35f56c 2022-07-16 thomas
180 8a35f56c 2022-07-16 thomas n = snprintf(sock->conf.name, GOTWEBD_MAXTEXT, "%s_child",
181 8a35f56c 2022-07-16 thomas p_sock->conf.srv_name);
182 8a35f56c 2022-07-16 thomas if (n < 0) {
183 8a35f56c 2022-07-16 thomas free(p_sock->conf.al);
184 8a35f56c 2022-07-16 thomas free(p_sock);
185 8a35f56c 2022-07-16 thomas free(sock->conf.al);
186 8a35f56c 2022-07-16 thomas free(sock);
187 8a35f56c 2022-07-16 thomas fatalx("%s: snprintf", __func__);
188 8a35f56c 2022-07-16 thomas }
189 8a35f56c 2022-07-16 thomas
190 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(a, p_sock->conf.al, entry) {
191 8a35f56c 2022-07-16 thomas if (a->ss.ss_family == AF_INET)
192 8a35f56c 2022-07-16 thomas continue;
193 8a35f56c 2022-07-16 thomas
194 8a35f56c 2022-07-16 thomas if ((acp = calloc(1, sizeof(*acp))) == NULL)
195 8a35f56c 2022-07-16 thomas fatal("%s: calloc", __func__);
196 8a35f56c 2022-07-16 thomas memcpy(&acp->ss, &a->ss, sizeof(acp->ss));
197 8a35f56c 2022-07-16 thomas acp->ipproto = a->ipproto;
198 8a35f56c 2022-07-16 thomas acp->prefixlen = a->prefixlen;
199 8a35f56c 2022-07-16 thomas acp->port = a->port;
200 8a35f56c 2022-07-16 thomas if (strlen(a->ifname) != 0) {
201 8a35f56c 2022-07-16 thomas if (strlcpy(acp->ifname, a->ifname,
202 8a35f56c 2022-07-16 thomas sizeof(acp->ifname)) >= sizeof(acp->ifname)) {
203 8a35f56c 2022-07-16 thomas fatalx("%s: interface name truncated",
204 8a35f56c 2022-07-16 thomas __func__);
205 8a35f56c 2022-07-16 thomas }
206 8a35f56c 2022-07-16 thomas }
207 8a35f56c 2022-07-16 thomas
208 8a35f56c 2022-07-16 thomas TAILQ_INSERT_TAIL(sock->conf.al, acp, entry);
209 8a35f56c 2022-07-16 thomas }
210 8a35f56c 2022-07-16 thomas }
211 8a35f56c 2022-07-16 thomas
212 8a35f56c 2022-07-16 thomas struct socket *
213 8a35f56c 2022-07-16 thomas sockets_conf_new_socket(struct gotwebd *env, struct server *srv, int id,
214 8a35f56c 2022-07-16 thomas int type, int is_dup)
215 8a35f56c 2022-07-16 thomas {
216 8a35f56c 2022-07-16 thomas struct socket *sock;
217 8a35f56c 2022-07-16 thomas struct address *a, *acp;
218 8a35f56c 2022-07-16 thomas int n;
219 8a35f56c 2022-07-16 thomas
220 8a35f56c 2022-07-16 thomas if ((sock = calloc(1, sizeof(*sock))) == NULL)
221 8a35f56c 2022-07-16 thomas fatalx("%s: calloc", __func__);
222 8a35f56c 2022-07-16 thomas
223 8a35f56c 2022-07-16 thomas if ((sock->conf.al = calloc(1, sizeof(*sock->conf.al))) == NULL) {
224 8a35f56c 2022-07-16 thomas free(sock);
225 8a35f56c 2022-07-16 thomas fatalx("%s: calloc", __func__);
226 8a35f56c 2022-07-16 thomas }
227 8a35f56c 2022-07-16 thomas TAILQ_INIT(sock->conf.al);
228 8a35f56c 2022-07-16 thomas
229 8a35f56c 2022-07-16 thomas sock->conf.parent_id = 0;
230 8a35f56c 2022-07-16 thomas sock->conf.id = id;
231 8a35f56c 2022-07-16 thomas
232 8a35f56c 2022-07-16 thomas sock->fd = -1;
233 8a35f56c 2022-07-16 thomas
234 8a35f56c 2022-07-16 thomas sock->conf.type = type;
235 8a35f56c 2022-07-16 thomas
236 8a35f56c 2022-07-16 thomas if (type == UNIX) {
237 8a35f56c 2022-07-16 thomas if (strlcpy(sock->conf.unix_socket_name,
238 8a35f56c 2022-07-16 thomas srv->unix_socket_name,
239 8a35f56c 2022-07-16 thomas sizeof(sock->conf.unix_socket_name)) >=
240 8a35f56c 2022-07-16 thomas sizeof(sock->conf.unix_socket_name)) {
241 8a35f56c 2022-07-16 thomas free(sock->conf.al);
242 8a35f56c 2022-07-16 thomas free(sock);
243 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
244 8a35f56c 2022-07-16 thomas }
245 8a35f56c 2022-07-16 thomas } else
246 8a35f56c 2022-07-16 thomas sock->conf.ipv4 = 1;
247 8a35f56c 2022-07-16 thomas
248 8a35f56c 2022-07-16 thomas sock->conf.fcgi_socket_port = srv->fcgi_socket_port;
249 8a35f56c 2022-07-16 thomas
250 8a35f56c 2022-07-16 thomas if (is_dup)
251 8a35f56c 2022-07-16 thomas goto done;
252 8a35f56c 2022-07-16 thomas
253 8a35f56c 2022-07-16 thomas n = snprintf(sock->conf.name, GOTWEBD_MAXTEXT, "%s_parent",
254 8a35f56c 2022-07-16 thomas srv->name);
255 8a35f56c 2022-07-16 thomas if (n < 0) {
256 8a35f56c 2022-07-16 thomas free(sock->conf.al);
257 8a35f56c 2022-07-16 thomas free(sock);
258 8a35f56c 2022-07-16 thomas fatalx("%s: snprintf", __func__);
259 8a35f56c 2022-07-16 thomas }
260 8a35f56c 2022-07-16 thomas
261 8a35f56c 2022-07-16 thomas if (strlcpy(sock->conf.srv_name, srv->name,
262 8a35f56c 2022-07-16 thomas sizeof(sock->conf.srv_name)) >= sizeof(sock->conf.srv_name)) {
263 8a35f56c 2022-07-16 thomas free(sock->conf.al);
264 8a35f56c 2022-07-16 thomas free(sock);
265 8a35f56c 2022-07-16 thomas fatalx("%s: strlcpy", __func__);
266 8a35f56c 2022-07-16 thomas }
267 8a35f56c 2022-07-16 thomas
268 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(a, srv->al, entry) {
269 8a35f56c 2022-07-16 thomas if ((acp = calloc(1, sizeof(*acp))) == NULL) {
270 8a35f56c 2022-07-16 thomas free(sock->conf.al);
271 8a35f56c 2022-07-16 thomas free(sock);
272 8a35f56c 2022-07-16 thomas fatal("%s: calloc", __func__);
273 8a35f56c 2022-07-16 thomas }
274 8a35f56c 2022-07-16 thomas memcpy(&acp->ss, &a->ss, sizeof(acp->ss));
275 8a35f56c 2022-07-16 thomas acp->ipproto = a->ipproto;
276 8a35f56c 2022-07-16 thomas acp->prefixlen = a->prefixlen;
277 8a35f56c 2022-07-16 thomas acp->port = a->port;
278 8a35f56c 2022-07-16 thomas if (strlen(a->ifname) != 0) {
279 8a35f56c 2022-07-16 thomas if (strlcpy(acp->ifname, a->ifname,
280 8a35f56c 2022-07-16 thomas sizeof(acp->ifname)) >= sizeof(acp->ifname)) {
281 8a35f56c 2022-07-16 thomas fatalx("%s: interface name truncated",
282 8a35f56c 2022-07-16 thomas __func__);
283 8a35f56c 2022-07-16 thomas }
284 8a35f56c 2022-07-16 thomas }
285 8a35f56c 2022-07-16 thomas
286 8a35f56c 2022-07-16 thomas TAILQ_INSERT_TAIL(sock->conf.al, acp, entry);
287 8a35f56c 2022-07-16 thomas }
288 8a35f56c 2022-07-16 thomas done:
289 8a35f56c 2022-07-16 thomas return (sock);
290 8a35f56c 2022-07-16 thomas }
291 8a35f56c 2022-07-16 thomas
292 8a35f56c 2022-07-16 thomas int
293 8a35f56c 2022-07-16 thomas sockets_socket_af(struct sockaddr_storage *ss, in_port_t port)
294 8a35f56c 2022-07-16 thomas {
295 8a35f56c 2022-07-16 thomas switch (ss->ss_family) {
296 8a35f56c 2022-07-16 thomas case AF_INET:
297 8a35f56c 2022-07-16 thomas ((struct sockaddr_in *)ss)->sin_port = port;
298 ff36aeea 2022-07-16 thomas /* TA: Iffy... */
299 ff36aeea 2022-07-16 thomas #ifndef __linux__
300 8a35f56c 2022-07-16 thomas ((struct sockaddr_in *)ss)->sin_len =
301 8a35f56c 2022-07-16 thomas sizeof(struct sockaddr_in);
302 ff36aeea 2022-07-16 thomas #endif
303 8a35f56c 2022-07-16 thomas break;
304 8a35f56c 2022-07-16 thomas case AF_INET6:
305 8a35f56c 2022-07-16 thomas ((struct sockaddr_in6 *)ss)->sin6_port = port;
306 ff36aeea 2022-07-16 thomas /* TA: Iffy... */
307 ff36aeea 2022-07-16 thomas #ifndef __linux__
308 8a35f56c 2022-07-16 thomas ((struct sockaddr_in6 *)ss)->sin6_len =
309 8a35f56c 2022-07-16 thomas sizeof(struct sockaddr_in6);
310 ff36aeea 2022-07-16 thomas #endif
311 8a35f56c 2022-07-16 thomas break;
312 8a35f56c 2022-07-16 thomas default:
313 8a35f56c 2022-07-16 thomas return -1;
314 8a35f56c 2022-07-16 thomas }
315 8a35f56c 2022-07-16 thomas
316 8a35f56c 2022-07-16 thomas return 0;
317 8a35f56c 2022-07-16 thomas }
318 8a35f56c 2022-07-16 thomas
319 8a35f56c 2022-07-16 thomas void
320 8a35f56c 2022-07-16 thomas sockets_launch(void)
321 8a35f56c 2022-07-16 thomas {
322 8a35f56c 2022-07-16 thomas struct socket *sock;
323 8a35f56c 2022-07-16 thomas
324 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(sock, gotwebd_env->sockets, entry) {
325 8a35f56c 2022-07-16 thomas log_debug("%s: configuring socket %d (%d)", __func__,
326 8a35f56c 2022-07-16 thomas sock->conf.id, sock->fd);
327 8a35f56c 2022-07-16 thomas
328 8a35f56c 2022-07-16 thomas event_set(&sock->ev, sock->fd, EV_READ | EV_PERSIST,
329 8a35f56c 2022-07-16 thomas sockets_socket_accept, sock);
330 8a35f56c 2022-07-16 thomas
331 8a35f56c 2022-07-16 thomas if (event_add(&sock->ev, NULL))
332 8a35f56c 2022-07-16 thomas fatalx("event add sock");
333 8a35f56c 2022-07-16 thomas
334 8a35f56c 2022-07-16 thomas evtimer_set(&sock->pause, sockets_accept_paused, sock);
335 8a35f56c 2022-07-16 thomas
336 8a35f56c 2022-07-16 thomas log_debug("%s: running socket listener %d", __func__,
337 8a35f56c 2022-07-16 thomas sock->conf.id);
338 8a35f56c 2022-07-16 thomas }
339 8a35f56c 2022-07-16 thomas }
340 8a35f56c 2022-07-16 thomas
341 8a35f56c 2022-07-16 thomas void
342 8a35f56c 2022-07-16 thomas sockets_purge(struct gotwebd *env)
343 8a35f56c 2022-07-16 thomas {
344 8a35f56c 2022-07-16 thomas struct socket *sock, *tsock;
345 8a35f56c 2022-07-16 thomas
346 8a35f56c 2022-07-16 thomas /* shutdown and remove sockets */
347 8a35f56c 2022-07-16 thomas TAILQ_FOREACH_SAFE(sock, env->sockets, entry, tsock) {
348 8a35f56c 2022-07-16 thomas if (event_initialized(&sock->ev))
349 8a35f56c 2022-07-16 thomas event_del(&sock->ev);
350 8a35f56c 2022-07-16 thomas if (evtimer_initialized(&sock->evt))
351 8a35f56c 2022-07-16 thomas evtimer_del(&sock->evt);
352 8a35f56c 2022-07-16 thomas if (evtimer_initialized(&sock->pause))
353 8a35f56c 2022-07-16 thomas evtimer_del(&sock->pause);
354 8a35f56c 2022-07-16 thomas if (sock->fd != -1)
355 8a35f56c 2022-07-16 thomas close(sock->fd);
356 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(env->sockets, sock, entry);
357 8a35f56c 2022-07-16 thomas }
358 8a35f56c 2022-07-16 thomas }
359 8a35f56c 2022-07-16 thomas
360 8a35f56c 2022-07-16 thomas int
361 8a35f56c 2022-07-16 thomas sockets_dispatch_gotwebd(int fd, struct privsep_proc *p, struct imsg *imsg)
362 8a35f56c 2022-07-16 thomas {
363 8a35f56c 2022-07-16 thomas struct privsep *ps = p->p_ps;
364 8a35f56c 2022-07-16 thomas int res = 0, cmd = 0, verbose;
365 8a35f56c 2022-07-16 thomas
366 8a35f56c 2022-07-16 thomas switch (imsg->hdr.type) {
367 8a35f56c 2022-07-16 thomas case IMSG_CFG_SRV:
368 8a35f56c 2022-07-16 thomas config_getserver(gotwebd_env, imsg);
369 8a35f56c 2022-07-16 thomas break;
370 8a35f56c 2022-07-16 thomas case IMSG_CFG_SOCK:
371 8a35f56c 2022-07-16 thomas config_getsock(gotwebd_env, imsg);
372 8a35f56c 2022-07-16 thomas break;
373 8a35f56c 2022-07-16 thomas case IMSG_CFG_FD:
374 8a35f56c 2022-07-16 thomas config_getfd(gotwebd_env, imsg);
375 8a35f56c 2022-07-16 thomas break;
376 8a35f56c 2022-07-16 thomas case IMSG_CFG_DONE:
377 8a35f56c 2022-07-16 thomas config_getcfg(gotwebd_env, imsg);
378 8a35f56c 2022-07-16 thomas break;
379 8a35f56c 2022-07-16 thomas case IMSG_CTL_START:
380 8a35f56c 2022-07-16 thomas sockets_launch();
381 8a35f56c 2022-07-16 thomas break;
382 8a35f56c 2022-07-16 thomas case IMSG_CTL_VERBOSE:
383 8a35f56c 2022-07-16 thomas IMSG_SIZE_CHECK(imsg, &verbose);
384 8a35f56c 2022-07-16 thomas memcpy(&verbose, imsg->data, sizeof(verbose));
385 8a35f56c 2022-07-16 thomas log_setverbose(verbose);
386 8a35f56c 2022-07-16 thomas break;
387 8a35f56c 2022-07-16 thomas default:
388 8a35f56c 2022-07-16 thomas return -1;
389 8a35f56c 2022-07-16 thomas }
390 8a35f56c 2022-07-16 thomas
391 8a35f56c 2022-07-16 thomas switch (cmd) {
392 8a35f56c 2022-07-16 thomas case 0:
393 8a35f56c 2022-07-16 thomas break;
394 8a35f56c 2022-07-16 thomas default:
395 8a35f56c 2022-07-16 thomas if (proc_compose_imsg(ps, PROC_GOTWEBD, -1, cmd,
396 8a35f56c 2022-07-16 thomas imsg->hdr.peerid, -1, &res, sizeof(res)) == -1)
397 8a35f56c 2022-07-16 thomas return -1;
398 8a35f56c 2022-07-16 thomas break;
399 8a35f56c 2022-07-16 thomas }
400 8a35f56c 2022-07-16 thomas
401 8a35f56c 2022-07-16 thomas return 0;
402 8a35f56c 2022-07-16 thomas }
403 8a35f56c 2022-07-16 thomas
404 8a35f56c 2022-07-16 thomas void
405 8a35f56c 2022-07-16 thomas sockets_sighdlr(int sig, short event, void *arg)
406 8a35f56c 2022-07-16 thomas {
407 8a35f56c 2022-07-16 thomas switch (sig) {
408 8a35f56c 2022-07-16 thomas case SIGHUP:
409 8a35f56c 2022-07-16 thomas log_info("%s: ignoring SIGHUP", __func__);
410 8a35f56c 2022-07-16 thomas break;
411 8a35f56c 2022-07-16 thomas case SIGPIPE:
412 8a35f56c 2022-07-16 thomas log_info("%s: ignoring SIGPIPE", __func__);
413 8a35f56c 2022-07-16 thomas break;
414 8a35f56c 2022-07-16 thomas case SIGUSR1:
415 8a35f56c 2022-07-16 thomas log_info("%s: ignoring SIGUSR1", __func__);
416 8a35f56c 2022-07-16 thomas break;
417 8a35f56c 2022-07-16 thomas case SIGCHLD:
418 8a35f56c 2022-07-16 thomas break;
419 8a35f56c 2022-07-16 thomas default:
420 8a35f56c 2022-07-16 thomas log_info("SIGNAL: %d", sig);
421 8a35f56c 2022-07-16 thomas fatalx("unexpected signal");
422 8a35f56c 2022-07-16 thomas }
423 8a35f56c 2022-07-16 thomas }
424 8a35f56c 2022-07-16 thomas
425 8a35f56c 2022-07-16 thomas void
426 8a35f56c 2022-07-16 thomas sockets_shutdown(void)
427 8a35f56c 2022-07-16 thomas {
428 8a35f56c 2022-07-16 thomas struct server *srv, *tsrv;
429 8a35f56c 2022-07-16 thomas struct socket *sock, *tsock;
430 8a35f56c 2022-07-16 thomas
431 8a35f56c 2022-07-16 thomas sockets_purge(gotwebd_env);
432 8a35f56c 2022-07-16 thomas
433 8a35f56c 2022-07-16 thomas /* clean sockets */
434 8a35f56c 2022-07-16 thomas TAILQ_FOREACH_SAFE(sock, gotwebd_env->sockets, entry, tsock) {
435 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(gotwebd_env->sockets, sock, entry);
436 8a35f56c 2022-07-16 thomas close(sock->fd);
437 8a35f56c 2022-07-16 thomas free(sock);
438 8a35f56c 2022-07-16 thomas }
439 8a35f56c 2022-07-16 thomas
440 8a35f56c 2022-07-16 thomas /* clean servers */
441 8a35f56c 2022-07-16 thomas TAILQ_FOREACH_SAFE(srv, gotwebd_env->servers, entry, tsrv)
442 8a35f56c 2022-07-16 thomas free(srv);
443 8a35f56c 2022-07-16 thomas
444 8a35f56c 2022-07-16 thomas free(gotwebd_env->sockets);
445 8a35f56c 2022-07-16 thomas free(gotwebd_env->servers);
446 8a35f56c 2022-07-16 thomas free(gotwebd_env);
447 8a35f56c 2022-07-16 thomas }
448 8a35f56c 2022-07-16 thomas
449 8a35f56c 2022-07-16 thomas int
450 8a35f56c 2022-07-16 thomas sockets_privinit(struct gotwebd *env, struct socket *sock)
451 8a35f56c 2022-07-16 thomas {
452 8a35f56c 2022-07-16 thomas struct privsep *ps = env->gotwebd_ps;
453 8a35f56c 2022-07-16 thomas
454 8a35f56c 2022-07-16 thomas if (sock->conf.type == UNIX) {
455 8a35f56c 2022-07-16 thomas log_debug("%s: initializing unix socket %s", __func__,
456 8a35f56c 2022-07-16 thomas sock->conf.unix_socket_name);
457 8a35f56c 2022-07-16 thomas sock->fd = sockets_unix_socket_listen(ps, sock);
458 8a35f56c 2022-07-16 thomas if (sock->fd == -1) {
459 8a35f56c 2022-07-16 thomas log_warnx("%s: create unix socket failed", __func__);
460 8a35f56c 2022-07-16 thomas return -1;
461 8a35f56c 2022-07-16 thomas }
462 8a35f56c 2022-07-16 thomas }
463 8a35f56c 2022-07-16 thomas
464 8a35f56c 2022-07-16 thomas if (sock->conf.type == FCGI) {
465 8a35f56c 2022-07-16 thomas log_debug("%s: initializing fcgi socket for %s", __func__,
466 8a35f56c 2022-07-16 thomas sock->conf.name);
467 8a35f56c 2022-07-16 thomas sock->fd = sockets_create_socket(sock->conf.al,
468 8a35f56c 2022-07-16 thomas sock->conf.fcgi_socket_port);
469 8a35f56c 2022-07-16 thomas if (sock->fd == -1) {
470 8a35f56c 2022-07-16 thomas log_warnx("%s: create unix socket failed", __func__);
471 8a35f56c 2022-07-16 thomas return -1;
472 8a35f56c 2022-07-16 thomas }
473 8a35f56c 2022-07-16 thomas }
474 8a35f56c 2022-07-16 thomas
475 8a35f56c 2022-07-16 thomas return 0;
476 8a35f56c 2022-07-16 thomas }
477 8a35f56c 2022-07-16 thomas
478 8a35f56c 2022-07-16 thomas int
479 8a35f56c 2022-07-16 thomas sockets_unix_socket_listen(struct privsep *ps, struct socket *sock)
480 8a35f56c 2022-07-16 thomas {
481 8a35f56c 2022-07-16 thomas struct gotwebd *env = ps->ps_env;
482 8a35f56c 2022-07-16 thomas struct sockaddr_un sun;
483 8a35f56c 2022-07-16 thomas struct socket *tsock;
484 8a35f56c 2022-07-16 thomas int u_fd = -1;
485 8a35f56c 2022-07-16 thomas mode_t old_umask, mode;
486 8a35f56c 2022-07-16 thomas
487 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(tsock, env->sockets, entry) {
488 8a35f56c 2022-07-16 thomas if (strcmp(tsock->conf.unix_socket_name,
489 8a35f56c 2022-07-16 thomas sock->conf.unix_socket_name) == 0 &&
490 8a35f56c 2022-07-16 thomas tsock->fd != -1)
491 8a35f56c 2022-07-16 thomas return (tsock->fd);
492 8a35f56c 2022-07-16 thomas }
493 8a35f56c 2022-07-16 thomas
494 8a35f56c 2022-07-16 thomas u_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
495 8a35f56c 2022-07-16 thomas if (u_fd == -1) {
496 8a35f56c 2022-07-16 thomas log_warn("%s: socket", __func__);
497 8a35f56c 2022-07-16 thomas return -1;
498 8a35f56c 2022-07-16 thomas }
499 8a35f56c 2022-07-16 thomas
500 8a35f56c 2022-07-16 thomas sun.sun_family = AF_UNIX;
501 8a35f56c 2022-07-16 thomas if (strlcpy(sun.sun_path, sock->conf.unix_socket_name,
502 8a35f56c 2022-07-16 thomas sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
503 8a35f56c 2022-07-16 thomas log_warn("%s: %s name too long", __func__,
504 8a35f56c 2022-07-16 thomas sock->conf.unix_socket_name);
505 8a35f56c 2022-07-16 thomas close(u_fd);
506 8a35f56c 2022-07-16 thomas return -1;
507 8a35f56c 2022-07-16 thomas }
508 8a35f56c 2022-07-16 thomas
509 8a35f56c 2022-07-16 thomas if (unlink(sock->conf.unix_socket_name) == -1) {
510 8a35f56c 2022-07-16 thomas if (errno != ENOENT) {
511 8a35f56c 2022-07-16 thomas log_warn("%s: unlink %s", __func__,
512 8a35f56c 2022-07-16 thomas sock->conf.unix_socket_name);
513 8a35f56c 2022-07-16 thomas close(u_fd);
514 8a35f56c 2022-07-16 thomas return -1;
515 8a35f56c 2022-07-16 thomas }
516 8a35f56c 2022-07-16 thomas }
517 8a35f56c 2022-07-16 thomas
518 8a35f56c 2022-07-16 thomas old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
519 8a35f56c 2022-07-16 thomas mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
520 8a35f56c 2022-07-16 thomas
521 8a35f56c 2022-07-16 thomas if (bind(u_fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
522 8a35f56c 2022-07-16 thomas log_warn("%s: bind: %s", __func__, sock->conf.unix_socket_name);
523 8a35f56c 2022-07-16 thomas close(u_fd);
524 8a35f56c 2022-07-16 thomas (void)umask(old_umask);
525 8a35f56c 2022-07-16 thomas return -1;
526 8a35f56c 2022-07-16 thomas }
527 8a35f56c 2022-07-16 thomas
528 8a35f56c 2022-07-16 thomas (void)umask(old_umask);
529 8a35f56c 2022-07-16 thomas
530 8a35f56c 2022-07-16 thomas if (chmod(sock->conf.unix_socket_name, mode) == -1) {
531 8a35f56c 2022-07-16 thomas log_warn("%s: chmod", __func__);
532 8a35f56c 2022-07-16 thomas close(u_fd);
533 8a35f56c 2022-07-16 thomas (void)unlink(sock->conf.unix_socket_name);
534 8a35f56c 2022-07-16 thomas return -1;
535 8a35f56c 2022-07-16 thomas }
536 8a35f56c 2022-07-16 thomas
537 8a35f56c 2022-07-16 thomas if (chown(sock->conf.unix_socket_name, ps->ps_pw->pw_uid,
538 8a35f56c 2022-07-16 thomas ps->ps_pw->pw_gid) == -1) {
539 8a35f56c 2022-07-16 thomas log_warn("%s: chown", __func__);
540 8a35f56c 2022-07-16 thomas close(u_fd);
541 8a35f56c 2022-07-16 thomas (void)unlink(sock->conf.unix_socket_name);
542 8a35f56c 2022-07-16 thomas return -1;
543 8a35f56c 2022-07-16 thomas }
544 8a35f56c 2022-07-16 thomas
545 8a35f56c 2022-07-16 thomas if (listen(u_fd, SOCKS_BACKLOG) == -1) {
546 8a35f56c 2022-07-16 thomas log_warn("%s: listen", __func__);
547 8a35f56c 2022-07-16 thomas return -1;
548 8a35f56c 2022-07-16 thomas }
549 8a35f56c 2022-07-16 thomas
550 8a35f56c 2022-07-16 thomas return u_fd;
551 8a35f56c 2022-07-16 thomas }
552 8a35f56c 2022-07-16 thomas
553 8a35f56c 2022-07-16 thomas int
554 8a35f56c 2022-07-16 thomas sockets_create_socket(struct addresslist *al, in_port_t port)
555 8a35f56c 2022-07-16 thomas {
556 8a35f56c 2022-07-16 thomas struct addrinfo hints;
557 8a35f56c 2022-07-16 thomas struct address *a;
558 8a35f56c 2022-07-16 thomas int fd = -1, o_val = 1, flags;
559 8a35f56c 2022-07-16 thomas
560 8a35f56c 2022-07-16 thomas memset(&hints, 0, sizeof(hints));
561 8a35f56c 2022-07-16 thomas hints.ai_family = AF_UNSPEC;
562 8a35f56c 2022-07-16 thomas hints.ai_socktype = SOCK_STREAM;
563 8a35f56c 2022-07-16 thomas hints.ai_flags |= AI_PASSIVE;
564 8a35f56c 2022-07-16 thomas
565 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(a, al, entry) {
566 8a35f56c 2022-07-16 thomas if (sockets_socket_af(&a->ss, port) == -1) {
567 8a35f56c 2022-07-16 thomas log_warnx("%s: sockets_socket_af", __func__);
568 8a35f56c 2022-07-16 thomas goto fail;
569 8a35f56c 2022-07-16 thomas }
570 8a35f56c 2022-07-16 thomas
571 8a35f56c 2022-07-16 thomas fd = socket(a->ss.ss_family, hints.ai_socktype,
572 8a35f56c 2022-07-16 thomas a->ipproto);
573 8a35f56c 2022-07-16 thomas log_debug("%s: opening socket (%d) for %s", __func__,
574 8a35f56c 2022-07-16 thomas fd, a->ifname);
575 8a35f56c 2022-07-16 thomas
576 8a35f56c 2022-07-16 thomas if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &o_val,
577 8a35f56c 2022-07-16 thomas sizeof(int)) == -1) {
578 8a35f56c 2022-07-16 thomas log_warn("%s: setsockopt error", __func__);
579 8a35f56c 2022-07-16 thomas return -1;
580 8a35f56c 2022-07-16 thomas }
581 8a35f56c 2022-07-16 thomas
582 8a35f56c 2022-07-16 thomas /* non-blocking */
583 8a35f56c 2022-07-16 thomas flags = fcntl(fd, F_GETFL);
584 8a35f56c 2022-07-16 thomas flags |= O_NONBLOCK;
585 8a35f56c 2022-07-16 thomas fcntl(fd, F_SETFL, flags);
586 8a35f56c 2022-07-16 thomas
587 ff36aeea 2022-07-16 thomas /* TA: 'sizeof a' vs a->ss.len */
588 ff36aeea 2022-07-16 thomas if (bind(fd, (struct sockaddr *)&a->ss, sizeof a) == -1) {
589 8a35f56c 2022-07-16 thomas close(fd);
590 8a35f56c 2022-07-16 thomas log_info("%s: can't bind to port %d", __func__,
591 8a35f56c 2022-07-16 thomas ntohs(port));
592 8a35f56c 2022-07-16 thomas goto fail;
593 8a35f56c 2022-07-16 thomas }
594 8a35f56c 2022-07-16 thomas
595 8a35f56c 2022-07-16 thomas if (listen(fd, SOMAXCONN) == -1) {
596 8a35f56c 2022-07-16 thomas log_warn("%s, unable to listen on socket", __func__);
597 8a35f56c 2022-07-16 thomas goto fail;
598 8a35f56c 2022-07-16 thomas }
599 8a35f56c 2022-07-16 thomas }
600 8a35f56c 2022-07-16 thomas
601 8a35f56c 2022-07-16 thomas free(a);
602 8a35f56c 2022-07-16 thomas return (fd);
603 8a35f56c 2022-07-16 thomas fail:
604 8a35f56c 2022-07-16 thomas free(a);
605 8a35f56c 2022-07-16 thomas return -1;
606 8a35f56c 2022-07-16 thomas }
607 8a35f56c 2022-07-16 thomas
608 8a35f56c 2022-07-16 thomas int
609 8a35f56c 2022-07-16 thomas sockets_accept_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
610 8a35f56c 2022-07-16 thomas int reserve, volatile int *counter)
611 8a35f56c 2022-07-16 thomas {
612 8a35f56c 2022-07-16 thomas int ret;
613 8a35f56c 2022-07-16 thomas
614 8a35f56c 2022-07-16 thomas if (getdtablecount() + reserve +
615 8a35f56c 2022-07-16 thomas ((*counter + 1) * FD_NEEDED) >= getdtablesize()) {
616 8a35f56c 2022-07-16 thomas log_debug("inflight fds exceeded");
617 8a35f56c 2022-07-16 thomas errno = EMFILE;
618 8a35f56c 2022-07-16 thomas return -1;
619 8a35f56c 2022-07-16 thomas }
620 8a35f56c 2022-07-16 thomas
621 8a35f56c 2022-07-16 thomas if ((ret = accept4(sockfd, addr, addrlen,
622 8a35f56c 2022-07-16 thomas SOCK_NONBLOCK | SOCK_CLOEXEC)) > -1) {
623 8a35f56c 2022-07-16 thomas (*counter)++;
624 8a35f56c 2022-07-16 thomas log_debug("inflight incremented, now %d", *counter);
625 8a35f56c 2022-07-16 thomas }
626 8a35f56c 2022-07-16 thomas
627 8a35f56c 2022-07-16 thomas return ret;
628 8a35f56c 2022-07-16 thomas }
629 8a35f56c 2022-07-16 thomas
630 8a35f56c 2022-07-16 thomas void
631 8a35f56c 2022-07-16 thomas sockets_accept_paused(int fd, short events, void *arg)
632 8a35f56c 2022-07-16 thomas {
633 8a35f56c 2022-07-16 thomas struct socket *sock = (struct socket *)arg;
634 8a35f56c 2022-07-16 thomas
635 8a35f56c 2022-07-16 thomas event_add(&sock->ev, NULL);
636 8a35f56c 2022-07-16 thomas }
637 8a35f56c 2022-07-16 thomas
638 8a35f56c 2022-07-16 thomas void
639 8a35f56c 2022-07-16 thomas sockets_socket_accept(int fd, short event, void *arg)
640 8a35f56c 2022-07-16 thomas {
641 8a35f56c 2022-07-16 thomas struct socket *sock = (struct socket *)arg;
642 8a35f56c 2022-07-16 thomas struct sockaddr_storage ss;
643 8a35f56c 2022-07-16 thomas struct timeval backoff;
644 8a35f56c 2022-07-16 thomas struct request *c = NULL;
645 8a35f56c 2022-07-16 thomas socklen_t len;
646 8a35f56c 2022-07-16 thomas int s;
647 8a35f56c 2022-07-16 thomas
648 8a35f56c 2022-07-16 thomas backoff.tv_sec = 1;
649 8a35f56c 2022-07-16 thomas backoff.tv_usec = 0;
650 8a35f56c 2022-07-16 thomas
651 8a35f56c 2022-07-16 thomas event_add(&sock->ev, NULL);
652 8a35f56c 2022-07-16 thomas if (event & EV_TIMEOUT)
653 8a35f56c 2022-07-16 thomas return;
654 8a35f56c 2022-07-16 thomas
655 8a35f56c 2022-07-16 thomas len = sizeof(ss);
656 8a35f56c 2022-07-16 thomas
657 8a35f56c 2022-07-16 thomas s = sockets_accept_reserve(fd, (struct sockaddr *)&ss, &len,
658 8a35f56c 2022-07-16 thomas FD_RESERVE, &cgi_inflight);
659 8a35f56c 2022-07-16 thomas
660 8a35f56c 2022-07-16 thomas if (s == -1) {
661 8a35f56c 2022-07-16 thomas switch (errno) {
662 8a35f56c 2022-07-16 thomas case EINTR:
663 8a35f56c 2022-07-16 thomas case EWOULDBLOCK:
664 8a35f56c 2022-07-16 thomas case ECONNABORTED:
665 8a35f56c 2022-07-16 thomas return;
666 8a35f56c 2022-07-16 thomas case EMFILE:
667 8a35f56c 2022-07-16 thomas case ENFILE:
668 8a35f56c 2022-07-16 thomas event_del(&sock->ev);
669 8a35f56c 2022-07-16 thomas evtimer_add(&sock->pause, &backoff);
670 8a35f56c 2022-07-16 thomas return;
671 8a35f56c 2022-07-16 thomas default:
672 8a35f56c 2022-07-16 thomas log_warn("%s: accept", __func__);
673 8a35f56c 2022-07-16 thomas }
674 8a35f56c 2022-07-16 thomas }
675 8a35f56c 2022-07-16 thomas
676 8a35f56c 2022-07-16 thomas if (client_cnt > GOTWEBD_MAXCLIENTS)
677 8a35f56c 2022-07-16 thomas goto err;
678 8a35f56c 2022-07-16 thomas
679 8a35f56c 2022-07-16 thomas c = calloc(1, sizeof(struct request));
680 8a35f56c 2022-07-16 thomas if (c == NULL) {
681 8a35f56c 2022-07-16 thomas log_warn("%s", __func__);
682 8a35f56c 2022-07-16 thomas close(s);
683 8a35f56c 2022-07-16 thomas cgi_inflight--;
684 8a35f56c 2022-07-16 thomas return;
685 8a35f56c 2022-07-16 thomas }
686 8a35f56c 2022-07-16 thomas
687 8a35f56c 2022-07-16 thomas c->fd = s;
688 8a35f56c 2022-07-16 thomas c->sock = sock;
689 8a35f56c 2022-07-16 thomas memcpy(c->priv_fd, sock->priv_fd, sizeof(c->priv_fd));
690 8a35f56c 2022-07-16 thomas c->buf_pos = 0;
691 8a35f56c 2022-07-16 thomas c->buf_len = 0;
692 8a35f56c 2022-07-16 thomas c->request_started = 0;
693 8a35f56c 2022-07-16 thomas c->sock->client_status = CLIENT_CONNECT;
694 8a35f56c 2022-07-16 thomas
695 8a35f56c 2022-07-16 thomas event_set(&c->ev, s, EV_READ, fcgi_request, c);
696 8a35f56c 2022-07-16 thomas event_add(&c->ev, NULL);
697 8a35f56c 2022-07-16 thomas
698 8a35f56c 2022-07-16 thomas evtimer_set(&c->tmo, fcgi_timeout, c);
699 8a35f56c 2022-07-16 thomas evtimer_add(&c->tmo, &timeout);
700 8a35f56c 2022-07-16 thomas
701 8a35f56c 2022-07-16 thomas client_cnt++;
702 8a35f56c 2022-07-16 thomas
703 8a35f56c 2022-07-16 thomas return;
704 8a35f56c 2022-07-16 thomas err:
705 8a35f56c 2022-07-16 thomas cgi_inflight--;
706 8a35f56c 2022-07-16 thomas close(s);
707 8a35f56c 2022-07-16 thomas if (c != NULL)
708 8a35f56c 2022-07-16 thomas free(c);
709 8a35f56c 2022-07-16 thomas }
710 8a35f56c 2022-07-16 thomas
711 8a35f56c 2022-07-16 thomas void
712 8a35f56c 2022-07-16 thomas sockets_rlimit(int maxfd)
713 8a35f56c 2022-07-16 thomas {
714 8a35f56c 2022-07-16 thomas struct rlimit rl;
715 8a35f56c 2022-07-16 thomas
716 8a35f56c 2022-07-16 thomas if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
717 8a35f56c 2022-07-16 thomas fatal("%s: failed to get resource limit", __func__);
718 8a35f56c 2022-07-16 thomas log_debug("%s: max open files %llu", __func__, rl.rlim_max);
719 8a35f56c 2022-07-16 thomas
720 8a35f56c 2022-07-16 thomas /*
721 8a35f56c 2022-07-16 thomas * Allow the maximum number of open file descriptors for this
722 8a35f56c 2022-07-16 thomas * login class (which should be the class "daemon" by default).
723 8a35f56c 2022-07-16 thomas */
724 8a35f56c 2022-07-16 thomas if (maxfd == -1)
725 8a35f56c 2022-07-16 thomas rl.rlim_cur = rl.rlim_max;
726 8a35f56c 2022-07-16 thomas else
727 8a35f56c 2022-07-16 thomas rl.rlim_cur = MAXIMUM(rl.rlim_max, (rlim_t)maxfd);
728 8a35f56c 2022-07-16 thomas if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
729 8a35f56c 2022-07-16 thomas fatal("%s: failed to set resource limit", __func__);
730 ff36aeea 2022-07-16 thomas }