Debian Codegraph @

src/tls/socket-activation-helper.c

raw
1 /*
2 * Copyright (C) 2019 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5 #include "config.h"
6
7 #include <assert.h>
8 #include <err.h>
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <pthread.h>
12 #include <stdbool.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <sys/poll.h>
17 #include <sys/socket.h>
18 #include <sys/wait.h>
19 #include <unistd.h>
20
21 #include "socket-io.h"
22 #include "testing.h"
23 #include "utils.h"
24
25 #define MAX_COCKPIT_WS_ARGS 5
26
27 static struct instance_type
28 {
29 const char *sockname;
30 const char *argv[MAX_COCKPIT_WS_ARGS];
31 } instance_types[] = {
32 {"https-factory.sock", {}}, /* treated specially */
33 /* support up to 3 ws instances (+1 special); increase this if the unit test needs more */
34 {"https@" SHA256_NIL ".sock", {"--for-tls-proxy", "--port=0"}},
35 {"https@" CLIENT_CERT_FINGERPRINT ".sock", {"--for-tls-proxy", "--port=0"}},
36 {"https@" CLIENT_EXPIRED_FINGERPRINT ".sock", {"--for-tls-proxy", "--port=0"}},
37 {"https@" ALTERNATE_FINGERPRINT ".sock", {}}, /* treated specially */
38 {"http.sock", {"--no-tls", "--port", "0"}},
39 };
40
41 static int socket_to_pid[N_ELEMENTS (instance_types)];
42 static struct pollfd ws_pollfds[N_ELEMENTS (instance_types)];
43
44 volatile sig_atomic_t terminated = 0;
45
46 static void
47 term (int signum)
48 {
49 debug (HELPER, "SIGTERM received");
50 terminated = 1;
51 }
52
53
54 static void
55 handle_cockpit_ws_exited (int sigid)
56 {
57 pid_t pid;
58 debug (HELPER, "SIGCHLD received");
59 while ((pid = waitpid (-1, NULL, WNOHANG)) > 0)
60 {
61 bool found = false;
62 debug (HELPER, "SIGCHLD received for pid %u", pid);
63 for (size_t i = 0; i < N_ELEMENTS (instance_types); i++)
64 {
65 if (pid == socket_to_pid[i])
66 {
67 debug (HELPER, "-> ws instance type %zu, cleaning up reference", i);
68 ws_pollfds[i].events = POLLIN;
69 socket_to_pid[i] = 0;
70 found = true;
71 break;
72 }
73 }
74
75 /* This can only be reached in case cockpit-ws exited fast and the parent
76 * process did not manage to insert its PID into the array
77 */
78 if (!found)
79 warnx ("Could not find the process in socket_to_pid array");
80 }
81
82 if (pid < 0 && errno != ECHILD)
83 err (EXIT_FAILURE, "waitpid failed");
84 }
85
86 static pid_t
87 spawn_cockpit_ws (const char *ws_path, int fd,
88 const char **cockpit_ws_args)
89 {
90 pid_t pid = fork ();
91
92 if (pid == -1)
93 {
94 err (EXIT_FAILURE, "fork() failed");
95 }
96 else if (pid > 0)
97 {
98 debug (HELPER, "spawned cockpit-ws instance pid %u", pid);
99 return pid;
100 }
101 else
102 {
103 const char *args[MAX_COCKPIT_WS_ARGS + 1] = { ws_path };
104 int res;
105 int duped_fd;
106 char pid_str[10];
107
108 /* pass the socket to ws like systemd activation does, see sd_listen_fds(3) */
109 /* fd is CLOEXEC, so dup2 on our first fd 3 will be a no-op; force duping */
110 duped_fd = dup (fd);
111 if (duped_fd < 0)
112 err (EXIT_FAILURE, "dup() failed");
113
114 if (duped_fd != SD_LISTEN_FDS_START)
115 {
116 if (dup2 (duped_fd, SD_LISTEN_FDS_START) < 0)
117 err (EXIT_FAILURE, "dup2() failed");
118 assert (close (duped_fd) == 0);
119 }
120
121 setenv ("LISTEN_FDS", "1", 1);
122 res = snprintf (pid_str, sizeof (pid_str), "%i", getpid ());
123 assert (res < sizeof (pid_str));
124 setenv ("LISTEN_PID", pid_str, 1);
125
126 for (int i = 0; cockpit_ws_args[i] != NULL; i++)
127 args[i + 1] = cockpit_ws_args[i];
128
129 execv (ws_path, (char **) args);
130
131 err (EXIT_FAILURE, "spawning cockpit-ws instance failed");
132 }
133 }
134
135 /* keep this in sync with src/ws/[email protected] */
136 /* this is blocking! if this program ever stops being an unit-test only thing
137 * and gets used in production, rewrite as proper child process */
138 static void
139 handle_https_factory (int listen_fd)
140 {
141 int fd = accept4 (listen_fd, NULL, NULL, SOCK_CLOEXEC);
142 char instance[WSINSTANCE_MAX];
143 const char *result;
144
145 debug (HELPER, "connection to https-factory.sock:");
146
147 if (fd < 0)
148 err (EXIT_FAILURE, "accept connection to https-factory.sock");
149
150 debug (HELPER, " -> reading instance name... ");
151 if (!recv_alnum (fd, instance, sizeof instance, 10 * 1000000))
152 errx (EXIT_FAILURE, "failed to read instance name");
153
154 debug (HELPER, " -> success: '%s'", instance);
155 if (strcmp (instance, SHA256_NIL) == 0) /* we check this value from the tests */
156 result = "done";
157 else
158 result = "fail";
159
160 debug (HELPER, " -> sending reply '%s'", result);
161 if (!send_all (fd, result, strlen (result), 10 * 1000000))
162 errx (EXIT_FAILURE, "failed to write https-factory.sock response");
163 debug (HELPER, " -> done.");
164
165 close (fd);
166 }
167
168 static void *
169 handle_alternate_thread (void *fd_as_ptr)
170 {
171 int fd = (intptr_t) fd_as_ptr;
172 ssize_t s;
173 char b;
174
175 do
176 s = write (fd, "hello", 5);
177 while (s == -1 && errno == EINTR);
178 assert (s == 5);
179
180
181 do
182 s = read (fd, &b, 1);
183 while (s == -1 && errno == EINTR);
184 assert (s == 0);
185
186 close (fd);
187
188 return NULL;
189 }
190
191 static void
192 handle_alternate (int listen_fd)
193 {
194 int fd = accept4 (listen_fd, NULL, NULL, SOCK_CLOEXEC);
195 pthread_attr_t attr;
196 pthread_t thread;
197
198 /* This is used from a testcase which spins up a whole bunch of
199 * parallel connections, so we need to handle this asynchronously.
200 * Use threads.
201 */
202 pthread_attr_init (&attr);
203 pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
204 pthread_create (&thread, &attr, handle_alternate_thread, (void *) (intptr_t) fd);
205 pthread_attr_destroy (&attr);
206 }
207
208 int
209 main (int argc, char *argv[])
210 {
211 if (argc != 3)
212 errx (EXIT_FAILURE, "Usage: socket-activation-helper $WS_PATH $SOCKETS_DIR");
213
214 const char *ws_path = argv[1];
215 const char *socket_dir = argv[2];
216 int socket_dir_fd = open (socket_dir, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
217 if (socket_dir_fd < 0)
218 err (EXIT_FAILURE, "failed to open %s", socket_dir);
219
220 for (size_t i = 0; i < N_ELEMENTS (instance_types); i++)
221 ws_pollfds[i].fd = -1;
222
223 /* Set up signal handler for when cockpit-ws has exited */
224 struct sigaction sa;
225 sigemptyset (&sa.sa_mask);
226 sa.sa_flags = 0;
227 sa.sa_handler = handle_cockpit_ws_exited;
228 sigaction (SIGCHLD, &sa, NULL);
229
230 /* clean up ws child processes on SIGTERM */
231 sa.sa_handler = term;
232 sigaction (SIGTERM, &sa, NULL);
233
234 for (size_t i = 0; i < N_ELEMENTS (instance_types); i++)
235 {
236 int listen_fd;
237
238 if (unlinkat (socket_dir_fd, instance_types[i].sockname, 0) < 0 && errno != ENOENT)
239 err (EXIT_FAILURE, "unlink() failed");
240
241 /* create a listening socket for each cockpit-ws mode */
242 listen_fd = socket (AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
243 if (listen_fd < 0)
244 err (EXIT_FAILURE, "socket() failed");
245
246 if (af_unix_bindat (listen_fd, socket_dir_fd, instance_types[i].sockname) < 0)
247 err (EXIT_FAILURE, "%s/%s: bind() failed", socket_dir, instance_types[i].sockname);
248
249 if (listen (listen_fd, 32) < 0)
250 err (EXIT_FAILURE, "%s/%s: listen() failed", socket_dir, instance_types[i].sockname);
251
252 ws_pollfds[i].fd = listen_fd;
253 ws_pollfds[i].events = POLLIN;
254 }
255
256 /* signal the unit test that we are ready to connect */
257 int fd = openat (socket_dir_fd, "ready", O_CREAT | O_WRONLY | O_EXCL, 0666);
258 if (fd < 0)
259 err (EXIT_FAILURE, "failed to create %s/stamp", socket_dir);
260 close (fd);
261 close (socket_dir_fd);
262
263 /* Loop waiting for incoming connects on any of the connected sockets. */
264 while (!terminated)
265 {
266 int rv = poll (ws_pollfds, N_ELEMENTS (instance_types), -1);
267
268 if (rv == -1)
269 {
270 if (errno == EINTR)
271 continue;
272 err (EXIT_FAILURE, "poll() failed");
273 }
274 else
275 {
276 debug (HELPER, "got %i poll() events", rv);
277 for (size_t i = 0; i < N_ELEMENTS (instance_types); i++)
278 {
279 if (ws_pollfds[i].revents == POLLIN)
280 {
281 /* is this the https factory? */
282 if (strstr (instance_types[i].sockname, "factory"))
283 {
284 debug (HELPER, "got POLLIN on fd %i https factory", ws_pollfds[i].fd);
285 handle_https_factory (ws_pollfds[i].fd);
286 continue;
287 }
288 else if (strstr (instance_types[i].sockname, ALTERNATE_FINGERPRINT))
289 {
290 debug (HELPER, "got POLLIN on fd %i alternate cert socket", ws_pollfds[i].fd);
291 handle_alternate (ws_pollfds[i].fd);
292 continue;
293 }
294
295 ws_pollfds[i].events = 0;
296 debug (HELPER, "got POLLIN on fd %i, spawning ws for %s",
297 ws_pollfds[i].fd, instance_types[i].sockname);
298 socket_to_pid[i] = spawn_cockpit_ws (ws_path, ws_pollfds[i].fd,
299 instance_types[i].argv);
300 }
301 }
302 }
303 }
304
305 debug (HELPER, "exiting, cleaning up cockpit-ws children");
306 for (size_t i = 0; i < N_ELEMENTS (socket_to_pid); i++)
307 if (socket_to_pid[i] > 0)
308 kill (socket_to_pid[i], SIGTERM);
309
310 return EXIT_SUCCESS;
311 }