diff options
Diffstat (limited to 'src/mobileactivation.c')
| -rw-r--r-- | src/mobileactivation.c | 209 |
1 files changed, 209 insertions, 0 deletions
diff --git a/src/mobileactivation.c b/src/mobileactivation.c new file mode 100644 index 0000000..f14eb73 --- /dev/null +++ b/src/mobileactivation.c | |||
| @@ -0,0 +1,209 @@ | |||
| 1 | /* | ||
| 2 | * mobileactivation.c | ||
| 3 | * com.apple.mobileactivationd service implementation. | ||
| 4 | * | ||
| 5 | * Copyright (c) 2016 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 | #include <string.h> | ||
| 22 | #include <stdlib.h> | ||
| 23 | #include "mobileactivation.h" | ||
| 24 | #include "property_list_service.h" | ||
| 25 | #include "common/debug.h" | ||
| 26 | |||
| 27 | /** | ||
| 28 | * Convert a property_list_service_error_t value to a mobileactivation_error_t value. | ||
| 29 | * Used internally to get correct error codes. | ||
| 30 | * | ||
| 31 | * @param err An property_list_service_error_t error code | ||
| 32 | * | ||
| 33 | * @return A matching mobileactivation_error_t error code, | ||
| 34 | * MOBILEACTIVATION_E_UNKNOWN_ERROR otherwise. | ||
| 35 | */ | ||
| 36 | static mobileactivation_error_t mobileactivation_error(property_list_service_error_t err) | ||
| 37 | { | ||
| 38 | switch (err) { | ||
| 39 | case PROPERTY_LIST_SERVICE_E_SUCCESS: | ||
| 40 | return MOBILEACTIVATION_E_SUCCESS; | ||
| 41 | case PROPERTY_LIST_SERVICE_E_INVALID_ARG: | ||
| 42 | return MOBILEACTIVATION_E_INVALID_ARG; | ||
| 43 | case PROPERTY_LIST_SERVICE_E_PLIST_ERROR: | ||
| 44 | return MOBILEACTIVATION_E_PLIST_ERROR; | ||
| 45 | case PROPERTY_LIST_SERVICE_E_MUX_ERROR: | ||
| 46 | return MOBILEACTIVATION_E_MUX_ERROR; | ||
| 47 | default: | ||
| 48 | break; | ||
| 49 | } | ||
| 50 | return MOBILEACTIVATION_E_UNKNOWN_ERROR; | ||
| 51 | } | ||
| 52 | |||
| 53 | LIBIMOBILEDEVICE_API mobileactivation_error_t mobileactivation_client_new(idevice_t device, lockdownd_service_descriptor_t service, mobileactivation_client_t *client) | ||
| 54 | { | ||
| 55 | if (!device || !service || service->port == 0 || !client || *client) { | ||
| 56 | return MOBILEACTIVATION_E_INVALID_ARG; | ||
| 57 | } | ||
| 58 | |||
| 59 | property_list_service_client_t plistclient = NULL; | ||
| 60 | if (property_list_service_client_new(device, service, &plistclient) != PROPERTY_LIST_SERVICE_E_SUCCESS) { | ||
| 61 | return MOBILEACTIVATION_E_MUX_ERROR; | ||
| 62 | } | ||
| 63 | |||
| 64 | /* create client object */ | ||
| 65 | mobileactivation_client_t client_loc = (mobileactivation_client_t) malloc(sizeof(struct mobileactivation_client_private)); | ||
| 66 | client_loc->parent = plistclient; | ||
| 67 | |||
| 68 | /* all done, return success */ | ||
| 69 | *client = client_loc; | ||
| 70 | return MOBILEACTIVATION_E_SUCCESS; | ||
| 71 | } | ||
| 72 | |||
| 73 | LIBIMOBILEDEVICE_API mobileactivation_error_t mobileactivation_client_start_service(idevice_t device, mobileactivation_client_t * client, const char* label) | ||
| 74 | { | ||
| 75 | mobileactivation_error_t err = MOBILEACTIVATION_E_UNKNOWN_ERROR; | ||
| 76 | service_client_factory_start_service(device, MOBILEACTIVATION_SERVICE_NAME, (void**)client, label, SERVICE_CONSTRUCTOR(mobileactivation_client_new), &err); | ||
| 77 | return err; | ||
| 78 | } | ||
| 79 | |||
| 80 | LIBIMOBILEDEVICE_API mobileactivation_error_t mobileactivation_client_free(mobileactivation_client_t client) | ||
| 81 | { | ||
| 82 | if (!client) | ||
| 83 | return MOBILEACTIVATION_E_INVALID_ARG; | ||
| 84 | |||
| 85 | if (property_list_service_client_free(client->parent) != PROPERTY_LIST_SERVICE_E_SUCCESS) { | ||
| 86 | return MOBILEACTIVATION_E_UNKNOWN_ERROR; | ||
| 87 | } | ||
| 88 | free(client); | ||
| 89 | return MOBILEACTIVATION_E_SUCCESS; | ||
| 90 | } | ||
| 91 | |||
| 92 | static mobileactivation_error_t mobileactivation_check_result(plist_t dict, const char *command) | ||
| 93 | { | ||
| 94 | mobileactivation_error_t ret = MOBILEACTIVATION_E_UNKNOWN_ERROR; | ||
| 95 | |||
| 96 | if (!dict || plist_get_node_type(dict) != PLIST_DICT) { | ||
| 97 | return MOBILEACTIVATION_E_PLIST_ERROR; | ||
| 98 | } | ||
| 99 | |||
| 100 | plist_t err_node = plist_dict_get_item(dict, "Error"); | ||
| 101 | if (!err_node) { | ||
| 102 | return MOBILEACTIVATION_E_SUCCESS; | ||
| 103 | } else { | ||
| 104 | char *errmsg = NULL; | ||
| 105 | plist_get_string_val(err_node, &errmsg); | ||
| 106 | debug_info("ERROR: %s: %s", command, errmsg); | ||
| 107 | free(errmsg); | ||
| 108 | ret = MOBILEACTIVATION_E_REQUEST_FAILED; | ||
| 109 | } | ||
| 110 | return ret; | ||
| 111 | } | ||
| 112 | |||
| 113 | static mobileactivation_error_t mobileactivation_send_command(mobileactivation_client_t client, const char* command, plist_t value, plist_t *result) | ||
| 114 | { | ||
| 115 | if (!client || !command || !result) | ||
| 116 | return MOBILEACTIVATION_E_INVALID_ARG; | ||
| 117 | |||
| 118 | mobileactivation_error_t ret = MOBILEACTIVATION_E_UNKNOWN_ERROR; | ||
| 119 | *result = NULL; | ||
| 120 | |||
| 121 | plist_t dict = plist_new_dict(); | ||
| 122 | plist_dict_set_item(dict, "Command", plist_new_string(command)); | ||
| 123 | if (value) { | ||
| 124 | plist_dict_set_item(dict, "Value", plist_copy(value)); | ||
| 125 | } | ||
| 126 | |||
| 127 | ret = mobileactivation_error(property_list_service_send_binary_plist(client->parent, dict)); | ||
| 128 | plist_free(dict); | ||
| 129 | dict = NULL; | ||
| 130 | |||
| 131 | ret = mobileactivation_error(property_list_service_receive_plist(client->parent, &dict)); | ||
| 132 | if (!dict) { | ||
| 133 | debug_info("ERROR: Did not get reply for %s command", command); | ||
| 134 | return MOBILEACTIVATION_E_PLIST_ERROR; | ||
| 135 | } | ||
| 136 | |||
| 137 | *result = dict; | ||
| 138 | return mobileactivation_check_result(dict, command); | ||
| 139 | } | ||
| 140 | |||
| 141 | LIBIMOBILEDEVICE_API mobileactivation_error_t mobileactivation_get_activation_state(mobileactivation_client_t client, plist_t *state) | ||
| 142 | { | ||
| 143 | if (!client || !state) | ||
| 144 | return MOBILEACTIVATION_E_INVALID_ARG; | ||
| 145 | |||
| 146 | plist_t result = NULL; | ||
| 147 | mobileactivation_error_t ret = mobileactivation_send_command(client, "GetActivationStateRequest", NULL, &result); | ||
| 148 | if (ret == MOBILEACTIVATION_E_SUCCESS) { | ||
| 149 | plist_t node = plist_dict_get_item(result, "Value"); | ||
| 150 | if (!node) { | ||
| 151 | debug_info("ERROR: GetActivationStateRequest command returned success but has no value in reply"); | ||
| 152 | ret = MOBILEACTIVATION_E_UNKNOWN_ERROR; | ||
| 153 | } else { | ||
| 154 | *state = plist_copy(node); | ||
| 155 | } | ||
| 156 | } | ||
| 157 | plist_free(result); | ||
| 158 | result = NULL; | ||
| 159 | |||
| 160 | return ret; | ||
| 161 | } | ||
| 162 | |||
| 163 | LIBIMOBILEDEVICE_API mobileactivation_error_t mobileactivation_create_activation_info(mobileactivation_client_t client, plist_t *info) | ||
| 164 | { | ||
| 165 | if (!client || !info) | ||
| 166 | return MOBILEACTIVATION_E_INVALID_ARG; | ||
| 167 | |||
| 168 | plist_t result = NULL; | ||
| 169 | mobileactivation_error_t ret = mobileactivation_send_command(client, "CreateActivationInfoRequest", NULL, &result); | ||
| 170 | if (ret == MOBILEACTIVATION_E_SUCCESS) { | ||
| 171 | plist_t node = plist_dict_get_item(result, "Value"); | ||
| 172 | if (!node) { | ||
| 173 | debug_info("ERROR: CreateActivationInfoRequest command returned success but has no value in reply"); | ||
| 174 | ret = MOBILEACTIVATION_E_UNKNOWN_ERROR; | ||
| 175 | } else { | ||
| 176 | *info = plist_copy(node); | ||
| 177 | } | ||
| 178 | } | ||
| 179 | plist_free(result); | ||
| 180 | result = NULL; | ||
| 181 | |||
| 182 | return ret; | ||
| 183 | } | ||
| 184 | |||
| 185 | LIBIMOBILEDEVICE_API mobileactivation_error_t mobileactivation_activate(mobileactivation_client_t client, plist_t activation_record) | ||
| 186 | { | ||
| 187 | if (!client || !activation_record) | ||
| 188 | return MOBILEACTIVATION_E_INVALID_ARG; | ||
| 189 | |||
| 190 | plist_t result = NULL; | ||
| 191 | mobileactivation_error_t ret = mobileactivation_send_command(client, "HandleActivationInfoRequest", activation_record, &result); | ||
| 192 | plist_free(result); | ||
| 193 | result = NULL; | ||
| 194 | |||
| 195 | return ret; | ||
| 196 | } | ||
| 197 | |||
| 198 | LIBIMOBILEDEVICE_API mobileactivation_error_t mobileactivation_deactivate(mobileactivation_client_t client) | ||
| 199 | { | ||
| 200 | if (!client) | ||
| 201 | return MOBILEACTIVATION_E_INVALID_ARG; | ||
| 202 | |||
| 203 | plist_t result = NULL; | ||
| 204 | mobileactivation_error_t ret = mobileactivation_send_command(client, "DeactivateRequest", NULL, &result); | ||
| 205 | plist_free(result); | ||
| 206 | result = NULL; | ||
| 207 | |||
| 208 | return ret; | ||
| 209 | } | ||
