]> git.ipfire.org Git - people/ms/network.git/blame - src/hooks/configs/dhcp
hook: Rename HOOK_CONFIG_SETTINGS to HOOK_SETTINGS
[people/ms/network.git] / src / hooks / configs / dhcp
CommitLineData
e9ea243e
MT
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
f41fa3d7 22. /usr/lib/network/header-config
e9ea243e 23
636f1b96
MT
24HOOK_SETTINGS=(
25 "ENABLE_IPV6"
26 "ENABLE_IPV4"
27)
e9ea243e 28
636f1b96
MT
29DEFAULT_ENABLE_IPV6="on"
30DEFAULT_ENABLE_IPV4="on"
e9ea243e 31
b6d9bf2b 32hook_check_config_settings() {
9353a4e4
JS
33 assert isset ENABLE_IPV6
34 assert isbool ENABLE_IPV6
35 assert isset ENABLE_IPV4
36 assert isbool ENABLE_IPV4
e9ea243e
MT
37}
38
9353a4e4 39hook_parse_cmdline() {
320f1fda
JS
40 local id="${1}"
41 shift
42
e9ea243e
MT
43 while [ $# -gt 0 ]; do
44 case "${1}" in
9353a4e4
JS
45 --enable-ipv6)
46 ENABLE_IPV6="on"
47 ;;
48 --disable-ipv6)
49 ENABLE_IPV6="off"
50 ;;
51 --enable-ipv4)
52 ENABLE_IPV4="on"
53 ;;
54 --disable-ipv4)
55 ENABLE_IPV4="off"
56 ;;
57 *)
58 warning "Ignoring unknown option '${1}'"
59 ;;
e9ea243e
MT
60 esac
61 shift
62 done
e36f775d
JS
63
64 # Check if the user disabled ipv6 and ipv4
65 if ! enabled ENABLE_IPV6 && ! enabled ENABLE_IPV4; then
66 log ERROR "You disabled IPv6 and IPv4. At least one must be enabled"
67 return ${EXIT_ERROR}
68 fi
9353a4e4
JS
69}
70
71hook_new() {
72 local zone="${1}"
73 shift
74
75 if zone_config_hook_is_configured ${zone} "dhcp"; then
76 log ERROR "You can configure the dhcp hook only once for a zone"
77 return ${EXIT_ERROR}
78 fi
79
320f1fda
JS
80 local id=$(zone_config_get_new_id ${zone})
81 log DEBUG "ID for the config is: ${id}"
82
636f1b96
MT
83 # Import defaults
84 hook_set_defaults
85
320f1fda 86 if ! hook_parse_cmdline "${id}" "$@"; then
9353a4e4
JS
87 # Return an error if the parsing of the cmd line fails
88 return ${EXIT_ERROR}
89 fi
90
320f1fda 91 zone_config_settings_write "${zone}" "${HOOK}" "${id}"
e9ea243e
MT
92
93 exit ${EXIT_OK}
94}
95
1c6a4e30 96hook_up() {
e9ea243e
MT
97 local zone=${1}
98 local config=${2}
99 shift 2
100
101 if ! device_exists ${zone}; then
102 error "Zone '${zone}' doesn't exist."
103 exit ${EXIT_ERROR}
104 fi
105
9353a4e4
JS
106 zone_config_settings_read "${zone}" "${config}"
107
108 # Start dhclient for IPv6 on this zone if enabled.
109 if enabled ENABLE_IPV6; then
110 dhclient_start ${zone} ipv6
111 fi
112
113 # Start dhclient for IPv4 on this zone if enabled.
114 if enabled ENABLE_IPV4; then
115 dhclient_start ${zone} ipv4
116 fi
e9ea243e
MT
117
118 exit ${EXIT_OK}
119}
120
1c6a4e30 121hook_down() {
e9ea243e
MT
122 local zone=${1}
123 local config=${2}
124 shift 2
125
126 if ! device_exists ${zone}; then
127 error "Zone '${zone}' doesn't exist."
128 exit ${EXIT_ERROR}
129 fi
130
9353a4e4
JS
131 # Stop dhclient for IPv6 on this zone.
132 dhclient_stop ${zone} ipv6
133
e9ea243e
MT
134 # Stop dhclient for IPv4 on this zone.
135 dhclient_stop ${zone} ipv4
136
137 exit ${EXIT_OK}
138}
139
1c6a4e30 140hook_status() {
e9ea243e
MT
141 local zone=${1}
142 local config=${2}
143 shift 2
144
145 if ! device_exists ${zone}; then
146 error "Zone '${zone}' doesn't exist."
147 exit ${EXIT_ERROR}
148 fi
e9df08ad 149
b6d9bf2b 150 zone_config_settings_read "${zone}" "${config}"
e9ea243e 151
8e3508ac 152 local status
9353a4e4 153 if dhclient_status ${zone} ipv4 || dhclient_status ${zone} ipv6; then
8e3508ac 154 status="${MSG_HOOK_UP}"
e9ea243e 155 else
8e3508ac 156 status="${MSG_HOOK_DOWN}"
e9ea243e 157 fi
8e3508ac 158 cli_statusline 3 "${HOOK}" "${status}"
e9ea243e 159
9353a4e4
JS
160 cli_space
161
162 local proto
163 for proto in "IPv6" "IPv4"; do
164 local _proto=${proto,,}
165
166 cli_print_fmt1 3 "${proto}"
167
168 if enabled ENABLE_${proto^^}; then
169 cli_print_fmt1 4 "Status" "enabled"
170
171 local address="$(db_get "${zone}/${_proto}/local-ip-address")"
172 if isset address; then
173 cli_print_fmt1 4 "Address" "${address}"
174 fi
175
176 local gateway="$(db_get "${zone}/${_proto}/remote-ip-address")"
177 if isset gateway; then
178 cli_print_fmt1 4 "Gateway" "${gateway}"
179 fi
180
181 local dns_servers="$(db_get "${zone}/${_proto}/domain-name-servers")"
182 if isset dns_servers; then
183 cli_print_fmt1 4 "DNS Servers" "${dns_servers}"
184 fi
185 else
186 cli_print_fmt1 4 "Status" "disabled"
187 fi
188
ffea9f57 189 cli_space
9353a4e4
JS
190
191 done
e9ea243e
MT
192
193 exit ${EXIT_OK}
194}