/[vps]/baselayout-vserver/trunk/net-scripts/net.modules.d/pppd
Gentoo

Contents of /baselayout-vserver/trunk/net-scripts/net.modules.d/pppd

Parent Directory Parent Directory | Revision Log Revision Log


Revision 236 - (show annotations) (download)
Tue Feb 14 13:00:44 2006 UTC (8 years, 7 months ago) by phreak
File size: 8515 byte(s)
Merging r1881
1 #!/bin/bash
2 # Copyright (c) 2004-2005 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
4
5 # void pppd_depend(void)
6 #
7 # Sets up the dependancies for the module
8 pppd_depend() {
9 after interface
10 before dhcp
11 provide ppp
12 }
13
14 # bool pppd_check_installed(void)
15 #
16 # Returns 1 if pppd is installed, otherwise 0
17 pppd_check_installed() {
18 if [[ ! -x /usr/sbin/pppd ]] ; then
19 ${1:-false} && eerror "For PPP support, emerge net-dialup/ppp"
20 return 1
21 fi
22 return 0
23 }
24
25 # char *pppd_regex_escape(char *string)
26 #
27 # Returns the supplied string with any special regex
28 # characters escaped so they don't provide regex intructions
29 # This may be a candidate for adding to /sbin/functions.sh or
30 # net-scripts functions at some point
31 pppd_regex_escape() {
32 local escaped_result="$*"
33 escaped_result="${escaped_result//\\/\\\\}"
34 escaped_result="${escaped_result//./\\.}"
35 escaped_result="${escaped_result//+/\\+}"
36 escaped_result="${escaped_result//(/\\(}"
37 escaped_result="${escaped_result//)/\\)}"
38 escaped_result="${escaped_result//[/\\[}"
39 escaped_result="${escaped_result//]/\\]}"
40 escaped_result="${escaped_result//\{/\\\{}"
41 escaped_result="${escaped_result//\}/\\\}}"
42 escaped_result="${escaped_result//\?/\\\?}"
43 escaped_result="${escaped_result//\*/\\\*}"
44 escaped_result="${escaped_result//\//\\/}"
45 escaped_result="${escaped_result//|/\\|}"
46 escaped_result="${escaped_result//&/\\&}"
47 escaped_result="${escaped_result//~/\\~}"
48 escaped_result="${escaped_result//^/\\^}"
49 escaped_result="${escaped_result//$/\\$}"
50 echo "${escaped_result}"
51 }
52
53 # bool pppd_update_secrets_file(char* filepath, char* username, \
54 # char* remotename, char* password)
55 #
56 # Add/update PAP/CHAP authentication information
57 pppd_update_secrets_file() {
58 local filepath="$1" username="$2" remotename="$3" password="$4"
59 if [[ ! -s ${filepath} ]] ; then
60 echo '#'client$'\t'server$'\t'secret$'\t'IP addresses > "${filepath}" \
61 && chmod 0600 "${filepath}" \
62 || return 1
63 fi
64
65 #escape username and remotename, used in following sed calls
66 local regex_username=$(pppd_regex_escape ${username})
67 local regex_remotename=$(pppd_regex_escape ${remotename})
68 local regex_password
69 local regex_filter="[ \t]*\"?${regex_username}\"?[ \t]*\"?${regex_remotename}\"?[ \t]*"
70
71 #read old password, including " chars
72 #for being able to distinct when we need to add or update auth info
73 local old_password=$(
74 sed -r -e "/^${regex_filter}\".*\"[ \t]*\$/\
75 {s/^${regex_filter}(\".*\")[ \t]*\$/\1/;q;};\
76 d;" \
77 ${filepath}
78 )
79
80 if [[ -z "${old_password}" ]] ; then
81 regex_username="${username//\\/\\\\}"
82 regex_remotename="${remotename//\\/\\\\}"
83 regex_password="${password//\\/\\\\}"
84 regex_password=${password//"/\\\\"}
85 sed -r -i -e "\$a\"${regex_username}\" ${regex_remotename} \"${regex_password}\"" ${filepath}
86 vewarn "Authentication info has been added to ${filepath}"
87 elif [[ "\"${password//\"/\\\"}\"" != "${old_password}" ]] ; then
88 regex_password="${password//\\/\\\\}"
89 regex_password="${regex_password//\//\\/}"
90 regex_password="${regex_password//&/\\&}"
91 regex_password=${regex_password//\"/\\\\\"}
92 sed -r -i -e "s/^(${regex_filter}\").*(\"[ \t]*)\$/\1${regex_password}\2/" ${filepath}
93 vewarn "Authentication info has been updated in ${filepath}"
94 fi
95 return 0
96 }
97
98 # bool pppd_start(char *iface)
99 #
100 # Start PPP on an interface by calling pppd
101 #
102 # Returns 0 (true) when successful, otherwise 1
103 pppd_start() {
104 ${IN_BACKGROUND} && return 0
105
106 local iface="$1" ifvar=$( bash_variable "$1" ) opts="" link
107 if [[ ${iface%%[0-9]*} != "ppp" ]] ; then
108 eerror "PPP can only be invoked from net.ppp[0-9]"
109 return 1
110 fi
111
112 local unit="${iface#ppp}"
113 if [[ -z ${unit} ]] ; then
114 eerror "PPP requires a unit - use net.ppp[0-9] instead of net.ppp"
115 return 1
116 fi
117
118 # PPP requires a link to communicate over - normally a serial port
119 # PPPoE communicates over Ethernet
120 # PPPoA communicates over ATM
121 # In all cases, the link needs to be available before we start PPP
122 link="link_${ifvar}"
123 if [[ -z ${!link} ]] ; then
124 eerror "${link} has not been set in /etc/conf.d/net"
125 return 1
126 fi
127
128 # Might or might not be set in conf.d/net
129 local user password i
130 username="username_${ifvar}"
131 password="password_${ifvar}"
132
133 #Add/update info in PAP/CHAP secrets files
134 if [[ -n ${!username} && -n ${!password} ]] ; then
135 for i in chap pap ; do
136 if ! pppd_update_secrets_file "/etc/ppp/${i}-secrets" \
137 "${!username}" "${iface}" "${!password}" ; then
138 eerror "Failed to update /etc/ppp/${i}-secrets"
139 return 1
140 fi
141 done
142 fi
143
144 # Load any commandline options
145 opts="pppd_${ifvar}[@]"
146 opts="${!opts}"
147
148 # We don't work with these options set by the user
149 for i in unit nodetach linkname ; do
150 if [[ " ${opts} " == *" ${i} "* ]] ; then
151 eerror "The option \"${i}\" is not allowed in pppd_${ifvar}"
152 return 1
153 fi
154 done
155
156 # Check for mtu/mru
157 local mtu="mtu_${ifvar}"
158 if [[ -n ${!mtu} ]] ; then
159 [[ " ${opts} " != *" mtu "* ]] && opts="${opts} mtu ${!mtu}"
160 [[ " ${opts} " != *" mru "* ]] && opts="${opts} mru ${!mtu}"
161 fi
162
163 # Set linkname because we need /var/run/ppp-${linkname}.pid
164 # This pidfile has the advantage of being there, even if ${iface} interface was never started
165 opts="linkname ${iface} ${opts}"
166
167 # Setup auth info
168 [[ -n ${!username} ]] && opts="user '"${!username}"' ${opts}"
169 opts="remotename ${iface} ${opts}"
170
171 # Load a custom interface configuration file if it exists
172 [[ -f "/etc/ppp/options.${iface}" ]] \
173 && opts="${opts} file /etc/ppp/options.${iface}"
174
175 # Set unit
176 opts="unit ${unit} ${opts}"
177
178 # Default maxfail to 0 unless specified
179 [[ " ${opts} " != *" maxfail "* ]] && opts="${opts} maxfail 0"
180
181 # Append persist
182 [[ " ${opts} " != *" persist "* ]] && opts="${opts} persist"
183
184 # Setup connect script
185 local chat="chat_${ifvar}[@]"
186 if [[ -n "${!chat}" ]] ; then
187 local chatopts="/usr/sbin/chat -e -E -v"
188 local -a phone_number="phone_number_${ifvar}[@]"
189 phone_number=( "${!phone_number}" )
190 if [[ ${#phone_number[@]} -ge 1 ]] ; then
191 chatopts="${chatopts} -T '${phone_number[0]}'"
192 if [[ ${#phone_number[@]} -ge 2 ]] ; then
193 chatopts="${chatopts} -U '${phone_number[1]}'"
194 fi
195 fi
196 opts="${opts} connect $(requote "${chatopts} $(requote "${!chat}")")"
197 fi
198
199 # Add plugins
200 local plugins="plugins_${ifvar}[@]"
201 for i in "${!plugins}" ; do
202 local -a plugin=( ${i} )
203 # Bound to be some users who do this
204 [[ ${plugin[0]} == "pppoe" ]] && plugin[0]="rp-pppoe"
205 [[ ${plugin[0]} == "pppoa" ]] && plugin[0]="pppoatm"
206 [[ ${plugin[0]} == "capi" ]] && plugin[0]="capiplugin"
207
208 [[ ${plugin[0]} == "rp-pppoe" ]] && opts="${opts} connect true"
209 opts="${opts} plugin ${plugin[0]}.so ${plugin[@]:1}"
210 [[ ${plugin[0]} == "rp-pppoe" ]] && opts="${opts} ${!link}"
211 done
212
213 #Specialized stuff. Insert here actions particular to connection type (pppoe,pppoa,capi)
214 local insert_link_in_opts=1
215 if [[ " ${opts} " == *" plugin rp-pppoe.so "* ]] ; then
216 if [[ ! -e /proc/net/pppoe ]] ; then
217 # Load the PPPoE kernel module
218 if ! modprobe pppoe ; then
219 eerror "kernel does not support PPPoE"
220 return 1
221 fi
222 fi
223
224 # Ensure that the link exists and is up
225 interface_exists "${!link}" true || return 1
226 interface_up "${!link}"
227
228 insert_link_in_opts=0
229 fi
230
231 if [[ " ${opts} " == *" plugin pppoatm.so "* ]] ; then
232 if [[ ! -d /proc/net/atm ]] ; then
233 # Load the PPPoA kernel module
234 if ! modprobe pppoatm ; then
235 eerror "kernel does not support PPPoATM"
236 return 1
237 fi
238 fi
239 fi
240 [[ ${insert_link_in_opts} -eq 0 ]] || opts="${!link} ${opts}"
241
242 ebegin "Running pppd"
243 [[ " ${opts} " != *" updetach "* ]] && mark_service_inactive "net.${iface}"
244 eval start-stop-daemon --start --exec /usr/sbin/pppd \
245 --pidfile "/var/run/ppp-${iface}.pid" -- "${opts}" >/dev/null
246 if [[ $? != "0" ]] ; then
247 eend $? "Failed to start PPP"
248 mark_service_starting "net.${iface}"
249 return $?
250 fi
251
252 if [[ " ${opts} " == *" updetach "* ]] ; then
253 local addr=$( interface_get_address "${iface}" )
254 einfo "${iface} received address ${addr}"
255 else
256 einfo "Backgrounding ..."
257 exit 0
258 fi
259
260 return 0
261 }
262
263 # bool pppd_stop(char *iface)
264 #
265 # Stop PPP link by killing the associated pppd process
266 #
267 # Returns 0 (true) if no process to kill or it terminates successfully,
268 # otherwise non-zero (false)
269 pppd_stop() {
270 ${IN_BACKGROUND} && return 0
271 local iface="$1" pidfile="/var/run/ppp-$1.pid"
272
273 [[ ! -s ${pidfile} ]] && return 0
274
275 einfo "Stopping pppd on ${iface}"
276 start-stop-daemon --stop --exec /usr/sbin/pppd --pidfile "${pidfile}"
277 eend $?
278 }
279
280 # vim: ts=4

  ViewVC Help
Powered by ViewVC 1.1.20