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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
|
/*
Authors:
Pavel Březina <pbrezina@redhat.com>
Copyright (C) 2014 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 <dbus/dbus.h>
#include <stdlib.h>
#include <string.h>
#include "lib/sifp/sss_sifp.h"
#include "lib/sifp/sss_sifp_dbus.h"
#include "lib/sifp/sss_sifp_private.h"
#define DBUS_IFACE_PROP "org.freedesktop.DBus.Properties"
static void * default_alloc(size_t size, void *pvt)
{
return malloc(size);
}
static void default_free(void *ptr, void *pvt)
{
free(ptr);
}
static DBusMessage * sss_sifp_create_prop_msg(const char *object_path,
const char *method)
{
return sss_sifp_create_message(object_path, DBUS_IFACE_PROP, method);
}
sss_sifp_error
sss_sifp_init(sss_sifp_ctx **_ctx)
{
return sss_sifp_init_ex(NULL, default_alloc, default_free, _ctx);
}
sss_sifp_error
sss_sifp_init_ex(void *alloc_pvt,
sss_sifp_alloc_func *alloc_func,
sss_sifp_free_func *free_func,
sss_sifp_ctx **_ctx)
{
sss_sifp_ctx *ctx = NULL;
DBusConnection *conn = NULL;
DBusError dbus_error;
sss_sifp_error ret;
if (_ctx == NULL || alloc_func == NULL || free_func == NULL) {
return SSS_SIFP_INVALID_ARGUMENT;
}
dbus_error_init(&dbus_error);
ctx = alloc_func(sizeof(sss_sifp_ctx), alloc_pvt);
if (ctx == NULL) {
ret = SSS_SIFP_OUT_OF_MEMORY;
goto done;
}
ctx->conn = NULL;
ctx->alloc_fn = alloc_func;
ctx->free_fn = free_func;
ctx->alloc_pvt = alloc_pvt;
ctx->io_error = alloc_func(sizeof(DBusError), alloc_pvt);
if (ctx->io_error == NULL) {
ret = SSS_SIFP_OUT_OF_MEMORY;
goto done;
}
*_ctx = ctx;
dbus_error_init(ctx->io_error);
conn = dbus_bus_get(DBUS_BUS_SYSTEM, &dbus_error);
if (dbus_error_is_set(&dbus_error)) {
sss_sifp_set_io_error(ctx, &dbus_error);
ret = SSS_SIFP_IO_ERROR;
goto done;
}
ctx->conn = conn;
ret = SSS_SIFP_OK;
done:
if (ret != SSS_SIFP_OK) {
sss_sifp_free(&ctx);
}
dbus_error_free(&dbus_error);
return ret;
}
const char *
sss_sifp_get_last_io_error_name(sss_sifp_ctx *ctx)
{
if (ctx == NULL) {
return "Invalid sss_sifp context";
}
if (!dbus_error_is_set(ctx->io_error)) {
return NULL;
}
return ctx->io_error->name;
}
const char *
sss_sifp_get_last_io_error_message(sss_sifp_ctx *ctx)
{
if (ctx == NULL) {
return "Invalid sss_sifp context";
}
if (!dbus_error_is_set(ctx->io_error)) {
return NULL;
}
return ctx->io_error->message;
}
const char *
sss_sifp_strerr(sss_sifp_error error)
{
switch (error) {
case SSS_SIFP_OK:
return "Success";
case SSS_SIFP_OUT_OF_MEMORY:
return "Out of memory";
case SSS_SIFP_INVALID_ARGUMENT:
return "Invalid argument";
case SSS_SIFP_IO_ERROR:
return "Communication error";
case SSS_SIFP_INTERNAL_ERROR:
return "Internal error";
case SSS_SIFP_NOT_SUPPORTED:
return "Not supported";
case SSS_SIFP_ATTR_MISSING:
return "Attribute does not exist";
case SSS_SIFP_ATTR_NULL:
return "Attribute does not have any value set";
case SSS_SIFP_INCORRECT_TYPE:
return "Incorrect type";
case SSS_SIFP_ERROR_SENTINEL:
return "Invalid error code";
}
return "Invalid error code";
}
sss_sifp_error
sss_sifp_fetch_attr(sss_sifp_ctx *ctx,
const char *object_path,
const char *interface,
const char *name,
sss_sifp_attr ***_attrs)
{
DBusMessage *msg = NULL;
DBusMessage *reply = NULL;
dbus_bool_t bret;
sss_sifp_error ret;
if (ctx == NULL || object_path == NULL || interface == NULL
|| name == NULL || _attrs == NULL) {
return SSS_SIFP_INVALID_ARGUMENT;
}
/* Message format:
* In: string:interface
* In: string:attribute
* Out: variant(misc:value)
*/
msg = sss_sifp_create_prop_msg(object_path, "Get");
if (msg == NULL) {
ret = SSS_SIFP_OUT_OF_MEMORY;
goto done;
}
bret = dbus_message_append_args(msg, DBUS_TYPE_STRING, &interface,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_INVALID);
if (!bret) {
ret = SSS_SIFP_OUT_OF_MEMORY;
goto done;
}
ret = sss_sifp_send_message(ctx, msg, &reply);
if (ret != SSS_SIFP_OK) {
goto done;
}
ret = sss_sifp_parse_attr(ctx, name, reply, _attrs);
done:
if (msg != NULL) {
dbus_message_unref(msg);
}
if (reply != NULL) {
dbus_message_unref(reply);
}
return ret;
}
sss_sifp_error
sss_sifp_fetch_all_attrs(sss_sifp_ctx *ctx,
const char *object_path,
const char *interface,
sss_sifp_attr ***_attrs)
{
DBusMessage *msg = NULL;
DBusMessage *reply = NULL;
dbus_bool_t bret;
sss_sifp_error ret;
if (ctx == NULL || object_path == NULL || interface == NULL
|| _attrs == NULL) {
return SSS_SIFP_INVALID_ARGUMENT;
}
msg = sss_sifp_create_prop_msg(object_path, "GetAll");
if (msg == NULL) {
ret = SSS_SIFP_OUT_OF_MEMORY;
goto done;
}
bret = dbus_message_append_args(msg, DBUS_TYPE_STRING, &interface,
DBUS_TYPE_INVALID);
if (!bret) {
ret = SSS_SIFP_OUT_OF_MEMORY;
goto done;
}
ret = sss_sifp_send_message(ctx, msg, &reply);
if (ret != SSS_SIFP_OK) {
goto done;
}
ret = sss_sifp_parse_attr_list(ctx, reply, _attrs);
done:
if (msg != NULL) {
dbus_message_unref(msg);
}
if (reply != NULL) {
dbus_message_unref(reply);
}
return ret;
}
sss_sifp_error
sss_sifp_fetch_object(sss_sifp_ctx *ctx,
const char *object_path,
const char *interface,
sss_sifp_object **_object)
{
sss_sifp_object *object = NULL;
sss_sifp_attr **attrs = NULL;
const char *name = NULL;
sss_sifp_error ret;
if (ctx == NULL || object_path == NULL || interface == NULL
|| _object == NULL) {
return SSS_SIFP_INVALID_ARGUMENT;
}
ret = sss_sifp_fetch_all_attrs(ctx, object_path, interface, &attrs);
if (ret != SSS_SIFP_OK) {
goto done;
}
ret = sss_sifp_find_attr_as_string(attrs, "name", &name);
if (ret != SSS_SIFP_OK) {
goto done;
}
object = _alloc_zero(ctx, sss_sifp_object, 1);
if (object == NULL) {
ret = SSS_SIFP_OUT_OF_MEMORY;
goto done;
}
object->attrs = attrs;
object->name = sss_sifp_strdup(ctx, name);
if (object->name == NULL) {
ret = SSS_SIFP_OUT_OF_MEMORY;
goto done;
}
object->object_path = sss_sifp_strdup(ctx, object_path);
if (object->object_path == NULL) {
ret = SSS_SIFP_OUT_OF_MEMORY;
goto done;
}
object->interface = sss_sifp_strdup(ctx, interface);
if (object->interface == NULL) {
ret = SSS_SIFP_OUT_OF_MEMORY;
goto done;
}
*_object = object;
ret = SSS_SIFP_OK;
done:
if (ret != SSS_SIFP_OK) {
sss_sifp_free_object(ctx, &object);
}
return ret;
}
void
sss_sifp_free(sss_sifp_ctx **_ctx)
{
sss_sifp_ctx *ctx = NULL;
if (_ctx == NULL || *_ctx == NULL) {
return;
}
ctx = *_ctx;
if (ctx->conn != NULL) {
dbus_connection_unref(ctx->conn);
}
if (ctx->io_error != NULL) {
dbus_error_free(ctx->io_error);
_free(ctx, ctx->io_error);
}
_free(ctx, ctx);
*_ctx = NULL;
return;
}
void
sss_sifp_free_attrs(sss_sifp_ctx *ctx,
sss_sifp_attr ***_attrs)
{
sss_sifp_attr **attrs = NULL;
unsigned int i, j;
if (_attrs == NULL || *_attrs == NULL) {
return;
}
attrs = *_attrs;
for (i = 0; attrs[i] != NULL; i++) {
switch (attrs[i]->type) {
case SSS_SIFP_ATTR_TYPE_BOOL:
_free(ctx, attrs[i]->data.boolean);
break;
case SSS_SIFP_ATTR_TYPE_INT16:
_free(ctx, attrs[i]->data.int16);
break;
case SSS_SIFP_ATTR_TYPE_UINT16:
_free(ctx, attrs[i]->data.uint16);
break;
case SSS_SIFP_ATTR_TYPE_INT32:
_free(ctx, attrs[i]->data.int32);
break;
case SSS_SIFP_ATTR_TYPE_UINT32:
_free(ctx, attrs[i]->data.uint32);
break;
case SSS_SIFP_ATTR_TYPE_INT64:
_free(ctx, attrs[i]->data.int64);
break;
case SSS_SIFP_ATTR_TYPE_UINT64:
_free(ctx, attrs[i]->data.uint64);
break;
case SSS_SIFP_ATTR_TYPE_STRING:
for (j = 0; j < attrs[i]->num_values; j++) {
_free(ctx, attrs[i]->data.str[j]);
}
_free(ctx, attrs[i]->data.str);
break;
case SSS_SIFP_ATTR_TYPE_STRING_DICT:
if (attrs[i]->data.str_dict != NULL) {
hash_destroy(attrs[i]->data.str_dict);
}
attrs[i]->data.str_dict = NULL;
break;
}
_free(ctx, attrs[i]->name);
_free(ctx, attrs[i]);
}
_free(ctx, attrs);
*_attrs = NULL;
}
void
sss_sifp_free_object(sss_sifp_ctx *ctx,
sss_sifp_object **_object)
{
sss_sifp_object *object = NULL;
if (_object == NULL || *_object == NULL) {
return;
}
object = *_object;
sss_sifp_free_attrs(ctx, &object->attrs);
_free(ctx, object->object_path);
_free(ctx, object->interface);
_free(ctx, object->name);
_free(ctx, object);
*_object = NULL;
}
void
sss_sifp_free_string(sss_sifp_ctx *ctx,
char **_str)
{
if (_str == NULL || *_str == NULL) {
return;
}
_free(ctx, *_str);
*_str = NULL;
}
void
sss_sifp_free_string_array(sss_sifp_ctx *ctx,
char ***_str_array)
{
char **str_array = NULL;
int i;
if (_str_array == NULL || *_str_array == NULL) {
return;
}
str_array = *_str_array;
for (i = 0; str_array[i] != NULL; i++) {
_free(ctx, str_array[i]);
}
_free(ctx, str_array);
*_str_array = NULL;
}
|