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