Skip to content

Commit

Permalink
logind: require polkit auth for cancelling shutdowns
Browse files Browse the repository at this point in the history
  • Loading branch information
lnussel committed Jan 25, 2022
1 parent 030f37c commit ec14fba
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/login/logind-dbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -2323,20 +2323,49 @@ static int method_schedule_shutdown(sd_bus_message *message, void *userdata, sd_

static int method_cancel_scheduled_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
const char *action;
bool cancelled;
int r;

assert(m);
assert(message);

cancelled = m->scheduled_shutdown_type != NULL;

if (!cancelled)
goto done;

// mirrors code in method_schedule_shutdown()
if (streq(m->scheduled_shutdown_type, "poweroff")) {
action = "org.freedesktop.login1.power-off";
} else if (STR_IN_SET(m->scheduled_shutdown_type, "reboot", "kexec")) {
action = "org.freedesktop.login1.reboot";
} else if (streq(m->scheduled_shutdown_type, "halt")) {
action = "org.freedesktop.login1.halt";
} else
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Unsupported shutdown type");

r = bus_verify_polkit_async(
message,
CAP_SYS_BOOT,
action,
NULL,
false,
UID_INVALID,
&m->polkit_registry,
error);
if (r < 0)
return r;
if (r == 0)
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */

reset_scheduled_shutdown(m);

if (cancelled && m->enable_wall_messages) {
if (m->enable_wall_messages) {
_cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
_cleanup_free_ char *username = NULL;
const char *tty = NULL;
uid_t uid = 0;
int r;

r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_AUGMENT|SD_BUS_CREDS_TTY|SD_BUS_CREDS_UID, &creds);
if (r >= 0) {
Expand All @@ -2349,6 +2378,7 @@ static int method_cancel_scheduled_shutdown(sd_bus_message *message, void *userd
username, tty, logind_wall_tty_filter, m);
}

done:
return sd_bus_reply_method_return(message, "b", cancelled);
}

Expand Down

0 comments on commit ec14fba

Please sign in to comment.