| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-bus.h" |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "bus-get-properties.h" |
| 7 |
#include "bus-object.h" |
| 8 |
#include "bus-polkit.h" |
| 9 |
#include "format-util.h" |
| 10 |
#include "hashmap.h" |
| 11 |
#include "logind.h" |
| 12 |
#include "logind-dbus.h" |
| 13 |
#include "logind-session.h" |
| 14 |
#include "logind-session-dbus.h" |
| 15 |
#include "logind-user.h" |
| 16 |
#include "logind-user-dbus.h" |
| 17 |
#include "signal-util.h" |
| 18 |
#include "string-util.h" |
| 19 |
#include "strv.h" |
| 20 |
#include "user-record.h" |
| 21 |
#include "user-util.h" |
| 22 |
|
| 23 |
static int property_get_uid( |
| 24 |
sd_bus *bus, |
| 25 |
const char *path, |
| 26 |
const char *interface, |
| 27 |
const char *property, |
| 28 |
sd_bus_message *reply, |
| 29 |
void *userdata, |
| 30 |
sd_bus_error *error) { |
| 31 |
|
| 32 |
User *u = ASSERT_PTR(userdata); |
| 33 |
|
| 34 |
assert(bus); |
| 35 |
assert(reply); |
| 36 |
|
| 37 |
return sd_bus_message_append(reply, "u", (uint32_t) u->user_record->uid); |
| 38 |
} |
| 39 |
|
| 40 |
static int property_get_gid( |
| 41 |
sd_bus *bus, |
| 42 |
const char *path, |
| 43 |
const char *interface, |
| 44 |
const char *property, |
| 45 |
sd_bus_message *reply, |
| 46 |
void *userdata, |
| 47 |
sd_bus_error *error) { |
| 48 |
|
| 49 |
User *u = ASSERT_PTR(userdata); |
| 50 |
|
| 51 |
assert(bus); |
| 52 |
assert(reply); |
| 53 |
|
| 54 |
return sd_bus_message_append(reply, "u", (uint32_t) u->user_record->gid); |
| 55 |
} |
| 56 |
|
| 57 |
static int property_get_name( |
| 58 |
sd_bus *bus, |
| 59 |
const char *path, |
| 60 |
const char *interface, |
| 61 |
const char *property, |
| 62 |
sd_bus_message *reply, |
| 63 |
void *userdata, |
| 64 |
sd_bus_error *error) { |
| 65 |
|
| 66 |
User *u = ASSERT_PTR(userdata); |
| 67 |
|
| 68 |
assert(bus); |
| 69 |
assert(reply); |
| 70 |
|
| 71 |
return sd_bus_message_append(reply, "s", u->user_record->user_name); |
| 72 |
} |
| 73 |
|
| 74 |
static BUS_DEFINE_PROPERTY_GET2(property_get_state, "s", User, user_get_state, user_state_to_string); |
| 75 |
|
| 76 |
static int property_get_display( |
| 77 |
sd_bus *bus, |
| 78 |
const char *path, |
| 79 |
const char *interface, |
| 80 |
const char *property, |
| 81 |
sd_bus_message *reply, |
| 82 |
void *userdata, |
| 83 |
sd_bus_error *error) { |
| 84 |
|
| 85 |
_cleanup_free_ char *p = NULL; |
| 86 |
User *u = ASSERT_PTR(userdata); |
| 87 |
|
| 88 |
assert(bus); |
| 89 |
assert(reply); |
| 90 |
|
| 91 |
p = u->display ? session_bus_path(u->display) : strdup("/"); |
| 92 |
if (!p) |
| 93 |
return -ENOMEM; |
| 94 |
|
| 95 |
return sd_bus_message_append(reply, "(so)", u->display ? u->display->id : "", p); |
| 96 |
} |
| 97 |
|
| 98 |
static int property_get_sessions( |
| 99 |
sd_bus *bus, |
| 100 |
const char *path, |
| 101 |
const char *interface, |
| 102 |
const char *property, |
| 103 |
sd_bus_message *reply, |
| 104 |
void *userdata, |
| 105 |
sd_bus_error *error) { |
| 106 |
|
| 107 |
User *u = ASSERT_PTR(userdata); |
| 108 |
int r; |
| 109 |
|
| 110 |
assert(bus); |
| 111 |
assert(reply); |
| 112 |
|
| 113 |
r = sd_bus_message_open_container(reply, 'a', "(so)"); |
| 114 |
if (r < 0) |
| 115 |
return r; |
| 116 |
|
| 117 |
LIST_FOREACH(sessions_by_user, session, u->sessions) { |
| 118 |
_cleanup_free_ char *p = NULL; |
| 119 |
|
| 120 |
p = session_bus_path(session); |
| 121 |
if (!p) |
| 122 |
return -ENOMEM; |
| 123 |
|
| 124 |
r = sd_bus_message_append(reply, "(so)", session->id, p); |
| 125 |
if (r < 0) |
| 126 |
return r; |
| 127 |
|
| 128 |
} |
| 129 |
|
| 130 |
return sd_bus_message_close_container(reply); |
| 131 |
} |
| 132 |
|
| 133 |
static int property_get_idle_hint( |
| 134 |
sd_bus *bus, |
| 135 |
const char *path, |
| 136 |
const char *interface, |
| 137 |
const char *property, |
| 138 |
sd_bus_message *reply, |
| 139 |
void *userdata, |
| 140 |
sd_bus_error *error) { |
| 141 |
|
| 142 |
User *u = ASSERT_PTR(userdata); |
| 143 |
|
| 144 |
assert(bus); |
| 145 |
assert(reply); |
| 146 |
|
| 147 |
return sd_bus_message_append(reply, "b", user_get_idle_hint(u, /* ret_timestamp= */ NULL)); |
| 148 |
} |
| 149 |
|
| 150 |
static int property_get_idle_since_hint( |
| 151 |
sd_bus *bus, |
| 152 |
const char *path, |
| 153 |
const char *interface, |
| 154 |
const char *property, |
| 155 |
sd_bus_message *reply, |
| 156 |
void *userdata, |
| 157 |
sd_bus_error *error) { |
| 158 |
|
| 159 |
User *u = ASSERT_PTR(userdata); |
| 160 |
dual_timestamp t = DUAL_TIMESTAMP_NULL; |
| 161 |
uint64_t k; |
| 162 |
|
| 163 |
assert(bus); |
| 164 |
assert(reply); |
| 165 |
|
| 166 |
user_get_idle_hint(u, &t); |
| 167 |
k = streq(property, "IdleSinceHint") ? t.realtime : t.monotonic; |
| 168 |
|
| 169 |
return sd_bus_message_append(reply, "t", k); |
| 170 |
} |
| 171 |
|
| 172 |
static int property_get_linger( |
| 173 |
sd_bus *bus, |
| 174 |
const char *path, |
| 175 |
const char *interface, |
| 176 |
const char *property, |
| 177 |
sd_bus_message *reply, |
| 178 |
void *userdata, |
| 179 |
sd_bus_error *error) { |
| 180 |
|
| 181 |
User *u = ASSERT_PTR(userdata); |
| 182 |
int r; |
| 183 |
|
| 184 |
assert(bus); |
| 185 |
assert(reply); |
| 186 |
|
| 187 |
r = user_check_linger_file(u); |
| 188 |
|
| 189 |
return sd_bus_message_append(reply, "b", r > 0); |
| 190 |
} |
| 191 |
|
| 192 |
int bus_user_method_terminate(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 193 |
User *u = ASSERT_PTR(userdata); |
| 194 |
int r; |
| 195 |
|
| 196 |
assert(message); |
| 197 |
|
| 198 |
r = bus_verify_polkit_async_full( |
| 199 |
message, |
| 200 |
"org.freedesktop.login1.manage", |
| 201 |
/* details= */ NULL, |
| 202 |
u->user_record->uid, |
| 203 |
/* flags= */ 0, |
| 204 |
&u->manager->polkit_registry, |
| 205 |
error); |
| 206 |
if (r < 0) |
| 207 |
return r; |
| 208 |
if (r == 0) |
| 209 |
return 1; /* Will call us back */ |
| 210 |
|
| 211 |
r = user_stop(u, /* force= */ true); |
| 212 |
if (r < 0) |
| 213 |
return r; |
| 214 |
|
| 215 |
return sd_bus_reply_method_return(message, NULL); |
| 216 |
} |
| 217 |
|
| 218 |
int bus_user_method_kill(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
| 219 |
User *u = ASSERT_PTR(userdata); |
| 220 |
int32_t signo; |
| 221 |
int r; |
| 222 |
|
| 223 |
assert(message); |
| 224 |
|
| 225 |
r = bus_verify_polkit_async_full( |
| 226 |
message, |
| 227 |
"org.freedesktop.login1.manage", |
| 228 |
/* details= */ NULL, |
| 229 |
u->user_record->uid, |
| 230 |
/* flags= */ 0, |
| 231 |
&u->manager->polkit_registry, |
| 232 |
error); |
| 233 |
if (r < 0) |
| 234 |
return r; |
| 235 |
if (r == 0) |
| 236 |
return 1; /* Will call us back */ |
| 237 |
|
| 238 |
r = sd_bus_message_read(message, "i", &signo); |
| 239 |
if (r < 0) |
| 240 |
return r; |
| 241 |
|
| 242 |
if (!SIGNAL_VALID(signo)) |
| 243 |
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid signal %i", signo); |
| 244 |
|
| 245 |
r = user_kill(u, signo); |
| 246 |
if (r < 0) |
| 247 |
return r; |
| 248 |
|
| 249 |
return sd_bus_reply_method_return(message, NULL); |
| 250 |
} |
| 251 |
|
| 252 |
static int user_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) { |
| 253 |
Manager *m = ASSERT_PTR(userdata); |
| 254 |
uid_t uid; |
| 255 |
User *user; |
| 256 |
int r; |
| 257 |
|
| 258 |
assert(bus); |
| 259 |
assert(path); |
| 260 |
assert(interface); |
| 261 |
assert(found); |
| 262 |
|
| 263 |
if (streq(path, "/org/freedesktop/login1/user/self")) { |
| 264 |
sd_bus_message *message; |
| 265 |
|
| 266 |
message = sd_bus_get_current_message(bus); |
| 267 |
|
| 268 |
r = manager_get_user_from_creds(m, message, UID_INVALID, error, &user); |
| 269 |
if (r == -ENXIO) { |
| 270 |
sd_bus_error_free(error); |
| 271 |
return 0; |
| 272 |
} |
| 273 |
if (r < 0) |
| 274 |
return r; |
| 275 |
} else { |
| 276 |
const char *p; |
| 277 |
|
| 278 |
p = startswith(path, "/org/freedesktop/login1/user/_"); |
| 279 |
if (!p) |
| 280 |
return 0; |
| 281 |
|
| 282 |
r = parse_uid(p, &uid); |
| 283 |
if (r < 0) |
| 284 |
return 0; |
| 285 |
|
| 286 |
user = hashmap_get(m->users, UID_TO_PTR(uid)); |
| 287 |
if (!user) |
| 288 |
return 0; |
| 289 |
} |
| 290 |
|
| 291 |
*found = user; |
| 292 |
return 1; |
| 293 |
} |
| 294 |
|
| 295 |
char* user_bus_path(User *u) { |
| 296 |
char *s; |
| 297 |
|
| 298 |
assert(u); |
| 299 |
|
| 300 |
if (asprintf(&s, "/org/freedesktop/login1/user/_"UID_FMT, u->user_record->uid) < 0) |
| 301 |
return NULL; |
| 302 |
|
| 303 |
return s; |
| 304 |
} |
| 305 |
|
| 306 |
static int user_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) { |
| 307 |
_cleanup_strv_free_ char **l = NULL; |
| 308 |
sd_bus_message *message; |
| 309 |
Manager *m = userdata; |
| 310 |
User *user; |
| 311 |
int r; |
| 312 |
|
| 313 |
assert(bus); |
| 314 |
assert(path); |
| 315 |
assert(nodes); |
| 316 |
|
| 317 |
HASHMAP_FOREACH(user, m->users) { |
| 318 |
char *p; |
| 319 |
|
| 320 |
p = user_bus_path(user); |
| 321 |
if (!p) |
| 322 |
return -ENOMEM; |
| 323 |
|
| 324 |
r = strv_consume(&l, p); |
| 325 |
if (r < 0) |
| 326 |
return r; |
| 327 |
} |
| 328 |
|
| 329 |
message = sd_bus_get_current_message(bus); |
| 330 |
if (message) { |
| 331 |
_cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
| 332 |
|
| 333 |
r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_OWNER_UID|SD_BUS_CREDS_AUGMENT, &creds); |
| 334 |
if (r >= 0) { |
| 335 |
uid_t uid; |
| 336 |
|
| 337 |
r = sd_bus_creds_get_owner_uid(creds, &uid); |
| 338 |
if (r >= 0) { |
| 339 |
user = hashmap_get(m->users, UID_TO_PTR(uid)); |
| 340 |
if (user) { |
| 341 |
r = strv_extend(&l, "/org/freedesktop/login1/user/self"); |
| 342 |
if (r < 0) |
| 343 |
return r; |
| 344 |
} |
| 345 |
} |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
*nodes = TAKE_PTR(l); |
| 350 |
|
| 351 |
return 1; |
| 352 |
} |
| 353 |
|
| 354 |
static const sd_bus_vtable user_vtable[] = { |
| 355 |
SD_BUS_VTABLE_START(0), |
| 356 |
|
| 357 |
SD_BUS_PROPERTY("UID", "u", property_get_uid, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
| 358 |
SD_BUS_PROPERTY("GID", "u", property_get_gid, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
| 359 |
SD_BUS_PROPERTY("Name", "s", property_get_name, 0, SD_BUS_VTABLE_PROPERTY_CONST), |
| 360 |
BUS_PROPERTY_DUAL_TIMESTAMP("Timestamp", offsetof(User, timestamp), SD_BUS_VTABLE_PROPERTY_CONST), |
| 361 |
SD_BUS_PROPERTY("RuntimePath", "s", NULL, offsetof(User, runtime_path), SD_BUS_VTABLE_PROPERTY_CONST), |
| 362 |
SD_BUS_PROPERTY("Service", "s", NULL, offsetof(User, service_manager_unit), SD_BUS_VTABLE_PROPERTY_CONST), |
| 363 |
SD_BUS_PROPERTY("Slice", "s", NULL, offsetof(User, slice), SD_BUS_VTABLE_PROPERTY_CONST), |
| 364 |
SD_BUS_PROPERTY("Display", "(so)", property_get_display, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 365 |
SD_BUS_PROPERTY("State", "s", property_get_state, 0, 0), |
| 366 |
SD_BUS_PROPERTY("Sessions", "a(so)", property_get_sessions, 0, 0), |
| 367 |
SD_BUS_PROPERTY("IdleHint", "b", property_get_idle_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 368 |
SD_BUS_PROPERTY("IdleSinceHint", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 369 |
SD_BUS_PROPERTY("IdleSinceHintMonotonic", "t", property_get_idle_since_hint, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 370 |
SD_BUS_PROPERTY("Linger", "b", property_get_linger, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 371 |
|
| 372 |
SD_BUS_METHOD("Terminate", NULL, NULL, bus_user_method_terminate, SD_BUS_VTABLE_UNPRIVILEGED), |
| 373 |
SD_BUS_METHOD_WITH_ARGS("Kill", |
| 374 |
SD_BUS_ARGS("i", signal_number), |
| 375 |
SD_BUS_NO_RESULT, |
| 376 |
bus_user_method_kill, |
| 377 |
SD_BUS_VTABLE_UNPRIVILEGED), |
| 378 |
|
| 379 |
SD_BUS_VTABLE_END |
| 380 |
}; |
| 381 |
|
| 382 |
const BusObjectImplementation user_object = { |
| 383 |
"/org/freedesktop/login1/user", |
| 384 |
"org.freedesktop.login1.User", |
| 385 |
.fallback_vtables = BUS_FALLBACK_VTABLES({user_vtable, user_object_find}), |
| 386 |
.node_enumerator = user_node_enumerator, |
| 387 |
}; |
| 388 |
|
| 389 |
int user_send_signal(User *u, bool new_user) { |
| 390 |
_cleanup_free_ char *p = NULL; |
| 391 |
|
| 392 |
assert(u); |
| 393 |
|
| 394 |
p = user_bus_path(u); |
| 395 |
if (!p) |
| 396 |
return -ENOMEM; |
| 397 |
|
| 398 |
return sd_bus_emit_signal( |
| 399 |
u->manager->bus, |
| 400 |
"/org/freedesktop/login1", |
| 401 |
"org.freedesktop.login1.Manager", |
| 402 |
new_user ? "UserNew" : "UserRemoved", |
| 403 |
"uo", (uint32_t) u->user_record->uid, p); |
| 404 |
} |
| 405 |
|
| 406 |
int user_send_changed_strv(User *u, char **properties) { |
| 407 |
_cleanup_free_ char *p = NULL; |
| 408 |
|
| 409 |
assert(u); |
| 410 |
|
| 411 |
if (!u->started) |
| 412 |
return 0; |
| 413 |
|
| 414 |
p = user_bus_path(u); |
| 415 |
if (!p) |
| 416 |
return -ENOMEM; |
| 417 |
|
| 418 |
return sd_bus_emit_properties_changed_strv(u->manager->bus, p, "org.freedesktop.login1.User", properties); |
| 419 |
} |