Imported Upstream version 0.10.0
[platform/upstream/augeas.git] / lenses / iptables.aug
1 module Iptables =
2   autoload xfm
3
4 (*
5 Module: Iptables
6    Parse the iptables file format as produced by iptables-save. The
7    resulting tree is fairly simple; in particular a rule is simply
8    a long list of options/switches and their values (if any)
9
10    This lens should be considered experimental
11 *)
12
13 let comment = Util.comment
14 let empty = Util.empty
15 let eol = Util.eol
16 let spc = Util.del_ws_spc
17 let dels = Util.del_str
18
19 let chain_name = store /[A-Za-z0-9_-]+/
20 let chain =
21   let policy = [ label "policy" . store /ACCEPT|DROP|REJECT|-/ ] in
22   let counters_eol = del /[ \t]*(\[[0-9:]+\])?[ \t]*\n/ "\n" in
23     [ label "chain" .
24         dels ":" . chain_name . spc . policy . counters_eol ]
25
26 let param (long:string) (short:string) =
27   [ label long .
28       spc . del (/--/ . long | /-/ . short) ("-" . short) . spc .
29       store /(![ \t]*)?[^ \t\n!-][^ \t\n]*/ ]
30
31 (* A negatable parameter, which can either be FTW
32      ! --param arg
33    or
34      --param ! arg
35 *)
36 let neg_param (long:string) (short:string) =
37   [ label long .
38       [ spc . dels "!" . label "not" ]? .
39       spc . del (/--/ . long | /-/ . short) ("-" . short) . spc .
40       store /(![ \t]*)?[^ \t\n!-][^ \t\n]*/ ]
41
42 let tcp_flags =
43   let flags = /SYN|ACK|FIN|RST|URG|PSH|ALL|NONE/ in
44   let flag_list (name:string) =
45     Build.opt_list [label name . store flags] (dels ",") in
46   [ label "tcp-flags" .
47       spc . dels "--tcp-flags" .
48       spc . flag_list "mask" . spc . flag_list "set" ]
49
50 (* misses --set-counters *)
51 let ipt_match =
52   let any_key = /[a-zA-Z-][a-zA-Z0-9-]+/ -
53     /protocol|source|destination|jump|goto|in-interface|out-interface|fragment|match|tcp-flags/ in
54   let any_val = /([^" \t\n!-][^ \t\n]*)|"([^"\\\n]|\\\\.)*"/ in
55   let any_param =
56     [ [ spc . dels "!" . label "not" ]? .
57       spc . dels "--" . key any_key . (spc . store any_val)? ] in
58     (neg_param "protocol" "p"
59     |neg_param "source" "s"
60     |neg_param "destination" "d"
61     |param "jump" "j"
62     |param "goto" "g"
63     |neg_param "in-interface" "i"
64     |neg_param "out-interface" "o"
65     |neg_param "fragment" "f"
66     |param "match" "m"
67     |tcp_flags
68     |any_param)*
69
70 let chain_action (n:string) (o:string) =
71     [ label n .
72         del (/--/ . n | o) o .
73         spc . chain_name . ipt_match . eol ]
74
75 let table_rule = chain_action "append" "-A"
76                | chain_action "insert" "-I"
77                | empty
78
79
80 let table = [ del /\*/ "*" . label "table" . store /[a-z]+/ . eol .
81                 (chain|comment|table_rule)* .
82                 dels "COMMIT" . eol ]
83
84 let lns = (comment|empty|table)*
85 let xfm = transform lns (incl "/etc/sysconfig/iptables"
86                        . incl "/etc/iptables-save")