Add support for multiple services per configuration file
[framework/connectivity/connman.git] / src / config.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  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 *ident;
35         char *name;
36         char *type;
37         void *ssid;
38         unsigned int ssid_len;
39         char *eap;
40         char *identity;
41         char *ca_cert_file;
42         char *client_cert_file;
43         char *private_key_file;
44         char *private_key_passphrase;
45         char *phase2;
46         connman_bool_t passphrase_from_fsid;
47 };
48
49 struct connman_config {
50         char *ident;
51         char *name;
52         char *description;
53         GHashTable *service_table;
54 };
55
56 static GHashTable *config_table = NULL;
57
58 static void unregister_config(gpointer data)
59 {
60         struct connman_config *config = data;
61
62         connman_info("Removing configuration %s", config->ident);
63
64         g_hash_table_destroy(config->service_table);
65
66         g_free(config->description);
67         g_free(config->name);
68         g_free(config->ident);
69         g_free(config);
70 }
71
72 static void unregister_service(gpointer data)
73 {
74         struct connman_config_service *service = data;
75
76         connman_info("Removing service configuration %s", service->ident);
77
78         g_free(service->ident);
79         g_free(service->type);
80         g_free(service->name);
81         g_free(service->ssid);
82         g_free(service->eap);
83         g_free(service->identity);
84         g_free(service->ca_cert_file);
85         g_free(service->client_cert_file);
86         g_free(service->private_key_file);
87         g_free(service->private_key_passphrase);
88         g_free(service->phase2);
89         g_free(service);
90 }
91
92 static int load_service(GKeyFile *keyfile, const char *group,
93                                                 struct connman_config *config)
94 {
95         struct connman_config_service *service;
96         const char *ident;
97         char *str, *hex_ssid;
98         gboolean pass_from_fsid;
99         GError *error = NULL;
100
101         /* Strip off "service_" prefix */
102         ident = group + 8;
103
104         if (strlen(ident) < 1)
105                 return -EINVAL;
106
107         service = g_hash_table_lookup(config->service_table, ident);
108         if (service == NULL) {
109                 service = g_try_new0(struct connman_config_service, 1);
110                 if (service == NULL)
111                         return -ENOMEM;
112
113                 service->ident = g_strdup(ident);
114         }
115
116         str = g_key_file_get_string(keyfile, group, "Type", NULL);
117         if (str != NULL) {
118                 g_free(service->type);
119                 service->type = str;
120         }
121
122         str = g_key_file_get_string(keyfile, group, "Name", NULL);
123         if (str != NULL) {
124                 g_free(service->type);
125                 service->name = str;
126         }
127
128         hex_ssid = g_key_file_get_string(keyfile, group, "SSID", NULL);
129         if (hex_ssid != NULL) {
130                 char *ssid;
131                 unsigned int i, j = 0, hex;
132                 size_t hex_ssid_len = strlen(hex_ssid);
133
134                 ssid = g_try_malloc0(hex_ssid_len / 2);
135                 if (ssid == NULL) {
136                         g_free(hex_ssid);
137                         return -ENOMEM;
138                 }
139
140                 for (i = 0; i < hex_ssid_len; i += 2) {
141                         sscanf(hex_ssid + i, "%02x", &hex);
142                         ssid[j++] = hex;
143                 }
144
145                 g_free(hex_ssid);
146
147                 g_free(service->ssid);
148                 service->ssid = ssid;
149                 service->ssid_len = hex_ssid_len / 2;
150         } else if (service->name != NULL) {
151                 char *ssid;
152                 unsigned int ssid_len;
153
154                 ssid_len = strlen(service->name);
155                 ssid = g_try_malloc0(ssid_len);
156                 if (ssid == NULL)
157                         return -ENOMEM;
158
159                 memcpy(ssid, service->name, ssid_len);
160                 g_free(service->ssid);
161                 service->ssid = ssid;
162                 service->ssid_len = ssid_len;
163         }
164
165         str = g_key_file_get_string(keyfile, group, "EAP", NULL);
166         if (str != NULL) {
167                 g_free(service->eap);
168                 service->eap = str;
169         }
170
171         str = g_key_file_get_string(keyfile, group, "CACertFile", NULL);
172         if (str != NULL) {
173                 g_free(service->ca_cert_file);
174                 service->ca_cert_file = str;
175         }
176
177         str = g_key_file_get_string(keyfile, group, "ClientCertFile", NULL);
178         if (str != NULL) {
179                 g_free(service->client_cert_file);
180                 service->client_cert_file = str;
181         }
182
183         str = g_key_file_get_string(keyfile, group, "PrivateKeyFile", NULL);
184         if (str != NULL) {
185                 g_free(service->private_key_file);
186                 service->private_key_file = str;
187         }
188
189         str = g_key_file_get_string(keyfile, group,
190                                                 "PrivateKeyPassphrase", NULL);
191         if (str != NULL) {
192                 g_free(service->private_key_passphrase);
193                 service->private_key_passphrase = str;
194         }
195
196         str = g_key_file_get_string(keyfile, group, "Identity", NULL);
197         if (str != NULL) {
198                 g_free(service->identity);
199                 service->identity = str;
200         }
201
202         str = g_key_file_get_string(keyfile, group, "Phase2", NULL);
203         if (str != NULL) {
204                 g_free(service->phase2);
205                 service->phase2 = str;
206         }
207
208         pass_from_fsid = g_key_file_get_boolean(keyfile, group,
209                                                 "PassphraseFromFsid", &error);
210         if (error == NULL)
211                 service->passphrase_from_fsid = pass_from_fsid;
212
213         g_hash_table_replace(config->service_table, service->ident, service);
214
215         connman_info("Adding service configuration %s", service->ident);
216
217         return 0;
218 }
219
220 static int load_config(struct connman_config *config)
221 {
222         GKeyFile *keyfile;
223         gsize length;
224         char **groups;
225         char *str;
226         int i;
227
228         DBG("config %p", config);
229
230         keyfile = __connman_storage_open_config(config->ident);
231         if (keyfile == NULL)
232                 return -EIO;
233
234         str = g_key_file_get_string(keyfile, "global", "Name", NULL);
235         if (str != NULL) {
236                 g_free(config->name);
237                 config->name = str;
238         }
239
240         str = g_key_file_get_string(keyfile, "global", "Description", NULL);
241         if (str != NULL) {
242                 g_free(config->description);
243                 config->description = str;
244         }
245
246         groups = g_key_file_get_groups(keyfile, &length);
247
248         for (i = 0; groups[i] != NULL; i++) {
249                 if (g_str_has_prefix(groups[i], "service_") == TRUE)
250                         load_service(keyfile, groups[i], config);
251         }
252
253         g_strfreev(groups);
254
255         __connman_storage_close_config(config->ident, keyfile, FALSE);
256
257         return 0;
258 }
259
260 static int create_config(const char *ident)
261 {
262         struct connman_config *config;
263
264         DBG("ident %s", ident);
265
266         if (g_hash_table_lookup(config_table, ident) != NULL)
267                 return -EEXIST;
268
269         config = g_try_new0(struct connman_config, 1);
270         if (config == NULL)
271                 return -ENOMEM;
272
273         config->ident = g_strdup(ident);
274
275         config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
276                                                 NULL, unregister_service);
277
278         g_hash_table_insert(config_table, config->ident, config);
279
280         connman_info("Adding configuration %s", config->ident);
281
282         load_config(config);
283
284         return 0;
285 }
286
287 static int read_configs(void)
288 {
289         GDir *dir;
290
291         DBG("");
292
293         dir = g_dir_open(STORAGEDIR, 0, NULL);
294         if (dir != NULL) {
295                 const gchar *file;
296
297                 while ((file = g_dir_read_name(dir)) != NULL) {
298                         GString *str;
299                         gchar *ident;
300
301                         if (g_str_has_suffix(file, ".config") == FALSE)
302                                 continue;
303
304                         ident = g_strrstr(file, ".config");
305                         if (ident == NULL)
306                                 continue;
307
308                         str = g_string_new_len(file, ident - file);
309                         if (str == NULL)
310                                 continue;
311
312                         ident = g_string_free(str, FALSE);
313
314                         if (connman_dbus_validate_ident(ident) == TRUE)
315                                 create_config(ident);
316
317                         g_free(ident);
318                 }
319
320                 g_dir_close(dir);
321         }
322
323         return 0;
324 }
325
326 int __connman_config_init(void)
327 {
328         DBG("");
329
330         config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
331                                                 NULL, unregister_config);
332
333         return read_configs();
334 }
335
336 void __connman_config_cleanup(void)
337 {
338         DBG("");
339
340         g_hash_table_destroy(config_table);
341         config_table = NULL;
342 }
343
344 static char *config_pem_fsid(const char *pem_file)
345 {
346         struct statfs buf;
347         unsigned *fsid = (unsigned *) &buf.f_fsid;
348         unsigned long long fsid64;
349
350         if (pem_file == NULL)
351                 return NULL;
352
353         if (statfs(pem_file, &buf) < 0) {
354                 connman_error("statfs error %s for %s",
355                                                 strerror(errno), pem_file);
356                 return NULL;
357         }
358
359         fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
360
361         return g_strdup_printf("%llx", fsid64);
362 }
363
364 static void provision_service(gpointer key, gpointer value, gpointer user_data)
365 {
366         struct connman_service *service = user_data;
367         struct connman_config_service *config = value;
368         struct connman_network *network;
369         const void *ssid;
370         unsigned int ssid_len;
371
372         /* For now only WiFi service entries are supported */
373         if (g_strcmp0(config->type, "wifi") != 0)
374                 return;
375
376         network = __connman_service_get_network(service);
377         if (network == NULL) {
378                 connman_error("Service has no network set");
379                 return;
380         }
381
382         ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
383         if (ssid == NULL) {
384                 connman_error("Network SSID not set");
385                 return;
386         }
387
388         if (config->ssid == NULL || ssid_len != config->ssid_len)
389                 return;
390
391         if (memcmp(config->ssid, ssid, ssid_len) != 0)
392                 return;
393
394         if (config->eap)
395                 __connman_service_set_string(service, "EAP", config->eap);
396
397         if (config->identity)
398                 __connman_service_set_string(service, "Identity",
399                                                         config->identity);
400
401         if (config->ca_cert_file)
402                 __connman_service_set_string(service, "CACertFile",
403                                                         config->ca_cert_file);
404
405         if (config->client_cert_file)
406                 __connman_service_set_string(service, "ClientCertFile",
407                                                 config->client_cert_file);
408
409         if (config->private_key_file)
410                 __connman_service_set_string(service, "PrivateKeyFile",
411                                                 config->private_key_file);
412
413         if (config->passphrase_from_fsid == TRUE &&
414                                                 config->private_key_file) {
415                 char *fsid;
416
417                 fsid = config_pem_fsid(config->private_key_file);
418                 if (fsid == NULL)
419                         return;
420
421                 g_free(config->private_key_passphrase);
422                 config->private_key_passphrase = fsid;
423         }
424
425         if (config->private_key_passphrase) {
426                 __connman_service_set_string(service, "PrivateKeyPassphrase",
427                                                 config->private_key_passphrase);
428                 /*
429                  * TODO: Support for PEAP with both identity and key passwd.
430                  * In that case, we should check if both of them are found
431                  * from the config file. If not, we should not set the
432                  * service passphrase in order for the UI to request for an
433                  * additional passphrase.
434                  */
435                 __connman_service_set_string(service, "Passphrase",
436                                                 config->private_key_passphrase);
437         }
438
439         if (config->phase2)
440                 __connman_service_set_string(service, "Phase2", config->phase2);
441 }
442
443 int __connman_config_provision_service(struct connman_service *service)
444 {
445         enum connman_service_type type;
446         GHashTableIter iter;
447         gpointer value, key;
448
449         DBG("service %p", service);
450
451         /* For now only WiFi services are supported */
452         type = connman_service_get_type(service);
453         if (type != CONNMAN_SERVICE_TYPE_WIFI)
454                 return -ENOSYS;
455
456         g_hash_table_iter_init(&iter, config_table);
457
458         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
459                 struct connman_config *config = value;
460
461                 g_hash_table_foreach(config->service_table,
462                                                 provision_service, service);
463         }
464
465         return 0;
466 }