Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
Merge "Change setsebool syntax to be consistent with other init built…
Browse files Browse the repository at this point in the history
…-ins."
  • Loading branch information
Dima Zavin authored and Gerrit Code Review committed Dec 19, 2012
2 parents 516943c + 0e23fee commit 82ea44f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 48 deletions.
39 changes: 17 additions & 22 deletions init/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -754,34 +754,29 @@ int do_restorecon(int nargs, char **args) {
}

int do_setsebool(int nargs, char **args) {
SELboolean *b = alloca(nargs * sizeof(SELboolean));
char *v;
int i;
const char *name = args[1];
const char *value = args[2];
SELboolean b;
int ret;

if (is_selinux_enabled() <= 0)
return 0;

for (i = 1; i < nargs; i++) {
char *name = args[i];
v = strchr(name, '=');
if (!v) {
ERROR("setsebool: argument %s had no =\n", name);
return -EINVAL;
}
*v++ = 0;
b[i-1].name = name;
if (!strcmp(v, "1") || !strcasecmp(v, "true") || !strcasecmp(v, "on"))
b[i-1].value = 1;
else if (!strcmp(v, "0") || !strcasecmp(v, "false") || !strcasecmp(v, "off"))
b[i-1].value = 0;
else {
ERROR("setsebool: invalid value %s\n", v);
return -EINVAL;
}
b.name = name;
if (!strcmp(value, "1") || !strcasecmp(value, "true") || !strcasecmp(value, "on"))
b.value = 1;
else if (!strcmp(value, "0") || !strcasecmp(value, "false") || !strcasecmp(value, "off"))
b.value = 0;
else {
ERROR("setsebool: invalid value %s\n", value);
return -EINVAL;
}

if (security_set_boolean_list(nargs - 1, b, 0) < 0)
return -errno;
if (security_set_boolean_list(1, &b, 0) < 0) {
ret = -errno;
ERROR("setsebool: could not set %s to %s\n", name, value);
return ret;
}

return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion init/keywords.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ enum {
KEYWORD(setkey, COMMAND, 0, do_setkey)
KEYWORD(setprop, COMMAND, 2, do_setprop)
KEYWORD(setrlimit, COMMAND, 3, do_setrlimit)
KEYWORD(setsebool, COMMAND, 1, do_setsebool)
KEYWORD(setsebool, COMMAND, 2, do_setsebool)
KEYWORD(socket, OPTION, 0, 0)
KEYWORD(start, COMMAND, 1, do_start)
KEYWORD(stop, COMMAND, 1, do_stop)
Expand Down
2 changes: 1 addition & 1 deletion init/readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ setprop <name> <value>
setrlimit <resource> <cur> <max>
Set the rlimit for a resource.

setsebool <name>=<value>
setsebool <name> <value>
Set SELinux boolean <name> to <value>.
<value> may be 1|true|on or 0|false|off

Expand Down
39 changes: 15 additions & 24 deletions toolbox/setsebool.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,26 @@
#include <errno.h>

static int do_setsebool(int nargs, char **args) {
SELboolean *b = alloca(nargs * sizeof(SELboolean));
char *v;
int i;
const char *name = args[1];
const char *value = args[2];
SELboolean b;

if (is_selinux_enabled() <= 0)
return 0;

for (i = 1; i < nargs; i++) {
char *name = args[i];
v = strchr(name, '=');
if (!v) {
fprintf(stderr, "setsebool: argument %s had no =\n", name);
return -1;
}
*v++ = 0;
b[i-1].name = name;
if (!strcmp(v, "1") || !strcasecmp(v, "true") || !strcasecmp(v, "on"))
b[i-1].value = 1;
else if (!strcmp(v, "0") || !strcasecmp(v, "false") || !strcasecmp(v, "off"))
b[i-1].value = 0;
else {
fprintf(stderr, "setsebool: invalid value %s\n", v);
return -1;
}
b.name = name;
if (!strcmp(value, "1") || !strcasecmp(value, "true") || !strcasecmp(value, "on"))
b.value = 1;
else if (!strcmp(value, "0") || !strcasecmp(value, "false") || !strcasecmp(value, "off"))
b.value = 0;
else {
fprintf(stderr, "setsebool: invalid value %s\n", value);
return -1;
}

if (security_set_boolean_list(nargs - 1, b, 0) < 0)
if (security_set_boolean_list(1, &b, 0) < 0)
{
fprintf(stderr, "setsebool: unable to set booleans: %s", strerror(errno));
fprintf(stderr, "setsebool: could not set %s to %s: %s", name, value, strerror(errno));
return -1;
}

Expand All @@ -46,8 +37,8 @@ static int do_setsebool(int nargs, char **args) {

int setsebool_main(int argc, char **argv)
{
if (argc < 2) {
fprintf(stderr, "Usage: %s name=value...\n", argv[0]);
if (argc != 3) {
fprintf(stderr, "Usage: %s name value\n", argv[0]);
exit(1);
}

Expand Down

0 comments on commit 82ea44f

Please sign in to comment.