summaryrefslogtreecommitdiffstats
path: root/src/userpref.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/userpref.c')
-rw-r--r--src/userpref.c115
1 files changed, 65 insertions, 50 deletions
diff --git a/src/userpref.c b/src/userpref.c
index 5b53775..57946f7 100644
--- a/src/userpref.c
+++ b/src/userpref.c
@@ -39,10 +39,11 @@ extern int debug;
39 39
40/** Creates a freedesktop compatible configuration directory for libiphone. 40/** Creates a freedesktop compatible configuration directory for libiphone.
41 */ 41 */
42inline void create_config_dir() { 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); 43{
44 gchar *config_dir = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, NULL);
44 45
45 if (!g_file_test(config_dir, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR) )) 46 if (!g_file_test(config_dir, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
46 g_mkdir_with_parents(config_dir, 0755); 47 g_mkdir_with_parents(config_dir, 0755);
47 48
48 g_free(config_dir); 49 g_free(config_dir);
@@ -55,26 +56,29 @@ inline void create_config_dir() {
55 * 56 *
56 * @return The string containing the HostID or NULL 57 * @return The string containing the HostID or NULL
57 */ 58 */
58char* get_host_id() { 59char *get_host_id()
59 char* host_id = NULL; 60{
60 gchar* config_file; 61 char *host_id = NULL;
61 GKeyFile* key_file; 62 gchar *config_file;
62 gchar* loc_host_id; 63 GKeyFile *key_file;
64 gchar *loc_host_id;
63 65
64 config_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL); 66 config_file =
67 g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL);
65 68
66 /* now parse file to get the HostID */ 69 /* now parse file to get the HostID */
67 key_file = g_key_file_new(); 70 key_file = g_key_file_new();
68 if(g_key_file_load_from_file(key_file, config_file, G_KEY_FILE_KEEP_COMMENTS, NULL)) { 71 if (g_key_file_load_from_file(key_file, config_file, G_KEY_FILE_KEEP_COMMENTS, NULL)) {
69 loc_host_id = g_key_file_get_value(key_file, "Global", "HostID", NULL); 72 loc_host_id = g_key_file_get_value(key_file, "Global", "HostID", NULL);
70 if (loc_host_id) 73 if (loc_host_id)
71 host_id = strdup((char*)loc_host_id); 74 host_id = strdup((char *) loc_host_id);
72 g_free(loc_host_id); 75 g_free(loc_host_id);
73 } 76 }
74 g_key_file_free(key_file); 77 g_key_file_free(key_file);
75 g_free(config_file); 78 g_free(config_file);
76 79
77 if (debug) printf("get_host_id(): Using %s as HostID\n",host_id); 80 if (debug)
81 printf("get_host_id(): Using %s as HostID\n", host_id);
78 return host_id; 82 return host_id;
79} 83}
80 84
@@ -85,16 +89,17 @@ char* get_host_id() {
85 * @return 1 if the iPhone has been connected previously to this configuration 89 * @return 1 if the iPhone has been connected previously to this configuration
86 * or 0 otherwise. 90 * or 0 otherwise.
87 */ 91 */
88int is_device_known(char* uid) { 92int is_device_known(char *uid)
93{
89 int ret = 0; 94 int ret = 0;
90 gchar *config_file; 95 gchar *config_file;
91 GKeyFile *key_file; 96 GKeyFile *key_file;
92 gchar **devices_list, **pcur, *keyfilepath, *stored_key; 97 gchar **devices_list, **pcur, *keyfilepath, *stored_key;
93 GIOChannel *keyfile; 98 GIOChannel *keyfile;
94 99
95 /* first get config file */ 100 /* first get config file */
96 gchar* device_file = g_strconcat(uid, ".pem", NULL); 101 gchar *device_file = g_strconcat(uid, ".pem", NULL);
97 config_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, device_file, NULL); 102 config_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, device_file, NULL);
98 if (g_file_test(config_file, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))) 103 if (g_file_test(config_file, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)))
99 ret = 1; 104 ret = 1;
100 g_free(config_file); 105 g_free(config_file);
@@ -110,7 +115,8 @@ int is_device_known(char* uid) {
110 * @return 1 on success and 0 if no public key is given or if it has already 115 * @return 1 on success and 0 if no public key is given or if it has already
111 * been marked as connected previously. 116 * been marked as connected previously.
112 */ 117 */
113int store_device_public_key(char* uid, char* public_key) { 118int store_device_public_key(char *uid, char *public_key)
119{
114 120
115 if (NULL == public_key || is_device_known(uid)) 121 if (NULL == public_key || is_device_known(uid))
116 return 0; 122 return 0;
@@ -119,14 +125,14 @@ int store_device_public_key(char* uid, char* public_key) {
119 create_config_dir(); 125 create_config_dir();
120 126
121 /* build file path */ 127 /* build file path */
122 gchar* device_file = g_strconcat(uid, ".pem", NULL); 128 gchar *device_file = g_strconcat(uid, ".pem", NULL);
123 gchar* pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, device_file, NULL); 129 gchar *pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, device_file, NULL);
124 130
125 /* decode public key for storing */ 131 /* decode public key for storing */
126 gsize decoded_size; 132 gsize decoded_size;
127 gchar* data = g_base64_decode (public_key, &decoded_size); 133 gchar *data = g_base64_decode(public_key, &decoded_size);
128 /* store file */ 134 /* store file */
129 FILE* pFile = fopen(pem , "wb"); 135 FILE *pFile = fopen(pem, "wb");
130 fwrite(data, 1, decoded_size, pFile); 136 fwrite(data, 1, decoded_size, pFile);
131 fclose(pFile); 137 fclose(pFile);
132 g_free(pem); 138 g_free(pem);
@@ -142,24 +148,25 @@ int store_device_public_key(char* uid, char* public_key) {
142 * 148 *
143 * @return 1 if the file contents where read successfully and 0 otherwise. 149 * @return 1 if the file contents where read successfully and 0 otherwise.
144 */ 150 */
145int read_file_in_confdir(char* file, gnutls_datum_t* data) { 151int read_file_in_confdir(char *file, gnutls_datum_t * data)
152{
146 gboolean success; 153 gboolean success;
147 gsize size; 154 gsize size;
148 char *content; 155 char *content;
149 gchar *filepath; 156 gchar *filepath;
150 157
151 if (NULL == file || NULL == data) 158 if (NULL == file || NULL == data)
152 return 0; 159 return 0;
153 160
154 /* Read file */ 161 /* Read file */
155 filepath = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, file, NULL); 162 filepath = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, file, NULL);
156 success = g_file_get_contents(filepath, &content, &size, NULL); 163 success = g_file_get_contents(filepath, &content, &size, NULL);
157 g_free(filepath); 164 g_free(filepath);
158 165
159 /* Add it to the gnutls_datnum_t structure */ 166 /* Add it to the gnutls_datnum_t structure */
160 data->data = content; 167 data->data = content;
161 data->size = size; 168 data->size = size;
162 169
163 return success; 170 return success;
164} 171}
165 172
@@ -169,7 +176,8 @@ int read_file_in_confdir(char* file, gnutls_datum_t* data) {
169 * 176 *
170 * @return 1 if the file was successfully read and 0 otherwise. 177 * @return 1 if the file was successfully read and 0 otherwise.
171 */ 178 */
172int get_root_private_key(gnutls_datum_t* root_privkey) { 179int get_root_private_key(gnutls_datum_t * root_privkey)
180{
173 return read_file_in_confdir(LIBIPHONE_ROOT_PRIVKEY, root_privkey); 181 return read_file_in_confdir(LIBIPHONE_ROOT_PRIVKEY, root_privkey);
174} 182}
175 183
@@ -179,7 +187,8 @@ int get_root_private_key(gnutls_datum_t* root_privkey) {
179 * 187 *
180 * @return 1 if the file was successfully read and 0 otherwise. 188 * @return 1 if the file was successfully read and 0 otherwise.
181 */ 189 */
182int get_host_private_key(gnutls_datum_t* host_privkey) { 190int get_host_private_key(gnutls_datum_t * host_privkey)
191{
183 return read_file_in_confdir(LIBIPHONE_HOST_PRIVKEY, host_privkey); 192 return read_file_in_confdir(LIBIPHONE_HOST_PRIVKEY, host_privkey);
184} 193}
185 194
@@ -189,7 +198,8 @@ int get_host_private_key(gnutls_datum_t* host_privkey) {
189 * 198 *
190 * @return 1 if the file was successfully read and 0 otherwise. 199 * @return 1 if the file was successfully read and 0 otherwise.
191 */ 200 */
192int get_root_certificate(gnutls_datum_t* root_cert) { 201int get_root_certificate(gnutls_datum_t * root_cert)
202{
193 return read_file_in_confdir(LIBIPHONE_ROOT_CERTIF, root_cert); 203 return read_file_in_confdir(LIBIPHONE_ROOT_CERTIF, root_cert);
194} 204}
195 205
@@ -199,7 +209,8 @@ int get_root_certificate(gnutls_datum_t* root_cert) {
199 * 209 *
200 * @return 1 if the file was successfully read and 0 otherwise. 210 * @return 1 if the file was successfully read and 0 otherwise.
201 */ 211 */
202int get_host_certificate(gnutls_datum_t* host_cert) { 212int get_host_certificate(gnutls_datum_t * host_cert)
213{
203 return read_file_in_confdir(LIBIPHONE_HOST_CERTIF, host_cert); 214 return read_file_in_confdir(LIBIPHONE_HOST_CERTIF, host_cert);
204} 215}
205 216
@@ -215,30 +226,34 @@ int get_host_certificate(gnutls_datum_t* host_cert) {
215 * 226 *
216 * @return 1 on success and 0 otherwise. 227 * @return 1 on success and 0 otherwise.
217 */ 228 */
218int 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) { 229int init_config_file(char *host_id, gnutls_datum_t * root_key, gnutls_datum_t * host_key, gnutls_datum_t * root_cert,
219 FILE * pFile; 230 gnutls_datum_t * host_cert)
220 gchar* pem; 231{
221 GKeyFile* key_file; 232 FILE *pFile;
233 gchar *pem;
234 GKeyFile *key_file;
222 gsize length; 235 gsize length;
223 gchar *buf, *config_file; 236 gchar *buf, *config_file;
224 GIOChannel* file; 237 GIOChannel *file;
225 238
226 if (!host_id || !root_key || !host_key || !root_cert || !host_cert) 239 if (!host_id || !root_key || !host_key || !root_cert || !host_cert)
227 return 0; 240 return 0;
228 241
229 /* Make sure config directory exists*/ 242 /* Make sure config directory exists */
230 create_config_dir(); 243 create_config_dir();
231 244
232 /* Now parse file to get the HostID */ 245 /* Now parse file to get the HostID */
233 key_file = g_key_file_new(); 246 key_file = g_key_file_new();
234 247
235 /* Store in config file */ 248 /* Store in config file */
236 if (debug) printf("init_config_file(): setting hostID to %s\n", host_id); 249 if (debug)
250 printf("init_config_file(): setting hostID to %s\n", host_id);
237 g_key_file_set_value(key_file, "Global", "HostID", host_id); 251 g_key_file_set_value(key_file, "Global", "HostID", host_id);
238 252
239 /* Write config file on disk */ 253 /* Write config file on disk */
240 buf = g_key_file_to_data(key_file, &length,NULL); 254 buf = g_key_file_to_data(key_file, &length, NULL);
241 config_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL); 255 config_file =
256 g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL);
242 file = g_io_channel_new_file(config_file, "w", NULL); 257 file = g_io_channel_new_file(config_file, "w", NULL);
243 g_free(config_file); 258 g_free(config_file);
244 g_io_channel_write_chars(file, buf, length, NULL, NULL); 259 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
248 g_key_file_free(key_file); 263 g_key_file_free(key_file);
249 264
250 /* Now write keys and certificates to disk */ 265 /* Now write keys and certificates to disk */
251 pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_ROOT_PRIVKEY, NULL); 266 pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_ROOT_PRIVKEY, NULL);
252 pFile = fopen(pem , "wb"); 267 pFile = fopen(pem, "wb");
253 fwrite(root_key->data, 1 , root_key->size , pFile ); 268 fwrite(root_key->data, 1, root_key->size, pFile);
254 fclose(pFile); 269 fclose(pFile);
255 g_free(pem); 270 g_free(pem);
256 271
257 pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_HOST_PRIVKEY, NULL); 272 pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_HOST_PRIVKEY, NULL);
258 pFile = fopen(pem , "wb"); 273 pFile = fopen(pem, "wb");
259 fwrite(host_key->data, 1 , host_key->size , pFile); 274 fwrite(host_key->data, 1, host_key->size, pFile);
260 fclose(pFile); 275 fclose(pFile);
261 g_free(pem); 276 g_free(pem);
262 277
263 pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_ROOT_CERTIF, NULL); 278 pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_ROOT_CERTIF, NULL);
264 pFile = fopen(pem , "wb"); 279 pFile = fopen(pem, "wb");
265 fwrite(root_cert->data, 1 , root_cert->size , pFile); 280 fwrite(root_cert->data, 1, root_cert->size, pFile);
266 fclose(pFile); 281 fclose(pFile);
267 g_free(pem); 282 g_free(pem);
268 283
269 pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_HOST_CERTIF, NULL); 284 pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_HOST_CERTIF, NULL);
270 pFile = fopen(pem , "wb"); 285 pFile = fopen(pem, "wb");
271 fwrite(host_cert->data, 1 , host_cert->size , pFile); 286 fwrite(host_cert->data, 1, host_cert->size, pFile);
272 fclose(pFile); 287 fclose(pFile);
273 g_free(pem); 288 g_free(pem);
274 289