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