Imported Upstream version 1.7.0
[platform/upstream/augeas.git] / lenses / hosts_access.aug
1 (*
2 Module: Hosts_Access
3   Parses /etc/hosts.{allow,deny}
4
5 Author: Raphael Pinson <raphink@gmail.com>
6
7 About: Reference
8   This lens tries to keep as close as possible to `man 5 hosts_access` and `man 5 hosts_options` where possible.
9
10 About: License
11    This file is licenced under the LGPL v2+, like the rest of Augeas.
12
13 About: Lens Usage
14    To be documented
15
16 About: Configuration files
17    This lens applies to /etc/hosts.{allow,deny}. See <filter>.
18 *)
19
20 module Hosts_Access =
21
22 autoload xfm
23
24 (************************************************************************
25  * Group:                 USEFUL PRIMITIVES
26  *************************************************************************)
27
28 (* View: colon *)
29 let colon = del /[ \t]*(\\\\[ \t]*\n[ \t]+)?:[ \t]*(\\\\[ \t]*\n[ \t]+)?/ ": "
30
31 (* Variable: comma_sep *)
32 let comma_sep = /([ \t]|(\\\\\n))*,([ \t]|(\\\\\n))*/
33
34 (* Variable: ws_sep *)
35 let ws_sep = / +/
36
37 (* View: list_sep *)
38 let list_sep = del ( comma_sep | ws_sep ) ", "
39
40 (* View: list_item *)
41 let list_item = store ( Rx.word - /EXCEPT/i )
42
43 (* View: client_host_item
44    Allows @ for netgroups, supports [ipv6] syntax *)
45 let client_host_item =
46   let client_hostname_rx = /[A-Za-z0-9_.@?*-][A-Za-z0-9_.?*-]*/ in
47   let client_ipv6_rx = "[" . /[A-Za-z0-9:?*%]+/ . "]" in
48     let client_host_rx = client_hostname_rx | client_ipv6_rx in
49     let netmask = [ Util.del_str "/" . label "netmask" . store Rx.word ] in
50       store ( client_host_rx - /EXCEPT/i ) . netmask?
51
52 (* View: client_file_item *)
53 let client_file_item =
54   let client_file_rx = /\/[^ \t\n,:]+/ in
55     store ( client_file_rx - /EXCEPT/i )
56
57 (* Variable: option_kw
58    Since either an option or a shell command can be given, use an explicit list
59    of known options to avoid misinterpreting a command as an option *)
60 let option_kw = "severity"
61               | "spawn"
62               | "twist"
63               | "keepalive"
64               | "linger"
65               | "rfc931"
66               | "banners"
67               | "nice"
68               | "setenv"
69               | "umask"
70               | "user"
71               | /allow/i
72               | /deny/i
73
74 (* Variable: shell_command_rx *)
75 let shell_command_rx = /[^ \t\n:][^\n]*[^ \t\n]|[^ \t\n:\\\\]/
76                          - ( option_kw . /.*/ )
77
78 (* View: sto_to_colon
79    Allows escaped colon sequences *)
80 let sto_to_colon = store /[^ \t\n:=][^\n:]*((\\\\:|\\\\[ \t]*\n[ \t]+)[^\n:]*)*[^ \\\t\n:]|[^ \t\n:\\\\]/
81
82 (* View: except
83  * The except operator makes it possible to write very compact rules.
84  *)
85 let except (lns:lens) = [ label "except" . Sep.space
86                         . del /except/i "EXCEPT"
87                         . Sep.space . lns ]
88
89 (************************************************************************
90  * Group:                 ENTRY TYPES
91  *************************************************************************)
92
93 (* View: daemon *)
94 let daemon =
95   let host = [ label "host"
96              . Util.del_str "@"
97              . list_item ] in
98    [ label "process"
99    . list_item
100    . host? ]
101
102 (* View: daemon_list
103     A list of <daemon>s *)
104 let daemon_list = Build.opt_list daemon list_sep
105
106 (* View: client *)
107 let client =
108   let user = [ label "user"
109              . list_item
110              . Util.del_str "@" ] in
111     [ label "client"
112     . user?
113     . client_host_item ]
114
115 (* View: client_file *)
116 let client_file = [ label "file" . client_file_item ]
117
118 (* View: client_list
119     A list of <client>s *)
120 let client_list = Build.opt_list ( client | client_file ) list_sep
121
122 (* View: option
123    Optional extensions defined in hosts_options(5) *)
124 let option = [ key option_kw
125              . ( del /([ \t]*=[ \t]*|[ \t]+)/ " " . sto_to_colon )? ]
126
127 (* View: shell_command *)
128 let shell_command = [ label "shell_command"
129                     . store shell_command_rx ]
130
131 (* View: entry *)
132 let entry = [ seq "line"
133             . daemon_list
134             . (except daemon_list)?
135             . colon
136             . client_list
137             . (except client_list)?
138             . ( (colon . option)+ | (colon . shell_command)? )
139             . Util.eol ]
140
141 (************************************************************************
142  * Group:                 LENS AND FILTER
143  *************************************************************************)
144
145 (* View: lns *)
146 let lns = (Util.empty | Util.comment | entry)*
147
148 (* View: filter *)
149 let filter = incl "/etc/hosts.allow"
150            . incl "/etc/hosts.deny"
151
152 let xfm = transform lns filter