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