Debian Codegraph @

src/dbus/connection.c

raw
1 /*
2 * Connection
3 */
4
5 #include <c-stdaux.h>
6 #include <stdlib.h>
7 #include <sys/epoll.h>
8 #include <sys/types.h>
9 #include "dbus/connection.h"
10 #include "dbus/message.h"
11 #include "dbus/socket.h"
12 #include "util/dispatch.h"
13 #include "util/error.h"
14 #include "util/user.h"
15
16 static int connection_init(Connection *c,
17 DispatchContext *dispatch_ctx,
18 DispatchFn dispatch_fn,
19 User *user,
20 int fd) {
21 _c_cleanup_(connection_deinitp) Connection *connection = c;
22 int r;
23
24 *connection = (Connection)CONNECTION_NULL(*connection);
25 socket_init(&connection->socket, user, fd);
26
27 r = dispatch_file_init(&connection->socket_file,
28 dispatch_ctx,
29 dispatch_fn,
30 fd,
31 EPOLLHUP | EPOLLIN | EPOLLOUT,
32 EPOLLIN | EPOLLOUT);
33 if (r)
34 return error_fold(r);
35
36 connection = NULL;
37 return 0;
38 }
39
40 /**
41 * connection_init_server() - XXX
42 */
43 int connection_init_server(Connection *c,
44 DispatchContext *dispatch_ctx,
45 DispatchFn dispatch_fn,
46 User *user,
47 const char *guid,
48 int fd) {
49 int r;
50
51 r = connection_init(c,
52 dispatch_ctx,
53 dispatch_fn,
54 user,
55 fd);
56 if (r)
57 return error_trace(r);
58
59 c->server = true;
60 sasl_server_init(&c->sasl_server, user->uid, guid);
61 return 0;
62 }
63
64 /**
65 * connection_init_client() - XXX
66 */
67 int connection_init_client(Connection *c,
68 DispatchContext *dispatch_ctx,
69 DispatchFn dispatch_fn,
70 User *user,
71 int fd) {
72 int r;
73
74 r = connection_init(c,
75 dispatch_ctx,
76 dispatch_fn,
77 user,
78 fd);
79 if (r)
80 return error_trace(r);
81
82 c->server = false;
83 sasl_client_init(&c->sasl_client);
84 return 0;
85 }
86
87 /**
88 * connection_deinit() - XXX
89 */
90 void connection_deinit(Connection *connection) {
91 sasl_client_deinit(&connection->sasl_client);
92 sasl_server_deinit(&connection->sasl_server);
93 dispatch_file_deinit(&connection->socket_file);
94 socket_deinit(&connection->socket);
95 }
96
97 /**
98 * connection_get_stats() - XXX
99 */
100 void connection_get_stats(Connection *connection,
101 unsigned int *n_in_bytesp,
102 unsigned int *n_in_fdsp,
103 unsigned int *n_out_bytesp,
104 unsigned int *n_out_fdsp) {
105 socket_get_stats(&connection->socket, n_in_bytesp, n_in_fdsp, n_out_bytesp, n_out_fdsp);
106 }
107
108 static int connection_feed_sasl(Connection *connection, const char *input, size_t n_input) {
109 const char *output;
110 size_t n_output;
111 int r;
112
113 /* client SASL allows NULL input as bootstrap */
114 c_assert(!connection->server || input);
115 c_assert(!connection->authenticated);
116
117 if (connection->server) {
118 r = sasl_server_dispatch(&connection->sasl_server, input, n_input, &output, &n_output);
119 if (r) {
120 switch (r) {
121 case SASL_E_PROTOCOL_VIOLATION:
122 return CONNECTION_E_SASL_VIOLATION;
123 default:
124 return error_fold(r);
125 }
126 }
127 } else {
128 r = sasl_client_dispatch(&connection->sasl_client, input, n_input, &output, &n_output);
129 if (r) {
130 switch (r) {
131 case SASL_E_FAILURE:
132 return CONNECTION_E_SASL_FAILURE;
133 case SASL_E_PROTOCOL_VIOLATION:
134 return CONNECTION_E_SASL_VIOLATION;
135 default:
136 return error_fold(r);
137 }
138 }
139 }
140
141 connection->authenticated = connection->server ?
142 sasl_server_is_done(&connection->sasl_server) :
143 sasl_client_is_done(&connection->sasl_client);
144
145 /*
146 * If the SASL exchange triggered an outgoing message, we will queue it
147 * on the socket. There're 3 things that might fail:
148 *
149 * 1) The socket was already closed for output. In that case we
150 * black-hole the message. The trigger of the write-side
151 * shutdown must have already taken care of everything else.
152 *
153 * 2) The message exceeds the connection quota. Since this is a
154 * self-triggered message, the connection itself is responsible
155 * and thus at fault. We simply close the write-side of the
156 * connection and wait for them to react to it.
157 *
158 * 3) A fatal error. Just like always, we simply fold it.
159 */
160 if (output) {
161 r = socket_queue_line(&connection->socket, NULL, output, n_output);
162 if (!r)
163 dispatch_file_select(&connection->socket_file, EPOLLOUT);
164 else if (r == SOCKET_E_QUOTA)
165 connection_shutdown(connection);
166 else if (r != SOCKET_E_SHUTDOWN)
167 return error_fold(r);
168 }
169
170 return 0;
171 }
172
173 /**
174 * connection_open() - XXX
175 */
176 int connection_open(Connection *connection) {
177 int r;
178
179 c_assert(socket_is_running(&connection->socket));
180
181 if (!connection->server) {
182 /* bootstrap client SASL, this should always succeed */
183 r = connection_feed_sasl(connection, NULL, 0);
184 if (r)
185 return error_fold(r);
186 }
187
188 dispatch_file_select(&connection->socket_file, EPOLLHUP | EPOLLIN);
189 return 0;
190 }
191
192 /**
193 * connection_shutdown() - XXX
194 */
195 void connection_shutdown(Connection *connection) {
196 /*
197 * A connection shutdown stops the write-side channel. If that happens
198 * after the read-side was already torn down, we must re-select
199 * EPOLLHUP so the main-loop of @connection gets woken up again. We
200 * know that EPOLLHUP must be signalled, since it is implied by the
201 * combination (hup_in && hup_out).
202 */
203 socket_shutdown(&connection->socket);
204 if (!socket_is_running(&connection->socket))
205 dispatch_file_select(&connection->socket_file, EPOLLHUP);
206 }
207
208 /**
209 * connection_close() - XXX
210 */
211 void connection_close(Connection *connection) {
212 socket_close(&connection->socket);
213 }
214
215 /**
216 * connection_dispatch() - XXX
217 */
218 int connection_dispatch(Connection *connection, uint32_t events) {
219 static const uint32_t interest[] = { EPOLLIN, EPOLLHUP, EPOLLOUT };
220 size_t i;
221 int r;
222
223 for (i = 0; i < C_ARRAY_SIZE(interest); ++i) {
224 if (events & interest[i]) {
225 r = socket_dispatch(&connection->socket, interest[i]);
226 if (!r)
227 dispatch_file_clear(&connection->socket_file, interest[i]);
228 else if (r == SOCKET_E_LOST_INTEREST)
229 dispatch_file_deselect(&connection->socket_file, interest[i]);
230 else if (r != SOCKET_E_PREEMPTED)
231 return error_fold(r);
232 }
233 }
234
235 return 0;
236 }
237
238 /**
239 * connection_dequeue() - XXX
240 */
241 int connection_dequeue(Connection *connection, Message **messagep) {
242 _c_cleanup_(message_unrefp) Message *message = NULL;
243 const char *input;
244 size_t n_input;
245 int r;
246
247 if (_c_unlikely_(!connection->authenticated)) {
248 do {
249 r = socket_dequeue_line(&connection->socket, &input, &n_input);
250 if (r)
251 return (r == SOCKET_E_EOF) ? CONNECTION_E_EOF : error_fold(r);
252
253 if (!input) {
254 *messagep = NULL;
255 return 0;
256 }
257
258 r = connection_feed_sasl(connection, input, n_input);
259 if (r)
260 return error_trace(r);
261 } while (!connection->authenticated);
262 }
263
264 r = socket_dequeue(&connection->socket, &message);
265 if (r) {
266 if (r == SOCKET_E_EOF)
267 return CONNECTION_E_EOF;
268 else if (r == SOCKET_E_QUOTA)
269 return CONNECTION_E_QUOTA;
270
271 return error_fold(r);
272 }
273
274 /* If there is no message pending, return NULL to the caller. */
275 if (!message)
276 goto exit;
277
278 /*
279 * We now dequeued a message from the socket layer. The socket layer
280 * only tokenizes messages, and ensures stream integrity. Here in the
281 * connection layer, we can verify further properties. We mainly verify
282 * the messages do not violate the negotiations that we got from SASL.
283 */
284
285 if (fdlist_count(message->fds) > 0) {
286 /*
287 * If the message carries FDs, but we never negotiated FD
288 * passing, this constitutes a protocol violation. Reject it
289 * and tell the caller.
290 */
291 if (connection->server) {
292 if (_c_unlikely_(!connection->sasl_server.fds_allowed))
293 return CONNECTION_E_UNEXPECTED_FDS;
294 } else {
295 /* We always forcibly enable FD-passing as client. */
296 }
297 }
298
299 exit:
300 *messagep = message;
301 message = NULL;
302 return 0;
303 }
304
305 /**
306 * connection_queue() - XXX
307 */
308 int connection_queue(Connection *connection, User *user, Message *message) {
309 int r;
310
311 if (fdlist_count(message->fds) > 0) {
312 /*
313 * We must not send messages out that carry FDs, unless FD
314 * passing was successfully negotiated. The negotiation must be
315 * triggered by a client during SASL. Any message following the
316 * negotiation is allowed to carry FDs.
317 *
318 * From an API viewpoint, this is not ideal, since we do not
319 * expose this flag to the caller. However, in case of the
320 * broker it does not matter, since we only ever send messages
321 * when triggered by the client. Hence, we never accidentally
322 * send FDs out, racing a possible pending FD negotiation in
323 * SASL.
324 */
325 if (connection->server) {
326 if (_c_unlikely_(!connection->sasl_server.fds_allowed))
327 return CONNECTION_E_UNEXPECTED_FDS;
328 } else {
329 /* We always forcibly enable FD-passing as client. */
330 }
331 }
332
333 r = socket_queue(&connection->socket, user, message);
334 if (r == SOCKET_E_QUOTA)
335 return CONNECTION_E_QUOTA;
336 else if (r == SOCKET_E_SHUTDOWN)
337 return 0;
338 else if (r)
339 return error_fold(r);
340
341 dispatch_file_select(&connection->socket_file, EPOLLOUT);
342 return 0;
343 }