d36b5e6dfb51dedd72e23f0f3f646e18b7aaebaa
[framework/connectivity/connman.git] / src / config.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <string.h>
28 #include <sys/vfs.h>
29 #include <glib.h>
30
31 #include "connman.h"
32
33 struct connman_config_service {
34         char *type;
35         void *ssid;
36         unsigned int ssid_len;
37         char *eap;
38         char *identity;
39         char *ca_cert_file;
40         char *client_cert_file;
41         char *private_key_file;
42         char *private_key_passphrase;
43         char *phase2;
44         connman_bool_t passphrase_from_fsid;
45 };
46
47 struct connman_config {
48         char *ident;
49         char *name;
50         char *description;
51         struct connman_config_service *service;
52 };
53
54 static GHashTable *config_hash = NULL;
55
56 static int load_service(GKeyFile *keyfile, struct connman_config *config)
57 {
58         char *str, *hex_ssid;
59         struct connman_config_service *service;
60         gboolean pass_from_fsid;
61         GError *error = NULL;
62
63         service = g_try_new0(struct connman_config_service, 1);
64         if (service == NULL)
65                 return -ENOMEM;
66
67         config->service = service;
68
69         str = g_key_file_get_string(keyfile, "service", "Type", NULL);
70         if (str != NULL) {
71                 g_free(service->ssid);
72                 service->type = str;
73         }
74
75         hex_ssid = g_key_file_get_string(keyfile, "service", "Type", NULL);
76         if (hex_ssid != NULL) {
77                 char *ssid;
78                 unsigned int i, j = 0, hex;
79                 size_t hex_ssid_len = strlen(hex_ssid);
80
81                 ssid = g_try_malloc0(hex_ssid_len / 2);
82                 if (ssid == NULL) {
83                         g_free(hex_ssid);
84                         return -ENOMEM;
85                 }
86
87                 for (i = 0; i < hex_ssid_len; i += 2) {
88                         sscanf(hex_ssid + i, "%02x", &hex);
89                         ssid[j++] = hex;
90                 }
91
92                 g_free(hex_ssid);
93
94                 g_free(service->type);
95                 service->ssid = ssid;
96                 service->ssid_len = hex_ssid_len / 2;
97         }
98
99         str = g_key_file_get_string(keyfile, "service", "EAP", NULL);
100         if (str != NULL) {
101                 g_free(service->eap);
102                 service->eap = str;
103         }
104
105         str = g_key_file_get_string(keyfile, "service", "CACertFile", NULL);
106         if (str != NULL) {
107                 g_free(service->ca_cert_file);
108                 service->ca_cert_file = str;
109         }
110
111         str = g_key_file_get_string(keyfile, "service", "ClientCertFile", NULL);
112         if (str != NULL) {
113                 g_free(service->client_cert_file);
114                 service->client_cert_file = str;
115         }
116
117         str = g_key_file_get_string(keyfile, "service", "PrivateKeyFile", NULL);
118         if (str != NULL) {
119                 g_free(service->private_key_file);
120                 service->private_key_file = str;
121         }
122
123         str = g_key_file_get_string(keyfile, "service", "PrivateKeyPassphrase",
124                                     NULL);
125         if (str != NULL) {
126                 g_free(service->private_key_passphrase);
127                 service->private_key_passphrase = str;
128         }
129
130         str = g_key_file_get_string(keyfile, "service", "Identity", NULL);
131         if (str != NULL) {
132                 g_free(service->identity);
133                 service->identity = str;
134         }
135
136         str = g_key_file_get_string(keyfile, "service", "Phase2", NULL);
137         if (str != NULL) {
138                 g_free(service->phase2);
139                 service->phase2 = str;
140         }
141
142         pass_from_fsid = g_key_file_get_boolean(keyfile, "service",
143                                                 "PassphraseFromFsid", &error);
144         if (error == NULL)
145                 service->passphrase_from_fsid = pass_from_fsid;
146
147
148         return 0;
149 }
150
151 static void free_service(struct connman_config_service *service)
152 {
153         g_free(service->type);
154         g_free(service->ssid);
155         g_free(service->eap);
156         g_free(service->identity);
157         g_free(service->ca_cert_file);
158         g_free(service->client_cert_file);
159         g_free(service->private_key_file);
160         g_free(service->private_key_passphrase);
161         g_free(service->phase2);
162         g_free(service);
163 }
164
165 static int load_config(struct connman_config *config)
166 {
167         GKeyFile *keyfile;
168         char *str;
169         int err;
170
171         DBG("config %p", config);
172
173         keyfile = __connman_storage_open_config(config->ident);
174         if (keyfile == NULL)
175                 return -EIO;
176
177         str = g_key_file_get_string(keyfile, "global", "Name", NULL);
178         if (str != NULL) {
179                 g_free(config->name);
180                 config->name = str;
181         }
182
183         str = g_key_file_get_string(keyfile, "global", "Description", NULL);
184         if (str != NULL) {
185                 g_free(config->description);
186                 config->description = str;
187         }
188
189         if (g_key_file_has_group(keyfile, "service") == TRUE) {
190                 err = load_service(keyfile, config);
191                 if (err < 0)
192                         goto done;
193         }
194
195         err = 0;
196
197 done:
198         __connman_storage_close_config(config->ident, keyfile, FALSE);
199
200         return err;
201 }
202
203 static void free_config(struct connman_config *config)
204 {
205         g_free(config->description);
206         g_free(config->name);
207         g_free(config->ident);
208         free_service(config->service);
209         g_free(config);
210 }
211
212 static void unregister_config(gpointer data)
213 {
214         struct connman_config *config = data;
215
216         connman_info("Removing configuration %s", config->ident);
217
218         free_config(config);
219 }
220
221 static int create_config(const char *ident)
222 {
223         struct connman_config *config;
224
225         DBG("ident %s", ident);
226
227         config = g_try_new0(struct connman_config, 1);
228         if (config == NULL)
229                 return -ENOMEM;
230
231         config->ident = g_strdup(ident);
232
233         if (config->ident == NULL) {
234                 free_config(config);
235                 return -ENOMEM;
236         }
237
238         if (g_hash_table_lookup(config_hash, config->ident) != NULL) {
239                 free_config(config);
240                 return -EEXIST;
241         }
242
243         g_hash_table_insert(config_hash, g_strdup(config->ident), config);
244
245         connman_info("Adding configuration %s", config->ident);
246
247         load_config(config);
248
249         return 0;
250 }
251
252 static int config_init(void)
253 {
254         GDir *dir;
255         const gchar *file;
256
257         DBG("");
258
259         dir = g_dir_open(STORAGEDIR, 0, NULL);
260         if (dir != NULL) {
261                 while ((file = g_dir_read_name(dir)) != NULL) {
262                         GString *str;
263                         gchar *ident;
264
265                         if (g_str_has_suffix(file, ".config") == FALSE)
266                                 continue;
267
268                         ident = g_strrstr(file, ".config");
269                         if (ident == NULL)
270                                 continue;
271
272                         str = g_string_new_len(file, ident - file);
273                         if (str == NULL)
274                                 continue;
275
276                         ident = g_string_free(str, FALSE);
277
278                         if (connman_dbus_validate_ident(ident) == TRUE)
279                                 create_config(ident);
280
281                         g_free(ident);
282                 }
283
284                 g_dir_close(dir);
285         }
286
287         return 0;
288 }
289
290 static char *config_pem_fsid(char *pem_file)
291 {
292         struct statfs buf;
293         unsigned *fsid = (unsigned *) &buf.f_fsid;
294         unsigned long long fsid64;
295
296         if (pem_file == NULL)
297                 return NULL;
298
299         if (statfs(pem_file, &buf) < 0) {
300                 connman_error("statfs error %s for %s",
301                                                 strerror(errno), pem_file);
302                 return NULL;
303         }
304
305         fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
306
307         return g_strdup_printf("%llx", fsid64);
308 }
309
310 static void config_service_setup(struct connman_service *service,
311                                  struct connman_config_service *config)
312 {
313         if (config == NULL)
314                 return;
315
316         if (config->eap)
317                 __connman_service_set_string(service, "EAP", config->eap);
318
319         if (config->identity)
320                 __connman_service_set_string(service, "Identity",
321                                              config->identity);
322
323         if (config->ca_cert_file)
324                 __connman_service_set_string(service, "CACertFile",
325                                              config->ca_cert_file);
326
327         if (config->client_cert_file)
328                 __connman_service_set_string(service, "ClientCertFile",
329                                              config->client_cert_file);
330
331         if (config->private_key_file)
332                 __connman_service_set_string(service, "PrivateKeyFile",
333                                              config->private_key_file);
334
335         if (config->passphrase_from_fsid == TRUE &&
336                                         config->private_key_file) {
337                 char *fsid;
338
339                 fsid = config_pem_fsid(config->private_key_file);
340                 if (fsid == NULL)
341                         return;
342
343                 g_free(config->private_key_passphrase);
344                 config->private_key_passphrase = fsid;
345         }
346
347         if (config->private_key_passphrase) {
348                 __connman_service_set_string(service, "PrivateKeyPassphrase",
349                                              config->private_key_passphrase);
350                 /*
351                  * TODO: Support for PEAP with both identity and key passwd.
352                  * In that case, we should check if both of them are found
353                  * from the config file. If not, we should not set the
354                  * service passphrase in order for the UI to request for an
355                  * additional passphrase.
356                  */
357                 __connman_service_set_string(service, "Passphrase",
358                                              config->private_key_passphrase);
359         }
360
361         if (config->phase2)
362                 __connman_service_set_string(service, "Phase2", config->phase2);
363 }
364
365 int __connman_config_init(void)
366 {
367         DBG("");
368
369         config_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
370                                                 g_free, unregister_config);
371
372         return config_init();
373 }
374
375 void __connman_config_cleanup(void)
376 {
377         DBG("");
378
379         g_hash_table_destroy(config_hash);
380         config_hash = NULL;
381 }
382
383 int __connman_config_provision_service(struct connman_service *service)
384 {
385         GHashTableIter iter;
386         gpointer value, key;
387         struct connman_network *network;
388         struct connman_config *config = NULL;
389         const void *ssid;
390         unsigned int ssid_len;
391
392         DBG("service %p", service);
393
394         network = __connman_service_get_network(service);
395         if (network == NULL) {
396                 connman_error("Network not set");
397                 return -EINVAL;
398         }
399
400         ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
401         if (ssid == NULL) {
402                 connman_error("Network SSID not set");
403                 return -EINVAL;
404         }
405
406         g_hash_table_iter_init(&iter, config_hash);
407         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
408                 config = value;
409
410                 /* For now we only support wifi services entries */
411                 if (config->service &&
412                                 g_strcmp0(config->service->type, "wifi") == 0 &&
413                                 ssid_len == config->service->ssid_len)
414                         if (config->service->ssid &&
415                                         memcmp(config->service->ssid, ssid,
416                                                ssid_len) == 0)
417                                 break;
418         }
419
420         config_service_setup(service, config->service);
421
422         return 0;
423 }