profile: Ignore malformed profiles
[framework/connectivity/connman.git] / src / profile.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 <string.h>
27
28 #include <glib.h>
29 #include <gdbus.h>
30
31 #include "connman.h"
32
33 #define PROFILE_DEFAULT_IDENT  "default"
34
35 struct connman_profile {
36         char *ident;
37         char *path;
38         char *name;
39         connman_bool_t offlinemode;
40 };
41
42 static GHashTable *profile_hash = NULL;
43 static struct connman_profile *default_profile = NULL;
44
45 static DBusConnection *connection = NULL;
46
47 static void append_path(gpointer key, gpointer value, gpointer user_data)
48 {
49         struct connman_profile *profile = value;
50         DBusMessageIter *iter = user_data;
51
52         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
53                                                         &profile->path);
54 }
55
56 void __connman_profile_list(DBusMessageIter *iter, void *user_data)
57 {
58         g_hash_table_foreach(profile_hash, append_path, iter);
59 }
60
61 static void profiles_changed(void)
62 {
63         connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
64                         CONNMAN_MANAGER_INTERFACE, "Profiles",
65                         DBUS_TYPE_OBJECT_PATH, __connman_profile_list, NULL);
66 }
67
68 static void name_changed(struct connman_profile *profile)
69 {
70         connman_dbus_property_changed_basic(profile->path,
71                                 CONNMAN_PROFILE_INTERFACE, "Name",
72                                         DBUS_TYPE_STRING, &profile->name);
73 }
74
75 static void offlinemode_changed(struct connman_profile *profile)
76 {
77         connman_dbus_property_changed_basic(profile->path,
78                                 CONNMAN_PROFILE_INTERFACE, "OfflineMode",
79                                 DBUS_TYPE_BOOLEAN, &profile->offlinemode);
80
81         if (profile != default_profile)
82                 return;
83
84         connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
85                                 CONNMAN_MANAGER_INTERFACE, "OfflineMode",
86                                 DBUS_TYPE_BOOLEAN, &profile->offlinemode);
87 }
88
89 connman_bool_t __connman_profile_get_offlinemode(void)
90 {
91         if (default_profile == NULL)
92                 return FALSE;
93
94         DBG("offlinemode %d", default_profile->offlinemode);
95
96         return default_profile->offlinemode;
97 }
98
99 int __connman_profile_set_offlinemode(connman_bool_t offlinemode,
100                                         connman_bool_t all_devices)
101 {
102         DBG("offlinemode %d", offlinemode);
103
104         if (default_profile == NULL)
105                 return -EINVAL;
106
107         if (default_profile->offlinemode == offlinemode)
108                 return -EALREADY;
109
110         default_profile->offlinemode = offlinemode;
111         offlinemode_changed(default_profile);
112
113         if (all_devices)
114                 __connman_device_set_offlinemode(offlinemode);
115
116         return 0;
117 }
118
119 int __connman_profile_save_default(void)
120 {
121         DBG("");
122
123         if (default_profile != NULL)
124                 __connman_storage_save_profile(default_profile);
125
126         return 0;
127 }
128
129 const char *__connman_profile_active_ident(void)
130 {
131         DBG("");
132
133         return PROFILE_DEFAULT_IDENT;
134 }
135
136 const char *__connman_profile_active_path(void)
137 {
138         DBG("");
139
140         if (default_profile == NULL)
141                 return NULL;
142
143         return default_profile->path;
144 }
145
146 static guint changed_timeout = 0;
147
148 static gboolean services_changed(gpointer user_data)
149 {
150         struct connman_profile *profile = default_profile;
151         connman_dbus_append_cb_t function = NULL;
152
153         changed_timeout = 0;
154
155         if (profile == NULL)
156                 return FALSE;
157
158         if (g_strcmp0(profile->ident, PROFILE_DEFAULT_IDENT) == 0) {
159                 function = __connman_service_list;
160
161                 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
162                                 CONNMAN_MANAGER_INTERFACE, "Services",
163                                 DBUS_TYPE_OBJECT_PATH, function, NULL);
164         }
165
166         connman_dbus_property_changed_array(profile->path,
167                                 CONNMAN_PROFILE_INTERFACE, "Services",
168                                 DBUS_TYPE_OBJECT_PATH, function, NULL);
169
170         return FALSE;
171 }
172
173 void __connman_profile_changed(gboolean delayed)
174 {
175         DBG("");
176
177         if (changed_timeout > 0) {
178                 g_source_remove(changed_timeout);
179                 changed_timeout = 0;
180         }
181
182         if (__connman_connection_update_gateway() == TRUE) {
183                 services_changed(NULL);
184                 return;
185         }
186
187         if (delayed == FALSE) {
188                 services_changed(NULL);
189                 return;
190         }
191
192         changed_timeout = g_timeout_add_seconds(1, services_changed, NULL);
193 }
194
195 int __connman_profile_add_network(struct connman_network *network)
196 {
197         struct connman_service *service;
198
199         DBG("network %p", network);
200
201         service = __connman_service_create_from_network(network);
202         if (service == NULL)
203                 return -EINVAL;
204
205         return 0;
206 }
207
208 int __connman_profile_update_network(struct connman_network *network)
209 {
210         DBG("network %p", network);
211
212         __connman_service_update_from_network(network);
213
214         return 0;
215 }
216
217 int __connman_profile_remove_network(struct connman_network *network)
218 {
219         DBG("network %p", network);
220
221         __connman_service_remove_from_network(network);
222
223         return 0;
224 }
225
226 static DBusMessage *get_properties(DBusConnection *conn,
227                                         DBusMessage *msg, void *data)
228 {
229         struct connman_profile *profile = data;
230         DBusMessage *reply;
231         DBusMessageIter array, dict;
232
233         DBG("conn %p", conn);
234
235         reply = dbus_message_new_method_return(msg);
236         if (reply == NULL)
237                 return NULL;
238
239         dbus_message_iter_init_append(reply, &array);
240
241         connman_dbus_dict_open(&array, &dict);
242
243         if (profile->name != NULL)
244                 connman_dbus_dict_append_basic(&dict, "Name",
245                                         DBUS_TYPE_STRING, &profile->name);
246
247         connman_dbus_dict_append_basic(&dict, "OfflineMode",
248                                 DBUS_TYPE_BOOLEAN, &profile->offlinemode);
249
250         connman_dbus_dict_append_array(&dict, "Services",
251                         DBUS_TYPE_OBJECT_PATH, __connman_service_list, NULL);
252
253         connman_dbus_dict_close(&array, &dict);
254
255         return reply;
256 }
257
258 static DBusMessage *set_property(DBusConnection *conn,
259                                         DBusMessage *msg, void *data)
260 {
261         struct connman_profile *profile = data;
262         DBusMessageIter iter, value;
263         const char *name;
264         int type;
265
266         DBG("conn %p", conn);
267
268         if (dbus_message_iter_init(msg, &iter) == FALSE)
269                 return __connman_error_invalid_arguments(msg);
270
271         dbus_message_iter_get_basic(&iter, &name);
272         dbus_message_iter_next(&iter);
273         dbus_message_iter_recurse(&iter, &value);
274
275         type = dbus_message_iter_get_arg_type(&value);
276
277         if (g_str_equal(name, "Name") == TRUE) {
278                 const char *name;
279
280                 if (type != DBUS_TYPE_STRING)
281                         return __connman_error_invalid_arguments(msg);
282
283                 dbus_message_iter_get_basic(&value, &name);
284
285                 g_free(profile->name);
286                 profile->name = g_strdup(name);
287
288                 if (profile->name != NULL)
289                         name_changed(profile);
290
291                 __connman_storage_save_profile(profile);
292         } else if (g_str_equal(name, "OfflineMode") == TRUE) {
293                 connman_bool_t offlinemode;
294
295                 if (type != DBUS_TYPE_BOOLEAN)
296                         return __connman_error_invalid_arguments(msg);
297
298                 dbus_message_iter_get_basic(&value, &offlinemode);
299
300                 if (profile->offlinemode == offlinemode)
301                         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
302
303                 profile->offlinemode = offlinemode;
304                 offlinemode_changed(profile);
305
306                 __connman_storage_save_profile(profile);
307         } else
308                 return __connman_error_invalid_property(msg);
309
310         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
311 }
312
313 static GDBusMethodTable profile_methods[] = {
314         { "GetProperties", "",   "a{sv}", get_properties },
315         { "SetProperty",   "sv", "",      set_property   },
316         { },
317 };
318
319 static GDBusSignalTable profile_signals[] = {
320         { "PropertyChanged", "sv" },
321         { },
322 };
323
324 static void free_profile(struct connman_profile *profile)
325 {
326         g_free(profile->name);
327         g_free(profile->path);
328         g_free(profile->ident);
329         g_free(profile);
330 }
331
332 static void unregister_profile(gpointer data)
333 {
334         struct connman_profile *profile = data;
335
336         DBG("profile %p", profile);
337
338         connman_info("Removing profile %s", profile->ident);
339
340         g_dbus_unregister_interface(connection, profile->path,
341                                                 CONNMAN_PROFILE_INTERFACE);
342
343         if (g_strcmp0(profile->ident, PROFILE_DEFAULT_IDENT) == 0)
344                 default_profile = NULL;
345
346         free_profile(profile);
347 }
348
349 static int create_profile(const char *ident, const char *name,
350                                                         const char **path)
351 {
352         struct connman_profile *profile;
353
354         DBG("ident %s name %s", ident, name);
355
356         profile = g_try_new0(struct connman_profile, 1);
357         if (profile == NULL)
358                 return -ENOMEM;
359
360         profile->ident = g_strdup(ident);
361         profile->path = g_strdup_printf("/profile/%s", ident);
362
363         if (profile->ident == NULL || profile->path == NULL) {
364                 free_profile(profile);
365                 return -ENOMEM;
366         }
367
368         if (g_hash_table_lookup(profile_hash, profile->path) != NULL) {
369                 free_profile(profile);
370                 return -EEXIST;
371         }
372
373         profile->name = g_strdup(name);
374
375         __connman_storage_load_profile(profile);
376
377         g_hash_table_insert(profile_hash, g_strdup(profile->path), profile);
378
379         connman_info("Adding profile %s", ident);
380
381         if (g_strcmp0(ident, PROFILE_DEFAULT_IDENT) == 0)
382                 default_profile = profile;
383
384         g_dbus_register_interface(connection, profile->path,
385                                         CONNMAN_PROFILE_INTERFACE,
386                                         profile_methods, profile_signals,
387                                                         NULL, profile, NULL);
388
389         if (path != NULL)
390                 *path = profile->path;
391
392         DBG("profile %p path %s", profile, profile->path);
393
394         return 0;
395 }
396
397 int __connman_profile_create(const char *name, const char **path)
398 {
399         struct connman_profile *profile;
400         int err;
401
402         DBG("name %s", name);
403
404         if (connman_dbus_validate_ident(name) == FALSE)
405                 return -EINVAL;
406
407         err = create_profile(name, NULL, path);
408         if (err < 0)
409                 return err;
410
411         profile = g_hash_table_lookup(profile_hash, *path);
412         if (profile == NULL)
413                 return -EIO;
414
415         __connman_storage_save_profile(profile);
416
417         profiles_changed();
418
419         return 0;
420 }
421
422 int __connman_profile_remove(const char *path)
423 {
424         struct connman_profile *profile;
425
426         DBG("path %s", path);
427
428         if (default_profile != NULL &&
429                                 g_strcmp0(path, default_profile->path) == 0)
430                 return -EINVAL;
431
432         profile = g_hash_table_lookup(profile_hash, path);
433         if (profile == NULL)
434                 return -ENXIO;
435
436         __connman_storage_delete_profile(profile->ident);
437
438         g_hash_table_remove(profile_hash, path);
439
440         profiles_changed();
441
442         return 0;
443 }
444
445 static int profile_init(void)
446 {
447         GDir *dir;
448         const gchar *file;
449
450         DBG("");
451
452         dir = g_dir_open(STORAGEDIR, 0, NULL);
453         if (dir != NULL) {
454                 while ((file = g_dir_read_name(dir)) != NULL) {
455                         GString *str;
456                         gchar *ident;
457
458                         if (g_str_has_suffix(file, ".profile") == FALSE)
459                                 continue;
460
461                         ident = g_strrstr(file, ".profile");
462                         if (ident == NULL)
463                                 continue;
464
465                         /*
466                          * If there is no non-extension portion to the file
467                          * (i.e. it is precisely named ".profile"), then ignore
468                          * it. Passing such files to create_profile will result
469                          * in a SIGABRT.
470                          */
471
472                         if (file == ident) {
473                                 connman_info("Ignoring malformed profile %s\n",
474                                                 file);
475                                 continue;
476                         }
477
478                         str = g_string_new_len(file, ident - file);
479                         if (str == NULL)
480                                 continue;
481
482                         ident = g_string_free(str, FALSE);
483
484                         if (connman_dbus_validate_ident(ident) == TRUE)
485                                 create_profile(ident, NULL, NULL);
486
487                         g_free(ident);
488                 }
489
490                 g_dir_close(dir);
491         }
492
493         if (default_profile == NULL)
494                 create_profile(PROFILE_DEFAULT_IDENT, "Default", NULL);
495
496         profiles_changed();
497
498         return 0;
499 }
500
501 static int profile_load(struct connman_profile *profile)
502 {
503         GKeyFile *keyfile;
504         GError *error = NULL;
505         connman_bool_t offlinemode;
506         char *name;
507
508         DBG("profile %p", profile);
509
510         keyfile = __connman_storage_open_profile(profile->ident);
511         if (keyfile == NULL)
512                 return -EIO;
513
514         name = g_key_file_get_string(keyfile, "global", "Name", NULL);
515         if (name != NULL) {
516                 g_free(profile->name);
517                 profile->name = name;
518         }
519
520         offlinemode = g_key_file_get_boolean(keyfile, "global",
521                                                 "OfflineMode", &error);
522         if (error == NULL)
523                 profile->offlinemode = offlinemode;
524         g_clear_error(&error);
525
526         __connman_storage_close_profile(profile->ident, keyfile, FALSE);
527
528         return 0;
529 }
530
531 static int profile_save(struct connman_profile *profile)
532 {
533         GKeyFile *keyfile;
534
535         DBG("profile %p", profile);
536
537         keyfile = __connman_storage_open_profile(profile->ident);
538         if (keyfile == NULL)
539                 return -EIO;
540
541         if (profile->name != NULL)
542                 g_key_file_set_string(keyfile, "global",
543                                                 "Name", profile->name);
544
545         g_key_file_set_boolean(keyfile, "global",
546                                         "OfflineMode", profile->offlinemode);
547
548         __connman_storage_close_profile(profile->ident, keyfile, TRUE);
549
550         return 0;
551 }
552
553 static struct connman_storage profile_storage = {
554         .name           = "profile",
555         .priority       = CONNMAN_STORAGE_PRIORITY_LOW,
556         .profile_init   = profile_init,
557         .profile_load   = profile_load,
558         .profile_save   = profile_save,
559 };
560
561 int __connman_profile_init(void)
562 {
563         DBG("");
564
565         connection = connman_dbus_get_connection();
566         if (connection == NULL)
567                 return -1;
568
569         if (connman_storage_register(&profile_storage) < 0)
570                 connman_error("Failed to register profile storage");
571
572         profile_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
573                                                 g_free, unregister_profile);
574
575         return 0;
576 }
577
578 void __connman_profile_cleanup(void)
579 {
580         DBG("");
581
582         if (connection == NULL)
583                 return;
584
585         g_hash_table_destroy(profile_hash);
586         profile_hash = NULL;
587
588         connman_storage_unregister(&profile_storage);
589
590         dbus_connection_unref(connection);
591 }