1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
/*
Authors:
Pavel Březina <pbrezina@redhat.com>
Copyright (C) 2017 Red Hat
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <talloc.h>
#include <tevent.h>
#include <sys/types.h>
#include <dbus/dbus.h>
#include "util/util.h"
#include "util/dlinklist.h"
#include "util/sss_chain_id.h"
#include "sbus/sbus_private.h"
struct sbus_message_meta {
int type;
const char *destination;
const char *interface;
const char *member;
const char *sender;
const char *path;
};
static void
sbus_message_meta_read(DBusMessage *message,
struct sbus_message_meta *meta)
{
meta->type = dbus_message_get_type(message);
meta->destination = dbus_message_get_destination(message);
meta->interface = dbus_message_get_interface(message);
meta->member = dbus_message_get_member(message);
meta->sender = dbus_message_get_sender(message);
meta->path = dbus_message_get_path(message);
}
struct sbus_issue_request_state {
struct sbus_connection *conn;
DBusMessageIter message_iter;
DBusMessage *message;
enum sbus_request_type type;
};
static void sbus_issue_request_done(struct tevent_req *subreq);
static errno_t
sbus_issue_request(TALLOC_CTX *mem_ctx,
struct sbus_message_meta *meta,
struct sbus_connection *conn,
DBusMessage *message,
enum sbus_request_type type,
const struct sbus_invoker *invoker,
const struct sbus_handler *handler)
{
struct sbus_issue_request_state *state;
struct sbus_request *request;
struct tevent_req *subreq;
errno_t ret;
state = talloc_zero(mem_ctx, struct sbus_issue_request_state);
if (state == NULL) {
return ENOMEM;
}
state->conn = conn;
state->message = dbus_message_ref(message);
state->type = type;
ret = sbus_message_bound(state, state->message);
if (ret != EOK) {
dbus_message_unref(state->message);
goto done;
}
dbus_message_iter_init(message, &state->message_iter);
request = sbus_request_create(state, conn, type, meta->destination,
meta->interface, meta->member, meta->path);
if (request == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, "Unable to create request data!\n");
ret = ENOMEM;
goto done;
}
subreq = sbus_incoming_request_send(state, conn->ev, conn, request,
invoker, handler, meta->sender,
&state->message_iter, message);
if (subreq == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, "Unable to create request!\n");
ret = ENOMEM;
goto done;
}
tevent_req_set_callback(subreq, sbus_issue_request_done, state);
ret = EOK;
done:
if (ret != EOK) {
talloc_free(state);
}
return ret;
}
static void sbus_issue_request_done(struct tevent_req *subreq)
{
struct sbus_issue_request_state *state;
struct sbus_message_meta meta;
const char *error_name;
const char *error_msg;
uint64_t old_chain_id;
DBusMessage *reply;
errno_t ret;
/* This is a top level request and a place where we loose tracking of the
* correct chain id. We got here from sbus_incoming_request_done
* which may finish multiple identical requests at once but we know chain
* id only of the one requests that actually run its handler.
*
* Therefore we need to set the id to 0 since it is not known at this
* moment, but it is ok. */
old_chain_id = sss_chain_id_set(0);
state = tevent_req_callback_data(subreq, struct sbus_issue_request_state);
sbus_message_meta_read(state->message, &meta);
ret = sbus_incoming_request_recv(state, subreq, &reply);
talloc_zfree(subreq);
if (ret == EOK) {
DEBUG(SSSDBG_TRACE_FUNC, "%s.%s: Success\n",
meta.interface, meta.member);
} else {
int msg_level = SSSDBG_OP_FAILURE;
if (ret == ERR_MISSING_DP_TARGET) msg_level = SSSDBG_FUNC_DATA;
DEBUG(msg_level, "%s.%s: Error [%d]: %s\n",
meta.interface, meta.member, ret, sss_strerror(ret));
}
/* Signals do not send a reply. */
if (state->type == SBUS_REQUEST_SIGNAL) {
goto done;
}
if (ret == EOK) {
/* sbus_reply decreases the refcount of @reply. This usuall means that
* refcount drops to zero and the message is freed. However, under
* special circumstances the refcount is increased inside libdbus,
* the refcount will be 1 when we leave the function and we drop it
* to zero in talloc_free(state) later in this function. This will
* leave an invalid message to be send inside dbus connection and
* eventually crash.
*
* Increasing the refcount here makes sure that the refcount is always
* correct. */
dbus_message_ref(reply);
sbus_reply(state->conn, reply);
} else {
sbus_errno_to_error(state, ret, &error_name, &error_msg);
sbus_reply_error(state->conn, state->message, error_name, error_msg);
}
done:
if (ret == ERR_SBUS_KILL_CONNECTION) {
DEBUG(SSSDBG_TRACE_FUNC, "Handler requested to kill the connection!\n");
sbus_connection_free(state->conn);
}
talloc_free(state);
sss_chain_id_set(old_chain_id);
}
DBusHandlerResult
sbus_method_handler(struct sbus_connection *conn,
struct sbus_router *router,
struct sbus_message_meta *meta,
DBusMessage *message)
{
const struct sbus_method *method;
struct sbus_interface *iface;
TALLOC_CTX *error_ctx;
const char *error_name;
const char *error_msg;
errno_t ret;
DEBUG(SSSDBG_TRACE_INTERNAL, "Received D-Bus method %s.%s on %s\n",
meta->interface, meta->member, meta->path);
/* Mark this connection as active. */
sbus_connection_mark_active(conn);
iface = sbus_router_paths_lookup(router->paths, meta->path,
meta->interface);
if (iface == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, "Unknown interface!\n");
sbus_reply_error(conn, message, DBUS_ERROR_UNKNOWN_INTERFACE,
meta->interface);
return DBUS_HANDLER_RESULT_HANDLED;
}
method = sbus_interface_find_method(iface, meta->member);
if (method == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, "Unknown method!\n");
sbus_reply_error(conn, message, DBUS_ERROR_UNKNOWN_METHOD,
meta->member);
return DBUS_HANDLER_RESULT_HANDLED;
}
sbus_annotation_warn(iface, method);
ret = sbus_issue_request(conn, meta, conn, message, SBUS_REQUEST_METHOD,
&method->invoker, &method->handler);
if (ret != EOK) {
DEBUG(SSSDBG_CRIT_FAILURE, "Unable to issue request [%d]: %s\n",
ret, sss_strerror(ret));
if (ret == ENOMEM) {
return DBUS_HANDLER_RESULT_NEED_MEMORY;
}
error_ctx = talloc_new(NULL);
if (error_ctx == NULL) {
return DBUS_HANDLER_RESULT_NEED_MEMORY;
}
sbus_errno_to_error(error_ctx, ret, &error_name, &error_msg);
sbus_reply_error(conn, message, error_name, error_msg);
talloc_free(error_ctx);
return DBUS_HANDLER_RESULT_HANDLED;
}
return DBUS_HANDLER_RESULT_HANDLED;
}
DBusHandlerResult
sbus_signal_handler(struct sbus_connection *conn,
struct sbus_router *router,
struct sbus_message_meta *meta,
DBusMessage *message)
{
struct sbus_listener_list *list;
struct sbus_listener_list *item;
errno_t ret;
DEBUG(SSSDBG_TRACE_INTERNAL, "Received D-Bus signal %s.%s on %s\n",
meta->interface, meta->member, meta->path);
list = sbus_router_listeners_lookup(router->listeners, meta->interface,
meta->member);
if (list == NULL) {
/* Most probably not fully initialized yet */
DEBUG(SSSDBG_FUNC_DATA, "We do not listen to this signal!\n");
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
DLIST_FOR_EACH(item, list) {
ret = sbus_issue_request(conn, meta, conn, message,
SBUS_REQUEST_SIGNAL,
&item->listener->invoker,
&item->listener->handler);
if (ret != EOK) {
/* Nothing to do, try the next one. */
DEBUG(SSSDBG_CRIT_FAILURE, "Unable to issue request [%d]: %s\n",
ret, sss_strerror(ret));
}
}
return DBUS_HANDLER_RESULT_HANDLED;
}
DBusHandlerResult
sbus_router_filter(struct sbus_connection *conn,
struct sbus_router *router,
DBusMessage *message)
{
struct sbus_message_meta meta;
sbus_message_meta_read(message, &meta);
switch (meta.type) {
case DBUS_MESSAGE_TYPE_SIGNAL:
return sbus_signal_handler(conn, router, &meta, message);
case DBUS_MESSAGE_TYPE_METHOD_CALL:
return sbus_method_handler(conn, router, &meta, message);
case DBUS_MESSAGE_TYPE_METHOD_RETURN:
case DBUS_MESSAGE_TYPE_ERROR:
/* This will be processed by the caller. */
return DBUS_HANDLER_RESULT_HANDLED;
default:
DEBUG(SSSDBG_CRIT_FAILURE, "Invalid message type: %d\n", meta.type);
return DBUS_HANDLER_RESULT_HANDLED;
}
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
DBusHandlerResult
sbus_connection_filter(DBusConnection *dbus_conn,
DBusMessage *message,
void *handler_data)
{
struct sbus_connection *conn;
conn = talloc_get_type(handler_data, struct sbus_connection);
return sbus_router_filter(conn, conn->router, message);
}
|