Commit 2b05e48c authored by Matt Colyer's avatar Matt Colyer

Enforce a modified kr style.

Use "make indent" from now on before committing.
parent 7ac3d681
......@@ -3,3 +3,6 @@ SUBDIRS = src
doc:
doxygen doxygen.cfg
indent:
indent -kr -ut -ts4 -l120 src/*.c src/*.h
This diff is collapsed.
......@@ -71,4 +71,3 @@ enum {
AFC_READ = 0x0000000f,
AFC_WRITE = 0x00000010
};
......@@ -42,7 +42,8 @@ iphone_lckd_client_t control = NULL;
int debug = 0;
static int ifuse_getattr(const char *path, struct stat *stbuf) {
static int ifuse_getattr(const char *path, struct stat *stbuf)
{
int res = 0;
iphone_afc_client_t afc = fuse_get_context()->private_data;
......@@ -54,15 +55,15 @@ static int ifuse_getattr(const char *path, struct stat *stbuf) {
return res;
}
static int ifuse_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi) {
static int ifuse_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi)
{
int i;
char **dirs = NULL;
iphone_afc_client_t afc = fuse_get_context()->private_data;
iphone_afc_get_dir_list(afc, path, &dirs);
if(!dirs)
if (!dirs)
return -ENOENT;
for (i = 0; dirs[i]; i++) {
......@@ -74,7 +75,8 @@ static int ifuse_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
return 0;
}
static int ifuse_create(const char *path, mode_t mode, struct fuse_file_info *fi) {
static int ifuse_create(const char *path, mode_t mode, struct fuse_file_info *fi)
{
// exactly the same as open but using a different mode
iphone_afc_file_t file = NULL;
iphone_afc_client_t afc = fuse_get_context()->private_data;
......@@ -86,7 +88,8 @@ static int ifuse_create(const char *path, mode_t mode, struct fuse_file_info *fi
return 0;
}
static int ifuse_open(const char *path, struct fuse_file_info *fi) {
static int ifuse_open(const char *path, struct fuse_file_info *fi)
{
iphone_afc_file_t file = NULL;
iphone_afc_client_t afc = fuse_get_context()->private_data;
uint32_t mode = 0;
......@@ -108,8 +111,8 @@ static int ifuse_open(const char *path, struct fuse_file_info *fi) {
return 0;
}
static int ifuse_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi) {
static int ifuse_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi)
{
int bytes = 0;
iphone_afc_file_t file;
iphone_afc_client_t afc = fuse_get_context()->private_data;
......@@ -118,7 +121,7 @@ static int ifuse_read(const char *path, char *buf, size_t size, off_t offset,
return 0;
file = g_hash_table_lookup(file_handles, &(fi->fh));
if (!file){
if (!file) {
return -ENOENT;
}
......@@ -127,31 +130,36 @@ static int ifuse_read(const char *path, char *buf, size_t size, off_t offset,
return bytes;
}
static int ifuse_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
static int ifuse_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi)
{
int bytes = 0;
iphone_afc_file_t file = NULL;
iphone_afc_client_t afc = fuse_get_context()->private_data;
if (size == 0) return 0;
if (size == 0)
return 0;
file = g_hash_table_lookup(file_handles, &(fi->fh));
if (!file) return -ENOENT;
if (!file)
return -ENOENT;
if (IPHONE_E_SUCCESS == iphone_afc_seek_file(afc, file, offset))
iphone_afc_write_file(afc, file, buf, size, &bytes);
return bytes;
}
static int ifuse_fsync(const char *path, int datasync, struct fuse_file_info *fi) {
static int ifuse_fsync(const char *path, int datasync, struct fuse_file_info *fi)
{
return 0;
}
static int ifuse_release(const char *path, struct fuse_file_info *fi){
static int ifuse_release(const char *path, struct fuse_file_info *fi)
{
iphone_afc_file_t file = NULL;
iphone_afc_client_t afc = fuse_get_context()->private_data;
file = g_hash_table_lookup(file_handles, &(fi->fh));
if (!file){
if (!file) {
return -ENOENT;
}
iphone_afc_close_file(afc, file);
......@@ -161,7 +169,8 @@ static int ifuse_release(const char *path, struct fuse_file_info *fi){
return 0;
}
void *ifuse_init(struct fuse_conn_info *conn) {
void *ifuse_init(struct fuse_conn_info *conn)
{
int port = 0;
iphone_afc_client_t afc = NULL;
......@@ -170,7 +179,7 @@ void *ifuse_init(struct fuse_conn_info *conn) {
file_handles = g_hash_table_new(g_int_hash, g_int_equal);
iphone_get_device(&phone);
if (!phone){
if (!phone) {
fprintf(stderr, "No iPhone found, is it connected?\n");
return NULL;
}
......@@ -194,33 +203,37 @@ void *ifuse_init(struct fuse_conn_info *conn) {
return afc;
}
void ifuse_cleanup(void *data) {
iphone_afc_client_t afc = (iphone_afc_client_t )data;
void ifuse_cleanup(void *data)
{
iphone_afc_client_t afc = (iphone_afc_client_t) data;
iphone_afc_free_client(afc);
iphone_lckd_free_client(control);
iphone_free_device(phone);
}
int ifuse_flush(const char *path, struct fuse_file_info *fi) {
int ifuse_flush(const char *path, struct fuse_file_info *fi)
{
return 0;
}
int ifuse_statfs(const char *path, struct statvfs *stats) {
int ifuse_statfs(const char *path, struct statvfs *stats)
{
iphone_afc_client_t afc = fuse_get_context()->private_data;
char **info_raw = NULL;
uint32_t totalspace = 0, freespace = 0, blocksize = 0, i = 0;
iphone_afc_get_devinfo(afc, &info_raw);
if (!info_raw) return -ENOENT;
if (!info_raw)
return -ENOENT;
for (i = 0; info_raw[i]; i++) {
if (!strcmp(info_raw[i], "FSTotalBytes")) {
totalspace = atoi(info_raw[i+1]);
totalspace = atoi(info_raw[i + 1]);
} else if (!strcmp(info_raw[i], "FSFreeBytes")) {
freespace = atoi(info_raw[i+1]);
freespace = atoi(info_raw[i + 1]);
} else if (!strcmp(info_raw[i], "FSBlockSize")) {
blocksize = atoi(info_raw[i+1]);
blocksize = atoi(info_raw[i + 1]);
}
}
free_dictionary(info_raw);
......@@ -234,42 +247,55 @@ int ifuse_statfs(const char *path, struct statvfs *stats) {
return 0;
}
int ifuse_truncate(const char *path, off_t size) {
int ifuse_truncate(const char *path, off_t size)
{
int result = 0;
iphone_afc_client_t afc = fuse_get_context()->private_data;
iphone_afc_file_t tfile = NULL;
iphone_afc_open_file(afc, path, IPHONE_AFC_FILE_READ, &tfile);
if (!tfile) return -1;
if (!tfile)
return -1;
result = iphone_afc_truncate_file(afc, tfile, size);
iphone_afc_close_file(afc, tfile);
return result;
}
int ifuse_ftruncate(const char *path, off_t size, struct fuse_file_info *fi) {
int ifuse_ftruncate(const char *path, off_t size, struct fuse_file_info *fi)
{
iphone_afc_client_t afc = fuse_get_context()->private_data;
iphone_afc_file_t file = g_hash_table_lookup(file_handles, &fi->fh);
if (!file) return -ENOENT;
if (!file)
return -ENOENT;
return iphone_afc_truncate_file(afc, file, size);
}
int ifuse_unlink(const char *path) {
int ifuse_unlink(const char *path)
{
iphone_afc_client_t afc = fuse_get_context()->private_data;
if (IPHONE_E_SUCCESS == iphone_afc_delete_file(afc, path)) return 0;
else return -1;
if (IPHONE_E_SUCCESS == iphone_afc_delete_file(afc, path))
return 0;
else
return -1;
}
int ifuse_rename(const char *from, const char *to) {
int ifuse_rename(const char *from, const char *to)
{
iphone_afc_client_t afc = fuse_get_context()->private_data;
if (IPHONE_E_SUCCESS == iphone_afc_rename_file(afc, from, to)) return 0;
else return -1;
if (IPHONE_E_SUCCESS == iphone_afc_rename_file(afc, from, to))
return 0;
else
return -1;
}
int ifuse_mkdir(const char *dir, mode_t ignored) {
int ifuse_mkdir(const char *dir, mode_t ignored)
{
iphone_afc_client_t afc = fuse_get_context()->private_data;
if (IPHONE_E_SUCCESS == iphone_afc_mkdir(afc, dir)) return 0;
else return -1;
if (IPHONE_E_SUCCESS == iphone_afc_mkdir(afc, dir))
return 0;
else
return -1;
}
static struct fuse_operations ifuse_oper = {
......@@ -292,6 +318,7 @@ static struct fuse_operations ifuse_oper = {
.destroy = ifuse_cleanup
};
int main(int argc, char *argv[]) {
int main(int argc, char *argv[])
{
return fuse_main(argc, argv, &ifuse_oper, NULL);
}
......@@ -36,38 +36,43 @@ int debug = 1;
*
* @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);
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){
void progress_bar(gpointer mutex)
{
const char *spinner = "|/-\\|/-\\";
int i = 0;
while (!g_static_mutex_trylock((GStaticMutex*)mutex)){
while (!g_static_mutex_trylock((GStaticMutex *) mutex)) {
usleep(500000);
printf("Generating key... %c\r", spinner[i++]);
fflush(stdout);
if (i > 8) i = 0;
if (i > 8)
i = 0;
}
printf("Generating key... done\n");
g_thread_exit(0);
}
int main(int argc, char *argv[]) {
int main(int argc, char *argv[])
{
GThread *progress_thread, *key_thread;
GError *err;
static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
char* host_id = NULL;
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;
// Create the thread
if (!g_thread_supported()){
if (!g_thread_supported()) {
g_thread_init(NULL);
}
gnutls_global_init();
......@@ -88,13 +93,13 @@ int main(int argc, char *argv[]) {
/* 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 ((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) ;
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);
......@@ -103,13 +108,13 @@ int main(int argc, char *argv[]) {
/* 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 ((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) ;
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);
......@@ -136,33 +141,33 @@ int main(int argc, char *argv[]) {
/* export to PEM format */
gnutls_datum_t root_key_pem = {NULL, 0};
gnutls_datum_t host_key_pem = {NULL, 0};
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);
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_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_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);
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);
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);
gnutls_x509_crt_export(host_cert, GNUTLS_X509_FMT_PEM, host_cert_pem.data, &host_cert_pem.size);
printf("done\n");
......@@ -176,4 +181,3 @@ int main(int argc, char *argv[]) {
return 0;
}
......@@ -34,14 +34,15 @@ extern int debug;
* @return A structure with data on the first iPhone it finds. (Or NULL, on
* error)
*/
iphone_error_t iphone_get_device ( iphone_device_t *device ){
iphone_error_t iphone_get_device(iphone_device_t * device)
{
//check we can actually write in device
if (!device || (device && *device))
return IPHONE_E_INVALID_ARG;
struct usb_bus *bus, *busses;
struct usb_device *dev;
iphone_device_t phone = (iphone_device_t)malloc(sizeof(struct iphone_device_int));
iphone_device_t phone = (iphone_device_t) malloc(sizeof(struct iphone_device_int));
// Initialize the struct
phone->device = NULL;
......@@ -60,9 +61,7 @@ iphone_error_t iphone_get_device ( iphone_device_t *device ){
for (dev = bus->devices; dev; dev = dev->next) {
if (dev->descriptor.idVendor == 0x05ac &&
(dev->descriptor.idProduct == 0x1290 ||
dev->descriptor.idProduct == 0x1291 ||
dev->descriptor.idProduct == 0x1292
)
dev->descriptor.idProduct == 0x1291 || dev->descriptor.idProduct == 0x1292)
) {
phone->__device = dev;
phone->device = usb_open(phone->__device);
......@@ -71,20 +70,21 @@ iphone_error_t iphone_get_device ( iphone_device_t *device ){
break;
}
}
if (phone->__device && phone->device) break;
if (phone->__device && phone->device)
break;
}
// Check to see if we are connected
if (!phone->device || !phone->__device) {
iphone_free_device(phone);
if (debug) fprintf(stderr, "get_iPhone(): iPhone not found\n");
if (debug)
fprintf(stderr, "get_iPhone(): iPhone not found\n");
return IPHONE_E_NO_DEVICE;
}
// Send the version command to the phone
int bytes = 0;
usbmux_version_header *version = version_header();
bytes = usb_bulk_write(phone->device, BULKOUT, (char*)version, sizeof(*version), 800);
bytes = usb_bulk_write(phone->device, BULKOUT, (char *) version, sizeof(*version), 800);
if (bytes < 20 && debug) {
fprintf(stderr, "get_iPhone(): libusb did NOT send enough!\n");
if (bytes < 0) {
......@@ -92,20 +92,20 @@ iphone_error_t iphone_get_device ( iphone_device_t *device ){
bytes, usb_strerror(), strerror(-bytes));
}
}
// Read the phone's response
bytes = usb_bulk_read(phone->device, BULKIN, (char*)version, sizeof(*version), 800);
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);
if (debug) fprintf(stderr, "get_iPhone(): Invalid version message -- header too short.\n");
if (debug && bytes < 0) fprintf(stderr, "get_iPhone(): libusb error message %d: %s (%s)\n",
if (debug)
fprintf(stderr, "get_iPhone(): Invalid version message -- header too short.\n");
if (debug && bytes < 0)
fprintf(stderr, "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.
......@@ -117,12 +117,14 @@ iphone_error_t iphone_get_device ( iphone_device_t *device ){
// Bad header
iphone_free_device(phone);
free(version);
if (debug) fprintf(stderr, "get_iPhone(): Received a bad header/invalid version number.");
if (debug)
fprintf(stderr, "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
if (debug) fprintf(stderr, "get_iPhone(): Unknown error.\n");
if (debug)
fprintf(stderr, "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
......@@ -134,8 +136,10 @@ iphone_error_t iphone_get_device ( iphone_device_t *device ){
*
* @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 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) {
......@@ -159,17 +163,22 @@ iphone_error_t iphone_free_device ( iphone_device_t device ) {
* @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 send_to_phone(iphone_device_t phone, char *data, int datalen)
{
if (!phone)
return -1;
int bytes = 0;
if (!phone) return -1;
if (debug) fprintf(stderr, "send_to_phone: Attempting to send datalen = %i data = %p\n", datalen, data);
if (!phone)
return -1;
if (debug)
fprintf(stderr, "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(debug && bytes < 0)
fprintf(stderr, "send_to_iphone(): libusb gave me the error %d: %s - %s\n", bytes, usb_strerror(), strerror(-bytes));
if (debug && bytes < 0)
fprintf(stderr, "send_to_iphone(): libusb gave me the error %d: %s - %s\n", bytes, usb_strerror(),
strerror(-bytes));
return -1;
} else {
return bytes;
......@@ -186,16 +195,22 @@ int send_to_phone(iphone_device_t phone, char *data, int datalen) {
*
* @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 recv_from_phone(iphone_device_t phone, char *data, int datalen)
{
if (!phone)
return -1;
int bytes = 0;
if (!phone) return -1;
if (debug) fprintf(stderr, "recv_from_phone(): attempting to receive %i bytes\n", datalen);
if (!phone)
return -1;
if (debug)
fprintf(stderr, "recv_from_phone(): attempting to receive %i bytes\n", datalen);
bytes = usb_bulk_read(phone->device, BULKIN, data, datalen, 3500);
if (bytes < 0) {
if(debug) fprintf(stderr, "recv_from_phone(): libusb gave me the error %d: %s (%s)\n", bytes, usb_strerror(), strerror(-bytes));
if (debug)
fprintf(stderr, "recv_from_phone(): libusb gave me the error %d: %s (%s)\n", bytes, usb_strerror(),
strerror(-bytes));
return -1;
}
......
This diff is collapsed.
......@@ -47,7 +47,8 @@ iphone_error_t lockdownd_hello(iphone_lckd_client_t control);
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, char **public_key);
iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_b64, char **host_cert_b64, char **root_cert_b64);
iphone_error_t lockdownd_gen_pair_cert(char *public_key_b64, char **device_cert_b64, char **host_cert_b64,
char **root_cert_b64);
iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *public_key, char *host_id);
void lockdownd_close(iphone_lckd_client_t control);
......
......@@ -34,12 +34,13 @@
int debug = 1;
int main(int argc, char *argv[]) {
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")){
if (argc > 1 && !strcasecmp(argv[1], "--debug")) {
debug = 1;
} else {
debug = 0;
......@@ -50,7 +51,7 @@ int main(int argc, char *argv[]) {
return -1;
}
if (IPHONE_E_SUCCESS != iphone_lckd_new_client(phone, &control)){
if (IPHONE_E_SUCCESS != iphone_lckd_new_client(phone, &control)) {
iphone_free_device(phone);
return -1;
}
......@@ -69,7 +70,8 @@ int main(int argc, char *argv[]) {
if (afc) {
char **dirs = NULL;
iphone_afc_get_dir_list(afc, "/eafaedf", &dirs);
if (!dirs) iphone_afc_get_dir_list(afc, "/", &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]);
......@@ -78,18 +80,19 @@ int main(int argc, char *argv[]) {
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]);
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) {
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", stbuf.st_size);
char *file_data = (char*)malloc(sizeof(char) * 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");
......@@ -98,35 +101,45 @@ int main(int argc, char *argv[]) {
printf("\nClosing my file.\n");
iphone_afc_close_file(afc, my_file);
free(file_data);
} else printf("couldn't open a file\n");
} 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");
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");
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");
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);
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");
if (bytes > 0)
printf("Result: %s\n", threeletterword);
else
printf("Couldn't read!\n");
free(threeletterword);
iphone_afc_close_file(afc, my_file);
......@@ -143,4 +156,3 @@ int main(int argc, char *argv[]) {
return 0;
}
......@@ -40,12 +40,12 @@ const char *plist_base = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
*
* @return The formatted string.
*/
char* format_string(const char* buf, int cols, int depth)
char *format_string(const char *buf, int cols, int depth)
{
int colw = depth + cols + 1;
int len = strlen(buf);
int nlines = len / cols + 1;
char* new_buf = (char*)malloc(nlines * colw + depth + 1);
char *new_buf = (char *) malloc(nlines * colw + depth + 1);
int i = 0;
int j = 0;
......@@ -53,18 +53,18 @@ char* format_string(const char* buf, int cols, int depth)
assert(depth >= 0);
// Inserts new lines and tabs at appropriate locations
for (i = 0; i < nlines; i++){
for (i = 0; i < nlines; i++) {
new_buf[i * colw] = '\n';
for (j = 0; j < depth; j++)
new_buf[i * colw + 1 + j] = '\t';
memcpy(new_buf + i * colw + 1 + depth, buf + i * cols, cols);
}
new_buf[len+(1+depth)*nlines] = '\n';
new_buf[len + (1 + depth) * nlines] = '\n';
// Inserts final row of indentation and termination character
for (j = 0; j < depth; j++)
new_buf[len+(1+depth)*nlines + 1 + j] = '\t';
new_buf[len+(1+depth)*nlines+depth+1] = '\0';
new_buf[len + (1 + depth) * nlines + 1 + j] = '\t';
new_buf[len + (1 + depth) * nlines + depth + 1] = '\0';
return new_buf;
}
......@@ -73,11 +73,13 @@ char* format_string(const char* buf, int cols, int depth)
*
* @return The plist XML document.
*/
xmlDocPtr new_plist() {
xmlDocPtr new_plist()
{
char *plist = strdup(plist_base);
xmlDocPtr plist_xml = xmlReadMemory(plist, strlen(plist), NULL, NULL, 0);
if (!plist_xml) return NULL;
if (!plist_xml)
return NULL;
free(plist);
......@@ -88,8 +90,10 @@ xmlDocPtr new_plist() {
*
* @param plist The XML document to destroy.
*/
void free_plist(xmlDocPtr plist) {
if (!plist) return;
void free_plist(xmlDocPtr plist)
{
if (!plist)
return;
xmlFreeDoc(plist);
}
......@@ -109,13 +113,16 @@ void free_plist(xmlDocPtr plist) {
*
* @return The newly created node.
*/
xmlNode *add_child_to_plist(xmlDocPtr plist, const char *name, const char *content, xmlNode *to_node, int depth) {
xmlNode *add_child_to_plist(xmlDocPtr plist, const char *name, const char *content, xmlNode * to_node, int depth)
{
int i = 0;
xmlNode *child;
if (!plist) return NULL;
if (!plist)
return NULL;
assert(depth >= 0);
if (!to_node) to_node = xmlDocGetRootElement(plist);
if (!to_node)
to_node = xmlDocGetRootElement(plist);
for (i = 0; i < depth; i++) {
xmlNodeAddContent(to_node, "\t");
......@@ -136,7 +143,8 @@ xmlNode *add_child_to_plist(xmlDocPtr plist, const char *name, const char *conte
*
* @return The newly created key node.
*/
xmlNode *add_key_str_dict_element(xmlDocPtr plist, xmlNode *dict, const char *key, const char *value, int depth) {
xmlNode *add_key_str_dict_element(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth)
{
xmlNode *keyPtr;
keyPtr = add_child_to_plist(plist, "key", key, dict, depth);
......@@ -155,7 +163,8 @@ xmlNode *add_key_str_dict_element(xmlDocPtr plist, xmlNode *dict, const char *ke
*
* @return The newly created dict node.
*/
xmlNode *add_key_dict_node(xmlDocPtr plist, xmlNode *dict, const char *key, const char *value, int depth) {
xmlNode *add_key_dict_node(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth)
{
xmlNode *child;
add_child_to_plist(plist, "key", key, dict, depth);
......@@ -174,7 +183,8 @@ xmlNode *add_key_dict_node(xmlDocPtr plist, xmlNode *dict, const char *key, cons
*
* @return The newly created key node.
*/
xmlNode *add_key_data_dict_element(xmlDocPtr plist, xmlNode *dict, const char *key, const char *value, int depth) {
xmlNode *add_key_data_dict_element(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth)
{
xmlNode *keyPtr;
keyPtr = add_child_to_plist(plist, "key", key, dict, depth);
......@@ -190,7 +200,8 @@ xmlNode *add_key_data_dict_element(xmlDocPtr plist, xmlNode *dict, const char *k
* @return An array where each even number is a key and the odd numbers are
* values. If the odd number is \0, that's the end of the list.
*/
char **read_dict_element_strings(xmlNode *dict) {
char **read_dict_element_strings(xmlNode * dict)
{
char **return_me = NULL, **old = NULL;
int current_length = 0;
int current_pos = 0;
......@@ -200,7 +211,7 @@ char **read_dict_element_strings(xmlNode *dict) {
if (!xmlStrcmp(dict_walker->name, "key")) {
current_length += 2;
old = return_me;
return_me = realloc(return_me, sizeof(char*) * current_length);
return_me = realloc(return_me, sizeof(char *) * current_length);
if (!return_me) {
free(old);
return NULL;
......@@ -211,7 +222,7 @@ char **read_dict_element_strings(xmlNode *dict) {
}
old = return_me;
return_me = realloc(return_me, sizeof(char*) * (current_length+1));
return_me = realloc(return_me, sizeof(char *) * (current_length + 1));
return_me[current_pos] = NULL;
return return_me;
......@@ -219,10 +230,12 @@ char **read_dict_element_strings(xmlNode *dict) {
/** Destroys a dictionary as returned by read_dict_element_strings
*/
void free_dictionary(char **dictionary) {
void free_dictionary(char **dictionary)
{
int i = 0;
if (!dictionary) return;
if (!dictionary)
return;
for (i = 0; dictionary[i]; i++) {
free(dictionary[i]);
......@@ -230,4 +243,3 @@ void free_dictionary(char **dictionary) {
free(dictionary);
}
......@@ -25,14 +25,14 @@
#include <libxml/parser.h>
#include <libxml/tree.h>
xmlNode *add_key_dict_node(xmlDocPtr plist, xmlNode *dict, const char *key, const char *value, int depth);
xmlNode *add_key_str_dict_element(xmlDocPtr plist, xmlNode *dict, const char *key, const char *value, int depth);
xmlNode *add_key_data_dict_element(xmlDocPtr plist, xmlNode *dict, const char *key, const char *value, int depth);
xmlNode *add_child_to_plist(xmlDocPtr plist, const char *name, const char *content, xmlNode *to_node, int depth);
xmlNode *add_key_dict_node(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth);
xmlNode *add_key_str_dict_element(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth);
xmlNode *add_key_data_dict_element(xmlDocPtr plist, xmlNode * dict, const char *key, const char *value, int depth);
xmlNode *add_child_to_plist(xmlDocPtr plist, const char *name, const char *content, xmlNode * to_node, int depth);
void free_plist(xmlDocPtr plist);
xmlDocPtr new_plist();
char **read_dict_element_strings(xmlNode *dict);
char **read_dict_element_strings(xmlNode * dict);
void free_dictionary(char **dictionary);
#endif
This diff is collapsed.
......@@ -39,10 +39,11 @@ extern int debug;
/** 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);
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) ))
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);
......@@ -55,26 +56,29 @@ inline void create_config_dir() {
*
* @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;
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);
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)) {
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);
host_id = strdup((char *) loc_host_id);
g_free(loc_host_id);
}
g_key_file_free(key_file);
g_free(config_file);
if (debug) printf("get_host_id(): Using %s as HostID\n",host_id);
if (debug)
printf("get_host_id(): Using %s as HostID\n", host_id);
return host_id;
}
......@@ -85,7 +89,8 @@ char* get_host_id() {
* @return 1 if the iPhone has been connected previously to this configuration
* or 0 otherwise.
*/
int is_device_known(char* uid) {
int is_device_known(char *uid)
{
int ret = 0;
gchar *config_file;
GKeyFile *key_file;
......@@ -93,7 +98,7 @@ int is_device_known(char* uid) {
GIOChannel *keyfile;
/* first get config file */
gchar* device_file = g_strconcat(uid, ".pem", NULL);
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;
......@@ -110,7 +115,8 @@ int is_device_known(char* uid) {
* @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, char* public_key) {
int store_device_public_key(char *uid, char *public_key)
{
if (NULL == public_key || is_device_known(uid))
return 0;
......@@ -119,14 +125,14 @@ int store_device_public_key(char* uid, char* public_key) {
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);
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);
/* decode public key for storing */
gsize decoded_size;
gchar* data = g_base64_decode (public_key, &decoded_size);
gchar *data = g_base64_decode(public_key, &decoded_size);
/* store file */
FILE* pFile = fopen(pem , "wb");
FILE *pFile = fopen(pem, "wb");
fwrite(data, 1, decoded_size, pFile);
fclose(pFile);
g_free(pem);
......@@ -142,7 +148,8 @@ int store_device_public_key(char* uid, char* public_key) {
*
* @return 1 if the file contents where read successfully and 0 otherwise.
*/
int read_file_in_confdir(char* file, gnutls_datum_t* data) {
int read_file_in_confdir(char *file, gnutls_datum_t * data)
{
gboolean success;
gsize size;
char *content;
......@@ -169,7 +176,8 @@ int read_file_in_confdir(char* file, gnutls_datum_t* data) {
*
* @return 1 if the file was successfully read and 0 otherwise.
*/
int get_root_private_key(gnutls_datum_t* root_privkey) {
int get_root_private_key(gnutls_datum_t * root_privkey)
{
return read_file_in_confdir(LIBIPHONE_ROOT_PRIVKEY, root_privkey);
}
......@@ -179,7 +187,8 @@ int get_root_private_key(gnutls_datum_t* root_privkey) {
*
* @return 1 if the file was successfully read and 0 otherwise.
*/
int get_host_private_key(gnutls_datum_t* host_privkey) {
int get_host_private_key(gnutls_datum_t * host_privkey)
{
return read_file_in_confdir(LIBIPHONE_HOST_PRIVKEY, host_privkey);
}
......@@ -189,7 +198,8 @@ int get_host_private_key(gnutls_datum_t* host_privkey) {
*
* @return 1 if the file was successfully read and 0 otherwise.
*/
int get_root_certificate(gnutls_datum_t* root_cert) {
int get_root_certificate(gnutls_datum_t * root_cert)
{
return read_file_in_confdir(LIBIPHONE_ROOT_CERTIF, root_cert);
}
......@@ -199,7 +209,8 @@ int get_root_certificate(gnutls_datum_t* root_cert) {
*
* @return 1 if the file was successfully read and 0 otherwise.
*/
int get_host_certificate(gnutls_datum_t* host_cert) {
int get_host_certificate(gnutls_datum_t * host_cert)
{
return read_file_in_confdir(LIBIPHONE_HOST_CERTIF, host_cert);
}
......@@ -215,30 +226,34 @@ int get_host_certificate(gnutls_datum_t* host_cert) {
*
* @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;
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;
GIOChannel *file;
if (!host_id || !root_key || !host_key || !root_cert || !host_cert)
return 0;
/* Make sure config directory exists*/
/* 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 */
if (debug) printf("init_config_file(): setting hostID to %s\n", host_id);
if (debug)
printf("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);
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);
......@@ -249,26 +264,26 @@ int init_config_file(char* host_id, gnutls_datum_t* root_key, gnutls_datum_t* ho
/* 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 );
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);
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);
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);
pFile = fopen(pem, "wb");
fwrite(host_cert->data, 1, host_cert->size, pFile);
fclose(pFile);
g_free(pem);
......
......@@ -28,44 +28,44 @@
*
* @return the HostID if exist in config file. Returns NULL otherwise.
*/
char* get_host_id();
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);
int is_device_known(char *uid);
/**
* @return 1 if everything went well. Returns 0 otherwise.
*/
int store_device_public_key(char* uid, char* public_key);
int store_device_public_key(char *uid, char *public_key);
/**
* @return 1 if everything went well. Returns 0 otherwise.
*/
int get_root_private_key(gnutls_datum_t* root_privkey);
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);
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);
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);
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);
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