/[baselayout]/trunk/net-scripts/net/pppd.sh
Gentoo

Contents of /trunk/net-scripts/net/pppd.sh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2050 - (show annotations) (download) (as text)
Fri Jun 2 20:02:40 2006 UTC (8 years, 1 month ago) by uberlord
File MIME type: text/x-sh
File size: 8766 byte(s)
    Allow LC_* and LANG vars through to runscript.sh by default.

    net.lo now comes after bootmisc, #135118 thanks to Oldrich Jedlicka.

    arping.sh now has MAC address support, #134253 thanks to Oldrich Jedlicka.

    pppd.sh now updates secret files with a blank password, #134337

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

  ViewVC Help
Powered by ViewVC 1.1.20