| 1 |
/* |
| 2 |
* property_list_service.c |
| 3 |
* PropertyList service implementation. |
| 4 |
* |
| 5 |
* Copyright (c) 2010 Nikias Bassen. All Rights Reserved. |
| 6 |
* |
| 7 |
* This library is free software; you can redistribute it and/or |
| 8 |
* modify it under the terms of the GNU Lesser General Public |
| 9 |
* License as published by the Free Software Foundation; either |
| 10 |
* version 2.1 of the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This library is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
* Lesser General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Lesser General Public |
| 18 |
* License along with this library; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 |
*/ |
| 21 |
#ifdef HAVE_CONFIG_H |
| 22 |
#include <config.h> |
| 23 |
#endif |
| 24 |
#include <stdlib.h> |
| 25 |
#include <string.h> |
| 26 |
|
| 27 |
#include "property_list_service.h" |
| 28 |
#include "common/debug.h" |
| 29 |
#include "endianness.h" |
| 30 |
|
| 31 |
/** |
| 32 |
* Convert a service_error_t value to a property_list_service_error_t value. |
| 33 |
* Used internally to get correct error codes. |
| 34 |
* |
| 35 |
* @param err A service_error_t error code |
| 36 |
* |
| 37 |
* @return A matching property_list_service_error_t error code, |
| 38 |
* PROPERTY_LIST_SERVICE_E_UNKNOWN_ERROR otherwise. |
| 39 |
*/ |
| 40 |
static property_list_service_error_t service_to_property_list_service_error(service_error_t err) |
| 41 |
{ |
| 42 |
switch (err) { |
| 43 |
case SERVICE_E_SUCCESS: |
| 44 |
return PROPERTY_LIST_SERVICE_E_SUCCESS; |
| 45 |
case SERVICE_E_INVALID_ARG: |
| 46 |
return PROPERTY_LIST_SERVICE_E_INVALID_ARG; |
| 47 |
case SERVICE_E_MUX_ERROR: |
| 48 |
return PROPERTY_LIST_SERVICE_E_MUX_ERROR; |
| 49 |
case SERVICE_E_SSL_ERROR: |
| 50 |
return PROPERTY_LIST_SERVICE_E_SSL_ERROR; |
| 51 |
case SERVICE_E_NOT_ENOUGH_DATA: |
| 52 |
return PROPERTY_LIST_SERVICE_E_NOT_ENOUGH_DATA; |
| 53 |
case SERVICE_E_TIMEOUT: |
| 54 |
return PROPERTY_LIST_SERVICE_E_RECEIVE_TIMEOUT; |
| 55 |
default: |
| 56 |
break; |
| 57 |
} |
| 58 |
return PROPERTY_LIST_SERVICE_E_UNKNOWN_ERROR; |
| 59 |
} |
| 60 |
|
| 61 |
property_list_service_error_t property_list_service_client_new(idevice_t device, lockdownd_service_descriptor_t service, property_list_service_client_t *client) |
| 62 |
{ |
| 63 |
if (!device || !service || service->port == 0 || !client || *client) |
| 64 |
return PROPERTY_LIST_SERVICE_E_INVALID_ARG; |
| 65 |
|
| 66 |
service_client_t parent = NULL; |
| 67 |
service_error_t rerr = service_client_new(device, service, &parent); |
| 68 |
if (rerr != SERVICE_E_SUCCESS) { |
| 69 |
return service_to_property_list_service_error(rerr); |
| 70 |
} |
| 71 |
|
| 72 |
/* create client object */ |
| 73 |
property_list_service_client_t client_loc = (property_list_service_client_t)malloc(sizeof(struct property_list_service_client_private)); |
| 74 |
client_loc->parent = parent; |
| 75 |
|
| 76 |
/* all done, return success */ |
| 77 |
*client = client_loc; |
| 78 |
return PROPERTY_LIST_SERVICE_E_SUCCESS; |
| 79 |
} |
| 80 |
|
| 81 |
property_list_service_error_t property_list_service_client_free(property_list_service_client_t client) |
| 82 |
{ |
| 83 |
if (!client) |
| 84 |
return PROPERTY_LIST_SERVICE_E_INVALID_ARG; |
| 85 |
|
| 86 |
property_list_service_error_t err = service_to_property_list_service_error(service_client_free(client->parent)); |
| 87 |
|
| 88 |
free(client); |
| 89 |
client = NULL; |
| 90 |
|
| 91 |
return err; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Sends a plist using the given property list service client. |
| 96 |
* Internally used generic plist send function. |
| 97 |
* |
| 98 |
* @param client The property list service client to use for sending. |
| 99 |
* @param plist plist to send |
| 100 |
* @param binary 1 = send binary plist, 0 = send xml plist |
| 101 |
* |
| 102 |
* @return PROPERTY_LIST_SERVICE_E_SUCCESS on success, |
| 103 |
* PROPERTY_LIST_SERVICE_E_INVALID_ARG when one or more parameters are |
| 104 |
* invalid, PROPERTY_LIST_SERVICE_E_PLIST_ERROR when dict is not a valid |
| 105 |
* plist, PROPERTY_LIST_SERVICE_E_MUX_ERROR when a communication error |
| 106 |
* occurs, or PROPERTY_LIST_SERVICE_E_UNKNOWN_ERROR when an unspecified |
| 107 |
* error occurs. |
| 108 |
*/ |
| 109 |
static property_list_service_error_t internal_plist_send(property_list_service_client_t client, plist_t plist, int binary) |
| 110 |
{ |
| 111 |
property_list_service_error_t res = PROPERTY_LIST_SERVICE_E_UNKNOWN_ERROR; |
| 112 |
char *content = NULL; |
| 113 |
uint32_t length = 0; |
| 114 |
uint32_t nlen = 0; |
| 115 |
uint32_t bytes = 0; |
| 116 |
|
| 117 |
if (!client || (client && !client->parent) || !plist) { |
| 118 |
return PROPERTY_LIST_SERVICE_E_INVALID_ARG; |
| 119 |
} |
| 120 |
|
| 121 |
if (binary) { |
| 122 |
plist_to_bin(plist, &content, &length); |
| 123 |
} else { |
| 124 |
plist_to_xml(plist, &content, &length); |
| 125 |
} |
| 126 |
|
| 127 |
if (!content || length == 0) { |
| 128 |
return PROPERTY_LIST_SERVICE_E_PLIST_ERROR; |
| 129 |
} |
| 130 |
|
| 131 |
nlen = htobe32(length); |
| 132 |
debug_info("sending %d bytes", length); |
| 133 |
service_send(client->parent, (const char*)&nlen, sizeof(nlen), &bytes); |
| 134 |
if (bytes == sizeof(nlen)) { |
| 135 |
service_send(client->parent, content, length, &bytes); |
| 136 |
if (bytes > 0) { |
| 137 |
debug_info("sent %d bytes", bytes); |
| 138 |
debug_plist(plist); |
| 139 |
if (bytes == length) { |
| 140 |
res = PROPERTY_LIST_SERVICE_E_SUCCESS; |
| 141 |
} else { |
| 142 |
debug_info("ERROR: Could not send all data (%d of %d)!", bytes, length); |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
if (bytes <= 0) { |
| 147 |
debug_info("ERROR: sending to device failed."); |
| 148 |
res = PROPERTY_LIST_SERVICE_E_MUX_ERROR; |
| 149 |
} |
| 150 |
|
| 151 |
free(content); |
| 152 |
return res; |
| 153 |
} |
| 154 |
|
| 155 |
property_list_service_error_t property_list_service_send_xml_plist(property_list_service_client_t client, plist_t plist) |
| 156 |
{ |
| 157 |
return internal_plist_send(client, plist, 0); |
| 158 |
} |
| 159 |
|
| 160 |
property_list_service_error_t property_list_service_send_binary_plist(property_list_service_client_t client, plist_t plist) |
| 161 |
{ |
| 162 |
return internal_plist_send(client, plist, 1); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Receives a plist using the given property list service client. |
| 167 |
* Internally used generic plist receive function. |
| 168 |
* |
| 169 |
* @param client The property list service client to use for receiving |
| 170 |
* @param plist pointer to a plist_t that will point to the received plist |
| 171 |
* upon successful return |
| 172 |
* @param timeout Maximum time in milliseconds to wait for data. |
| 173 |
* |
| 174 |
* @return PROPERTY_LIST_SERVICE_E_SUCCESS on success, |
| 175 |
* PROPERTY_LIST_SERVICE_E_INVALID_ARG when client or *plist is NULL, |
| 176 |
* PROPERTY_LIST_SERVICE_E_NOT_ENOUGH_DATA when not enough data |
| 177 |
* received, PROPERTY_LIST_SERVICE_E_RECEIVE_TIMEOUT when the connection times out, |
| 178 |
* PROPERTY_LIST_SERVICE_E_PLIST_ERROR when the received data cannot be |
| 179 |
* converted to a plist, PROPERTY_LIST_SERVICE_E_MUX_ERROR when a |
| 180 |
* communication error occurs, or PROPERTY_LIST_SERVICE_E_UNKNOWN_ERROR |
| 181 |
* when an unspecified error occurs. |
| 182 |
*/ |
| 183 |
static property_list_service_error_t internal_plist_receive_timeout(property_list_service_client_t client, plist_t *plist, unsigned int timeout) |
| 184 |
{ |
| 185 |
property_list_service_error_t res = PROPERTY_LIST_SERVICE_E_UNKNOWN_ERROR; |
| 186 |
uint32_t pktlen = 0; |
| 187 |
uint32_t bytes = 0; |
| 188 |
|
| 189 |
if (!client || (client && !client->parent) || !plist) { |
| 190 |
return PROPERTY_LIST_SERVICE_E_INVALID_ARG; |
| 191 |
} |
| 192 |
|
| 193 |
*plist = NULL; |
| 194 |
service_error_t serr = service_receive_with_timeout(client->parent, (char*)&pktlen, sizeof(pktlen), &bytes, timeout); |
| 195 |
if (serr != SERVICE_E_SUCCESS) { |
| 196 |
debug_info("initial read failed!"); |
| 197 |
return service_to_property_list_service_error(serr); |
| 198 |
} |
| 199 |
|
| 200 |
if (bytes == 0) { |
| 201 |
/* success but 0 bytes length, assume timeout */ |
| 202 |
return PROPERTY_LIST_SERVICE_E_RECEIVE_TIMEOUT; |
| 203 |
} |
| 204 |
|
| 205 |
debug_info("initial read=%i", bytes); |
| 206 |
|
| 207 |
uint32_t curlen = 0; |
| 208 |
char *content = NULL; |
| 209 |
|
| 210 |
pktlen = be32toh(pktlen); |
| 211 |
debug_info("%d bytes following", pktlen); |
| 212 |
content = (char*)malloc(pktlen); |
| 213 |
if (!content) { |
| 214 |
debug_info("out of memory when allocating %d bytes", pktlen); |
| 215 |
return PROPERTY_LIST_SERVICE_E_UNKNOWN_ERROR; |
| 216 |
} |
| 217 |
|
| 218 |
while (curlen < pktlen) { |
| 219 |
serr = service_receive(client->parent, content+curlen, pktlen-curlen, &bytes); |
| 220 |
if (serr != SERVICE_E_SUCCESS) { |
| 221 |
res = service_to_property_list_service_error(serr); |
| 222 |
break; |
| 223 |
} |
| 224 |
debug_info("received %d bytes", bytes); |
| 225 |
curlen += bytes; |
| 226 |
} |
| 227 |
|
| 228 |
if (curlen < pktlen) { |
| 229 |
debug_info("received incomplete packet (%d of %d bytes)", curlen, pktlen); |
| 230 |
if (curlen > 0) { |
| 231 |
debug_info("incomplete packet following:"); |
| 232 |
debug_buffer(content, curlen); |
| 233 |
} |
| 234 |
free(content); |
| 235 |
return res; |
| 236 |
} |
| 237 |
|
| 238 |
if ((pktlen > 8) && !memcmp(content, "bplist00", 8)) { |
| 239 |
plist_from_bin(content, pktlen, plist); |
| 240 |
} else if ((pktlen > 5) && !memcmp(content, "<?xml", 5)) { |
| 241 |
/* iOS 4.3+ hack: plist data might contain invalid characters, thus we convert those to spaces */ |
| 242 |
for (bytes = 0; bytes < pktlen-1; bytes++) { |
| 243 |
if ((content[bytes] >= 0) && (content[bytes] < 0x20) && (content[bytes] != 0x09) && (content[bytes] != 0x0a) && (content[bytes] != 0x0d)) |
| 244 |
content[bytes] = 0x20; |
| 245 |
} |
| 246 |
plist_from_xml(content, pktlen, plist); |
| 247 |
} else { |
| 248 |
debug_info("WARNING: received unexpected non-plist content"); |
| 249 |
debug_buffer(content, pktlen); |
| 250 |
} |
| 251 |
|
| 252 |
if (*plist) { |
| 253 |
debug_plist(*plist); |
| 254 |
res = PROPERTY_LIST_SERVICE_E_SUCCESS; |
| 255 |
} else { |
| 256 |
res = PROPERTY_LIST_SERVICE_E_PLIST_ERROR; |
| 257 |
} |
| 258 |
|
| 259 |
free(content); |
| 260 |
content = NULL; |
| 261 |
|
| 262 |
return res; |
| 263 |
} |
| 264 |
|
| 265 |
property_list_service_error_t property_list_service_receive_plist_with_timeout(property_list_service_client_t client, plist_t *plist, unsigned int timeout) |
| 266 |
{ |
| 267 |
return internal_plist_receive_timeout(client, plist, timeout); |
| 268 |
} |
| 269 |
|
| 270 |
property_list_service_error_t property_list_service_receive_plist(property_list_service_client_t client, plist_t *plist) |
| 271 |
{ |
| 272 |
return internal_plist_receive_timeout(client, plist, 30000); |
| 273 |
} |
| 274 |
|
| 275 |
property_list_service_error_t property_list_service_enable_ssl(property_list_service_client_t client) |
| 276 |
{ |
| 277 |
if (!client || !client->parent) |
| 278 |
return PROPERTY_LIST_SERVICE_E_INVALID_ARG; |
| 279 |
return service_to_property_list_service_error(service_enable_ssl(client->parent)); |
| 280 |
} |
| 281 |
|
| 282 |
property_list_service_error_t property_list_service_disable_ssl(property_list_service_client_t client) |
| 283 |
{ |
| 284 |
if (!client || !client->parent) |
| 285 |
return PROPERTY_LIST_SERVICE_E_INVALID_ARG; |
| 286 |
return service_to_property_list_service_error(service_disable_ssl(client->parent)); |
| 287 |
} |
| 288 |
|
| 289 |
property_list_service_error_t property_list_service_get_service_client(property_list_service_client_t client, service_client_t *service_client) |
| 290 |
{ |
| 291 |
if (!client || !client->parent || !service_client) |
| 292 |
return PROPERTY_LIST_SERVICE_E_INVALID_ARG; |
| 293 |
*service_client = client->parent; |
| 294 |
return PROPERTY_LIST_SERVICE_E_SUCCESS; |
| 295 |
} |