config: Fix service configuration update
[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 <unistd.h>
28 #include <string.h>
29 #include <sys/vfs.h>
30 #include <sys/inotify.h>
31 #include <glib.h>
32
33 #include "connman.h"
34
35 struct connman_config_service {
36         char *ident;
37         char *name;
38         char *type;
39         void *ssid;
40         unsigned int ssid_len;
41         char *eap;
42         char *identity;
43         char *ca_cert_file;
44         char *client_cert_file;
45         char *private_key_file;
46         char *private_key_passphrase;
47         char *private_key_passphrase_type;
48         char *phase2;
49         char *passphrase;
50 };
51
52 struct connman_config {
53         char *ident;
54         char *name;
55         char *description;
56         GHashTable *service_table;
57 };
58
59 static GHashTable *config_table = NULL;
60
61 static int inotify_wd = -1;
62
63 static GIOChannel *inotify_channel = NULL;
64 static uint inotify_watch = 0;
65
66 /* Definition of possible strings in the .config files */
67 #define CONFIG_KEY_NAME                "Name"
68 #define CONFIG_KEY_DESC                "Description"
69
70 #define SERVICE_KEY_TYPE               "Type"
71 #define SERVICE_KEY_NAME               "Name"
72 #define SERVICE_KEY_SSID               "SSID"
73 #define SERVICE_KEY_EAP                "EAP"
74 #define SERVICE_KEY_CA_CERT            "CACertFile"
75 #define SERVICE_KEY_CL_CERT            "ClientCertFile"
76 #define SERVICE_KEY_PRV_KEY            "PrivateKeyFile"
77 #define SERVICE_KEY_PRV_KEY_PASS       "PrivateKeyPassphrase"
78 #define SERVICE_KEY_PRV_KEY_PASS_TYPE  "PrivateKeyPassphraseType"
79 #define SERVICE_KEY_IDENTITY           "Identity"
80 #define SERVICE_KEY_PHASE2             "Phase2"
81 #define SERVICE_KEY_PASSPHRASE         "Passphrase"
82
83 static const char *config_possible_keys[] = {
84         CONFIG_KEY_NAME,
85         CONFIG_KEY_DESC,
86         NULL,
87 };
88
89 static const char *service_possible_keys[] = {
90         SERVICE_KEY_TYPE,
91         SERVICE_KEY_NAME,
92         SERVICE_KEY_SSID,
93         SERVICE_KEY_EAP,
94         SERVICE_KEY_CA_CERT,
95         SERVICE_KEY_CL_CERT,
96         SERVICE_KEY_PRV_KEY,
97         SERVICE_KEY_PRV_KEY_PASS,
98         SERVICE_KEY_PRV_KEY_PASS_TYPE,
99         SERVICE_KEY_IDENTITY,
100         SERVICE_KEY_PHASE2,
101         SERVICE_KEY_PASSPHRASE,
102         NULL,
103 };
104
105 static void unregister_config(gpointer data)
106 {
107         struct connman_config *config = data;
108
109         connman_info("Removing configuration %s", config->ident);
110
111         g_hash_table_destroy(config->service_table);
112
113         g_free(config->description);
114         g_free(config->name);
115         g_free(config->ident);
116         g_free(config);
117 }
118
119 static void unregister_service(gpointer data)
120 {
121         struct connman_config_service *service = data;
122
123         connman_info("Removing service configuration %s", service->ident);
124
125         g_free(service->ident);
126         g_free(service->type);
127         g_free(service->name);
128         g_free(service->ssid);
129         g_free(service->eap);
130         g_free(service->identity);
131         g_free(service->ca_cert_file);
132         g_free(service->client_cert_file);
133         g_free(service->private_key_file);
134         g_free(service->private_key_passphrase);
135         g_free(service->private_key_passphrase_type);
136         g_free(service->phase2);
137         g_free(service->passphrase);
138         g_free(service);
139 }
140
141 static void check_keys(GKeyFile *keyfile, const char *group,
142                         const char **possible_keys)
143 {
144         char **avail_keys;
145         gsize nb_avail_keys, i, j;
146
147         avail_keys = g_key_file_get_keys(keyfile, group, &nb_avail_keys, NULL);
148         if (avail_keys == NULL)
149                 return;
150
151         /*
152          * For each key in the configuration file,
153          * verify it is understood by connman
154          */
155         for (i = 0 ; i < nb_avail_keys; i++) {
156                 for (j = 0; possible_keys[j] ; j++)
157                         if (g_strcmp0(avail_keys[i], possible_keys[j]) == 0)
158                                 break;
159
160                 if (possible_keys[j] == NULL)
161                         connman_warn("Unknown configuration key %s in [%s]",
162                                         avail_keys[i], group);
163         }
164
165         g_strfreev(avail_keys);
166 }
167
168 static int load_service(GKeyFile *keyfile, const char *group,
169                                                 struct connman_config *config)
170 {
171         struct connman_config_service *service;
172         const char *ident;
173         char *str, *hex_ssid;
174         gboolean service_created = FALSE;
175
176         /* Strip off "service_" prefix */
177         ident = group + 8;
178
179         if (strlen(ident) < 1)
180                 return -EINVAL;
181
182         /* Verify that provided keys are good */
183         check_keys(keyfile, group, service_possible_keys);
184
185         service = g_hash_table_lookup(config->service_table, ident);
186         if (service == NULL) {
187                 service = g_try_new0(struct connman_config_service, 1);
188                 if (service == NULL)
189                         return -ENOMEM;
190
191                 service->ident = g_strdup(ident);
192
193                 service_created = TRUE;
194         }
195
196         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_TYPE, NULL);
197         if (str != NULL) {
198                 g_free(service->type);
199                 service->type = str;
200         }
201
202         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_NAME, NULL);
203         if (str != NULL) {
204                 g_free(service->name);
205                 service->name = str;
206         }
207
208         hex_ssid = g_key_file_get_string(keyfile, group, SERVICE_KEY_SSID,
209                                          NULL);
210         if (hex_ssid != NULL) {
211                 char *ssid;
212                 unsigned int i, j = 0, hex;
213                 size_t hex_ssid_len = strlen(hex_ssid);
214
215                 ssid = g_try_malloc0(hex_ssid_len / 2);
216                 if (ssid == NULL) {
217                         g_free(hex_ssid);
218                         return -ENOMEM;
219                 }
220
221                 for (i = 0; i < hex_ssid_len; i += 2) {
222                         sscanf(hex_ssid + i, "%02x", &hex);
223                         ssid[j++] = hex;
224                 }
225
226                 g_free(hex_ssid);
227
228                 g_free(service->ssid);
229                 service->ssid = ssid;
230                 service->ssid_len = hex_ssid_len / 2;
231         } else if (service->name != NULL) {
232                 char *ssid;
233                 unsigned int ssid_len;
234
235                 ssid_len = strlen(service->name);
236                 ssid = g_try_malloc0(ssid_len);
237                 if (ssid == NULL)
238                         return -ENOMEM;
239
240                 memcpy(ssid, service->name, ssid_len);
241                 g_free(service->ssid);
242                 service->ssid = ssid;
243                 service->ssid_len = ssid_len;
244         }
245
246         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_EAP, NULL);
247         if (str != NULL) {
248                 g_free(service->eap);
249                 service->eap = str;
250         }
251
252         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CA_CERT, NULL);
253         if (str != NULL) {
254                 g_free(service->ca_cert_file);
255                 service->ca_cert_file = str;
256         }
257
258         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_CL_CERT, NULL);
259         if (str != NULL) {
260                 g_free(service->client_cert_file);
261                 service->client_cert_file = str;
262         }
263
264         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PRV_KEY, NULL);
265         if (str != NULL) {
266                 g_free(service->private_key_file);
267                 service->private_key_file = str;
268         }
269
270         str = g_key_file_get_string(keyfile, group,
271                                                 SERVICE_KEY_PRV_KEY_PASS, NULL);
272         if (str != NULL) {
273                 g_free(service->private_key_passphrase);
274                 service->private_key_passphrase = str;
275         }
276
277         str = g_key_file_get_string(keyfile, group,
278                                         SERVICE_KEY_PRV_KEY_PASS_TYPE, NULL);
279         if (str != NULL) {
280                 g_free(service->private_key_passphrase_type);
281                 service->private_key_passphrase_type = str;
282         }
283
284         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_IDENTITY, NULL);
285         if (str != NULL) {
286                 g_free(service->identity);
287                 service->identity = str;
288         }
289
290         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PHASE2, NULL);
291         if (str != NULL) {
292                 g_free(service->phase2);
293                 service->phase2 = str;
294         }
295
296         str = g_key_file_get_string(keyfile, group, SERVICE_KEY_PASSPHRASE,
297                                         NULL);
298         if (str != NULL) {
299                 g_free(service->passphrase);
300                 service->passphrase = str;
301         }
302
303         if (service_created)
304                 g_hash_table_insert(config->service_table, service->ident,
305                                         service);
306
307         connman_info("Adding service configuration %s", service->ident);
308
309         return 0;
310 }
311
312 static int load_config(struct connman_config *config)
313 {
314         GKeyFile *keyfile;
315         gsize length;
316         char **groups;
317         char *str;
318         int i;
319
320         DBG("config %p", config);
321
322         keyfile = __connman_storage_open_config(config->ident);
323         if (keyfile == NULL)
324                 return -EIO;
325
326         /* Verify keys validity of the global section */
327         check_keys(keyfile, "global", config_possible_keys);
328
329         str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_NAME, NULL);
330         if (str != NULL) {
331                 g_free(config->name);
332                 config->name = str;
333         }
334
335         str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_DESC, NULL);
336         if (str != NULL) {
337                 g_free(config->description);
338                 config->description = str;
339         }
340
341         groups = g_key_file_get_groups(keyfile, &length);
342
343         for (i = 0; groups[i] != NULL; i++) {
344                 if (g_str_has_prefix(groups[i], "service_") == TRUE)
345                         load_service(keyfile, groups[i], config);
346         }
347
348         g_strfreev(groups);
349
350         __connman_storage_close_config(config->ident, keyfile, FALSE);
351
352         return 0;
353 }
354
355 static struct connman_config *create_config(const char *ident)
356 {
357         struct connman_config *config;
358
359         DBG("ident %s", ident);
360
361         if (g_hash_table_lookup(config_table, ident) != NULL)
362                 return NULL;
363
364         config = g_try_new0(struct connman_config, 1);
365         if (config == NULL)
366                 return NULL;
367
368         config->ident = g_strdup(ident);
369
370         config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
371                                                 NULL, unregister_service);
372
373         g_hash_table_insert(config_table, config->ident, config);
374
375         connman_info("Adding configuration %s", config->ident);
376
377         return config;
378 }
379
380 static int read_configs(void)
381 {
382         GDir *dir;
383
384         DBG("");
385
386         dir = g_dir_open(STORAGEDIR, 0, NULL);
387         if (dir != NULL) {
388                 const gchar *file;
389
390                 while ((file = g_dir_read_name(dir)) != NULL) {
391                         GString *str;
392                         gchar *ident;
393
394                         if (g_str_has_suffix(file, ".config") == FALSE)
395                                 continue;
396
397                         ident = g_strrstr(file, ".config");
398                         if (ident == NULL)
399                                 continue;
400
401                         str = g_string_new_len(file, ident - file);
402                         if (str == NULL)
403                                 continue;
404
405                         ident = g_string_free(str, FALSE);
406
407                         if (connman_dbus_validate_ident(ident) == TRUE) {
408                                 struct connman_config *config;
409
410                                 config = create_config(ident);
411                                 if (config != NULL)
412                                         load_config(config);
413                         }
414                         g_free(ident);
415                 }
416
417                 g_dir_close(dir);
418         }
419
420         return 0;
421 }
422
423 static gboolean inotify_data(GIOChannel *channel, GIOCondition cond,
424                                                         gpointer user_data)
425 {
426         char buffer[256];
427         char *next_event;
428         gsize bytes_read;
429         GIOStatus status;
430
431         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
432                 inotify_watch = 0;
433                 return FALSE;
434         }
435
436         status = g_io_channel_read_chars(channel, buffer,
437                                         sizeof(buffer) -1, &bytes_read, NULL);
438
439         switch (status) {
440         case G_IO_STATUS_NORMAL:
441                 break;
442         case G_IO_STATUS_AGAIN:
443                 return TRUE;
444         default:
445                 connman_error("Reading from inotify channel failed");
446                 inotify_watch = 0;
447                 return FALSE;
448         }
449
450         next_event = buffer;
451
452         while (bytes_read > 0) {
453                 struct inotify_event *event;
454                 gchar *ext;
455                 gchar *ident;
456                 gsize len;
457
458                 event = (struct inotify_event *) next_event;
459                 if (event->len)
460                         ident = next_event + sizeof(struct inotify_event);
461                 else
462                         ident = NULL;
463
464                 len = sizeof(struct inotify_event) + event->len;
465
466                 /* check if inotify_event block fit */
467                 if (len > bytes_read)
468                         break;
469
470                 next_event += len;
471                 bytes_read -= len;
472
473                 if (ident == NULL)
474                         continue;
475
476                 if (g_str_has_suffix(ident, ".config") == FALSE)
477                         continue;
478
479                 ext = g_strrstr(ident, ".config");
480                 if (ext == NULL)
481                         continue;
482
483                 *ext = '\0';
484
485                 if (connman_dbus_validate_ident(ident) == FALSE)
486                         continue;
487
488                 if (event->mask & IN_CREATE)
489                         create_config(ident);
490
491                 if (event->mask & IN_MODIFY) {
492                         struct connman_config *config;
493
494                         config = g_hash_table_lookup(config_table, ident);
495                         if (config != NULL) {
496                                 g_hash_table_remove_all(config->service_table);
497                                 load_config(config);
498                         }
499                 }
500
501                 if (event->mask & IN_DELETE)
502                         g_hash_table_remove(config_table, ident);
503         }
504
505         return TRUE;
506 }
507
508 static int create_watch(void)
509 {
510         int fd;
511
512         fd = inotify_init();
513         if (fd < 0)
514                 return -EIO;
515
516         inotify_wd = inotify_add_watch(fd, STORAGEDIR,
517                                         IN_MODIFY | IN_CREATE | IN_DELETE);
518         if (inotify_wd < 0) {
519                 connman_error("Creation of STORAGEDIR  watch failed");
520                 close(fd);
521                 return -EIO;
522         }
523
524         inotify_channel = g_io_channel_unix_new(fd);
525         if (inotify_channel == NULL) {
526                 connman_error("Creation of inotify channel failed");
527                 inotify_rm_watch(fd, inotify_wd);
528                 inotify_wd = 0;
529
530                 close(fd);
531                 return -EIO;
532         }
533
534         g_io_channel_set_close_on_unref(inotify_channel, TRUE);
535         g_io_channel_set_encoding(inotify_channel, NULL, NULL);
536         g_io_channel_set_buffered(inotify_channel, FALSE);
537
538         inotify_watch = g_io_add_watch(inotify_channel,
539                                 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
540                                 inotify_data, NULL);
541
542         return 0;
543 }
544
545 static void remove_watch(void)
546 {
547         int fd;
548
549         if (inotify_channel == NULL)
550                 return;
551
552         if (inotify_watch > 0) {
553                 g_source_remove(inotify_watch);
554                 inotify_watch = 0;
555         }
556
557         fd = g_io_channel_unix_get_fd(inotify_channel);
558
559         if (inotify_wd >= 0) {
560                 inotify_rm_watch(fd, inotify_wd);
561                 inotify_wd = 0;
562         }
563
564         g_io_channel_unref(inotify_channel);
565 }
566
567 int __connman_config_init(void)
568 {
569         DBG("");
570
571         config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
572                                                 NULL, unregister_config);
573
574         create_watch();
575
576         return read_configs();
577 }
578
579 void __connman_config_cleanup(void)
580 {
581         DBG("");
582
583         remove_watch();
584
585         g_hash_table_destroy(config_table);
586         config_table = NULL;
587 }
588
589 static char *config_pem_fsid(const char *pem_file)
590 {
591         struct statfs buf;
592         unsigned *fsid = (unsigned *) &buf.f_fsid;
593         unsigned long long fsid64;
594
595         if (pem_file == NULL)
596                 return NULL;
597
598         if (statfs(pem_file, &buf) < 0) {
599                 connman_error("statfs error %s for %s",
600                                                 strerror(errno), pem_file);
601                 return NULL;
602         }
603
604         fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
605
606         return g_strdup_printf("%llx", fsid64);
607 }
608
609 static void provision_service(gpointer key, gpointer value, gpointer user_data)
610 {
611         struct connman_service *service = user_data;
612         struct connman_config_service *config = value;
613         struct connman_network *network;
614         const void *ssid;
615         unsigned int ssid_len;
616
617         /* For now only WiFi service entries are supported */
618         if (g_strcmp0(config->type, "wifi") != 0)
619                 return;
620
621         network = __connman_service_get_network(service);
622         if (network == NULL) {
623                 connman_error("Service has no network set");
624                 return;
625         }
626
627         ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
628         if (ssid == NULL) {
629                 connman_error("Network SSID not set");
630                 return;
631         }
632
633         if (config->ssid == NULL || ssid_len != config->ssid_len)
634                 return;
635
636         if (memcmp(config->ssid, ssid, ssid_len) != 0)
637                 return;
638
639         __connman_service_set_immutable(service, TRUE);
640         __connman_service_set_favorite(service, TRUE);
641
642         if (config->eap != NULL)
643                 __connman_service_set_string(service, "EAP", config->eap);
644
645         if (config->identity != NULL)
646                 __connman_service_set_string(service, "Identity",
647                                                         config->identity);
648
649         if (config->ca_cert_file != NULL)
650                 __connman_service_set_string(service, "CACertFile",
651                                                         config->ca_cert_file);
652
653         if (config->client_cert_file != NULL)
654                 __connman_service_set_string(service, "ClientCertFile",
655                                                 config->client_cert_file);
656
657         if (config->private_key_file != NULL)
658                 __connman_service_set_string(service, "PrivateKeyFile",
659                                                 config->private_key_file);
660
661         if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
662                                         config->private_key_file != NULL) {
663                 char *fsid;
664
665                 fsid = config_pem_fsid(config->private_key_file);
666                 if (fsid == NULL)
667                         return;
668
669                 g_free(config->private_key_passphrase);
670                 config->private_key_passphrase = fsid;
671         }
672
673         if (config->private_key_passphrase != NULL) {
674                 __connman_service_set_string(service, "PrivateKeyPassphrase",
675                                                 config->private_key_passphrase);
676                 /*
677                  * TODO: Support for PEAP with both identity and key passwd.
678                  * In that case, we should check if both of them are found
679                  * from the config file. If not, we should not set the
680                  * service passphrase in order for the UI to request for an
681                  * additional passphrase.
682                  */
683         }
684
685         if (config->phase2 != NULL)
686                 __connman_service_set_string(service, "Phase2", config->phase2);
687
688         if (config->passphrase != NULL)
689                 __connman_service_set_string(service, "Passphrase", config->passphrase);
690 }
691
692 int __connman_config_provision_service(struct connman_service *service)
693 {
694         enum connman_service_type type;
695         GHashTableIter iter;
696         gpointer value, key;
697
698         DBG("service %p", service);
699
700         /* For now only WiFi services are supported */
701         type = connman_service_get_type(service);
702         if (type != CONNMAN_SERVICE_TYPE_WIFI)
703                 return -ENOSYS;
704
705         g_hash_table_iter_init(&iter, config_table);
706
707         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
708                 struct connman_config *config = value;
709
710                 g_hash_table_foreach(config->service_table,
711                                                 provision_service, service);
712         }
713
714         return 0;
715 }