Check insert option when iptables commit
[platform/core/connectivity/stc-manager.git] / src / stc-restriction.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-restrictions.h"
19 #include "stc-restriction.h"
20 #include "stc-manager-gdbus.h"
21 #include "stc-monitor.h"
22
23 #define RESTRICTION_DBUS_ERROR_NAME "net.stc.restriction.Error.Failed"
24
25 #define STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, err_num) \
26         g_dbus_method_invocation_return_dbus_error((invocation), \
27                                                    RESTRICTION_DBUS_ERROR_NAME, \
28                                                    stc_err_strs[-(err_num)])
29
30 static const gchar *stc_err_strs[] = {
31         "ERROR_NONE",
32         "FAIL",
33         "DB_FAILED",
34         "OUT_OF_MEMORY",
35         "INVALID_PARAMETER",
36         "NO_DATA",
37         "ALREADY_DATA",
38         "UNINITIALIZED",
39         "PERMISSION_DENIED",
40         "NOTIMPL"
41 };
42
43 void __initialize_rstn_rule(table_restrictions_info *rule)
44 {
45         rule->app_id = NULL;
46
47         rule->iftype = STC_IFACE_ALL;
48         rule->ifname = NULL;
49
50         rule->subscriber_id = NULL;
51         rule->roaming = STC_ROAMING_DISABLE;
52
53         rule->rstn_type = STC_RSTN_TYPE_UNKNOWN;
54         rule->data_limit = -1;
55         rule->data_warn_limit = -1;
56
57         rule->month_start_date = 1;
58         rule->monthly_limit = -1;
59         rule->weekly_limit = -1;
60         rule->daily_limit = -1;
61
62         rule->mac = NULL;
63 }
64
65 gboolean __validate_rstn_rule(table_restrictions_info *rule)
66 {
67         __STC_LOG_FUNC_ENTER__;
68
69         if (rule == NULL) {
70                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
71                 return FALSE; //LCOV_EXCL_LINE
72         }
73
74         if (rule->iftype <= STC_IFACE_UNKNOWN ||
75             rule->iftype >= STC_IFACE_LAST_ELEM) {
76                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
77                 return FALSE; //LCOV_EXCL_LINE
78         }
79
80         if (rule->roaming > STC_ROAMING_ENABLE) {
81                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
82                 return FALSE; //LCOV_EXCL_LINE
83         }
84
85         if (rule->subscriber_id == NULL) {
86                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
87                 return FALSE; //LCOV_EXCL_LINE
88         }
89
90         if (rule->app_id == NULL) {
91                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
92                 return FALSE; //LCOV_EXCL_LINE
93         }
94
95         __STC_LOG_FUNC_EXIT__;
96         return TRUE;
97 }
98
99 void __stc_restriction_app_info_builder_add(GVariantBuilder *builder,
100                                             const table_restrictions_info *info)
101 {
102         __STC_LOG_FUNC_ENTER__;
103
104         if (!builder || !info) {
105                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
106                 return; //LCOV_EXCL_LINE
107         }
108
109         g_variant_builder_add(builder, "{sv}", "app_id",
110                               g_variant_new_string(info->app_id));
111
112         g_variant_builder_add(builder, "{sv}", "ifname",
113                               g_variant_new_string(info->ifname));
114
115         g_variant_builder_add(builder, "{sv}", "iftype",
116                               g_variant_new_uint16(info->iftype));
117
118         g_variant_builder_add(builder, "{sv}", "rstn_type",
119                               g_variant_new_uint16(info->rstn_type));
120
121         g_variant_builder_add(builder, "{sv}", "data_limit",
122                               g_variant_new_int64(info->data_limit));
123
124         g_variant_builder_add(builder, "{sv}", "data_warn_limit",
125                               g_variant_new_int64(info->data_warn_limit));
126
127         g_variant_builder_add(builder, "{sv}", "month_start_date",
128                               g_variant_new_int32(info->month_start_date));
129
130         g_variant_builder_add(builder, "{sv}", "monthly_limit",
131                               g_variant_new_int64(info->monthly_limit));
132
133         g_variant_builder_add(builder, "{sv}", "weekly_limit",
134                               g_variant_new_int64(info->weekly_limit));
135
136         g_variant_builder_add(builder, "{sv}", "daily_limit",
137                               g_variant_new_int64(info->daily_limit));
138
139         g_variant_builder_add(builder, "{sv}", "roaming",
140                               g_variant_new_uint16(info->roaming));
141
142         g_variant_builder_add(builder, "{sv}", "subscriber_id",
143                               g_variant_new_string(info->subscriber_id));
144
145         __STC_LOG_FUNC_EXIT__;
146 }
147
148 stc_cb_ret_e __table_restrictions_foreach_app_cb(const table_restrictions_info *info,
149                                                  void *user_data)
150 {
151         __STC_LOG_FUNC_ENTER__;
152         GVariantBuilder *builder = (GVariantBuilder *)user_data;
153         GVariantBuilder sub_builder;
154
155         if (!info || !builder) {
156                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
157                 return STC_CANCEL; //LCOV_EXCL_LINE
158         }
159
160         g_variant_builder_init(&sub_builder, G_VARIANT_TYPE("a{sv}"));
161         __stc_restriction_app_info_builder_add(&sub_builder, info);
162
163         g_variant_builder_add_value(builder,
164                                     g_variant_builder_end(&sub_builder));
165
166         __STC_LOG_FUNC_EXIT__;
167         return STC_CONTINUE;
168 }
169
170 stc_cb_ret_e __table_restrictions_per_app_cb(const table_restrictions_info *info,
171                                              void *user_data)
172 {
173         __STC_LOG_FUNC_ENTER__;
174         GVariantBuilder *builder = (GVariantBuilder *)user_data;
175
176         if (!info || !builder) {
177                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
178                 return STC_CANCEL; //LCOV_EXCL_LINE
179         }
180
181         __stc_restriction_app_info_builder_add(builder, info);
182
183         __STC_LOG_FUNC_EXIT__;
184         return STC_CONTINUE;
185 }
186
187 static void __stc_extract_restriction_rule(const char *key, GVariant *value,
188                                            void *user_data)
189 {
190         table_restrictions_info *rule =
191                 (table_restrictions_info *) user_data;
192         if (rule == NULL) {
193                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
194                 return; //LCOV_EXCL_LINE
195         }
196
197         if (!g_strcmp0(key, "app_id")) {
198                 guint str_length;
199                 const gchar *str = g_variant_get_string(value, &str_length);
200                 rule->app_id = g_strdup(str);
201                 STC_LOGD("app_id: [%s]", rule->app_id);
202
203         } else if (!g_strcmp0(key, "ifname")) {
204                 guint str_length;
205                 const gchar *str = g_variant_get_string(value, &str_length);
206                 rule->ifname = g_strdup(str);
207                 STC_LOGD("ifname: [%s]", rule->ifname);
208
209         } else if (!g_strcmp0(key, "iftype")) {
210                 rule->iftype = g_variant_get_uint16(value);
211                 STC_LOGD("iftype: [%u]", (unsigned int) rule->iftype);
212
213         } else if (!g_strcmp0(key, "data_limit")) {
214                 rule->data_limit = g_variant_get_int64(value);
215                 STC_LOGD("data_limit: [%lld]", rule->data_limit);
216
217         } else if (!g_strcmp0(key, "data_warn_limit")) {
218                 rule->data_warn_limit = g_variant_get_int64(value);
219                 STC_LOGD("data_warn_limit: [%lld]", rule->data_warn_limit);
220
221         } else if (!g_strcmp0(key, "month_start_date")) {
222                 rule->month_start_date = g_variant_get_int32(value);
223                 STC_LOGD("month_start_date: [%d]", rule->month_start_date);
224
225         } else if (!g_strcmp0(key, "monthly_limit")) {
226                 rule->monthly_limit = g_variant_get_int64(value);
227                 STC_LOGD("monthly_limit: [%lld]", rule->monthly_limit);
228
229         } else if (!g_strcmp0(key, "weekly_limit")) {
230                 rule->weekly_limit = g_variant_get_int64(value);
231                 STC_LOGD("weekly_limit: [%lld]", rule->weekly_limit);
232
233         } else if (!g_strcmp0(key, "daily_limit")) {
234                 rule->daily_limit = g_variant_get_int64(value);
235                 STC_LOGD("daily_limit: [%lld]", rule->daily_limit);
236
237         } else if (!g_strcmp0(key, "roaming")) {
238                 rule->roaming = g_variant_get_uint16(value);
239                 STC_LOGD("roaming: [%u]", rule->roaming);
240
241         } else if (!g_strcmp0(key, "subscriber_id")) {
242                 guint str_length;
243                 const gchar *str = g_variant_get_string(value, &str_length);
244                 rule->subscriber_id = g_strdup(str);
245                 STC_LOGD("subscriber_id: [%s]", rule->subscriber_id);
246
247         } else if (!g_strcmp0(key, "rstn_type")) {
248                 rule->rstn_type = g_variant_get_uint16(value);
249                 STC_LOGD("type: [%u]", (unsigned int) rule->rstn_type);
250
251         } else if (!g_strcmp0(key, "mac")) {
252                 guint str_length;
253                 const gchar *str = g_variant_get_string(value, &str_length);
254                 rule->mac = g_strdup(str);
255                 STC_LOGD("mac: [%s]", rule->mac);
256
257         } else {
258                 STC_LOGD("Unknown select rule"); //LCOV_EXCL_LINE
259         }
260 }
261
262 gboolean handle_restriction_set(StcRestriction *object,
263                                 GDBusMethodInvocation *invocation,
264                                 GVariant *parameters,
265                                 void *user_data)
266 {
267         __STC_LOG_FUNC_ENTER__;
268         GVariantIter *iter = NULL;
269         table_restrictions_info rule;
270         stc_error_e ret = STC_ERROR_NONE;
271
272         memset(&rule, 0, sizeof(table_restrictions_info));
273         __initialize_rstn_rule(&rule);
274
275         g_variant_get(parameters, "a{sv}", &iter);
276         if (iter != NULL) {
277                 stc_manager_gdbus_dict_foreach(iter,
278                                                __stc_extract_restriction_rule,
279                                                &rule);
280                 g_variant_iter_free(iter);
281         }
282
283         if (__validate_rstn_rule(&rule) == FALSE) {
284                 STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, //LCOV_EXCL_LINE
285                                                  STC_ERROR_INVALID_PARAMETER);
286                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
287                 return TRUE;
288         }
289
290         ret = table_restrictions_update(&rule);
291         if (ret != STC_ERROR_NONE) {
292                 STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, //LCOV_EXCL_LINE
293                                                  STC_ERROR_DB_FAILED);
294                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
295                 return TRUE;
296         }
297
298         stc_monitor_rstn_add(&rule);
299         STC_DBUS_REPLY_ERROR_NONE(invocation);
300         __STC_LOG_FUNC_EXIT__;
301         return TRUE;
302 }
303
304 gboolean handle_restriction_unset(StcRestriction *object,
305                                    GDBusMethodInvocation *invocation,
306                                    GVariant *parameters,
307                                    void *user_data)
308 {
309         __STC_LOG_FUNC_ENTER__;
310         GVariantIter *iter = NULL;
311         table_restrictions_info rule;
312         stc_error_e ret = STC_ERROR_NONE;
313
314         memset(&rule, 0, sizeof(table_restrictions_info));
315         __initialize_rstn_rule(&rule);
316
317         g_variant_get(parameters, "a{sv}", &iter);
318         if (iter != NULL) {
319                 stc_manager_gdbus_dict_foreach(iter,
320                                                __stc_extract_restriction_rule,
321                                                &rule);
322                 g_variant_iter_free(iter);
323         }
324
325         if (__validate_rstn_rule(&rule) == FALSE) {
326                 STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, //LCOV_EXCL_LINE
327                                                  STC_ERROR_INVALID_PARAMETER);
328                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
329                 return TRUE;
330         }
331
332         ret = table_restrictions_delete(rule.app_id, rule.iftype, rule.ifname,
333                                         rule.subscriber_id, rule.roaming);
334         if (ret != STC_ERROR_NONE) {
335                 STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, //LCOV_EXCL_LINE
336                                                  STC_ERROR_DB_FAILED);
337                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
338                 return TRUE;
339         }
340
341         stc_monitor_rstn_remove(&rule);
342         STC_DBUS_REPLY_ERROR_NONE(invocation);
343         __STC_LOG_FUNC_EXIT__;
344         return TRUE;
345 }
346
347 gboolean handle_restriction_get(StcRestriction *object,
348                                 GDBusMethodInvocation *invocation,
349                                 const gchar *app_id,
350                                 void *user_data)
351 {
352         __STC_LOG_FUNC_ENTER__;
353         GVariantBuilder *builder = NULL;
354         GVariant *return_parameters = NULL;
355         stc_error_e ret;
356
357         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
358
359         ret = table_restrictions_per_app(app_id,
360                                          __table_restrictions_per_app_cb,
361                                          builder);
362         if (ret < STC_ERROR_NONE) {
363                 g_variant_builder_unref(builder); //LCOV_EXCL_LINE
364                 STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE
365                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
366                 return TRUE; //LCOV_EXCL_LINE
367         }
368
369         return_parameters = g_variant_new("(ia{sv})", STC_ERROR_NONE, builder);
370         g_variant_builder_unref(builder);
371
372         DEBUG_GDBUS_VARIANT("Return parameters: ", return_parameters);
373         STC_DBUS_REPLY(invocation, return_parameters);
374         __STC_LOG_FUNC_EXIT__;
375         return TRUE;
376 }
377
378 gboolean handle_restriction_get_all(StcRestriction *object,
379                                     GDBusMethodInvocation *invocation,
380                                     void *user_data)
381 {
382         __STC_LOG_FUNC_ENTER__;
383         GVariantBuilder *builder = NULL;
384         GVariant *return_parameters = NULL;
385         stc_error_e ret;
386
387         builder = g_variant_builder_new(G_VARIANT_TYPE("aa{sv}"));
388
389         ret = table_restrictions_foreach(__table_restrictions_foreach_app_cb,
390                                          builder);
391         if (ret < STC_ERROR_NONE) {
392                 g_variant_builder_unref(builder); //LCOV_EXCL_LINE
393                 STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE
394                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
395                 return TRUE; //LCOV_EXCL_LINE
396         }
397
398         return_parameters = g_variant_new("(iaa{sv})", STC_ERROR_NONE, builder);
399         g_variant_builder_unref(builder);
400
401         DEBUG_GDBUS_VARIANT("Return parameters: ", return_parameters);
402         STC_DBUS_REPLY(invocation, return_parameters);
403         __STC_LOG_FUNC_EXIT__;
404         return TRUE;
405 }
406
407 gboolean handle_restriction_get_type(StcRestriction *object,
408                                       GDBusMethodInvocation *invocation,
409                                       const gchar *app_id,
410                                       int iftype,
411                                       void *user_data)
412 {
413         __STC_LOG_FUNC_ENTER__;
414         GVariant *return_parameters = NULL;
415         stc_rstn_type_e type = STC_RSTN_TYPE_UNKNOWN;
416         stc_error_e ret;
417
418         ret = table_restrictions_get_restriction_type(app_id, iftype, &type);
419         if (ret < STC_ERROR_NONE) {
420                 STC_RESTRICTION_DBUS_REPLY_ERROR(invocation, ret); //LCOV_EXCL_LINE
421                 __STC_LOG_FUNC_EXIT__; //LCOV_EXCL_LINE
422                 return TRUE;
423         }
424
425         return_parameters = g_variant_new("(ii)", STC_ERROR_NONE, type);
426         STC_DBUS_REPLY(invocation, return_parameters);
427         __STC_LOG_FUNC_EXIT__;
428         return TRUE;
429 }