]> git.ipfire.org Git - people/arne_f/network.git/blame - functions.hook
network: Add some initialization handlers.
[people/arne_f/network.git] / functions.hook
CommitLineData
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
22function 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 32function 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
46function 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
62function config_get_hook() {
63 local config=${1}
64
943e3f7e
MT
65 assert isset config
66
d61a01d4
MT
67 (
68 . ${config}
69 echo "${HOOK}"
70 )
71}
72
73## Wrappers around the hook functions for zones
74
75function hook_zone_exists() {
76 hook_exists zone $@
77}
78
79function hook_zone_port_exists() {
1848564d
MT
80 local hook_zone=${1}
81 local hook_port=${2}
82
d61a01d4 83 hook_zone_exists ${hook_zone} || return ${EXIT_ERROR}
1848564d 84
d61a01d4 85 [ -x "$(hook_dir zone)/${hook_zone}.ports/${hook_port}" ]
1848564d
MT
86}
87
d61a01d4 88function hook_zone_config_exists() {
1848564d
MT
89 local hook_zone=${1}
90 local hook_config=${2}
91
d61a01d4 92 hook_zone_exists ${hook_zone} || return ${EXIT_ERROR}
1848564d 93
d61a01d4 94 [ -x "$(hook_dir zone)/${hook_zone}.configs/${hook_config}" ]
1848564d
MT
95}
96
d61a01d4 97function hook_zone_has_ports() {
1848564d
MT
98 local hook=${1}
99
d61a01d4 100 [ -d "$(hook_dir zone)/${hook}.ports" ]
1848564d
MT
101}
102
711ffac1
MT
103function hook_zone_port_exists() {
104 : # XXX WANTED
105}
106
d61a01d4 107function hook_zone_has_configs() {
1848564d
MT
108 local hook=${1}
109
d61a01d4 110 [ -d "$(hook_dir zone)/${hook}.configs" ]
1848564d
MT
111}
112
d61a01d4
MT
113function hook_zone_exec() {
114 hook_exec zone $@
1848564d
MT
115}
116
d61a01d4 117function hook_zone_port_exec() {
1848564d
MT
118 local hook_zone=${1}
119 local hook_port=${2}
120 shift 2
121
d61a01d4 122 if ! hook_exists zone ${hook_zone}; then
1848564d
MT
123 error "Hook '${hook_zone}' does not exist."
124 return ${EXIT_ERROR}
125 fi
126
d61a01d4 127 if ! hook_zone_port_exists ${hook_zone} ${hook_port}; then
1848564d
MT
128 error "Port hook '${hook_port}' does not exist."
129 return ${EXIT_ERROR}
130 fi
131
711ffac1 132 exec_cmd $(hook_dir zone)/${hook_zone}.ports/${hook_port} $@
1848564d
MT
133}
134
d61a01d4 135function hook_zone_config_exec() {
1848564d
MT
136 local hook_zone=${1}
137 local hook_config=${2}
138 shift 2
139
d61a01d4 140 if ! hook_zone_exists ${hook_zone}; then
1848564d
MT
141 error "Hook '${hook_zone}' does not exist."
142 return ${EXIT_ERROR}
143 fi
144
d61a01d4 145 if ! hook_zone_config_exists ${hook_zone} ${hook_config}; then
1848564d
MT
146 error "Config hook '${hook_config}' does not exist."
147 return ${EXIT_ERROR}
148 fi
149
711ffac1 150 exec_cmd $(hook_dir zone)/${hook_zone}.configs/${hook_config} $@
1848564d
MT
151}
152
d61a01d4 153function hook_zone_get_all() {
1848564d
MT
154 local type=${1}
155
156 local hook
d61a01d4 157 for hook in $(hook_dir zone)/*; do
1848564d 158 hook=$(basename ${hook})
d61a01d4 159 hook_zone_exists ${hook} && echo "${hook}"
1848564d
MT
160 done | sort
161}
162
d61a01d4 163function hook_zone_ports_get_all() {
1848564d
MT
164 local hook=${1}
165
d61a01d4 166 if ! hook_exists zone ${hook}; then
1848564d
MT
167 error "Hook '${hook}' does not exist."
168 return ${EXIT_ERROR}
169 fi
170
711ffac1
MT
171 local h
172 for h in $(hook_dir zone)/${hook}.ports/*; do
173 h=$(basename ${h})
1848564d 174 ## XXX executeable?
711ffac1 175 echo "${h}"
1848564d
MT
176 done | sort
177}
711ffac1
MT
178
179function hook_device_exists() {
180 hook_exists device $@
181}