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