summaryrefslogtreecommitdiffstats
path: root/src/conf.c
blob: 84f43265e2ea7a379f820ad95c4362c1d5f1c0a6 (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
/*
 * conf.c
 *
 * Copyright (C) 2013 Nikias Bassen <nikias@gmx.li>
 *
 * 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 or version 3.
 *
 * 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
 */

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

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif

#include <dirent.h>
#include <libgen.h>
#include <sys/stat.h>
#include <errno.h>

#ifdef WIN32
#include <shlobj.h>
#endif

#include <libimobiledevice-glue/utils.h>

#include "conf.h"
#include "utils.h"
#include "log.h"

#ifdef WIN32
#define DIR_SEP '\\'
#define DIR_SEP_S "\\"
#else
#define DIR_SEP '/'
#define DIR_SEP_S "/"
#endif

#define CONFIG_SYSTEM_BUID_KEY "SystemBUID"
#define CONFIG_HOST_ID_KEY "HostID"

#define CONFIG_EXT ".plist"

#ifdef WIN32
#define CONFIG_DIR "Apple"DIR_SEP_S"Lockdown"
#else
#define CONFIG_DIR "lockdown"
#endif

#define CONFIG_FILE "SystemConfiguration"CONFIG_EXT

static char *__config_dir = NULL;

#ifdef WIN32
static char *config_utf16_to_utf8(wchar_t *unistr, long len, long *items_read, long *items_written)
{
	if (!unistr || (len <= 0)) return NULL;
	char *outbuf = (char*)malloc(3*(len+1));
	int p = 0;
	int i = 0;

	wchar_t wc;

	while (i < len) {
		wc = unistr[i++];
		if (wc >= 0x800) {
			outbuf[p++] = (char)(0xE0 + ((wc >> 12) & 0xF));
			outbuf[p++] = (char)(0x80 + ((wc >> 6) & 0x3F));
			outbuf[p++] = (char)(0x80 + (wc & 0x3F));
		} else if (wc >= 0x80) {
			outbuf[p++] = (char)(0xC0 + ((wc >> 6) & 0x1F));
			outbuf[p++] = (char)(0x80 + (wc & 0x3F));
		} else {
			outbuf[p++] = (char)(wc & 0x7F);
		}
	}
	if (items_read) {
		*items_read = i;
	}
	if (items_written) {
		*items_written = p;
	}
	outbuf[p] = 0;

	return outbuf;
}
#endif

const char *config_get_config_dir()
{
	char *base_config_dir = NULL;

	if (__config_dir)
		return __config_dir;

#ifdef WIN32
	wchar_t path[MAX_PATH+1];
	HRESULT hr;
	LPITEMIDLIST pidl = NULL;
	BOOL b = FALSE;

	hr = SHGetSpecialFolderLocation (NULL, CSIDL_COMMON_APPDATA, &pidl);
	if (hr == S_OK) {
		b = SHGetPathFromIDListW (pidl, path);
		if (b) {
			base_config_dir = config_utf16_to_utf8 (path, wcslen(path), NULL, NULL);
			CoTaskMemFree (pidl);
		}
	}
#else
#ifdef __APPLE__
	base_config_dir = strdup("/var/db");
#else
	base_config_dir = strdup("/var/lib");
#endif
#endif
	__config_dir = string_concat(base_config_dir, DIR_SEP_S, CONFIG_DIR, NULL);

	if (__config_dir) {
		int i = strlen(__config_dir)-1;
		while ((i > 0) && (__config_dir[i] == DIR_SEP)) {
			__config_dir[i--] = '\0';
		}
	}

	free(base_config_dir);

	usbmuxd_log(LL_DEBUG, "Initialized config_dir to %s", __config_dir);

	return __config_dir;
}

static int __mkdir(const char *dir, int mode)
{
#ifdef WIN32
	return mkdir(dir);
#else
	return mkdir(dir, mode);
#endif
}

static int mkdir_with_parents(const char *dir, int mode)
{
	if (!dir) return -1;
	if (__mkdir(dir, mode) == 0) {
		return 0;
	} else {
		if (errno == EEXIST) return 0;
	}
	int res;
	char *parent = strdup(dir);
	char* parentdir = dirname(parent);
	if (parentdir) {
		res = mkdir_with_parents(parentdir, mode);
	} else {
		res = -1;
	}
	free(parent);
	return res;
}

/**
 * Creates a freedesktop compatible configuration directory.
 */
static void config_create_config_dir(void)
{
	const char *config_path = config_get_config_dir();
	struct stat st;
	if (stat(config_path, &st) != 0) {
		mkdir_with_parents(config_path, 0755);
	}
}

static int get_rand(int min, int max)
{
	int retval = (rand() % (max - min)) + min;
	return retval;
}

static char *config_generate_uuid(int idx)
{
	char *uuid = (char *) malloc(sizeof(char) * 37);
	const char *chars = "ABCDEF0123456789";
	srand(time(NULL) - idx);
	int i = 0;

	for (i = 0; i < 36; i++) {
		if (i == 8 || i == 13 || i == 18 || i == 23) {
			uuid[i] = '-';
			continue;
		} else {
			uuid[i] = chars[get_rand(0, 16)];
		}
	}
	/* make it a real string */
	uuid[36] = '\0';
	return uuid;
}

/**
 * Generates a valid BUID for this system (which is actually a UUID).
 *
 * @return A null terminated string containing a valid BUID.
 */
static char *config_generate_system_buid()
{
	return config_generate_uuid(1);
}

static int internal_set_value(const char *config_file, const char *key, plist_t value)
{
	if (!config_file)
		return 0;

	/* read file into plist */
	plist_t config = NULL;

	plist_read_from_filename(&config, config_file);
	if (!config) {
		config = plist_new_dict();
		plist_dict_set_item(config, key, value);
	} else {
		plist_t n = plist_dict_get_item(config, key);
		if (n) {
			plist_dict_remove_item(config, key);
		}
		plist_dict_set_item(config, key, value);
		remove(config_file);
	}

	/* store in config file */
	char *value_string = NULL;
	if (plist_get_node_type(value) == PLIST_STRING) {
		plist_get_string_val(value, &value_string);
		usbmuxd_log(LL_DEBUG, "Setting key %s to %s in config file %s", key, value_string, config_file);
		if (value_string)
			free(value_string);
	} else {
		usbmuxd_log(LL_DEBUG, "Setting key %s in config file %s", key, config_file);
	}

	int res = plist_write_to_filename(config, config_file, PLIST_FORMAT_XML);

	plist_free(config);

	return res;
}

static int config_set_value(const char *key, plist_t value)
{
	const char *config_path = NULL;
	char *config_file = NULL;

	/* Make sure config directory exists */
	config_create_config_dir();

	config_path = config_get_config_dir();
	config_file = string_concat(config_path, DIR_SEP_S, CONFIG_FILE, NULL);

	int result = internal_set_value(config_file, key, value);
	if (!result) {
		usbmuxd_log(LL_ERROR, "ERROR: Failed to write to '%s': %s", config_file, strerror(errno));
	}

	free(config_file);

	return result;
}

static int internal_get_value(const char* config_file, const char *key, plist_t *value)
{
	*value = NULL;

	/* now parse file to get the SystemBUID */
	plist_t config = NULL;
	if (plist_read_from_filename(&config, config_file)) {
		usbmuxd_log(LL_DEBUG, "Reading key %s from config file %s", key, config_file);
		plist_t n = plist_dict_get_item(config, key);
		if (n) {
			*value = plist_copy(n);
			n = NULL;
		}
	}
	plist_free(config);

	return 1;
}

static int config_get_value(const char *key, plist_t *value)
{
	const char *config_path = NULL;
	char *config_file = NULL;

	config_path = config_get_config_dir();
	config_file = string_concat(config_path, DIR_SEP_S, CONFIG_FILE, NULL);

	int result = internal_get_value(config_file, key, value);

	free(config_file);

	return result;
}

/**
 * Store SystemBUID in config file.
 *
 * @param system_buid A null terminated string containing a valid SystemBUID.
 */
static int config_set_system_buid(const char *system_buid)
{
	return config_set_value(CONFIG_SYSTEM_BUID_KEY, plist_new_string(system_buid));
}

/**
 * Determines whether a pairing record is present for the given device.
 *
 * @param udid The device UDID as given by the device.
 *
 * @return 1 if there's a pairing record for the given udid or 0 otherwise.
 */
int config_has_device_record(const char *udid)
{
	int res = 0;
	if (!udid) return 0;

	/* ensure config directory exists */
	config_create_config_dir();

	/* build file path */
	const char *config_path = config_get_config_dir();
	char *device_record_file = string_concat(config_path, DIR_SEP_S, udid, CONFIG_EXT, NULL);

	struct stat st;

	if ((stat(device_record_file, &st) == 0) && S_ISREG(st.st_mode))
		res = 1;

	free(device_record_file);

	return res;
}

/**
 * Reads the BUID from a previously generated configuration file.
 *
 * @param system_buid pointer to a variable that will be set to point to a
 *     newly allocated string containing the BUID.
 *
 * @note It is the responsibility of the calling function to free the returned system_buid
 */
void config_get_system_buid(char **system_buid)
{
	plist_t value = NULL;

	config_get_value(CONFIG_SYSTEM_BUID_KEY, &value);

	if (value && (plist_get_node_type(value) == PLIST_STRING)) {
		plist_get_string_val(value, system_buid);
		usbmuxd_log(LL_DEBUG, "Got %s %s", CONFIG_SYSTEM_BUID_KEY, *system_buid);
	}

	if (value)
		plist_free(value);

	if (!*system_buid) {
		/* no config, generate system_buid */
		usbmuxd_log(LL_DEBUG, "No previous %s found", CONFIG_SYSTEM_BUID_KEY);
		*system_buid = config_generate_system_buid();
		if (!config_set_system_buid(*system_buid)) {
			usbmuxd_log(LL_WARNING, "WARNING: Failed to store SystemBUID, this might be a problem");
		}
	}

	usbmuxd_log(LL_DEBUG, "Using %s as %s", *system_buid, CONFIG_SYSTEM_BUID_KEY);
}

/**
 * Store a pairing record for the given device identifier.
 *
 * @param udid device identifier
 * @param record_data buffer containing a pairing record
 * @param record_size size of buffer passed in record_data
 *
 * @return 0 on success or a negative errno otherwise.
 */
int config_set_device_record(const char *udid, char* record_data, uint64_t record_size)
{
	int res = 0;

	if (!udid || !record_data || record_size < 8)
		return -EINVAL;

	plist_t plist = NULL;
	if (memcmp(record_data, "bplist00", 8) == 0) {
		plist_from_bin(record_data, record_size, &plist);
	} else {
		plist_from_xml(record_data, record_size, &plist);
	}

	if (!plist || plist_get_node_type(plist) != PLIST_DICT) {
		if (plist)
			plist_free(plist);
		return -EINVAL;
	}

	/* ensure config directory exists */
	config_create_config_dir();

	/* build file path */
	const char *config_path = config_get_config_dir();
	char *device_record_file = string_concat(config_path, DIR_SEP_S, udid, CONFIG_EXT, NULL);

	remove(device_record_file);

	/* store file */
	if (!plist_write_to_filename(plist, device_record_file, PLIST_FORMAT_XML)) {
		usbmuxd_log(LL_DEBUG, "Could not open '%s' for writing: %s", device_record_file, strerror(errno));
		res = -ENOENT;
	}
	free(device_record_file);
	if (plist)
		plist_free(plist);

	return res;
}

/**
 * Retrieve a pairing record for the given device identifier
 *
 * @param udid device identifier
 * @param record_data pointer to a variable that will be set to point to a
 *     newly allocated buffer holding the pairing record
 * @param record_size pointer to a variable that will be set to the size
 *     of the buffer given in record_data.
 *
 * @return 0 on success or a negative errno otherwise.
 */
int config_get_device_record(const char *udid, char **record_data, uint64_t *record_size)
{
	int res = 0;

	/* ensure config directory exists */
	config_create_config_dir();

	/* build file path */
	const char *config_path = config_get_config_dir();
	char *device_record_file = string_concat(config_path, DIR_SEP_S, udid, CONFIG_EXT, NULL);

	/* read file */
	buffer_read_from_filename(device_record_file, record_data, record_size);
	if (!*record_data) {
		usbmuxd_log(LL_ERROR, "ERROR: Failed to read '%s': %s", device_record_file, strerror(errno));
		res = -ENOENT;
	}
	free(device_record_file);

	return res;
}

/**
 * Remove the pairing record stored for a device from this host.
 *
 * @param udid The udid of the device
 *
 * @return 0 on success or a negative errno otherwise.
 */
int config_remove_device_record(const char *udid)
{
	int res = 0;

	/* build file path */
	const char *config_path = config_get_config_dir();
	char *device_record_file = string_concat(config_path, DIR_SEP_S, udid, CONFIG_EXT, NULL);

	/* remove file */
	if (remove(device_record_file) != 0) {
		res = -errno;
		usbmuxd_log(LL_DEBUG, "Could not remove %s: %s", device_record_file, strerror(errno));
	}

	free(device_record_file);

	return res;
}

static int config_device_record_get_value(const char *udid, const char *key, plist_t *value)
{
	const char *config_path = NULL;
	char *config_file = NULL;

	config_path = config_get_config_dir();
	config_file = string_concat(config_path, DIR_SEP_S, udid, CONFIG_EXT, NULL);

	int result = internal_get_value(config_file, key, value);

	free(config_file);

	return result;
}

void config_device_record_get_host_id(const char *udid, char **host_id)
{
	plist_t value = NULL;

	config_device_record_get_value(udid, CONFIG_HOST_ID_KEY, &value);

	if (value && (plist_get_node_type(value) == PLIST_STRING)) {
		plist_get_string_val(value, host_id);
	}

	if (value)
		plist_free(value);

	if (!*host_id) {
		usbmuxd_log(LL_ERROR, "ERROR: Could not get HostID from pairing record for udid %s", udid);
	}
}