diff options
Diffstat (limited to 'src/core/dbus-manager.c')
-rw-r--r-- | src/core/dbus-manager.c | 218 |
1 files changed, 127 insertions, 91 deletions
diff --git a/src/core/dbus-manager.c b/src/core/dbus-manager.c index 8ba665dc3d..c8937eeb26 100644 --- a/src/core/dbus-manager.c +++ b/src/core/dbus-manager.c @@ -359,9 +359,26 @@ static int method_get_unit(sd_bus *bus, sd_bus_message *message, void *userdata, if (r < 0) return r; - u = manager_get_unit(m, name); - if (!u) - return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s not loaded.", name); + if (isempty(name)) { + _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL; + pid_t pid; + + r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds); + if (r < 0) + return r; + + r = sd_bus_creds_get_pid(creds, &pid); + if (r < 0) + return r; + + u = manager_get_unit_by_pid(m, pid); + if (!u) + return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Client not member of any unit."); + } else { + u = manager_get_unit(m, name); + if (!u) + return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Unit %s not loaded.", name); + } r = mac_selinux_unit_access_check(u, message, "status", error); if (r < 0) @@ -392,6 +409,8 @@ static int method_get_unit_by_pid(sd_bus *bus, sd_bus_message *message, void *us r = sd_bus_message_read(message, "u", &pid); if (r < 0) return r; + if (pid < 0) + return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid PID " PID_FMT, pid); if (pid == 0) { _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL; @@ -437,9 +456,26 @@ static int method_load_unit(sd_bus *bus, sd_bus_message *message, void *userdata if (r < 0) return r; - r = manager_load_unit(m, name, NULL, error, &u); - if (r < 0) - return r; + if (isempty(name)) { + _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL; + pid_t pid; + + r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds); + if (r < 0) + return r; + + r = sd_bus_creds_get_pid(creds, &pid); + if (r < 0) + return r; + + u = manager_get_unit_by_pid(m, pid); + if (!u) + return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_UNIT, "Client not member of any unit."); + } else { + r = manager_load_unit(m, name, NULL, error, &u); + if (r < 0) + return r; + } r = mac_selinux_unit_access_check(u, message, "status", error); if (r < 0) @@ -461,12 +497,6 @@ static int method_start_unit_generic(sd_bus *bus, sd_bus_message *message, Manag assert(message); assert(m); - r = bus_verify_manage_unit_async(m, message, 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 */ - r = sd_bus_message_read(message, "s", &name); if (r < 0) return r; @@ -516,12 +546,6 @@ static int method_start_unit_replace(sd_bus *bus, sd_bus_message *message, void assert(message); assert(m); - r = bus_verify_manage_unit_async(m, message, 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 */ - r = sd_bus_message_read(message, "s", &old_name); if (r < 0) return r; @@ -543,13 +567,6 @@ static int method_kill_unit(sd_bus *bus, sd_bus_message *message, void *userdata assert(message); assert(m); - /* Like bus_verify_manage_unit_async(), but uses CAP_SYS_KILL */ - r = bus_verify_manage_unit_async_for_kill(m, message, 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 */ - r = sd_bus_message_read(message, "s", &name); if (r < 0) return r; @@ -571,12 +588,6 @@ static int method_reset_failed_unit(sd_bus *bus, sd_bus_message *message, void * assert(message); assert(m); - r = bus_verify_manage_unit_async(m, message, 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 */ - r = sd_bus_message_read(message, "s", &name); if (r < 0) return r; @@ -598,12 +609,6 @@ static int method_set_unit_properties(sd_bus *bus, sd_bus_message *message, void assert(message); assert(m); - r = bus_verify_manage_unit_async(m, message, 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 */ - r = sd_bus_message_read(message, "s", &name); if (r < 0) return r; @@ -713,11 +718,9 @@ static int method_start_transient_unit(sd_bus *bus, sd_bus_message *message, voi assert(message); assert(m); - r = bus_verify_manage_unit_async(m, message, error); + r = mac_selinux_access_check(message, "start", 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 */ r = sd_bus_message_read(message, "ss", &name, &smode); if (r < 0) @@ -734,9 +737,11 @@ static int method_start_transient_unit(sd_bus *bus, sd_bus_message *message, voi if (mode < 0) return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Job mode %s is invalid.", smode); - r = mac_selinux_access_check(message, "start", error); + r = bus_verify_manage_units_async(m, message, 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 */ r = transient_unit_from_message(m, message, name, &u, error); if (r < 0) @@ -818,10 +823,16 @@ static int method_clear_jobs(sd_bus *bus, sd_bus_message *message, void *userdat assert(message); assert(m); - r = mac_selinux_access_check(message, "reboot", error); + r = mac_selinux_access_check(message, "reload", error); if (r < 0) return r; + r = bus_verify_manage_units_async(m, message, 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 */ + manager_clear_jobs(m); return sd_bus_reply_method_return(message, NULL); @@ -839,6 +850,12 @@ static int method_reset_failed(sd_bus *bus, sd_bus_message *message, void *userd if (r < 0) return r; + r = bus_verify_manage_units_async(m, message, 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 */ + manager_reset_failed(m); return sd_bus_reply_method_return(message, NULL); @@ -1103,6 +1120,12 @@ static int method_create_snapshot(sd_bus *bus, sd_bus_message *message, void *us if (isempty(name)) name = NULL; + r = bus_verify_manage_units_async(m, message, 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 */ + r = snapshot_create(m, name, cleanup, error, &s); if (r < 0) return r; @@ -1124,10 +1147,6 @@ static int method_remove_snapshot(sd_bus *bus, sd_bus_message *message, void *us assert(message); assert(m); - r = mac_selinux_access_check(message, "stop", error); - if (r < 0) - return r; - r = sd_bus_message_read(message, "s", &name); if (r < 0) return r; @@ -1150,15 +1169,15 @@ static int method_reload(sd_bus *bus, sd_bus_message *message, void *userdata, s assert(message); assert(m); - r = bus_verify_reload_daemon_async(m, message, error); + r = mac_selinux_access_check(message, "reload", 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 */ - r = mac_selinux_access_check(message, "reload", error); + r = bus_verify_reload_daemon_async(m, message, 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 */ /* Instead of sending the reply back right away, we just * remember that we need to and then send it after the reload @@ -1184,15 +1203,15 @@ static int method_reexecute(sd_bus *bus, sd_bus_message *message, void *userdata assert(message); assert(m); - r = bus_verify_reload_daemon_async(m, message, error); + r = mac_selinux_access_check(message, "reload", 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 */ - r = mac_selinux_access_check(message, "reload", error); + r = bus_verify_reload_daemon_async(m, message, 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 */ /* We don't send a reply back here, the client should * just wait for us disconnecting. */ @@ -1241,7 +1260,6 @@ static int method_reboot(sd_bus *bus, sd_bus_message *message, void *userdata, s return sd_bus_reply_method_return(message, NULL); } - static int method_poweroff(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { Manager *m = userdata; int r; @@ -1386,6 +1404,12 @@ static int method_set_environment(sd_bus *bus, sd_bus_message *message, void *us if (!strv_env_is_valid(plus)) return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid environment assignments"); + r = bus_verify_set_environment_async(m, message, 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 */ + r = manager_environment_add(m, NULL, plus); if (r < 0) return r; @@ -1413,6 +1437,12 @@ static int method_unset_environment(sd_bus *bus, sd_bus_message *message, void * if (!strv_env_name_or_assignment_is_valid(minus)) return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid environment variable names or assignments"); + r = bus_verify_set_environment_async(m, message, 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 */ + r = manager_environment_add(m, minus, NULL); if (r < 0) return r; @@ -1446,6 +1476,12 @@ static int method_unset_and_set_environment(sd_bus *bus, sd_bus_message *message if (!strv_env_is_valid(plus)) return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid environment assignments"); + r = bus_verify_set_environment_async(m, message, 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 */ + r = manager_environment_add(m, minus, plus); if (r < 0) return r; @@ -1647,12 +1683,6 @@ static int method_enable_unit_files_generic( assert(message); assert(m); - r = bus_verify_manage_unit_files_async(m, message, 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 */ - r = sd_bus_message_read_strv(message, &l); if (r < 0) return r; @@ -1665,6 +1695,12 @@ static int method_enable_unit_files_generic( if (r < 0) return r; + r = bus_verify_manage_unit_files_async(m, message, 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 */ + scope = m->running_as == SYSTEMD_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER; r = call(scope, runtime, NULL, l, force, &changes, &n_changes); @@ -1713,12 +1749,6 @@ static int method_preset_unit_files_with_mode(sd_bus *bus, sd_bus_message *messa assert(message); assert(m); - r = bus_verify_manage_unit_files_async(m, message, 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 */ - r = sd_bus_message_read_strv(message, &l); if (r < 0) return r; @@ -1739,6 +1769,12 @@ static int method_preset_unit_files_with_mode(sd_bus *bus, sd_bus_message *messa if (r < 0) return r; + r = bus_verify_manage_unit_files_async(m, message, 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 */ + scope = m->running_as == SYSTEMD_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER; r = unit_file_preset(scope, runtime, NULL, l, mm, force, &changes, &n_changes); @@ -1766,12 +1802,6 @@ static int method_disable_unit_files_generic( assert(message); assert(m); - r = bus_verify_manage_unit_files_async(m, message, 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 */ - r = mac_selinux_access_check(message, verb, error); if (r < 0) return r; @@ -1786,6 +1816,12 @@ static int method_disable_unit_files_generic( scope = m->running_as == SYSTEMD_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER; + r = bus_verify_manage_unit_files_async(m, message, 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 */ + r = call(scope, runtime, NULL, l, &changes, &n_changes); if (r < 0) return r; @@ -1813,19 +1849,19 @@ static int method_set_default_target(sd_bus *bus, sd_bus_message *message, void assert(message); assert(m); - r = bus_verify_manage_unit_files_async(m, message, error); + r = mac_selinux_access_check(message, "enable", 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 */ - r = mac_selinux_access_check(message, "enable", error); + r = sd_bus_message_read(message, "sb", &name, &force); if (r < 0) return r; - r = sd_bus_message_read(message, "sb", &name, &force); + r = bus_verify_manage_unit_files_async(m, message, 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 */ scope = m->running_as == SYSTEMD_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER; @@ -1849,12 +1885,6 @@ static int method_preset_all_unit_files(sd_bus *bus, sd_bus_message *message, vo assert(message); assert(m); - r = bus_verify_manage_unit_files_async(m, message, 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 */ - r = mac_selinux_access_check(message, "enable", error); if (r < 0) return r; @@ -1871,6 +1901,12 @@ static int method_preset_all_unit_files(sd_bus *bus, sd_bus_message *message, vo return -EINVAL; } + r = bus_verify_manage_unit_files_async(m, message, 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 */ + scope = m->running_as == SYSTEMD_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER; r = unit_file_preset_all(scope, runtime, NULL, mm, force, &changes, &n_changes); @@ -1982,16 +2018,16 @@ const sd_bus_vtable bus_manager_vtable[] = { SD_BUS_METHOD("StartTransientUnit", "ssa(sv)a(sa(sv))", "o", method_start_transient_unit, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("GetJob", "u", "o", method_get_job, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("CancelJob", "u", NULL, method_cancel_job, SD_BUS_VTABLE_UNPRIVILEGED), - SD_BUS_METHOD("ClearJobs", NULL, NULL, method_clear_jobs, 0), - SD_BUS_METHOD("ResetFailed", NULL, NULL, method_reset_failed, 0), + SD_BUS_METHOD("ClearJobs", NULL, NULL, method_clear_jobs, SD_BUS_VTABLE_UNPRIVILEGED), + SD_BUS_METHOD("ResetFailed", NULL, NULL, method_reset_failed, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("ListUnits", NULL, "a(ssssssouso)", method_list_units, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("ListUnitsFiltered", "as", "a(ssssssouso)", method_list_units_filtered, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("ListJobs", NULL, "a(usssoo)", method_list_jobs, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("Subscribe", NULL, NULL, method_subscribe, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("Unsubscribe", NULL, NULL, method_unsubscribe, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("Dump", NULL, "s", method_dump, SD_BUS_VTABLE_UNPRIVILEGED), - SD_BUS_METHOD("CreateSnapshot", "sb", "o", method_create_snapshot, 0), - SD_BUS_METHOD("RemoveSnapshot", "s", NULL, method_remove_snapshot, 0), + SD_BUS_METHOD("CreateSnapshot", "sb", "o", method_create_snapshot, SD_BUS_VTABLE_UNPRIVILEGED), + SD_BUS_METHOD("RemoveSnapshot", "s", NULL, method_remove_snapshot, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("Reload", NULL, NULL, method_reload, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("Reexecute", NULL, NULL, method_reexecute, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("Exit", NULL, NULL, method_exit, 0), @@ -2000,9 +2036,9 @@ const sd_bus_vtable bus_manager_vtable[] = { SD_BUS_METHOD("Halt", NULL, NULL, method_halt, SD_BUS_VTABLE_CAPABILITY(CAP_SYS_BOOT)), SD_BUS_METHOD("KExec", NULL, NULL, method_kexec, SD_BUS_VTABLE_CAPABILITY(CAP_SYS_BOOT)), SD_BUS_METHOD("SwitchRoot", "ss", NULL, method_switch_root, SD_BUS_VTABLE_CAPABILITY(CAP_SYS_BOOT)), - SD_BUS_METHOD("SetEnvironment", "as", NULL, method_set_environment, 0), - SD_BUS_METHOD("UnsetEnvironment", "as", NULL, method_unset_environment, 0), - SD_BUS_METHOD("UnsetAndSetEnvironment", "asas", NULL, method_unset_and_set_environment, 0), + SD_BUS_METHOD("SetEnvironment", "as", NULL, method_set_environment, SD_BUS_VTABLE_UNPRIVILEGED), + SD_BUS_METHOD("UnsetEnvironment", "as", NULL, method_unset_environment, SD_BUS_VTABLE_UNPRIVILEGED), + SD_BUS_METHOD("UnsetAndSetEnvironment", "asas", NULL, method_unset_and_set_environment, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("ListUnitFiles", NULL, "a(ss)", method_list_unit_files, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("GetUnitFileState", "s", "s", method_get_unit_file_state, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("EnableUnitFiles", "asbb", "ba(sss)", method_enable_unit_files, SD_BUS_VTABLE_UNPRIVILEGED), |