summaryrefslogtreecommitdiffstats
path: root/src/companion_proxy.c
blob: edce50049a23bf33d7dd8522e8cbe79c638d4e13 (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
/*
 * companion_proxy.c
 * com.apple.companion_proxy service implementation.
 *
 * Copyright (c) 2019-2020 Nikias Bassen, All Rights Reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * 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.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; 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 <string.h>
#include <stdlib.h>
#include <plist/plist.h>

#include "companion_proxy.h"
#include "lockdown.h"
#include "common/debug.h"
#include "common/thread.h"

/**
 * Convert a property_list_service_error_t value to a compproxy_error_t value.
 * Used internally to get correct error codes.
 *
 * @param err An property_list_service_error_t error code
 *
 * @return A matching compproxy_error_t error code,
 *     COMPPROXY_E_UNKNOWN_ERROR otherwise.
 */
static compproxy_error_t compproxy_error(property_list_service_error_t err)
{
	switch (err) {
		case PROPERTY_LIST_SERVICE_E_SUCCESS:
			return COMPPROXY_E_SUCCESS;
		case PROPERTY_LIST_SERVICE_E_INVALID_ARG:
			return COMPPROXY_E_INVALID_ARG;
		case PROPERTY_LIST_SERVICE_E_PLIST_ERROR:
			return COMPPROXY_E_PLIST_ERROR;
		case PROPERTY_LIST_SERVICE_E_MUX_ERROR:
			return COMPPROXY_E_MUX_ERROR;
		case PROPERTY_LIST_SERVICE_E_SSL_ERROR:
			return COMPPROXY_E_SSL_ERROR;
		case PROPERTY_LIST_SERVICE_E_NOT_ENOUGH_DATA:
			return COMPPROXY_E_NOT_ENOUGH_DATA;
		case PROPERTY_LIST_SERVICE_E_RECEIVE_TIMEOUT:
			return COMPPROXY_E_TIMEOUT;
		default:
			break;
	}
	return COMPPROXY_E_UNKNOWN_ERROR;
}

LIBIMOBILEDEVICE_API compproxy_error_t compproxy_client_new(idevice_t device, lockdownd_service_descriptor_t service, compproxy_client_t * client)
{
	*client = NULL;

	if (!device || !service || service->port == 0 || !client || *client) {
		debug_info("Incorrect parameter passed to compproxy_client_new.");
		return COMPPROXY_E_INVALID_ARG;
	}

	debug_info("Creating compproxy_client, port = %d.", service->port);

	property_list_service_client_t plclient = NULL;
	compproxy_error_t ret = compproxy_error(property_list_service_client_new(device, service, &plclient));
	if (ret != COMPPROXY_E_SUCCESS) {
		debug_info("Creating a property list client failed. Error: %i", ret);
		return ret;
	}

	compproxy_client_t client_loc = (compproxy_client_t) malloc(sizeof(struct compproxy_client_private));
	client_loc->parent = plclient;
	client_loc->event_thread = THREAD_T_NULL;

	*client = client_loc;

	debug_info("compproxy_client successfully created.");
	return COMPPROXY_E_SUCCESS;
}

LIBIMOBILEDEVICE_API compproxy_error_t compproxy_client_start_service(idevice_t device, compproxy_client_t * client, const char* label)
{
	compproxy_error_t err = COMPPROXY_E_UNKNOWN_ERROR;
	service_client_factory_start_service(device, COMPPROXY_SERVICE_NAME, (void**)client, label, SERVICE_CONSTRUCTOR(compproxy_client_new), &err);
	return err;
}

LIBIMOBILEDEVICE_API compproxy_error_t compproxy_client_free(compproxy_client_t client)
{
	if (!client)
		return COMPPROXY_E_INVALID_ARG;

	property_list_service_client_t parent = client->parent;
	client->parent = NULL;
	if (client->event_thread) {
		debug_info("joining event thread");
		thread_join(client->event_thread);
		thread_free(client->event_thread);
		client->event_thread = THREAD_T_NULL;
	}
	compproxy_error_t err = compproxy_error(property_list_service_client_free(parent));
	free(client);

	return err;
}

LIBIMOBILEDEVICE_API compproxy_error_t compproxy_send(compproxy_client_t client, plist_t plist)
{
	compproxy_error_t res = COMPPROXY_E_UNKNOWN_ERROR;

	res = compproxy_error(property_list_service_send_binary_plist(client->parent, plist));
	if (res != COMPPROXY_E_SUCCESS) {
		debug_info("Sending plist failed with error %d", res);
		return res;
	}

	return res;
}

LIBIMOBILEDEVICE_API compproxy_error_t compproxy_receive(compproxy_client_t client, plist_t * plist)
{
	compproxy_error_t res = COMPPROXY_E_UNKNOWN_ERROR;
	plist_t outplist = NULL;
	res = compproxy_error(property_list_service_receive_plist_with_timeout(client->parent, &outplist, 10000));
	if (res != COMPPROXY_E_SUCCESS && res != COMPPROXY_E_TIMEOUT) {
		debug_info("Could not receive plist, error %d", res);
		plist_free(outplist);
	} else if (res == COMPPROXY_E_SUCCESS) {
		*plist = outplist;
	}
	return res;
}

LIBIMOBILEDEVICE_API compproxy_error_t compproxy_get_device_registry(compproxy_client_t client, plist_t* paired_devices)
{
	if (!client || !paired_devices) {
		return COMPPROXY_E_INVALID_ARG;
	}

	plist_t dict = plist_new_dict();
	plist_dict_set_item(dict, "Command", plist_new_string("GetDeviceRegistry"));

	compproxy_error_t res = compproxy_send(client, dict);
	plist_free(dict);
	dict = NULL;
	if (res != COMPPROXY_E_SUCCESS) {
		return res;
	}

	res = compproxy_receive(client, &dict);
	if (res != COMPPROXY_E_SUCCESS) {
		return res;
	}
	if (!dict || !PLIST_IS_DICT(dict)) {
		return COMPPROXY_E_PLIST_ERROR;
	}
	plist_t val = plist_dict_get_item(dict, "PairedDevicesArray");
	if (val) {
		*paired_devices = plist_copy(val);
		res = COMPPROXY_E_SUCCESS;
	} else {
		res = COMPPROXY_E_UNKNOWN_ERROR;
		val = plist_dict_get_item(dict, "Error");
		if (val) {
			if (plist_string_val_compare(val, "NoPairedWatches")) {
				res = COMPPROXY_E_NO_DEVICES;
			}
		}
	}
	plist_free(dict);
	return res;
}

struct compproxy_cb_data {
	compproxy_client_t client;
	compproxy_device_event_cb_t cbfunc;
	void* user_data;
};

static void* compproxy_event_thread(void* arg)
{
	struct compproxy_cb_data* data = (struct compproxy_cb_data*)arg;
	compproxy_client_t client = data->client;
	compproxy_error_t res;

	plist_t command = plist_new_dict();
	plist_dict_set_item(command, "Command", plist_new_string("StartListeningForDevices"));
	res = compproxy_send(client, command);
	plist_free(command);

	if (res != COMPPROXY_E_SUCCESS) {
		free(data);
		client->event_thread = THREAD_T_NULL;
		return NULL;
	}

	while (client && client->parent) {
		plist_t node = NULL;
		res = compproxy_error(property_list_service_receive_plist_with_timeout(client->parent, &node, 1000));
		if (res != COMPPROXY_E_SUCCESS && res != COMPPROXY_E_TIMEOUT) {
			debug_info("could not receive plist, error %d", res);
			break;
		}

		if (node) {
			data->cbfunc(node, data->user_data);
		}
		plist_free(node);
	}

	client->event_thread = THREAD_T_NULL;
	free(data);

	return NULL;
}

LIBIMOBILEDEVICE_API compproxy_error_t compproxy_start_listening_for_devices(compproxy_client_t client, compproxy_device_event_cb_t callback, void* userdata)
{
	if (!client || !client->parent || !callback) {
		return COMPPROXY_E_INVALID_ARG;
	}

	if (client->event_thread) {
		return COMPPROXY_E_OP_IN_PROGRESS;
	}

	compproxy_error_t res = COMPPROXY_E_UNKNOWN_ERROR;
	struct compproxy_cb_data *data = (struct compproxy_cb_data*)malloc(sizeof(struct compproxy_cb_data));
	if (data) {
		data->client = client;
		data->cbfunc = callback;
		data->user_data = userdata;

		if (thread_new(&client->event_thread, compproxy_event_thread, data) == 0) {
			res = COMPPROXY_E_SUCCESS;
		} else {
			free(data);
		}
	}
	return res;
}

LIBIMOBILEDEVICE_API compproxy_error_t compproxy_stop_listening_for_devices(compproxy_client_t client)
{
	property_list_service_client_t parent = client->parent;
	client->parent = NULL;
	if (client->event_thread) {
		debug_info("joining event thread");
		thread_join(client->event_thread);
		thread_free(client->event_thread);
		client->event_thread = THREAD_T_NULL;
	}
	client->parent = parent;
	return COMPPROXY_E_SUCCESS;
}

LIBIMOBILEDEVICE_API compproxy_error_t compproxy_get_value_from_registry(compproxy_client_t client, const char* companion_udid, const char* key, plist_t* value)
{
	if (!client || !companion_udid || !key || !value) {
		return COMPPROXY_E_INVALID_ARG;
	}

	plist_t dict = plist_new_dict();
	plist_dict_set_item(dict, "Command", plist_new_string("GetValueFromRegistry"));
	plist_dict_set_item(dict, "GetValueGizmoUDIDKey", plist_new_string(companion_udid));
	plist_dict_set_item(dict, "GetValueKeyKey", plist_new_string(key));

	compproxy_error_t res = compproxy_send(client, dict);
	plist_free(dict);
	dict = NULL;
	if (res != COMPPROXY_E_SUCCESS) {
		return res;
	}

	res = compproxy_receive(client, &dict);
	if (res != COMPPROXY_E_SUCCESS) {
		return res;
	}
	if (!dict || !PLIST_IS_DICT(dict)) {
		return COMPPROXY_E_PLIST_ERROR;
	}
	plist_t val = plist_dict_get_item(dict, "RetrievedValueDictionary");
	if (val) {
		*value = plist_copy(val);
		res = COMPPROXY_E_SUCCESS;
	} else {
		res = COMPPROXY_E_UNKNOWN_ERROR;
		val = plist_dict_get_item(dict, "Error");
		if (val) {
			if (!plist_string_val_compare(val, "UnsupportedWatchKey")) {
				res = COMPPROXY_E_UNSUPPORTED_KEY;
			} else if (plist_string_val_compare(val, "TimeoutReply")) {
				res = COMPPROXY_E_TIMEOUT_REPLY;
			}
		}
	}
	plist_free(dict);
	return res;
}

LIBIMOBILEDEVICE_API compproxy_error_t compproxy_start_forwarding_service_port(compproxy_client_t client, uint16_t remote_port, const char* service_name, uint16_t* forward_port, plist_t options)
{
	if (!client) {
		return COMPPROXY_E_INVALID_ARG;
	}

	plist_t dict = plist_new_dict();
	plist_dict_set_item(dict, "Command", plist_new_string("StartForwardingServicePort"));
	plist_dict_set_item(dict, "GizmoRemotePortNumber", plist_new_uint(remote_port));
	if (service_name) {
		plist_dict_set_item(dict, "ForwardedServiceName", plist_new_string(service_name));
	}
	plist_dict_set_item(dict, "IsServiceLowPriority", plist_new_bool(0));
	plist_dict_set_item(dict, "PreferWifi", plist_new_bool(0));
	if (options) {
		plist_dict_merge(dict, options);
	}

	compproxy_error_t res = compproxy_send(client, dict);
	plist_free(dict);
	dict = NULL;
	if (res != COMPPROXY_E_SUCCESS) {
		return res;
	}

	res = compproxy_receive(client, &dict);
	if (res != COMPPROXY_E_SUCCESS) {
		return res;
	}
	plist_t val = plist_dict_get_item(dict, "CompanionProxyServicePort");
	if (val) {
		uint64_t u64val = 0;
		plist_get_uint_val(val, &u64val);
		*forward_port = (uint16_t)u64val;
		res = COMPPROXY_E_SUCCESS;
	} else {
		res = COMPPROXY_E_UNKNOWN_ERROR;
	}
	plist_free(dict);

	return res;
}

LIBIMOBILEDEVICE_API compproxy_error_t compproxy_stop_forwarding_service_port(compproxy_client_t client, uint16_t remote_port)
{
	if (!client) {
		return COMPPROXY_E_INVALID_ARG;
	}

	plist_t dict = plist_new_dict();
	plist_dict_set_item(dict, "Command", plist_new_string("StopForwardingServicePort"));
	plist_dict_set_item(dict, "GizmoRemotePortNumber", plist_new_uint(remote_port));

	compproxy_error_t res = compproxy_send(client, dict);
	plist_free(dict);
	dict = NULL;
	if (res != COMPPROXY_E_SUCCESS) {
		return res;
	}

	res = compproxy_receive(client, &dict);
	if (res != COMPPROXY_E_SUCCESS) {
		return res;
	}
	plist_free(dict);

	return res;
}