setenforce.c 862 B

1234567891011121314151617181920212223242526272829303132
  1. /* setenforce.c - Set the current SELinux mode
  2. *
  3. * Copyright 2014 The Android Open Source Project
  4. USE_SETENFORCE(NEWTOY(setenforce, "<1>1", TOYFLAG_USR|TOYFLAG_SBIN))
  5. config SETENFORCE
  6. bool "setenforce"
  7. default y
  8. depends on TOYBOX_SELINUX
  9. help
  10. usage: setenforce [enforcing|permissive|1|0]
  11. Sets whether SELinux is enforcing (1) or permissive (0).
  12. */
  13. #define FOR_setenforce
  14. #include "toys.h"
  15. void setenforce_main(void)
  16. {
  17. char *new = *toys.optargs;
  18. int state, ret;
  19. if (!is_selinux_enabled()) error_exit("SELinux is disabled");
  20. else if (!strcmp(new, "1") || !strcasecmp(new, "enforcing")) state = 1;
  21. else if (!strcmp(new, "0") || !strcasecmp(new, "permissive")) state = 0;
  22. else error_exit("Invalid state: %s", new);
  23. ret = security_setenforce(state);
  24. if (ret == -1) perror_msg("Couldn't set enforcing status to '%s'", new);
  25. }