]> git.ipfire.org Git - people/stevee/network.git/blob - functions.hook
Don't use connection tracking for loopback traffic.
[people/stevee/network.git] / functions.hook
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 function hook_dir() {
23 local type=${1}
24
25 if [ -n "${type}" ]; then
26 type="/${type}s"
27 fi
28
29 echo "${NETWORK_HOOKS_DIR}${type}"
30 }
31 NETWORK_HOOKS_DIR_ZONES="$(hook_dir zone)"
32
33 function hook_exists() {
34 local type=${1}
35 local hook=${2}
36
37 assert isset type
38 assert isset hook
39
40 # Add the path prefix.
41 hook="$(hook_dir ${type})/${hook}"
42
43 [ ! -d "${hook}" ] && [ -x "${hook}" ]
44 }
45
46 function hook_exec() {
47 local type=${1}
48 local hook=${2}
49 local cmd=${3}
50 shift 3
51
52 assert isset type
53 assert isset hook
54 assert isset cmd
55
56 assert hook_exists "${type}" "${hook}"
57
58 # For performance reasons, all hooks are executed
59 # in a subshell and so will inherit the currently
60 # running environment.
61 (
62 # Set the name of the hook.
63 HOOK=$(basename ${hook})
64
65 # Source the code of the hook.
66 source "$(hook_dir ${type})/${hook}"
67
68 # Make sure HOOK is still properly set.
69 assert isset HOOK
70
71 # Execute the requested command.
72 _${cmd} $@
73 )
74 local ret=$?
75
76 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
77 log ERROR "Hook exited with an assertion error."
78 exit ${ret}
79 fi
80
81 return ${ret}
82 }
83
84 function config_get_hook() {
85 local config=${1}
86
87 assert isset config
88 assert [ -e "${config}" ]
89
90 (
91 . ${config}
92 echo "${HOOK}"
93 )
94 }
95
96 function hook_zone_exists() {
97 hook_exists zone $@
98 }
99
100 function hook_zone_port_exists() {
101 local hook_zone=${1}
102 local hook_port=${2}
103
104 hook_exists zone "${hook_zone}.ports/${hook_port}"
105 }
106
107 function hook_zone_config_exists() {
108 local hook_zone=${1}
109 local hook_config=${2}
110
111 hook_exists zone "${hook_zone}.configs/${hook_config}"
112 }
113
114 function hook_zone_has_ports() {
115 local hook=${1}
116
117 [ -d "${NETWORK_HOOKS_DIR_ZONES}/${hook}.ports" ]
118 }
119
120 function hook_zone_has_configs() {
121 local hook=${1}
122
123 [ -d "${NETWORK_HOOKS_DIR_ZONES}/${hook}.configs" ]
124 }
125
126 function hook_zone_exec() {
127 hook_exec zone $@
128 }
129
130 function hook_zone_port_exec() {
131 local hook_zone=${1}
132 local hook_port=${2}
133 shift 2
134
135 hook_zone_exec "${hook_zone}.ports/${hook_port}" $@
136 }
137
138 function hook_zone_config_exec() {
139 local hook_zone=${1}
140 local hook_port=${2}
141 shift 2
142
143 hook_zone_exec "${hook_zone}.configs/${hook_port}" $@
144 }
145
146 function hook_zone_get_all() {
147 local type=${1}
148
149 local hook
150 for hook in $(hook_dir zone)/*; do
151 hook=$(basename ${hook})
152 hook_zone_exists ${hook} && echo "${hook}"
153 done
154 }
155
156 function hook_zone_ports_get_all() {
157 local hook=${1}
158
159 if ! hook_exists zone ${hook}; then
160 error "Hook '${hook}' does not exist."
161 return ${EXIT_ERROR}
162 fi
163
164 # If the zone hook has got no ports we exit silently
165 if ! hook_zone_has_ports ${hook}; then
166 return ${EXIT_OK}
167 fi
168
169 local h
170 for h in $(hook_dir zone)/${hook}.ports/*; do
171 h=$(basename ${h})
172 if hook_zone_port_exists ${hook} ${h}; then
173 echo "${h}"
174 fi
175 done
176 }
177
178 function hook_zone_configs_get_all() {
179 local hook=${1}
180
181 if ! hook_exists zone ${hook}; then
182 error "Hook '${hook}' does not exist."
183 return ${EXIT_ERROR}
184 fi
185
186 # If the zone hook has got no configurations we exit silently
187 if ! hook_zone_has_configs ${hook}; then
188 return ${EXIT_OK}
189 fi
190
191 local h
192 for h in $(hook_dir zone)/${hook}.configs/*; do
193 h=$(basename ${h})
194 if hook_zone_config_exists ${hook} ${h}; then
195 echo "${h}"
196 fi
197 done
198
199 return ${EXIT_OK}
200 }