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