summaryrefslogtreecommitdiffstats
path: root/src/userpref.c
diff options
context:
space:
mode:
authorGravatar Jonathan Beck2008-08-22 18:32:20 +0200
committerGravatar Jonathan Beck2008-08-22 18:32:20 +0200
commit7539f5fbf43a8d5b454cd0f7725cb5247d227647 (patch)
tree09ef1e2aabd356eb485425d3a2a5d551fc765b91 /src/userpref.c
parent6b706ced7bea4223cce3b83f25268130226a9756 (diff)
downloadlibimobiledevice-7539f5fbf43a8d5b454cd0f7725cb5247d227647.tar.gz
libimobiledevice-7539f5fbf43a8d5b454cd0f7725cb5247d227647.tar.bz2
Handle known device through their UniqueDeviceID and use it as name to store PEM device public key.
Diffstat (limited to 'src/userpref.c')
-rw-r--r--src/userpref.c143
1 files changed, 39 insertions, 104 deletions
diff --git a/src/userpref.c b/src/userpref.c
index 41b11bc..5b53775 100644
--- a/src/userpref.c
+++ b/src/userpref.c
@@ -25,7 +25,7 @@
25#include <string.h> 25#include <string.h>
26#include "userpref.h" 26#include "userpref.h"
27#include <string.h> 27#include <string.h>
28#include <stdio.h> 28#include <stdlib.h>
29 29
30#define LIBIPHONE_CONF_DIR "libiphone" 30#define LIBIPHONE_CONF_DIR "libiphone"
31#define LIBIPHONE_CONF_FILE "libiphonerc" 31#define LIBIPHONE_CONF_FILE "libiphonerc"
@@ -37,6 +37,18 @@
37 37
38extern int debug; 38extern int debug;
39 39
40/** Creates a freedesktop compatible configuration directory for libiphone.
41 */
42inline void create_config_dir() {
43 gchar* config_dir = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, NULL);
44
45 if (!g_file_test(config_dir, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR) ))
46 g_mkdir_with_parents(config_dir, 0755);
47
48 g_free(config_dir);
49}
50
51
40/** Reads the HostID from a previously generated configuration file. 52/** Reads the HostID from a previously generated configuration file.
41 * 53 *
42 * @note It is the responsibility of the calling function to free the returned host_id 54 * @note It is the responsibility of the calling function to free the returned host_id
@@ -68,12 +80,12 @@ char* get_host_id() {
68 80
69/** Determines whether this iPhone has been connected to this system before. 81/** Determines whether this iPhone has been connected to this system before.
70 * 82 *
71 * @param public_key The public key as given by the iPhone. 83 * @param uid The device uid as given by the iPhone.
72 * 84 *
73 * @return 1 if the iPhone has been connected previously to this configuration 85 * @return 1 if the iPhone has been connected previously to this configuration
74 * or 0 otherwise. 86 * or 0 otherwise.
75 */ 87 */
76int is_device_known(char* public_key) { 88int is_device_known(char* uid) {
77 int ret = 0; 89 int ret = 0;
78 gchar *config_file; 90 gchar *config_file;
79 GKeyFile *key_file; 91 GKeyFile *key_file;
@@ -81,41 +93,12 @@ int is_device_known(char* public_key) {
81 GIOChannel *keyfile; 93 GIOChannel *keyfile;
82 94
83 /* first get config file */ 95 /* first get config file */
84 config_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL); 96 gchar* device_file = g_strconcat(uid, ".pem", NULL);
85 if (g_file_test(config_file, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))) { 97 config_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, device_file, NULL);
86 98 if (g_file_test(config_file, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)))
87 /* now parse file to get knwon devices list */ 99 ret = 1;
88 key_file = g_key_file_new (); 100 g_free(config_file);
89 if(g_key_file_load_from_file(key_file, config_file, G_KEY_FILE_KEEP_COMMENTS, NULL)) { 101 g_free(device_file);
90
91 devices_list = g_key_file_get_string_list (key_file, "Global", "DevicesList", NULL, NULL);
92 if (devices_list) {
93 pcur = devices_list;
94 while(*pcur && !ret) {
95 /* open associated base64 encoded key */
96 keyfilepath = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, *pcur, NULL);
97 if (g_file_test(keyfilepath, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))) {
98 keyfile = g_io_channel_new_file (keyfilepath, "r", NULL);
99
100 stored_key = NULL;
101 g_io_channel_read_to_end (keyfile, &stored_key, NULL, NULL);
102
103 /* now compare to input */
104 if (strcmp(public_key, stored_key) == 2 || !strcmp(public_key, stored_key))
105 ret = 1;
106 g_free(stored_key);
107 g_io_channel_shutdown(keyfile, FALSE, NULL);
108 g_io_channel_unref(keyfile);
109 pcur++;
110 }
111 g_free(keyfilepath);
112 }
113 }
114 g_strfreev(devices_list);
115 }
116 g_key_file_free(key_file);
117 g_free(config_file);
118 }
119 return ret; 102 return ret;
120} 103}
121 104
@@ -127,65 +110,28 @@ int is_device_known(char* public_key) {
127 * @return 1 on success and 0 if no public key is given or if it has already 110 * @return 1 on success and 0 if no public key is given or if it has already
128 * been marked as connected previously. 111 * been marked as connected previously.
129 */ 112 */
130int store_device_public_key(char* public_key) { 113int store_device_public_key(char* uid, char* public_key) {
131 gchar *config_file;
132 GKeyFile *key_file;
133 gchar **devices_list;
134 guint len = 0;
135 guint wlength = 0;
136 gchar dev_file[20];
137 int i;
138 const gchar** new_devices_list;
139 gsize length;
140 gchar *buf;
141 GIOChannel *file;
142 gchar* device_file;
143 114
144 if (NULL == public_key || is_device_known(public_key)) 115 if (NULL == public_key || is_device_known(uid))
145 return 0; 116 return 0;
146 117
147 /* first get config file */ 118 /* ensure config directory exists */
148 config_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL); 119 create_config_dir();
149 if (g_file_test(config_file, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))) { 120
150 key_file = g_key_file_new(); 121 /* build file path */
151 if(g_key_file_load_from_file (key_file, config_file, G_KEY_FILE_KEEP_COMMENTS, NULL)) { 122 gchar* device_file = g_strconcat(uid, ".pem", NULL);
152 123 gchar* pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, device_file, NULL);
153 /* Determine device name */
154 devices_list = g_key_file_get_string_list (key_file, "Global", "DevicesList", NULL, NULL);
155 if (devices_list)
156 len = g_strv_length(devices_list);
157 g_strfreev(devices_list);
158 g_sprintf(dev_file, "Device%i", len);
159
160 /* Write device file to disk */
161 device_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, dev_file, NULL);
162 file = g_io_channel_new_file (device_file, "w", NULL);
163 g_free(device_file);
164 wlength = strlen(public_key); // why this wasn't discovered before... ugh
165 g_io_channel_write_chars(file, public_key, wlength, NULL, NULL);
166 g_io_channel_shutdown(file, TRUE, NULL);
167 g_io_channel_unref(file);
168
169 /* Append device to list */
170 new_devices_list = (const gchar**)g_malloc(sizeof(gchar*)* (len + 2));
171 for( i = 0; i < len; i++)
172 new_devices_list[i] = devices_list[i];
173 new_devices_list[len] = dev_file;
174 new_devices_list[len+1] = NULL;
175 g_key_file_set_string_list(key_file,"Global", "DevicesList", new_devices_list, len+1);
176 g_free(new_devices_list);
177
178 }
179
180 /* Write config file to disk */
181 buf = g_key_file_to_data(key_file, &length, NULL);
182 file = g_io_channel_new_file(config_file, "w", NULL);
183 g_io_channel_write_chars(file, buf, length, NULL, NULL);
184 g_io_channel_shutdown(file, TRUE, NULL);
185 g_io_channel_unref(file);
186 g_key_file_free(key_file);
187 }
188 124
125 /* decode public key for storing */
126 gsize decoded_size;
127 gchar* data = g_base64_decode (public_key, &decoded_size);
128 /* store file */
129 FILE* pFile = fopen(pem , "wb");
130 fwrite(data, 1, decoded_size, pFile);
131 fclose(pFile);
132 g_free(pem);
133 g_free(data);
134 g_free(device_file);
189 return 1; 135 return 1;
190} 136}
191 137
@@ -257,17 +203,6 @@ int get_host_certificate(gnutls_datum_t* host_cert) {
257 return read_file_in_confdir(LIBIPHONE_HOST_CERTIF, host_cert); 203 return read_file_in_confdir(LIBIPHONE_HOST_CERTIF, host_cert);
258} 204}
259 205
260/** Creates a freedesktop compatible configuration directory for libiphone.
261 */
262inline void create_config_dir() {
263 gchar* config_dir = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, NULL);
264
265 if (!g_file_test(config_dir, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR) ))
266 g_mkdir_with_parents(config_dir, 0755);
267
268 g_free(config_dir);
269}
270
271/** Create and save a configuration file containing the given data. 206/** Create and save a configuration file containing the given data.
272 * 207 *
273 * @note: All fields must specified and be non-null 208 * @note: All fields must specified and be non-null