#! /bin/bash
# c't/ODS-Kommunikationsserver V3.1+    
# (c) Klaus Fller & Redaktion c't 1996
# 
# Anpassung Shadow und MPPP von Reiner Klaproth, klaproth@online.de
# Anpassung an Dial-In von Klaus Werner, kwerner@ash.ks.he.schule.de
# dabei eingebaut: Kostenabrechnung nach Dr. A. Furbach, zene@afu.ping.de
# header angepasst, efl, 23aug01

# fr ISDN-sync-ppp Connects zum Provider

# Nutzung gem der GNU Public License  
# $Header: PPP-config V 3.1+, 16.09.2000

PATH=/usr/lib/ods-server/bin:$PATH	

if [ -z "$1" ]; then
	echo $0: Wird nicht gesetzt >&2
	exit 1
fi

scriptname=`basename $0`
cd /etc/Systemverwaltung

if [ "$1" = "-r" ]; then
	co -p $scriptname 2>/dev/null
	exit 0
fi

if [ ! -f $scriptname ]; then
	co $scriptname >/dev/null 2>&1
fi
if [ ! -f $scriptname ]; then
	touch $scriptname
	chmod 600 $scriptname
	ci -u $scriptname < /dev/null >/dev/null 2>&1
fi
chmod 600 $scriptname

if [ "$1" = "-d" ]; then
	cp -f $scriptname $scriptname.old
	egrep -v ^$2: $scriptname.old >$scriptname
	rm -f $scriptname.old
	# chap+pap-secrets bearbeiten (ps)
	cp -f /etc/ppp/pap-secrets /etc/ppp/pap-secrets.alt
	cp -f /etc/ppp/chap-secrets /etc/ppp/chap-secrets.alt
	egrep -v $2 /etc/ppp/pap-secrets.alt > /etc/ppp/pap-secrets
	egrep -v $2 /etc/ppp/chap-secrets.alt > /etc/ppp/chap-secrets
	rm -f /etc/ppp/chap-secrets.alt
	rm -f /etc/ppp/pap-secrets.alt
	exit 0
fi

if [ "$1" = "-dd" ]; then
	rm -f $scriptname
	exit 0
fi

if [ "$1" = "-rr" ]; then
	if [ -f $scriptname ]; then
		cat $scriptname
	fi
	exit 0
fi

if [ "$1" = "-rn" ]; then		# Nur die Namen ausgeben
	if [ -f $scriptname ]; then
		cut -d: -f1 < $scriptname 
	fi
	exit 0
fi

if [ "$1" != "-install" ]; then
	echo $1 >> $scriptname
	exit 0
fi

if [ ! -f $scriptname ]; then
	# Es wurde nichts vorgemerkt.  Also brauchen wir
	# nichts zu installieren.
	exit 0
fi

# BITTE, BITTE!!  Jetzt kein Absturz!

co -l -p $scriptname >/dev/null 2>&1
ci $scriptname </dev/null >/dev/null 2>&1

co -l -p $scriptname >/etc/ppp/SYS 2>/dev/null

#hier haben wir eine dumme querbeziehung.
PPP-chat -install

cd /etc/ppp

if [ ! -r SYS ]; then
	echo "Kann Datei /etc/ppp/SYS nicht lesen" >&2
	exit 1
fi

# 1     2    3      4      5     6           7      8      9
# $name:$tel:$modus:$login:$pass:$nameserver:$proxy:$route:$auth

# chap+pap-secrets neu aufbauen (ps, c't)
awk -F: '{
		if ( $9 == "1" )
			printf("\"%s\"\t*\t%s\n", $4,$5);
	 }' SYS > chap-secrets
awk -F: '{
		if ( $9 == "1" )
			printf("\"%s\"\t*\t%s\n", $4,$5);
	 }' SYS > pap-secrets

# Am Ende die Eintraege fr Dial-In mit auth login anfuegen
echo "*	Arktur-IPPP	\"\"" >>pap-secrets
echo "*	Arktur-PPP	\"\" *" >>pap-secrets
chmod 600 *-secrets

awk -F: 'BEGIN{
		print "#! /bin/bash\n\nif [ -z \"$1\" -o -z \"$2\" ]; then\n" \
			"\techo $0 DEVICE ATD MODEM SYS 1>&2 \n" \
			"\texit 1\nfi\n\n" \
			"DEVICE=$1\nATD=ATD$2\nMODEM=$3\nSYS=$4\n\n" \
			"case $SYS in"
	}
	{
		printf("%s)\n" \
			"\tPHONE=%s\n" \
			"\tUSER=%s\n" \
			"\tPASSWORD=%s\n" \
			"\tif chat -t 45 ABORT \"NO CARRIER\" ABORT BUSY ", \
			$1, $2, $4, $5);
		system ("cat CHAT_" $1);
		printf("\tthen\n\t\techo $SYS >/var/lock/$DEVICE\n" \
			"\t\texit 0\n\telse\n" \
			"\t\techo Verbindungsaufbau gescheitert. 1>&2\n" \
			"\t\texit 1\n\tfi ;;\n");
	}
	END{ print "esac\n\necho System unbekannt. 1>&2\nexit 1" }
	' SYS > ppp-chat

chmod 700 ppp-chat

rm -f CHAT_*

# ip-up-Generierung 

