source code open - smack
[framework/security/smack.git] / utils / chsmack.c
1 /*
2  * chsmack - Set smack attributes on files
3  *
4  * Copyright (C) 2011 Nokia Corporation.
5  * Copyright (C) 2012 Samsung Electronics Co.
6  *
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.
10  *
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.
15  *
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
19  *      02110-1301 USA
20  *
21  * Author:
22  *      Casey Schaufler <casey@schaufler-ca.com>
23  *      Rafal Krypa <r.krypa@samsung.com>
24  */
25
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/smack.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35
36 static inline int leads(char *in, char *lead)
37 {
38         return (strncmp(in, lead, strlen(lead)) == 0);
39 }
40
41 int
42 main(int argc, char *argv[])
43 {
44         int rc;
45         int argi;
46         int transmute = 0;
47         char *buffer;
48         char *access = NULL;
49         char *mm = NULL;
50         char *execute = NULL;
51
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)
64                         mm = argv[++argi];
65                 else if (leads(argv[argi], "--mmap="))
66                         mm = argv[argi] + strlen("--mmap=");
67                 else if (strcmp(argv[argi], "-t") == 0)
68                         transmute = 1;
69                 else if (strcmp(argv[argi], "--transmute") == 0)
70                         transmute = 1;
71                 else if (*argv[argi] == '-') {
72                         fprintf(stderr, "Invalid argument \"%s\".\n",
73                                 argv[argi]);
74                         exit(1);
75                 }
76                 /*
77                  * Indicates the start of filenames.
78                  */
79                 else
80                         break;
81         }
82         if (argi >= argc) {
83                 fprintf(stderr, "No files specified.\n");
84                 exit(1);
85         }
86         if (access != NULL && strlen(access) > SMACK_LABEL_LEN) {
87                 fprintf(stderr, "Access label \"%s\" exceeds %d characters.\n",
88                         access, SMACK_LABEL_LEN);
89                 exit(1);
90         }
91         if (mm != NULL && strlen(mm) > SMACK_LABEL_LEN) {
92                 fprintf(stderr, "mmap label \"%s\" exceeds %d characters.\n",
93                         mm, SMACK_LABEL_LEN);
94                 exit(1);
95         }
96         if (execute != NULL && strlen(execute) > SMACK_LABEL_LEN) {
97                 fprintf(stderr, "execute label \"%s\" exceeds %d characters.\n",
98                         execute, SMACK_LABEL_LEN);
99                 exit(1);
100         }
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);
108                                 free(buffer);
109                         }
110                         rc = smack_lgetlabel(argv[argi], &buffer, SMACK_LABEL_EXEC);
111                         if (rc == 0 && buffer != NULL) {
112                                 printf(" execute=\"%s\"", buffer);
113                                 free(buffer);
114                         }
115                         rc = smack_lgetlabel(argv[argi], &buffer, SMACK_LABEL_MMAP);
116                         if (rc == 0 && buffer != NULL) {
117                                 printf(" mmap=\"%s\"", buffer);
118                                 free(buffer);
119                         }
120                         rc = smack_lgetlabel(argv[argi], &buffer, SMACK_LABEL_TRANSMUTE);
121                         if (rc == 0 && buffer != NULL) {
122                                 printf(" transmute=\"%s\"", buffer);
123                                 free(buffer);
124                         }
125                         printf("\n");
126                         continue;
127                 }
128                 if (access != NULL) {
129                         rc = smack_lsetlabel(argv[argi], access, SMACK_LABEL_ACCESS);
130                         if (rc < 0)
131                                 perror(argv[argi]);
132                 }
133                 if (execute != NULL) {
134                         rc = smack_lsetlabel(argv[argi], execute, SMACK_LABEL_EXEC);
135                         if (rc < 0)
136                                 perror(argv[argi]);
137                 }
138                 if (mm != NULL) {
139                         rc = smack_lsetlabel(argv[argi], mm, SMACK_LABEL_MMAP);
140                         if (rc < 0)
141                                 perror(argv[argi]);
142                 }
143                 if (transmute) {
144                         rc = smack_lsetlabel(argv[argi], "1", SMACK_LABEL_TRANSMUTE);
145                         if (rc < 0)
146                                 perror(argv[argi]);
147                 }
148         }
149         exit(0);
150 }