10f66f451Sopenharmony_ci/* restorecon.c - Restore default security contexts for files
20f66f451Sopenharmony_ci *
30f66f451Sopenharmony_ci * Copyright 2015 The Android Open Source Project
40f66f451Sopenharmony_ci
50f66f451Sopenharmony_ciUSE_RESTORECON(NEWTOY(restorecon, "<1DFnRrv", TOYFLAG_USR|TOYFLAG_SBIN))
60f66f451Sopenharmony_ci
70f66f451Sopenharmony_ciconfig RESTORECON
80f66f451Sopenharmony_ci  bool "restorecon"
90f66f451Sopenharmony_ci  depends on TOYBOX_SELINUX
100f66f451Sopenharmony_ci  default y
110f66f451Sopenharmony_ci  help
120f66f451Sopenharmony_ci    usage: restorecon [-D] [-F] [-R] [-n] [-v] FILE...
130f66f451Sopenharmony_ci
140f66f451Sopenharmony_ci    Restores the default security contexts for the given files.
150f66f451Sopenharmony_ci
160f66f451Sopenharmony_ci    -D	Apply to /data/data too
170f66f451Sopenharmony_ci    -F	Force reset
180f66f451Sopenharmony_ci    -R	Recurse into directories
190f66f451Sopenharmony_ci    -n	Don't make any changes; useful with -v to see what would change
200f66f451Sopenharmony_ci    -v	Verbose
210f66f451Sopenharmony_ci*/
220f66f451Sopenharmony_ci
230f66f451Sopenharmony_ci#define FOR_restorecon
240f66f451Sopenharmony_ci#include "toys.h"
250f66f451Sopenharmony_ci
260f66f451Sopenharmony_ci#if defined(__ANDROID__)
270f66f451Sopenharmony_ci#include <selinux/android.h>
280f66f451Sopenharmony_ci#endif
290f66f451Sopenharmony_ci
300f66f451Sopenharmony_civoid restorecon_main(void)
310f66f451Sopenharmony_ci{
320f66f451Sopenharmony_ci#if defined(__ANDROID__)
330f66f451Sopenharmony_ci  char **s;
340f66f451Sopenharmony_ci  int flags = 0;
350f66f451Sopenharmony_ci
360f66f451Sopenharmony_ci  if (toys.optflags & FLAG_D) flags |= SELINUX_ANDROID_RESTORECON_DATADATA;
370f66f451Sopenharmony_ci  if (toys.optflags & FLAG_F) flags |= SELINUX_ANDROID_RESTORECON_FORCE;
380f66f451Sopenharmony_ci  if (toys.optflags & (FLAG_R|FLAG_r))
390f66f451Sopenharmony_ci    flags |= SELINUX_ANDROID_RESTORECON_RECURSE;
400f66f451Sopenharmony_ci  if (toys.optflags & FLAG_n) flags |= SELINUX_ANDROID_RESTORECON_NOCHANGE;
410f66f451Sopenharmony_ci  if (toys.optflags & FLAG_v) flags |= SELINUX_ANDROID_RESTORECON_VERBOSE;
420f66f451Sopenharmony_ci
430f66f451Sopenharmony_ci  for (s = toys.optargs; *s; s++)
440f66f451Sopenharmony_ci    if (selinux_android_restorecon(*s, flags) < 0)
450f66f451Sopenharmony_ci      perror_msg("restorecon failed: %s", *s);
460f66f451Sopenharmony_ci#endif
470f66f451Sopenharmony_ci}
48