xref: /aosp_15_r20/external/toybox/toys/android/restorecon.c (revision cf5a6c84e2b8763fc1a7db14496fd4742913b199)
1 /* restorecon.c - Restore default security contexts for files
2  *
3  * Copyright 2015 The Android Open Source Project
4 
5 USE_RESTORECON(NEWTOY(restorecon, "<1DFnRrv", TOYFLAG_USR|TOYFLAG_SBIN))
6 
7 config RESTORECON
8   bool "restorecon"
9   depends on TOYBOX_SELINUX
10   default y
11   help
12     usage: restorecon [-D] [-F] [-R] [-n] [-v] FILE...
13 
14     Restores the default security contexts for the given files.
15 
16     -D	Apply to /data/data too
17     -F	Force reset
18     -R	Recurse into directories
19     -n	Don't make any changes; useful with -v to see what would change
20     -v	Verbose
21 */
22 
23 #define FOR_restorecon
24 #include "toys.h"
25 
26 #if defined(__ANDROID__)
27 #include <selinux/android.h>
28 #endif
29 
restorecon_main(void)30 void restorecon_main(void)
31 {
32 #if defined(__ANDROID__)
33   char **s;
34   int flags = 0;
35 
36   if (FLAG(D)) flags |= SELINUX_ANDROID_RESTORECON_DATADATA;
37   if (FLAG(F)) flags |= SELINUX_ANDROID_RESTORECON_FORCE;
38   if (FLAG(R) || FLAG(r)) flags |= SELINUX_ANDROID_RESTORECON_RECURSE;
39   if (FLAG(n)) flags |= SELINUX_ANDROID_RESTORECON_NOCHANGE;
40   if (FLAG(v)) flags |= SELINUX_ANDROID_RESTORECON_VERBOSE;
41 
42   for (s = toys.optargs; *s; s++)
43     if (selinux_android_restorecon(*s, flags) < 0)
44       perror_msg("restorecon failed: %s", *s);
45 #endif
46 }
47