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