aboutsummaryrefslogtreecommitdiffstats
path: root/wgtpkg-zip.c
blob: 6ca321f145f902292c17d5e0c196f1001d8f3558 (plain)
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
 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 _BSD_SOURCE /* see readdir */

#include <limits.h>
#include <zip.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <assert.h>
#include <dirent.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>

#include "wgtpkg.h"


#if !defined(MODE_OF_FILE_CREATION)
#define MODE_OF_FILE_CREATION 0640
#endif
#if !defined(MODE_OF_DIRECTORY_CREATION)
#define MODE_OF_DIRECTORY_CREATION 0750
#endif

static int is_valid_filename(const char *filename)
{
	int lastsp = 0;
	int index = 0;
	unsigned char c;

	c = (unsigned char)filename[index];
	while (c) {
		if ((c < 0x1f)
		 || ((lastsp = (c == 0x20)) && index == 0)
		 || c == 0x7f || c == 0x3c || c == 0x3e
		 || c == 0x3a || c == 0x22 || c == 0x2f
		 || c == 0x5c || c == 0x7c || c == 0x3f
		 || c == 0x2a || c == 0x5e || c == 0x60
		 || c == 0x7b || c == 0x7d || c == 0x21)
			return 0;
		c = (unsigned char)filename[++index];
	}
	return !lastsp;
}

static int create_directory(char *file, int mode)
{
	int rc;
	char *last = strrchr(file, '/');
	if (last != NULL)
		*last = 0;
	rc = mkdir(file, mode);
	if (rc) {
		if (errno == EEXIST)
			rc = 0;
		else if (errno == ENOENT) {
			rc = create_directory(file, mode);
			if (!rc)
				rc = mkdir(file, mode);
		}
	}
	if (rc)
		syslog(LOG_ERR, "can't create directory %s", file);
	if (last != NULL)
		*last = '/';
	return rc;
}

static int create_file(char *file, int fmode, int dmode)
{
	int fd = creat(file, fmode);
	if (fd < 0 && errno == ENOENT) {
		if (!create_directory(file, dmode))
			fd = creat(file, fmode);
	}
	if (fd < 0)
		syslog(LOG_ERR, "can't create file %s", file);
	return fd;
}

/* read (extract) 'zipfile' in current directory */
int zread(const char *zipfile, unsigned long long maxsize)
{
	struct filedesc *fdesc;
	int err, fd, len;
	struct zip *zip;
	zip_int64_t z64;
	unsigned int count, index;
	struct zip_file *zfile;
	struct zip_stat zstat;
	char buffer[32768];
	ssize_t sizr, sizw;
	size_t esize;

	/* open the zip file */
	zip = zip_open(zipfile, ZIP_CHECKCONS, &err);
	if (!zip) {
		syslog(LOG_ERR, "Can't connect to file %s", zipfile);
		return -1;
	}

	z64 = zip_get_num_entries(zip, 0);
	if (z64 < 0 || z64 > UINT_MAX) {
		syslog(LOG_ERR, "too many entries in %s", zipfile);
		goto error;
	}
	count = (unsigned int)z64;

	/* records the files */
	file_reset();
	esize = 0;
	for (index = 0 ; index < count ; index++) {
		err = zip_stat_index(zip, index, ZIP_FL_ENC_GUESS, &zstat);
		/* check the file name */
		if (!is_valid_filename(zstat.name)) {
			syslog(LOG_ERR, "invalid entry %s found in %s", zstat.name, zipfile);
			goto error;
		}
		if (zstat.name[0] == '/') {
			syslog(LOG_ERR, "absolute entry %s found in %s", zstat.name, zipfile);
			goto error;
		}
		len = strlen(zstat.name);
		if (len == 0) {
			syslog(LOG_ERR, "empty entry found in %s", zipfile);
			goto error;
		}
		if (zstat.size == 0) {
			/* directory name */
			if (zstat.name[len - 1] != '/') {
				syslog(LOG_ERR, "bad directory name %s in %s", zstat.name, zipfile);
				goto error;
			}
			/* record */
			fdesc = file_add_directory(zstat.name);
		} else {
			/* directory name */
			if (zstat.name[len - 1] == '/') {
				syslog(LOG_ERR, "bad file name %s in %s", zstat.name, zipfile);
				goto error;
			}
			/* get the size */
			esize += zstat.size;
			/* record */
			fdesc = file_add_file(zstat.name);
		}
		if (!fdesc)
			goto error;
		fdesc->zindex = index;
	}

	/* check the size */
	if (maxsize && esize > maxsize) {
		syslog(LOG_ERR, "extracted size %zu greater than allowed size %llu", esize, maxsize);
		goto error;
	}

	/* unpack the recorded files */
	assert(count == file_count());
	for (index = 0 ; index < count ; index++) {
		fdesc = file_of_index(index);
		assert(fdesc != NULL);
		err = zip_stat_index(zip, fdesc->zindex, ZIP_FL_ENC_GUESS, &zstat);
		assert(zstat.name[0] != '/');
		if (zstat.size == 0) {
			/* directory name */
			err = create_directory((char*)zstat.name, MODE_OF_DIRECTORY_CREATION);
			if (err && errno != EEXIST)
				goto error;
		} else {
			/* file name */
			zfile = zip_fopen_index(zip, fdesc->zindex, 0);
			if (!zfile) {
				syslog(LOG_ERR, "Can't open %s in %s", zstat.name, zipfile);
				goto error;
			}
			fd = create_file((char*)zstat.name, MODE_OF_FILE_CREATION, MODE_OF_DIRECTORY_CREATION);
			if (fd < 0)
				goto errorz;
			/* extract */
			z64 = zstat.size;
			while (z64) {
				sizr = zip_fread(zfile, buffer, sizeof buffer);
				if (sizr < 0) {
					syslog(LOG_ERR, "error while reading %s in %s", zstat.name, zipfile);
					goto errorzf;
				}
				sizw = write(fd, buffer, sizr);
				if (sizw < 0) {
					syslog(LOG_ERR, "error while writing %s", zstat.name);
					goto errorzf;
				}
				z64 -= sizw;
			}
			close(fd);
			zip_fclose(zfile);
		}
	}

	zip_close(zip);
	return 0;

errorzf:
	close(fd);
errorz:
	zip_fclose(zfile);
error:
	zip_close(zip);
	return -1;
}

