Commit 3fdd24ae authored by Jonathan Beck's avatar Jonathan Beck

Fork libiphone and remove anything non plist specific.

Update library and make related files acordingly .
parent 3d8ba053
Zach C. Zach C.
Jonathan Beck Jonathan Beck
Matt Colyer
Martin Aumueller
Christophe Fergeau
Martin S.
Paul Sladen
Patrick Walton
AUTOMAKE_OPTIONS = foreign AUTOMAKE_OPTIONS = foreign
SUBDIRS = src include fdi $(DEV_SUB) SUBDIRS = src include
pkgconfigdir = $(libdir)/pkgconfig pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = libiphone-1.0.pc pkgconfig_DATA = libplist-1.0.pc
doc: doc:
doxygen doxygen.cfg doxygen doxygen.cfg
......
...@@ -2,13 +2,9 @@ INSTALLATION ...@@ -2,13 +2,9 @@ INSTALLATION
================================================================================ ================================================================================
For: For:
Apple iPhone/iPod Touch 1.0/1.1/1.1.1/1.2/1.3/2.0+ Apple Binary and XML Property Lists
+ iPod USB cable
You must have: You must have:
libgnutls-dev
libusb-dev
libfuse-dev (and the associated kernel modules)
libglib2.0-dev libglib2.0-dev
libxml2-dev libxml2-dev
make make
...@@ -22,94 +18,10 @@ To compile run: ...@@ -22,94 +18,10 @@ To compile run:
./configure ./configure
make make
sudo make install # (if you want to install it into your system directories) sudo make install # (if you want to install it into your system directories)
libiphone-initconf # (as the user you intend to user the library)
On Ubuntu/Debian, you can do: On Ubuntu/Debian, you can do:
sudo apt-get install build-essential automake autoconf \ sudo apt-get install build-essential automake autoconf \
libgnutls-dev libusb-dev libfuse-dev libglib2.0-dev libxml2-dev \ libglib2.0-dev libxml2-dev
libreadline5-dev
USAGE
================================================================================
Now comes the fun bit!
== Generating keys ==
IMPORTANT: Before using the library you must run "libiphone-initconf"
as your own user (not root). It will generate keys and a host id for your
system to be able to communicate with 'lockdownd' on the iPhone.
It will probably take 5-10 minutes, but thankfully only needs to be
run _once_. It MUST be run otherwise communication will not work:
libiphone-initconf
The generated keys are saved in '~/.config/libiphone/' in your home directory.
== Tools ==
There are currently two more executables 'ifuse' and 'iphoneclient',
both located in src/.
=== iFuse ===
This is probably what you're after; this mounts a view of your
iPhone/iPod Touch's filesystem over the USB interface using the native
Apple protocol (AFC/"com.apple.afc").
ifuse is a Fuse filesystem which allows you to mount your iPhone to a directory
like this:
./src/ifuse <mountpoint> -s
To unmount:
umount <mountpoint>
(nb: '-s' is to force single-threaded mode, as ifuse maybe unstable without it).
Eg:
mkdir ~/iphone
ifuse ~/iphone -s
ls -l ~/iphone
...
umount ~/iphone
Currently ifuse (via the AFC protocol) only gives access to the
'/var/root/Media/' chroot on the iPhone (containing music/pictures).
If you have a device that has been jailedbreaked then an additional
("com.apple.afc2") service will have been installed, without the chroot.
On jailbroken devices only, you can do:
ifuse ~/iphone --root -s
And this will mount a full view of the iPhone's filesystem.
==== Setting up FUSE ====
Note that on some systems, you may have to load the 'fuse' kernel
module first and to ensure that you are a member of the 'fuse' group:
sudo modprobe fuse
sudo adduser $USER fuse
You can check your membership of the 'fuse' group with:
id | grep fuse && echo yes! || echo not yet...
If you have just added yourself, you will need to logout and log back
in for the group change to become visible.
=== iphoneclient ===
'iphoneclient' is a basic commandline interface for testing, it just
runs a few various test operations such as attempting to view/create a
test file in the iPhone, but is mainly a developer tool.
== Who/what/where? == == Who/what/where? ==
...@@ -118,10 +30,10 @@ wiki: ...@@ -118,10 +30,10 @@ wiki:
http://matt.colyer.name/projects/iphone-linux/index.php?title=Main_Page http://matt.colyer.name/projects/iphone-linux/index.php?title=Main_Page
code: code:
git clone http://git.matt.colyer.name/2008/libiphone/ git clone git://github.com/JonathanBeck/libplist.git
mailing list: mailing list:
http://lists.mattcolyer.com/listinfo.cgi/iphone-linux-dev-mattcolyer.com http://lists.mattcolyer.com/listinfo.cgi/iphone-linux-dev-mattcolyer.com
updated: updated:
2008-09-02 2008-12-12
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
# Process this file with autoconf to produce a configure script. # Process this file with autoconf to produce a configure script.
AC_PREREQ(2.61) AC_PREREQ(2.61)
AC_INIT(libiphone, 0.1.0, nospam@nowhere.com) AC_INIT(libplist, 0.1.0, nospam@nowhere.com)
AM_INIT_AUTOMAKE(libiphone, 0.1.0) AM_INIT_AUTOMAKE(libplist, 0.1.0)
AC_CONFIG_SRCDIR([src/]) AC_CONFIG_SRCDIR([src/])
AC_CONFIG_HEADER([config.h]) AC_CONFIG_HEADER([config.h])
...@@ -15,15 +15,11 @@ AM_PROG_CC_C_O ...@@ -15,15 +15,11 @@ AM_PROG_CC_C_O
# Checks for libraries. # Checks for libraries.
PKG_CHECK_MODULES(libxml2, libxml-2.0 >= 2.6.30) PKG_CHECK_MODULES(libxml2, libxml-2.0 >= 2.6.30)
PKG_CHECK_MODULES(libusb, libusb >= 0.1.12)
PKG_CHECK_MODULES(libglib2, glib-2.0 >= 2.14.1) PKG_CHECK_MODULES(libglib2, glib-2.0 >= 2.14.1)
PKG_CHECK_MODULES(libgthread2, gthread-2.0 >= 2.14.1)
PKG_CHECK_MODULES(libgnutls, gnutls >= 1.6.3 gnutls <= 2.5.0 )
PKG_CHECK_MODULES(libtasn1, libtasn1 >= 1.1)
# Checks for header files. # Checks for header files.
AC_HEADER_STDC AC_HEADER_STDC
AC_CHECK_HEADERS([arpa/inet.h stdint.h stdlib.h string.h]) AC_CHECK_HEADERS([stdint.h stdlib.h string.h])
# Checks for typedefs, structures, and compiler characteristics. # Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST AC_C_CONST
...@@ -31,6 +27,7 @@ AC_TYPE_SIZE_T ...@@ -31,6 +27,7 @@ AC_TYPE_SIZE_T
AC_TYPE_SSIZE_T AC_TYPE_SSIZE_T
AC_TYPE_UINT16_T AC_TYPE_UINT16_T
AC_TYPE_UINT32_T AC_TYPE_UINT32_T
AC_TYPE_UINT64_T
AC_TYPE_UINT8_T AC_TYPE_UINT8_T
# Checks for library functions. # Checks for library functions.
...@@ -38,18 +35,6 @@ AC_FUNC_MALLOC ...@@ -38,18 +35,6 @@ AC_FUNC_MALLOC
AC_FUNC_REALLOC AC_FUNC_REALLOC
AC_CHECK_FUNCS([strcasecmp strdup strerror strndup]) AC_CHECK_FUNCS([strcasecmp strdup strerror strndup])
AC_ARG_ENABLE([dev-tools],
[AS_HELP_STRING([--enable-dev-tools],
[build development helper tools (default is no)])],
[build_dev_tools=true],
[build_dev_tools=false])
if test "$build_dev_tools" = true; then
DEV_SUB=dev
else
DEV_SUB=
fi
AC_SUBST([DEV_SUB])
AC_ARG_ENABLE([debug-code], AC_ARG_ENABLE([debug-code],
[AS_HELP_STRING([--disable-debug-code], [AS_HELP_STRING([--disable-debug-code],
[disable debug message reporting in library (default is yes)])], [disable debug message reporting in library (default is yes)])],
...@@ -59,4 +44,4 @@ if test "$no_debug_code" = true; then ...@@ -59,4 +44,4 @@ if test "$no_debug_code" = true; then
AC_DEFINE(STRIP_DEBUG_CODE,1,[Strip debug reporting code]) AC_DEFINE(STRIP_DEBUG_CODE,1,[Strip debug reporting code])
fi fi
AC_OUTPUT(Makefile src/Makefile include/Makefile fdi/Makefile dev/Makefile libiphone-1.0.pc) AC_OUTPUT(Makefile src/Makefile include/Makefile libplist-1.0.pc)
INCLUDES = -I$(top_srcdir)/include
AM_CFLAGS = $(libxml2_CFLAGS) $(libusb_CFLAGS) $(libglib2_CFLAGS) $(libgnutls_CFLAGS) $(libtasn1_CFLAGS) $(libgthread2_CFLAGS) -g
AM_LDFLAGS = $(libxml2_LIBS) $(libusb_LIBS) $(libglib2_LIBS) $(libgnutls_LIBS) $(libtasn1_LIBS) $(libgthread2_LIBS)
bin_PROGRAMS = iphoneclient lckd-client afccheck plutil
iphoneclient_SOURCES = main.c
iphoneclient_LDADD = ../src/libiphone.la
lckd_client_SOURCES = lckdclient.c
lckd_client_CFLAGS = $(AM_CFLAGS)
lckd_client_LDFLAGS = -lreadline $(AM_LDFLAGS)
lckd_client_LDADD = ../src/libiphone.la
afccheck_SOURCES = afccheck.c
afccheck_CFLAGS = $(AM_CFLAGS)
afccheck_LDFLAGS = $(AM_LDFLAGS)
afccheck_LDADD = ../src/libiphone.la
plutil_SOURCES = plutil.c
plutil_CFLAGS = $(AM_CFLAGS)
plutil_LDFLAGS = $(AM_LDFLAGS)
plutil_LDADD = ../src/libiphone.la
/*
* afccheck.c
* creates threads and check communication through AFC is done rigth
*
* Copyright (c) 2008 Jonathan Beck 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 <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <glib.h>
#include <libiphone/libiphone.h>
#define BUFFER_SIZE 20000
#define NB_THREADS 10
typedef struct {
iphone_afc_client_t afc;
int id;
} param;
void check_afc(gpointer data)
{
//prepare a buffer
int buffersize = BUFFER_SIZE * sizeof(int);
int *buf = (int *) malloc(buffersize);
int *buf2 = (int *) malloc(buffersize);
int bytes = 0;
//fill buffer
int i = 0;
for (i = 0; i < BUFFER_SIZE; i++) {
buf[i] = ((param *) data)->id * i;
}
//now writes buffer on iphone
iphone_afc_file_t file = NULL;
char path[50];
sprintf(path, "/Buf%i", ((param *) data)->id);
iphone_afc_open_file(((param *) data)->afc, path, IPHONE_AFC_FILE_WRITE, &file);
iphone_afc_write_file(((param *) data)->afc, file, (char *) buf, buffersize, &bytes);
iphone_afc_close_file(((param *) data)->afc, file);
file = NULL;
if (bytes != buffersize)
printf("Write operation failed\n");
//now read it
bytes = 0;
iphone_afc_open_file(((param *) data)->afc, path, IPHONE_AFC_FILE_READ, &file);
iphone_afc_read_file(((param *) data)->afc, file, (char *) buf2, buffersize, &bytes);
iphone_afc_close_file(((param *) data)->afc, file);
if (bytes != buffersize)
printf("Read operation failed\n");
//compare buffers
for (i = 0; i < BUFFER_SIZE; i++) {
if (buf[i] != buf2[i]) {
printf("Buffers are differents, stream corrupted\n");
break;
}
}
//cleanup
iphone_afc_delete_file(((param *) data)->afc, path);
g_thread_exit(0);
}
int main(int argc, char *argv[])
{
iphone_lckd_client_t control = NULL;
iphone_device_t phone = NULL;
GError *err;
int port = 0;
iphone_afc_client_t afc = NULL;
if (IPHONE_E_SUCCESS != iphone_get_device(&phone)) {
printf("No iPhone found, is it plugged in?\n");
return 1;
}
if (IPHONE_E_SUCCESS != iphone_lckd_new_client(phone, &control)) {
iphone_free_device(phone);
return 1;
}
if (IPHONE_E_SUCCESS == iphone_lckd_start_service(control, "com.apple.afc", &port) && !port) {
iphone_lckd_free_client(control);
iphone_free_device(phone);
fprintf(stderr, "Something went wrong when starting AFC.");
return 1;
}
iphone_afc_new_client(phone, 3432, port, &afc);
//makes sure thread environment is available
if (!g_thread_supported())
g_thread_init(NULL);
GThread *threads[NB_THREADS];
param data[NB_THREADS];
int i = 0;
for (i = 0; i < NB_THREADS; i++) {
data[i].afc = afc;
data[i].id = i + 1;
threads[i] = g_thread_create((GThreadFunc) check_afc, data + i, TRUE, &err);
}
for (i = 0; i < NB_THREADS; i++) {
g_thread_join(threads[i]);
}
iphone_lckd_free_client(control);
iphone_free_device(phone);
return 0;
}
/*
* lckdclient.c
* Rudimentary command line interface to the Lockdown protocol
*
* Copyright (c) 2008 Jonathan Beck 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <libiphone/libiphone.h>
int main(int argc, char *argv[])
{
int bytes = 0, port = 0, i = 0;
iphone_lckd_client_t control = NULL;
iphone_device_t phone = NULL;
iphone_set_debug(1);
if (IPHONE_E_SUCCESS != iphone_get_device(&phone)) {
printf("No iPhone found, is it plugged in?\n");
return -1;
}
if (IPHONE_E_SUCCESS != iphone_lckd_new_client(phone, &control)) {
iphone_free_device(phone);
return -1;
}
char *uid = NULL;
if (IPHONE_E_SUCCESS == lockdownd_get_device_uid(control, &uid)) {
printf("DeviceUniqueID : %s\n", uid);
free(uid);
}
using_history();
int loop = TRUE;
while (loop) {
char *cmd = readline("> ");
if (cmd) {
gchar **args = g_strsplit(cmd, " ", 0);
int len = 0;
if (args) {
while (*(args + len)) {
g_strstrip(*(args + len));
len++;
}
}
if (len > 0) {
add_history(cmd);
if (!strcmp(*args, "quit"))
loop = FALSE;
if (!strcmp(*args, "get") && len == 3) {
char *value = NULL;
if (IPHONE_E_SUCCESS == lockdownd_generic_get_value(control, *(args + 1), *(args + 2), &value))
printf("Success : value = %s\n", value);
else
printf("Error\n");
}
if (!strcmp(*args, "start") && len == 2) {
int port = 0;
iphone_lckd_start_service(control, *(args + 1), &port);
printf("%i\n", port);
}
}
g_strfreev(args);
}
free(cmd);
cmd = NULL;
}
clear_history();
iphone_lckd_free_client(control);
iphone_free_device(phone);
return 0;
}
/*
* main.c
* Rudimentary interface to the iPhone
*
* Copyright (c) 2008 Zach C. 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 <stdio.h>
#include <string.h>
#include <errno.h>
#include <usb.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libiphone/libiphone.h>
int main(int argc, char *argv[])
{
int bytes = 0, port = 0, i = 0;
iphone_lckd_client_t control = NULL;
iphone_device_t phone = NULL;
if (argc > 1 && !strcasecmp(argv[1], "--debug")) {
iphone_set_debug(1);
} else {
iphone_set_debug(0);
}
if (IPHONE_E_SUCCESS != iphone_get_device(&phone)) {
printf("No iPhone found, is it plugged in?\n");
return -1;
}
if (IPHONE_E_SUCCESS != iphone_lckd_new_client(phone, &control)) {
iphone_free_device(phone);
return -1;
}
char *uid = NULL;
if (IPHONE_E_SUCCESS == lockdownd_get_device_uid(control, &uid)) {
printf("DeviceUniqueID : %s\n", uid);
free(uid);
}
iphone_lckd_start_service(control, "com.apple.afc", &port);
if (port) {
iphone_afc_client_t afc = NULL;
iphone_afc_new_client(phone, 3432, port, &afc);
if (afc) {
char **dirs = NULL;
iphone_afc_get_dir_list(afc, "/eafaedf", &dirs);
if (!dirs)
iphone_afc_get_dir_list(afc, "/", &dirs);
printf("Directory time.\n");
for (i = 0; dirs[i]; i++) {
printf("/%s\n", dirs[i]);
}
g_strfreev(dirs);
iphone_afc_get_devinfo(afc, &dirs);
if (dirs) {
for (i = 0; dirs[i]; i += 2) {
printf("%s: %s\n", dirs[i], dirs[i + 1]);
}
}
g_strfreev(dirs);
iphone_afc_file_t my_file = NULL;
struct stat stbuf;
iphone_afc_get_file_attr(afc, "/iTunesOnTheGoPlaylist.plist", &stbuf);
if (IPHONE_E_SUCCESS ==
iphone_afc_open_file(afc, "/iTunesOnTheGoPlaylist.plist", IPHONE_AFC_FILE_READ, &my_file) && my_file) {
printf("A file size: %i\n", (int) stbuf.st_size);
char *file_data = (char *) malloc(sizeof(char) * stbuf.st_size);
iphone_afc_read_file(afc, my_file, file_data, stbuf.st_size, &bytes);
if (bytes >= 0) {
printf("The file's data:\n");
fwrite(file_data, 1, bytes, stdout);
}
printf("\nClosing my file.\n");
iphone_afc_close_file(afc, my_file);
free(file_data);
} else
printf("couldn't open a file\n");
iphone_afc_open_file(afc, "/readme.libiphone.fx", IPHONE_AFC_FILE_WRITE, &my_file);
if (my_file) {
char *outdatafile = strdup("this is a bitchin text file\n");
iphone_afc_write_file(afc, my_file, outdatafile, strlen(outdatafile), &bytes);
free(outdatafile);
if (bytes > 0)
printf("Wrote a surprise. ;)\n");
else
printf("I wanted to write a surprise, but... :(\n");
iphone_afc_close_file(afc, my_file);
}
printf("Deleting a file...\n");
bytes = iphone_afc_delete_file(afc, "/delme");
if (bytes)
printf("Success.\n");
else
printf("Failure. (expected unless you have a /delme file on your phone)\n");
printf("Renaming a file...\n");
bytes = iphone_afc_rename_file(afc, "/renme", "/renme2");
if (bytes > 0)
printf("Success.\n");
else
printf("Failure. (expected unless you have a /renme file on your phone)\n");
printf("Seek & read\n");
iphone_afc_open_file(afc, "/readme.libiphone.fx", IPHONE_AFC_FILE_READ, &my_file);
if (IPHONE_E_SUCCESS != iphone_afc_seek_file(afc, my_file, 5))
printf("WARN: SEEK DID NOT WORK\n");
char *threeletterword = (char *) malloc(sizeof(char) * 5);
iphone_afc_read_file(afc, my_file, threeletterword, 3, &bytes);
threeletterword[3] = '\0';
if (bytes > 0)
printf("Result: %s\n", threeletterword);
else
printf("Couldn't read!\n");
free(threeletterword);
iphone_afc_close_file(afc, my_file);
}
iphone_afc_free_client(afc);
} else {
printf("Start service failure.\n");
}
printf("All done.\n");
iphone_lckd_free_client(control);
iphone_free_device(phone);
return 0;
}
/*
* main.c for plistutil
* right now just prints debug shit
*/
#include "../src/plist.h"
#include "plutil.h"
#include <glib.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
struct stat *filestats = (struct stat *) malloc(sizeof(struct stat));
Options *options = parse_arguments(argc, argv);
if (!options) {
print_usage();
return 0;
}
iphone_set_debug(options->debug);
//read input file
FILE *iplist = fopen(options->in_file, "r");
if (!iplist)
return 1;
stat(options->in_file, filestats);
char *plist_entire = (char *) malloc(sizeof(char) * (filestats->st_size + 1));
fread(plist_entire, sizeof(char), filestats->st_size, iplist);
fclose(iplist);
//convert one format to another
plist_t root_node = NULL;
char *plist_out = NULL;
int size = 0;
if (memcmp(plist_entire, "bplist00", 8) == 0) {
bin_to_plist(plist_entire, filestats->st_size, &root_node);
plist_to_xml(root_node, &plist_out, &size);
} else {
xml_to_plist(plist_entire, filestats->st_size, &root_node);
plist_to_bin(root_node, &plist_out, &size);
}
if (plist_out) {
if (options->out_file != NULL) {
FILE *oplist = fopen(options->out_file, "wb");
if (!oplist)
return 1;
fwrite(plist_out, size, sizeof(char), oplist);
fclose(oplist);
}
//if no output file specified, write to stdout
else
fwrite(plist_out, size, sizeof(char), stdout);
} else
printf("ERROR\n");
return 0;
}
Options *parse_arguments(int argc, char *argv[])
{
int i = 0;
Options *options = (Options *) malloc(sizeof(Options));
memset(options, 0, sizeof(Options));
for (i = 1; i < argc; i++) {
if (!strcmp(argv[i], "--infile") || !strcmp(argv[i], "-i")) {
if ((i + 1) == argc) {
free(options);
return NULL;
}
options->in_file = argv[i + 1];
i++;
continue;
}
if (!strcmp(argv[i], "--outfile") || !strcmp(argv[i], "-o")) {
if ((i + 1) == argc) {
free(options);
return NULL;
}
options->out_file = argv[i + 1];
i++;
continue;
}
if (!strcmp(argv[i], "--debug") || !strcmp(argv[i], "-d") || !strcmp(argv[i], "-v")) {
options->debug = 1;
}
if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")) {
free(options);
return NULL;
}
}
if (!options->in_file /*|| !options->out_file */ ) {
free(options);
return NULL;
}
return options;
}
void print_usage()
{
printf("Usage: plistutil -i|--infile in_file.plist -o|--outfile out_file.plist [--debug]\n");
printf("\n");
printf("\t-i or --infile: The file to read in.\n");
printf("\t-o or --outfile: The file to convert to.\n");
printf("\t-d, -v or --debug: Provide extended debug information.\n\n");
}
/*
* main.h - header for plistutil
* Written by FxChiP
*/
typedef struct _options {
char *in_file, *out_file;
uint8_t debug, in_fmt, out_fmt;
} Options;
Options *parse_arguments(int argc, char *argv[]);
void print_usage();
<?xml version="1.0"?>
<deviceinfo version="0.2">
<device>
<match key="info.subsystem" string="usb">
<match key="usb.vendor_id" int="0x05ac">
<match key="usb.product_id" compare_ge="0x1290">
<match key="usb.product_id" compare_le="0x1293">
<append key="info.capabilities" type="strlist">afc</append>
</match>
</match>
</match>
</match>
</device>
</deviceinfo>
freedesktopfdidir=$(prefix)/share/hal/fdi/information/20thirdparty/
freedesktopfdi_DATA=31-apple-mobile-device.fdi
nobase_include_HEADERS = libiphone/libiphone.h nobase_include_HEADERS = plist/plist.h
/*
* libiphone.h
* Main include of libiphone
*
* Copyright (c) 2008 Jonathan Beck 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 LIBIPHONE_H
#define LIBIPHONE_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
//general errors
#define IPHONE_E_SUCCESS 0
#define IPHONE_E_INVALID_ARG -1
#define IPHONE_E_UNKNOWN_ERROR -2
#define IPHONE_E_NO_DEVICE -3
#define IPHONE_E_TIMEOUT -4
#define IPHONE_E_NOT_ENOUGH_DATA -5
#define IPHONE_E_BAD_HEADER -6
//lockdownd specific error
#define IPHONE_E_INVALID_CONF -7
#define IPHONE_E_PAIRING_FAILED -8
#define IPHONE_E_SSL_ERROR -9
#define IPHONE_E_PLIST_ERROR -10
#define IPHONE_E_DICT_ERROR -11
//afc specific error
#define IPHONE_E_NO_SUCH_FILE -12
typedef int16_t iphone_error_t;
typedef enum {
IPHONE_AFC_FILE_READ = 0x00000002, // seems to be able to read and write files
IPHONE_AFC_FILE_WRITE = 0x00000003, // writes and creates a file, blanks it out, etc.
IPHONE_AFC_FILE_RW = 0x00000005, // seems to do the same as 2. Might even create the file.
IPHONE_AFC_FILE_OP4 = 0x00000004, // no idea -- appears to be "write" -- clears file beforehand like 3
IPHONE_AFC_FILE_OP6 = 0x00000006, // no idea yet -- appears to be the same as 5.
IPHONE_AFC_FILE_OP1 = 0x00000001, // no idea juuust yet... probably read.
IPHONE_AFC_FILE_OP0 = 0x00000000,
IPHONE_AFC_FILE_OP10 = 0x0000000a
} iphone_afc_file_mode_t;
struct iphone_device_int;
typedef struct iphone_device_int *iphone_device_t;
struct iphone_lckd_client_int;
typedef struct iphone_lckd_client_int *iphone_lckd_client_t;
struct iphone_umux_client_int;
typedef struct iphone_umux_client_int *iphone_umux_client_t;
struct iphone_afc_client_int;
typedef struct iphone_afc_client_int *iphone_afc_client_t;
struct iphone_afc_file_int;
typedef struct iphone_afc_file_int *iphone_afc_file_t;
//device related functions
void iphone_set_debug(int level);
iphone_error_t iphone_get_device ( iphone_device_t *device );
iphone_error_t iphone_free_device ( iphone_device_t device );
//lockdownd related functions
iphone_error_t iphone_lckd_new_client ( iphone_device_t device, iphone_lckd_client_t *client );
iphone_error_t iphone_lckd_free_client( iphone_lckd_client_t client );
iphone_error_t iphone_lckd_start_service ( iphone_lckd_client_t client, const char *service, int *port );
iphone_error_t iphone_lckd_recv ( iphone_lckd_client_t client, char **dump_data, uint32_t *recv_bytes );
iphone_error_t iphone_lckd_send ( iphone_lckd_client_t client, char *raw_data, uint32_t length, uint32_t *recv_bytes );
//usbmux related functions
iphone_error_t iphone_mux_new_client ( iphone_device_t device, uint16_t src_port, uint16_t dst_port, iphone_umux_client_t *client );
iphone_error_t iphone_mux_free_client ( iphone_umux_client_t client );
iphone_error_t iphone_mux_send ( iphone_umux_client_t client, const char *data, uint32_t datalen, uint32_t *sent_bytes );
iphone_error_t iphone_mux_recv ( iphone_umux_client_t client, char *data, uint32_t datalen, uint32_t *recv_bytes );
//afc related functions
iphone_error_t iphone_afc_new_client ( iphone_device_t device, int src_port, int dst_port, iphone_afc_client_t *client );
iphone_error_t iphone_afc_free_client ( iphone_afc_client_t client );
iphone_error_t iphone_afc_get_devinfo ( iphone_afc_client_t client, char ***infos );
iphone_error_t iphone_afc_get_dir_list ( iphone_afc_client_t client, const char *dir, char ***list);
iphone_error_t iphone_afc_get_file_attr ( iphone_afc_client_t client, const char *filename, struct stat *stbuf );
iphone_error_t iphone_afc_open_file ( iphone_afc_client_t client, const char *filename, iphone_afc_file_mode_t file_mode, iphone_afc_file_t *file );
iphone_error_t iphone_afc_close_file ( iphone_afc_client_t client, iphone_afc_file_t file);
iphone_error_t iphone_afc_read_file ( iphone_afc_client_t client, iphone_afc_file_t file, char *data, int length, uint32_t *bytes);
iphone_error_t iphone_afc_write_file ( iphone_afc_client_t client, iphone_afc_file_t file, const char *data, int length, uint32_t *bytes);
iphone_error_t iphone_afc_seek_file ( iphone_afc_client_t client, iphone_afc_file_t file, int seekpos);
iphone_error_t iphone_afc_truncate_file ( iphone_afc_client_t client, iphone_afc_file_t file, uint32_t newsize);
iphone_error_t iphone_afc_delete_file ( iphone_afc_client_t client, const char *path);
iphone_error_t iphone_afc_rename_file ( iphone_afc_client_t client, const char *from, const char *to);
iphone_error_t iphone_afc_mkdir ( iphone_afc_client_t client, const char *dir);
#ifdef __cplusplus
}
#endif
#endif
/* /*
* iphone.h * plist.h
* iPhone struct * Main include of libplist
* *
* Copyright (c) 2008 Zach C. All Rights Reserved. * Copyright (c) 2008 Jonathan Beck All Rights Reserved.
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
...@@ -19,27 +19,21 @@ ...@@ -19,27 +19,21 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
#ifndef IPHONE_H #ifndef LIBPLIST_H
#define IPHONE_H #define LIBPLIST_H
#ifndef USBMUX_H #ifdef __cplusplus
#include "usbmux.h" extern "C" {
#warning usbmux not included?
#endif #endif
#include <usb.h> #include <stdint.h>
#include <libiphone/libiphone.h> #include <sys/types.h>
#define BULKIN 0x85
#define BULKOUT 0x04
struct iphone_device_int {
char *buffer;
struct usb_dev_handle *device;
struct usb_device *__device;
};
// Function definitions #ifdef __cplusplus
int send_to_phone(iphone_device_t phone, char *data, int datalen); }
int recv_from_phone(iphone_device_t phone, char *data, int datalen);
#endif #endif
#endif
...@@ -3,10 +3,10 @@ exec_prefix=@exec_prefix@ ...@@ -3,10 +3,10 @@ exec_prefix=@exec_prefix@
libdir=@libdir@ libdir=@libdir@
includedir=@includedir@ includedir=@includedir@
Name: libiphone Name: libplist
Description: A library to communicate with the Lockdown server on the iPhone Description: A library to handle Apple Property Lists whereas they are binary or XML
Version: @VERSION@ Version: @VERSION@
Requires: libxml-2.0 >= 2.6.30 libusb >= 0.1.12 glib-2.0 >= 2.14.1 gthread-2.0 >= 2.14.1 gnutls >= 1.6.3 libtasn1 >= 1.1 Requires: libxml-2.0 >= 2.6.30 glib-2.0 >= 2.14.1
Libs: -L${libdir} -liphone Libs: -L${libdir} -lplist
Cflags: -I${includedir} Cflags: -I${includedir}
This diff is collapsed.
/*
* AFC.h
* Defines and structs and the like for the built-in AFC client
*
* Copyright (c) 2008 Zach C. 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 "usbmux.h"
#include "iphone.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <glib.h>
typedef struct {
uint32_t header1, header2;
uint32_t entire_length, unknown1, this_length, unknown2, packet_num, unknown3, operation, unknown4;
} AFCPacket;
typedef struct {
uint32_t filehandle, unknown1, size, unknown2;
} AFCFilePacket;
typedef struct __AFCToken {
struct __AFCToken *last, *next;
char *token;
} AFCToken;
struct iphone_afc_client_int {
iphone_umux_client_t connection;
AFCPacket *afc_packet;
int file_handle;
int lock;
GMutex *mutex;
};
struct iphone_afc_file_int {
uint32_t filehandle, blocks, size, type;
};
enum {
AFC_ERROR = 0x00000001,
AFC_GET_INFO = 0x0000000a,
AFC_GET_DEVINFO = 0x0000000b,
AFC_LIST_DIR = 0x00000003,
AFC_MAKE_DIR = 0x00000009,
AFC_DELETE = 0x00000008,
AFC_RENAME = 0x00000018,
AFC_SUCCESS_RESPONSE = 0x00000002,
AFC_FILE_OPEN = 0x0000000d,
AFC_FILE_CLOSE = 0x00000014,
AFC_FILE_SEEK = 0x00000011,
AFC_FILE_TRUNCATE = 0x00000015,
AFC_FILE_HANDLE = 0x0000000e,
AFC_READ = 0x0000000f,
AFC_WRITE = 0x00000010
};
uint32_t iphone_afc_get_file_handle(iphone_afc_file_t file);
INCLUDES = -I$(top_srcdir)/include INCLUDES = -I$(top_srcdir)/include
AM_CFLAGS = $(libxml2_CFLAGS) $(libusb_CFLAGS) $(libglib2_CFLAGS) $(libgnutls_CFLAGS) $(libtasn1_CFLAGS) $(libgthread2_CFLAGS) -g AM_CFLAGS = $(libxml2_CFLAGS) $(libglib2_CFLAGS)
AM_LDFLAGS = $(libxml2_LIBS) $(libusb_LIBS) $(libglib2_LIBS) $(libgnutls_LIBS) $(libtasn1_LIBS) $(libgthread2_LIBS) AM_LDFLAGS = $(libxml2_LIBS) $(libglib2_LIBS)
bin_PROGRAMS = libiphone-initconf lib_LTLIBRARIES = libplist.la
libplist_la_SOURCES = plist.c bplist.c xplist.c utils.c
libiphone_initconf_SOURCES = initconf.c userpref.c utils.c
libiphone_initconf_CFLAGS = $(libgthread2_CFLAGS) $(AM_CFLAGS)
libiphone_initconf_LDFLAGS = $(libgthread2_LIBS) $(AM_LDFLAGS)
lib_LTLIBRARIES = libiphone.la
libiphone_la_SOURCES = usbmux.c iphone.c plist.c bplist.c xplist.c lockdown.c AFC.c userpref.c utils.c
/*
* userpref.c
* contains methods to access user specific certificates IDs and more.
*
* Copyright (c) 2008 Jonathan Beck 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gnutls/gnutls.h>
#include <gnutls/x509.h>
#include <glib.h>
#include "libiphone/libiphone.h"
#include "userpref.h"
#include "utils.h"
/** Generates a 2048 byte key, split into a function so that it can be run in a
* thread.
*
* @param key The pointer to the desired location of the new key.
*/
void generate_key(gpointer key)
{
gnutls_x509_privkey_generate(*((gnutls_x509_privkey_t *) key), GNUTLS_PK_RSA, 2048, 0);
g_thread_exit(0);
}
/** Simple function that generates a spinner until the mutex is released.
*/
void progress_bar(gpointer mutex)
{
const char *spinner = "|/-\\|/-\\";
int i = 0;
while (!g_static_mutex_trylock((GStaticMutex *) mutex)) {
usleep(500000);
printf("Generating key... %c\r", spinner[i++]);
fflush(stdout);
if (i > 8)
i = 0;
}
printf("Generating key... done\n");
g_thread_exit(0);
}
int get_rand(int min, int max)
{
int retval = (rand() % (max - min)) + min;
return retval;
}
/** Generates a valid HostID (which is actually a UUID).
*
* @param A null terminated string containing a valid HostID.
*/
char *lockdownd_generate_hostid()
{
char *hostid = (char *) malloc(sizeof(char) * 37); // HostID's are just UUID's, and UUID's are 36 characters long
const char *chars = "ABCDEF0123456789";
srand(time(NULL));
int i = 0;
for (i = 0; i < 36; i++) {
if (i == 8 || i == 13 || i == 18 || i == 23) {
hostid[i] = '-';
continue;
} else {
hostid[i] = chars[get_rand(0, 16)];
}
}
hostid[36] = '\0'; // make it a real string
return hostid;
}
int main(int argc, char *argv[])
{
GThread *progress_thread, *key_thread;
GError *err;
static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
char *host_id = NULL;
gnutls_x509_privkey_t root_privkey;
gnutls_x509_privkey_t host_privkey;
gnutls_x509_crt_t root_cert;
gnutls_x509_crt_t host_cert;
iphone_set_debug(1);
// Create the thread
if (!g_thread_supported()) {
g_thread_init(NULL);
}
gnutls_global_init();
printf("This program generates keys required to connect with the iPhone\n");
printf("It only needs to be run ONCE.\n\n");
printf("Additionally it may take several minutes to run, please be patient.\n\n");
gnutls_x509_privkey_init(&root_privkey);
gnutls_x509_privkey_init(&host_privkey);
gnutls_x509_crt_init(&root_cert);
gnutls_x509_crt_init(&host_cert);
/* generate HostID */
host_id = lockdownd_generate_hostid();
/* generate root key */
g_static_mutex_lock(&mutex);
if ((key_thread = g_thread_create((GThreadFunc) generate_key, &root_privkey, TRUE, &err)) == NULL) {
printf("Thread create failed: %s!!\n", err->message);
g_error_free(err);
}
if ((progress_thread = g_thread_create((GThreadFunc) progress_bar, &mutex, TRUE, &err)) == NULL) {
printf("Thread create failed: %s!!\n", err->message);
g_error_free(err);
}
g_thread_join(key_thread);
g_static_mutex_unlock(&mutex);
g_thread_join(progress_thread);
/* generate host key */
g_static_mutex_init(&mutex);
g_static_mutex_lock(&mutex);
if ((key_thread = g_thread_create((GThreadFunc) generate_key, &host_privkey, TRUE, &err)) == NULL) {
printf("Thread create failed: %s!!\n", err->message);
g_error_free(err);
}
if ((progress_thread = g_thread_create((GThreadFunc) progress_bar, &mutex, TRUE, &err)) == NULL) {
printf("Thread create failed: %s!!\n", err->message);
g_error_free(err);
}
g_thread_join(key_thread);
g_static_mutex_unlock(&mutex);
g_thread_join(progress_thread);
/* generate certificates */
gnutls_x509_crt_set_key(root_cert, root_privkey);
gnutls_x509_crt_set_serial(root_cert, "\x00", 1);
gnutls_x509_crt_set_version(root_cert, 3);
gnutls_x509_crt_set_ca_status(root_cert, 1);
gnutls_x509_crt_set_activation_time(root_cert, time(NULL));
gnutls_x509_crt_set_expiration_time(root_cert, time(NULL) + (60 * 60 * 24 * 365 * 10));
gnutls_x509_crt_sign(root_cert, root_cert, root_privkey);
gnutls_x509_crt_set_key(host_cert, host_privkey);
gnutls_x509_crt_set_serial(host_cert, "\x00", 1);
gnutls_x509_crt_set_version(host_cert, 3);
gnutls_x509_crt_set_ca_status(host_cert, 0);
gnutls_x509_crt_set_key_usage(host_cert, GNUTLS_KEY_KEY_ENCIPHERMENT | GNUTLS_KEY_DIGITAL_SIGNATURE);
gnutls_x509_crt_set_activation_time(host_cert, time(NULL));
gnutls_x509_crt_set_expiration_time(host_cert, time(NULL) + (60 * 60 * 24 * 365 * 10));
gnutls_x509_crt_sign(host_cert, root_cert, root_privkey);
/* export to PEM format */
gnutls_datum_t root_key_pem = { NULL, 0 };
gnutls_datum_t host_key_pem = { NULL, 0 };
gnutls_x509_privkey_export(root_privkey, GNUTLS_X509_FMT_PEM, NULL, &root_key_pem.size);
gnutls_x509_privkey_export(host_privkey, GNUTLS_X509_FMT_PEM, NULL, &host_key_pem.size);
root_key_pem.data = gnutls_malloc(root_key_pem.size);
host_key_pem.data = gnutls_malloc(host_key_pem.size);
gnutls_x509_privkey_export(root_privkey, GNUTLS_X509_FMT_PEM, root_key_pem.data, &root_key_pem.size);
gnutls_x509_privkey_export(host_privkey, GNUTLS_X509_FMT_PEM, host_key_pem.data, &host_key_pem.size);
gnutls_datum_t root_cert_pem = { NULL, 0 };
gnutls_datum_t host_cert_pem = { NULL, 0 };
gnutls_x509_crt_export(root_cert, GNUTLS_X509_FMT_PEM, NULL, &root_cert_pem.size);
gnutls_x509_crt_export(host_cert, GNUTLS_X509_FMT_PEM, NULL, &host_cert_pem.size);
root_cert_pem.data = gnutls_malloc(root_cert_pem.size);
host_cert_pem.data = gnutls_malloc(host_cert_pem.size);
printf("Generating root certificate...");
gnutls_x509_crt_export(root_cert, GNUTLS_X509_FMT_PEM, root_cert_pem.data, &root_cert_pem.size);
printf("done\n");
printf("Generating host certificate...");
gnutls_x509_crt_export(host_cert, GNUTLS_X509_FMT_PEM, host_cert_pem.data, &host_cert_pem.size);
printf("done\n");
/* store values in config file */
init_config_file(host_id, &root_key_pem, &host_key_pem, &root_cert_pem, &host_cert_pem);
gnutls_free(root_key_pem.data);
gnutls_free(host_key_pem.data);
gnutls_free(root_cert_pem.data);
gnutls_free(host_cert_pem.data);
return 0;
}
/*
* iphone.c
* Functions for creating and initializing iPhone structures.
*
* Copyright (c) 2008 Zach C. 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 "usbmux.h"
#include "iphone.h"
#include "utils.h"
#include <arpa/inet.h>
#include <usb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**
* Given a USB bus and device number, returns a device handle to the iPhone on
* that bus. To aid compatibility with future devices, this function does not
* check the vendor and device IDs! To do that, you should use
* iphone_get_device() or a system-specific API (e.g. HAL).
*
* @param bus_n The USB bus number.
* @param dev_n The USB device number.
* @param device A pointer to a iphone_device_t, which must be set to NULL upon
* calling iphone_get_specific_device, which will be filled with a device
* descriptor on return.
* @return IPHONE_E_SUCCESS if ok, otherwise an error code.
*/
iphone_error_t iphone_get_specific_device(int bus_n, int dev_n, iphone_device_t * device)
{
struct usb_bus *bus, *busses;
struct usb_device *dev;
usbmux_version_header *version;
int bytes = 0;
//check we can actually write in device
if (!device || (device && *device))
return IPHONE_E_INVALID_ARG;
iphone_device_t phone = (iphone_device_t) malloc(sizeof(struct iphone_device_int));
// Initialize the struct
phone->device = NULL;
phone->__device = NULL;
phone->buffer = NULL;
// Initialize libusb
usb_init();
usb_find_busses();
usb_find_devices();
busses = usb_get_busses();
// Set the device configuration
for (bus = busses; bus; bus = bus->next)
if (bus->location == bus_n)
for (dev = bus->devices; dev != NULL; dev = dev->next)
if (dev->devnum == dev_n) {
phone->__device = dev;
phone->device = usb_open(phone->__device);
usb_set_configuration(phone->device, 3);
usb_claim_interface(phone->device, 1);
goto found;
}
iphone_free_device(phone);
log_debug_msg("iphone_get_specific_device: iPhone not found\n");
return IPHONE_E_NO_DEVICE;
found:
// Send the version command to the phone
version = version_header();
bytes = usb_bulk_write(phone->device, BULKOUT, (char *) version, sizeof(*version), 800);
if (bytes < 20) {
log_debug_msg("get_iPhone(): libusb did NOT send enough!\n");
if (bytes < 0) {
log_debug_msg("get_iPhone(): libusb gave me the error %d: %s (%s)\n",
bytes, usb_strerror(), strerror(-bytes));
}
}
// Read the phone's response
bytes = usb_bulk_read(phone->device, BULKIN, (char *) version, sizeof(*version), 800);
// Check for bad response
if (bytes < 20) {
free(version);
iphone_free_device(phone);
log_debug_msg("get_iPhone(): Invalid version message -- header too short.\n");
if (bytes < 0)
log_debug_msg("get_iPhone(): libusb error message %d: %s (%s)\n", bytes, usb_strerror(), strerror(-bytes));
return IPHONE_E_NOT_ENOUGH_DATA;
}
// Check for correct version
if (ntohl(version->major) == 1 && ntohl(version->minor) == 0) {
// We're all ready to roll.
fprintf(stderr, "get_iPhone() success\n");
free(version);
*device = phone;
return IPHONE_E_SUCCESS;
} else {
// Bad header
iphone_free_device(phone);
free(version);
log_debug_msg("get_iPhone(): Received a bad header/invalid version number.");
return IPHONE_E_BAD_HEADER;
}
// If it got to this point it's gotta be bad
log_debug_msg("get_iPhone(): Unknown error.\n");
iphone_free_device(phone);
free(version);
return IPHONE_E_UNKNOWN_ERROR; // if it got to this point it's gotta be bad
}
/**
* Scans all USB busses and devices for a known AFC-compatible device and
* returns a handle to the first such device it finds. Known devices include
* those with vendor ID 0x05ac and product ID between 0x1290 and 0x1293
* inclusive.
*
* This function is convenient, but on systems where higher-level abstractions
* (such as HAL) are available it may be preferable to use
* iphone_get_specific_device instead, because it can deal with multiple
* connected devices as well as devices not known to libiphone.
*
* @param device Upon calling this function, a pointer to a location of type
* iphone_device_t, which must have the value NULL. On return, this location
* will be filled with a handle to the device.
* @return IPHONE_E_SUCCESS if ok, otherwise an error code.
*/
iphone_error_t iphone_get_device(iphone_device_t * device)
{
struct usb_bus *bus, *busses;
struct usb_device *dev;
usb_init();
usb_find_busses();
usb_find_devices();
for (bus = usb_get_busses(); bus != NULL; bus = bus->next)
for (dev = bus->devices; dev != NULL; dev = dev->next)
if (dev->descriptor.idVendor == 0x05ac
&& dev->descriptor.idProduct >= 0x1290 && dev->descriptor.idProduct <= 0x1293)
return iphone_get_specific_device(bus->location, dev->devnum, device);
return IPHONE_E_NO_DEVICE;
}
/** Cleans up an iPhone structure, then frees the structure itself.
* This is a library-level function; deals directly with the iPhone to tear
* down relations, but otherwise is mostly internal.
*
* @param phone A pointer to an iPhone structure.
*/
iphone_error_t iphone_free_device(iphone_device_t device)
{
if (!device)
return IPHONE_E_INVALID_ARG;
iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
if (device->buffer) {
free(device->buffer);
}
if (device->device) {
usb_release_interface(device->device, 1);
usb_reset(device->device);
usb_close(device->device);
ret = IPHONE_E_SUCCESS;
}
free(device);
return ret;
}
/** Sends data to the phone
* This is a low-level (i.e. directly to phone) function.
*
* @param phone The iPhone to send data to
* @param data The data to send to the iPhone
* @param datalen The length of the data
* @return The number of bytes sent, or -1 on error or something.
*/
int send_to_phone(iphone_device_t phone, char *data, int datalen)
{
if (!phone)
return -1;
int bytes = 0;
if (!phone)
return -1;
log_debug_msg("send_to_phone: Attempting to send datalen = %i data = %p\n", datalen, data);
bytes = usb_bulk_write(phone->device, BULKOUT, data, datalen, 800);
if (bytes < datalen) {
if (bytes < 0)
log_debug_msg("send_to_iphone(): libusb gave me the error %d: %s - %s\n", bytes, usb_strerror(),
strerror(-bytes));
return -1;
} else {
return bytes;
}
return -1;
}
/** This function is a low-level (i.e. direct to iPhone) function.
*
* @param phone The iPhone to receive data from
* @param data Where to put data read
* @param datalen How much data to read in
*
* @return How many bytes were read in, or -1 on error.
*/
int recv_from_phone(iphone_device_t phone, char *data, int datalen)
{
if (!phone)
return -1;
int bytes = 0;
if (!phone)
return -1;
log_debug_msg("recv_from_phone(): attempting to receive %i bytes\n", datalen);
bytes = usb_bulk_read(phone->device, BULKIN, data, datalen, 3500);
if (bytes < 0) {
log_debug_msg("recv_from_phone(): libusb gave me the error %d: %s (%s)\n", bytes, usb_strerror(),
strerror(-bytes));
return -1;
}
return bytes;
}
This diff is collapsed.
/*
* lockdown.h
* Defines lockdown stuff, like the client struct.
*
* Copyright (c) 2008 Zach C. 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 LOCKDOWND_H
#define LOCKDOWND_H
#include "usbmux.h"
#include "plist.h"
#include <gnutls/gnutls.h>
#include <string.h>
#include <libiphone/libiphone.h>
struct iphone_lckd_client_int {
iphone_umux_client_t connection;
gnutls_session_t *ssl_session;
int in_SSL;
char *gtls_buffer_hack;
int gtls_buffer_hack_len;
};
iphone_lckd_client_t new_lockdownd_client(iphone_device_t phone);
iphone_error_t lockdownd_hello(iphone_lckd_client_t control);
iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, char *req_key, char *req_string,
gnutls_datum_t * value);
iphone_error_t lockdownd_get_device_uid(iphone_lckd_client_t control, char **uid);
iphone_error_t lockdownd_get_device_public_key(iphone_lckd_client_t control, gnutls_datum_t * public_key);
iphone_error_t lockdownd_gen_pair_cert(gnutls_datum_t public_key, gnutls_datum_t * device_cert,
gnutls_datum_t * host_cert, gnutls_datum_t * root_cert);
iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, char *host_id);
void lockdownd_close(iphone_lckd_client_t control);
// SSL functions
iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const char *HostID);
ssize_t lockdownd_securead(gnutls_transport_ptr_t transport, char *buffer, size_t length);
ssize_t lockdownd_secuwrite(gnutls_transport_ptr_t transport, char *buffer, size_t length);
#endif
This diff is collapsed.
/*
* usbmux.h
* Defines structures and variables pertaining to the usb multiplexing.
*
* Copyright (c) 2008 Zach C. 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 <sys/types.h>
#include <stdlib.h>
#include <stdint.h>
#include "libiphone/libiphone.h"
#ifndef USBMUX_H
#define USBMUX_H
#ifndef IPHONE_H
#include "iphone.h"
#endif
typedef struct {
uint32_t type, length;
uint16_t sport, dport;
uint32_t scnt, ocnt;
uint8_t offset, tcp_flags;
uint16_t window, nullnull, length16;
} usbmux_tcp_header;
struct iphone_umux_client_int {
usbmux_tcp_header *header;
iphone_device_t phone;
char *recv_buffer;
int r_len;
};
usbmux_tcp_header *new_mux_packet(uint16_t s_port, uint16_t d_port);
typedef struct {
uint32_t type, length, major, minor, allnull;
} usbmux_version_header;
usbmux_version_header *version_header();
#endif
/*
* userpref.c
* contains methods to access user specific certificates IDs and more.
*
* Copyright (c) 2008 Jonathan Beck 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 <glib.h>
#include <glib/gprintf.h>
#include <stdio.h>
#include <string.h>
#include "userpref.h"
#include "utils.h"
#include <string.h>
#include <stdlib.h>
#define LIBIPHONE_CONF_DIR "libiphone"
#define LIBIPHONE_CONF_FILE "libiphonerc"
#define LIBIPHONE_ROOT_PRIVKEY "RootPrivateKey.pem"
#define LIBIPHONE_HOST_PRIVKEY "HostPrivateKey.pem"
#define LIBIPHONE_ROOT_CERTIF "RootCertificate.pem"
#define LIBIPHONE_HOST_CERTIF "HostCertificate.pem"
/** Creates a freedesktop compatible configuration directory for libiphone.
*/
inline void create_config_dir()
{
gchar *config_dir = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, NULL);
if (!g_file_test(config_dir, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
g_mkdir_with_parents(config_dir, 0755);
g_free(config_dir);
}
/** Reads the HostID from a previously generated configuration file.
*
* @note It is the responsibility of the calling function to free the returned host_id
*
* @return The string containing the HostID or NULL
*/
char *get_host_id()
{
char *host_id = NULL;
gchar *config_file;
GKeyFile *key_file;
gchar *loc_host_id;
config_file =
g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL);
/* now parse file to get the HostID */
key_file = g_key_file_new();
if (g_key_file_load_from_file(key_file, config_file, G_KEY_FILE_KEEP_COMMENTS, NULL)) {
loc_host_id = g_key_file_get_value(key_file, "Global", "HostID", NULL);
if (loc_host_id)
host_id = strdup((char *) loc_host_id);
g_free(loc_host_id);
}
g_key_file_free(key_file);
g_free(config_file);
log_debug_msg("get_host_id(): Using %s as HostID\n", host_id);
return host_id;
}
/** Determines whether this iPhone has been connected to this system before.
*
* @param uid The device uid as given by the iPhone.
*
* @return 1 if the iPhone has been connected previously to this configuration
* or 0 otherwise.
*/
int is_device_known(char *uid)
{
int ret = 0;
gchar *config_file;
GKeyFile *key_file;
gchar **devices_list, **pcur, *keyfilepath, *stored_key;
GIOChannel *keyfile;
/* first get config file */
gchar *device_file = g_strconcat(uid, ".pem", NULL);
config_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, device_file, NULL);
if (g_file_test(config_file, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)))
ret = 1;
g_free(config_file);
g_free(device_file);
return ret;
}
/** Mark the iPhone (as represented by the key) as having connected to this
* configuration.
*
* @param public_key The public key given by the iPhone
*
* @return 1 on success and 0 if no public key is given or if it has already
* been marked as connected previously.
*/
int store_device_public_key(char *uid, gnutls_datum_t public_key)
{
if (NULL == public_key.data || is_device_known(uid))
return 0;
/* ensure config directory exists */
create_config_dir();
/* build file path */
gchar *device_file = g_strconcat(uid, ".pem", NULL);
gchar *pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, device_file, NULL);
/* store file */
FILE *pFile = fopen(pem, "wb");
fwrite(public_key.data, 1, public_key.size, pFile);
fclose(pFile);
g_free(pem);
g_free(device_file);
return 1;
}
/** Private function which reads the given file into a gnutls structure.
*
* @param file The filename of the file to read
* @param data The pointer at which to store the data.
*
* @return 1 if the file contents where read successfully and 0 otherwise.
*/
int read_file_in_confdir(char *file, gnutls_datum_t * data)
{
gboolean success;
gsize size;
char *content;
gchar *filepath;
if (NULL == file || NULL == data)
return 0;
/* Read file */
filepath = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, file, NULL);
success = g_file_get_contents(filepath, &content, &size, NULL);
g_free(filepath);
/* Add it to the gnutls_datnum_t structure */
data->data = content;
data->size = size;
return success;
}
/** Read the root private key
*
* @param root_privkey A pointer to the appropriate gnutls structure
*
* @return 1 if the file was successfully read and 0 otherwise.
*/
int get_root_private_key(gnutls_datum_t * root_privkey)
{
return read_file_in_confdir(LIBIPHONE_ROOT_PRIVKEY, root_privkey);
}
/** Read the host private key
*
* @param host_privkey A pointer to the appropriate gnutls structure
*
* @return 1 if the file was successfully read and 0 otherwise.
*/
int get_host_private_key(gnutls_datum_t * host_privkey)
{
return read_file_in_confdir(LIBIPHONE_HOST_PRIVKEY, host_privkey);
}
/** Read the root certificate
*
* @param root_privkey A pointer to the appropriate gnutls structure
*
* @return 1 if the file was successfully read and 0 otherwise.
*/
int get_root_certificate(gnutls_datum_t * root_cert)
{
return read_file_in_confdir(LIBIPHONE_ROOT_CERTIF, root_cert);
}
/** Read the host certificate
*
* @param root_privkey A pointer to the appropriate gnutls structure
*
* @return 1 if the file was successfully read and 0 otherwise.
*/
int get_host_certificate(gnutls_datum_t * host_cert)
{
return read_file_in_confdir(LIBIPHONE_HOST_CERTIF, host_cert);
}
/** Create and save a configuration file containing the given data.
*
* @note: All fields must specified and be non-null
*
* @param host_id The UUID of the host
* @param root_key The root key
* @param host_key The host key
* @param root_cert The root certificate
* @param host_cert The host certificate
*
* @return 1 on success and 0 otherwise.
*/
int init_config_file(char *host_id, gnutls_datum_t * root_key, gnutls_datum_t * host_key, gnutls_datum_t * root_cert,
gnutls_datum_t * host_cert)
{
FILE *pFile;
gchar *pem;
GKeyFile *key_file;
gsize length;
gchar *buf, *config_file;
GIOChannel *file;
if (!host_id || !root_key || !host_key || !root_cert || !host_cert)
return 0;
/* Make sure config directory exists */
create_config_dir();
/* Now parse file to get the HostID */
key_file = g_key_file_new();
/* Store in config file */
log_debug_msg("init_config_file(): setting hostID to %s\n", host_id);
g_key_file_set_value(key_file, "Global", "HostID", host_id);
/* Write config file on disk */
buf = g_key_file_to_data(key_file, &length, NULL);
config_file =
g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL);
file = g_io_channel_new_file(config_file, "w", NULL);
g_free(config_file);
g_io_channel_write_chars(file, buf, length, NULL, NULL);
g_io_channel_shutdown(file, TRUE, NULL);
g_io_channel_unref(file);
g_key_file_free(key_file);
/* Now write keys and certificates to disk */
pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_ROOT_PRIVKEY, NULL);
pFile = fopen(pem, "wb");
fwrite(root_key->data, 1, root_key->size, pFile);
fclose(pFile);
g_free(pem);
pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_HOST_PRIVKEY, NULL);
pFile = fopen(pem, "wb");
fwrite(host_key->data, 1, host_key->size, pFile);
fclose(pFile);
g_free(pem);
pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_ROOT_CERTIF, NULL);
pFile = fopen(pem, "wb");
fwrite(root_cert->data, 1, root_cert->size, pFile);
fclose(pFile);
g_free(pem);
pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_HOST_CERTIF, NULL);
pFile = fopen(pem, "wb");
fwrite(host_cert->data, 1, host_cert->size, pFile);
fclose(pFile);
g_free(pem);
return 1;
}
/*
* userpref.h
* contains methods to access user specific certificates IDs and more.
*
* Copyright (c) 2008 Jonathan Beck 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 USERPREF_H
#define USERPREF_H
#include <gnutls/gnutls.h>
/**
* Method to get user's HostID. Caller must free returned buffer.
*
* @return the HostID if exist in config file. Returns NULL otherwise.
*/
char *get_host_id();
/**
* Determine if we already paired this device.
*
* @return 1 if device is already paired. Returns 0 otherwise.
*/
int is_device_known(char *uid);
/**
* @return 1 if everything went well. Returns 0 otherwise.
*/
int store_device_public_key(char *uid, gnutls_datum_t public_key);
/**
* @return 1 if everything went well. Returns 0 otherwise.
*/
int get_root_private_key(gnutls_datum_t * root_privkey);
/**
* @return 1 if everything went well. Returns 0 otherwise.
*/
int get_host_private_key(gnutls_datum_t * host_privkey);
/**
* @return 1 if everything went well. Returns 0 otherwise.
*/
int get_root_certificate(gnutls_datum_t * root_cert);
/**
* @return 1 if everything went well. Returns 0 otherwise.
*/
int get_host_certificate(gnutls_datum_t * host_cert);
/**
* Setup a brand new config file.
* @return 1 if everything went well. Returns 0 otherwise.
*/
int init_config_file(char *host_id, gnutls_datum_t * root_key, gnutls_datum_t * host_key, gnutls_datum_t * root_cert,
gnutls_datum_t * host_cert);
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment