]> git.ipfire.org Git - people/stevee/network.git/blob - src/hooks/configs/dhcp
7f6780b5d34fc0b4b85e8e51b860c73db70d90a8
[people/stevee/network.git] / src / hooks / configs / dhcp
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 . /usr/lib/network/header-config
23
24 HOOK_CONFIG_SETTINGS="HOOK ENABLE_IPV6 ENABLE_IPV4"
25
26 # Default settings.
27 ENABLE_IPV6="on"
28 ENABLE_IPV4="on"
29
30 hook_check_config_settings() {
31 assert isset ENABLE_IPV6
32 assert isbool ENABLE_IPV6
33 assert isset ENABLE_IPV4
34 assert isbool ENABLE_IPV4
35 }
36
37 hook_parse_cmdline() {
38 while [ $# -gt 0 ]; do
39 case "${1}" in
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 ;;
55 esac
56 shift
57 done
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
64 }
65
66 hook_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
75 if ! hook_parse_cmdline $@; then
76 # Return an error if the parsing of the cmd line fails
77 return ${EXIT_ERROR}
78 fi
79
80 zone_config_settings_write "${zone}" "${HOOK}"
81
82 exit ${EXIT_OK}
83 }
84
85 hook_up() {
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
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
106
107 exit ${EXIT_OK}
108 }
109
110 hook_down() {
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
120 # Stop dhclient for IPv6 on this zone.
121 dhclient_stop ${zone} ipv6
122
123 # Stop dhclient for IPv4 on this zone.
124 dhclient_stop ${zone} ipv4
125
126 exit ${EXIT_OK}
127 }
128
129 hook_status() {
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
138
139 zone_config_settings_read "${zone}" "${config}"
140
141 local status
142 if dhclient_status ${zone} ipv4 || dhclient_status ${zone} ipv6; then
143 status="${MSG_HOOK_UP}"
144 else
145 status="${MSG_HOOK_DOWN}"
146 fi
147 cli_statusline 3 "${HOOK}" "${status}"
148
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
178 cli_space
179
180 done
181
182 exit ${EXIT_OK}
183 }