summaryrefslogtreecommitdiffstats
path: root/usr.bin/chat/connect-ppp
blob: d37067d06bc7b2281cc514d69378de0270088a44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/sh
#
#	USAGE: connect-ppp <host>
#
#	Set up a PPP link to host. 
#
#	This script locks the tty so that faxd and uucp will not
#	interfere. If you are running with faxd as you "getty" then
#	faxd will remove the lock once it notices that pppd is gone.
#	This is the reason for pppd running in with the -detach flag, 
#	and you probably would run this script in the background.
#
#	I had to create the nodropdtr option to pppd in order to be
#	able to do what the script is doing here. Pathces has been
#	sent to the respective people, but I don't know if they like
#	them :-).
#
# 	Look for comments with <LOCAL> in the string. They identify 
#	things that you want to set for your system

#<LOCAL> define whatever your config file is.
CON_DB=/etc/ppp-connections

#<LOCAL> define whatever your device is.
DEVICE=cuaa0

#<LOCAL> define whatever your device speed is.
DEVICESPEED=57600

#<LOCAL> define whatever your lock directory is.
LOCKDIR=/var/spool/lock
LOCKFILE=$LOCKDIR/LCK..$DEVICE

#<LOCAL> define whatever debug level you want.
DEBUG="-d -d -d -d"

# Check that we got a name to connect to. This need not be an actuall hostname
# just the name you specified in the config file.
if [ $# -ne 1 ] ; then
	echo "Usage: $0 <host> &"
	exit 1
fi

# Get the configuration that is in effect for <name>
LINE=`grep "^$1" $CON_DB`
if [ -z "$LINE" ] ; then
	echo "Unknow host $1"
	exit 1
fi

# parse the CON_DB. The format is:
#
# <hostname>:<phone number>:<user id>:<password>:<local ip address>:\
# <remove_ip_address><netmask>:<pppd options>
#
# The last three are optional. But I would recomend specifying a netmask also
# when you specify a ip address.

IP_ADDR=""
IFS=':'
set $LINE
IFS=' '
HOST=$1
PHONE=$2
USER=$3
PASSWORD=$4
OUR_IP_ADDR=$5
THEIR_IP_ADDR=$6
NETMASK=$7
shift 7
OPTIONS=$*

if [ -f $LOCKFILE ] ; then
    echo "PPP device is locked"
    exit 1
else

    # Lock the device
    # faxd and UUCP wants 10 character lock id.
    echo "$$" | awk '{printf("%10s",$0)}' > $LOCKFILE
fi




#Do we know our local ip address? If so pppd needs a : at the end of it.
if [ ! -z "$OUR_IP_ADDR" ] ; then
	IP_ADDR=${OUR_IP_ADDR}:${THEIR_IP_ADDR}
fi

#Did we specify a netmask? If so convert to pppd format.
if [ ! -z "$NETMASK" ] ; then
	NETMASK="netmask ${NETMASK}"
fi

# Do the actual work in a subshell so that we can turn off tostop and set
# the tty speed before chat dials. The second reason for doing in like
# is that if you aren't running BIDIR, and you are running faxd, clocal
# doesn't get turned on from pppd so chat will never work if you exec 
# it from within pppd. I found that I needed to run uucp with the
# HAVE_CLOCAL_BUG flag set to 1 in order to get it to work in conjunction
# with faxd. Anyway, this setup seem to work.
(
   
    stty $DEVICESPEED -tostop hupcl 2> /dev/null

	# <LOCAL> Modify the Modem initialization strings to be whatever works for you
    if chat -v  ABORT "NO CARRIER" ABORT BUSY "" ATZ0E1 OK ATS50=255DT$PHONE \
		CONNECT "" ogin: $USER ssword: \\q$PASSWORD
    then
	# We got connected.
	/usr/libexec/pppd $DEBUG $OPTIONS -detach modem defaultroute \
		crtscts $NETMASK $DEVICE $DEVICESPEED $IP_ADDR 

    else
	echo "PPP call failed" 1>&2
	exit 1
    fi
) < /dev/$DEVICE > /dev/$DEVICE
# Get the return code from the subshell.
RC=$?

# Clear the lock. Slight window here where someone could detect that
# pppd is no longer running, remove its lock file and create its own.
# How to fix??
rm -f $LOCKFILE

#Pass on the exit code.
exit $RC
OpenPOWER on IntegriCloud