Use g_io_channel_read_chars instead of g_io_channel_read
[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         GIOStatus status;
425
426         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
427                 inotify_watch = 0;
428                 return FALSE;
429         }
430
431         status = g_io_channel_read_chars(channel, buffer,
432                                         sizeof(buffer) -1, &bytes_read, NULL);
433
434         switch (status) {
435         case G_IO_STATUS_NORMAL:
436                 break;
437         case G_IO_STATUS_AGAIN:
438                 return TRUE;
439         default:
440                 connman_error("Reading from inotify channel failed");
441                 inotify_watch = 0;
442                 return FALSE;
443         }
444
445         next_event = buffer;
446
447         while (bytes_read > 0) {
448                 struct inotify_event *event;
449                 gchar *ext;
450                 gchar *ident;
451                 gsize len;
452
453                 event = (struct inotify_event *) next_event;
454                 if (event->len)
455                         ident = next_event + sizeof(struct inotify_event);
456                 else
457                         ident = NULL;
458
459                 len = sizeof(struct inotify_event) + event->len;
460
461                 /* check if inotify_event block fit */
462                 if (len > bytes_read)
463                         break;
464
465                 next_event += len;
466                 bytes_read -= len;
467
468                 if (ident == NULL)
469                         continue;
470
471                 if (g_str_has_suffix(ident, ".config") == FALSE)
472                         continue;
473
474                 ext = g_strrstr(ident, ".config");
475                 if (ext == NULL)
476                         continue;
477
478                 *ext = '\0';
479
480                 if (connman_dbus_validate_ident(ident) == FALSE)
481                         continue;
482
483                 if (event->mask & IN_CREATE)
484                         create_config(ident);
485
486                 if (event->mask & IN_MODIFY) {
487                         struct connman_config *config;
488
489                         config = g_hash_table_lookup(config_table, ident);
490                         if (config != NULL) {
491                                 g_hash_table_remove_all(config->service_table);
492                                 load_config(config);
493                         }
494                 }
495
496                 if (event->mask & IN_DELETE)
497                         g_hash_table_remove(config_table, ident);
498         }
499
500         return TRUE;
501 }
502
503 static int create_watch(void)
504 {
505         int fd;
506
507         fd = inotify_init();
508         if (fd < 0)
509                 return -EIO;
510
511         inotify_wd = inotify_add_watch(fd, STORAGEDIR,
512                                         IN_MODIFY | IN_CREATE | IN_DELETE);
513         if (inotify_wd < 0) {
514                 connman_error("Creation of STORAGEDIR  watch failed");
515                 close(fd);
516                 return -EIO;
517         }
518
519         inotify_channel = g_io_channel_unix_new(fd);
520         if (inotify_channel == NULL) {
521                 connman_error("Creation of inotify channel failed");
522                 inotify_rm_watch(fd, inotify_wd);
523                 inotify_wd = 0;
524
525                 close(fd);
526                 return -EIO;
527         }
528
529         g_io_channel_set_close_on_unref(inotify_channel, TRUE);
530         g_io_channel_set_encoding(inotify_channel, NULL, NULL);
531         g_io_channel_set_buffered(inotify_channel, FALSE);
532
533         inotify_watch = g_io_add_watch(inotify_channel,
534                                 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
535                                 inotify_data, NULL);
536
537         return 0;
538 }
539
540 static void remove_watch(void)
541 {
542         int fd;
543
544         if (inotify_channel == NULL)
545                 return;
546
547         if (inotify_watch > 0) {
548                 g_source_remove(inotify_watch);
549                 inotify_watch = 0;
550         }
551
552         fd = g_io_channel_unix_get_fd(inotify_channel);
553
554         if (inotify_wd >= 0) {
555                 inotify_rm_watch(fd, inotify_wd);
556                 inotify_wd = 0;
557         }
558
559         g_io_channel_unref(inotify_channel);
560 }
561
562 int __connman_config_init(void)
563 {
564         DBG("");
565
566         config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
567                                                 NULL, unregister_config);
568
569         create_watch();
570
571         return read_configs();
572 }
573
574 void __connman_config_cleanup(void)
575 {
576         DBG("");
577
578         remove_watch();
579
580         g_hash_table_destroy(config_table);
581         config_table = NULL;
582 }
583
584 static char *config_pem_fsid(const char *pem_file)
585 {
586         struct statfs buf;
587         unsigned *fsid = (unsigned *) &buf.f_fsid;
588         unsigned long long fsid64;
589
590         if (pem_file == NULL)
591                 return NULL;
592
593         if (statfs(pem_file, &buf) < 0) {
594                 connman_error("statfs error %s for %s",
595                                                 strerror(errno), pem_file);
596                 return NULL;
597         }
598
599         fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
600
601         return g_strdup_printf("%llx", fsid64);
602 }
603
604 static void provision_service(gpointer key, gpointer value, gpointer user_data)
605 {
606         struct connman_service *service = user_data;
607         struct connman_config_service *config = value;
608         struct connman_network *network;
609         const void *ssid;
610         unsigned int ssid_len;
611
612         /* For now only WiFi service entries are supported */
613         if (g_strcmp0(config->type, "wifi") != 0)
614                 return;
615
616         network = __connman_service_get_network(service);
617         if (network == NULL) {
618                 connman_error("Service has no network set");
619                 return;
620         }
621
622         ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
623         if (ssid == NULL) {
624                 connman_error("Network SSID not set");
625                 return;
626         }
627
628         if (config->ssid == NULL || ssid_len != config->ssid_len)
629                 return;
630
631         if (memcmp(config->ssid, ssid, ssid_len) != 0)
632                 return;
633
634         __connman_service_set_immutable(service, TRUE);
635         __connman_service_set_favorite(service, TRUE);
636
637         if (config->eap != NULL)
638                 __connman_service_set_string(service, "EAP", config->eap);
639
640         if (config->identity != NULL)
641                 __connman_service_set_string(service, "Identity",
642                                                         config->identity);
643
644         if (config->ca_cert_file != NULL)
645                 __connman_service_set_string(service, "CACertFile",
646                                                         config->ca_cert_file);
647
648         if (config->client_cert_file != NULL)
649                 __connman_service_set_string(service, "ClientCertFile",
650                                                 config->client_cert_file);
651
652         if (config->private_key_file != NULL)
653                 __connman_service_set_string(service, "PrivateKeyFile",
654                                                 config->private_key_file);
655
656         if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
657                                         config->private_key_file != NULL) {
658                 char *fsid;
659
660                 fsid = config_pem_fsid(config->private_key_file);
661                 if (fsid == NULL)
662                         return;
663
664                 g_free(config->private_key_passphrase);
665                 config->private_key_passphrase = fsid;
666         }
667
668         if (config->private_key_passphrase != NULL) {
669                 __connman_service_set_string(service, "PrivateKeyPassphrase",
670                                                 config->private_key_passphrase);
671                 /*
672                  * TODO: Support for PEAP with both identity and key passwd.
673                  * In that case, we should check if both of them are found
674                  * from the config file. If not, we should not set the
675                  * service passphrase in order for the UI to request for an
676                  * additional passphrase.
677                  */
678         }
679
680         if (config->phase2 != NULL)
681                 __connman_service_set_string(service, "Phase2", config->phase2);
682
683         if (config->passphrase != NULL)
684                 __connman_service_set_string(service, "Passphrase", config->passphrase);
685 }
686
687 int __connman_config_provision_service(struct connman_service *service)
688 {
689         enum connman_service_type type;
690         GHashTableIter iter;
691         gpointer value, key;
692
693         DBG("service %p", service);
694
695         /* For now only WiFi services are supported */
696         type = connman_service_get_type(service);
697         if (type != CONNMAN_SERVICE_TYPE_WIFI)
698                 return -ENOSYS;
699
700         g_hash_table_iter_init(&iter, config_table);
701
702         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
703                 struct connman_config *config = value;
704
705                 g_hash_table_foreach(config->service_table,
706                                                 provision_service, service);
707         }
708
709         return 0;
710 }