diff options
| author | 2008-08-03 20:21:23 +0200 | |
|---|---|---|
| committer | 2008-08-05 23:28:10 -0700 | |
| commit | c58482a7dbb3978bacfb5ffd8085d915bcde297d (patch) | |
| tree | c7c42d6359e99f092a6db0066f460dafa367bfcd /src | |
| parent | b6ed54477c00e2b4572e898fe0a47aa64a028c26 (diff) | |
| download | libimobiledevice-c58482a7dbb3978bacfb5ffd8085d915bcde297d.tar.gz libimobiledevice-c58482a7dbb3978bacfb5ffd8085d915bcde297d.tar.bz2 | |
prepare config for pairing
Signed-off-by: Matt Colyer <matt@colyer.name>
Diffstat (limited to 'src')
| -rw-r--r-- | src/userpref.c | 230 | ||||
| -rw-r--r-- | src/userpref.h | 44 |
2 files changed, 265 insertions, 9 deletions
diff --git a/src/userpref.c b/src/userpref.c index bc92f96..399e054 100644 --- a/src/userpref.c +++ b/src/userpref.c | |||
| @@ -29,27 +29,239 @@ | |||
| 29 | 29 | ||
| 30 | extern int debug; | 30 | extern int debug; |
| 31 | 31 | ||
| 32 | inline void create_config_dir() { | ||
| 33 | gchar* config_dir = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, NULL); | ||
| 34 | g_mkdir_with_parents (config_dir, 755); | ||
| 35 | return; | ||
| 36 | } | ||
| 37 | |||
| 32 | char* get_host_id() | 38 | char* get_host_id() |
| 33 | { | 39 | { |
| 34 | char* host_id = NULL; | 40 | char* host_id = NULL; |
| 35 | gchar* config_file = NULL; | 41 | gchar* config_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL); |
| 42 | |||
| 43 | /* now parse file to get the HostID */ | ||
| 44 | GKeyFile* key_file = g_key_file_new (); | ||
| 45 | if( g_key_file_load_from_file (key_file, config_file, G_KEY_FILE_KEEP_COMMENTS, NULL) ) { | ||
| 46 | |||
| 47 | gchar* loc_host_id = g_key_file_get_value(key_file, "Global", "HostID", NULL); | ||
| 48 | if (loc_host_id) | ||
| 49 | host_id = strdup((char*)loc_host_id); | ||
| 50 | g_free(loc_host_id); | ||
| 51 | } | ||
| 52 | g_key_file_free(key_file); | ||
| 53 | |||
| 54 | if (debug) printf("Using %s as HostID\n",host_id); | ||
| 55 | return host_id; | ||
| 56 | } | ||
| 57 | |||
| 58 | int is_device_known(char* public_key) | ||
| 59 | { | ||
| 60 | int ret = 0; | ||
| 36 | 61 | ||
| 37 | /* first get config file */ | 62 | /* first get config file */ |
| 38 | config_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL); | 63 | gchar* config_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL); |
| 39 | if (g_file_test(config_file, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))) { | 64 | if (g_file_test(config_file, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))) { |
| 40 | 65 | ||
| 41 | /* now parse file to get the HostID */ | 66 | /* now parse file to get knwon devices list */ |
| 42 | GKeyFile* key_file = g_key_file_new (); | 67 | GKeyFile* key_file = g_key_file_new (); |
| 43 | if( g_key_file_load_from_file (key_file, config_file, G_KEY_FILE_KEEP_COMMENTS, NULL) ) { | 68 | if( g_key_file_load_from_file (key_file, config_file, G_KEY_FILE_KEEP_COMMENTS, NULL) ) { |
| 44 | 69 | ||
| 45 | gchar* loc_host_id = g_key_file_get_value(key_file, "Global", "HostID", NULL); | 70 | gchar** devices_list = g_key_file_get_string_list (key_file, "Global", "DevicesList", NULL, NULL); |
| 46 | if (loc_host_id) | 71 | if (devices_list) { |
| 47 | host_id = strdup((char*)loc_host_id); | 72 | gchar** pcur = devices_list; |
| 48 | g_free(loc_host_id); | 73 | while(*pcur && !ret) { |
| 74 | /* open associated base64 encoded key */ | ||
| 75 | gchar* keyfilepath = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, *pcur, NULL); | ||
| 76 | if (g_file_test(keyfilepath, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))) { | ||
| 77 | GIOChannel* keyfile = g_io_channel_new_file (keyfilepath, "r", NULL); | ||
| 78 | |||
| 79 | gchar* stored_key = NULL; | ||
| 80 | g_io_channel_read_to_end (keyfile, &stored_key, NULL, NULL); | ||
| 81 | |||
| 82 | /* now compare to input */ | ||
| 83 | if (strcmp(public_key, stored_key) == 2) | ||
| 84 | ret = 1; | ||
| 85 | g_free(stored_key); | ||
| 86 | g_io_channel_shutdown(keyfile, FALSE, NULL); | ||
| 87 | pcur++; | ||
| 88 | } | ||
| 89 | } | ||
| 90 | } | ||
| 91 | g_strfreev(devices_list); | ||
| 49 | } | 92 | } |
| 50 | g_key_file_free(key_file); | 93 | g_key_file_free(key_file); |
| 51 | } | 94 | } |
| 52 | if (debug) printf("Using %s as HostID\n",host_id); | 95 | return ret; |
| 53 | return host_id; | 96 | } |
| 97 | |||
| 98 | int store_device_public_key(char* public_key) | ||
| 99 | { | ||
| 100 | if (NULL == public_key || is_device_known(public_key)) | ||
| 101 | return 0; | ||
| 102 | |||
| 103 | /* first get config file */ | ||
| 104 | gchar* config_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL); | ||
| 105 | if (g_file_test(config_file, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))) { | ||
| 106 | |||
| 107 | |||
| 108 | GKeyFile* key_file = g_key_file_new (); | ||
| 109 | if( g_key_file_load_from_file (key_file, config_file, G_KEY_FILE_KEEP_COMMENTS, NULL) ) { | ||
| 110 | |||
| 111 | gchar** devices_list = g_key_file_get_string_list (key_file, "Global", "DevicesList", NULL, NULL); | ||
| 112 | |||
| 113 | guint length = 0; | ||
| 114 | if (devices_list) | ||
| 115 | g_strv_length(devices_list); | ||
| 116 | g_strfreev(devices_list); | ||
| 117 | |||
| 118 | gchar dev_file[20]; | ||
| 119 | g_sprintf (dev_file, "Device%i", length); | ||
| 120 | |||
| 121 | gchar* device_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, dev_file, NULL); | ||
| 122 | GIOChannel* file = g_io_channel_new_file (device_file, "w", NULL); | ||
| 123 | g_io_channel_write_chars (file, public_key, length, NULL, NULL); | ||
| 124 | g_io_channel_shutdown(file, FALSE, NULL); | ||
| 125 | |||
| 126 | /* append device to list */ | ||
| 127 | gchar** new_devices_list = (gchar**)g_malloc(sizeof(gchar*)* (length + 1)); | ||
| 128 | int i; | ||
| 129 | for( i = 0; i < length; i++) | ||
| 130 | new_devices_list[i] = devices_list[i]; | ||
| 131 | new_devices_list[length] = dev_file; | ||
| 132 | new_devices_list[length+1] = NULL; | ||
| 133 | g_key_file_set_string_list (key_file,"Global", "DevicesList", new_devices_list, length+1); | ||
| 134 | g_free(new_devices_list); | ||
| 135 | |||
| 136 | } | ||
| 137 | gsize length; | ||
| 138 | gchar* buf = g_key_file_to_data (key_file, &length,NULL); | ||
| 139 | GIOChannel* file = g_io_channel_new_file (config_file, "w", NULL); | ||
| 140 | g_io_channel_write_chars (file, buf, length, NULL, NULL); | ||
| 141 | g_io_channel_shutdown(file, FALSE, NULL); | ||
| 142 | g_key_file_free(key_file); | ||
| 143 | } | ||
| 144 | |||
| 145 | return 1; | ||
| 146 | } | ||
| 147 | |||
| 148 | |||
| 149 | char* get_root_private_key() | ||
| 150 | { | ||
| 151 | char* private_key = NULL; | ||
| 152 | |||
| 153 | /* first get config file */ | ||
| 154 | gchar* config_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL); | ||
| 155 | if (g_file_test(config_file, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))) { | ||
| 156 | |||
| 157 | /* now parse file to get knwon devices list */ | ||
| 158 | GKeyFile* key_file = g_key_file_new (); | ||
| 159 | if( g_key_file_load_from_file (key_file, config_file, G_KEY_FILE_KEEP_COMMENTS, NULL) ) { | ||
| 160 | |||
| 161 | gchar* loc_private_key = g_key_file_get_value(key_file, "Global", "RootPrivateKey", NULL); | ||
| 162 | if (loc_private_key) | ||
| 163 | private_key = strdup((char*)loc_private_key); | ||
| 164 | g_free(loc_private_key); | ||
| 165 | } | ||
| 166 | g_key_file_free(key_file); | ||
| 167 | } | ||
| 168 | return private_key; | ||
| 54 | } | 169 | } |
| 55 | 170 | ||
| 171 | char* get_host_private_key() | ||
| 172 | { | ||
| 173 | char* private_key = NULL; | ||
| 174 | |||
| 175 | /* first get config file */ | ||
| 176 | gchar* config_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL); | ||
| 177 | if (g_file_test(config_file, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))) { | ||
| 178 | |||
| 179 | /* now parse file to get knwon devices list */ | ||
| 180 | GKeyFile* key_file = g_key_file_new (); | ||
| 181 | if( g_key_file_load_from_file (key_file, config_file, G_KEY_FILE_KEEP_COMMENTS, NULL) ) { | ||
| 182 | |||
| 183 | gchar* loc_private_key = g_key_file_get_value(key_file, "Global", "HostPrivateKey", NULL); | ||
| 184 | if (loc_private_key) | ||
| 185 | private_key = strdup((char*)loc_private_key); | ||
| 186 | g_free(loc_private_key); | ||
| 187 | } | ||
| 188 | g_key_file_free(key_file); | ||
| 189 | } | ||
| 190 | return private_key; | ||
| 191 | } | ||
| 192 | |||
| 193 | |||
| 194 | char* get_root_certificate() | ||
| 195 | { | ||
| 196 | char* cert = NULL; | ||
| 197 | |||
| 198 | /* first get config file */ | ||
| 199 | gchar* config_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL); | ||
| 200 | if (g_file_test(config_file, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))) { | ||
| 201 | |||
| 202 | /* now parse file to get knwon devices list */ | ||
| 203 | GKeyFile* key_file = g_key_file_new (); | ||
| 204 | if( g_key_file_load_from_file (key_file, config_file, G_KEY_FILE_KEEP_COMMENTS, NULL) ) { | ||
| 205 | |||
| 206 | gchar* loc_cert = g_key_file_get_value(key_file, "Global", "RootCertificate", NULL); | ||
| 207 | if (loc_cert) | ||
| 208 | cert = strdup((char*)loc_cert); | ||
| 209 | g_free(loc_cert); | ||
| 210 | } | ||
| 211 | g_key_file_free(key_file); | ||
| 212 | } | ||
| 213 | return cert; | ||
| 214 | } | ||
| 215 | |||
| 216 | char* get_host_certificate() | ||
| 217 | { | ||
| 218 | char* cert = NULL; | ||
| 219 | |||
| 220 | /* first get config file */ | ||
| 221 | gchar* config_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL); | ||
| 222 | if (g_file_test(config_file, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))) { | ||
| 223 | |||
| 224 | /* now parse file to get knwon devices list */ | ||
| 225 | GKeyFile* key_file = g_key_file_new (); | ||
| 226 | if( g_key_file_load_from_file (key_file, config_file, G_KEY_FILE_KEEP_COMMENTS, NULL) ) { | ||
| 227 | |||
| 228 | gchar* loc_cert = g_key_file_get_value(key_file, "Global", "HostCertificate", NULL); | ||
| 229 | if (loc_cert) | ||
| 230 | cert = strdup((char*)loc_cert); | ||
| 231 | g_free(loc_cert); | ||
| 232 | } | ||
| 233 | g_key_file_free(key_file); | ||
| 234 | } | ||
| 235 | return cert; | ||
| 236 | } | ||
| 237 | |||
| 238 | int init_config_file(char* host_id, char* root_private_key, char* host_private_key, char* root_cert, char* host_cert) | ||
| 239 | { | ||
| 240 | if (!host_id || !root_private_key || !host_private_key) | ||
| 241 | return 0; | ||
| 242 | |||
| 243 | gchar* config_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL); | ||
| 244 | /* make sure config directory exists*/ | ||
| 245 | create_config_dir(); | ||
| 246 | |||
| 247 | /* now parse file to get the HostID */ | ||
| 248 | GKeyFile* key_file = g_key_file_new (); | ||
| 249 | |||
| 250 | /* store in config file */ | ||
| 251 | g_key_file_set_value (key_file, "Global", "HostID", host_id); | ||
| 252 | g_key_file_set_value (key_file, "Global", "RootPrivateKey", root_private_key); | ||
| 253 | g_key_file_set_value (key_file, "Global", "HostPrivateKey", host_private_key); | ||
| 254 | g_key_file_set_value (key_file, "Global", "RootCertificate", root_cert); | ||
| 255 | g_key_file_set_value (key_file, "Global", "HostCertificate", host_cert); | ||
| 256 | |||
| 257 | /* write config file on disk */ | ||
| 258 | gsize length; | ||
| 259 | gchar* buf = g_key_file_to_data (key_file, &length,NULL); | ||
| 260 | GIOChannel* file = g_io_channel_new_file (config_file, "w", NULL); | ||
| 261 | g_io_channel_write_chars (file, buf, length, NULL, NULL); | ||
| 262 | g_io_channel_shutdown(file, FALSE, NULL); | ||
| 263 | |||
| 264 | g_key_file_free(key_file); | ||
| 265 | |||
| 266 | return 1; | ||
| 267 | } | ||
diff --git a/src/userpref.h b/src/userpref.h index b097084..cef0bed 100644 --- a/src/userpref.h +++ b/src/userpref.h | |||
| @@ -29,4 +29,48 @@ | |||
| 29 | */ | 29 | */ |
| 30 | char* get_host_id(); | 30 | char* get_host_id(); |
| 31 | 31 | ||
| 32 | /** | ||
| 33 | * \fn int is_device_known(char* public_key) | ||
| 34 | * determine if we already paired this device. | ||
| 35 | * \return 1 if device is already paired. Returns 0 otherwise. | ||
| 36 | */ | ||
| 37 | int is_device_known(char* public_key); | ||
| 38 | |||
| 39 | /** | ||
| 40 | * \fn int store_device_public_key(char* public_key) | ||
| 41 | * \return 1 if everything went well. Returns 0 otherwise. | ||
| 42 | */ | ||
| 43 | int store_device_public_key(char* public_key); | ||
| 44 | |||
| 45 | /** | ||
| 46 | * \fn char* get_root_private_key() | ||
| 47 | * \return RootPrivateKey if exists. Returns NULL otherwise. | ||
| 48 | */ | ||
| 49 | char* get_root_private_key(); | ||
| 50 | |||
| 51 | /** | ||
| 52 | * \fn char* get_host_private_key() | ||
| 53 | * \return HostPrivateKey if exists. Returns NULL otherwise. | ||
| 54 | */ | ||
| 55 | char* get_host_private_key(); | ||
| 56 | |||
| 57 | /** | ||
| 58 | * \fn char* get_root_certificate() | ||
| 59 | * \return RootCertificate if exists. Returns NULL otherwise. | ||
| 60 | */ | ||
| 61 | char* get_root_certificate(); | ||
| 62 | |||
| 63 | /** | ||
| 64 | * \fn char* get_host_certificate() | ||
| 65 | * \return HostCertificate if exists. Returns NULL otherwise. | ||
| 66 | */ | ||
| 67 | char* get_host_certificate(); | ||
| 68 | |||
| 69 | /** | ||
| 70 | * \fn int init_config_file(char* host_id, char* root_private_key, char* host_private_key, char* root_cert, char* host_cert) | ||
| 71 | * setup a brand new config file. | ||
| 72 | * \return 1 if everything went well. Returns 0 otherwise. | ||
| 73 | */ | ||
| 74 | int init_config_file(char* host_id, char* root_private_key, char* host_private_key, char* root_cert, char* host_cert); | ||
| 32 | #endif | 75 | #endif |
| 76 | |||
