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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
/*
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 <dhash.h>
#include <stdint.h>
#include <string.h>
#include <dbus/dbus.h>
#include "util/util.h"
#include "util/sss_ptr_hash.h"
#include "sbus/sbus_private.h"
#include "sbus/interface_dbus/sbus_dbus_client_async.h"
struct sbus_sender *
sbus_sender_create(TALLOC_CTX *mem_ctx,
const char *name,
int64_t uid)
{
struct sbus_sender sender = {.name = name, .uid = uid};
if (name == NULL) {
return NULL;
}
return sbus_sender_copy(mem_ctx, &sender);
}
struct sbus_sender *
sbus_sender_copy(TALLOC_CTX *mem_ctx,
const struct sbus_sender *input)
{
struct sbus_sender *copy;
copy = talloc_zero(mem_ctx, struct sbus_sender);
if (copy == NULL) {
return NULL;
}
copy->name = talloc_strdup(copy, input->name);
if (copy->name == NULL) {
talloc_free(copy);
return NULL;
}
copy->uid = input->uid;
return copy;
}
hash_table_t *
sbus_senders_init(TALLOC_CTX *mem_ctx)
{
return sss_ptr_hash_create(mem_ctx, NULL, NULL);
}
static errno_t
sbus_senders_add(hash_table_t *table,
struct sbus_sender *sender)
{
struct sbus_sender *copy;
DEBUG(SSSDBG_TRACE_INTERNAL, "Inserting identity of sender [%s]: %"PRIi64"\n",
sender->name, sender->uid);
copy = sbus_sender_copy(table, sender);
if (copy == NULL) {
return ENOMEM;
}
return sss_ptr_hash_add(table, sender->name, copy, struct sbus_sender);
}
static struct sbus_sender *
sbus_senders_lookup(hash_table_t *table,
const char *name)
{
DEBUG(SSSDBG_TRACE_INTERNAL, "Looking for identity of sender [%s]\n",
name);
return sss_ptr_hash_lookup(table, name, struct sbus_sender);
}
void
sbus_senders_delete(hash_table_t *table,
const char *name)
{
if (sss_ptr_hash_has_key(table, name)) {
DEBUG(SSSDBG_TRACE_INTERNAL, "Removing identity of sender [%s]\n",
name);
sss_ptr_hash_delete(table, name, true);
}
}
errno_t
sbus_sender_check_input(TALLOC_CTX *mem_ctx,
struct sbus_connection *conn,
enum sbus_request_type type,
const char *destination,
const char *object_path,
const char *interface,
const char *member,
const char *name,
struct sbus_sender **_sender)
{
/* This is a server call. We do not need to resolve sender in this case. */
if (destination != NULL && strcmp(destination, DBUS_SERVICE_DBUS) == 0) {
return EOK;
}
/* Hello is a special method that is used by clients to register on the
* bus. Upon registration server assigns unique name to the clients.
* Therefore it is not actually possible to resolve a sender name
* prior this call. */
if (name == NULL && type == SBUS_REQUEST_METHOD
&& strcmp(object_path, DBUS_PATH_DBUS) == 0
&& strcmp(interface, DBUS_INTERFACE_DBUS) == 0
&& strcmp(member, "Hello") == 0) {
*_sender = sbus_sender_create(mem_ctx, name, SBUS_SENDER_HELLO);
if (*_sender == NULL) {
return ENOMEM;
}
return EOK;
}
if (name == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, "Can not resolve empty name!\n");
return EINVAL;
}
/* Got signal from bus, this is OK. This name is not really resolvable. */
if (strcmp(name, DBUS_SERVICE_DBUS) == 0) {
*_sender = sbus_sender_create(mem_ctx, name, SBUS_SENDER_DBUS);
if (*_sender == NULL) {
return ENOMEM;
}
return EOK;
}
return EAGAIN;
}
struct sbus_sender_resolve_state {
struct sbus_connection *conn;
enum sbus_request_type type;
struct sbus_sender *sender;
const char *name;
};
static void sbus_sender_resolve_done(struct tevent_req *subreq);
struct tevent_req *
sbus_sender_resolve_send(TALLOC_CTX *mem_ctx,
struct tevent_context *ev,
struct sbus_connection *conn,
enum sbus_request_type type,
const char *destination,
const char *object_path,
const char *interface,
const char *member,
const char *name)
{
struct sbus_sender_resolve_state *state;
struct sbus_sender *sender;
struct tevent_req *subreq;
struct tevent_req *req;
errno_t ret;
req = tevent_req_create(mem_ctx, &state, struct sbus_sender_resolve_state);
if (req == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, "Unable to create tevent request!\n");
return NULL;
}
state->conn = conn;
state->type = type;
state->sender = NULL;
ret = sbus_sender_check_input(state, conn, type, destination, object_path,
interface, member, name, &state->sender);
if (ret != EAGAIN) {
goto done;
}
/* Check if the sender is already known. If yes, we must create a copy
* of it since it may be asynchronously deleted through NameOwnerChanged
* signal. */
sender = sbus_senders_lookup(conn->senders, name);
if (sender != NULL) {
state->sender = sbus_sender_copy(state, sender);
if (state->sender == NULL) {
ret = ENOMEM;
goto done;
}
ret = EOK;
goto done;
}
state->name = talloc_strdup(state, name);
if (state->name == NULL) {
ret = ENOMEM;
goto done;
}
subreq = sbus_call_DBus_GetConnectionUnixUser_send(state, conn,
DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, name);
if (subreq == NULL) {
DEBUG(SSSDBG_CRIT_FAILURE, "Unable to create subrequest!\n");
ret = ENOMEM;
goto done;
}
tevent_req_set_callback(subreq, sbus_sender_resolve_done, req);
ret = EAGAIN;
done:
if (ret == EOK) {
tevent_req_done(req);
tevent_req_post(req, ev);
} else if (ret != EAGAIN) {
tevent_req_error(req, ret);
tevent_req_post(req, ev);
}
return req;
}
static void sbus_sender_resolve_done(struct tevent_req *subreq)
{
struct sbus_sender_resolve_state *state;
struct sbus_sender *sender;
struct tevent_req *req;
uint32_t uid;
errno_t ret;
req = tevent_req_callback_data(subreq, struct tevent_req);
state = tevent_req_data(req, struct sbus_sender_resolve_state);
ret = sbus_call_DBus_GetConnectionUnixUser_recv(subreq, &uid);
talloc_zfree(subreq);
if (ret == ERR_SBUS_UNKNOWN_OWNER && state->type == SBUS_REQUEST_SIGNAL) {
/* If the caller of the signal exits before we translate the name,
* it is possible that the name is no longer known on the bus.
* E.g. when the signal is sent via dbus-send. */
DEBUG(SSSDBG_MINOR_FAILURE, "Identity of signal sender "
"[%s] is not known. Continue without it.\n", state->name);
state->sender = sbus_sender_create(state, state->name,
SBUS_SENDER_SIGNAL);
if (state->sender == NULL) {
ret = ENOMEM;
goto done;
}
ret = EOK;
goto done;
} else if (ret != EOK) {
goto done;
}
/* We don't have request chaining on this level so it is possible that
* a concurrent lookup finished first. If this is this case, we return
* the previous lookup result and just finish.
*
* We must create a copy of the result since it may be asynchronously
* deleted through NameOwnerChanged signal. */
sender = sbus_senders_lookup(state->conn->senders, state->name);
if (sender != NULL) {
state->sender = sbus_sender_copy(state, sender);
if (state->sender == NULL) {
ret = ENOMEM;
goto done;
}
ret = EOK;
goto done;
}
/* Otherwise we insert this result into the table. The add operation
* will create a copy of this structure so we can return state->sender
* directly in the result. */
state->sender = sbus_sender_create(state, state->name, uid);
if (state->sender == NULL) {
ret = ENOMEM;
goto done;
}
ret = sbus_senders_add(state->conn->senders, state->sender);
if (ret != EOK) {
goto done;
}
ret = EOK;
done:
if (ret != EOK) {
tevent_req_error(req, ret);
return;
}
tevent_req_done(req);
}
errno_t
sbus_sender_resolve_recv(TALLOC_CTX *mem_ctx,
struct tevent_req *req,
struct sbus_sender **_sender)
{
struct sbus_sender_resolve_state *state;
state = tevent_req_data(req, struct sbus_sender_resolve_state);
TEVENT_REQ_RETURN_ON_ERROR(req);
if (_sender) {
*_sender = talloc_steal(mem_ctx, state->sender);
}
return EOK;
}
|