aboutsummaryrefslogtreecommitdiffstats
path: root/src/fbuilder/filedb.c
blob: 94a226cb749a2d27a8660925305ec1650f42c863 (plain) (blame)
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
/*
 * Copyright (C) 2014-2021 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 "fbuilder.h"

// find exact name or an exact name in a parent directory
FileDB *filedb_find(FileDB *head, const char *fname) {
	assert(fname);
	FileDB *ptr = head;
	int found = 0;
	int len = strlen(fname);

	while (ptr) {
		// exact name
		if (strcmp(fname, ptr->fname) == 0) {
			found = 1;
			break;
		}

		// parent directory in the list
		if (len > ptr->len &&
		    fname[ptr->len] == '/' &&
		    strncmp(ptr->fname, fname, ptr->len) == 0) {
		    	found = 1;
		    	break;
		}

		ptr = ptr->next;
	}

	if (found)
		return ptr;

	return NULL;
}

FileDB *filedb_add(FileDB *head, const char *fname) {
	assert(fname);

	// todo: support fnames such as ${RUNUSER}/.mutter-Xwaylandauth.*

	// don't add it if it is already there or if the parent directory is already in the list
	if (filedb_find(head, fname))
		return head;

	// add a new entry
	FileDB *entry = malloc(sizeof(FileDB));
	if (!entry)
		errExit("malloc");
	memset(entry, 0, sizeof(FileDB));
	entry->fname = strdup(fname);
	if (!entry->fname)
		errExit("strdup");
	entry->len = strlen(entry->fname);
	entry->next = head;
	return entry;
};

void filedb_print(FileDB *head, const char *prefix, FILE *fp) {
	assert(head);
	assert(prefix);

	FileDB *ptr = head;
	while (ptr) {
		if (fp)
			fprintf(fp, "%s%s\n", prefix, ptr->fname);
		else
			printf("%s%s\n", prefix, ptr->fname);
		ptr = ptr->next;
	}
}

FileDB *filedb_load_whitelist(FileDB *head, const char *fname, const char *prefix) {
	assert(fname);
	assert(prefix);
	int len = strlen(prefix);
	char *f;
	if (asprintf(&f, "%s/%s", SYSCONFDIR, fname) == -1)
		errExit("asprintf");
	FILE *fp = fopen(f, "r");
	if (!fp) {
		fprintf(stderr, "Error: cannot open whitelist-common.inc\n");
		free(f);
		exit(1);
	}

	char buf[MAX_BUF];
	while (fgets(buf, MAX_BUF, fp)) {
		if (strncmp(buf, prefix, len) != 0)
			continue;

		char *fn = buf + len;
		char *ptr = strchr(buf, '\n');
		if (!ptr)
			continue;
		*ptr = '\0';

		// add the file to skip list
		head = filedb_add(head, fn);
	}

	fclose(fp);
	free(f);
//printf("***************************************************\n");
//filedb_print(head, prefix, NULL);
//printf("***************************************************\n");
	return head;
}