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
};
This diff is collapsed.
......@@ -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,28 +93,28 @@ 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);
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 ((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;
}
......@@ -27,43 +27,42 @@
#include <stdlib.h>
#include <string.h>
extern int debug;
extern int debug;
/** Gets a handle to an iPhone
*
* @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;
phone->__device = NULL;
phone->buffer = 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) {
for (bus = busses; bus; bus = bus->next) {
for (dev = bus->devices; dev; dev = dev->next) {
if (dev->descriptor.idVendor == 0x05ac &&
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);
usb_set_configuration(phone->device, 3);
......@@ -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",
bytes, usb_strerror(), strerror(-bytes));
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,15 +117,17 @@ 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
return IPHONE_E_UNKNOWN_ERROR; // if it got to this point it's gotta be bad
}
/** Cleans up an iPhone structure, then frees the structure itself.
......@@ -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) {
......@@ -150,7 +154,7 @@ iphone_error_t iphone_free_device ( iphone_device_t device ) {
free(device);
return ret;
}
/** Sends data to the phone
* This is a low-level (i.e. directly to phone) function.
*
......@@ -159,22 +163,27 @@ 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;
}
return -1;
}
......@@ -186,18 +195,24 @@ 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;
}
return bytes;
}
......@@ -22,7 +22,7 @@
#ifndef IPHONE_H
#define IPHONE_H
#ifndef USBMUX_H
#ifndef USBMUX_H
#include "usbmux.h"
#warning usbmux not included?
#endif
......
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,23 +34,24 @@
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;
}
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)){
if (IPHONE_E_SUCCESS != iphone_lckd_new_client(phone, &control)) {
iphone_free_device(phone);
return -1;
}
......@@ -62,34 +63,36 @@ int main(int argc, char *argv[]) {
}
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);
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]);
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,49 +101,58 @@ 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);
}
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;
}
......@@ -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,12 +73,14 @@ 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);
return plist_xml;
......@@ -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,14 +113,17 @@ 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,12 +143,13 @@ 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);
add_child_to_plist(plist, "string", value, dict, depth);
return keyPtr;
}
......@@ -155,9 +163,10 @@ 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);
child = add_child_to_plist(plist, "dict", value, dict, depth);
......@@ -174,12 +183,13 @@ 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);
add_child_to_plist(plist, "data", format_string(value, 60, depth), dict, depth);
return keyPtr;
}
......@@ -190,17 +200,18 @@ 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;
xmlNode *dict_walker;
for (dict_walker = dict->children; dict_walker; dict_walker = dict_walker->next) {
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;
......@@ -209,25 +220,26 @@ char **read_dict_element_strings(xmlNode *dict) {
return_me[current_pos++] = xmlNodeGetContent(dict_walker->next->next);
}
}
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;
}
/** 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]);
}
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,16 +89,17 @@ 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;
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);
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);
......@@ -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,24 +148,25 @@ 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;
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);
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;
}
......@@ -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);
......@@ -248,27 +263,27 @@ int init_config_file(char* host_id, gnutls_datum_t* root_key, gnutls_datum_t* ho
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 );
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);
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);
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);
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);
......
......@@ -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