Return errors to caller
[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_set_keep_alive(TRUE);
51
52         stc_plugin_firewall_lock();
53
54         STC_DBUS_REPLY_ERROR_NONE(invocation);
55         __STC_LOG_FUNC_EXIT__;
56         return TRUE;
57 }
58
59 gboolean handle_firewall_unlock(StcFirewall *object,
60                                         GDBusMethodInvocation *invocation,
61                                         void *user_data)
62 {
63         __STC_LOG_FUNC_ENTER__;
64
65         stc_set_keep_alive(TRUE);
66
67         stc_plugin_firewall_unlock();
68
69         STC_DBUS_REPLY_ERROR_NONE(invocation);
70         __STC_LOG_FUNC_EXIT__;
71         return TRUE;
72 }
73
74 gboolean handle_firewall_get_lock(StcFirewall *object,
75                                         GDBusMethodInvocation *invocation,
76                                         void *user_data)
77 {
78         __STC_LOG_FUNC_ENTER__;
79         GVariant *return_parameters = NULL;
80         int state = 0;
81
82         stc_set_keep_alive(TRUE);
83
84         stc_plugin_firewall_get_lock(&state);
85
86         return_parameters = g_variant_new("(i)", state);
87         STC_DBUS_REPLY(invocation, return_parameters);
88         __STC_LOG_FUNC_EXIT__;
89         return TRUE;
90 }
91
92 gboolean handle_firewall_add_chain(StcFirewall *object,
93                                         GDBusMethodInvocation *invocation,
94                                         gchar *chain,
95                                         void *user_data)
96 {
97         __STC_LOG_FUNC_ENTER__;
98         int ret = STC_ERROR_NONE;
99
100         stc_set_keep_alive(TRUE);
101
102         if (chain == NULL) {
103                 STC_FIREWALL_DBUS_REPLY_ERROR(invocation,
104                                                 STC_ERROR_INVALID_PARAMETER);
105                 __STC_LOG_FUNC_EXIT__;
106                 return TRUE;
107         }
108
109         ret = stc_plugin_firewall_add_chain(chain);
110         if (ret != STC_ERROR_NONE) {
111                 STC_FIREWALL_DBUS_REPLY_ERROR(invocation, ret);
112                 __STC_LOG_FUNC_EXIT__;
113                 return TRUE;
114         }
115
116         STC_DBUS_REPLY_ERROR_NONE(invocation);
117         __STC_LOG_FUNC_EXIT__;
118         return TRUE;
119 }
120
121 gboolean handle_firewall_remove_chain(StcFirewall *object,
122                                         GDBusMethodInvocation *invocation,
123                                         gchar *chain,
124                                         void *user_data)
125 {
126         __STC_LOG_FUNC_ENTER__;
127         int ret = STC_ERROR_NONE;
128
129         stc_set_keep_alive(TRUE);
130
131         if (chain == NULL) {
132                 STC_FIREWALL_DBUS_REPLY_ERROR(invocation,
133                                                 STC_ERROR_INVALID_PARAMETER);
134                 __STC_LOG_FUNC_EXIT__;
135                 return TRUE;
136         }
137
138         ret = stc_plugin_firewall_remove_chain(chain);
139         if (ret != STC_ERROR_NONE) {
140                 STC_FIREWALL_DBUS_REPLY_ERROR(invocation, ret);
141                 __STC_LOG_FUNC_EXIT__;
142                 return TRUE;
143         }
144
145         STC_DBUS_REPLY_ERROR_NONE(invocation);
146         __STC_LOG_FUNC_EXIT__;
147         return TRUE;
148 }
149
150 gboolean handle_firewall_flush_chain(StcFirewall *object,
151                                         GDBusMethodInvocation *invocation,
152                                         gchar *chain,
153                                         void *user_data)
154 {
155         __STC_LOG_FUNC_ENTER__;
156         int ret = STC_ERROR_NONE;
157
158         stc_set_keep_alive(TRUE);
159
160         if (chain == NULL) {
161                 STC_FIREWALL_DBUS_REPLY_ERROR(invocation,
162                                                 STC_ERROR_INVALID_PARAMETER);
163                 __STC_LOG_FUNC_EXIT__;
164                 return TRUE;
165         }
166
167         ret = stc_plugin_firewall_flush_chain(chain);
168         if (ret != STC_ERROR_NONE) {
169                 STC_FIREWALL_DBUS_REPLY_ERROR(invocation, ret);
170                 __STC_LOG_FUNC_EXIT__;
171                 return TRUE;
172         }
173
174         STC_DBUS_REPLY_ERROR_NONE(invocation);
175         __STC_LOG_FUNC_EXIT__;
176         return TRUE;
177 }
178
179 gboolean handle_firewall_get_all_chain(StcFirewall *object,
180                                         GDBusMethodInvocation *invocation,
181                                         void *user_data)
182 {
183         __STC_LOG_FUNC_ENTER__;
184         GVariantBuilder *builder = NULL;
185         GVariant *return_parameters = NULL;
186
187         stc_set_keep_alive(TRUE);
188
189         builder = g_variant_builder_new(G_VARIANT_TYPE("aa{sv}"));
190
191         stc_plugin_firewall_get_all_chain(builder);
192
193         return_parameters = g_variant_new("(aa{sv})", builder);
194         g_variant_builder_unref(builder);
195
196         DEBUG_GDBUS_VARIANT("Return parameters: ", return_parameters);
197         STC_DBUS_REPLY(invocation, return_parameters);
198         __STC_LOG_FUNC_EXIT__;
199         return TRUE;
200 }
201
202 gboolean handle_firewall_set_chain(StcFirewall *object,
203                                         GDBusMethodInvocation *invocation,
204                                         gchar *chain,
205                                         unsigned int target,
206                                         void *user_data)
207 {
208         __STC_LOG_FUNC_ENTER__;
209         int ret = STC_ERROR_NONE;
210
211         stc_set_keep_alive(TRUE);
212
213         if (chain == NULL ||
214                 target >= STC_FW_CHAIN_TARGET_MAX) {
215                 STC_FIREWALL_DBUS_REPLY_ERROR(invocation,
216                                                 STC_ERROR_INVALID_PARAMETER);
217                 __STC_LOG_FUNC_EXIT__;
218                 return TRUE;
219         }
220
221         ret = stc_plugin_firewall_set_chain(chain, target);
222         if (ret != STC_ERROR_NONE) {
223                 STC_FIREWALL_DBUS_REPLY_ERROR(invocation, ret);
224                 __STC_LOG_FUNC_EXIT__;
225                 return TRUE;
226         }
227
228         STC_DBUS_REPLY_ERROR_NONE(invocation);
229         __STC_LOG_FUNC_EXIT__;
230         return TRUE;
231 }
232
233 gboolean handle_firewall_unset_chain(StcFirewall *object,
234                                         GDBusMethodInvocation *invocation,
235                                         gchar *chain,
236                                         void *user_data)
237 {
238         __STC_LOG_FUNC_ENTER__;
239         int ret = STC_ERROR_NONE;
240
241         stc_set_keep_alive(TRUE);
242
243         if (chain == NULL) {
244                 STC_FIREWALL_DBUS_REPLY_ERROR(invocation,
245                                                 STC_ERROR_INVALID_PARAMETER);
246                 __STC_LOG_FUNC_EXIT__;
247                 return TRUE;
248         }
249
250         ret = stc_plugin_firewall_unset_chain(chain);
251         if (ret != STC_ERROR_NONE) {
252                 STC_FIREWALL_DBUS_REPLY_ERROR(invocation, ret);
253                 __STC_LOG_FUNC_EXIT__;
254                 return TRUE;
255         }
256
257         STC_DBUS_REPLY_ERROR_NONE(invocation);
258         __STC_LOG_FUNC_EXIT__;
259         return TRUE;
260 }
261
262 gboolean handle_firewall_add_rule(StcFirewall *object,
263                                    GDBusMethodInvocation *invocation,
264                                    GVariant *parameters,
265                                    void *user_data)
266 {
267         __STC_LOG_FUNC_ENTER__;
268         int ret = STC_ERROR_NONE;
269
270         stc_set_keep_alive(TRUE);
271
272         ret = stc_plugin_firewall_add_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_remove_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         stc_set_keep_alive(TRUE);
293
294         ret = stc_plugin_firewall_remove_rule(parameters);
295         if (ret != STC_ERROR_NONE) {
296                 STC_FIREWALL_DBUS_REPLY_ERROR(invocation, ret);
297                 __STC_LOG_FUNC_EXIT__;
298                 return TRUE;
299         }
300
301         STC_DBUS_REPLY_ERROR_NONE(invocation);
302         __STC_LOG_FUNC_EXIT__;
303         return TRUE;
304 }
305
306 gboolean handle_firewall_update_rule(StcFirewall *object,
307                                    GDBusMethodInvocation *invocation,
308                                    GVariant *parameters,
309                                    void *user_data)
310 {
311         __STC_LOG_FUNC_ENTER__;
312         int ret = STC_ERROR_NONE;
313
314         stc_set_keep_alive(TRUE);
315
316         ret = stc_plugin_firewall_update_rule(parameters);
317         if (ret != STC_ERROR_NONE) {
318                 STC_FIREWALL_DBUS_REPLY_ERROR(invocation, ret);
319                 __STC_LOG_FUNC_EXIT__;
320                 return TRUE;
321         }
322
323         STC_DBUS_REPLY_ERROR_NONE(invocation);
324         __STC_LOG_FUNC_EXIT__;
325         return TRUE;
326 }
327
328 gboolean handle_firewall_get_all_rule(StcFirewall *object,
329                                    GDBusMethodInvocation *invocation,
330                                    void *user_data)
331 {
332         __STC_LOG_FUNC_ENTER__;
333         GVariantBuilder *builder = NULL;
334         GVariant *return_parameters = NULL;
335
336         stc_set_keep_alive(TRUE);
337
338         builder = g_variant_builder_new(G_VARIANT_TYPE("aa{sv}"));
339
340         stc_plugin_firewall_get_all_rule(builder);
341
342         return_parameters = g_variant_new("(aa{sv})", builder);
343         g_variant_builder_unref(builder);
344
345         DEBUG_GDBUS_VARIANT("Return parameters: ", return_parameters);
346         STC_DBUS_REPLY(invocation, return_parameters);
347         __STC_LOG_FUNC_EXIT__;
348         return TRUE;
349 }