awk -F: 'BEGIN{
		print "#! /bin/bash\n\n" \
			"DEVICE=`basename $2`\ntest -z \"$DEVICE\" && DEVICE=$1\n" \
			"SYS=`cat /var/lock/$DEVICE`\n" \
			"IF=$1\nSPEED=$3\nMYIP=$4\nTHEIRIP=$5\n" \
			"rm -f /var/lock/watchdog.$DEVICE\n\n" \
                        "if ${IF%%ippp*}; then\n" \
			"\t/sbin/isdnctrl dialmode $IF manual\n" \
                        "\trm -f /var/lock/$SYS.standby\n" \
			"else\n" \
			"\techo \" \"`cat /var/run/$IF.pid` > /var/lock/LCK..$DEVICE\n" \
			"fi\n\n" \
			"case $SYS in"
	}
	{
		printf("%s)\n", $1);

		print \
			"\t/usr/bin/logger -i -p daemon.notice \"Suche Zeitserver...\"\n" \
			"\tTIMEBEFORE=\`/bin/date\`\n" \
			"\techo $DEVICE >/var/lock/$SYS\n" \
			"\techo $SYS >/var/lock/$DEVICE\n" \
                        "\tchmod 644 /var/lock/\$SYS /var/lock/\$DEVICE\n";
	
		if ($8 != "") {
			n=split($8, a, "[ \t]+")
			for (i=1; i<=n; ++i) {
				printf("\t/sbin/route add %s $IF\n", a[i]); }
		}
		print "\t/usr/sbin/ntpdate 130.149.17.8 &\n"
		 
		if ( $6 != "" )
			printf("\ttest -z \"$DNS1\" && DNS1=%s\n", $6);

		printf("\t/usr/lib/ods-server/bin/nameserver -root %s $DNS1\n", $1);
			
		if ( $7 != "" ) {
			gsub(";", ":", $7);
                        
			printf("\t/usr/lib/ods-server/bin/proxy-config '%s'\n", $7);
			}
                else
			print "\t/usr/lib/ods-server/bin/proxy-config \"\"\n";

		if ( $3 == "4" ) {
			print "\t/sbin/isdnctrl dial ippp11\n" } ;
			
		print \
			"\tTIMEAFTER=\`/bin/date\`\n" \
			"\t/sbin/clock -w\n" \
			"\t/usr/bin/logger -i -p daemon.notice \"Setze Systemzeit von \$TIMEBEFORE auf \$TIMEAFTER\"\n" \
                        "\techo \"Verbindung zu $SYS aufgebaut: \$TIMEAFTER\" >>/var/log/log.ppp\n" \
			"\techo \$SYS > /var/log/connect.time\n" \
                        "\techo \$TIMEAFTER >> /var/log/connect.time\n" \
			"\texit 0 ;;"
	}
	END{ print \
          	"*Dialin*)\n" \
          	"\tNUTZER=\$\{SYS\%\%\@Dialin\*\}\n" \
          	"\tif \[ \$NUTZER = \"auto\" \]; then\n" \
          	"\t\tNUTZER=\`w -sfuh \| grep \$DEVICE \| cut -d\" \" -f1\`\n" \
          	"\t\tNUTZER=\$\{NUTZER:=unknown\}\$\{SYS##auto\}\n" \
		"\telse\n\t\tNUTZER=\$SYS\n\tfi\n" \
		"\techo \"Dial-In von \$NUTZER: \`date\`\" >> /var/log/log.ppp.in\n" \
          	"\techo \$IF >/var/lock/\$SYS\n" \
          	"\tchmod 644 /var/lock/\$SYS\n" \
          	"\techo \$NUTZER >/var/lock/\$DEVICE\n" \
          	"\techo \$NUTZER >/var/lock/\$IF\n" \
          	"\tchmod 644 /var/lock/\$IF\n" \
                "\t/sbin/route add -host \$THEIRIP dev \$IF\n" \
		"\texit 0;;\n" \
          	"esac\n\necho System unbekannt. 1>&2\nexit 1" }
	' SYS > ip-up

chmod u+x ip-up

# Bearbeiten der provider-Datei fr das Online-Interface
awk -F: '{ $prov=$1
	print $prov , "\t", $prov }
	' SYS > /home/www/admin/providers

# Eintragen der Provider in die callerid.conf
cp -f /etc/isdn/callerid.conf /etc/isdn/callerid.conf.old
grep -v "Dieser Eintrag kommt von PPP" /etc/isdn/callerid.conf.old > /etc/isdn/callerid.conf

awk -F: '{ if ( $3 < 5) {print \
	"[NUMBER] \t\t# Dieser Eintrag kommt von PPP\n" \
	"NUMBER = ", $2, " \t# Dieser Eintrag kommt von PPP\n" \
	"ALIAS  = ", $1, " \t# Dieser Eintrag kommt von PPP\n" \
	"ZONE   = 1 \t\t# Dieser Eintrag kommt von PPP" 
	if ( $3 != 1 ) {print \
	"SI     = 7 \t\t# Dieser Eintrag kommt von PPP\n" \
	"INTERFACE = ippp1 \t# Dieser Eintrag kommt von PPP\n" }
	else {print \
	"SI     = 1 \t\t# Dieser Eintrag kommt von PPP\n" } }
	}' SYS >> /etc/isdn/callerid.conf 

rm -f SYS

exit 0
