1 /* chcon.c - Change file security context
2 *
3 * Copyright 2014 The Android Open Source Project
4
5 USE_CHCON(NEWTOY(chcon, "<2hvR", TOYFLAG_USR|TOYFLAG_BIN))
6
7 config CHCON
8 bool "chcon"
9 depends on TOYBOX_SELINUX
10 default y
11 help
12 usage: chcon [-hRv] CONTEXT FILE...
13
14 Change the SELinux security context of listed file[s].
15
16 -h change symlinks instead of what they point to.
17 -R recurse into subdirectories.
18 -v verbose output.
19 */
20
21 #define FOR_chcon
22 #include "toys.h"
23
do_chcon(struct dirtree * try)24 int do_chcon(struct dirtree *try)
25 {
26 char *path, *con = *toys.optargs;
27
28 if (!dirtree_notdotdot(try)) return 0;
29
30 path = dirtree_path(try, 0);
31 if (toys.optflags & FLAG_v) printf("chcon '%s' to %s\n", path, con);
32 if (-1 == ((toys.optflags & FLAG_h) ? lsetfilecon : setfilecon)(path, con))
33 perror_msg("'%s' to %s", path, con);
34 free(path);
35
36 return (toys.optflags & FLAG_R)*DIRTREE_RECURSE;
37 }
38
chcon_main(void)39 void chcon_main(void)
40 {
41 char **file;
42
43 for (file = toys.optargs+1; *file; file++) dirtree_read(*file, do_chcon);
44 }
45