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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
/*
* Copyright (C) 2014-2022 Firejail Authors
*
* This file is part of firejail project
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <assert.h>
#include "../include/etc_groups.h"
#define errExit(msg) do { char msgout[500]; sprintf(msgout, "Error %s:%s(%d)", msg, __FUNCTION__, __LINE__); perror(msgout); exit(1);} while (0)
#define MAX_BUF 4098
#define MAX_ARR 1024
char *arr[MAX_ARR] = {NULL};
int arr_cnt = 0;
static int arr_tls_ca = 0;
static int arr_x11 = 0;
static int arr_games = 0;
static char outbuf[256 * 1024];
static char *outptr;
void outprintf(char* fmt, ...) {
va_list args;
va_start(args,fmt);
outptr += vsprintf(outptr, fmt, args);
va_end(args);
}
static int arr_check(const char *fname, char **pptr) {
assert(fname);
assert(pptr);
while (*pptr != NULL) {
if (strcmp(fname, *pptr) == 0)
return 1;
pptr++;
}
return 0;
}
static void arr_add(const char *fname) {
assert(fname);
assert(arr_cnt < MAX_ARR);
int i;
for (i = 0; i < arr_cnt; i++)
if (strcmp(arr[i], fname) == 0)
return;
arr[arr_cnt] = strdup(fname);
if (!arr[arr_cnt])
errExit("strdup");
arr_cnt++;
}
static void arr_clean(void) {
int i;
for (i = 0; i < arr_cnt; i++) {
free(arr[i]);
arr[i] = NULL;
}
arr_cnt = 0;
arr_games = 0;
arr_tls_ca = 0;
arr_x11 = 0;
}
static char *arr_print(void) {
char *last_line = outptr;
outprintf("private-etc ");
if (arr_games)
outprintf("@games,");
if (arr_tls_ca)
outprintf("@tls-ca,");
if (arr_x11)
outprintf("@x11,");
int i;
for (i = 0; i < arr_cnt; i++)
outprintf("%s,", arr[i]);
if (*(outptr - 1) == ' ' || *(outptr - 1) == ',') {
outptr--;
*outptr = '\0';
}
outprintf("\n");
return last_line;
}
static void process_file(const char *fname) {
assert(fname);
FILE *fp = fopen(fname, "r");
if (!fp) {
fprintf(stderr, "Error: cannot open profile file\n");
exit(1);
}
outptr = outbuf;
*outptr = '\0';
arr_clean();
char line[MAX_BUF];
char orig_line[MAX_BUF];
int cnt = 0;
int print = 0;
while (fgets(line, MAX_BUF, fp)) {
cnt++;
if (strncmp(line, "private-etc ", 12) != 0) {
outprintf("%s", line);
continue;
}
strcpy(orig_line,line);
char *ptr = strchr(line, '\n');
if (ptr)
*ptr = '\0';
ptr = line + 12;
while (*ptr == ' ' || *ptr == '\t')
ptr++;
// check for blanks and tabs
char *ptr2 = ptr;
while (*ptr2 != '\0') {
if (*ptr2 == ' ' || *ptr2 == '\t') {
fprintf(stderr, "Error: invalid private-etc line %s:%d\n", fname, cnt);
exit(1);
}
ptr2++;
}
ptr = strtok(ptr, ",");
while (ptr) {
if (arr_check(ptr, &etc_list[0]));
else if (arr_check(ptr, &etc_group_sound[0]));
else if (arr_check(ptr, &etc_group_network[0]));
else if (strcmp(ptr, "@games") == 0)
arr_games = 1;
else if (strcmp(ptr, "@tls-ca") == 0)
arr_tls_ca = 1;
else if (strcmp(ptr, "@x11") == 0)
arr_x11 = 1;
else if (arr_check(ptr, &etc_group_games[0]))
arr_games = 1;
else if (arr_check(ptr, &etc_group_tls_ca[0]))
arr_tls_ca = 1;
else if (arr_check(ptr, &etc_group_x11[0]))
arr_x11 = 1;
else
arr_add(ptr);
ptr = strtok(NULL, ",");
}
char *last_line = arr_print();
if (strcmp(last_line, orig_line) == 0) {
fclose(fp);
return;
}
printf("\n********************\n%s\n\n%s\n%s\n", fname, orig_line, last_line);
print = 1;
}
fclose(fp);
if (print) {
// printf("Replace? (Y/N): ", fname);
// fgets(line, MAX_BUF, stdin);
// if (*line == 'y' || *line == 'Y') {
fp = fopen(fname, "w");
if (!fp) {
fprintf(stderr, "Error: cannot open profile file\n");
exit(1);
}
fprintf(fp, "%s", outbuf);
fclose(fp);
// }
}
}
static void usage(void) {
printf("usage: cleanup-etc file.profile\n");
}
int main(int argc, char **argv) {
if (argc < 2) {
fprintf(stderr, "Error: invalid number of parameters\n");
usage();
return 1;
}
int i;
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-h") == 0) {
usage();
return 0;
}
}
for (i = 1; i < argc; i++)
process_file(argv[i]);
return 0;
}
|