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 a596b957 2022-07-14 tracey
57 a596b957 2022-07-14 tracey #include "proc.h"
58 a596b957 2022-07-14 tracey #include "gotwebd.h"
59 ed619ca0 2022-12-14 op #include "tmpl.h"
60 a596b957 2022-07-14 tracey
61 a596b957 2022-07-14 tracey #define SOCKS_BACKLOG 5
62 a596b957 2022-07-14 tracey #define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b))
63 a596b957 2022-07-14 tracey
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 026ac2c4 2022-08-20 stsp static void sockets_run(struct privsep *, struct privsep_proc *, 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 026ac2c4 2022-08-20 stsp static int sockets_dispatch_gotwebd(int, struct privsep_proc *,
77 026ac2c4 2022-08-20 stsp struct imsg *);
78 026ac2c4 2022-08-20 stsp static int sockets_unix_socket_listen(struct privsep *, struct socket *);
79 026ac2c4 2022-08-20 stsp static int sockets_create_socket(struct address *, in_port_t);
80 026ac2c4 2022-08-20 stsp static int sockets_accept_reserve(int, struct sockaddr *, socklen_t *,
81 026ac2c4 2022-08-20 stsp int, volatile int *);
82 a596b957 2022-07-14 tracey
83 026ac2c4 2022-08-20 stsp static struct socket *sockets_conf_new_socket_unix(struct gotwebd *,
84 026ac2c4 2022-08-20 stsp struct server *, int);
85 026ac2c4 2022-08-20 stsp static struct socket *sockets_conf_new_socket_fcgi(struct gotwebd *,
86 026ac2c4 2022-08-20 stsp struct server *, int, struct address *);
87 a596b957 2022-07-14 tracey
88 a596b957 2022-07-14 tracey int cgi_inflight = 0;
89 a596b957 2022-07-14 tracey
90 a596b957 2022-07-14 tracey static struct privsep_proc procs[] = {
91 a596b957 2022-07-14 tracey { "gotwebd", PROC_GOTWEBD, sockets_dispatch_gotwebd },
92 a596b957 2022-07-14 tracey };
93 a596b957 2022-07-14 tracey
94 a596b957 2022-07-14 tracey void
95 a596b957 2022-07-14 tracey sockets(struct privsep *ps, struct privsep_proc *p)
96 a596b957 2022-07-14 tracey {
97 a596b957 2022-07-14 tracey proc_run(ps, p, procs, nitems(procs), sockets_run, NULL);
98 a596b957 2022-07-14 tracey }
99 a596b957 2022-07-14 tracey
100 026ac2c4 2022-08-20 stsp static void
101 a596b957 2022-07-14 tracey sockets_run(struct privsep *ps, struct privsep_proc *p, void *arg)
102 a596b957 2022-07-14 tracey {
103 a596b957 2022-07-14 tracey if (config_init(ps->ps_env) == -1)
104 a596b957 2022-07-14 tracey fatal("failed to initialize configuration");
105 a596b957 2022-07-14 tracey
106 a596b957 2022-07-14 tracey p->p_shutdown = sockets_shutdown;
107 a596b957 2022-07-14 tracey
108 a596b957 2022-07-14 tracey sockets_rlimit(-1);
109 a596b957 2022-07-14 tracey
110 a596b957 2022-07-14 tracey signal_del(&ps->ps_evsigchld);
111 a596b957 2022-07-14 tracey signal_set(&ps->ps_evsigchld, SIGCHLD, sockets_sighdlr, ps);
112 a596b957 2022-07-14 tracey signal_add(&ps->ps_evsigchld, NULL);
113 a596b957 2022-07-14 tracey
114 a596b957 2022-07-14 tracey #ifndef PROFILE
115 a596b957 2022-07-14 tracey if (pledge("stdio rpath wpath cpath inet recvfd proc exec sendfd",
116 a596b957 2022-07-14 tracey NULL) == -1)
117 a596b957 2022-07-14 tracey fatal("pledge");
118 a596b957 2022-07-14 tracey #endif
119 a596b957 2022-07-14 tracey }
120 a596b957 2022-07-14 tracey
121 a596b957 2022-07-14 tracey void
122 a596b957 2022-07-14 tracey sockets_parse_sockets(struct gotwebd *env)
123 a596b957 2022-07-14 tracey {
124 a596b957 2022-07-14 tracey struct server *srv;
125 610dd8c9 2022-08-19 stsp struct address *a;
126 4fcc9f74 2022-08-16 stsp struct socket *new_sock = NULL;
127 610dd8c9 2022-08-19 stsp int sock_id = 1;
128 a596b957 2022-07-14 tracey
129 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(srv, &env->servers, entry) {
130 a596b957 2022-07-14 tracey if (srv->unix_socket) {
131 610dd8c9 2022-08-19 stsp new_sock = sockets_conf_new_socket_unix(env, srv,
132 610dd8c9 2022-08-19 stsp sock_id);
133 4fcc9f74 2022-08-16 stsp if (new_sock) {
134 610dd8c9 2022-08-19 stsp sock_id++;
135 4fcc9f74 2022-08-16 stsp TAILQ_INSERT_TAIL(&env->sockets, new_sock,
136 4fcc9f74 2022-08-16 stsp entry);
137 a596b957 2022-07-14 tracey }
138 610dd8c9 2022-08-19 stsp }
139 a596b957 2022-07-14 tracey
140 610dd8c9 2022-08-19 stsp if (srv->fcgi_socket) {
141 610dd8c9 2022-08-19 stsp if (TAILQ_EMPTY(&srv->al)) {
142 610dd8c9 2022-08-19 stsp fatalx("%s: server %s has no IP addresses to "
143 610dd8c9 2022-08-19 stsp "listen for FCGI connections", __func__,
144 610dd8c9 2022-08-19 stsp srv->name);
145 4fcc9f74 2022-08-16 stsp }
146 610dd8c9 2022-08-19 stsp TAILQ_FOREACH(a, &srv->al, entry) {
147 610dd8c9 2022-08-19 stsp if (a->ss.ss_family != AF_INET &&
148 610dd8c9 2022-08-19 stsp a->ss.ss_family != AF_INET6)
149 610dd8c9 2022-08-19 stsp continue;
150 610dd8c9 2022-08-19 stsp new_sock = sockets_conf_new_socket_fcgi(env,
151 610dd8c9 2022-08-19 stsp srv, sock_id, a);
152 610dd8c9 2022-08-19 stsp if (new_sock) {
153 610dd8c9 2022-08-19 stsp sock_id++;
154 610dd8c9 2022-08-19 stsp TAILQ_INSERT_TAIL(&env->sockets,
155 610dd8c9 2022-08-19 stsp new_sock, entry);
156 610dd8c9 2022-08-19 stsp }
157 a596b957 2022-07-14 tracey }
158 a596b957 2022-07-14 tracey }
159 a596b957 2022-07-14 tracey }
160 a596b957 2022-07-14 tracey }
161 a596b957 2022-07-14 tracey
162 026ac2c4 2022-08-20 stsp static struct socket *
163 610dd8c9 2022-08-19 stsp sockets_conf_new_socket_unix(struct gotwebd *env, struct server *srv, int id)
164 a596b957 2022-07-14 tracey {
165 a596b957 2022-07-14 tracey struct socket *sock;
166 a596b957 2022-07-14 tracey int n;
167 a596b957 2022-07-14 tracey
168 a596b957 2022-07-14 tracey if ((sock = calloc(1, sizeof(*sock))) == NULL)
169 a596b957 2022-07-14 tracey fatalx("%s: calloc", __func__);
170 a596b957 2022-07-14 tracey
171 a596b957 2022-07-14 tracey sock->conf.id = id;
172 a596b957 2022-07-14 tracey sock->fd = -1;
173 610dd8c9 2022-08-19 stsp sock->conf.af_type = AF_UNIX;
174 a596b957 2022-07-14 tracey
175 610dd8c9 2022-08-19 stsp if (strlcpy(sock->conf.unix_socket_name,
176 610dd8c9 2022-08-19 stsp srv->unix_socket_name,
177 610dd8c9 2022-08-19 stsp sizeof(sock->conf.unix_socket_name)) >=
178 610dd8c9 2022-08-19 stsp sizeof(sock->conf.unix_socket_name)) {
179 610dd8c9 2022-08-19 stsp free(sock);
180 610dd8c9 2022-08-19 stsp fatalx("%s: strlcpy", __func__);
181 4fcc9f74 2022-08-16 stsp }
182 a596b957 2022-07-14 tracey
183 a596b957 2022-07-14 tracey n = snprintf(sock->conf.name, GOTWEBD_MAXTEXT, "%s_parent",
184 a596b957 2022-07-14 tracey srv->name);
185 438d0cc3 2022-08-16 op if (n < 0 || (size_t)n >= GOTWEBD_MAXTEXT) {
186 a596b957 2022-07-14 tracey free(sock);
187 a596b957 2022-07-14 tracey fatalx("%s: snprintf", __func__);
188 a596b957 2022-07-14 tracey }
189 a596b957 2022-07-14 tracey
190 a596b957 2022-07-14 tracey if (strlcpy(sock->conf.srv_name, srv->name,
191 a596b957 2022-07-14 tracey sizeof(sock->conf.srv_name)) >= sizeof(sock->conf.srv_name)) {
192 a596b957 2022-07-14 tracey free(sock);
193 a596b957 2022-07-14 tracey fatalx("%s: strlcpy", __func__);
194 a596b957 2022-07-14 tracey }
195 a596b957 2022-07-14 tracey
196 610dd8c9 2022-08-19 stsp return sock;
197 610dd8c9 2022-08-19 stsp }
198 4fcc9f74 2022-08-16 stsp
199 026ac2c4 2022-08-20 stsp static struct socket *
200 610dd8c9 2022-08-19 stsp sockets_conf_new_socket_fcgi(struct gotwebd *env, struct server *srv, int id,
201 610dd8c9 2022-08-19 stsp struct address *a)
202 610dd8c9 2022-08-19 stsp {
203 610dd8c9 2022-08-19 stsp struct socket *sock;
204 610dd8c9 2022-08-19 stsp struct address *acp;
205 610dd8c9 2022-08-19 stsp int n;
206 a596b957 2022-07-14 tracey
207 610dd8c9 2022-08-19 stsp if ((sock = calloc(1, sizeof(*sock))) == NULL)
208 610dd8c9 2022-08-19 stsp fatalx("%s: calloc", __func__);
209 4fcc9f74 2022-08-16 stsp
210 610dd8c9 2022-08-19 stsp sock->conf.id = id;
211 610dd8c9 2022-08-19 stsp sock->fd = -1;
212 610dd8c9 2022-08-19 stsp sock->conf.af_type = a->ss.ss_family;
213 610dd8c9 2022-08-19 stsp
214 67d8de2a 2022-08-29 stsp sock->conf.fcgi_socket_port = a->port;
215 610dd8c9 2022-08-19 stsp
216 610dd8c9 2022-08-19 stsp n = snprintf(sock->conf.name, GOTWEBD_MAXTEXT, "%s_parent",
217 610dd8c9 2022-08-19 stsp srv->name);
218 610dd8c9 2022-08-19 stsp if (n < 0 || (size_t)n >= GOTWEBD_MAXTEXT) {
219 4fcc9f74 2022-08-16 stsp free(sock);
220 610dd8c9 2022-08-19 stsp fatalx("%s: snprintf", __func__);
221 4fcc9f74 2022-08-16 stsp }
222 4fcc9f74 2022-08-16 stsp
223 610dd8c9 2022-08-19 stsp if (strlcpy(sock->conf.srv_name, srv->name,
224 610dd8c9 2022-08-19 stsp sizeof(sock->conf.srv_name)) >= sizeof(sock->conf.srv_name)) {
225 610dd8c9 2022-08-19 stsp free(sock);
226 610dd8c9 2022-08-19 stsp fatalx("%s: strlcpy", __func__);
227 610dd8c9 2022-08-19 stsp }
228 610dd8c9 2022-08-19 stsp
229 610dd8c9 2022-08-19 stsp acp = &sock->conf.addr;
230 610dd8c9 2022-08-19 stsp
231 610dd8c9 2022-08-19 stsp memcpy(&acp->ss, &a->ss, sizeof(acp->ss));
232 610dd8c9 2022-08-19 stsp acp->ipproto = a->ipproto;
233 610dd8c9 2022-08-19 stsp acp->prefixlen = a->prefixlen;
234 610dd8c9 2022-08-19 stsp acp->port = a->port;
235 610dd8c9 2022-08-19 stsp if (strlen(a->ifname) != 0) {
236 610dd8c9 2022-08-19 stsp if (strlcpy(acp->ifname, a->ifname,
237 610dd8c9 2022-08-19 stsp sizeof(acp->ifname)) >= sizeof(acp->ifname)) {
238 610dd8c9 2022-08-19 stsp fatalx("%s: interface name truncated",
239 610dd8c9 2022-08-19 stsp __func__);
240 610dd8c9 2022-08-19 stsp }
241 610dd8c9 2022-08-19 stsp }
242 610dd8c9 2022-08-19 stsp
243 a596b957 2022-07-14 tracey return (sock);
244 a596b957 2022-07-14 tracey }
245 a596b957 2022-07-14 tracey
246 026ac2c4 2022-08-20 stsp static void
247 a596b957 2022-07-14 tracey sockets_launch(void)
248 a596b957 2022-07-14 tracey {
249 a596b957 2022-07-14 tracey struct socket *sock;
250 a596b957 2022-07-14 tracey
251 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(sock, &gotwebd_env->sockets, entry) {
252 a596b957 2022-07-14 tracey log_debug("%s: configuring socket %d (%d)", __func__,
253 a596b957 2022-07-14 tracey sock->conf.id, sock->fd);
254 a596b957 2022-07-14 tracey
255 a596b957 2022-07-14 tracey event_set(&sock->ev, sock->fd, EV_READ | EV_PERSIST,
256 a596b957 2022-07-14 tracey sockets_socket_accept, sock);
257 a596b957 2022-07-14 tracey
258 a596b957 2022-07-14 tracey if (event_add(&sock->ev, NULL))
259 a596b957 2022-07-14 tracey fatalx("event add sock");
260 a596b957 2022-07-14 tracey
261 a596b957 2022-07-14 tracey evtimer_set(&sock->pause, sockets_accept_paused, sock);
262 a596b957 2022-07-14 tracey
263 a596b957 2022-07-14 tracey log_debug("%s: running socket listener %d", __func__,
264 a596b957 2022-07-14 tracey sock->conf.id);
265 a596b957 2022-07-14 tracey }
266 a596b957 2022-07-14 tracey }
267 a596b957 2022-07-14 tracey
268 026ac2c4 2022-08-20 stsp static void
269 a596b957 2022-07-14 tracey sockets_purge(struct gotwebd *env)
270 a596b957 2022-07-14 tracey {
271 a596b957 2022-07-14 tracey struct socket *sock, *tsock;
272 a596b957 2022-07-14 tracey
273 a596b957 2022-07-14 tracey /* shutdown and remove sockets */
274 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH_SAFE(sock, &env->sockets, entry, tsock) {
275 a596b957 2022-07-14 tracey if (event_initialized(&sock->ev))
276 a596b957 2022-07-14 tracey event_del(&sock->ev);
277 a596b957 2022-07-14 tracey if (evtimer_initialized(&sock->evt))
278 a596b957 2022-07-14 tracey evtimer_del(&sock->evt);
279 a596b957 2022-07-14 tracey if (evtimer_initialized(&sock->pause))
280 a596b957 2022-07-14 tracey evtimer_del(&sock->pause);
281 a596b957 2022-07-14 tracey if (sock->fd != -1)
282 a596b957 2022-07-14 tracey close(sock->fd);
283 2ad48e9a 2022-08-16 stsp TAILQ_REMOVE(&env->sockets, sock, entry);
284 a596b957 2022-07-14 tracey }
285 a596b957 2022-07-14 tracey }
286 a596b957 2022-07-14 tracey
287 026ac2c4 2022-08-20 stsp static int
288 a596b957 2022-07-14 tracey sockets_dispatch_gotwebd(int fd, struct privsep_proc *p, struct imsg *imsg)
289 a596b957 2022-07-14 tracey {
290 a596b957 2022-07-14 tracey struct privsep *ps = p->p_ps;
291 a596b957 2022-07-14 tracey int res = 0, cmd = 0, verbose;
292 a596b957 2022-07-14 tracey
293 a596b957 2022-07-14 tracey switch (imsg->hdr.type) {
294 a596b957 2022-07-14 tracey case IMSG_CFG_SRV:
295 a596b957 2022-07-14 tracey config_getserver(gotwebd_env, imsg);
296 a596b957 2022-07-14 tracey break;
297 a596b957 2022-07-14 tracey case IMSG_CFG_SOCK:
298 a596b957 2022-07-14 tracey config_getsock(gotwebd_env, imsg);
299 a596b957 2022-07-14 tracey break;
300 a596b957 2022-07-14 tracey case IMSG_CFG_FD:
301 a596b957 2022-07-14 tracey config_getfd(gotwebd_env, imsg);
302 a596b957 2022-07-14 tracey break;
303 a596b957 2022-07-14 tracey case IMSG_CFG_DONE:
304 a596b957 2022-07-14 tracey config_getcfg(gotwebd_env, imsg);
305 a596b957 2022-07-14 tracey break;
306 a596b957 2022-07-14 tracey case IMSG_CTL_START:
307 a596b957 2022-07-14 tracey sockets_launch();
308 a596b957 2022-07-14 tracey break;
309 a596b957 2022-07-14 tracey case IMSG_CTL_VERBOSE:
310 a596b957 2022-07-14 tracey IMSG_SIZE_CHECK(imsg, &verbose);
311 a596b957 2022-07-14 tracey memcpy(&verbose, imsg->data, sizeof(verbose));
312 a596b957 2022-07-14 tracey log_setverbose(verbose);
313 a596b957 2022-07-14 tracey break;
314 a596b957 2022-07-14 tracey default:
315 a596b957 2022-07-14 tracey return -1;
316 a596b957 2022-07-14 tracey }
317 a596b957 2022-07-14 tracey
318 a596b957 2022-07-14 tracey switch (cmd) {
319 a596b957 2022-07-14 tracey case 0:
320 a596b957 2022-07-14 tracey break;
321 a596b957 2022-07-14 tracey default:
322 a596b957 2022-07-14 tracey if (proc_compose_imsg(ps, PROC_GOTWEBD, -1, cmd,
323 a596b957 2022-07-14 tracey imsg->hdr.peerid, -1, &res, sizeof(res)) == -1)
324 a596b957 2022-07-14 tracey return -1;
325 a596b957 2022-07-14 tracey break;
326 a596b957 2022-07-14 tracey }
327 a596b957 2022-07-14 tracey
328 a596b957 2022-07-14 tracey return 0;
329 a596b957 2022-07-14 tracey }
330 a596b957 2022-07-14 tracey
331 026ac2c4 2022-08-20 stsp static void
332 a596b957 2022-07-14 tracey sockets_sighdlr(int sig, short event, void *arg)
333 a596b957 2022-07-14 tracey {
334 a596b957 2022-07-14 tracey switch (sig) {
335 a596b957 2022-07-14 tracey case SIGHUP:
336 a596b957 2022-07-14 tracey log_info("%s: ignoring SIGHUP", __func__);
337 a596b957 2022-07-14 tracey break;
338 a596b957 2022-07-14 tracey case SIGPIPE:
339 a596b957 2022-07-14 tracey log_info("%s: ignoring SIGPIPE", __func__);
340 a596b957 2022-07-14 tracey break;
341 a596b957 2022-07-14 tracey case SIGUSR1:
342 a596b957 2022-07-14 tracey log_info("%s: ignoring SIGUSR1", __func__);
343 a596b957 2022-07-14 tracey break;
344 a596b957 2022-07-14 tracey case SIGCHLD:
345 a596b957 2022-07-14 tracey break;
346 a596b957 2022-07-14 tracey default:
347 a596b957 2022-07-14 tracey log_info("SIGNAL: %d", sig);
348 a596b957 2022-07-14 tracey fatalx("unexpected signal");
349 a596b957 2022-07-14 tracey }
350 a596b957 2022-07-14 tracey }
351 a596b957 2022-07-14 tracey
352 a596b957 2022-07-14 tracey void
353 a596b957 2022-07-14 tracey sockets_shutdown(void)
354 a596b957 2022-07-14 tracey {
355 a596b957 2022-07-14 tracey struct server *srv, *tsrv;
356 a596b957 2022-07-14 tracey struct socket *sock, *tsock;
357 b5c757f5 2022-09-01 stsp int i;
358 a596b957 2022-07-14 tracey
359 a596b957 2022-07-14 tracey sockets_purge(gotwebd_env);
360 a596b957 2022-07-14 tracey
361 a596b957 2022-07-14 tracey /* clean sockets */
362 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH_SAFE(sock, &gotwebd_env->sockets, entry, tsock) {
363 2ad48e9a 2022-08-16 stsp TAILQ_REMOVE(&gotwebd_env->sockets, sock, entry);
364 a596b957 2022-07-14 tracey close(sock->fd);
365 a596b957 2022-07-14 tracey free(sock);
366 a596b957 2022-07-14 tracey }
367 a596b957 2022-07-14 tracey
368 a596b957 2022-07-14 tracey /* clean servers */
369 b5c757f5 2022-09-01 stsp TAILQ_FOREACH_SAFE(srv, &gotwebd_env->servers, entry, tsrv) {
370 b5c757f5 2022-09-01 stsp for (i = 0; i < srv->ncached_repos; i++)
371 b5c757f5 2022-09-01 stsp got_repo_close(srv->cached_repos[i].repo);
372 a596b957 2022-07-14 tracey free(srv);
373 b5c757f5 2022-09-01 stsp }
374 a596b957 2022-07-14 tracey
375 a596b957 2022-07-14 tracey free(gotwebd_env);
376 a596b957 2022-07-14 tracey }
377 a596b957 2022-07-14 tracey
378 a596b957 2022-07-14 tracey int
379 a596b957 2022-07-14 tracey sockets_privinit(struct gotwebd *env, struct socket *sock)
380 a596b957 2022-07-14 tracey {
381 a596b957 2022-07-14 tracey struct privsep *ps = env->gotwebd_ps;
382 a596b957 2022-07-14 tracey
383 4fcc9f74 2022-08-16 stsp if (sock->conf.af_type == AF_UNIX) {
384 a596b957 2022-07-14 tracey log_debug("%s: initializing unix socket %s", __func__,
385 a596b957 2022-07-14 tracey sock->conf.unix_socket_name);
386 a596b957 2022-07-14 tracey sock->fd = sockets_unix_socket_listen(ps, sock);
387 a596b957 2022-07-14 tracey if (sock->fd == -1) {
388 a596b957 2022-07-14 tracey log_warnx("%s: create unix socket failed", __func__);
389 a596b957 2022-07-14 tracey return -1;
390 a596b957 2022-07-14 tracey }
391 a596b957 2022-07-14 tracey }
392 a596b957 2022-07-14 tracey
393 4fcc9f74 2022-08-16 stsp if (sock->conf.af_type == AF_INET || sock->conf.af_type == AF_INET6) {
394 4fcc9f74 2022-08-16 stsp log_debug("%s: initializing %s FCGI socket on port %d for %s",
395 4fcc9f74 2022-08-16 stsp __func__, sock->conf.af_type == AF_INET ? "inet" : "inet6",
396 4fcc9f74 2022-08-16 stsp sock->conf.fcgi_socket_port, sock->conf.name);
397 610dd8c9 2022-08-19 stsp sock->fd = sockets_create_socket(&sock->conf.addr,
398 a596b957 2022-07-14 tracey sock->conf.fcgi_socket_port);
399 a596b957 2022-07-14 tracey if (sock->fd == -1) {
400 610dd8c9 2022-08-19 stsp log_warnx("%s: create FCGI socket failed", __func__);
401 a596b957 2022-07-14 tracey return -1;
402 a596b957 2022-07-14 tracey }
403 a596b957 2022-07-14 tracey }
404 a596b957 2022-07-14 tracey
405 a596b957 2022-07-14 tracey return 0;
406 a596b957 2022-07-14 tracey }
407 a596b957 2022-07-14 tracey
408 026ac2c4 2022-08-20 stsp static int
409 a596b957 2022-07-14 tracey sockets_unix_socket_listen(struct privsep *ps, struct socket *sock)
410 a596b957 2022-07-14 tracey {
411 a596b957 2022-07-14 tracey struct gotwebd *env = ps->ps_env;
412 a596b957 2022-07-14 tracey struct sockaddr_un sun;
413 a596b957 2022-07-14 tracey struct socket *tsock;
414 a596b957 2022-07-14 tracey int u_fd = -1;
415 a596b957 2022-07-14 tracey mode_t old_umask, mode;
416 a596b957 2022-07-14 tracey
417 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(tsock, &env->sockets, entry) {
418 a596b957 2022-07-14 tracey if (strcmp(tsock->conf.unix_socket_name,
419 a596b957 2022-07-14 tracey sock->conf.unix_socket_name) == 0 &&
420 a596b957 2022-07-14 tracey tsock->fd != -1)
421 a596b957 2022-07-14 tracey return (tsock->fd);
422 a596b957 2022-07-14 tracey }
423 a596b957 2022-07-14 tracey
424 a596b957 2022-07-14 tracey u_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
425 a596b957 2022-07-14 tracey if (u_fd == -1) {
426 a596b957 2022-07-14 tracey log_warn("%s: socket", __func__);
427 a596b957 2022-07-14 tracey return -1;
428 a596b957 2022-07-14 tracey }
429 a596b957 2022-07-14 tracey
430 a596b957 2022-07-14 tracey sun.sun_family = AF_UNIX;
431 a596b957 2022-07-14 tracey if (strlcpy(sun.sun_path, sock->conf.unix_socket_name,
432 a596b957 2022-07-14 tracey sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
433 a596b957 2022-07-14 tracey log_warn("%s: %s name too long", __func__,
434 a596b957 2022-07-14 tracey sock->conf.unix_socket_name);
435 a596b957 2022-07-14 tracey close(u_fd);
436 a596b957 2022-07-14 tracey return -1;
437 a596b957 2022-07-14 tracey }
438 a596b957 2022-07-14 tracey
439 a596b957 2022-07-14 tracey if (unlink(sock->conf.unix_socket_name) == -1) {
440 a596b957 2022-07-14 tracey if (errno != ENOENT) {
441 a596b957 2022-07-14 tracey log_warn("%s: unlink %s", __func__,
442 a596b957 2022-07-14 tracey sock->conf.unix_socket_name);
443 a596b957 2022-07-14 tracey close(u_fd);
444 a596b957 2022-07-14 tracey return -1;
445 a596b957 2022-07-14 tracey }
446 a596b957 2022-07-14 tracey }
447 a596b957 2022-07-14 tracey
448 a596b957 2022-07-14 tracey old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
449 a596b957 2022-07-14 tracey mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
450 a596b957 2022-07-14 tracey
451 a596b957 2022-07-14 tracey if (bind(u_fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
452 a596b957 2022-07-14 tracey log_warn("%s: bind: %s", __func__, sock->conf.unix_socket_name);
453 a596b957 2022-07-14 tracey close(u_fd);
454 a596b957 2022-07-14 tracey (void)umask(old_umask);
455 a596b957 2022-07-14 tracey return -1;
456 a596b957 2022-07-14 tracey }
457 a596b957 2022-07-14 tracey
458 a596b957 2022-07-14 tracey (void)umask(old_umask);
459 a596b957 2022-07-14 tracey
460 a596b957 2022-07-14 tracey if (chmod(sock->conf.unix_socket_name, mode) == -1) {
461 a596b957 2022-07-14 tracey log_warn("%s: chmod", __func__);
462 a596b957 2022-07-14 tracey close(u_fd);
463 a596b957 2022-07-14 tracey (void)unlink(sock->conf.unix_socket_name);
464 a596b957 2022-07-14 tracey return -1;
465 a596b957 2022-07-14 tracey }
466 a596b957 2022-07-14 tracey
467 a596b957 2022-07-14 tracey if (chown(sock->conf.unix_socket_name, ps->ps_pw->pw_uid,
468 a596b957 2022-07-14 tracey ps->ps_pw->pw_gid) == -1) {
469 a596b957 2022-07-14 tracey log_warn("%s: chown", __func__);
470 a596b957 2022-07-14 tracey close(u_fd);
471 a596b957 2022-07-14 tracey (void)unlink(sock->conf.unix_socket_name);
472 a596b957 2022-07-14 tracey return -1;
473 a596b957 2022-07-14 tracey }
474 a596b957 2022-07-14 tracey
475 a596b957 2022-07-14 tracey if (listen(u_fd, SOCKS_BACKLOG) == -1) {
476 a596b957 2022-07-14 tracey log_warn("%s: listen", __func__);
477 a596b957 2022-07-14 tracey return -1;
478 a596b957 2022-07-14 tracey }
479 a596b957 2022-07-14 tracey
480 a596b957 2022-07-14 tracey return u_fd;
481 a596b957 2022-07-14 tracey }
482 a596b957 2022-07-14 tracey
483 026ac2c4 2022-08-20 stsp static int
484 610dd8c9 2022-08-19 stsp sockets_create_socket(struct address *a, in_port_t port)
485 a596b957 2022-07-14 tracey {
486 a596b957 2022-07-14 tracey struct addrinfo hints;
487 a596b957 2022-07-14 tracey int fd = -1, o_val = 1, flags;
488 a596b957 2022-07-14 tracey
489 a596b957 2022-07-14 tracey memset(&hints, 0, sizeof(hints));
490 a596b957 2022-07-14 tracey hints.ai_family = AF_UNSPEC;
491 a596b957 2022-07-14 tracey hints.ai_socktype = SOCK_STREAM;
492 a596b957 2022-07-14 tracey hints.ai_flags |= AI_PASSIVE;
493 a596b957 2022-07-14 tracey
494 610dd8c9 2022-08-19 stsp switch (a->ss.ss_family) {
495 610dd8c9 2022-08-19 stsp case AF_INET:
496 859aa9f4 2022-08-19 stsp ((struct sockaddr_in *)(&a->ss))->sin_port = htons(port);
497 610dd8c9 2022-08-19 stsp break;
498 610dd8c9 2022-08-19 stsp case AF_INET6:
499 859aa9f4 2022-08-19 stsp ((struct sockaddr_in6 *)(&a->ss))->sin6_port = htons(port);
500 610dd8c9 2022-08-19 stsp break;
501 610dd8c9 2022-08-19 stsp default:
502 610dd8c9 2022-08-19 stsp log_warnx("%s: unknown address family", __func__);
503 610dd8c9 2022-08-19 stsp return -1;
504 610dd8c9 2022-08-19 stsp }
505 a596b957 2022-07-14 tracey
506 610dd8c9 2022-08-19 stsp fd = socket(a->ss.ss_family, hints.ai_socktype, a->ipproto);
507 610dd8c9 2022-08-19 stsp if (fd == -1)
508 610dd8c9 2022-08-19 stsp return -1;
509 a596b957 2022-07-14 tracey
510 610dd8c9 2022-08-19 stsp log_debug("%s: opened socket (%d) for %s", __func__,
511 610dd8c9 2022-08-19 stsp fd, a->ifname);
512 a596b957 2022-07-14 tracey
513 610dd8c9 2022-08-19 stsp if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &o_val,
514 610dd8c9 2022-08-19 stsp sizeof(int)) == -1) {
515 610dd8c9 2022-08-19 stsp log_warn("%s: setsockopt error", __func__);
516 610dd8c9 2022-08-19 stsp close(fd);
517 610dd8c9 2022-08-19 stsp return -1;
518 610dd8c9 2022-08-19 stsp }
519 a596b957 2022-07-14 tracey
520 610dd8c9 2022-08-19 stsp /* non-blocking */
521 610dd8c9 2022-08-19 stsp flags = fcntl(fd, F_GETFL);
522 610dd8c9 2022-08-19 stsp flags |= O_NONBLOCK;
523 610dd8c9 2022-08-19 stsp if (fcntl(fd, F_SETFL, flags) == -1) {
524 610dd8c9 2022-08-19 stsp log_info("%s: could not enable non-blocking I/O", __func__);
525 610dd8c9 2022-08-19 stsp close(fd);
526 610dd8c9 2022-08-19 stsp return -1;
527 610dd8c9 2022-08-19 stsp }
528 a596b957 2022-07-14 tracey
529 610dd8c9 2022-08-19 stsp if (bind(fd, (struct sockaddr *)&a->ss, a->ss.ss_len) == -1) {
530 610dd8c9 2022-08-19 stsp close(fd);
531 610dd8c9 2022-08-19 stsp log_info("%s: can't bind to port %d", __func__,
532 610dd8c9 2022-08-19 stsp ntohs(port));
533 610dd8c9 2022-08-19 stsp return -1;
534 a596b957 2022-07-14 tracey }
535 a596b957 2022-07-14 tracey
536 610dd8c9 2022-08-19 stsp if (listen(fd, SOMAXCONN) == -1) {
537 610dd8c9 2022-08-19 stsp log_warn("%s, unable to listen on socket", __func__);
538 610dd8c9 2022-08-19 stsp close(fd);
539 610dd8c9 2022-08-19 stsp return -1;
540 610dd8c9 2022-08-19 stsp }
541 610dd8c9 2022-08-19 stsp
542 a596b957 2022-07-14 tracey return (fd);
543 a596b957 2022-07-14 tracey }
544 a596b957 2022-07-14 tracey
545 026ac2c4 2022-08-20 stsp static int
546 a596b957 2022-07-14 tracey sockets_accept_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
547 a596b957 2022-07-14 tracey int reserve, volatile int *counter)
548 a596b957 2022-07-14 tracey {
549 a596b957 2022-07-14 tracey int ret;
550 a596b957 2022-07-14 tracey
551 a596b957 2022-07-14 tracey if (getdtablecount() + reserve +
552 a596b957 2022-07-14 tracey ((*counter + 1) * FD_NEEDED) >= getdtablesize()) {
553 a596b957 2022-07-14 tracey log_debug("inflight fds exceeded");
554 a596b957 2022-07-14 tracey errno = EMFILE;
555 a596b957 2022-07-14 tracey return -1;
556 a596b957 2022-07-14 tracey }
557 a596b957 2022-07-14 tracey
558 a596b957 2022-07-14 tracey if ((ret = accept4(sockfd, addr, addrlen,
559 a596b957 2022-07-14 tracey SOCK_NONBLOCK | SOCK_CLOEXEC)) > -1) {
560 a596b957 2022-07-14 tracey (*counter)++;
561 a596b957 2022-07-14 tracey log_debug("inflight incremented, now %d", *counter);
562 a596b957 2022-07-14 tracey }
563 a596b957 2022-07-14 tracey
564 a596b957 2022-07-14 tracey return ret;
565 a596b957 2022-07-14 tracey }
566 a596b957 2022-07-14 tracey
567 026ac2c4 2022-08-20 stsp static void
568 a596b957 2022-07-14 tracey sockets_accept_paused(int fd, short events, void *arg)
569 a596b957 2022-07-14 tracey {
570 a596b957 2022-07-14 tracey struct socket *sock = (struct socket *)arg;
571 a596b957 2022-07-14 tracey
572 a596b957 2022-07-14 tracey event_add(&sock->ev, NULL);
573 a596b957 2022-07-14 tracey }
574 a596b957 2022-07-14 tracey
575 a596b957 2022-07-14 tracey void
576 a596b957 2022-07-14 tracey sockets_socket_accept(int fd, short event, void *arg)
577 a596b957 2022-07-14 tracey {
578 a596b957 2022-07-14 tracey struct socket *sock = (struct socket *)arg;
579 a596b957 2022-07-14 tracey struct sockaddr_storage ss;
580 a596b957 2022-07-14 tracey struct timeval backoff;
581 a596b957 2022-07-14 tracey struct request *c = NULL;
582 a596b957 2022-07-14 tracey socklen_t len;
583 a596b957 2022-07-14 tracey int s;
584 a596b957 2022-07-14 tracey
585 a596b957 2022-07-14 tracey backoff.tv_sec = 1;
586 a596b957 2022-07-14 tracey backoff.tv_usec = 0;
587 a596b957 2022-07-14 tracey
588 a596b957 2022-07-14 tracey event_add(&sock->ev, NULL);
589 a596b957 2022-07-14 tracey if (event & EV_TIMEOUT)
590 a596b957 2022-07-14 tracey return;
591 a596b957 2022-07-14 tracey
592 a596b957 2022-07-14 tracey len = sizeof(ss);
593 a596b957 2022-07-14 tracey
594 a596b957 2022-07-14 tracey s = sockets_accept_reserve(fd, (struct sockaddr *)&ss, &len,
595 a596b957 2022-07-14 tracey FD_RESERVE, &cgi_inflight);
596 a596b957 2022-07-14 tracey
597 a596b957 2022-07-14 tracey if (s == -1) {
598 a596b957 2022-07-14 tracey switch (errno) {
599 a596b957 2022-07-14 tracey case EINTR:
600 a596b957 2022-07-14 tracey case EWOULDBLOCK:
601 a596b957 2022-07-14 tracey case ECONNABORTED:
602 a596b957 2022-07-14 tracey return;
603 a596b957 2022-07-14 tracey case EMFILE:
604 a596b957 2022-07-14 tracey case ENFILE:
605 a596b957 2022-07-14 tracey event_del(&sock->ev);
606 a596b957 2022-07-14 tracey evtimer_add(&sock->pause, &backoff);
607 a596b957 2022-07-14 tracey return;
608 a596b957 2022-07-14 tracey default:
609 a596b957 2022-07-14 tracey log_warn("%s: accept", __func__);
610 a596b957 2022-07-14 tracey }
611 a596b957 2022-07-14 tracey }
612 a596b957 2022-07-14 tracey
613 a596b957 2022-07-14 tracey if (client_cnt > GOTWEBD_MAXCLIENTS)
614 a596b957 2022-07-14 tracey goto err;
615 a596b957 2022-07-14 tracey
616 a596b957 2022-07-14 tracey c = calloc(1, sizeof(struct request));
617 a596b957 2022-07-14 tracey if (c == NULL) {
618 ed619ca0 2022-12-14 op log_warn("%s", __func__);
619 ed619ca0 2022-12-14 op close(s);
620 ed619ca0 2022-12-14 op cgi_inflight--;
621 ed619ca0 2022-12-14 op return;
622 ed619ca0 2022-12-14 op }
623 ed619ca0 2022-12-14 op
624 ed619ca0 2022-12-14 op c->tp = template(c, fcgi_puts, fcgi_putc);
625 ed619ca0 2022-12-14 op if (c->tp == NULL) {
626 a596b957 2022-07-14 tracey log_warn("%s", __func__);
627 a596b957 2022-07-14 tracey close(s);
628 a596b957 2022-07-14 tracey cgi_inflight--;
629 ed619ca0 2022-12-14 op free(c);
630 a596b957 2022-07-14 tracey return;
631 a596b957 2022-07-14 tracey }
632 a596b957 2022-07-14 tracey
633 a596b957 2022-07-14 tracey c->fd = s;
634 a596b957 2022-07-14 tracey c->sock = sock;
635 a596b957 2022-07-14 tracey memcpy(c->priv_fd, sock->priv_fd, sizeof(c->priv_fd));
636 a596b957 2022-07-14 tracey c->buf_pos = 0;
637 a596b957 2022-07-14 tracey c->buf_len = 0;
638 a596b957 2022-07-14 tracey c->request_started = 0;
639 a596b957 2022-07-14 tracey c->sock->client_status = CLIENT_CONNECT;
640 a596b957 2022-07-14 tracey
641 5add7f42 2023-03-10 op event_set(&c->ev, s, EV_READ|EV_PERSIST, fcgi_request, c);
642 a596b957 2022-07-14 tracey event_add(&c->ev, NULL);
643 a596b957 2022-07-14 tracey
644 a596b957 2022-07-14 tracey evtimer_set(&c->tmo, fcgi_timeout, c);
645 a596b957 2022-07-14 tracey evtimer_add(&c->tmo, &timeout);
646 a596b957 2022-07-14 tracey
647 a596b957 2022-07-14 tracey client_cnt++;
648 a596b957 2022-07-14 tracey
649 a596b957 2022-07-14 tracey return;
650 a596b957 2022-07-14 tracey err:
651 a596b957 2022-07-14 tracey cgi_inflight--;
652 a596b957 2022-07-14 tracey close(s);
653 a596b957 2022-07-14 tracey if (c != NULL)
654 a596b957 2022-07-14 tracey free(c);
655 a596b957 2022-07-14 tracey }
656 a596b957 2022-07-14 tracey
657 026ac2c4 2022-08-20 stsp static void
658 a596b957 2022-07-14 tracey sockets_rlimit(int maxfd)
659 a596b957 2022-07-14 tracey {
660 a596b957 2022-07-14 tracey struct rlimit rl;
661 a596b957 2022-07-14 tracey
662 a596b957 2022-07-14 tracey if (getrlimit(RLIMIT_NOFILE, &rl) == -1)
663 a596b957 2022-07-14 tracey fatal("%s: failed to get resource limit", __func__);
664 d6267a62 2022-07-17 naddy log_debug("%s: max open files %llu", __func__,
665 d6267a62 2022-07-17 naddy (unsigned long long)rl.rlim_max);
666 a596b957 2022-07-14 tracey
667 a596b957 2022-07-14 tracey /*
668 a596b957 2022-07-14 tracey * Allow the maximum number of open file descriptors for this
669 a596b957 2022-07-14 tracey * login class (which should be the class "daemon" by default).
670 a596b957 2022-07-14 tracey */
671 a596b957 2022-07-14 tracey if (maxfd == -1)
672 a596b957 2022-07-14 tracey rl.rlim_cur = rl.rlim_max;
673 a596b957 2022-07-14 tracey else
674 a596b957 2022-07-14 tracey rl.rlim_cur = MAXIMUM(rl.rlim_max, (rlim_t)maxfd);
675 a596b957 2022-07-14 tracey if (setrlimit(RLIMIT_NOFILE, &rl) == -1)
676 a596b957 2022-07-14 tracey fatal("%s: failed to set resource limit", __func__);
677 d6267a62 2022-07-17 naddy }