]> git.ipfire.org Git - people/arne_f/network.git/blob - functions.hook
network: Some work on configuration code.
[people/arne_f/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 "${HOOKS_DIR}${type}"
30 }
31
32 function hook_exists() {
33 local type=${1}
34 local hook=${2}
35
36 assert isset type
37 assert isset hook
38
39 local hook_dir=$(hook_dir ${type})
40
41 [ -d "${hook_dir}/${hook}" ] && return ${EXIT_ERROR}
42
43 [ -x "${hook_dir}/${hook}" ]
44 }
45
46 function hook_exec() {
47 local type=${1}
48 local hook=${2}
49 shift 2
50
51 assert isset type
52 assert isset hook
53
54 if ! hook_exists ${type} ${hook}; then
55 error "Hook '${hook}' does not exist."
56 return ${EXIT_ERROR}
57 fi
58
59 exec_cmd $(hook_dir ${type})/${hook} $@
60 }
61
62 function config_get_hook() {
63 local config=${1}
64
65 assert isset config
66 assert [ -e "${config}" ]
67
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() {
81 local hook_zone=${1}
82 local hook_port=${2}
83
84 hook_zone_exists ${hook_zone} || return ${EXIT_ERROR}
85
86 [ -x "$(hook_dir zone)/${hook_zone}.ports/${hook_port}" ]
87 }
88
89 function hook_zone_config_exists() {
90 local hook_zone=${1}
91 local hook_config=${2}
92
93 hook_zone_exists ${hook_zone} || return ${EXIT_ERROR}
94
95 [ -x "$(hook_dir zone)/${hook_zone}.configs/${hook_config}" ]
96 }
97
98 function hook_zone_has_ports() {
99 local hook=${1}
100
101 [ -d "$(hook_dir zone)/${hook}.ports" ]
102 }
103
104 function hook_zone_port_exists() {
105 : # XXX WANTED
106 }
107
108 function hook_zone_has_configs() {
109 local hook=${1}
110
111 [ -d "$(hook_dir zone)/${hook}.configs" ]
112 }
113
114 function hook_zone_exec() {
115 hook_exec zone $@
116 }
117
118 function hook_zone_port_exec() {
119 local hook_zone=${1}
120 local hook_port=${2}
121 shift 2
122
123 if ! hook_exists zone ${hook_zone}; then
124 error "Hook '${hook_zone}' does not exist."
125 return ${EXIT_ERROR}
126 fi
127
128 if ! hook_zone_port_exists ${hook_zone} ${hook_port}; then
129 error "Port hook '${hook_port}' does not exist."
130 return ${EXIT_ERROR}
131 fi
132
133 exec_cmd $(hook_dir zone)/${hook_zone}.ports/${hook_port} $@
134 }
135
136 function hook_zone_config_exec() {
137 local hook_zone=${1}
138 local hook_config=${2}
139 shift 2
140
141 assert isset hook_zone
142 assert isset hook_config
143
144 if ! hook_zone_exists ${hook_zone}; then
145 error "Hook '${hook_zone}' does not exist."
146 return ${EXIT_ERROR}
147 fi
148
149 if ! hook_zone_config_exists ${hook_zone} ${hook_config}; then
150 error "Config hook '${hook_config}' does not exist."
151 return ${EXIT_ERROR}
152 fi
153
154 exec_cmd $(hook_dir zone)/${hook_zone}.configs/${hook_config} $@
155 }
156
157 function hook_zone_get_all() {
158 local type=${1}
159
160 local hook
161 for hook in $(hook_dir zone)/*; do
162 hook=$(basename ${hook})
163 hook_zone_exists ${hook} && echo "${hook}"
164 done
165 }
166
167 function hook_zone_ports_get_all() {
168 local hook=${1}
169
170 if ! hook_exists zone ${hook}; then
171 error "Hook '${hook}' does not exist."
172 return ${EXIT_ERROR}
173 fi
174
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
180 local h
181 for h in $(hook_dir zone)/${hook}.ports/*; do
182 h=$(basename ${h})
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
208 done
209
210 return ${EXIT_OK}
211 }