Skip to content

Commit

Permalink
Cache support!
Browse files Browse the repository at this point in the history
  • Loading branch information
boxingsquirrel committed Aug 11, 2010
1 parent eee38f3 commit 5cc51bb
Show file tree
Hide file tree
Showing 11 changed files with 1,068 additions and 124 deletions.
675 changes: 675 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
all:
make -C src
make -C src

clean:
rm src/ideviceactivate
20 changes: 20 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
ideviceactivate is a small utility to activate iDevices from the comfort of your GNU/Linux box.

===Credits===

Almost all of the code, with the major exception of the caching code, was written by p0sixninja.
The caching code and the documentation was written by boxingsquirrel.

===Building===

Fetch your dependencies. Because these vary from distro to distro, I won't list specific packages here, that said you need developement packages for: libimobiledevice, libplist, usbmuxd, libgthread2, librt, gnutls, libtasn1, libxml2, libglib2, and libcurl.

Then:
git clone git://github.com/boxingsquirrel/ideviceactivate.git
cd ideviceactivate
make
sudo make install

===Running===

For straight-up activation with nothing fancy: ideviceactivate
2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ CFLAGS := -g -pthread -I/usr/local/include -I/usr/include/glib-2.0 -I/usr/lib/gl
LDFLAGS := -pthread -L/usr/local/lib -limobiledevice -lplist -lusbmuxd -lgthread-2.0 -lrt -lgnutls -ltasn1 -lxml2 -lglib-2.0 -lcurl

all:
gcc -o ideviceactivate ideviceactivate.c activate.c $(CFLAGS) $(LDFLAGS)
gcc -o ideviceactivate ideviceactivate.c activate.c cache.c util.c $(CFLAGS) $(LDFLAGS)
127 changes: 71 additions & 56 deletions src/activate.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,21 @@
#include <curl/curl.h>
#include <plist/plist.h>
#include <libimobiledevice/lockdown.h>
#include "cache.h"

typedef struct {
int length;
char* content;
} activate_response;

typedef struct {
char* imei;
char* imsi;
char* iccid;
char* serial_number;
char* activation_info;
} activate_info;

