1 |
# Author : Arnaud Desmons <adesmons@mandrakesoft.com> |
# Copyright (C) 2003 Arnaud DESMONS (adesmons@mandrakesoft.com), |
2 |
# |
# Damien KROTKINE (damien@tuxfamily.org), |
3 |
# Copyright (C) 2002 damien KROTKINE (damien@tuxfamily.org) |
# Charles LONGEAU (chl@tuxfamily.org) |
|
# Copyright (C) 2002 Charles LONGEAU (chl@tuxfamily.org) |
|
|
# Copyright (C) 2002 Arnaud Desmons <adesmons@mandrakesoft.com> |
|
4 |
# |
# |
5 |
# This program is free software; you can redistribute it and/or modify |
# This program is free software; you can redistribute it and/or modify |
6 |
# it under the terms of the GNU General Public License as published by |
# it under the terms of the GNU General Public License as published by |
15 |
# You should have received a copy of the GNU General Public License |
# You should have received a copy of the GNU General Public License |
16 |
# along with this program; if not, write to the Free Software |
# along with this program; if not, write to the Free Software |
17 |
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
|
|
|
# Libconf Template : passwd |
|
|
# |
|
|
# This templates is used to parse passwd style configuration files. |
|
|
# There is no section handling. |
|
18 |
# |
# |
19 |
|
# Libconf Template for pf.conf, OpenBSD Packet Filter configuration file. |
20 |
|
# $Author$ |
21 |
|
# $Id$ |
22 |
|
|
23 |
package Libconf; |
package Libconf; |
24 |
|
|
|
|
|
25 |
$templates{pf} = { |
$templates{pf} = { |
26 |
rules => [ |
rules => [q(foreach my $regex ('pass|block|antispoof|scrub', 'in|out', 'log', 'quick', 'keep\s+state', |
27 |
q( |
'all') { |
28 |
foreach my $regex ('proto', 'on') { |
if ($in =~ s/(?<!\S)($regex)(?!\S)//) { |
29 |
if ($in =~ s/\s*$regex\s+(\S*)//) { |
$atom->{type} = 'VALUES_LIST'; |
30 |
$atom->{type} = 'VALUES_LIST'; |
$matched = 1; |
31 |
$atom->{type2} = 'PF_RULE'; |
push @{$atom->{list}}, $1; |
32 |
push @{$atom->{list}}, {$regex => $1}; |
} |
33 |
} |
} |
34 |
|
foreach my $regex ('proto', 'on', 'flags') { |
35 |
|
if ($in =~ s/(?<!\S)$regex\s+(\S*)//) { |
36 |
|
push @{$atom->{list}}, {$regex => $1}; |
37 |
|
} |
38 |
|
} |
39 |
|
foreach my $regex ('from', 'to') { |
40 |
|
my $peer; |
41 |
|
if ($in =~ /(?<!\S)$regex(?!\S)/) { |
42 |
|
if ($in =~ /$regex\s+{([^{}]*)}/) { |
43 |
|
$peer->{host} = [split(/\s*,\s*/, $1)]; |
44 |
|
} |
45 |
|
elsif ($in =~ /$regex\s+(\S+)/) { |
46 |
|
$peer->{host} = $1; |
47 |
|
} |
48 |
|
if ($in =~ s/$regex\s+(\S*|{.*})\s+port\s+{(.*)}//) { |
49 |
|
$peer->{port}{value} = [split(/\s*,\s*/, $2)]; |
50 |
|
$peer->{port}{type} = "LIST"; |
51 |
|
} |
52 |
|
elsif ($in =~ s/$&\s+port\s+(\S+)\s+([<>]+)\s+(\S+)//) { |
53 |
|
$peer->{port}{value} = [$1, $2, $3]; |
54 |
|
} |
55 |
|
$in =~ s/$&//; |
56 |
|
push @{$atom->{list}}, { $regex => $peer}; |
57 |
|
} |
58 |
|
} |
59 |
|
), |
60 |
|
q( if ($in =~ s/^\s*(\w+)\s*=\s*("([^"]*)"|'([^']*)'|[^'"\s]+)\s*$//) { |
61 |
|
$atom->{type} = 'KEY_VALUE'; |
62 |
|
$atom->{type2} = 'PF_EQ'; |
63 |
|
$atom->{key} = $1; |
64 |
|
$atom->{value} = defined $3 ? $3 : $2; |
65 |
|
$matched = 1; |
66 |
|
} |
67 |
|
), |
68 |
|
q( if ($in =~ s/^\s*set\s+(\S+)\s+//) { |
69 |
|
$atom->{type} = 'KEY_VALUE'; |
70 |
|
$atom->{type2} = 'PF_SET'; |
71 |
|
$atom->{key} = $1; |
72 |
|
if ($in =~ s/^(\S+)\s*$//) { |
73 |
|
$atom->{value} = $1; |
74 |
|
} |
75 |
|
elsif ($in =~ s/{?([^{}]+)}?//) { |
76 |
|
foreach (split(/\s*,\s*/, $1)) { |
77 |
|
my $h; |
78 |
|
($h->{key}, $h->{value}) = split; |
79 |
|
push @{$atom->{value}}, $h; |
80 |
|
} |
81 |
|
} |
82 |
|
} |
83 |
|
) |
84 |
|
], |
85 |
|
comments => [ ['#'] ], |
86 |
|
comment_output => q(/^(\s*)$/ ? "$_\n" : "#$_\n"), |
87 |
|
output => { |
88 |
|
KEY_VALUE => q( |
89 |
|
if ($atom->{type2} eq 'PF_SET') { |
90 |
|
if (!ref $atom->{value}) { |
91 |
|
"set ". $atom->{key} ." ". $atom->{value} . "\n" |
92 |
} |
} |
93 |
foreach my $regex ('in|out', 'log', 'pass|block|antispoof|scrub', 'quick', 'keep state', 'all') { |
elsif (ref $atom->{value} eq "ARRAY") { |
94 |
if ($in =~ s/\s*($regex)//) { |
my $out; |
95 |
$atom->{type} = 'VALUES_LIST'; |
foreach (@{$atom->{value}}) { |
96 |
$atom->{type2} = 'PF_RULE'; |
$out.= "$_->{key} $_->{value},"; |
97 |
push @{$atom->{list}}, $1; |
} |
98 |
} |
chop $out; |
99 |
|
if (@{$atom->{value}} > 1) { |
100 |
|
return "set $atom->{key} {$out}\n"; |
101 |
|
} |
102 |
|
"set $atom->{key} $out\n"; |
103 |
} |
} |
104 |
foreach my $regex ('from', 'to') { |
} |
105 |
$atom->{type} = 'VALUES_LIST'; |
elsif ($atom->{type2} eq 'PF_EQ') { |
106 |
$atom->{type2} = 'PF_RULE'; |
$atom->{key} ."=". $atom->{value} ."\n"; |
107 |
if ($in =~ s/\s*$regex\s+(\S*)\s+port\s+{(.*)}//) { |
} |
108 |
push @{$atom->{list}}, {$regex => $1, port => [split(/\s*,\s*/, $2)]}; |
), |
109 |
} |
VALUES_LIST => q( |
110 |
elsif ($in =~ s/\s*$regex\s+(\S*)\s+port\s+(\S+)\s+(\S+)//) { |
my $out; |
111 |
push @{$atom->{list}}, {$regex => $1, port => {port => $3, cmp => $2}}; |
foreach (@{$atom->{list}}) { |
112 |
} |
if (!ref) { |
113 |
elsif ($in =~ s/\s*$regex\s+(\S*)//) { |
$out .= "$_ "; |
114 |
push @{$atom->{list}}, {$regex => $1}; |
} |
115 |
} |
elsif (ref eq "HASH") { |
116 |
} |
my $h = $_; |
117 |
) |
foreach my $keys (keys %{$_}){ |
118 |
], |
$out .= $keys . " "; |
119 |
comments => [ ['#'] ], |
if (!ref $h->{$keys}) { |
120 |
comment_output => q(/^(\s*)$/ ? "$_\n" : "#$_\n"), |
$out .= $h->{$keys} ." "; |
121 |
output => { |
} |
122 |
|
elsif (ref $h->{$keys} eq "HASH") { |
123 |
}, |
if (!ref $h->{$keys}{host}) { |
124 |
edit_atom => q( |
$out .= $h->{$keys}{host} . " "; |
125 |
if ($index == -1) { |
} |
126 |
my $i = 0; |
elsif (ref $h->{$keys}{host} eq "ARRAY") { |
127 |
foreach (@{$out->{atoms}}) { |
$out .= "{" . join(", ", @{$h->{$keys}{host}}) . "} "; |
128 |
$_->{key} eq $args{key} and $index = $i; |
} |
129 |
$i++; |
if (defined $h->{$keys}{port}) { |
130 |
} |
$out .= "port "; |
131 |
$index == -1 and return -2; |
if ($h->{$keys}{port}{type} eq "LIST") { |
132 |
} |
$out .= "{" . join(", ", @{$h->{$keys}{port}{value}}) . "} "; |
133 |
@{@{$out->{atoms}}[$index]}{keys(%args)} = values(%args); |
} |
134 |
return $index; |
else { |
135 |
), |
$out .= join(" ", @{$h->{$keys}{port}{value}}) . " "; |
136 |
find_atom_pos => q( |
} |
137 |
my $i = 0; |
} |
138 |
my @res; |
} |
139 |
foreach my $atom (@{$out->{atoms}}) { |
} |
140 |
my $flag = 1; |
} |
141 |
foreach my $key (keys(%args)) { |
} |
142 |
if ($key eq 'values') { |
chop $out; |
143 |
foreach (keys(%{$args{values}})) { |
"$out\n";; |
144 |
$atom->{values}{$_} eq $args{values}->{$_} or $flag = 0, last; |
) |
145 |
} |
}, |
146 |
} else { |
edit_atom => q( |
147 |
$atom->{$key} eq $args{$key} or $flag = 0, last; |
if ($index == -1) { |
148 |
} |
my $i = 0; |
149 |
} |
foreach (@{$out->{atoms}}) { |
150 |
$flag and push(@res, $i); |
$_->{key} eq $args{key} and $index = $i; |
151 |
$i++; |
$i++; |
152 |
} |
} |
153 |
wantarray ? @res : $res[0]; |
$index == -1 and return -2; |
154 |
), |
} |
155 |
}; |
@{@{$out->{atoms}}[$index]}{keys(%args)} = values(%args); |
156 |
|
return $index; |
157 |
|
), |
158 |
|
find_atom_pos => q( |
159 |
|
my $i = 0; |
160 |
|
my @res; |
161 |
|
foreach my $atom (@{$out->{atoms}}) { |
162 |
|
my $flag = 1; |
163 |
|
foreach my $key (keys(%args)) { |
164 |
|
if ($key eq 'values') { |
165 |
|
foreach (keys(%{$args{values}})) { |
166 |
|
$atom->{values}{$_} eq $args{values}->{$_} or $flag = 0, last; |
167 |
|
} |
168 |
|
} else { |
169 |
|
$atom->{$key} eq $args{$key} or $flag = 0, last; |
170 |
|
} |
171 |
|
} |
172 |
|
$flag and push(@res, $i); |
173 |
|
$i++; |
174 |
|
} |
175 |
|
wantarray ? @res : $res[0]; |
176 |
|
), |
177 |
|
}; |
178 |
1 |
1 |