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