size_t activate_write_callback(char* data, size_t size, size_t nmemb, activate_response* response) {
size_t total = size * nmemb;
if (total != 0) {
Expand All @@ -43,68 +52,57 @@ size_t activate_write_callback(char* data, size_t size, size_t nmemb, activate_r
return total;
}

void deactivate_device(lockdownd_client_t client)
{
printf("Deactivating device... ");
int client_error = lockdownd_deactivate(client);
if (client_error == LOCKDOWN_E_SUCCESS) {
printf("SUCCESS\n");
} else {
fprintf(stderr, "ERROR\nUnable to deactivate device: %d\n", client_error);
}
}

int activate_fetch_record(lockdownd_client_t client, plist_t* record) {
int size = 0;
char* request = NULL;
struct curl_httppost* post = NULL;
struct curl_httppost* last = NULL;
activate_response* response = NULL;

char* imei = NULL;
char* imsi = NULL;
char* iccid = NULL;
char* serial_number = NULL;
char* activation_info = NULL;

plist_t imei_node = NULL;
plist_t imsi_node = NULL;
plist_t iccid_node = NULL;
plist_t serial_number_node = NULL;
plist_t activation_info_node = NULL;

char* activation_info;

activate_info* info;

char* device_class = NULL;
plist_t device_class_node = NULL;
lockdownd_get_value(client, NULL, "DeviceClass", &device_class_node);
if (!device_class_node || plist_get_node_type(device_class_node) != PLIST_STRING) {
fprintf(stderr, "Unable to get DeviceClass from lockdownd\n");
return -1;
}
plist_get_string_val(device_class_node, &device_class);
plist_free(device_class_node);
device_class=(char*)lockdownd_get_string_value(client, "DeviceClass");

if (!strcmp(device_class, "iPhone")) {
lockdownd_get_value(client, NULL, "IntegratedCircuitCardIdentity", &iccid_node);
if (!iccid_node || plist_get_node_type(iccid_node) != PLIST_STRING) {
fprintf(stderr, "Unable to get ICCID from lockdownd\n");
return -1;
}
plist_get_string_val(iccid_node, &iccid);
plist_free(iccid_node);
if (use_cache!=1)
{
info->iccid=(char*)lockdownd_get_string_value(client, "IntegratedCircuitCardIdentity");

lockdownd_get_value(client, NULL, "InternationalMobileEquipmentIdentity", &imei_node);
if (!imei_node || plist_get_node_type(imei_node) != PLIST_STRING) {
fprintf(stderr, "Unable to get IMEI from lockdownd\n");
return -1;
info->imei=(char*)lockdownd_get_string_value(client, "InternationalMobileEquipmentIdentity");

info->imsi=(char*)lockdownd_get_string_value(client, "InternationalMobileSubscriberIdentity");
}
plist_get_string_val(imei_node, &imei);
plist_free(imei_node);

lockdownd_get_value(client, NULL, "InternationalMobileSubscriberIdentity", &imsi_node);
if (!imsi_node || plist_get_node_type(imsi_node) != PLIST_STRING) {
fprintf(stderr, "Unable to get IMSI from lockdownd\n");
return -1;
else {
info->iccid=get_from_cache("ICCID");
info->imei=get_from_cache("IMEI");
info->imsi=get_from_cache("IMSI");
}
plist_get_string_val(imsi_node, &imsi);
plist_free(imsi_node);
}

lockdownd_get_value(client, NULL, "SerialNumber", &serial_number_node);
if (!serial_number_node || plist_get_node_type(serial_number_node) != PLIST_STRING) {
fprintf(stderr, "Unable to get SerialNumber from lockdownd\n");
return -1;
if (use_cache!=1)
{
info->serial_number=(char*)lockdownd_get_string_value(client, "SerialNumber");
}
else {
info->serial_number=get_from_cache("SerialNumber");
}
plist_get_string_val(serial_number_node, &serial_number);
plist_free(serial_number_node);

lockdownd_get_value(client, NULL, "ActivationInfo", &activation_info_node);
int type = plist_get_node_type(activation_info_node);
Expand Down Expand Up @@ -149,29 +147,46 @@ int activate_fetch_record(lockdownd_client_t client, plist_t* record) {

curl_formadd(&post, &last, CURLFORM_COPYNAME, "machineName", CURLFORM_COPYCONTENTS, "linux", CURLFORM_END);
curl_formadd(&post, &last, CURLFORM_COPYNAME, "InStoreActivation", CURLFORM_COPYCONTENTS, "false", CURLFORM_END);
if (imei != NULL) {
curl_formadd(&post, &last, CURLFORM_COPYNAME, "IMEI", CURLFORM_COPYCONTENTS, imei, CURLFORM_END);
free(imei);
if (info->imei != NULL) {
curl_formadd(&post, &last, CURLFORM_COPYNAME, "IMEI", CURLFORM_COPYCONTENTS, info->imei, CURLFORM_END);
cache("IMEI", (const char *)info->imei);
//free(info->imei);
}
else {
cache("IMEI", "");
}

if (imsi != NULL) {
curl_formadd(&post, &last, CURLFORM_COPYNAME, "IMSI", CURLFORM_COPYCONTENTS, imsi, CURLFORM_END);
free(imsi);
if (info->imsi != NULL) {
curl_formadd(&post, &last, CURLFORM_COPYNAME, "IMSI", CURLFORM_COPYCONTENTS, info->imsi, CURLFORM_END);
cache("IMSI", (const char *)info->imsi);
//free(imsi);
}
else {
cache("IMSI", "");
}

if (iccid != NULL) {
curl_formadd(&post, &last, CURLFORM_COPYNAME, "ICCID", CURLFORM_COPYCONTENTS, iccid, CURLFORM_END);
free(iccid);
if (info->iccid != NULL) {
curl_formadd(&post, &last, CURLFORM_COPYNAME, "ICCID", CURLFORM_COPYCONTENTS, info->iccid, CURLFORM_END);
cache("ICCID", (const char *)info->iccid);
//free(info->iccid);
}
else {
cache("ICCID", "");
}

if (serial_number != NULL) {
curl_formadd(&post, &last, CURLFORM_COPYNAME, "AppleSerialNumber", CURLFORM_COPYCONTENTS, serial_number, CURLFORM_END);
free(serial_number);
if (info->serial_number != NULL) {
curl_formadd(&post, &last, CURLFORM_COPYNAME, "AppleSerialNumber", CURLFORM_COPYCONTENTS, info->serial_number, CURLFORM_END);
cache("SerialNumber", (const char *)info->serial_number);
//free(info->serial_number);
}
else {
cache("SeralNumber", "");
}

if (activation_info != NULL) {
curl_formadd(&post, &last, CURLFORM_COPYNAME, "activation-info", CURLFORM_COPYCONTENTS, activation_info, CURLFORM_END);
free(activation_info);
cache("ActivationInfo", activation_info);
//free(activation_info);
}

struct curl_slist* header = NULL;
Expand Down
1 change: 1 addition & 0 deletions src/activate.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@
#include <libimobiledevice/lockdown.h>

int activate_fetch_record(lockdownd_client_t lockdown, plist_t* record);
extern void deactivate_device(lockdownd_client_t client);

#endif
136 changes: 136 additions & 0 deletions src/cache.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
* cache.c
* Load data from/add data to the activation info cache.
*
* Copyright (c) 2010 boxingsquirrel. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <plist/plist.h>
#include <libimobiledevice/lockdown.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cache.h"

int write_file(const char *filename, char data[BUFSIZE])
{
FILE *f=fopen(filename, "w");

if (f==NULL)
{
printf("ERROR: Could not open %s for writing\n", filename);
fclose(f);
return -1;
}

else {
fwrite(data, strlen(data), 1, f);
fclose(f);

return 0;
}
}

int read_file(const char *filename, char data[BUFSIZE])
{
FILE *f=fopen(filename, "r");

if (f==NULL)
{
printf("ERROR: Could not open %s for reading\n", filename);
fclose(f);
return -1;
}

else {
uint32_t read=fread(data, 1, BUFSIZE, f);

while (read>0)
{
read=fread(data, 1, BUFSIZE, f);
printf(".");
}

printf("\nINFO: Read sucessfully from %s\n", filename);

return 0;
}
}

int cache(const char *fname, const char *what)
{
if (backup_to_cache==1)
{
char data[BUFSIZE];
snprintf(data, BUFSIZE, "%s", what);

char f_name[512];
snprintf(f_name, BUFSIZE, "%s/%s", cachedir, fname);

write_file((const char *)f_name, data);
return 1;
}

else {
return -1;
}
}

int cache_plist(const char *fname, plist_t plist)
{
if (backup_to_cache==1)
{
uint32_t len=0;
char **xml=NULL;

plist_to_xml(plist, &xml, &len);

cache(fname, (const char *)xml);
return 1;
}

else {
return -1;
}
}

char* get_from_cache(const char *what)
{
char fname[512];
snprintf(fname, 512, "%s/%s", cachedir, what);

char *d[BUFSIZE];
read_file((const char *)fname, &d);

return (char *)d;
}

/* Just prints a little notice about what caching actually does... */
void cache_warning()
{
printf("The process of creating a cache is simple: perform a legit activation, storing all the required data. That way, you can borrow (or, I guess, steal (don't do that, though)) a sim for the carrier your iPhone is locked to, and be able to reactivate without having to get that sim back.\n\nThis data is stored in a folder where you want it (hence the folder passed with -c/-r). It does not get sent to me (boxingsquirrel), p0sixninja, or anyone else. Plus, we really have better things to do than look at your activation data.\n\nThis really isn't needed for iPod Touches or Wi-Fi only iPads (and I don't know if 3G iPad users need this, but be safe and do it).\n\nPress any key to continue or CONTROL-C to abort...\n\n");

getchar();
}

/* Validates the cache to make sure it really is the cache for the connected device... */
int check_cache(lockdownd_client_t c)
{
char* uuid_from_cache=get_from_cache("UUID");
char* uuid_from_device=(char *)lockdownd_get_string_value(c, "UniqueDeviceID");

return strcmp(uuid_from_cache, uuid_from_device);
}
16 changes: 16 additions & 0 deletions src/cache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <plist/plist.h>

#define BUFSIZE 0x200000

extern char* cachedir;
extern int use_cache;
extern int backup_to_cache;

extern int write_file(const char *filename, char data[BUFSIZE]);
extern int cache(const char *fname, const char *what);
extern int cache_plist(const char *fname, plist_t plist);
extern char* get_from_cache(const char *what);

extern void cache_warning();

extern int check_cache(lockdownd_client_t c);
Loading

0 comments on commit 5cc51bb

Please sign in to comment.