diff options
Diffstat (limited to 'src/fids/db.c')
-rw-r--r-- | src/fids/db.c | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/src/fids/db.c b/src/fids/db.c new file mode 100644 index 000000000..35caf7eeb --- /dev/null +++ b/src/fids/db.c | |||
@@ -0,0 +1,158 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2014-2021 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 | #include"fids.h" | ||
21 | |||
22 | typedef struct db_t { | ||
23 | struct db_t *next; | ||
24 | char *fname; | ||
25 | char *checksum; | ||
26 | char *mode; | ||
27 | int checked; | ||
28 | } DB; | ||
29 | |||
30 | #define MAXBUF 4096 | ||
31 | static DB *database[HASH_MAX] = {NULL}; | ||
32 | |||
33 | // djb2 hash function by Dan Bernstein | ||
34 | static unsigned hash(const char *str) { | ||
35 | unsigned long hash = 5381; | ||
36 | int c; | ||
37 | |||
38 | while ((c = *str++) != '\0') | ||
39 | hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ | ||
40 | |||
41 | return hash & (HASH_MAX - 1); | ||
42 | } | ||
43 | |||
44 | #if 0 | ||
45 | // for testing the hash table | ||
46 | static void db_print(void) { | ||
47 | int i; | ||
48 | for (i = 0; i < HASH_MAX; i++) { | ||
49 | int cnt = 0; | ||
50 | DB *ptr = database[i]; | ||
51 | while (ptr) { | ||
52 | cnt++; | ||
53 | ptr = ptr->next; | ||
54 | } | ||
55 | printf("%d ", cnt); | ||
56 | fflush(0); | ||
57 | } | ||
58 | printf("\n"); | ||
59 | } | ||
60 | #endif | ||
61 | |||
62 | static void db_add(const char *fname, const char *checksum, const char *mode) { | ||
63 | DB *ptr = malloc(sizeof(DB)); | ||
64 | if (!ptr) | ||
65 | errExit("malloc"); | ||
66 | ptr->fname = strdup(fname); | ||
67 | ptr->checksum = strdup(checksum); | ||
68 | ptr->mode = strdup(mode); | ||
69 | ptr->checked = 0; | ||
70 | if (!ptr->fname || !ptr->checksum || !ptr->mode) | ||
71 | errExit("strdup"); | ||
72 | |||
73 | unsigned h = hash(fname); | ||
74 | ptr->next = database[h]; | ||
75 | database[h] = ptr; | ||
76 | } | ||
77 | |||
78 | void db_check(const char *fname, const char *checksum, const char *mode) { | ||
79 | assert(fname); | ||
80 | assert(checksum); | ||
81 | assert(mode); | ||
82 | |||
83 | unsigned h =hash(fname); | ||
84 | DB *ptr = database[h]; | ||
85 | while (ptr) { | ||
86 | if (strcmp(fname, ptr->fname) == 0) { | ||
87 | ptr->checked = 1; | ||
88 | break; | ||
89 | } | ||
90 | ptr = ptr->next; | ||
91 | } | ||
92 | |||
93 | if (ptr ) { | ||
94 | if (strcmp(checksum, ptr->checksum)) { | ||
95 | f_modified++; | ||
96 | fprintf(stderr, "\nWarning: modified %s\n", fname); | ||
97 | } | ||
98 | if (strcmp(mode, ptr->mode)) { | ||
99 | f_permissions++; | ||
100 | fprintf(stderr, "\nWarning: permissions %s: old %s, new %s\n", | ||
101 | fname, ptr->mode, mode); | ||
102 | } | ||
103 | } | ||
104 | else { | ||
105 | f_new++; | ||
106 | fprintf(stderr, "\nWarning: new file %s\n", fname); | ||
107 | } | ||
108 | } | ||
109 | |||
110 | void db_missing(void) { | ||
111 | int i; | ||
112 | for (i = 0; i < HASH_MAX; i++) { | ||
113 | DB *ptr = database[i]; | ||
114 | while (ptr) { | ||
115 | if (!ptr->checked) { | ||
116 | f_removed++; | ||
117 | fprintf(stderr, "Warning: removed %s\n", ptr->fname); | ||
118 | } | ||
119 | ptr = ptr->next; | ||
120 | } | ||
121 | } | ||
122 | } | ||
123 | |||
124 | // return 0 if ok, 1 if error | ||
125 | int db_init(void) { | ||
126 | char buf[MAXBUF]; | ||
127 | while(fgets(buf, MAXBUF, stdin)) { | ||
128 | // split - tab separated | ||
129 | |||
130 | char *mode = buf; | ||
131 | char *ptr = strchr(buf, '\t'); | ||
132 | if (!ptr) | ||
133 | goto errexit; | ||
134 | *ptr = '\0'; | ||
135 | |||
136 | char *checksum = ptr + 1; | ||
137 | ptr = strchr(checksum, '\t'); | ||
138 | if (!ptr) | ||
139 | goto errexit; | ||
140 | *ptr = '\0'; | ||
141 | |||
142 | char *fname = ptr + 1; | ||
143 | ptr = strchr(fname, '\n'); | ||
144 | if (!ptr) | ||
145 | goto errexit; | ||
146 | *ptr = '\0'; | ||
147 | |||
148 | db_add(fname, checksum, mode); | ||
149 | } | ||
150 | // db_print(); | ||
151 | |||
152 | return 0; | ||
153 | |||
154 | errexit: | ||
155 | fprintf(stderr, "Error fids: database corrupted\n"); | ||
156 | exit(1); | ||
157 | } | ||
158 | |||