]> git.ipfire.org Git - network.git/blob - functions.ppp
640b0cac830915271a713006e84ad528391750c6
[network.git] / functions.ppp
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 PPP_SUPPORTED_AUTH_METHODS="chap pap"
23
24 function pppd_start() {
25 local interface=${1}
26 assert isset interface
27
28 # This will block until the connection has been established or
29 # pppd exited.
30 service_start "pppd@${interface}"
31
32 # Get the exit code of the ppp daemon and figure out
33 # how to handle this.
34 local ret=$(service_get_exitcode "pppd@${interface}")
35 case "${ret}" in
36 0)
37 return ${EXIT_OK}
38 ;;
39 1)
40 error "pppd crashed for an unknown reason"
41 ;;
42 2)
43 error "pppd: Configuration error"
44 ;;
45 5)
46 error "pppd terminated"
47 ;;
48 19)
49 error "pppd: Authentication failed"
50 ;;
51 *)
52 error "pppd: Unhandled exit code: ${ret}"
53 ;;
54 esac
55
56 return ${ret}
57 }
58
59 function pppd_stop() {
60 local interface=${1}
61 assert isset interface
62
63 service_stop "pppd@${interface}"
64 }
65
66 function pppd_status() {
67 local interface=${1}
68 assert isset interface
69
70 service_status "pppd@${interface}"
71 }
72
73 function ppp_common_ip_pre_up() {
74 local zone=${1}
75 shift
76
77 if ! zone_exists ${zone}; then
78 error "Zone '${zone}' does not exist."
79 return ${EXIT_ERROR}
80 fi
81
82 routing_db_from_ppp ${zone} ipv4
83
84 # Request firewall reload
85 event_emit firewall-reload
86
87 return ${EXIT_OK}
88 }
89
90 function ppp_common_ip_up() {
91 local zone=${1}
92 shift
93
94 if ! zone_exists ${zone}; then
95 error "Zone '${zone}' does not exist."
96 return ${EXIT_ERROR}
97 fi
98
99 routing_db_set ${zone} ipv4 active 1
100 routing_update ${zone} ipv4
101 routing_default_update
102
103 # Emit interface-up event
104 event_interface_up ${zone}
105
106 return ${EXIT_OK}
107 }
108
109 function ppp_common_ip_down() {
110 local zone=${1}
111 shift
112
113 if ! zone_exists ${zone}; then
114 error "Zone '${zone}' does not exist."
115 return ${EXIT_ERROR}
116 fi
117
118 # Remove the information about this zone from the routing database
119 # and update the routing table.
120 routing_db_remove ${zone} ipv4
121 routing_update ${zone} ipv4
122 routing_default_update
123
124 # Save accounting information
125 ppp_accounting ${zone}
126
127 # Emit interface-up event
128 event_interface_down ${zone}
129
130 return ${EXIT_OK}
131 }
132
133 function ppp_common_ipv6_up() {
134 local zone=${1}
135 shift
136
137 if ! zone_exists ${zone}; then
138 error "Zone '${zone}' does not exist."
139 return ${EXIT_ERROR}
140 fi
141
142 # Add information about this zone to the routing database.
143 routing_db_from_ppp ${zone} ipv6
144
145 routing_db_set ${zone} ipv6 active 1
146 routing_update ${zone} ipv6
147 routing_default_update
148
149 # Emit interface-up event
150 event_interface_up ${zone}
151
152 return ${EXIT_OK}
153 }
154
155 function ppp_common_ipv6_down() {
156 local zone=${1}
157 shift
158
159 if ! zone_exists ${zone}; then
160 error "Zone '${zone}' does not exist."
161 return ${EXIT_ERROR}
162 fi
163
164 # Remove the information about this zone from the routing database
165 # and update the routing table.
166 routing_db_remove ${zone} ipv6
167 routing_update ${zone} ipv6
168 routing_default_update
169
170 # Save accounting information
171 ppp_accounting ${zone}
172
173 # Emit interface-up event
174 event_interface_down ${zone}
175
176 return ${EXIT_OK}
177 }
178
179 function ppp_secret() {
180 local USER=${1}
181 local SECRET=${2}
182 local a
183 local secret
184 local user
185
186 # Updateing secret file
187 > ${PPP_SECRETS}.tmp
188 while read user a secret; do
189 if [ "'${USER}'" != "${user}" ]; then
190 echo "${user} ${a} ${secret}" >> ${PPP_SECRETS}.tmp
191 fi
192 done < ${PPP_SECRETS}
193 echo "'${USER}' * '${SECRET}'" >> ${PPP_SECRETS}.tmp
194 cat ${PPP_SECRETS}.tmp > ${PPP_SECRETS}
195 rm -f ${PPP_SECRETS}.tmp
196 }
197
198 function ppp_accounting() {
199 local zone=${1}
200 shift
201
202 db_ppp_update ${zone} --duration="${CONNECT_TIME}" \
203 --rcvd="${BYTES_RCVD}" --sent="${BYTES_SENT}"
204 }
205
206 function pppd_exec() {
207 log DEBUG "Running pppd with parameters '$@'."
208
209 pppd $@ > /dev/null
210 }
211
212 function pppd_write_config() {
213 local file=${1}; shift
214 assert isset file
215
216 local auth
217 local default_asyncmap="true"
218 local interface
219 local lcp_echo_failure=3
220 local lcp_echo_interval=20
221 local linkname
222 local mtu mru
223 local plugin plugin_options
224 local user
225 local value
226
227 while [ $# -gt 0 ]; do
228 case "${1}" in
229 --auth=*)
230 auth=$(cli_get_val ${1})
231 ;;
232 # Enable or disable the use of the default asyncmap.
233 --default-asyncmap=*)
234 value=$(cli_get_val ${1})
235 if enabled value; then
236 default_asyncmap="true"
237 else
238 default_asyncmap="false"
239 fi
240 ;;
241 # The name of the created ppp interface.
242 --interface=*)
243 interface=$(cli_get_val ${1})
244 ;;
245 # LCP echo failure.
246 --lcr-echo-failure=*)
247 lcr_echo_failure=$(cli_get_val ${1})
248
249 if ! isinteger ${lcr_echo_failure}; then
250 error "--lcr-echo-failure= requires a number"
251 return ${EXIT_ERROR}
252 fi
253 ;;
254 # LCP echo interval.
255 --lcr-echo-interval=*)
256 lcr_echo_interval=$(cli_get_val ${1})
257
258 if ! isinteger ${lcr_echo_failure}; then
259 error "--lcr-echo-interval= requires a number"
260 return ${EXIT_ERROR}
261 fi
262 ;;
263 # Maximum Transmission Unit
264 --mtu=*)
265 mtu=$(cli_get_val ${1})
266 ;;
267 # Maximum Receive Unit
268 --mru=*)
269 mru=$(cli_get_val ${1})
270 ;;
271 --plugin=*)
272 plugin=$(cli_get_val ${1})
273 ;;
274 --plugin-options=*)
275 plugin_options=$(cli_get_val ${1})
276 ;;
277 --user=*)
278 user=$(cli_get_val ${1})
279 ;;
280 *)
281 log WARNING "Unhandled argument: ${1}"
282 ;;
283 esac
284 shift
285 done
286
287 if [ -z "${interface}" ]; then
288 log ERROR "You need to set the interface name: ${interface}"
289 return ${EXIT_ERROR}
290 fi
291 linkname="${interface}"
292
293 if isset auth; then
294 if ! isoneof ${auth} ${PPP_SUPPORTED_AUTH_METHODS}; then
295 log ERROR "Unsupported auth method: ${auth}"
296 return ${EXIT_ERROR}
297 fi
298 fi
299
300 # Write the configuration header.
301 mkdir -p $(dirname ${file}) 2>/dev/null
302 config_header "PPP daemon configuration file" > ${file}
303
304 # At first, set the name of the link.
305 print "linkname ${linkname}\n" >> ${file}
306
307 # Configure the interface name.
308 print "# Interface name\nifname ${interface}\n" >> ${file}
309
310 # Plugin settings
311 if isset plugin; then
312 (
313 print "# Plugin settings"
314 print "plugin ${plugin} ${plugin_options}"
315 print
316 ) >> ${file}
317 fi
318
319 # User authentication
320 if isset user; then
321 (
322 print "# User authentication"
323 print "user ${user}"
324
325 print "noauth"
326 if isset auth; then
327 print "require-${auth}"
328 fi
329 print
330 ) >> ${file}
331 fi
332
333 # MTU/MRU settings
334 if isset mtu; then
335 isset mru || mru=${mtu}
336
337 (
338 print "# MTU/MRU settings"
339 print "mtu ${mtu}"
340 print "mru ${mru}"
341 print
342 ) >> ${file}
343 fi
344
345 # Default asyncmap.
346 if enabled default_asyncmap; then
347 (
348 print "# Use the default asyncmap."
349 print "default-asyncmap"
350 print
351 ) >> ${file}
352 fi
353
354 # LCP settings.
355 (
356 print "# LCP settings"
357 print "lcp-echo-failure ${lcp_echo_failure}"
358 print "lcp-echo-interval ${lcp_echo_interval}"
359 print
360 ) >> ${file}
361
362 # Add the default settings.
363 (
364 print "# Disable the compression"
365 print "noccp noaccomp nodeflate nopcomp novj novjccomp nobsdcomp nomppe"
366
367 print "noipdefault updetach debug"
368 ) >> ${file}
369
370 return ${EXIT_OK}
371 }