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