]>
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 | ||
29 | echo "${HOOKS_DIR}${type}" | |
d61a01d4 MT |
30 | } |
31 | ||
1848564d | 32 | function hook_exists() { |
d61a01d4 MT |
33 | local type=${1} |
34 | local hook=${2} | |
1848564d | 35 | |
943e3f7e MT |
36 | assert isset type |
37 | assert isset hook | |
38 | ||
d61a01d4 | 39 | local hook_dir=$(hook_dir ${type}) |
1848564d | 40 | |
d61a01d4 MT |
41 | [ -d "${hook_dir}/${hook}" ] && return ${EXIT_ERROR} |
42 | ||
43 | [ -x "${hook_dir}/${hook}" ] | |
1848564d MT |
44 | } |
45 | ||
d61a01d4 MT |
46 | function hook_exec() { |
47 | local type=${1} | |
48 | local hook=${2} | |
49 | shift 2 | |
50 | ||
943e3f7e MT |
51 | assert isset type |
52 | assert isset hook | |
53 | ||
d61a01d4 MT |
54 | if ! hook_exists ${type} ${hook}; then |
55 | error "Hook '${hook}' does not exist." | |
56 | return ${EXIT_ERROR} | |
57 | fi | |
58 | ||
711ffac1 | 59 | exec_cmd $(hook_dir ${type})/${hook} $@ |
d61a01d4 MT |
60 | } |
61 | ||
62 | function config_get_hook() { | |
63 | local config=${1} | |
64 | ||
943e3f7e | 65 | assert isset config |
a5ebb169 | 66 | assert [ -e "${config}" ] |
943e3f7e | 67 | |
d61a01d4 MT |
68 | ( |
69 | . ${config} | |
70 | echo "${HOOK}" | |
71 | ) | |
72 | } | |
73 | ||
74 | ## Wrappers around the hook functions for zones | |
75 | ||
76 | function hook_zone_exists() { | |
77 | hook_exists zone $@ | |
78 | } | |
79 | ||
80 | function hook_zone_port_exists() { | |
1848564d MT |
81 | local hook_zone=${1} |
82 | local hook_port=${2} | |
83 | ||
d61a01d4 | 84 | hook_zone_exists ${hook_zone} || return ${EXIT_ERROR} |
1848564d | 85 | |
d61a01d4 | 86 | [ -x "$(hook_dir zone)/${hook_zone}.ports/${hook_port}" ] |
1848564d MT |
87 | } |
88 | ||
d61a01d4 | 89 | function hook_zone_config_exists() { |
1848564d MT |
90 | local hook_zone=${1} |
91 | local hook_config=${2} | |
92 | ||
d61a01d4 | 93 | hook_zone_exists ${hook_zone} || return ${EXIT_ERROR} |
1848564d | 94 | |
d61a01d4 | 95 | [ -x "$(hook_dir zone)/${hook_zone}.configs/${hook_config}" ] |
1848564d MT |
96 | } |
97 | ||
d61a01d4 | 98 | function hook_zone_has_ports() { |
1848564d MT |
99 | local hook=${1} |
100 | ||
d61a01d4 | 101 | [ -d "$(hook_dir zone)/${hook}.ports" ] |
1848564d MT |
102 | } |
103 | ||
711ffac1 MT |
104 | function hook_zone_port_exists() { |
105 | : # XXX WANTED | |
106 | } | |
107 | ||
d61a01d4 | 108 | function hook_zone_has_configs() { |
1848564d MT |
109 | local hook=${1} |
110 | ||
d61a01d4 | 111 | [ -d "$(hook_dir zone)/${hook}.configs" ] |
1848564d MT |
112 | } |
113 | ||
d61a01d4 MT |
114 | function hook_zone_exec() { |
115 | hook_exec zone $@ | |
1848564d MT |
116 | } |
117 | ||
d61a01d4 | 118 | function hook_zone_port_exec() { |
1848564d MT |
119 | local hook_zone=${1} |
120 | local hook_port=${2} | |
121 | shift 2 | |
122 | ||
d61a01d4 | 123 | if ! hook_exists zone ${hook_zone}; then |
1848564d MT |
124 | error "Hook '${hook_zone}' does not exist." |
125 | return ${EXIT_ERROR} | |
126 | fi | |
127 | ||
d61a01d4 | 128 | if ! hook_zone_port_exists ${hook_zone} ${hook_port}; then |
1848564d MT |
129 | error "Port hook '${hook_port}' does not exist." |
130 | return ${EXIT_ERROR} | |
131 | fi | |
132 | ||
711ffac1 | 133 | exec_cmd $(hook_dir zone)/${hook_zone}.ports/${hook_port} $@ |
1848564d MT |
134 | } |
135 | ||
d61a01d4 | 136 | function hook_zone_config_exec() { |
1848564d MT |
137 | local hook_zone=${1} |
138 | local hook_config=${2} | |
139 | shift 2 | |
140 | ||
a5ebb169 MT |
141 | assert isset hook_zone |
142 | assert isset hook_config | |
143 | ||
d61a01d4 | 144 | if ! hook_zone_exists ${hook_zone}; then |
1848564d MT |
145 | error "Hook '${hook_zone}' does not exist." |
146 | return ${EXIT_ERROR} | |
147 | fi | |
148 | ||
d61a01d4 | 149 | if ! hook_zone_config_exists ${hook_zone} ${hook_config}; then |
1848564d MT |
150 | error "Config hook '${hook_config}' does not exist." |
151 | return ${EXIT_ERROR} | |
152 | fi | |
153 | ||
711ffac1 | 154 | exec_cmd $(hook_dir zone)/${hook_zone}.configs/${hook_config} $@ |
1848564d MT |
155 | } |
156 | ||
d61a01d4 | 157 | function hook_zone_get_all() { |
1848564d MT |
158 | local type=${1} |
159 | ||
160 | local hook | |
d61a01d4 | 161 | for hook in $(hook_dir zone)/*; do |
1848564d | 162 | hook=$(basename ${hook}) |
d61a01d4 | 163 | hook_zone_exists ${hook} && echo "${hook}" |
03170817 | 164 | done |
1848564d MT |
165 | } |
166 | ||
d61a01d4 | 167 | function hook_zone_ports_get_all() { |
1848564d MT |
168 | local hook=${1} |
169 | ||
d61a01d4 | 170 | if ! hook_exists zone ${hook}; then |
1848564d MT |
171 | error "Hook '${hook}' does not exist." |
172 | return ${EXIT_ERROR} | |
173 | fi | |
174 | ||
a5ebb169 MT |
175 | # If the zone hook has got no ports we exit silently |
176 | if ! hook_zone_has_ports ${hook}; then | |
177 | return ${EXIT_OK} | |
178 | fi | |
179 | ||
711ffac1 MT |
180 | local h |
181 | for h in $(hook_dir zone)/${hook}.ports/*; do | |
182 | h=$(basename ${h}) | |
a5ebb169 MT |
183 | if hook_zone_port_exists ${hook} ${h}; then |
184 | echo "${h}" | |
185 | fi | |
186 | done | |
187 | } | |
188 | ||
189 | function hook_zone_configs_get_all() { | |
190 | local hook=${1} | |
191 | ||
192 | if ! hook_exists zone ${hook}; then | |
193 | error "Hook '${hook}' does not exist." | |
194 | return ${EXIT_ERROR} | |
195 | fi | |
196 | ||
197 | # If the zone hook has got no configurations we exit silently | |
198 | if ! hook_zone_has_configs ${hook}; then | |
199 | return ${EXIT_OK} | |
200 | fi | |
201 | ||
202 | local h | |
203 | for h in $(hook_dir zone)/${hook}.configs/*; do | |
204 | h=$(basename ${h}) | |
205 | if hook_zone_config_exists ${hook} ${h}; then | |
206 | echo "${h}" | |
207 | fi | |
03170817 | 208 | done |
a5ebb169 MT |
209 | |
210 | return ${EXIT_OK} | |
1848564d | 211 | } |