struct zws {
	struct zip *zip;
	char name[PATH_MAX];
	char buffer[32768];
};

static int zwr(struct zws *zws, int offset)
{
	int len, err;
	DIR *dir;
	struct dirent *ent;
	zip_int64_t z64;
	struct zip_source *zsrc;

	if (offset == 0)
		dir = opendir(".");
	else {
		dir = opendir(zws->name);
		zws->name[offset++] = '/';
	}
	if (!dir) {
		syslog(LOG_ERR, "opendir %.*s failed in zwr", offset, zws->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 >= sizeof(zws->name)) {
			syslog(LOG_ERR, "name too long in zwr");
			errno = ENAMETOOLONG;
			goto error;
		} else {
			memcpy(zws->name + offset, ent->d_name, 1+len);
			if (!is_valid_filename(ent->d_name)) {
				syslog(LOG_ERR, "invalid name %s", zws->name);
				goto error;
			}
			switch (ent->d_type) {
			case DT_DIR:
				z64 = zip_dir_add(zws->zip, zws->name, ZIP_FL_ENC_UTF_8);
				if (z64 < 0) {
					syslog(LOG_ERR, "zip_dir_add of %s failed", zws->name);
					goto error;
				}
				err = zwr(zws, offset + len);
				if (err)
					goto error;
				break;
			case DT_REG:
				zsrc = zip_source_file(zws->zip, zws->name, 0, 0);
				if (zsrc == NULL) {
					syslog(LOG_ERR, "zip_source_file of %s failed", zws->name);
					goto error;
				}
				z64 = zip_file_add(zws->zip, zws->name, zsrc, ZIP_FL_ENC_UTF_8);
				if (z64 < 0) {
					syslog(LOG_ERR, "zip_file_add of %s failed", zws->name);
					zip_source_free(zsrc);
					goto error;
				}
				break;
			default:
				break;
			}
		}
		ent = readdir(dir);
	}

	closedir(dir);
	return 0;
error:
	closedir(dir);
	return -1;
}

/* write (pack) content of the current directory in 'zipfile' */
int zwrite(const char *zipfile)
{
	int err;
	struct zws zws;

	zws.zip = zip_open(zipfile, ZIP_CREATE|ZIP_TRUNCATE, &err);
	if (!zws.zip) {
		syslog(LOG_ERR, "Can't open %s for write", zipfile);
		return -1;
	}

	err = zwr(&zws, 0);
	zip_close(zws.zip);
	return err;
}


#if defined(TEST_READ)
int main(int ac, char **av)
{
	for(av++ ; *av ; av++)
		zread(*av);
	return 0;
}
#endif

#if defined(TEST_WRITE)
int main(int ac, char **av)
{
	for(av++ ; *av ; av++)
		zwrite(*av);
	return 0;
}
#endif