1{
2# =============================
3# ===== Parsing Rule Body =====
4# =============================
5tcpflagstat	= "0";
6icmptypestat	= "0";
7NEW		= "0";
8ESTABLISHED	= "0";
9RELATED		= "0";
10INVALID		= "0";
11level		= "0";
12tcp_seq		= "0";
13tcp_opt		= "0";
14ip_opt		= "0";
15prefix		= "0";
16kuralno		= $1;
17islem		= $4;
18if (length($5)>4) {
19	proto	= substr($5,1,length($5)-2);
20	opt	= substr($5,length($5)-1,length($5));
21	position= 5;
22} else {
23	proto	= $5;
24	opt	= $6;
25	position= 6;
26}
27if_in		= $(position+1);
28if_out		= $(position+2);
29srcaddr		= $(position+3);
30dstaddr		= $(position+4);
31
32if (opt == "-f") frag = "1"
33else frag = "0"
34
35if (proto == "tcp" || proto == "udp" || proto == "ddp") {
36        if ($(position+6) ~ /^spt:/ || $(position+6) ~ /^spts:/) {
37                sub(/spt:/,"",$(position+6));
38                sub(/spts:/,"",$(position+6));
39                srcport = $(position+6);
40		srcportstat = "1";
41                $(position+6)="";
42                $(position+5)="";
43        } else {
44		srcportstat = "0";
45		srcport = " ";
46	}
47        if ($(position+6) ~ /^dpt:/ || $(position+6) ~ /^dpts:/) {
48                sub(/dpt:/,"",$(position+6));
49                sub(/dpts:/,"",$(position+6));
50                dstport=$(position+6);
51		dstportstat = "1";
52                $(position+6)="";
53                $(position+5)="";
54        } else if ($(position+7) ~ /^dpt:/ || $(position+7) ~ /^dpts:/) {
55                sub(/dpt:/,"",$(position+7));
56                sub(/dpts:/,"",$(position+7));
57                dstport=$(position+7);
58		dstportstat = "1";
59                $(position+7)="";
60                $(position+5)="";
61        } else {
62		dstportstat = "0";
63		dstport = " ";
64	}
65} else {
66	srcportstat = "0";
67	srcport = " ";
68	dstportstat = "0";
69	dstport = " ";
70}
71
72
73#===================
74#== Parse Options ==
75#===================
76
77for (i=(position+5) ; i<=NF; i++) {
78	if ($i == "state") {
79		i++;
80		if (index($i,"NEW")!=0) NEW="1";
81		if (index($i,"ESTABLISHED")!=0) ESTABLISHED="1";
82		if (index($i,"RELATED")!=0) RELATED="1";
83		if (index($i,"INVALID")!=0) INVALID="1";
84	}
85        if ($i == "LOG") {
86		i++;
87		if (index($i,"level")!=0) {
88			i++;
89			level=toupper($i);
90			i++;
91		}
92		if (index($i,"tcp-sequence")!=0) {
93			i++;
94			tcp_seq="1";
95		}
96		if (index($i,"tcp-options")!=0) {
97			i++;
98			tcp_opt="1";
99		}
100		if (index($i,"ip-options")!=0) {
101			i++;
102			ip_opt="1";
103		}
104		if (index($i,"prefix")!=0) {
105			i++;
106			gsub(/'|`/,"",$i);
107			prefix=$i;
108			i++;
109		}
110	}
111	if ($i == "reject-with") {
112		i++;
113		print "locate_select_index('reject_with','"$i"')";
114		i++;
115	}
116	if ($i == "icmp" && proto == "icmp") {
117		icmptypestat = "1";
118		i++;
119		icmp_type = $i;
120		i++;
121	}
122};
123
124#=================================
125#== Send The Values Of The Form ==
126#=================================
127print "function locate_select_index(selectid, s_value) {";
128print "for (i = 0;i<document.getElementById(selectid).length;i++) {";
129print "\t if (document.getElementById(selectid)[i].value == s_value) {";
130print "\t \t document.getElementById(selectid).selectedIndex = i;";
131print "\t \t break;";
132print "\t }";
133print "}";
134print "}";
135print " ";
136
137if (if_in != "any") {
138	print "locate_select_index('if_in','"if_in"')";
139	print "document.getElementById('if_in_stat').checked=1";
140	print "document.getElementById('if_in').disabled=0";
141}
142if (if_out != "any") {
143	print "locate_select_index('if_out','"if_out"')";
144	print "document.getElementById('if_out_stat').checked=1";
145	print "document.getElementById('if_out').disabled=0";
146}
147print "locate_select_index('chain','"chain"')";
148print "document.getElementById('rule_no').value='"kuralno"';";
149print "locate_select_index('action','"islem"');";
150if (islem == "unreach")
151	print "locate_select_index('unreach_code','"unreach"');";
152
153print "locate_select_index('protocol','"proto"');";
154if (proto == "icmp") {
155	print "document.getElementById('icmp_type_stat').checked="icmptypestat";";
156	if (icmptypestat == "1") {
157		print "document.getElementById('icmp_type').disabled=0;";
158		print "locate_select_index('icmp_type','"icmp_type"');";
159	}
160}
161else if (proto == "udp") {
162	print "document.getElementById('dst_port_stat').checked="dstportstat";";
163	if (dstportstat =="1") {
164		print "document.getElementById('dst_port').disabled=0;";
165		print "document.getElementById('dst_port').value='"dstport"';";
166	}
167}
168else if (proto == "tcp") {
169	print "document.getElementById('tcp_flags_stat').checked="tcpflagstat";";
170	if (tcpflagstat == "1") {
171		print "document.getElementById('tcp_flags').disabled=0;";
172		print "locate_select_index('tcp_flags','"tcpflag"');";
173	}
174	print "document.getElementById('src_port_stat').checked="srcportstat";";
175	if (srcportstat == "1") {
176		print "document.getElementById('src_port').disabled=0;";
177		print "document.getElementById('src_port').value='"srcport"';";
178	}
179	print "document.getElementById('dst_port_stat').checked="dstportstat";";
180	if (dstportstat == "1") {
181		print "document.getElementById('dst_port').disabled=0;";
182		print "document.getElementById('dst_port').value='"dstport"';";
183	}
184}
185print "document.getElementById('NEW').checked="NEW";";
186print "document.getElementById('ESTABLISHED').checked="ESTABLISHED";";
187print "document.getElementById('RELATED').checked="RELATED";";
188print "document.getElementById('INVALID').checked="INVALID";";
189if (level!="0") {
190	print "document.getElementById('log_level_stat').checked=1";
191	print "document.getElementById('log_level').disabled=0";
192	print "locate_select_index('log_level','"level"')";
193	print "document.getElementById('log_tcp_seq').checked="tcp_seq;
194	print "document.getElementById('log_tcp_opt').checked="tcp_opt;
195	print "document.getElementById('log_ip_opt').checked="ip_opt;
196	if (prefix!="0") {
197		print "document.getElementById('log_prefix_stat').checked=1";
198		print "document.getElementById('log_prefix').disabled=0";
199		print "document.getElementById('log_prefix').value='"prefix"'";
200	}
201}
202
203print "document.getElementById('src_addr').value='"srcaddr"';";
204if (srcaddr != "anywhere") {
205	print "document.getElementById('src_addr').disabled=0;";
206	print "document.rulebody.src_addr_stat[1].checked=1;";
207}
208
209print "document.getElementById('dst_addr').value='"dstaddr"';";
210if (dstaddr != "anywhere") {
211	print "document.getElementById('dst_addr').disabled=0;";
212	print "document.rulebody.dst_addr_stat[1].checked=1;";
213}
214
215print "document.getElementById('frag_stat').checked="frag";";
216print "action_check_vis();";
217print "proto_check_vis();";
218}
219