]>
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 | # Allow only one instance of this hook | |
23 | HOOK_UNIQUE="true" | |
24 | ||
25 | hook_new() { | |
26 | local zone="${1}" | |
27 | shift | |
28 | ||
29 | # Check if we are allowed to have multiple configurations of $HOOK | |
30 | if enabled HOOK_UNIQUE && zone_config_hook_is_configured "${zone}" "${HOOK}"; then | |
31 | error "You can only have one configuration of type ${HOOK}" | |
32 | return ${EXIT_CONF_ERROR} | |
33 | fi | |
34 | ||
35 | local id=$(zone_config_get_new_id ${zone}) | |
36 | log DEBUG "ID for the config is: ${id}" | |
37 | ||
38 | # Import all default variables | |
39 | hook_set_defaults | |
40 | ||
41 | # Parse command line arguments | |
42 | if ! hook_parse_cmdline "${id}" "$@"; then | |
43 | # Return an error if the parsing of the cmd line fails | |
44 | return ${EXIT_ERROR} | |
45 | fi | |
46 | ||
47 | # Write configuration to disk | |
48 | if ! zone_config_settings_write "${zone}" "${HOOK}" "${id}"; then | |
49 | return ${EXIT_ERROR} | |
50 | fi | |
51 | ||
52 | return ${EXIT_OK} | |
53 | } | |
54 | ||
55 | hook_edit() { | |
56 | local zone=${1} | |
57 | local config=${2} | |
58 | shift 2 | |
59 | ||
60 | local hook=$(config_get_hook_from_config ${config}) | |
61 | local id=$(config_get_id_from_config ${config}) | |
62 | ||
63 | assert isset hook | |
64 | assert isset id | |
65 | ||
66 | if ! zone_exists ${zone}; then | |
67 | error "Zone '${zone}' doesn't exist." | |
68 | exit ${EXIT_ERROR} | |
69 | fi | |
70 | ||
71 | # Bring the config down | |
72 | if device_exists ${zone}; then | |
73 | if ! hook_config_cmd "down" "${zone}" "${hook}" "${hook}.${id}"; then | |
74 | log ERROR "Could not bring config ${config} down for zone ${zone}" | |
75 | return ${EXIT_ERROR} | |
76 | fi | |
77 | fi | |
78 | ||
79 | local ${HOOK_SETTINGS} | |
80 | ||
81 | # If reading the config fails we cannot go on | |
82 | if ! zone_config_settings_read "${zone}" "${config}"; then | |
83 | log ERROR "Could read the config ${config} for zone ${zone}" | |
84 | return ${EXIT_ERROR} | |
85 | fi | |
86 | ||
87 | if ! hook_parse_cmdline "${id}" "$@"; then | |
88 | # Return an error if the parsing of the cmd line fails | |
89 | return ${EXIT_ERROR} | |
90 | fi | |
91 | ||
92 | # Write the settings to the config file | |
93 | if ! zone_config_settings_write "${zone}" "${hook}" ${id}; then | |
94 | log ERROR "Could not write config settings file ${config} for ${zone}" | |
95 | return ${EXIT_ERROR} | |
96 | fi | |
97 | ||
98 | # Bring the config up | |
99 | if device_exists ${zone}; then | |
100 | if ! hook_config_cmd "up" "${zone}" "${hook}" "${hook}.${id}"; then | |
101 | log ERROR "Could not bring config ${config} up for zone ${zone}" | |
102 | return ${EXIT_ERROR} | |
103 | fi | |
104 | fi | |
105 | ||
106 | exit ${EXIT_OK} | |
107 | } | |
108 | ||
109 | hook_destroy() { | |
110 | return ${EXIT_OK} | |
111 | } | |
112 | ||
113 | ||
114 | # Returns the ID as a unique identifier | |
115 | # Should always be overwritten by a hook | |
116 | hook_hid() { | |
117 | local zone=${1} | |
118 | local config=${2} | |
119 | ||
120 | config_get_id_from_config "${config}" | |
121 | } |