Debian Codegraph @

src/ws/cockpitwebinject.c

raw
1 /*
2 * Copyright (C) 2015 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 "cockpitwebinject.h"
15
16 #include <string.h>
17
18 /**
19 * CockpitWebInject
20 *
21 * This is a CockpitWebFilter which looks for a marker data
22 * and inject additional data after that point. The data is
23 * not injected more than the specified number of times.
24 */
25 struct _CockpitWebInject {
26 GObject parent;
27 /* partial_matches stores the lengths of partial matches, size is (marker length) - 1
28 * e.g. "ABA" with marker "ABAC" will result in [TRUE, FALSE, TRUE]
29 */
30 GArray *partial_matches;
31 GBytes *marker;
32 GBytes *inject;
33
34 guint maximum;
35 guint injected;
36 };
37
38 static void cockpit_web_filter_inject_iface (CockpitWebFilterInterface *iface);
39
40 G_DEFINE_TYPE_WITH_CODE (CockpitWebInject, cockpit_web_inject, G_TYPE_OBJECT,
41 G_IMPLEMENT_INTERFACE (COCKPIT_TYPE_WEB_FILTER, cockpit_web_filter_inject_iface)
42 )
43
44 static void
45 cockpit_web_inject_init (CockpitWebInject *self)
46 {
47
48 }
49
50 static void
51 cockpit_web_inject_finalize (GObject *object)
52 {
53 CockpitWebInject *self = COCKPIT_WEB_INJECT (object);
54
55 if (self->partial_matches)
56 g_array_unref (self->partial_matches);
57 if (self->marker)
58 g_bytes_unref (self->marker);
59 if (self->inject)
60 g_bytes_unref (self->inject);
61
62 G_OBJECT_CLASS (cockpit_web_inject_parent_class)->finalize (object);
63 }
64
65 static void
66 cockpit_web_inject_class_init (CockpitWebInjectClass *klass)
67 {
68 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
69
70 gobject_class->finalize = cockpit_web_inject_finalize;
71 }
72
73 static void
74 cockpit_web_inject_push (CockpitWebFilter *filter,
75 GBytes *block,
76 void (* function) (gpointer, GBytes *),
77 gpointer func_data)
78 {
79 CockpitWebInject *self = (CockpitWebInject *)filter;
80 const gchar *mark, *data, *pos = NULL;
81 gsize mark_len, data_len, at;
82 GBytes *bytes;
83 gsize written;
84 gint partial_len, remaining_len;
85
86 mark = g_bytes_get_data (self->marker, &mark_len);
87 data = g_bytes_get_data (block, &data_len);
88
89 if (data_len == 0)
90 return;
91
92 written = at = 0;
93
94 /* look at our partial matches first
95 * longest partial matches have precedence (they either get longer or don't pan out)
96 * only do this if we haven't reached the maximum yet
97 */
98 if (self->injected < self->maximum)
99 {
100 for (partial_len = self->partial_matches->len - 1; partial_len >= 0; --partial_len)
101 {
102 if (g_array_index (self->partial_matches, gboolean, partial_len))
103 {
104 /* our match can only grow longer */
105 g_array_index (self->partial_matches, gboolean, partial_len) = FALSE;
106 remaining_len = mark_len - partial_len;
107 /* our current block might be too short */
108 if (remaining_len > data_len)
109 {
110 if (memcmp (mark + partial_len, data, data_len) == 0)
111 g_array_index (self->partial_matches, gboolean, partial_len + data_len) = TRUE;
112 }
113 else if (memcmp (mark + partial_len, data, remaining_len) == 0)
114 {
115 /* we have a match */
116 at = remaining_len;
117 pos = data + at;
118 /* reset partials */
119 g_array_set_size(self->partial_matches, 0);
120 g_array_set_size(self->partial_matches, mark_len);
121 break;
122 }
123 }
124 }
125 }
126
127 /* keep searching until we have found the maximum number of allowed matches or reached the end */
128 for(;;)
129 {
130 if (at != written)
131 {
132 bytes = g_bytes_new_from_bytes (block, written, at - written);
133 function (func_data, bytes);
134 g_bytes_unref (bytes);
135 written = at;
136
137 /* did we have a match? */
138 if (pos == (data + at) && self->injected < self->maximum)
139 {
140 function (func_data, self->inject);
141 self->injected++;
142 }
143 }
144
145 if (at >= data_len)
146 break;
147
148 /* if there are enough chars left, try to find a complete match */
149 if (self->injected < self->maximum &&
150 data_len >= (at + mark_len) &&
151 (pos = memmem (data + at, data_len - at, mark, mark_len)))
152 {
153 /* we found a match, but we want to write out the mark also before we inject */
154 pos += mark_len;
155 at = pos - data;
156 }
157 else
158 {
159 /* nothing found, forward */
160 at = data_len;
161 pos = NULL;
162 }
163 }
164
165 /* if we haven't reached our max number of injections, look for partial matches at the end */
166 partial_len = mark_len - 1;
167 if (partial_len > data_len)
168 partial_len = data_len;
169 while (partial_len > 0)
170 {
171 if (memcmp (mark, data + data_len - partial_len, partial_len) == 0)
172 g_array_index (self->partial_matches, gboolean, partial_len) = TRUE;
173 partial_len--;
174 }
175 }
176
177 static void
178 cockpit_web_filter_inject_iface (CockpitWebFilterInterface *iface)
179 {
180 iface->push = cockpit_web_inject_push;
181 }
182
183 /**
184 * cockpit_web_filter_new:
185 * @marker: marker to search for
186 * @inject: bytes to inject after marker
187 * @count: number of times to inject
188 *
189 * Create a new CockpitWebFilter which injects @inject bytes
190 * after the @marker. It injects the data once.
191 *
192 * Returns: A new CockpitWebFilter
193 */
194 CockpitWebFilter *
195 cockpit_web_inject_new (const gchar *marker,
196 GBytes *inject,
197 guint count)
198 {
199 CockpitWebInject *self;
200 gsize len;
201
202 g_return_val_if_fail (marker != NULL, NULL);
203 g_return_val_if_fail (inject != NULL, NULL);
204
205 len = strlen (marker);
206 g_return_val_if_fail (len > 0, NULL);
207
208 self = g_object_new (COCKPIT_TYPE_WEB_INJECT, NULL);
209 self->marker = g_bytes_new (marker, len);
210 self->partial_matches = g_array_sized_new(FALSE, TRUE, sizeof(gboolean), len);
211 g_array_set_size(self->partial_matches, len);
212 self->inject = g_bytes_ref (inject);
213 self->maximum = count;
214
215 return COCKPIT_WEB_FILTER (self);
216 }