summaryrefslogtreecommitdiffstats
path: root/src/devicedb.c
blob: 9ca0cc8a530ce75225b7076604da2c666eb2346d (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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
/*
 * devicedb.c
 * Device database: built-in table merged with optional device files
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-2.1.html
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * Device files start with an optional "#!format N" line (N = DEVICES_FORMAT_VERSION);
 * a file without it is format 1. Files declaring a newer format are ignored.
 *
 * The table is assembled from, in order (later entries override earlier
 * ones with the same hardware_model):
 *   1. the built-in table generated from data/devices.txt
 *   2. <pkgdatadir>/devices.txt                  (not on Windows)
 *   3. ~/.libirecovery/devices.txt               (%APPDATA%\libirecovery\devices.txt on Windows)
 *      or the file named by $LIBIRECOVERY_DEVICE_DB
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef _WIN32
#include <windows.h>
#include <direct.h>
#define strcasecmp _stricmp
#define PATH_SEP "\\"
#else
#include <unistd.h>
#include <strings.h>
#include <pwd.h>
#define PATH_SEP "/"
#endif

#include <libimobiledevice-glue/thread.h>

#ifdef IRECV_STATIC
  #define IRECV_API
#elif defined(_WIN32)
  #define IRECV_API __declspec( dllexport )
#else
  #if __GNUC__ >= 4
    #define IRECV_API __attribute__((visibility("default")))
  #else
    #define IRECV_API
  #endif
#endif

#include "libirecovery.h"
#include "devicedb.h"

#define DEVICES_FILE_NAME "devices.txt"
#define USER_DIR_NAME     "libirecovery"
#define ENV_DEVICE_DB     "LIBIRECOVERY_DEVICE_DB"
#define DEVICES_FORMAT_VERSION 1
#define MAX_LINE_LENGTH   512
#define MAX_ENTRIES       16384

#define debug(...) if (db_debug) fprintf(stderr, __VA_ARGS__)

struct db_generation {
	struct irecv_device* table;
	void** blocks;
	size_t block_count;
	struct db_generation* prev;
};

struct db_builder {
	struct irecv_device* table;
	size_t count;
	size_t capacity;
	void** blocks;
	size_t block_count;
	size_t block_capacity;
};

static mutex_t db_mutex;
static int db_debug = 0;
static int db_loaded = 0;
static struct db_generation* db_current = NULL;

static char* join_path(const char* dir, const char* name)
{
	size_t len = strlen(dir) + strlen(PATH_SEP) + strlen(name) + 1;
	char* path = malloc(len);
	if (path) {
		snprintf(path, len, "%s%s%s", dir, PATH_SEP, name);
	}
	return path;
}

static char* get_user_dir(void)
{
#ifdef _WIN32
	const char* base = getenv("APPDATA");
	if (!base || !*base) {
		return NULL;
	}
	return join_path(base, USER_DIR_NAME);
#else
	const char* home = getenv("HOME");
	if (!home || !*home) {
		struct passwd* pw = getpwuid(getuid());
		home = pw ? pw->pw_dir : NULL;
	}
	if (!home || !*home) {
		return NULL;
	}
	return join_path(home, "." USER_DIR_NAME);
#endif
}

static char* get_user_file(int* is_default)
{
	const char* env = getenv(ENV_DEVICE_DB);
	char* dir;
	char* path;

	if (env && *env) {
		*is_default = 0;
		return strdup(env);
	}
	*is_default = 1;
	dir = get_user_dir();
	if (!dir) {
		return NULL;
	}
	path = join_path(dir, DEVICES_FILE_NAME);
	free(dir);
	return path;
}

/* A user-controlled file must belong to us (or root) and not be world-writable. */
static int file_is_trusted(FILE* f)
{
#ifdef _WIN32
	(void)f;
	return 1;
#else
	struct stat st;
	if (fstat(fileno(f), &st) != 0 || !S_ISREG(st.st_mode)) {
		return 0;
	}
	if (st.st_uid != geteuid() && st.st_uid != 0) {
		return 0;
	}
	return (st.st_mode & S_IWOTH) == 0;
#endif
}

static int has_control_chars(const char* s)
{
	for (; *s; s++) {
		if ((unsigned char)*s < 0x20 || *s == 0x7f) {
			return 1;
		}
	}
	return 0;
}

static int parse_u32(const char* s, unsigned int* out)
{
	char* end = NULL;
	unsigned long v;

	if (!isdigit((unsigned char)s[0])) {
		return -1;
	}
	errno = 0;
	v = strtoul(s, &end, 0);
	if (errno != 0 || *end != '\0' || v > 0xFFFFFFFFUL) {
		return -1;
	}
	*out = (unsigned int)v;
	return 0;
}

/* Parses one line in place. Returns 1 for an entry, 0 for a blank or comment line, -1 if malformed.
 * The strings in 'dev' point into 'line'. */
static int parse_line(char* line, struct irecv_device* dev)
{
	char* tok[4];
	char* p = line;
	size_t len;
	int i;

	while (isspace((unsigned char)*p)) p++;
	if (*p == '\0' || *p == '#') {
		return 0;
	}
	for (i = 0; i < 4; i++) {
		tok[i] = p;
		while (*p && !isspace((unsigned char)*p)) p++;
		if (*p == '\0') {
			return -1;
		}
		*p++ = '\0';
		while (isspace((unsigned char)*p)) p++;
	}
	len = strlen(p);
	while (len > 0 && isspace((unsigned char)p[len-1])) {
		p[--len] = '\0';
	}
	if (len == 0 || has_control_chars(p)) {
		return -1;
	}
	if (parse_u32(tok[2], &dev->board_id) != 0 || parse_u32(tok[3], &dev->chip_id) != 0) {
		return -1;
	}
	dev->product_type = tok[0];
	dev->hardware_model = tok[1];
	dev->display_name = p;
	return 1;
}

static int builder_add_block(struct db_builder* b, void* block)
{
	if (b->block_count == b->block_capacity) {
		size_t ncap = b->block_capacity ? b->block_capacity * 2 : 16;
		void** nb = realloc(b->blocks, ncap * sizeof(void*));
		if (!nb) {
			return -1;
		}
		b->blocks = nb;
		b->block_capacity = ncap;
	}
	b->blocks[b->block_count++] = block;
	return 0;
}

/* Adds or replaces an entry (matched by hardware_model); copies the strings. */
static int builder_apply(struct db_builder* b, const struct irecv_device* dev)
{
	size_t l1 = strlen(dev->product_type) + 1;
	size_t l2 = strlen(dev->hardware_model) + 1;
	size_t l3 = strlen(dev->display_name) + 1;
	struct irecv_device* slot = NULL;
	char* block;
	size_t i;

	for (i = 0; i < b->count; i++) {
		if (!strcasecmp(b->table[i].hardware_model, dev->hardware_model)) {
			slot = &b->table[i];
			break;
		}
	}
	if (!slot && b->count >= MAX_ENTRIES) {
		return -1;
	}
	if (!slot && b->count + 2 > b->capacity) {
		size_t ncap = b->capacity * 2;
		struct irecv_device* nt = realloc(b->table, ncap * sizeof(struct irecv_device));
		if (!nt) {
			return -1;
		}
		b->table = nt;
		b->capacity = ncap;
	}
	block = malloc(l1 + l2 + l3);
	if (!block) {
		return -1;
	}
	if (builder_add_block(b, block) != 0) {
		free(block);
		return -1;
	}
	memcpy(block, dev->product_type, l1);
	memcpy(block + l1, dev->hardware_model, l2);
	memcpy(block + l1 + l2, dev->display_name, l3);

	if (!slot) {
		slot = &b->table[b->count++];
	}
	slot->product_type = block;
	slot->hardware_model = block + l1;
	slot->display_name = block + l1 + l2;
	slot->board_id = dev->board_id;
	slot->chip_id = dev->chip_id;
	return 0;
}

/* Returns the declared format version (1 if none), or -1 if it is invalid.
 * Only the directive lines before the first entry count. Leaves the file position undefined. */
static int scan_format(FILE* f)
{
	char line[MAX_LINE_LENGTH];
	int version = 1;

	rewind(f);
	while (fgets(line, sizeof(line), f)) {
		char* p = line;
		char* end = NULL;
		long v;

		while (isspace((unsigned char)*p)) p++;
		if (*p == '\0') continue;
		if (*p != '#') break;
		if (strncmp(p, "#!format", 8) != 0 || !isspace((unsigned char)p[8])) continue;
		p += 8;
		while (isspace((unsigned char)*p)) p++;
		errno = 0;
		v = strtol(p, &end, 10);
		if (end == p || errno != 0 || v < 1 || v > 0x7fff) {
			return -1;
		}
		while (isspace((unsigned char)*end)) end++;
		if (*end != '\0') {
			return -1;
		}
		version = (int)v;
		break;
	}
	return version;
}

static int format_supported(FILE* f, const char* path)
{
	int version = scan_format(f);
	if (version < 1 || version > DEVICES_FORMAT_VERSION) {
		debug("%s: %s: unsupported or invalid device file format, ignored\n", __func__, path);
		return 0;
	}
	rewind(f);
	return 1;
}

/* Returns the number of entries applied, or -1 if the file could not be used. */
static int load_file(struct db_builder* b, const char* path, int check_trust)
{
	char line[MAX_LINE_LENGTH];
	int lineno = 0;
	int applied = 0;
	FILE* f = fopen(path, "r");

	if (!f) {
		return -1;
	}
	if (check_trust && !file_is_trusted(f)) {
		debug("%s: ignoring device file with unsafe ownership or permissions: %s\n", __func__, path);
		fclose(f);
		return -1;
	}
	if (!format_supported(f, path)) {
		fclose(f);
		return -1;
	}
	while (fgets(line, sizeof(line), f)) {
		struct irecv_device dev;
		size_t len = strlen(line);
		int r;

		lineno++;
		if (len == sizeof(line) - 1 && line[len-1] != '\n') {
			int c;
			while ((c = fgetc(f)) != EOF && c != '\n') {}
			debug("%s: %s:%d: line too long, skipped\n", __func__, path, lineno);
			continue;
		}
		r = parse_line(line, &dev);
		if (r < 0) {
			debug("%s: %s:%d: malformed entry, skipped\n", __func__, path, lineno);
		} else if (r > 0) {
			if (builder_apply(b, &dev) != 0) {
				debug("%s: %s:%d: unable to add entry\n", __func__, path, lineno);
				break;
			}
			applied++;
		}
	}
	fclose(f);
	return applied;
}

/* Returns NULL if no device file contributed anything, i.e. the built-in table is to be used as is. */
static struct db_generation* build_generation(void)
{
	struct db_builder b;
	struct db_generation* gen = NULL;
	size_t builtin_count = 0;
	int total = 0;
	int is_default = 0;
	char* user_file;
	int r;

	while (irecv_devices_builtin[builtin_count].product_type != NULL) {
		builtin_count++;
	}
	memset(&b, 0, sizeof(b));
	b.capacity = builtin_count + 32;
	b.table = malloc(b.capacity * sizeof(struct irecv_device));
	if (!b.table) {
		return NULL;
	}
	memcpy(b.table, irecv_devices_builtin, builtin_count * sizeof(struct irecv_device));
	b.count = builtin_count;

#if defined(IRECV_DATADIR) && !defined(_WIN32)
	{
		char* sys_file = join_path(IRECV_DATADIR, DEVICES_FILE_NAME);
		if (sys_file) {
			r = load_file(&b, sys_file, 0);
			if (r > 0) total += r;
			free(sys_file);
		}
	}
#endif
	user_file = get_user_file(&is_default);
	if (user_file) {
		r = load_file(&b, user_file, 1);
		if (r > 0) total += r;
		free(user_file);
	}

	if (total > 0) {
		gen = calloc(1, sizeof(*gen));
	}
	if (!gen) {
		size_t i;
		for (i = 0; i < b.block_count; i++) free(b.blocks[i]);
		free(b.blocks);
		free(b.table);
		return NULL;
	}
	memset(&b.table[b.count], 0, sizeof(struct irecv_device));
	b.table[b.count].board_id = (unsigned int)-1;
	b.table[b.count].chip_id = (unsigned int)-1;
	gen->table = b.table;
	gen->blocks = b.blocks;
	gen->block_count = b.block_count;
	return gen;
}

static void free_generation(struct db_generation* gen)
{
	size_t i;
	for (i = 0; i < gen->block_count; i++) free(gen->blocks[i]);
	free(gen->blocks);
	free(gen->table);
	free(gen);
}

void irecv_devicedb_init(void)
{
	mutex_init(&db_mutex);
}

void irecv_devicedb_cleanup(void)
{
	mutex_lock(&db_mutex);
	while (db_current) {
		struct db_generation* prev = db_current->prev;
		free_generation(db_current);
		db_current = prev;
	}
	db_loaded = 0;
	mutex_unlock(&db_mutex);
	mutex_destroy(&db_mutex);
}

void irecv_devicedb_set_debug(int level)
{
	db_debug = level;
}

struct irecv_device* irecv_devicedb_get(void)
{
	struct irecv_device* table;

	mutex_lock(&db_mutex);
	if (!db_loaded) {
		db_current = build_generation();
		db_loaded = 1;
	}
	table = db_current ? db_current->table : irecv_devices_builtin;
	mutex_unlock(&db_mutex);
	return table;
}

/* Re-reads all device files. Tables handed out earlier remain valid. */
static void reload(void)
{
	struct db_generation* gen;

	mutex_lock(&db_mutex);
	gen = build_generation();
	if (gen) {
		gen->prev = db_current;
		db_current = gen;
	}
	db_loaded = 1;
	mutex_unlock(&db_mutex);
}

static int token_valid(const char* s)
{
	if (!s || !*s) {
		return 0;
	}
	for (; *s; s++) {
		if (isspace((unsigned char)*s) || (unsigned char)*s < 0x20 || *s == 0x7f) {
			return 0;
		}
	}
	return 1;
}

static int name_valid(const char* s)
{
	size_t len;
	if (!s || !*s || has_control_chars(s)) {
		return 0;
	}
	len = strlen(s);
	return !isspace((unsigned char)s[0]) && !isspace((unsigned char)s[len-1]);
}

static irecv_error_t add_entry(const char* product_type, const char* hardware_model, unsigned int board_id, unsigned int chip_id, const char* display_name)
{
	struct irecv_device* table;
	char* path;
	int is_default = 0;
	FILE* f;
	long size;
	int ok;
	size_t i;

	/* the whole line, including separators and the IDs, must fit what the loader accepts */
	if (!name_valid(display_name) ||
	    strlen(product_type) + strlen(hardware_model) + strlen(display_name) + 32 > MAX_LINE_LENGTH) {
		return IRECV_E_INVALID_INPUT;
	}

	table = irecv_devicedb_get();
	for (i = 0; table[i].product_type != NULL; i++) {
		if (!strcasecmp(table[i].hardware_model, hardware_model)) {
			if (!strcmp(table[i].product_type, product_type) &&
			    table[i].board_id == board_id && table[i].chip_id == chip_id &&
			    !strcmp(table[i].display_name, display_name)) {
				return IRECV_E_SUCCESS;
			}
			break;
		}
	}

	path = get_user_file(&is_default);
	if (!path) {
		return IRECV_E_UNKNOWN_ERROR;
	}
	if (is_default) {
		char* dir = get_user_dir();
		if (dir) {
#ifdef _WIN32
			_mkdir(dir);
#else
			mkdir(dir, 0700);
#endif
			free(dir);
		}
	}

	f = fopen(path, "a+");
	free(path);
	if (!f) {
		return IRECV_E_UNKNOWN_ERROR;
	}
	if (!file_is_trusted(f)) {
		fclose(f);
		return IRECV_E_UNKNOWN_ERROR;
	}

	if (!format_supported(f, "user device file")) {
		fclose(f);
		return IRECV_E_UNSUPPORTED;
	}

	/* a missing final newline must not glue our line onto the previous entry */
	ok = (fseek(f, 0, SEEK_END) == 0);
	size = ok ? ftell(f) : -1;
	if (size == 0) {
		ok = fputs("#!format 1\n"
		           "# libirecovery user device database\n"
		           "# <product_type> <hardware_model> <board_id> <chip_id> <display name>\n"
		           "# Entries override built-in ones with the same hardware_model.\n", f) >= 0;
	} else if (size > 0) {
		if (fseek(f, -1, SEEK_END) != 0) {
			ok = 0;
		} else if (fgetc(f) != '\n') {
			ok = (fseek(f, 0, SEEK_END) == 0) && fputc('\n', f) != EOF;
		}
	} else {
		ok = 0;
	}
	if (ok) {
		ok = fprintf(f, "%s %s 0x%02x 0x%04x %s\n", product_type, hardware_model, board_id, chip_id, display_name) > 0;
	}
	if (fclose(f) != 0) {
		ok = 0;
	}
	if (!ok) {
		return IRECV_E_UNKNOWN_ERROR;
	}

	reload();
	return IRECV_E_SUCCESS;
}

IRECV_API irecv_error_t irecv_devices_add(const char* product_type, const char* hardware_model, unsigned int board_id, unsigned int chip_id, const char* display_name)
{
	struct irecv_device* table;
	const struct irecv_device* existing = NULL;
	const char* name = display_name;
	char* generated = NULL;
	irecv_error_t err;
	size_t i;

	if (!token_valid(product_type) || !token_valid(hardware_model)) {
		return IRECV_E_INVALID_INPUT;
	}

	table = irecv_devicedb_get();
	for (i = 0; table[i].product_type != NULL; i++) {
		if (!strcasecmp(table[i].hardware_model, hardware_model)) {
			existing = &table[i];
		} else if (table[i].chip_id == chip_id && table[i].board_id == board_id) {
			/* the existing entry would shadow the new one in lookups by chip/board ID */
			return IRECV_E_INVALID_INPUT;
		}
	}

	if (!name || !*name) {
		if (existing) {
			name = existing->display_name;
		} else {
			size_t len = strlen(product_type) + strlen(hardware_model) + 4;
			generated = malloc(len);
			if (!generated) {
				return IRECV_E_OUT_OF_MEMORY;
			}
			snprintf(generated, len, "%s (%s)", product_type, hardware_model);
			name = generated;
		}
	}

	err = add_entry(product_type, hardware_model, board_id, chip_id, name);
	free(generated);
	return err;
}