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