]>
Commit | Line | Data |
---|---|---|
1848564d MT |
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 | ||
d61a01d4 MT |
22 | function hook_dir() { |
23 | local type=${1} | |
24 | ||
943e3f7e MT |
25 | if [ -n "${type}" ]; then |
26 | type="/${type}s" | |
27 | fi | |
28 | ||
d2a21d01 | 29 | echo "${NETWORK_HOOKS_DIR}${type}" |
d61a01d4 | 30 | } |
f41fa3d7 | 31 | NETWORK_HOOKS_DIR_ZONES="$(hook_dir zone)" |
d61a01d4 | 32 | |
1848564d | 33 | function hook_exists() { |
d61a01d4 MT |
34 | local type=${1} |
35 | local hook=${2} | |
1848564d | 36 | |
943e3f7e MT |
37 | assert isset type |
38 | assert isset hook | |
39 | ||
f41fa3d7 MT |
40 | # Add the path prefix. |
41 | hook="$(hook_dir ${type})/${hook}" | |
1848564d | 42 | |
f41fa3d7 | 43 | [ ! -d "${hook}" ] && [ -x "${hook}" ] |
1848564d MT |
44 | } |
45 | ||
d61a01d4 MT |
46 | function hook_exec() { |
47 | local type=${1} | |
48 | local hook=${2} | |
f41fa3d7 MT |
49 | local cmd=${3} |
50 | shift 3 | |
d61a01d4 | 51 | |
943e3f7e MT |
52 | assert isset type |
53 | assert isset hook | |
f41fa3d7 MT |
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 | |
943e3f7e | 70 | |
f41fa3d7 MT |
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} | |
d61a01d4 MT |
79 | fi |
80 | ||
f41fa3d7 | 81 | return ${ret} |
d61a01d4 MT |
82 | } |
83 | ||
84 | function config_get_hook() { | |
85 | local config=${1} | |
86 | ||
943e3f7e | 87 | assert isset config |
a5ebb169 | 88 | assert [ -e "${config}" ] |
943e3f7e | 89 | |
d61a01d4 MT |
90 | ( |
91 | . ${config} | |
92 | echo "${HOOK}" | |
93 | ) | |
94 | } | |
95 | ||
d61a01d4 MT |
96 | function hook_zone_exists() { |
97 | hook_exists zone $@ | |
98 | } | |
99 | ||
100 | function hook_zone_port_exists() { | |
1848564d MT |
101 | local hook_zone=${1} |
102 | local hook_port=${2} | |
103 | ||
f41fa3d7 | 104 | hook_exists zone "${hook_zone}.ports/${hook_port}" |
1848564d MT |
105 | } |
106 | ||
d61a01d4 | 107 | function hook_zone_config_exists() { |
1848564d MT |
108 | local hook_zone=${1} |
109 | local hook_config=${2} | |
110 | ||
f41fa3d7 | 111 | hook_exists zone "${hook_zone}.configs/${hook_config}" |
1848564d MT |
112 | } |
113 | ||
d61a01d4 | 114 | function hook_zone_has_ports() { |
1848564d MT |
115 | local hook=${1} |
116 | ||
f41fa3d7 | 117 | [ -d "${NETWORK_HOOKS_DIR_ZONES}/${hook}.ports" ] |
711ffac1 MT |
118 | } |
119 | ||
d61a01d4 | 120 | function hook_zone_has_configs() { |
1848564d MT |
121 | local hook=${1} |
122 | ||
f41fa3d7 | 123 | [ -d "${NETWORK_HOOKS_DIR_ZONES}/${hook}.configs" ] |
1848564d MT |
124 | } |
125 | ||
d61a01d4 MT |
126 | function hook_zone_exec() { |
127 | hook_exec zone $@ | |
1848564d MT |
128 | } |
129 | ||
d61a01d4 | 130 | function hook_zone_port_exec() { |
1848564d MT |
131 | local hook_zone=${1} |
132 | local hook_port=${2} | |
133 | shift 2 | |
134 | ||
f41fa3d7 | 135 | hook_zone_exec "${hook_zone}.ports/${hook_port}" $@ |
1848564d MT |
136 | } |
137 | ||
d61a01d4 | 138 | function hook_zone_config_exec() { |
1848564d | 139 | local hook_zone=${1} |
f41fa3d7 | 140 | local hook_port=${2} |
1848564d MT |
141 | shift 2 |
142 | ||
f41fa3d7 | 143 | hook_zone_exec "${hook_zone}.configs/${hook_port}" $@ |
1848564d MT |
144 | } |
145 | ||
d61a01d4 | 146 | function hook_zone_get_all() { |
1848564d MT |
147 | local type=${1} |
148 | ||
149 | local hook | |
d61a01d4 | 150 | for hook in $(hook_dir zone)/*; do |
1848564d | 151 | hook=$(basename ${hook}) |
d61a01d4 | 152 | hook_zone_exists ${hook} && echo "${hook}" |
03170817 | 153 | done |
1848564d MT |
154 | } |
155 | ||
d61a01d4 | 156 | function hook_zone_ports_get_all() { |
1848564d MT |
157 | local hook=${1} |
158 | ||
d61a01d4 | 159 | if ! hook_exists zone ${hook}; then |
1848564d MT |
160 | error "Hook '${hook}' does not exist." |
161 | return ${EXIT_ERROR} | |
162 | fi | |
163 | ||
a5ebb169 MT |
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 | ||
711ffac1 MT |
169 | local h |
170 | for h in $(hook_dir zone)/${hook}.ports/*; do | |
171 | h=$(basename ${h}) | |
a5ebb169 MT |
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 | |
03170817 | 197 | done |
a5ebb169 MT |
198 | |
199 | return ${EXIT_OK} | |
1848564d | 200 | } |