From 5250024b53b799a427a486ae133ecb927f5c555e Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Sat, 2 Jul 2016 01:37:50 +0200 Subject: Add basic mobileactivation service implementation --- include/Makefile.am | 1 + include/libimobiledevice/mobileactivation.h | 144 +++++++++++++++++++ src/Makefile.am | 1 + src/mobileactivation.c | 209 ++++++++++++++++++++++++++++ src/mobileactivation.h | 32 +++++ 5 files changed, 387 insertions(+) create mode 100644 include/libimobiledevice/mobileactivation.h create mode 100644 src/mobileactivation.c create mode 100644 src/mobileactivation.h 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 \ libimobiledevice/diagnostics_relay.h\ libimobiledevice/debugserver.h\ libimobiledevice/syslog_relay.h\ + libimobiledevice/mobileactivation.h\ libimobiledevice/property_list_service.h\ 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 @@ +/** + * @file libimobiledevice/mobileactivation.h + * @brief Handle device activation and deactivation. + * \internal + * + * Copyright (c) 2016 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 + */ + +#ifndef IMOBILEACTIVATION_H +#define IMOBILEACTIVATION_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#define MOBILEACTIVATION_SERVICE_NAME "com.apple.mobileactivationd" + +/** Error Codes */ +typedef enum { + MOBILEACTIVATION_E_SUCCESS = 0, + MOBILEACTIVATION_E_INVALID_ARG = -1, + MOBILEACTIVATION_E_PLIST_ERROR = -2, + MOBILEACTIVATION_E_MUX_ERROR = -3, + MOBILEACTIVATION_E_UNKNOWN_REQUEST = -4, + MOBILEACTIVATION_E_REQUEST_FAILED = -5, + MOBILEACTIVATION_E_UNKNOWN_ERROR = -256 +} mobileactivation_error_t; + +typedef struct mobileactivation_client_private mobileactivation_client_private; +typedef mobileactivation_client_private *mobileactivation_client_t; /**< The client handle. */ + +/** + * Connects to the mobileactivation service on the specified device. + * + * @param device The device to connect to. + * @param service The service descriptor returned by lockdownd_start_service. + * @param client Reference that will point to a newly allocated + * mobileactivation_client_t upon successful return. + * + * @return MOBILEACTIVATION_E_SUCCESS on success, + * MOBILEACTIVATION_E_INVALID_ARG when one of the parameters is invalid, + * or MOBILEACTIVATION_E_MUX_ERROR when the connection failed. + */ +mobileactivation_error_t mobileactivation_client_new(idevice_t device, lockdownd_service_descriptor_t service, mobileactivation_client_t *client); + +/** + * Starts a new mobileactivation service on the specified device and connects to it. + * + * @param device The device to connect to. + * @param client Pointer that will point to a newly allocated + * mobileactivation_client_t upon successful return. Must be freed using + * mobileactivation_client_free() after use. + * @param label The label to use for communication. Usually the program name. + * Pass NULL to disable sending the label in requests to lockdownd. + * + * @return MOBILEACTIVATION_E_SUCCESS on success, or an MOBILEACTIVATION_E_* + * error code otherwise. + */ +mobileactivation_error_t mobileactivation_client_start_service(idevice_t device, mobileactivation_client_t* client, const char* label); + +/** + * Disconnects a mobileactivation client from the device and frees up the + * mobileactivation client data. + * + * @param client The mobileactivation client to disconnect and free. + * + * @return MOBILEACTIVATION_E_SUCCESS on success, + * MOBILEACTIVATION_E_INVALID_ARG when one of client or client->parent + * is invalid, or MOBILEACTIVATION_E_UNKNOWN_ERROR when the was an + * error freeing the parent property_list_service client. + */ +mobileactivation_error_t mobileactivation_client_free(mobileactivation_client_t client); + + +/** + * Retrieves the device's activation state. + * + * @param client The mobileactivation client. + * @param state Pointer to a plist_t variable that will be set to the + * activation state reported by the mobileactivation service. The + * consumer is responsible for freeing the returned object using + * plist_free(). + * + * @return MOBILEACTIVATION_E_SUCCESS on success, or an MOBILEACTIVATION_E_* + * error code otherwise. + */ +mobileactivation_error_t mobileactivation_get_activation_state(mobileactivation_client_t client, plist_t *state); + +/** + * Retrieves the activation info required for device activation. + * + * @param client The mobileactivation client + * @param info Pointer to a plist_t variable that will be set to the + * activation info created by the mobileactivation service. The + * consumer is responsible for freeing the returned object using + * plist_free(). + * + * @return MOBILEACTIVATION_E_SUCCESS on success, or an MOBILEACTIVATION_E_* + * error code otherwise. + */ +mobileactivation_error_t mobileactivation_create_activation_info(mobileactivation_client_t client, plist_t *info); + +/** + * Activates the device with the given activation record. + * The activation record plist dictionary must be obtained using the + * activation protocol requesting from Apple's https webservice. + * + * @param client The mobileactivation client + * @param activation_record The activation record plist dictionary + * + * @return MOBILEACTIVATION_E_SUCCESS on success, or an MOBILEACTIVATION_E_* + * error code otherwise. + */ +mobileactivation_error_t mobileactivation_activate(mobileactivation_client_t client, plist_t activation_record); + +/** + * Deactivates the device. + * + * @param client The mobileactivation client + */ +mobileactivation_error_t mobileactivation_deactivate(mobileactivation_client_t client); + +#ifdef __cplusplus +} +#endif + +#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 \ heartbeat.c heartbeat.h\ debugserver.c debugserver.h\ webinspector.c webinspector.h\ + mobileactivation.c mobileactivation.h\ syslog_relay.c syslog_relay.h 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 @@ +/* + * mobileactivation.c + * com.apple.mobileactivationd service implementation. + * + * Copyright (c) 2016 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 + */ +#include +#include +#include "mobileactivation.h" +#include "property_list_service.h" +#include "common/debug.h" + +/** + * Convert a property_list_service_error_t value to a mobileactivation_error_t value. + * Used internally to get correct error codes. + * + * @param err An property_list_service_error_t error code + * + * @return A matching mobileactivation_error_t error code, + * MOBILEACTIVATION_E_UNKNOWN_ERROR otherwise. + */ +static mobileactivation_error_t mobileactivation_error(property_list_service_error_t err) +{ + switch (err) { + case PROPERTY_LIST_SERVICE_E_SUCCESS: + return MOBILEACTIVATION_E_SUCCESS; + case PROPERTY_LIST_SERVICE_E_INVALID_ARG: + return MOBILEACTIVATION_E_INVALID_ARG; + case PROPERTY_LIST_SERVICE_E_PLIST_ERROR: + return MOBILEACTIVATION_E_PLIST_ERROR; + case PROPERTY_LIST_SERVICE_E_MUX_ERROR: + return MOBILEACTIVATION_E_MUX_ERROR; + default: + break; + } + return MOBILEACTIVATION_E_UNKNOWN_ERROR; +} + +LIBIMOBILEDEVICE_API mobileactivation_error_t mobileactivation_client_new(idevice_t device, lockdownd_service_descriptor_t service, mobileactivation_client_t *client) +{ + if (!device || !service || service->port == 0 || !client || *client) { + return MOBILEACTIVATION_E_INVALID_ARG; + } + + property_list_service_client_t plistclient = NULL; + if (property_list_service_client_new(device, service, &plistclient) != PROPERTY_LIST_SERVICE_E_SUCCESS) { + return MOBILEACTIVATION_E_MUX_ERROR; + } + + /* create client object */ + mobileactivation_client_t client_loc = (mobileactivation_client_t) malloc(sizeof(struct mobileactivation_client_private)); + client_loc->parent = plistclient; + + /* all done, return success */ + *client = client_loc; + return MOBILEACTIVATION_E_SUCCESS; +} + +LIBIMOBILEDEVICE_API mobileactivation_error_t mobileactivation_client_start_service(idevice_t device, mobileactivation_client_t * client, const char* label) +{ + mobileactivation_error_t err = MOBILEACTIVATION_E_UNKNOWN_ERROR; + service_client_factory_start_service(device, MOBILEACTIVATION_SERVICE_NAME, (void**)client, label, SERVICE_CONSTRUCTOR(mobileactivation_client_new), &err); + return err; +} + +LIBIMOBILEDEVICE_API mobileactivation_error_t mobileactivation_client_free(mobileactivation_client_t client) +{ + if (!client) + return MOBILEACTIVATION_E_INVALID_ARG; + + if (property_list_service_client_free(client->parent) != PROPERTY_LIST_SERVICE_E_SUCCESS) { + return MOBILEACTIVATION_E_UNKNOWN_ERROR; + } + free(client); + return MOBILEACTIVATION_E_SUCCESS; +} + +static mobileactivation_error_t mobileactivation_check_result(plist_t dict, const char *command) +{ + mobileactivation_error_t ret = MOBILEACTIVATION_E_UNKNOWN_ERROR; + + if (!dict || plist_get_node_type(dict) != PLIST_DICT) { + return MOBILEACTIVATION_E_PLIST_ERROR; + } + + plist_t err_node = plist_dict_get_item(dict, "Error"); + if (!err_node) { + return MOBILEACTIVATION_E_SUCCESS; + } else { + char *errmsg = NULL; + plist_get_string_val(err_node, &errmsg); + debug_info("ERROR: %s: %s", command, errmsg); + free(errmsg); + ret = MOBILEACTIVATION_E_REQUEST_FAILED; + } + return ret; +} + +static mobileactivation_error_t mobileactivation_send_command(mobileactivation_client_t client, const char* command, plist_t value, plist_t *result) +{ + if (!client || !command || !result) + return MOBILEACTIVATION_E_INVALID_ARG; + + mobileactivation_error_t ret = MOBILEACTIVATION_E_UNKNOWN_ERROR; + *result = NULL; + + plist_t dict = plist_new_dict(); + plist_dict_set_item(dict, "Command", plist_new_string(command)); + if (value) { + plist_dict_set_item(dict, "Value", plist_copy(value)); + } + + ret = mobileactivation_error(property_list_service_send_binary_plist(client->parent, dict)); + plist_free(dict); + dict = NULL; + + ret = mobileactivation_error(property_list_service_receive_plist(client->parent, &dict)); + if (!dict) { + debug_info("ERROR: Did not get reply for %s command", command); + return MOBILEACTIVATION_E_PLIST_ERROR; + } + + *result = dict; + return mobileactivation_check_result(dict, command); +} + +LIBIMOBILEDEVICE_API mobileactivation_error_t mobileactivation_get_activation_state(mobileactivation_client_t client, plist_t *state) +{ + if (!client || !state) + return MOBILEACTIVATION_E_INVALID_ARG; + + plist_t result = NULL; + mobileactivation_error_t ret = mobileactivation_send_command(client, "GetActivationStateRequest", NULL, &result); + if (ret == MOBILEACTIVATION_E_SUCCESS) { + plist_t node = plist_dict_get_item(result, "Value"); + if (!node) { + debug_info("ERROR: GetActivationStateRequest command returned success but has no value in reply"); + ret = MOBILEACTIVATION_E_UNKNOWN_ERROR; + } else { + *state = plist_copy(node); + } + } + plist_free(result); + result = NULL; + + return ret; +} + +LIBIMOBILEDEVICE_API mobileactivation_error_t mobileactivation_create_activation_info(mobileactivation_client_t client, plist_t *info) +{ + if (!client || !info) + return MOBILEACTIVATION_E_INVALID_ARG; + + plist_t result = NULL; + mobileactivation_error_t ret = mobileactivation_send_command(client, "CreateActivationInfoRequest", NULL, &result); + if (ret == MOBILEACTIVATION_E_SUCCESS) { + plist_t node = plist_dict_get_item(result, "Value"); + if (!node) { + debug_info("ERROR: CreateActivationInfoRequest command returned success but has no value in reply"); + ret = MOBILEACTIVATION_E_UNKNOWN_ERROR; + } else { + *info = plist_copy(node); + } + } + plist_free(result); + result = NULL; + + return ret; +} + +LIBIMOBILEDEVICE_API mobileactivation_error_t mobileactivation_activate(mobileactivation_client_t client, plist_t activation_record) +{ + if (!client || !activation_record) + return MOBILEACTIVATION_E_INVALID_ARG; + + plist_t result = NULL; + mobileactivation_error_t ret = mobileactivation_send_command(client, "HandleActivationInfoRequest", activation_record, &result); + plist_free(result); + result = NULL; + + return ret; +} + +LIBIMOBILEDEVICE_API mobileactivation_error_t mobileactivation_deactivate(mobileactivation_client_t client) +{ + if (!client) + return MOBILEACTIVATION_E_INVALID_ARG; + + plist_t result = NULL; + mobileactivation_error_t ret = mobileactivation_send_command(client, "DeactivateRequest", NULL, &result); + plist_free(result); + result = NULL; + + return ret; +} 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 @@ +/* + * mobileactivation.h + * com.apple.mobileactivationd service header file. + * + * Copyright (c) 2016 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 + */ + +#ifndef __MOBILEACTIVATION_H +#define __MOBILEACTIVATION_H + +#include "libimobiledevice/mobileactivation.h" +#include "property_list_service.h" + +struct mobileactivation_client_private { + property_list_service_client_t parent; +}; + +#endif -- cgit v1.1-32-gdbae