Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "private-etc: big profile changes" #5645

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
private-etc: groups modified
(cherry picked from commit 0f996ea)

Committer note: This only applies the changes from src/.
  • Loading branch information
netblue30 authored and kmk3 committed Feb 7, 2023
commit c6c901bc1f9b0f7ba4915b95389acafb134f2aa0
2 changes: 2 additions & 0 deletions src/include/etc_groups.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ static char *etc_list[ETC_MAX + 1] = { // plus 1 for ending NULL pointer
"login.defs", // firejail reading UID/GID MIN and MAX at startup
"nsswitch.conf",
"passwd",
"selinux",
NULL
};

Expand Down Expand Up @@ -89,6 +90,7 @@ static char *etc_group_x11[] = {
"kde5rc",
"nvidia", // 3D
"pango", // text rendering/internationalization
"Trolltech.conf", // old QT config file
"X11",
"xdg",
NULL
Expand Down
66 changes: 48 additions & 18 deletions src/tools/cleanup_etc.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ static int arr_x11 = 0;
static int arr_games = 0;
static char outbuf[256 * 1024];
static char *outptr;
static int arg_replace = 0;
static int arg_debug = 0;

void outprintf(char* fmt, ...) {
va_list args;
Expand Down Expand Up @@ -78,6 +80,17 @@ static void arr_add(const char *fname) {
arr_cnt++;
}

int arr_cmp(const void *p1, const void *p2) {
char **ptr1 = (char **) p1;
char **ptr2 = (char **) p2;

return strcmp(*ptr1, *ptr2);
}

static void arr_sort(void) {
qsort(&arr[0], arr_cnt, sizeof(char *), arr_cmp);
}

static void arr_clean(void) {
int i;
for (i = 0; i < arr_cnt; i++) {
Expand Down Expand Up @@ -119,7 +132,7 @@ static void process_file(const char *fname) {

FILE *fp = fopen(fname, "r");
if (!fp) {
fprintf(stderr, "Error: cannot open profile file\n");
fprintf(stderr, "Error: cannot open %s file\n", fname);
exit(1);
}

Expand All @@ -133,10 +146,11 @@ static void process_file(const char *fname) {
int print = 0;
while (fgets(line, MAX_BUF, fp)) {
cnt++;
if (strncmp(line, "private-etc ", 12) != 0) {
if (strncmp(line, "private-etc", 11) != 0) {
outprintf("%s", line);
continue;
}

strcpy(orig_line,line);
char *ptr = strchr(line, '\n');
if (ptr)
Expand All @@ -158,6 +172,8 @@ static void process_file(const char *fname) {

ptr = strtok(ptr, ",");
while (ptr) {
if (arg_debug)
printf("%s\n", 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]));
Expand All @@ -179,34 +195,36 @@ static void process_file(const char *fname) {
ptr = strtok(NULL, ",");
}

arr_sort();
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);
printf("\n********************\nfile: %s\n\nold: %s\nnew: %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);
// }
if (print && arg_replace) {
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");
printf("usage: cleanup-etc [options] file.profile [file.profile]\n");
printf("Group and clean private-etc entries in one or more profile files.\n");
printf("Options:\n");
printf(" --debug - print debug messages\n");
printf(" --help - this help screen\n");
printf(" --replace - replace profile file\n");
}

int main(int argc, char **argv) {
Expand All @@ -218,13 +236,25 @@ int main(int argc, char **argv) {

int i;
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-h") == 0) {
if (strcmp(argv[i], "-h") == 0 ||
strcmp(argv[i], "-?") == 0 ||
strcmp(argv[i], "--help") == 0) {
usage();
return 0;
}
else if (strcmp(argv[i], "--debug") == 0)
arg_debug = 1;
else if (strcmp(argv[i], "--replace") == 0)
arg_replace = 1;
else if (*argv[i] == '-') {
fprintf(stderr, "Error: invalid program option %s\n", argv[i]);
return 1;
}
else
break;
}

for (i = 1; i < argc; i++)
for (; i < argc; i++)
process_file(argv[i]);

return 0;
Expand Down