Fix some svace issues for unchecking return value
[platform/core/connectivity/stc-manager.git] / src / database / tables / table-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 /**
18  * This file implements restrictions entity handler methods.
19  *
20  * @file        table-restrictions.c
21  */
22
23 #include "stc-db.h"
24 #include "db-internal.h"
25 #include "table-firewall.h"
26
27 #define BUF_SIZE_FOR_IP   64
28
29 /* DELETE statements */
30 #define DELETE_FIREWALL_CHAIN "DELETE FROM fw_chains " \
31         "WHERE chain = ?"
32
33 #define DELETE_FIREWALL_RULE "DELETE FROM fw_rules " \
34         "WHERE key = ?"
35
36 #define DELETE_FIREWALL_RULE_PER_CHAIN "DELETE FROM fw_rules " \
37         "WHERE chain = ?"
38
39 /* SELECT statements */
40 #define SELECT_FIREWALL_LOCK "SELECT state FROM fw_lock " \
41         "WHERE name = ?"
42
43 #define SELECT_FIREWALL_CHAIN "SELECT chain, " \
44         "target, priority FROM fw_chains"
45
46 #define SELECT_FIREWALL_RULE "SELECT key, " \
47         "chain, direction, s_ip_type, d_ip_type, s_port_type, " \
48         "d_port_type, protocol, family, s_ip1, s_ip2, d_ip1, d_ip2, " \
49         "s_port1, s_port2, d_port1, d_port2, ifname, target, " \
50         "log_level, log_prefix, " \
51         "nflog_group, nflog_prefix, nflog_range, nflog_threshold, " \
52         "identifier " \
53         "FROM fw_rules"
54
55 #define SELECT_FIREWALL_RULE_PER_CHAIN "SELECT key, " \
56         "chain, direction, s_ip_type, d_ip_type, s_port_type, " \
57         "d_port_type, protocol, family, s_ip1, s_ip2, d_ip1, d_ip2, " \
58         "s_port1, s_port2, d_port1, d_port2, ifname, target, " \
59         "log_level, log_prefix, " \
60         "nflog_group, nflog_prefix, nflog_range, nflog_threshold, " \
61         "identifier " \
62         "FROM fw_rules INDEXED BY rules_index " \
63         "WHERE chain = ?"
64
65 /* UPDATE statement */
66 #define UPDATE_FIREWALL_LOCK "UPDATE fw_lock " \
67         "SET state = ? WHERE name = ?"
68
69 #define UPDATE_FIREWALL_CHAIN "UPDATE fw_chains " \
70         "SET target = ?, priority = ? " \
71         "WHERE chain = ?"
72
73 #define UPDATE_FIREWALL_RULE "UPDATE fw_rules " \
74         "SET chain = ?, direction = ?, s_ip_type = ?, d_ip_type = ?, " \
75         "s_port_type = ?, d_port_type = ?, protocol = ?, family = ?, " \
76         "s_ip1 = ?, s_ip2 = ?, d_ip1 = ?, d_ip2 = ?, s_port1 = ?, " \
77         "s_port2 = ?, d_port1 = ?, d_port2 = ?, ifname = ?, target = ?, " \
78         "log_level = ?, log_prefix = ?, " \
79         "nflog_group = ?, nflog_prefix = ?, nflog_range = ?, nflog_threshold = ?, " \
80         "identifier = ?, key = ? " \
81         "WHERE key = ?"
82
83 /* INSERT statement */
84 #define INSERT_FIREWALL_LOCK "INSERT INTO fw_lock " \
85         "(name, state) VALUES (?, ?)"
86
87 #define INSERT_FIREWALL_CHAIN "INSERT INTO fw_chains " \
88         "(chain, target, priority) " \
89         "VALUES (?, ?, ?)"
90
91 #define INSERT_FIREWALL_RULE "INSERT INTO fw_rules " \
92         "(key, chain, direction, s_ip_type, d_ip_type, s_port_type, " \
93         "d_port_type, protocol, family, s_ip1, s_ip2, d_ip1, d_ip2, " \
94         "s_port1, s_port2, d_port1, d_port2, ifname, target, " \
95         "log_level, log_prefix, " \
96         "nflog_group, nflog_prefix, nflog_range, nflog_threshold, " \
97         "identifier) " \
98         "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " \
99         "?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
100
101 static void __finalize_delete(void);
102
103 #define PREPARE_DELETE(stm, query) do { \
104         rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
105         if (rc != SQLITE_OK) { \
106                 stm = NULL; \
107                 __finalize_delete(); \
108                 STC_LOGE("Failed to prepare \"%s\" query" \
109                          , query); \
110                 return rc; \
111         } \
112 } while (0)
113
114 static void __finalize_select(void);
115
116 #define PREPARE_SELECT(stm, query) do { \
117         rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
118         if (rc != SQLITE_OK) { \
119                 stm = NULL; \
120                 __finalize_select(); \
121                 STC_LOGE("Failed to prepare \"%s\" query" \
122                          , query); \
123                 return rc; \
124         } \
125 } while (0)
126
127 static void __finalize_update(void);
128
129 #define PREPARE_UPDATE(stm, query) do { \
130         rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
131         if (rc != SQLITE_OK) { \
132                 stm = NULL; \
133                 __finalize_update(); \
134                 STC_LOGE("Failed to prepare \"%s\" query" \
135                          , query); \
136                 return rc; \
137         } \
138 } while (0)
139
140 static void __finalize_insert(void);
141
142 #define PREPARE_INSERT(stm, query) do { \
143         rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
144         if (rc != SQLITE_OK) { \
145                 stm = NULL; \
146                 __finalize_insert(); \
147                 STC_LOGE("Failed to prepare \"%s\" query" \
148                          , query); \
149                 return rc; \
150         } \
151 } while (0)
152
153 #define FINALIZE(stm) do { \
154         if (stm) { \
155                 sqlite3_finalize(stm); \
156                 stm = NULL; \
157         } \
158 } while (0)
159
160 /* DELETE statements */
161 static sqlite3_stmt *delete_fw_chain;
162 static sqlite3_stmt *delete_fw_rule;
163 static sqlite3_stmt *delete_fw_rule_per_chain;
164
165 /* SELECT statements */
166 static sqlite3_stmt *select_fw_lock;
167 static sqlite3_stmt *select_fw_chain;
168 static sqlite3_stmt *select_fw_rule;
169 static sqlite3_stmt *select_fw_rule_per_chain;
170
171 /* UPDATE statements */
172 static sqlite3_stmt *update_fw_lock;
173 static sqlite3_stmt *update_fw_chain;
174 static sqlite3_stmt *update_fw_rule;
175
176 /* INSERT statements */
177 static sqlite3_stmt *insert_fw_lock;
178 static sqlite3_stmt *insert_fw_chain;
179 static sqlite3_stmt *insert_fw_rule;
180
181 static int __prepare_delete(sqlite3 *db)
182 {
183         __STC_LOG_FUNC_ENTER__;
184         int rc;
185         static int initialized;
186
187         if (initialized) {
188                 __STC_LOG_FUNC_EXIT__;
189                 return SQLITE_OK;
190         }
191
192         PREPARE_DELETE(delete_fw_chain, DELETE_FIREWALL_CHAIN);
193         PREPARE_DELETE(delete_fw_rule, DELETE_FIREWALL_RULE);
194         PREPARE_DELETE(delete_fw_rule_per_chain, DELETE_FIREWALL_RULE_PER_CHAIN);
195
196         initialized = 1;
197         __STC_LOG_FUNC_EXIT__;
198         return rc;
199 }
200
201 static void __finalize_delete(void)
202 {
203         __STC_LOG_FUNC_ENTER__;
204
205         FINALIZE(delete_fw_chain);
206         FINALIZE(delete_fw_rule);
207         FINALIZE(delete_fw_rule_per_chain);
208
209         __STC_LOG_FUNC_EXIT__;
210 }
211
212 static int __prepare_select(sqlite3 *db)
213 {
214         __STC_LOG_FUNC_ENTER__;
215         int rc;
216         static int initialized;
217
218         if (initialized) {
219                 __STC_LOG_FUNC_EXIT__;
220                 return SQLITE_OK;
221         }
222
223         PREPARE_SELECT(select_fw_lock, SELECT_FIREWALL_LOCK);
224         PREPARE_SELECT(select_fw_chain, SELECT_FIREWALL_CHAIN);
225         PREPARE_SELECT(select_fw_rule, SELECT_FIREWALL_RULE);
226         PREPARE_SELECT(select_fw_rule_per_chain, SELECT_FIREWALL_RULE_PER_CHAIN);
227
228         initialized = 1;
229         __STC_LOG_FUNC_EXIT__;
230         return rc;
231 }
232
233 static void __finalize_select(void)
234 {
235         __STC_LOG_FUNC_ENTER__;
236
237         FINALIZE(select_fw_lock);
238         FINALIZE(select_fw_chain);
239         FINALIZE(select_fw_rule);
240         FINALIZE(select_fw_rule_per_chain);
241
242         __STC_LOG_FUNC_EXIT__;
243 }
244
245 static int __prepare_update(sqlite3 *db)
246 {
247         __STC_LOG_FUNC_ENTER__;
248         int rc;
249         static int initialized;
250
251         if (initialized) {
252                 __STC_LOG_FUNC_EXIT__;
253                 return SQLITE_OK;
254         }
255
256         PREPARE_UPDATE(update_fw_lock, UPDATE_FIREWALL_LOCK);
257         PREPARE_UPDATE(update_fw_chain, UPDATE_FIREWALL_CHAIN);
258         PREPARE_UPDATE(update_fw_rule, UPDATE_FIREWALL_RULE);
259
260         initialized = 1;
261         __STC_LOG_FUNC_EXIT__;
262         return rc;
263 }
264
265 static void __finalize_update(void)
266 {
267         __STC_LOG_FUNC_ENTER__;
268
269         FINALIZE(update_fw_lock);
270         FINALIZE(update_fw_chain);
271         FINALIZE(update_fw_rule);
272
273         __STC_LOG_FUNC_EXIT__;
274 }
275
276 static int __prepare_insert(sqlite3 *db)
277 {
278         __STC_LOG_FUNC_ENTER__;
279         int rc;
280         static int initialized;
281
282         if (initialized) {
283                 __STC_LOG_FUNC_EXIT__;
284                 return SQLITE_OK;
285         }
286
287         PREPARE_INSERT(insert_fw_lock, INSERT_FIREWALL_LOCK);
288         PREPARE_INSERT(insert_fw_chain, INSERT_FIREWALL_CHAIN);
289         PREPARE_INSERT(insert_fw_rule, INSERT_FIREWALL_RULE);
290
291         initialized = 1;
292         __STC_LOG_FUNC_EXIT__;
293         return rc;
294 }
295
296 static void __finalize_insert(void)
297 {
298         __STC_LOG_FUNC_ENTER__;
299
300         FINALIZE(insert_fw_lock);
301         FINALIZE(insert_fw_chain);
302         FINALIZE(insert_fw_rule);
303
304         __STC_LOG_FUNC_EXIT__;
305 }
306
307 API stc_error_e table_firewall_insert_lock(char *name, int state)
308 {
309         stc_error_e error_code = STC_ERROR_NONE;
310         sqlite3_stmt *stmt = insert_fw_lock;
311
312         DB_ACTION(sqlite3_bind_text(stmt, 1, name ? name : "",
313                                         -1, SQLITE_TRANSIENT));
314         DB_ACTION(sqlite3_bind_int(stmt, 2, state));
315
316         if (sqlite3_step(stmt) != SQLITE_DONE) {
317                 STC_LOGE("Failed to insert firewall lock state: %s\n",
318                          sqlite3_errmsg(stc_db_get_database()));
319
320                 error_code = STC_ERROR_DB_FAILED;
321                 goto handle_error;
322         }
323
324         STC_LOGD("Firewall lock state inserted [%d]", state);
325
326 handle_error:
327         if (sqlite3_reset(stmt) != SQLITE_OK)
328                 error_code = STC_ERROR_DB_FAILED;
329
330         return error_code;
331 }
332
333 API stc_error_e table_firewall_update_lock(char *name, int state)
334 {
335         stc_error_e error_code = STC_ERROR_NONE;
336         sqlite3_stmt *stmt = update_fw_lock;
337
338         DB_ACTION(sqlite3_bind_int(stmt, 1, state));
339         DB_ACTION(sqlite3_bind_text(stmt, 2, name ? name : "",
340                                         -1, SQLITE_TRANSIENT));
341
342         if (sqlite3_step(stmt) != SQLITE_DONE) {
343                 STC_LOGE("Failed to update firewall lock state: %s\n",
344                          sqlite3_errmsg(stc_db_get_database()));
345
346                 error_code = STC_ERROR_DB_FAILED;
347                 goto handle_error;
348         }
349
350         STC_LOGD("Firewall lock state updated [%d]", state);
351
352 handle_error:
353         if (sqlite3_reset(stmt) != SQLITE_OK)
354                 error_code = STC_ERROR_DB_FAILED;
355
356         return error_code;
357 }
358
359 API stc_error_e table_firewall_get_lock(char *name, int *state)
360 {
361         int rc;
362         stc_error_e error_code = STC_ERROR_NONE;
363         sqlite3_stmt *stmt = select_fw_lock;
364
365         if (!name)
366                 return STC_ERROR_DB_FAILED;
367
368         DB_ACTION(sqlite3_bind_text(stmt, 1, name,
369                                         -1, SQLITE_TRANSIENT));
370
371         rc = sqlite3_step(stmt);
372
373         switch (rc) {
374         case SQLITE_DONE:
375                 STC_LOGD("There is no lock state [%s]", name);
376                 error_code = STC_ERROR_NO_DATA;
377                 goto handle_error;
378         case SQLITE_ROW:
379                 *state = sqlite3_column_int(stmt, 0);
380                 break;
381         case SQLITE_ERROR:
382         default:
383                 STC_LOGE("Failed to get firewall lock state: %s\n",
384                          sqlite3_errmsg(stc_db_get_database()));
385
386                 error_code = STC_ERROR_DB_FAILED;
387                 goto handle_error;
388         }
389
390         STC_LOGD("Firewall lock state [%d]", *state);
391
392 handle_error:
393         if (sqlite3_reset(stmt) != SQLITE_OK)
394                 error_code = STC_ERROR_DB_FAILED;
395
396         return error_code;
397 }
398
399 API stc_error_e table_firewall_insert_chain(firewall_chain_s *info)
400 {
401         stc_error_e error_code = STC_ERROR_NONE;
402         sqlite3_stmt *stmt = insert_fw_chain;
403
404         if (!info) {
405                 error_code = STC_ERROR_INVALID_PARAMETER;
406                 goto handle_error;
407         }
408
409         DB_ACTION(sqlite3_bind_text(stmt, 1, info->chain ? info->chain : "",
410                                         -1, SQLITE_TRANSIENT));
411         DB_ACTION(sqlite3_bind_int(stmt, 2, info->target));
412         DB_ACTION(sqlite3_bind_int(stmt, 3, info->priority));
413
414         if (sqlite3_step(stmt) != SQLITE_DONE) {
415                 STC_LOGE("Failed to insert firewall chain: %s\n",
416                          sqlite3_errmsg(stc_db_get_database()));
417                 error_code = STC_ERROR_DB_FAILED;
418                 goto handle_error;
419         }
420
421         STC_LOGD("Firewall chain inserted [%s]", info->chain);
422
423 handle_error:
424         if (sqlite3_reset(stmt) != SQLITE_OK)
425                 error_code = STC_ERROR_DB_FAILED;
426
427         return error_code;
428 }
429
430 API stc_error_e table_firewall_delete_chain(firewall_chain_s *info)
431 {
432         stc_error_e error_code = STC_ERROR_NONE;
433         sqlite3_stmt *stmt = delete_fw_chain;
434
435         if (!info) {
436                 error_code = STC_ERROR_INVALID_PARAMETER;
437                 goto handle_error;
438         }
439
440         DB_ACTION(sqlite3_bind_text(stmt, 1, info->chain ? info->chain : "",
441                                     -1, SQLITE_TRANSIENT));
442
443         if (sqlite3_step(stmt) != SQLITE_DONE) {
444                 STC_LOGE("Failed to delete firewall chain %s\n",
445                          sqlite3_errmsg(stc_db_get_database()));
446                 error_code = STC_ERROR_DB_FAILED;
447                 goto handle_error;
448         }
449
450         STC_LOGD("Firewall chain deleted [%s]", info->chain);
451
452 handle_error:
453         if (sqlite3_reset(stmt) != SQLITE_OK)
454                 error_code = STC_ERROR_DB_FAILED;
455
456         return error_code;
457 }
458
459 API stc_error_e table_firewall_flush_chain(firewall_chain_s *info)
460 {
461         stc_error_e error_code = STC_ERROR_NONE;
462         sqlite3_stmt *stmt = delete_fw_rule_per_chain;
463
464         if (!info) {
465                 error_code = STC_ERROR_INVALID_PARAMETER;
466                 goto handle_error;
467         }
468
469         DB_ACTION(sqlite3_bind_text(stmt, 1, info->chain ? info->chain : "",
470                                     -1, SQLITE_TRANSIENT));
471
472         if (sqlite3_step(stmt) != SQLITE_DONE) {
473                 STC_LOGE("Failed to flush firewall chain %s\n",
474                          sqlite3_errmsg(stc_db_get_database()));
475                 error_code = STC_ERROR_DB_FAILED;
476                 goto handle_error;
477         }
478
479         STC_LOGD("Firewall chain flushed [%s]", info->chain);
480
481 handle_error:
482         if (sqlite3_reset(stmt) != SQLITE_OK)
483                 error_code = STC_ERROR_DB_FAILED;
484
485         return error_code;
486 }
487
488 API stc_error_e table_firewall_update_chain(firewall_chain_s *info)
489 {
490         stc_error_e error_code = STC_ERROR_NONE;
491         sqlite3_stmt *stmt = update_fw_chain;
492
493         if (!info) {
494                 error_code = STC_ERROR_INVALID_PARAMETER;
495                 goto handle_error;
496         }
497
498         DB_ACTION(sqlite3_bind_int(stmt, 1, info->target));
499         DB_ACTION(sqlite3_bind_int(stmt, 2, info->priority));
500         DB_ACTION(sqlite3_bind_text(stmt, 3, info->chain ? info->chain : "",
501                                         -1, SQLITE_TRANSIENT));
502
503         if (sqlite3_step(stmt) != SQLITE_DONE) {
504                 STC_LOGE("Failed to update firewall chain: %s\n",
505                          sqlite3_errmsg(stc_db_get_database()));
506                 error_code = STC_ERROR_DB_FAILED;
507                 goto handle_error;
508         }
509
510         STC_LOGD("Firewall chain updated [%s]", info->chain);
511
512 handle_error:
513         if (sqlite3_reset(stmt) != SQLITE_OK)
514                 error_code = STC_ERROR_DB_FAILED;
515
516         return error_code;
517 }
518
519 API stc_error_e table_firewall_foreach_chain(firewall_chain_cb info_cb,
520                                        void *user_data)
521 {
522         firewall_chain_s info;
523         int rc;
524         stc_error_e error_code = STC_ERROR_NONE;
525         sqlite3_stmt *stmt = select_fw_chain;
526
527         do {
528                 rc = sqlite3_step(stmt);
529
530                 memset(&info, 0, sizeof(info));
531
532                 switch (rc) {
533                 case SQLITE_DONE:
534                         break;
535                 case SQLITE_ROW:
536                         info.chain = (char *)sqlite3_column_text(stmt, 0);
537                         info.target = sqlite3_column_int(stmt, 1);
538                         info.priority = sqlite3_column_int(stmt, 2);
539
540                         if (info_cb(&info, user_data) == STC_CANCEL)
541                                 rc = SQLITE_DONE;
542                         break;
543                 case SQLITE_ERROR:
544                 default:
545                         STC_LOGE("Failed to enumerate firewall chains: %s\n",
546                                  sqlite3_errmsg(stc_db_get_database()));
547
548                         error_code = STC_ERROR_DB_FAILED;
549                 }
550         } while (rc == SQLITE_ROW);
551
552         if (sqlite3_reset(stmt) != SQLITE_OK)
553                 error_code = STC_ERROR_DB_FAILED;
554
555         return error_code;
556 }
557
558 API stc_error_e table_firewall_insert_rule(firewall_rule_s *info)
559 {
560         stc_error_e error_code = STC_ERROR_NONE;
561         char buf[BUF_SIZE_FOR_IP];
562         sqlite3_stmt *stmt = insert_fw_rule;
563
564         if (!info) {
565                 error_code = STC_ERROR_INVALID_PARAMETER;
566                 goto handle_error;
567         }
568
569         DB_ACTION(sqlite3_bind_int64(stmt, 1, info->key));
570         DB_ACTION(sqlite3_bind_text(stmt, 2, info->chain ? info->chain : "",
571                                         -1, SQLITE_TRANSIENT));
572         DB_ACTION(sqlite3_bind_int(stmt, 3, info->direction));
573         DB_ACTION(sqlite3_bind_int(stmt, 4, info->s_ip_type));
574         DB_ACTION(sqlite3_bind_int(stmt, 5, info->d_ip_type));
575         DB_ACTION(sqlite3_bind_int(stmt, 6, info->s_port_type));
576         DB_ACTION(sqlite3_bind_int(stmt, 7, info->d_port_type));
577         DB_ACTION(sqlite3_bind_int(stmt, 8, info->protocol));
578         DB_ACTION(sqlite3_bind_int(stmt, 9, info->family));
579         if (info->family == STC_FW_FAMILY_V4) {
580                 memset(buf, 0, sizeof(buf));
581                 snprintf(buf, sizeof(buf), "%08x", info->s_ip1.Ipv4.s_addr);
582                 DB_ACTION(sqlite3_bind_text(stmt, 10, buf, -1, SQLITE_TRANSIENT));
583
584                 memset(buf, 0, sizeof(buf));
585                 snprintf(buf, sizeof(buf), "%08x", info->s_ip2.Ipv4.s_addr);
586                 DB_ACTION(sqlite3_bind_text(stmt, 11, buf, -1, SQLITE_TRANSIENT));
587
588                 memset(buf, 0, sizeof(buf));
589                 snprintf(buf, sizeof(buf), "%08x", info->d_ip1.Ipv4.s_addr);
590                 DB_ACTION(sqlite3_bind_text(stmt, 12, buf, -1, SQLITE_TRANSIENT));
591
592                 memset(buf, 0, sizeof(buf));
593                 snprintf(buf, sizeof(buf), "%08x", info->d_ip2.Ipv4.s_addr);
594                 DB_ACTION(sqlite3_bind_text(stmt, 13, buf, -1, SQLITE_TRANSIENT));
595         } else if (info->family == STC_FW_FAMILY_V6) {
596                 memset(buf, 0, sizeof(buf));
597                 snprintf(buf, sizeof(buf), "%08x:%08x:%08x:%08x",
598                         info->s_ip1.Ipv6.s6_addr32[0], info->s_ip1.Ipv6.s6_addr32[1],
599                         info->s_ip1.Ipv6.s6_addr32[2], info->s_ip1.Ipv6.s6_addr32[3]);
600                 DB_ACTION(sqlite3_bind_text(stmt, 10, buf, -1, SQLITE_TRANSIENT));
601
602                 memset(buf, 0, sizeof(buf));
603                 snprintf(buf, sizeof(buf), "%08x:%08x:%08x:%08x",
604                         info->s_ip2.Ipv6.s6_addr32[0], info->s_ip2.Ipv6.s6_addr32[1],
605                         info->s_ip2.Ipv6.s6_addr32[2], info->s_ip2.Ipv6.s6_addr32[3]);
606                 DB_ACTION(sqlite3_bind_text(stmt, 11, buf, -1, SQLITE_TRANSIENT));
607
608                 memset(buf, 0, sizeof(buf));
609                 snprintf(buf, sizeof(buf), "%08x:%08x:%08x:%08x",
610                         info->d_ip1.Ipv6.s6_addr32[0], info->d_ip1.Ipv6.s6_addr32[1],
611                         info->d_ip1.Ipv6.s6_addr32[2], info->d_ip1.Ipv6.s6_addr32[3]);
612                 DB_ACTION(sqlite3_bind_text(stmt, 12, buf, -1, SQLITE_TRANSIENT));
613
614                 memset(buf, 0, sizeof(buf));
615                 snprintf(buf, sizeof(buf), "%08x:%08x:%08x:%08x",
616                         info->d_ip2.Ipv6.s6_addr32[0], info->d_ip2.Ipv6.s6_addr32[1],
617                         info->d_ip2.Ipv6.s6_addr32[2], info->d_ip2.Ipv6.s6_addr32[3]);
618                 DB_ACTION(sqlite3_bind_text(stmt, 13, buf, -1, SQLITE_TRANSIENT));
619         } else {
620                 DB_ACTION(sqlite3_bind_text(stmt, 10, "", -1, SQLITE_TRANSIENT));
621                 DB_ACTION(sqlite3_bind_text(stmt, 11, "", -1, SQLITE_TRANSIENT));
622                 DB_ACTION(sqlite3_bind_text(stmt, 12, "", -1, SQLITE_TRANSIENT));
623                 DB_ACTION(sqlite3_bind_text(stmt, 13, "", -1, SQLITE_TRANSIENT));
624         }
625         DB_ACTION(sqlite3_bind_int(stmt, 14, info->s_port1));
626         DB_ACTION(sqlite3_bind_int(stmt, 15, info->s_port2));
627         DB_ACTION(sqlite3_bind_int(stmt, 16, info->d_port1));
628         DB_ACTION(sqlite3_bind_int(stmt, 17, info->d_port2));
629         DB_ACTION(sqlite3_bind_text(stmt, 18, info->ifname ? info->ifname : "",
630                                         -1, SQLITE_TRANSIENT));
631         DB_ACTION(sqlite3_bind_int(stmt, 19, info->target));
632         DB_ACTION(sqlite3_bind_int(stmt, 20, info->log_level));
633         DB_ACTION(sqlite3_bind_text(stmt, 21, info->log_prefix ? info->log_prefix : "",
634                                         -1, SQLITE_TRANSIENT));
635         DB_ACTION(sqlite3_bind_int(stmt, 22, info->nflog_group));
636         DB_ACTION(sqlite3_bind_text(stmt, 23, info->nflog_prefix ? info->nflog_prefix : "",
637                                         -1, SQLITE_TRANSIENT));
638         DB_ACTION(sqlite3_bind_int(stmt, 24, info->nflog_range));
639         DB_ACTION(sqlite3_bind_int(stmt, 25, info->nflog_threshold));
640         DB_ACTION(sqlite3_bind_text(stmt, 26, info->identifier ? info->identifier : "",
641                                         -1, SQLITE_TRANSIENT));
642
643         if (sqlite3_step(stmt) != SQLITE_DONE) {
644                 STC_LOGE("Failed to insert firewall rule: %s\n",
645                          sqlite3_errmsg(stc_db_get_database()));
646                 error_code = STC_ERROR_DB_FAILED;
647                 goto handle_error;
648         }
649
650         STC_LOGD("Firewall rule inserted [%s]", info->chain);
651
652 handle_error:
653         if (sqlite3_reset(stmt) != SQLITE_OK)
654                 error_code = STC_ERROR_DB_FAILED;
655
656         return error_code;
657 }
658
659 API stc_error_e table_firewall_delete_rule(firewall_rule_s *info)
660 {
661         stc_error_e error_code = STC_ERROR_NONE;
662         sqlite3_stmt *stmt = delete_fw_rule;
663
664         if (!info) {
665                 error_code = STC_ERROR_INVALID_PARAMETER;
666                 goto handle_error;
667         }
668
669         DB_ACTION(sqlite3_bind_int64(stmt, 1, info->key));
670
671         if (sqlite3_step(stmt) != SQLITE_DONE) {
672                 STC_LOGE("Failed to delete firewall rule %s\n",
673                          sqlite3_errmsg(stc_db_get_database()));
674                 error_code = STC_ERROR_DB_FAILED;
675                 goto handle_error;
676         }
677
678         STC_LOGD("Firewall rule deleted [%s]", info->chain);
679
680 handle_error:
681         if (sqlite3_reset(stmt) != SQLITE_OK)
682                 error_code = STC_ERROR_DB_FAILED;
683
684         return error_code;
685 }
686
687 API stc_error_e table_firewall_update_rule(firewall_rule_s *info, guint key)
688 {
689         stc_error_e error_code = STC_ERROR_NONE;
690         char buf[BUF_SIZE_FOR_IP];
691         sqlite3_stmt *stmt = update_fw_rule;
692
693         if (!info) {
694                 error_code = STC_ERROR_INVALID_PARAMETER;
695                 goto handle_error;
696         }
697
698         DB_ACTION(sqlite3_bind_text(stmt, 1, info->chain ? info->chain : "",
699                                         -1, SQLITE_TRANSIENT));
700         DB_ACTION(sqlite3_bind_int(stmt, 2, info->direction));
701         DB_ACTION(sqlite3_bind_int(stmt, 3, info->s_ip_type));
702         DB_ACTION(sqlite3_bind_int(stmt, 4, info->d_ip_type));
703         DB_ACTION(sqlite3_bind_int(stmt, 5, info->s_port_type));
704         DB_ACTION(sqlite3_bind_int(stmt, 6, info->d_port_type));
705         DB_ACTION(sqlite3_bind_int(stmt, 7, info->protocol));
706         DB_ACTION(sqlite3_bind_int(stmt, 8, info->family));
707         if (info->family == STC_FW_FAMILY_V4) {
708                 memset(buf, 0, sizeof(buf));
709                 snprintf(buf, sizeof(buf), "%08x", info->s_ip1.Ipv4.s_addr);
710                 DB_ACTION(sqlite3_bind_text(stmt, 9, buf, -1, SQLITE_TRANSIENT));
711
712                 memset(buf, 0, sizeof(buf));
713                 snprintf(buf, sizeof(buf), "%08x", info->s_ip2.Ipv4.s_addr);
714                 DB_ACTION(sqlite3_bind_text(stmt, 10, buf, -1, SQLITE_TRANSIENT));
715
716                 memset(buf, 0, sizeof(buf));
717                 snprintf(buf, sizeof(buf), "%08x", info->d_ip1.Ipv4.s_addr);
718                 DB_ACTION(sqlite3_bind_text(stmt, 11, buf, -1, SQLITE_TRANSIENT));
719
720                 memset(buf, 0, sizeof(buf));
721                 snprintf(buf, sizeof(buf), "%08x", info->d_ip2.Ipv4.s_addr);
722                 DB_ACTION(sqlite3_bind_text(stmt, 12, buf, -1, SQLITE_TRANSIENT));
723         } else if (info->family == STC_FW_FAMILY_V6) {
724                 memset(buf, 0, sizeof(buf));
725                 snprintf(buf, sizeof(buf), "%08x:%08x:%08x:%08x",
726                         info->s_ip1.Ipv6.s6_addr32[0], info->s_ip1.Ipv6.s6_addr32[1],
727                         info->s_ip1.Ipv6.s6_addr32[2], info->s_ip1.Ipv6.s6_addr32[3]);
728                 DB_ACTION(sqlite3_bind_text(stmt, 9, buf, -1, SQLITE_TRANSIENT));
729
730                 memset(buf, 0, sizeof(buf));
731                 snprintf(buf, sizeof(buf), "%08x:%08x:%08x:%08x",
732                         info->s_ip2.Ipv6.s6_addr32[0], info->s_ip2.Ipv6.s6_addr32[1],
733                         info->s_ip2.Ipv6.s6_addr32[2], info->s_ip2.Ipv6.s6_addr32[3]);
734                 DB_ACTION(sqlite3_bind_text(stmt, 10, buf, -1, SQLITE_TRANSIENT));
735
736                 memset(buf, 0, sizeof(buf));
737                 snprintf(buf, sizeof(buf), "%08x:%08x:%08x:%08x",
738                         info->d_ip1.Ipv6.s6_addr32[0], info->d_ip1.Ipv6.s6_addr32[1],
739                         info->d_ip1.Ipv6.s6_addr32[2], info->d_ip1.Ipv6.s6_addr32[3]);
740                 DB_ACTION(sqlite3_bind_text(stmt, 11, buf, -1, SQLITE_TRANSIENT));
741
742                 memset(buf, 0, sizeof(buf));
743                 snprintf(buf, sizeof(buf), "%08x:%08x:%08x:%08x",
744                         info->d_ip2.Ipv6.s6_addr32[0], info->d_ip2.Ipv6.s6_addr32[1],
745                         info->d_ip2.Ipv6.s6_addr32[2], info->d_ip2.Ipv6.s6_addr32[3]);
746                 DB_ACTION(sqlite3_bind_text(stmt, 12, buf, -1, SQLITE_TRANSIENT));
747         } else {
748                 DB_ACTION(sqlite3_bind_text(stmt, 9, "", -1, SQLITE_TRANSIENT));
749                 DB_ACTION(sqlite3_bind_text(stmt, 10, "", -1, SQLITE_TRANSIENT));
750                 DB_ACTION(sqlite3_bind_text(stmt, 11, "", -1, SQLITE_TRANSIENT));
751                 DB_ACTION(sqlite3_bind_text(stmt, 12, "", -1, SQLITE_TRANSIENT));
752         }
753         DB_ACTION(sqlite3_bind_int(stmt, 13, info->s_port1));
754         DB_ACTION(sqlite3_bind_int(stmt, 14, info->s_port2));
755         DB_ACTION(sqlite3_bind_int(stmt, 15, info->d_port1));
756         DB_ACTION(sqlite3_bind_int(stmt, 16, info->d_port2));
757         DB_ACTION(sqlite3_bind_text(stmt, 17, info->ifname ? info->ifname : "",
758                                         -1, SQLITE_TRANSIENT));
759         DB_ACTION(sqlite3_bind_int(stmt, 18, info->target));
760         DB_ACTION(sqlite3_bind_int(stmt, 19, info->log_level));
761         DB_ACTION(sqlite3_bind_text(stmt, 20, info->log_prefix ? info->log_prefix : "",
762                                         -1, SQLITE_TRANSIENT));
763         DB_ACTION(sqlite3_bind_int(stmt, 21, info->nflog_group));
764         DB_ACTION(sqlite3_bind_text(stmt, 22, info->nflog_prefix ? info->nflog_prefix : "",
765                                         -1, SQLITE_TRANSIENT));
766         DB_ACTION(sqlite3_bind_int(stmt, 23, info->nflog_range));
767         DB_ACTION(sqlite3_bind_int(stmt, 24, info->nflog_threshold));
768         DB_ACTION(sqlite3_bind_text(stmt, 25, info->identifier ? info->identifier : "",
769                                         -1, SQLITE_TRANSIENT));
770         DB_ACTION(sqlite3_bind_int64(stmt, 26, info->key));
771         DB_ACTION(sqlite3_bind_int64(stmt, 27, key));
772
773         if (sqlite3_step(stmt) != SQLITE_DONE) {
774                 STC_LOGE("Failed to update firewall rule %s\n",
775                          sqlite3_errmsg(stc_db_get_database()));
776                 error_code = STC_ERROR_DB_FAILED;
777                 goto handle_error;
778         }
779
780         STC_LOGD("Firewall rule updated [%s]", info->chain);
781
782 handle_error:
783         if (sqlite3_reset(stmt) != SQLITE_OK)
784                 error_code = STC_ERROR_DB_FAILED;
785
786         return error_code;
787 }
788
789 API stc_error_e table_firewall_foreach_rule(firewall_rule_cb info_cb,
790                                        void *user_data)
791 {
792         firewall_rule_s info;
793         int rc;
794         stc_error_e error_code = STC_ERROR_NONE;
795         sqlite3_stmt *stmt = select_fw_rule;
796
797         do {
798                 rc = sqlite3_step(stmt);
799
800                 memset(&info, 0, sizeof(info));
801
802                 switch (rc) {
803                 case SQLITE_DONE:
804                         break;
805                 case SQLITE_ROW:
806                         info.key = sqlite3_column_int64(stmt, 0);
807                         info.chain = (char *)sqlite3_column_text(stmt, 1);
808                         info.direction = sqlite3_column_int(stmt, 2);
809                         info.s_ip_type = sqlite3_column_int(stmt, 3);
810                         info.d_ip_type = sqlite3_column_int(stmt, 4);
811                         info.s_port_type = sqlite3_column_int(stmt, 5);
812                         info.d_port_type = sqlite3_column_int(stmt, 6);
813                         info.protocol = sqlite3_column_int(stmt, 7);
814                         info.family = sqlite3_column_int(stmt, 8);
815                         if (info.family == STC_FW_FAMILY_V4) {
816                                 sscanf((char *)sqlite3_column_text(stmt, 9), "%08x",
817                                                                 &(info.s_ip1.Ipv4.s_addr));
818                                 sscanf((char *)sqlite3_column_text(stmt, 10), "%08x",
819                                                                 &(info.s_ip2.Ipv4.s_addr));
820                                 sscanf((char *)sqlite3_column_text(stmt, 11), "%08x",
821                                                                 &(info.d_ip1.Ipv4.s_addr));
822                                 sscanf((char *)sqlite3_column_text(stmt, 12), "%08x",
823                                                                 &(info.d_ip2.Ipv4.s_addr));
824                         } else if (info.family == STC_FW_FAMILY_V6) {
825                                 sscanf((char *)sqlite3_column_text(stmt, 9), "%08x:%08x:%08x:%08x",
826                                         &(info.s_ip1.Ipv6.s6_addr32[0]), &(info.s_ip1.Ipv6.s6_addr32[1]),
827                                         &(info.s_ip1.Ipv6.s6_addr32[2]), &(info.s_ip1.Ipv6.s6_addr32[3]));
828                                 sscanf((char *)sqlite3_column_text(stmt, 10), "%08x:%08x:%08x:%08x",
829                                         &(info.s_ip2.Ipv6.s6_addr32[0]), &(info.s_ip2.Ipv6.s6_addr32[1]),
830                                         &(info.s_ip2.Ipv6.s6_addr32[2]), &(info.s_ip2.Ipv6.s6_addr32[3]));
831                                 sscanf((char *)sqlite3_column_text(stmt, 11), "%08x:%08x:%08x:%08x",
832                                         &(info.d_ip1.Ipv6.s6_addr32[0]), &(info.d_ip1.Ipv6.s6_addr32[1]),
833                                         &(info.d_ip1.Ipv6.s6_addr32[2]), &(info.d_ip1.Ipv6.s6_addr32[3]));
834                                 sscanf((char *)sqlite3_column_text(stmt, 12), "%08x:%08x:%08x:%08x",
835                                         &(info.d_ip2.Ipv6.s6_addr32[0]), &(info.d_ip2.Ipv6.s6_addr32[1]),
836                                         &(info.d_ip2.Ipv6.s6_addr32[2]), &(info.d_ip2.Ipv6.s6_addr32[3]));
837                         }
838                         info.s_port1 = sqlite3_column_int(stmt, 13);
839                         info.s_port2 = sqlite3_column_int(stmt, 14);
840                         info.d_port1 = sqlite3_column_int(stmt, 15);
841                         info.d_port2 = sqlite3_column_int(stmt, 16);
842                         info.ifname = (char *)sqlite3_column_text(stmt, 17);
843                         info.target = sqlite3_column_int(stmt, 18);
844                         info.log_level = sqlite3_column_int(stmt, 19);
845                         info.log_prefix = (char *)sqlite3_column_text(stmt, 20);
846                         info.nflog_group = sqlite3_column_int(stmt, 21);
847                         info.nflog_prefix = (char *)sqlite3_column_text(stmt, 22);
848                         info.nflog_range = sqlite3_column_int(stmt, 23);
849                         info.nflog_threshold = sqlite3_column_int(stmt, 24);
850                         info.identifier = (char *)sqlite3_column_text(stmt, 25);
851
852                         if (info_cb(&info, user_data) == STC_CANCEL)
853                                 rc = SQLITE_DONE;
854                         break;
855                 case SQLITE_ERROR:
856                 default:
857                         STC_LOGE("Failed to enumerate firewall rules: %s\n",
858                                  sqlite3_errmsg(stc_db_get_database()));
859
860                         error_code = STC_ERROR_DB_FAILED;
861                 }
862         } while (rc == SQLITE_ROW);
863
864         if (sqlite3_reset(stmt) != SQLITE_OK)
865                 error_code = STC_ERROR_DB_FAILED;
866
867         return error_code;
868 }
869
870 stc_error_e table_firewall_prepare(sqlite3 *db)
871 {
872         __STC_LOG_FUNC_ENTER__;
873
874         stc_error_e error_code = STC_ERROR_NONE;
875
876         if (db == NULL) {
877                 __STC_LOG_FUNC_EXIT__;
878                 return STC_ERROR_FAIL;
879         }
880
881         DB_ACTION(__prepare_delete(db));
882         DB_ACTION(__prepare_select(db));
883         DB_ACTION(__prepare_update(db));
884         DB_ACTION(__prepare_insert(db));
885
886 handle_error:
887
888         __STC_LOG_FUNC_EXIT__;
889         return error_code;
890 }
891
892 void table_firewall_finalize(void)
893 {
894         __STC_LOG_FUNC_ENTER__;
895         __finalize_delete();
896         __finalize_select();
897         __finalize_update();
898         __finalize_insert();
899         __STC_LOG_FUNC_EXIT__;
900 }