forked from eucalyptus/eucalyptus
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathipt_handler.c
1046 lines (936 loc) · 36 KB
/
ipt_handler.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -*- mode: C; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil -*-
// vim: set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:
/*************************************************************************
* Copyright 2016 Ent. Services Development Corporation LP
*
* Redistribution and use of this software in source and binary forms,
* with or without modification, are permitted provided that the
* following conditions are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
************************************************************************/
//!
//! @file net/ipt_handler.c
//! This file needs a description
//!
/*----------------------------------------------------------------------------*\
| |
| INCLUDES |
| |
\*----------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <limits.h>
#include <eucalyptus.h>
#include <log.h>
#include <euca_string.h>
#include "ipt_handler.h"
#include "eucanetd_util.h"
/*----------------------------------------------------------------------------*\
| |
| DEFINES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| TYPEDEFS |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| ENUMERATIONS |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| STRUCTURES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| EXTERNAL VARIABLES |
| |
\*----------------------------------------------------------------------------*/
/* Should preferably be handled in header file */
/*----------------------------------------------------------------------------*\
| |
| GLOBAL VARIABLES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| STATIC VARIABLES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| STATIC PROTOTYPES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| MACROS |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| IMPLEMENTATION |
| |
\*----------------------------------------------------------------------------*/
/**
* Initialize an IP table handler structure
*
* @param pIpt [in] pointer to the IP table handler structure
* @param psCmdPrefix [in] a constant pointer to a string containing the prefix for EUCA commands
* @param psPreloadPath [in] a constant pointer to a string containing the path to the IP table preload file
*
* @return 0 on success or 1 if any failure occurred
*
* @see ipt_handler_free()
*
* @pre
* - The pIpt pointer should not be NULL
* - We should be able to create temporary files on the system
* - We should be able to execute the iptables commands
*
* @post
* On success, the IP table structure will be initialized with the following:
* - The ipt_file will point to a temporary file under /tmp/ipt_file-XXXXXX
* - If psCmdPrefix was provided, the table's cmdprefix field will be set with it
* - If psPreloadPath was provided, the structure's preloadPath will be set with it
* -
*
* @note
* - Once temporary file is initialized the filename will be reused throughout the process
* lifetime. The file will be truncated/created on each successive calls to the *_handler_init()
* method.
*/
int ipt_handler_init(ipt_handler *pIpt, const char *psCmdPrefix, const char *psPreloadPath) {
int fd = 0;
char sTempFileName[EUCA_MAX_PATH] = ""; // Used to temporarily hold name while we zero out the struct
// Make sure our pointers are valid
if (!pIpt) {
return (1);
}
//
// Initialize the temporary file *ONCE* per process execution
// This handler init function is called many times, but the temporary file
// will always be the same for the associated handler struct.
//
if (pIpt->init) {
//
// Copy filename out of the current ipt_handler struct.
//
snprintf(sTempFileName, EUCA_MAX_PATH, "%s", pIpt->ipt_file);
// Truncate the file to ensure we're dealing with a clean slate.
if (truncate_file(sTempFileName)){
return (1);
}
LOGDEBUG("Using already allocated temporary filename: %s\n", sTempFileName);
} else {
//
// Initialize a new temporaty file name
//
snprintf(sTempFileName, EUCA_MAX_PATH, "/tmp/ipt_file-XXXXXX");
if ((fd = safe_mkstemp(sTempFileName)) < 0) {
LOGERROR("cannot create tmpfile '%s': check permissions\n", sTempFileName);
return (1);
}
// Check to see if we can set the permissions to 0600
if (chmod(sTempFileName, 0600) < 0) {
LOGWARN("chmod failed: ipt_file '%s' errno: %d\n", sTempFileName, errno);
}
LOGDEBUG("Using newly created temporary filename: %s\n", sTempFileName);
close(fd);
}
// Empty this structure
bzero(pIpt, sizeof(ipt_handler));
// Populate the temporary filename
snprintf(pIpt->ipt_file, EUCA_MAX_PATH, "%s", sTempFileName);
// If we have a command prefix (like euca_rootwrap) set it
pIpt->cmdprefix[0] = '\0';
if (psCmdPrefix) {
snprintf(pIpt->cmdprefix, EUCA_MAX_PATH, "%s", psCmdPrefix);
}
// If we have a preload file path, set it.
pIpt->preloadPath[0] = '\0';
if (psPreloadPath) {
snprintf(pIpt->preloadPath, EUCA_MAX_PATH, "%s", psPreloadPath);
}
// test required shell-outs
if (euca_execlp_redirect(NULL, NULL, "/dev/null", FALSE, "/dev/null", FALSE, pIpt->cmdprefix, "iptables-save", NULL) != EUCA_OK) {
LOGERROR("could not execute iptables-save. check command/permissions\n");
return (1);
}
pIpt->init = 1;
return (0);
}
/**
* Runs iptables-save and store the content in our configured IP table file
*
* @param pIpt [in] pointer to the IP table handler structure
*
* @return 0 on success or any other values if any failure occurred
*
* @see ipt_system_restore()
*
* @pre
* - pIpt MUST not be NULL
* - We should be able to write to the configured IP table structure temporary file
*
* @post
* On success, the content from iptables-save is stored in ipth->ipt_file. On failure,
* the destination file should remain unchanged.
*
* @note
*/
int ipt_system_save(ipt_handler *pIpt) {
if (euca_execlp_redirect(NULL, NULL, pIpt->ipt_file, FALSE, NULL, FALSE, pIpt->cmdprefix, "iptables-save", "-c", NULL) != EUCA_OK) {
LOGERROR("iptables-save failed\n");
return EUCA_ERROR;
}
return EUCA_OK;
}
/**
* Runs the iptables-restore program provided with our IP table configured file.
*
* @param pIpt [in] pointer to the IP table handler structure
*
* @return 0 on success or any other value if any failure occurred
*
* @see ipt_system_save()
*
* @pre
* - pIpt MUST not be NULL
* - The IP table structure temporary file must exists on the system
*
* @post
* On success, the system IP tables have been restored with the content from our
* configured file. On failure, the system IP tables should remain unchanged and
* the content of the file saved in /tmp/euca_ipt_file_failed.
*
* @note
*/
int ipt_system_restore(ipt_handler *pIpt) {
int rc = EUCA_OK;
if (euca_execlp_redirect(NULL, pIpt->ipt_file, NULL, FALSE, NULL, FALSE, pIpt->cmdprefix, "iptables-restore", "-c", NULL) != EUCA_OK) {
copy_file(pIpt->ipt_file, "/tmp/euca_ipt_file_failed");
LOGERROR("iptables-restore failed. copying failed input file to '/tmp/euca_ipt_file_failed' for manual retry.\n");
rc = EUCA_ERROR;
}
unlink_handler_file(pIpt->ipt_file);
return (rc);
}
/**
* Takes our latest IP table virtual content and puts it into a file in IP tables format that
* will be passed to ip_system_restore(). Once completed, the system IP tables should contain
* the latest changes we made.
*
* @param pIpt [in] pointer to the IP table handler structure
*
* @return 0 on success or any other value if any failure occurred
*
* @see ipt_system_restore()
*
* @pre
* - Our given pointers must not be NULL
* - The IP table structure must have been intialized
* - The system must allow us to write to the file configured in the IP table structure
*
* @post
* On success, the system IP tables will contain what we put in our structure. On failure, the
* system IP tables should remain unchanged.
*
* @note
*/
int ipt_handler_deploy(ipt_handler * pIpt) {
int i = 0;
int j = 0;
int k = 0;
char *psPreload = NULL;
FILE *pFh = NULL;
if (!pIpt || !pIpt->init) {
return (1);
}
ipt_handler_update_refcounts(pIpt);
if ((pFh = fopen(pIpt->ipt_file, "w")) == NULL) {
LOGERROR("could not open file for write '%s': check permissions\n", pIpt->ipt_file);
return (1);
}
// do the preload stuff first if needed
if (strlen(pIpt->preloadPath)) {
if ((psPreload = file2str(pIpt->preloadPath)) == NULL) {
LOGTRACE("Fail to load IP table preload content from '%s'.\n", pIpt->preloadPath);
} else {
fprintf(pFh, "%s\n", psPreload);
EUCA_FREE(psPreload);
}
}
for (i = 0; i < pIpt->max_tables; i++) {
fprintf(pFh, "*%s\n", pIpt->tables[i].name);
for (j = 0; j < pIpt->tables[i].max_chains; j++) {
if (!pIpt->tables[i].chains[j].flushed && pIpt->tables[i].chains[j].ref_count) {
fprintf(pFh, ":%s %s %s\n", pIpt->tables[i].chains[j].name, pIpt->tables[i].chains[j].policyname, pIpt->tables[i].chains[j].counters);
}
}
for (j = 0; j < pIpt->tables[i].max_chains; j++) {
if (!pIpt->tables[i].chains[j].flushed && pIpt->tables[i].chains[j].ref_count) {
// qsort!
qsort(pIpt->tables[i].chains[j].rules, pIpt->tables[i].chains[j].max_rules, sizeof(ipt_rule), ipt_ruleordercmp);
for (k = 0; k < pIpt->tables[i].chains[j].max_rules; k++) {
if (!pIpt->tables[i].chains[j].rules[k].flushed) {
fprintf(pFh, "%s %s\n", pIpt->tables[i].chains[j].rules[k].counterstr, pIpt->tables[i].chains[j].rules[k].iptrule);
}
}
}
}
fprintf(pFh, "COMMIT\n");
}
fclose(pFh);
return (ipt_system_restore(pIpt));
}
/**
* Compares two given rules to see which comes first
*
* @param p1 [in] a pointer to the left hand side IP table rule
* @param p2 [in] a pointer to the right hand side IP table rule
*
* @return 0 if p1 an p2 are of the same order, -1 if p1 comes before p2 and 1 if p2 comes before p1.
*/
int ipt_ruleordercmp(const void *p1, const void *p2) {
ipt_rule *a = NULL, *b = NULL;
a = (ipt_rule *) p1;
b = (ipt_rule *) p2;
if (a->order == b->order) {
return (0);
} else if (a->order > b->order) {
return (1);
} else if (a->order < b->order) {
return (-1);
}
return (0);
}
/**
* Retrieves the current iptables system state.
*
* @param ipth [in] pointer to the IP table handler structure
*
* @return 0 on success or 1 if any failure occurred
*/
int ipt_handler_repopulate(ipt_handler *ipth) {
int rc = 0;
FILE *FH = NULL;
char buf[1024] = "";
char tmpbuf[1024] = "";
char *strptr = NULL;
char newrule[1024] = "";
char tablename[64] = "";
char chainname[64] = "";
char policyname[64] = "";
char counters[64] = "";
char counterstr[256] = "";
struct timeval tv = { 0 };
// long long int countersa, countersb;
eucanetd_timer_usec(&tv);
if (!ipth || !ipth->init) {
LOGERROR("Invalid argument: cannot reinitialize NULL ipt handler.\n");
return (1);
}
rc = ipt_handler_free(ipth);
if (rc) {
LOGERROR("could not reinitialize ipt handler.\n");
return (1);
}
rc = ipt_system_save(ipth);
if (rc) {
LOGERROR("could not save current IPT rules to file, exiting re-populate\n");
return (1);
}
FH = fopen(ipth->ipt_file, "r");
if (!FH) {
LOGERROR("could not open file for read '%s': check permissions\n", ipth->ipt_file);
return (1);
}
while (fgets(buf, 1024, FH)) {
if ((strptr = strchr(buf, '\n'))) {
*strptr = '\0';
}
if (strlen(buf) < 1) {
continue;
}
while (buf[strlen(buf) - 1] == ' ') {
buf[strlen(buf) - 1] = '\0';
}
if (buf[0] == '*') {
tablename[0] = '\0';
sscanf(buf, "%[*]%s", tmpbuf, tablename);
if (strlen(tablename)) {
ipt_handler_add_table(ipth, tablename);
}
} else if (buf[0] == ':') {
chainname[0] = '\0';
sscanf(buf, "%[:]%s %s %s", tmpbuf, chainname, policyname, counters);
if (strlen(chainname)) {
ipt_table_add_chain(ipth, tablename, chainname, policyname, counters);
}
} else if (strstr(buf, "COMMIT")) {
} else if (buf[0] == '#') {
} else if (buf[0] == '[' && strstr(buf, "-A")) {
// sscanf(buf, "[%lld:%lld] %[-A] %s", &countersa, &countersb, tmpbuf, chainname);
sscanf(buf, "%s %[-A] %s", counterstr, tmpbuf, chainname);
snprintf(newrule, 1024, "%s", strstr(buf, "-A"));
// ipt_chain_insert_rule(ipth, tablename, chainname, newrule, countersa, countersb, IPT_NO_ORDER);
ipt_chain_insert_rule(ipth, tablename, chainname, newrule, counterstr, IPT_NO_ORDER);
} else {
LOGWARN("unknown IPT rule on ingress, will be thrown out: (%s)\n", buf);
}
}
fclose(FH);
unlink_handler_file(ipth->ipt_file);
LOGDEBUG("ipt populated in %.2f ms.\n", eucanetd_timer_usec(&tv) / 1000.0);
return (0);
}
/**
* Adds table tablename. No-op if tablename is already present.
*
* @param ipth [in] pointer to the IP table handler structure
* @param tablename [in] a string pointer to the table name
*
* @return 0 on success or 1 if any failure occurred
*
*/
int ipt_handler_add_table(ipt_handler *ipth, char *tablename) {
ipt_table *table = NULL;
if (!ipth || !tablename || !ipth->init) {
return (1);
}
table = ipt_handler_find_table(ipth, tablename);
if (!table) {
ipth->tables = realloc(ipth->tables, sizeof(ipt_table) * (ipth->max_tables + 1));
if (!ipth->tables) {
LOGFATAL("out of memory!\n");
exit(1);
}
bzero(&(ipth->tables[ipth->max_tables]), sizeof(ipt_table));
snprintf(ipth->tables[ipth->max_tables].name, 64, "%s", tablename);
ipth->max_tables++;
}
return (0);
}
/**
* Add chain chainname to table tablename with policy policyname and counters.
*
* @param ipth [in] pointer to the IP table handler structure
* @param tablename [in] a string pointer to the table name
* @param chainname [in] a string pointer to the chain name
* @param policyname [in] a string pointer to the policy to apply
* @param counters [in] a string pointer to the counters
*
* @return 0 on success or 1 if any failure occurred
*/
int ipt_table_add_chain(ipt_handler *ipth, char *tablename, char *chainname, char *policyname, char *counters) {
ipt_table *table = NULL;
ipt_chain *chain = NULL;
if (!ipth || !tablename || !chainname || !counters || !ipth->init) {
return (1);
}
table = ipt_handler_find_table(ipth, tablename);
if (!table) {
return (1);
}
chain = ipt_table_find_chain(ipth, tablename, chainname);
if (!chain) {
table->chains = realloc(table->chains, sizeof(ipt_chain) * (table->max_chains + 1));
if (!table->chains) {
LOGFATAL("out of memory!\n");
exit(1);
}
bzero(&(table->chains[table->max_chains]), sizeof(ipt_chain));
snprintf(table->chains[table->max_chains].name, 64, "%s", chainname);
snprintf(table->chains[table->max_chains].policyname, 64, "%s", policyname);
snprintf(table->chains[table->max_chains].counters, 64, "%s", counters);
if (!strcmp(table->chains[table->max_chains].name, "INPUT") ||
!strcmp(table->chains[table->max_chains].name, "FORWARD") ||
!strcmp(table->chains[table->max_chains].name, "OUTPUT") ||
!strcmp(table->chains[table->max_chains].name, "PREROUTING") ||
!strcmp(table->chains[table->max_chains].name, "POSTROUTING")) {
table->chains[table->max_chains].ref_count = 1;
}
chain = &(table->chains[table->max_chains]);
table->max_chains++;
}
chain->flushed = 0;
return (0);
}
/**
* Add rule newrule to chain chainname in table tablename.
*
* @param ipth [in] pointer to the IP table handler structure
* @param tablename [in] a string pointer to the table name
* @param chainname [in] a string pointer to the chain name
* @param newrule [in] a string pointer to the rule to add
*
* @return 0 on success or 1 if any failure occurred
*/
int ipt_chain_add_rule(ipt_handler *ipth, char *tablename, char *chainname, char *newrule) {
return (ipt_chain_add_rule_with_counters(ipth, tablename, chainname, newrule, NULL));
}
/**
* Add rule newrule with counter counterstr to chain chainname in table tablename.
*
* @param ipth [in] pointer to the IP table handler structure
* @param tablename [in] a string pointer to the table name
* @param chainname [in] a string pointer to the chain name
* @param newrule [in] a string pointer to the rule to add
* @param counterstr [in] a string pointer to the counters
*
* @return 0 on success or 1 if any failure occurred
*/
int ipt_chain_add_rule_with_counters(ipt_handler *ipth, char *tablename, char *chainname, char *newrule, char *counterstr) {
return (ipt_chain_insert_rule(ipth, tablename, chainname, newrule, counterstr, IPT_ORDER));
}
/**
* Inserts rule newrule to chain chainname in table tablename with counter counterstr
* in the position dictated by order.
*
* @param ipth [in] pointer to the IP table handler structure
* @param tablename [in] a string pointer to the table name
* @param chainname [in] a string pointer to the chain name
* @param newrule [in] a string pointer to the new rule
* @param counterstr [in] a string pointer to the counters
* @param order [in] the order in which to insert this rule
*
* @return 0 on success or 1 if any failure occurred
*/
int ipt_chain_insert_rule(ipt_handler *ipth, char *tablename, char *chainname, char *newrule, char *counterstr, int order) {
int ret = 0;
ipt_table *table = NULL;
ipt_chain *chain = NULL;
ipt_rule *rule = NULL;
if (!ipth || !tablename || !chainname || !newrule || !ipth->init) {
return (1);
}
table = ipt_handler_find_table(ipth, tablename);
if (!table) {
return (1);
}
chain = ipt_table_find_chain(ipth, tablename, chainname);
if (!chain) {
return (1);
}
rule = ipt_chain_find_rule(ipth, tablename, chainname, newrule);
if (!rule) {
chain->rules = realloc(chain->rules, sizeof(ipt_rule) * (chain->max_rules + 1));
if (!chain->rules) {
LOGFATAL("out of memory!\n");
exit(1);
}
rule = &(chain->rules[chain->max_rules]);
bzero(rule, sizeof(ipt_rule));
snprintf(rule->iptrule, 1024, "%s", newrule);
snprintf(rule->counterstr, 256, "[0:0]");
chain->max_rules++;
}
if (counterstr && strlen(counterstr)) {
snprintf(rule->counterstr, 256, "%s", counterstr);
}
chain->ruleorder++;
if (order == IPT_ORDER) {
rule->order = chain->ruleorder;
} else if (order == IPT_NO_ORDER) {
rule->order = INT_MAX;
} else {
LOGERROR("BUG: invalid ordering mode passed to routine\n");
}
rule->flushed = 0;
return (ret);
}
/**
* Update reference conts.
*
* @param ipth [in] pointer to the IP table handler structure
*
* @return Always returns 0
*/
int ipt_handler_update_refcounts(ipt_handler *ipth) {
char *jumpptr = NULL, jumpchain[64], tmp[64];
int i, j, k;
ipt_table *table = NULL;
ipt_chain *chain = NULL, *refchain = NULL;
ipt_rule *rule = NULL;
for (i = 0; i < ipth->max_tables; i++) {
table = &(ipth->tables[i]);
for (j = 0; j < table->max_chains; j++) {
chain = &(table->chains[j]);
for (k = 0; k < chain->max_rules; k++) {
rule = &(chain->rules[k]);
if (0 == rule->flushed) {
jumpptr = strstr(rule->iptrule, "-j");
if (jumpptr) {
jumpchain[0] = '\0';
sscanf(jumpptr, "%[-j] %s", tmp, jumpchain);
if (strlen(jumpchain)) {
refchain = ipt_table_find_chain(ipth, table->name, jumpchain);
if (refchain) {
LOGDEBUG("FOUND REF TO CHAIN (name=%s sourcechain=%s jumpchain=%s currref=%d) (rule=%s)\n",
refchain->name, chain->name, jumpchain, refchain->ref_count, rule->iptrule);
refchain->ref_count++;
}
}
}
}
}
}
}
return (0);
}
/**
* Searches for table named findtable.
*
* @param ipth [in] pointer to the IP table handler structure
* @param findtable [in] a string pointer to the name of the table we're looking for
*
* @return a pointer to the IP table structure if found. Otherwise, NULL is returned
*/
ipt_table *ipt_handler_find_table(ipt_handler *ipth, const char *findtable)
{
int i, tableidx = 0, found = 0;
if (!ipth || !findtable || !ipth->init) {
return (NULL);
}
found = 0;
for (i = 0; i < ipth->max_tables && !found; i++) {
tableidx = i;
if (!strcmp(ipth->tables[i].name, findtable))
found++;
}
if (!found) {
return (NULL);
}
return (&(ipth->tables[tableidx]));
}
/**
* Searches for chain named findchain in table tablename.
*
* @param ipth [in] pointer to the IP table handler structure
* @param tablename [in] a string pointer to the table name
* @param findchain [in] a string pointer to the chain name we're looking for
*
* @return a pointer to the IP table chain structure if found. Otherwise, NULL is returned
*/
ipt_chain *ipt_table_find_chain(ipt_handler *ipth, const char *tablename, const char *findchain) {
int i, found = 0, chainidx = 0;
ipt_table *table = NULL;
if (!ipth || !tablename || !findchain || !ipth->init) {
return (NULL);
}
table = ipt_handler_find_table(ipth, tablename);
if (!table) {
return (NULL);
}
found = 0;
for (i = 0; i < table->max_chains && !found; i++) {
chainidx = i;
if (!strcmp(table->chains[i].name, findchain))
found++;
}
if (!found) {
return (NULL);
}
return (&(table->chains[chainidx]));
}
/**
* Finds a given IPT chain in a given IPT and set its default policy.
*
* @param pIpth [in] pointer to the IP table handler structure
* @param tablename [in] a constant string pointer to the name of the table (e.g. 'filter', 'nat', 'mangle', etc.)
* @param chainname [in] a constant string pointer to the name of the chain (e.g. 'INPUT', 'FORWARD', etc.)
* @param policyname [in] a constant string pointer to the policy name (e.g. 'ACCEPT', 'DROP')
*
* @return 0 on success or 1 if any failure occurred
*
* @pre \li All pointers and strings must not be NULL
* \li The referred table and chain must exists
*
* @post On success the chain default policy has been changed. On failure, the original policy
* will remain.
*/
int ipt_table_set_chain_policy(ipt_handler * pIpth, const char *tablename, const char *chainname, const char *policyname)
{
ipt_table *pTable = NULL;
ipt_chain *pChain = NULL;
if (!pIpth || !tablename || !chainname || !pIpth->init) {
return (1);
}
if ((pTable = ipt_handler_find_table(pIpth, tablename)) == NULL) {
return (1);
}
if ((pChain = ipt_table_find_chain(pIpth, tablename, chainname)) != NULL) {
snprintf(pChain->policyname, 64, "%s", policyname);
}
return (0);
}
/**
* Searches for rule findrule in chain chainname in table tablename.
*
* @param ipth [in] pointer to the IP table handler structure
* @param tablename [in] a string pointer to the table name
* @param chainname [in] a string pointer to the chain name
* @param findrule [in] a string pointer to the rule we're looking for
*
* @return a pointer to the IP table rule structure if found. Otherwise, NULL is returned
*/
ipt_rule *ipt_chain_find_rule(ipt_handler *ipth, char *tablename, char *chainname, char *findrule) {
int i, found = 0, ruleidx = 0;
ipt_chain *chain;
if (!ipth || !tablename || !chainname || !findrule || !ipth->init) {
return (NULL);
}
chain = ipt_table_find_chain(ipth, tablename, chainname);
if (!chain) {
return (NULL);
}
for (i = 0; i < chain->max_rules && !found; i++) {
ruleidx = i;
if (!strcmp(chain->rules[i].iptrule, findrule))
found++;
}
if (!found) {
return (NULL);
}
return (&(chain->rules[ruleidx]));
}
/**
* Remove all empty chains from table tablename.
*
* @param ipth [in] pointer to the IP table handler structure
* @param tablename [in] a string pointer to the table name
*
* @return 0 on success or 1 if any failure occurred
*
* @see
*
* @pre
*
* @post
*
* @note
*/
int ipt_table_deletechainempty(ipt_handler *ipth, char *tablename) {
int i, found = 0;
ipt_table *table = NULL;
if (!ipth || !tablename || !ipth->init) {
return (1);
}
table = ipt_handler_find_table(ipth, tablename);
if (!table) {
return (1);
}
found = 0;
for (i = 0; i < table->max_chains && !found; i++) {
if (table->chains[i].max_rules == 0) {
ipt_table_deletechainmatch(ipth, tablename, table->chains[i].name);
found++;
}
}
if (!found) {
return (1);
}
return (0);
}
/**
* Remove all chains that partially match chainmatch from table tablename.
*
* @param ipth [in] pointer to the IP table handler structure
* @param tablename [in] a string pointer to the table name
* @param chainmatch [in] a string pointer to the list of characters to match
*
* @return 0 on success or 1 if any failure occurred
*/
int ipt_table_deletechainmatch(ipt_handler *ipth, char *tablename, char *chainmatch) {
int i = 0;
ipt_table *table = NULL;
ipt_chain *chain = NULL;
if (!ipth || !tablename || !chainmatch || !ipth->init) {
return (1);
}
table = ipt_handler_find_table(ipth, tablename);
if (!table) {
return (1);
}
for (i = 0; i < table->max_chains; i++) {
if (strstr(table->chains[i].name, chainmatch)) {
chain = &(table->chains[i]);
ipt_chain_flush(ipth, tablename, chain->name);
chain->flushed = 1;
}
}
return (0);
}
/**
* Mark all rules in the chain chain name of table tablename as flushed.
*
* @param ipth [in] pointer to the IP table handler structure
* @param tablename [in] a string pointer to the table name
* @param chainname [in] a string pointer to the chain name
*
* @return 0 on success or 1 if any failure occurred
*/
int ipt_chain_flush(ipt_handler *ipth, char *tablename, char *chainname) {
int i;
ipt_table *table = NULL;
ipt_chain *chain = NULL;
if (!ipth || !tablename || !chainname || !ipth->init) {
return (1);
}
table = ipt_handler_find_table(ipth, tablename);
if (!table) {
return (1);
}
chain = ipt_table_find_chain(ipth, tablename, chainname);
if (!chain) {
return (1);
}
for (i = 0; i < chain->max_rules; i++) {
chain->rules[i].flushed = 1;
chain->rules[i].order = 0;
}
chain->ruleorder = 0;
return (0);
}
/**
* Flushes/removes the rule of interest from a chain.
*
* @param ipth [in] pointer to the IP table handler structure
* @param tablename [in] a string pointer to the table name
* @param chainname [in] a string pointer to the chain name
* @param findrule [in] a string pointer to the rule we're looking for
*
* @return 0 on success or 1 if any failure occurred
*/
int ipt_chain_flush_rule(ipt_handler *ipth, char *tablename, char *chainname, char *findrule) {
int i, found = 0;
ipt_chain *chain;
if (!ipth || !tablename || !chainname || !findrule || !ipth->init) {
return (EUCA_INVALID_ERROR);
}
chain = ipt_table_find_chain(ipth, tablename, chainname);
if (!chain) {
return (EUCA_INVALID_ERROR);
}
for (i = 0; i < chain->max_rules && !found; i++) {
if (!strcmp(chain->rules[i].iptrule, findrule)) {
found++;
chain->rules[i].flushed = 1;
chain->rules[i].order = 0;
}
}
if (!found) {
return (EUCA_NOT_FOUND_ERROR);
}
return (EUCA_OK);
}
/**
* Releases resources allocated for ipth and re-initializes ipth.
*
* @param ipth [in] pointer to the IP table handler structure
*
* @return 0 on success or 1 if any failure occurred
*/
int ipt_handler_free(ipt_handler *ipth) {
int i = 0;
int j = 0;
char saved_cmdprefix[EUCA_MAX_PATH] = "";
char saved_preloadPath[EUCA_MAX_PATH] = "";
if (!ipth || !ipth->init) {
return (1);
}
snprintf(saved_cmdprefix, EUCA_MAX_PATH, "%s", ipth->cmdprefix);
snprintf(saved_preloadPath, EUCA_MAX_PATH, "%s", ipth->preloadPath);
for (i = 0; i < ipth->max_tables; i++) {
for (j = 0; j < ipth->tables[i].max_chains; j++) {
EUCA_FREE(ipth->tables[i].chains[j].rules);
}
EUCA_FREE(ipth->tables[i].chains);
}
EUCA_FREE(ipth->tables);
return (ipt_handler_init(ipth, saved_cmdprefix, saved_preloadPath));
}
/**
* Release all resources associated with the given ipt_handler.
*
* @param ipth [in] pointer to the IP table handler structure
*
* @return 0 on success or 1 if any failure occurred
*/
int ipt_handler_close(ipt_handler *ipth) {
int i = 0;
int j = 0;
if (!ipth || !ipth->init) {
LOGDEBUG("Invalid argument. NULL or uninitialized ipt_handler.\n");
return (1);
}