| 1 |
/* |
| 2 |
* Bus Context |
| 3 |
*/ |
| 4 |
|
| 5 |
#include <c-rbtree.h> |
| 6 |
#include <c-stdaux.h> |
| 7 |
#include <stdlib.h> |
| 8 |
#include <sys/auxv.h> |
| 9 |
#include <sys/socket.h> |
| 10 |
#include "bus/bus.h" |
| 11 |
#include "bus/driver.h" |
| 12 |
#include "bus/match.h" |
| 13 |
#include "bus/name.h" |
| 14 |
#include "dbus/address.h" |
| 15 |
#include "util/error.h" |
| 16 |
#include "util/log.h" |
| 17 |
#include "util/user.h" |
| 18 |
|
| 19 |
int bus_init(Bus *bus, |
| 20 |
Log *log, |
| 21 |
const char *machine_id, |
| 22 |
unsigned int max_bytes, |
| 23 |
unsigned int max_fds, |
| 24 |
unsigned int max_matches, |
| 25 |
unsigned int max_objects) { |
| 26 |
unsigned int maxima[] = { max_bytes, max_fds, max_matches, max_objects }; |
| 27 |
void *random; |
| 28 |
int r; |
| 29 |
|
| 30 |
static_assert(_USER_SLOT_N == C_ARRAY_SIZE(maxima), |
| 31 |
"User accounting slot mismatch"); |
| 32 |
|
| 33 |
if (strlen(machine_id) + 1 != sizeof(bus->machine_id)) |
| 34 |
return error_origin(-EINVAL); |
| 35 |
|
| 36 |
*bus = (Bus)BUS_NULL(*bus); |
| 37 |
bus->log = log; |
| 38 |
|
| 39 |
c_memcpy(bus->machine_id, machine_id, sizeof(bus->machine_id)); |
| 40 |
|
| 41 |
random = (void *)getauxval(AT_RANDOM); |
| 42 |
c_assert(random); |
| 43 |
c_memcpy(bus->guid, random, sizeof(bus->guid)); |
| 44 |
|
| 45 |
r = user_registry_init(&bus->users, log, _USER_SLOT_N, maxima); |
| 46 |
if (r) |
| 47 |
return error_fold(r); |
| 48 |
|
| 49 |
return 0; |
| 50 |
} |
| 51 |
|
| 52 |
void bus_deinit(Bus *bus) { |
| 53 |
bus->n_seclabel = 0; |
| 54 |
bus->seclabel = c_free(bus->seclabel); |
| 55 |
bus->n_gids = 0; |
| 56 |
bus->gids = c_free(bus->gids); |
| 57 |
bus->pid = 0; |
| 58 |
bus->pid_fd = c_close(bus->pid_fd); |
| 59 |
bus->user = user_unref(bus->user); |
| 60 |
metrics_deinit(&bus->metrics); |
| 61 |
peer_registry_deinit(&bus->peers); |
| 62 |
user_registry_deinit(&bus->users); |
| 63 |
name_registry_deinit(&bus->names); |
| 64 |
match_registry_deinit(&bus->sender_matches); |
| 65 |
match_registry_deinit(&bus->wildcard_matches); |
| 66 |
} |
| 67 |
|
| 68 |
Peer *bus_find_peer_by_name(Bus *bus, Name **namep, const char *name_str) { |
| 69 |
NameOwnership *ownership; |
| 70 |
Address addr; |
| 71 |
Peer *peer = NULL; |
| 72 |
Name *name = NULL; |
| 73 |
|
| 74 |
address_from_string(&addr, name_str); |
| 75 |
switch (addr.type) { |
| 76 |
case ADDRESS_TYPE_ID: |
| 77 |
peer = peer_registry_find_peer(&bus->peers, addr.id); |
| 78 |
break; |
| 79 |
case ADDRESS_TYPE_NAME: |
| 80 |
name = name_registry_find_name(&bus->names, addr.name); |
| 81 |
if (name) { |
| 82 |
ownership = name_primary(name); |
| 83 |
if (ownership) |
| 84 |
peer = c_container_of(ownership->owner, Peer, owned_names); |
| 85 |
} |
| 86 |
break; |
| 87 |
} |
| 88 |
|
| 89 |
if (namep) |
| 90 |
*namep = name; |
| 91 |
return peer; |
| 92 |
} |
| 93 |
|
| 94 |
void bus_get_monitor_destinations(Bus *bus, CList *destinations, Peer *sender, MessageMetadata *metadata) { |
| 95 |
if (!bus->n_monitors) |
| 96 |
return; |
| 97 |
|
| 98 |
match_registry_get_monitors(&bus->wildcard_matches, destinations, metadata); |
| 99 |
|
| 100 |
if (sender) { |
| 101 |
NameOwnership *ownership; |
| 102 |
|
| 103 |
c_rbtree_for_each_entry(ownership, &sender->owned_names.ownership_tree, owner_node) { |
| 104 |
if (!name_ownership_is_primary(ownership)) |
| 105 |
continue; |
| 106 |
|
| 107 |
match_registry_get_monitors(&ownership->name->sender_matches, destinations, metadata); |
| 108 |
} |
| 109 |
|
| 110 |
match_registry_get_monitors(&sender->sender_matches, destinations, metadata); |
| 111 |
} else { |
| 112 |
/* sent from the driver */ |
| 113 |
match_registry_get_monitors(&bus->sender_matches, destinations, metadata); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
void bus_get_broadcast_destinations(Bus *bus, CList *destinations, MatchRegistry *matches, Peer *sender, MessageMetadata *metadata) { |
| 118 |
match_registry_get_subscribers(&bus->wildcard_matches, destinations, metadata); |
| 119 |
|
| 120 |
if (matches) { |
| 121 |
match_registry_get_subscribers(matches, destinations, metadata); |
| 122 |
} |
| 123 |
|
| 124 |
if (sender) { |
| 125 |
NameOwner *owner = &sender->owned_names; |
| 126 |
NameOwnership *ownership; |
| 127 |
|
| 128 |
c_rbtree_for_each_entry(ownership, &owner->ownership_tree, owner_node) { |
| 129 |
if (!name_ownership_is_primary(ownership)) |
| 130 |
continue; |
| 131 |
|
| 132 |
match_registry_get_subscribers(&ownership->name->sender_matches, destinations, metadata); |
| 133 |
} |
| 134 |
} else { |
| 135 |
/* sent from the driver */ |
| 136 |
match_registry_get_subscribers(&bus->sender_matches, destinations, metadata); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
void bus_log_append_sender(Bus *bus, uint64_t sender_id, NameSet *sender_names, const char *sender_label) { |
| 141 |
Log *log = bus->log; |
| 142 |
|
| 143 |
if (sender_label) |
| 144 |
log_appendf(log, "DBUS_BROKER_SENDER_SECURITY_LABEL=%s\n", |
| 145 |
sender_label); |
| 146 |
|
| 147 |
if (sender_id == ADDRESS_ID_INVALID) { |
| 148 |
log_appendf(log, |
| 149 |
"DBUS_BROKER_SENDER_UNIQUE_NAME=org.freedesktop.DBus\n"); |
| 150 |
} else { |
| 151 |
log_appendf(log, |
| 152 |
"DBUS_BROKER_SENDER_UNIQUE_NAME=:1.%llu\n", |
| 153 |
sender_id); |
| 154 |
} |
| 155 |
|
| 156 |
if (sender_names) { |
| 157 |
if (sender_names->type == NAME_SET_TYPE_OWNER) { |
| 158 |
NameOwnership *ownership; |
| 159 |
size_t i = 0; |
| 160 |
|
| 161 |
c_rbtree_for_each_entry(ownership, |
| 162 |
&sender_names->owner->ownership_tree, |
| 163 |
owner_node) |
| 164 |
log_appendf(log, |
| 165 |
"DBUS_BROKER_SENDER_WELL_KNOWN_NAME_%zu=%s\n", |
| 166 |
i++, ownership->name->name); |
| 167 |
} else if (sender_names->type == NAME_SET_TYPE_SNAPSHOT) { |
| 168 |
for (size_t i = 0; i < sender_names->snapshot->n_names; ++i) |
| 169 |
log_appendf(log, |
| 170 |
"DBUS_BROKER_SENDER_WELL_KNOWN_NAME_%zu=%s\n", |
| 171 |
i, sender_names->snapshot->names[i]->name); |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
void bus_log_append_receiver(Bus *bus, uint64_t receiver_id, NameSet *receiver_names, const char *receiver_label) { |
| 177 |
Log *log = bus->log; |
| 178 |
|
| 179 |
if (receiver_label) |
| 180 |
log_appendf(log, "DBUS_BROKER_RECEIVER_SECURITY_LABEL=%s\n", |
| 181 |
receiver_label); |
| 182 |
|
| 183 |
if (receiver_id == ADDRESS_ID_INVALID) { |
| 184 |
log_appendf(log, |
| 185 |
"DBUS_BOKER_RECEIVER_UNIQUE_NAME=org.freedesktop.DBus\n"); |
| 186 |
} else { |
| 187 |
log_appendf(log, |
| 188 |
"DBUS_BROKER_RECEIVER_UNIQUE_NAME=:1.%llu\n", |
| 189 |
receiver_id); |
| 190 |
} |
| 191 |
|
| 192 |
if (receiver_names) { |
| 193 |
if (receiver_names->type == NAME_SET_TYPE_OWNER) { |
| 194 |
NameOwnership *ownership; |
| 195 |
size_t i = 0; |
| 196 |
|
| 197 |
c_rbtree_for_each_entry(ownership, |
| 198 |
&receiver_names->owner->ownership_tree, |
| 199 |
owner_node) |
| 200 |
log_appendf(log, |
| 201 |
"DBUS_BROKER_RECEIVER_WELL_KNOWN_NAME_%zu=%s\n", |
| 202 |
i++, ownership->name->name); |
| 203 |
} else if (receiver_names->type == NAME_SET_TYPE_SNAPSHOT) { |
| 204 |
for (size_t i = 0; i < receiver_names->snapshot->n_names; ++i) |
| 205 |
log_appendf(log, |
| 206 |
"DBUS_BROKER_RECEIVER_WELL_KNOWN_NAME_%zu=%s\n", |
| 207 |
i, receiver_names->snapshot->names[i]->name); |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
void bus_log_append_transaction(Bus *bus, uint64_t sender_id, uint64_t receiver_id, |
| 213 |
NameSet *sender_names, NameSet *receiver_names, const char *sender_label, const char *receiver_label, |
| 214 |
Message *message) { |
| 215 |
message_log_append(message, bus->log); |
| 216 |
bus_log_append_sender(bus, sender_id, sender_names, sender_label); |
| 217 |
bus_log_append_receiver(bus, receiver_id, receiver_names, receiver_label); |
| 218 |
} |
| 219 |
|
| 220 |
void bus_log_append_policy_send(Bus *bus, int policy_type, uint64_t sender_id, uint64_t receiver_id, NameSet *sender_names, NameSet *receiver_names, const char *sender_label, const char *receiver_label, Message *message) { |
| 221 |
switch (policy_type) { |
| 222 |
case BUS_LOG_POLICY_TYPE_INTERNAL: |
| 223 |
log_appendf(bus->log, "DBUS_BROKER_POLICY_TYPE=internal\n"); |
| 224 |
break; |
| 225 |
case BUS_LOG_POLICY_TYPE_SELINUX: |
| 226 |
log_appendf(bus->log, "DBUS_BROKER_POLICY_TYPE=selinux\n"); |
| 227 |
break; |
| 228 |
case BUS_LOG_POLICY_TYPE_APPARMOR: |
| 229 |
log_appendf(bus->log, "DBUS_BROKER_POLICY_TYPE=apparmor\n"); |
| 230 |
break; |
| 231 |
default: |
| 232 |
c_assert(0); |
| 233 |
abort(); |
| 234 |
} |
| 235 |
|
| 236 |
log_appendf(bus->log, "DBUS_BROKER_TRANSMIT_ACTION=send\n"); |
| 237 |
bus_log_append_transaction(bus, sender_id, receiver_id, sender_names, receiver_names, sender_label, receiver_label, message); |
| 238 |
} |
| 239 |
|
| 240 |
void bus_log_append_policy_receive(Bus *bus, int policy_type, uint64_t receiver_id, uint64_t sender_id, NameSet *sender_names, NameSet *receiver_names, Message *message) { |
| 241 |
switch (policy_type) { |
| 242 |
case BUS_LOG_POLICY_TYPE_INTERNAL: |
| 243 |
log_appendf(bus->log, "DBUS_BROKER_POLICY_TYPE=internal\n"); |
| 244 |
break; |
| 245 |
case BUS_LOG_POLICY_TYPE_SELINUX: |
| 246 |
log_appendf(bus->log, "DBUS_BROKER_POLICY_TYPE=selinux\n"); |
| 247 |
break; |
| 248 |
case BUS_LOG_POLICY_TYPE_APPARMOR: |
| 249 |
log_appendf(bus->log, "DBUS_BROKER_POLICY_TYPE=apparmor\n"); |
| 250 |
break; |
| 251 |
default: |
| 252 |
c_assert(0); |
| 253 |
abort(); |
| 254 |
} |
| 255 |
log_appendf(bus->log, "DBUS_BROKER_TRANSMIT_ACTION=receive\n"); |
| 256 |
bus_log_append_transaction(bus, sender_id, receiver_id, sender_names, receiver_names, NULL, NULL, message); |
| 257 |
} |