Remove backup files at cleanup stage
[connman] / src / security.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2008  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 "connman.h"
27
28 static GStaticRWLock security_lock = G_STATIC_RW_LOCK_INIT;
29 static GSList *security_list = NULL;
30
31 static gint compare_priority(gconstpointer a, gconstpointer b)
32 {
33         const struct connman_security *security1 = a;
34         const struct connman_security *security2 = b;
35
36         return security2->priority - security1->priority;
37 }
38
39 /**
40  * connman_security_register:
41  * @security: security module
42  *
43  * Register a new security module
44  *
45  * Returns: %0 on success
46  */
47 int connman_security_register(struct connman_security *security)
48 {
49         DBG("security %p name %s", security, security->name);
50
51         g_static_rw_lock_writer_lock(&security_lock);
52
53         security_list = g_slist_insert_sorted(security_list, security,
54                                                         compare_priority);
55
56         g_static_rw_lock_writer_unlock(&security_lock);
57
58         return 0;
59 }
60
61 /**
62  * connman_security_unregister:
63  * @security: security module
64  *
65  * Remove a previously registered security module
66  */
67 void connman_security_unregister(struct connman_security *security)
68 {
69         DBG("security %p name %s", security, security->name);
70
71         g_static_rw_lock_writer_lock(&security_lock);
72
73         security_list = g_slist_remove(security_list, security);
74
75         g_static_rw_lock_writer_unlock(&security_lock);
76 }
77
78 int __connman_security_check_privileges(DBusMessage *message)
79 {
80         GSList *list;
81         const char *sender;
82         int err = 0;
83
84         DBG("message %p", message);
85
86         sender = dbus_message_get_sender(message);
87
88         g_static_rw_lock_reader_lock(&security_lock);
89
90         for (list = security_list; list; list = list->next) {
91                 struct connman_security *security = list->data;
92
93                 DBG("%s", security->name);
94
95                 if (security->authorize_sender) {
96                         err = security->authorize_sender(sender);
97                         break;
98                 }
99         }
100
101         g_static_rw_lock_reader_unlock(&security_lock);
102
103         return err;
104 }