summaryrefslogtreecommitdiffstats
path: root/src/userpref.c
diff options
context:
space:
mode:
authorGravatar Jonathan Beck2008-12-13 12:21:03 +0100
committerGravatar Jonathan Beck2008-12-13 12:21:03 +0100
commit3fdd24aea06a9bf38d9d34fb8bccbb7023ed3100 (patch)
tree1080d26eca01c885efb33f3f98821a981a25e8b4 /src/userpref.c
parent3d8ba053deeacd74e621469d3d45d1db38ee411a (diff)
downloadlibplist-3fdd24aea06a9bf38d9d34fb8bccbb7023ed3100.tar.gz
libplist-3fdd24aea06a9bf38d9d34fb8bccbb7023ed3100.tar.bz2
Fork libiphone and remove anything non plist specific.
Update library and make related files acordingly .
Diffstat (limited to 'src/userpref.c')
-rw-r--r--src/userpref.c285
1 files changed, 0 insertions, 285 deletions
diff --git a/src/userpref.c b/src/userpref.c
deleted file mode 100644
index b707957..0000000
--- a/src/userpref.c
+++ /dev/null
@@ -1,285 +0,0 @@
1/*
2 * userpref.c
3 * contains methods to access user specific certificates IDs and more.
4 *
5 * Copyright (c) 2008 Jonathan Beck All Rights Reserved.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include <glib.h>
23#include <glib/gprintf.h>
24#include <stdio.h>
25#include <string.h>
26#include "userpref.h"
27#include "utils.h"
28#include <string.h>
29#include <stdlib.h>
30
31#define LIBIPHONE_CONF_DIR "libiphone"
32#define LIBIPHONE_CONF_FILE "libiphonerc"
33
34#define LIBIPHONE_ROOT_PRIVKEY "RootPrivateKey.pem"
35#define LIBIPHONE_HOST_PRIVKEY "HostPrivateKey.pem"
36#define LIBIPHONE_ROOT_CERTIF "RootCertificate.pem"
37#define LIBIPHONE_HOST_CERTIF "HostCertificate.pem"
38
39
40/** Creates a freedesktop compatible configuration directory for libiphone.
41 */
42inline void create_config_dir()
43{
44 gchar *config_dir = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, NULL);
45
46 if (!g_file_test(config_dir, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
47 g_mkdir_with_parents(config_dir, 0755);
48
49 g_free(config_dir);
50}
51
52
53/** Reads the HostID from a previously generated configuration file.
54 *
55 * @note It is the responsibility of the calling function to free the returned host_id
56 *
57 * @return The string containing the HostID or NULL
58 */
59char *get_host_id()
60{
61 char *host_id = NULL;
62 gchar *config_file;
63 GKeyFile *key_file;
64 gchar *loc_host_id;
65
66 config_file =
67 g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL);
68
69 /* now parse file to get the HostID */
70 key_file = g_key_file_new();
71 if (g_key_file_load_from_file(key_file, config_file, G_KEY_FILE_KEEP_COMMENTS, NULL)) {
72 loc_host_id = g_key_file_get_value(key_file, "Global", "HostID", NULL);
73 if (loc_host_id)
74 host_id = strdup((char *) loc_host_id);
75 g_free(loc_host_id);
76 }
77 g_key_file_free(key_file);
78 g_free(config_file);
79
80 log_debug_msg("get_host_id(): Using %s as HostID\n", host_id);
81 return host_id;
82}
83
84/** Determines whether this iPhone has been connected to this system before.
85 *
86 * @param uid The device uid as given by the iPhone.
87 *
88 * @return 1 if the iPhone has been connected previously to this configuration
89 * or 0 otherwise.
90 */
91int is_device_known(char *uid)
92{
93 int ret = 0;
94 gchar *config_file;
95 GKeyFile *key_file;
96 gchar **devices_list, **pcur, *keyfilepath, *stored_key;
97 GIOChannel *keyfile;
98
99 /* first get config file */
100 gchar *device_file = g_strconcat(uid, ".pem", NULL);
101 config_file = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, device_file, NULL);
102 if (g_file_test(config_file, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)))
103 ret = 1;
104 g_free(config_file);
105 g_free(device_file);
106 return ret;
107}
108
109/** Mark the iPhone (as represented by the key) as having connected to this
110 * configuration.
111 *
112 * @param public_key The public key given by the iPhone
113 *
114 * @return 1 on success and 0 if no public key is given or if it has already
115 * been marked as connected previously.
116 */
117int store_device_public_key(char *uid, gnutls_datum_t public_key)
118{
119
120 if (NULL == public_key.data || is_device_known(uid))
121 return 0;
122
123 /* ensure config directory exists */
124 create_config_dir();
125
126 /* build file path */
127 gchar *device_file = g_strconcat(uid, ".pem", NULL);
128 gchar *pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, device_file, NULL);
129
130 /* store file */
131 FILE *pFile = fopen(pem, "wb");
132 fwrite(public_key.data, 1, public_key.size, pFile);
133 fclose(pFile);
134 g_free(pem);
135 g_free(device_file);
136 return 1;
137}
138
139/** Private function which reads the given file into a gnutls structure.
140 *
141 * @param file The filename of the file to read
142 * @param data The pointer at which to store the data.
143 *
144 * @return 1 if the file contents where read successfully and 0 otherwise.
145 */
146int read_file_in_confdir(char *file, gnutls_datum_t * data)
147{
148 gboolean success;
149 gsize size;
150 char *content;
151 gchar *filepath;
152
153 if (NULL == file || NULL == data)
154 return 0;
155
156 /* Read file */
157 filepath = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, file, NULL);
158 success = g_file_get_contents(filepath, &content, &size, NULL);
159 g_free(filepath);
160
161 /* Add it to the gnutls_datnum_t structure */
162 data->data = content;
163 data->size = size;
164
165 return success;
166}
167
168/** Read the root private key
169 *
170 * @param root_privkey A pointer to the appropriate gnutls structure
171 *
172 * @return 1 if the file was successfully read and 0 otherwise.
173 */
174int get_root_private_key(gnutls_datum_t * root_privkey)
175{
176 return read_file_in_confdir(LIBIPHONE_ROOT_PRIVKEY, root_privkey);
177}
178
179/** Read the host private key
180 *
181 * @param host_privkey A pointer to the appropriate gnutls structure
182 *
183 * @return 1 if the file was successfully read and 0 otherwise.
184 */
185int get_host_private_key(gnutls_datum_t * host_privkey)
186{
187 return read_file_in_confdir(LIBIPHONE_HOST_PRIVKEY, host_privkey);
188}
189
190/** Read the root certificate
191 *
192 * @param root_privkey A pointer to the appropriate gnutls structure
193 *
194 * @return 1 if the file was successfully read and 0 otherwise.
195 */
196int get_root_certificate(gnutls_datum_t * root_cert)
197{
198 return read_file_in_confdir(LIBIPHONE_ROOT_CERTIF, root_cert);
199}
200
201/** Read the host certificate
202 *
203 * @param root_privkey A pointer to the appropriate gnutls structure
204 *
205 * @return 1 if the file was successfully read and 0 otherwise.
206 */
207int get_host_certificate(gnutls_datum_t * host_cert)
208{
209 return read_file_in_confdir(LIBIPHONE_HOST_CERTIF, host_cert);
210}
211
212/** Create and save a configuration file containing the given data.
213 *
214 * @note: All fields must specified and be non-null
215 *
216 * @param host_id The UUID of the host
217 * @param root_key The root key
218 * @param host_key The host key
219 * @param root_cert The root certificate
220 * @param host_cert The host certificate
221 *
222 * @return 1 on success and 0 otherwise.
223 */
224int init_config_file(char *host_id, gnutls_datum_t * root_key, gnutls_datum_t * host_key, gnutls_datum_t * root_cert,
225 gnutls_datum_t * host_cert)
226{
227 FILE *pFile;
228 gchar *pem;
229 GKeyFile *key_file;
230 gsize length;
231 gchar *buf, *config_file;
232 GIOChannel *file;
233
234 if (!host_id || !root_key || !host_key || !root_cert || !host_cert)
235 return 0;
236
237 /* Make sure config directory exists */
238 create_config_dir();
239
240 /* Now parse file to get the HostID */
241 key_file = g_key_file_new();
242
243 /* Store in config file */
244 log_debug_msg("init_config_file(): setting hostID to %s\n", host_id);
245 g_key_file_set_value(key_file, "Global", "HostID", host_id);
246
247 /* Write config file on disk */
248 buf = g_key_file_to_data(key_file, &length, NULL);
249 config_file =
250 g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_CONF_FILE, NULL);
251 file = g_io_channel_new_file(config_file, "w", NULL);
252 g_free(config_file);
253 g_io_channel_write_chars(file, buf, length, NULL, NULL);
254 g_io_channel_shutdown(file, TRUE, NULL);
255 g_io_channel_unref(file);
256
257 g_key_file_free(key_file);
258
259 /* Now write keys and certificates to disk */
260 pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_ROOT_PRIVKEY, NULL);
261 pFile = fopen(pem, "wb");
262 fwrite(root_key->data, 1, root_key->size, pFile);
263 fclose(pFile);
264 g_free(pem);
265
266 pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_HOST_PRIVKEY, NULL);
267 pFile = fopen(pem, "wb");
268 fwrite(host_key->data, 1, host_key->size, pFile);
269 fclose(pFile);
270 g_free(pem);
271
272 pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_ROOT_CERTIF, NULL);
273 pFile = fopen(pem, "wb");
274 fwrite(root_cert->data, 1, root_cert->size, pFile);
275 fclose(pFile);
276 g_free(pem);
277
278 pem = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), LIBIPHONE_CONF_DIR, LIBIPHONE_HOST_CERTIF, NULL);
279 pFile = fopen(pem, "wb");
280 fwrite(host_cert->data, 1, host_cert->size, pFile);
281 fclose(pFile);
282 g_free(pem);
283
284 return 1;
285}