Separate firewall function plugin
[platform/core/connectivity/stc-manager.git] / src / stc-firewall.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "stc-db.h"
18 #include "table-firewall.h"
19 #include "helper-firewall.h"
20 #include "stc-firewall.h"
21 #include "stc-manager-gdbus.h"
22 #include "stc-manager-plugin-firewall.h"
23
24 #define FIREWALL_DBUS_ERROR_NAME "net.stc.firewall.Error.Failed"
25
26 #define STC_FIREWALL_DBUS_REPLY_ERROR(invocation, err_num) \
27         g_dbus_method_invocation_return_dbus_error((invocation), \
28                                                    FIREWALL_DBUS_ERROR_NAME, \
29                                                    stc_err_strs[-(err_num)])
30
31 static const gchar *stc_err_strs[] = {
32         "ERROR_NONE",
33         "FAIL",
34         "DB_FAILED",
35         "OUT_OF_MEMORY",
36         "INVALID_PARAMETER",
37         "NO_DATA",
38         "ALREADY_DATA",
39         "UNINITIALIZED",
40         "PERMISSION_DENIED",
41         "NOTIMPL"
42 };
43
44 gboolean handle_firewall_lock(StcFirewall *object,
45                                         GDBusMethodInvocation *invocation,
46                                         void *user_data)
47 {
48         __STC_LOG_FUNC_ENTER__;
49
50         stc_plugin_firewall_lock();
51
52         STC_DBUS_REPLY_ERROR_NONE(invocation);
53         __STC_LOG_FUNC_EXIT__;
54         return TRUE;
55 }
56
57 gboolean handle_firewall_unlock(StcFirewall *object,
58                                         GDBusMethodInvocation *invocation,
59                                         void *user_data)
60 {
61         __STC_LOG_FUNC_ENTER__;
62
63         stc_plugin_firewall_unlock();
64
65         STC_DBUS_REPLY_ERROR_NONE(invocation);
66         __STC_LOG_FUNC_EXIT__;
67         return TRUE;
68 }
69
70 gboolean handle_firewall_get_lock(StcFirewall *object,
71                                         GDBusMethodInvocation *invocation,
72                                         void *user_data)
73 {
74         __STC_LOG_FUNC_ENTER__;
75         GVariant *return_parameters = NULL;
76         int state = 0;
77
78         stc_plugin_firewall_get_lock(&state);
79
80         return_parameters = g_variant_new("(i)", state);
81         STC_DBUS_REPLY(invocation, return_parameters);
82         __STC_LOG_FUNC_EXIT__;
83         return TRUE;
84 }
85
86 gboolean handle_firewall_add_chain(StcFirewall *object,
87                                         GDBusMethodInvocation *invocation,
88                                         gchar *chain,
89                                         void *user_data)
90 {
91         __STC_LOG_FUNC_ENTER__;
92         int ret = STC_ERROR_NONE;
93
94         if (chain == NULL) {
95                 STC_FIREWALL_DBUS_REPLY_ERROR(invocation,
96                                                 STC_ERROR_INVALID_PARAMETER);
97                 __STC_LOG_FUNC_EXIT__;
98                 return TRUE;
99         }
100
101         ret = stc_plugin_firewall_add_chain(chain);
102         if (ret != STC_ERROR_NONE) {
103                 STC_FIREWALL_DBUS_REPLY_ERROR(invocation, ret);
104                 __STC_LOG_FUNC_EXIT__;
105                 return TRUE;
106         }
107
108         STC_DBUS_REPLY_ERROR_NONE(invocation);
109         __STC_LOG_FUNC_EXIT__;
110         return TRUE;
111 }
112
113 gboolean handle_firewall_remove_chain(StcFirewall *object,
114                                         GDBusMethodInvocation *invocation,
115                                         gchar *chain,
116                                         void *user_data)
117 {
118         __STC_LOG_FUNC_ENTER__;
119         int ret = STC_ERROR_NONE;
120
121         if (chain == NULL) {
122                 STC_FIREWALL_DBUS_REPLY_ERROR(invocation,
123                                                 STC_ERROR_INVALID_PARAMETER);
124                 __STC_LOG_FUNC_EXIT__;
125                 return TRUE;
126         }
127
128         ret = stc_plugin_firewall_remove_chain(chain);
129         if (ret != STC_ERROR_NONE) {
130                 STC_FIREWALL_DBUS_REPLY_ERROR(invocation, ret);
131                 __STC_LOG_FUNC_EXIT__;
132                 return TRUE;
133         }
134
135         STC_DBUS_REPLY_ERROR_NONE(invocation);
136         __STC_LOG_FUNC_EXIT__;
137         return TRUE;
138 }
139
140 gboolean handle_firewall_flush_chain(StcFirewall *object,
141                                         GDBusMethodInvocation *invocation,
142                                         gchar *chain,
143                                         void *user_data)
144 {
145         __STC_LOG_FUNC_ENTER__;
146         int ret = STC_ERROR_NONE;
147
148         if (chain == NULL) {
149                 STC_FIREWALL_DBUS_REPLY_ERROR(invocation,
150                                                 STC_ERROR_INVALID_PARAMETER);
151                 __STC_LOG_FUNC_EXIT__;
152                 return TRUE;
153         }
154
155         ret = stc_plugin_firewall_flush_chain(chain);
156         if (ret != STC_ERROR_NONE) {
157                 STC_FIREWALL_DBUS_REPLY_ERROR(invocation, ret);
158                 __STC_LOG_FUNC_EXIT__;
159                 return TRUE;
160         }
161
162         STC_DBUS_REPLY_ERROR_NONE(invocation);
163         __STC_LOG_FUNC_EXIT__;
164         return TRUE;
165 }
166
167 gboolean handle_firewall_get_all_chain(StcFirewall *object,
168                                         GDBusMethodInvocation *invocation,
169                                         void *user_data)
170 {
171         __STC_LOG_FUNC_ENTER__;
172         GVariantBuilder *builder = NULL;
173         GVariant *return_parameters = NULL;
174
175         builder = g_variant_builder_new(G_VARIANT_TYPE("aa{sv}"));
176
177         stc_plugin_firewall_get_all_chain(builder);
178
179         return_parameters = g_variant_new("(aa{sv})", builder);
180         g_variant_builder_unref(builder);
181
182         DEBUG_GDBUS_VARIANT("Return parameters: ", return_parameters);
183         STC_DBUS_REPLY(invocation, return_parameters);
184         __STC_LOG_FUNC_EXIT__;
185         return TRUE;
186 }
187
188 gboolean handle_firewall_set_chain(StcFirewall *object,
189                                         GDBusMethodInvocation *invocation,
190                                         gchar *chain,
191                                         unsigned int target,
192                                         void *user_data)
193 {
194         __STC_LOG_FUNC_ENTER__;
195         int ret = STC_ERROR_NONE;
196
197         if (chain == NULL ||
198                 target >= STC_FW_CHAIN_TARGET_MAX) {
199                 STC_FIREWALL_DBUS_REPLY_ERROR(invocation,
200                                                 STC_ERROR_INVALID_PARAMETER);
201                 __STC_LOG_FUNC_EXIT__;
202                 return TRUE;
203         }
204
205         ret = stc_plugin_firewall_set_chain(chain, target);
206         if (ret != STC_ERROR_NONE) {
207                 STC_FIREWALL_DBUS_REPLY_ERROR(invocation, ret);
208                 __STC_LOG_FUNC_EXIT__;
209                 return TRUE;
210         }
211
212         STC_DBUS_REPLY_ERROR_NONE(invocation);
213         __STC_LOG_FUNC_EXIT__;
214         return TRUE;
215 }
216
217 gboolean handle_firewall_unset_chain(StcFirewall *object,
218                                         GDBusMethodInvocation *invocation,
219                                         gchar *chain,
220                                         void *user_data)
221 {
222         __STC_LOG_FUNC_ENTER__;
223         int ret = STC_ERROR_NONE;
224
225         if (chain == NULL) {
226                 STC_FIREWALL_DBUS_REPLY_ERROR(invocation,
227                                                 STC_ERROR_INVALID_PARAMETER);
228                 __STC_LOG_FUNC_EXIT__;
229                 return TRUE;
230         }
231
232         ret = stc_plugin_firewall_unset_chain(chain);
233         if (ret != STC_ERROR_NONE) {
234                 STC_FIREWALL_DBUS_REPLY_ERROR(invocation, ret);
235                 __STC_LOG_FUNC_EXIT__;
236                 return TRUE;
237         }
238
239         STC_DBUS_REPLY_ERROR_NONE(invocation);
240         __STC_LOG_FUNC_EXIT__;
241         return TRUE;
242 }
243
244 gboolean handle_firewall_add_rule(StcFirewall *object,
245                                    GDBusMethodInvocation *invocation,
246                                    GVariant *parameters,
247                                    void *user_data)
248 {
249         __STC_LOG_FUNC_ENTER__;
250         int ret = STC_ERROR_NONE;
251
252         ret = stc_plugin_firewall_add_rule(parameters);
253         if (ret != STC_ERROR_NONE) {
254                 STC_FIREWALL_DBUS_REPLY_ERROR(invocation, ret);
255                 __STC_LOG_FUNC_EXIT__;
256                 return TRUE;
257         }
258
259         STC_DBUS_REPLY_ERROR_NONE(invocation);
260         __STC_LOG_FUNC_EXIT__;
261         return TRUE;
262 }
263
264 gboolean handle_firewall_remove_rule(StcFirewall *object,
265                                    GDBusMethodInvocation *invocation,
266                                    GVariant *parameters,
267                                    void *user_data)
268 {
269         __STC_LOG_FUNC_ENTER__;
270         int ret = STC_ERROR_NONE;
271
272         ret = stc_plugin_firewall_remove_rule(parameters);
273         if (ret != STC_ERROR_NONE) {
274                 STC_FIREWALL_DBUS_REPLY_ERROR(invocation, ret);
275                 __STC_LOG_FUNC_EXIT__;
276                 return TRUE;
277         }
278
279         STC_DBUS_REPLY_ERROR_NONE(invocation);
280         __STC_LOG_FUNC_EXIT__;
281         return TRUE;
282 }
283
284 gboolean handle_firewall_update_rule(StcFirewall *object,
285                                    GDBusMethodInvocation *invocation,
286                                    GVariant *parameters,
287                                    void *user_data)
288 {
289         __STC_LOG_FUNC_ENTER__;
290         int ret = STC_ERROR_NONE;
291
292         ret = stc_plugin_firewall_update_rule(parameters);
293         if (ret != STC_ERROR_NONE) {
294                 STC_FIREWALL_DBUS_REPLY_ERROR(invocation, ret);
295                 __STC_LOG_FUNC_EXIT__;
296                 return TRUE;
297         }
298
299         STC_DBUS_REPLY_ERROR_NONE(invocation);
300         __STC_LOG_FUNC_EXIT__;
301         return TRUE;
302 }
303
304 gboolean handle_firewall_get_all_rule(StcFirewall *object,
305                                    GDBusMethodInvocation *invocation,
306                                    void *user_data)
307 {
308         __STC_LOG_FUNC_ENTER__;
309         GVariantBuilder *builder = NULL;
310         GVariant *return_parameters = NULL;
311
312         builder = g_variant_builder_new(G_VARIANT_TYPE("aa{sv}"));
313
314         stc_plugin_firewall_get_all_rule(builder);
315
316         return_parameters = g_variant_new("(aa{sv})", builder);
317         g_variant_builder_unref(builder);
318
319         DEBUG_GDBUS_VARIANT("Return parameters: ", return_parameters);
320         STC_DBUS_REPLY(invocation, return_parameters);
321         __STC_LOG_FUNC_EXIT__;
322         return TRUE;
323 }