config: Set from_fs to connman_bool_t
[framework/connectivity/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         connman_bool_t 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 int load_config(struct connman_config *config)
321 {
322         GKeyFile *keyfile;
323         gsize length;
324         char **groups;
325         char *str;
326         int i;
327
328         DBG("config %p", config);
329
330         keyfile = __connman_storage_open_config(config->ident);
331         if (keyfile == NULL)
332                 return -EIO;
333
334         /* Verify keys validity of the global section */
335         check_keys(keyfile, "global", config_possible_keys);
336
337         str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_NAME, NULL);
338         if (str != NULL) {
339                 g_free(config->name);
340                 config->name = str;
341         }
342
343         str = g_key_file_get_string(keyfile, "global", CONFIG_KEY_DESC, NULL);
344         if (str != NULL) {
345                 g_free(config->description);
346                 config->description = str;
347         }
348
349         groups = g_key_file_get_groups(keyfile, &length);
350
351         for (i = 0; groups[i] != NULL; i++) {
352                 if (g_str_has_prefix(groups[i], "service_") == TRUE)
353                         load_service(keyfile, groups[i], config);
354         }
355
356         g_strfreev(groups);
357
358         __connman_storage_close_config(config->ident, keyfile, FALSE);
359
360         return 0;
361 }
362
363 static struct connman_config *create_config(const char *ident)
364 {
365         struct connman_config *config;
366
367         DBG("ident %s", ident);
368
369         if (g_hash_table_lookup(config_table, ident) != NULL)
370                 return NULL;
371
372         config = g_try_new0(struct connman_config, 1);
373         if (config == NULL)
374                 return NULL;
375
376         config->ident = g_strdup(ident);
377
378         config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
379                                                 NULL, unregister_service);
380
381         g_hash_table_insert(config_table, config->ident, config);
382
383         connman_info("Adding configuration %s", config->ident);
384
385         return config;
386 }
387
388 int __connman_config_load_service(GKeyFile *keyfile, const char *group)
389 {
390         struct connman_config *config = g_hash_table_lookup(config_table,
391                                                         NONFS_CONFIG_NAME);
392
393         if (config == NULL) {
394                 config = create_config(NONFS_CONFIG_NAME);
395                 if (config == NULL)
396                         return -ENOMEM;
397         }
398
399         return load_service(keyfile, group, config);
400 }
401
402 static int read_configs(void)
403 {
404         GDir *dir;
405
406         DBG("");
407
408         dir = g_dir_open(STORAGEDIR, 0, NULL);
409         if (dir != NULL) {
410                 const gchar *file;
411
412                 while ((file = g_dir_read_name(dir)) != NULL) {
413                         GString *str;
414                         gchar *ident;
415
416                         if (g_str_has_suffix(file, ".config") == FALSE)
417                                 continue;
418
419                         ident = g_strrstr(file, ".config");
420                         if (ident == NULL)
421                                 continue;
422
423                         if (g_str_equal(ident, NONFS_CONFIG_NAME) == TRUE)
424                                 continue;
425
426                         str = g_string_new_len(file, ident - file);
427                         if (str == NULL)
428                                 continue;
429
430                         ident = g_string_free(str, FALSE);
431
432                         if (connman_dbus_validate_ident(ident) == TRUE) {
433                                 struct connman_config *config;
434
435                                 config = create_config(ident);
436                                 if (config != NULL)
437                                         load_config(config);
438                         }
439                         g_free(ident);
440                 }
441
442                 g_dir_close(dir);
443         }
444
445         return 0;
446 }
447
448 static gboolean inotify_data(GIOChannel *channel, GIOCondition cond,
449                                                         gpointer user_data)
450 {
451         char buffer[256];
452         char *next_event;
453         gsize bytes_read;
454         GIOStatus status;
455
456         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
457                 inotify_watch = 0;
458                 return FALSE;
459         }
460
461         status = g_io_channel_read_chars(channel, buffer,
462                                         sizeof(buffer) -1, &bytes_read, NULL);
463
464         switch (status) {
465         case G_IO_STATUS_NORMAL:
466                 break;
467         case G_IO_STATUS_AGAIN:
468                 return TRUE;
469         default:
470                 connman_error("Reading from inotify channel failed");
471                 inotify_watch = 0;
472                 return FALSE;
473         }
474
475         next_event = buffer;
476
477         while (bytes_read > 0) {
478                 struct inotify_event *event;
479                 gchar *ext;
480                 gchar *ident;
481                 gsize len;
482
483                 event = (struct inotify_event *) next_event;
484                 if (event->len)
485                         ident = next_event + sizeof(struct inotify_event);
486                 else
487                         ident = NULL;
488
489                 len = sizeof(struct inotify_event) + event->len;
490
491                 /* check if inotify_event block fit */
492                 if (len > bytes_read)
493                         break;
494
495                 next_event += len;
496                 bytes_read -= len;
497
498                 if (ident == NULL)
499                         continue;
500
501                 if (g_str_has_suffix(ident, ".config") == FALSE)
502                         continue;
503
504                 ext = g_strrstr(ident, ".config");
505                 if (ext == NULL)
506                         continue;
507
508                 *ext = '\0';
509
510                 if (g_str_equal(ident, NONFS_CONFIG_NAME) == TRUE)
511                         continue;
512
513                 if (connman_dbus_validate_ident(ident) == FALSE)
514                         continue;
515
516                 if (event->mask & IN_CREATE)
517                         create_config(ident);
518
519                 if (event->mask & IN_MODIFY) {
520                         struct connman_config *config;
521
522                         config = g_hash_table_lookup(config_table, ident);
523                         if (config != NULL) {
524                                 g_hash_table_remove_all(config->service_table);
525                                 load_config(config);
526                         }
527                 }
528
529                 if (event->mask & IN_DELETE)
530                         g_hash_table_remove(config_table, ident);
531         }
532
533         return TRUE;
534 }
535
536 static int create_watch(void)
537 {
538         int fd;
539
540         fd = inotify_init();
541         if (fd < 0)
542                 return -EIO;
543
544         inotify_wd = inotify_add_watch(fd, STORAGEDIR,
545                                         IN_MODIFY | IN_CREATE | IN_DELETE);
546         if (inotify_wd < 0) {
547                 connman_error("Creation of STORAGEDIR  watch failed");
548                 close(fd);
549                 return -EIO;
550         }
551
552         inotify_channel = g_io_channel_unix_new(fd);
553         if (inotify_channel == NULL) {
554                 connman_error("Creation of inotify channel failed");
555                 inotify_rm_watch(fd, inotify_wd);
556                 inotify_wd = 0;
557
558                 close(fd);
559                 return -EIO;
560         }
561
562         g_io_channel_set_close_on_unref(inotify_channel, TRUE);
563         g_io_channel_set_encoding(inotify_channel, NULL, NULL);
564         g_io_channel_set_buffered(inotify_channel, FALSE);
565
566         inotify_watch = g_io_add_watch(inotify_channel,
567                                 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
568                                 inotify_data, NULL);
569
570         return 0;
571 }
572
573 static void remove_watch(void)
574 {
575         int fd;
576
577         if (inotify_channel == NULL)
578                 return;
579
580         if (inotify_watch > 0) {
581                 g_source_remove(inotify_watch);
582                 inotify_watch = 0;
583         }
584
585         fd = g_io_channel_unix_get_fd(inotify_channel);
586
587         if (inotify_wd >= 0) {
588                 inotify_rm_watch(fd, inotify_wd);
589                 inotify_wd = 0;
590         }
591
592         g_io_channel_unref(inotify_channel);
593 }
594
595 int __connman_config_init(void)
596 {
597         DBG("");
598
599         config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
600                                                 NULL, unregister_config);
601
602         create_watch();
603
604         return read_configs();
605 }
606
607 void __connman_config_cleanup(void)
608 {
609         DBG("");
610
611         remove_watch();
612
613         g_hash_table_destroy(config_table);
614         config_table = NULL;
615 }
616
617 static char *config_pem_fsid(const char *pem_file)
618 {
619         struct statfs buf;
620         unsigned *fsid = (unsigned *) &buf.f_fsid;
621         unsigned long long fsid64;
622
623         if (pem_file == NULL)
624                 return NULL;
625
626         if (statfs(pem_file, &buf) < 0) {
627                 connman_error("statfs error %s for %s",
628                                                 strerror(errno), pem_file);
629                 return NULL;
630         }
631
632         fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
633
634         return g_strdup_printf("%llx", fsid64);
635 }
636
637 static void provision_service(gpointer key, gpointer value, gpointer user_data)
638 {
639         struct connman_service *service = user_data;
640         struct connman_config_service *config = value;
641         struct connman_network *network;
642         const void *ssid;
643         unsigned int ssid_len;
644
645         /* For now only WiFi service entries are supported */
646         if (g_strcmp0(config->type, "wifi") != 0)
647                 return;
648
649         network = __connman_service_get_network(service);
650         if (network == NULL) {
651                 connman_error("Service has no network set");
652                 return;
653         }
654
655         ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
656         if (ssid == NULL) {
657                 connman_error("Network SSID not set");
658                 return;
659         }
660
661         if (config->ssid == NULL || ssid_len != config->ssid_len)
662                 return;
663
664         if (memcmp(config->ssid, ssid, ssid_len) != 0)
665                 return;
666
667         /* do not provision immutable services with non-fs originated configs */
668         if (config->from_fs == FALSE &&
669                         __connman_service_get_immutable(service) == TRUE)
670                 return;
671
672         /* only lock services with a config originated from the filesystem */
673         if (config->from_fs == TRUE)
674                 __connman_service_set_immutable(service, TRUE);
675
676         __connman_service_set_favorite(service, TRUE);
677
678         if (config->eap != NULL)
679                 __connman_service_set_string(service, "EAP", config->eap);
680
681         if (config->identity != NULL)
682                 __connman_service_set_string(service, "Identity",
683                                                         config->identity);
684
685         if (config->ca_cert_file != NULL)
686                 __connman_service_set_string(service, "CACertFile",
687                                                         config->ca_cert_file);
688
689         if (config->client_cert_file != NULL)
690                 __connman_service_set_string(service, "ClientCertFile",
691                                                 config->client_cert_file);
692
693         if (config->private_key_file != NULL)
694                 __connman_service_set_string(service, "PrivateKeyFile",
695                                                 config->private_key_file);
696
697         if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
698                                         config->private_key_file != NULL) {
699                 char *fsid;
700
701                 fsid = config_pem_fsid(config->private_key_file);
702                 if (fsid == NULL)
703                         return;
704
705                 g_free(config->private_key_passphrase);
706                 config->private_key_passphrase = fsid;
707         }
708
709         if (config->private_key_passphrase != NULL) {
710                 __connman_service_set_string(service, "PrivateKeyPassphrase",
711                                                 config->private_key_passphrase);
712                 /*
713                  * TODO: Support for PEAP with both identity and key passwd.
714                  * In that case, we should check if both of them are found
715                  * from the config file. If not, we should not set the
716                  * service passphrase in order for the UI to request for an
717                  * additional passphrase.
718                  */
719         }
720
721         if (config->phase2 != NULL)
722                 __connman_service_set_string(service, "Phase2", config->phase2);
723
724         if (config->passphrase != NULL)
725                 __connman_service_set_string(service, "Passphrase", config->passphrase);
726 }
727
728 int __connman_config_provision_service(struct connman_service *service)
729 {
730         enum connman_service_type type;
731         GHashTableIter iter;
732         gpointer value, key;
733
734         DBG("service %p", service);
735
736         /* For now only WiFi services are supported */
737         type = connman_service_get_type(service);
738         if (type != CONNMAN_SERVICE_TYPE_WIFI)
739                 return -ENOSYS;
740
741         g_hash_table_iter_init(&iter, config_table);
742
743         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
744                 struct connman_config *config = value;
745
746                 g_hash_table_foreach(config->service_table,
747                                                 provision_service, service);
748         }
749
750         return 0;
751 }