Fix copy-and-paste issue with SSID configuration field
[platform/upstream/connman.git] / src / config.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  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->ssid);
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->type);
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         g_free(service->type);
155         g_free(service->ssid);
156         g_free(service->eap);
157         g_free(service->identity);
158         g_free(service->ca_cert_file);
159         g_free(service->client_cert_file);
160         g_free(service->private_key_file);
161         g_free(service->private_key_passphrase);
162         g_free(service->phase2);
163         g_free(service);
164 }
165
166 static int load_config(struct connman_config *config)
167 {
168         GKeyFile *keyfile;
169         char *str;
170         int err;
171
172         DBG("config %p", config);
173
174         keyfile = __connman_storage_open_config(config->ident);
175         if (keyfile == NULL)
176                 return -EIO;
177
178         str = g_key_file_get_string(keyfile, "global", "Name", NULL);
179         if (str != NULL) {
180                 g_free(config->name);
181                 config->name = str;
182         }
183
184         str = g_key_file_get_string(keyfile, "global", "Description", NULL);
185         if (str != NULL) {
186                 g_free(config->description);
187                 config->description = str;
188         }
189
190         if (g_key_file_has_group(keyfile, "service") == TRUE) {
191                 err = load_service(keyfile, config);
192                 if (err < 0)
193                         goto done;
194         }
195
196         err = 0;
197
198 done:
199         __connman_storage_close_config(config->ident, keyfile, FALSE);
200
201         return err;
202 }
203
204 static void free_config(struct connman_config *config)
205 {
206         g_free(config->description);
207         g_free(config->name);
208         g_free(config->ident);
209         free_service(config->service);
210         g_free(config);
211 }
212
213 static void unregister_config(gpointer data)
214 {
215         struct connman_config *config = data;
216
217         connman_info("Removing configuration %s", config->ident);
218
219         free_config(config);
220 }
221
222 static int create_config(const char *ident)
223 {
224         struct connman_config *config;
225
226         DBG("ident %s", ident);
227
228         config = g_try_new0(struct connman_config, 1);
229         if (config == NULL)
230                 return -ENOMEM;
231
232         config->ident = g_strdup(ident);
233
234         if (config->ident == NULL) {
235                 free_config(config);
236                 return -ENOMEM;
237         }
238
239         if (g_hash_table_lookup(config_hash, config->ident) != NULL) {
240                 free_config(config);
241                 return -EEXIST;
242         }
243
244         g_hash_table_insert(config_hash, g_strdup(config->ident), config);
245
246         connman_info("Adding configuration %s", config->ident);
247
248         load_config(config);
249
250         return 0;
251 }
252
253 static int config_init(void)
254 {
255         GDir *dir;
256         const gchar *file;
257
258         DBG("");
259
260         dir = g_dir_open(STORAGEDIR, 0, NULL);
261         if (dir != NULL) {
262                 while ((file = g_dir_read_name(dir)) != NULL) {
263                         GString *str;
264                         gchar *ident;
265
266                         if (g_str_has_suffix(file, ".config") == FALSE)
267                                 continue;
268
269                         ident = g_strrstr(file, ".config");
270                         if (ident == NULL)
271                                 continue;
272
273                         str = g_string_new_len(file, ident - file);
274                         if (str == NULL)
275                                 continue;
276
277                         ident = g_string_free(str, FALSE);
278
279                         if (connman_dbus_validate_ident(ident) == TRUE)
280                                 create_config(ident);
281
282                         g_free(ident);
283                 }
284
285                 g_dir_close(dir);
286         }
287
288         return 0;
289 }
290
291 static char *config_pem_fsid(char *pem_file)
292 {
293         struct statfs buf;
294         unsigned *fsid = (unsigned *) &buf.f_fsid;
295         unsigned long long fsid64;
296
297         if (pem_file == NULL)
298                 return NULL;
299
300         if (statfs(pem_file, &buf) < 0) {
301                 connman_error("statfs error %s for %s",
302                                                 strerror(errno), pem_file);
303                 return NULL;
304         }
305
306         fsid64 = ((unsigned long long) fsid[0] << 32) | fsid[1];
307
308         return g_strdup_printf("%llx", fsid64);
309 }
310
311 static void config_service_setup(struct connman_service *service,
312                                  struct connman_config_service *config)
313 {
314         if (config == NULL)
315                 return;
316
317         if (config->eap)
318                 __connman_service_set_string(service, "EAP", config->eap);
319
320         if (config->identity)
321                 __connman_service_set_string(service, "Identity",
322                                                         config->identity);
323
324         if (config->ca_cert_file)
325                 __connman_service_set_string(service, "CACertFile",
326                                                         config->ca_cert_file);
327
328         if (config->client_cert_file)
329                 __connman_service_set_string(service, "ClientCertFile",
330                                                 config->client_cert_file);
331
332         if (config->private_key_file)
333                 __connman_service_set_string(service, "PrivateKeyFile",
334                                                 config->private_key_file);
335
336         if (config->passphrase_from_fsid == TRUE &&
337                                         config->private_key_file) {
338                 char *fsid;
339
340                 fsid = config_pem_fsid(config->private_key_file);
341                 if (fsid == NULL)
342                         return;
343
344                 g_free(config->private_key_passphrase);
345                 config->private_key_passphrase = fsid;
346         }
347
348         if (config->private_key_passphrase) {
349                 __connman_service_set_string(service, "PrivateKeyPassphrase",
350                                                 config->private_key_passphrase);
351                 /*
352                  * TODO: Support for PEAP with both identity and key passwd.
353                  * In that case, we should check if both of them are found
354                  * from the config file. If not, we should not set the
355                  * service passphrase in order for the UI to request for an
356                  * additional passphrase.
357                  */
358                 __connman_service_set_string(service, "Passphrase",
359                                                 config->private_key_passphrase);
360         }
361
362         if (config->phase2)
363                 __connman_service_set_string(service, "Phase2", config->phase2);
364 }
365
366 int __connman_config_init(void)
367 {
368         DBG("");
369
370         config_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
371                                                 g_free, unregister_config);
372
373         return config_init();
374 }
375
376 void __connman_config_cleanup(void)
377 {
378         DBG("");
379
380         g_hash_table_destroy(config_hash);
381         config_hash = NULL;
382 }
383
384 int __connman_config_provision_service(struct connman_service *service)
385 {
386         GHashTableIter iter;
387         gpointer value, key;
388         struct connman_network *network;
389         struct connman_config *config = NULL;
390         const void *ssid;
391         unsigned int ssid_len;
392
393         DBG("service %p", service);
394
395         network = __connman_service_get_network(service);
396         if (network == NULL) {
397                 connman_error("Network not set");
398                 return -EINVAL;
399         }
400
401         ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
402         if (ssid == NULL) {
403                 connman_error("Network SSID not set");
404                 return -EINVAL;
405         }
406
407         g_hash_table_iter_init(&iter, config_hash);
408
409         while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
410                 config = value;
411
412                 /* For now only WiFi service entries are supported */
413                 if (config->service &&
414                                 g_strcmp0(config->service->type, "wifi") == 0 &&
415                                 ssid_len == config->service->ssid_len)
416                         if (config->service->ssid &&
417                                         memcmp(config->service->ssid, ssid,
418                                                                 ssid_len) == 0)
419                                 break;
420         }
421
422         config_service_setup(service, config->service);
423
424         return 0;
425 }