aboutsummaryrefslogtreecommitdiff
path: root/toys/pending/chcon.c
blob: 41259de6ca39cacf05d54ae07315e5ddd68d7484 (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
48
49
50
51
/* chcon.c - Change file security context
 *
 * Copyright 2014 The Android Open Source Project

USE_CHCON(NEWTOY(chcon, "hRv", TOYFLAG_USR|TOYFLAG_BIN))

config CHCON
  bool "chcon"
  default n
  help
    usage: chcon [-hRv] CONTEXT FILE...

    Change the SELinux security context of listed file[s] (recursively with -R).

    -h change symlinks instead of what they point to.
    -R recurse into subdirectories.
    -v verbose output.
*/

#define FOR_chcon
#include "toys.h"
#include <selinux/selinux.h>

GLOBALS(
  char *context;
)

int do_chcon(struct dirtree *try)
{
  int ret;

  if (!dirtree_notdotdot(try)) return 0;

  char *path = dirtree_path(try, 0);
  if (toys.optflags & FLAG_v)
    printf("chcon '%s' to %s\n", path, TT.context);
  ret = ((toys.optflags&FLAG_h) ? lsetfilecon : setfilecon)(path, TT.context);
  if (ret == -1)
    perror_msg("'%s' to %s", path, TT.context);
  free(path);

  return (toys.optflags & FLAG_R) ? DIRTREE_RECURSE : 0;
}

void chcon_main(void)
{
  TT.context = *toys.optargs;
  char **file;

  for (file = toys.optargs+1; *file; file++) dirtree_read(*file, do_chcon);
}