Add support for setting immutable flag from configurations
[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 <string.h>
28 #include <sys/vfs.h>
29 #include <glib.h>
30
31 #include "connman.h"
32
33 struct connman_config_service {
34         char *ident;
35         char *name;
36         char *type;
37         void *ssid;
38         unsigned int ssid_len;
39         char *eap;
40         char *identity;
41         char *ca_cert_file;
42         char *client_cert_file;
43         char *private_key_file;
44         char *private_key_passphrase;
45         char *private_key_passphrase_type;
46         char *phase2;
47 };
48
49 struct connman_config {
50         char *ident;
51         char *name;
52         char *description;
53         GHashTable *service_table;
54 };
55
56 static GHashTable *config_table = NULL;
57
58 static void unregister_config(gpointer data)
59 {
60         struct connman_config *config = data;
61
62         connman_info("Removing configuration %s", config->ident);
63
64         g_hash_table_destroy(config->service_table);
65
66         g_free(config->description);
67         g_free(config->name);
68         g_free(config->ident);
69         g_free(config);
70 }
71
72 static void unregister_service(gpointer data)
73 {
74         struct connman_config_service *service = data;
75
76         connman_info("Removing service configuration %s", service->ident);
77
78         g_free(service->ident);
79         g_free(service->type);
80         g_free(service->name);
81         g_free(service->ssid);
82         g_free(service->eap);
83         g_free(service->identity);
84         g_free(service->ca_cert_file);
85         g_free(service->client_cert_file);
86         g_free(service->private_key_file);
87         g_free(service->private_key_passphrase);
88         g_free(service->private_key_passphrase_type);
89         g_free(service->phase2);
90         g_free(service);
91 }
92
93 static int load_service(GKeyFile *keyfile, const char *group,
94                                                 struct connman_config *config)
95 {
96         struct connman_config_service *service;
97         const char *ident;
98         char *str, *hex_ssid;
99
100         /* Strip off "service_" prefix */
101         ident = group + 8;
102
103         if (strlen(ident) < 1)
104                 return -EINVAL;
105
106         service = g_hash_table_lookup(config->service_table, ident);
107         if (service == NULL) {
108                 service = g_try_new0(struct connman_config_service, 1);
109                 if (service == NULL)
110                         return -ENOMEM;
111
112                 service->ident = g_strdup(ident);
113         }
114
115         str = g_key_file_get_string(keyfile, group, "Type", NULL);
116         if (str != NULL) {
117                 g_free(service->type);
118                 service->type = str;
119         }
120
121         str = g_key_file_get_string(keyfile, group, "Name", NULL);
122         if (str != NULL) {
123                 g_free(service->type);
124                 service->name = str;
125         }
126
127         hex_ssid = g_key_file_get_string(keyfile, group, "SSID", NULL);
128         if (hex_ssid != NULL) {
129                 char *ssid;
130                 unsigned int i, j = 0, hex;
131                 size_t hex_ssid_len = strlen(hex_ssid);
132
133                 ssid = g_try_malloc0(hex_ssid_len / 2);
134                 if (ssid == NULL) {
135                         g_free(hex_ssid);
136                         return -ENOMEM;
137                 }
138
139                 for (i = 0; i < hex_ssid_len; i += 2) {
140                         sscanf(hex_ssid + i, "%02x", &hex);
141                         ssid[j++] = hex;
142                 }
143
144                 g_free(hex_ssid);
145
146                 g_free(service->ssid);
147                 service->ssid = ssid;
148                 service->ssid_len = hex_ssid_len / 2;
149         } else if (service->name != NULL) {
150                 char *ssid;
151                 unsigned int ssid_len;
152
153                 ssid_len = strlen(service->name);
154                 ssid = g_try_malloc0(ssid_len);
155                 if (ssid == NULL)
156                         return -ENOMEM;
157
158                 memcpy(ssid, service->name, ssid_len);
159                 g_free(service->ssid);
160                 service->ssid = ssid;
161                 service->ssid_len = ssid_len;
162         }
163
164         str = g_key_file_get_string(keyfile, group, "EAP", NULL);
165         if (str != NULL) {
166                 g_free(service->eap);
167                 service->eap = str;
168         }
169
170         str = g_key_file_get_string(keyfile, group, "CACertFile", NULL);
171         if (str != NULL) {
172                 g_free(service->ca_cert_file);
173                 service->ca_cert_file = str;
174         }
175
176         str = g_key_file_get_string(keyfile, group, "ClientCertFile", NULL);
177         if (str != NULL) {
178                 g_free(service->client_cert_file);
179                 service->client_cert_file = str;
180         }
181
182         str = g_key_file_get_string(keyfile, group, "PrivateKeyFile", NULL);
183         if (str != NULL) {
184                 g_free(service->private_key_file);
185                 service->private_key_file = str;
186         }
187
188         str = g_key_file_get_string(keyfile, group,
189                                                 "PrivateKeyPassphrase", NULL);
190         if (str != NULL) {
191                 g_free(service->private_key_passphrase);
192                 service->private_key_passphrase = str;
193         }
194
195         str = g_key_file_get_string(keyfile, group,
196                                         "PrivateKeyPassphraseType", NULL);
197         if (str != NULL) {
198                 g_free(service->private_key_passphrase_type);
199                 service->private_key_passphrase_type = str;
200         }
201
202         str = g_key_file_get_string(keyfile, group, "Identity", NULL);
203         if (str != NULL) {
204                 g_free(service->identity);
205                 service->identity = str;
206         }
207
208         str = g_key_file_get_string(keyfile, group, "Phase2", NULL);
209         if (str != NULL) {
210                 g_free(service->phase2);
211                 service->phase2 = str;
212         }
213
214         g_hash_table_replace(config->service_table, service->ident, service);
215
216         connman_info("Adding service configuration %s", service->ident);
217
218         return 0;
219 }
220
221 static int load_config(struct connman_config *config)
222 {
223         GKeyFile *keyfile;
224         gsize length;
225         char **groups;
226         char *str;
227         int i;
228
229         DBG("config %p", config);
230
231         keyfile = __connman_storage_open_config(config->ident);
232         if (keyfile == NULL)
233                 return -EIO;
234
235         str = g_key_file_get_string(keyfile, "global", "Name", NULL);
236         if (str != NULL) {
237                 g_free(config->name);
238                 config->name = str;
239         }
240
241         str = g_key_file_get_string(keyfile, "global", "Description", NULL);
242         if (str != NULL) {
243                 g_free(config->description);
244                 config->description = str;
245         }
246
247         groups = g_key_file_get_groups(keyfile, &length);
248
249         for (i = 0; groups[i] != NULL; i++) {
250                 if (g_str_has_prefix(groups[i], "service_") == TRUE)
251                         load_service(keyfile, groups[i], config);
252         }
253
254         g_strfreev(groups);
255
256         __connman_storage_close_config(config->ident, keyfile, FALSE);
257
258         return 0;
259 }
260
261 static int create_config(const char *ident)
262 {
263         struct connman_config *config;
264
265         DBG("ident %s", ident);
266
267         if (g_hash_table_lookup(config_table, ident) != NULL)
268                 return -EEXIST;
269
270         config = g_try_new0(struct connman_config, 1);
271         if (config == NULL)
272                 return -ENOMEM;
273
274         config->ident = g_strdup(ident);
275
276         config->service_table = g_hash_table_new_full(g_str_hash, g_str_equal,
277                                                 NULL, unregister_service);
278
279         g_hash_table_insert(config_table, config->ident, config);
280
281         connman_info("Adding configuration %s", config->ident);
282
283         load_config(config);
284
285         return 0;
286 }
287
288 static int read_configs(void)
289 {
290         GDir *dir;
291
292         DBG("");
293
294         dir = g_dir_open(STORAGEDIR, 0, NULL);
295         if (dir != NULL) {
296                 const gchar *file;
297
298                 while ((file = g_dir_read_name(dir)) != NULL) {
299                         GString *str;
300                         gchar *ident;
301
302                         if (g_str_has_suffix(file, ".config") == FALSE)
303                                 continue;
304
305                         ident = g_strrstr(file, ".config");
306                         if (ident == NULL)
307                                 continue;
308
309                         str = g_string_new_len(file, ident - file);
310                         if (str == NULL)
311                                 continue;
312
313                         ident = g_string_free(str, FALSE);
314
315                         if (connman_dbus_validate_ident(ident) == TRUE)
316                                 create_config(ident);
317
318                         g_free(ident);
319                 }
320
321                 g_dir_close(dir);
322         }
323
324         return 0;
325 }
326
327 int __connman_config_init(void)
328 {
329         DBG("");
330
331         config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
332                                                 NULL, unregister_config);
333
334         return read_configs();
335 }
336
337 void __connman_config_cleanup(void)
338 {
339         DBG("");
340
341         g_hash_table_destroy(config_table);
342         config_table = NULL;
343 }
344
345 static char *config_pem_fsid(const char *pem_file)
346 {
347         struct statfs buf;
348         unsigned *fsid = (unsigned *) &buf.f_fsid;
349         unsigned long long fsid64;
350
351         if (pem_file == NULL)
352                 return NULL;
353
354         if (statfs(pem_file, &buf) < 0) {
355                 connman_error("statfs error %s for %s",
356                                                 strerror(errno), pem_file);
357                 return NULL;
358         }
359
360         fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
361
362         return g_strdup_printf("%llx", fsid64);
363 }
364
365 static void provision_service(gpointer key, gpointer value, gpointer user_data)
366 {
367         struct connman_service *service = user_data;
368         struct connman_config_service *config = value;
369         struct connman_network *network;
370         const void *ssid;
371         unsigned int ssid_len;
372
373         /* For now only WiFi service entries are supported */
374         if (g_strcmp0(config->type, "wifi") != 0)
375                 return;
376
377         network = __connman_service_get_network(service);
378         if (network == NULL) {
379                 connman_error("Service has no network set");
380                 return;
381         }
382
383         ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
384         if (ssid == NULL) {
385                 connman_error("Network SSID not set");
386                 return;
387         }
388
389         if (config->ssid == NULL || ssid_len != config->ssid_len)
390                 return;
391
392         if (memcmp(config->ssid, ssid, ssid_len) != 0)
393                 return;
394
395         __connman_service_set_immutable(service, TRUE);
396         __connman_service_set_favorite(service, TRUE);
397
398         if (config->eap)
399                 __connman_service_set_string(service, "EAP", config->eap);
400
401         if (config->identity)
402                 __connman_service_set_string(service, "Identity",
403                                                         config->identity);
404
405         if (config->ca_cert_file)
406                 __connman_service_set_string(service, "CACertFile",
407                                                         config->ca_cert_file);
408
409         if (config->client_cert_file)
410                 __connman_service_set_string(service, "ClientCertFile",
411                                                 config->client_cert_file);
412
413         if (config->private_key_file)
414                 __connman_service_set_string(service, "PrivateKeyFile",
415                                                 config->private_key_file);
416
417         if (g_strcmp0(config->private_key_passphrase_type, "fsid") == 0 &&
418                                                 config->private_key_file) {
419                 char *fsid;
420
421                 fsid = config_pem_fsid(config->private_key_file);
422                 if (fsid == NULL)
423                         return;
424
425                 g_free(config->private_key_passphrase);
426                 config->private_key_passphrase = fsid;
427         }
428
429         if (config->private_key_passphrase) {
430                 __connman_service_set_string(service, "PrivateKeyPassphrase",
431                                                 config->private_key_passphrase);
432                 /*
433                  * TODO: Support for PEAP with both identity and key passwd.
434                  * In that case, we should check if both of them are found
435                  * from the config file. If not, we should not set the
436                  * service passphrase in order for the UI to request for an
437                  * additional passphrase.
438                  */
439                 __connman_service_set_string(service, "Passphrase",
440                                                 config->private_key_passphrase);
441         }
442
443         if (config->phase2)
444                 __connman_service_set_string(service, "Phase2", config->phase2);
445 }
446
447 int __connman_config_provision_service(struct connman_service *service)
448 {
449         enum connman_service_type type;
450         GHashTableIter iter;
451         gpointer value, key;
452
453         DBG("service %p", service);
454
455         /* For now only WiFi services are supported */
456         type = connman_service_get_type(service);
457         if (type != CONNMAN_SERVICE_TYPE_WIFI)
458                 return -ENOSYS;
459
460         g_hash_table_iter_init(&iter, config_table);
461
462         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
463                 struct connman_config *config = value;
464
465                 g_hash_table_foreach(config->service_table,
466                                                 provision_service, service);
467         }
468
469         return 0;
470 }