10f66f451Sopenharmony_ci/* chcon.c - Change file security context
20f66f451Sopenharmony_ci *
30f66f451Sopenharmony_ci * Copyright 2014 The Android Open Source Project
40f66f451Sopenharmony_ci
50f66f451Sopenharmony_ciUSE_CHCON(NEWTOY(chcon, "<2hvR", TOYFLAG_USR|TOYFLAG_BIN))
60f66f451Sopenharmony_ci
70f66f451Sopenharmony_ciconfig CHCON
80f66f451Sopenharmony_ci  bool "chcon"
90f66f451Sopenharmony_ci  depends on TOYBOX_SELINUX
100f66f451Sopenharmony_ci  default y
110f66f451Sopenharmony_ci  help
120f66f451Sopenharmony_ci    usage: chcon [-hRv] CONTEXT FILE...
130f66f451Sopenharmony_ci
140f66f451Sopenharmony_ci    Change the SELinux security context of listed file[s].
150f66f451Sopenharmony_ci
160f66f451Sopenharmony_ci    -h	Change symlinks instead of what they point to
170f66f451Sopenharmony_ci    -R	Recurse into subdirectories
180f66f451Sopenharmony_ci    -v	Verbose
190f66f451Sopenharmony_ci*/
200f66f451Sopenharmony_ci
210f66f451Sopenharmony_ci#define FOR_chcon
220f66f451Sopenharmony_ci#include "toys.h"
230f66f451Sopenharmony_ci
240f66f451Sopenharmony_cistatic int do_chcon(struct dirtree *try)
250f66f451Sopenharmony_ci{
260f66f451Sopenharmony_ci  char *path, *con = *toys.optargs;
270f66f451Sopenharmony_ci
280f66f451Sopenharmony_ci  if (!dirtree_notdotdot(try)) return 0;
290f66f451Sopenharmony_ci
300f66f451Sopenharmony_ci  path = dirtree_path(try, 0);
310f66f451Sopenharmony_ci  if (toys.optflags & FLAG_v) printf("chcon '%s' to %s\n", path, con);
320f66f451Sopenharmony_ci  if (-1 == ((toys.optflags & FLAG_h) ? lsetfilecon : setfilecon)(path, con))
330f66f451Sopenharmony_ci    perror_msg("'%s' to %s", path, con);
340f66f451Sopenharmony_ci  free(path);
350f66f451Sopenharmony_ci
360f66f451Sopenharmony_ci  return (toys.optflags & FLAG_R)*DIRTREE_RECURSE;
370f66f451Sopenharmony_ci}
380f66f451Sopenharmony_ci
390f66f451Sopenharmony_civoid chcon_main(void)
400f66f451Sopenharmony_ci{
410f66f451Sopenharmony_ci  char **file;
420f66f451Sopenharmony_ci
430f66f451Sopenharmony_ci  for (file = toys.optargs+1; *file; file++) dirtree_read(*file, do_chcon);
440f66f451Sopenharmony_ci}
45