diff options
Diffstat (limited to 'src/etc-cleanup/main.c')
-rw-r--r-- | src/etc-cleanup/main.c | 255 |
1 files changed, 255 insertions, 0 deletions
diff --git a/src/etc-cleanup/main.c b/src/etc-cleanup/main.c new file mode 100644 index 000000000..47fe1556b --- /dev/null +++ b/src/etc-cleanup/main.c | |||
@@ -0,0 +1,255 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2014-2022 Firejail Authors | ||
3 | * | ||
4 | * This file is part of firejail project | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU General Public License as published by | ||
8 | * the Free Software Foundation; either version 2 of the License, or | ||
9 | * (at your option) any later version. | ||
10 | * | ||
11 | * This program is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | * GNU General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU General Public License along | ||
17 | * with this program; if not, write to the Free Software Foundation, Inc., | ||
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
19 | */ | ||
20 | |||
21 | #include "../include/etc_groups.h" | ||
22 | #include "../include/common.h" | ||
23 | #include <stdarg.h> | ||
24 | |||
25 | #define MAX_BUF 4098 | ||
26 | #define MAX_ARR 1024 | ||
27 | char *arr[MAX_ARR] = {NULL}; | ||
28 | int arr_cnt = 0; | ||
29 | |||
30 | static int arr_tls_ca = 0; | ||
31 | static int arr_x11 = 0; | ||
32 | static int arr_games = 0; | ||
33 | static char outbuf[256 * 1024]; | ||
34 | static char *outptr; | ||
35 | static int arg_replace = 0; | ||
36 | static int arg_debug = 0; | ||
37 | |||
38 | void outprintf(char* fmt, ...) { | ||
39 | va_list args; | ||
40 | va_start(args,fmt); | ||
41 | outptr += vsprintf(outptr, fmt, args); | ||
42 | va_end(args); | ||
43 | } | ||
44 | |||
45 | |||
46 | |||
47 | static int arr_check(const char *fname, char **pptr) { | ||
48 | assert(fname); | ||
49 | assert(pptr); | ||
50 | |||
51 | while (*pptr != NULL) { | ||
52 | if (strcmp(fname, *pptr) == 0) | ||
53 | return 1; | ||
54 | pptr++; | ||
55 | } | ||
56 | |||
57 | return 0; | ||
58 | } | ||
59 | |||
60 | |||
61 | |||
62 | static void arr_add(const char *fname) { | ||
63 | assert(fname); | ||
64 | assert(arr_cnt < MAX_ARR); | ||
65 | |||
66 | int i; | ||
67 | for (i = 0; i < arr_cnt; i++) | ||
68 | if (strcmp(arr[i], fname) == 0) | ||
69 | return; | ||
70 | |||
71 | arr[arr_cnt] = strdup(fname); | ||
72 | if (!arr[arr_cnt]) | ||
73 | errExit("strdup"); | ||
74 | arr_cnt++; | ||
75 | } | ||
76 | |||
77 | int arr_cmp(const void *p1, const void *p2) { | ||
78 | char **ptr1 = (char **) p1; | ||
79 | char **ptr2 = (char **) p2; | ||
80 | |||
81 | return strcmp(*ptr1, *ptr2); | ||
82 | } | ||
83 | |||
84 | static void arr_sort(void) { | ||
85 | qsort(&arr[0], arr_cnt, sizeof(char *), arr_cmp); | ||
86 | } | ||
87 | |||
88 | static void arr_clean(void) { | ||
89 | int i; | ||
90 | for (i = 0; i < arr_cnt; i++) { | ||
91 | free(arr[i]); | ||
92 | arr[i] = NULL; | ||
93 | } | ||
94 | |||
95 | arr_cnt = 0; | ||
96 | arr_games = 0; | ||
97 | arr_tls_ca = 0; | ||
98 | arr_x11 = 0; | ||
99 | } | ||
100 | |||
101 | static char *arr_print(void) { | ||
102 | char *last_line = outptr; | ||
103 | outprintf("private-etc "); | ||
104 | |||
105 | if (arr_games) | ||
106 | outprintf("@games,"); | ||
107 | if (arr_tls_ca) | ||
108 | outprintf("@tls-ca,"); | ||
109 | if (arr_x11) | ||
110 | outprintf("@x11,"); | ||
111 | |||
112 | int i; | ||
113 | for (i = 0; i < arr_cnt; i++) | ||
114 | outprintf("%s,", arr[i]); | ||
115 | if (*(outptr - 1) == ' ' || *(outptr - 1) == ',') { | ||
116 | outptr--; | ||
117 | *outptr = '\0'; | ||
118 | } | ||
119 | outprintf("\n"); | ||
120 | |||
121 | return last_line; | ||
122 | } | ||
123 | |||
124 | static void process_file(const char *fname) { | ||
125 | assert(fname); | ||
126 | |||
127 | FILE *fp = fopen(fname, "r"); | ||
128 | if (!fp) { | ||
129 | fprintf(stderr, "Error: cannot open %s file\n", fname); | ||
130 | exit(1); | ||
131 | } | ||
132 | |||
133 | outptr = outbuf; | ||
134 | *outptr = '\0'; | ||
135 | arr_clean(); | ||
136 | |||
137 | char line[MAX_BUF]; | ||
138 | char orig_line[MAX_BUF]; | ||
139 | int cnt = 0; | ||
140 | int print = 0; | ||
141 | while (fgets(line, MAX_BUF, fp)) { | ||
142 | cnt++; | ||
143 | if (strncmp(line, "private-etc", 11) != 0) { | ||
144 | outprintf("%s", line); | ||
145 | continue; | ||
146 | } | ||
147 | |||
148 | strcpy(orig_line,line); | ||
149 | char *ptr = strchr(line, '\n'); | ||
150 | if (ptr) | ||
151 | *ptr = '\0'; | ||
152 | |||
153 | ptr = line + 12; | ||
154 | while (*ptr == ' ' || *ptr == '\t') | ||
155 | ptr++; | ||
156 | |||
157 | // check for blanks and tabs | ||
158 | char *ptr2 = ptr; | ||
159 | while (*ptr2 != '\0') { | ||
160 | if (*ptr2 == ' ' || *ptr2 == '\t') { | ||
161 | fprintf(stderr, "Error: invalid private-etc line %s:%d\n", fname, cnt); | ||
162 | exit(1); | ||
163 | } | ||
164 | ptr2++; | ||
165 | } | ||
166 | |||
167 | ptr = strtok(ptr, ","); | ||
168 | while (ptr) { | ||
169 | if (arg_debug) | ||
170 | printf("%s\n", ptr); | ||
171 | if (arr_check(ptr, &etc_list[0])); | ||
172 | else if (arr_check(ptr, &etc_group_sound[0])); | ||
173 | else if (arr_check(ptr, &etc_group_network[0])); | ||
174 | else if (strcmp(ptr, "@games") == 0) | ||
175 | arr_games = 1; | ||
176 | else if (strcmp(ptr, "@tls-ca") == 0) | ||
177 | arr_tls_ca = 1; | ||
178 | else if (strcmp(ptr, "@x11") == 0) | ||
179 | arr_x11 = 1; | ||
180 | else if (arr_check(ptr, &etc_group_games[0])) | ||
181 | arr_games = 1; | ||
182 | else if (arr_check(ptr, &etc_group_tls_ca[0])) | ||
183 | arr_tls_ca = 1; | ||
184 | else if (arr_check(ptr, &etc_group_x11[0])) | ||
185 | arr_x11 = 1; | ||
186 | else | ||
187 | arr_add(ptr); | ||
188 | |||
189 | ptr = strtok(NULL, ","); | ||
190 | } | ||
191 | |||
192 | arr_sort(); | ||
193 | char *last_line = arr_print(); | ||
194 | if (strcmp(last_line, orig_line) == 0) { | ||
195 | fclose(fp); | ||
196 | return; | ||
197 | } | ||
198 | printf("\n********************\nfile: %s\n\nold: %s\nnew: %s\n", fname, orig_line, last_line); | ||
199 | print = 1; | ||
200 | } | ||
201 | |||
202 | fclose(fp); | ||
203 | |||
204 | if (print && arg_replace) { | ||
205 | fp = fopen(fname, "w"); | ||
206 | if (!fp) { | ||
207 | fprintf(stderr, "Error: cannot open profile file\n"); | ||
208 | exit(1); | ||
209 | } | ||
210 | fprintf(fp, "%s", outbuf); | ||
211 | fclose(fp); | ||
212 | } | ||
213 | } | ||
214 | |||
215 | static void usage(void) { | ||
216 | printf("usage: cleanup-etc [options] file.profile [file.profile]\n"); | ||
217 | printf("Group and clean private-etc entries in one or more profile files.\n"); | ||
218 | printf("Options:\n"); | ||
219 | printf(" --debug - print debug messages\n"); | ||
220 | printf(" -h, -?, --help - this help screen\n"); | ||
221 | printf(" --replace - replace profile file\n"); | ||
222 | } | ||
223 | |||
224 | int main(int argc, char **argv) { | ||
225 | if (argc < 2) { | ||
226 | fprintf(stderr, "Error: invalid number of parameters\n"); | ||
227 | usage(); | ||
228 | return 1; | ||
229 | } | ||
230 | |||
231 | int i; | ||
232 | for (i = 1; i < argc; i++) { | ||
233 | if (strcmp(argv[i], "-h") == 0 || | ||
234 | strcmp(argv[i], "-?") == 0 || | ||
235 | strcmp(argv[i], "--help") == 0) { | ||
236 | usage(); | ||
237 | return 0; | ||
238 | } | ||
239 | else if (strcmp(argv[i], "--debug") == 0) | ||
240 | arg_debug = 1; | ||
241 | else if (strcmp(argv[i], "--replace") == 0) | ||
242 | arg_replace = 1; | ||
243 | else if (*argv[i] == '-') { | ||
244 | fprintf(stderr, "Error: invalid program option %s\n", argv[i]); | ||
245 | return 1; | ||
246 | } | ||
247 | else | ||
248 | break; | ||
249 | } | ||
250 | |||
251 | for (; i < argc; i++) | ||
252 | process_file(argv[i]); | ||
253 | |||
254 | return 0; | ||
255 | } \ No newline at end of file | ||