198184Sgordon#!/bin/sh
298184Sgordon#
398184Sgordon# $FreeBSD: releng/11.0/etc/rc.d/ppp 298514 2016-04-23 16:10:54Z lme $
498184Sgordon#
598184Sgordon
6151806Syar# PROVIDE: ppp
7179315Sbz# REQUIRE: netif
8136224Smtm# KEYWORD: nojail
998184Sgordon
1098184Sgordon. /etc/rc.subr
1198184Sgordon
1298184Sgordonname="ppp"
13298514Slmedesc="Point to Point Protocol"
14230099Sdougbrcvar="ppp_enable"
15151807Syarcommand="/usr/sbin/${name}"
16172586Semaxstart_cmd="ppp_start"
17172761Semaxstop_cmd="ppp_stop"
18168119Smtmstart_postcmd="ppp_poststart"
1998184Sgordon
20172586Semaxppp_start_profile()
2198184Sgordon{
22177683Sbrooks	local _ppp_profile _ppp_mode _ppp_nat _ppp_unit
23178056Sbrooks	local _ppp_profile_cleaned _punct _punct_c
24172586Semax
25172586Semax	_ppp_profile=$1
26177649Sbrooks	_ppp_profile_cleaned=$1
27177649Sbrooks	_punct=". - / +"
28177649Sbrooks	for _punct_c in $_punct; do
29177649Sbrooks		_ppp_profile_cleaned=`ltr ${_ppp_profile_cleaned} ${_punct_c} '_'`
30177649Sbrooks	done
31172586Semax
32172586Semax	# Check for ppp profile mode override.
33172586Semax	#
34177649Sbrooks	eval _ppp_mode=\$ppp_${_ppp_profile_cleaned}_mode
35172586Semax	if [ -z "$_ppp_mode" ]; then
36172586Semax		_ppp_mode=$ppp_mode
37172586Semax	fi
38172586Semax
39172586Semax	# Check for ppp profile nat override.
40172586Semax	#
41177649Sbrooks	eval _ppp_nat=\$ppp_${_ppp_profile_cleaned}_nat
42172586Semax	if [ -z "$_ppp_nat" ]; then
43172586Semax		_ppp_nat=$ppp_nat
44172586Semax	fi
45172586Semax
4698184Sgordon	# Establish ppp mode.
4798184Sgordon	#
48172586Semax	if [ "${_ppp_mode}" != "ddial" -a "${_ppp_mode}" != "direct" \
49172586Semax		-a "${_ppp_mode}" != "dedicated" \
50172586Semax		-a "${_ppp_mode}" != "background" ]; then
51172586Semax		_ppp_mode="auto"
5298184Sgordon	fi
5398184Sgordon
54172586Semax	rc_flags="-quiet -${_ppp_mode}"
5598184Sgordon
5698184Sgordon	# Switch on NAT mode?
5798184Sgordon	#
58172586Semax	case ${_ppp_nat} in
5998184Sgordon	[Yy][Ee][Ss])
60138889Sbrian		rc_flags="$rc_flags -nat"
6198184Sgordon		;;
6298184Sgordon	esac
6398184Sgordon
64177683Sbrooks	# Check for hard wired unit
65177683Sbrooks	eval _ppp_unit=\$ppp_${_ppp_profile_cleaned}_unit
66177959Sbrooks	if [ -n "${_ppp_unit}" ]; then
67177683Sbrooks		_ppp_unit="-unit${_ppp_unit}"
68177683Sbrooks	fi
69177683Sbrooks	rc_flags="$rc_flags $_ppp_unit"
70177683Sbrooks
71172586Semax	# Run!
72172586Semax	#
73172586Semax	su -m $ppp_user -c "$command ${rc_flags} ${_ppp_profile}"
74138889Sbrian}
7598184Sgordon
76172586Semaxppp_start()
77172586Semax{
78172761Semax	local _ppp_profile _p
79172586Semax
80172761Semax	_ppp_profile=$*
81172761Semax	if [ -z "${_ppp_profile}" ]; then
82172761Semax		_ppp_profile=$ppp_profile
83172761Semax	fi
84172761Semax
85172586Semax	echo -n "Starting PPP profile:"
86172586Semax
87172761Semax	for _p in $_ppp_profile; do
88172586Semax		echo -n " $_p"
89172586Semax		ppp_start_profile $_p
90172586Semax	done
91172586Semax
92172586Semax	echo "."
93172586Semax}
94172586Semax
95168119Smtmppp_poststart()
96138889Sbrian{
97163696Savatar	# Re-Sync ipfilter and pf so they pick up any new network interfaces
9898184Sgordon	#
99197196Semaste	if [ -f /etc/rc.d/ipfilter ]; then
100197196Semaste		/etc/rc.d/ipfilter quietresync
101197196Semaste	fi
102197196Semaste	if [ -f /etc/rc.d/pf ]; then
103197196Semaste		/etc/rc.d/pf quietresync
104197196Semaste	fi
10598184Sgordon}
10698184Sgordon
107172761Semaxppp_stop_profile() {
108172761Semax	local _ppp_profile
109172761Semax
110172761Semax	_ppp_profile=$1
111172761Semax
112172761Semax	/bin/pkill -f "^${command}.*[[:space:]]${_ppp_profile}\$" || \
113172761Semax		echo -n "(not running)"
114172761Semax}
115172761Semax
116172761Semaxppp_stop() {
117172761Semax	local _ppp_profile _p
118172761Semax
119172761Semax	_ppp_profile=$*
120172761Semax	if [ -z "${_ppp_profile}" ]; then
121172761Semax		_ppp_profile=$ppp_profile
122172761Semax	fi
123172761Semax
124172761Semax	echo -n "Stopping PPP profile:"
125172761Semax
126172761Semax	for _p in $_ppp_profile; do
127172761Semax		echo -n " $_p"
128172761Semax		ppp_stop_profile $_p
129172761Semax	done
130172761Semax
131172761Semax	echo "."
132172761Semax}
133172761Semax
13498184Sgordonload_rc_config $name
135172761Semaxrun_rc_command $*
136