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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
/*
Copyright 2015 IoT.bzh
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <syslog.h>
#include <dirent.h>
#include <stdio.h>
#include "wgtpkg.h"
struct fdb {
unsigned int count;
struct filedesc **files;
};
static struct fdb allfiles = { .count = 0, .files = NULL };
static struct fdb allsignatures = { .count = 0, .files = NULL };
static const char author_file[] = "author-signature.xml";
static const char distributor_file_prefix[] = "signature";
static const char distributor_file_suffix[] = ".xml";
static unsigned int what_signature(const char *name)
{
unsigned int len, id, nid;
if (!strcmp(name, author_file))
return UINT_MAX;
len = sizeof(distributor_file_prefix)-1;
if (memcmp(name, distributor_file_prefix, len))
return 0;
if (name[len] <= '0' || name[len] > '9')
return 0;
id = (unsigned int)(name[len++] - '0');
while ('0' <= name[len] && name[len] <= '9') {
nid = 10 * id + (unsigned int)(name[len++] - '0');
if (nid < id || nid == UINT_MAX) {
syslog(LOG_WARNING, "number too big for %s", name);
return 0;
}
id = nid;
}
if (strcmp(name+len, distributor_file_suffix))
return 0;
return id;
}
static struct filedesc *get_filedesc(const char *name, int create)
{
int cmp;
unsigned int low, up, mid, sig;
struct filedesc *result, **grow;
/* search */
low = 0;
up = allfiles.count;
while(low < up) {
mid = (low + up) >> 1;
result = allfiles.files[mid];
cmp = strcmp(result->name, name);
if (!cmp)
return result; /* found */
if (cmp > 0)
up = mid;
else
low = mid + 1;
}
/* not found, can create ? */
if (!create)
return NULL;
sig = what_signature(name);
/* allocations */
grow = realloc(allfiles.files, (allfiles.count + 1) * sizeof(struct filedesc *));
if (grow == NULL) {
syslog(LOG_ERR, "realloc failed in get_filedesc");
return NULL;
}
allfiles.files = grow;
if (sig) {
grow = realloc(allsignatures.files, (allsignatures.count + 1) * sizeof(struct filedesc *));
if (grow == NULL) {
syslog(LOG_ERR, "second realloc failed in get_filedesc");
return NULL;
}
allsignatures.files = grow;
}
result = malloc(sizeof(struct filedesc) + strlen(name));
if (!result) {
syslog(LOG_ERR, "calloc failed in get_filedesc");
return NULL;
}
/* initialisation */
result->type = type_unset;
result->flags = sig == 0 ? 0 : sig == UINT_MAX ? flag_author_signature : flag_distributor_signature;
result->zindex = 0;
result->signum = sig;
strcpy(result->name, name);
/* insertion */
if (low < allfiles.count)
memmove(allfiles.files+low+1, allfiles.files+low, (allfiles.count - low) * sizeof(struct filedesc *));
allfiles.files[low] = result;
allfiles.count++;
if (sig) {
for (low = 0 ; low < allsignatures.count && sig > allsignatures.files[low]->signum ; low++);
if (low < allsignatures.count)
memmove(allsignatures.files+low+1, allsignatures.files+low, (allsignatures.count - low) * sizeof(struct filedesc *));
allsignatures.files[low] = result;
allsignatures.count++;
}
return result;
}
static struct filedesc *file_add(const char *name, enum entrytype type)
{
struct filedesc *desc;
desc = get_filedesc(name, 1);
if (!desc)
errno = ENOMEM;
else if (desc->type == type_unset)
desc->type = type;
else {
syslog(LOG_ERR, "redeclaration of %s in file_add", name);
errno = EEXIST;
desc = NULL;
}
return desc;
}
void file_reset()
{
unsigned int i;
allsignatures.count = 0;
for (i = 0 ; i < allfiles.count ; i++)
free(allfiles.files[i]);
allfiles.count = 0;
}
unsigned int file_count()
{
return allfiles.count;
}
struct filedesc *file_of_index(unsigned int index)
{
assert(index < allfiles.count);
return allfiles.files[index];
}
struct filedesc *file_of_name(const char *name)
{
return get_filedesc(name, 0);
}
struct filedesc *file_add_directory(const char *name)
{
return file_add(name, type_directory);
}
struct filedesc *file_add_file(const char *name)
{
return file_add(name, type_file);
}
unsigned int signature_count()
{
return allsignatures.count;
}
struct filedesc *signature_of_index(unsigned int index)
{
assert(index < allsignatures.count);
return allsignatures.files[index];
}
struct filedesc *get_signature(unsigned int number)
{
unsigned int idx;
if (number == 0)
number = UINT_MAX;
for (idx = 0 ; idx < allsignatures.count ; idx++)
if (allsignatures.files[idx]->signum == number)
return allsignatures.files[idx];
return NULL;
}
struct filedesc *create_signature(unsigned int number)
{
struct filedesc *result;
char *name;
int len;
result = NULL;
if (number == 0 || number == UINT_MAX)
len = asprintf(&name, "%s", author_file);
else
len = asprintf(&name, "%s%u%s", distributor_file_prefix, number, distributor_file_suffix);
if (len < 0)
syslog(LOG_ERR, "asprintf failed in create_signature");
else {
assert(len > 0);
result = file_of_name(name);
if (result == NULL)
result = file_add_file(name);
free(name);
}
return result;
}
void file_clear_flags()
{
unsigned int i;
for (i = 0 ; i < allfiles.count ; i++)
allfiles.files[i]->flags &= flag_signature;
}
static int fill_files_rec(char name[PATH_MAX], int offset)
{
int len, err;
DIR *dir;
struct dirent *ent;
if (offset == 0)
dir = opendir(".");
else {
dir = opendir(name);
name[offset++] = '/';
}
if (!dir) {
syslog(LOG_ERR, "opendir %.*s failed in zwr", offset, name);
return -1;
}
ent = readdir(dir);
while (ent != NULL) {
len = strlen(ent->d_name);
if (ent->d_name[0] == '.' && (len == 1 ||
(ent->d_name[1] == '.' && len == 2)))
;
else if (offset + len >= PATH_MAX) {
closedir(dir);
syslog(LOG_ERR, "name too long in fill_files_rec");
errno = ENAMETOOLONG;
return -1;
} else {
memcpy(name + offset, ent->d_name, 1+len);
switch (ent->d_type) {
case DT_DIR:
if (file_add_directory(name) == NULL) {
closedir(dir);
return -1;
}
err = fill_files_rec(name, offset + len);
if (err) {
closedir(dir);
return err;
}
break;
case DT_REG:
if (file_add_file(name) == NULL) {
closedir(dir);
return -1;
}
break;
default:
break;
}
}
ent = readdir(dir);
}
closedir(dir);
return 0;
}
int fill_files()
{
char name[PATH_MAX];
return fill_files_rec(name, 0);
}
|