]>
Commit | Line | Data |
---|---|---|
5b20e43a MT |
1 | #!/bin/bash |
2 | ############################################################################### | |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
1848564d | 5 | # Copyright (C) 2010 Michael Tremer & Christian Schmidt # |
5b20e43a MT |
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 | ||
98f4dae6 | 22 | INFO_SETTINGS="HOOK PORT_PARENTS PORT_CHILDREN" |
1848564d | 23 | |
96088590 MT |
24 | HOOK_PORT_PATTERN="${PORT_PATTERN}" |
25 | ||
2eaf16f3 MT |
26 | # This function is called after a device has been plugged |
27 | # into the system and got its correct name. | |
28 | # The function is intended to create child ports and things | |
29 | # like that. | |
1c6a4e30 | 30 | hook_hotplug() { |
05365355 MT |
31 | # If the hook does not handle the hotplug event, it |
32 | # must return EXIT_NOT_HANDLED. | |
33 | exit ${EXIT_NOT_HANDLED} | |
2eaf16f3 MT |
34 | } |
35 | ||
36 | # This function gets called when a device is plugged in | |
37 | # to determine the right name. | |
38 | # The first argument is the port which should be tested | |
39 | # against the second argument which is the device that | |
40 | # has been plugged in. | |
1c6a4e30 | 41 | hook_hotplug_rename() { |
2eaf16f3 | 42 | exit ${EXIT_FALSE} |
8895cf8f MT |
43 | } |
44 | ||
12f9c8d2 MT |
45 | hook_hotplug_rename_by_address() { |
46 | local port="${1}" | |
47 | assert isset port | |
48 | ||
49 | local device="${2}" | |
50 | assert isset device | |
51 | ||
52 | # Read in the conifguration file. | |
53 | if ! port_settings_read "${port}"; then | |
54 | return ${EXIT_ERROR} | |
55 | fi | |
56 | ||
57 | # Get the current MAC address of the device. | |
58 | local address="$(device_get_address "${device}")" | |
59 | assert isset address | |
60 | ||
61 | # Check if the address matches with the configuration. | |
62 | if list_match "${address}" "${ADDRESS}" "${DEVICE}"; then | |
63 | log DEBUG "Device '${device}' is port '${port}'" | |
64 | return ${EXIT_OK} | |
65 | fi | |
66 | ||
67 | log DEBUG "Device '${device}' is not port '${port}'" | |
68 | return ${EXIT_ERROR} | |
69 | } | |
70 | ||
d673165c MT |
71 | # Returns the suggested name of the port |
72 | hook_find_port_name() { | |
73 | assert isset HOOK_PORT_PATTERN | |
74 | port_find_free "${HOOK_PORT_PATTERN}" | |
75 | } | |
76 | ||
54bae947 | 77 | hook_default_new() { |
d389e96b | 78 | local ${HOOK_SETTINGS[*]} |
4637109c MT |
79 | |
80 | # Import all default variables | |
81 | hook_set_defaults | |
82 | ||
54bae947 MT |
83 | if ! hook_parse_cmdline "$@"; then |
84 | return ${EXIT_ERROR} | |
85 | fi | |
86 | ||
d673165c MT |
87 | # Determine a name for this port |
88 | local port="$(hook_find_port_name)" | |
54bae947 MT |
89 | assert isset port |
90 | ||
d673165c MT |
91 | # Save settings |
92 | if ! port_settings_write "${port}" ${HOOK_SETTINGS[*]}; then | |
93 | return ${EXIT_ERROR} | |
94 | fi | |
54bae947 | 95 | |
d673165c | 96 | return ${EXIT_OK} |
54bae947 MT |
97 | } |
98 | ||
99 | hook_new() { | |
100 | hook_default_new "$@" | |
c2441156 SS |
101 | } |
102 | ||
a6fcc369 | 103 | hook_default_edit() { |
cd6d94b5 MT |
104 | local port=${1} |
105 | assert isset port | |
106 | shift | |
107 | ||
2e83362d | 108 | # Read settings |
d389e96b | 109 | if ! port_settings_read "${port}" ${HOOK_SETTINGS[*]}; then |
2e83362d MT |
110 | error "Could not read settings for port ${port}" |
111 | return ${EXIT_ERROR} | |
112 | fi | |
cd6d94b5 | 113 | |
2e83362d | 114 | # Parse command line arguments |
2212045f | 115 | if ! hook_parse_cmdline "$@"; then |
cd6d94b5 MT |
116 | return ${EXIT_ERROR} |
117 | fi | |
118 | ||
2e83362d | 119 | # Save settings |
d389e96b | 120 | if ! port_settings_write "${port}" ${HOOK_SETTINGS[*]}; then |
2e83362d MT |
121 | error "Could not write settings for port ${port}" |
122 | return ${EXIT_ERROR} | |
123 | fi | |
cd6d94b5 | 124 | |
79271f35 MT |
125 | # Apply settings |
126 | port_restart "${port}" | |
127 | ||
a6fcc369 MT |
128 | return ${EXIT_OK} |
129 | } | |
130 | ||
131 | hook_edit() { | |
2212045f | 132 | hook_default_edit "$@" |
cd6d94b5 MT |
133 | } |
134 | ||
7da8cf02 MT |
135 | # Returns a list of all children of this port |
136 | hook_children() { | |
137 | local port="${1}" | |
138 | ||
d389e96b | 139 | if ! port_settings_read "${port}" ${HOOK_SETTINGS[*]}; then |
7da8cf02 MT |
140 | log ERROR "Could not read port settings: ${port}" |
141 | return ${EXIT_OK} | |
142 | fi | |
143 | ||
144 | print "${SLAVES}" | |
145 | } | |
146 | ||
1c6a4e30 | 147 | hook_status() { |
2181765d | 148 | local port="${1}" |
fe8e6d69 MT |
149 | assert isset port |
150 | ||
2181765d | 151 | cli_device_headline "${port}" --long |
fe8e6d69 MT |
152 | exit ${EXIT_OK} |
153 | } | |
1ba6a2bb MT |
154 | |
155 | # Create any virtual devices, but don't bring them up | |
156 | # Must tolerate that the device may already exist | |
1c6a4e30 | 157 | hook_create() { |
1ba6a2bb MT |
158 | cmd_not_implemented |
159 | } | |
160 | ||
161 | # Must tolerate that the device may not exist | |
1c6a4e30 | 162 | hook_remove() { |
1ba6a2bb MT |
163 | cmd_not_implemented |
164 | } | |
165 | ||
166 | # Just bring up the device | |
1c6a4e30 | 167 | hook_default_up() { |
1ba6a2bb MT |
168 | local port="${1}" |
169 | assert isset port | |
170 | ||
171 | if ! device_exists "${port}"; then | |
172 | log ERROR "Port '${port}' does not exist and cannot be brought up" | |
173 | exit ${EXIT_ERROR} | |
174 | fi | |
175 | ||
176 | # Bring up the port | |
177 | device_set_up "${port}" | |
178 | ||
179 | # Bring up all slaves if the port has any | |
180 | local slave | |
181 | for slave in $(port_get_slaves "${port}"); do | |
182 | port_up "${slave}" | |
183 | done | |
184 | } | |
185 | ||
54bae947 | 186 | # Depends on the port existing |
1c6a4e30 | 187 | hook_up() { |
2212045f | 188 | hook_default_up "$@" |
1ba6a2bb MT |
189 | } |
190 | ||
1c6a4e30 | 191 | hook_default_down() { |
1ba6a2bb MT |
192 | local port="${1}" |
193 | assert isset port | |
194 | ||
195 | if device_exists "${port}"; then | |
196 | device_set_down "${port}" | |
197 | fi | |
198 | ||
199 | # Bring down all slaves if the port has any | |
200 | local slave | |
201 | for slave in $(port_get_slaves "${port}"); do | |
202 | port_down "${slave}" | |
203 | done | |
204 | } | |
205 | ||
1c6a4e30 | 206 | hook_down() { |
2212045f | 207 | hook_default_down "$@" |
1ba6a2bb | 208 | } |