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