Debian Codegraph @

src/ws/cockpitpipetransport.c

raw
1 /*
2 * Copyright (C) 2014 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 #include "config.h"
7
8 /* This gets logged as part of the (more verbose) protocol logging */
9 #ifdef G_LOG_DOMAIN
10 #undef G_LOG_DOMAIN
11 #endif
12 #define G_LOG_DOMAIN "cockpit-protocol"
13
14 #include "cockpitpipetransport.h"
15
16 #include "common/cockpitframe.h"
17 #include "cockpitpipe.h"
18
19 #include <glib-unix.h>
20
21 #include <sys/socket.h>
22 #include <sys/uio.h>
23 #include <sys/wait.h>
24
25 #include <errno.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <unistd.h>
29
30 /**
31 * CockpitPipeTransport:
32 *
33 * A #CockpitTransport implementation that shuttles data over a
34 * #CockpitPipe. See doc/protocol.md for information on how the
35 * framing looks ... including the MSB length prefix.
36 */
37
38 struct _CockpitPipeTransport {
39 CockpitTransport parent_instance;
40 gchar *name;
41 CockpitPipe *pipe;
42 gboolean closed;
43 gulong read_sig;
44 gulong close_sig;
45 };
46
47 enum {
48 PROP_0,
49 PROP_NAME,
50 PROP_PIPE,
51 };
52
53 static void cockpit_transport_read_from_pipe (CockpitTransport *self,
54 const gchar *logname,
55 CockpitPipe *pipe,
56 gboolean *closed,
57 GByteArray *input,
58 gboolean end_of_data);
59
60 G_DEFINE_TYPE (CockpitPipeTransport, cockpit_pipe_transport, COCKPIT_TYPE_TRANSPORT);
61
62 static void
63 cockpit_pipe_transport_init (CockpitPipeTransport *self)
64 {
65
66 }
67
68 static void
69 on_pipe_read (CockpitPipe *pipe,
70 GByteArray *input,
71 gboolean end_of_data,
72 gpointer user_data)
73 {
74 CockpitPipeTransport *self = COCKPIT_PIPE_TRANSPORT (user_data);
75 cockpit_transport_read_from_pipe (COCKPIT_TRANSPORT (self), self->name,
76 pipe, &self->closed, input, end_of_data);
77
78 if (end_of_data)
79 cockpit_pipe_close (self->pipe, NULL);
80 }
81
82 static void
83 on_pipe_close (CockpitPipe *pipe,
84 const gchar *problem,
85 gpointer user_data)
86 {
87 CockpitPipeTransport *self = COCKPIT_PIPE_TRANSPORT (user_data);
88 gboolean is_cockpit;
89 GError *error = NULL;
90 gint status;
91
92 self->closed = TRUE;
93
94 /* This function is called by the base class when it is closed */
95 if (cockpit_pipe_get_pid (pipe, NULL))
96 {
97 is_cockpit = g_str_equal (self->name, "cockpit-bridge") ||
98 g_str_has_suffix (self->name, "/cockpit-session");
99
100 if (problem == NULL ||
101 g_str_equal (problem, "internal-error"))
102 {
103 status = cockpit_pipe_exit_status (pipe);
104 // HUP: bridge child cleaned up by our PR_SET_PDEATHSIG
105 if (WIFSIGNALED (status) && (WTERMSIG (status) == SIGTERM || WTERMSIG (status) == SIGHUP))
106 problem = "terminated";
107 else if (is_cockpit && WIFEXITED (status) && WEXITSTATUS (status) == 127)
108 problem = "no-cockpit"; // cockpit-bridge not installed
109 else if (is_cockpit && WIFEXITED (status) && WEXITSTATUS (status) == 1)
110 problem = "access-denied"; // disabled user shells like /bin/false or /sbin/nologin exit 1
111 else if (WIFEXITED (status) && WEXITSTATUS (status) == 255)
112 problem = "terminated"; // failed or got a signal, etc.
113 else if (!g_spawn_check_exit_status (status, &error))
114 {
115 problem = "internal-error";
116 if (is_cockpit)
117 g_warning ("%s: bridge program failed: %s", self->name, error->message);
118 else
119 g_debug ("%s: process failed: %s", self->name, error->message);
120 g_error_free (error);
121 }
122 }
123 else if (g_str_equal (problem, "not-found"))
124 {
125 if (is_cockpit)
126 {
127 g_message ("%s: failed to execute bridge: not found", self->name);
128 problem = "no-cockpit";
129 }
130 else
131 {
132 g_debug ("%s: failed to run: not found", self->name);
133 }
134 }
135 }
136
137 g_debug ("%s: closed%s%s", self->name,
138 problem ? ": " : "", problem ? problem : "");
139
140 cockpit_transport_emit_closed (COCKPIT_TRANSPORT (self), problem);
141 }
142
143 static void
144 cockpit_pipe_transport_constructed (GObject *object)
145 {
146 CockpitPipeTransport *self = COCKPIT_PIPE_TRANSPORT (object);
147
148 G_OBJECT_CLASS (cockpit_pipe_transport_parent_class)->constructed (object);
149
150 g_return_if_fail (self->pipe != NULL);
151 g_object_get (self->pipe, "name", &self->name, NULL);
152 self->read_sig = g_signal_connect (self->pipe, "read", G_CALLBACK (on_pipe_read), self);
153 self->close_sig = g_signal_connect (self->pipe, "close", G_CALLBACK (on_pipe_close), self);
154 }
155
156 static void
157 cockpit_pipe_transport_get_property (GObject *object,
158 guint prop_id,
159 GValue *value,
160 GParamSpec *pspec)
161 {
162 CockpitPipeTransport *self = COCKPIT_PIPE_TRANSPORT (object);
163
164 switch (prop_id)
165 {
166 case PROP_NAME:
167 g_value_set_string (value, self->name);
168 break;
169 case PROP_PIPE:
170 g_value_set_object (value, cockpit_pipe_transport_get_pipe (self));
171 break;
172 default:
173 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
174 break;
175 }
176 }
177
178 static void
179 cockpit_pipe_transport_set_property (GObject *object,
180 guint prop_id,
181 const GValue *value,
182 GParamSpec *pspec)
183 {
184 CockpitPipeTransport *self = COCKPIT_PIPE_TRANSPORT (object);
185
186 switch (prop_id)
187 {
188 case PROP_PIPE:
189 self->pipe = g_value_dup_object (value);
190 break;
191 default:
192 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
193 break;
194 }
195 }
196
197 static void
198 cockpit_pipe_transport_finalize (GObject *object)
199 {
200 CockpitPipeTransport *self = COCKPIT_PIPE_TRANSPORT (object);
201
202 g_signal_handler_disconnect (self->pipe, self->read_sig);
203 g_signal_handler_disconnect (self->pipe, self->close_sig);
204
205 g_free (self->name);
206 g_clear_object (&self->pipe);
207
208 G_OBJECT_CLASS (cockpit_pipe_transport_parent_class)->finalize (object);
209 }
210
211 static void
212 cockpit_pipe_transport_send (CockpitTransport *transport,
213 const gchar *channel_id,
214 GBytes *payload)
215 {
216 CockpitPipeTransport *self = COCKPIT_PIPE_TRANSPORT (transport);
217 GBytes *prefix;
218 gchar *prefix_str;
219 gsize payload_len;
220 gsize channel_len;
221
222 if (self->closed)
223 {
224 g_debug ("dropping message on closed transport");
225 return;
226 }
227
228 channel_len = channel_id ? strlen (channel_id) : 0;
229 payload_len = g_bytes_get_size (payload);
230
231 prefix_str = g_strdup_printf ("%" G_GSIZE_FORMAT "\n%s\n",
232 channel_len + 1 + payload_len,
233 channel_id ? channel_id : "");
234 prefix = g_bytes_new_take (prefix_str, strlen (prefix_str));
235
236 cockpit_pipe_write (self->pipe, prefix);
237 cockpit_pipe_write (self->pipe, payload);
238 g_bytes_unref (prefix);
239
240 g_debug ("%s: queued %" G_GSIZE_FORMAT " byte payload", self->name, payload_len);
241 }
242
243 static void
244 cockpit_pipe_transport_close (CockpitTransport *transport,
245 const gchar *problem)
246 {
247 CockpitPipeTransport *self = COCKPIT_PIPE_TRANSPORT (transport);
248 cockpit_pipe_close (self->pipe, problem);
249 }
250
251 static void
252 cockpit_pipe_transport_class_init (CockpitPipeTransportClass *klass)
253 {
254 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
255 CockpitTransportClass *transport_class = COCKPIT_TRANSPORT_CLASS (klass);
256
257 transport_class->send = cockpit_pipe_transport_send;
258 transport_class->close = cockpit_pipe_transport_close;
259
260 gobject_class->constructed = cockpit_pipe_transport_constructed;
261 gobject_class->get_property = cockpit_pipe_transport_get_property;
262 gobject_class->set_property = cockpit_pipe_transport_set_property;
263 gobject_class->finalize = cockpit_pipe_transport_finalize;
264
265 g_object_class_override_property (gobject_class, PROP_NAME, "name");
266
267 g_object_class_install_property (gobject_class, PROP_PIPE,
268 g_param_spec_object ("pipe", NULL, NULL,
269 COCKPIT_TYPE_PIPE,
270 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
271 }
272
273 /**
274 * cockpit_pipe_transport_new:
275 * @pipe: the pipe to send data over
276 *
277 * Create a new CockpitPipeTransport for a pipe
278 *
279 * Returns: (transfer full): the new transport
280 */
281 CockpitTransport *
282 cockpit_pipe_transport_new (CockpitPipe *pipe)
283 {
284 return g_object_new (COCKPIT_TYPE_PIPE_TRANSPORT,
285 "pipe", pipe,
286 NULL);
287 }
288
289 /**
290 * cockpit_pipe_transport_new_fds:
291 * @name: name for debugging
292 * @in_fd: the file descriptor to read from
293 * @out_fd: the file descriptor to write to
294 *
295 * Create a new CockpitPipeTransport for a pair
296 * of file descriptors.
297 *
298 * Returns: (transfer full): the new transport
299 */
300 CockpitTransport *
301 cockpit_pipe_transport_new_fds (const gchar *name,
302 gint in_fd,
303 gint out_fd)
304 {
305 CockpitTransport *transport;
306 CockpitPipe *pipe;
307
308 pipe = cockpit_pipe_new (name, in_fd, out_fd);
309 transport = cockpit_pipe_transport_new (pipe);
310 g_object_unref (pipe);
311
312 return transport;
313 }
314
315 CockpitPipe *
316 cockpit_pipe_transport_get_pipe (CockpitPipeTransport *self)
317 {
318 g_return_val_if_fail (COCKPIT_IS_PIPE_TRANSPORT (self), NULL);
319 return self->pipe;
320 }
321
322 /**
323 * cockpit_transport_read_from_pipe:
324 *
325 * Meant to be used in a "read" handler for a #CockpitPipe
326 * Closed is pointer to a boolean value that may be updated
327 * during the read and parse loop.
328 */
329 static void
330 cockpit_transport_read_from_pipe (CockpitTransport *self,
331 const gchar *logname,
332 CockpitPipe *pipe,
333 gboolean *closed,
334 GByteArray *input,
335 gboolean end_of_data)
336 {
337 /* This may be updated during the loop. */
338 g_assert (closed != NULL);
339 g_object_ref (self);
340
341 while (!*closed)
342 {
343 gsize i;
344 gssize size = cockpit_frame_parse (input->data, input->len, &i);
345
346 if (size == 0)
347 {
348 if (!end_of_data)
349 g_debug ("%s: want more data", logname);
350 break;
351 }
352 else if (size < 0)
353 {
354 g_warning ("%s: incorrect protocol: received invalid length prefix", logname);
355 cockpit_pipe_close (pipe, "protocol-error");
356 break;
357 }
358 else if (input->len < i + size)
359 {
360 g_debug ("%s: want more data 2", logname);
361 break;
362 }
363
364 g_autoptr(GBytes) message = cockpit_pipe_consume (input, i, size, 0);
365 g_autofree gchar *channel = NULL;
366 g_autoptr(GBytes) payload = cockpit_transport_parse_frame (message, &channel);
367 if (payload)
368 {
369 g_debug ("%s: received a %d byte payload", logname, (int)size);
370 cockpit_transport_emit_recv (self, channel, payload);
371 }
372 }
373
374 if (end_of_data)
375 {
376 /* Received a partial message */
377 if (input->len > 0)
378 {
379 g_debug ("%s: received truncated %d byte frame", logname, input->len);
380 cockpit_pipe_close (pipe, "disconnected");
381 }
382 }
383
384 g_object_unref (self);
385 }