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