aboutsummaryrefslogtreecommitdiff
path: root/toys/android/restorecon.c
blob: 5ea6b3f948b252f805851a438fc3915a98438de2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/* restorecon.c - Restore default security contexts for files
 *
 * Copyright 2015 The Android Open Source Project

USE_RESTORECON(NEWTOY(restorecon, "<1DFnRrv", TOYFLAG_USR|TOYFLAG_SBIN))

config RESTORECON
  bool "restorecon"
  depends on TOYBOX_SELINUX
  default y
  help
    usage: restorecon [-D] [-F] [-R] [-n] [-v] FILE...

    Restores the default security contexts for the given files.

    -D	apply to /data/data too
    -F	force reset
    -R	recurse into directories
    -n	don't make any changes; useful with -v to see what would change
    -v	verbose: show any changes
*/

#define FOR_restorecon
#include "toys.h"

#if defined(__ANDROID__)
#include <selinux/android.h>
#endif

void restorecon_main(void)
{
#if defined(__ANDROID__)
  char **s;
  int flags = 0;

  if (toys.optflags & FLAG_D) flags |= SELINUX_ANDROID_RESTORECON_DATADATA;
  if (toys.optflags & FLAG_F) flags |= SELINUX_ANDROID_RESTORECON_FORCE;
  if (toys.optflags & (FLAG_R|FLAG_r))
    flags |= SELINUX_ANDROID_RESTORECON_RECURSE;
  if (toys.optflags & FLAG_n) flags |= SELINUX_ANDROID_RESTORECON_NOCHANGE;
  if (toys.optflags & FLAG_v) flags |= SELINUX_ANDROID_RESTORECON_VERBOSE;

  for (s = toys.optargs; *s; s++)
    if (selinux_android_restorecon(*s, flags) < 0)
      perror_msg("restorecon failed: %s", *s);
#endif
}