/[baselayout]/branches/bsd-porting/net-scripts/net.modules.d/pppd
Gentoo

Contents of /branches/bsd-porting/net-scripts/net.modules.d/pppd

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1537 - (show annotations) (download)
Wed Sep 14 11:30:45 2005 UTC (9 years ago) by uberlord
File size: 8773 byte(s)
    netplug module renamed to netplugd for consistency

    pppd module added, #53954 thanks to Alin Nastac

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

  ViewVC Help
Powered by ViewVC 1.1.20