| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-bus.h" |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "ansi-color.h" |
| 7 |
#include "bus-dump.h" |
| 8 |
#include "bus-internal.h" |
| 9 |
#include "bus-message.h" |
| 10 |
#include "bus-type.h" |
| 11 |
#include "capability-list.h" |
| 12 |
#include "capability-util.h" |
| 13 |
#include "fileio.h" |
| 14 |
#include "format-util.h" |
| 15 |
#include "glyph-util.h" |
| 16 |
#include "log.h" |
| 17 |
#include "pcapng.h" |
| 18 |
#include "string-util.h" |
| 19 |
#include "strv.h" |
| 20 |
#include "time-util.h" |
| 21 |
|
| 22 |
static char* indent(unsigned level, uint64_t flags) { |
| 23 |
if (FLAGS_SET(flags, SD_BUS_MESSAGE_DUMP_SUBTREE_ONLY) && level > 0) |
| 24 |
level -= 1; |
| 25 |
|
| 26 |
unsigned n = level * 8; |
| 27 |
if (FLAGS_SET(flags, SD_BUS_MESSAGE_DUMP_WITH_HEADER)) |
| 28 |
n += 2; |
| 29 |
|
| 30 |
char *p = new(char, n + 1); |
| 31 |
if (!p) |
| 32 |
return NULL; |
| 33 |
|
| 34 |
memset(p, ' ', n); |
| 35 |
p[n] = '\0'; |
| 36 |
|
| 37 |
return p; |
| 38 |
} |
| 39 |
|
| 40 |
_public_ int sd_bus_message_dump(sd_bus_message *m, FILE *f, uint64_t flags) { |
| 41 |
unsigned level = 1; |
| 42 |
int r; |
| 43 |
|
| 44 |
assert_return(m, -EINVAL); |
| 45 |
assert_return((flags & ~_SD_BUS_MESSAGE_DUMP_KNOWN_FLAGS) == 0, -EINVAL); |
| 46 |
|
| 47 |
if (!f) |
| 48 |
f = stdout; |
| 49 |
|
| 50 |
if (FLAGS_SET(flags, SD_BUS_MESSAGE_DUMP_WITH_HEADER)) { |
| 51 |
usec_t ts = m->realtime; |
| 52 |
|
| 53 |
if (ts == 0) |
| 54 |
ts = now(CLOCK_REALTIME); |
| 55 |
|
| 56 |
fprintf(f, |
| 57 |
"%s%s%s Type=%s%s%s Endian=%c Flags=%u Version=%u", |
| 58 |
m->header->type == SD_BUS_MESSAGE_METHOD_ERROR ? ansi_highlight_red() : |
| 59 |
m->header->type == SD_BUS_MESSAGE_METHOD_RETURN ? ansi_highlight_green() : |
| 60 |
m->header->type != SD_BUS_MESSAGE_SIGNAL ? ansi_highlight() : "", |
| 61 |
glyph(GLYPH_TRIANGULAR_BULLET), |
| 62 |
ansi_normal(), |
| 63 |
|
| 64 |
ansi_highlight(), |
| 65 |
bus_message_type_to_string(m->header->type) ?: "(unknown)", |
| 66 |
ansi_normal(), |
| 67 |
|
| 68 |
m->header->endian, |
| 69 |
m->header->flags, |
| 70 |
m->header->version); |
| 71 |
|
| 72 |
/* Display synthetic message serial number in a more readable |
| 73 |
* format than UINT32_MAX */ |
| 74 |
if (BUS_MESSAGE_COOKIE(m) == UINT32_MAX) |
| 75 |
fprintf(f, " Cookie=-1"); |
| 76 |
else |
| 77 |
fprintf(f, " Cookie=%" PRIu64, BUS_MESSAGE_COOKIE(m)); |
| 78 |
|
| 79 |
if (m->reply_cookie != 0) |
| 80 |
fprintf(f, " ReplyCookie=%" PRIu64, m->reply_cookie); |
| 81 |
|
| 82 |
fprintf(f, " Timestamp=\"%s\"\n", strna(FORMAT_TIMESTAMP_STYLE(ts, TIMESTAMP_US_UTC))); |
| 83 |
|
| 84 |
if (m->sender) |
| 85 |
fprintf(f, " Sender=%s%s%s", ansi_highlight(), m->sender, ansi_normal()); |
| 86 |
if (m->destination) |
| 87 |
fprintf(f, " Destination=%s%s%s", ansi_highlight(), m->destination, ansi_normal()); |
| 88 |
if (m->path) |
| 89 |
fprintf(f, " Path=%s%s%s", ansi_highlight(), m->path, ansi_normal()); |
| 90 |
if (m->interface) |
| 91 |
fprintf(f, " Interface=%s%s%s", ansi_highlight(), m->interface, ansi_normal()); |
| 92 |
if (m->member) |
| 93 |
fprintf(f, " Member=%s%s%s", ansi_highlight(), m->member, ansi_normal()); |
| 94 |
|
| 95 |
if (m->sender || m->destination || m->path || m->interface || m->member) |
| 96 |
fputs("\n", f); |
| 97 |
|
| 98 |
if (sd_bus_error_is_set(&m->error)) |
| 99 |
fprintf(f, |
| 100 |
" ErrorName=%s%s%s" |
| 101 |
" ErrorMessage=%s\"%s\"%s\n", |
| 102 |
ansi_highlight_red(), strna(m->error.name), ansi_normal(), |
| 103 |
ansi_highlight_red(), strna(m->error.message), ansi_normal()); |
| 104 |
|
| 105 |
if (m->monotonic != 0) |
| 106 |
fprintf(f, " Monotonic="USEC_FMT, m->monotonic); |
| 107 |
if (m->realtime != 0) |
| 108 |
fprintf(f, " Realtime="USEC_FMT, m->realtime); |
| 109 |
if (m->seqnum != 0) |
| 110 |
fprintf(f, " SequenceNumber=%"PRIu64, m->seqnum); |
| 111 |
|
| 112 |
if (m->monotonic != 0 || m->realtime != 0 || m->seqnum != 0) |
| 113 |
fputs("\n", f); |
| 114 |
|
| 115 |
bus_creds_dump(&m->creds, f, true); |
| 116 |
} |
| 117 |
|
| 118 |
r = sd_bus_message_rewind(m, !FLAGS_SET(flags, SD_BUS_MESSAGE_DUMP_SUBTREE_ONLY)); |
| 119 |
if (r < 0) |
| 120 |
return log_debug_errno(r, "Failed to rewind: %m"); |
| 121 |
|
| 122 |
if (!FLAGS_SET(flags, SD_BUS_MESSAGE_DUMP_SUBTREE_ONLY)) { |
| 123 |
_cleanup_free_ char *prefix = NULL; |
| 124 |
|
| 125 |
prefix = indent(0, flags); |
| 126 |
if (!prefix) |
| 127 |
return log_oom_debug(); |
| 128 |
|
| 129 |
fprintf(f, "%sMESSAGE \"%s\" {\n", prefix, strempty(m->root_container.signature)); |
| 130 |
} |
| 131 |
|
| 132 |
for (;;) { |
| 133 |
_cleanup_free_ char *prefix = NULL; |
| 134 |
const char *contents = NULL; |
| 135 |
char type; |
| 136 |
union { |
| 137 |
uint8_t u8; |
| 138 |
uint16_t u16; |
| 139 |
int16_t s16; |
| 140 |
uint32_t u32; |
| 141 |
int32_t s32; |
| 142 |
uint64_t u64; |
| 143 |
int64_t s64; |
| 144 |
double d64; |
| 145 |
const char *string; |
| 146 |
int i; |
| 147 |
} basic; |
| 148 |
|
| 149 |
r = sd_bus_message_peek_type(m, &type, &contents); |
| 150 |
if (r < 0) |
| 151 |
return log_debug_errno(r, "Failed to peek type: %m"); |
| 152 |
|
| 153 |
if (r == 0) { |
| 154 |
if (level <= 1) |
| 155 |
break; |
| 156 |
|
| 157 |
r = sd_bus_message_exit_container(m); |
| 158 |
if (r < 0) |
| 159 |
return log_debug_errno(r, "Failed to exit container: %m"); |
| 160 |
|
| 161 |
level--; |
| 162 |
|
| 163 |
prefix = indent(level, flags); |
| 164 |
if (!prefix) |
| 165 |
return log_oom_debug(); |
| 166 |
|
| 167 |
fprintf(f, "%s};\n", prefix); |
| 168 |
continue; |
| 169 |
} |
| 170 |
|
| 171 |
prefix = indent(level, flags); |
| 172 |
if (!prefix) |
| 173 |
return log_oom_debug(); |
| 174 |
|
| 175 |
if (bus_type_is_container(type) > 0) { |
| 176 |
r = sd_bus_message_enter_container(m, type, contents); |
| 177 |
if (r < 0) |
| 178 |
return log_debug_errno(r, "Failed to enter container: %m"); |
| 179 |
|
| 180 |
if (type == SD_BUS_TYPE_ARRAY) |
| 181 |
fprintf(f, "%sARRAY \"%s\" {\n", prefix, contents); |
| 182 |
else if (type == SD_BUS_TYPE_VARIANT) |
| 183 |
fprintf(f, "%sVARIANT \"%s\" {\n", prefix, contents); |
| 184 |
else if (type == SD_BUS_TYPE_STRUCT) |
| 185 |
fprintf(f, "%sSTRUCT \"%s\" {\n", prefix, contents); |
| 186 |
else if (type == SD_BUS_TYPE_DICT_ENTRY) |
| 187 |
fprintf(f, "%sDICT_ENTRY \"%s\" {\n", prefix, contents); |
| 188 |
|
| 189 |
level++; |
| 190 |
|
| 191 |
continue; |
| 192 |
} |
| 193 |
|
| 194 |
r = sd_bus_message_read_basic(m, type, &basic); |
| 195 |
if (r < 0) |
| 196 |
return log_debug_errno(r, "Failed to get basic: %m"); |
| 197 |
|
| 198 |
assert(r > 0); |
| 199 |
|
| 200 |
switch (type) { |
| 201 |
|
| 202 |
case SD_BUS_TYPE_BYTE: |
| 203 |
fprintf(f, "%sBYTE %s%u%s;\n", prefix, ansi_highlight(), basic.u8, ansi_normal()); |
| 204 |
break; |
| 205 |
|
| 206 |
case SD_BUS_TYPE_BOOLEAN: |
| 207 |
fprintf(f, "%sBOOLEAN %s%s%s;\n", prefix, ansi_highlight(), true_false(basic.i), ansi_normal()); |
| 208 |
break; |
| 209 |
|
| 210 |
case SD_BUS_TYPE_INT16: |
| 211 |
fprintf(f, "%sINT16 %s%i%s;\n", prefix, ansi_highlight(), basic.s16, ansi_normal()); |
| 212 |
break; |
| 213 |
|
| 214 |
case SD_BUS_TYPE_UINT16: |
| 215 |
fprintf(f, "%sUINT16 %s%u%s;\n", prefix, ansi_highlight(), basic.u16, ansi_normal()); |
| 216 |
break; |
| 217 |
|
| 218 |
case SD_BUS_TYPE_INT32: |
| 219 |
fprintf(f, "%sINT32 %s%i%s;\n", prefix, ansi_highlight(), basic.s32, ansi_normal()); |
| 220 |
break; |
| 221 |
|
| 222 |
case SD_BUS_TYPE_UINT32: |
| 223 |
fprintf(f, "%sUINT32 %s%u%s;\n", prefix, ansi_highlight(), basic.u32, ansi_normal()); |
| 224 |
break; |
| 225 |
|
| 226 |
case SD_BUS_TYPE_INT64: |
| 227 |
fprintf(f, "%sINT64 %s%"PRIi64"%s;\n", prefix, ansi_highlight(), basic.s64, ansi_normal()); |
| 228 |
break; |
| 229 |
|
| 230 |
case SD_BUS_TYPE_UINT64: |
| 231 |
fprintf(f, "%sUINT64 %s%"PRIu64"%s;\n", prefix, ansi_highlight(), basic.u64, ansi_normal()); |
| 232 |
break; |
| 233 |
|
| 234 |
case SD_BUS_TYPE_DOUBLE: |
| 235 |
fprintf(f, "%sDOUBLE %s%g%s;\n", prefix, ansi_highlight(), basic.d64, ansi_normal()); |
| 236 |
break; |
| 237 |
|
| 238 |
case SD_BUS_TYPE_STRING: |
| 239 |
fprintf(f, "%sSTRING \"%s%s%s\";\n", prefix, ansi_highlight(), basic.string, ansi_normal()); |
| 240 |
break; |
| 241 |
|
| 242 |
case SD_BUS_TYPE_OBJECT_PATH: |
| 243 |
fprintf(f, "%sOBJECT_PATH \"%s%s%s\";\n", prefix, ansi_highlight(), basic.string, ansi_normal()); |
| 244 |
break; |
| 245 |
|
| 246 |
case SD_BUS_TYPE_SIGNATURE: |
| 247 |
fprintf(f, "%sSIGNATURE \"%s%s%s\";\n", prefix, ansi_highlight(), basic.string, ansi_normal()); |
| 248 |
break; |
| 249 |
|
| 250 |
case SD_BUS_TYPE_UNIX_FD: |
| 251 |
fprintf(f, "%sUNIX_FD %s%i%s;\n", prefix, ansi_highlight(), basic.i, ansi_normal()); |
| 252 |
break; |
| 253 |
|
| 254 |
default: |
| 255 |
assert_not_reached(); |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
if (!FLAGS_SET(flags, SD_BUS_MESSAGE_DUMP_SUBTREE_ONLY)) { |
| 260 |
_cleanup_free_ char *prefix = NULL; |
| 261 |
|
| 262 |
prefix = indent(0, flags); |
| 263 |
if (!prefix) |
| 264 |
return log_oom_debug(); |
| 265 |
|
| 266 |
fprintf(f, "%s};\n\n", prefix); |
| 267 |
} |
| 268 |
|
| 269 |
return 0; |
| 270 |
} |
| 271 |
|
| 272 |
static void dump_capabilities( |
| 273 |
sd_bus_creds *c, |
| 274 |
FILE *f, |
| 275 |
const char *name, |
| 276 |
bool terse, |
| 277 |
int (*has)(sd_bus_creds *c, int capability)) { |
| 278 |
|
| 279 |
unsigned long i, last_cap; |
| 280 |
unsigned n = 0; |
| 281 |
int r; |
| 282 |
|
| 283 |
assert(c); |
| 284 |
assert(f); |
| 285 |
assert(name); |
| 286 |
assert(has); |
| 287 |
|
| 288 |
i = 0; |
| 289 |
r = has(c, i); |
| 290 |
if (r < 0) |
| 291 |
return; |
| 292 |
|
| 293 |
fprintf(f, "%s%s=%s", terse ? " " : "", name, terse ? "" : ansi_highlight()); |
| 294 |
last_cap = cap_last_cap(); |
| 295 |
|
| 296 |
for (;;) { |
| 297 |
if (r > 0) { |
| 298 |
|
| 299 |
if (n > 0) |
| 300 |
fputc(' ', f); |
| 301 |
if (n % 4 == 3) |
| 302 |
fprintf(f, terse ? "\n " : "\n "); |
| 303 |
|
| 304 |
fprintf(f, "%s", strna(capability_to_name(i))); |
| 305 |
n++; |
| 306 |
} |
| 307 |
|
| 308 |
i++; |
| 309 |
|
| 310 |
if (i > last_cap) |
| 311 |
break; |
| 312 |
|
| 313 |
r = has(c, i); |
| 314 |
} |
| 315 |
|
| 316 |
fputs("\n", f); |
| 317 |
|
| 318 |
if (!terse) |
| 319 |
fputs(ansi_normal(), f); |
| 320 |
} |
| 321 |
|
| 322 |
int bus_creds_dump(sd_bus_creds *c, FILE *f, bool terse) { |
| 323 |
uid_t owner, audit_loginuid; |
| 324 |
uint32_t audit_sessionid; |
| 325 |
char **cmdline = NULL, **well_known = NULL; |
| 326 |
const char *prefix, *color, *suffix, *s; |
| 327 |
int r, q, v, w, z; |
| 328 |
|
| 329 |
assert(c); |
| 330 |
|
| 331 |
if (!f) |
| 332 |
f = stdout; |
| 333 |
|
| 334 |
if (terse) { |
| 335 |
prefix = " "; |
| 336 |
suffix = ""; |
| 337 |
color = ""; |
| 338 |
} else { |
| 339 |
const char *off; |
| 340 |
|
| 341 |
prefix = ""; |
| 342 |
color = ansi_highlight(); |
| 343 |
|
| 344 |
off = ansi_normal(); |
| 345 |
suffix = strjoina(off, "\n"); |
| 346 |
} |
| 347 |
|
| 348 |
if (c->mask & SD_BUS_CREDS_PID) |
| 349 |
fprintf(f, "%sPID=%s"PID_FMT"%s", prefix, color, c->pid, suffix); |
| 350 |
if (c->mask & SD_BUS_CREDS_PIDFD) |
| 351 |
fprintf(f, "%sPIDFD=%syes%s", prefix, color, suffix); |
| 352 |
if (c->mask & SD_BUS_CREDS_TID) |
| 353 |
fprintf(f, "%sTID=%s"PID_FMT"%s", prefix, color, c->tid, suffix); |
| 354 |
if (c->mask & SD_BUS_CREDS_PPID) { |
| 355 |
if (c->ppid == 0) |
| 356 |
fprintf(f, "%sPPID=%sn/a%s", prefix, color, suffix); |
| 357 |
else |
| 358 |
fprintf(f, "%sPPID=%s"PID_FMT"%s", prefix, color, c->ppid, suffix); |
| 359 |
} |
| 360 |
if (c->mask & SD_BUS_CREDS_TTY) |
| 361 |
fprintf(f, "%sTTY=%s%s%s", prefix, color, strna(c->tty), suffix); |
| 362 |
|
| 363 |
if (terse && ((c->mask & (SD_BUS_CREDS_PID|SD_BUS_CREDS_TID|SD_BUS_CREDS_PPID|SD_BUS_CREDS_TTY)))) |
| 364 |
fputs("\n", f); |
| 365 |
|
| 366 |
if (c->mask & SD_BUS_CREDS_UID) |
| 367 |
fprintf(f, "%sUID=%s"UID_FMT"%s", prefix, color, c->uid, suffix); |
| 368 |
if (c->mask & SD_BUS_CREDS_EUID) |
| 369 |
fprintf(f, "%sEUID=%s"UID_FMT"%s", prefix, color, c->euid, suffix); |
| 370 |
if (c->mask & SD_BUS_CREDS_SUID) |
| 371 |
fprintf(f, "%sSUID=%s"UID_FMT"%s", prefix, color, c->suid, suffix); |
| 372 |
if (c->mask & SD_BUS_CREDS_FSUID) |
| 373 |
fprintf(f, "%sFSUID=%s"UID_FMT"%s", prefix, color, c->fsuid, suffix); |
| 374 |
r = sd_bus_creds_get_owner_uid(c, &owner); |
| 375 |
if (r >= 0) |
| 376 |
fprintf(f, "%sOwnerUID=%s"UID_FMT"%s", prefix, color, owner, suffix); |
| 377 |
if (c->mask & SD_BUS_CREDS_GID) |
| 378 |
fprintf(f, "%sGID=%s"GID_FMT"%s", prefix, color, c->gid, suffix); |
| 379 |
if (c->mask & SD_BUS_CREDS_EGID) |
| 380 |
fprintf(f, "%sEGID=%s"GID_FMT"%s", prefix, color, c->egid, suffix); |
| 381 |
if (c->mask & SD_BUS_CREDS_SGID) |
| 382 |
fprintf(f, "%sSGID=%s"GID_FMT"%s", prefix, color, c->sgid, suffix); |
| 383 |
if (c->mask & SD_BUS_CREDS_FSGID) |
| 384 |
fprintf(f, "%sFSGID=%s"GID_FMT"%s", prefix, color, c->fsgid, suffix); |
| 385 |
|
| 386 |
if (c->mask & SD_BUS_CREDS_SUPPLEMENTARY_GIDS) { |
| 387 |
fprintf(f, "%sSupplementaryGIDs=%s", prefix, color); |
| 388 |
for (unsigned i = 0; i < c->n_supplementary_gids; i++) |
| 389 |
fprintf(f, "%s" GID_FMT, i > 0 ? " " : "", c->supplementary_gids[i]); |
| 390 |
fprintf(f, "%s", suffix); |
| 391 |
} |
| 392 |
|
| 393 |
if (terse && ((c->mask & (SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_SUID|SD_BUS_CREDS_FSUID| |
| 394 |
SD_BUS_CREDS_GID|SD_BUS_CREDS_EGID|SD_BUS_CREDS_SGID|SD_BUS_CREDS_FSGID| |
| 395 |
SD_BUS_CREDS_SUPPLEMENTARY_GIDS)) || r >= 0)) |
| 396 |
fputs("\n", f); |
| 397 |
|
| 398 |
if (c->mask & SD_BUS_CREDS_COMM) |
| 399 |
fprintf(f, "%sComm=%s%s%s", prefix, color, c->comm, suffix); |
| 400 |
if (c->mask & SD_BUS_CREDS_TID_COMM) |
| 401 |
fprintf(f, "%sTIDComm=%s%s%s", prefix, color, c->tid_comm, suffix); |
| 402 |
if (c->mask & SD_BUS_CREDS_EXE) |
| 403 |
fprintf(f, "%sExe=%s%s%s", prefix, color, strna(c->exe), suffix); |
| 404 |
|
| 405 |
if (terse && (c->mask & (SD_BUS_CREDS_EXE|SD_BUS_CREDS_COMM|SD_BUS_CREDS_TID_COMM))) |
| 406 |
fputs("\n", f); |
| 407 |
|
| 408 |
r = sd_bus_creds_get_cmdline(c, &cmdline); |
| 409 |
if (r >= 0) { |
| 410 |
fprintf(f, "%sCommandLine=%s", prefix, color); |
| 411 |
STRV_FOREACH(i, cmdline) { |
| 412 |
if (i != cmdline) |
| 413 |
fputc(' ', f); |
| 414 |
|
| 415 |
fputs(*i, f); |
| 416 |
} |
| 417 |
|
| 418 |
fprintf(f, "%s", suffix); |
| 419 |
} else if (r != -ENODATA) |
| 420 |
fprintf(f, "%sCommandLine=%sn/a%s", prefix, color, suffix); |
| 421 |
|
| 422 |
if (c->mask & SD_BUS_CREDS_SELINUX_CONTEXT) |
| 423 |
fprintf(f, "%sLabel=%s%s%s", prefix, color, c->label, suffix); |
| 424 |
if (c->mask & SD_BUS_CREDS_DESCRIPTION) |
| 425 |
fprintf(f, "%sDescription=%s%s%s", prefix, color, c->description, suffix); |
| 426 |
|
| 427 |
if (terse && (c->mask & (SD_BUS_CREDS_SELINUX_CONTEXT|SD_BUS_CREDS_DESCRIPTION))) |
| 428 |
fputs("\n", f); |
| 429 |
|
| 430 |
if (c->mask & SD_BUS_CREDS_CGROUP) |
| 431 |
fprintf(f, "%sCGroup=%s%s%s", prefix, color, c->cgroup, suffix); |
| 432 |
s = NULL; |
| 433 |
r = sd_bus_creds_get_unit(c, &s); |
| 434 |
if (r != -ENODATA) |
| 435 |
fprintf(f, "%sUnit=%s%s%s", prefix, color, strna(s), suffix); |
| 436 |
s = NULL; |
| 437 |
v = sd_bus_creds_get_slice(c, &s); |
| 438 |
if (v != -ENODATA) |
| 439 |
fprintf(f, "%sSlice=%s%s%s", prefix, color, strna(s), suffix); |
| 440 |
s = NULL; |
| 441 |
q = sd_bus_creds_get_user_unit(c, &s); |
| 442 |
if (q != -ENODATA) |
| 443 |
fprintf(f, "%sUserUnit=%s%s%s", prefix, color, strna(s), suffix); |
| 444 |
s = NULL; |
| 445 |
w = sd_bus_creds_get_user_slice(c, &s); |
| 446 |
if (w != -ENODATA) |
| 447 |
fprintf(f, "%sUserSlice=%s%s%s", prefix, color, strna(s), suffix); |
| 448 |
s = NULL; |
| 449 |
z = sd_bus_creds_get_session(c, &s); |
| 450 |
if (z != -ENODATA) |
| 451 |
fprintf(f, "%sSession=%s%s%s", prefix, color, strna(s), suffix); |
| 452 |
|
| 453 |
if (terse && ((c->mask & SD_BUS_CREDS_CGROUP) || r != -ENODATA || q != -ENODATA || v != -ENODATA || w != -ENODATA || z != -ENODATA)) |
| 454 |
fputs("\n", f); |
| 455 |
|
| 456 |
r = sd_bus_creds_get_audit_login_uid(c, &audit_loginuid); |
| 457 |
if (r >= 0) |
| 458 |
fprintf(f, "%sAuditLoginUID=%s"UID_FMT"%s", prefix, color, audit_loginuid, suffix); |
| 459 |
else if (r != -ENODATA) |
| 460 |
fprintf(f, "%sAuditLoginUID=%sn/a%s", prefix, color, suffix); |
| 461 |
q = sd_bus_creds_get_audit_session_id(c, &audit_sessionid); |
| 462 |
if (q >= 0) |
| 463 |
fprintf(f, "%sAuditSessionID=%s%"PRIu32"%s", prefix, color, audit_sessionid, suffix); |
| 464 |
else if (q != -ENODATA) |
| 465 |
fprintf(f, "%sAuditSessionID=%sn/a%s", prefix, color, suffix); |
| 466 |
|
| 467 |
if (terse && (r != -ENODATA || q != -ENODATA)) |
| 468 |
fputs("\n", f); |
| 469 |
|
| 470 |
if (c->mask & SD_BUS_CREDS_UNIQUE_NAME) |
| 471 |
fprintf(f, "%sUniqueName=%s%s%s", prefix, color, c->unique_name, suffix); |
| 472 |
|
| 473 |
if (sd_bus_creds_get_well_known_names(c, &well_known) >= 0) { |
| 474 |
fprintf(f, "%sWellKnownNames=%s", prefix, color); |
| 475 |
STRV_FOREACH(i, well_known) { |
| 476 |
if (i != well_known) |
| 477 |
fputc(' ', f); |
| 478 |
|
| 479 |
fputs(*i, f); |
| 480 |
} |
| 481 |
|
| 482 |
fprintf(f, "%s", suffix); |
| 483 |
} |
| 484 |
|
| 485 |
if (terse && (c->mask & SD_BUS_CREDS_UNIQUE_NAME || well_known)) |
| 486 |
fputc('\n', f); |
| 487 |
|
| 488 |
dump_capabilities(c, f, "EffectiveCapabilities", terse, sd_bus_creds_has_effective_cap); |
| 489 |
dump_capabilities(c, f, "PermittedCapabilities", terse, sd_bus_creds_has_permitted_cap); |
| 490 |
dump_capabilities(c, f, "InheritableCapabilities", terse, sd_bus_creds_has_inheritable_cap); |
| 491 |
dump_capabilities(c, f, "BoundingCapabilities", terse, sd_bus_creds_has_bounding_cap); |
| 492 |
|
| 493 |
return 0; |
| 494 |
} |
| 495 |
|
| 496 |
static uint16_t pcapng_optlen(size_t len) { |
| 497 |
return ALIGN4(len + sizeof(struct pcapng_option)); |
| 498 |
} |
| 499 |
|
| 500 |
static void pcapng_putopt(FILE *f, uint16_t code, const void *data, size_t len) { |
| 501 |
struct pcapng_option opt = { |
| 502 |
.code = code, |
| 503 |
.length = len, |
| 504 |
}; |
| 505 |
|
| 506 |
assert(f); |
| 507 |
assert((uint16_t) len == len); |
| 508 |
assert(data || len == 0); |
| 509 |
|
| 510 |
fwrite(&opt, 1, sizeof(opt), f); |
| 511 |
if (len > 0) { |
| 512 |
size_t pad = ALIGN4(len) - len; |
| 513 |
|
| 514 |
fwrite(data, 1, len, f); |
| 515 |
|
| 516 |
assert(pad < sizeof(uint32_t)); |
| 517 |
while (pad-- > 0) |
| 518 |
fputc('\0', f); |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
static void pcapng_section_header(FILE *f, const char *os, const char *app) { |
| 523 |
uint32_t len; |
| 524 |
|
| 525 |
assert(f); |
| 526 |
|
| 527 |
/* determine length of section header and options */ |
| 528 |
len = sizeof(struct pcapng_section); |
| 529 |
if (os) |
| 530 |
len += pcapng_optlen(strlen(os)); |
| 531 |
if (app) |
| 532 |
len += pcapng_optlen(strlen(app)); |
| 533 |
len += pcapng_optlen(0); /* OPT_END */ |
| 534 |
len += sizeof(uint32_t); /* trailer length */ |
| 535 |
|
| 536 |
struct pcapng_section hdr = { |
| 537 |
.block_type = PCAPNG_SECTION_BLOCK, |
| 538 |
.block_length = len, |
| 539 |
.byte_order_magic = PCAPNG_BYTE_ORDER_MAGIC, |
| 540 |
.major_version = PCAPNG_MAJOR_VERS, |
| 541 |
.minor_version = PCAPNG_MINOR_VERS, |
| 542 |
.section_length = UINT64_MAX, |
| 543 |
}; |
| 544 |
|
| 545 |
fwrite(&hdr, 1, sizeof(hdr), f); |
| 546 |
if (os) |
| 547 |
pcapng_putopt(f, PCAPNG_SHB_OS, os, strlen(os)); |
| 548 |
if (app) |
| 549 |
pcapng_putopt(f, PCAPNG_SHB_USERAPPL, app, strlen(app)); |
| 550 |
pcapng_putopt(f, PCAPNG_OPT_END, NULL, 0); |
| 551 |
fwrite(&len, 1, sizeof(uint32_t), f); |
| 552 |
} |
| 553 |
|
| 554 |
/* Only have a single instance of dbus pseudo interface */ |
| 555 |
static void pcapng_interface_header(FILE *f, size_t snaplen) { |
| 556 |
uint32_t len; |
| 557 |
|
| 558 |
assert(f); |
| 559 |
assert(snaplen > 0); |
| 560 |
assert((size_t) (uint32_t) snaplen == snaplen); |
| 561 |
|
| 562 |
/* no options (yet) */ |
| 563 |
len = sizeof(struct pcapng_interface_block) + sizeof(uint32_t); |
| 564 |
struct pcapng_interface_block hdr = { |
| 565 |
.block_type = PCAPNG_INTERFACE_BLOCK, |
| 566 |
.block_length = len, |
| 567 |
.link_type = 231, /* D-Bus */ |
| 568 |
.snap_len = snaplen, |
| 569 |
}; |
| 570 |
|
| 571 |
fwrite(&hdr, 1, sizeof(hdr), f); |
| 572 |
fwrite(&len, 1, sizeof(uint32_t), f); |
| 573 |
} |
| 574 |
|
| 575 |
int bus_pcap_header(size_t snaplen, const char *os, const char *info, FILE *f) { |
| 576 |
if (!f) |
| 577 |
f = stdout; |
| 578 |
|
| 579 |
pcapng_section_header(f, os, info); |
| 580 |
pcapng_interface_header(f, snaplen); |
| 581 |
return fflush_and_check(f); |
| 582 |
} |
| 583 |
|
| 584 |
int bus_message_pcap_frame(sd_bus_message *m, size_t snaplen, FILE *f) { |
| 585 |
BusMessageBodyPart *part; |
| 586 |
size_t msglen, caplen, pad; |
| 587 |
uint32_t length; |
| 588 |
uint64_t ts; |
| 589 |
unsigned i; |
| 590 |
size_t w; |
| 591 |
|
| 592 |
assert(m); |
| 593 |
assert(snaplen > 0); |
| 594 |
assert((size_t) (uint32_t) snaplen == snaplen); |
| 595 |
|
| 596 |
if (!f) |
| 597 |
f = stdout; |
| 598 |
|
| 599 |
ts = m->realtime ?: now(CLOCK_REALTIME); |
| 600 |
msglen = BUS_MESSAGE_SIZE(m); |
| 601 |
caplen = MIN(msglen, snaplen); |
| 602 |
pad = ALIGN4(caplen) - caplen; |
| 603 |
|
| 604 |
/* packet block has no options */ |
| 605 |
length = sizeof(struct pcapng_enhance_packet_block) |
| 606 |
+ caplen + pad + sizeof(uint32_t); |
| 607 |
|
| 608 |
struct pcapng_enhance_packet_block epb = { |
| 609 |
.block_type = PCAPNG_ENHANCED_PACKET_BLOCK, |
| 610 |
.block_length = length, |
| 611 |
.interface_id = 0, |
| 612 |
.timestamp_hi = (uint32_t)(ts >> 32), |
| 613 |
.timestamp_lo = (uint32_t)ts, |
| 614 |
.original_length = msglen, |
| 615 |
.capture_length = caplen, |
| 616 |
}; |
| 617 |
|
| 618 |
/* write the pcapng enhanced packet block header */ |
| 619 |
fwrite(&epb, 1, sizeof(epb), f); |
| 620 |
|
| 621 |
/* write the dbus header */ |
| 622 |
w = MIN(BUS_MESSAGE_BODY_BEGIN(m), snaplen); |
| 623 |
fwrite(m->header, 1, w, f); |
| 624 |
snaplen -= w; |
| 625 |
|
| 626 |
/* write the dbus body */ |
| 627 |
MESSAGE_FOREACH_PART(part, i, m) { |
| 628 |
if (snaplen <= 0) |
| 629 |
break; |
| 630 |
|
| 631 |
w = MIN(part->size, snaplen); |
| 632 |
fwrite(part->data, 1, w, f); |
| 633 |
snaplen -= w; |
| 634 |
} |
| 635 |
|
| 636 |
while (pad-- > 0) |
| 637 |
fputc('\0', f); |
| 638 |
|
| 639 |
/* trailing block length */ |
| 640 |
fwrite(&length, 1, sizeof(uint32_t), f); |
| 641 |
|
| 642 |
return fflush_and_check(f); |
| 643 |
} |