]> git.ipfire.org Git - people/stevee/network.git/blob - src/functions/functions.hook
device: Rewrite device_set_address
[people/stevee/network.git] / src / functions / 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 HOOK_COMMANDS_CONFIG="hook_create hook_down hook_status hook_up"
23
24 HOOK_COMMANDS_PORT="hook_create hook_down hook_edit hook_hotplug \
25 hook_hotplug_rename hook_info hook_status hook_up"
26
27 HOOK_COMMANDS_ZONE="hook_add hook_create hook_discover hook_down hook_edit hook_help \
28 hook_info hook_remove hook_status hook_up \
29 \
30 hook_config_create hook_config_edit hook_config_remove hook_config_show \
31 \
32 hook_port hook_port_add hook_port_edit hook_port_remove hook_port_show \
33 hook_port_status hook_port_up hook_port_down \
34 \
35 hook_ppp_ip_pre_up hook_ppp_ipv4_down hook_ppp_ipv4_up \
36 hook_ipv6_down hook_ipv6_up hook_ppp_write_config"
37
38 function hook_dir() {
39 local type=${1}
40
41 if [ -n "${type}" ]; then
42 type="/${type}s"
43 fi
44
45 echo "${NETWORK_HOOKS_DIR}${type}"
46 }
47 NETWORK_HOOKS_DIR_ZONES="$(hook_dir zone)"
48
49 function hook_exists() {
50 local type=${1}
51 local hook=${2}
52
53 assert isset type
54 assert isset hook
55
56 # Add the path prefix.
57 hook="$(hook_dir ${type})/${hook}"
58
59 [ ! -d "${hook}" ] && [ -x "${hook}" ]
60 }
61
62 function hook_exec() {
63 local type="${1}"
64 assert isset type
65
66 local hook="${2}"
67 assert isset hook
68
69 local cmd="${3}"
70 assert isset cmd
71
72 assert hook_exists "${type}" "${hook}"
73 shift 3
74
75 # Complete the hook command by prepending "hook_"
76 local hook_cmd="hook_${cmd}"
77
78 # Check if the hook action is valid.
79 local valid_commands
80 case "${type}" in
81 "config")
82 valid_commands="${HOOK_COMMANDS_CONFIG}"
83 ;;
84 "port")
85 valid_commands="${HOOK_COMMANDS_PORT}"
86 ;;
87 "zone")
88 valid_commands="${HOOK_COMMANDS_ZONE}"
89 ;;
90 esac
91 isset valid_commands && assert list_match "${hook_cmd}" ${valid_commands}
92
93 local hook_path="$(hook_dir ${type})/${hook}"
94
95 # For performance reasons, all hooks are executed
96 # in a subshell and so will inherit the currently
97 # running environment.
98 (
99 # Set the name of the hook.
100 HOOK=$(basename ${hook})
101
102 # Source the code of the hook.
103 source "${hook_path}"
104
105 # Make sure HOOK is still properly set.
106 assert isset HOOK
107
108 # Execute the requested command.
109 cmd "${hook_cmd}" "$@"
110 )
111 local ret=$?
112
113 case "${ret}" in
114 ${EXIT_COMMAND_NOT_FOUND}|${EXIT_NOT_SUPPORTED})
115 log ERROR "Hook '${hook}' does not implement the method '${cmd}':"
116 log ERROR " arguments: $@"
117 exit ${EXIT_COMMAND_NOT_FOUND}
118 ;;
119 ${EXIT_ERROR_ASSERT})
120 log ERROR "Hook exited with an assertion error."
121 exit ${EXIT_ERROR_ASSERT}
122 ;;
123 esac
124
125 return ${ret}
126 }
127
128 function config_get_hook() {
129 local config=${1}
130
131 assert isset config
132 assert [ -e "${config}" ]
133
134 (
135 . ${config}
136 echo "${HOOK}"
137 )
138 }
139
140 function hook_zone_exists() {
141 hook_exists zone $@
142 }
143
144 function hook_zone_config_exists() {
145 local hook_zone=${1}
146 local hook_config=${2}
147
148 hook_exists zone "${hook_zone}.configs/${hook_config}"
149 }
150
151 function hook_zone_has_configs() {
152 local hook=${1}
153
154 [ -d "${NETWORK_HOOKS_DIR_ZONES}/${hook}.configs" ]
155 }
156
157 function hook_zone_exec() {
158 hook_exec zone $@
159 }
160
161 function hook_zone_config_exec() {
162 local hook_zone=${1}
163 local hook_port=${2}
164 shift 2
165
166 hook_zone_exec "${hook_zone}.configs/${hook_port}" $@
167 }
168
169 function hook_zone_get_all() {
170 local type=${1}
171
172 local hook
173 for hook in $(hook_dir zone)/*; do
174 hook=$(basename ${hook})
175 hook_zone_exists ${hook} && echo "${hook}"
176 done
177 }
178
179 function hook_zone_configs_get_all() {
180 local hook=${1}
181
182 if ! hook_exists zone ${hook}; then
183 error "Hook '${hook}' does not exist."
184 return ${EXIT_ERROR}
185 fi
186
187 # If the zone hook has got no configurations we exit silently
188 if ! hook_zone_has_configs ${hook}; then
189 return ${EXIT_OK}
190 fi
191
192 local h
193 for h in $(hook_dir zone)/${hook}.configs/*; do
194 h=$(basename ${h})
195 if hook_zone_config_exists ${hook} ${h}; then
196 echo "${h}"
197 fi
198 done
199
200 return ${EXIT_OK}
201 }