c94c17e19e8b38f8a2c7244d45bc2fa0d538b653
[platform/upstream/dbus.git] / bus / policy.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* policy.c  Policies for what a connection can do
3  *
4  * Copyright (C) 2003  Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 1.2
7  * 
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 #include "policy.h"
25
26 BusPolicyRule*
27 bus_policy_rule_new (BusPolicyRuleType type,
28                      dbus_bool_t       allow)
29 {
30   BusPolicyRule *rule;
31
32   rule = dbus_new0 (BusPolicyRule, 1);
33   if (rule == NULL)
34     return NULL;
35
36   rule->type = type;
37   rule->refcount = 1;
38   rule->allow = allow;
39
40   return rule;
41 }
42
43 void
44 bus_policy_rule_ref (BusPolicyRule *rule)
45 {
46   _dbus_assert (rule->refcount > 0);
47
48   rule->refcount += 1;
49 }
50
51 void
52 bus_policy_rule_unref (BusPolicyRule *rule)
53 {
54   _dbus_assert (rule->refcount > 0);
55
56   rule->refcount -= 1;
57
58   if (rule->refcount == 0)
59     {
60       switch (rule->type)
61         {
62         case DBUS_POLICY_RULE_SEND:
63           dbus_free (rule->d.send.message_name);
64           dbus_free (rule->d.send.destination);
65           break;
66         case DBUS_POLICY_RULE_RECEIVE:
67           dbus_free (rule->d.receive.message_name);
68           dbus_free (rule->d.receive.origin);
69           break;
70         case DBUS_POLICY_RULE_OWN:
71           dbus_free (rule->d.own.service_name);
72           break;
73         }
74       
75       dbus_free (rule);
76     }
77 }
78
79 struct BusPolicy
80 {
81   int refcount;
82
83   DBusList *rules;
84 };
85
86 BusPolicy*
87 bus_policy_new (void)
88 {
89   BusPolicy *policy;
90
91   policy = dbus_new0 (BusPolicy, 1);
92   if (policy == NULL)
93     return NULL;
94
95   policy->refcount = 1;
96
97   return policy;
98 }
99
100 void
101 bus_policy_ref (BusPolicy *policy)
102 {
103   _dbus_assert (policy->refcount > 0);
104
105   policy->refcount += 1;
106 }
107
108 static void
109 rule_unref_foreach (void *data,
110                     void *user_data)
111 {
112   BusPolicyRule *rule = data;
113
114   bus_policy_rule_unref (rule);
115 }
116
117 void
118 bus_policy_unref (BusPolicy *policy)
119 {
120   _dbus_assert (policy->refcount > 0);
121
122   policy->refcount -= 1;
123
124   if (policy->refcount == 0)
125     {
126       _dbus_list_foreach (&policy->rules,
127                           rule_unref_foreach,
128                           NULL);
129
130       _dbus_list_clear (&policy->rules);
131       
132       dbus_free (policy);
133     }
134 }
135
136 dbus_bool_t
137 bus_policy_check_can_send (BusPolicy      *policy,
138                            DBusConnection *sender,
139                            DBusMessage    *message)
140 {
141   
142
143 }
144
145 dbus_bool_t
146 bus_policy_check_can_receive (BusPolicy      *policy,
147                               DBusConnection *receiver,
148                               DBusMessage    *message)
149 {
150
151
152 }
153
154 dbus_bool_t
155 bus_policy_check_can_own (BusPolicy      *policy,
156                           DBusConnection *connection,
157                           const char     *service_name)
158 {
159
160
161 }
162
163 #endif /* BUS_POLICY_H */