diff options
| -rw-r--r-- | include/Makefile.am | 1 | ||||
| -rw-r--r-- | include/libimobiledevice/mobileactivation.h | 144 | ||||
| -rw-r--r-- | src/Makefile.am | 1 | ||||
| -rw-r--r-- | src/mobileactivation.c | 209 | ||||
| -rw-r--r-- | src/mobileactivation.h | 32 |
5 files changed, 387 insertions, 0 deletions
diff --git a/include/Makefile.am b/include/Makefile.am index 9f61e6b..f2b93ed 100644 --- a/include/Makefile.am +++ b/include/Makefile.am | |||
| @@ -20,5 +20,6 @@ nobase_include_HEADERS = libimobiledevice/libimobiledevice.h \ | |||
| 20 | libimobiledevice/diagnostics_relay.h\ | 20 | libimobiledevice/diagnostics_relay.h\ |
| 21 | libimobiledevice/debugserver.h\ | 21 | libimobiledevice/debugserver.h\ |
| 22 | libimobiledevice/syslog_relay.h\ | 22 | libimobiledevice/syslog_relay.h\ |
| 23 | libimobiledevice/mobileactivation.h\ | ||
| 23 | libimobiledevice/property_list_service.h\ | 24 | libimobiledevice/property_list_service.h\ |
| 24 | libimobiledevice/service.h | 25 | libimobiledevice/service.h |
diff --git a/include/libimobiledevice/mobileactivation.h b/include/libimobiledevice/mobileactivation.h new file mode 100644 index 0000000..bb977fe --- /dev/null +++ b/include/libimobiledevice/mobileactivation.h | |||
| @@ -0,0 +1,144 @@ | |||
| 1 | /** | ||
| 2 | * @file libimobiledevice/mobileactivation.h | ||
| 3 | * @brief Handle device activation and deactivation. | ||
| 4 | * \internal | ||
| 5 | * | ||
| 6 | * Copyright (c) 2016 Nikias Bassen, All Rights Reserved. | ||
| 7 | * | ||
| 8 | * This library is free software; you can redistribute it and/or | ||
| 9 | * modify it under the terms of the GNU Lesser General Public | ||
| 10 | * License as published by the Free Software Foundation; either | ||
| 11 | * version 2.1 of the License, or (at your option) any later version. | ||
| 12 | * | ||
| 13 | * This library is distributed in the hope that it will be useful, | ||
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 16 | * Lesser General Public License for more details. | ||
| 17 | * | ||
| 18 | * You should have received a copy of the GNU Lesser General Public | ||
| 19 | * License along with this library; if not, write to the Free Software | ||
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 21 | */ | ||
| 22 | |||
| 23 | #ifndef IMOBILEACTIVATION_H | ||
| 24 | #define IMOBILEACTIVATION_H | ||
| 25 | |||
| 26 | #ifdef __cplusplus | ||
| 27 | extern "C" { | ||
| 28 | #endif | ||
| 29 | |||
| 30 | #include <libimobiledevice/libimobiledevice.h> | ||
| 31 | #include <libimobiledevice/lockdown.h> | ||
| 32 | |||
| 33 | #define MOBILEACTIVATION_SERVICE_NAME "com.apple.mobileactivationd" | ||
| 34 | |||
| 35 | /** Error Codes */ | ||
| 36 | typedef enum { | ||
| 37 | MOBILEACTIVATION_E_SUCCESS = 0, | ||
| 38 | MOBILEACTIVATION_E_INVALID_ARG = -1, | ||
| 39 | MOBILEACTIVATION_E_PLIST_ERROR = -2, | ||
| 40 | MOBILEACTIVATION_E_MUX_ERROR = -3, | ||
| 41 | MOBILEACTIVATION_E_UNKNOWN_REQUEST = -4, | ||
| 42 | MOBILEACTIVATION_E_REQUEST_FAILED = -5, | ||
| 43 | MOBILEACTIVATION_E_UNKNOWN_ERROR = -256 | ||
| 44 | } mobileactivation_error_t; | ||
| 45 | |||
| 46 | typedef struct mobileactivation_client_private mobileactivation_client_private; | ||
| 47 | typedef mobileactivation_client_private *mobileactivation_client_t; /**< The client handle. */ | ||
| 48 | |||
| 49 | /** | ||
| 50 | * Connects to the mobileactivation service on the specified device. | ||
| 51 | * | ||
| 52 | * @param device The device to connect to. | ||
| 53 | * @param service The service descriptor returned by lockdownd_start_service. | ||
| 54 | * @param client Reference that will point to a newly allocated | ||
| 55 | * mobileactivation_client_t upon successful return. | ||
| 56 | * | ||
| 57 | * @return MOBILEACTIVATION_E_SUCCESS on success, | ||
| 58 | * MOBILEACTIVATION_E_INVALID_ARG when one of the parameters is invalid, | ||
| 59 | * or MOBILEACTIVATION_E_MUX_ERROR when the connection failed. | ||
| 60 | */ | ||
| 61 | mobileactivation_error_t mobileactivation_client_new(idevice_t device, lockdownd_service_descriptor_t service, mobileactivation_client_t *client); | ||
| 62 | |||
| 63 | /** | ||
| 64 | * Starts a new mobileactivation service on the specified device and connects to it. | ||
| 65 | * | ||
| 66 | * @param device The device to connect to. | ||
| 67 | * @param client Pointer that will point to a newly allocated | ||
| 68 | * mobileactivation_client_t upon successful return. Must be freed using | ||
| 69 | * mobileactivation_client_free() after use. | ||
| 70 | * @param label The label to use for communication. Usually the program name. | ||
| 71 | * Pass NULL to disable sending the label in requests to lockdownd. | ||
| 72 | * | ||
| 73 | * @return MOBILEACTIVATION_E_SUCCESS on success, or an MOBILEACTIVATION_E_* | ||
| 74 | * error code otherwise. | ||
| 75 | */ | ||
| 76 | mobileactivation_error_t mobileactivation_client_start_service(idevice_t device, mobileactivation_client_t* client, const char* label); | ||
| 77 | |||
| 78 | /** | ||
| 79 | * Disconnects a mobileactivation client from the device and frees up the | ||
| 80 | * mobileactivation client data. | ||
| 81 | * | ||
| 82 | * @param client The mobileactivation client to disconnect and free. | ||
| 83 | * | ||
| 84 | * @return MOBILEACTIVATION_E_SUCCESS on success, | ||
| 85 | * MOBILEACTIVATION_E_INVALID_ARG when one of client or client->parent | ||
| 86 | * is invalid, or MOBILEACTIVATION_E_UNKNOWN_ERROR when the was an | ||
| 87 | * error freeing the parent property_list_service client. | ||
| 88 | */ | ||
| 89 | mobileactivation_error_t mobileactivation_client_free(mobileactivation_client_t client); | ||
| 90 | |||
| 91 | |||
| 92 | /** | ||
| 93 | * Retrieves the device's activation state. | ||
| 94 | * | ||
| 95 | * @param client The mobileactivation client. | ||
| 96 | * @param state Pointer to a plist_t variable that will be set to the | ||
| 97 | * activation state reported by the mobileactivation service. The | ||
| 98 | * consumer is responsible for freeing the returned object using | ||
| 99 | * plist_free(). | ||
| 100 | * | ||
| 101 | * @return MOBILEACTIVATION_E_SUCCESS on success, or an MOBILEACTIVATION_E_* | ||
| 102 | * error code otherwise. | ||
| 103 | */ | ||
| 104 | mobileactivation_error_t mobileactivation_get_activation_state(mobileactivation_client_t client, plist_t *state); | ||
| 105 | |||
| 106 | /** | ||
| 107 | * Retrieves the activation info required for device activation. | ||
| 108 | * | ||
| 109 | * @param client The mobileactivation client | ||
| 110 | * @param info Pointer to a plist_t variable that will be set to the | ||
| 111 | * activation info created by the mobileactivation service. The | ||
| 112 | * consumer is responsible for freeing the returned object using | ||
| 113 | * plist_free(). | ||
| 114 | * | ||
| 115 | * @return MOBILEACTIVATION_E_SUCCESS on success, or an MOBILEACTIVATION_E_* | ||
| 116 | * error code otherwise. | ||
| 117 | */ | ||
| 118 | mobileactivation_error_t mobileactivation_create_activation_info(mobileactivation_client_t client, plist_t *info); | ||
| 119 | |||
| 120 | /** | ||
| 121 | * Activates the device with the given activation record. | ||
| 122 | * The activation record plist dictionary must be obtained using the | ||
| 123 | * activation protocol requesting from Apple's https webservice. | ||
| 124 | * | ||
| 125 | * @param client The mobileactivation client | ||
| 126 | * @param activation_record The activation record plist dictionary | ||
| 127 | * | ||
| 128 | * @return MOBILEACTIVATION_E_SUCCESS on success, or an MOBILEACTIVATION_E_* | ||
| 129 | * error code otherwise. | ||
| 130 | */ | ||
| 131 | mobileactivation_error_t mobileactivation_activate(mobileactivation_client_t client, plist_t activation_record); | ||
| 132 | |||
| 133 | /** | ||
| 134 | * Deactivates the device. | ||
| 135 | * | ||
| 136 | * @param client The mobileactivation client | ||
| 137 | */ | ||
| 138 | mobileactivation_error_t mobileactivation_deactivate(mobileactivation_client_t client); | ||
| 139 | |||
| 140 | #ifdef __cplusplus | ||
| 141 | } | ||
| 142 | #endif | ||
| 143 | |||
| 144 | #endif | ||
diff --git a/src/Makefile.am b/src/Makefile.am index efd95eb..fcde8ae 100644 --- a/src/Makefile.am +++ b/src/Makefile.am | |||
| @@ -28,6 +28,7 @@ libimobiledevice_la_SOURCES = idevice.c idevice.h \ | |||
| 28 | heartbeat.c heartbeat.h\ | 28 | heartbeat.c heartbeat.h\ |
| 29 | debugserver.c debugserver.h\ | 29 | debugserver.c debugserver.h\ |
| 30 | webinspector.c webinspector.h\ | 30 | webinspector.c webinspector.h\ |
| 31 | mobileactivation.c mobileactivation.h\ | ||
| 31 | syslog_relay.c syslog_relay.h | 32 | syslog_relay.c syslog_relay.h |
| 32 | 33 | ||
| 33 | if WIN32 | 34 | if WIN32 |
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 | } | ||
diff --git a/src/mobileactivation.h b/src/mobileactivation.h new file mode 100644 index 0000000..49b9ebc --- /dev/null +++ b/src/mobileactivation.h | |||
| @@ -0,0 +1,32 @@ | |||
| 1 | /* | ||
| 2 | * mobileactivation.h | ||
| 3 | * com.apple.mobileactivationd service header file. | ||
| 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 | |||
| 22 | #ifndef __MOBILEACTIVATION_H | ||
| 23 | #define __MOBILEACTIVATION_H | ||
| 24 | |||
| 25 | #include "libimobiledevice/mobileactivation.h" | ||
| 26 | #include "property_list_service.h" | ||
| 27 | |||
| 28 | struct mobileactivation_client_private { | ||
| 29 | property_list_service_client_t parent; | ||
| 30 | }; | ||
| 31 | |||
| 32 | #endif | ||
