This repository has been archived by the owner on Apr 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfirewall.c
1207 lines (939 loc) · 28.2 KB
/
firewall.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
///////////////////////////////////////////////////////////////////////////////
//
// ClearOS Firewall Engine
// Copyright (C) 2005-2008 Point Clark Networks
// Copyright (C) 2009-2016 ClearFoundation
//
///////////////////////////////////////////////////////////////////////////////
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this program; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
///////////////////////////////////////////////////////////////////////////////
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/time.h>
#include <linux/types.h>
#include <unistd.h>
#include <getopt.h>
#include <assert.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <syslog.h>
#include <signal.h>
#include <dirent.h>
#ifndef FIREWALL_IPV6
#include <iptables.h>
#define do_command do_command4
#define FIREWALL_NFPROTO NFPROTO_IPV4
#else
#include <ip6tables.h>
#define do_command do_command6
#define iptables_globals ip6tables_globals
#define iptc_builtin ip6tc_builtin
#define iptc_commit ip6tc_commit
#define iptc_create_chain ip6tc_create_chain
#define iptc_delete_chain ip6tc_delete_chain
#define iptc_first_chain ip6tc_first_chain
#define iptc_flush_entries ip6tc_flush_entries
#define iptc_free ip6tc_free
#define iptc_init ip6tc_init
#define iptc_next_chain ip6tc_next_chain
#define iptc_set_policy ip6tc_set_policy
#define iptc_strerror ip6tc_strerror
#define ipt_chainlabel ip6t_chainlabel
#define ipt_counters ip6t_counters
#define FIREWALL_NFPROTO NFPROTO_IPV6
#endif
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include "ifconfig.h"
// Debug, pretend, and strict mode flags
static int debug = 0;
static int pretend = 0;
static int strict = 0;
// Number of seconds to wait for a successful commit
static int wait_timeout = 0;
// Global return code, set by eprintf()
static int grc = 0;
// Network interface context handle
static if_ctx *IFC = NULL;
// Lua context handle
static lua_State *LUA = NULL;
// Iptables command argument count and array
static int iptc_argc = 0;
static char *iptc_argv[255];
// Pointer to source code (firewall.lua) filename
static char *source = NULL;
// Display or log an error. If strict mode is enabled,
// an error will terminate further Lua execution.
static int eprintf(const char *format, ...)
{
int rc = 0;
va_list ap;
char buffer[256];
grc = 1;
va_start(ap, format);
vsnprintf(buffer, sizeof(buffer), format, ap);
if(strict)
rc = luaL_error(LUA, buffer);
else
{
lua_Debug lD;
char prefix[256];
lua_getstack(LUA, 1, &lD);
lua_getinfo(LUA, "l", &lD);
snprintf(prefix, sizeof(prefix),
"Error: %s:%d: %s", source, lD.currentline, buffer);
syslog(LOG_ERR, prefix);
}
va_end(ap);
return rc;
}
// Add argument to iptables command line
static int argv_add(const char *arg)
{
if(arg && (iptc_argc + 1) < sizeof(iptc_argv) / sizeof(char *))
{
iptc_argv[iptc_argc] = strdup(arg);
iptc_argc++;
return 1;
}
return 0;
}
// Free iptables command line array
static void argv_free(void)
{
int i;
for(i = 0; i < iptc_argc; i++) free(iptc_argv[i]);
iptc_argc = 0;
}
// Pre-parse an iptables command line string.
// Ripped from iptables-restore.c with a few modifications.
static int argv_parse(const char *table, char *buffer)
{
char *c;
int quote = 0;
char *parse_start = buffer;
char *param_start = buffer;
argv_free();
argv_add(iptables_globals.program_name);
argv_add("-t");
argv_add(table);
for(c = parse_start; *c; c++)
{
if(*c == '"')
{
// Quote cannot be true if there was no previous character.
// Thus, c - 1 has to be within bounds.
if(quote && *(c - 1) != '\\')
{
quote = 0; *c = ' ';
}
else
{
quote = 1; param_start++;
}
}
if(*c == ' ' || !*(c + 1))
{
char param_buffer[1024];
int param_len = c - param_start;
if(quote) continue;
if(!*(c + 1)) param_len++;
if(!param_len)
{
// two spaces?
param_start++;
continue;
}
// end of one parameter
strncpy(param_buffer, param_start, param_len);
*(param_buffer + param_len) = '\0';
argv_add(param_buffer);
param_start += param_len + 1;
}
}
return 0;
}
// An iptables table name and context handle.
struct table_t
{
char *name;
#ifndef FIREWALL_IPV6
struct iptc_handle *handle;
#else
struct ip6tc_handle *handle;
#endif
};
// Global list of 'built-in' iptables tables (filter, mangle, nat).
static struct table_t *tables = NULL;
// Look-up iptables table name, returns -1 if not found.
static int find_table(const char *name)
{
int i;
for(i = 0; tables[i].name; i++)
if(!strncmp(tables[i].name, name, 32)) return i;
return -1;
}
// All exported Lua functions start with __lua. See main() below for their
// name mappings into Lua.
// Binary AND operator.
// Lua doesn't have one of these nor binary OR.
static int __lua_b_and(lua_State *L)
{
double a = luaL_checknumber(L, 1);
double b = luaL_checknumber(L, 2);
lua_pushnumber(L, ((unsigned)a & (unsigned)b));
return 1;
}
// Binary OR operator.
static int __lua_b_or(lua_State *L)
{
double a = luaL_checknumber(L, 1);
double b = luaL_checknumber(L, 2);
lua_pushnumber(L, ((unsigned)a | (unsigned)b));
return 1;
}
// Convert string IP address (x.x.x.x) to binary IP address in host byte order.
static int __lua_iptc_ip2bin(lua_State *L)
{
struct in_addr addr;
const char *ip = luaL_checkstring(L, 1);
if(inet_aton(ip, &addr) == 0)
return eprintf("Invalid IP address: %s", ip);
lua_pushnumber(L, ntohl(addr.s_addr));
return 1;
}
// Convert binary IP address to string IP address.
static int __lua_iptc_bin2ip(lua_State *L)
{
struct in_addr addr;
double bin = luaL_checknumber(L, 1);
addr.s_addr = htonl(bin);
lua_pushstring(L, inet_ntoa(addr));
return 1;
}
// Return the corresponding string that matches the numeric protocol (if any).
static int __lua_p_name(lua_State *L)
{
struct protoent *pe;
const char *proto = luaL_checkstring(L, 1);
if((pe = getprotobynumber(atoi(proto))))
lua_pushstring(L, pe->p_name);
else
lua_pushstring(L, proto);
return 1;
}
// Get host by name.
static int __lua_gethostbyname(lua_State *L)
{
struct sockaddr_in sa;
const char *host = luaL_checkstring(L, 1);
sa.sin_addr.s_addr = inet_addr(host);
if(sa.sin_addr.s_addr == INADDR_NONE)
{
struct hostent *he;
if((he = gethostbyname(host)))
{
memcpy(&sa.sin_addr, he->h_addr, he->h_length);
lua_pushstring(L, inet_ntoa(sa.sin_addr));
}
else lua_pushnil(L);
}
else lua_pushstring(L, inet_ntoa(sa.sin_addr));
return 1;
}
// Get address info.
static int __lua_getaddrinfo(lua_State *L)
{
int i = 0, rc;
struct addrinfo hints, *result, *rp;
const char *hostaddr = luaL_checkstring(L, 1);
lua_newtable(L);
memset(&hints, 0, sizeof(struct addrinfo));
#ifndef FIREWALL_IPV6
hints.ai_family = AF_INET;
#else
hints.ai_family = AF_UNSPEC;
#endif
hints.ai_socktype = SOCK_STREAM;
if ((rc = getaddrinfo(hostaddr,
NULL, &hints, &result)) != 0) {
syslog(LOG_ERR, "Unable to resolve: %s: %s",
hostaddr, gai_strerror(rc));
return 1;
}
for (rp = result; rp != NULL; rp = rp->ai_next) {
char *addr = NULL;
char dst4[INET_ADDRSTRLEN];
char dst6[INET6_ADDRSTRLEN];
if (rp->ai_family == AF_INET) {
struct sockaddr_in *sa_in;
sa_in = (struct sockaddr_in *)rp->ai_addr;
if (inet_ntop(rp->ai_family,
(const void *)&sa_in->sin_addr, dst4, INET_ADDRSTRLEN) != NULL)
addr = dst4;
}
else if (rp->ai_family == AF_INET6) {
struct sockaddr_in6 *sa_in;
sa_in = (struct sockaddr_in6 *)rp->ai_addr;
if (inet_ntop(rp->ai_family,
(const void *)&sa_in->sin6_addr, dst6, INET_ADDRSTRLEN) != NULL)
addr = dst6;
}
if (addr == NULL) continue;
lua_pushnumber(L, ++i);
lua_pushstring(L, addr);
lua_settable(L, -3);
}
freeaddrinfo(result);
return 1;
}
// Informational message 'echo'.
static int __lua_echo(lua_State *L)
{
const char *text = luaL_checkstring(L, 1);
syslog(LOG_NOTICE, text);
return 0;
}
// Debug message 'echo'.
static int __lua_debug(lua_State *L)
{
const char *text = luaL_checkstring(L, 1);
if(!debug) return 0;
syslog(LOG_DEBUG, text);
return 0;
}
// Execute a system command.
static int __lua_execute(lua_State *L)
{
FILE *h;
int rc = 0;
const char *command = luaL_checkstring(L, 1);
if(!pretend && (h = popen(command, "r"))) rc = pclose(h);
if(pretend) syslog(LOG_DEBUG, command);
else if(debug) syslog(LOG_DEBUG, "%s = %d", command, rc);
lua_pushnumber(L, rc);
return 1;
}
// Forward declaration for the directory iterator function
static int __lua_dir_iter(lua_State *L);
static int __lua_dir(lua_State *L)
{
const char *path = luaL_checkstring(L, 1);
// Create a userdatum to store a DIR address
DIR **d = (DIR **)lua_newuserdata(L, sizeof(DIR *));
// Set its metatable
luaL_getmetatable(L, "LuaBook.dir");
lua_setmetatable(L, -2);
// Try to open the given directory
*d = opendir(path);
if (*d == NULL)
luaL_error(L, "Cannot open directory %s: %s", path, strerror(errno));
// Creates and returns the iterator function
// (its sole upvalue, the directory userdatum,
// is already on the stack top.
lua_pushcclosure(L, __lua_dir_iter, 1);
return 1;
}
static int __lua_dir_iter(lua_State *L)
{
DIR *d = *(DIR **)lua_touserdata(L, lua_upvalueindex(1));
struct dirent *entry;
if ((entry = readdir(d)) != NULL) {
lua_pushstring(L, entry->d_name);
return 1;
}
// No more values to return
return 0;
}
static int __lua_dir_gc(lua_State *L)
{
DIR *d = *(DIR **)lua_touserdata(L, 1);
if (d) closedir(d);
return 0;
}
// Initialize built-in iptable tables (filter, mangle, nat).
static int __lua_iptc_init(lua_State *L)
{
int i;
if(!xtables_lock(wait_timeout))
return eprintf("Unable to acquire xtables lock.");
if(!tables)
{
tables = calloc(4, sizeof(struct table_t));
tables[0].name = strdup("filter");
tables[1].name = strdup("mangle");
tables[2].name = strdup("nat");
}
for(i = 0; tables[i].name; i++)
{
if(tables[i].handle) continue;
tables[i].handle = iptc_init(tables[i].name);
if(!tables[i].handle)
{
return eprintf("Unable to initialize table: %s: %s.",
tables[i].name, iptc_strerror(errno));
}
}
return 0;
}
// Create a new iptables chain.
static int __lua_iptc_create_chain(lua_State *L)
{
int i, r;
const char *table = luaL_checkstring(L, 1);
const char *chain = luaL_checkstring(L, 2);
i = find_table(table);
if(i == -1)
return eprintf("Invalid table: %s", table);
if(!tables[i].handle)
return eprintf("Invalid table: %s", table);
r = iptc_create_chain(chain, tables[i].handle);
if(!r) return eprintf("Unable to create chain: %s: %s", table, chain);
lua_pushnumber(L, (r) ? 0 : -1);
return 1;
}
// Delete existing iptables chain.
static int __lua_iptc_delete_chain(lua_State *L)
{
int i, r;
const char *table = luaL_checkstring(L, 1);
const char *chain = luaL_checkstring(L, 2);
i = find_table(table);
if(i == -1)
return eprintf("Invalid table: %s", table);
if(!tables[i].handle)
return eprintf("Invalid table: %s", table);
r = iptc_delete_chain(chain, tables[i].handle);
if(!r) return eprintf("Unable to delete chain: %s: %s", table, chain);
lua_pushnumber(L, 0);
return 1;
}
// Delete all rules from existing iptables chain (flush).
static int __lua_iptc_flush_chain(lua_State *L)
{
int i;
const char *table = luaL_checkstring(L, 1);
const char *chain = luaL_checkstring(L, 2);
i = find_table(table);
if(i == -1)
return eprintf("Invalid table: %s", table);
if(!tables[i].handle)
return eprintf("Invalid table: %s", table);
iptc_flush_entries(chain, tables[i].handle);
return 0;
}
// Recursive delete of all iptables chains.
static int __lua_iptc_flush_all_chains(lua_State *L)
{
int i, r;
const char *chain;
const char *table = luaL_checkstring(L, 1);
i = find_table(table);
if(i == -1)
return eprintf("Invalid table: %s", table);
if(!tables[i].handle)
return eprintf("Invalid table: %s", table);
chain = iptc_first_chain(tables[i].handle);
while(chain)
{
r = iptc_flush_entries(chain, tables[i].handle);
if(!r) return eprintf("Unable to flush chain: %s: %s", table, chain);
chain = iptc_next_chain(tables[i].handle);
}
return 0;
}
// Commit table changes.
static int __lua_iptc_commit(lua_State *L)
{
int i, r;
const char *table = luaL_checkstring(L, 1);
i = find_table(table);
if(i == -1)
return eprintf("Invalid table: %s", table);
if(!tables[i].handle)
return eprintf("Invalid table: %s", table);
if(pretend) return 0;
r = iptc_commit(tables[i].handle);
tables[i].handle = NULL;
if(!r)
{
return eprintf("Unable to commit: %s: %s.",
table, iptc_strerror(errno));
}
return 0;
}
// Iptables command. Essentially the same as the command line version.
static int __lua_iptables(lua_State *L)
{
int i, r;
char *rule_copy = NULL;
const char *table = luaL_checkstring(L, 1);
const char *rule = luaL_checkstring(L, 2);
i = find_table(table);
if(i == -1)
return eprintf("Invalid table: %s", table);
if(!tables[i].handle)
return eprintf("Invalid table: %s", table);
if(debug) {
#ifndef FIREWALL_IPV6
syslog(LOG_DEBUG, "iptables -t %s %s", table, rule);
#else
syslog(LOG_DEBUG, "ip6tables -t %s %s", table, rule);
#endif
}
rule_copy = strdup(rule);
argv_parse(table, rule_copy);
r = do_command(iptc_argc, iptc_argv, &iptc_argv[2], &tables[i].handle, false);
if(!r) {
#ifndef FIREWALL_IPV6
return eprintf("iptables -t %s %s", table, rule);
#else
return eprintf("ip6tables -t %s %s", table, rule);
#endif
}
argv_free();
free(rule_copy);
return 0;
}
// Set default policy for an iptables table.
static int __lua_iptc_set_policy(lua_State *L)
{
int i, r;
struct ipt_counters counters;
const char *table = luaL_checkstring(L, 1);
const char *chain = luaL_checkstring(L, 2);
const char *policy = luaL_checkstring(L, 3);
i = find_table(table);
if(i == -1)
return eprintf("Invalid table: %s", table);
if(!tables[i].handle)
return eprintf("Invalid table: %s", table);
memset(&counters, 0, sizeof(struct ipt_counters));
r = iptc_set_policy(chain, policy, &counters, tables[i].handle);
if(!r)
{
return eprintf("Unable to set policy to %s: %s: %s",
policy, table, chain);
}
return 0;
}
// Delete all user-defined iptables chains.
static int __lua_iptc_delete_user_chains(lua_State *L)
{
int i;
const char *chain;
const char *table = luaL_checkstring(L, 1);
char *chains;
unsigned int c, count = 0;
i = find_table(table);
if(i == -1)
return eprintf("Invalid table: %s", table);
if(!tables[i].handle)
return eprintf("Invalid table: %s", table);
chain = iptc_first_chain(tables[i].handle);
while(chain)
{
count++;
chain = iptc_next_chain(tables[i].handle);
}
chains = malloc(sizeof(ipt_chainlabel) * count);
c = 0;
chain = iptc_first_chain(tables[i].handle);
while(chain)
{
strcpy(chains + c * sizeof(ipt_chainlabel), chain);
c++;
chain = iptc_next_chain(tables[i].handle);
}
for(c = 0; c < count; c++)
{
if(iptc_builtin(chains + c * sizeof(ipt_chainlabel), tables[i].handle))
continue;
iptc_delete_chain(chains + c * sizeof(ipt_chainlabel), tables[i].handle);
}
free(chains);
return 0;
}
// Return true if interface exists.
static int __lua_if_exists(lua_State *L)
{
int rc;
const char *ifn = luaL_checkstring(L, 1);
rc = if_exists(IFC, ifn);
lua_pushboolean(L, (rc == 0) ? 1 : 0);
return 1;
}
// Return list of all network interfaces.
static int __lua_if_list(lua_State *L)
{
int i, count;
lua_newtable(L);
count = if_list(IFC);
if (count < 0) eprintf(IFC->last_error);
for(i = 0; i < count; i++)
{
lua_pushnumber(L, i + 1);
lua_pushstring(L, IFC->interfaces[i]);
lua_settable(L, -3);
}
return 1;
}
// Return list of all PPPoE interfaces.
static int __lua_if_list_pppoe(lua_State *L)
{
int i, count;
lua_newtable(L);
count = if_list_pppoe(IFC);
if (count < 0) eprintf(IFC->last_error);
for(i = 0; i < count; i++)
{
lua_pushnumber(L, i + 1);
lua_pushstring(L, IFC->pppoe[i]);
lua_settable(L, -3);
}
return 1;
}
// Return IP address of selected network interface.
static int __lua_if_address(lua_State *L)
{
char *buffer;
size_t size = 32;
const char *ifn = luaL_checkstring(L, 1);
buffer = calloc(size, sizeof(char));
if(if_get_address(IFC, ifn, buffer, size) == -1)
{
// XXX: No longer is this a fatal error... The firewall should
// be able to trap this...
syslog(LOG_DEBUG, IFC->last_error);
free(buffer);
return 0;
}
lua_pushstring(L, buffer);
free(buffer);
return 1;
}
// Return destination IP address of selected network interface (PPP).
static int __lua_if_dst_address(lua_State *L)
{
char *buffer;
size_t size = 32;
const char *ifn = luaL_checkstring(L, 1);
buffer = calloc(size, sizeof(char));
if(if_get_dst_address(IFC, ifn, buffer, size) == -1)
{
free(buffer);
return eprintf(IFC->last_error);
}
lua_pushstring(L, buffer);
free(buffer);
return 1;
}
// Return netmask of selected network interface.
static int __lua_if_netmask(lua_State *L)
{
char *buffer;
size_t size = 32;
const char *ifn = luaL_checkstring(L, 1);
buffer = calloc(size, sizeof(char));
if(if_get_netmask(IFC, ifn, buffer, size) == -1)
{
free(buffer);
return eprintf(IFC->last_error);
}
lua_pushstring(L, buffer);
free(buffer);
return 1;
}
// Calculate network address from IP address and netmask.
static int __lua_if_network(lua_State *L)
{
char *buffer;
size_t size = 32;
const char *ip = luaL_checkstring(L, 1);
const char *mask = luaL_checkstring(L, 2);
buffer = calloc(size, sizeof(char));
if(if_get_network(IFC, ip, mask, buffer, size) == -1)
{
free(buffer);
return eprintf(IFC->last_error);
}
lua_pushstring(L, buffer);
free(buffer);
return 1;
}
// Calculate network prefix from IP address and netmask.
static int __lua_if_prefix(lua_State *L)
{
char *buffer;
size_t size = 4;
const char *mask = luaL_checkstring(L, 1);
buffer = calloc(size, sizeof(char));
if(if_get_prefix(IFC, mask, buffer, size) == -1)
{
free(buffer);
return eprintf(IFC->last_error);
}
lua_pushstring(L, buffer);
free(buffer);
return 1;
}
// Return true if network interface is 'up'.
static int __lua_if_isup(lua_State *L)
{
short flags;
const char *ifn = luaL_checkstring(L, 1);
if(if_get_flags(IFC, ifn, &flags) == -1)
return eprintf(IFC->last_error);
lua_pushboolean(L, (flags & IFF_UP));
return 1;
}
// Return true if interface is a PPP device.
static int __lua_if_isppp(lua_State *L)
{
int ppp;
const char *ifn = luaL_checkstring(L, 1);
if((ppp = if_isppp(IFC, ifn)) == -1)
return eprintf(IFC->last_error);
lua_pushboolean(L, ppp);
return 1;
}
// Clean-up upon shutdown.
static void exit_handler(void)
{
int i;
if(tables)
{
for(i = 0; tables[i].name; i++)
{
free(tables[i].name);
if(tables[i].handle)
iptc_free(tables[i].handle);
}
free(tables);
}
argv_free();
if(IFC) if_free(IFC);
if(LUA) lua_close(LUA);
}
// Command line arguments.
static struct option options[] =
{
{ "help", 0, 0, 'h' },
{ "version", 0, 0, 'V' },
{ "debug", 0, 0, 'd'},
{ "pretend", 0, 0, 'p'},
{ "strict", 0, 0, 's'},
{ "wait", 2, 0, 'w'},
{ 0 }
};
// Fire it up!
int main(int argc, char *argv[])
{
int c;
float seconds;
struct timeval tva, tvb;
#ifndef FIREWALL_IPV6
iptables_globals.program_name = "firewall";
#else
iptables_globals.program_name = "firewall6";
#endif
if (xtables_init_all(&iptables_globals, FIREWALL_NFPROTO) < 0) {
fprintf(stderr, "%s: Failed to initialize xtables\n", argv[0]);
exit(1);
}
#if defined(ALL_INCLUSIVE) || defined(NO_SHARED_LIBS)
init_extensions();
#endif
// Command-line processing
while((c = getopt_long(argc, argv, "hVdpsw:", options, NULL)) != -1)
{
switch(c)
{
case 'p':
pretend = 1;
// fall through to enable debug also...
case 'd':
debug = 1;
break;
case 's':
strict = 1;