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