1
0
mirror of https://github.com/systemd/systemd.git synced 2024-11-01 00:51:24 +03:00

verbs: add a new VERB_MUSTBEROOT flag

Given that we regularly have verbs that require privileges, let's just
make this a flag of the verb.
This commit is contained in:
Lennart Poettering 2017-12-11 23:10:11 +01:00
parent fba868fa71
commit 0c63eb7138
4 changed files with 21 additions and 23 deletions

View File

@ -33,7 +33,7 @@ int dispatch_verb(int argc, char *argv[], const Verb verbs[], void *userdata) {
const Verb *verb; const Verb *verb;
const char *name; const char *name;
unsigned i; unsigned i;
int left; int left, r;
assert(verbs); assert(verbs);
assert(verbs[0].dispatch); assert(verbs[0].dispatch);
@ -89,6 +89,12 @@ int dispatch_verb(int argc, char *argv[], const Verb verbs[], void *userdata) {
return 0; return 0;
} }
if (verb->flags & VERB_MUSTBEROOT) {
r = must_be_root();
if (r < 0)
return r;
}
if (name) if (name)
return verb->dispatch(left, argv + optind, userdata); return verb->dispatch(left, argv + optind, userdata);
else { else {

View File

@ -21,13 +21,17 @@
***/ ***/
#define VERB_ANY ((unsigned) -1) #define VERB_ANY ((unsigned) -1)
#define VERB_DEFAULT 1U
#define VERB_NOCHROOT 2U typedef enum VerbFlags {
VERB_DEFAULT = 1 << 0,
VERB_NOCHROOT = 1 << 1,
VERB_MUSTBEROOT = 1 << 2,
} VerbFlags;
typedef struct { typedef struct {
const char *verb; const char *verb;
unsigned min_args, max_args; unsigned min_args, max_args;
unsigned flags; VerbFlags flags;
int (* const dispatch)(int argc, char *argv[], void *userdata); int (* const dispatch)(int argc, char *argv[], void *userdata);
} Verb; } Verb;

View File

@ -1087,10 +1087,6 @@ static int verb_install(int argc, char *argv[], void *userdata) {
bool install; bool install;
int r; int r;
r = must_be_root();
if (r < 0)
return r;
r = acquire_esp(false, &part, &pstart, &psize, &uuid); r = acquire_esp(false, &part, &pstart, &psize, &uuid);
if (r < 0) if (r < 0)
return r; return r;
@ -1122,10 +1118,6 @@ static int verb_remove(int argc, char *argv[], void *userdata) {
sd_id128_t uuid = SD_ID128_NULL; sd_id128_t uuid = SD_ID128_NULL;
int r; int r;
r = must_be_root();
if (r < 0)
return r;
r = acquire_esp(false, NULL, NULL, NULL, &uuid); r = acquire_esp(false, NULL, NULL, NULL, &uuid);
if (r < 0) if (r < 0)
return r; return r;
@ -1146,12 +1138,12 @@ static int verb_remove(int argc, char *argv[], void *userdata) {
static int bootctl_main(int argc, char *argv[]) { static int bootctl_main(int argc, char *argv[]) {
static const Verb verbs[] = { static const Verb verbs[] = {
{ "help", VERB_ANY, VERB_ANY, 0, help }, { "help", VERB_ANY, VERB_ANY, 0, help },
{ "status", VERB_ANY, 1, VERB_DEFAULT, verb_status }, { "status", VERB_ANY, 1, VERB_DEFAULT, verb_status },
{ "list", VERB_ANY, 1, 0, verb_list }, { "list", VERB_ANY, 1, 0, verb_list },
{ "install", VERB_ANY, 1, 0, verb_install }, { "install", VERB_ANY, 1, VERB_MUSTBEROOT, verb_install },
{ "update", VERB_ANY, 1, 0, verb_install }, { "update", VERB_ANY, 1, VERB_MUSTBEROOT, verb_install },
{ "remove", VERB_ANY, 1, 0, verb_remove }, { "remove", VERB_ANY, 1, VERB_MUSTBEROOT, verb_remove },
{} {}
}; };

View File

@ -2074,10 +2074,6 @@ static int list_machines(int argc, char *argv[], void *userdata) {
sd_bus *bus; sd_bus *bus;
int r; int r;
r = must_be_root();
if (r < 0)
return r;
r = acquire_bus(BUS_MANAGER, &bus); r = acquire_bus(BUS_MANAGER, &bus);
if (r < 0) if (r < 0)
return r; return r;
@ -8408,7 +8404,7 @@ static int systemctl_main(int argc, char *argv[]) {
{ "list-sockets", VERB_ANY, VERB_ANY, VERB_NOCHROOT, list_sockets }, { "list-sockets", VERB_ANY, VERB_ANY, VERB_NOCHROOT, list_sockets },
{ "list-timers", VERB_ANY, VERB_ANY, VERB_NOCHROOT, list_timers }, { "list-timers", VERB_ANY, VERB_ANY, VERB_NOCHROOT, list_timers },
{ "list-jobs", VERB_ANY, VERB_ANY, VERB_NOCHROOT, list_jobs }, { "list-jobs", VERB_ANY, VERB_ANY, VERB_NOCHROOT, list_jobs },
{ "list-machines", VERB_ANY, VERB_ANY, VERB_NOCHROOT, list_machines }, { "list-machines", VERB_ANY, VERB_ANY, VERB_NOCHROOT|VERB_MUSTBEROOT, list_machines },
{ "clear-jobs", VERB_ANY, 1, VERB_NOCHROOT, trivial_method }, { "clear-jobs", VERB_ANY, 1, VERB_NOCHROOT, trivial_method },
{ "cancel", VERB_ANY, VERB_ANY, VERB_NOCHROOT, cancel_job }, { "cancel", VERB_ANY, VERB_ANY, VERB_NOCHROOT, cancel_job },
{ "start", 2, VERB_ANY, VERB_NOCHROOT, start_unit }, { "start", 2, VERB_ANY, VERB_NOCHROOT, start_unit },