| 1 |
/* |
| 2 |
* house_arrest.c |
| 3 |
* com.apple.mobile.house_arrest 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 |
|
| 22 |
#ifdef HAVE_CONFIG_H |
| 23 |
#include <config.h> |
| 24 |
#endif |
| 25 |
#include <string.h> |
| 26 |
#include <stdlib.h> |
| 27 |
|
| 28 |
#ifndef _MSC_VER |
| 29 |
#include <unistd.h> |
| 30 |
#endif |
| 31 |
|
| 32 |
#include <plist/plist.h> |
| 33 |
|
| 34 |
#include "house_arrest.h" |
| 35 |
#include "property_list_service.h" |
| 36 |
#include "afc.h" |
| 37 |
#include "common/debug.h" |
| 38 |
|
| 39 |
/** |
| 40 |
* Convert a property_list_service_error_t value to a house_arrest_error_t |
| 41 |
* value. Used internally to get correct error codes. |
| 42 |
* |
| 43 |
* @param err A property_list_service_error_t error code |
| 44 |
* |
| 45 |
* @return A matching house_arrest_error_t error code, |
| 46 |
* HOUSE_ARREST_E_UNKNOWN_ERROR otherwise. |
| 47 |
*/ |
| 48 |
static house_arrest_error_t house_arrest_error(property_list_service_error_t err) |
| 49 |
{ |
| 50 |
switch (err) { |
| 51 |
case PROPERTY_LIST_SERVICE_E_SUCCESS: |
| 52 |
return HOUSE_ARREST_E_SUCCESS; |
| 53 |
case PROPERTY_LIST_SERVICE_E_INVALID_ARG: |
| 54 |
return HOUSE_ARREST_E_INVALID_ARG; |
| 55 |
case PROPERTY_LIST_SERVICE_E_PLIST_ERROR: |
| 56 |
return HOUSE_ARREST_E_PLIST_ERROR; |
| 57 |
case PROPERTY_LIST_SERVICE_E_MUX_ERROR: |
| 58 |
return HOUSE_ARREST_E_CONN_FAILED; |
| 59 |
default: |
| 60 |
break; |
| 61 |
} |
| 62 |
return HOUSE_ARREST_E_UNKNOWN_ERROR; |
| 63 |
} |
| 64 |
|
| 65 |
house_arrest_error_t house_arrest_client_new(idevice_t device, lockdownd_service_descriptor_t service, house_arrest_client_t *client) |
| 66 |
{ |
| 67 |
property_list_service_client_t plistclient = NULL; |
| 68 |
house_arrest_error_t err = house_arrest_error(property_list_service_client_new(device, service, &plistclient)); |
| 69 |
if (err != HOUSE_ARREST_E_SUCCESS) { |
| 70 |
return err; |
| 71 |
} |
| 72 |
|
| 73 |
house_arrest_client_t client_loc = (house_arrest_client_t) malloc(sizeof(struct house_arrest_client_private)); |
| 74 |
client_loc->parent = plistclient; |
| 75 |
client_loc->mode = HOUSE_ARREST_CLIENT_MODE_NORMAL; |
| 76 |
|
| 77 |
*client = client_loc; |
| 78 |
return HOUSE_ARREST_E_SUCCESS; |
| 79 |
} |
| 80 |
|
| 81 |
house_arrest_error_t house_arrest_client_start_service(idevice_t device, house_arrest_client_t * client, const char* label) |
| 82 |
{ |
| 83 |
house_arrest_error_t err = HOUSE_ARREST_E_UNKNOWN_ERROR; |
| 84 |
service_client_factory_start_service(device, HOUSE_ARREST_SERVICE_NAME, (void**)client, label, SERVICE_CONSTRUCTOR(house_arrest_client_new), &err); |
| 85 |
return err; |
| 86 |
} |
| 87 |
|
| 88 |
house_arrest_error_t house_arrest_client_free(house_arrest_client_t client) |
| 89 |
{ |
| 90 |
if (!client) |
| 91 |
return HOUSE_ARREST_E_INVALID_ARG; |
| 92 |
|
| 93 |
house_arrest_error_t err = HOUSE_ARREST_E_SUCCESS; |
| 94 |
if (client->parent && client->parent->parent->connection) { |
| 95 |
house_arrest_error(property_list_service_client_free(client->parent)); |
| 96 |
} |
| 97 |
client->parent = NULL; |
| 98 |
free(client); |
| 99 |
|
| 100 |
return err; |
| 101 |
} |
| 102 |
|
| 103 |
house_arrest_error_t house_arrest_send_request(house_arrest_client_t client, plist_t dict) |
| 104 |
{ |
| 105 |
if (!client || !client->parent || !dict) |
| 106 |
return HOUSE_ARREST_E_INVALID_ARG; |
| 107 |
if (plist_get_node_type(dict) != PLIST_DICT) |
| 108 |
return HOUSE_ARREST_E_PLIST_ERROR; |
| 109 |
if (client->mode != HOUSE_ARREST_CLIENT_MODE_NORMAL) |
| 110 |
return HOUSE_ARREST_E_INVALID_MODE; |
| 111 |
|
| 112 |
house_arrest_error_t res = house_arrest_error(property_list_service_send_xml_plist(client->parent, dict)); |
| 113 |
if (res != HOUSE_ARREST_E_SUCCESS) { |
| 114 |
debug_info("could not send plist, error %d", res); |
| 115 |
} |
| 116 |
return res; |
| 117 |
} |
| 118 |
|
| 119 |
house_arrest_error_t house_arrest_send_command(house_arrest_client_t client, const char *command, const char *appid) |
| 120 |
{ |
| 121 |
if (!client || !client->parent || !command || !appid) |
| 122 |
return HOUSE_ARREST_E_INVALID_ARG; |
| 123 |
if (client->mode != HOUSE_ARREST_CLIENT_MODE_NORMAL) |
| 124 |
return HOUSE_ARREST_E_INVALID_MODE; |
| 125 |
|
| 126 |
house_arrest_error_t res = HOUSE_ARREST_E_UNKNOWN_ERROR; |
| 127 |
|
| 128 |
plist_t dict = plist_new_dict(); |
| 129 |
plist_dict_set_item(dict, "Command", plist_new_string(command)); |
| 130 |
plist_dict_set_item(dict, "Identifier", plist_new_string(appid)); |
| 131 |
|
| 132 |
res = house_arrest_send_request(client, dict); |
| 133 |
|
| 134 |
plist_free(dict); |
| 135 |
|
| 136 |
return res; |
| 137 |
} |
| 138 |
|
| 139 |
house_arrest_error_t house_arrest_get_result(house_arrest_client_t client, plist_t *dict) |
| 140 |
{ |
| 141 |
if (!client || !client->parent) |
| 142 |
return HOUSE_ARREST_E_INVALID_ARG; |
| 143 |
if (client->mode != HOUSE_ARREST_CLIENT_MODE_NORMAL) |
| 144 |
return HOUSE_ARREST_E_INVALID_MODE; |
| 145 |
|
| 146 |
house_arrest_error_t res = house_arrest_error(property_list_service_receive_plist(client->parent, dict)); |
| 147 |
if (res != HOUSE_ARREST_E_SUCCESS) { |
| 148 |
debug_info("could not get result, error %d", res); |
| 149 |
if (*dict) { |
| 150 |
plist_free(*dict); |
| 151 |
*dict = NULL; |
| 152 |
} |
| 153 |
} |
| 154 |
return res; |
| 155 |
} |
| 156 |
|
| 157 |
afc_error_t afc_client_new_from_house_arrest_client(house_arrest_client_t client, afc_client_t *afc_client) |
| 158 |
{ |
| 159 |
if (!client || !client->parent || (client->mode == HOUSE_ARREST_CLIENT_MODE_AFC)) { |
| 160 |
return AFC_E_INVALID_ARG; |
| 161 |
} |
| 162 |
afc_error_t err = afc_client_new_with_service_client(client->parent->parent, afc_client); |
| 163 |
if (err == AFC_E_SUCCESS) { |
| 164 |
client->mode = HOUSE_ARREST_CLIENT_MODE_AFC; |
| 165 |
} |
| 166 |
return err; |
| 167 |
} |