2 * chsmack - Set smack attributes on files
4 * Copyright (C) 2011 Nokia Corporation.
5 * Copyright (C) 2012 Samsung Electronics Co.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, version 2.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 * Casey Schaufler <casey@schaufler-ca.com>
23 * Rafal Krypa <r.krypa@samsung.com>
26 #include <sys/types.h>
28 #include <sys/smack.h>
36 static inline int leads(char *in, char *lead)
38 return (strncmp(in, lead, strlen(lead)) == 0);
42 main(int argc, char *argv[])
52 for (argi = 1; argi < argc; argi++) {
53 if (strcmp(argv[argi], "-a") == 0)
54 access = argv[++argi];
55 else if (leads(argv[argi], "--access="))
56 access = argv[argi] + strlen("--access=");
57 else if (strcmp(argv[argi], "-e") == 0)
58 execute = argv[++argi];
59 else if (leads(argv[argi], "--exec="))
60 execute = argv[argi] + strlen("--exec=");
61 else if (leads(argv[argi], "--execute="))
62 execute = argv[argi] + strlen("--execute=");
63 else if (strcmp(argv[argi], "-m") == 0)
65 else if (leads(argv[argi], "--mmap="))
66 mm = argv[argi] + strlen("--mmap=");
67 else if (strcmp(argv[argi], "-t") == 0)
69 else if (strcmp(argv[argi], "--transmute") == 0)
71 else if (*argv[argi] == '-') {
72 fprintf(stderr, "Invalid argument \"%s\".\n",
77 * Indicates the start of filenames.
83 fprintf(stderr, "No files specified.\n");
86 if (access != NULL && strlen(access) > SMACK_LABEL_LEN) {
87 fprintf(stderr, "Access label \"%s\" exceeds %d characters.\n",
88 access, SMACK_LABEL_LEN);
91 if (mm != NULL && strlen(mm) > SMACK_LABEL_LEN) {
92 fprintf(stderr, "mmap label \"%s\" exceeds %d characters.\n",
96 if (execute != NULL && strlen(execute) > SMACK_LABEL_LEN) {
97 fprintf(stderr, "execute label \"%s\" exceeds %d characters.\n",
98 execute, SMACK_LABEL_LEN);
101 for (; argi < argc; argi++) {
102 if (access == NULL && mm == NULL &&
103 execute == NULL && !transmute) {
104 printf("%s", argv[argi]);
105 rc = smack_lgetlabel(argv[argi], &buffer, SMACK_LABEL_ACCESS);
106 if (rc == 0 && buffer != NULL) {
107 printf(" access=\"%s\"", buffer);
110 rc = smack_lgetlabel(argv[argi], &buffer, SMACK_LABEL_EXEC);
111 if (rc == 0 && buffer != NULL) {
112 printf(" execute=\"%s\"", buffer);
115 rc = smack_lgetlabel(argv[argi], &buffer, SMACK_LABEL_MMAP);
116 if (rc == 0 && buffer != NULL) {
117 printf(" mmap=\"%s\"", buffer);
120 rc = smack_lgetlabel(argv[argi], &buffer, SMACK_LABEL_TRANSMUTE);
121 if (rc == 0 && buffer != NULL) {
122 printf(" transmute=\"%s\"", buffer);
128 if (access != NULL) {
129 rc = smack_lsetlabel(argv[argi], access, SMACK_LABEL_ACCESS);
133 if (execute != NULL) {
134 rc = smack_lsetlabel(argv[argi], execute, SMACK_LABEL_EXEC);
139 rc = smack_lsetlabel(argv[argi], mm, SMACK_LABEL_MMAP);
144 rc = smack_lsetlabel(argv[argi], "1", SMACK_LABEL_TRANSMUTE);