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
|
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "util.h"
#include "capability.h"
#include "strv.h"
#include "audit.h"
#include "bus-message.h"
#include "bus-internal.h"
#include "bus-type.h"
#include "bus-dump.h"
static char *indent(unsigned level) {
char *p;
p = new(char, 2 + level + 1);
if (!p)
return NULL;
p[0] = p[1] = ' ';
memset(p + 2, '\t', level);
p[2 + level] = 0;
return p;
}
int bus_message_dump(sd_bus_message *m, FILE *f, bool with_header) {
unsigned level = 1;
int r;
assert(m);
if (!f)
f = stdout;
if (with_header) {
fprintf(f,
"%s%s%sType=%s%s%s Endian=%c Flags=%u Version=%u",
m->header->type == SD_BUS_MESSAGE_METHOD_ERROR ? ansi_highlight_red() :
m->header->type == SD_BUS_MESSAGE_METHOD_RETURN ? ansi_highlight_green() :
m->header->type != SD_BUS_MESSAGE_SIGNAL ? ansi_highlight() : "", draw_special_char(DRAW_TRIANGULAR_BULLET), ansi_highlight_off(),
ansi_highlight(), bus_message_type_to_string(m->header->type), ansi_highlight_off(),
m->header->endian,
m->header->flags,
m->header->version);
/* Display synthetic message serial number in a more readable
* format than (uint32_t) -1 */
if (BUS_MESSAGE_COOKIE(m) == 0xFFFFFFFFULL)
fprintf(f, " Cookie=-1");
else
fprintf(f, " Cookie=%lu", (unsigned long) BUS_MESSAGE_COOKIE(m));
if (m->reply_cookie != 0)
fprintf(f, " ReplyCookie=%lu", (unsigned long) m->reply_cookie);
fputs("\n", f);
if (m->sender)
fprintf(f, " Sender=%s%s%s", ansi_highlight(), m->sender, ansi_highlight_off());
if (m->destination)
fprintf(f, " Destination=%s%s%s", ansi_highlight(), m->destination, ansi_highlight_off());
if (m->path)
fprintf(f, " Path=%s%s%s", ansi_highlight(), m->path, ansi_highlight_off());
if (m->interface)
fprintf(f, " Interface=%s%s%s", ansi_highlight(), m->interface, ansi_highlight_off());
if (m->member)
fprintf(f, " Member=%s%s%s", ansi_highlight(), m->member, ansi_highlight_off());
if (m->sender || m->destination || m->path || m->interface || m->member)
fputs("\n", f);
if (sd_bus_error_is_set(&m->error))
fprintf(f,
" ErrorName=%s%s%s"
" ErrorMessage=%s\"%s\"%s\n",
ansi_highlight_red(), strna(m->error.name), ansi_highlight_off(),
ansi_highlight_red(), strna(m->error.message), ansi_highlight_off());
if (m->monotonic != 0)
fprintf(f, " Monotonic=%llu", (unsigned long long) m->monotonic);
if (m->realtime != 0)
fprintf(f, " Realtime=%llu", (unsigned long long) m->realtime);
if (m->seqnum != 0)
fprintf(f, " SequenceNumber=%llu", (unsigned long long) m->seqnum);
if (m->monotonic != 0 || m->realtime != 0 || m->seqnum != 0)
fputs("\n", f);
bus_creds_dump(&m->creds, f);
}
r = sd_bus_message_rewind(m, true);
if (r < 0) {
log_error("Failed to rewind: %s", strerror(-r));
return r;
}
fprintf(f, " MESSAGE \"%s\" {\n", strempty(m->root_container.signature));
for (;;) {
_cleanup_free_ char *prefix = NULL;
const char *contents = NULL;
char type;
union {
uint8_t u8;
uint16_t u16;
int16_t s16;
uint32_t u32;
int32_t s32;
uint64_t u64;
int64_t s64;
double d64;
const char *string;
int i;
} basic;
r = sd_bus_message_peek_type(m, &type, &contents);
if (r < 0) {
log_error("Failed to peek type: %s", strerror(-r));
return r;
}
if (r == 0) {
if (level <= 1)
break;
r = sd_bus_message_exit_container(m);
if (r < 0) {
log_error("Failed to exit container: %s", strerror(-r));
return r;
}
level--;
prefix = indent(level);
if (!prefix)
return log_oom();
fprintf(f, "%s};\n", prefix);
continue;
}
prefix = indent(level);
if (!prefix)
return log_oom();
if (bus_type_is_container(type) > 0) {
r = sd_bus_message_enter_container(m, type, contents);
if (r < 0) {
log_error("Failed to enter container: %s", strerror(-r));
return r;
}
if (type == SD_BUS_TYPE_ARRAY)
fprintf(f, "%sARRAY \"%s\" {\n", prefix, contents);
else if (type == SD_BUS_TYPE_VARIANT)
fprintf(f, "%sVARIANT \"%s\" {\n", prefix, contents);
else if (type == SD_BUS_TYPE_STRUCT)
fprintf(f, "%sSTRUCT \"%s\" {\n", prefix, contents);
else if (type == SD_BUS_TYPE_DICT_ENTRY)
fprintf(f, "%sDICT_ENTRY \"%s\" {\n", prefix, contents);
level ++;
continue;
}
r = sd_bus_message_read_basic(m, type, &basic);
if (r < 0) {
log_error("Failed to get basic: %s", strerror(-r));
return r;
}
assert(r > 0);
switch (type) {
case SD_BUS_TYPE_BYTE:
fprintf(f, "%sBYTE %s%u%s;\n", prefix, ansi_highlight(), basic.u8, ansi_highlight_off());
break;
case SD_BUS_TYPE_BOOLEAN:
fprintf(f, "%sBOOLEAN %s%s%s;\n", prefix, ansi_highlight(), true_false(basic.i), ansi_highlight_off());
break;
case SD_BUS_TYPE_INT16:
fprintf(f, "%sINT16 %s%i%s;\n", prefix, ansi_highlight(), basic.s16, ansi_highlight_off());
break;
case SD_BUS_TYPE_UINT16:
fprintf(f, "%sUINT16 %s%u%s;\n", prefix, ansi_highlight(), basic.u16, ansi_highlight_off());
break;
case SD_BUS_TYPE_INT32:
fprintf(f, "%sINT32 %s%i%s;\n", prefix, ansi_highlight(), basic.s32, ansi_highlight_off());
break;
case SD_BUS_TYPE_UINT32:
fprintf(f, "%sUINT32 %s%u%s;\n", prefix, ansi_highlight(), basic.u32, ansi_highlight_off());
break;
case SD_BUS_TYPE_INT64:
fprintf(f, "%sINT64 %s%lli%s;\n", prefix, ansi_highlight(), (long long) basic.s64, ansi_highlight_off());
break;
case SD_BUS_TYPE_UINT64:
fprintf(f, "%sUINT64 %s%llu%s;\n", prefix, ansi_highlight(), (unsigned long long) basic.u64, ansi_highlight_off());
break;
case SD_BUS_TYPE_DOUBLE:
fprintf(f, "%sDOUBLE %s%g%s;\n", prefix, ansi_highlight(), basic.d64, ansi_highlight_off());
break;
case SD_BUS_TYPE_STRING:
fprintf(f, "%sSTRING \"%s%s%s\";\n", prefix, ansi_highlight(), basic.string, ansi_highlight_off());
break;
case SD_BUS_TYPE_OBJECT_PATH:
fprintf(f, "%sOBJECT_PATH \"%s%s%s\";\n", prefix, ansi_highlight(), basic.string, ansi_highlight_off());
break;
case SD_BUS_TYPE_SIGNATURE:
fprintf(f, "%sSIGNATURE \"%s%s%s\";\n", prefix, ansi_highlight(), basic.string, ansi_highlight_off());
break;
case SD_BUS_TYPE_UNIX_FD:
fprintf(f, "%sUNIX_FD %s%i%s;\n", prefix, ansi_highlight(), basic.i, ansi_highlight_off());
break;
default:
assert_not_reached("Unknown basic type.");
}
}
fprintf(f, " };\n\n");
return 0;
}
static void dump_capabilities(
sd_bus_creds *c,
FILE *f,
const char *name,
int (*has)(sd_bus_creds *c, int capability)) {
unsigned long i, last_cap;
unsigned n = 0;
int r;
assert(c);
assert(f);
assert(name);
assert(has);
i = 0;
r = has(c, i);
if (r < 0)
return;
fprintf(f, " %s=", name);
last_cap = cap_last_cap();
for (;;) {
if (r > 0) {
_cleanup_cap_free_charp_ char *t;
if (n > 0)
fputc(' ', f);
if (n % 4 == 3)
fputs("\n ", f);
t = cap_to_name(i);
fprintf(f, "%s", t);
n++;
}
i++;
if (i > last_cap)
break;
r = has(c, i);
}
fputs("\n", f);
}
int bus_creds_dump(sd_bus_creds *c, FILE *f) {
bool audit_sessionid_is_set = false, audit_loginuid_is_set = false;
const char *u = NULL, *uu = NULL, *s = NULL, *sl = NULL;
uid_t owner, audit_loginuid;
uint32_t audit_sessionid;
char **cmdline = NULL, **well_known = NULL;
int r;
assert(c);
if (!f)
f = stdout;
if (c->mask & SD_BUS_CREDS_PID)
fprintf(f, " PID=%lu", (unsigned long) c->pid);
if (c->mask & SD_BUS_CREDS_PID_STARTTIME)
fprintf(f, " PIDStartTime=%llu", (unsigned long long) c->pid_starttime);
if (c->mask & SD_BUS_CREDS_TID)
fprintf(f, " TID=%lu", (unsigned long) c->tid);
if (c->mask & SD_BUS_CREDS_UID)
fprintf(f, " UID=%lu", (unsigned long) c->uid);
r = sd_bus_creds_get_owner_uid(c, &owner);
if (r >= 0)
fprintf(f, " OwnerUID=%lu", (unsigned long) owner);
if (c->mask & SD_BUS_CREDS_GID)
fprintf(f, " GID=%lu", (unsigned long) c->gid);
if ((c->mask & (SD_BUS_CREDS_PID|SD_BUS_CREDS_PID_STARTTIME|SD_BUS_CREDS_TID|SD_BUS_CREDS_UID|SD_BUS_CREDS_GID)) || r >= 0)
fputs("\n", f);
if (c->mask & SD_BUS_CREDS_EXE)
fprintf(f, " Exe=%s", c->exe);
if (c->mask & SD_BUS_CREDS_COMM)
fprintf(f, " Comm=%s", c->comm);
if (c->mask & SD_BUS_CREDS_TID_COMM)
fprintf(f, " TIDComm=%s", c->tid_comm);
if (c->mask & (SD_BUS_CREDS_EXE|SD_BUS_CREDS_COMM|SD_BUS_CREDS_TID_COMM))
fputs("\n", f);
if (c->mask & SD_BUS_CREDS_SELINUX_CONTEXT)
fprintf(f, " Label=%s", c->label);
if (c->mask & SD_BUS_CREDS_CONNECTION_NAME)
fprintf(f, " ConnectionName=%s", c->conn_name);
if (c->mask & (SD_BUS_CREDS_SELINUX_CONTEXT|SD_BUS_CREDS_CONNECTION_NAME))
fputs("\n", f);
if (sd_bus_creds_get_cmdline(c, &cmdline) >= 0) {
char **i;
fputs(" CommandLine={", f);
STRV_FOREACH(i, cmdline) {
if (i != cmdline)
fputc(' ', f);
fputs(*i, f);
}
fputs("}\n", f);
}
if (c->mask & SD_BUS_CREDS_CGROUP)
fprintf(f, " CGroup=%s", c->cgroup);
sd_bus_creds_get_unit(c, &u);
if (u)
fprintf(f, " Unit=%s", u);
sd_bus_creds_get_user_unit(c, &uu);
if (uu)
fprintf(f, " UserUnit=%s", uu);
sd_bus_creds_get_slice(c, &sl);
if (sl)
fprintf(f, " Slice=%s", sl);
sd_bus_creds_get_session(c, &s);
if (s)
fprintf(f, " Session=%s", s);
if ((c->mask & SD_BUS_CREDS_CGROUP) || u || uu || sl || s)
fputs("\n", f);
if (sd_bus_creds_get_audit_login_uid(c, &audit_loginuid) >= 0) {
audit_loginuid_is_set = true;
fprintf(f, " AuditLoginUID=%lu", (unsigned long) audit_loginuid);
}
if (sd_bus_creds_get_audit_session_id(c, &audit_sessionid) >= 0) {
audit_sessionid_is_set = true;
fprintf(f, " AuditSessionID=%lu", (unsigned long) audit_sessionid);
}
if (audit_loginuid_is_set || audit_sessionid_is_set)
fputs("\n", f);
if (c->mask & SD_BUS_CREDS_UNIQUE_NAME)
fprintf(f, " UniqueName=%s", c->unique_name);
if (sd_bus_creds_get_well_known_names(c, &well_known) >= 0) {
char **i;
fputs(" WellKnownNames={", f);
STRV_FOREACH(i, well_known) {
if (i != well_known)
fputc(' ', f);
fputs(*i, f);
}
fputc('}', f);
}
if (c->mask & SD_BUS_CREDS_UNIQUE_NAME || well_known)
fputc('\n', f);
dump_capabilities(c, f, "EffectiveCapabilities", sd_bus_creds_has_effective_cap);
dump_capabilities(c, f, "PermittedCapabilities", sd_bus_creds_has_permitted_cap);
dump_capabilities(c, f, "InheritableCapabilities", sd_bus_creds_has_inheritable_cap);
dump_capabilities(c, f, "BoundingCapabilities", sd_bus_creds_has_bounding_cap);
return 0;
}
|